public void SerializesMessageCorrectly_MsgSerial(long msgSerial) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { MsgSerial = msgSerial }; List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x82); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add(0); expectedMessage.AddRange(SerializeString("msgSerial")); if (Math.Abs(msgSerial) < 255) { expectedMessage.Add(BitConverter.GetBytes(msgSerial).First()); } else { expectedMessage.Add(0xd1); expectedMessage.AddRange(BitConverter.GetBytes(msgSerial).TakeWhile(c => c > 0).Reverse()); } // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <byte[]>(result); Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]); }
public void SerializesMessageCorrectly_Channel(string channel) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Channel = channel }; List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x82); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add(0); if (!string.IsNullOrEmpty(channel)) { expectedMessage[0]++; expectedMessage.AddRange(SerializeString("channel")); expectedMessage.AddRange(SerializeString(channel)); } expectedMessage.AddRange(SerializeString("msgSerial")); expectedMessage.Add(0); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <byte[]>(result); Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]); }
public void OnImportsSatisfied() { var context = new SerializationContext(); context.CompatibilityOptions.PackerCompatibilityOptions = MsgPack.PackerCompatibilityOptions.PackBinaryAsRaw; serializer = new MsgPackMessageSerializer(context); }
public void OnImportsSatisfied() { var context = new SerializationContext(); context.CompatibilityOptions.PackerCompatibilityOptions = MsgPack.PackerCompatibilityOptions.PackBinaryAsRaw; serializer = new MsgPackMessageSerializer(context); }
public void ShouldSerializeToMsgPack() { IMessageSerializer serializer = new MsgPackMessageSerializer(); var message = new TestMessage { Id = 123, Name = "SomeName" }; var serialize = serializer.Serialize(message); Assert.That(serialize, Is.Not.Null); Assert.That(serialize, Is.Not.Empty); Assert.That(serialize, Has.Length.EqualTo(20)); }
public void ShouldSerializeToMsgPack() { IMessageSerializer serializer = new MsgPackMessageSerializer(); var message = new TestMessageProto { Id = 123, Name = "SomeName" }; var serialize = serializer.Serialize(message); Assert.NotNull(serialize); Assert.Equal(21, serialize.Length); }
public void ShouldDeserializeFromMsgPack() { IMessageSerializer serializer = new MsgPackMessageSerializer(); IMessageDeserializer <TestMessageProto> deserializer = new MsgPackMessageDeserializer <TestMessageProto>(); // see MsgPack spec limitation regarding UTC dates // https://github.com/msgpack/msgpack-cli/wiki#datetime var message = new TestMessageProto { Id = 123, Name = "SomeName", Date = new DateTime(2010, 2, 10, 13, 22, 59, DateTimeKind.Utc) }; var serialize = serializer.Serialize(message); var deserialize = deserializer.Deserialize(serialize); Assert.Equal(message.Id, deserialize.Id); Assert.Equal(message.Name, deserialize.Name); Assert.Equal(message.Date, deserialize.Date); }
public void DeserializesMessageCorrectly_Flags(int flags) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("flags")); expectedMessage.Add((byte)flags); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <byte>((byte)flags, (byte)target.Flags); }
public void DeserializesMessageCorrectly_Count(int count) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("count")); expectedMessage.Add(BitConverter.GetBytes(count).First()); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <int>(count, target.Count); }
public void DeserializesMessageCorrectly_MsgSerial(long serial) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("msgSerial")); expectedMessage.Add(BitConverter.GetBytes(serial).First()); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <long>(serial, target.MsgSerial); }
public void DeserializesMessageCorrectly_Id(string id) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("id")); expectedMessage.AddRange(SerializeString(id)); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <string>(id, target.Id); }
public void DeserializesMessageCorrectly_ConnectionKey(string connectionKey) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("connectionKey")); expectedMessage.AddRange(SerializeString(connectionKey)); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <string>(connectionKey, target.ConnectionKey); }
public void DeserializesMessageCorrectly_Action(ProtocolMessage.MessageAction action) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add((byte)action); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <ProtocolMessage.MessageAction>(action, target.Action); }
public void SerializesMessageCorrectly_Action(ProtocolMessage.MessageAction messageAction) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); ProtocolMessage message = new ProtocolMessage(messageAction); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x82); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add((byte)messageAction); expectedMessage.AddRange(SerializeString("msgSerial")); expectedMessage.Add(0); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <byte[]>(result); Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]); }
public void SerializesMessageCorrectly_Presence(params PresenceMessage[] messages) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Presence = messages }; List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x82); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add(0); expectedMessage.AddRange(SerializeString("msgSerial")); expectedMessage.Add(0); if (messages.Length > 0) { expectedMessage[0]++; expectedMessage.AddRange(SerializeString("presence")); expectedMessage.Add((byte)((0x09 << 4) + messages.Length)); foreach (PresenceMessage msg in messages) { expectedMessage.Add((0x08 << 4) + 1); expectedMessage[expectedMessage.Count - 1] += (byte)(string.IsNullOrEmpty(msg.ClientId) ? 0 : 1); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add((byte)msg.Action); if (!string.IsNullOrEmpty(msg.ClientId)) { expectedMessage.AddRange(SerializeString("clientId")); expectedMessage.AddRange(SerializeString(msg.ClientId)); } } } // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <byte[]>(result); Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]); }
public void DeserializesMessageCorrectly_Messages(byte[] messageBin, params Message[] expectedMessages) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("messages")); expectedMessage.AddRange(messageBin); // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.NotNull(target.Messages); Assert.Equal <int>(expectedMessages.Length, target.Messages.Length); for (int i = 0; i < expectedMessages.Length; i++) { Assert.Equal <string>(expectedMessages[i].Name, target.Messages[i].Name); Assert.Equal(expectedMessages[i].Data, target.Messages[i].Data); } }
public void SerializesMessageCorrectly_Messages(params Message[] messages) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Messages = messages }; List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x82); expectedMessage.AddRange(SerializeString("action")); expectedMessage.Add(0); expectedMessage.AddRange(SerializeString("msgSerial")); expectedMessage.Add(0); var validMessages = messages.Where(c => !string.IsNullOrEmpty(c.Name)); if (validMessages.Any()) { expectedMessage[0]++; expectedMessage.AddRange(SerializeString("messages")); expectedMessage.Add((byte)((0x09 << 4) + validMessages.Count())); foreach (Message msg in validMessages) { expectedMessage.Add((0x08 << 4) + 1); expectedMessage.AddRange(SerializeString("name")); expectedMessage.AddRange(SerializeString(msg.Name)); } } // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <byte[]>(result); Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]); }
public void DeserializesMessageCorrectly_ChannelSerial(string serial) { // Arrange MsgPackMessageSerializer serializer = new MsgPackMessageSerializer(); List <byte> expectedMessage = new List <byte>(); expectedMessage.Add(0x81); expectedMessage.AddRange(SerializeString("channelSerial")); if (serial != null) { expectedMessage.AddRange(SerializeString(serial)); } else { expectedMessage.Add(0xc0); } // Act ProtocolMessage target = serializer.DeserializeProtocolMessage(expectedMessage.ToArray()); // Assert Assert.NotNull(target); Assert.Equal <string>(serial, target.ChannelSerial); }
public ITransport CreateTransport(TransportParams parameters) { IMessageSerializer serializer = null; if (parameters.Options.UseBinaryProtocol) { serializer = new MsgPackMessageSerializer(); } else { serializer = new JsonMessageSerializer(); } WebSocketTransport socketTransport = new WebSocketTransport(serializer); socketTransport.Host = parameters.Host; socketTransport.channelBinaryMode = parameters.Options.UseBinaryProtocol; socketTransport.socket = CreateSocket(parameters); socketTransport.socket.Opened += socketTransport.socket_Opened; socketTransport.socket.Closed += socketTransport.socket_Closed; socketTransport.socket.Error += socketTransport.socket_Error; socketTransport.socket.MessageReceived += socketTransport.socket_MessageReceived; socketTransport.socket.DataReceived += socketTransport.socket_DataReceived; return(socketTransport); }