public void BeginReadingFromClients()
        {
            Task.Run(async() =>
            {
                while (true)
                {
                    var clients = _clientsHolder.ClientConnections.TakeCopy();

                    foreach (var client in clients)
                    {
                        try
                        {
                            if (client.Stream.DataAvailable)
                            {
                                var header = await _networkDataService.ReadAndDecodeHeader(client.Stream);

                                var message = await _networkDataService.ReadAndDecodeMessage(header, client.Stream);

                                await ProcessMessage(message);
                            }
                        }
                        catch (System.Exception e)
                        {
                            Console.WriteLine("Begin reading exception: " + e.Message + " on client with id: " + client.UserId);
                        }
                    }
                }
            });
        }
        public void BeginListeningForMessages()
        {
            if (_currentUser.NetworkStream == null)
            {
                throw new InvalidOperationException("Connect() must be called prior to reading messages.");
            }


            Task.Run(async() =>
            {
                while (!_cancellationToken.IsCancellationRequested)
                {
                    if (_currentUser.NetworkStream.DataAvailable)
                    {
                        Debug.WriteLine("Data being read.");
                        try
                        {
                            var header = await _networkDataService.ReadAndDecodeHeader(_currentUser.NetworkStream);

                            var message = await _networkDataService.ReadAndDecodeMessage(header, _currentUser.NetworkStream);

                            if (message.MessageType == MessageType.UserOffline)
                            {
                                Console.WriteLine("User Offline message recveid ......");
                            }

                            MessageReceivedEventHandler.Invoke(this, new MessageReceivedEventArgs {
                                Message = message, TimeStamp = header.TimeStamp
                            });
                        }
                        catch (Exception e)
                        {
                            throw e;
                        }
                    }
                }

                _currentUser.NetworkStream.Close();
            }, _cancellationToken);
        }
Example #3
0
        public async Task <bool> TryAddUser(TcpClient newClient)
        {
            var stream = newClient.GetStream();

            stream.ReadTimeout = 1000;

            IMessage message;

            try
            {
                var header = await _networkDataService.ReadAndDecodeHeader(stream);

                message = await _networkDataService.ReadAndDecodeMessage(header, stream);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                return(false);
            }

            return(await ProcessMessage(message, stream));
        }