NetworkAPI.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #ifdef URHO3D_NETWORK
  24. #include "APITemplates.h"
  25. #include "Controls.h"
  26. #include "HttpRequest.h"
  27. #include "Network.h"
  28. #include "NetworkPriority.h"
  29. #include "Protocol.h"
  30. namespace Urho3D
  31. {
  32. static void ConstructControls(Controls* ptr)
  33. {
  34. new(ptr) Controls();
  35. }
  36. static void ConstructControlsCopy(const Controls& controls, Controls* ptr)
  37. {
  38. new(ptr) Controls(controls);
  39. }
  40. static void DestructControls(Controls* ptr)
  41. {
  42. ptr->~Controls();
  43. }
  44. static void RegisterControls(asIScriptEngine* engine)
  45. {
  46. engine->RegisterObjectType("Controls", sizeof(Controls), asOBJ_VALUE | asOBJ_APP_CLASS_CDK);
  47. engine->RegisterObjectBehaviour("Controls", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructControls), asCALL_CDECL_OBJLAST);
  48. engine->RegisterObjectBehaviour("Controls", asBEHAVE_CONSTRUCT, "void f(const Controls&in)", asFUNCTION(ConstructControlsCopy), asCALL_CDECL_OBJLAST);
  49. engine->RegisterObjectBehaviour("Controls", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructControls), asCALL_CDECL_OBJLAST);
  50. engine->RegisterObjectMethod("Controls", "Controls& opAssign(const Controls&in)", asMETHOD(Controls, operator =), asCALL_THISCALL);
  51. engine->RegisterObjectMethod("Controls", "void Reset()", asMETHOD(Controls, Reset), asCALL_THISCALL);
  52. engine->RegisterObjectMethod("Controls", "void Set(uint, bool)", asMETHOD(Controls, Set), asCALL_THISCALL);
  53. engine->RegisterObjectMethod("Controls", "bool IsDown(uint) const", asMETHOD(Controls, IsDown), asCALL_THISCALL);
  54. engine->RegisterObjectMethod("Controls", "bool IsPressed(uint, const Controls&in) const", asMETHOD(Controls, IsPressed), asCALL_THISCALL);
  55. engine->RegisterObjectProperty("Controls", "uint buttons", offsetof(Controls, buttons_));
  56. engine->RegisterObjectProperty("Controls", "float yaw", offsetof(Controls, yaw_));
  57. engine->RegisterObjectProperty("Controls", "float pitch", offsetof(Controls, pitch_));
  58. engine->RegisterObjectProperty("Controls", "VariantMap extraData", offsetof(Controls, extraData_));
  59. }
  60. static void RegisterNetworkPriority(asIScriptEngine* engine)
  61. {
  62. RegisterComponent<NetworkPriority>(engine, "NetworkPriority");
  63. engine->RegisterObjectMethod("NetworkPriority", "void set_basePriority(float)", asMETHOD(NetworkPriority, SetBasePriority), asCALL_THISCALL);
  64. engine->RegisterObjectMethod("NetworkPriority", "float get_basePriority() const", asMETHOD(NetworkPriority, GetBasePriority), asCALL_THISCALL);
  65. engine->RegisterObjectMethod("NetworkPriority", "void set_distanceFactor(float)", asMETHOD(NetworkPriority, SetDistanceFactor), asCALL_THISCALL);
  66. engine->RegisterObjectMethod("NetworkPriority", "float get_distanceFactor() const", asMETHOD(NetworkPriority, GetDistanceFactor), asCALL_THISCALL);
  67. engine->RegisterObjectMethod("NetworkPriority", "void set_minPriority(float)", asMETHOD(NetworkPriority, SetMinPriority), asCALL_THISCALL);
  68. engine->RegisterObjectMethod("NetworkPriority", "float get_minPriority() const", asMETHOD(NetworkPriority, GetMinPriority), asCALL_THISCALL);
  69. engine->RegisterObjectMethod("NetworkPriority", "void set_alwaysUpdateOwner(bool)", asMETHOD(NetworkPriority, SetAlwaysUpdateOwner), asCALL_THISCALL);
  70. engine->RegisterObjectMethod("NetworkPriority", "bool get_alwaysUpdateOwner() const", asMETHOD(NetworkPriority, GetAlwaysUpdateOwner), asCALL_THISCALL);
  71. }
  72. void SendRemoteEvent(const String& eventType, bool inOrder, const VariantMap& eventData, Connection* ptr)
  73. {
  74. ptr->SendRemoteEvent(eventType, inOrder, eventData);
  75. }
  76. void SendRemoteNodeEvent(Node* receiver, const String& eventType, bool inOrder, const VariantMap& eventData, Connection* ptr)
  77. {
  78. ptr->SendRemoteEvent(receiver, eventType, inOrder, eventData);
  79. }
  80. static void RegisterConnection(asIScriptEngine* engine)
  81. {
  82. RegisterObject<Connection>(engine, "Connection");
  83. engine->RegisterObjectMethod("Connection", "void SendMessage(int, bool, bool, const VectorBuffer&in, uint contentID = 0)", asMETHODPR(Connection, SendMessage, (int, bool, bool, const VectorBuffer&, unsigned), void), asCALL_THISCALL);
  84. engine->RegisterObjectMethod("Connection", "void SendRemoteEvent(const String&in, bool, const VariantMap&in eventData = VariantMap())", asFUNCTION(SendRemoteEvent), asCALL_CDECL_OBJLAST);
  85. engine->RegisterObjectMethod("Connection", "void SendRemoteEvent(Node@+, const String&in, bool, const VariantMap&in eventData = VariantMap())", asFUNCTION(SendRemoteNodeEvent), asCALL_CDECL_OBJLAST);
  86. engine->RegisterObjectMethod("Connection", "void Disconnect(int waitMSec = 0)", asMETHOD(Connection, Disconnect), asCALL_THISCALL);
  87. engine->RegisterObjectMethod("Connection", "String ToString() const", asMETHOD(Connection, ToString), asCALL_THISCALL);
  88. engine->RegisterObjectMethod("Connection", "void set_scene(Scene@+)", asMETHOD(Connection, SetScene), asCALL_THISCALL);
  89. engine->RegisterObjectMethod("Connection", "Scene@+ get_scene() const", asMETHOD(Connection, GetScene), asCALL_THISCALL);
  90. engine->RegisterObjectMethod("Connection", "void set_logStatistics(bool)", asMETHOD(Connection, SetLogStatistics), asCALL_THISCALL);
  91. engine->RegisterObjectMethod("Connection", "bool get_logStatistics() const", asMETHOD(Connection, GetLogStatistics), asCALL_THISCALL);
  92. engine->RegisterObjectMethod("Connection", "bool get_client() const", asMETHOD(Connection, IsClient), asCALL_THISCALL);
  93. engine->RegisterObjectMethod("Connection", "bool get_connected() const", asMETHOD(Connection, IsConnected), asCALL_THISCALL);
  94. engine->RegisterObjectMethod("Connection", "bool get_connectPending() const", asMETHOD(Connection, IsConnectPending), asCALL_THISCALL);
  95. engine->RegisterObjectMethod("Connection", "bool get_sceneLoaded() const", asMETHOD(Connection, IsSceneLoaded), asCALL_THISCALL);
  96. engine->RegisterObjectMethod("Connection", "String get_address() const", asMETHOD(Connection, GetAddress), asCALL_THISCALL);
  97. engine->RegisterObjectMethod("Connection", "uint16 get_port() const", asMETHOD(Connection, GetPort), asCALL_THISCALL);
  98. engine->RegisterObjectMethod("Connection", "uint get_numDownloads() const", asMETHOD(Connection, GetNumDownloads), asCALL_THISCALL);
  99. engine->RegisterObjectMethod("Connection", "const String& get_downloadName() const", asMETHOD(Connection, GetDownloadName), asCALL_THISCALL);
  100. engine->RegisterObjectMethod("Connection", "float get_downloadProgress() const", asMETHOD(Connection, GetDownloadProgress), asCALL_THISCALL);
  101. engine->RegisterObjectMethod("Connection", "void set_position(const Vector3&in)", asMETHOD(Connection, SetPosition), asCALL_THISCALL);
  102. engine->RegisterObjectMethod("Connection", "const Vector3& get_position() const", asMETHOD(Connection, GetPosition), asCALL_THISCALL);
  103. engine->RegisterObjectMethod("Connection", "void set_rotation(const Quaternion&in)", asMETHOD(Connection, SetRotation), asCALL_THISCALL);
  104. engine->RegisterObjectMethod("Connection", "const Quaternion& get_rotation() const", asMETHOD(Connection, GetRotation), asCALL_THISCALL);
  105. engine->RegisterObjectMethod("Connection", "void SendPackageToClient(PackageFile@+)", asMETHOD(Connection, SendPackageToClient), asCALL_THISCALL);
  106. engine->RegisterObjectProperty("Connection", "Controls controls", offsetof(Connection, controls_));
  107. engine->RegisterObjectProperty("Connection", "VariantMap identity", offsetof(Connection, identity_));
  108. // Register SetOwner/GetOwner now
  109. engine->RegisterObjectMethod("Node", "void set_owner(Connection@+)", asMETHOD(Node, SetOwner), asCALL_THISCALL);
  110. engine->RegisterObjectMethod("Node", "Connection@+ get_owner() const", asMETHOD(Node, GetOwner), asCALL_THISCALL);
  111. }
  112. static void RegisterHttpRequest(asIScriptEngine* engine)
  113. {
  114. engine->RegisterEnum("HttpRequestState");
  115. engine->RegisterEnumValue("HttpRequestState", "HTTP_INITIALIZING", HTTP_INITIALIZING);
  116. engine->RegisterEnumValue("HttpRequestState", "HTTP_ERROR", HTTP_ERROR);
  117. engine->RegisterEnumValue("HttpRequestState", "HTTP_OPEN", HTTP_OPEN);
  118. engine->RegisterEnumValue("HttpRequestState", "HTTP_CLOSED", HTTP_CLOSED);
  119. RegisterRefCounted<HttpRequest>(engine, "HttpRequest");
  120. RegisterDeserializer<HttpRequest>(engine, "HttpRequest");
  121. engine->RegisterObjectMethod("HttpRequest", "const String& get_url() const", asMETHOD(HttpRequest, GetURL), asCALL_THISCALL);
  122. engine->RegisterObjectMethod("HttpRequest", "const String& get_verb() const", asMETHOD(HttpRequest, GetVerb), asCALL_THISCALL);
  123. engine->RegisterObjectMethod("HttpRequest", "String get_error() const", asMETHOD(HttpRequest, GetError), asCALL_THISCALL);
  124. engine->RegisterObjectMethod("HttpRequest", "HttpRequestState get_state() const", asMETHOD(HttpRequest, GetState), asCALL_THISCALL);
  125. engine->RegisterObjectMethod("HttpRequest", "uint get_availableSize() const", asMETHOD(HttpRequest, GetAvailableSize), asCALL_THISCALL);
  126. engine->RegisterObjectMethod("HttpRequest", "bool get_open() const", asMETHOD(HttpRequest, IsOpen), asCALL_THISCALL);
  127. }
  128. static Network* GetNetwork()
  129. {
  130. return GetScriptContext()->GetSubsystem<Network>();
  131. }
  132. static CScriptArray* NetworkGetClientConnections(Network* ptr)
  133. {
  134. const Vector<SharedPtr<Connection> >& connections = ptr->GetClientConnections();
  135. return VectorToHandleArray(connections, "Array<Connection@>");
  136. }
  137. static void NetworkBroadcastRemoteEvent(const String& eventType, bool inOrder, const VariantMap& eventData, Network* ptr)
  138. {
  139. ptr->BroadcastRemoteEvent(eventType, inOrder, eventData);
  140. }
  141. static void NetworkBroadcastRemoteSceneEvent(Scene* scene, const String& eventType, bool inOrder, const VariantMap& eventData, Network* ptr)
  142. {
  143. ptr->BroadcastRemoteEvent(scene, eventType, inOrder, eventData);
  144. }
  145. static void NetworkBroadcastRemoteNodeEvent(Node* node, const String& eventType, bool inOrder, const VariantMap& eventData, Network* ptr)
  146. {
  147. ptr->BroadcastRemoteEvent(node, eventType, inOrder, eventData);
  148. }
  149. static void NetworkRegisterRemoteEvent(const String& eventType, Network* ptr)
  150. {
  151. ptr->RegisterRemoteEvent(eventType);
  152. }
  153. static void NetworkUnregisterRemoteEvent(const String& eventType, Network* ptr)
  154. {
  155. ptr->UnregisterRemoteEvent(eventType);
  156. }
  157. static bool NetworkCheckRemoteEvent(const String& eventType, Network* ptr)
  158. {
  159. return ptr->CheckRemoteEvent(eventType);
  160. }
  161. static HttpRequest* NetworkMakeHttpRequest(const String& url, const String& verb, CScriptArray* headers, const String& postData, Network* ptr)
  162. {
  163. SharedPtr<HttpRequest> request = ptr->MakeHttpRequest(url, verb, ArrayToVector<String>(headers), postData);
  164. // The shared pointer will go out of scope, so have to increment the reference count
  165. // (here an auto handle can not be used)
  166. if (request)
  167. request->AddRef();
  168. return request.Get();
  169. }
  170. void RegisterNetwork(asIScriptEngine* engine)
  171. {
  172. RegisterObject<Network>(engine, "Network");
  173. engine->RegisterObjectMethod("Network", "bool Connect(const String&in, uint16, Scene@+, const VariantMap&in identity = VariantMap())", asMETHOD(Network, Connect), asCALL_THISCALL);
  174. engine->RegisterObjectMethod("Network", "void Disconnect(int waitMSec = 0)", asMETHOD(Network, Disconnect), asCALL_THISCALL);
  175. engine->RegisterObjectMethod("Network", "bool StartServer(uint16)", asMETHOD(Network, StartServer), asCALL_THISCALL);
  176. engine->RegisterObjectMethod("Network", "void StopServer()", asMETHOD(Network, StopServer), asCALL_THISCALL);
  177. engine->RegisterObjectMethod("Network", "void BroadcastMessage(int, bool, bool, const VectorBuffer&in, uint contentID = 0)", asMETHODPR(Network, BroadcastMessage, (int, bool, bool, const VectorBuffer&, unsigned), void), asCALL_THISCALL);
  178. engine->RegisterObjectMethod("Network", "void BroadcastRemoteEvent(const String&in, bool, const VariantMap&in eventData = VariantMap())", asFUNCTION(NetworkBroadcastRemoteEvent), asCALL_CDECL_OBJLAST);
  179. engine->RegisterObjectMethod("Network", "void BroadcastRemoteEvent(Scene@+, const String&in, bool, const VariantMap&in eventData = VariantMap())", asFUNCTION(NetworkBroadcastRemoteSceneEvent), asCALL_CDECL_OBJLAST);
  180. engine->RegisterObjectMethod("Network", "void BroadcastRemoteEvent(Node@+, const String&in, bool, const VariantMap&in eventData = VariantMap())", asFUNCTION(NetworkBroadcastRemoteNodeEvent), asCALL_CDECL_OBJLAST);
  181. engine->RegisterObjectMethod("Network", "void RegisterRemoteEvent(const String&in) const", asFUNCTION(NetworkRegisterRemoteEvent), asCALL_CDECL_OBJLAST);
  182. engine->RegisterObjectMethod("Network", "void UnregisterRemoteEvent(const String&in) const", asFUNCTION(NetworkUnregisterRemoteEvent), asCALL_CDECL_OBJLAST);
  183. engine->RegisterObjectMethod("Network", "void UnregisterAllRemoteEvents()", asMETHOD(Network, UnregisterAllRemoteEvents), asCALL_THISCALL);
  184. engine->RegisterObjectMethod("Network", "bool CheckRemoteEvent(const String&in) const", asFUNCTION(NetworkCheckRemoteEvent), asCALL_CDECL_OBJLAST);
  185. engine->RegisterObjectMethod("Network", "HttpRequest@ MakeHttpRequest(const String&in, const String&in verb = String(), Array<String>@+ headers = null, const String&in postData = String())", asFUNCTION(NetworkMakeHttpRequest), asCALL_CDECL_OBJLAST);
  186. engine->RegisterObjectMethod("Network", "void SendPackageToClients(Scene@+, PackageFile@+)", asMETHOD(Network, SendPackageToClients), asCALL_THISCALL);
  187. engine->RegisterObjectMethod("Network", "void set_updateFps(int)", asMETHOD(Network, SetUpdateFps), asCALL_THISCALL);
  188. engine->RegisterObjectMethod("Network", "int get_updateFps() const", asMETHOD(Network, GetUpdateFps), asCALL_THISCALL);
  189. engine->RegisterObjectMethod("Network", "void set_packageCacheDir(const String&in)", asMETHOD(Network, SetPackageCacheDir), asCALL_THISCALL);
  190. engine->RegisterObjectMethod("Network", "const String& get_packageCacheDir() const", asMETHOD(Network, GetPackageCacheDir), asCALL_THISCALL);
  191. engine->RegisterObjectMethod("Network", "bool get_serverRunning() const", asMETHOD(Network, IsServerRunning), asCALL_THISCALL);
  192. engine->RegisterObjectMethod("Network", "Connection@+ get_serverConnection() const", asMETHOD(Network, GetServerConnection), asCALL_THISCALL);
  193. engine->RegisterObjectMethod("Network", "Array<Connection@>@ get_clientConnections() const", asFUNCTION(NetworkGetClientConnections), asCALL_CDECL_OBJLAST);
  194. engine->RegisterGlobalFunction("Network@+ get_network()", asFUNCTION(GetNetwork), asCALL_CDECL);
  195. }
  196. void RegisterNetworkAPI(asIScriptEngine* engine)
  197. {
  198. RegisterControls(engine);
  199. RegisterNetworkPriority(engine);
  200. RegisterConnection(engine);
  201. RegisterHttpRequest(engine);
  202. RegisterNetwork(engine);
  203. }
  204. }
  205. #endif