public void Run() { Random r = new Random(); LongMessage l1 = new LongMessage(); l1.FieldX1 = r.Next(); l1.FieldX2 = r.Next(); l1.FieldX3 = r.Next(); l1.FieldX4 = r.Next(); MemoryStream ms1 = new MemoryStream(); LongMessage.Serialize(ms1, l1); //Console.WriteLine(BitConverter.ToString(ms1.ToArray())); MemoryStream ms2 = new MemoryStream(ms1.ToArray()); LongMessage l2 = LongMessage.Deserialize(ms2); //Test Assert.AreEqual(l1.FieldX1, l2.FieldX1, "LongMessage FieldX1"); Assert.AreEqual(l1.FieldX2, l2.FieldX2, "LongMessage FieldX2"); Assert.AreEqual(l1.FieldX3, l2.FieldX3, "LongMessage FieldX3"); Assert.AreEqual(l1.FieldX4, l2.FieldX4, "LongMessage FieldX4"); }
/// <summary> /// Test serializing a message without any low id fields. /// </summary> public static void Run() { Random r = new Random(); LongMessage l1 = new LongMessage(); l1.FieldX1 = r.Next(); l1.FieldX2 = r.Next(); l1.FieldX3 = r.Next(); l1.FieldX4 = r.Next(); MemoryStream ms1 = new MemoryStream(); LongMessage.Serialize(ms1, l1); //Console.WriteLine(BitConverter.ToString(ms1.ToArray())); MemoryStream ms2 = new MemoryStream(ms1.ToArray()); LongMessage l2 = LongMessage.Deserialize(ms2); //Test Test("LongMessage FieldX1", l1.FieldX1 == l2.FieldX1); Test("LongMessage FieldX2", l1.FieldX2 == l2.FieldX2); Test("LongMessage FieldX3", l1.FieldX3 == l2.FieldX3); Test("LongMessage FieldX4", l1.FieldX4 == l2.FieldX4); }
static void Main(string[] args) { IMessageSender sender = new SmsMessageSender(); Message message = new ShortMessage(sender); message.SendMessage("Hello Friends"); IMessageSender sender2 = new MailMessageSender(); Message message2 = new LongMessage(sender); message.SendMessage("Email message"); Console.ReadKey(); }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> public static byte[] SerializeToBytes(LongMessage instance) { using (var ms = new MemoryStream()) { Serialize(ms, instance); return ms.ToArray(); } }
/// <summary>Helper: Serialize with a varint length prefix</summary> public static void SerializeLengthDelimited(Stream stream, LongMessage instance) { var data = SerializeToBytes(instance); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length); stream.Write(data, 0, data.Length); }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, LongMessage instance) { // Key for field: 32, Varint stream.Write(new byte[]{128, 2}, 0, 2); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX1); // Key for field: 64, Varint stream.Write(new byte[]{128, 4}, 0, 2); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX2); // Key for field: 96, Varint stream.Write(new byte[]{128, 6}, 0, 2); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX3); // Key for field: 100, Varint stream.Write(new byte[]{160, 6}, 0, 2); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX4); }
/// <summary>Helper: create a new instance to deserializing into</summary> public static LongMessage DeserializeLengthDelimited(Stream stream) { LongMessage instance = new LongMessage(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static LongMessage DeserializeLength(Stream stream, int length) { LongMessage instance = new LongMessage(); DeserializeLength(stream, length, instance); return instance; }
/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> public static LongMessage Deserialize(byte[] buffer) { LongMessage instance = new LongMessage(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static LongMessage Deserialize(Stream stream) { LongMessage instance = new LongMessage(); Deserialize(stream, instance); return instance; }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, LongMessage instance) { var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop(); // Key for field: 32, Varint stream.WriteByte(128); stream.WriteByte(2); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX1); // Key for field: 64, Varint stream.WriteByte(128); stream.WriteByte(4); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX2); // Key for field: 96, Varint stream.WriteByte(128); stream.WriteByte(6); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX3); // Key for field: 100, Varint stream.WriteByte(160); stream.WriteByte(6); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldX4); global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField); }
public void Test_message() { string longM = "this is a very very very long message..."; string shortM = "short SMS"; IMessageSender smsMessageSender = new SMSMessageSender(); IMessageSender emailMessageSender = new EmailMessageSender(); // Short message ShortMessage shortMessage = new ShortMessage(smsMessageSender); { shortMessage.SendMessage(shortM); Assert.That(shortMessage.GetMessageSentCount(), Is.EqualTo(1)); shortMessage.SendMessage(shortM); shortMessage.SendMessage(shortM); shortMessage.SendMessage(shortM); shortMessage.SendMessage(shortM); Assert.That(shortMessage.GetMessageSentCount(), Is.EqualTo(5)); Assert.That(shortMessage.GetSenderType(), Is.EqualTo(SenderType.SMS)); Assert.That(shortMessage.GetSenderType(), !Is.EqualTo(SenderType.Email)); } // Null message { var ex = Assert.Throws <ArgumentNullException>(() => { shortMessage.SendMessage(string.Empty); }); Assert.That(ex.Message, Is.EqualTo("Invalid message. (Parameter 'message')")); } // Long message { var ex = Assert.Throws <ArgumentException>(() => { shortMessage.SendMessage(longM); }); Assert.That(ex.Message, Is.EqualTo("Invalid message: message is too long. (Parameter 'message')")); } // Long message LongMessage longMessage = new LongMessage(emailMessageSender); { longMessage.SendMessage(longM); Assert.That(longMessage.GetMessageSentCount(), Is.EqualTo(1)); longMessage.SendMessage(longM); longMessage.SendMessage(longM); Assert.That(longMessage.GetMessageSentCount(), Is.EqualTo(3)); Assert.That(longMessage.GetSenderType(), !Is.EqualTo(SenderType.SMS)); Assert.That(longMessage.GetSenderType(), Is.EqualTo(SenderType.Email)); } // Null message { var ex = Assert.Throws <ArgumentNullException>(() => { longMessage.SendMessage(string.Empty); }); Assert.That(ex.Message, Is.EqualTo("Invalid message. (Parameter 'message')")); } // Short message { var ex = Assert.Throws <ArgumentException>(() => { longMessage.SendMessage(shortM); }); Assert.That(ex.Message, Is.EqualTo("Invalid message: message is too short. (Parameter 'message')")); } }