Quake源代碼分析(草稿).5

来源:互联网 发布:淘宝护肤品正品吗 编辑:程序博客网 时间:2024/05/22 04:29

網絡部分補充說明:
         負責傳送網間封包Sys_SendPacket()函數,調用了sendto這個Win socket API,它的作用是:
         The sendto function is normally used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Even if the connectionless socket has been previously connected to a specific address, the to parameter overrides the destination address for that particular datagram only. On a connection-oriented socket, the to and tolen parameters are ignored, making sendto equivalent to
send.
          所以很明顯,使用sendto並不需要建立可靠的連接,也就是不必先調用connect,直接能夠發送datagram.

客戶端連接服務器端的步驟:

1.   客戶端執行CL_Connect_f()

如果客戶端連接的服務器不是本地機器,需要多执行SVC_GetChallenge这一步

cls.state置為CA_CONNECTING.

判斷語句如下:

cls.state=NET_IsLocalAddress(&clc.server_address)?CA_CHALLENGING: CA_CONNECTING;

2.   如果客戶端變量cls.state = CA_CONNECTING,那麼當系統執行到

CL_CheckForResend()時就會啟動

NET_OutOfBandPrint( NS_CLIENT, &clc.server_address, "getchallenge" );

發送”getchallenge”消息給服務器端

3.   服務器端接收到”getchallenge”消息就會調用SVC_GetChallenge()à

NET_OutOfBandPrint(NS_SERVER,address,"challengeResponse %i", challenge );

發送”challengeResponse”消息給客戶端

4.   客戶端接受到”challengeResponse”消息:

cls.state = CA_CHALLENGING;

5.   如果客戶端變量cls.state = CA_CHALLENGING,那麼當系統執行到

CL_CheckForResend()時就會啟動
NET_OutOfBandPrint(NS_CLIENT, &clc.server_address, "connect /"%s/"", info );

發送”connect”消息給服務器端


6.   服務器端接受收到”connect”消息啟動SVC_DirectConnect(),如果客戶身分被確認,那麼就調用函數NET_OutOfBandPrint( NS_SERVER, address, "connectResponse" );

發送”connectResponse”消息給客戶端

否則NET_OutOfBandPrint( NS_SERVER, address, "disconnect" );

發送” disconnect”消息給客戶端


7.   客戶端接收到”connectResponse”,啟動Netchan_Setup,然後調用函數CL_WritePacket()寫入一條空白消息給服務器端.