Skip to content

Commit

Permalink
Changed listener to an interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
crykn committed Jul 16, 2017
1 parent 421e0d1 commit 2ba475f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

A fork of [kryonet](https://github.com/EsotericSoftware/kryonet/), a Java library that provides a clean and simple API for efficient network communication.

This fork was specifically made for [ProjectGG]() and adds only a few small improvements.
This fork was specifically made for [ProjectGG](https://github.com/Meidimax99/ProjektGG) but also adds the most demanded features on kryonet's issue tracker.

### Key Changes
* A TypeListener for easier message handling (see the example below)
* Fixes for the Android 5 and iOS crashes
* Listener is now a interface ([#39](https://github.com/EsotericSoftware/kryonet/issues/39))
* Uses kryo 4.0.0 ([#77](https://github.com/EsotericSoftware/kryonet/issues/77))
* Fixes for the Android 5 and iOS crashes ([#106](https://github.com/EsotericSoftware/kryonet/issues/106))

### Usage of the changes

This code adds a listener to handle receiving objects:

```java
TypeListener typeListener = new TypeListener();
TypeListener typeListener = new TypeListener();

typeListener.addTypeHandler(SomeRequest.class,
(con, msg) -> {
System.out.println(msg.getSomeData());
});
typeListener.addTypeHandler(SomeOtherRequest.class,
(con, msg) -> {
con.sendTCP(new SomeResponse());
});
typeListener.addTypeHandler(SomeRequest.class,
(con, msg) -> {
System.out.println(msg.getSomeData());
});
typeListener.addTypeHandler(SomeOtherRequest.class,
(con, msg) -> {
con.sendTCP(new SomeResponse());
});

server.addListener(typeListener);
});
server.addListener(typeListener);
```
52 changes: 42 additions & 10 deletions src/main/java/com/esotericsoftware/kryonet/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,21 @@
/**
* Used to be notified about connection events.
*/
public class Listener {
public interface Listener {
/**
* Called when the remote end has been connected. This will be invoked
* before any objects are received by {@link #received(Connection, Object)}.
* This will be invoked on the same thread as {@link Client#update(int)} and
* {@link Server#update(int)}. This method should not block for long periods
* as other network activity will not be processed until it returns.
*/
public void connected(Connection connection) {
}
public void connected(Connection connection);

/**
* Called when the remote end is no longer connected. There is no guarantee
* as to what thread will invoke this method.
*/
public void disconnected(Connection connection) {
}
public void disconnected(Connection connection);

/**
* Called when an object has been received from the remote end of the
Expand All @@ -55,14 +53,36 @@ public void disconnected(Connection connection) {
* should not block for long periods as other network activity will not be
* processed until it returns.
*/
public void received(Connection connection, Object object) {
}
public void received(Connection connection, Object object);

/**
* Called when the connection is below the
* {@link Connection#setIdleThreshold(float) idle threshold}.
*/
public void idle(Connection connection) {
public void idle(Connection connection);

/**
* Wraps the listener interface and implements
* {@link Listener#idle(Connection)} and
* {@link Listener#received(Connection, Object)}.
*
*/
public abstract class ConnectionListener implements Listener {

@Override
public abstract void disconnected(Connection arg0);

@Override
public abstract void connected(Connection arg0);

@Override
public void received(Connection connection, Object object) {
}

@Override
public void idle(Connection connection) {
}

}

/**
Expand All @@ -72,7 +92,7 @@ public void idle(Connection connection) {
* Add a handler for a specific type via
* {@link #addHandler(Class, BiConsumer)}.
*/
public class TypeListener extends Listener {
static public class TypeListener implements Listener {

/**
* All type listeners.
Expand Down Expand Up @@ -102,14 +122,26 @@ public <T> void addTypeHandler(Class<T> clazz,
listeners.put(clazz, listener);
}

@Override
public void connected(Connection connection) {
}

@Override
public void disconnected(Connection connection) {
}

@Override
public void idle(Connection connection) {
}

}

/**
* Wraps a listener and queues notifications as {@link Runnable runnables}.
* This allows the runnables to be processed on a different thread,
* preventing the connection's update thread from being blocked.
*/
static public abstract class QueuedListener extends Listener {
static public abstract class QueuedListener implements Listener {
final Listener listener;

public QueuedListener(Listener listener) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/esotericsoftware/kryonet/rmi/ObjectSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public void run() {
public void disconnected(Connection connection) {
removeConnection(connection);
}

@Override
public void connected(Connection connection) {
}

@Override
public void idle(Connection connection) {
}
};

/**
Expand Down Expand Up @@ -450,6 +458,14 @@ public void received(Connection connection, Object object) {
public void disconnected(Connection connection) {
close();
}

@Override
public void connected(Connection connection) {
}

@Override
public void idle(Connection connection) {
}
};
connection.addListener(responseListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

abstract public class TcpIdleSender extends Listener {
abstract public class TcpIdleSender implements Listener {
boolean started;

public void idle(Connection connection) {
Expand Down

0 comments on commit 2ba475f

Please sign in to comment.