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-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 (
|
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.
|
2021-10-21 18:22:47 +08:00
|
|
|
// 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,
|
|
|
|
}
|
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.
|
2021-12-22 22:22:42 +08:00
|
|
|
func (s *Server) Close() (err error) {
|
|
|
|
err = s.conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
err = gerror.Wrap(err, "connection failed")
|
|
|
|
}
|
|
|
|
return
|
2019-06-05 22:12:22 +08:00
|
|
|
}
|
|
|
|
|
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-08-24 21:18:59 +08:00
|
|
|
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 {
|
2021-12-22 22:22:42 +08:00
|
|
|
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 {
|
2021-12-22 22:22:42 +08:00
|
|
|
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
|
|
|
}
|