Example #1
0
 private void ProcessGamerJoined(CommandGamerJoined command)
 {
   NetworkGamer aGamer;
   if ((command.State & GamerStates.Local) != (GamerStates) 0)
   {
     aGamer = (NetworkGamer) new LocalNetworkGamer(this, (byte) command.InternalIndex, command.State);
     this._allGamers.AddGamer(aGamer);
     this._localGamers.AddGamer((LocalNetworkGamer) aGamer);
     if (Gamer.SignedInGamers.Count >= this._localGamers.Count)
       ((LocalNetworkGamer) aGamer).SignedInGamer = ((List<SignedInGamer>) Gamer.SignedInGamers)[this._localGamers.Count - 1];
     aGamer.PropertyChanged += new PropertyChangedEventHandler(this.HandleGamerPropertyChanged);
   }
   else
   {
     aGamer = new NetworkGamer(this, (byte) command.InternalIndex, command.State);
     aGamer.DisplayName = command.DisplayName;
     aGamer.Gamertag = command.GamerTag;
     aGamer.RemoteUniqueIdentifier = command.remoteUniqueIdentifier;
     this._allGamers.AddGamer(aGamer);
     this._remoteGamers.AddGamer(aGamer);
   }
   if ((command.State & GamerStates.Host) != (GamerStates) 0)
     this.hostingGamer = aGamer;
   aGamer.Machine = new NetworkMachine();
   aGamer.Machine.Gamers.AddGamer(aGamer);
   if (this.GamerJoined == null)
     return;
   this.GamerJoined((object) this, new GamerJoinedEventArgs(aGamer));
 }
Example #2
0
        private void ProcessGamerJoined(CommandGamerJoined command)
        {
            NetworkGamer gamer;

            if ((command.State & GamerStates.Local) != 0)
            {
                gamer = new LocalNetworkGamer(this, (byte)command.InternalIndex, command.State);
                _allGamers.AddGamer(gamer);
                _localGamers.AddGamer((LocalNetworkGamer)gamer);

                // Note - This might be in the wrong place for certain connections
                //  Take a look at HoneycombRush tut for debugging later.
                if (Gamer.SignedInGamers.Count >= _localGamers.Count)
                {
                    ((LocalNetworkGamer)gamer).SignedInGamer = Gamer.SignedInGamers[_localGamers.Count - 1];
                }

                // We will attach a property change handler to local gamers
                //  se that we can broadcast the change to other peers.
                gamer.PropertyChanged += HandleGamerPropertyChanged;
            }
            else
            {
                gamer                        = new NetworkGamer(this, (byte)command.InternalIndex, command.State);
                gamer.DisplayName            = command.DisplayName;
                gamer.Gamertag               = command.GamerTag;
                gamer.RemoteUniqueIdentifier = command.remoteUniqueIdentifier;
                _allGamers.AddGamer(gamer);
                _remoteGamers.AddGamer(gamer);
            }

            if ((command.State & GamerStates.Host) != 0)
            {
                hostingGamer = gamer;
            }

            gamer.Machine = new NetworkMachine();
            gamer.Machine.Gamers.AddGamer(gamer);
            //gamer.IsReady = true;

            if (GamerJoined != null)
            {
                GamerJoined(this, new GamerJoinedEventArgs(gamer));
            }

#if !PORTABLE
            if (networkPeer != null && (command.State & GamerStates.Local) == 0)
            {
                networkPeer.SendPeerIntroductions(gamer);
            }

            if (networkPeer != null)
            {
                networkPeer.UpdateLiveSession(this);
            }
#endif
        }
        private NetworkSession(NetworkSessionType sessionType, int maxGamers, int privateGamerSlots, NetworkSessionProperties sessionProperties, bool isHost, int hostGamer, AvailableNetworkSession availableSession) : this()
        {
            if (sessionProperties == null)
            {
                throw new ArgumentNullException("sessionProperties");
            }

            _allGamers   = new GamerCollection <NetworkGamer>();
            _localGamers = new GamerCollection <LocalNetworkGamer>();
//			for (int x = 0; x < Gamer.SignedInGamers.Count; x++) {
//				GamerStates states = GamerStates.Local;
//				if (x == 0)
//					states |= GamerStates.Host;
//				LocalNetworkGamer localGamer = new LocalNetworkGamer(this, (byte)x, states);
//				localGamer.SignedInGamer = Gamer.SignedInGamers[x];
//				_allGamers.AddGamer(localGamer);
//				_localGamers.AddGamer(localGamer);
//
//				// We will attach a property change handler to local gamers
//				//  se that we can broadcast the change to other peers.
//				localGamer.PropertyChanged += HandleGamerPropertyChanged;
//
//			}

            _remoteGamers   = new GamerCollection <NetworkGamer>();
            _previousGamers = new GamerCollection <NetworkGamer>();
            hostingGamer    = null;

            commandQueue = new Queue <CommandEvent>();

            this.sessionType       = sessionType;
            this.maxGamers         = maxGamers;
            this.privateGamerSlots = privateGamerSlots;
            this.sessionProperties = sessionProperties;
            this.isHost            = isHost;
            this.hostGamerIndex    = hostGamer;
            if (isHost)
            {
                networkPeer = new MonoGamerPeer(this, null);
            }
            else
            {
                if (networkPeer == null)
                {
                    networkPeer = new MonoGamerPeer(this, availableSession);
                }
            }

            CommandGamerJoined gj = new CommandGamerJoined(hostGamer, this.isHost, true);

            commandQueue.Enqueue(new CommandEvent(gj));
        }
Example #4
0
        private void ProcessGamerJoined(CommandGamerJoined command)
        {
            NetworkGamer networkGamer;

            if ((command.State & GamerStates.Local) != (GamerStates)0)
            {
                networkGamer = (NetworkGamer) new LocalNetworkGamer(this, (byte)command.InternalIndex, command.State);
                this._allGamers.AddGamer(networkGamer);
                this._localGamers.AddGamer((LocalNetworkGamer)networkGamer);
                if (Gamer.SignedInGamers.Count >= this._localGamers.Count)
                {
                    ((LocalNetworkGamer)networkGamer).SignedInGamer = ((List <SignedInGamer>)Gamer.SignedInGamers)[this._localGamers.Count - 1];
                }
                networkGamer.PropertyChanged += new PropertyChangedEventHandler(this.HandleGamerPropertyChanged);
            }
            else
            {
                networkGamer                        = new NetworkGamer(this, (byte)command.InternalIndex, command.State);
                networkGamer.DisplayName            = command.DisplayName;
                networkGamer.Gamertag               = command.GamerTag;
                networkGamer.RemoteUniqueIdentifier = command.remoteUniqueIdentifier;
                this._allGamers.AddGamer(networkGamer);
                this._remoteGamers.AddGamer(networkGamer);
            }
            if ((command.State & GamerStates.Host) != (GamerStates)0)
            {
                this.hostingGamer = networkGamer;
            }
            networkGamer.Machine = new NetworkMachine();
            networkGamer.Machine.Gamers.AddGamer(networkGamer);
            if (this.GamerJoined != null)
            {
                this.GamerJoined((object)this, new GamerJoinedEventArgs(networkGamer));
            }
            if (this.networkPeer != null && (command.State & GamerStates.Local) == (GamerStates)0)
            {
                this.networkPeer.SendPeerIntroductions(networkGamer);
            }
            if (this.networkPeer == null)
            {
                return;
            }
            this.networkPeer.UpdateLiveSession(this);
        }
Example #5
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);
		}
Example #6
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);
        }
Example #7
0
		private void ProcessGamerJoined(CommandGamerJoined command) 
		{
			NetworkGamer gamer;
			
			if ((command.State & GamerStates.Local) != 0) {
				gamer = new LocalNetworkGamer(this, (byte)command.InternalIndex, command.State);
				_allGamers.AddGamer(gamer);
				_localGamers.AddGamer((LocalNetworkGamer)gamer);

				// Note - This might be in the wrong place for certain connections
				//  Take a look at HoneycombRush tut for debugging later.
				if (Gamer.SignedInGamers.Count >= _localGamers.Count)
					((LocalNetworkGamer)gamer).SignedInGamer = Gamer.SignedInGamers[_localGamers.Count - 1];
				
				// We will attach a property change handler to local gamers
				//  se that we can broadcast the change to other peers.
				gamer.PropertyChanged += HandleGamerPropertyChanged;				
			}
			else {
				gamer = new NetworkGamer (this, (byte)command.InternalIndex, command.State);
				gamer.DisplayName = command.DisplayName;
				gamer.Gamertag = command.GamerTag;
				gamer.RemoteUniqueIdentifier = command.remoteUniqueIdentifier;
				_allGamers.AddGamer(gamer);
				_remoteGamers.AddGamer(gamer);
			}
			
			if ((command.State & GamerStates.Host) != 0)
				hostingGamer = gamer;
			
			gamer.Machine = new NetworkMachine();
			gamer.Machine.Gamers.AddGamer(gamer);
			//gamer.IsReady = true;
			
			if (GamerJoined != null) {
				GamerJoined(this, new GamerJoinedEventArgs(gamer));
			}
			
			if (networkPeer !=  null && (command.State & GamerStates.Local) == 0) {
				
				networkPeer.SendPeerIntroductions(gamer);
			}
			
			if (networkPeer != null)
			{
				networkPeer.UpdateLiveSession(this);
			}
			
			
		}
Example #8
0
		private NetworkSession (NetworkSessionType sessionType, int maxGamers, int privateGamerSlots, NetworkSessionProperties sessionProperties, bool isHost, int hostGamer, AvailableNetworkSession availableSession) : this()
		{
			if (sessionProperties == null) {
				throw new ArgumentNullException ("sessionProperties");
			}
			
			_allGamers = new GamerCollection<NetworkGamer>();
			_localGamers = new GamerCollection<LocalNetworkGamer>();
//			for (int x = 0; x < Gamer.SignedInGamers.Count; x++) {
//				GamerStates states = GamerStates.Local;
//				if (x == 0)
//					states |= GamerStates.Host;
//				LocalNetworkGamer localGamer = new LocalNetworkGamer(this, (byte)x, states);
//				localGamer.SignedInGamer = Gamer.SignedInGamers[x];
//				_allGamers.AddGamer(localGamer);
//				_localGamers.AddGamer(localGamer);
//				
//				// We will attach a property change handler to local gamers
//				//  se that we can broadcast the change to other peers.
//				localGamer.PropertyChanged += HandleGamerPropertyChanged;	
//				
//			}

			_remoteGamers = new GamerCollection<NetworkGamer>();
			_previousGamers = new GamerCollection<NetworkGamer>();
			hostingGamer = null;
			
			commandQueue = new Queue<CommandEvent>();			
			
			this.sessionType = sessionType;
			this.maxGamers = maxGamers;
			this.privateGamerSlots = privateGamerSlots;
			this.sessionProperties = sessionProperties;
			this.isHost = isHost;
			this.hostGamerIndex = hostGamer;            
            if (isHost)
                networkPeer = new MonoGamerPeer(this, null);
            else
            {
                if (networkPeer == null)
                    networkPeer = new MonoGamerPeer(this, availableSession);
            }
            			
			CommandGamerJoined gj = new CommandGamerJoined(hostGamer, this.isHost, true);
			commandQueue.Enqueue(new CommandEvent(gj));
		}
Example #9
0
        private NetworkSession(NetworkSessionType sessionType, int maxGamers, int privateGamerSlots, NetworkSessionProperties sessionProperties, bool isHost, int hostGamer, AvailableNetworkSession availableSession)
            : this()
        {
            if (sessionProperties == null) {
                throw new ArgumentNullException ("sessionProperties");
            }

            _allGamers = new GamerCollection<NetworkGamer>();
            _localGamers = new GamerCollection<LocalNetworkGamer>();
            _remoteGamers = new GamerCollection<NetworkGamer>();
            _previousGamers = new GamerCollection<NetworkGamer>();
            hostingGamer = null;

            commandQueue = new Queue<CommandEvent>();

            this.sessionType = sessionType;
            this.maxGamers = maxGamers;
            this.privateGamerSlots = privateGamerSlots;
            this.sessionProperties = sessionProperties;
            this.isHost = isHost;
            this.hostGamerIndex = hostGamer;
            if (isHost)
                networkPeer = new MonoGamerPeer(this, null);
            else
            {
                if (networkPeer == null)
                    networkPeer = new MonoGamerPeer(this, availableSession);
            }

            CommandGamerJoined gj = new CommandGamerJoined(hostGamer, this.isHost, true);
            commandQueue.Enqueue(new CommandEvent(gj));
        }
Example #10
0
        private void ProcessGamerJoined(CommandGamerJoined command)
        {
            NetworkGamer gamer;

            if ((command.State & GamerStates.Local) != 0) {
                gamer = new LocalNetworkGamer(this, (byte)command.InternalIndex, command.State);
                _allGamers.AddGamer(gamer);
                _localGamers.AddGamer((LocalNetworkGamer)gamer);

                // We will attach a property change handler to local gamers
                //  se that we can broadcast the change to other peers.
                gamer.PropertyChanged += HandleGamerPropertyChanged;
            }
            else {
                gamer = new NetworkGamer (this, (byte)command.InternalIndex, command.State);
                gamer.RemoteUniqueIdentifier = command.remoteUniqueIdentifier;
                _allGamers.AddGamer(gamer);
                _remoteGamers.AddGamer(gamer);
            }

            if ((command.State & GamerStates.Host) != 0)
                hostingGamer = gamer;

            gamer.Machine = new NetworkMachine();
            gamer.Machine.Gamers.AddGamer(gamer);
            //gamer.IsReady = true;

            if (GamerJoined != null) {
                GamerJoined(this, new GamerJoinedEventArgs(gamer));
            }

            if (networkPeer !=  null && (command.State & GamerStates.Local) == 0) {

                networkPeer.SendPeerIntroductions(gamer);
            }

            if (networkPeer != null)
            {
                networkPeer.UpdateLiveSession(this);
            }
        }