import com.badlogic.gdx.net.SocketHints;\r
\r
public class AndroidNet implements Net {\r
+ \r
+ // IMPORTANT: The Gdx.net classes are a currently duplicated for LWJGL + Android!\r
+ // If you make changes here, make changes in the other backend as well.\r
+\r
@Override\r
public HttpResult httpGet (String url, String... parameters) {\r
throw new UnsupportedOperationException("Not implemented");\r
\r
@Override\r
public ServerSocket newServerSocket (Protocol protocol, int port, ServerSocketHints hints) {\r
- throw new UnsupportedOperationException("Not implemented");\r
+ return new AndroidServerSocket(protocol, port, hints);\r
}\r
\r
@Override\r
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {\r
- throw new UnsupportedOperationException("Not implemented");\r
+ return new AndroidSocket(protocol, host, port, hints);\r
}\r
}\r
--- /dev/null
+package com.badlogic.gdx.backends.android;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+
+import com.badlogic.gdx.Net.Protocol;
+import com.badlogic.gdx.net.ServerSocket;
+import com.badlogic.gdx.net.ServerSocketHints;
+import com.badlogic.gdx.net.Socket;
+import com.badlogic.gdx.net.SocketHints;
+import com.badlogic.gdx.utils.GdxRuntimeException;
+
+/**
+ * Server socket implementation using java.net.ServerSocket.
+ *
+ * @author noblemaster
+ */
+public class AndroidServerSocket implements ServerSocket {
+
+ private Protocol protocol;
+
+ /** Our server or null for disposed, aka closed. */
+ private java.net.ServerSocket server;
+
+
+ public AndroidServerSocket(Protocol protocol, int port, ServerSocketHints hints) {
+ this.protocol = protocol;
+
+ // create the server socket
+ try {
+ // initialize
+ server = new java.net.ServerSocket();
+ if (hints != null) {
+ server.setPerformancePreferences(hints.performancePrefConnectionTime,
+ hints.performancePrefLatency,
+ hints.performancePrefBandwidth);
+ server.setReuseAddress(hints.reuseAddress);
+ server.setSoTimeout(hints.acceptTimeout);
+ server.setReceiveBufferSize(hints.receiveBufferSize);
+ }
+
+ // and bind the server...
+ InetSocketAddress address = new InetSocketAddress(port);
+ if (hints != null) {
+ server.bind(address, hints.backlog);
+ }
+ else {
+ server.bind(address);
+ }
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Cannot create a server socket at port " + port + ".", e);
+ }
+ }
+
+ @Override
+ public Protocol getProtocol () {
+ return protocol;
+ }
+
+ @Override
+ public Socket accept(SocketHints hints) {
+ try {
+ return new AndroidSocket(server.accept(), hints);
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error accepting socket.", e);
+ }
+ }
+
+ @Override
+ public void dispose() {
+ if (server != null) {
+ try {
+ server.close();
+ server = null;
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error closing server.", e);
+ }
+ }
+ }
+}
--- /dev/null
+package com.badlogic.gdx.backends.android;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+
+import com.badlogic.gdx.Net.Protocol;
+import com.badlogic.gdx.net.ServerSocketHints;
+import com.badlogic.gdx.net.Socket;
+import com.badlogic.gdx.net.SocketHints;
+import com.badlogic.gdx.utils.GdxRuntimeException;
+
+/**
+ * Socket implementation using java.net.Socket.
+ *
+ * @author noblemaster
+ */
+public class AndroidSocket implements Socket {
+
+ /** Our socket or null for disposed, aka closed. */
+ private java.net.Socket socket;
+
+
+ public AndroidSocket(Protocol protocol, String host, int port, SocketHints hints) {
+ try {
+ // create the socket
+ socket = new java.net.Socket();
+ applyHints(hints); // better to call BEFORE socket is connected!
+
+ // and connect...
+ InetSocketAddress address = new InetSocketAddress(host, port);
+ if (hints != null) {
+ socket.connect(address, hints.connectTimeout);
+ }
+ else {
+ socket.connect(address);
+ }
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error making a socket connection to " + host + ":" + port, e);
+ }
+ }
+
+ public AndroidSocket(java.net.Socket socket, SocketHints hints) {
+ this.socket = socket;
+ applyHints(hints);
+ }
+
+ private void applyHints(SocketHints hints) {
+ if (hints != null) {
+ try {
+ socket.setPerformancePreferences(hints.performancePrefConnectionTime,
+ hints.performancePrefLatency,
+ hints.performancePrefBandwidth);
+ socket.setTrafficClass(hints.trafficClass);
+ socket.setTcpNoDelay(hints.tcpNoDelay);
+ socket.setKeepAlive(hints.keepAlive);
+ socket.setSendBufferSize(hints.sendBufferSize);
+ socket.setReceiveBufferSize(hints.receiveBufferSize);
+ socket.setSoLinger(hints.linger, hints.lingerDuration);
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error setting socket hints." , e);
+ }
+ }
+ }
+
+ @Override
+ public boolean isConnected () {
+ if (socket != null) {
+ return socket.isConnected();
+ }
+ else {
+ return false;
+ }
+ }
+
+ @Override
+ public InputStream getInputStream () {
+ try {
+ return socket.getInputStream();
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error getting input stream from socket.", e);
+ }
+ }
+
+ @Override
+ public OutputStream getOutputStream () {
+ try {
+ return socket.getOutputStream();
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error getting output stream from socket.", e);
+ }
+ }
+
+ @Override
+ public void dispose() {
+ if (socket != null) {
+ try {
+ socket.close();
+ socket = null;
+ }
+ catch (Exception e) {
+ throw new GdxRuntimeException("Error closing socket.", e);
+ }
+ }
+ }
+}
\r
public class LwjglNet implements Net {\r
\r
+ // IMPORTANT: The Gdx.net classes are a currently duplicated for LWJGL + Android!\r
+ // If you make changes here, make changes in the other backend as well.\r
+ \r
@Override\r
public HttpResult httpGet (String url, String... parameters) {\r
throw new UnsupportedOperationException("Not implemented");\r
*/\r
public class ServerSocketHints {\r
\r
- /** The listen backlog length. Needs to be greater than 0, otherwise the default is used. */\r
+ /** \r
+ * The listen backlog length. Needs to be greater than 0, otherwise the system default is used. \r
+ * backlog is the maximum queue length for incoming connection, i.e. maximum number of connections\r
+ * waiting for accept(...). If a connection indication arrives when the queue \r
+ * is full, the connection is refused. \r
+ */\r
public int backlog = 16;\r
\r
/**\r
* time, then it could invoke this method with the values (0, 1, 2).\r
*/\r
public int performancePrefConnectionTime = 0;\r
+ /** See performancePrefConnectionTime for details. */\r
public int performancePrefLatency = 1; // low latency\r
+ /** See performancePrefConnectionTime for details. */\r
public int performancePrefBandwidth = 0;\r
/** Enable/disable the SO_REUSEADDR socket option. */\r
public boolean reuseAddress = true;\r