gf/net/gudp/gudp_server.go

103 lines
2.6 KiB
Go
Raw Normal View History

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,
// 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-11-13 23:23:55 +08:00
"net"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/util/gconv"
2017-11-23 10:21:28 +08:00
)
2017-12-30 18:35:24 +08:00
const (
defaultServer = "default"
2017-12-30 18:35:24 +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.
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 {
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
2019-10-01 17:53:49 +08:00
// 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,
}
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
}
// Close closes the connection.
// It will make server shutdowns immediately.
func (s *Server) Close() (err error) {
err = s.conn.Close()
if err != nil {
err = gerror.Wrap(err, "connection failed")
}
return
}
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 {
err := gerror.NewCode(gcode.CodeMissingConfiguration, "start running failed: socket handler not defined")
2019-06-19 09:06:52 +08:00
return err
}
addr, err := net.ResolveUDPAddr("udp", s.address)
if err != nil {
err = gerror.Wrapf(err, `net.ResolveUDPAddr failed for address "%s"`, s.address)
2019-06-19 09:06:52 +08:00
return err
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
err = gerror.Wrapf(err, `net.ListenUDP failed for address "%s"`, s.address)
2019-06-19 09:06:52 +08:00
return err
}
s.conn = NewConnByNetConn(conn)
s.handler(s.conn)
return nil
2018-07-18 11:43:30 +08:00
}