From: Charles Date: Mon, 29 Mar 2010 16:26:21 +0000 (+0000) Subject: New delegate method for getting and handling errors during the async calls. Need... X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=87550d29aa4aa52e9c3e413e58877b494d7d7017;p=chaz%2Fcarfire New delegate method for getting and handling errors during the async calls. Need this because an exception will be thrown during CreateSession or JoinSession if the player isn't logged in. git-svn-id: https://bd85.net/svn/cs3505_group@37 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs index 3a7d875..78ee37b 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs @@ -165,6 +165,14 @@ namespace CS_3505_Project_06 } } + // TODO: New method. + void AsyncCallbackFailed(Exception exception, NetworkGame networkGame) + { + currentState = lobbyState.Welcome; + Console.WriteLine("Exception as thrown during async call: " + exception.Message); + } + + public long Update(GameTime gameTime, NetworkGame networkGame) { @@ -210,6 +218,7 @@ namespace CS_3505_Project_06 if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y)) { currentState = lobbyState.JoiningGame; + networkGame.ErrorDelegate = AsyncCallbackFailed; networkGame.CreateSession(JoinedSession); } break; @@ -220,6 +229,7 @@ namespace CS_3505_Project_06 currentState = lobbyState.Welcome; ready = false; } + networkGame.ErrorDelegate = AsyncCallbackFailed; networkGame.FindSessions(FoundSessions); currentState = lobbyState.FindingGames; break; diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs index 72a588c..792d16d 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs @@ -36,6 +36,21 @@ namespace CS_3505_Project_06 public delegate void FoundSessionsDelegate(AvailableNetworkSessionCollection sessions, NetworkGame networkGame); + /// + /// Called when an exception is thrown during an asynchronous operation. + /// + /// The exception that was thrown. + /// The NetworkGame that errored. + public delegate void CaughtErrorDelegate(Exception exception, NetworkGame networkGame); + + /// + /// Get and set the error delegate, called when an exception is thrown during + /// and asynchronous operation. This will occur if you try to create or join a + /// session without being logged into a profile. + /// + public CaughtErrorDelegate ErrorDelegate; + + /// /// Construct a NetworkGame with a lobby and a game. /// @@ -101,11 +116,20 @@ namespace CS_3505_Project_06 { Debug.Assert(mNetworkSession == null); - mNetworkSession = NetworkSession.EndCreate(result); - mNetworkSession.AllowHostMigration = true; - mNetworkSession.AllowJoinInProgress = false; - mNetworkSession.GameStarted += new EventHandler(mNetworkSession_GameStarted); + try + { + mNetworkSession = NetworkSession.EndCreate(result); + mNetworkSession.AllowHostMigration = true; + mNetworkSession.AllowJoinInProgress = false; + mNetworkSession.GameStarted += new EventHandler(mNetworkSession_GameStarted); + } + catch (Exception e) + { + if (ErrorDelegate != null) ErrorDelegate(e, this); + return; + } mJoinedSessionDelegate(mNetworkSession, this); + mJoinedSessionDelegate = null; } @@ -142,8 +166,18 @@ namespace CS_3505_Project_06 } private void FindSessionsEnd(IAsyncResult result) { - AvailableNetworkSessionCollection sessions = NetworkSession.EndFind(result); + AvailableNetworkSessionCollection sessions; + try + { + sessions = NetworkSession.EndFind(result); + } + catch (Exception e) + { + if (ErrorDelegate != null) ErrorDelegate(e, this); + return; + } mFoundSessionsDelegate(sessions, this); + mFoundSessionsDelegate = null; } /// @@ -163,12 +197,18 @@ namespace CS_3505_Project_06 { Debug.Assert(mNetworkSession == null); - mNetworkSession = NetworkSession.EndJoin(result); - + try + { + mNetworkSession = NetworkSession.EndJoin(result); + mNetworkSession.GameStarted += new EventHandler(mNetworkSession_GameStarted); + } + catch (Exception e) + { + if (ErrorDelegate != null) ErrorDelegate(e, this); + return; + } mJoinedSessionDelegate(mNetworkSession, this); mJoinedSessionDelegate = null; - - mNetworkSession.GameStarted += new EventHandler(mNetworkSession_GameStarted); }