]> Dogcows Code - chaz/carfire/commitdiff
New delegate method for getting and handling errors during the async calls. Need...
authorCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 29 Mar 2010 16:26:21 +0000 (16:26 +0000)
committerCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 29 Mar 2010 16:26:21 +0000 (16:26 +0000)
git-svn-id: https://bd85.net/svn/cs3505_group@37 92bb83a3-7c8f-8a45-bc97-515c4e399668

Project06/CS 3505 Project 06/CS 3505 Project 06/LobbyGUI.cs
Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs

index 3a7d8753afa18bb05eecb31d917148ca1d59d1bd..78ee37b7a9912c9dad0ebf700bf7b83be60d9dd7 100644 (file)
@@ -165,6 +165,14 @@ namespace CS_3505_Project_06
             }\r
         }\r
 \r
+        // TODO: New method.\r
+        void AsyncCallbackFailed(Exception exception, NetworkGame networkGame)\r
+        {\r
+            currentState = lobbyState.Welcome;\r
+            Console.WriteLine("Exception as thrown during async call: " + exception.Message);\r
+        }\r
+\r
+\r
         public long Update(GameTime gameTime, NetworkGame networkGame)\r
         {\r
             \r
@@ -210,6 +218,7 @@ namespace CS_3505_Project_06
                     if (currentKeyboardState.IsKeyDown(Keys.Y) && previousKeyboardState.IsKeyUp(Keys.Y))\r
                     {\r
                         currentState = lobbyState.JoiningGame;\r
+                        networkGame.ErrorDelegate = AsyncCallbackFailed;\r
                         networkGame.CreateSession(JoinedSession);\r
                     }   \r
                     break;\r
@@ -220,6 +229,7 @@ namespace CS_3505_Project_06
                         currentState = lobbyState.Welcome;\r
                         ready = false;\r
                     }\r
+                    networkGame.ErrorDelegate = AsyncCallbackFailed;\r
                     networkGame.FindSessions(FoundSessions);\r
                     currentState = lobbyState.FindingGames;\r
                     break;\r
index 72a588cf62f6fcbab846b40794fd387dbf529a52..792d16d2063e8b2fe3f756bd216cc442ca7d819f 100644 (file)
@@ -36,6 +36,21 @@ namespace CS_3505_Project_06
         public delegate void FoundSessionsDelegate(AvailableNetworkSessionCollection sessions, NetworkGame networkGame);\r
 \r
 \r
+        /// <summary>\r
+        /// Called when an exception is thrown during an asynchronous operation.\r
+        /// </summary>\r
+        /// <param name="exception">The exception that was thrown.</param>\r
+        /// <param name="networkGame">The NetworkGame that errored.</param>\r
+        public delegate void CaughtErrorDelegate(Exception exception, NetworkGame networkGame);\r
+\r
+        /// <summary>\r
+        /// Get and set the error delegate, called when an exception is thrown during\r
+        /// and asynchronous operation.  This will occur if you try to create or join a\r
+        /// session without being logged into a profile.\r
+        /// </summary>\r
+        public CaughtErrorDelegate ErrorDelegate;\r
+\r
+\r
         /// <summary>\r
         /// Construct a NetworkGame with a lobby and a game.\r
         /// </summary>\r
@@ -101,11 +116,20 @@ namespace CS_3505_Project_06
         {\r
             Debug.Assert(mNetworkSession == null);\r
 \r
-            mNetworkSession = NetworkSession.EndCreate(result);\r
-            mNetworkSession.AllowHostMigration = true;\r
-            mNetworkSession.AllowJoinInProgress = false;\r
-            mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(mNetworkSession_GameStarted);\r
+            try\r
+            {\r
+                mNetworkSession = NetworkSession.EndCreate(result);\r
+                mNetworkSession.AllowHostMigration = true;\r
+                mNetworkSession.AllowJoinInProgress = false;\r
+                mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(mNetworkSession_GameStarted);\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                if (ErrorDelegate != null) ErrorDelegate(e, this);\r
+                return;\r
+            }\r
             mJoinedSessionDelegate(mNetworkSession, this);\r
+            mJoinedSessionDelegate = null;\r
         }\r
 \r
 \r
@@ -142,8 +166,18 @@ namespace CS_3505_Project_06
         }\r
         private void FindSessionsEnd(IAsyncResult result)\r
         {\r
-            AvailableNetworkSessionCollection sessions = NetworkSession.EndFind(result);\r
+            AvailableNetworkSessionCollection sessions;\r
+            try\r
+            {\r
+                sessions = NetworkSession.EndFind(result);\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                if (ErrorDelegate != null) ErrorDelegate(e, this);\r
+                return;\r
+            }\r
             mFoundSessionsDelegate(sessions, this);\r
+            mFoundSessionsDelegate = null;\r
         }\r
 \r
         /// <summary>\r
@@ -163,12 +197,18 @@ namespace CS_3505_Project_06
         {\r
             Debug.Assert(mNetworkSession == null);\r
 \r
-            mNetworkSession = NetworkSession.EndJoin(result);\r
-\r
+            try\r
+            {\r
+                mNetworkSession = NetworkSession.EndJoin(result);\r
+                mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(mNetworkSession_GameStarted);\r
+            }\r
+            catch (Exception e)\r
+            {\r
+                if (ErrorDelegate != null) ErrorDelegate(e, this);\r
+                return;\r
+            }\r
             mJoinedSessionDelegate(mNetworkSession, this);\r
             mJoinedSessionDelegate = null;\r
-\r
-            mNetworkSession.GameStarted += new EventHandler<GameStartedEventArgs>(mNetworkSession_GameStarted);\r
         }\r
 \r
 \r
This page took 0.026434 seconds and 4 git commands to generate.