Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions examples/src/main/java/io/tlf/monkeynetty/test/JmeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
package io.tlf.monkeynetty.test;

import io.netty.handler.logging.LogLevel;
import io.tlf.monkeynetty.ConnectionListener;
import io.tlf.monkeynetty.client.*;
import io.tlf.monkeynetty.test.messages.TestUDPBigMessageA;
import io.tlf.monkeynetty.test.messages.TestTCPBigMessageA;
import io.tlf.monkeynetty.test.messages.TestTCPMessage;
Expand All @@ -34,10 +34,6 @@ of this software and associated documentation files (the "Software"), to deal
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import io.tlf.monkeynetty.MessageListener;
import io.tlf.monkeynetty.NetworkClient;
import io.tlf.monkeynetty.NetworkServer;
import io.tlf.monkeynetty.client.NettyClient;
import io.tlf.monkeynetty.msg.NetworkMessage;
import io.tlf.monkeynetty.test.messages.TestTCPBigMessageB;
import io.tlf.monkeynetty.test.messages.TestUDPBigMessageB;
Expand All @@ -51,12 +47,19 @@ public class JmeClient extends SimpleApplication {

@Override
public void simpleInitApp() {
client = new NettyClient("test", true, 10000, "localhost");
NetworkClientSettings settings = new NetworkClientSettings();
settings.setService("test");
settings.setAddress("localhost");
settings.setTcpPort(10000);
settings.setUdpPort(10000);
settings.setSsl(true);
settings.setSslSelfSigned(true);
client = new NettyClient(settings);
stateManager.attach(client);
client.setLogLevel(LogLevel.INFO);
client.registerListener(new MessageListener() {
client.registerListener(new ClientMessageListener() {
@Override
public void onMessage(NetworkMessage msg, NetworkServer server, NetworkClient client) {
public void onMessage(NetworkMessage msg, NetworkClient client) {
System.out.println("Got message " + msg);
}

Expand All @@ -66,7 +69,7 @@ public Class<? extends NetworkMessage>[] getSupportedMessages() {
}
});

client.registerListener(new ConnectionListener() {
client.registerListener(new ClientConnectionListener() {
@Override
public void onConnect(NetworkClient client) {
client.send(new TestTCPMessage());
Expand Down
27 changes: 16 additions & 11 deletions examples/src/main/java/io/tlf/monkeynetty/test/JmeServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ of this software and associated documentation files (the "Software"), to deal

package io.tlf.monkeynetty.test;

import io.tlf.monkeynetty.server.*;
import io.tlf.monkeynetty.test.messages.TestTCPMessage;
import io.tlf.monkeynetty.test.messages.TestUDPMessage;
import com.jme3.app.SimpleApplication;
import com.jme3.system.JmeContext;
import io.netty.handler.logging.LogLevel;
import io.tlf.monkeynetty.ConnectionListener;
import io.tlf.monkeynetty.MessageListener;
import io.tlf.monkeynetty.NetworkClient;
import io.tlf.monkeynetty.NetworkServer;
import io.tlf.monkeynetty.msg.NetworkMessage;
import io.tlf.monkeynetty.server.NettyServer;
import io.tlf.monkeynetty.test.messages.TestTCPBigMessageA;
import io.tlf.monkeynetty.test.messages.TestTCPBigMessageB;
import io.tlf.monkeynetty.test.messages.TestUDPBigMessageA;
Expand All @@ -47,23 +43,32 @@ public class JmeServer extends SimpleApplication {

@Override
public void simpleInitApp() {
NettyServer server = new NettyServer("test", true, 10000);

NetworkServerSettings serverSettings = new NetworkServerSettings();
serverSettings.setService("test");
serverSettings.setTcpPort(10000);
serverSettings.setUdpPort(10000);
serverSettings.setSsl(true);
serverSettings.setSslSelfSigned(true);

NettyServer server = new NettyServer(serverSettings);
server.setLogLevel(LogLevel.INFO);

stateManager.attach(server);
server.registerListener(new ConnectionListener() {
server.registerListener(new ServerConnectionListener() {
@Override
public void onConnect(NetworkClient client) {
public void onConnect(NetworkConnection client) {
System.out.println("Client connected: " + client.getAddress());
}

@Override
public void onDisconnect(NetworkClient client) {
public void onDisconnect(NetworkConnection client) {
System.out.println("Client disconnected: " + client.getAddress());
}
});
server.registerListener(new MessageListener() {
server.registerListener(new ServerMessageListener() {
@Override
public void onMessage(NetworkMessage msg, NetworkServer server, NetworkClient client) {
public void onMessage(NetworkMessage msg, NetworkServer server, NetworkConnection client) {
System.out.println("Got message " + msg.getName() + " from client " + client.getAddress());
System.out.println(msg.toString());
client.send(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/

package io.tlf.monkeynetty;
package io.tlf.monkeynetty.client;

/**
* @author Trevor Flynn trevorflynn@liquidcrystalstudios.com
*/
public interface ConnectionListener {
public interface ClientConnectionListener {

/**
* Called when a client is connected
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
MIT License

Copyright (c) 2020 Trevor Flynn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package io.tlf.monkeynetty.client;

import io.tlf.monkeynetty.msg.NetworkMessage;

/**
* @author Trevor Flynn trevorflynn@liquidcrystalstudios.com
*/
public interface ClientMessageListener {

/**
* When the server/client receives a message, this will be called.
* This is to be implemented by the user code.
*
* @param msg The message received
* @param client The client that received the message
*/
public void onMessage(NetworkMessage msg, NetworkClient client);

/**
* The listener <code>onMessage</code> will only get called if the message received
* is within this returned list.
*
* @return A list of supported messages by this listener
*/
public Class<? extends NetworkMessage>[] getSupportedMessages();
}
Loading