Network.pkg 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. $#include "Network.h"
  2. /// %Network subsystem. Manages client-server communications using the UDP protocol.
  3. class Network
  4. {
  5. public:
  6. /// Connect to a server using UDP protocol. Return true if connection process successfully started.
  7. bool Connect(const String& address, unsigned short port, Scene* scene);
  8. bool Connect(const String& address, unsigned short port, Scene* scene, const VariantMap& identity = Variant::emptyVariantMap);
  9. /// Disconnect the connection to the server. If wait time is non-zero, will block while waiting for disconnect to finish.
  10. void Disconnect(int waitMSec = 0);
  11. /// Start a server on a port using UDP protocol. Return true if successful.
  12. bool StartServer(unsigned short port);
  13. /// Stop the server.
  14. void StopServer();
  15. /// Broadcast a message with content ID to all client connections.
  16. void BroadcastMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg, unsigned contentID = 0);
  17. /// Broadcast a message with content ID to all client connections.
  18. void BroadcastMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes, unsigned contentID = 0);
  19. /// Broadcast a remote event to all client connections.
  20. void BroadcastRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
  21. /// Broadcast a remote event to all client connections in a specific scene.
  22. void BroadcastRemoteEvent(Scene* scene, StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
  23. /// Broadcast a remote event with the specified node as a sender. Is sent to all client connections in the node's scene.
  24. void BroadcastRemoteEvent(Node* node, StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
  25. /// Set network update FPS.
  26. void SetUpdateFps(int fps);
  27. /// Register a remote event as allowed to be sent and received. If no events are registered, all are allowed.
  28. // void RegisterRemoteEvent(StringHash eventType);
  29. tolua_outside void NetworkRegisterRemoteEvent @ RegisterRemoteEvent(const char* eventType);
  30. /// Unregister a remote event as allowed to be sent and received.
  31. // void UnregisterRemoteEvent(StringHash eventType);
  32. tolua_outside void NetworkUnregisterRemoteEvent @ UnregisterRemoteEvent(const char* eventType);
  33. /// Unregister all remote events. This results in all being allowed.
  34. void UnregisterAllRemoteEvents();
  35. /// Set the package download cache directory.
  36. void SetPackageCacheDir(const String& path);
  37. /// Return network update FPS.
  38. int GetUpdateFps() const { return updateFps_; }
  39. /// Return the connection to the server. Null if not connected.
  40. Connection* GetServerConnection() const;
  41. /// Return whether the server is running.
  42. bool IsServerRunning() const;
  43. /// Return whether a remote event is allowed to be sent and received. If no events are registered, all are allowed.
  44. bool CheckRemoteEvent(StringHash eventType) const;
  45. /// Return the package download cache directory.
  46. const String& GetPackageCacheDir() const { return packageCacheDir_; }
  47. };
  48. ${
  49. static void NetworkRegisterRemoteEvent(Network* network, const char* eventType)
  50. {
  51. network->RegisterRemoteEvent(StringHash(eventType));
  52. }
  53. static void NetworkUnregisterRemoteEvent(Network* network, const char* eventType)
  54. {
  55. network->UnregisterRemoteEvent(StringHash(eventType));
  56. }
  57. $}