@@ -21,14 +21,22 @@ var (
21
21
errUnexpectedAdditionalNIC = errors .New ("unexpected additional NEW_INCOMING_CONNECTION packet" )
22
22
)
23
23
24
- func (h * listenerConnectionHandler ) handleUnconnected (b []byte , addr net.Addr ) error {
24
+ func (h listenerConnectionHandler ) limitsEnabled () bool {
25
+ return true
26
+ }
27
+
28
+ func (h listenerConnectionHandler ) close (conn * Conn ) {
29
+ h .l .connections .Delete (resolve (conn .raddr ))
30
+ }
31
+
32
+ func (h listenerConnectionHandler ) handleUnconnected (b []byte , addr net.Addr ) error {
25
33
switch b [0 ] {
26
34
case message .IDUnconnectedPing , message .IDUnconnectedPingOpenConnections :
27
- return handleUnconnectedPing ( h . l , b [1 :], addr )
35
+ return h . handleUnconnectedPing ( b [1 :], addr )
28
36
case message .IDOpenConnectionRequest1 :
29
- return handleOpenConnectionRequest1 ( h . l , b [1 :], addr )
37
+ return h . handleOpenConnectionRequest1 ( b [1 :], addr )
30
38
case message .IDOpenConnectionRequest2 :
31
- return handleOpenConnectionRequest2 ( h . l , b [1 :], addr )
39
+ return h . handleOpenConnectionRequest2 ( b [1 :], addr )
32
40
}
33
41
if b [0 ]& bitFlagDatagram != 0 {
34
42
// In some cases, the client will keep trying to send datagrams
@@ -39,14 +47,14 @@ func (h *listenerConnectionHandler) handleUnconnected(b []byte, addr net.Addr) e
39
47
return fmt .Errorf ("unknown packet received (len=%v): %x" , len (b ), b )
40
48
}
41
49
42
- func (h * listenerConnectionHandler ) handle (conn * Conn , b []byte ) (handled bool , err error ) {
50
+ func (h listenerConnectionHandler ) handle (conn * Conn , b []byte ) (handled bool , err error ) {
43
51
switch b [0 ] {
44
52
case message .IDConnectionRequest :
45
- return true , handleConnectionRequest (conn , b [1 :])
53
+ return true , h . handleConnectionRequest (conn , b [1 :])
46
54
case message .IDConnectionRequestAccepted :
47
55
return true , errUnexpectedCRA
48
56
case message .IDNewIncomingConnection :
49
- return true , handleNewIncomingConnection (conn )
57
+ return true , h . handleNewIncomingConnection (conn )
50
58
case message .IDConnectedPing :
51
59
return true , handleConnectedPing (conn , b [1 :])
52
60
case message .IDConnectedPong :
@@ -62,62 +70,54 @@ func (h *listenerConnectionHandler) handle(conn *Conn, b []byte) (handled bool,
62
70
}
63
71
}
64
72
65
- func (h * listenerConnectionHandler ) limitsEnabled () bool {
66
- return true
67
- }
68
-
69
- func (h * listenerConnectionHandler ) close (conn * Conn ) {
70
- h .l .connections .Delete (resolve (conn .raddr ))
71
- }
72
-
73
73
// handleUnconnectedPing handles an unconnected ping packet stored in buffer b,
74
74
// coming from an address.
75
- func handleUnconnectedPing ( listener * Listener , b []byte , addr net.Addr ) error {
75
+ func ( h listenerConnectionHandler ) handleUnconnectedPing ( b []byte , addr net.Addr ) error {
76
76
pk := & message.UnconnectedPing {}
77
77
if err := pk .UnmarshalBinary (b ); err != nil {
78
78
return fmt .Errorf ("read UNCONNECTED_PING: %w" , err )
79
79
}
80
- data , _ := (& message.UnconnectedPong {ServerGUID : listener . id , SendTimestamp : pk .SendTimestamp , Data : * listener .pongData .Load ()}).MarshalBinary ()
81
- _ , err := listener .conn .WriteTo (data , addr )
80
+ data , _ := (& message.UnconnectedPong {ServerGUID : h . l . id , SendTimestamp : pk .SendTimestamp , Data : * h . l .pongData .Load ()}).MarshalBinary ()
81
+ _ , err := h . l .conn .WriteTo (data , addr )
82
82
return err
83
83
}
84
84
85
85
// handleOpenConnectionRequest1 handles an open connection request 1 packet
86
86
// stored in buffer b, coming from an address.
87
- func handleOpenConnectionRequest1 ( listener * Listener , b []byte , addr net.Addr ) error {
87
+ func ( h listenerConnectionHandler ) handleOpenConnectionRequest1 ( b []byte , addr net.Addr ) error {
88
88
pk := & message.OpenConnectionRequest1 {}
89
89
if err := pk .UnmarshalBinary (b ); err != nil {
90
90
return fmt .Errorf ("read OPEN_CONNECTION_REQUEST_1: %w" , err )
91
91
}
92
92
mtuSize := min (pk .MaximumSizeNotDropped , maxMTUSize )
93
93
94
94
if pk .Protocol != protocolVersion {
95
- data , _ := (& message.IncompatibleProtocolVersion {ServerGUID : listener .id , ServerProtocol : protocolVersion }).MarshalBinary ()
96
- _ , _ = listener .conn .WriteTo (data , addr )
95
+ data , _ := (& message.IncompatibleProtocolVersion {ServerGUID : h . l .id , ServerProtocol : protocolVersion }).MarshalBinary ()
96
+ _ , _ = h . l .conn .WriteTo (data , addr )
97
97
return fmt .Errorf ("handle OPEN_CONNECTION_REQUEST_1: incompatible protocol version %v (listener protocol = %v)" , pk .Protocol , protocolVersion )
98
98
}
99
99
100
- data , _ := (& message.OpenConnectionReply1 {ServerGUID : listener .id , Secure : false , ServerPreferredMTUSize : mtuSize }).MarshalBinary ()
101
- _ , err := listener .conn .WriteTo (data , addr )
100
+ data , _ := (& message.OpenConnectionReply1 {ServerGUID : h . l .id , Secure : false , ServerPreferredMTUSize : mtuSize }).MarshalBinary ()
101
+ _ , err := h . l .conn .WriteTo (data , addr )
102
102
return err
103
103
}
104
104
105
105
// handleOpenConnectionRequest2 handles an open connection request 2 packet
106
106
// stored in buffer b, coming from an address.
107
- func handleOpenConnectionRequest2 ( listener * Listener , b []byte , addr net.Addr ) error {
107
+ func ( h listenerConnectionHandler ) handleOpenConnectionRequest2 ( b []byte , addr net.Addr ) error {
108
108
pk := & message.OpenConnectionRequest2 {}
109
109
if err := pk .UnmarshalBinary (b ); err != nil {
110
110
return fmt .Errorf ("read OPEN_CONNECTION_REQUEST_2: %w" , err )
111
111
}
112
112
mtuSize := min (pk .ClientPreferredMTUSize , maxMTUSize )
113
113
114
- data , _ := (& message.OpenConnectionReply2 {ServerGUID : listener .id , ClientAddress : resolve (addr ), MTUSize : mtuSize }).MarshalBinary ()
115
- if _ , err := listener .conn .WriteTo (data , addr ); err != nil {
114
+ data , _ := (& message.OpenConnectionReply2 {ServerGUID : h . l .id , ClientAddress : resolve (addr ), MTUSize : mtuSize }).MarshalBinary ()
115
+ if _ , err := h . l .conn .WriteTo (data , addr ); err != nil {
116
116
return fmt .Errorf ("send OPEN_CONNECTION_REPLY_2: %w" , err )
117
117
}
118
118
119
- conn := newConn (listener . conn , addr , mtuSize , listener . h )
120
- listener .connections .Store (resolve (addr ), conn )
119
+ conn := newConn (h . l . conn , addr , mtuSize , h )
120
+ h . l .connections .Store (resolve (addr ), conn )
121
121
122
122
go func () {
123
123
t := time .NewTimer (time .Second * 10 )
@@ -126,8 +126,8 @@ func handleOpenConnectionRequest2(listener *Listener, b []byte, addr net.Addr) e
126
126
case <- conn .connected :
127
127
// Add the connection to the incoming channel so that a caller of
128
128
// Accept() can receive it.
129
- listener .incoming <- conn
130
- case <- listener .closed :
129
+ h . l .incoming <- conn
130
+ case <- h . l .closed :
131
131
_ = conn .Close ()
132
132
case <- t .C :
133
133
// It took too long to complete this connection. We closed it and go
@@ -139,6 +139,28 @@ func handleOpenConnectionRequest2(listener *Listener, b []byte, addr net.Addr) e
139
139
return nil
140
140
}
141
141
142
+ // handleConnectionRequest handles a connection request packet inside of buffer
143
+ // b. An error is returned if the packet was invalid.
144
+ func (h listenerConnectionHandler ) handleConnectionRequest (conn * Conn , b []byte ) error {
145
+ pk := & message.ConnectionRequest {}
146
+ if err := pk .UnmarshalBinary (b ); err != nil {
147
+ return fmt .Errorf ("read CONNECTION_REQUEST: %w" , err )
148
+ }
149
+ return conn .send (& message.ConnectionRequestAccepted {ClientAddress : resolve (conn .raddr ), RequestTimestamp : pk .RequestTimestamp , AcceptedTimestamp : timestamp ()})
150
+ }
151
+
152
+ // handleNewIncomingConnection handles an incoming connection packet from the
153
+ // client, finalising the Conn.
154
+ func (h listenerConnectionHandler ) handleNewIncomingConnection (conn * Conn ) error {
155
+ select {
156
+ case <- conn .connected :
157
+ return errUnexpectedAdditionalNIC
158
+ default :
159
+ close (conn .connected )
160
+ }
161
+ return nil
162
+ }
163
+
142
164
type dialerConnectionHandler struct {}
143
165
144
166
var (
@@ -147,12 +169,20 @@ var (
147
169
errUnexpectedNIC = errors .New ("unexpected NEW_INCOMING_CONNECTION packet" )
148
170
)
149
171
150
- func (h * dialerConnectionHandler ) handle (conn * Conn , b []byte ) (handled bool , err error ) {
172
+ func (h dialerConnectionHandler ) close (conn * Conn ) {
173
+ _ = conn .conn .Close ()
174
+ }
175
+
176
+ func (h dialerConnectionHandler ) limitsEnabled () bool {
177
+ return false
178
+ }
179
+
180
+ func (h dialerConnectionHandler ) handle (conn * Conn , b []byte ) (handled bool , err error ) {
151
181
switch b [0 ] {
152
182
case message .IDConnectionRequest :
153
183
return true , errUnexpectedCR
154
184
case message .IDConnectionRequestAccepted :
155
- return true , handleConnectionRequestAccepted (conn , b [1 :])
185
+ return true , h . handleConnectionRequestAccepted (conn , b [1 :])
156
186
case message .IDNewIncomingConnection :
157
187
return true , errUnexpectedNIC
158
188
case message .IDConnectedPing :
@@ -170,12 +200,22 @@ func (h *dialerConnectionHandler) handle(conn *Conn, b []byte) (handled bool, er
170
200
}
171
201
}
172
202
173
- func (h * dialerConnectionHandler ) close (conn * Conn ) {
174
- _ = conn .conn .Close ()
175
- }
176
-
177
- func (h * dialerConnectionHandler ) limitsEnabled () bool {
178
- return false
203
+ // handleConnectionRequestAccepted handles a serialised connection request
204
+ // accepted packet in b, and returns an error if not successful.
205
+ func (h dialerConnectionHandler ) handleConnectionRequestAccepted (conn * Conn , b []byte ) error {
206
+ pk := & message.ConnectionRequestAccepted {}
207
+ if err := pk .UnmarshalBinary (b ); err != nil {
208
+ return fmt .Errorf ("read CONNECTION_REQUEST_ACCEPTED: %w" , err )
209
+ }
210
+ select {
211
+ case <- conn .connected :
212
+ return errUnexpectedAdditionalCRA
213
+ default :
214
+ // Make sure to send NewIncomingConnection before closing conn.connected.
215
+ err := conn .send (& message.NewIncomingConnection {ServerAddress : resolve (conn .raddr ), RequestTimestamp : pk .AcceptedTimestamp , AcceptedTimestamp : timestamp ()})
216
+ close (conn .connected )
217
+ return err
218
+ }
179
219
}
180
220
181
221
// handleConnectedPing handles a connected ping packet inside of buffer b. An
@@ -204,43 +244,3 @@ func handleConnectedPong(b []byte) error {
204
244
// unreliable and doesn't give a good idea of the connection quality.
205
245
return nil
206
246
}
207
-
208
- // handleConnectionRequest handles a connection request packet inside of buffer
209
- // b. An error is returned if the packet was invalid.
210
- func handleConnectionRequest (conn * Conn , b []byte ) error {
211
- pk := & message.ConnectionRequest {}
212
- if err := pk .UnmarshalBinary (b ); err != nil {
213
- return fmt .Errorf ("read CONNECTION_REQUEST: %w" , err )
214
- }
215
- return conn .send (& message.ConnectionRequestAccepted {ClientAddress : resolve (conn .raddr ), RequestTimestamp : pk .RequestTimestamp , AcceptedTimestamp : timestamp ()})
216
- }
217
-
218
- // handleConnectionRequestAccepted handles a serialised connection request
219
- // accepted packet in b, and returns an error if not successful.
220
- func handleConnectionRequestAccepted (conn * Conn , b []byte ) error {
221
- pk := & message.ConnectionRequestAccepted {}
222
- if err := pk .UnmarshalBinary (b ); err != nil {
223
- return fmt .Errorf ("read CONNECTION_REQUEST_ACCEPTED: %w" , err )
224
- }
225
- select {
226
- case <- conn .connected :
227
- return errUnexpectedAdditionalCRA
228
- default :
229
- // Make sure to send NewIncomingConnection before closing conn.connected.
230
- err := conn .send (& message.NewIncomingConnection {ServerAddress : resolve (conn .raddr ), RequestTimestamp : pk .AcceptedTimestamp , AcceptedTimestamp : timestamp ()})
231
- close (conn .connected )
232
- return err
233
- }
234
- }
235
-
236
- // handleNewIncomingConnection handles an incoming connection packet from the
237
- // client, finalising the Conn.
238
- func handleNewIncomingConnection (conn * Conn ) error {
239
- select {
240
- case <- conn .connected :
241
- return errUnexpectedAdditionalNIC
242
- default :
243
- close (conn .connected )
244
- }
245
- return nil
246
- }
0 commit comments