/// <summary> /// Sends a Join query to the Session asynchronously /// </summary> /// <param name="availableSession">The Session we are trying to join</param> public override void JoinSession(AvailableSession availableSession) { _networkSessionLocker = JoiningSession; NetworkSession.BeginJoin(((LiveAvailableSession)availableSession).AvailableNetworkSession, OnLiveSessionJoined, _networkSessionLocker); }
/// <summary> /// Callback to receive the network-session search results from quick-match. /// </summary> void QuickMatchSearchCompleted(object sender, OperationCompletedEventArgs e) { try { AvailableNetworkSessionCollection availableSessions = NetworkSession.EndFind(e.AsyncResult); if ((availableSessions != null) && (availableSessions.Count > 0)) { // join the session try { IAsyncResult asyncResult = NetworkSession.BeginJoin( availableSessions[0], null, null); // create the busy screen NetworkBusyScreen busyScreen = new NetworkBusyScreen( "Joining the session...", asyncResult); busyScreen.OperationCompleted += QuickMatchSessionJoined; ScreenManager.AddScreen(busyScreen); } catch (NetworkException ne) { const string message = "Failed joining the session."; MessageBoxScreen messageBox = new MessageBoxScreen(message); messageBox.Accepted += FailedMessageBox; messageBox.Cancelled += FailedMessageBox; ScreenManager.AddScreen(messageBox); System.Console.WriteLine("Failed to join 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); } } else { const string message = "No matches were found."; MessageBoxScreen messageBox = new MessageBoxScreen(message); messageBox.Accepted += FailedMessageBox; messageBox.Cancelled += FailedMessageBox; ScreenManager.AddScreen(messageBox); } } catch (GamerPrivilegeException gpe) { MessageBoxScreen messageBox = new MessageBoxScreen(gpe.Message); messageBox.Accepted += FailedMessageBox; messageBox.Cancelled += FailedMessageBox; ScreenManager.AddScreen(messageBox); } }
/// <summary> /// Event handler for when an available session menu entry is selected. /// </summary> void AvailableSessionMenuEntrySelected(object sender, PlayerIndexEventArgs e) { // Which menu entry was selected? AvailableSessionMenuEntry menuEntry = (AvailableSessionMenuEntry)sender; AvailableNetworkSession availableSession = menuEntry.AvailableSession; try { // Begin an asynchronous join network session operation. IAsyncResult asyncResult = NetworkSession.BeginJoin(availableSession, null, null); // Activate the network busy screen, which will display // an animation until this operation has completed. NetworkBusyScreen busyScreen = new NetworkBusyScreen(asyncResult); busyScreen.OperationCompleted += JoinSessionOperationCompleted; ScreenManager.AddScreen(busyScreen, ControllingPlayer); } catch (Exception exception) { NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception); ScreenManager.AddScreen(errorScreen, ControllingPlayer); } }
/* Call this internally when a connection to a given available session is desired. */ protected virtual void ConnectToSession(AvailableNetworkSession sess) { Trace.WriteLine("Connecting to session host: {0}", sess.HostGamertag); Debug.Assert(joinAsync_ == null); joinAsync_ = NetworkSession.BeginJoin(sess, OnSessionConnected, null); Debug.Assert(joinAsync_.CompletedSynchronously == false); }
/// <summary> /// Begins to asynchronously join the specified <see cref="AvailableNetworkSession"/>. /// </summary> /// <param name="toJoin">The session to join.</param> public void JoinSession(AvailableNetworkSession toJoin) { if (toJoin == null) { throw new ArgumentNullException("toJoin"); } if (_asyncSessionOperationsInProgress > 0) { throw new InvalidOperationException("An asynchronous session operation is already in progress."); } _asyncSessionOperationsInProgress++; NetworkSession.BeginJoin(toJoin, NetSessionJoin, null); }
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 called when the system delivers an invite notification. /// This can occur when the user accepts an invite that was sent to them by /// a friend (pull mode), or if they choose the "Join Session In Progress" /// option in their friends screen (push mode). The handler leaves the /// current session (if any), then joins the session referred to by the /// invite. It is not necessary to prompt the user before doing this, as /// the Guide will already have taken care of the necessary confirmations /// before the invite was delivered to you. /// </summary> public static void InviteAccepted(ScreenManager screenManager, InviteAcceptedEventArgs e) { // If we are already in a network session, leave it now. NetworkSessionComponent self = FindSessionComponent(screenManager.Game); if (self != null) { self.Dispose(); } try { // Which local profiles should we include in this session? IEnumerable <SignedInGamer> localGamers = ChooseGamers(NetworkSessionType.PlayerMatch, e.Gamer.PlayerIndex); // Begin an asynchronous join-from-invite operation. IAsyncResult asyncResult = NetworkSession.BeginJoin(null, null, localGamers); // null, null); // Use the loading screen to replace whatever screens were previously // active. This will completely reset the screen state, regardless of // whether we were in the menus or playing a game when the invite was // delivered. When the loading screen finishes, it will activate the // network busy screen, which displays an animation as it waits for // the join operation to complete. NetworkBusyScreen busyScreen = new NetworkBusyScreen(asyncResult); busyScreen.OperationCompleted += JoinInvitedOperationCompleted; LoadingScreen.Load(screenManager, false, null, busyScreen); } catch (Exception exception) { NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception); LoadingScreen.Load(screenManager, false, null, new MainMenuScreen(), errorScreen); } }
/// <summary> /// Responds to user menu selections. /// </summary> protected override void OnSelectEntry(int entryIndex) { if ((availableSessions != null) && (entryIndex >= 0) && (entryIndex < availableSessions.Count)) { // start to join try { IAsyncResult asyncResult = NetworkSession.BeginJoin( availableSessions[entryIndex], null, null); // create the busy screen NetworkBusyScreen busyScreen = new NetworkBusyScreen( "Joining the session...", asyncResult); busyScreen.OperationCompleted += LoadLobbyScreen; ScreenManager.AddScreen(busyScreen); } catch (NetworkException ne) { const string message = "Failed joining the session."; MessageBoxScreen messageBox = new MessageBoxScreen(message); messageBox.Accepted += FailedMessageBox; messageBox.Cancelled += FailedMessageBox; ScreenManager.AddScreen(messageBox); System.Console.WriteLine("Failed to join 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); } } }
/// <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; } } } }