コード例 #1
0
        /// <summary>
        /// Interprets the received NetworkCommand message.
        /// </summary>
        /// <param name="data">Deserialized NetworkCommand message.</param>
        /// <param name="id">ClientID of client sending message.</param>
        public void ReceiveSimMessage(string data, int id)
        {
            TcpClient        client = list_clients[id];
            MessageInterface mi     = JsonConvert.DeserializeObject <MessageInterface>(data);

            switch (mi.Type)
            {
            case "AuthRequest":
                // Request connection to server
                break;

            case "WhoIs":
                WhoIs wi = (WhoIs)mi;
                if (connected_clients.ContainsKey(wi.MACAddress))
                {
                    connected_clients.Remove(wi.MACAddress);
                }
                connected_clients.Add(wi.MACAddress, new ClientConnections(wi.IPAddress, wi.MACAddress, id));
                Thread stillAliveThread = new Thread(() => StillAlive(client, wi.MACAddress));
                stillAliveThread.Start();
                break;

            case "KillKey":
                KillKey k = (KillKey)mi;
                OnClientConnect(new ClientConnectEventArgs(2, k.IPAddress, k.MACAddress));
                ClientDisconnects(connected_clients[k.MACAddress].DictionaryID, k.MACAddress);
                break;

            default:
                CmdAdmin.ReceiveCommand(mi);
                break;
            }

            /*NetworkCommand nc = JsonConvert.DeserializeObject<NetworkCommand>(data);
             * switch (nc.Type)
             * {
             *  case NetworkCommand.CommandTypes.AuthRequest:
             *      // Request connection to the server
             *      break;
             *  case NetworkCommand.CommandTypes.WhoIs:
             *      if (connected_clients.ContainsKey(nc.MacAddress))
             *      {
             *          connected_clients.Remove(nc.MacAddress);
             *      }
             *      connected_clients.Add(nc.MacAddress, new ClientConnections(nc.IpAddress, nc.MacAddress, id));
             *      Thread stillAliveThread = new Thread(() => StillAlive(client, nc.MacAddress));
             *      stillAliveThread.Start();
             *      break;
             *  case NetworkCommand.CommandTypes.KillKey:
             *      OnClientConnect(new ClientConnectEventArgs(2, "", nc.MacAddress));
             *      ClientDisconnects(connected_clients[nc.MacAddress].DictionaryID, nc.MacAddress);
             *      break;
             *  default:
             *      CmdAdmin.ReceiveCommand(nc);
             *      break;
             * }*/
        }
コード例 #2
0
        /////////////////////////////////////////////////////////////////////
        ///  Client System
        /////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Connects this client to the server detected from the beacon listener.
        /// </summary>
        /// /// <param name="ipInput">IP address of the server retrieved from the beacon listener.</param>
        private void ConnectToServer(string ipInput)
        {
            try
            {
                IPAddress ip = IPAddress.Parse(ipInput);
                tcpConn = new TcpClient();
                tcpConn.Connect(ip, commPort);

                clientNS     = tcpConn.GetStream();
                clientThread = new Thread(o => ReceiveData((TcpClient)o));
                clientThread.Start();
                hasConnection = true;
                clientMAC     = GetMACAddress();
                //string establishConnMsg = commands[1] + "~" + GetInternalIP() + "~" + clientMAC;
                WhoIs wi = new WhoIs(clientMAC, GetInternalIP());
                //NetworkCommand nc = new NetworkCommand(NetworkCommand.CommandTypes.WhoIs, "", "");
                //nc.MacAddress = clientMAC;
                //nc.IpAddress = GetInternalIP();
                //byte[] buffer = Encoding.ASCII.GetBytes(establishConnMsg);
                //byte[] buffer = Encoding.ASCII.GetBytes(nc.ToJson());
                byte[] buffer = Encoding.ASCII.GetBytes(wi.ToJson());
                clientNS.Write(buffer, 0, buffer.Length);
                EntryOutput("Connected to the console.");
                OnConnectionEstablished(new ConnectionEstablishedArgs(true));
                if (beaconSearch != null)
                {
                    beaconSearch.Abort();
                }
            }
            catch (SocketException e)
            {
                tcpConn = null;
                if (clientThread != null)
                {
                    clientThread.Abort();
                }
                EntryOutput("Connection failed. Unable to connect to IP " + ipInput + ".");
                Console.WriteLine(e.StackTrace);
            }
        }