Exemplo n.º 1
0
        private void ListenForClients()
        {
            this.tcpListener.Start();

            TcpClient client;

            while (this.keepListening)
            {
                try {
                    client = this.tcpListener.AcceptTcpClient();
                } catch {
                    break;
                }

                iControlClient icClient = new iControlClient(client);
                this.icClients.Add(icClient.IPAddress, icClient);

                Program.Log("[" + icClient.IPAddress + "] Connection accepted. Starting client thread.");

                icClient.Thread = new Thread(new ParameterizedThreadStart(HandleClientCommunication));
                icClient.Thread.Start(icClient);
            }

            Program.Log("Stop listening.");
        }
Exemplo n.º 2
0
        private void HandleClientCommunication(object client)
        {
            iControlClient icClient     = (iControlClient)client;
            NetworkStream  clientStream = icClient.TCP.GetStream();

            clientStream.ReadTimeout = 10;

            int bufflen = 4096;

            byte[] message = new byte[bufflen];
            int    bytesRead;

            while (this.keepReceiving && icClient.keepConnected)
            {
                bytesRead = 0;

                try {
                    bytesRead = clientStream.Read(message, 0, bufflen);
                } catch {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }
                ProcessReceivedData(icClient, ParseData(message, bytesRead));
            }

            Program.Log("[" + icClient.IPAddress + "] Connection closed.");

            icClient.TCP.Close();
            this.icClients.Remove(icClient.IPAddress);
        }
Exemplo n.º 3
0
        private void ProcessReceivedData(iControlClient icClient, String[] commands)
        {
            Program.Log("[" + icClient.IPAddress + "] >> " + String.Join(" ", commands));

            if (this.CommandReceived != null)
            {
                CommandReceived(this, new CommandReceivedEventArgs(String.Join(" ", commands), icClient));
            }

            NetworkStream clientStream = icClient.TCP.GetStream();
            ASCIIEncoding encoder      = new ASCIIEncoding();

            byte[] buffer = encoder.GetBytes("::ok");
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();

            icClient.keepConnected = false;
        }
Exemplo n.º 4
0
 public CommandReceivedEventArgs(string command, iControlClient client)
 {
     _command          = command;
     _splittedCommands = command.Split(new Char[] { ' ' });
     _client           = client;
 }