Просмотр исходного кода

debug messages removed, client disconnecting fixed

Arnis Lielturks 5 лет назад
Родитель
Сommit
4e201d64be

+ 0 - 13
Source/Urho3D/Network/Connection.cpp

@@ -683,20 +683,17 @@ void Connection::ProcessSceneUpdate(int msgID, MemoryBuffer& msg)
     {
     case MSG_CREATENODE:
         {
-            URHO3D_LOGINFOF("MSG_CREATENODE received with size %d", msg.GetSize());
             unsigned nodeID = msg.ReadNetID();
             // In case of the root node (scene), it should already exist. Do not create in that case
             Node* node = scene_->GetNode(nodeID);
             if (!node)
             {
-                URHO3D_LOGINFOF("CREATE NODE 1: %d", nodeID);
                 // Add initially to the root level. May be moved as we receive the parent attribute
                 node = scene_->CreateChild(nodeID, REPLICATED);
                 // Create smoothed transform component
                 node->CreateComponent<SmoothedTransform>(LOCAL);
             }
 
-            URHO3D_LOGINFOF("CREATE NODE 2: %d", nodeID);
             // Read initial attributes, then snap the motion smoothing immediately to the end
             node->ReadDeltaUpdate(msg);
             auto* transform = node->GetComponent<SmoothedTransform>();
@@ -705,7 +702,6 @@ void Connection::ProcessSceneUpdate(int msgID, MemoryBuffer& msg)
 
             // Read initial user variables
             unsigned numVars = msg.ReadVLE();
-            URHO3D_LOGINFOF("CREATE NODE 3");
             while (numVars)
             {
                 StringHash key = msg.ReadStringHash();
@@ -713,14 +709,10 @@ void Connection::ProcessSceneUpdate(int msgID, MemoryBuffer& msg)
                 --numVars;
             }
 
-            URHO3D_LOGINFOF("CREATE NODE 4");
-
             // Read components
             unsigned numComponents = msg.ReadVLE();
-            URHO3D_LOGINFOF("NUm components %d", numComponents);
             while (numComponents)
             {
-                URHO3D_LOGINFOF("CREATE NODE 5");
                 --numComponents;
 
                 StringHash type = msg.ReadStringHash();
@@ -733,7 +725,6 @@ void Connection::ProcessSceneUpdate(int msgID, MemoryBuffer& msg)
                     if (component)
                         component->Remove();
                     component = node->CreateComponent(type, REPLICATED, componentID);
-                    URHO3D_LOGINFOF("CREATE NODE 6");
                 }
 
                 // If was unable to create the component, would desync the message and therefore have to abort
@@ -746,7 +737,6 @@ void Connection::ProcessSceneUpdate(int msgID, MemoryBuffer& msg)
                 // Read initial attributes and apply
                 component->ReadDeltaUpdate(msg);
                 component->ApplyAttributes();
-                URHO3D_LOGINFOF("CREATE NODE 7");
             }
         }
         break;
@@ -1402,10 +1392,7 @@ void Connection::ProcessNewNode(Node* node)
         component->WriteInitialDeltaUpdate(msg_, timeStamp_);
     }
 
-    URHO3D_LOGINFOF("MSG_CREATENODE sending with size %d", msg_.GetSize());
     SendMessage(MSG_CREATENODE, true, true, msg_);
-//    auto test = MemoryBuffer(msg_.GetData(), msg_.GetSize());
-//    ProcessSceneUpdate(MSG_CREATENODE, test);
 
     nodeState.markedDirty_ = false;
     sceneState_.dirtyNodes_.Erase(node->GetID());

+ 55 - 75
Source/Urho3D/Network/WS/WSClient.cpp

@@ -131,50 +131,8 @@ static int WSCallback(struct lws *wsi, enum lws_callback_reasons reason, void *u
 
         case LWS_CALLBACK_CLIENT_WRITEABLE:
             if (WSClientInstance) {
-#ifdef __EMSCRIPTEN__
-                if(WSClientInstance->GetNumOutgoingPackets(wsi))
-                {
-                    auto packet = WSClientInstance->GetOutgoingPacket(wsi);
-                    if (packet)
-                    {
-                        int retval = EM_ASM_INT({
-                            var socket = Module.__libwebsocket.socket;
-                            if( ! socket ) {
-                                return -1;
-                            }
-                            // alloc a Uint8Array backed by the incoming data.
-                            var data_in = new Uint8Array(Module.HEAPU8.buffer, $1, $2 );
-                            // allow the dest array
-                            var data = new Uint8Array($2);
-                            // set the dest from the src
-                            data.set(data_in);
-                            socket.send(data);
-
-                            return $2;
-
-                        }, packet->second_.GetData(), packet->second_.GetSize());
-                        if (retval <  packet->second_.GetSize()) {
-                            URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
-                            break;
-                        }
-                        WSClientInstance->RemoveOutgoingPacket(wsi);
-                    }
-                }
-#else
-                if(WSClientInstance->GetNumOutgoingPackets(wsi)) {
-                    auto packet = WSClientInstance->GetOutgoingPacket(wsi);
-                    if (packet) {
-                        unsigned char buf[LWS_PRE + packet->second_.GetSize()];
-                        memcpy(&buf[LWS_PRE],  packet->second_.GetData(),  packet->second_.GetSize());
-                        int retval = lws_write(wsi, &buf[LWS_PRE],  packet->second_.GetSize(), LWS_WRITE_BINARY);
-                        if (retval <  packet->second_.GetSize()) {
-                            URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
-                            break;:
-                        }
-                        WSClientInstance->RemoveOutgoingPacket(wsi);
-                    }
-                }
-                lws_callback_on_writable(wsi);
+#ifndef __EMSCRIPTEN__
+               WSClientInstance->SendOutMessages(wsi);
 #endif
             }
             break;
@@ -294,8 +252,8 @@ ws_(nullptr)
             this.destroy();
         };
         libwebsocket.destroy = function() {
+            libwebsocket.on_event(libwebsocket.socket.id, 75, 0, 0, 0);
             libwebsocket.socket = false;
-            libwebsocket.on_event(this.id, 75, 0, 0, 0);
         };
 
         Module.__libwebsocket = libwebsocket;
@@ -373,36 +331,8 @@ void WSClient::Update(float timestep)
         }
     }
 #else
-    if(WSClientInstance->GetNumOutgoingPackets(ws_))
-    {
-        auto packet = WSClientInstance->GetOutgoingPacket(ws_);
-        if (packet)
-        {
-            int retval = EM_ASM_INT({
-                var socket = Module.__libwebsocket.socket;
-                if( ! socket ) {
-                    return -1;
-                }
-                // alloc a Uint8Array backed by the incoming data.
-                var data_in = new Uint8Array(Module.HEAPU8.buffer, $0, $1);
-                // allow the dest array
-                var data = new Uint8Array($1);
-                // set the dest from the src
-                data.set(data_in);
-                socket.send(data);
-
-                return $1;
-
-            }, packet->second_.GetData(), packet->second_.GetSize());
-            if (retval <  packet->second_.GetSize())
-            {
-                URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
-            } 
-            else 
-                WSClientInstance->RemoveOutgoingPacket(ws_);
-
-        }
-    }
+    // For web port we can send out messages immediatelly
+    SendOutMessages(GetWSConnection());
 #endif
 
     while (GetNumIncomingPackets()) {
@@ -454,4 +384,54 @@ void WSClient::HandleWorkItemFinished(StringHash eventType, VariantMap& eventDat
     }
 }
 
+void WSClient::SendOutMessages(lws *ws)
+{
+#ifdef __EMSCRIPTEN__
+    if(GetNumOutgoingPackets(ws))
+    {
+        auto packet = GetOutgoingPacket(ws);
+        if (packet)
+        {
+            int retval = EM_ASM_INT({
+                var socket = Module.__libwebsocket.socket;
+                if( ! socket ) {
+                    return -1;
+                }
+                // alloc a Uint8Array backed by the incoming data.
+                var data_in = new Uint8Array(Module.HEAPU8.buffer, $0, $1);
+                // allow the dest array
+                var data = new Uint8Array($1);
+                // set the dest from the src
+                data.set(data_in);
+                socket.send(data);
+
+                return $1;
+
+            }, packet->second_.GetData(), packet->second_.GetSize());
+            if (retval <  packet->second_.GetSize())
+            {
+                URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
+            }
+            else
+                RemoveOutgoingPacket(ws);
+        }
+    }
+#else
+    if(WSClientInstance->GetNumOutgoingPackets(ws)) {
+        auto packet = WSClientInstance->GetOutgoingPacket(ws);
+        if (packet) {
+            unsigned char buf[LWS_PRE + packet->second_.GetSize()];
+            memcpy(&buf[LWS_PRE],  packet->second_.GetData(),  packet->second_.GetSize());
+            int retval = lws_write(ws, &buf[LWS_PRE],  packet->second_.GetSize(), LWS_WRITE_BINARY);
+            if (retval <  packet->second_.GetSize()) {
+                URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
+                break;:
+            }
+            WSClientInstance->RemoveOutgoingPacket(ws);
+        }
+    }
+    lws_callback_on_writable(ws);
+#endif
+}
+
 #endif

+ 3 - 0
Source/Urho3D/Network/WS/WSClient.h

@@ -73,6 +73,9 @@ private:
     /// Handle work item finished event
     void HandleWorkItemFinished(StringHash eventType, VariantMap& eventData);
 
+    /// Send out client messages
+    void SendOutMessages(lws *ws);
+
     /// Work item which runs the WS service loop
     SharedPtr<WorkItem> serviceWorkItem_;
     /// Websocket connection information

+ 1 - 1
Source/Urho3D/Network/WS/WSServer.cpp

@@ -117,7 +117,7 @@ static void RunService(const WorkItem* item, unsigned threadIndex) {
     for (auto it = packets->Begin(); it != packets->End(); ++it) {
         if (!(*it).second_.Empty()) {
             auto ws = (*it).second_.Front().first_;
-            URHO3D_LOGINFOF("Outgoing packet count (server) %d", WSServerInstance->GetNumOutgoingPackets(ws));
+            // URHO3D_LOGINFOF("Outgoing packet count (server) %d", WSServerInstance->GetNumOutgoingPackets(ws));
             lws_callback_on_writable(ws.GetWS());
         }
     }

+ 1 - 1
bin/shell.html

@@ -80,7 +80,7 @@
     </div>
     <script>
         var canvasElement = document.getElementById('canvas');
-        var devicePixelRatio = window.devicePixelRatio || 1;
+        var devicePixelRatio = 1;//window.devicePixelRatio || 1;
         var canvasWidth = 0;
         var canvasHeight = 0;