X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=Project06%2FCS%203505%20Project%2006%2FCS%203505%20Project%2006%2FNetworkGame.cs;h=792d16d2063e8b2fe3f756bd216cc442ca7d819f;hb=87550d29aa4aa52e9c3e413e58877b494d7d7017;hp=2386ef60717983e77271ee1aa4a81a16683769c9;hpb=9187849a37e447f2440ef8257e15e8652f0c51e8;p=chaz%2Fcarfire
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 2386ef6..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,27 @@ namespace CS_3505_Project_06
{
Debug.Assert(mNetworkSession == null);
- mNetworkSession = NetworkSession.EndCreate(result);
- mNetworkSession.AllowHostMigration = true;
- mNetworkSession.AllowJoinInProgress = false;
-
+ 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;
+ }
+
+
+ //gamestarted event
+ void mNetworkSession_GameStarted(object sender, GameStartedEventArgs e)
+ {
+ Reset();
}
///
@@ -135,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;
}
///
@@ -156,8 +197,16 @@ 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;
}
@@ -232,25 +281,15 @@ namespace CS_3505_Project_06
if (mNetworkSession.SessionState == NetworkSessionState.Lobby)
{
mLobby.Update(gameTime, this);
-
- //if state started as lobby and was changed to playing, call reset.
- if (mNetworkSession.SessionState == NetworkSessionState.Playing)
- Reset();
}
else if (mNetworkSession.SessionState == NetworkSessionState.Playing)
{
- //TODO reset needs to be called for all players, currently LocalGamerInfo throughs exception for all nonhosts
- // because in start game reset is only called on hosts games thus other clients never get an updated list
- // gamers.
-
-
-//thoughs exeption see TODO above
- //if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo))
- //{
- // // TODO: Should support moving back to the session lobby.
- // LeaveSession();
- // return;
- //}
+ if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo))
+ {
+ // TODO: Should support moving back to the session lobby.
+ LeaveSession();
+ return;
+ }
if (HaveNeededEvents)
{