Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Control the objects on the screen and give your application some user interaction. Mouse input is never available on the Xbox360,and The Zune only supports its emulated controls—you won’t be plugging a mouse, a keyboard, or any other input devices into a Zune anytime soon.
  • Network

    1. 1. Tran Minh Triet – Nguyen Khac Huy Faculty of Information Technology University of Science, VNU-HCM
    2. 2. <ul><li>This slide is based on </li></ul><ul><li>Chapter 25 and 26 of </li></ul><ul><li>Microsoft® XNA™ Game Studio 3.0 Unleashed </li></ul><ul><li>by Chad Carter, SAMS (2009) </li></ul>
    3. 3. <ul><li>Client/Server </li></ul><ul><li>Peer to Peer </li></ul><ul><li>Hybrid </li></ul>
    4. 4. <ul><li>One machine (server) acts as the host, other machines (clients) connect to the host. </li></ul><ul><li>All the data is passed to the server. The server then sends the messages to the individual clients. </li></ul><ul><li>This scenario is common on Windows because cheating by modifying the packets is possible. The server can ignore any attempts at modifying the data because it knows exactly where all the players are and the status of each player. </li></ul><ul><li>Typically when trying to join a session, a particular machine needs to be the server so everyone who wants to join the game can find the game. Each client sends a ready command, and the host will start the game by sending a start command to each client. </li></ul>
    5. 5. <ul><li>The peer-to-peer approach broadcasts data from one peer to all the other peers. Or it can send a private message to one peer. </li></ul><ul><li>The data can travel quicker in this environment because it does not have to be re-sent from a central location. </li></ul><ul><li>Cheating: Each peer can modify the packet to cheat the game. </li></ul><ul><li>Xbox LIVE helps us with these concerns because the network traffic is secure. The data is encrypted, which makes it really difficult for anyone to decrypt a packet, modify it, encrypt it again, and then process it. </li></ul>
    6. 6. <ul><li>Most games actually use both approaches. </li></ul><ul><li>The client/server architecture is used for creating and joining a network session </li></ul><ul><li>Once the game play starts it moves into a peer-to-peer architecture, where each player is sending its data to the peers (or just the peers that are within range, on the team, and so on). </li></ul><ul><li>Programming multiplayer games is all about compromise. Writing a game that is both completely accurate and completely lag-free is not possible. It may make sense to be less accurate in order to have a more immediate response. </li></ul><ul><li>Using a single authority machine (client/server approach) to decide the answers for important problems in a game </li></ul>
    7. 7. <ul><li>NetworkSession .InviteAccepted += OnInviteAccepted ; </li></ul><ul><li>private void OnInviteAccepted </li></ul><ul><li>( object sender, InviteAcceptedEventArgs args) </li></ul><ul><li>{ </li></ul><ul><li>// Quit the current session </li></ul><ul><li>if ( session != null ) </li></ul><ul><li>{ </li></ul><ul><li>session. Dispose (); </li></ul><ul><li>session = null; </li></ul><ul><li>} </li></ul><ul><li>// Join the new session </li></ul><ul><li>session = NetworkSession . JoinInvited (maxLocalGamers); </li></ul><ul><li>} </li></ul>
    8. 8. <ul><li>private NetworkSession networkSession; </li></ul><ul><li>private PacketReader packetReader = new PacketReader (); </li></ul><ul><li>private PacketWriter packetWriter = new PacketWriter (); </li></ul><ul><li>const int maxGamers = 16; </li></ul><ul><li>const int maxLocalGamers = 4; </li></ul>
    9. 9. <ul><li>if (networkSession == null ) </li></ul><ul><li>{ </li></ul><ul><li>// If we are not in a network session, update the </li></ul><ul><li>// menu screen that will let us create or join one. </li></ul><ul><li>UpdateMenuScreen (); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>// If we are in a network session, update it. </li></ul><ul><li>UpdateNetworkSession (); </li></ul><ul><li>} </li></ul>
    10. 10. <ul><li>private void UpdateMenuScreen () </li></ul><ul><li>{ </li></ul><ul><li>if ( IsActive) </li></ul><ul><li>{ </li></ul><ul><li>if ( Gamer .SignedInGamers.Count == 0) </li></ul><ul><li>{ </li></ul><ul><li>// If there are no profiles signed in, we cannot proceed. </li></ul><ul><li>// Show the Guide so the user can sign in. </li></ul><ul><li>Guide . ShowSignIn (maxLocalGamers, false ); </li></ul><ul><li>} </li></ul><ul><li>. . . . . . </li></ul>
    11. 11. <ul><li>. . . . . . </li></ul><ul><li>else if ( input. WasPressed (0, InputHandler . ButtonType . A , </li></ul><ul><li>Keys. A)) </li></ul><ul><li>{ // Create a new session? </li></ul><ul><li>CreateSession (); </li></ul><ul><li>} </li></ul><ul><li>else if ( input. WasPressed (0, InputHandler . ButtonType . B , </li></ul><ul><li>Keys. B )) </li></ul><ul><li>{ // Join an existing session? </li></ul><ul><li>JoinSession (); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    12. 12. <ul><li>private void CreateSession () </li></ul><ul><li>{ </li></ul><ul><li>DrawMessage (&quot;Creating session...&quot;); </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>networkSession = NetworkSession . Create </li></ul><ul><li>( NetworkSessionType . SystemLink , </li></ul><ul><li>maxLocalGamers, maxGamers); </li></ul><ul><li>HookSessionEvents (); </li></ul><ul><li>} </li></ul><ul><li>. . . . . . </li></ul>
    13. 13. <ul><li>. . . . . . </li></ul><ul><li>catch ( Exception e) </li></ul><ul><li>{ </li></ul><ul><li>errorMessage = e.Message; </li></ul><ul><li>if ( networkSession != null ) </li></ul><ul><li>{ </li></ul><ul><li>net workSession. Dispose (); </li></ul><ul><li>networkSession = null; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    14. 14. <ul><li>private void DrawMessage ( string message) </li></ul><ul><li>{ </li></ul><ul><li>if (! BeginDraw ()) </li></ul><ul><li>return; </li></ul><ul><li>GraphicsDevice . Clear ( Color . CornflowerBlue ); </li></ul><ul><li>spriteBatch. Begin (); </li></ul><ul><li>spriteBatch. DrawString </li></ul><ul><li>(font, message, new Vector2 (6, 6), Color. Black ); </li></ul><ul><li>spriteBatch. DrawString </li></ul><ul><li>(font, message, new Vector2 (5, 5), Color. White ); </li></ul><ul><li>spriteBatch. End (); </li></ul><ul><li>EndDraw (); </li></ul><ul><li>} </li></ul>
    15. 15. <ul><li>private void HookSessionEvents () </li></ul><ul><li>{ </li></ul><ul><li>networkSession. GamerJoined += GamerJoinedEventHandler ; </li></ul><ul><li>networkSession. SessionEnded += SessionEndedEventHandler ; </li></ul><ul><li>} </li></ul>
    16. 16. <ul><li>private void GamerJoinedEventHandler </li></ul><ul><li>( object sender, GamerJoinedEventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>int gamerIndex = </li></ul><ul><li>networkSession.AllGamers. IndexOf (e.Gamer); </li></ul><ul><li>Texture2D gamerProfilePic = blankProfilePicture; </li></ul><ul><li>. . . . . . </li></ul>
    17. 17. <ul><li>. . . . . . </li></ul><ul><li>foreach ( SignedInGamer signedInGamer in </li></ul><ul><li>SignedInGamer .SignedInGamers) </li></ul><ul><li>{ if ( signedInGamer.Gamertag == e.Gamer.Gamertag && </li></ul><ul><li>signedInGamer.IsSignedInToLive) </li></ul><ul><li>{ </li></ul><ul><li>GamerProfile gp = e.Gamer. GetProfile (); </li></ul><ul><li>gamerProfilePic = gp.GamerPicture; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>e.Gamer.Tag = new GamerObject </li></ul><ul><li>(gamerIndex, gamerProfilePic, screenWidth, screenHeight); </li></ul><ul><li>} </li></ul>
    18. 18. <ul><li>private void SessionEndedEventHandler </li></ul><ul><li>( object sender, NetworkSessionEndedEventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>errorMessage = e.EndReason. ToString (); </li></ul><ul><li>networkSession. Dispose (); </li></ul><ul><li>networkSession = null; </li></ul><ul><li>} </li></ul>
    19. 19. <ul><li>private void JoinSession () </li></ul><ul><li>{ DrawMessage (“Joining session...”); </li></ul><ul><li>try </li></ul><ul><li>{ // Search for sessions. </li></ul><ul><li>using ( AvailableNetworkSessionCollection availableSessions </li></ul><ul><li>= NetworkSession . Find ( NetworkSessionType . SystemLink , </li></ul><ul><li>maxLocalGamers, null )) </li></ul><ul><li>{ </li></ul><ul><li>if ( availableSessions.Count == 0) </li></ul><ul><li>{ </li></ul><ul><li>errorMessage = “No network sessions found.”; </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul><ul><li>. . . . . . </li></ul>
    20. 20. <ul><li>. . . . . . </li></ul><ul><li>// Join the first session we found. </li></ul><ul><li>networkSession = </li></ul><ul><li>NetworkSession . Join (availableSessions[0]); </li></ul><ul><li>HookSessionEvents (); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>catch ( Exception e) </li></ul><ul><li>{ errorMessage = e.Message; </li></ul><ul><li>if ( networkSession != null) </li></ul><ul><li>{ networkSession. Dispose (); </li></ul><ul><li>networkSession = null; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    21. 21. <ul><li>private void UpdateNetworkSession () </li></ul><ul><li>{ </li></ul><ul><li>//Update our locally controlled player and </li></ul><ul><li>//send their latest position to everyone else </li></ul><ul><li>foreach ( LocalNetworkGamer gamer in </li></ul><ul><li>networkSession.LocalGamers) </li></ul><ul><li>{ </li></ul><ul><li>UpdateLocalGamer (gamer); </li></ul><ul><li>} </li></ul><ul><li>. . . . . . </li></ul>
    22. 22. <ul><li>. . . . . . </li></ul><ul><li>//Need to call Update on every frame </li></ul><ul><li>networkSession.Update (); </li></ul><ul><li>//Make sure the session has not ended </li></ul><ul><li>if ( networkSession == null) </li></ul><ul><li>return; </li></ul><ul><li>//Get packets that contain positions of remote players </li></ul><ul><li>foreach ( LocalNetworkGamer gamer in </li></ul><ul><li>networkSession.LocalGamers) </li></ul><ul><li>{ </li></ul><ul><li>ReadIncomingPackets (gamer); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    23. 23. <ul><li>private void UpdateLocalGamer ( LocalNetworkGamer gamer) </li></ul><ul><li>{ </li></ul><ul><li>// Look up what gamerObject is associated with </li></ul><ul><li>// this local player </li></ul><ul><li>GamerObject gamerObject = gamer.Tag as GamerObject ; </li></ul><ul><li>// Update the object </li></ul><ul><li>ReadInputs (gamerObject, gamer.SignedInGamer.PlayerIndex); </li></ul><ul><li>gamerObject. Update (); </li></ul><ul><li>//Write the player state into a network packet </li></ul><ul><li>packetWriter. Write (gamerObject.Position); </li></ul><ul><li>// Send the data to everyone in the session. </li></ul><ul><li>gamer. SendData (packetWriter, SendDataOptions . InOrder ); </li></ul><ul><li>} </li></ul>
    24. 24. <ul><li>private void ReadIncomingPackets ( LocalNetworkGamer gamer) </li></ul><ul><li>{ </li></ul><ul><li>//As long as incoming packets are available keep reading them </li></ul><ul><li>while ( gamer.IsDataAvailable) </li></ul><ul><li>{ </li></ul><ul><li>NetworkGamer sender; </li></ul><ul><li>//Read a single network packet </li></ul><ul><li>gamer. ReceiveData (packetReader, out sender); </li></ul><ul><li>//Ignore packets sent by local gamers </li></ul><ul><li>//since we already know their state </li></ul><ul><li>if ( sender.IsLocal) </li></ul><ul><li>continue; </li></ul><ul><li>. . . . . . </li></ul>
    25. 25. <ul><li>. . . . . . </li></ul><ul><li>//Look up the player associated with </li></ul><ul><li>// whoever sent this packet </li></ul><ul><li>GamerObject remoteGamerObject = </li></ul><ul><li>sender.Tag as GamerObject ; </li></ul><ul><li>//Read the state of this gamer object from the network </li></ul><ul><li>remoteGamerObject.Position = </li></ul><ul><li>packetReader. ReadVector2 (); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>