Example #1
0
        public void ReadData(TcpClient tcpClient, ServerThreadPool cn)
        {
            _nstream = tcpClient.GetStream();
            _client  = tcpClient;

            new Thread(Read).Start();
        }
Example #2
0
        public void ReadData(TcpClient tcpClient, ServerThreadPool cn)
        {
            _nstream = tcpClient.GetStream();
            _client  = tcpClient;

            Task.Factory.StartNew(Read);
        }
Example #3
0
        public async void ReadData(TcpClient tcpClient, ServerThreadPool cn)
        {
            _nstream  = tcpClient.GetStream();
            _client   = tcpClient;
            Connected = true;

            try
            {
                while (true)
                {
                    byte[] buffer    = new byte[2];
                    int    bytesRead = await _nstream.ReadAsync(buffer, 0, 2);

                    if (bytesRead != 2)
                    {
                        throw new Exception("Wrong packet");
                    }

                    short length = BitConverter.ToInt16(buffer, 0);

                    buffer    = new byte[length];
                    bytesRead = await _nstream.ReadAsync(buffer, 0, length);

                    if (bytesRead != length)
                    {
                        throw new Exception("Wrong packet");
                    }

                    Task.Factory.StartNew(() => _packetHandler.Handle(buffer.ToPacket(), this));
                }
            }
            catch (Exception e)
            {
                Log.Error($"ServerThread: {e.Message}");
                Termination();
            }
        }