-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_unicast.go
214 lines (194 loc) · 6.47 KB
/
handle_unicast.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
package p2p
import (
"context"
"net"
"p2p-snake/internal/log"
"p2p-snake/internal/p2p/protocol"
)
const (
gameNameNotMatchError = "game name does not match"
receiverIsNotInGameError = "receiver is not in any game"
receiverIsMasterError = "receiver is master for this game"
receiverIsNotMasterError = "receiver is not master for this game"
senderIsNotInGameError = "sender is not in this game"
senderIsViewerError = "sender is viewer"
duplicatePlayerNameError = "player with such name already exists"
duplicatePlayerAddrError = "player with such address already exists"
)
func (p *Peer) listenUnicast(ctx context.Context) {
defer p.wg.Done()
log.Logger.Debug("listenUnicast goroutine is running")
for {
select {
case <-ctx.Done():
log.Logger.Debug("listenUnicast goroutine has completed")
return
default:
gameMsg := &protocol.GameMessage{}
addr, ok := p.receiveUnicastProto(gameMsg)
if ok {
go p.handleUnicastMsg(gameMsg, addr)
}
}
}
}
func (p *Peer) handleUnicastMsg(gameMsg *protocol.GameMessage, addr *net.UDPAddr) {
switch gameMsg.GetType().(type) {
case *protocol.GameMessage_Ack:
p.handleAckMsg(gameMsg, addr)
case *protocol.GameMessage_Error:
p.handleErrorMsg(gameMsg, addr)
case *protocol.GameMessage_Join:
p.handleJoinMsg(gameMsg, addr)
case *protocol.GameMessage_Ping:
p.handlePingMsg(gameMsg, addr)
case *protocol.GameMessage_RoleChange:
p.handleRoleChangeMsg(gameMsg, addr)
case *protocol.GameMessage_State:
p.handleStateMsg(gameMsg, addr)
case *protocol.GameMessage_Steer:
p.handleSteerMsg(gameMsg, addr)
}
}
func (p *Peer) handleAckMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo != nil && p.gameInfo.CurrentNode().PlayerId() != msg.GetReceiverId() {
return
}
if response, ok := p.notAckMsg[msg.GetMsgSeq()]; ok {
response <- msg
}
if p.gameInfo != nil {
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok {
node.UpdateTimeAsNow()
}
}
}
func (p *Peer) handleErrorMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo != nil && p.gameInfo.CurrentNode().PlayerId() != msg.GetReceiverId() {
return
}
if response, ok := p.notAckMsg[msg.GetMsgSeq()]; ok {
response <- msg
}
if p.gameInfo != nil {
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok {
node.UpdateTimeAsNow()
}
}
}
func (p *Peer) handleJoinMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo == nil {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsNotInGameError, addr)
return
}
if !p.gameInfo.CurrentNode().IsMasterNode() {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsNotMasterError, addr)
return
}
if msg.GetJoin().GetGameName() != p.gameInfo.GameName() {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), gameNameNotMatchError, addr)
return
}
if p.gameInfo.ExistsPlayerByName(msg.GetJoin().GetPlayerName()) {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), duplicatePlayerNameError, addr)
return
}
if p.gameInfo.ExistsPlayerByAddr(addr) {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), duplicatePlayerAddrError, addr)
return
}
node, err := p.gameInfo.AddPlayer(msg.GetJoin().GetPlayerName(), msg.GetJoin().GetRequestedRole(), addr)
if err != nil {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), err.Error(), addr)
return
}
p.sendAckMsg(
msg.GetMsgSeq(),
p.gameInfo.CurrentNode().PlayerId(),
node.PlayerId(),
addr,
)
}
func (p *Peer) handlePingMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo == nil {
return
}
if p.gameInfo.CurrentNode().PlayerId() != msg.GetReceiverId() {
return
}
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok {
node.UpdateTimeAsNow()
}
}
func (p *Peer) handleRoleChangeMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo == nil {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsNotInGameError, addr)
return
}
if p.gameInfo.CurrentNode().PlayerId() != msg.GetReceiverId() {
return
}
if msg.GetRoleChange().SenderRole != nil {
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok && msg.GetRoleChange().GetSenderRole() != protocol.NodeRole_MASTER {
node.SetRole(msg.GetRoleChange().GetSenderRole())
}
}
if msg.GetRoleChange().ReceiverRole != nil {
p.gameInfo.CurrentNode().SetRole(msg.GetRoleChange().GetSenderRole())
}
p.sendAckMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), addr)
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok {
node.UpdateTimeAsNow()
}
}
func (p *Peer) handleStateMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo == nil {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsNotInGameError, addr)
return
}
if p.gameInfo.CurrentNode().PlayerId() != msg.GetReceiverId() {
return
}
if p.gameInfo.CurrentNode().IsMasterNode() {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsMasterError, addr)
return
}
if p.gameInfo.StateOrder() >= msg.GetState().GetState().GetStateOrder() {
return
}
p.sendAckMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), addr)
p.gameInfo.SetState(msg.GetReceiverId(), msg.GetState().GetState(), addr)
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok {
node.UpdateTimeAsNow()
}
}
func (p *Peer) handleSteerMsg(msg *protocol.GameMessage, addr *net.UDPAddr) {
if p.gameInfo == nil {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsNotInGameError, addr)
return
}
if p.gameInfo.CurrentNode().PlayerId() != msg.GetReceiverId() {
return
}
if !p.gameInfo.CurrentNode().IsMasterNode() {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), receiverIsNotMasterError, addr)
return
}
node, ok := p.gameInfo.Node(msg.GetSenderId())
if !ok {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), senderIsNotInGameError, addr)
return
}
if node.Role() == protocol.NodeRole_VIEWER {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), senderIsViewerError, addr)
return
}
if err := p.gameInfo.AddMove(msg.GetSenderId(), msg.GetSteer().GetDirection()); err != nil {
p.sendErrorMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), err.Error(), addr)
return
}
p.sendAckMsg(msg.GetMsgSeq(), msg.GetReceiverId(), msg.GetSenderId(), addr)
if node, ok := p.gameInfo.Node(msg.GetSenderId()); ok {
node.UpdateTimeAsNow()
}
}