コード例 #1
0
        /// <summary>
        /// Starts the beacon broadcast. Clients will use this beacon to connect to the system.
        /// </summary>
        private void StartBeaconThread()
        {
            string localIP = GetInternalIP();
            //string bkey = commands[2] + "~" + localIP;
            //NetworkCommand nc = new NetworkCommand(NetworkCommand.CommandTypes.CommSystem);
            //nc.IpAddress = localIP;
            CommSystem cs          = new CommSystem(localIP);
            string     broadcastIP = GetBroadcastAddress(localIP);

            EntryOutput(">> Beacon started on IP " + localIP + " and port " + commPort);
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse(broadcastIP), commPort);

            try
            {
                while (CheckRunBeacon)
                {
                    beaconUDP = new UdpClient();
                    Thread.Sleep(1000);
                    byte[] bytes = Encoding.ASCII.GetBytes(cs.ToJson());
                    beaconUDP.Send(bytes, bytes.Length, ip);
                    beaconUDP.Close();
                }
            } catch (SocketException e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }
コード例 #2
0
        /// <summary>
        /// Triggered when a beacon is found on the network. Begins the connection process.
        /// </summary>
        /// <param name="ar">AsyncResult from the beacon being triggered.</param>
        private void BeaconFound(IAsyncResult ar)
        {
            if (!CheckRunBeacon)
            {
                try
                {
                    IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, commPort);
                    byte[]     bytes    = beaconUDP.EndReceive(ar, ref endpoint);
                    string     msg      = Encoding.ASCII.GetString(bytes);
                    //NetworkCommand nc = JsonConvert.DeserializeObject<NetworkCommand>(msg);
                    MessageInterface mi = JsonConvert.DeserializeObject <MessageInterface>(msg);
                    if (mi.Type == "CommSystem")
                    {
                        CommSystem cs = (CommSystem)mi;
                        beaconIP = cs.IPAddress;
                        ConnectToServer(beaconIP);
                    }

                    /*if(nc.Type == NetworkCommand.CommandTypes.CommSystem)
                     * {
                     *  beaconIP = nc.IpAddress;
                     *  ConnectToServer(beaconIP);
                     * }*/
                }
                catch (ObjectDisposedException)
                {
                    EntryOutput("Error 2");
                }
                catch (NullReferenceException)
                {
                    EntryOutput("Error 3");
                }
                catch (ArgumentException)
                {
                    EntryOutput("Error 4");
                }
            }
        }