void Send(UserToken token, byte type, byte request, byte[] message) { DataModel model = new DataModel(type, request, message); byte[] data = MessageCodec.Encode(model); token.WaitSend(data); }
public void TestEmpty() { var message = new Message(new Dictionary <string, string>() { }, new byte[] {}); var encoder = new MessageCodec(); var encoded = encoder.Encode(message); var decoded = encoder.Decode(encoded); Assert.AreEqual(decoded.Headers.Count, 0); Assert.AreEqual(decoded.Payload.Length, 0); }
public void TestNoHeaders() { var message = new Message(new Dictionary <string, string>() { }, new byte[] { 1, 2, 3 }); var encoder = new MessageCodec(); var encoded = encoder.Encode(message); var decoded = encoder.Decode(encoded); Assert.AreEqual(decoded.Headers.Count, 0); Assert.AreEqual(decoded.Payload.Length, 3); Assert.AreEqual(decoded.Payload[0], 1); Assert.AreEqual(decoded.Payload[1], 2); Assert.AreEqual(decoded.Payload[2], 3); }
public void TestNoPayload() { var message = new Message(new Dictionary <string, string>() { { "A", "B" }, { "B1", "789" }, }, new byte[] { }); var encoder = new MessageCodec(); var encoded = encoder.Encode(message); var decoded = encoder.Decode(encoded); Assert.AreEqual(decoded.Headers.Count, 2); Assert.AreEqual(decoded.Headers.Keys.First(), "A"); Assert.AreEqual(decoded.Headers.Keys.Last(), "B1"); Assert.AreEqual(decoded.Headers["A"], "B"); Assert.AreEqual(decoded.Headers["B1"], "789"); Assert.AreEqual(decoded.Payload.Length, 0); }
public void TestEncodeDecode() { var codec = new MessageCodec(manager, wcfCodec, 65536); // invalid encode try { codec.Encode(null); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } try { codec.Encode(null, null); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } try { codec.Encode(CreateMessage(1), null); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } try { codec.Encode(null, new MemoryStream()); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } // invalid decode Assert.AreEqual(codec.Decode((Byte[])null), null); Assert.AreEqual(codec.Decode(new Byte[0]), null); try { codec.Decode(new Byte[1024]); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } Assert.AreEqual(codec.Decode(default(ArraySegment<Byte>)), null); try { codec.Decode(new ArraySegment<Byte>(new Byte[1024])); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } Assert.AreEqual(codec.Decode((Stream)null), null); Assert.AreEqual(codec.Decode(new MemoryStream()), null); try { var stream = new MemoryStream(); stream.Write(new Byte[1024], 0, 1024); stream.Position = 0; codec.Decode(stream); Assert.Fail("Expected: Exception"); } catch (AssertFailedException) { throw; } catch { } // valid buffered message AssertIsEqualMessage( CreateMessage(1), codec.Decode(codec.Encode(CreateMessage(1))) ); var encoded = codec.Encode(CreateMessage(1)); AssertIsEqualMessage( CreateMessage(1), codec.Decode(encoded.Raw.Skip(encoded.Offset).Take(encoded.Length).ToArray()) ); // valid streamed message using (var stream = new MemoryStream()) { codec.Encode(CreateMessage(2), stream); stream.Position = 0; AssertIsEqualMessage(CreateMessage(2), codec.Decode(stream)); } // using encoded codec.UsingEncoded( CreateMessage(3), buffer1 => { var buffer2 = codec.Allocate(); Array.Copy(buffer1.Raw, buffer1.Offset, buffer2.Raw, buffer2.Offset, buffer1.Length); AssertIsEqualMessage( CreateMessage(3), codec.Decode(new ArraySegment<Byte>(buffer2.Raw, buffer2.Offset, buffer1.Length)) ); } ); }