OSDN Git Service

Android Gdx.net backend implemented for sockets (code duplication from LWJGL backend...
authorChristoph Aschwanden <contact@noblemaster.com>
Tue, 2 Oct 2012 02:53:13 +0000 (11:53 +0900)
committerChristoph Aschwanden <contact@noblemaster.com>
Tue, 2 Oct 2012 02:53:13 +0000 (11:53 +0900)
backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidNet.java
backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidServerSocket.java [new file with mode: 0644]
backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSocket.java [new file with mode: 0644]
backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglNet.java
gdx/src/com/badlogic/gdx/net/ServerSocketHints.java

index 9773661..8702cca 100755 (executable)
@@ -25,6 +25,10 @@ import com.badlogic.gdx.net.Socket;
 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
@@ -37,11 +41,11 @@ public class AndroidNet implements Net {
 \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
diff --git a/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidServerSocket.java b/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidServerSocket.java
new file mode 100644 (file)
index 0000000..f333598
--- /dev/null
@@ -0,0 +1,84 @@
+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);
+                       }
+               }
+       }
+}
diff --git a/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSocket.java b/backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidSocket.java
new file mode 100644 (file)
index 0000000..5082847
--- /dev/null
@@ -0,0 +1,110 @@
+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);
+                       }
+               }
+       }
+}
index e2355d1..faca58e 100755 (executable)
@@ -24,6 +24,9 @@ import com.badlogic.gdx.net.SocketHints;
 \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
index b979321..1403ff8 100755 (executable)
@@ -23,7 +23,12 @@ package com.badlogic.gdx.net;
  */\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
@@ -38,7 +43,9 @@ public class ServerSocketHints {
         * 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