28
Game Network Programming @CoreyClarkPhD @GameTheoryLabs

Introduction to Game Network Programming

Embed Size (px)

DESCRIPTION

An introduction to C++ network programming in video games for guest lecture at SMU Guildhall.

Citation preview

Page 1: Introduction to Game Network Programming

Game Network Programming

@CoreyClarkPhD @GameTheoryLabs

Page 2: Introduction to Game Network Programming

!

What I Did?

Page 3: Introduction to Game Network Programming

What I Do Now Crowd Sourced

Games

Distributed Computing MMOs

FoldIt Crowd Process

GTL

Page 4: Introduction to Game Network Programming

Determinism & Lock Step

Authoritative Server

Mid-Game Join

Floating point

Page 5: Introduction to Game Network Programming

Latency vs Lag

Page 6: Introduction to Game Network Programming

Fight Lag

!  What is the Cause?

!  Slower But Consistent Frame Rate

!  Animations

!  Client Side Prediction

!  Learning Algorithms !  Markov Models

!  Artificial Neural Networks

Page 7: Introduction to Game Network Programming

Client Side Prediction

Linear Interpolation

Jitter / Wobble

Page 8: Introduction to Game Network Programming

The Internets …

Page 9: Introduction to Game Network Programming

Peer To Peer (P2P)

Page 10: Introduction to Game Network Programming

Client / Server

Page 11: Introduction to Game Network Programming

Mesh

Page 12: Introduction to Game Network Programming

Network Stack

Page 13: Introduction to Game Network Programming

Sockets

!   Extends Network Layer (Host to Host)

!   End – to – End (App to App)

!   APIs: WinSock & Berkley (BSD)

Page 14: Introduction to Game Network Programming

TCP vs UDP

TCP

!   Connection Based

!   Delivery Guarantee

!   Order Guarantee

!   Flow Control

!   Duplicates Deleted

UDP

!   Connectionless

!   No Delivery Guarantee

!   No Order Guarantee

!   No Flow Control

!   Duplicates

Page 15: Introduction to Game Network Programming

TCP/IP Network

Host

App

Socket

TCP

IP

Channel Channel

Host

App

Socket

TCP

IP

Ethernet, WiFi, Bluetooth

Page 16: Introduction to Game Network Programming

Addresses

External Address

Internal Address

MAC Address

ISP Geo Referenced

Router

Hardware IEEE Regulated

Page 17: Introduction to Game Network Programming

Ports

!   Communication End Point

!   Port Forwarding !   Internal Static IP

!   Stay above 1024

!   Common Ports !   20: FTP

!   80: HTTP

!   443: HTTPS

!   Registered Ports !   7778: Unreal

!   26000: Quake

!   27010: Half-Life

!   27960: Quake III

Page 18: Introduction to Game Network Programming

Programming Sockets

!   Blocking

!   Non-Blocking

!   Overlapped I/O !   Event Handle

!   Completion Port

Page 19: Introduction to Game Network Programming

WinSock Procedure

Client

!   WinSock Init

!   Create Socket

!   Connect

!   Send/Recv

!   Close Socket

!   WinSock Clean Up

Server

!   WinSock Init

!   Create Socket

!   Bind

!   Listen

!   Accept (New Socket)

!   Send /Recv

!   Close Socket

!   WinSock Clean Up

Page 20: Introduction to Game Network Programming

TCP Procedure

Create Socket Bind Listen Accept

Create Socket Connect

Send Recv

Send Recv

Page 21: Introduction to Game Network Programming

Byte Order

Endianness

!   Big Endian !   MSB in Smallest Addr

!   Intel

!   Little Endian !   LSB in Smallest Addr

!   Motorola

Host vs Network

!   Host Byte Order (Intel (Big-Endian)

!   Network Byte Order (Big-Endian)

! htons, htonl

! ntohs, ntohl

SockAddr.sin_port = htons(nPort);

Page 22: Introduction to Game Network Programming

Startup & Cleanup

WSAStartup( MAKEWORD(2,2), &WSADATA)

WSACleanup()

Page 23: Introduction to Game Network Programming

Create Socket

socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

socket(AF_INET, SOCK_DGRAM,

IPPROTO_UDP);

Page 24: Introduction to Game Network Programming

Connect

connect(SOCKET tcp, const struct sockaddr* SockAddr,

sizeof(SockAddr))

Page 25: Introduction to Game Network Programming

Send

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

Page 26: Introduction to Game Network Programming

Demo

NodeJS Server Web Viewer

C++ TCP Client WebSocket Client

Page 27: Introduction to Game Network Programming

Help Me Out

gametheorylabs.com/smu Go To:

Pick Color

Set Color

Page 28: Introduction to Game Network Programming

Questions?