/external/jmonkeyengine/engine/src/networking/com/jme3/network/Network.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 192 lines · 75 code · 19 blank · 98 comment · 2 complexity · aaa4dbeb5b22094f31de08469a3fd2d1 MD5 · raw file

  1. /*
  2. * Copyright (c) 2011 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.network;
  33. import com.jme3.network.base.DefaultClient;
  34. import com.jme3.network.base.DefaultServer;
  35. import com.jme3.network.base.TcpConnectorFactory;
  36. import com.jme3.network.kernel.tcp.SelectorKernel;
  37. import com.jme3.network.kernel.tcp.SocketConnector;
  38. import com.jme3.network.kernel.udp.UdpConnector;
  39. import com.jme3.network.kernel.udp.UdpKernel;
  40. import java.io.IOException;
  41. import java.net.InetAddress;
  42. /**
  43. * The main service provider for conveniently creating
  44. * server and client instances.
  45. *
  46. * @version $Revision: 8979 $
  47. * @author Paul Speed
  48. */
  49. public class Network
  50. {
  51. public static final String DEFAULT_GAME_NAME = "Unnamed jME3 Game";
  52. public static final int DEFAULT_VERSION = 42;
  53. /**
  54. * Creates a Server that will utilize both reliable and fast
  55. * transports to communicate with clients. The specified port
  56. * will be used for both TCP and UDP communication.
  57. */
  58. public static Server createServer( int port ) throws IOException
  59. {
  60. return createServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, port, port );
  61. }
  62. /**
  63. * Creates a Server that will utilize both reliable and fast
  64. * transports to communicate with clients. The specified port
  65. * will be used for both TCP and UDP communication.
  66. */
  67. public static Server createServer( int tcpPort, int udpPort ) throws IOException
  68. {
  69. return createServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, tcpPort, udpPort );
  70. }
  71. /**
  72. * Creates a named and versioned Server that will utilize both reliable and fast
  73. * transports to communicate with clients. The specified port
  74. * will be used for both TCP and UDP communication.
  75. *
  76. * @param gameName This is the name that identifies the game. Connecting clients
  77. * must use this name or be turned away.
  78. * @param version This is a game-specific verison that helps detect when out-of-date
  79. * clients have connected to an incompatible server.
  80. * @param tcpPort The port upon which the TCP hosting will listen for new connections.
  81. * @param udpPort The port upon which the UDP hosting will listen for new 'fast' UDP
  82. * messages. Set to -1 if 'fast' traffic should go over TCP. This will
  83. * completely disable UDP traffic for this server.
  84. */
  85. public static Server createServer( String gameName, int version, int tcpPort, int udpPort ) throws IOException
  86. {
  87. UdpKernel fast = udpPort == -1 ? null : new UdpKernel(udpPort);
  88. SelectorKernel reliable = new SelectorKernel(tcpPort);
  89. return new DefaultServer( gameName, version, reliable, fast );
  90. }
  91. /**
  92. * Creates a client that can be connected at a later time.
  93. */
  94. public static NetworkClient createClient()
  95. {
  96. return createClient( DEFAULT_GAME_NAME, DEFAULT_VERSION );
  97. }
  98. /**
  99. * Creates a client that can be connected at a later time. The specified
  100. * game name and version must match the server or the client will be turned
  101. * away.
  102. */
  103. public static NetworkClient createClient( String gameName, int version )
  104. {
  105. return new NetworkClientImpl(gameName, version);
  106. }
  107. /**
  108. * Creates a Client that communicates with the specified host and port
  109. * using both reliable and fast transports.
  110. */
  111. public static Client connectToServer( String host, int hostPort ) throws IOException
  112. {
  113. return connectToServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, host, hostPort, hostPort );
  114. }
  115. /**
  116. * Creates a Client that communicates with the specified host and separate TCP and UDP ports
  117. * using both reliable and fast transports.
  118. */
  119. public static Client connectToServer( String host, int hostPort, int remoteUdpPort ) throws IOException
  120. {
  121. return connectToServer( DEFAULT_GAME_NAME, DEFAULT_VERSION, host, hostPort, remoteUdpPort );
  122. }
  123. /**
  124. * Creates a Client that communicates with the specified host and port
  125. * using both reliable and fast transports.
  126. */
  127. public static Client connectToServer( String gameName, int version,
  128. String host, int hostPort ) throws IOException
  129. {
  130. return connectToServer( gameName, version, host, hostPort, hostPort );
  131. }
  132. /**
  133. * Creates a Client that communicates with the specified host and and separate TCP and UDP ports
  134. * using both reliable and fast transports.
  135. *
  136. * @param gameName This is the name that identifies the game. This must match
  137. * the target server's name or this client will be turned away.
  138. * @param version This is a game-specific verison that helps detect when out-of-date
  139. * clients have connected to an incompatible server. This must match
  140. * the server's version of this client will be turned away.
  141. * @param hostPort The remote TCP port on the server to which this client should
  142. * send reliable messages.
  143. * @param remoteUdpPort The remote UDP port on the server to which this client should
  144. * send 'fast'/unreliable messages. Set to -1 if 'fast' traffic should
  145. * go over TCP. This will completely disable UDP traffic for this
  146. * client.
  147. */
  148. public static Client connectToServer( String gameName, int version,
  149. String host, int hostPort, int remoteUdpPort ) throws IOException
  150. {
  151. InetAddress remoteAddress = InetAddress.getByName(host);
  152. UdpConnector fast = remoteUdpPort == -1 ? null : new UdpConnector( remoteAddress, remoteUdpPort );
  153. SocketConnector reliable = new SocketConnector( remoteAddress, hostPort );
  154. return new DefaultClient( gameName, version, reliable, fast, new TcpConnectorFactory(remoteAddress) );
  155. }
  156. protected static class NetworkClientImpl extends DefaultClient implements NetworkClient
  157. {
  158. public NetworkClientImpl(String gameName, int version)
  159. {
  160. super( gameName, version );
  161. }
  162. public void connectToServer( String host, int port, int remoteUdpPort ) throws IOException
  163. {
  164. connectToServer( InetAddress.getByName(host), port, remoteUdpPort );
  165. }
  166. public void connectToServer( InetAddress address, int port, int remoteUdpPort ) throws IOException
  167. {
  168. UdpConnector fast = new UdpConnector( address, remoteUdpPort );
  169. SocketConnector reliable = new SocketConnector( address, port );
  170. setPrimaryConnectors( reliable, fast, new TcpConnectorFactory(address) );
  171. }
  172. }
  173. }