Exemplo n.º 1
0
        private byte[] HandleMessage(TcpClient client, byte[] data)
        {
            int i = 0;

            NetworkStream networkStream = client.GetStream();

            byte[] respData = new byte[_maxResponseBufferSize];

            if (data.Length > 0)
            {
                _messageWriter.WriteLine("--->Recvd {0} bytes from client", data.Length);
                _messageWriter.WriteLine(ConversionUtils.ConvertToString(data));

                //write new data from client to socket
                networkStream.Write(data, 0, data.Length);
            }

            try
            {
                if (networkStream.DataAvailable)
                {
                    i = networkStream.Read(respData, 0, respData.Length);
                    _messageWriter.WriteLine("Read Resp: {0}", i);
                }
            }
            catch (Exception except)
            {
                _messageWriter.WriteLine("Error: {0}", except.Message);
            }

            Array.Resize(ref respData, i);

            return(respData);
        }
Exemplo n.º 2
0
        private void Tunnel_DataReceived(object sender, DataReceivedEventArgs e)
        {
            byte[] data     = e.Data;
            string response = ConversionUtils.ConvertToString(data);

            _messageWriter.WriteLine("Response from Server: " + response);
        }
Exemplo n.º 3
0
        public void Send(byte[] data)
        {
            byte[] messageBuffer = new byte[MAX_MESSAGE_SIZE];
            int    currentIndex  = 0;

            while (currentIndex < data.Length)
            {
                messageBuffer = data.Skip(currentIndex).Take(MAX_MESSAGE_SIZE).ToArray();
                string messageString = ConversionUtils.ConvertToString(messageBuffer);
                _service.SendDirectMessage(_serverUserName, messageString);
                currentIndex += MAX_MESSAGE_SIZE;
            }
        }
Exemplo n.º 4
0
        public void HandleMessages()
        {
            int i;

            Byte[] temp = new Byte[_maxClientBufferSize];
            Byte[] bytes;


            _networkStream = _client.GetStream();

            // Loop to receive all the data sent by the client.
            while (_client.Connected && !_disconnect)
            {
                i = 0;

                try
                {
                    i = _networkStream.Read(temp, 0, temp.Length);
                }
                catch
                {
                    _disconnect = true;
                }


                if (i > 0)
                {
                    bytes = new byte[i + (int)HeaderIndex.HeaderSize];

                    //copy connection number into transmit array.
                    byte[] connNumBytes = BitConverter.GetBytes(_socketId);
                    Array.Copy(connNumBytes, 0, bytes, (int)HeaderIndex.ConnectionNumber, sizeof(UInt16));

                    //copy data from networkstream into transmit array
                    Array.Copy(temp, 0, bytes, (int)HeaderIndex.HeaderSize, i);

                    _messageWriter.WriteLine("--->Sent {0} bytes to server", bytes.Length);
                    _messageWriter.WriteLine(ConversionUtils.ConvertToString(bytes));
                    _tunnel.Send(bytes);
                }
                else
                {
                    break;
                }
            }

            // Shutdown and end connection
            _tunnel.DataReceived -= new EventHandler <DataReceivedEventArgs>(Tunnel_DataReceived);
            _client.Close();
        }