network
DESCRIPTION
TRANSCRIPT
![Page 1: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/1.jpg)
Tran Minh Triet – Nguyen Khac HuyTran Minh Triet – Nguyen Khac HuyFaculty of Information TechnologyFaculty of Information TechnologyUniversity of Science, VNU-HCMUniversity of Science, VNU-HCM
![Page 2: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/2.jpg)
This slide is based on This slide is based on
Chapter 25 and 26 of Chapter 25 and 26 of
Microsoft® XNA™ Game Studio 3.0 UnleashedMicrosoft® XNA™ Game Studio 3.0 Unleashed
by Chad Carter, SAMS (2009)by Chad Carter, SAMS (2009)
![Page 3: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/3.jpg)
Client/Server
Peer to Peer
Hybrid
![Page 4: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/4.jpg)
One machine (server) acts as the host, other machines (clients) connect to the host.
All the data is passed to the server. The server then sends the messages to the individual clients.
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.
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.
![Page 5: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/5.jpg)
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. The data can travel quicker in this environment because it does not have to be re-sent from a central location. Cheating: Each peer can modify the packet to cheat the game. 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.
![Page 6: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/6.jpg)
Most games actually use both approaches.
The client/server architecture is used for creating and joining a network session
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).
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.
Using a single authority machine (client/server approach) to decide the answers for important problems in a game
![Page 7: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/7.jpg)
NetworkSession.InviteAccepted += OnInviteAccepted;
private void OnInviteAccepted
(object sender, InviteAcceptedEventArgs args)
{
// Quit the current session
if (session != null)
{
session.Dispose();
session = null;
}
// Join the new session
session = NetworkSession.JoinInvited(maxLocalGamers);
}
![Page 8: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/8.jpg)
private NetworkSession networkSession;
private PacketReader packetReader = new PacketReader();
private PacketWriter packetWriter = new PacketWriter();
const int maxGamers = 16;
const int maxLocalGamers = 4;
![Page 9: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/9.jpg)
if (networkSession == null)
{
// If we are not in a network session, update the
// menu screen that will let us create or join one.
UpdateMenuScreen();
}
else
{
// If we are in a network session, update it.
UpdateNetworkSession();
}
![Page 10: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/10.jpg)
private void UpdateMenuScreen()
{
if (IsActive)
{
if (Gamer.SignedInGamers.Count == 0)
{
// If there are no profiles signed in, we cannot proceed.
// Show the Guide so the user can sign in.
Guide.ShowSignIn(maxLocalGamers, false);
}
. . . . . .
![Page 11: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/11.jpg)
. . . . . .
else if (input.WasPressed(0, InputHandler.ButtonType.A,
Keys.A))
{ // Create a new session?
CreateSession();
}
else if (input.WasPressed(0, InputHandler.ButtonType.B,
Keys.B))
{ // Join an existing session?
JoinSession();
}
}
}
![Page 12: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/12.jpg)
private void CreateSession()
{
DrawMessage("Creating session...");
try
{
networkSession = NetworkSession.Create
(NetworkSessionType.SystemLink,
maxLocalGamers, maxGamers);
HookSessionEvents();
}
. . . . . .
![Page 13: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/13.jpg)
. . . . . .
catch (Exception e)
{
errorMessage = e.Message;
if (networkSession != null)
{
networkSession.Dispose();
networkSession = null;
}
}
}
![Page 14: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/14.jpg)
private void DrawMessage(string message)
{
if (!BeginDraw())
return;
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString
(font, message, new Vector2(6, 6), Color.Black);
spriteBatch.DrawString
(font, message, new Vector2(5, 5), Color.White);
spriteBatch.End();
EndDraw();
}
![Page 15: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/15.jpg)
private void HookSessionEvents()
{
networkSession.GamerJoined += GamerJoinedEventHandler;
networkSession.SessionEnded += SessionEndedEventHandler;
}
![Page 16: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/16.jpg)
private void GamerJoinedEventHandler
(object sender, GamerJoinedEventArgs e)
{
int gamerIndex =
networkSession.AllGamers.IndexOf(e.Gamer);
Texture2D gamerProfilePic = blankProfilePicture;
. . . . . .
![Page 17: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/17.jpg)
. . . . . .
foreach (SignedInGamer signedInGamer in
SignedInGamer.SignedInGamers)
{ if (signedInGamer.Gamertag == e.Gamer.Gamertag &&
signedInGamer.IsSignedInToLive)
{
GamerProfile gp = e.Gamer.GetProfile();
gamerProfilePic = gp.GamerPicture;
}
}
e.Gamer.Tag = new GamerObject
(gamerIndex, gamerProfilePic, screenWidth, screenHeight);
}
![Page 18: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/18.jpg)
private void SessionEndedEventHandler
(object sender, NetworkSessionEndedEventArgs e)
{
errorMessage = e.EndReason.ToString();
networkSession.Dispose();
networkSession = null;
}
![Page 19: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/19.jpg)
private void JoinSession()
{ DrawMessage(“Joining session...”);
try
{ // Search for sessions.
using (AvailableNetworkSessionCollection availableSessions
= NetworkSession.Find(NetworkSessionType.SystemLink,
maxLocalGamers, null))
{
if (availableSessions.Count == 0)
{
errorMessage = “No network sessions found.”;
return;
}
. . . . . .
![Page 20: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/20.jpg)
. . . . . . // Join the first session we found. networkSession = NetworkSession.Join(availableSessions[0]); HookSessionEvents(); } } catch (Exception e) { errorMessage = e.Message; if (networkSession != null) { networkSession.Dispose(); networkSession = null; } }}
![Page 21: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/21.jpg)
private void UpdateNetworkSession()
{
//Update our locally controlled player and
//send their latest position to everyone else
foreach (LocalNetworkGamer gamer in
networkSession.LocalGamers)
{
UpdateLocalGamer(gamer);
}
. . . . . .
![Page 22: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/22.jpg)
. . . . . .
//Need to call Update on every frame
networkSession.Update();
//Make sure the session has not ended
if (networkSession == null)
return;
//Get packets that contain positions of remote players
foreach (LocalNetworkGamer gamer in
networkSession.LocalGamers)
{
ReadIncomingPackets(gamer);
}
}
![Page 23: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/23.jpg)
private void UpdateLocalGamer(LocalNetworkGamer gamer)
{
// Look up what gamerObject is associated with
// this local player
GamerObject gamerObject = gamer.Tag as GamerObject;
// Update the object
ReadInputs(gamerObject, gamer.SignedInGamer.PlayerIndex);
gamerObject.Update();
//Write the player state into a network packet
packetWriter.Write(gamerObject.Position);
// Send the data to everyone in the session.
gamer.SendData(packetWriter, SendDataOptions.InOrder);
}
![Page 24: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/24.jpg)
private void ReadIncomingPackets(LocalNetworkGamer gamer)
{
//As long as incoming packets are available keep reading them
while (gamer.IsDataAvailable)
{
NetworkGamer sender;
//Read a single network packet
gamer.ReceiveData(packetReader, out sender);
//Ignore packets sent by local gamers
//since we already know their state
if (sender.IsLocal)
continue;
. . . . . .
![Page 25: Network](https://reader036.vdocuments.us/reader036/viewer/2022070301/5453f4d3af7959a4058b907f/html5/thumbnails/25.jpg)
. . . . . .
//Look up the player associated with
// whoever sent this packet
GamerObject remoteGamerObject =
sender.Tag as GamerObject;
//Read the state of this gamer object from the network
remoteGamerObject.Position =
packetReader.ReadVector2();
}
}