示例#1
0
        public P2PServer(P2PNetwork owner, string playerName, byte[] playerData, string gameName, bool openToInternet, bool sideChannelAuth, ulong sideChannelId)
        {
            this.owner = owner;

            owner.Log("Starting P2P Server");

            // Directly setup info about the current game (it's good to be the server):
            owner.GameInfo = new GameInfo(gameName, openToInternet, sideChannelAuth);

            OpenToConnections(openToInternet, sideChannelAuth);

            IPEndPoint myAddress = owner.GetInternalEndpoint();

            owner.LocalPeerInfo = new PeerInfo()
            {
                ConnectionId    = nextConnectionId++,
                PlayerName      = playerName.FilterName(),
                PlayerData      = playerData,
                InputAssignment = AssignInput(),

                InternalEndPoint = myAddress,
                ExternalEndPoint = myAddress, // <- we have no way to determine our external IP (except for UPnP, which is unreliable); we could STUN but technically it can change!

                SideChannelId = sideChannelId,

                IsApplicationConnected = true,
                IsServer = true,
            };

            owner.Log("I am " + owner.LocalPeerInfo);
        }
        internal Discovery(P2PNetwork owner, int[] discoveryPorts)
        {
            Debug.Assert(owner.Discovery == null);

            this.owner          = owner;
            this.discoveryPorts = discoveryPorts;

            discoveryList = new List <DiscoveredGame>();
            Items         = discoveryList.AsReadOnly();

            NetPeer.Configuration.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
        }
示例#3
0
        /// <summary>Construct for host migration only!</summary>
        internal P2PServer(P2PNetwork owner, bool hostMigrationValidatedByServer, int maxConnectionId)
        {
            this.owner = owner;
            owner.Log("Becoming P2P Server (host migration)");


            hostMigrationTime    = NetTime.Now;
            clientDisconnections = new ClientDisconnections();

            if (hostMigrationValidatedByServer)
            {
                ValidateHostMigration();
            }


            owner.LocalPeerInfo.IsServer = true;


            // Recover information from application-connected peer list:
            var leavingPeers = new List <RemotePeer>();

            foreach (var remotePeer in owner.RemotePeers)
            {
                Debug.Assert(remotePeer.PeerInfo.IsApplicationConnected);
                Debug.Assert(remotePeer.PeerInfo.ConnectionId <= maxConnectionId);

                Debug.Assert(remotePeer.PeerInfo.InputAssignment != 0);
                Debug.Assert((assignedInputs & remotePeer.PeerInfo.InputAssignment) == 0);
                assignedInputs |= remotePeer.PeerInfo.InputAssignment;

                Debug.Assert(!(remotePeer.PeerInfo.IsServer &&
                               remotePeer.IsConnected));                 // old server should not still be connected

                if (remotePeer.IsConnected)
                {
                    locallyConnected.Add(remotePeer.Connection);
                }
                // Anyone not connected will get removed from the game in CompleteHostMigration

                remotePeer.PeerInfo.IsServer = false;                 // Un-server-ify the original server
            }

            // And also from ourself:
            Debug.Assert(owner.LocalPeerInfo.IsApplicationConnected);
            Debug.Assert(owner.LocalPeerInfo.ConnectionId <= maxConnectionId);
            Debug.Assert(owner.LocalPeerInfo.InputAssignment != 0);
            Debug.Assert((assignedInputs & owner.LocalPeerInfo.InputAssignment) == 0);
            assignedInputs |= owner.LocalPeerInfo.InputAssignment;


            // This should be safe, as host migration should remove all traces peers beyond this point (including in the app layer) and is atomic
            nextConnectionId = maxConnectionId + 1;
        }