Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Games 程式設計 潘仁義 CCU COMM.

Similar presentations


Presentation on theme: "Networking Games 程式設計 潘仁義 CCU COMM."— Presentation transcript:

1 Networking Games 程式設計 潘仁義 CCU COMM

2 網路遊戲系統架構 Pure Client/Server 架構 Complete world Game Server database
Protocol State Computed result

3 貪食蛇

4 程式架構 Server Client Protocol 記載所有蛇的位置,移動方向等資訊 負責接受連線
遊戲規則計算(上下左右移,有沒有撞到) Client 接受使用者輸入 顯示蛇在螢幕上 與Server端連線 Protocol 連線後,先告知client蛇的ID,以便特殊顯示 Client 傳key給server Server 傳 (ID, row, col)給Client畫

5 Client 程式流程 start 主要運作迴圈 N 是否有網路或使用者輸入 與server連線 Y 處理網路資料 畫圖 接受MyID
stop

6 Server 資料結構 蛇, struct { 蛇有好幾條 蛇移動的時候… 蛇撞到的時候… 有一條新的蛇的時候…
fd, row[], col[], d_row, d_col 蛇有好幾條 Array of struct 蛇移動的時候… 蛇撞到的時候… 有一條新的蛇的時候…

7 Server 程式流程 主迴圈 start 處理已連線的蛇 準備被連線 處理要連線的蛇 時間進行 將蛇移動 如果撞到 stop

8 I/O Multiplexing select()

9 I/O Multiplexing We often need to be able to monitor multiple descriptors: a generic TCP client (like telnet) A server that handles both TCP and UDP Client that can make multiple concurrent requests (browser?).

10 Example - generic TCP client
Input from standard input should be sent to a TCP socket. Input from a TCP socket should be sent to standard output. How do we know when to check for input from each source?

11 Generic TCP Client STDIN TCP SOCKET STDOUT

12 select() int select( int maxfd + 1, fd_set *readset, fd_set *writeset,
fd_set *excepset, const struct timeval *timeout); maxfd: highest number assigned to a descriptor. readset: set of descriptors we want to read from. writeset: set of descriptors we want to write to. excepset: set of descriptors to watch for exceptions. timeout: maximum time select should wait

13 struct timeval struct timeval { long tv_sec; /* seconds */
long tv_usec; /* microseconds */ } struct timeval max = {1,0};

14 fd_set Implementation is not important
Operations you can use with an fd_set: void FD_ZERO( fd_set *fdset); void FD_SET( int fd, fd_set *fdset); void FD_CLR( int fd, fd_set *fdset); int FD_ISSET( int fd, fd_set *fdset);

15 Using select() Create fd_set Clear the whole thing with FD_ZERO
Add each descriptor you want to watch using FD_SET. Call select when select returns, use FD_ISSET to see if I/O is possible on each descriptor.

16 Server的multiplexing Server 時間進行 接受新的蛇的連線 好幾條蛇的輸入 新的蛇

17 Client 的multiplexing Client Server的畫圖指示 接受使用者輸入 使用者


Download ppt "Networking Games 程式設計 潘仁義 CCU COMM."

Similar presentations


Ads by Google