From d6278665c70e0adeaaf27dce0a811dd9bcba5492 Mon Sep 17 00:00:00 2001 From: Juliano Franz Date: Sun, 25 Mar 2018 15:33:37 -0300 Subject: [PATCH] Network fix + TODO: Deserialization fix --- AnnelidaDispatcher/Model/DispatcherServer.cs | 30 ++++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/AnnelidaDispatcher/Model/DispatcherServer.cs b/AnnelidaDispatcher/Model/DispatcherServer.cs index a534bf9..3a2c43f 100644 --- a/AnnelidaDispatcher/Model/DispatcherServer.cs +++ b/AnnelidaDispatcher/Model/DispatcherServer.cs @@ -199,9 +199,15 @@ public void ReadHandler(IAsyncResult ar) else { state.RecvBytesCount += bytesRead; + if (state.RecvBytesCount < state.BufferSize) - handler.BeginReceive(state.Buffer, 4 + state.RecvBytesCount - 1, state.BufferSize, 0, - ReadHandler, state); + { + var revLeft = state.BufferSize - state.RecvBytesCount; + + handler.BeginReceive(state.Buffer, 4 + state.RecvBytesCount - 1, + revLeft, 0, + ReadHandler, state); + } else { HandleMessage(state.Buffer, state); @@ -237,6 +243,8 @@ public void HandleMessage(byte[] bytes, DispatcherClientObject state) case ClientTypes.Types.Robot: //Save to sensor DB async var deserializedDocument = ProcessSerializedBson(bytes); + if(deserializedDocument == null) + return; var t = deserializedDocument["timestamp"].ToUniversalTime(); //Send data to DB in batches of 1s @@ -303,10 +311,20 @@ private void NotifyNetworkViewListeners(ClientTypes.Types sender, byte[] documen private static BsonDocument ProcessSerializedBson(byte[] bytes) { - var doc = BsonSerializer.Deserialize(bytes); - BsonDateTime timestamp = DateTime.UtcNow; - doc["timestamp"] = timestamp; - return doc; + try + { + var doc = BsonSerializer.Deserialize(bytes); + BsonDateTime timestamp = DateTime.UtcNow; + doc["timestamp"] = timestamp; + return doc; + } + catch (Exception e) + { + Console.WriteLine(e); + return null; + } + + } }