protected void InitDirectPlay() { // create a direct play connection dpc = new DPlay.Client(); DPlay.ApplicationDescription appdesc = new DPlay.ApplicationDescription(); appdesc.GuidApplication = new Guid("B32DD425-DB33-4f9c-972F-C68269C409F6"); DPlay.Address dpa = new DPlay.Address(hoststring, 895); DPlay.Address dpdi = new DPlay.Address(); dpdi.ServiceProvider = DPlay.Address.ServiceProviderTcpIp; // Set up our event handlers dpc.ConnectComplete += new DPlay.ConnectCompleteEventHandler(this.ConnectComplete); // dpc.ConnectComplete += new ConnectCompleteEventHandler(this.ConnectComplete); dpc.Receive += new DPlay.ReceiveEventHandler(this.DataReceivedMsg); // dpc.SessionTerminated += new SessionTerminatedEventHandler(this.SessionLost); dpc.Connect(appdesc, dpa, dpdi, null, 0); }
/// <summary>Connect to a server.</summary> /// <param name="serverName">IP address or hostname of the server.</param> /// <param name="serverPort">IP port on which the server is listening.</param> /// <remarks>The first client to connect becomes the hosting player.</remarks> public void Connect(string serverName, int serverPort) { Debug.Assert(status == NetworkStatus.Disconnected); serverIsOnSameComputer = (serverName == "localhost"); outboundVideoFrameHistory = new OutboundVideoFrameHistory(); inboundVideoFrameHistories = new Dictionary <int, InboundVideoFrameHistory>(); soundBuffers = new Dictionary <int, Buffer3D>(); client = new Microsoft.DirectX.DirectPlay.Client(InitializeFlags.DisableParameterValidation); client.ConnectComplete += new ConnectCompleteEventHandler(onConnectComplete); client.Receive += new ReceiveEventHandler(onReceive); client.SessionTerminated += new SessionTerminatedEventHandler(onSessionTerminated); status = NetworkStatus.Connecting; // trigger NAT traversal EnabledAddresses enabledAddresses = NatResolver.TestNatTraversal(serverName, serverPort); ApplicationDescription description = new ApplicationDescription(); description.GuidApplication = new Guid("{920BAF09-A06C-47d8-BCE0-21B30D0C3586}"); // try first using the host's public address using (Address hostAddress = (enabledAddresses == null ? new Address(serverName, serverPort) : new Address(enabledAddresses.HostPublicAddress, enabledAddresses.HostPublicPort))) { hostAddress.ServiceProvider = Address.ServiceProviderTcpIp; using (Address device = new Address()) { device.ServiceProvider = Address.ServiceProviderTcpIp; device.AddComponent(Address.KeyTraversalMode, Address.TraversalModeNone); if (enabledAddresses != null) { device.AddComponent(Address.KeyPort, enabledAddresses.ClientPrivatePort); } using (NetworkPacket packet = new NetworkPacket()) { try { client.Connect(description, hostAddress, device, packet, 0); } catch (Exception e) { status = NetworkStatus.Disconnected; ConnectionFailureCause cause = (e is NoConnectionException ? ConnectionFailureCause.NoConnection : (e is NotHostException ? ConnectionFailureCause.NotHost : (e is SessionFullException ? ConnectionFailureCause.SessionFull : ConnectionFailureCause.Other))); // try again using the host's private address if (enabledAddresses != null) { using (Address hostPrivateAddress = new Address(enabledAddresses.HostPrivateAddress, enabledAddresses.HostPrivatePort)) { try { client.Connect(description, hostAddress, device, packet, 0); } catch { NetworkMessage message = new NetworkMessage(0, (byte)ReservedMessageType.ConnectionFailed, new byte[1] { (byte)cause }); lock (networkMessages) { networkMessages.Enqueue(message); } return; } } } else { NetworkMessage message = new NetworkMessage(0, (byte)ReservedMessageType.ConnectionFailed, new byte[1] { (byte)cause }); lock (networkMessages) { networkMessages.Enqueue(message); } return; } } } } } // launch a timer to monitor timeout timeoutTimer = new System.Threading.Timer(onTimeout, client, 4000, 0); }