Ejemplo n.º 1
0
        private void MGServer_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker     backgroundWorker  = sender as BackgroundWorker;
            NetPeerConfiguration peerConfiguration = new NetPeerConfiguration(MonoGamerPeer.applicationIdentifier);

            peerConfiguration.EnableMessageType((NetIncomingMessageType)32);
            peerConfiguration.EnableMessageType((NetIncomingMessageType)64);
            peerConfiguration.EnableMessageType((NetIncomingMessageType)2048);
            if (this.availableSession == null)
            {
                peerConfiguration.set_Port(MonoGamerPeer.port);
            }
            this.peer = new NetServer(peerConfiguration);
            ((NetPeer)this.peer).Start();
            this.myLocalAddress  = MonoGamerPeer.GetMyLocalIpAddress();
            this.myLocalEndPoint = new IPEndPoint(IPAddress.Parse(this.myLocalAddress), MonoGamerPeer.port);
            while (this.session.LocalGamers.Count <= 0)
            {
                Thread.Sleep(10);
            }
            if (this.availableSession != null)
            {
                if (!this.online)
                {
                    ((NetPeer)this.peer).Connect(this.availableSession.EndPoint);
                }
                else
                {
                    MonoGamerPeer.RequestNATIntroduction(this.availableSession.EndPoint, (NetPeer)this.peer);
                }
            }
            else if (this.online)
            {
                IPAddress address = NetUtility.Resolve(MonoGamerPeer.masterServer);
                if (address == null)
                {
                    throw new Exception("Could not resolve live host");
                }
                MonoGamerPeer.m_masterServer = new IPEndPoint(address, MonoGamerPeer.masterserverport);
                LocalNetworkGamer  localNetworkGamer = this.session.LocalGamers[0];
                NetOutgoingMessage message           = ((NetPeer)this.peer).CreateMessage();
                message.Write((byte)0);
                message.Write(this.session.AllGamers.Count);
                message.Write(localNetworkGamer.Gamertag);
                message.Write(this.session.PrivateGamerSlots);
                message.Write(this.session.MaxGamers);
                message.Write(localNetworkGamer.IsHost);
                message.Write(this.myLocalEndPoint);
                message.Write(((NetPeer)this.peer).get_Configuration().get_AppIdentifier());
                int[] propertyData = new int[this.session.SessionProperties.Count * 2];
                NetworkSessionProperties.WriteProperties(this.session.SessionProperties, propertyData);
                for (int index = 0; index < propertyData.Length; ++index)
                {
                    message.Write(propertyData[index]);
                }
                ((NetPeer)this.peer).SendUnconnectedMessage(message, MonoGamerPeer.m_masterServer);
            }
            do
            {
                NetIncomingMessage netIncomingMessage;
                while ((netIncomingMessage = ((NetPeer)this.peer).ReadMessage()) != null)
                {
                    NetIncomingMessageType messageType = netIncomingMessage.get_MessageType();
                    if (messageType <= 128)
                    {
                        if (messageType <= 8)
                        {
                            switch (messageType - 1)
                            {
                            case 0:
                                NetConnectionStatus connectionStatus = (NetConnectionStatus)(int)netIncomingMessage.ReadByte();
                                if (connectionStatus == 5)
                                {
                                    this.session.commandQueue.Enqueue(new CommandEvent((ICommand) new CommandGamerLeft(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier())));
                                }
                                if (connectionStatus == 3 && !this.pendingGamers.ContainsKey(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier()))
                                {
                                    this.pendingGamers.Add(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier(), netIncomingMessage.get_SenderConnection());
                                    this.SendProfileRequest(netIncomingMessage.get_SenderConnection());
                                    break;
                                }
                                else
                                {
                                    break;
                                }

                            case 1:
                                break;

                            default:
                                if (messageType == 8)
                                {
                                    switch (netIncomingMessage.ReadByte())
                                    {
                                    case (byte)0:
                                        byte[] data = new byte[netIncomingMessage.get_LengthBytes() - 1];
                                        netIncomingMessage.ReadBytes(data, 0, data.Length);
                                        this.session.commandQueue.Enqueue(new CommandEvent((ICommand) new CommandReceiveData(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier(), data)));
                                        break;

                                    case (byte)3:
                                        string endPoint1 = netIncomingMessage.ReadString();
                                        try
                                        {
                                            IPEndPoint endPoint2 = MonoGamerPeer.ParseIPEndPoint(endPoint1);
                                            if (this.myLocalEndPoint.ToString() != endPoint2.ToString() && !this.AlreadyConnected(endPoint2))
                                            {
                                                ((NetPeer)this.peer).Connect(endPoint2);
                                                break;
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            break;
                                        }

                                    case (byte)4:
                                        if (this.pendingGamers.ContainsKey(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier()))
                                        {
                                            this.pendingGamers.Remove(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier());
                                            netIncomingMessage.ReadInt32();
                                            string str = netIncomingMessage.ReadString();
                                            netIncomingMessage.ReadInt32();
                                            netIncomingMessage.ReadInt32();
                                            GamerStates gamerStates = (GamerStates)(netIncomingMessage.ReadInt32() & -2);
                                            this.session.commandQueue.Enqueue(new CommandEvent((ICommand) new CommandGamerJoined(netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier())
                                            {
                                                GamerTag = str,
                                                State    = gamerStates
                                            }));
                                            break;
                                        }
                                        else
                                        {
                                            break;
                                        }

                                    case (byte)5:
                                        this.SendProfile(netIncomingMessage.get_SenderConnection());
                                        break;

                                    case (byte)6:
                                        GamerStates gamerStates1 = (GamerStates)(netIncomingMessage.ReadInt32() & -2);
                                        using (IEnumerator <NetworkGamer> enumerator = this.session.RemoteGamers.GetEnumerator())
                                        {
                                            while (enumerator.MoveNext())
                                            {
                                                NetworkGamer current = enumerator.Current;
                                                if (current.RemoteUniqueIdentifier == netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier())
                                                {
                                                    current.State = gamerStates1;
                                                }
                                            }
                                            break;
                                        }

                                    case (byte)7:
                                        NetworkSessionState networkSessionState = (NetworkSessionState)netIncomingMessage.ReadInt32();
                                        using (IEnumerator <NetworkGamer> enumerator = this.session.RemoteGamers.GetEnumerator())
                                        {
                                            while (enumerator.MoveNext())
                                            {
                                                NetworkGamer current = enumerator.Current;
                                                if (current.RemoteUniqueIdentifier == netIncomingMessage.get_SenderConnection().get_RemoteUniqueIdentifier() && (current.IsHost && networkSessionState == NetworkSessionState.Playing))
                                                {
                                                    this.session.StartGame();
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        else if (messageType != 32)
                        {
                            if (messageType == 128)
                            {
                                ;
                            }
                        }
                        else
                        {
                            LocalNetworkGamer  localNetworkGamer = this.session.LocalGamers[0];
                            NetOutgoingMessage message           = ((NetPeer)this.peer).CreateMessage();
                            message.Write(this.session.RemoteGamers.Count);
                            message.Write(localNetworkGamer.Gamertag);
                            message.Write(this.session.PrivateGamerSlots);
                            message.Write(this.session.MaxGamers);
                            message.Write(localNetworkGamer.IsHost);
                            int[] propertyData = new int[this.session.SessionProperties.Count * 2];
                            NetworkSessionProperties.WriteProperties(this.session.SessionProperties, propertyData);
                            for (int index = 0; index < propertyData.Length; ++index)
                            {
                                message.Write(propertyData[index]);
                            }
                            ((NetPeer)this.peer).SendDiscoveryResponse(message, netIncomingMessage.get_SenderEndpoint());
                        }
                    }
                    else if (messageType <= 512)
                    {
                        if (messageType == 256 || messageType == 512)
                        {
                            ;
                        }
                    }
                    else if (messageType != 1024 && messageType == 2048)
                    {
                        ((NetPeer)this.peer).Connect(netIncomingMessage.get_SenderEndpoint());
                    }
                }
                Thread.Sleep(1);
                if (backgroundWorker.CancellationPending)
                {
                    e.Cancel  = true;
                    this.done = true;
                }
            }while (!this.done);
        }