forked from Anuken/Arc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.java
More file actions
340 lines (302 loc) · 10.9 KB
/
Connection.java
File metadata and controls
340 lines (302 loc) · 10.9 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package arc.net;
import arc.net.FrameworkMessage.Ping;
import java.io.IOException;
import java.net.Socket;
import java.net.*;
import java.nio.channels.SocketChannel;
/**
* Represents a TCP and optionally a UDP connection between a {@link Client} and
* a {@link Server}. If either underlying connection is closed or errors, both
* connections are closed.
* @author Nathan Sweet <misc@n4te.com>
*/
public class Connection{
protected int id = -1;
protected String name;
EndPoint endPoint;
TcpConnection tcp;
UdpConnection udp;
InetSocketAddress udpRemoteAddress;
protected NetListener[] listeners = {};
private final Object listenerLock = new Object();
protected int lastPingID;
protected long lastPingSendTime;
protected int returnTripTime;
volatile boolean isConnected;
volatile ArcNetException lastProtocolError;
private Object arbitraryData;
protected Connection(){
}
void initialize(NetSerializer serialization, int writeBufferSize, int objectBufferSize){
tcp = new TcpConnection(serialization, writeBufferSize,
objectBufferSize);
}
/**
* Returns the server assigned ID. Will return -1 if this connection has
* never been connected or the last assigned ID if this connection has been
* disconnected.
*/
public int getID(){
return id;
}
/**
* Returns true if this connection is connected to the remote end. Note that
* a connection can become disconnected at any time.
*/
public boolean isConnected(){
return isConnected;
}
/**
* Returns the last protocol error that occured on the connection.
* @return The last protocol error or null if none error occured.
*/
public ArcNetException getLastProtocolError(){
return lastProtocolError;
}
/**
* Sends the object over the network using TCP.
* @return The number of bytes sent.
*/
public int sendTCP(Object object){
if(object == null) throw new IllegalArgumentException("object cannot be null.");
try{
return tcp.send(object);
}catch(IOException | ArcNetException ex){
close(DcReason.error);
ArcNet.handleError(ex);
return 0;
}
}
/**
* Sends the object over the network using UDP.
* @return The number of bytes sent.
* @throws IllegalStateException if this connection was not opened with both TCP and UDP.
*/
public int sendUDP(Object object){
if(object == null)
throw new IllegalArgumentException("object cannot be null.");
SocketAddress address = udpRemoteAddress;
if(address == null && udp != null)
address = udp.connectedAddress;
if(address == null && isConnected)
throw new IllegalStateException("Connection is not connected via UDP.");
try{
if(address == null) throw new SocketException("Connection is closed.");
return udp.send(object, address);
}catch(IOException | ArcNetException ex){
close(DcReason.error);
ArcNet.handleError(ex);
return 0;
}
}
public void close(DcReason reason){
boolean wasConnected = isConnected;
isConnected = false;
tcp.close();
if(udp != null && udp.connectedAddress != null)
udp.close();
if(wasConnected){
notifyDisconnected(reason);
}
setConnected(false);
}
/**
* Requests the connection to communicate with the remote computer to
* determine a new value for the {@link #getReturnTripTime() return trip
* time}. When the connection receives a {@link FrameworkMessage.Ping}
* object with {@link Ping#isReply isReply} set to true, the new return trip
* time is available.
*/
public void updateReturnTripTime(){
Ping ping = new Ping();
ping.id = lastPingID++;
lastPingSendTime = System.currentTimeMillis();
sendTCP(ping);
}
/**
* Returns the last calculated TCP return trip time, or -1 if
* {@link #updateReturnTripTime()} has never been called or the
* {@link FrameworkMessage.Ping} response has not yet been received.
*/
public int getReturnTripTime(){
return returnTripTime;
}
/**
* An empty object will be sent if the TCP connection has not sent an object
* within the specified milliseconds. Periodically sending a keep alive
* ensures that an abnormal close is detected in a reasonable amount of time
* (see {@link #setTimeout(int)} ). Also, some network hardware will close a
* TCP connection that ceases to transmit for a period of time (typically 1+
* minutes). Set to zero to disable. Defaults to 8000.
*/
public void setKeepAliveTCP(int keepAliveMillis){
tcp.keepAliveMillis = keepAliveMillis;
}
/**
* If the specified amount of time passes without receiving an object over
* TCP, the connection is considered closed. When a TCP socket is closed
* normally, the remote end is notified immediately and this timeout is not
* needed. However, if a socket is closed abnormally (eg, power loss),
* ArcNet uses this timeout to detect the problem. The timeout should be
* set higher than the {@link #setKeepAliveTCP(int) TCP keep alive} for the
* remote end of the connection. The keep alive ensures that the remote end
* of the connection will be constantly sending objects, and setting the
* timeout higher than the keep alive allows for network latency. Set to
* zero to disable. Defaults to 12000.
*/
public void setTimeout(int timeoutMillis){
tcp.timeoutMillis = timeoutMillis;
}
/**
* Adds a listener to the connection. If the listener already exists, it is
* not added again.
* @param listener The listener to add.
*/
public void addListener(NetListener listener){
if(listener == null)
throw new IllegalArgumentException("listener cannot be null.");
synchronized(listenerLock){
NetListener[] listeners = this.listeners;
int n = listeners.length;
for(int i = 0; i < n; i++)
if(listener == listeners[i])
return;
NetListener[] newListeners = new NetListener[n + 1];
newListeners[0] = listener;
System.arraycopy(listeners, 0, newListeners, 1, n);
this.listeners = newListeners;
}
}
public void removeListener(NetListener listener){
if(listener == null)
throw new IllegalArgumentException("listener cannot be null.");
synchronized(listenerLock){
NetListener[] listeners = this.listeners;
int n = listeners.length;
if(n == 0)
return;
NetListener[] newListeners = new NetListener[n - 1];
for(int i = 0, ii = 0; i < n; i++){
NetListener copyListener = listeners[i];
if(listener == copyListener)
continue;
if(ii == n - 1)
return;
newListeners[ii++] = copyListener;
}
this.listeners = newListeners;
}
}
void notifyConnected(){
NetListener[] listeners = this.listeners;
for(NetListener listener : listeners){
listener.connected(this);
}
}
void notifyDisconnected(DcReason reason){
NetListener[] listeners = this.listeners;
for(NetListener listener : listeners){
listener.disconnected(this, reason);
}
}
void notifyIdle(){
NetListener[] listeners = this.listeners;
for(NetListener listener : listeners){
listener.idle(this);
if(!isIdle())
break;
}
}
void notifyReceived(Object object){
if(object instanceof Ping){
Ping ping = (Ping)object;
if(ping.isReply){
if(ping.id == lastPingID - 1){
returnTripTime = (int)(System.currentTimeMillis()
- lastPingSendTime);
}
}else{
ping.isReply = true;
sendTCP(ping);
}
}
NetListener[] listeners = this.listeners;
for(NetListener listener : listeners){
listener.received(this, object);
}
}
/**
* Returns the local {@link Client} or {@link Server} to which this
* connection belongs.
*/
public EndPoint getEndPoint(){
return endPoint;
}
/**
* Returns the IP address and port of the remote end of the TCP connection,
* or null if this connection is not connected.
*/
public InetSocketAddress getRemoteAddressTCP(){
SocketChannel socketChannel = tcp.socketChannel;
if(socketChannel != null){
Socket socket = tcp.socketChannel.socket();
if(socket != null){
return (InetSocketAddress)socket.getRemoteSocketAddress();
}
}
return null;
}
/**
* Returns the IP address and port of the remote end of the UDP connection,
* or null if this connection is not connected.
*/
public InetSocketAddress getRemoteAddressUDP(){
return udp.connectedAddress != null ? udp.connectedAddress : udpRemoteAddress;
}
/**
* Sets the friendly name of this connection. This is returned by
* {@link #toString()} and is useful for providing application specific
* identifying information in the logging. May be null for the default name
* of "Connection X", where X is the connection ID.
*/
public void setName(String name){
this.name = name;
}
/**
* Returns the number of bytes that are waiting to be written to the TCP
* socket, if any.
*/
public int getTcpWriteBufferSize(){
return tcp.writeBuffer.position();
}
/**
* @see #setIdleThreshold(float)
*/
public boolean isIdle(){
return tcp.writeBuffer.position() / (float)tcp.writeBuffer.capacity() < tcp.idleThreshold;
}
/**
* If the percent of the TCP write buffer that is filled is less than the
* specified threshold, {@link NetListener#idle(Connection)} will be called for
* each network thread update. Default is 0.1.
*/
public void setIdleThreshold(float idleThreshold){
tcp.idleThreshold = idleThreshold;
}
public String toString(){
if(name != null)
return name;
return "Connection " + id;
}
void setConnected(boolean isConnected){
this.isConnected = isConnected;
if(isConnected && name == null)
name = "Connection " + id;
}
public Object getArbitraryData(){
return arbitraryData;
}
public void setArbitraryData(Object arbitraryData){
this.arbitraryData = arbitraryData;
}
}