netConnection_ScriptBinding.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleMethodGroupBeginWithDocs(NetConnection, SimGroup)
  23. /*! Use the getAddress method to get the address and port that this NetConnection is currently attached to.
  24. @return Returns the address and port that this NetConnection is currently attached to, where the addres will be of the form: A.B.C.D:Port. A .. B are standard IP numbers between 0 and 255 and Port can be between 1000 and 65536. If the connection is local, the string 'local' will be returned. If a this NetConnection is not currently connected the method will return a NULL string.
  25. @sa connect, connectLocal
  26. */
  27. ConsoleMethodWithDocs(NetConnection,getAddress, ConsoleString,2,2, ())
  28. {
  29. if(object->isLocalConnection())
  30. return "local";
  31. char *buffer = Con::getReturnBuffer(256);
  32. Net::addressToString(object->getNetAddress(), buffer);
  33. return buffer;
  34. }
  35. /*! Use the setSimulatedNetParams method to force a connection to experience a certain degree of packet-loss and/or latency. This is a debug feature to allow us to see how a distributed game will behave in the face of poor connection quality.
  36. @param packetLoss A floating-point value between 0.0 (0%) and 1.0 (100%) dictating the percentage of packets to be artificially lost.
  37. @param delay An integer value specifying the number of milliseconds to insert into transmission latencies.
  38. @return No return value.
  39. @sa getPacketLoss, getPing
  40. */
  41. ConsoleMethodWithDocs(NetConnection,setSimulatedNetParams, ConsoleVoid,4, 4, ( packetLoss , delay ))
  42. {
  43. object->setSimulatedNetParams(dAtof(argv[2]), dAtoi(argv[3]));
  44. }
  45. /*! Use the getPing method to determine the round-trip travel time from this connection to the agent on the other end and back again.
  46. @return Returns an integer value representing the total time in milliseconds it takes for a ping request to travel to the agent on the other end of a connection and back to this agent.
  47. @sa getPacketLoss
  48. */
  49. ConsoleMethodWithDocs( NetConnection, getPing, ConsoleInt, 2, 2, ())
  50. {
  51. return( S32( object->getRoundTripTime() ) );
  52. }
  53. /*! Use the getPacketLoss method to determine the current packetLoss count for this connection.
  54. @return Returns an integer value between 0 and inf, indicating the number of packets that have been lost to date on this net connection.
  55. @sa getPing
  56. */
  57. ConsoleMethodWithDocs( NetConnection, getPacketLoss, ConsoleInt, 2, 2, ())
  58. {
  59. return( S32( 100 * object->getPacketLoss() ) );
  60. }
  61. /*! Use the checkMaxRate method to retrieve the current maximum packet rate for this connection.
  62. The period may not neccesarily be one second. To adjust packet rates, see the preference variables above
  63. @return Returns an integer value representing the maximum number of packets that can be transmitted by this connection per transmission period.
  64. */
  65. ConsoleMethodWithDocs( NetConnection, checkMaxRate, ConsoleVoid, 2, 2, ())
  66. {
  67. object->checkMaxRate();
  68. }
  69. #ifdef TORQUE_DEBUG_NET
  70. /*!
  71. */
  72. ConsoleMethodWithDocs( NetConnection, setLogging, ConsoleVoid, 3, 3, conn.setLogging(bool))
  73. {
  74. object->setLogging(dAtob(argv[2]));
  75. }
  76. #endif
  77. /*! Convert a ghost id from this connection to a real id.
  78. @return The ID as an integer
  79. */
  80. ConsoleMethodWithDocs(NetConnection, resolveGhostID, ConsoleInt, 3, 3, ( S32 ghostID ))
  81. {
  82. S32 gID = dAtoi(argv[2]);
  83. // Safety check
  84. if(gID < 0 || gID > NetConnection::MaxGhostCount) return 0;
  85. NetObject *foo = object->resolveGhost(gID);
  86. if(foo)
  87. return foo->getId();
  88. else
  89. return 0;
  90. }
  91. /*! Convert a ghost index from this connection to a real id.
  92. @return The ID as an integer
  93. */
  94. ConsoleMethodWithDocs(NetConnection, resolveObjectFromGhostIndex, ConsoleInt, 3, 3, ( S32 ghostIdx))
  95. {
  96. S32 gID = dAtoi(argv[2]);
  97. // Safety check
  98. if(gID < 0 || gID > NetConnection::MaxGhostCount) return 0;
  99. NetObject *foo = object->resolveObjectFromGhostIndex(gID);
  100. if(foo)
  101. return foo->getId();
  102. else
  103. return 0;
  104. }
  105. /*! Convert a real id to the ghost id for this connection.
  106. @return The ID as an integer
  107. */
  108. ConsoleMethodWithDocs(NetConnection, getGhostID, ConsoleInt, 3, 3, ( S32 realID ))
  109. {
  110. NetObject * foo;
  111. if(Sim::findObject(argv[2], foo))
  112. {
  113. return object->getGhostIndex(foo);
  114. }
  115. else
  116. {
  117. Con::errorf("NetConnection::serverToGhostID - could not find specified object");
  118. return -1;
  119. }
  120. }
  121. /*! Use the connect method to request a connection to a remote server at the address remoteAddress.
  122. @param remoteAddress A string containing an address of the form: A.B.C.D:Port, where A .. B are standard IP numbers between 0 and 255 and Port can be between 1000 and 65536.
  123. @return No return value.
  124. @sa connectLocal, getAddress
  125. */
  126. ConsoleMethodWithDocs(NetConnection, connect, ConsoleVoid, 3, 3, ( remoteAddress ))
  127. {
  128. NetAddress addr;
  129. if(Net::stringToAddress(argv[2], &addr) != Net::NoError)
  130. {
  131. Con::errorf("NetConnection::connect: invalid address - %s", argv[2]);
  132. return;
  133. }
  134. object->connect(&addr);
  135. }
  136. /*! Use the connectLocal method to connect the current client-side connection to a local NetConnection, that is to create an internal connection from this client to the internal server. This is accomplished through the use of a back door mechanism and has an extremely high bandwidth.
  137. @return No return value.
  138. @sa connect, getAddress
  139. */
  140. ConsoleMethodWithDocs(NetConnection, connectLocal, ConsoleString, 2, 2, ())
  141. {
  142. ConsoleObject *co = ConsoleObject::create(object->getClassName());
  143. NetConnection *client = object;
  144. NetConnection *server = dynamic_cast<NetConnection *>(co);
  145. BitStream *stream = BitStream::getPacketStream();
  146. if(!server || !server->canRemoteCreate())
  147. {
  148. delete co;
  149. return "error";
  150. }
  151. server->registerObject();
  152. server->setIsLocalClientConnection();
  153. server->setSequence(0);
  154. client->setSequence(0);
  155. client->setRemoteConnectionObject(server);
  156. server->setRemoteConnectionObject(client);
  157. //We need to reset the maxrate's here, because we
  158. // can't test if it is a local connection until RemoteConnectionObject
  159. // has been set
  160. server->checkMaxRate();
  161. client->checkMaxRate();
  162. stream->setPosition(0);
  163. client->writeConnectRequest(stream);
  164. stream->setPosition(0);
  165. const char* error;
  166. if(!server->readConnectRequest(stream, &error))
  167. {
  168. client->onConnectionRejected(error);
  169. server->deleteObject();
  170. return "error";
  171. }
  172. stream->setPosition(0);
  173. server->writeConnectAccept(stream);
  174. stream->setPosition(0);
  175. if(!client->readConnectAccept(stream, &error))
  176. {
  177. client->handleStartupError(error);
  178. server->deleteObject();
  179. return "error";
  180. }
  181. client->onConnectionEstablished(true);
  182. server->onConnectionEstablished(false);
  183. client->setEstablished();
  184. server->setEstablished();
  185. client->setConnectSequence(0);
  186. server->setConnectSequence(0);
  187. NetConnection::setLocalClientConnection(server);
  188. server->assignName("LocalClientConnection");
  189. return "";
  190. }
  191. /*! Use the getGhostsActive method to determine how many ghosts are active on a particular connection.
  192. @return Returns an integer value between 0 and inf, specifying how many objects are being ghosted
  193. to a client on the other side of a specific connection
  194. */
  195. ConsoleMethodWithDocs( NetConnection, getGhostsActive, S32, 2, 2, ())
  196. {
  197. return object->getGhostsActive();
  198. }
  199. ConsoleMethodGroupEndWithDocs(NetConnection)