-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathserver.go
220 lines (203 loc) · 5.22 KB
/
server.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package gop2p
import (
"encoding/json"
"fmt"
"log"
"net"
"time"
)
type Server interface {
Close()
}
type server struct {
c *net.UDPConn
exit bool
clients map[string]net.Addr
sendQueue chan *Package
}
func NewServer(addr string) (Server, error) {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
c, err := net.ListenUDP("udp", udpAddr)
if err != nil {
return nil, err
}
s := &server{
c: c,
exit: false,
clients: make(map[string]net.Addr),
sendQueue: make(chan *Package, 1000),
}
go s.listen()
go s.sendLoop()
return s, nil
}
func (s *server) Close() {
s.exit = true
s.c.Close()
}
func (s *server) findIDByAddr(addrstring string) string {
for id, addr := range s.clients {
if addr.String() == addrstring {
return id
}
}
return ""
}
func (s *server) send(pack *Package) {
s.sendQueue <- pack
}
func (s *server) response(msgId uint16, seqId uint32, data []byte, addr net.Addr) {
resp := Package{
SeqID: seqId,
MsgID: msgId,
IsResponse: true,
Data: data,
Addr: addr,
}
s.send(&resp)
}
func (s *server) process(pack *Package) {
if pack.IsResponse {
switch pack.MsgID {
case MSGID_TO_CONNECT:
var commonResp CommonResponse
err := json.Unmarshal(pack.Data, &commonResp)
if err != nil {
log.Println("MSGID_TO_CONNECT - Parse error:", err)
break
}
if commonResp.Ecode != ERROR_CODE_SUCCESS {
log.Printf("MSGID_TO_CONNECT - response: %d\r\n", commonResp.Ecode)
break
}
log.Printf("MSGID_TO_CONNECT - OK (addr: %s)\r\n", pack.Addr.String())
}
} else {
switch pack.MsgID {
case MSGID_REG:
var req RegRequest
err := json.Unmarshal(pack.Data, &req)
if err != nil {
log.Println("MSGID_REG - Parse error:", err)
s.response(MSGID_REG, pack.SeqID, COMMON_RESP_FAILED, pack.Addr)
break
}
log.Printf("Client register: %+v\r\n", req)
s.clients[req.ID] = pack.Addr
s.response(MSGID_REG, pack.SeqID, COMMON_RESP_SUCCESS, pack.Addr)
case MSGID_LIST:
list := `{"ecode":200,"clients":{`
first := true
for cid, client_addr := range s.clients {
if first {
first = false
} else {
list = list + ","
}
list = list + fmt.Sprintf(`"%s":"%s"`, cid, client_addr.String())
}
list = list + "}}"
s.response(MSGID_LIST, pack.SeqID, []byte(list), pack.Addr)
case MSGID_CONNECT:
var req ConnectRequest
err := json.Unmarshal(pack.Data, &req)
if err != nil {
log.Println("MSGID_CONNECT - Parse error:", err)
s.response(MSGID_CONNECT, pack.SeqID, COMMON_RESP_FAILED, pack.Addr)
break
}
log.Printf("Connect: %+v\r\n", req)
client, found := s.clients[req.PeerID]
if !found {
log.Println("MSGID_CONNECT - not found client:", req.PeerID)
s.response(MSGID_CONNECT, pack.SeqID, COMMON_RESP_NOT_FOUND, pack.Addr)
break
}
promoterID := s.findIDByAddr(pack.Addr.String())
if len(promoterID) == 0 {
log.Println("MSGID_CONNECT - not found promoter ID:", promoterID)
s.response(MSGID_CONNECT, pack.SeqID, COMMON_RESP_NOT_FOUND, pack.Addr)
break
}
log.Printf("promoterID: %s, pack.Addr.String(): %s\r\n", promoterID, pack.Addr.String())
s.response(MSGID_CONNECT, pack.SeqID, []byte(fmt.Sprintf(`{"ecode":200,"peerid":"%s","addr":"%s"}`, req.PeerID, client.String())), pack.Addr)
// To connect
toConnectPack := Package{
MsgID: MSGID_TO_CONNECT,
SeqID: NextSeqID(),
IsResponse: false,
Addr: client,
Data: []byte(fmt.Sprintf(`{"id":"%s","address":"%s"}`, promoterID, pack.Addr.String())),
}
s.send(&toConnectPack)
case MSGID_KEEPALIVE:
s.response(MSGID_KEEPALIVE, pack.SeqID, COMMON_RESP_SUCCESS, pack.Addr)
case MSGID_UNREG:
var req UnregRequest
err := json.Unmarshal(pack.Data, &req)
if err != nil {
log.Println("MSGID_UNREG - Parse error:", err)
s.response(MSGID_UNREG, pack.SeqID, COMMON_RESP_FAILED, pack.Addr)
break
}
log.Printf("Client unregister: %+v\r\n", req)
client, found := s.clients[req.ID]
if found {
if client.String() == pack.Addr.String() {
delete(s.clients, req.ID)
}
}
s.response(MSGID_UNREG, pack.SeqID, COMMON_RESP_SUCCESS, pack.Addr)
}
}
}
func (s *server) listen() {
log.Println("listening ...")
buf := make([]byte, 65507)
for !s.exit {
s.c.SetReadDeadline(time.Now().Add(5 * time.Second))
_, addr, err := s.c.ReadFromUDP(buf)
if err != nil {
log.Println("Read error:", err)
opErr, ok := err.(*net.OpError)
if ok && opErr.Timeout() {
continue
} else {
log.Println("Read error:", err, ", Addr:", addr)
continue
}
}
pack, err := ParsePackage(buf, addr)
if err != nil {
log.Printf("ParsePackage(% 0x) error: %s\r\n", buf, err)
continue
}
go s.process(pack)
}
}
func (s *server) sendLoop() {
log.Println("sending ...")
for !s.exit {
select {
case pack := <-s.sendQueue:
s.c.SetWriteDeadline(time.Now().Add(5 * time.Second))
_, err := pack.Send(s.c)
if err != nil {
opErr, ok := err.(*net.OpError)
if ok && opErr.Timeout() {
continue
} else {
// Todo: reconnect
log.Println("Send error:", err)
break
}
}
case <-time.After(5 * time.Second):
// Check to exit
log.Println("sending ...")
}
}
}