Beispiel #1
0
 public void Send <T>(IdentifiedClient identifiedClient, T data)
 {
     if (identifiedClient == null)
     {
         foreach (var c in _connectedClients)
         {
             _dataProvider.SendData(c, data);
         }
     }
     else
     {
         _dataProvider.SendData(identifiedClient, data);
     }
 }
Beispiel #2
0
        public bool SendData <T>(IdentifiedClient identifiedClient, T data)
        {
            if (!identifiedClient.TcpClient.Connected)
            {
                return(false);
            }

            var serializedData = Serialize(data);

            var dataString = serializedData.ToString();

            var dataBytes = dataString.GetBytes();

            identifiedClient.TcpClient.GetStream().Write(dataBytes, 0, dataBytes.Length);

            _server.L.Log("TCP sent: " + dataString);

            return(true);
        }
Beispiel #3
0
        private void ClientAcceptedCallback(IAsyncResult ar)
        {
            Task.Run(() =>
            {
                try
                {
                    using (var connectedClient = new IdentifiedClient(_listener.EndAcceptTcpClient(ar)))
                    {
                        _connectedClients.Add(connectedClient);

                        using (var stream = connectedClient.TcpClient.GetStream())
                        {
                            while (connectedClient.TcpClient.Connected)
                            {
                                var data = _dataProvider.ReceiveData(stream);

                                try
                                {
                                    var m = UnpackResponse(data, connectedClient);

                                    if (m != null)
                                    {
                                        M.SendAsync(m);
                                    }
                                }
                                catch (Exception e)
                                {
                                    L.Error("Exception in TcpServer: " + e.Message);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    ExceptionOccurred?.Invoke(this, e);
                }
            });

            Log("Client connected");
            WaitForClient();
        }
Beispiel #4
0
 public ExternalMessage UnpackResponse(object response, IdentifiedClient sender)
 {
     return(!(_dataProvider.Deserialize(response) is BaseTransportMessage message) ? null : new ExternalMessage(sender?.Id, message));
 }