Repository navigation
Expand file tree
/
Copy pathgudp_server.go
More file actions
95 lines (83 loc) · 2.12 KB
/
Copy pathgudp_server.go
File metadata and controls
95 lines (83 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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.
package gudp
import (
"errors"
"net"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/util/gconv"
)
const (
gDEFAULT_SERVER = "default"
)
// tcp server结构体
type Server struct {
conn *Conn // UDP server connection object.
address string // Listening address.
handler func(*Conn)
}
// Server表,用以存储和检索名称与Server对象之间的关联关系
var serverMapping = gmap.NewStrAnyMap(true)
// 获取/创建一个空配置的UDP Server
// 单例模式,请保证name的唯一性
func GetServer(name ...interface{}) *Server {
serverName := gDEFAULT_SERVER
if len(name) > 0 && name[0] != "" {
serverName = gconv.String(name[0])
}
if s := serverMapping.Get(serverName); s != nil {
return s.(*Server)
}
s := NewServer("", nil)
serverMapping.Set(serverName, s)
return s
}
// 创建一个tcp server对象,并且可以选择指定一个单例名字
func NewServer(address string, handler func(*Conn), names ...string) *Server {
s := &Server{
address: address,
handler: handler,
}
if len(names) > 0 {
serverMapping.Set(names[0], s)
}
return s
}
// 设置参数 - address
func (s *Server) SetAddress(address string) {
s.address = address
}
// 设置参数 - handler
func (s *Server) SetHandler(handler func(*Conn)) {
s.handler = handler
}
// Close closes the connection.
// It will make server shutdowns immediately.
func (s *Server) Close() error {
return s.conn.Close()
}
// 执行监听
func (s *Server) Run() error {
if s.handler == nil {
err := errors.New("start running failed: socket handler not defined")
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
}