network

25
Tran Minh Triet – Nguyen Khac H Tran Minh Triet – Nguyen Khac H Faculty of Information Technology Faculty of Information Technology University of Science, VNU-HCM University of Science, VNU-HCM

Upload: boybuon205

Post on 01-Nov-2014

757 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Network

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

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

Client/Server

Peer to Peer

Hybrid

Page 4: Network

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

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

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

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

private NetworkSession networkSession;

private PacketReader packetReader = new PacketReader();

private PacketWriter packetWriter = new PacketWriter();

const int maxGamers = 16;

const int maxLocalGamers = 4;

Page 9: Network

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

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

. . . . . .

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

private void CreateSession()

{

DrawMessage("Creating session...");

try

{

networkSession = NetworkSession.Create

(NetworkSessionType.SystemLink,

maxLocalGamers, maxGamers);

HookSessionEvents();

}

. . . . . .

Page 13: Network

. . . . . .

catch (Exception e)

{

errorMessage = e.Message;

if (networkSession != null)

{

networkSession.Dispose();

networkSession = null;

}

}

}

Page 14: Network

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

private void HookSessionEvents()

{

networkSession.GamerJoined += GamerJoinedEventHandler;

networkSession.SessionEnded += SessionEndedEventHandler;

}

Page 16: Network

private void GamerJoinedEventHandler

(object sender, GamerJoinedEventArgs e)

{

int gamerIndex =

networkSession.AllGamers.IndexOf(e.Gamer);

Texture2D gamerProfilePic = blankProfilePicture;

. . . . . .

Page 17: Network

. . . . . .

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

private void SessionEndedEventHandler

(object sender, NetworkSessionEndedEventArgs e)

{

errorMessage = e.EndReason.ToString();

networkSession.Dispose();

networkSession = null;

}

Page 19: Network

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

. . . . . . // 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

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

. . . . . .

//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

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

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

. . . . . .

//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();

}

}