Example #1
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");
     }
     this._allGamers        = new GamerCollection <NetworkGamer>();
     this._localGamers      = new GamerCollection <LocalNetworkGamer>();
     this._remoteGamers     = new GamerCollection <NetworkGamer>();
     this._previousGamers   = new GamerCollection <NetworkGamer>();
     this.hostingGamer      = (NetworkGamer)null;
     this.commandQueue      = new Queue <CommandEvent>();
     this.sessionType       = sessionType;
     this.maxGamers         = maxGamers;
     this.privateGamerSlots = privateGamerSlots;
     this.sessionProperties = sessionProperties;
     this.isHost            = isHost;
     this.hostGamerIndex    = hostGamer;
     if (isHost)
     {
         this.networkPeer = new MonoGamerPeer(this, (AvailableNetworkSession)null);
     }
     else if (this.networkPeer == null)
     {
         this.networkPeer = new MonoGamerPeer(this, availableSession);
     }
     this.commandQueue.Enqueue(new CommandEvent((ICommand) new CommandGamerJoined(hostGamer, this.isHost, true)));
 }
        public static AvailableNetworkSessionCollection EndFind(IAsyncResult result)
        {
            AvailableNetworkSessionCollection returnValue     = null;
            List <AvailableNetworkSession>    networkSessions = new List <AvailableNetworkSession>();

            try {
                // Retrieve the delegate.
                AsyncResult asyncResult = (AsyncResult)result;

                // Wait for the WaitHandle to become signaled.
                result.AsyncWaitHandle.WaitOne();


                // Call EndInvoke to retrieve the results.
                if (asyncResult.AsyncDelegate is NetworkSessionAsynchronousFind)
                {
                    returnValue = ((NetworkSessionAsynchronousFind)asyncResult.AsyncDelegate).EndInvoke(result);

                    MonoGamerPeer.FindResults(networkSessions);
                }
            } finally {
                // Close the wait handle.
                result.AsyncWaitHandle.Close();
            }
            returnValue = new AvailableNetworkSessionCollection(networkSessions);
            return(returnValue);
        }
        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 static AvailableNetworkSessionCollection Find(NetworkSessionType sessionType, int hostGamer, int maxLocalGamers, NetworkSessionProperties searchProperties)
 {
     try
     {
         if (maxLocalGamers < 1 || maxLocalGamers > 4)
         {
             throw new ArgumentOutOfRangeException("maxLocalGamers must be between 1 and 4.");
         }
         List <AvailableNetworkSession> list = new List <AvailableNetworkSession>();
         MonoGamerPeer.Find(sessionType);
         return(new AvailableNetworkSessionCollection((IList <AvailableNetworkSession>)list));
     }
     finally
     {
     }
 }
Example #5
0
        internal void UpdateLiveSession(NetworkSession networkSession)
        {
            if (this.peer == null || MonoGamerPeer.m_masterServer == null || !networkSession.IsHost)
            {
                return;
            }
            NetOutgoingMessage message = ((NetPeer)this.peer).CreateMessage();

            message.Write((byte)0);
            message.Write(this.session.AllGamers.Count);
            message.Write(this.session.LocalGamers[0].Gamertag);
            message.Write(this.session.PrivateGamerSlots);
            message.Write(this.session.MaxGamers);
            message.Write(this.session.LocalGamers[0].IsHost);
            IPAddress address = IPAddress.Parse(MonoGamerPeer.GetMyLocalIpAddress());

            message.Write(new IPEndPoint(address, MonoGamerPeer.port));
            message.Write(((NetPeer)this.peer).get_Configuration().get_AppIdentifier());
            ((NetPeer)this.peer).SendUnconnectedMessage(message, MonoGamerPeer.m_masterServer);
        }
Example #6
0
        public static AvailableNetworkSessionCollection EndFind(IAsyncResult result)
        {
            AvailableNetworkSessionCollection sessionCollection = (AvailableNetworkSessionCollection)null;
            List <AvailableNetworkSession>    networkSessions   = new List <AvailableNetworkSession>();

            try
            {
                AsyncResult asyncResult = (AsyncResult)result;
                result.AsyncWaitHandle.WaitOne();
                if (asyncResult.AsyncDelegate is NetworkSessionAsynchronousFind)
                {
                    sessionCollection = ((NetworkSessionAsynchronousFind)asyncResult.AsyncDelegate).EndInvoke(result);
                    MonoGamerPeer.FindResults(networkSessions);
                }
            }
            finally
            {
                result.AsyncWaitHandle.Close();
            }
            return(new AvailableNetworkSessionCollection((IList <AvailableNetworkSession>)networkSessions));
        }
Example #7
0
        public static void RequestNATIntroduction(IPEndPoint host, NetPeer peer)
        {
            if (host == null)
            {
                return;
            }
            if (MonoGamerPeer.m_masterServer == null)
            {
                throw new Exception("Must connect to master server first!");
            }
            NetOutgoingMessage message = peer.CreateMessage();

            message.Write((byte)2);
            IPAddress address = IPAddress.Parse(MonoGamerPeer.GetMyLocalIpAddress());

            message.Write(new IPEndPoint(address, peer.get_Port()));
            IPEndPoint ipEndPoint = new IPEndPoint(host.Address, MonoGamerPeer.port);

            message.Write(ipEndPoint);
            message.Write(peer.get_Configuration().get_AppIdentifier());
            peer.SendUnconnectedMessage(message, MonoGamerPeer.m_masterServer);
        }
Example #8
0
        internal static void Find(NetworkSessionType sessionType)
        {
            NetPeerConfiguration peerConfiguration = new NetPeerConfiguration(MonoGamerPeer.applicationIdentifier);

            if (sessionType == NetworkSessionType.PlayerMatch)
            {
                peerConfiguration.EnableMessageType((NetIncomingMessageType)2);
                peerConfiguration.EnableMessageType((NetIncomingMessageType)2048);
            }
            else
            {
                peerConfiguration.EnableMessageType((NetIncomingMessageType)32);
            }
            if (MonoGameNetworkConfiguration.Broadcast != IPAddress.None)
            {
                peerConfiguration.set_BroadcastAddress(MonoGameNetworkConfiguration.Broadcast);
            }
            MonoGamerPeer.netPeer = new NetPeer(peerConfiguration);
            MonoGamerPeer.netPeer.Start();
            if (sessionType == NetworkSessionType.PlayerMatch)
            {
                MonoGamerPeer.GetServerList(MonoGamerPeer.netPeer);
            }
            else
            {
                MonoGamerPeer.netPeer.DiscoverLocalPeers(MonoGamerPeer.port);
            }
            DateTime now = DateTime.Now;

            MonoGamerPeer.discoveryMsgs = new List <NetIncomingMessage>();
            do
            {
                NetIncomingMessage netIncomingMessage;
                while ((netIncomingMessage = MonoGamerPeer.netPeer.ReadMessage()) != null)
                {
                    NetIncomingMessageType messageType = netIncomingMessage.get_MessageType();
                    if (messageType <= 128)
                    {
                        if (messageType != 2)
                        {
                            if (messageType != 64)
                            {
                                if (messageType == 128)
                                {
                                    ;
                                }
                            }
                            else
                            {
                                MonoGamerPeer.discoveryMsgs.Add(netIncomingMessage);
                            }
                        }
                        else if (netIncomingMessage.get_SenderEndpoint().Equals((object)MonoGamerPeer.m_masterServer))
                        {
                            MonoGamerPeer.discoveryMsgs.Add(netIncomingMessage);
                        }
                    }
                    else if (messageType == 256 || messageType == 512 || messageType == 1024)
                    {
                        ;
                    }
                }
            }while ((DateTime.Now - now).Seconds <= 2);
            MonoGamerPeer.netPeer.Shutdown("Find shutting down");
        }
Example #9
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);
        }
Example #10
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 #11
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");
   this._allGamers = new GamerCollection<NetworkGamer>();
   this._localGamers = new GamerCollection<LocalNetworkGamer>();
   this._remoteGamers = new GamerCollection<NetworkGamer>();
   this._previousGamers = new GamerCollection<NetworkGamer>();
   this.hostingGamer = (NetworkGamer) null;
   this.commandQueue = new Queue<CommandEvent>();
   this.sessionType = sessionType;
   this.maxGamers = maxGamers;
   this.privateGamerSlots = privateGamerSlots;
   this.sessionProperties = sessionProperties;
   this.isHost = isHost;
   this.hostGamerIndex = hostGamer;
   if (isHost)
     this.networkPeer = new MonoGamerPeer(this, (AvailableNetworkSession) null);
   else if (this.networkPeer == null)
     this.networkPeer = new MonoGamerPeer(this, availableSession);
   this.commandQueue.Enqueue(new CommandEvent((ICommand) new CommandGamerJoined(hostGamer, this.isHost, true)));
 }