示例#1
0
 public NetworkGamer(NetworkSession session, byte id, GamerStates state)
 {
     this.id            = id;
     this.session       = session;
     this.gamerState    = state;
     this.gamerState    = state;
     this.oldGamerState = state;
 }
示例#2
0
 public CommandGamerJoined(int internalIndex, bool isHost, bool isLocal)
 {
   this.gamerInternalIndex = internalIndex;
   if (isHost)
     this.states = this.states | GamerStates.Host;
   if (!isLocal)
     return;
   this.states = this.states | GamerStates.Local;
 }
示例#3
0
        public CommandGamerJoined(int internalIndex, bool isHost, bool isLocal)
        {
            gamerInternalIndex = internalIndex;

            if (isHost)
                states = states | GamerStates.Host;
            if (isLocal)
                states = states | GamerStates.Local;
        }
示例#4
0
        public CommandGamerJoined(int internalIndex, bool isHost, bool isLocal)
        {
            gamerInternalIndex = internalIndex;

            if (isHost)
            {
                states = states | GamerStates.Host;
            }
            if (isLocal)
            {
                states = states | GamerStates.Local;
            }
        }
示例#5
0
 public CommandGamerJoined(int internalIndex, bool isHost, bool isLocal)
 {
     this.gamerInternalIndex = internalIndex;
     if (isHost)
     {
         this.states = this.states | GamerStates.Host;
     }
     if (!isLocal)
     {
         return;
     }
     this.states = this.states | GamerStates.Local;
 }
示例#6
0
        public NetworkGamer(NetworkSession session, byte id, GamerStates state)
        {
            this.id         = id;
            this.session    = session;
            this.gamerState = state;
            // We will modify these HasFlags to inline code because MonoTouch does not support
            // the HasFlag method.  Also after reading this : http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx#2
            // it just might be better to inline it anyway.
            //this.isHost = (state & GamerStates.Host) != 0; // state.HasFlag(GamerStates.Host);
            //this.isLocal = (state & GamerStates.Local) != 0; // state.HasFlag(GamerStates.Local);
            //this.hasVoice = (state & GamerStates.HasVoice) != 0; //state.HasFlag(GamerStates.HasVoice);

            // *** NOTE TODO **
            // This whole state stuff need to be looked at again.  Maybe we should not be using local
            //  variables here and instead just use the flags within the gamerState.

            this.gamerState    = state;
            this.oldGamerState = state;
        }
示例#7
0
		public NetworkGamer ( NetworkSession session, byte id, GamerStates state)
		{
			this.id = id;
			this.session = session;
			this.gamerState = state;
			// We will modify these HasFlags to inline code because MonoTouch does not support
			// the HasFlag method.  Also after reading this : http://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx#2
			// it just might be better to inline it anyway.
			//this.isHost = (state & GamerStates.Host) != 0; // state.HasFlag(GamerStates.Host);
			//this.isLocal = (state & GamerStates.Local) != 0; // state.HasFlag(GamerStates.Local);
			//this.hasVoice = (state & GamerStates.HasVoice) != 0; //state.HasFlag(GamerStates.HasVoice);
			
			// *** NOTE TODO **
			// This whole state stuff need to be looked at again.  Maybe we should not be using local
			//  variables here and instead just use the flags within the gamerState.
			
			this.gamerState = state;
			this.oldGamerState = state;
		}
示例#8
0
        private void MGServer_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            NetPeerConfiguration config = new NetPeerConfiguration(applicationIdentifier);

            config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
            config.EnableMessageType(NetIncomingMessageType.NatIntroductionSuccess);

            if (availableSession == null)
            {
                config.Port = port;
            }

            // create and start server
            peer = new NetServer(config);
            peer.Start();

            myLocalAddress = GetMyLocalIpAddress();

            IPAddress adr = IPAddress.Parse(myLocalAddress);

            myLocalEndPoint = new IPEndPoint(adr, port);

            // force a little wait until we have a LocalGamer otherwise things
            // break. This is the first item in the queue so it shouldnt take long before we
            // can continue.
            while (session.LocalGamers.Count <= 0)
            {
                Thread.Sleep(10);
            }

            if (availableSession != null)
            {
                if (!this.online)
                {
                    peer.Connect(availableSession.EndPoint);
                }
                else
                {
                    RequestNATIntroduction(availableSession.EndPoint, peer);
                }
            }
            else
            {
                if (this.online)
                {
                    IPAddress ipaddr = NetUtility.Resolve(masterServer);
                    if (ipaddr != null)
                    {
                        m_masterServer = new IPEndPoint(ipaddr, masterserverport);
                        LocalNetworkGamer localMe = session.LocalGamers [0];

                        NetOutgoingMessage om = peer.CreateMessage();

                        om.Write((byte)0);
                        om.Write(session.AllGamers.Count);
                        om.Write(localMe.Gamertag);
                        om.Write(session.PrivateGamerSlots);
                        om.Write(session.MaxGamers);
                        om.Write(localMe.IsHost);
                        om.Write(myLocalEndPoint);
                        om.Write(peer.Configuration.AppIdentifier);
                        // send up session properties
                        int[] propertyData = new int[session.SessionProperties.Count * 2];
                        NetworkSessionProperties.WriteProperties(session.SessionProperties, propertyData);
                        for (int x = 0; x < propertyData.Length; x++)
                        {
                            om.Write(propertyData [x]);
                        }
                        peer.SendUnconnectedMessage(om, m_masterServer);                          // send message to peer
                    }
                    else
                    {
                        throw new Exception("Could not resolve live host");
                    }
                }
            }

            // run until we are done
            do
            {
                NetIncomingMessage msg;
                while ((msg = peer.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.UnconnectedData:
                        break;

                    case NetIncomingMessageType.NatIntroductionSuccess:
#if !WINDOWS_PHONE
                        Game.Instance.Log("NAT punch through OK " + msg.SenderEndPoint);
#endif
                        peer.Connect(msg.SenderEndPoint);
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:
                        //
                        // Server received a discovery request from a client; send a discovery response (with no extra data attached)
                        //
                        // Get the primary local gamer
                        LocalNetworkGamer localMe = session.LocalGamers [0];

                        NetOutgoingMessage om = peer.CreateMessage();

                        om.Write(session.RemoteGamers.Count);
                        om.Write(localMe.Gamertag);
                        om.Write(session.PrivateGamerSlots);
                        om.Write(session.MaxGamers);
                        om.Write(localMe.IsHost);
                        int[] propertyData = new int[session.SessionProperties.Count * 2];
                        NetworkSessionProperties.WriteProperties(session.SessionProperties, propertyData);
                        for (int x = 0; x < propertyData.Length; x++)
                        {
                            om.Write(propertyData [x]);
                        }

                        peer.SendDiscoveryResponse(om, msg.SenderEndPoint);
                        break;

                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        //
                        // Just print diagnostic messages to console
                        //
#if !WINDOWS_PHONE
                        Game.Instance.Log(msg.ReadString());
#endif
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                        if (status == NetConnectionStatus.Disconnected)
                        {
#if !WINDOWS_PHONE
                            Game.Instance.Log(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " disconnected! from " + msg.SenderEndPoint);
#endif
                            CommandGamerLeft cgj  = new CommandGamerLeft(msg.SenderConnection.RemoteUniqueIdentifier);
                            CommandEvent     cmde = new CommandEvent(cgj);
                            session.commandQueue.Enqueue(cmde);
                        }
                        if (status == NetConnectionStatus.Connected)
                        {
                            //
                            // A new player just connected!
                            //
                            if (!pendingGamers.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                            {
#if !WINDOWS_PHONE
                                Game.Instance.Log(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected! from " + msg.SenderEndPoint);
#endif
                                pendingGamers.Add(msg.SenderConnection.RemoteUniqueIdentifier, msg.SenderConnection);
                                SendProfileRequest(msg.SenderConnection);
                            }
                            else
                            {
#if !WINDOWS_PHONE
                                Game.Instance.Log("Already have a connection for that user, this is probably due to both NAT intro requests working");
#endif
                            }
                        }

                        break;

                    case NetIncomingMessageType.Data:

                        NetworkMessageType mt = (NetworkMessageType)msg.ReadByte();
                        switch (mt)
                        {
                        case NetworkMessageType.Data:
                            byte[] data = new byte[msg.LengthBytes - 1];
                            msg.ReadBytes(data, 0, data.Length);
                            CommandEvent cme = new CommandEvent(new CommandReceiveData(msg.SenderConnection.RemoteUniqueIdentifier,
                                                                                       data));
                            session.commandQueue.Enqueue(cme);
                            break;

                        case NetworkMessageType.Introduction:

                            var introductionAddress = msg.ReadString();

                            try {
                                IPEndPoint endPoint = ParseIPEndPoint(introductionAddress);

                                if (myLocalEndPoint.ToString() != endPoint.ToString() && !AlreadyConnected(endPoint))
                                {
#if !WINDOWS_PHONE
                                    Game.Instance.Log("Received Introduction for: " + introductionAddress +
                                                      " and I am: " + myLocalEndPoint + " from: " + msg.SenderEndPoint);
#endif
                                    peer.Connect(endPoint);
                                }
                            } catch (Exception exc) {
#if !WINDOWS_PHONE
                                Game.Instance.Log("Error parsing Introduction: " + introductionAddress + " : " + exc.Message);
#endif
                            }

                            break;

                        case NetworkMessageType.GamerProfile:
#if !WINDOWS_PHONE
                            Game.Instance.Log("Profile recieved from: " + NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier));
#endif
                            if (pendingGamers.ContainsKey(msg.SenderConnection.RemoteUniqueIdentifier))
                            {
                                pendingGamers.Remove(msg.SenderConnection.RemoteUniqueIdentifier);
                                msg.ReadInt32();
                                string gamerTag = msg.ReadString();
                                msg.ReadInt32();
                                msg.ReadInt32();
                                GamerStates state = (GamerStates)msg.ReadInt32();
                                state &= ~GamerStates.Local;
                                CommandGamerJoined cgj = new CommandGamerJoined(msg.SenderConnection.RemoteUniqueIdentifier);
                                cgj.GamerTag = gamerTag;
                                cgj.State    = state;
                                CommandEvent cmde = new CommandEvent(cgj);
                                session.commandQueue.Enqueue(cmde);
                            }
                            else
                            {
#if !WINDOWS_PHONE
                                Game.Instance.Log("We received a profile for an existing gamer.  Need to update it.");
#endif
                            }
                            break;

                        case NetworkMessageType.RequestGamerProfile:
#if !WINDOWS_PHONE
                            Game.Instance.Log("Profile Request recieved from: " + msg.SenderEndPoint);
#endif
                            SendProfile(msg.SenderConnection);
                            break;

                        case NetworkMessageType.GamerStateChange:
                            GamerStates gamerstate = (GamerStates)msg.ReadInt32();
                            gamerstate &= ~GamerStates.Local;
#if !WINDOWS_PHONE
                            Game.Instance.Log("State Change from: " + msg.SenderEndPoint + " new State: " + gamerstate);
#endif
                            foreach (var gamer in session.RemoteGamers)
                            {
                                if (gamer.RemoteUniqueIdentifier == msg.SenderConnection.RemoteUniqueIdentifier)
                                {
                                    gamer.State = gamerstate;
                                }
                            }
                            break;

                        case NetworkMessageType.SessionStateChange:
                            NetworkSessionState sessionState = (NetworkSessionState)msg.ReadInt32();

                            foreach (var gamer in session.RemoteGamers)
                            {
                                if (gamer.RemoteUniqueIdentifier == msg.SenderConnection.RemoteUniqueIdentifier)
                                {
#if !WINDOWS_PHONE
                                    Game.Instance.Log("Session State change from: " + NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) +
                                                      " session is now: " + sessionState);
#endif
                                    if (gamer.IsHost && sessionState == NetworkSessionState.Playing)
                                    {
                                        session.StartGame();
                                    }
                                }
                            }

                            break;
                        }
                        break;
                    }
                }

                // sleep to allow other processes to run smoothly
                // This may need to be changed depending on network throughput
                Thread.Sleep(1);

                if (worker.CancellationPending)
                {
#if !WINDOWS_PHONE
                    Game.Instance.Log("worker CancellationPending");
#endif
                    e.Cancel = true;
                    done     = true;
                }
            } while (!done);
        }
示例#9
0
 public LocalNetworkGamer(NetworkSession session, byte id, GamerStates state)
     : base(session, id, state | GamerStates.Local)
 {
     sig          = new SignedInGamer();
     receivedData = new Queue <CommandReceiveData>();
 }
示例#10
0
		public LocalNetworkGamer (NetworkSession session,byte id,GamerStates state)
			: base(session, id, state | GamerStates.Local)
		{
			sig = new SignedInGamer ();
			receivedData = new Queue<CommandReceiveData>();
		}
示例#11
0
 public NetworkGamer(NetworkSession session, byte id, GamerStates state)
 {
   this.id = id;
   this.session = session;
   this.gamerState = state;
   this.gamerState = state;
   this.oldGamerState = state;
 }
示例#12
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);
        }
 public CommandGamerStateChange(NetworkGamer gamer)
 {
     this.gamer    = gamer;
     this.newState = gamer.State;
     this.oldState = gamer.OldState;
 }
		public CommandGamerStateChange (NetworkGamer gamer)
		{
			this.gamer = gamer;
			this.newState = gamer.State;
			this.oldState = gamer.OldState;
		}