Ejemplo n.º 1
0
        private void OnSessionTerminate(object sender, SessionTerminatedEventArgs e)
        {
            connected = false;

            // Dispose of our connection, and set it to null
            ThinClient.Dispose();
            ThinClient = null;
        }
Ejemplo n.º 2
0
        public bool InitializeClient(string name)
        {
            // Create our client object
            ThinClient  = new Microsoft.DirectX.DirectPlay.Client();
            NewDataList = new List <DataPacket>();
            bool connectionSuccess = true;

            // Check to see if we can create a TCP/IP connection
            if (!IsServiceProviderValid(Address.ServiceProviderTcpIp))
            {
                connectionSuccess = false;
            }

            // Create a new address for our local machine
            Address deviceAddress = new Address();

            deviceAddress.ServiceProvider = Address.ServiceProviderTcpIp;
            // Create a new address for our host machine
            Address hostaddress = new Address();

            hostaddress.ServiceProvider = Address.ServiceProviderTcpIp;
            hostaddress.AddComponent(Address.KeyPort, ServerInfo.DataPort);
            // Find out about the newer standard!!
            // Set our name
            PlayerInformation info = new PlayerInformation();

            info.Name = name;
            ThinClient.SetClientInformation(info, SyncFlags.ClientInformation);

            // Set up an application description
            ApplicationDescription desc = new ApplicationDescription();

            desc.GuidApplication = ServerInfo.ApplicationGuid;

            //Attach Event handlers
            ThinClient.ConnectComplete   += new ConnectCompleteEventHandler(OnConnectComplete);
            ThinClient.FindHostResponse  += new FindHostResponseEventHandler(OnFindHost);
            ThinClient.Receive           += new ReceiveEventHandler(OnDataReceive);
            ThinClient.SessionTerminated += new SessionTerminatedEventHandler(OnSessionTerminate);

            try
            {
                // Search for a server
                //This will trigger the FindHostResponse event if a server is up and running
                ThinClient.FindHosts(desc, hostaddress, deviceAddress, null, 0, 0, 0, FindHostsFlags.None);
            }
            catch
            {
                connectionSuccess = false;
            }

            return(connectionSuccess);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
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);
        }