Пример #1
0
 /// <summary>
 /// Send action 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, 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, int action, byte[] data = null,
                                                               TimeSpan?timeout = null, bool compression = false)
 {
     if (compression && data != null)
     {
         data = CompressionUtil.Compress(data);
     }
     return(await client.SendAndGetReplyAsync(timeout, BitConverter.GetBytes(action), data));
 }
Пример #2
0
 /// <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 async Task <Message> SendAndGetReplyAsync(this EasyTcpClient client, byte[] data,
                                                         TimeSpan?timeout = null, bool compression = false)
 {
     if (compression)
     {
         data = CompressionUtil.Compress(data);
     }
     return(await client.SendAndGetReplyAsync(timeout, data));
 }
Пример #3
0
        public async Task SendAndGetReplyUShort()
        {
            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, _port));

            ushort data = 123;
            var    m    = await client.SendAndGetReplyAsync(data, _timeout);

            Assert.AreEqual(data, m.ToUShort());
        }
Пример #4
0
        public async Task SendAndGetReplyArray()
        {
            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, _port));

            byte[] data = new byte[100];
            var    m    = await client.SendAndGetReplyAsync(data, _timeout);

            Assert.IsTrue(data.SequenceEqual(m.Data));
        }
Пример #5
0
        public async Task SendAndGetReplyBool()
        {
            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Any, _port));

            bool data = true;
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            var m = await client.SendAndGetReplyAsync(data, _timeout);

            Assert.AreEqual(true, m.ToBool());
        }
Пример #6
0
        public async Task 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    = await client.SendAndGetReplyAsync(data);

            Assert.IsTrue(data.SequenceEqual(m.Data));
            Assert.IsFalse(triggered);
        }
Пример #7
0
 /// <summary>
 /// Send data (int) 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 async Task <Message> SendAndGetReplyAsync(this EasyTcpClient client, int data,
                                                         TimeSpan?timeout = null) =>
 await client.SendAndGetReplyAsync(BitConverter.GetBytes(data), timeout);
Пример #8
0
 /// <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 data or null</returns>
 public static async Task <Message> SendAndGetReplyAsync(this EasyTcpClient client, object data,
                                                         TimeSpan?timeout = null, bool compression = false)
 => await client.SendAndGetReplyAsync(client?.Serialize(data), timeout, compression);
Пример #9
0
 /// <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 data or null</returns>
 public static async Task <Message> SendAndGetReplyAsync(this EasyTcpClient client, IEasyTcpPacket data,
                                                         TimeSpan?timeout = null, bool compression = false)
 => await client.SendAndGetReplyAsync(data.Data, timeout, compression);
Пример #10
0
 /// <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 data or null</returns>
 public static async Task <Message> SendAndGetReplyAsync(this EasyTcpClient client, string data,
                                                         TimeSpan?timeout = null, Encoding encoding = null, bool compression = false)
 => await client.SendAndGetReplyAsync((encoding ?? Encoding.UTF8).GetBytes(data), timeout, compression);
Пример #11
0
        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
        }
 SendAndGetReplyAsync(this EasyTcpClient client, short data, TimeSpan?timeout = null) =>
 await client.SendAndGetReplyAsync(BitConverter.GetBytes(data), timeout);