diff --git a/examples/src/main/java/io/tlf/monkeynetty/test/JmeClient.java b/examples/src/main/java/io/tlf/monkeynetty/test/JmeClient.java index 4030ae5..76130d6 100644 --- a/examples/src/main/java/io/tlf/monkeynetty/test/JmeClient.java +++ b/examples/src/main/java/io/tlf/monkeynetty/test/JmeClient.java @@ -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; @@ -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; @@ -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); } @@ -66,7 +69,7 @@ public Class[] getSupportedMessages() { } }); - client.registerListener(new ConnectionListener() { + client.registerListener(new ClientConnectionListener() { @Override public void onConnect(NetworkClient client) { client.send(new TestTCPMessage()); diff --git a/examples/src/main/java/io/tlf/monkeynetty/test/JmeServer.java b/examples/src/main/java/io/tlf/monkeynetty/test/JmeServer.java index d4449cf..4e12dc6 100644 --- a/examples/src/main/java/io/tlf/monkeynetty/test/JmeServer.java +++ b/examples/src/main/java/io/tlf/monkeynetty/test/JmeServer.java @@ -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; @@ -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); diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/ConnectionListener.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/ClientConnectionListener.java similarity index 94% rename from monkey-netty/src/main/java/io/tlf/monkeynetty/ConnectionListener.java rename to monkey-netty/src/main/java/io/tlf/monkeynetty/client/ClientConnectionListener.java index 6a475cf..b218731 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/ConnectionListener.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/ClientConnectionListener.java @@ -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 diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/client/ClientMessageListener.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/ClientMessageListener.java new file mode 100644 index 0000000..49594c4 --- /dev/null +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/ClientMessageListener.java @@ -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 onMessage will only get called if the message received + * is within this returned list. + * + * @return A list of supported messages by this listener + */ + public Class[] getSupportedMessages(); +} diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NettyClient.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NettyClient.java index 8eccc8c..ca0231b 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NettyClient.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NettyClient.java @@ -66,22 +66,12 @@ public class NettyClient extends BaseAppState implements NetworkClient { private final static Logger LOGGER = Logger.getLogger(NettyClient.class.getName()); - protected String service; - protected int port; - protected String server; - protected boolean ssl; - protected boolean sslSelfSigned; protected volatile boolean reconnect = false; protected volatile boolean disconnecting = false; private volatile boolean udpHandshakeComplete = false; private volatile boolean pendingEstablish = true; - /* - * Connection timeout in milliseconds used when client is unable connect to server - * Note: Currently it do not apply when server is "off" - */ - protected int connectionTimeout = 10000; - private MessageCacheMode cacheMode = MessageCacheMode.TCP_ENABLED; + protected final NetworkClientSettings settings; private LogLevel logLevel; //Netty @@ -95,50 +85,23 @@ public class NettyClient extends BaseAppState implements NetworkClient { private DatagramChannel udpChannel; private SslContext sslContext; - private final Set handlers = ConcurrentHashMap.newKeySet(); - private final Set listeners = ConcurrentHashMap.newKeySet(); + private final Set handlers = ConcurrentHashMap.newKeySet(); + private final Set listeners = ConcurrentHashMap.newKeySet(); private final ConcurrentLinkedQueue messageCache = new ConcurrentLinkedQueue<>(); private final Map atts = new ConcurrentHashMap<>(); - /** - * Creates a new client configured to connect to the server. - * This connection will have SSL disabled. - * - * @param service The name of the service running on this client - * @param port The port the server is listening on - * @param server The host/ip of the server - */ - public NettyClient(String service, int port, String server) { - this(service, false, false, port, server); - } /** * Creates a new client configured to connect to the server. - * - * @param service The name of the service running on this client - * @param ssl If the client should attempt to connect with ssl - * @param port The port the server is listening on - * @param server The host/ip of the server + * @param settings The settings to build the client */ - public NettyClient(String service, boolean ssl, int port, String server) { - this(service, ssl, true, port, server); + public NettyClient(NetworkClientSettings settings) { + this.settings = settings; } - /** - * Creates a new client configured to connect to the server. - * - * @param service The name of the service running on this client - * @param ssl If the client should attempt to connect with ssl - * @param sslSelfSigned If the client will allow the server to use a self signed ssl certificate - * @param port The port the server is listening on - * @param server The host/ip of the server - */ - public NettyClient(String service, boolean ssl, boolean sslSelfSigned, int port, String server) { - this.service = service; - this.port = port; - this.server = server; - this.ssl = ssl; - this.sslSelfSigned = sslSelfSigned; + @Override + public NetworkClientSettings getSettings() { + return settings; } @Override @@ -162,22 +125,6 @@ public void onDisable() { disconnect(); } - /** - * Set the timeout duration in milliseconds for creating a new connection from the client to the server. - * This does not effect the read/write timeouts for messages after the connection has been established. - * - * @param connectionTimeout The timeout in milliseconds for creating a new connection. - */ - public void setConnectionTimeout(int connectionTimeout) { - this.connectionTimeout = connectionTimeout; - } - - /** - * @return The timeout in milliseconds for creating a new connection - */ - public int getConnectionTimeout() { - return connectionTimeout; - } /** * Sets the Netty.IO internal log level. @@ -196,22 +143,6 @@ public LogLevel getLogLevel() { return logLevel; } - /** - * Sets the message cache mode. By default the mode is MessageCacheMode.ENABLE_TCP - * See MessageCacheMode for more information about the supported mode options. - * - * @param mode The desired message cache mode. - */ - public void setMessageCacheMode(MessageCacheMode mode) { - this.cacheMode = mode; - } - - /** - * @return The current message cache mode. - */ - public MessageCacheMode getMessageCacheMode() { - return cacheMode; - } /** * Internal use only @@ -224,9 +155,9 @@ public MessageCacheMode getMessageCacheMode() { */ private void setupTcp() { LOGGER.fine("Setting up tcp"); - if (ssl) { + if (settings.isSsl()) { try { - if (sslSelfSigned) { + if (settings.isSslSelfSigned()) { sslContext = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { @@ -234,7 +165,7 @@ private void setupTcp() { } } catch (Exception ex) { LOGGER.log(Level.WARNING, "Failed to load ssl, failing back to no ssl", ex); - ssl = false; + settings.setSsl(false); } } //Setup TCP @@ -242,17 +173,17 @@ private void setupTcp() { tcpClientBootstrap = new Bootstrap(); tcpClientBootstrap.group(tcpGroup); tcpClientBootstrap.channel(NioSocketChannel.class); - tcpClientBootstrap.remoteAddress(new InetSocketAddress(server, port)); + tcpClientBootstrap.remoteAddress(new InetSocketAddress(settings.getAddress(), settings.getTcpPort())); tcpClientBootstrap.handler(new ChannelInitializer() { protected void initChannel(SocketChannel socketChannel) { tcpChannel = socketChannel; SocketChannelConfig cfg = tcpChannel.config(); - cfg.setConnectTimeoutMillis(connectionTimeout); + cfg.setConnectTimeoutMillis(settings.getConnectionTimeout()); ChannelPipeline p = socketChannel.pipeline(); //Setup ssl - if (ssl) { - p.addLast(sslContext.newHandler(socketChannel.alloc(), server, port)); + if (settings.isSsl()) { + p.addLast(sslContext.newHandler(socketChannel.alloc(), settings.getAddress(), settings.getTcpPort())); } //Set log level if (logLevel != null) { @@ -334,12 +265,12 @@ private void setupUdp(String hash) { udpClientBootstrap = new Bootstrap(); udpClientBootstrap.group(tcpGroup); udpClientBootstrap.channel(NioDatagramChannel.class); - udpClientBootstrap.remoteAddress(new InetSocketAddress(server, port)); + udpClientBootstrap.remoteAddress(new InetSocketAddress(settings.getAddress(), settings.getUdpPort())); udpClientBootstrap.handler(new ChannelInitializer() { protected void initChannel(DatagramChannel socketChannel) { udpChannel = socketChannel; DatagramChannelConfig cfg = udpChannel.config(); - cfg.setConnectTimeoutMillis(connectionTimeout); + cfg.setConnectTimeoutMillis(settings.getConnectionTimeout()); //Setup pipeline ChannelPipeline p = socketChannel.pipeline(); //Setup pipeline @@ -399,7 +330,7 @@ protected void completeConnection() { pendingEstablish = false; LOGGER.log(Level.FINEST, "Connection established"); //Notify that we have completed the connection process - for (ConnectionListener listener : listeners) { + for (ClientConnectionListener listener : listeners) { try { listener.onConnect(NettyClient.this); } catch (Exception ex) { @@ -511,11 +442,11 @@ public void send(NetworkMessage message) { */ private void send(NetworkMessage message, boolean enableCache) { if (!isConnected() && enableCache) { - if (cacheMode == MessageCacheMode.ENABLED) { + if (settings.getCacheMode() == MessageCacheMode.ENABLED) { messageCache.add(message); - } else if (cacheMode == MessageCacheMode.TCP_ENABLED && message.getProtocol() == NetworkProtocol.TCP) { + } else if (settings.getCacheMode() == MessageCacheMode.TCP_ENABLED && message.getProtocol() == NetworkProtocol.TCP) { messageCache.add(message); - } else if (cacheMode == MessageCacheMode.UDP_ENABLED && message.getProtocol() == NetworkProtocol.UDP) { + } else if (settings.getCacheMode() == MessageCacheMode.UDP_ENABLED && message.getProtocol() == NetworkProtocol.UDP) { messageCache.add(message); } return; @@ -537,7 +468,7 @@ private void send(NetworkMessage message, boolean enableCache) { public void disconnect() { disconnecting = true; try { - for (ConnectionListener listener : listeners) { + for (ClientConnectionListener listener : listeners) { listener.onDisconnect(this); } } catch (Exception ex) { @@ -552,40 +483,15 @@ public void disconnect() { disconnecting = false; } - @Override - public String getAddress() { - return server; - } - - @Override - public int getPort() { - return port; - } - - @Override - public String getService() { - return service; - } - - @Override - public boolean isSsl() { - return ssl; - } - - @Override - public NetworkProtocol[] getProtocol() { - return new NetworkProtocol[]{NetworkProtocol.TCP, NetworkProtocol.UDP}; - } - @Override public void receive(NetworkMessage message) { LOGGER.finest("Got message: " + message.getName()); //Handlers try { - for (MessageListener handler : handlers) { + for (ClientMessageListener handler : handlers) { for (Class a : handler.getSupportedMessages()) { if (a.isInstance(message)) { - handler.onMessage(message, null, this); + handler.onMessage(message, this); } } } @@ -595,22 +501,22 @@ public void receive(NetworkMessage message) { } @Override - public void registerListener(MessageListener handler) { + public void registerListener(ClientMessageListener handler) { handlers.add(handler); } @Override - public void unregisterListener(MessageListener handler) { + public void unregisterListener(ClientMessageListener handler) { handlers.remove(handler); } @Override - public void registerListener(ConnectionListener listener) { + public void registerListener(ClientConnectionListener listener) { listeners.add(listener); } @Override - public void unregisterListener(ConnectionListener listener) { + public void unregisterListener(ClientConnectionListener listener) { listeners.remove(listener); } diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NetworkClient.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NetworkClient.java new file mode 100644 index 0000000..5f2df7c --- /dev/null +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NetworkClient.java @@ -0,0 +1,110 @@ +/* +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 + *

+ * Used as a basis for building a custom network client. + */ +public interface NetworkClient { + + /** + * @return if the client is connected to the server + */ + public boolean isConnected(); + + /** + * @return The settings of the client + */ + public NetworkClientSettings getSettings(); + + /** + * Send a message from the client to the server + * + * @param message The message to send + */ + public void send(NetworkMessage message); + + /** + * Internal Use Only + * Called by the server when the server receives a message for the server side connection client. + * + * @param message The message received + */ + public void receive(NetworkMessage message); + + /** + * Disconnects the client from the server + */ + public void disconnect(); + + /** + * Register a message listener with the client. + * + * @param handler The message listener to register + */ + public void registerListener(ClientMessageListener handler); + + /** + * Unregister a message listener with the client. + * + * @param handler The message listener to unregister + */ + public void unregisterListener(ClientMessageListener handler); + + /** + * Register a connection listener with the client. + * + * @param listener The connection listener to register + */ + public void registerListener(ClientConnectionListener listener); + + /** + * Unregister a connection listener with the client. + * + * @param listener The connection listener to unregister + */ + public void unregisterListener(ClientConnectionListener listener); + + /** + * Set obj value attribute for key param + * + * @param key key for attribute + * @param obj value object for attribute + */ + public void setUserData(String key, Object obj); + + /** + * Return attribute stored under key param + * + * @param type + * @param key key for attribute + * @return object casted to T type + */ + public T getUserData(String key); + +} diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NetworkClientSettings.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NetworkClientSettings.java new file mode 100644 index 0000000..1c6d77f --- /dev/null +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/client/NetworkClientSettings.java @@ -0,0 +1,149 @@ +package io.tlf.monkeynetty.client; + +import io.netty.handler.logging.LogLevel; +import io.tlf.monkeynetty.NetworkProtocol; +import io.tlf.monkeynetty.client.MessageCacheMode; + +import java.io.File; + +public class NetworkClientSettings { + + private String service = "monkey-netty"; + private int tcpPort = 13900; + private int udpPort = 13900; + private String address = "localhost"; + private boolean ssl = false; + private boolean sslSelfSigned = false; + private File sslCertFile = null; + private File sslKeyFile = null; + private NetworkProtocol[] protocols = {NetworkProtocol.TCP, NetworkProtocol.UDP}; + private MessageCacheMode cacheMode = MessageCacheMode.TCP_ENABLED; + + /** + * Connection timeout in milliseconds used when client is unable connect to server + * Note: Currently it do not apply when server is "off" + */ + private int connectionTimeout = 10000; + + public void load(File file) { + //TODO + } + + public void load(String service) { + //TODO + } + + public void save() { + //TODO + } + + public void save(File file) { + //TODO + } + + public String getService() { + return service; + } + + public void setService(String service) { + this.service = service; + } + + public int getTcpPort() { + return tcpPort; + } + + public void setTcpPort(int tcpPort) { + this.tcpPort = tcpPort; + } + + public boolean isSsl() { + return ssl; + } + + public void setSsl(boolean ssl) { + this.ssl = ssl; + } + + public boolean isSslSelfSigned() { + return sslSelfSigned; + } + + public void setSslSelfSigned(boolean sslSelfSigned) { + this.sslSelfSigned = sslSelfSigned; + } + + public File getSslCertFile() { + return sslCertFile; + } + + public void setSslCertFile(File sslCertFile) { + this.sslCertFile = sslCertFile; + } + + public File getSslKeyFile() { + return sslKeyFile; + } + + public void setSslKeyFile(File sslKeyFile) { + this.sslKeyFile = sslKeyFile; + } + + public int getUdpPort() { + return udpPort; + } + + public void setUdpPort(int udpPort) { + this.udpPort = udpPort; + } + + public NetworkProtocol[] getProtocols() { + return protocols; + } + + public void setProtocols(NetworkProtocol[] protocols) { + this.protocols = protocols; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + /** + * @return The current message cache mode. + */ + public MessageCacheMode getCacheMode() { + return cacheMode; + } + + /** + * Sets the message cache mode. By default the mode is MessageCacheMode.ENABLE_TCP + * See MessageCacheMode for more information about the supported mode options. + * + * @param cacheMode The desired message cache mode. + */ + public void setCacheMode(MessageCacheMode cacheMode) { + this.cacheMode = cacheMode; + } + + /** + * @return The timeout in milliseconds for creating a new connection + */ + public int getConnectionTimeout() { + return connectionTimeout; + } + + /** + * Set the timeout duration in milliseconds for creating a new connection from the client to the server. + * This does not effect the read/write timeouts for messages after the connection has been established. + * + * @param connectionTimeout The timeout in milliseconds for creating a new connection. + */ + public void setConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } +} diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyConnection.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyConnection.java index 7f91c8c..c3a6a0c 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyConnection.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyConnection.java @@ -27,12 +27,8 @@ of this software and associated documentation files (the "Software"), to deal import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.socket.SocketChannel; -import io.tlf.monkeynetty.ConnectionListener; -import io.tlf.monkeynetty.NetworkClient; -import io.tlf.monkeynetty.NetworkServer; -import io.tlf.monkeynetty.MessageListener; +import io.tlf.monkeynetty.*; import io.tlf.monkeynetty.msg.NetworkMessage; -import io.tlf.monkeynetty.NetworkProtocol; import java.nio.channels.ClosedChannelException; import java.util.Collections; @@ -42,23 +38,22 @@ of this software and associated documentation files (the "Software"), to deal import java.util.logging.Level; import java.util.logging.Logger; -import static io.netty.channel.ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE; - /** * @author Trevor Flynn trevorflynn@liquidcrystalstudios.com */ -public class NettyConnection implements NetworkClient { +public class NettyConnection implements NetworkConnection { private final static Logger LOGGER = Logger.getLogger(NettyConnection.class.getName()); + private final HashSet handlers = new HashSet<>(); + private final Object handlerLock = new Object(); + private final Set listeners = Collections.synchronizedSet(new HashSet<>()); + private final HashMap atts = new HashMap<>(); + private final NetworkServer server; + private SocketChannel tcpConn; private UdpChannel udpConn; - private final NetworkServer server; - private boolean connected = false; - private final HashSet handlers = new HashSet<>(); - private final Object handlerLock = new Object(); - private final Set listeners = Collections.synchronizedSet(new HashSet<>()); - private final HashMap atts = new HashMap<>(); + private boolean connected = false; public NettyConnection(NetworkServer server) { this.server = server; @@ -78,7 +73,7 @@ public void setTcp(SocketChannel conn) { * of running all connection listeners. */ protected void connect() { - for (ConnectionListener listener : listeners) { + for (ServerConnectionListener listener : listeners) { listener.onConnect(this); } connected = true; @@ -97,7 +92,7 @@ public boolean isConnected() { @Override public void disconnect() { - for (ConnectionListener listener : listeners) { + for (ServerConnectionListener listener : listeners) { listener.onDisconnect(this); } //Close connection @@ -106,8 +101,13 @@ public void disconnect() { } @Override - public int getPort() { - return server.getPort(); + public int getTcpPort() { + return server.getSettings().getTcpPort(); + } + + @Override + public int getUdpPort() { + return server.getSettings().getUdpPort(); } public NetworkServer getServer() { @@ -116,16 +116,21 @@ public NetworkServer getServer() { @Override public String getService() { - return server.getService(); + return server.getSettings().getService(); } public boolean isSsl() { - return server.isSsl(); + return server.getSettings().isSsl(); + } + + @Override + public String getAddress() { + return getUserData("address"); } @Override public NetworkProtocol[] getProtocol() { - return server.getProtocol(); + return server.getSettings().getProtocols(); } @Override @@ -156,7 +161,7 @@ public void send(NetworkMessage message) { public void receive(NetworkMessage message) { //Handlers synchronized (handlerLock) { - for (MessageListener handler : handlers) { + for (ServerMessageListener handler : handlers) { for (Class a : handler.getSupportedMessages()) { if (a.isInstance(message)) { handler.onMessage(message, null, this); @@ -166,11 +171,6 @@ public void receive(NetworkMessage message) { } } - @Override - public String getAddress() { - return getUserData("address").toString(); - } - @Override public void setUserData(String key, Object obj) { atts.put(key, obj); @@ -180,28 +180,28 @@ public void setUserData(String key, Object obj) { public T getUserData(String key) { return (T) atts.get(key); } - + @Override - public void registerListener(MessageListener handler) { + public void registerListener(ServerMessageListener handler) { synchronized (handlerLock) { handlers.add(handler); } } @Override - public void unregisterListener(MessageListener handler) { + public void unregisterListener(ServerMessageListener handler) { synchronized (handlerLock) { handlers.remove(handler); } } @Override - public void registerListener(ConnectionListener listener) { + public void registerListener(ServerConnectionListener listener) { listeners.add(listener); } @Override - public void unregisterListener(ConnectionListener listener) { + public void unregisterListener(ServerConnectionListener listener) { listeners.remove(listener); } } diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyServer.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyServer.java index e39d014..de5a437 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyServer.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NettyServer.java @@ -41,12 +41,12 @@ import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateHandler; import io.tlf.monkeynetty.*; +import io.tlf.monkeynetty.client.NetworkClient; import io.tlf.monkeynetty.msg.ConnectionEstablishedMessage; import io.tlf.monkeynetty.msg.NetworkMessage; import io.tlf.monkeynetty.msg.PingMessage; import io.tlf.monkeynetty.msg.UdpConHashMessage; -import java.io.File; import java.security.SecureRandom; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -56,15 +56,14 @@ public class NettyServer extends BaseAppState implements NetworkServer { private final static Logger LOGGER = Logger.getLogger(NettyServer.class.getName()); - private final Set messageListeners = ConcurrentHashMap.newKeySet(); - private final Set connectionListeners = ConcurrentHashMap.newKeySet(); + private final Set messageListeners = ConcurrentHashMap.newKeySet(); + private final Set connectionListeners = ConcurrentHashMap.newKeySet(); private final Map tcpClients = new ConcurrentHashMap<>(); private final Map udpClients = new ConcurrentHashMap<>(); private final Map secrets = new ConcurrentHashMap<>(); - private final Set pendingConnections = ConcurrentHashMap.newKeySet(); + private final Set pendingConnections = ConcurrentHashMap.newKeySet(); - private int maxConnections = 10; - private boolean blocking = false; + protected final NetworkServerSettings settings; private LogLevel logLevel; //Netty objects @@ -77,72 +76,17 @@ public class NettyServer extends BaseAppState implements NetworkServer { private ServerBootstrap udpServer; private ChannelFuture udpFuture; private SslContext sslContext; + private LoggingHandler tcpLogger; + private LoggingHandler udpLogger; - private final String service; - private final int port; - private boolean ssl; - private boolean selfGenCert; - private File cert; - private File key; /** * Create a new UDP/TCP server. - * The server will be created without SSL * - * @param service The name of the service the server is running - * @param port The port the TCP/UDP server will listen on + * @param settings The server settings to use when creating the server */ - public NettyServer(String service, int port) { - this(service, false, port); - } - - /** - * Create a new UDP/TCP server. - * If ssl is enabled, the TCP server will generate a self signed certificate and use ssl. - * - * @param service The name of the service the server is running - * @param ssl If ssl should be used on the TCP server - * @param port The port the TCP/UDP server will listen on - */ - public NettyServer(String service, boolean ssl, int port) { - this(service, ssl, true, null, null, port); - } - - /** - * Create a new UDP/TCP server. - * If ssl is enabled, and a certificate key pair are provided, the TCP server will use ssl. - * If the server failes to load the cert key pair, or they are null, it will fail back to non-ssl. - * - * @param service The name of the service the server is running - * @param ssl If ssl should be used on the TCP server - * @param cert The certificate file, or null - * @param key The certificate key, or null - * @param port The port the TCP/UDP server will listen on - */ - public NettyServer(String service, boolean ssl, File cert, File key, int port) { - this(service, ssl, false, cert, key, port); - } - - /** - * Create a new UDP/TCP server. - * If ssl is enabled, and a certificate key pair are provided, the TCP server will use ssl. - * If the server failes to load the cert key pair, or they are null, it will fail back to - * a self signed certificate if enabled, otherwise will fail back to non-ssl. - * - * @param service The name of the service the server is running - * @param ssl If ssl should be used on the TCP server - * @param selfGenCert If a self signed certificate can be used. - * @param cert The certificate file, or null to use self signed cert - * @param key The certificate key, or null to use self signed cert - * @param port The port the TCP/UDP server will listen on - */ - private NettyServer(String service, boolean ssl, boolean selfGenCert, File cert, File key, int port) { - this.service = service; - this.port = port; - this.ssl = ssl; - this.cert = cert; - this.key = key; - this.selfGenCert = selfGenCert; + public NettyServer(NetworkServerSettings settings) { + this.settings = settings; } @Override @@ -157,15 +101,17 @@ protected void cleanup(Application app) { @Override public void onEnable() { - LOGGER.log(Level.INFO, "Loading Netty.IO Server {0} on port {1,number,#}", new Object[]{getService(), getPort()}); + LOGGER.log(Level.INFO, "Loading Netty.IO Server {0} on tcp port {1,number,#} and udp port {2,number,#}", + new Object[]{settings.getService(), settings.getTcpPort(), settings.getUdpPort()}); setupTcp(); setupUdp(); - LOGGER.log(Level.INFO, "Server {0} running on port {1,number,#}", new Object[]{getService(), getPort()}); + LOGGER.log(Level.INFO, "Server {0} running on tcp port {1,number,#} and udp port {2,number,#}", + new Object[]{settings.getService(), settings.getTcpPort(), settings.getUdpPort()}); } @Override public void onDisable() { - LOGGER.log(Level.INFO, "Unloading Netty.IO Server {0} on port {1,number,#}", new Object[]{getService(), getPort()}); + LOGGER.log(Level.INFO, "Unloading Netty.IO Server {0} on tcp port {1,number,#} and udp port {2,number,#}", new Object[]{settings.getService(), settings.getTcpPort(), settings.getUdpPort()}); try { tcpConGroup.shutdownGracefully(); @@ -178,7 +124,7 @@ public void onDisable() { LOGGER.log(Level.SEVERE, "Failed to stop server", ex); } - LOGGER.log(Level.INFO, "Server {0} stopped on port {1,number,#}", new Object[]{getService(), getPort()}); + LOGGER.log(Level.INFO, "Server {0} stopped on tcp port {1,number,#} and udp port {2,number,#}", new Object[]{settings.getService(), settings.getTcpPort(), settings.getUdpPort()}); } @Override @@ -187,23 +133,8 @@ public int getConnections() { } @Override - public boolean isBlocking() { - return blocking; - } - - @Override - public void setBlocking(boolean blocking) { - this.blocking = blocking; - } - - @Override - public int getMaxConnections() { - return maxConnections; - } - - @Override - public void setMaxConnections(int maxConnections) { - this.maxConnections = maxConnections; + public NetworkServerSettings getSettings() { + return settings; } /** @@ -211,10 +142,11 @@ public void setMaxConnections(int maxConnections) { * Process an incoming client connection. * Will handle max connections and blocking mode. * Will fire connection listeners. + * * @param client The client making the connection */ - private void receive(NetworkClient client) { - if (isBlocking() || getConnections() >= getMaxConnections() || !(client instanceof NettyConnection)) { + private void receive(NetworkConnection client) { + if (settings.isBlocking() || getConnections() >= settings.getMaxConnections() || !(client instanceof NettyConnection)) { client.disconnect(); LOGGER.log(Level.INFO, "Server rejected connection from {0}", client.getAddress()); } else { @@ -223,7 +155,7 @@ private void receive(NetworkClient client) { ((NettyConnection) client).connect(); LOGGER.log(Level.INFO, "Connection received from {0}", client.getAddress()); try { - for (ConnectionListener listener : connectionListeners) { + for (ServerConnectionListener listener : connectionListeners) { listener.onConnect(client); } client.send(new ConnectionEstablishedMessage()); @@ -247,12 +179,12 @@ private void receive(NetworkClient client) { * Process an incoming message from a client. * Will notify message listeners. * - * @param client The client the message was from + * @param client The client the message was from * @param message The message sent */ - private void receive(NetworkClient client, NetworkMessage message) { + private void receive(NetworkConnection client, NetworkMessage message) { client.receive(message); - for (MessageListener handler : messageListeners) { + for (ServerMessageListener handler : messageListeners) { for (Class a : handler.getSupportedMessages()) { if (a.isInstance(message)) { try { @@ -276,29 +208,10 @@ public void send(NetworkMessage message, NetworkClient client) { client.send(message); } - @Override - public int getPort() { - return port; - } - - @Override - public String getService() { - return service; - } - - @Override - public boolean isSsl() { - return ssl; - } - - @Override - public NetworkProtocol[] getProtocol() { - return new NetworkProtocol[]{NetworkProtocol.UDP, NetworkProtocol.TCP}; - } - /** * Sets the Netty.IO internal log level. * This will not change the java.util.logger Logger for Monkey-Netty. + * Changing the log level will not take affect if the server is running. * @param logLevel The internal Netty.IO log level */ public void setLogLevel(LogLevel logLevel) { @@ -323,18 +236,18 @@ public LogLevel getLogLevel() { */ private void setupTcp() { //Setup ssl - if (ssl) { + if (settings.isSsl()) { try { - if (selfGenCert) { + if (settings.isSslSelfSigned()) { LOGGER.log(Level.WARNING, "No SSL cert or key provided, using self signed certificate"); SelfSignedCertificate ssc = new SelfSignedCertificate(); - cert = ssc.certificate(); - key = ssc.privateKey(); + settings.setSslCertFile(ssc.certificate()); + settings.setSslKeyFile(ssc.privateKey()); } - sslContext = SslContextBuilder.forServer(cert, key).build(); + sslContext = SslContextBuilder.forServer(settings.getSslCertFile(), settings.getSslKeyFile()).build(); } catch (Exception ex) { LOGGER.log(Level.WARNING, "Failed to load ssl, failing back to no ssl", ex); - ssl = false; + settings.setSsl(false); } } //Setup tcp socket @@ -375,7 +288,7 @@ public void initChannel(SocketChannel ch) { tcpClients.remove(future.channel()); try { - for (ConnectionListener listener : connectionListeners) { + for (ServerConnectionListener listener : connectionListeners) { listener.onDisconnect(client); } } catch (Exception ex) { @@ -384,13 +297,14 @@ public void initChannel(SocketChannel ch) { }); //Setup ssl - if (ssl) { + if (settings.isSsl()) { p.addLast(sslContext.newHandler(ch.alloc())); } //Setup pipeline if (logLevel != null) { - p.addLast(new LoggingHandler(logLevel)); + tcpLogger = new LoggingHandler(logLevel); + p.addLast(tcpLogger); } p.addLast( new NetworkMessageEncoder(), @@ -442,7 +356,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { receive(client); } }); - tcpFuture = tcpServer.bind(port).sync(); + tcpFuture = tcpServer.bind(settings.getTcpPort()).sync(); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "Outside TCP server crash", ex); } @@ -475,7 +389,8 @@ public void initChannel(UdpChannel ch) { //Setup pipeline if (logLevel != null) { - p.addLast(new LoggingHandler(logLevel)); + udpLogger = new LoggingHandler(logLevel); + p.addLast(udpLogger); } p.addLast( new NetworkMessageEncoder(), @@ -524,7 +439,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { }); } }); - udpFuture = udpServer.bind(port).sync(); + udpFuture = udpServer.bind(settings.getUdpPort()).sync(); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "Outside UDP server crash", ex); } @@ -533,6 +448,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { /** * Internal use only * Catch a network error. This will cause the error to be sent to the logger. + * * @param cause The error to catch */ private void catchNetworkError(Throwable cause) { @@ -543,22 +459,22 @@ private void catchNetworkError(Throwable cause) { } @Override - public void registerListener(MessageListener handler) { + public void registerListener(ServerMessageListener handler) { messageListeners.add(handler); } @Override - public void unregisterListener(MessageListener handler) { + public void unregisterListener(ServerMessageListener handler) { messageListeners.remove(handler); } @Override - public void registerListener(ConnectionListener listener) { + public void registerListener(ServerConnectionListener listener) { connectionListeners.add(listener); } @Override - public void unregisterListener(ConnectionListener listener) { + public void unregisterListener(ServerConnectionListener listener) { connectionListeners.remove(listener); } diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/NetworkClient.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkConnection.java similarity index 81% rename from monkey-netty/src/main/java/io/tlf/monkeynetty/NetworkClient.java rename to monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkConnection.java index bd08f13..b153c43 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/NetworkClient.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkConnection.java @@ -21,42 +21,25 @@ of this software and associated documentation files (the "Software"), to deal OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package io.tlf.monkeynetty; +package io.tlf.monkeynetty.server; +import io.tlf.monkeynetty.NetworkProtocol; import io.tlf.monkeynetty.msg.NetworkMessage; +import io.tlf.monkeynetty.server.ServerConnectionListener; +import io.tlf.monkeynetty.server.ServerMessageListener; /** * @author Trevor Flynn trevorflynn@liquidcrystalstudios.com *

- * Used as a basis for building a custom network client. + * Used as a basis for building a custom network connection from a server. */ -public interface NetworkClient { +public interface NetworkConnection { /** * @return if the client is connected to the server */ public boolean isConnected(); - /** - * Send a message from the client to the server - * - * @param message The message to send - */ - public void send(NetworkMessage message); - - /** - * Internal Use Only - * Called by the server when the server receives a message for the server side connection client. - * - * @param message The message received - */ - public void receive(NetworkMessage message); - - /** - * Disconnects the client from the server - */ - public void disconnect(); - /** * @return If the client is connected to the server using SSL */ @@ -68,9 +51,14 @@ public interface NetworkClient { public String getAddress(); /** - * @return The port of the server the client is connecting to + * @return The tcp port of the server the client is connecting to + */ + public int getTcpPort(); + + /** + * @return The udp port of the server the client is connecting to */ - public int getPort(); + public int getUdpPort(); /** * The service string should be unique to each server. @@ -85,34 +73,54 @@ public interface NetworkClient { */ public NetworkProtocol[] getProtocol(); + /** + * Send a message from the client to the server + * + * @param message The message to send + */ + public void send(NetworkMessage message); + + /** + * Internal Use Only + * Called by the server when the server receives a message for the server side connection client. + * + * @param message The message received + */ + public void receive(NetworkMessage message); + + /** + * Disconnects the client from the server + */ + public void disconnect(); + /** * Register a message listener with the client. * * @param handler The message listener to register */ - public void registerListener(MessageListener handler); + public void registerListener(ServerMessageListener handler); /** * Unregister a message listener with the client. * * @param handler The message listener to unregister */ - public void unregisterListener(MessageListener handler); + public void unregisterListener(ServerMessageListener handler); /** * Register a connection listener with the client. * * @param listener The connection listener to register */ - public void registerListener(ConnectionListener listener); + public void registerListener(ServerConnectionListener listener); /** * Unregister a connection listener with the client. * * @param listener The connection listener to unregister */ - public void unregisterListener(ConnectionListener listener); - + public void unregisterListener(ServerConnectionListener listener); + /** * Set obj value attribute for key param * diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/NetworkServer.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkServer.java similarity index 55% rename from monkey-netty/src/main/java/io/tlf/monkeynetty/NetworkServer.java rename to monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkServer.java index 9e718b5..c2a8ef8 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/NetworkServer.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkServer.java @@ -22,8 +22,9 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. */ -package io.tlf.monkeynetty; +package io.tlf.monkeynetty.server; +import io.tlf.monkeynetty.client.NetworkClient; import io.tlf.monkeynetty.msg.NetworkMessage; /** @@ -38,57 +39,12 @@ public interface NetworkServer { */ public int getConnections(); - /** - * @return The port number the server is listening on - */ - public int getPort(); - - /** - * @return If the server is using SSL for TCP transport - */ - public boolean isSsl(); - - /** - * The service string should be unique to each server. - * This is entirely for user use, and is not validated between server and client. - * - * @return The service the server is running. - */ - public String getService(); - - /** - * @return Which network protocols the server supports - */ - public NetworkProtocol[] getProtocol(); - - /** - * @return true if the server is currently blocking new connections - */ - public boolean isBlocking(); /** - * Set if the server should block incoming connections. If true - * the server will close all incoming connections immediately without - * performing a handshake after establishing the connection. - * - * @param blocking If the server should block incoming connections. + * @return The settings of the server */ - public void setBlocking(boolean blocking); + public NetworkServerSettings getSettings(); - /** - * @return The maximum number of connections the server will allow. - */ - public int getMaxConnections(); - - /** - * This sets the maximum number of connections the server will be allowed to have at any given time. - * If a connection is attempted to the server and the server currently has the maximum number of - * connections, it will immediately close the connection without performing a handshake after establishing - * the connection. - * - * @param maxConnections The maximum number of connections the server will allow. - */ - public void setMaxConnections(int maxConnections); /** * Send a message to all clients connected to the server. @@ -110,26 +66,26 @@ public interface NetworkServer { * * @param handler The message listener to register */ - public void registerListener(MessageListener handler); + public void registerListener(ServerMessageListener handler); /** * Unregister a message listener with the server. * * @param handler The message listener to unregister */ - public void unregisterListener(MessageListener handler); + public void unregisterListener(ServerMessageListener handler); /** * Register a connection listener with the server. * * @param listener The connection listener to register */ - public void registerListener(ConnectionListener listener); + public void registerListener(ServerConnectionListener listener); /** * Unregister a connection listener with the server. * * @param listener The connection listener to unregister */ - public void unregisterListener(ConnectionListener listener); + public void unregisterListener(ServerConnectionListener listener); } diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkServerSettings.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkServerSettings.java new file mode 100644 index 0000000..1fb74b5 --- /dev/null +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/NetworkServerSettings.java @@ -0,0 +1,115 @@ +package io.tlf.monkeynetty.server; + +import io.tlf.monkeynetty.NetworkProtocol; + +import java.io.File; + +public class NetworkServerSettings { + + private String service = "monkey-netty"; + private int tcpPort = 13900; + private int udpPort = 13900; + private boolean ssl = false; + private boolean sslSelfSigned = false; + private File sslCertFile = null; + private File sslKeyFile = null; + private int maxConnections = 10; + private boolean blocking = false; + private NetworkProtocol[] protocols = {NetworkProtocol.TCP, NetworkProtocol.UDP}; + + public void load(File file) { + //TODO + } + + public void load(String service) { + //TODO + } + + public void save() { + //TODO + } + + public void save(File file) { + //TODO + } + + public String getService() { + return service; + } + + public void setService(String service) { + this.service = service; + } + + public int getTcpPort() { + return tcpPort; + } + + public void setTcpPort(int tcpPort) { + this.tcpPort = tcpPort; + } + + public boolean isSsl() { + return ssl; + } + + public void setSsl(boolean ssl) { + this.ssl = ssl; + } + + public boolean isSslSelfSigned() { + return sslSelfSigned; + } + + public void setSslSelfSigned(boolean sslSelfSigned) { + this.sslSelfSigned = sslSelfSigned; + } + + public File getSslCertFile() { + return sslCertFile; + } + + public void setSslCertFile(File sslCertFile) { + this.sslCertFile = sslCertFile; + } + + public File getSslKeyFile() { + return sslKeyFile; + } + + public void setSslKeyFile(File sslKeyFile) { + this.sslKeyFile = sslKeyFile; + } + + public int getMaxConnections() { + return maxConnections; + } + + public void setMaxConnections(int maxConnections) { + this.maxConnections = maxConnections; + } + + public boolean isBlocking() { + return blocking; + } + + public void setBlocking(boolean blocking) { + this.blocking = blocking; + } + + public int getUdpPort() { + return udpPort; + } + + public void setUdpPort(int udpPort) { + this.udpPort = udpPort; + } + + public NetworkProtocol[] getProtocols() { + return protocols; + } + + public void setProtocols(NetworkProtocol[] protocols) { + this.protocols = protocols; + } +} diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/server/ServerConnectionListener.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/ServerConnectionListener.java new file mode 100644 index 0000000..80be30b --- /dev/null +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/ServerConnectionListener.java @@ -0,0 +1,45 @@ +/* +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.server; + +/** + * @author Trevor Flynn trevorflynn@liquidcrystalstudios.com + */ +public interface ServerConnectionListener { + + /** + * Called when a client is connected + * + * @param connection The client connection to the server. + */ + public void onConnect(NetworkConnection connection); + + /** + * Called when a client disconnects + * + * @param connection The client connection to the server. + */ + public void onDisconnect(NetworkConnection connection); +} diff --git a/monkey-netty/src/main/java/io/tlf/monkeynetty/MessageListener.java b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/ServerMessageListener.java similarity index 91% rename from monkey-netty/src/main/java/io/tlf/monkeynetty/MessageListener.java rename to monkey-netty/src/main/java/io/tlf/monkeynetty/server/ServerMessageListener.java index e72d893..38f8d57 100644 --- a/monkey-netty/src/main/java/io/tlf/monkeynetty/MessageListener.java +++ b/monkey-netty/src/main/java/io/tlf/monkeynetty/server/ServerMessageListener.java @@ -22,14 +22,14 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. */ -package io.tlf.monkeynetty; +package io.tlf.monkeynetty.server; import io.tlf.monkeynetty.msg.NetworkMessage; /** * @author Trevor Flynn trevorflynn@liquidcrystalstudios.com */ -public interface MessageListener { +public interface ServerMessageListener { /** * When the server/client receives a message, this will be called. @@ -37,9 +37,9 @@ public interface MessageListener { * * @param msg The message received * @param server The server that sent the message, will be null on client side application - * @param client The client that received the message + * @param connection The client that received the message */ - public void onMessage(NetworkMessage msg, NetworkServer server, NetworkClient client); + public void onMessage(NetworkMessage msg, NetworkServer server, NetworkConnection connection); /** * The listener onMessage will only get called if the message received