CMPSEM027-Lecture2-NetworkIssues and Design

cavalcadejewelSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

71 views

1


CMPSEM027


Online Gaming

Abdennour El Rhalibi

2

Content


Lecture 1
: Aspects of Network Games, TCP/IP
Fundamentals, Internet Services and Application Layer
Protocol, DirectPlay Overview,
Online Games Issues


Lecture 2: Network Game Design Fundamentals


Lecture 3
: DirectPlay (1)


Lecture 4
: DirectPlay (2)


Lecture 5
: Internet
-
based Database Systems


Lecture 6
: Concurrent/Multi
-
Threading Programming in
Client/Server Software


Lecture 7
: Socket Programming (1)


Lecture 8
: Socket Programming (2)


Lecture 9
: Massively Multiplayer Games Design and
Issues and Solutions Implementation


Lecture 10 & 11
: The Fundamentals and Issues of
Mobile Games & Mobile Game Development


Lecture 12
: Advanced Topics about Online Gaming

3




Network Issues in Online
Games

4

Networking for Games


There are four primary concerns in building networks
for games:


Latency
: How long does it take for state to be
transmitted


Reliability
: How often is data lost or corrupted


Bandwidth
: How much data can be transmitted in
a given time


Security
: How is the game
-
play protected from
tampering


All of these considerations interact, and trade
-
offs
must be made

5

Latency in Games


Latency is the time between when the user acts and
when they see the result


Latency is arguably the most important aspect of a
game network


Too much latency makes the game
-
play harder to
understand because the player cannot associate cause and
effect


It makes it harder to target objects (the lead becomes too
large)


Latency is
not

the same as bandwidth


A freeway has higher bandwidth than a country road, but the
speed limit, and hence the latency, can be the same


Excess bandwidth can reduce the
variance

in latency, but
cannot reduce the minimum latency

6

Sources of Latency


Consider a client sending and receiving data from a
server


There are four sources of latency in a game network


Frame rate latency:

Data only goes out on or comes in from
the network layer once per frame, and user interaction is
only sampled once per frame


Network protocol latency
: It takes time for the operating
system to put data onto the physical network, and time to get
it off a physical network and to an application


Transmission latency:

It takes time for data to be transmitted
to the receiver


Processing latency:

The time taken for the server (or client)
to compute a response to the input


You cannot make any of these sources go away


You don’t even have control over some of them


7

Reducing Latency


Frame rate latency:


Increase the frame rate (faster graphics, faster AI, faster physics)


Network protocol latency:


Switch to a protocol with lower latency


But may have impact on reliability and security


Transmission latency:


Upgrade your physical network (from dial
-
up to DSL, for instance)


Processing latency:


Make your server faster


The fact is, networking researchers and practitioners are
almost never concerned with latency


Most applications can handle higher latency (who else cares about
latency?)


8

Working With Latency


If you can’t get rid of latency, you can try to hide it


Any technique will introduce errors in some form
-

you
simply cannot provide immediate, accurate information


Option 1: Sacrifice accurate information, and show
approximate positions


Ignore the lag and show a given player “old” information about the
other players


Try to improve upon this by guessing where the other players are.
But if your guess is wrong, incorrect information is shown


Option 2: Sacrifice game
-
play:


Deliberately introduce lag into the local player’s experience, so that
you have enough time to deal with the network

9

Dead Reckoning


Dead reckoning

uses prediction to move objects about
even when their positions are not precisely known,
reducing the appearance of lag


Each client maintains precise state for some objects (the local
player, for instance)


Each client receives periodic updates of the position of
everyone else, along with velocity information, and maybe
acceleration


On each frame, the non
-
local objects are updated by
extrapolating their most recent position using the given velocity


There are other variants with different extrapolating schemes,
particularly if you know more about how an object moves


With a client
-
server model, each player runs their own
version of the game, while the server maintains
absolute authority


10

Fixing Extrapolation Errors


What do you do when using dead reckoning,
and a new position arrives for another player?


The position that just came in will not agree with the
place you have the object, due to extrapolation
errors


Two options:


Jump to the correct position


Interpolate the two positions over some period


Path followed will never be exact, but will match
reasonably well

Your Guess

Actual

Extrapolations from new data

New Data

Target Path

11

Network Reliability


Some protocols attempt to ensure that every packet is
delivered (TCP)


It costs, in latency and bandwidth, to ensure delivery



Others try less hard to ensure delivery, and will not tell
you if packets get lost (UDP)


Latency and bandwidth requirements are lower for such protocols



Other aspects of reliability are error checking (do the right
bits arrive) and order consistency (do things arrive in the
same order they were sent)


In a game, does everything need to be completely
reliable?


Are all aspects of reliability equally important?

12

Reliability Requirements


Some information must be communicated:


Discrete changes in game state
-

if they go missing, there is
no chance to recapture them


Information about payments, joining, dropping, etc…


Some information does not need to be reliably
communicated:


Information that rapidly becomes out of date, and will hence
be sent frequently


All aspects of reliability are less important in such cases


For example, player position information, maybe even
weapon firing information


The good part is, the data that goes out of date quickly
is also sent more often, so you get big payoffs for
reducing the cost of sending it

13

Reducing Bandwidth
Demands


Bandwidth is plentiful on the internet today, so it
only becomes an issue with large environments


Even “slow” modems have more impact through high latency
than low bandwidth (due to compression, error checking and
analogue/digital conversion)


Regardless, smaller packets reduce both
bandwidth
and

latency


Latency is measured from the time the first bit leaves to the time
the last bit arrives
-

so fewer bits have lower latency


There are two primary ways to reduce bandwidth
demands:


Dead reckoning allows you to send state less frequently


Area of interest management

avoids sending irrelevant data

14

Area of Interest Management


Area of interest management

is the networking
equivalent of visibility
-

only send data to the people
who need it


There is a catch, however: In a network you may not
know where everyone is, so you don’t know what
they can see


Hence, area
-
of
-
interest schemes are typically
employed in client
-
server environments:


The server has complete information


It decides who needs to receive what information, and only
sends information to those who need it


Two approaches:
grid methods

and
aura methods


Sound familiar? (replace aura with bounding box)

15

Grid and Aura Methods


Grid methods

break the world into a grid


Associate information with cells


Associate players with cells


Only send information to players in the same, or neighboring,
cells


This has all the same issues as grid based visibility and collision
detection


Aura methods

associate an aura with each piece of
information


Only send information to players that intersect the aura


Just like broad
-
phase collision detection with bounding volumes


Players need to find out all the information about a
space when they enter it, regardless how long ago that
information last changed


Hence, the important thing is
changes

in association, not the
moment to picture

16

Conclusion


TCP provides reliable and connection
-
oriented data
transmission service.


IP use connectionless and packet
-
switching method to
route the packet from the source to destination with best
effort delivery.


IP address used to identify which computer in the
Internet.


UDP is an alternative protocol which is used for fast and
uni
-
directional data transmission.


There are four primary concerns in building networks for
games:


Latency


Reliability


Bandwidth


Security

17

Network Game Design
Fundamentals

18

Introduction


Points to note for the design of a multiplayer
game/game platform


Platform (UNIX
-

BSD Socket or Windows


Winsock
or Java Socket)


Programming language (C, C++, VB or Java)


Mode of connection (Client/Server or Peer to Peer)


Storage/Database (Internet based or proprietary
network based or text file)


Network performance (Bandwidth and QoS, LAN or
WAN)


Mode of I/O Operations (Blocking or non
-
blocking)


Data Formatting (encryption and compression)


Scalability




19

Platform (1)


Sockets have more than one address data
type. This because different protocols
require unique information.


However, we can still write protocol
-
independent code by using generic
address data type.


As we need to write our code for multiple
platforms, we must be aware of data types
and rename the required data types to
match the other platform’s types.

20

Platform (2)


E.g.






In the above table which shows two data
types that have different names on UNIX and
Win32, but are really the
same thing
.

UNIX

Win32

Address length data type

socklen_t

int

Socket descriptor data type

int

SOCKET

21

Platform (3)


Both UNIX and Win32 are using similar
sockets functions (API):


SOCKET
socket
(int family, int type, int
protocol);


int
bind
(SOCKET s, const struct sockaddr *
addr,

socklen_t addrlen);


int
connect
(SOCKET s, const struct sockaddr
*addr,

socklen_t addrlen);


int
listen
(SOCKET s, int backlog);


int
accept
(SOCKET s, struct sockaddr *addr,

socklen_t addrlen);


int
close
(SOCKET s);


22

Platform (4)


Both UNIX and Win32 are using similar
Input/Output functions (API):


int
send
(SOCKET s, const void *buf, size_t len,
int flags);


int
recv
(SOCKET s, void *buf, size_t len, int
flags);


int
sendto
(SOCKET s, const void *buf, size_t
len,

int flags, const struct sockaddr *to,
socklen_t

addrlen);


int
recvfrom
(SOCKET s, void *buf, size_t len,
int

flags, struct sockaddr *from, socklen_t
*addrlen);

23

Platform (5)


try {


server_socket = new
ServerSocket(port)
;


System.out.println("Server waiting for client on port " +


server_socket.getLocalPort());





// server infinite loop


while(true) {


Socket socket = server_socket.
accept()
;


System.out.println("New connection accepted " +


socket.getInetAddress() + ":" + socket.getPort());





input = new BufferedReader(new InputStreamReader(socket.getInputStream()));


// print received data


try {



while(true) {



String message = input.
readLine()
;



if (message==null) break;



System.out.
println
(message);



}


}


// connection closed by client


try {



socket.
close()
;



System.out.println("Connection closed by client");


}


}

}





24

Programming language


For the server side programming, interface
design is not the critical part, the most important
is code effectiveness.


Compared to the “overall” performance,
C/C++
faster than Java
, and Java faster than VB.


C++ tends to be slightly slower then C and about
15% larger then C but the code is object
-
oriented.


Please refer to:


http://groups.msn.com/CCVisualCProgram/cvsjavaperformance.msnw


http://www.sosnoski.com/Java/Compare.html


25

Mode of connection (1)


Client/Server



In these games, the players connect to one
server where they download the game world
from and upload update to.


As long as the server is owned by a trusted
party and isn't hacked, it is very hard for
players to cheat.


26

Mode of connection (2)


Servers can be very
passive applications
. There can be
times when they do not do anything at all. These can be
happen if there are no clients to be served.


However, please remind that even though there is no
any client connections, it can
easily drain all the CPU
time just by running an “empty loop”.


If the server can only support one client at a time, it is
called an
iterative server
.


Computer games’ servers simply cannot work this way,
because it is against the principal idea of these servers.


The idea is that
multiple players connect to a server and
then communicate via that server to play the game
.


Servers that process multiple clients at once called
concurrent servers
.

27

Mode of connection (3)


Clients are
active applications
. They initiate the
connection to the server, so they never wait for
things to happen.


The client application is the game itself. The
player should not know anything else about the
server , except the server IP address and port
number.


When working in a LAN, we can
create a server
search system
, which is one way to remove the
need for manually setting the address
information.

28

Mode of connection (4)


Peer to Peer


A multiplayer network game where all
computers store a copy of the game world.


These computers in a peer to peer game can
prevent cheating if they detect that one
computers game world is very different from
the other computers' game worlds.


29

Mode of connection (5)


No one is the client or server
, both are same level
which can get or send information to each other.


For a small group of players (i.e. less than 5), the
communication is fast. However, when the number
of player is increased, the total no. of
communication increased tremendously.


Total no. of communication link
(C) = (N * (N
-
1))/2

:


N=2, C=1


N=3, C=3




:


N=6, C=15




:


N=15, C=105

30

Storage/Database (1)


Internet based database system


Since the most common communication
protocol between the game server and the
database server is TCP/IP. Therefore, a
TCP/IP friendly database server is
recommended
.


For example, the following database servers
can support Internet connection/retrieving:


MS SQL server (using port 1433)


MySQL (using port 3306)


Oracle (using port 1521)

31

Storage/Database (2)


Proprietary network based database or
text file


In fact, we can developed the game server
and the database server in the same
machine, however, it will decreased the
performance and scalability in a long run.


Using plain text file for data storing is a
simpler way compare to the steps in table
design and table normalization, however, a
flat file
doesn’t contain any indexing and fast
SQL operation
.

32

Network performance (1)


LAN


Bandwidth is high (i.e. 1Gbps and 100Mbps)


Time delay is less (compared to WAN)


Can use
broadcast

within a LAN environment,
one server can use broadcast to
communication with multiple machines (up to
hundred)


One very good use for broadcasting is to
search for available servers on LAN.

33

Network performance (2)


WAN


Bandwidth is low (i.e. 56Kbps (NB)


1.5Mbps


10Mbps (BB))


Time delay is significant


Cannot use broadcast within a WAN

environment,
one server connection can only communication with
one machine. Therefore, a concurrent server is
necessary.


QoS is necessary to maintain the baseline of
bandwidth requirement
, otherwise a lot of data
loss/delay occur in the game


RSVP can be employed in the lower level of network
implementation

34

Mode of I/O Operations (1)


Blocking I/O (While
-
true loop)


The simplest form of I/O strategies.


For example, if we try to read data off a
blocking socket, the read function we use will
not return before the data is read. If there is
no one sending anything to that socket, the
function will block until someone sends data
to it. Each socket is blocking by default.

35

Mode of I/O Operations (2)


Non
-
blocking I/O (Polling)


We can set a socket non
-
blocking if we want
to. Then the socket functions will return even
if the action cannot be fulfilled immediately.


Usually, this means that we need to loop the
function to create our own blocking effect.
This is called
polling
.


If we were not polling the incoming data, we
would most likely miss it because the data
must have reached the local host before we
call the function to read the data.

36

Mode of I/O Operations (3)


Signal
-
Driven I/O (Event
-
Driven I/O)


We do not always have to use the data
reading function to monitor the socket for
incoming data.


We can setup our operating system to
signal
us when incoming data is available for us to
read
. Then all we have to do is call the
function to read the data from the socket.


The obvious advantages in this strategy
compared to the previous ones is that we can
do other things while we wait for the data to
come in.

37

Mode of I/O Operations (4)


Multiplexing I/O


Another way to avoid using the actual data reading
function to tell if there is incoming data is to use the
select()

function.


It is used similarly to a blocking socket
read()

function, because the
select()

function will stop
and wait for the data to come in.


Multiplexing means that
we have multiple sources,
but we use only one at a time
. We need to be able to
choose the source that has something to offer us.


We use the
select()

function for that. This is the
advantage of multiplexing I/O; we can wait for more
than one socket to have data to read with one
function call.

38

Data formatting (1)


Encryption


Increase the security


Increase the CPU overhead


Most likely, also increase the bandwidth
consumption


Therefore, depends on what kind of game that
the server support.

39

Data formatting (2)


Compression


Increase the CPU overhead


Decrease the bandwidth consumption


Therefore, if the bandwidth is low and the
server or client is fast enough to compensate
the compression time. It is worth to do
compression


Compression should be a option for the
player.

40

Scalability


The game/game server design should
cater for future expansion.


Standard communication protocols between
the game and game server (or two peers)


Specify a port for inter
-
game server
communication


Define an inter
-
game server communication
protocol


Reserve some space/fields in the protocol for
the future use


Should be backward compatible with the
obsolete games

41

What else?


Login/profile management


Game management


Game room management


Connection management


Players management


Graphics/characters management


Game master for each game/game room


……