private NetPeerConfiguration CreateNetPeerConfiguration(BadNetworkSimulation badNetworkSimulation) { var config = new NetPeerConfiguration(appConfig.AppId); if (badNetworkSimulation != null) { badNetworkSimulation.ApplySettingsToConfig(config); } // TODO: Re-enable UPnP... or, better yet, replace it with an implementation that doesn't block the network thread! //config.EnableUPnP = true; // <- Lidgren locks this property, so it must be set here // NOTE: Additional configuration options are enabled/disabled as required: // These NetIncomingMessageTypes: DiscoveryRequest, DiscoveryResponse, UnconnectedData (for NAT punch through) // Also: AcceptIncomingConnections // NOTE: Hard-coded time-outs in the P2P layer (see "P2P Network.pptx") depend on the // configuration using these default values. // (Ideally the time-outs should not be hard-coded, but calculated from these values) Debug.Assert(config.ConnectionTimeout == 25.0f); Debug.Assert(config.PingInterval == 4.0f); Debug.Assert(config.ResendHandshakeInterval == 3.0f); Debug.Assert(config.MaximumHandshakeAttempts == 5); return(config); }
private void SetupSocket(BadNetworkSimulation badNetworkSimulation) { // Try each known port to see if we can open a socket... for (int i = 0; i < appConfig.KnownPorts.Length; i++) { try { NetPeerConfiguration config = CreateNetPeerConfiguration(badNetworkSimulation); config.Port = appConfig.KnownPorts[i]; netPeer = new NetPeer(config); netPeer.Start(); Log("Socket opened on port " + PortNumber); return; } catch (SocketException socketException) // Probably port unavailable { if (socketException.SocketErrorCode != SocketError.AddressAlreadyInUse) { throw; // Actually, it was something else } else { continue; // Try the next port } } } Log("Known port unavailable, auto-assigning port..."); { // Try again with auto-assigned port NetPeerConfiguration config = CreateNetPeerConfiguration(badNetworkSimulation); config.DisableMessageType(NetIncomingMessageType.DiscoveryRequest); // <- will enable when we need it config.Port = 0; netPeer = new NetPeer(config); netPeer.Start(); Log("Socket opened on port " + PortNumber); } }
public P2PNetwork(NetworkAppConfig appConfig, NetworkLogHandler networkLogHandler = null, BadNetworkSimulation badNetworkSimulation = null) { if (appConfig == null) { throw new ArgumentNullException("appConfig"); } this.appConfig = appConfig; NetworkLogHandler = networkLogHandler; Log("Configuration:"); Log(" AppId = " + appConfig.AppId); Log(" Version = " + appConfig.ApplicationVersion); Log(" Signature = " + BitConverter.ToString(appConfig.ApplicationSignature)); SetupSocket(badNetworkSimulation); LocalisedDisconnectReason = null; }