Game Server Development in node.js Charlie Circle @ @xiecc.

51
Game Server Development in node.js Charlie Circle @ 圈圈圈圈圈 @xiecc

Transcript of Game Server Development in node.js Charlie Circle @ @xiecc.

Page 1: Game Server Development in node.js Charlie Circle @ @xiecc.

Game Server Development in node.js

Charlie Circle@圈圈套圈圈@xiecc

Page 2: Game Server Development in node.js Charlie Circle @ @xiecc.

CategoryOverviewFrameworkPracticePerformance

Page 3: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview---node.js and game server

Game ServerFast Scalable Network Real-time

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Page 4: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview --- advantagesScalability --- event driven I/O

Game, high density network communication

Language, javascriptHTML5 – same language in client and serverReally quick iteration

Multi-Process, Single threadGoogle grits, mozilla browser quest, single

process multi process VS multi thread

Page 5: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview -- disadvantageSome CPU sensitive actions

Path findingAI

SolutionOptimizationDivide processAll can be solved in practice

Page 6: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview -- our demo

Page 7: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview---architecture of demo

Page 8: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview --- game VS webLong connection VS Short

connectionPartition: area based VS Load

balanced clusterState VS StatelessBroadcast VS Request/responseRequest&tick driven VS Request

driven

Page 9: Game Server Development in node.js Charlie Circle @ @xiecc.

Overview --- how to solve complexity

Too … complicated?

solution: framework

Page 10: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- web VS game

Web

Tomcat/JettyStruts/Springruby on railsdjangoexpress

Game

ReddwarfSmartfoxServerBigworld

Page 11: Game Server Development in node.js Charlie Circle @ @xiecc.

CategoryOverviewFrameworkPracticePerformance

Page 12: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework ---

framework

data-sync

seq-queue

schedule

ai robot

pathfind

aoi

Library

. . .

Admin console

tools

Realtime, multi-server app framework

Page 13: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- design goalAbstract of servers(processes)

Auto extend server typesAuto extend servers

Abstract of request , response and broadcastZero config request Simple broadcast apiOther mechanisms: filter, session

Servers communication---rpc framework

Page 14: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- server abstraction

frontend

frontend

backend

backend

backend

backend

forward message

push message

by channelrpc

master

Page 15: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- server abstraction

Frontend( connector)

Client connectionMaintain session

informationDispatch request to

backendPush message to

client

BackendHandle request from

frontendPush messages to

frontend, through channel or response

Rpc service

Page 16: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework--- server abstraction

Duck type

frontend

connector

backend

area cha

t

status

Page 17: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework---server abstractionConvention Over Configuration

Classify servers by foldershandler: for client request remote: for rpcAll server code in one projectDeveloper job: fill the handler

and remote

server type

Page 18: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework---server abstraction

Page 19: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- request abstractionZero configClient, like ajax

Server, like web mvc framework

Page 20: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- rpc framework Zero configAuto route

app.rpc.chat.chatRemote.kick

Page 21: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- channel&broadcastPush messages to a group of users

channelService.pushMessageByUids(msg, uids, callback);

var channel = channel.getLocalChannelSync(‘area1’);

channel.pushMessage(msg);

Page 22: Game Server Development in node.js Charlie Circle @ @xiecc.

Framework --- channel&broadcast

areaconnectorsclient

channel

uids

connector1

connector2

client1

client2

clientn

regroup

uids1

uids2

… …

broadcast

Easy APIMost frequent actionPotentially performance

problem

Page 23: Game Server Development in node.js Charlie Circle @ @xiecc.

CategoryOverviewFrameworkPracticePerformance

Page 24: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- game demoDevelop time(first version): 2012-5-

14~2012-6-30

ClientHtml 5, based on colorbox frameworkAlmost 6,000 lines code

Server Node.js, based on pomelo frameworkAlmost 6,000 lines code

Page 25: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- simplest player move

client

Area1

connector

client1

client2

clientn

1、Move request

3、Move Handler

2、 Forward

4、 Backward

5、 Broadcast

6、 Play move animation

Page 26: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- Client Move Request

… find path, move animationpomelo.request({route:’area.playeH

andler.move’, path: path}, function (result){…

});

Page 27: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- area server handler

handler.move = function( req, session, next) {… verify path

… handle move

channelService.pushMessagesByUids(

route:’onMove’, ….);

session.response({code:OK});next();

}

Page 28: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- client play move

pomelo.on(‘onMove’, function(data) {play move animation…

});

Page 29: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- character moveCharacter Move, isn’t that easy?

In reality , it’s hard

Page 30: Game Server Development in node.js Charlie Circle @ @xiecc.

Practice --- handle moveDifferent situations

Player move, mob moveAI driven or player driven

Smooth effectClient predictionLatency Compensate

How to notifyAOI(area of interest)

Page 31: Game Server Development in node.js Charlie Circle @ @xiecc.

CategoryOverviewFrameworkPracticePerformance

Page 32: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- overviewThe indicator

The max online usersResponse time/throughputSingle area or game?

The variationGame logic: round or realtime, room or

infiniteMap size, character densityBalance of areasTest parameters: Think time, test action

Page 33: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- targetArea online users

next-gen: Socket.io: 25,000 concurrent users

But in the real worldThe real online data: maximum 1,000 concurrent

users per area, 8,000 concurrent users per group game servers

Page 34: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- toolsStress testing for websocket--pomelo-robot

master

agent agent

robot robot robot robot robot

Page 35: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- tools

Stress test console

Page 36: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- tools, profilerServer profiler

Page 37: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- stress testing

Stress on single area, increasing step by step

Real game logic simulationRoam, fight, pickThink time: 2s~4s

Page 38: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- hardwareCPU , 24 cores

Mem, 48G

Page 39: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- stress testing

Page 40: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- stress testing

Page 41: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- progress5 roundsOnline users: 200 to 1600…Response time: 300msProblems solved:

Html 5, client side memory leakPath finding and aoi optimizationData too… fat, reduce weight Unclean connection data, make socket.io

crazySome api implementation: dataApiDivide process: path findingAnd… broadcast

Page 42: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- broadcast200 online users, connector 100% cpu

Page 43: Game Server Development in node.js Charlie Circle @ @xiecc.

Areaconnectorsclient

channel

uids

connector1

connector2

client1

client2

clientn

regroup

uids1

uids2

… …

broadcast

tick: 100ms

Performance --- channel, where is wrong?

tick:50ms

serializedeserialize

serialize

serialize

serialize

deserialize

Page 44: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- connectorConnector--- the middle manWhat do I need data for?

connector1

connector2

Message in

forward

Parse the route

area{route:’area.playerHandler.hello’, data:{…}}

area

forward

broadcast

Client

encode

Decode: only route

Stringify

parse

Serializedeserialize

Page 45: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- the packagePomelo-protocal

Only parse head for route information:

\0\0\0\3\34connector.loginHandler.login{“username”:”xcc”,……..}

Page 46: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- the result1600 onlines

Page 47: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- the result1600 onlines , server loadIsn’t that amazing? no

Page 48: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- the result800 onlines, fight each other

Page 49: Game Server Development in node.js Charlie Circle @ @xiecc.

Performance --- the resultServer load, fight each other

Page 50: Game Server Development in node.js Charlie Circle @ @xiecc.

TODOPerformance improvement

The broadcast data sizeGC, object pool, or… really crazyrpc underlying protocal : socket.io, need

change

Interface, tools improvementDocument, websiteOpen source: 2012-11

Page 51: Game Server Development in node.js Charlie Circle @ @xiecc.

Q&A