Networking functions for making connections and sending data between clients, traversing NATs when possible.
หมายเหตุ: API นี้เลิกใช้งานแล้วและอาจถูกลบออกในการเปิดตัว Steamworks SDK ในอนาคต กรุณาใช้
ISteamNetworkingSockets หรือ
ISteamNetworkingMessages แทน กรุณาอ่านข้อมูลเพิ่มเติมในภาพรวม
ระบบเครือข่าย SteamMember Functions
ฟังก์ชันสมาชิกสำหรับ
ISteamNetworking
จะถูกเรียกผ่านทางฟังก์ชันตัวเข้าถึงส่วนกลาง
SteamNetworking()
AcceptP2PSessionWithUser
bool AcceptP2PSessionWithUser( CSteamID steamIDRemote );
Name | Type | Description |
steamIDRemote | CSteamID | The Steam ID of the user that sent the initial packet to us. |
ทำให้เกมสามารถแจ้งการตอบรับแพ็กเก็ตขาเข้าได้ จำเป็นต้องเรียกฟังก์ชันนี้ก่อนทำการเชื่อมต่อจริงกับโฮสต์ระยะไกล เพื่อให้เกมมีโอกาสแจ้งว่าผู้ใช้ระยะไกลได้รับอนุญาตให้พูดคุยกับเกมหรือไม่
เมื่อผู้ใช้ระยะไกลที่คุณไม่ได้ส่งแพ็กเก็ตให้ในช่วงนี้ พยายามจะส่งแพ็กเก็ตถึงคุณเป็นครั้งแรก เกมของคุณจะได้รับคอลแบ็ก
P2PSessionRequest_t คอลแบ็กนี้ประกอบด้วย Steam ID ของผู้ใช้ที่ต้องการส่งแพ็กเก็ตถึงคุณ เพื่อเป็นการตอบสนองคอลแบ็กนี้ คุณอาจต้องตรวจสอบว่าเป็นคนที่คุณคุยด้วย (เช่นบุคคลที่อยู่ในล็อบบี้กับคุณ) ใช่หรือไม่ และถ้าใช่ ให้ตอบรับการเชื่อมต่อ แต่ถ้าคุณไม่ต้องการพูดคุยกับผู้ใช้ ให้เพิกเฉยกับคำขอดังกล่าว ถ้าผู้ใช้รายนั้นยังส่งแพ็กเก็ตถึงคุณอย่างต่อเนื่อง จะมีการโพสต์
P2PSessionRequest_t อีกเป็นระยะ ๆ ถ้าคุณเรียก
SendP2PPacket กับผู้ใช้อื่น จะถือเป็นการยอมรับคำขอเซสชันทางอ้อม
โปรดทราบว่าการเรียกนี้ควรเป็นการตอบสนอง
P2PSessionRequest_t คอลแบ็กเท่านั้น!
คืนค่า: bool
true เมื่อสำเร็จ คืนค่า
false เมื่อ
steamIDRemote
ไม่ถูกต้องเท่านั้น
AllowP2PPacketRelay
bool AllowP2PPacketRelay( bool bAllow );
Name | Type | Description |
bAllow | bool | |
อนุญาตหรือไม่อนุญาตให้การเชื่อมต่อ P2P กลับไปรีเลย์ผ่านเซิร์ฟเวอร์ Steam หากไม่สามารถเชื่อมต่อโดยตรงหรือข้ามผ่าน NAT ได้
มีผลกับการเชื่อมต่อที่สร้างขึ้นหลังจากตั้งค่านี้ หรือการเชื่อมต่อที่มีอยู่แล้ว ซึ่งจำเป็นต้องเชื่อมต่อใหม่โดยอัตโนมัติหลังจากตั้งค่านี้เท่านั้น
โดยค่าเริ่มต้นแล้วจะอนุญาตการรีเลย์แพ็กเก็ต P2P
Returns: bool
ฟังก์ชันนี้จะคืนค่า
true เสมอ
CloseP2PChannelWithUser
bool CloseP2PChannelWithUser( CSteamID steamIDRemote, int nChannel );
Name | Type | Description |
steamIDRemote | CSteamID | The Steam ID of the user to close the connection with. |
nChannel | int | The channel to close. |
ปิดแชนแนล P2P เมื่อคุณพูดกับผู้ใช้ในแชนแนลที่เฉพาะเจาะจงเรียบร้อยแล้ว
เมื่อปิดแชนแนลถึงผู้ใช้ทุกแชนแนล เซสชันที่เปิดไว้กับผู้ใช้จะถูกปิดลง และข้อมูลใหม่จากผู้ใช้นี้จะทำให้เกิดคอลแบ็ก
P2PSessionRequest_t ใหม่
คืนค่า: bool
true ถ้าปิดแชนแนลสำเร็จ มิฉะนั้นจะคืนค่า
false ถ้าไม่มีการใช้งานเซสชันหรือแชนแนลกับผู้ใช้
CloseP2PSessionWithUser
bool CloseP2PSessionWithUser( CSteamID steamIDRemote );
Name | Type | Description |
steamIDRemote | CSteamID | The Steam ID of the user to close the connection with. |
ควรเรียกฟังก์ชันนี้เมื่อสื่อสารกับผู้ใช้เสร็จแล้ว เพื่อคืนทรัพยากรเบื้องหลังทั้งหมดที่จัดสรรให้กับการเชื่อมต่อ
ถ้าผู้ใช้ระยะไกลพยายามส่งข้อมูลถึงคุณอีกครั้ง จะมีการโพสต์
P2PSessionRequest_t คอลแบ็กใหม่
คืนค่า: bool
true ถ้าปิดเซสชันสำเร็จ มิฉะนั้นจะคืนค่า
false ถ้าไม่มีการเปิดการเชื่อมต่อด้วย
steamIDRemote
CreateConnectionSocket
SNetSocket_t CreateConnectionSocket( uint32 nIP, uint16 nPort, int nTimeoutSec );
สร้างซ็อกเก็ตและเริ่มการเชื่อมต่อกับที่หมายระยะไกล
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: SNetSocket_tCreateListenSocket
SNetListenSocket_t CreateListenSocket( int nVirtualP2PPort, uint32 nIP, uint16 nPort, bool bAllowUseOfPacketRelay );
Name | Type | Description |
nVirtualP2PPort | int | |
nIP | uint32 | |
nPort | uint16 | |
bAllowUseOfPacketRelay | bool | |
สร้างซ็อกเก็ตและรอ (Listen) การเชื่อมต่อจากบุคคลอื่น
จะเรียกคอลแบ็ก
SocketStatusCallback_t เมื่อมีไคลเอนต์อื่นกำลังเชื่อมต่อ
nVirtualP2PPort เป็น ID เฉพาะที่ไคลเอนต์จะเชื่อมต่อด้วย หากคุณมีหลายพอร์ต
โดยทั่วไปแล้วใช้ค่า 0 ได้ ยกเว้นว่าคุณต้องการการเชื่อมต่อหลายชุด
unIP คือที่อยู่ IP โลคอลที่จะเชื่อมโยง
ส่งค่า 0 เข้าไป ถ้าคุณแค่ต้องการ IP โลคอลเริ่มต้น
unPort คือพอร์ตที่จะใช้
ส่งค่า 0 เข้าไป ถ้าคุณไม่ต้องการให้ผู้ใช้เชื่อมต่อผ่าน IP/พอร์ตได้ แต่คาดหวังว่าจะเชื่อมต่อแบบ Peer-to-Peer เท่านั้น
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: SNetListenSocket_tCreateP2PConnectionSocket
SNetSocket_t CreateP2PConnectionSocket( CSteamID steamIDTarget, int nVirtualPort, int nTimeoutSec, bool bAllowUseOfPacketRelay );
Name | Type | Description |
steamIDTarget | CSteamID | |
nVirtualPort | int | |
nTimeoutSec | int | |
bAllowUseOfPacketRelay | bool | |
สร้างซ็อกเก็ตและเริ่มการเชื่อมต่อกับที่หมายระยะไกล
สามารถเชื่อมต่อผ่าน Steam ID ที่ทราบ (ไคลเอนต์หรือเซิร์ฟเวอร์เกม) หรือเชื่อมต่อโดยตรงกับ IP ได้
เมื่อสำเร็จ จะเรียกคอลแบ็ก SocketStatusCallback_t
เมื่อล้มเหลวหรือหมดเวลา จะเรียกคอลแบ็ก SocketStatusCallback_t พร้อมรหัสความล้มเหลวใน m_eSNetSocketState
ฟังก์ชันนี้เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: SNetSocket_tDestroyListenSocket
bool DestroyListenSocket( SNetListenSocket_t hSocket, bool bNotifyRemoteEnd );
การทำลายซ็อกเก็ต Listen จะเป็นการทำลายซ็อกเก็ตทั่วไปทั้งหมดที่สร้างจากซ็อกเก็ตนี้
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
DestroySocket
bool DestroySocket( SNetSocket_t hSocket, bool bNotifyRemoteEnd );
Name | Type | Description |
hSocket | SNetSocket_t | |
bNotifyRemoteEnd | bool | |
ยกเลิกการเชื่อมต่อกับซ็อกเก็ต ถ้ามี และทำให้แฮนเดิลใช้งานไม่ได้
ข้อมูลในซ็อกเก็ตที่ยังไม่ได้อ่านจะถูกทิ้งไป
ถ้าตั้งค่า bNotifyRemoteEnd ไว้ จะไม่สามารถทำลายซ็อกเก็ตได้อย่างสมบูรณ์ จนกว่าปลายทางระยะไกลจะตอบรับการยกเลิกการเชื่อมต่อแล้ว
ฟังก์ชันนี้เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
GetListenSocketInfo
bool GetListenSocketInfo( SNetListenSocket_t hListenSocket, uint32 *pnIP, uint16 *pnPort );
คืนค่าพอร์ตโลคอลที่เชื่อมโยงกับซ็อกเก็ต Listen
*pnIP และ *pnPort จะมีค่าเป็น 0 ถ้าตั้งค่าซ็อกเก็ตให้รอ (Listen) การเชื่อมต่อ P2P เท่านั้น
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
GetMaxPacketSize
int GetMaxPacketSize( SNetSocket_t hSocket );
รับขนาดแพ็กเก็ตสูงสุดในหน่วยไบต์
ฟังก์ชันนี้เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: int
GetP2PSessionState
bool GetP2PSessionState( CSteamID steamIDRemote, P2PSessionState_t *pConnectionState );
Name | Type | Description |
steamIDRemote | CSteamID | The user to get the active session state information of. |
pConnectionState | P2PSessionState_t * | Returns the state. |
ป้อนข้อมูลในโครงสร้าง
P2PSessionState_t ด้วยรายละเอียดเกี่ยวกับการเชื่อมต่อ เช่น มีการเชื่อมต่อที่ทำงานอยู่หรือไม่ จำนวนไบต์ที่ต่อคิวอยู่ในการเชื่อมต่อ รหัสข้อผิดพลาดล่าสุด ถ้ามี กำลังใช้งานเซิร์ฟเวอร์รีเลย์อยู่หรือไม่ และ IP กับพอร์ตของผู้ใช้ระยะไกล ถ้าทราบ
โดยทั่วไปแล้วจำเป็นต้องใช้ในการดีบักเท่านั้น
คืนค่า: bool
true ถ้า
pConnectionState
ได้รับการป้อนข้อมูล มิฉะนั้นจะคืนค่า
false ถ้าไม่มีการเปิดเซสชันกับผู้ใช้ที่ระบุ
ตัวอย่าง:P2PSessionState_t p2pSessionState;
SteamNetworking()->GetP2PSessionState( steamIDremote, &p2pSessionState );
GetSocketConnectionType
ESNetSocketConnectionType GetSocketConnectionType( SNetSocket_t hSocket );
คืนค่า true เพื่ออธิบายว่าเชื่อมต่อซ็อกเก็ตได้อย่างไรในที่สุด
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: ESNetSocketConnectionTypeGetSocketInfo
bool GetSocketInfo( SNetSocket_t hSocket, CSteamID *pSteamIDRemote, int *peSocketStatus, uint32 *punIPRemote, uint16 *punPortRemote );
คืนค่าข้อมูลเกี่ยวกับซ็อกเก็ตที่ระบุ พร้อมป้อนเนื้อหาของพอยน์เตอร์
ฟังก์ชันนี้เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
IsDataAvailable
bool IsDataAvailable( SNetListenSocket_t hListenSocket, uint32 *pcubMsgSize, SNetSocket_t *phSocket );
ตรวจสอบข้อมูลจากซ็อกเก็ตที่เชื่อมต่อจากซ็อกเก็ต Listen นี้
คืนค่า false ถ้าไม่มีข้อมูลเหลืออยู่
ป้อนข้อมูลขนาดของข้อความถัดไปในหน่วยไบต์ลงใน *pcubMsgSize
ป้อนซ็อกเก็ตที่มีข้อมูลลงใน *phSocket
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
IsDataAvailableOnSocket
bool IsDataAvailableOnSocket( SNetSocket_t hSocket, uint32 *pcubMsgSize );
คืนค่า false ถ้าไม่มีข้อมูลเหลืออยู่
ป้อนข้อมูลขนาดของข้อความถัดไปในหน่วยไบต์ลงใน *pcubMsgSize
ฟังก์ชันนี้เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
IsP2PPacketAvailable
bool IsP2PPacketAvailable( uint32 *pcubMsgSize, int nChannel = 0 );
Name | Type | Description |
pcubMsgSize | uint32 * | Returns the size of the packet. |
nChannel | int | The channel to check if a packet is available in. |
ตรวจสอบว่ามีแพ็กเก็ต P2P ให้อ่านหรือไม่ แล้วรับขนาดข้อความ ถ้ามี
ควรเรียกในลูปสำหรับแต่ละแชนแนลที่คุณใช้ หากมีแพ็กเก็ต คุณควรเรียก
ReadP2PPacket เพื่อรับข้อมูลแพ็กเก็ต
คืนค่า: bool
true if there is a packet available; otherwise,
false.
ReadP2PPacket
bool ReadP2PPacket( void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, CSteamID *psteamIDRemote, int nChannel = 0 );
Name | Type | Description |
pubDest | void * | Returns the packet data by copying it into this buffer. |
cubDest | uint32 | The size allocated for pubDest . This should be either the same size returned by IsP2PPacketAvailable, or the size of your largest packet. |
pcubMsgSize | uint32 * | Returns the size of the packet. |
psteamIDRemote | CSteamID * | Returns the Steam ID of the user that sent this packet. |
nChannel | int | The channel the packet was sent over. |
อ่านแพ็กเก็ตที่ส่งจากผู้ใช้อื่นผ่าน
SendP2PPacketIf the
cubDest
buffer is too small for the packet, then the message will be truncated.
การเรียกนี้จะไม่เป็นการบล็อก และจะคืนค่า false ถ้าไม่มีข้อมูล
ก่อนเรียกฟังก์ชันนี้ คุณควรเรียก
IsP2PPacketAvailable ไปแล้ว
คืนค่า: bool
true ถ้าอ่านแพ็กเก็ตสำเร็จ มิฉะนั้นจะคืนค่า
false ถ้าไม่มีแพ็กเก็ต
ตัวอย่าง:uint32 msgSize = 0;
while ( SteamNetworking()->IsP2PPacketAvailable( &msgSize ) )
{
void *packet = malloc( msgSize );
CSteamID steamIDRemote;
uint32 bytesRead = 0;
if ( SteamNetworking()->ReadP2PPacket( packet, msgSize, &bytesRead, &steamIDRemote ) )
{
// message dispatch code goes here
}
free( packet );
}
RetrieveData
bool RetrieveData( SNetListenSocket_t hListenSocket, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, SNetSocket_t *phSocket );
รับข้อมูลจากซ็อกเก็ตใด ๆ ที่เชื่อมต่อจากซ็อกเก็ต Listen นี้
ป้อนเนื้อหาของข้อความลงใน pubDest
ข้อความจะอยู่ในลักษณะสมบูรณ์เสมอ โดยมีขนาดเท่ากับที่ส่ง (กล่าวคืออยู่ในรูปแบบแพ็กเก็ต ไม่ใช่สตรีม)
ถ้า *pcubMsgSize < cubDest แสดงว่ามีการเขียนข้อมูลบางส่วนเท่านั้น
คืนค่า false ถ้าไม่มีข้อมูล
ป้อนซ็อกเก็ตที่มีข้อมูลลงใน *phSocket
นี่เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
RetrieveDataFromSocket
bool RetrieveDataFromSocket( SNetSocket_t hSocket, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize );
ป้อนเนื้อหาของข้อความลงใน pubDest
ข้อความจะอยู่ในลักษณะสมบูรณ์เสมอ โดยมีขนาดเท่ากับที่ส่ง (กล่าวคืออยู่ในรูปแบบแพ็กเก็ต ไม่ใช่สตรีม)
ถ้า *pcubMsgSize < cubDest แสดงว่ามีการเขียนข้อมูลบางส่วนเท่านั้น
คืนค่า false ถ้าไม่มีข้อมูล
ฟังก์ชันนี้เป็นส่วนหนึ่งของชุดฟังก์ชันเก่าที่ออกแบบจากโมเดลซ็อกเก็ต TCP ของ Berkeley เราแนะนำให้ใช้ฟังก์ชัน P2P เนื่องจากไว้วางใจได้มากกว่า และเราจะเลิกใช้ฟังก์ชันเก่าเหล่านี้ในอนาคต
Returns: bool
SendDataOnSocket
bool SendDataOnSocket( SNetSocket_t hSocket, void *pubData, uint32 cubData, bool bReliable );
การส่งข้อมูล
จะต้องเป็นแฮนเดิลสำหรับซ็อกเก็ตที่เชื่อมต่อ
ข้อมูลทั้งหมดจะถูกส่งผ่าน UDP ดังนั้นขนาดการส่งจะถูกจำกัดอยู่ที่ 1200 ไบต์ หลังจากเกินค่านี้ เราเตอร์ส่วนใหญ่จะเริ่มทิ้งแพ็กเก็ต
ใช้แฟล็กตรวจสอบความน่าเชื่อถือด้วยความระมัดระวัง แม้ว่าอัตราการส่งใหม่จะค่อนข้างถี่
แต่การรับข้อมูลก็ยังอาจล่าช้าได้ (เหมือน TCP)
คืนค่า: bool
SendP2PPacket
bool SendP2PPacket( CSteamID steamIDRemote, const void *pubData, uint32 cubData, EP2PSend eP2PSendType, int nChannel = 0 );
Name | Type | Description |
steamIDRemote | CSteamID | The target user to send the packet to. |
pubData | const void * | The raw byte array for the packet data to send. The maximum size of this packet is defined by eP2PSendType . |
cubData | uint32 | The size in bytes of pubData . |
eP2PSendType | EP2PSend | Specifies how you want the data to be transmitted, such as reliably, unreliable, buffered, etc. |
nChannel | int | The channel which acts as a virtual port to send this packet on and allows you help route message to different systems. You'll have to call ReadP2PPacket on the other end with the same channel number in order to retrieve the data on the other end. Using different channels to talk to the same user will still use the same underlying P2P connection, saving on resources. Use 0 for the primary channel, or if you do not use this feature. |
ส่งแพ็กเก็ต P2P ให้กับผู้ใช้ที่ระบุ
API นี้เป็นแบบไม่มีเซสชัน ซึ่งจะสร้างการเชื่อมต่อแบบข้ามผ่าน NAT หรือการเชื่อมต่อกับเซิร์ฟเวอร์รีเลย์ของ Steam โดยอัตโนมัติ
หมายเหตุ: แพ็กเก็ตแรกที่ส่งอาจเกิดความล่าช้า เนื่องจากมีการรันโค้ดสำหรับข้าม NAT
ดูรายละเอียดวิธีต่าง ๆ ในการส่งแพ็กเก็ตได้ที่
EP2PSendข้อมูลที่คุณส่งจะอยู่ในลักษณะข้อมูลที่ไม่เจาะจงประเภท ดังนั้นจึงสามารถใช้ระบบที่ออกแบบไว้แล้วเช่น
Protocol Buffers หรือ
Cap'n Proto เพื่อเข้ารหัสแพ็กเก็ตอย่างมีประสิทธิภาพ หรือจะสร้างระบบการส่งข้อความของคุณเองก็ได้
คืนค่า: bool
เรียกใช้คอลแบ็ก
P2PSessionRequest_ttrue ถ้าส่งแพ็กเก็ตสำเร็จ
โปรดทราบว่าค่านี้ไม่ได้บ่งบอกว่าได้รับแพ็กเก็ตแล้ว ถ้าเราไม่สามารถไปถึงผู้ใช้ได้ภายในไทม์เอาต์ 20 วินาที จะมีการโพสต์ข้อผิดพลาดผ่านคอลแบ็ก
P2PSessionConnectFail_tfalse หากเป็นไปตามเงื่อนไขดังต่อไปนี้:
- The packet is too large for the send type.
- The target Steam ID is not valid.
- There are too many bytes queued up to be sent.
Callbacks
ฟังก์ชันเหล่านี้คือฟังก์ชันคอลแบ็กที่อาจถูกเรียกใช้งานเมื่อมีการเรียก
SteamAPI_RunCallbacks โดยฟังก์ชันเหล่านี้หลายตัวจะถูกเรียกใช้งานโดยตรงเพื่อตอบสนองกับฟังก์ชันสมาชิกของ
ISteamNetworking
P2PSessionConnectFail_t
ถูกเรียกเมื่อแพ็กเก็ตไม่สามารถเดินทางผ่านเข้าไปถึงผู้ใช้ที่ระบุได้
แพ็กเก็ตในคิวทั้งหมดที่ไม่ได้ส่งจะถูกทิ้งไป ถ้าพยายามส่งแพ็กเก็ตอีก จะเป็นการลองสร้างการเชื่อมต่ออีกครั้ง (แต่จะถูกทิ้งอีกถ้าเราล้มเหลวอีกครั้ง)
Name | Type | Description |
m_steamIDRemote | CSteamID | User we were trying to send the packets to. |
m_eP2PSessionError | uint8 | Indicates the reason why we're having trouble. Actually a EP2PSessionError. |
P2PSessionRequest_t
ผู้ใช้ต้องการสื่อสารกับเราผ่านแชนแนล P2P ด้วย 2PPacket
SendP2PPacket ถ้าต้องการตอบรับ คุณจำเป็นต้องเรียก
AcceptP2PSessionWithUser ถ้าต้องการเปิดแชนแนลเครือข่ายกับพวกเขา
Name | Type | Description |
m_steamIDRemote | CSteamID | The user who wants to start a P2P session with us. |
Associated Functions: SendP2PPacketSocketStatusCallback_t
Called when the status of a socket has changed, used as part of the
CreateListenSocket and
CreateP2PConnectionSocket calls.
This is part of an older set of functions designed around the Berkeley TCP sockets model. It's preferential that you use the P2P functions, they're more robust and these older functions will be removed eventually.
Name | Type | Description |
m_hSocket | SNetSocket_t | the socket used to send/receive data to the remote host |
m_hListenSocket | SNetListenSocket_t | this is the server socket that we were listening on; NULL if this was an outgoing connection |
m_steamIDRemote | CSteamID | remote Steam ID we have connected to, if it has one |
m_eSNetSocketState | int | socket state, ESNetSocketState |
Structs
These are structs which functions in ISteamNetworking may return and/or interact with.
P2PSessionState_t
Connection state to a specified user, returned by
GetP2PSessionState. This is the under-the-hood info about what's going on with a previous call to
SendP2PPacket. This typically shouldn't be needed except for debugging purposes.
Name | Type | Description |
m_bConnectionActive | uint8 | Do we have an active open connection with the user (true) or not (false)? |
m_bConnecting | uint8 | Are we currently trying to establish a connection with the user (true) or not (false)? |
m_eP2PSessionError | uint8 | Last error recorded on the socket. This returns a EP2PSessionError. |
m_bUsingRelay | uint8 | Is this connection going through a Steam relay server (true) or not (false)? |
m_nBytesQueuedForSend | int32 | The number of bytes queued up to be sent to the user. |
m_nPacketsQueuedForSend | int32 | The number of packets queued up to be sent to the user. |
m_nRemoteIP | uint32 | The IP of remote host if set. Could be a Steam relay server. This only exists for compatibility with older authentication api's. |
m_nRemotePort | uint16 | The Port of remote host if set. Could be a Steam relay server. This only exists for compatibility with older authentication api's. |
Enums
ค่าเหล่านี้คืออีนิวเมอเรต (Enum) ที่นิยามไว้สำหรับใช้งานร่วมกับ ISteamNetworking
EP2PSend
ระบุประเภทการส่งของ
SendP2PPacketโดยทั่วไปแล้ว
k_EP2PSendUnreliable
เป็นสิ่งที่คุณต้องการสำหรับแพ็กเก็ตแบบ UDP และ
k_EP2PSendReliable
สำหรับแพ็กเก็ตแบบ TCP
ชื่อ | ค่า | คำอธิบาย |
k_EP2PSendUnreliable | 0 | Basic UDP send. Packets can't be bigger than 1200 bytes (your typical MTU size). Can be lost, or arrive out of order (rare). The sending API does have some knowledge of the underlying connection, so if there is no NAT-traversal accomplished or there is a recognized adjustment happening on the connection, the packet will be batched until the connection is open again. |
k_EP2PSendUnreliableNoDelay | 1 | As above, but if the underlying P2P connection isn't yet established the packet will just be thrown away. Using this on the first packet sent to a remote host almost guarantees the packet will be dropped. This is only really useful for kinds of data that should never buffer up, i.e. voice payload packets |
k_EP2PSendReliable | 2 | Reliable message send. Can send up to 1MB of data in a single message. Does fragmentation/re-assembly of messages under the hood, as well as a sliding window for efficient sends of large chunks of data. |
k_EP2PSendReliableWithBuffering | 3 | As above, but applies the Nagle algorithm to the send - sends will accumulate until the current MTU size (typically ~1200 bytes, but can change) or ~200ms has passed (Nagle algorithm). This is useful if you want to send a set of smaller messages but have the coalesced into a single packet. Since the reliable stream is all ordered, you can do several small message sends with k_EP2PSendReliableWithBuffering and then do a normal k_EP2PSendReliable to force all the buffered data to be sent. |
EP2PSessionError
List of possible errors returned by
SendP2PPacket, these will be sent in the
P2PSessionConnectFail_t callback.
Name | Value | Description |
k_EP2PSessionErrorNone | 0 | There was no error. |
k_EP2PSessionErrorNotRunningApp | 1 | The target user is not running the same game. |
k_EP2PSessionErrorNoRightsToApp | 2 | The local user doesn't own the app that is running. |
k_EP2PSessionErrorDestinationNotLoggedIn | 3 | Target user isn't connected to Steam. |
k_EP2PSessionErrorTimeout | 4 | The connection timed out because the target user didn't respond, perhaps they aren't calling AcceptP2PSessionWithUser. Corporate firewalls can also block this (NAT traversal is not firewall traversal), make sure that UDP ports 3478, 4379, and 4380 are open in an outbound direction. |
k_EP2PSessionErrorMax | 5 | Unused. |
ESNetSocketConnectionType
Describes how the socket is currently connected. Only used by the old networking API.
Name | Value | Description |
k_ESNetSocketConnectionTypeNotConnected | 0 | |
k_ESNetSocketConnectionTypeUDP | 1 | |
k_ESNetSocketConnectionTypeUDPRelay | 2 | |
ESNetSocketState
Connection progress indicators, used by
CreateP2PConnectionSocket.
Name | Value | Description |
k_ESNetSocketStateInvalid | 0 | |
k_ESNetSocketStateConnected | 1 | communication is valid |
k_ESNetSocketStateInitiated | 10 | states while establishing a connection the connection state machine has started |
k_ESNetSocketStateLocalCandidatesFound | 11 | p2p connections we've found our local IP info |
k_ESNetSocketStateReceivedRemoteCandidates | 12 | we've received information from the remote machine, via the Steam back-end, about their IP info |
k_ESNetSocketStateChallengeHandshake | 15 | direct connections we've received a challenge packet from the server |
k_ESNetSocketStateDisconnecting | 21 | failure states the API shut it down, and we're in the process of telling the other end |
k_ESNetSocketStateLocalDisconnect | 22 | the API shut it down, and we've completed shutdown |
k_ESNetSocketStateTimeoutDuringConnect | 23 | we timed out while trying to creating the connection |
k_ESNetSocketStateRemoteEndDisconnected | 24 | the remote end has disconnected from us |
k_ESNetSocketStateConnectionBroken | 25 | connection has been broken; either the other end has disappeared or our local network connection has broke |
Typedefs
These are typedefs which are defined for use with ISteamNetworking.
Name | Base type | Description |
SNetListenSocket_t | uint32 | CreateListenSocket() |
SNetSocket_t | uint32 | handle to a socket CreateP2PConnectionSocket() |
Constants
These are constants which are defined for use with ISteamNetworking.
Name | ประเภท | Value | Description |
STEAMNETWORKING_INTERFACE_VERSION | const char * | "SteamNetworking005" | |