Example #1
0
 /// <summary>
 /// Closes the connection to the server. Triggers FindBeacon to start back up.
 /// </summary>
 public void CloseConnection()
 {
     if (tcpConn != null)
     {
         if (tcpConn.Connected)
         {
             //NetworkCommand nc = new NetworkCommand(NetworkCommand.CommandTypes.KillKey);
             KillKey k = new KillKey(clientMAC, "");
             //nc.MacAddress = clientMAC;
             SendMessage(k.ToJson());
             try
             {
                 tcpConn.Client.Shutdown(SocketShutdown.Send);
             }
             catch (IOException) { }
             clientThread.Abort();
             clientNS.Close();
             tcpConn.Close();
             hasConnection = false;
             EntryOutput(">> Connection closed.");
             beaconThread = new Thread(o => FindBeacon());
         }
     }
     else
     {
         tcpConn      = null;
         clientThread = null;
         clientNS     = null;
         EntryOutput(">> No existing connection.");
     }
 }
Example #2
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;
             * }*/
        }