public void SendData (
			byte[] data,
			SendDataOptions options)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, 0, data.Length, options, null, this ));
			Session.commandQueue.Enqueue(cme);
		}
		public void SendData (
			byte[] data,
			SendDataOptions options,
			NetworkGamer recipient)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, 0, data.Length, options, recipient, this ));
			Session.commandQueue.Enqueue(cme);
		}
		public void SendData (
			byte[] data,
			int offset,
			int count,
			SendDataOptions options)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, offset, count, options, null, this ));
			Session.commandQueue.Enqueue(cme);
		}
		public void SendData (
			byte[] data,
			int offset,
			int count,
			SendDataOptions options,
			NetworkGamer recipient)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, offset, count, options, recipient,this ));
			Session.commandQueue.Enqueue(cme);
		}
Exemple #5
0
        public void Update()
        {
            try
            {
                while (this.commandQueue.Count > 0 && this.networkPeer.IsReady)
                {
                    CommandEvent commandEvent = this.commandQueue.Dequeue();
                    if (commandEvent != null)
                    {
                        switch (commandEvent.Command)
                        {
                        case CommandEventType.GamerJoined:
                            this.ProcessGamerJoined((CommandGamerJoined)commandEvent.CommandObject);
                            break;

                        case CommandEventType.GamerLeft:
                            this.ProcessGamerLeft((CommandGamerLeft)commandEvent.CommandObject);
                            break;

                        case CommandEventType.SessionStateChange:
                            this.ProcessSessionStateChange((CommandSessionStateChange)commandEvent.CommandObject);
                            break;

                        case CommandEventType.SendData:
                            this.ProcessSendData((CommandSendData)commandEvent.CommandObject);
                            break;

                        case CommandEventType.ReceiveData:
                            this.ProcessReceiveData((CommandReceiveData)commandEvent.CommandObject);
                            break;

                        case CommandEventType.GamerStateChange:
                            this.ProcessGamerStateChange((CommandGamerStateChange)commandEvent.CommandObject);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
            }
        }
        void HandleGamerPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            NetworkGamer gamer = sender as NetworkGamer;

            if (gamer == null)
            {
                return;
            }

            // If the gamer is local then we need to broadcast that change to all other
            // connected peers.  This is a double check here as we should only be handling
            //  property changes for local gamers for now.
            if (gamer.IsLocal)
            {
                CommandGamerStateChange sc  = new CommandGamerStateChange(gamer);
                CommandEvent            cmd = new CommandEvent(sc);
                commandQueue.Enqueue(cmd);
            }
        }
Exemple #7
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);
		}
Exemple #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);
        }
		public void SendData (
			byte[] data,
			SendDataOptions options,
			NetworkGamer recipient)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, 0, data.Length, options, recipient, this ));
			Session.commandQueue.Enqueue(cme);
		}
		public void SendData (
			byte[] data,
			SendDataOptions options)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, 0, data.Length, options, null, this ));
			Session.commandQueue.Enqueue(cme);
		}
		public void SendData (
			byte[] data,
			int offset,
			int count,
			SendDataOptions options,
			NetworkGamer recipient)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, offset, count, options, recipient,this ));
			Session.commandQueue.Enqueue(cme);
		}
		public void SendData (
			byte[] data,
			int offset,
			int count,
			SendDataOptions options)
		{
			CommandEvent cme = new CommandEvent(new CommandSendData(data, offset, count, options, null, this ));
			Session.commandQueue.Enqueue(cme);
		}
Exemple #13
0
		void HandleGamerPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			NetworkGamer gamer = sender as NetworkGamer;
			if (gamer == null)
				return;
			
			// If the gamer is local then we need to broadcast that change to all other
			// connected peers.  This is a double check here as we should only be handling 
			//  property changes for local gamers for now.
			if (gamer.IsLocal) {
				CommandGamerStateChange sc = new CommandGamerStateChange(gamer);
				CommandEvent cmd = new CommandEvent(sc);
				commandQueue.Enqueue(cmd);
			}
		}