Beispiel #1
0
        public User(TcpClient tcpClient, IMessageSender sender)
        {
            _tcpClient = tcpClient;
            Stream     = _tcpClient.GetStream();
            Id         = Guid.NewGuid().ToString();
            Name       = Constants.DefaultIserName;

            _sender = sender;

            _processingConveyor = new ProcessingConveyor();
            _processingConveyor.LetterReceived  += OnMessageTransfer;
            _processingConveyor.NameSetReceived += OnSetName;
        }
Beispiel #2
0
        private void Listen()
        {
            while (true)
            {
                try
                {
                    byte[]        data    = new byte[512];
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;
                    do
                    {
                        bytes = Stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.UTF8.GetString(data, 0, bytes));
                    }while (Stream.DataAvailable);

                    var message = builder.ToString();
                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        var messages = message.Split('$');
                        foreach (var protocolMessage in messages)
                        {
                            if (string.IsNullOrWhiteSpace(protocolMessage))
                            {
                                continue;
                            }
                            ProcessingConveyor.Process(protocolMessage);
                        }
                    }
                }
                catch (Exception e)
                {
                    ConnectFailed?.Invoke();
                    break;
                }
            }
        }
Beispiel #3
0
 public ConnectManager()
 {
     ProcessingConveyor = new ProcessingConveyor();
     TcpClient          = new TcpClient();
 }