Example #1
0
        /// <summary>
        /// Handle client
        /// </summary>
        /// <returns></returns>
        private async Task HandleAsync()
        {
            byte[] buffer, bufferSize = new byte[2];
            int size;

            try
            {
                NetworkStream clientStream = Client.GetStream();
                while (Client.Connected)
                {
                    int bytesRead = await clientStream.ReadAsync(bufferSize, 0, bufferSize.Length);

                    if (bytesRead == 0)
                        break;

                    using (PacketReader pr = new PacketReader(bufferSize))
                    {
                        size = pr.ReadInt16();
                    }

                    buffer = new byte[size];
                    await clientStream.ReadAsync(buffer, 0, buffer.Length);
                    if (OnDataReceived != null)
                        OnDataReceived(buffer);
                }
            }
            catch(Exception ex)
            {
                //HResult == -2146232800 = Server closed
                if(Connected && ex.HResult != -2146232800)
                    DebugMessage("Failed to handle client : " + ex.Message);
            }
            finally
            {
                Disconnect();
            }
        }
Example #2
0
        /// <summary>
        /// Handle request client
        /// </summary>
        /// <param name="client">Client</param>
        /// <returns></returns>
        private async Task HandleClientRequestAsync(TcpClient client)
        {
            byte[] buffer, bufferSize = new byte[2];
            int size;

            try
            {
                NetworkStream clientStream = client.GetStream();
                while (client.Client.Connected)
                {
                    int bytesRead = await clientStream.ReadAsync(bufferSize, 0, bufferSize.Length);

                    if (bytesRead == 0)
                        break;

                    using (PacketReader pr = new PacketReader(bufferSize))
                    {
                        size = pr.ReadInt16();
                    }

                    buffer = new byte[size];
                    await clientStream.ReadAsync(buffer, 0, buffer.Length);

                    if (OnRequestReceived != null)
                        OnRequestReceived(client, buffer);
                }
            }
            catch (Exception ex)
            {
                DebugMessage("Failed to handle client request : " + ex.Message);
            }
        }
Example #3
0
        /// <summary>
        /// Send a request
        /// </summary>
        /// <param name="buffer">Buffer to send</param>
        /// <returns>Response</returns>
        public byte[] SendRequest(byte[] buffer)
        {
            if (ClientRequest != null)
            {
                try
                {
                    byte[] b = eUtils.GetBuffer(buffer);
                    ClientRequest.Send(b);

                    byte[] result, resultSize = new byte[2];
                    int size;


                    ClientRequest.GetStream().Read(resultSize, 0, resultSize.Length);

                    using (PacketReader pr = new PacketReader(resultSize))
                    {
                        size = pr.ReadInt16();
                    }

                    result = new byte[size];

                    ClientRequest.GetStream().Read(result, 0, result.Length);

                    return result;
                }
                catch (Exception ex)
                {
                    DebugMessage("Failed to send request : " + ex.Message);
                }
            }
            else
            {
                DebugMessage("This client cant send request");
            }
            return null;
        }