public void SendActionAndGetReplyBool() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); var m = client.SendActionAndGetReply(0, true, _timeout); Assert.AreEqual(true, m.ToBool()); var m2 = client.SendActionAndGetReply("ECHO", true); Assert.AreEqual(true, m2.ToBool()); }
public void SendActionAndGetReplyUInt() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); uint data = 123; var m = client.SendActionAndGetReply(0, data, _timeout); Assert.AreEqual(data, m.ToUInt()); var m2 = client.SendActionAndGetReply("ECHO", data); Assert.AreEqual(data, m2.ToUInt()); }
public void SendActionAndGetReplyArray() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); byte[] data = new byte[100]; var m = client.SendActionAndGetReply(0, data); Assert.IsTrue(data.SequenceEqual(m.Data)); var m2 = client.SendActionAndGetReply("ECHO", data); Assert.IsTrue(data.SequenceEqual(m2.Data)); }
public static void Connect() { var client = new EasyTcpClient(); if (!client.Connect("127.0.0.1", Port)) { return; } // All actions are send as an int, this is the action id of the "ECHO" action. // Integers can also be used as action codes. However this is not very readable, so when using integers enums are recommend! // Because everything is send as an int collisions are possible. (The algorithm used is named djb2a [http://www.cse.yorku.ca/~oz/hash.html]) // This is no problem because this is very rare, however keep it in mind! // Try for example these two: haggadot & loathsomenesses int actionCode = "ECHO".ToActionCode(); // Execute the "ECHO" action on our echo server client.SendAction("ECHO", "Hello me!"); // Execute the "BROADCAST" action on our echo server client.SendAction("BROADCAST", "Hello everyone!"); // Get the reply from our message var message = client.SendActionAndGetReply(actionCode, "Hello me!"); }
public static void Connect() { var client = new EasyTcpClient(); if (!client.Connect("127.0.0.1", Port)) { return; } // Send action client.SendAction("ECHO", "Hello me!"); client.SendAction("BROADCAST", "Hello everyone!"); // Send action and get reply var reply = client.SendActionAndGetReply("ECHO", "Hello me!"); Console.WriteLine(reply); /* All actions are converted to an int(actionCode), this is the actionCode of the "ECHO" action. * Integers can also be used as action codes. However this is not very readable. * Because everything is send as an int collisions are possible. (The algorithm used is named djb2a [http://www.cse.yorku.ca/~oz/hash.html]) * This is no problem because this is extremely rare, however keep it in mind! */ int actionCode = "ECHO".ToActionCode(); Console.WriteLine(actionCode); Console.WriteLine(actionCode.IsEqualToAction("ECHO2")); }
public void TestServer() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpActionServer(); server.Start(port); using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Loopback, port)); string data = "test"; var reply = client.SendActionAndGetReply(0, data); Assert.AreEqual(data, reply.ToString()); reply = client.SendActionAndGetReply("ECHO", data); Assert.AreEqual(data, reply.ToString()); }
public void TestInterceptorTrue() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpActionServer { Interceptor = (actionCode, m) => true }; server.Start(port); using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Loopback, port)); var message = client.SendActionAndGetReply("ECHO", "data"); Assert.IsNotNull(message); // Client did receive a message }
public void SendActionAndGetReplyTriggerOnDataReceive() { 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.SendActionAndGetReply(0, data); Assert.IsTrue(data.SequenceEqual(m.Data)); Assert.IsFalse(triggered); }
public void TestInterceptorTrue() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpActionServer { Interceptor = action => action.ActionCode.IsEqualToAction("ECHO") && action.ToString() == "data" }.Start(port); using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Loopback, port)); var message = client.SendActionAndGetReply("ECHO", "data"); Assert.IsNotNull(message); }
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.SendActionAndGetReply("ECHO", message); } sw.Stop(); Console.WriteLine($"ElapsedMilliseconds SpeedTest: {sw.ElapsedMilliseconds}"); Console.WriteLine($"Average SpeedTest: {sw.ElapsedMilliseconds / (double) MessageCount}"); }
/// <summary> /// Send action (short) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="action">action code as string</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 SendActionAndGetReply(this EasyTcpClient client, string action, short data, TimeSpan?timeout = null) => client.SendActionAndGetReply(action.ToActionCode(), BitConverter.GetBytes(data), timeout);
/// <summary> /// Send action (byte[]) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="action">action code as string</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, string action, byte[] data = null, TimeSpan?timeout = null, bool compression = false) => client.SendActionAndGetReply(action.ToActionCode(), data, timeout, compression);
/// <summary> /// Send action (object) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="action">action code as string</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, string action, object data, TimeSpan?timeout = null, bool compression = false) => client.SendActionAndGetReply(action.ToActionCode(), client?.Serialize(data), timeout, compression);
/// <summary> /// Send action (IEasyTcpPacket) 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, IEasyTcpPacket data, TimeSpan?timeout = null, bool compression = false) => client.SendActionAndGetReply(action, data.Data, timeout, compression);
/// <summary> /// Send action (string) to remote host. Then wait and return the reply /// </summary> /// <param name="client"></param> /// <param name="action">action code as string</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 SendActionAndGetReply(this EasyTcpClient client, string action, string data, TimeSpan?timeout = null, Encoding encoding = null, bool compression = false) => client.SendActionAndGetReply(action.ToActionCode(), (encoding ?? Encoding.UTF8).GetBytes(data), timeout, compression);
/// <summary> /// Send action (bool) 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> /// <returns>received reply</returns> public static Message SendActionAndGetReply(this EasyTcpClient client, int action, bool data, TimeSpan?timeout = null) => client.SendActionAndGetReply(action, BitConverter.GetBytes(data), timeout);