コード例 #1
0
ファイル: FAPServerHandler.cs プロジェクト: gmcclelland90/FAP
        private void ProcessLanConnections(object no)
        {
            while (run)
            {
                List <DetectedNode> localNodes = peerFinder.Peers.ToList();

                foreach (DetectedNode peer in localNodes)
                {
                    if (peer.Address == serverNode.Location)
                    {
                        continue;
                    }

                    //If not already connected to that peer then connect
                    if (extOverlordServers.ToList().Where(o => o.Destination.Location == peer.Address).Count() == 0)
                    {
                        logger.Debug("Server connecting as client to external overlord at {0}", peer.Address);
                        var verb = new ConnectVerb();
                        verb.Address    = serverNode.Location;
                        verb.ClientType = ClientType.Overlord;
                        verb.Secret     = IDService.CreateID();

                        var uplink = new Uplink(model.LocalNode,
                                                new Node
                        {
                            ID       = peer.OverlordID,
                            Location = peer.Address,
                            NodeType = ClientType.Overlord,
                            Secret   = verb.Secret
                        });
                        extOverlordServers.Add(uplink);


                        var client = new Client(serverNode);
                        if (client.Execute(verb, peer.Address, 5000))
                        {
                            //Connected as client on an external overlord

                            uplink.OnDisconnect += uplink_OnDisconnect;

                            uplink.Start();
                            logger.Debug("Server connected to client to external overlord at {0}", peer.Address);
                            break;
                        }
                        else
                        {
                            //Failed to connect ot the external overlord
                            logger.Debug("Server failed to connect to external overlord at {0}", peer.Address);
                            peerFinder.RemovePeer(peer);
                            extOverlordServers.Remove(uplink);
                        }
                    }
                }
                Thread.Sleep(3000);
            }
        }
コード例 #2
0
        public void Disconnect()
        {
            //Notify log off
            if (model.Network.State == ConnectionState.Connected)
            {
                var c    = new Client(model.LocalNode);
                var verb = new UpdateVerb();
                verb.Nodes.Add(new Node {
                    ID = model.LocalNode.ID, Online = false
                });
                c.Execute(verb, model.Network.Overlord, 3000);

                //Remove peer so we dont reconnect straight away most likely
                DetectedNode peer =
                    peerFinder.Peers.Where(p => p.Address == model.Network.Overlord.Location).FirstOrDefault();
                if (null != peer)
                {
                    peerFinder.RemovePeer(peer);
                }
                model.Network.State = ConnectionState.Disconnected;
            }
        }