private void NetSessionJoin(IAsyncResult res) { _asyncSessionOperationsInProgress--; Exception err = null; NetworkSession joined = null; try { joined = NetworkSession.EndJoin(res); } catch (Exception e) { err = e; } Session = joined; if (SessionJoined != null) { SessionJoined(this, new NetworkSessionJoinedEventArgs() { Error = err, Joined = joined }); } }
/// <summary> /// Raised when the the player joined the Session /// </summary> /// <param name="asyncResult"></param> private void OnLiveSessionJoined(IAsyncResult asyncResult) { NetworkSession networkSession = NetworkSession.EndJoin(asyncResult); CurrentSession = new LiveSession(networkSession); OnSessionJoined(); }
/* Called internally when a connection has been established to a session. */ protected virtual void OnSessionConnected(IAsyncResult ar) { joinAsync_ = null; session = NetworkSession.EndJoin(ar); if (session == null) { TryAgain(); } else { sessionEstablished = true; UpdateVoice(); } }
void info_Pressed(object sender, EventArgs e) { if (!Guide.IsVisible && !allScreens["chatScreen"].Visible) { AvailableNetworkSession asession = sender.Cast <SessionInfoDisplay>().Session; IAsyncResult beginJoinSess = NetworkSession.BeginJoin(asession, null, null); beginJoinSess.AsyncWaitHandle.WaitOne(); this.session = NetworkSession.EndJoin(beginJoinSess); session.GamerJoined += new EventHandler <GamerJoinedEventArgs>(session_GamerJoined); session.GameStarted += new EventHandler <GameStartedEventArgs>(session_GameStarted); Services.AddService(typeof(NetworkSession), session); } }
/// <summary> /// Event handler for when the asynchronous join network session /// operation has completed. /// </summary> void JoinSessionOperationCompleted(object sender, OperationCompletedEventArgs e) { try { // End the asynchronous join network session operation. NetworkSession networkSession = NetworkSession.EndJoin(e.AsyncResult); // Create a component that will manage the session we just joined. 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); availableSessions.Dispose(); } catch (Exception exception) { NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception); ScreenManager.AddScreen(errorScreen, ControllingPlayer); } }
/// <summary> /// Callback to load the lobby screen with the new session. /// </summary> private void LoadLobbyScreen(object sender, OperationCompletedEventArgs e) { NetworkSession networkSession = null; try { networkSession = NetworkSession.EndJoin(e.AsyncResult); } catch (NetworkException ne) { const string message = "Failed joining session."; MessageBoxScreen messageBox = new MessageBoxScreen(message); messageBox.Accepted += FailedMessageBox; messageBox.Cancelled += FailedMessageBox; ScreenManager.AddScreen(messageBox); System.Console.WriteLine("Failed joining session: " + ne.Message); } catch (GamerPrivilegeException gpe) { const string message = "You do not have permission to join a session."; MessageBoxScreen messageBox = new MessageBoxScreen(message); messageBox.Accepted += FailedMessageBox; messageBox.Cancelled += FailedMessageBox; ScreenManager.AddScreen(messageBox); System.Console.WriteLine( "Insufficient privilege to join session: " + gpe.Message); } if (networkSession != null) { LobbyScreen lobbyScreen = new LobbyScreen(networkSession); lobbyScreen.ScreenManager = this.ScreenManager; ScreenManager.AddScreen(lobbyScreen); } }
/// <summary> /// Event handler for when the asynchronous join network session /// operation has completed. /// </summary> void JoinSessionOperationCompleted(object sender, OperationCompletedEventArgs e) { try { // End the asynchronous join network session operation. NetworkSession networkSession = NetworkSession.EndJoin(e.AsyncResult); // Create a component that will manage the session we just joined. NetworkSessionComponent.Create(ScreenManager, networkSession); // Go to the lobby screen. ScreenManager.AddScreen(new LobbyScreen(networkSession)); availableSessions.Dispose(); } catch (NetworkException exception) { ScreenManager.AddScreen(new NetworkErrorScreen(exception)); } catch (GamerPrivilegeException exception) { ScreenManager.AddScreen(new NetworkErrorScreen(exception)); } }
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; } } }
/// <summary> /// Update /// </summary> public override void Update(GameTime gameTime) { // Process different phases. switch (phase) { case ConnectionPahse.EnsureSignedIn: GamerServicesDispatcher.Update(); break; case ConnectionPahse.FindSessions: GamerServicesDispatcher.Update(); if (asyncResult.IsCompleted) { AvailableNetworkSessionCollection sessions = NetworkSession.EndFind(asyncResult); if (sessions.Count > 0) { asyncResult = NetworkSession.BeginJoin(sessions[0], null, null); commandHost.EchoError("Connecting to the host..."); phase = ConnectionPahse.Joining; } else { commandHost.EchoError("Couldn't find a session."); phase = ConnectionPahse.None; } } break; case ConnectionPahse.Joining: GamerServicesDispatcher.Update(); if (asyncResult.IsCompleted) { NetworkSession = NetworkSession.EndJoin(asyncResult); NetworkSession.SessionEnded += new EventHandler <NetworkSessionEndedEventArgs>( NetworkSession_SessionEnded); OwnsNetworkSession = true; commandHost.EchoError("Connected to the host."); phase = ConnectionPahse.None; asyncResult = null; ConnectedToRemote(); } break; } // Update Network session. if (OwnsNetworkSession) { GamerServicesDispatcher.Update(); NetworkSession.Update(); if (NetworkSession != null) { // Process received packets. foreach (LocalNetworkGamer gamer in NetworkSession.LocalGamers) { while (gamer.IsDataAvailable) { NetworkGamer sender; gamer.ReceiveData(packetReader, out sender); if (!sender.IsLocal) { ProcessRecievedPacket(packetReader.ReadString()); } } } } } base.Update(gameTime); }
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; } } } }
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(); } } }