Example #1
0
        /// <summary>
        /// Disconnects a client
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public bool DisconnectClient(string clientId)
        {
            TcpClientInfo client = RemoveClientFromRepository(clientId);

            client?.Dispose();
            return(client != null);
        }
Example #2
0
 /// <summary>
 /// Disconnects client
 /// </summary>
 public void Disconnect()
 {
     try
     {
         _client?.Dispose();
     } catch (Exception)
     {
     }
 }
Example #3
0
        /// <summary>
        /// Reads data from stream
        /// </summary>
        /// <param name="client"></param>
        /// <param name="clientInfo"></param>
        /// <param name="ct"></param>
        protected async Task ReadData(TcpClientInfo client)
        {
            // get buffer size
            int bufferSize = _config.SocketBufferSize;

            // Buffer for reading data
            byte[] buffer = new byte[bufferSize];

            // Get a stream object for reading and writing
            NetworkStream stream = client.Client.GetStream();

            // Loop to receive all the data sent by the client.
            stream.ReadTimeout = 2000;
            int i = 0;

            while (true)
            {
                // read data from steram
                try
                {
                    i = await stream.ReadAsync(buffer, 0, bufferSize);
                }
                catch (IOException)
                {
                    break;
                }
                catch (ObjectDisposedException)
                {
                    break;
                } catch (Exception)
                {
                    break;
                }

                // No data (end of strem)
                if (i == 0)
                {
                    break;
                }

                // received data
                var receivedMessages = client.DataProcessor.ProcessReceivedRawData(buffer, i);

                // message received
                if (receivedMessages != null && receivedMessages.Any())
                {
                    receivedMessages.ToList().ForEach((msg) => _ = DataReceived(msg, client.Info.Id));
                }
            }

            // Close connection
            client.Dispose();
        }