/// <summary> /// Send data (byte[]) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendAndGetReply(this EasyTcpClient client, byte[] data, TimeSpan?timeout = null, bool compression = false) { if (compression) { data = CompressionUtil.Compress(data); } return(client.SendAndGetReply(timeout, data)); }
/// <summary> /// Send action (byte[]) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="action">action code</param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendActionAndGetReply(this EasyTcpClient client, int action, byte[] data = null, TimeSpan?timeout = null, bool compression = false) { if (compression && data != null) { data = CompressionUtil.Compress(data); } return(client.SendAndGetReply(timeout, BitConverter.GetBytes(action), data)); }
public void SendAndGetReplyUInt() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); uint data = 123; var m = client.SendAndGetReply(data, _timeout); Assert.AreEqual(data, m.ToUInt()); }
public void SendAndGetReplyArray() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); byte[] data = new byte[100]; var m = client.SendAndGetReply(data, _timeout); Assert.IsTrue(data.SequenceEqual(m.Data)); }
public void SendAndGetReplyString() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); string data = "123"; var m = client.SendAndGetReply(data, _timeout); Assert.AreEqual(data, m.ToString()); }
public void SendAndGetReplyBool() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); bool data = true; // ReSharper disable once ConditionIsAlwaysTrueOrFalse var m = client.SendAndGetReply(data, _timeout); Assert.AreEqual(true, m.ToBool()); }
public void SendAndGetReplyTriggerOnDataReceive() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); bool triggered = false; client.OnDataReceive += (sender, message) => triggered = true; byte[] data = new byte[100]; var m = client.SendAndGetReply(data); Assert.IsTrue(data.SequenceEqual(m.Data)); Assert.IsFalse(triggered); }
public void TestSplittingData() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpServer(new NoneProtocol()).Start(port); server.OnDataReceive += (sender, message) => message.Client.Send(message); using var client = new EasyTcpClient(new NoneProtocol(4)); Assert.IsTrue(client.Connect("127.0.0.1", port)); var data = "testMessage"; var reply = client.SendAndGetReply(data); Assert.IsNotNull(reply); Assert.AreEqual("test", reply.ToString()); }
public void TestReceivingAndSendingData() { ushort port = TestHelper.GetPort(); var protocol = new DelimiterProtocol("\r\n"); using var server = new EasyTcpServer(protocol).Start(port); server.OnDataReceive += (sender, message) => message.Client.Send(message); using var client = new EasyTcpClient(protocol); Assert.IsTrue(client.Connect("127.0.0.1", port)); var data = "testMessage"; var reply = client.SendAndGetReply(data); Assert.IsNotNull(reply); Assert.AreEqual(data, reply.ToString()); }
public void TestEncryption() { ushort port = TestHelper.GetPort(); using var encrypter = new EasyEncrypt(); using var server = new EasyTcpServer().UseServerEncryption(encrypter).Start(port); server.OnDataReceive += (sender, message) => { Console.WriteLine(message); message.Client.Send(message); }; using var client = new EasyTcpClient().UseClientEncryption(encrypter); Assert.IsTrue(client.Connect(IPAddress.Any, port)); Assert.AreEqual("Test", client.SendAndGetReply("Test").ToString()); }
public void SendEncryptedData() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpServer().Start(port); server.OnDataReceive += (sender, message) => message.Client.Send(message); var encryption = new EasyEncrypt(); using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, port)); string data = "123"; var m = client.SendAndGetReply(EasyTcpPacket.To <Message>(data).Encrypt(encryption).Compress()); Assert.AreEqual(data, m.Decompress().Decrypt(encryption).ToString()); }
public static void Connect() { var client = new EasyTcpClient(); /* OnConnect event, * triggered when client connects to the server */ client.OnConnect += (sender, c) => Console.WriteLine("Client: Connected to server"); /* OnDisconnect event, * triggered when client disconnects from server */ client.OnDisconnect += (sender, c) => { Console.WriteLine("Client: Disconnected from server"); }; /* OnDataReceive event, * triggered when client receives any data from the server */ client.OnDataReceive += (sender, message) => { Console.WriteLine($"Client: Received data, received: {message.ToString()}"); }; /* OnError get triggered when an error occurs in the server code, * This includes errors in the events because these are triggered by the server */ client.OnError += (sender, exception) => Console.WriteLine($"Server: Error occured, message: {exception.Message}"); /* Connect to server on ip 127.0.0.1 and port 5_000 (Our BasicServer.cs) * See ConnectUtil.cs and ConnectAsyncUtil.cs for the connect functions */ bool connected = client.Connect("127.0.0.1", Port); if (connected) { client.Send("Hello everyone!"); } else { Console.WriteLine("Client: Could not connect to server"); } // Send a message and get the reply Message reply = client.SendAndGetReply("Hello server!"); }
public void TestSending() { ushort port = TestHelper.GetPort(); var certificate = new X509Certificate2("certificate.pfx", "password"); using var protocol = new PrefixLengthSslProtocol(certificate); using var server = new EasyTcpServer(protocol).Start(port); server.OnDataReceive += (sender, message) => message.Client.Send(message); using var cprotocol = new PrefixLengthSslProtocol("localhost", true); using var client = new EasyTcpClient(cprotocol); Assert.IsTrue(client.Connect("127.0.0.1", port)); var message = client.SendAndGetReply("Test"); Assert.AreEqual("Test", message.ToString()); }
public void TestSplittingData() { var certificate = new X509Certificate2("certificate.pfx", "password"); ushort port = TestHelper.GetPort(); var protocol = new DelimiterSslProtocol("\r\n", certificate); using var server = new EasyTcpServer(protocol).Start(port); server.OnDataReceive += (sender, message) => message.Client.Send(message); using var client = new EasyTcpClient(new DelimiterSslProtocol("\r\n", "localhost", true, false)); Assert.IsTrue(client.Connect("127.0.0.1", port)); var data = "test\r\nMessage"; var reply = client.SendAndGetReply(data); Assert.IsNotNull(reply); Assert.AreEqual("test", reply.ToString()); }
public void TestJsonNetSerialization() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpServer().Start(port); server.OnDataReceive += (sender, message) => message.Client.Send(message); using var client = new EasyTcpClient { Serialize = o => Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(o)), Deserialize = (b, t) => JsonConvert.DeserializeObject(Encoding.UTF8.GetString(b), t) }; Assert.IsTrue(client.Connect(IPAddress.Loopback, port)); var testData = new List <string> { "testdata", "testdata2" }; var reply = client.SendAndGetReply(testData); Assert.AreEqual(testData, reply.Deserialize <List <string> >()); }
public static void RunSpeedTest() { var client = new EasyTcpClient(); if (!client.Connect(IPAddress.Loopback, Port)) { return; } byte[] message = Encoding.UTF8.GetBytes(Message); Stopwatch sw = new Stopwatch(); sw.Start(); for (int x = 0; x < MessageCount; x++) { client.SendAndGetReply(message); } sw.Stop(); Console.WriteLine($"ElapsedMilliseconds SpeedTest: {sw.ElapsedMilliseconds}"); Console.WriteLine($"Average SpeedTest: {sw.ElapsedMilliseconds / (double) MessageCount}"); }
public void TestEncryptionFail() { ushort port = TestHelper.GetPort(); using var encrypter = new EasyEncrypt(); using var server = new EasyTcpServer().Start(port); int isEncrypted = 0; server.OnDataReceive += (sender, message) => { if (message.ToString() != "Test") { Interlocked.Increment(ref isEncrypted); } Console.WriteLine(message); message.Client.Send(message); }; using var client = new EasyTcpClient().UseClientEncryption(encrypter); Assert.IsTrue(client.Connect(IPAddress.Any, port)); Assert.AreEqual("Test", client.SendAndGetReply("Test").ToString()); Assert.AreEqual(1, isEncrypted); }
/// <summary> /// Send data (uint) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <returns>received reply</returns> public static Message SendAndGetReply(this EasyTcpClient client, uint data, TimeSpan?timeout = null) => client.SendAndGetReply(BitConverter.GetBytes(data), timeout);
public static async Task Connect() { using var client = new EasyTcpClient(); var socket = client.BaseSocket; // Get baseSocket, null when client is disposed/disconnected // Connect to remote host with IP and Port if (!client.Connect("127.0.0.1", Port)) { return; } if (!client.Connect(IPAddress.Loopback, Port)) { return; } // Connect to remote host with IP, Port and timeout (Default: 5 seconds) if (!client.Connect("127.0.0.1", Port, TimeSpan.FromSeconds(10))) { return; } // Async connect to remote host with IP and Port if (!await client.ConnectAsync("127.0.0.1", Port)) { return; } // Async send data to server client.Send("Hello server!"); client.Send(1); client.Send(1.000); client.Send(true); client.Send("Hello server!", true); // Async send compressed data /* Send data to server and get reply * OnDataReceive is not triggered when receiving a reply * * ! SendAndGetReply does not work in OnDataReceive * ! first call client.Protocol.EnsureDataReceiverIsRunning(client) when using in OnDataReceive */ var reply = client.SendAndGetReply("data"); // Send data to server and get reply with a specified timeout (Default: 5 seconds) // SendAndGetReply returns null when no message is received within given time frame var replyWithTimeout = client.SendAndGetReply("data", TimeSpan.FromSeconds(5)); // Send data to server and get reply async var reply2 = await client.SendAndGetReplyAsync("data"); /* Get next received data as variable * OnDataReceive is not triggered */ var receivedData = await client.ReceiveAsync(); // Use timeout (Default: infinite) // Returns null when no message is received within given time frame var receivedDataWithTimeout = await client.ReceiveAsync(TimeSpan.FromSeconds(5)); // Determines whether client is still connected bool isConnected = client.IsConnected(); bool isConnectedWithPoll = client.IsConnected(true); // Use poll for a more accurate (but slower) result }
/// <summary> /// Send data (IEasyTcpPacket) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendAndGetReply(this EasyTcpClient client, object data, TimeSpan?timeout = null, bool compression = false) => client.SendAndGetReply(client?.Serialize(data), timeout, compression);
/// <summary> /// Send data (IEasyTcpPacket) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendAndGetReply(this EasyTcpClient client, IEasyTcpPacket data, TimeSpan?timeout = null, bool compression = false) => client.SendAndGetReply(data.Data, timeout, compression);
/// <summary> /// Send data (string) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="data">data to send to remote host</param> /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param> /// <param name="encoding">encoding type (Default: UTF8)</param> /// <param name="compression">compress data using GZIP if set to true</param> /// <returns>received reply</returns> public static Message SendAndGetReply(this EasyTcpClient client, string data, TimeSpan?timeout = null, Encoding encoding = null, bool compression = false) => client.SendAndGetReply((encoding ?? Encoding.UTF8).GetBytes(data), timeout, compression);