Browse Source

Update Network Lua API.

Aster Jian 12 years ago
parent
commit
29b3207a8c

+ 34 - 63
Extras/LuaScript/pkgs/Network/Connection.pkg

@@ -1,102 +1,73 @@
 $#include "Connection.h"
 
-/// Queued remote event.
 struct RemoteEvent
 {
-    /// Remote sender node ID (0 if not a remote node event.)
     unsigned senderID_ @ senderID;
-    /// Event type.
     StringHash eventType_ @ eventType;
-    /// Event data.
     VariantMap eventData_ @ eventData;
-    /// In order flag.
     bool inOrder_ @ inOrder;
 };
 
-/// %Connection to a remote network host.
 class Connection : public Object
 {
-public:
-    /// Send a message.
     void SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg, unsigned contentID = 0);
-    /// Send a message.
+    void SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg);
+    
     void SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes, unsigned contentID = 0);
-    /// Send a remote event.
-    // void SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
-    tolua_outside void ConnectionSendRemoteEvent @ SendRemoteEvent(const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
+
+    void SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void SendRemoteEvent(StringHash eventType, bool inOrder);
+    void SendRemoteEvent(const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void SendRemoteEvent(const char* eventType, bool inOrder);
     
-    /// Send a remote event with the specified node as sender.
     void SendRemoteEvent(Node* node, StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
-    tolua_outside void ConnectSendRemoteEvent @ SendRemoteEvent(Node* node, const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
-    /// Assign scene. On the server, this will cause the client to load it.
+    void SendRemoteEvent(Node* node, StringHash eventType, bool inOrder);
+    void SendRemoteEvent(Node* node, const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void SendRemoteEvent(Node* node, const char* eventType, bool inOrder);
+    
     void SetScene(Scene* newScene);
-    /// Assign identity. Called by Network.
     void SetIdentity(const VariantMap& identity);
-    /// Set new controls.
     void SetControls(const Controls& newControls);
-    /// Set the observer position for interest management.
     void SetPosition(const Vector3& position);
-    /// Set the connection pending status. Called by Network.
     void SetConnectPending(bool connectPending);
-    /// Set whether to log data in/out statistics.
     void SetLogStatistics(bool enable);
-    /// Disconnect. If wait time is non-zero, will block while waiting for disconnect to finish.
     void Disconnect(int waitMSec = 0);
-    /// Send scene update messages. Called by Network.
     void SendServerUpdate();
-    /// Send latest controls from the client. Called by Network.
     void SendClientUpdate();
-    /// Send queued remote events. Called by Network.
     void SendRemoteEvents();
-    /// Send package files to client. Called by network.
     void SendPackages();
-    /// Process pending latest data for nodes and components.
     void ProcessPendingLatestData();
-    /// Process a message from the server or client. Called by Network.
     bool ProcessMessage(int msgID, MemoryBuffer& msg);
     
-    /// Return client identity.
-    const VariantMap& GetIdentity() const { return identity_; }
-    /// Return the scene used by this connection.
+    const VariantMap& GetIdentity() const;
     Scene* GetScene() const;
-    /// Return the client controls of this connection.
-    const Controls& GetControls() const { return controls_; }
-    /// Return the observer position for interest management.
-    const Vector3& GetPosition() const { return position_; }
-    /// Return whether is a client connection.
-    bool IsClient() const { return isClient_; }
-    /// Return whether is fully connected.
+    const Controls& GetControls() const;
+    const Vector3& GetPosition() const;
+    bool IsClient() const;
     bool IsConnected() const;
-    /// Return whether connection is pending.
-    bool IsConnectPending() const { return connectPending_; }
-    /// Return whether the scene is loaded and ready to receive server updates.
-    bool IsSceneLoaded() const { return sceneLoaded_; }
-    /// Return whether to log data in/out statistics.
-    bool GetLogStatistics() const { return logStatistics_; }
-    /// Return remote address.
+    bool IsConnectPending() const;
+    bool IsSceneLoaded() const;
+    bool GetLogStatistics() const;
     String GetAddress() const;
-    /// Return remote port.
     unsigned short GetPort() const;
-    /// Return an address:port string.
     String ToString() const;
-    /// Return number of package downloads remaining.
     unsigned GetNumDownloads() const;
-    /// Return name of current package download, or empty if no downloads.
     const String& GetDownloadName() const;
-    /// Return progress of current package download, or 1.0 if no downloads.
     float GetDownloadProgress() const;
+    
+    tolua_property__get_set VariantMap& identity;
+    tolua_property__get_set Scene* scene;
+    tolua_property__get_set Controls& controls;
+    tolua_property__get_set Vector3& position;
+    tolua_readonly tolua_property__is_set bool client;
+    tolua_readonly tolua_property__is_set bool connected;
+    tolua_property__is_set bool connectPending;
+    tolua_readonly tolua_property__is_set bool sceneLoaded;
+    tolua_property__get_set bool logStatistics;
+    tolua_readonly tolua_property__get_set String address;
+    tolua_readonly tolua_property__get_set unsigned short port;
+    tolua_readonly tolua_property__get_set unsigned numDownloads;
+    tolua_readonly tolua_property__get_set String& downloadName;
+    tolua_readonly tolua_property__get_set float downloadProgress;
 };
-
-${
-
-static void ConnectionSendRemoteEvent(Connection* connection, const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap)
-{
-	connection->SendRemoteEvent(StringHash(eventType), inOrder, eventData);
-}
-
-static void ConnectSendRemoteEvent(Connection* connection, Node* node, const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap)
-{
-	connection->SendRemoteEvent(node, StringHash(eventType), inOrder, eventData);
-}
-
-$}

+ 3 - 25
Extras/LuaScript/pkgs/Network/Controls.pkg

@@ -1,36 +1,14 @@
 $#include "Controls.h"
 
-/// %Controls sent over the network.
 class Controls
 {
-public:
-    /// Reset to initial state.
     void Reset();
+    void Set(unsigned buttons, bool down = true);
+    bool IsDown(unsigned button) const;
+    bool IsPressed(unsigned button, const Controls& previousControls) const;
     
-    /// Set or release buttons.
-    void Set(unsigned buttons, bool down = true)
-    {
-        if (down)
-            buttons_ |= buttons;
-        else
-            buttons_ &= ~buttons;
-    }
-    
-    /// Check if a button is held down.
-    bool IsDown(unsigned button) const
-    {
-        return (buttons_ & button) != 0;
-    }
-    
-    /// Check if a button was pressed on this frame. Requires previous frame's controls.
-    bool IsPressed(unsigned button, const Controls& previousControls) const { return (buttons_ & button) != 0 && (previousControls.buttons_ & button) == 0; }
-    
-    /// Button state.
     unsigned buttons_ @ buttons;
-    /// Mouse yaw.
     float yaw_ @ yaw;
-    /// Mouse pitch.
     float pitch_ @ pitch;
-    /// Extra control data.
     VariantMap extraData_ @ extraData;
 };

+ 40 - 43
Extras/LuaScript/pkgs/Network/Network.pkg

@@ -1,63 +1,60 @@
 $#include "Network.h"
 
-/// %Network subsystem. Manages client-server communications using the UDP protocol.
 class Network
 {
-public:
-    /// Connect to a server using UDP protocol. Return true if connection process successfully started.
-    bool Connect(const String& address, unsigned short port, Scene* scene);
     bool Connect(const String& address, unsigned short port, Scene* scene, const VariantMap& identity = Variant::emptyVariantMap);
-    /// Disconnect the connection to the server. If wait time is non-zero, will block while waiting for disconnect to finish.
+    bool Connect(const String& address, unsigned short port, Scene* scene);
+    bool Connect(const char* address, unsigned short port, Scene* scene, const VariantMap& identity = Variant::emptyVariantMap);
+    bool Connect(const char* address, unsigned short port, Scene* scene);
+    
     void Disconnect(int waitMSec = 0);
-    /// Start a server on a port using UDP protocol. Return true if successful.
+    void Disconnect();
     bool StartServer(unsigned short port);
-    /// Stop the server.
     void StopServer();
-    /// Broadcast a message with content ID to all client connections.
+    
     void BroadcastMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg, unsigned contentID = 0);
-    /// Broadcast a message with content ID to all client connections.
-    void BroadcastMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes, unsigned contentID = 0);
-    /// Broadcast a remote event to all client connections.
+    void BroadcastMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg);
+    
+    // void BroadcastMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes, unsigned contentID = 0);
+    
     void BroadcastRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
-    /// Broadcast a remote event to all client connections in a specific scene.
+    void BroadcastRemoteEvent(StringHash eventType, bool inOrder);
+    void BroadcastRemoteEvent(const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void BroadcastRemoteEvent(const char* eventType, bool inOrder);
+    
     void BroadcastRemoteEvent(Scene* scene, StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
-    /// Broadcast a remote event with the specified node as a sender. Is sent to all client connections in the node's scene.
+    void BroadcastRemoteEvent(Scene* scene, StringHash eventType, bool inOrder);
+    void BroadcastRemoteEvent(Scene* scene, const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void BroadcastRemoteEvent(Scene* scene, const char* eventType, bool inOrder);
+    
     void BroadcastRemoteEvent(Node* node, StringHash eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
-    /// Set network update FPS.
+    void BroadcastRemoteEvent(Node* node, StringHash eventType, bool inOrder);
+    void BroadcastRemoteEvent(Node* node, const char* eventType, bool inOrder, const VariantMap& eventData = Variant::emptyVariantMap);
+    void BroadcastRemoteEvent(Node* node, const char* eventType, bool inOrder);
+    
     void SetUpdateFps(int fps);
-    /// Register a remote event as allowed to be sent and received. If no events are registered, all are allowed.
-    // void RegisterRemoteEvent(StringHash eventType);
-    tolua_outside void NetworkRegisterRemoteEvent @ RegisterRemoteEvent(const char* eventType);
-    /// Unregister a remote event as allowed to be sent and received.
-    // void UnregisterRemoteEvent(StringHash eventType);
-    tolua_outside void NetworkUnregisterRemoteEvent @ UnregisterRemoteEvent(const char* eventType);
-    /// Unregister all remote events. This results in all being allowed.
+    
+    void RegisterRemoteEvent(StringHash eventType);
+    void RegisterRemoteEvent(const char* eventType);
+    
+    void UnregisterRemoteEvent(StringHash eventType);
+    void UnregisterRemoteEvent(const char* eventType);
+    
     void UnregisterAllRemoteEvents();
-    /// Set the package download cache directory.
     void SetPackageCacheDir(const String& path);
+    void SetPackageCacheDir(const char* path);
     
-    /// Return network update FPS.
-    int GetUpdateFps() const { return updateFps_; }
-    /// Return the connection to the server. Null if not connected.
+    int GetUpdateFps() const;
     Connection* GetServerConnection() const;
-    /// Return whether the server is running.
+    
     bool IsServerRunning() const;
-    /// Return whether a remote event is allowed to be sent and received. If no events are registered, all are allowed.
+    
     bool CheckRemoteEvent(StringHash eventType) const;
-    /// Return the package download cache directory.
-    const String& GetPackageCacheDir() const { return packageCacheDir_; }
+    bool CheckRemoteEvent(const char* eventType) const;
+    const String& GetPackageCacheDir() const;
+    
+    tolua_property__get_set int updateFps;
+    tolua_readonly tolua_property__get_set Connection* serverConnection;
+    tolua_readonly tolua_property__is_set bool serverRunning;
+    tolua_property__get_set String& packageCacheDir;
 };
-
-${
-
-static void NetworkRegisterRemoteEvent(Network* network, const char* eventType)
-{
-    network->RegisterRemoteEvent(StringHash(eventType));
-}
-
-static void NetworkUnregisterRemoteEvent(Network* network, const char* eventType)
-{
-    network->UnregisterRemoteEvent(StringHash(eventType));
-}
-
-$}

+ 10 - 16
Extras/LuaScript/pkgs/Network/NetworkPriority.pkg

@@ -1,27 +1,21 @@
 $#include "NetworkPriority.h"
 
-/// %Network interest management settings component.
 class NetworkPriority : public Component
 {
-public:
-    /// Set base priority. Default 100 (send updates at full frequency.)
     void SetBasePriority(float priority);
-    /// Set priority reduction distance factor. Default 0 (no effect.)
     void SetDistanceFactor(float factor);
-    /// Set minimum priority. Default 0 (no updates when far away enough.)
     void SetMinPriority(float priority);
-    /// Set whether updates to owner should be sent always at full rate. Default true.
     void SetAlwaysUpdateOwner(bool enable);
+
+    float GetBasePriority() const;
+    float GetDistanceFactor() const;
+    float GetMinPriority() const;
+    bool GetAlwaysUpdateOwner() const;
     
-    /// Return base priority.
-    float GetBasePriority() const { return basePriority_; }
-    /// Return priority reduction distance factor.
-    float GetDistanceFactor() const { return distanceFactor_; }
-    /// Return minimum priority.
-    float GetMinPriority() const { return minPriority_; }
-    /// Return whether updates to owner should be sent always at full rate.
-    bool GetAlwaysUpdateOwner() const { return alwaysUpdateOwner_; }
-    
-    /// Increment and check priority accumulator. Return true if should update. Called by Connection.
     bool CheckUpdate(float distance, float& accumulator);
+    
+    tolua_property__get_set float basePriority;
+    tolua_property__get_set float distanceFactor;
+    tolua_property__get_set float minPriority;
+    tolua_property__get_set bool alwaysUpdateOwner;
 };