public void Should_be_able_to_stream(int count) { var jsonProtocol = new JsonProtocol(); var jsonPacketStream = new JsonPacketStream(); var messages = new List <byte[]> { jsonProtocol.SetPacketContents(1, "The quick brown fox jumped over the lazy dog"), jsonProtocol.SetPacketContents(2, "Second message to be appended") }; var message = messages[0].Concat(messages[1]).ToArray(); var messageResult = new List <byte[]>(); for (int i = 0; i < message.Length; i += count) { var array = message.Skip(i).Take(count).ToArray(); var result = jsonPacketStream.ParseBytes(array); if (result != null) { messageResult.AddRange(result); } } Assert.That(messageResult, Is.EqualTo(messages)); }
private BlyncTcpClient.OnConnectedEvent SendIdentityOnConnectedEvent() { return((sender, eventArgs) => { Console.WriteLine("Connected"); var devices = new List <Device>(); for (int i = 0; i < _blyncClient.NumberOfDevices; i++) { devices.Add(new Device { DeviceId = i, DeviceType = _blyncClient.GetDeviceType(i).ToString() }); } var status = new JsonIdentityPacket { Name = Environment.MachineName, Devices = devices }; var protocol = new JsonProtocol(); var json = protocol.Serialize(status); var packet = protocol.SetPacketContents(0, json); _blyncTcpClient.Send(packet, new CancellationToken()).Wait(); }); }
public void Should_be_able_to_recieve_packet_from_server() { var packetProtocol = new JsonProtocol(); var server = new BlyncTcpServer(new TcpServerSettings { ConnectionTimeoutPeriod = 5000, PacketStreamFactory = new JsonPacketStreamFactory(), Port = Port }); var client = new BlyncTcpClient(new TcpClientSettings { ConnectionTimeoutPeriod = 10000, Hostname = "127.0.0.1", PacketStreamFactory = new JsonPacketStreamFactory(), Port = Port }); var expectedMessage = packetProtocol.SetPacketContents(0, "Hello World!"); server.ClientConnectedEvent += (sender, args) => { var clientContext = sender as TcpClientContext; if (clientContext != null) { clientContext.Send(expectedMessage, new CancellationToken()).Wait(); } }; byte[] actualMessage = null; client.MessageReceivedEvent += (sender, args) => { actualMessage = args; client.Stop(); server.Stop(); }; server.Start(); client.Start(); var timeoutTask = Task.Delay(TimeSpan.FromSeconds(1)); while (!timeoutTask.IsCompleted && actualMessage == null) { // Wait } Assert.That(actualMessage, Is.EqualTo(expectedMessage)); }
public void Should_be_able_to_serialize_and_deserialize_correctly() { const string expectedMessage = "Hello World!"; const int expectedType = 4; var jsonProtocol = new JsonProtocol(); var buffer = jsonProtocol.SetPacketContents(expectedType, expectedMessage); var actualType = jsonProtocol.GetPacketType(buffer); var length = jsonProtocol.GetPacketLength(buffer); var actualMessage = jsonProtocol.GetPacketContents(buffer, length); Assert.That(actualType, Is.EqualTo(expectedType)); Assert.That(actualMessage, Is.EqualTo(expectedMessage)); }
static void Main(string[] args) { var port = Int32.Parse(args[0]); Console.WriteLine("Listening to port {0}", port); var protocol = new JsonProtocol(); var server = new BlyncTcpServer(new TcpServerSettings { ConnectionTimeoutPeriod = 15000, PacketStreamFactory = new JsonPacketStreamFactory(), Port = port }); server.ClientConnectedEvent += (sender, eventArgs) => { var client = sender as TcpClientContext; if (client != null) { Console.WriteLine("Client connected [{0}]", client.Id); var redPacket = protocol.SetPacketContents(1, protocol.Serialize(new JsonInstructionPacket { Color = "red" })); var greenPacket = protocol.SetPacketContents(1, protocol.Serialize(new JsonInstructionPacket { Color = "green" })); for (int i = 0; i < 10; i++) { if (i % 2 == 0) { client.Send(redPacket, new CancellationToken()).Wait(); } else { client.Send(greenPacket, new CancellationToken()).Wait(); } Thread.Sleep(2000); } } }; server.ClientDisconnectedEvent += (sender, eventArgs) => { var client = sender as TcpClientContext; if (client != null) { Console.WriteLine("Client disconnected [{0}]", client.Id); } }; server.MessageReceivedEvent += (sender, bytes) => { var client = sender as TcpClientContext; if (client != null) { Console.WriteLine("Client disconnected [{0}]", client.Id); var type = protocol.GetPacketType(bytes); var length = protocol.GetPacketLength(bytes); var content = protocol.GetPacketContents(bytes, length); Console.WriteLine(" [{0}] {1}", type, content); } }; try { server.Start(); Thread.Sleep(400000); } catch (Exception exception) { Console.WriteLine(exception.Message); if (exception.InnerException != null) { foreach (var innerExceptionMessage in exception.InnerException.Message) { Console.WriteLine(" {0}", innerExceptionMessage); } } } }