forked from EsotericSoftware/kryonet
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for serialization factories.
- Loading branch information
Showing
13 changed files
with
266 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,23 +22,18 @@ | |
import java.io.IOException; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryonet.serialization.KryoSerialization; | ||
import com.esotericsoftware.kryonet.serialization.Serialization; | ||
import com.esotericsoftware.kryonet.serialization.KryoSerializationFactory.KryoSerialization; | ||
|
||
/** | ||
* Represents the local end point of a connection. | ||
* | ||
* @author Nathan Sweet <[email protected]> | ||
*/ | ||
public interface EndPoint extends Runnable { | ||
/** | ||
* Gets the serialization instance that will be used to serialize and | ||
* deserialize objects. | ||
*/ | ||
public Serialization getSerialization(); | ||
|
||
/** | ||
* If the listener already exists, it is not added again. | ||
* Adds a listener to the endpoint. If the listener already exists, it is | ||
* <i>not</i> added again. | ||
*/ | ||
public void addListener(Listener listener); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,16 +47,17 @@ | |
import com.esotericsoftware.kryonet.FrameworkMessage.DiscoverHost; | ||
import com.esotericsoftware.kryonet.FrameworkMessage.RegisterTCP; | ||
import com.esotericsoftware.kryonet.FrameworkMessage.RegisterUDP; | ||
import com.esotericsoftware.kryonet.serialization.KryoSerialization; | ||
import com.esotericsoftware.kryonet.serialization.Serialization; | ||
import com.esotericsoftware.kryonet.serialization.KryoSerializationFactory; | ||
import com.esotericsoftware.kryonet.serialization.SerializationFactory; | ||
|
||
/** | ||
* Manages TCP and optionally UDP connections from many {@linkplain Client Clients}. | ||
* Manages TCP and optionally UDP connections from many {@linkplain Client | ||
* Clients}. | ||
* | ||
* @author Nathan Sweet <[email protected]> | ||
*/ | ||
public class Server implements EndPoint { | ||
private final Serialization serialization; | ||
private final SerializationFactory serializationFactory; | ||
private final int writeBufferSize, objectBufferSize; | ||
private final Selector selector; | ||
private int emptySelects; | ||
|
@@ -134,15 +135,15 @@ public Server() { | |
* largest object that will be sent or received. | ||
*/ | ||
public Server(int writeBufferSize, int objectBufferSize) { | ||
this(writeBufferSize, objectBufferSize, new KryoSerialization()); | ||
this(writeBufferSize, objectBufferSize, new KryoSerializationFactory()); | ||
} | ||
|
||
public Server(int writeBufferSize, int objectBufferSize, | ||
Serialization serialization) { | ||
SerializationFactory serializationFactory) { | ||
this.writeBufferSize = writeBufferSize; | ||
this.objectBufferSize = objectBufferSize; | ||
|
||
this.serialization = serialization; | ||
this.serializationFactory = serializationFactory; | ||
|
||
this.discoveryHandler = ServerDiscoveryHandler.DEFAULT; | ||
|
||
|
@@ -158,12 +159,12 @@ public void setDiscoveryHandler( | |
discoveryHandler = newDiscoveryHandler; | ||
} | ||
|
||
public Serialization getSerialization() { | ||
return serialization; | ||
public SerializationFactory getSerializationFactory() { | ||
return serializationFactory; | ||
} | ||
|
||
public Kryo getKryo() { | ||
return ((KryoSerialization) serialization).getKryo(); | ||
return ((KryoSerializationFactory) serializationFactory).getKryo(); | ||
} | ||
|
||
/** | ||
|
@@ -206,7 +207,9 @@ public void bind(InetSocketAddress tcpPort, InetSocketAddress udpPort) | |
+ "/TCP"); | ||
|
||
if (udpPort != null) { | ||
udp = new UdpConnection(serialization, objectBufferSize); | ||
udp = new UdpConnection( | ||
serializationFactory.newInstance(null), | ||
objectBufferSize); | ||
udp.bind(selector, udpPort); | ||
if (DEBUG) | ||
debug("kryonet", "Accepting connections on port: " | ||
|
@@ -282,7 +285,7 @@ public void update(int timeout) throws IOException { | |
try { | ||
while (true) { | ||
Object object = fromConnection.tcp | ||
.readObject(fromConnection); | ||
.readObject(); | ||
if (object == null) | ||
break; | ||
if (DEBUG) { | ||
|
@@ -391,7 +394,7 @@ public void update(int timeout) throws IOException { | |
|
||
Object object; | ||
try { | ||
object = udp.readObject(fromConnection); | ||
object = udp.readObject(); | ||
} catch (KryoNetException ex) { | ||
if (WARN) { | ||
if (fromConnection != null) { | ||
|
@@ -442,7 +445,7 @@ public void update(int timeout) throws IOException { | |
try { | ||
boolean responseSent = discoveryHandler | ||
.onDiscoverHost(udp.datagramChannel, | ||
fromAddress, serialization); | ||
fromAddress); | ||
if (DEBUG && responseSent) | ||
debug("kryonet", | ||
"Responded to host discovery from: " | ||
|
@@ -548,7 +551,8 @@ public void stop() { | |
|
||
private void acceptOperation(SocketChannel socketChannel) { | ||
Connection connection = newConnection(); | ||
connection.initialize(serialization, writeBufferSize, objectBufferSize); | ||
connection.initialize(serializationFactory.newInstance(connection), | ||
writeBufferSize, objectBufferSize); | ||
connection.endPoint = this; | ||
UdpConnection udp = this.udp; | ||
if (udp != null) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,11 @@ | |
|
||
package com.esotericsoftware.kryonet; | ||
|
||
import static com.esotericsoftware.minlog.Log.DEBUG; | ||
import static com.esotericsoftware.minlog.Log.TRACE; | ||
import static com.esotericsoftware.minlog.Log.debug; | ||
import static com.esotericsoftware.minlog.Log.trace; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.net.SocketAddress; | ||
|
@@ -30,11 +35,6 @@ | |
|
||
import com.esotericsoftware.kryonet.serialization.Serialization; | ||
|
||
import static com.esotericsoftware.minlog.Log.DEBUG; | ||
import static com.esotericsoftware.minlog.Log.TRACE; | ||
import static com.esotericsoftware.minlog.Log.debug; | ||
import static com.esotericsoftware.minlog.Log.trace; | ||
|
||
/** | ||
* @author Nathan Sweet <[email protected]> | ||
*/ | ||
|
@@ -130,7 +130,7 @@ public void connect(Selector selector, SocketAddress remoteAddress, | |
} | ||
} | ||
|
||
public Object readObject(Connection connection) throws IOException { | ||
public Object readObject() throws IOException { | ||
SocketChannel socketChannel = this.socketChannel; | ||
if (socketChannel == null) | ||
throw new SocketException("Connection is closed."); | ||
|
@@ -180,7 +180,7 @@ public Object readObject(Connection connection) throws IOException { | |
readBuffer.limit(startPosition + length); | ||
Object object; | ||
try { | ||
object = serialization.read(connection, readBuffer); | ||
object = serialization.read(readBuffer); | ||
} catch (Exception ex) { | ||
throw new KryoNetException("Error during deserialization.", ex); | ||
} | ||
|
@@ -227,7 +227,7 @@ private boolean writeToSocket() throws IOException { | |
/** | ||
* This method is thread safe. | ||
*/ | ||
public int send(Connection connection, Object object) throws IOException { | ||
public int send(Object object) throws IOException { | ||
SocketChannel socketChannel = this.socketChannel; | ||
if (socketChannel == null) | ||
throw new SocketException("Connection is closed."); | ||
|
@@ -239,7 +239,7 @@ public int send(Connection connection, Object object) throws IOException { | |
|
||
// Write data. | ||
try { | ||
serialization.write(connection, writeBuffer, object); | ||
serialization.write(writeBuffer, object); | ||
} catch (KryoNetException ex) { | ||
throw new KryoNetException("Error serializing object of type: " | ||
+ object.getClass().getName(), ex); | ||
|
@@ -267,13 +267,11 @@ public int send(Connection connection, Object object) throws IOException { | |
/ (float) writeBuffer.capacity(); | ||
if (DEBUG && percentage > 0.75f) | ||
debug("kryonet", | ||
connection | ||
+ " TCP write buffer is approaching capacity: " | ||
" TCP write buffer is approaching capacity: " | ||
+ percentage + "%"); | ||
else if (TRACE && percentage > 0.25f) | ||
trace("kryonet", | ||
connection + " TCP write buffer utilization: " | ||
+ percentage + "%"); | ||
trace("kryonet", " TCP write buffer utilization: " | ||
+ percentage + "%"); | ||
} | ||
|
||
lastWriteTime = System.currentTimeMillis(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
|
||
package com.esotericsoftware.kryonet; | ||
|
||
import static com.esotericsoftware.minlog.Log.DEBUG; | ||
import static com.esotericsoftware.minlog.Log.debug; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
|
@@ -30,9 +33,6 @@ | |
|
||
import com.esotericsoftware.kryonet.serialization.Serialization; | ||
|
||
import static com.esotericsoftware.minlog.Log.DEBUG; | ||
import static com.esotericsoftware.minlog.Log.debug; | ||
|
||
/** | ||
* @author Nathan Sweet <[email protected]> | ||
*/ | ||
|
@@ -114,11 +114,11 @@ public InetSocketAddress readFromAddress() throws IOException { | |
return connectedAddress; | ||
} | ||
|
||
public Object readObject(Connection connection) { | ||
public Object readObject() { | ||
readBuffer.flip(); | ||
try { | ||
try { | ||
Object object = serialization.read(connection, readBuffer); | ||
Object object = serialization.read(readBuffer); | ||
if (readBuffer.hasRemaining()) | ||
throw new KryoNetException("Incorrect number of bytes (" | ||
+ readBuffer.remaining() | ||
|
@@ -136,15 +136,14 @@ public Object readObject(Connection connection) { | |
/** | ||
* This method is thread safe. | ||
*/ | ||
public int send(Connection connection, Object object, SocketAddress address) | ||
throws IOException { | ||
public int send(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); | ||
serialization.write(writeBuffer, object); | ||
} catch (Exception ex) { | ||
throw new KryoNetException( | ||
"Error serializing object of type: " | ||
|
Oops, something went wrong.