Пример #1
0
 /* A session has actually been created (or failed to be created)
  */
 protected virtual void OnSessionCreated(IAsyncResult ar)
 {
     createAsync_ = null;
     try
     {
         session = NetworkSession.EndCreate(ar);
         if (session == null)
         {
             TryAgain();
         }
         else
         {
             //  After a while, close and try again, if I haven't gotten any players.
             //  There might be someone else waiting for me to connect.
             //  Wait online between 300 and 600 seconds, which works out to 5 - 10 minutes.
             sessionCloseTime   = Timing.NowTicks + (rand.Next(300) + 300) * 10000000L;
             sessionEstablished = true;
             UpdateVoice();
         }
     }
     catch (Microsoft.Xna.Framework.Net.NetworkException nex)
     {
         Trace.WriteLine(String.Format("Swallowing network exception: {0}", nex));
         session = null;
     }
 }
Пример #2
0
        void createSession_Pressed(object sender, EventArgs e)
        {
            if (!Guide.IsVisible && !allScreens["chatScreen"].Visible)
            {
                IAsyncResult beginCrSess = NetworkSession.BeginCreate(
                    NetworkSessionType.SystemLink,
                    maximumLocalPlayers, maximumGamers, null,
                    null);
                beginCrSess.AsyncWaitHandle.WaitOne();
                session = NetworkSession.EndCreate(beginCrSess);
                session.AllowJoinInProgress       = true;
                session.GamerJoined              += new EventHandler <GamerJoinedEventArgs>(session_GamerJoined);
                session.GameStarted              += new EventHandler <GameStartedEventArgs>(session_GameStarted);
                allScreens["titleScreen"].Visible = false;
                allScreens["playerList"].Visible  = true;
                Services.AddService(typeof(NetworkSession), session);

                /*
                 * Texture2D newGamerImage = Texture2D.FromStream(GraphicsDevice, Gamer.SignedInGamers[0].GetProfile().GetGamerPicture());
                 * Vector2 pos = new Vector2(100, 50);
                 * Sprite gamerIcon = new Sprite(newGamerImage, pos, spriteBatch);
                 * allScreens["playerList"].Sprites.Add(gamerIcon);
                 * TextSprite gamerName = new TextSprite(spriteBatch, new Vector2(pos.X + gamerIcon.Width + 5, pos.Y), font, Gamer.SignedInGamers[0].DisplayName == null ? Gamer.SignedInGamers[0].Gamertag : Gamer.SignedInGamers[0].DisplayName);
                 * allScreens["playerList"].AdditionalSprites.Add(gamerName);
                 */
            }
        }
Пример #3
0
        private void onSessionCreation(IAsyncResult res)
        {
            NetworkSession createdSession = null;

            try
            {
                createdSession = NetworkSession.EndCreate(res);
            }
            catch
            {
                createdSession = null;
            }

            if (createdSession == null)
            {
                if (!Guide.IsVisible)
                {
                    Guide.BeginShowMessageBox(PlayerIndex.One, "Error Creating Multiplayer Game", "An error occurred during the creation of the multiplayer session.", new string[] { "OK" }, 0, MessageBoxIcon.Error, null, null);
                }
                GLibXNASampleGame.Instance.SetScreen("MainMenu");
                return;
            }

            GLibXNASampleGame.Instance.SessionManagement.JoinSession(createdSession);
            GLibXNASampleGame.Instance.NetworkTransmitter.Session = createdSession;
            GLibXNASampleGame.Instance.SetScreen("NetworkLobby");
        }
Пример #4
0
        /// <summary>
        /// Raised when the Session is created
        /// </summary>
        /// <param name="asyncResult"></param>
        private void OnLiveSessionCreated(IAsyncResult asyncResult)
        {
            _networkSession = NetworkSession.EndCreate(asyncResult);

            CurrentSession = new LiveSession(_networkSession);

            OnSessionCreated();
        }
        /// <summary>
        /// Event handler for when the asynchronous create network session
        /// operation has completed.
        /// </summary>
        void CreateSessionOperationCompleted(object sender,
                                             OperationCompletedEventArgs e)
        {
            try {
                // End the asynchronous create network session operation.
                NetworkSession networkSession = NetworkSession.EndCreate(e.AsyncResult);

                // Create a component that will manage the session we just created.
                NetworkSessionComponent.Create(ScreenManager, networkSession);

                // Go to the lobby screen. We pass null as the controlling player,
                // because the lobby screen accepts input from all local players
                // who are in the session, not just a single controlling player.
                ScreenManager.AddScreen(new LobbyScreen(networkSession), null);
            } catch (Exception exception) {
                NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);

                ScreenManager.AddScreen(errorScreen, ControllingPlayer);
            }
        }
        /// <summary>
        /// Event handler for when the asynchronous create network session
        /// operation has completed.
        /// </summary>
        void CreateSessionOperationCompleted(object sender,
                                             OperationCompletedEventArgs e)
        {
            try
            {
                // End the asynchronous create network session operation.
                NetworkSession networkSession;
                try
                {
                    networkSession = NetworkSession.EndCreate(e.AsyncResult);
                    NetworkSessionComponent.Create(ScreenManager, networkSession);
                    // Go to the lobby screen. We pass null as the controlling player,
                    // because the lobby screen accepts input from all local players
                    // who are in the session, not just a single controlling player.
                    ScreenManager.AddScreen(new LobbyScreen(networkSession), null);
                }
                catch
                {
                    try
                    {
                        Game game = this.ScreenManager.Game;
                        game.Services.RemoveService(typeof(NetworkSession));
                        //game.Services.AddService(typeof(NetworkSession), networkSession);
                        LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                           new MainMenuScreen());
                    }
                    catch { }
                }

                // Create a component that will manage the session we just created.
            }
            catch (Exception exception)
            {
                NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);

                ScreenManager.AddScreen(errorScreen, ControllingPlayer);
            }
        }
        /// <summary>
        /// Callback when a session is created.
        /// </summary>
        void SessionCreated(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;

            try
            {
                networkSession = NetworkSession.EndCreate(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string     message    = "Failed creating the session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to create session:  " +
                                         ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to create a session. ";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + gpe.Message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to create session:  " + gpe.Message);
            }
            if (networkSession != null)
            {
                networkSession.AllowHostMigration  = true;
                networkSession.AllowJoinInProgress = false;
                LoadLobbyScreen(networkSession);
            }
        }
Пример #8
0
        /// <summary>
        /// Event handler for when the asynchronous join-from-invite
        /// operation has completed.
        /// </summary>
        static void JoinInvitedOperationCompleted(object sender,
                                                  OperationCompletedEventArgs e)
        {
            ScreenManager screenManager = ((GameScreen)sender).ScreenManager;

            try
            {
                // End the asynchronous join-from-invite operation.
                NetworkSession networkSession =
                    NetworkSession.EndCreate(e.AsyncResult);

                // Create a component that will manage the session we just created.
                NetworkSessionComponent.Create(screenManager, networkSession);

                // Go to the lobby screen.
                screenManager.AddScreen(new LobbyScreen(networkSession), null);
            }
            catch (Exception exception)
            {
                screenManager.AddScreen(new MainMenuScreen(), null);
                screenManager.AddScreen(new NetworkErrorScreen(exception), null);
            }
        }
Пример #9
0
        /// <summary>
        /// Event handler for when the asynchronous create network session
        /// operation has completed.
        /// </summary>
        void CreateSessionOperationCompleted(object sender,
                                             OperationCompletedEventArgs e)
        {
            try
            {
                // End the asynchronous create network session operation.
                NetworkSession networkSession = NetworkSession.EndCreate(e.AsyncResult);

                // Create a component that will manage the session we just created.
                NetworkSessionComponent.Create(ScreenManager, networkSession);

                // Go to the lobby screen.
                ScreenManager.AddScreen(new LobbyScreen(networkSession));
            }
            catch (NetworkException exception)
            {
                ScreenManager.AddScreen(new NetworkErrorScreen(exception));
            }
            catch (GamerPrivilegeException exception)
            {
                ScreenManager.AddScreen(new NetworkErrorScreen(exception));
            }
        }
Пример #10
0
        public void CheckNetworkSearch()
        {
            if (IsCreating || IsJoining || IsSearching)
            {
                bool TemparyProfiles = false;

                foreach (CompletePlayer player in game.ThisGamesPlayers)
                {
                    if (player.InUse && !player.IsProfile)
                    {
                        TemparyProfiles = true;
                    }
                }

                if (!TemparyProfiles)
                //if(true)
                {
                    if (IsSearching && FindResult.IsCompleted)
                    {
                        FindResult.AsyncWaitHandle.WaitOne();
                        try
                        {
                            availableSessions = NetworkSession.EndFind(FindResult);

                            if (availableSessions.Count == 0)
                            {
                                SetString("No Games Found, Creating Game...");

                                CreateGame();
                            }
                            else
                            {
                                SetString(availableSessions.Count.ToString() + " Games Found\nFinding Best Game");

                                SortSessions();
                            }

                            IsSearching = false;
                        }
                        catch (Exception e)
                        {
                            // if(networkSession==null)
                            if (e.Message != null)
                            {
                                OnlineString      = " ";
                                game.ErrorMessage = e.Message.Replace(". ", ".\n");
                                game.menus.GoTo("Error", false);
                                IsCreating  = false;
                                IsJoining   = false;
                                IsSearching = false;
                            }
                        }



                        IsSearching = false;
                    }

                    if (IsJoining && JoinResult.IsCompleted)
                    {
                        JoinResult.AsyncWaitHandle.WaitOne();

                        try
                        {
                            networkSession = NetworkSession.EndJoin(JoinResult);

                            HookSessionEvents();

                            BeginOnlineGameAsGuest();
                        }
                        catch (Exception e)
                        {
                            // if (networkSession == null)
                            if (e.Message != null)
                            {
                                OnlineString      = " ";
                                game.ErrorMessage = e.Message.Replace(". ", ".\n");
                                game.menus.GoTo("Error", false);
                                IsCreating  = false;
                                IsJoining   = false;
                                IsSearching = false;
                            }
                        }
                        IsJoining = false;
                    }

                    if (IsCreating && CreateResult.IsCompleted)
                    {
                        CreateResult.AsyncWaitHandle.WaitOne();
                        try
                        {
                            networkSession = NetworkSession.EndCreate(CreateResult);

                            HookSessionEvents();

                            BeginOnlineGameAsHost();
                        }
                        catch (Exception e)
                        {
                            if (e.Message != null)
                            {
                                OnlineString      = " ";
                                game.ErrorMessage = e.Message.Replace(". ", ".\n");
                                game.menus.GoTo("Error", false);
                                IsCreating  = false;
                                IsJoining   = false;
                                IsSearching = false;
                            }
                        }

                        IsCreating = false;
                    }
                }
                else
                {
                    OnlineString      = " ";
                    game.ErrorMessage = "No Temporary Profiles May Be Taken Online \n Sign into XBOX LIVE Guest Profiles Instead";
                    game.menus.GoTo("Error", false);
                    IsCreating  = false;
                    IsJoining   = false;
                    IsSearching = false;
                }
            }
        }
Пример #11
0
 private static void CreateSessionComplete(IAsyncResult result)
 {
     networkSession              = NetworkSession.EndCreate(result);
     networkSession.GamerJoined += new EventHandler <GamerJoinedEventArgs>(networkSession_GamerJoined);
 }
Пример #12
0
        public void Update()
        {
            if (PendingHost)
            {
                if (createResult.IsCompleted)
                {
                    netPlay.NetSession = NetworkSession.EndCreate(createResult);
                    netPlay.Hosting    = true;
                    PendingHost        = false;
                }
            }
            if (PendingFind)
            {
                if (findResult.IsCompleted)
                {
                    AvailableNetworkSessionCollection availableSessions =
                        NetworkSession.EndFind(findResult);
                    if (availableSessions.Count > 0)
                    {
                        joinResult = NetworkSession.BeginJoin(
                            availableSessions[0], new AsyncCallback(GotResult), null);
                        PendingJoin = true;
                    }
                    PendingFind = false;
                }
            }
            if (PendingJoin)
            {
                if (joinResult.IsCompleted)
                {
                    netPlay.NetSession = NetworkSession.EndJoin(joinResult);
                    netPlay.Joined     = true;
                    PendingJoin        = false;
                }
            }

            if (netPlay.Hosting)
            {
                //
            }
            if (netPlay.Joined)
            {
                //
            }
            if (netPlay.NetSession != null)
            {
                if (!netPlay.NetSession.IsDisposed)
                {
                    bool ended = false;
                    if (netPlay.NetSession.SessionState == NetworkSessionState.Playing)
                    {
                        if (netPlay.NetSession.AllGamers.Count < 2)
                        {
                            ended = true;
                        }
                    }
                    else if (netPlay.NetSession.SessionState == NetworkSessionState.Ended)
                    {
                        ended = true;
                    }

                    if (ended)
                    {
                        Game1.Menu.EndGame();
                        netPlay.NetSession.Dispose();
                        netPlay.Hosting = false;
                        netPlay.Joined  = false;
                    }
                }
            }
        }
Пример #13
0
        void DisposeSession()
        {
            if (session != null)
            {
                Trace.WriteLine(String.Format("Disposing session: sessionCloseTime {0} now {1}",
                                              new DateTime(sessionCloseTime), DateTime.Now));
                session.Dispose();
                session             = null;
                sessionDisconnected = true;
            }
            sessionCloseTime = 0;
            IDisposable id = null;

            /* If I'm currently looking for sessions to join, stop that.
             */
            if (findAsync_ != null)
            {
                try
                {
                    id = NetworkSession.EndFind(findAsync_);
                }
                catch (System.Exception x)
                {
                    Trace.WriteLine("Swallowing EndFind exception: " + x.ToString());
                }
                findAsync_ = null;
                if (id != null)
                {
                    id.Dispose();
                }
            }

            /* If I'mcurrently looking to join a session, stop that.
             */
            if (joinAsync_ != null)
            {
                try
                {
                    id = NetworkSession.EndJoin(joinAsync_);
                }
                catch (System.Exception x)
                {
                    Trace.WriteLine("Swallowing EndJoin exception: " + x.ToString());
                }
                joinAsync_ = null;
                if (id != null)
                {
                    id.Dispose();
                }
            }

            /* If I'm currently trying to host a session, stop that.
             */
            if (createAsync_ != null)
            {
                try
                {
                    id = NetworkSession.EndCreate(createAsync_);
                }
                catch (System.Exception x)
                {
                    Trace.WriteLine("Swallowing EndCreate exception: " + x.ToString());
                }
                createAsync_ = null;
                if (id != null)
                {
                    id.Dispose();
                }
            }
        }