Ejemplo n.º 1
0
        public RoomMember(Guid sessionId, TcpClient client, String nickName, IMessenger roomMessenger, Boolean sendEcho)
        {
            SessionId      = sessionId;
            _nickName      = nickName;
            _roomMessenger = roomMessenger;
            _sendEcho      = sendEcho;
            _buffer        = new ConnectionBuffer(true);
            _buffer.SetClient(client);
            _messageDictionary = new Dictionary <MessageType, Action <Byte[]> >()
            {
                { MessageType.Text, HandleTextMessage },
                { MessageType.KeepAlive, (data) => { } }
            };

            roomMessenger.Register <TextReceiveMessage>(this, RoomMessageTextReceived);
            ThreadPool.QueueUserWorkItem(MessageProcessingThread);
        }
        private void CheckWhoIsIt(TcpClient client)
        {
            try
            {
                var stream   = client.GetStream();
                var responce = stream.ReadObject <NetworkMessage>();
                if (responce.Type != MessageType.ConnectionRequest)
                {
                    return;
                }
                if (responce.Data == null)
                {
                    return;
                }

                var serverType = (ServerType)BitConverter.ToInt32(responce.Data, 0);
                switch (serverType)
                {
                case ServerType.Initiator:
                    if (_initiatorClient == null)
                    {
                        _initiatorClient = new ConnectionBuffer(responce.SessionId.ToGuid(), _config.NumberOfThreads);
                    }
                    _initiatorClient.SetClient(client);
                    break;

                case ServerType.Echo:
                    if (_initiatorClient == null)
                    {
                        _echoClient = new ConnectionBuffer(responce.SessionId.ToGuid(), _config.NumberOfThreads);
                    }
                    _echoClient.SetClient(client);
                    break;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        }
Ejemplo n.º 3
0
        private async Task <Boolean> TryConnectAsync()
        {
            try
            {
                var tcpClient = new TcpClient();
                await tcpClient.ConnectAsync(_config.NetworkConfig.Host, _config.NetworkConfig.Port);

                var stream = tcpClient.GetStream();
                await stream.WriteObjectAsync(new ConnectionRequest()
                {
                    RoomId    = _config.RoomId,
                    SessionId = _sessionId,
                    NickName  = _config.NickName
                });

                var response = await stream.ReadObjectAsync <ConnectionResponse>();

                if (response.SessionId == Guid.Empty)
                {
                    return(false);
                }

                _sessionId = response.SessionId;
                _bufferedClient.SetClient(tcpClient);
                return(true);
            }
            catch (Exception ex)
            {
                if (ex is SocketException || ex is IOException)
                {
                    return(false);
                }

                Debug.WriteLine(ex.Message);
                throw;
            }
        }
Ejemplo n.º 4
0
        private Boolean TryConnect()
        {
            try
            {
                var tcpClient = new TcpClient(_config.ProxyHost, _config.ProxyPort);
                _bufferedClient.SetClient(tcpClient);
                _bufferedClient.SendMessageQueue.Enqueue(new NetworkMessage()
                {
                    Type = MessageType.ConnectionRequest,
                    Data = BitConverter.GetBytes((Int32)Type)
                });
                return(true);
            }
            catch (Exception ex)
            {
                if (ex is SocketException || ex is IOException)
                {
                    return(false);
                }

                Debug.WriteLine(ex.Message);
                throw;
            }
        }
Ejemplo n.º 5
0
 // FUNCTIONS //////////////////////////////////////////////////////////////////////////////
 public void RefreshConnection(TcpClient client)
 {
     _buffer.SetClient(client);
 }