2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
package gudp
|
|
|
|
|
|
|
|
import (
|
2021-06-26 18:34:26 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
2019-06-19 09:06:52 +08:00
|
|
|
"net"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/container/gmap"
|
|
|
|
"github.com/gogf/gf/os/glog"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
2017-12-30 18:35:24 +08:00
|
|
|
const (
|
2021-08-08 13:10:21 +08:00
|
|
|
defaultServer = "default"
|
2017-12-30 18:35:24 +08:00
|
|
|
)
|
|
|
|
|
2019-10-30 19:55:50 +08:00
|
|
|
// Server is the UDP server.
|
2017-12-29 22:11:03 +08:00
|
|
|
type Server struct {
|
2019-10-01 17:53:49 +08:00
|
|
|
conn *Conn // UDP server connection object.
|
2019-10-30 19:55:50 +08:00
|
|
|
address string // UDP server listening address.
|
2019-10-01 17:53:49 +08:00
|
|
|
handler func(*Conn) // Handler for UDP connection.
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
var (
|
|
|
|
// serverMapping is used for instance name to its UDP server mappings.
|
|
|
|
serverMapping = gmap.NewStrAnyMap(true)
|
|
|
|
)
|
2017-12-30 17:09:00 +08:00
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// GetServer creates and returns a UDP server instance with given name.
|
2019-06-19 09:06:52 +08:00
|
|
|
func GetServer(name ...interface{}) *Server {
|
2021-08-08 13:10:21 +08:00
|
|
|
serverName := defaultServer
|
2019-07-28 17:37:13 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
2019-06-19 09:06:52 +08:00
|
|
|
serverName = gconv.String(name[0])
|
|
|
|
}
|
|
|
|
if s := serverMapping.Get(serverName); s != nil {
|
|
|
|
return s.(*Server)
|
|
|
|
}
|
|
|
|
s := NewServer("", nil)
|
|
|
|
serverMapping.Set(serverName, s)
|
|
|
|
return s
|
2017-12-30 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// NewServer creates and returns a UDP server.
|
|
|
|
// The optional parameter <name> is used to specify its name, which can be used for
|
|
|
|
// GetServer function to retrieve its instance.
|
|
|
|
func NewServer(address string, handler func(*Conn), name ...string) *Server {
|
2019-06-19 09:06:52 +08:00
|
|
|
s := &Server{
|
|
|
|
address: address,
|
|
|
|
handler: handler,
|
|
|
|
}
|
2019-12-13 13:43:28 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
2019-10-01 17:53:49 +08:00
|
|
|
serverMapping.Set(name[0], s)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
return s
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// SetAddress sets the server address for UDP server.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (s *Server) SetAddress(address string) {
|
|
|
|
s.address = address
|
2018-07-18 11:43:30 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// SetHandler sets the connection handler for UDP server.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (s *Server) SetHandler(handler func(*Conn)) {
|
|
|
|
s.handler = handler
|
2018-07-18 11:43:30 +08:00
|
|
|
}
|
|
|
|
|
2019-06-05 22:12:22 +08:00
|
|
|
// Close closes the connection.
|
|
|
|
// It will make server shutdowns immediately.
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:53:49 +08:00
|
|
|
// Run starts listening UDP connection.
|
2018-07-18 11:43:30 +08:00
|
|
|
func (s *Server) Run() error {
|
2019-06-19 09:06:52 +08:00
|
|
|
if s.handler == nil {
|
2021-07-20 23:02:02 +08:00
|
|
|
err := gerror.NewCode(gerror.CodeMissingConfiguration, "start running failed: socket handler not defined")
|
2019-06-19 09:06:52 +08:00
|
|
|
glog.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", s.address)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.conn = NewConnByNetConn(conn)
|
|
|
|
s.handler(s.conn)
|
|
|
|
return nil
|
2018-07-18 11:43:30 +08:00
|
|
|
}
|