My favorites | Sign in
Project Home
Checkout   Browse   Changes  
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

package com.esotericsoftware.kryonet;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

import static com.esotericsoftware.minlog.Log.*;

/** @author Nathan Sweet <misc@n4te.com> */
class UdpConnection {
InetSocketAddress connectedAddress;
DatagramChannel datagramChannel;
int keepAliveMillis = 19000;
final ByteBuffer readBuffer, writeBuffer;
private final Serialization serialization;
private SelectionKey selectionKey;
private final Object writeLock = new Object();
private long lastCommunicationTime;

public UdpConnection (Serialization serialization, int bufferSize) {
this.serialization = serialization;
readBuffer = ByteBuffer.allocate(bufferSize);
writeBuffer = ByteBuffer.allocateDirect(bufferSize);
}

public void bind (Selector selector, InetSocketAddress localPort) throws IOException {
close();
readBuffer.clear();
writeBuffer.clear();
try {
datagramChannel = selector.provider().openDatagramChannel();
datagramChannel.socket().bind(localPort);
datagramChannel.configureBlocking(false);
selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ);

lastCommunicationTime = System.currentTimeMillis();
} catch (IOException ex) {
close();
throw ex;
}
}

public void connect (Selector selector, InetSocketAddress remoteAddress) throws IOException {
close();
readBuffer.clear();
writeBuffer.clear();
try {
datagramChannel = selector.provider().openDatagramChannel();
datagramChannel.socket().bind(null);
datagramChannel.socket().connect(remoteAddress);
datagramChannel.configureBlocking(false);

selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ);

lastCommunicationTime = System.currentTimeMillis();

connectedAddress = remoteAddress;
} catch (IOException ex) {
close();
IOException ioEx = new IOException("Unable to connect to: " + remoteAddress);
ioEx.initCause(ex);
throw ioEx;
}
}

public InetSocketAddress readFromAddress () throws IOException {
DatagramChannel datagramChannel = this.datagramChannel;
if (datagramChannel == null) throw new SocketException("Connection is closed.");
lastCommunicationTime = System.currentTimeMillis();
return (InetSocketAddress)datagramChannel.receive(readBuffer);
}

public Object readObject (Connection connection) {
readBuffer.flip();
try {
try {
Object object = serialization.read(connection, readBuffer);
if (readBuffer.hasRemaining())
throw new KryoNetException("Incorrect number of bytes (" + readBuffer.remaining()
+ " remaining) used to deserialize object: " + object);
return object;
} catch (Exception ex) {
throw new KryoNetException("Error during deserialization.", ex);
}
} finally {
readBuffer.clear();
}
}

/** This method is thread safe. */
public int send (Connection connection, Object object, SocketAddress address) throws IOException {
DatagramChannel datagramChannel = this.datagramChannel;
if (datagramChannel == null) throw new SocketException("Connection is closed.");
synchronized (writeLock) {
try {
try {
serialization.write(connection, writeBuffer, object);
} catch (Exception ex) {
throw new KryoNetException("Error serializing object of type: " + object.getClass().getName(), ex);
}
writeBuffer.flip();
int length = writeBuffer.limit();
datagramChannel.send(writeBuffer, address);

lastCommunicationTime = System.currentTimeMillis();

boolean wasFullWrite = !writeBuffer.hasRemaining();
return wasFullWrite ? length : -1;
} finally {
writeBuffer.clear();
}
}
}

public void close () {
connectedAddress = null;
try {
if (datagramChannel != null) {
datagramChannel.close();
datagramChannel = null;
if (selectionKey != null) selectionKey.selector().wakeup();
}
} catch (IOException ex) {
if (DEBUG) debug("kryonet", "Unable to close UDP connection.", ex);
}
}

public boolean needsKeepAlive (long time) {
return connectedAddress != null && keepAliveMillis > 0 && time - lastCommunicationTime > keepAliveMillis;
}
}

Change log

r127 by nathan.sweet on Jul 19, 2012   Diff
Fixed exceptions during de/serialization
from blowing up network thread.
Go to: 
Project members, sign in to write a code review

Older revisions

r96 by nathan.sweet on Apr 17, 2012   Diff
Better source format.
r93 by nathan.sweet on Apr 17, 2012   Diff
Updated to latest Kryo.
r91 by nathan.sweet on Apr 16, 2012   Diff
Fixed buffers not being cleared when
connection is closed.
All revisions of this file

File info

Size: 4448 bytes, 137 lines
Powered by Google Project Hosting