public async Task ProcessAsync(IMessageProtocol messageProtocol)
        {
            if (Equals(messageProtocol.Header, MessageType.Message))
            {
                var message = messageProtocol.GetDataAs <Message>();

                try
                {
                    var meseClient = _clientsPool.FindClient(message.To.Id);

                    if (meseClient != null)
                    {
                        await MessageCommunicator.WriteAsync(meseClient.TcpClient, messageProtocol);
                    }
                    else
                    {
                        Logger.Error($"Client {message.To.Name} not found. ");
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error($"Exception: {ex}.");
                }
                return;
            }
            await _innerProcessor.ProcessAsync(messageProtocol);
        }
Esempio n. 2
0
            public TestModeCommunication()
            {
                comGen        = new TestModeCommand();
                commandServer = new MessageCommunicator();
                commandServer.MessageReceived += new MessageCommunicator.MessageReceivedEventHandler(commandServer_MessageReceived);

                sensorServer = new MessageCommunicator();
                sensorServer.MessageReceived += new MessageCommunicator.MessageReceivedEventHandler(sensorServer_MessageReceived);
            }
Esempio n. 3
0
        private async Task BroadcastClientConnectedAsync(User user, IEnumerable <IClient> others)
        {
            var messageProtocol = new MessageProtocol
            {
                Header = MessageType.ClientConnectedOthers,
                Data   = user
            };

            var tcpClients = others.Select(i => i.TcpClient).ToArray();
            await MessageCommunicator.BroadcastAsync(tcpClients, messageProtocol);
        }
Esempio n. 4
0
        public async Task ShutDownAsync()
        {
            _listening = false;

            DeregisterEvents();

            var messageProtocol = new MessageProtocol
            {
                Header = MessageType.ClientDisconnected,
                Data   = Me.User
            };

            await MessageCommunicator.WriteAsync(_tcpClient, messageProtocol);

            _tcpClient.Close();
        }
Esempio n. 5
0
        private async Task SendConnectionConfirmationAsync(IClient meseClient, IEnumerable <IClient> others)
        {
            var connectionEstablished = new ConnectionEstablished
            {
                Me     = meseClient.User,
                Others = others.Select(i => i.User).ToArray()
            };

            var messageProtocol = new MessageProtocol
            {
                Header = MessageType.ClientConnectedSelf,
                Data   = connectionEstablished
            };

            await MessageCommunicator.WriteAsync(meseClient.TcpClient, messageProtocol);
        }
Esempio n. 6
0
        private async Task <IClient> GetMeseClientInformation(TcpClient client)
        {
            MeseClient meseClient = null;

            WaitForClient(client);

            var messageProtocol = await MessageCommunicator.ReadAsync(client);

            var name = messageProtocol.GetDataAs <string>();

            if (!string.IsNullOrEmpty(name))
            {
                meseClient = new MeseClient(client, name);
            }

            return(meseClient);
        }
Esempio n. 7
0
        private void Listen()
        {
            new Thread(
                async() =>
            {
                while (_listening)
                {
                    while (_tcpClient.Available <= 0)
                    {
                        Thread.Sleep(100);
                    }

                    var messageProtocol = await MessageCommunicator.ReadAsync(_tcpClient);
                    await _protocolProcessor.ProcessAsync(messageProtocol);
                }
            }).Start();
        }
Esempio n. 8
0
        private async Task BroadcastClientDisconnectedAsync(User user)
        {
            if (user != null && _clientsPool.Remove(user.Id))
            {
                var messageProtocol = new MessageProtocol
                {
                    Header = MessageType.ClientDisconnected,
                    Data   = user
                };

                var tcpClients = _clientsPool.ConnectedClients.Select(i => i.Value.TcpClient).ToArray();
                if (tcpClients.Any())
                {
                    await MessageCommunicator.BroadcastAsync(tcpClients, messageProtocol);
                }
            }
        }
Esempio n. 9
0
        private async Task ForcelyDisconnectClient(IClient user)
        {
            if (user?.User != null && _clientsPool.Remove(user.User.Id))
            {
                var messageProtocol = new MessageProtocol
                {
                    Header = MessageType.ClientDisconnected,
                    Data   = user.User
                };

                var tcpClients = _clientsPool.ConnectedClients.Select(i => i.Value.TcpClient).ToArray();
                if (tcpClients.Any())
                {
                    await MessageCommunicator.BroadcastAsync(tcpClients, messageProtocol);
                }
            }

            Logger.Info($"Client disconnected abruptely. {user}");
        }
Esempio n. 10
0
        public async Task StartListeningAsync()
        {
            while (true)
            {
                try
                {
                    var messageProtocol = await MessageCommunicator.ReadAsync(_meseClient.TcpClient);

                    MessageProtocolReceived?.Invoke(this, new MessageProtocolReceivedEventArgs(messageProtocol));
                }
                catch (IOException exception)
                {
                    ThrowDisconnectException(exception);
                }
                catch (SocketException exception)
                {
                    ThrowDisconnectException(exception);
                }
            }
        }
Esempio n. 11
0
        public async Task SendMessageAsync(string text, User to)
        {
            if (string.IsNullOrEmpty(text) || to == null)
            {
                return;
            }

            var message = new Message
            {
                To   = to,
                From = Me.User,
                Text = text
            };

            var messageProtocol = new MessageProtocol
            {
                Header = MessageType.Message,
                Data   = message
            };

            await MessageCommunicator.WriteAsync(_tcpClient, messageProtocol);
        }
Esempio n. 12
0
        public async Task ConnectAsync(string name, ISettings settings)
        {
            if (string.IsNullOrEmpty(name))
            {
                return;
            }

            var host = ResolveHostname(settings);

            await ConnectAsyncWithTimeout(_tcpClient, host, settings.Port);

            var messageProtocol = new MessageProtocol
            {
                Header = MessageType.ClientConnection,
                Data   = name
            };

            await MessageCommunicator.WriteAsync(_tcpClient, messageProtocol);

            _listening = true;

            Listen();
        }
Esempio n. 13
0
        protected void Page_Load(object sender, EventArgs e)
        {
            _settings = new SettingsManager("CheongBot");
            _comm     = new MessageCommunicator(_settings.BotToken);

            var Bot = new Telegram.Bot.TelegramBotClient(_settings.BotToken);

            ChatMessage  msg = null;
            BotCommander bot;
            string       content     = "";
            long         defaultRcpt = _settings.DefaultRcpt;

            try
            {
                using (var reader = new StreamReader(Request.InputStream))
                {
                    content = reader.ReadToEnd();

                    msg = ChatMessage.FromJsonString(content);

                    //defaultRcpt = msg.Message.Chat.ID;

                    // Save message before the process (only for registered room)
                    if (msg.Message.Chat.ID == _settings.RegisteredRoom1)
                    {
                        SaveMessage(msg, "messages");
                    }
                    else if (msg.Message.Chat.ID == _settings.RegisteredRoom2)
                    {
                        SaveMessage(msg, "test.messages");
                    }

                    bot = new BotCommander(_settings);

                    string msgReturn;

                    if (bot.ProcessCommand(msg.Message.From.ToString(), msg.Message.Text, out msgReturn))
                    {
                        //Bot.SendTextMessageAsync(msg.Message.Chat.ID, msgReturn);
                        if (msgReturn != "")
                        {
                            _comm.SendMessage(msg.Message.Chat.ID, msgReturn);
                        }
                    }
                    else
                    {
                        BotAutoReply botAutoReply;
                        botAutoReply = new BotAutoReply(_settings.OracleURL, _settings.OraclePort, _settings.OracleDBName, _settings.OracleUserName, _settings.OracleUserPassword);

                        string reply = botAutoReply.FindMessage(msg.Message.Text);

                        if (reply != "")
                        {
                            _comm.SendMessage(msg.Message.Chat.ID, reply);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Bot.SendTextMessageAsync(defaultRcpt, ex.ToString());
                Bot.SendTextMessageAsync(defaultRcpt, content);
            }
        }
Esempio n. 14
0
 private void MakingNewDeviceChanges(MessageCommunicator changes)
 {
     //handle change
 }