/// <summary> /// /// </summary> /// <returns></returns> public IAsyncResult CreateNetwork(Game game, NetworkSessionType sessionType, int maxLocalGamers, int maxGamers, int privateGamerSlots, NetworkSessionProperties sessionProperties, bool AllowHostMigration, bool AllowJoinInProgress) { SessionManager sessionManager = new SessionManager(game); IAsyncResult asyncResult = sessionManager.CreateSession(sessionType, maxLocalGamers, maxGamers, privateGamerSlots, sessionProperties); /* * if (networkHelper.NetworkGameSession != null) * { * if (AllowHostMigration == true) * networkHelper.NetworkGameSession.AllowHostMigration = true; * * if (AllowJoinInProgress == true) * networkHelper.NetworkGameSession.AllowJoinInProgress = true; * return asyncResult; * } * else * { * //throw new Exception("Session was not Created"); * return asyncResult; * * }*/ return(asyncResult); }
/// <summary> /// /// </summary> /// <returns></returns> public IAsyncResult JoinNetwork(Game game, NetworkSessionType sessionType, int maxLocalGamers, NetworkSessionProperties sessionProperties) { SessionManager sessionManager = new SessionManager(game); IAsyncResult asyncResult = sessionManager.JoinSession(sessionType, maxLocalGamers, sessionProperties); /*if (networkHelper.NetworkGameSession == null) * { * return 1; * } * return 0;*/ return(asyncResult); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } if (this.IsActive) { switch (currentGameState) { case GameState.SignIn: { if (Gamer.SignedInGamers.Count < 1) { Guide.ShowSignIn(1, false); log.Add("Opened User SignIn Interface"); } else { currentGameState = GameState.SearchSession; log.Add(Gamer.SignedInGamers[0].Gamertag + " logged in - proceed to SearchSession"); } } break; case GameState.SearchSession: { NetworkSessionProperties findProperties = new NetworkSessionProperties(); findProperties[0] = 3; findProperties[1] = 4096; AvailableNetworkSessionCollection activeSessions = NetworkSession.Find(NetworkSessionType.SystemLink, 4, findProperties); if (activeSessions.Count == 0) { currentGameState = GameState.CreateSession; log.Add("No active sessions found - proceed to CreateSession"); } else { AvailableNetworkSession sessionToJoin = activeSessions[0]; networkSession = NetworkSession.Join(sessionToJoin); string myString = "Joined session hosted by " + sessionToJoin.HostGamertag; myString += " with " + sessionToJoin.CurrentGamerCount.ToString() + " players"; myString += " and " + sessionToJoin.OpenPublicGamerSlots.ToString() + " open player slots."; log.Add(myString); HookSessionEvents(); currentGameState = GameState.InSession; } } break; case GameState.CreateSession: { NetworkSessionProperties createProperties = new NetworkSessionProperties(); createProperties[0] = 3; createProperties[1] = 4096; networkSession = NetworkSession.Create(NetworkSessionType.SystemLink, 4, 16, 0, createProperties); networkSession.AllowHostMigration = true; networkSession.AllowJoinInProgress = false; log.Add("New session created"); HookSessionEvents(); currentGameState = GameState.InSession; } break; case GameState.InSession: { networkSession.Update(); } break; } } base.Update(gameTime); }
/* The swiss army knife of starting session matching. This will allow you to pass in a * number of filter values (up to 6) as well as various parameters relating to what kind * of session you want. The convenience functions use this function under the hood. * type - whether you want to share high scores, or play game, or both * nType - system link, playermatch, etc (typically, always playermatch) * matchParams - an array of up to 6 integers, which will be matched by other players. * Can be used for "desired game type" etc. Can be null. * localPlayerCount - how many local players you want to have space for * totalPlayerCount - how many players total you want to have space for */ public virtual void SetSessionTypeDesired(SessionType type, NetworkSessionType nType, int[] matchParams, int localPlayerCount, int totalPlayerCount) { if (matchParams != null && matchParams.Length > 6) { throw new InvalidOperationException("Too many matchParams in SetSessionTypeDesired() (6 is max)"); } if (session != null) { if (type == SessionType.None) { DisposeSession(); } } else { if (type != SessionType.None) { netType = nType; NetworkSessionProperties props = new NetworkSessionProperties(); int nProps = 0; props[nProps] = (int)type; ++nProps; if (type != SessionType.HighscoresOnly) { if (matchParams != null) { foreach (int val in matchParams) { props[nProps] = val; ++nProps; } } } // minimize the risk that two people using the same sample project GUID find // the same session during development if (type != SessionType.GameOnly) { props[nProps] = StableHash(Game.GetType().Name); Trace.WriteLine(String.Format("hash code is: {0:x}", props[nProps])); ++nProps; } /* Provide some useful defaults. 0/0 means "I want one other player to play with." */ if (totalPlayerCount == 0) { if (localPlayerCount == 0) { totalPlayerCount = 1; } else { totalPlayerCount = (localPlayerCount == 1) ? 2 : localPlayerCount + 4; } } /* 0 means "all the current local players" */ if (localPlayerCount == 0) { for (int i = 0; i != 4; ++i) { GamePadState gps = GamePad.GetState((PlayerIndex)i); if (gps.IsConnected) { ++localPlayerCount; } } if (SignedInGamer.SignedInGamers.Count > localPlayerCount) { localPlayerCount = SignedInGamer.SignedInGamers.Count; } totalPlayerCount += localPlayerCount; } saveLocalPlayerCount = localPlayerCount; saveTotalPlayerCount = totalPlayerCount; saveMatchParams = matchParams; saveProps = props; if (SignedInGamer.SignedInGamers.Count > 0) { /* remember that I want a new kind of network session */ actOnType = true; } } } sessionType = type; }
public IAsyncResult JoinSession(NetworkSessionType sessionType, int maxLocalGamers, NetworkSessionProperties sessionProperties) { IAsyncResult asyncResult = Microsoft.Xna.Framework.Net.NetworkSession.BeginFind(sessionType, maxLocalGamers, sessionProperties, null, null); return(asyncResult); /* * // Search for sessions. * using (AvailableNetworkSessionCollection availableSessions = * Microsoft.Xna.Framework.Net.NetworkSession.Find(sessionType, maxLocalGamers, sessionProperties)) * { * if (availableSessions.Count == 0) * { * return; * } * * // Join the first session we found. * networkHelper.NetworkGameSession = Microsoft.Xna.Framework.Net.NetworkSession.Join(availableSessions[0]); * }*/ }