public async Task SendActionAndGetReplyAsyncBool() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); var m = await client.SendActionAndGetReplyAsync(0, true, _timeout); Assert.AreEqual(true, m.ToBool()); var m2 = await client.SendActionAndGetReplyAsync("ECHO", true); Assert.AreEqual(true, m2.ToBool()); }
public async Task SendActionAndGetReplyAsyncUInt() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); uint data = 123; var m = await client.SendActionAndGetReplyAsync(0, data, _timeout); Assert.AreEqual(data, m.ToUInt()); var m2 = await client.SendActionAndGetReplyAsync("ECHO", data); Assert.AreEqual(data, m2.ToUInt()); }
public async Task SendActionAndGetReplyAsyncArray() { using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Any, _port)); byte[] data = new byte[100]; var m = await client.SendActionAndGetReplyAsync(0, data, _timeout); Assert.IsTrue(data.SequenceEqual(m.Data)); var m2 = await client.SendActionAndGetReplyAsync("ECHO", data); Assert.IsTrue(data.SequenceEqual(m2.Data)); }
public async Task TestInterceptorFalse() { ushort port = TestHelper.GetPort(); using var server = new EasyTcpActionServer { Interceptor = (actionCode, m) => false }; // Create useless server server.Start(port); using var client = new EasyTcpClient(); Assert.IsTrue(client.Connect(IPAddress.Loopback, port)); var message = await client.SendActionAndGetReplyAsync("ECHO", "data", TimeSpan.FromMilliseconds(500)); Assert.IsNull(message); // Client did not receive a message }
public async Task SendActionAndGetReplyAsyncTriggerOnDataReceive() { 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 = await client.SendActionAndGetReplyAsync(0, data); Assert.IsTrue(data.SequenceEqual(m.Data)); Assert.IsFalse(triggered); }
/// <summary> /// Send action 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, return null when time expires</param> /// <param name="compression">compress data using deflate if set to true</param> /// <returns>received reply</returns> public static async Task <Message> SendActionAndGetReplyAsync(this EasyTcpClient client, string action, IEasyPacket data, TimeSpan?timeout = null, bool compression = false) => await client.SendActionAndGetReplyAsync(action.ToActionCode(), data.Data, timeout, compression);
/// <summary> /// Send action 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, return null when time expires</param> /// <param name="encoding">encoding type (default: UTF8)</param> /// <param name="compression">compress data using deflate if set to true</param> /// <returns>received reply</returns> public static async Task <Message> SendActionAndGetReplyAsync(this EasyTcpClient client, string action, string data, TimeSpan?timeout = null, Encoding encoding = null, bool compression = false) => await client.SendActionAndGetReplyAsync(action.ToActionCode(), (encoding ?? Encoding.UTF8).GetBytes(data), timeout, compression);
/// <summary> /// Send action 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, return null when time expires</param> /// <param name="compression">compress data using deflate if set to true</param> /// <returns>received reply</returns> public static async Task <Message> SendActionAndGetReplyAsync(this EasyTcpClient client, string action, object data, TimeSpan?timeout = null, bool compression = false) => await client.SendActionAndGetReplyAsync(action.ToActionCode(), client?.Serialize(data), timeout, compression);
SendActionAndGetReplyAsync(this EasyTcpClient client, string action, short data, TimeSpan?timeout = null) => await client.SendActionAndGetReplyAsync(action.ToActionCode(), BitConverter.GetBytes(data), timeout);
SendActionAndGetReplyAsync(this EasyTcpClient client, int action, ushort data, TimeSpan?timeout = null) => await client.SendActionAndGetReplyAsync(action, BitConverter.GetBytes(data), timeout);
/// <summary> /// Send action with data (int) to the remote host. Then wait for a reply from the server. /// </summary> /// <param name="client"></param> /// <param name="action">action code as string</param> /// <param name="data">data to send to server</param> /// <param name="timeout">maximum time to wait for a reply, if time expired: return null</param> /// <returns>received data or null</returns> public static async Task <Message> SendActionAndGetReplyAsync(this EasyTcpClient client, string action, int data, TimeSpan?timeout = null) => await client.SendActionAndGetReplyAsync(action.ToActionCode(), BitConverter.GetBytes(data), timeout);