private async Task Listen() { byte[] headerBytes = new byte[MAX_HEADER_SIZE]; byte[] sizeBytes = new byte[4]; Stream str = Client.GetStream(); BinaryReader reader = new BinaryReader(str); while (Listening) { int read = 0; if ((read = await str.ReadAsync(headerBytes, 0, MAX_HEADER_SIZE, _cancel.Token)) != MAX_HEADER_SIZE) { throw new InvalidDataException($"Expected header of length {MAX_HEADER_SIZE}, found {read}"); } string header = Encoding.UTF8.GetString(headerBytes).Trim('_'); if ((read = await str.ReadAsync(sizeBytes, 0, 4, _cancel.Token)) != sizeBytes.Length) { throw new InvalidDataException($"Expected size of length {sizeBytes.Length}, found {read}"); } int size = (int)BinaryParser.Deserialize(sizeBytes, typeof(int)); Console.WriteLine($"Received {header} [{size}] from {Client.Client.RemoteEndPoint}"); HandlePacket(header, reader); } }
public void HandlePacket(string header, BinaryReader reader) { if (!BlendFarmMessage.HasPackageType(header)) { return; } Type packetType = BlendFarmMessage.GetPackageType(header); BlendFarmMessage req = null; try { req = (BlendFarmMessage)BinaryParser.Deserialize(reader, packetType); } catch (Exception ex) { throw new InvalidDataException($"Failed to parse {packetType.Name} due to:" + ex.Message); } Task.Run(() => { try { if (_handlers.ContainsKey(header)) { MethodInfo method = _handlers[header]; object resp = method.Invoke(this, new object[] { req }); if (resp != null && resp is BlendFarmMessage) { BlendFarmMessage bfresp = ((BlendFarmMessage)resp); if (req.RequestID != null) { bfresp.ResponseID = req.RequestID; } SendPacket((BlendFarmMessage)resp); } } else { OnMessage?.Invoke(this, req); } } catch (Exception ex) { Console.WriteLine($"Exception in handling [{header}] due to {ex.Message}"); } }); }
public void TestSerialize() { short sh = 5; int intt = 6; long lon = 7; byte b1 = 1; byte b2 = 2; short sh2 = 8; string str = "Test"; bool bl = true; TestClass c = new TestClass(); c.Short = sh; c.Int = intt; c.Long = lon; c.Bool = true; c.Test.Add(b1); c.Test.Add(b2); //c.Obj = null; c.Obj = new TestClass.SubClass() { Short = sh2 }; c.Str = str; for (int i = 0; i < 10000; i++) { byte[] b = BinaryParser.Serialize(c); TestClass val = (TestClass)BinaryParser.Deserialize(b, typeof(TestClass)); Assert.AreEqual(val.Short, sh, "class error"); Assert.AreEqual(val.Int, intt, "class error"); Assert.AreEqual(val.Long, lon, "class error"); Assert.AreEqual(val.Test[0], b1, "class error"); Assert.AreEqual(val.Test[1], b2, "class error"); Assert.AreEqual(val.Obj?.Short, (short)8, "class error"); Assert.AreEqual(val.Str, str, "class error"); Assert.AreEqual(val.Bool, bl, "class error"); } /* * TestStruct c2 = new TestStruct(); * c2.Short = sh; * c2.Int = intt; * c2.Long = lon; * c2.Bool = true; * * //c.Obj = null; * c2.Obj = new TestClass.SubClass() { Short = sh2 }; * * c2.Str = str; * * byte[] b3 = BinaryParser.Serialize(c2); * * TestStruct val2 = (TestStruct)BinaryParser.Deserialize(b3, typeof(TestStruct)); * * Assert.AreEqual(val2.Short, sh, "struct error"); * Assert.AreEqual(val2.Int, intt, "struct error"); * Assert.AreEqual(val2.Long, lon, "struct error"); * Assert.AreEqual(val2.Obj?.Short, (short)8, "struct error"); * Assert.AreEqual(val2.Str, str, "struct error"); * Assert.AreEqual(val2.Bool, bl, "struct error");*/ }