Client P2PConnection API Reference
P2P Connection.
API List
| API | Description |
|---|---|
| DetectNatType | Detect the NAT type of the current player. |
| P2PConnect | Initiate a p2p connection request to another player. |
| P2PClose | Close a p2p connection. |
| AcceptP2PConnect | Accept a p2p connection request that another player initiated. |
| RejectP2PConnect | Reject a p2p connection request that another player initiated. |
| SendP2PMessage | Send a message to the peer player. |
| GetP2PConnectionInfoByPlayerId | Get all the p2p connection info by the peer player id. Since multiple p2p connections may be established with the same player, this function will return all the p2p connection info. |
| GetP2PConnectionInfoByConnectionId | Get the p2p connection info by the connection id. Only one p2p connection info, if exist, will be returned. |
| GetAllP2PConnectionInfo | Get all the p2p connection info. |
Event List
| Event | Description |
|---|---|
| SetOnP2PConnectRequest | When a p2p connection request is received, this event will be triggered. |
| SetOnP2PMessage | When a p2p message is received, this event will be triggered. |
| SetOnP2PConnectionClosed | When a p2p connection is closed, this event will be triggered. |
API Details
DetectNatType
Detect the NAT type of the current player.
/**
* Detect the NAT type of the current player.
*
* @param result_callback The result callback after the API execution ends, and it will be called in an ASYNCHRONOUS CHILD THREAD.
*/
void DetectNatType(
PgosDataCallback<DetectNatTypeResult> result_callback);
Parameters:
| Param | Type | Description |
|---|---|---|
| result_callback | PgosDataCallback<DetectNatTypeResult> | The result callback after the API execution ends, and it will be called in an ASYNCHRONOUS CHILD THREAD. |
Return: void
P2PConnect
Initiate a p2p connection request to another player.
/**
* Initiate a p2p connection request to another player.
*
* @param result_callback The result callback after the API execution ends, and it will be called in an ASYNCHRONOUS CHILD THREAD.
* @return The connection id
*/
pgos::pstring P2PConnect(
const P2PConnectParams& params,
PgosDataCallback<P2PConnectResult> result_callback);
Parameters:
| Param | Type | Description |
|---|---|---|
| params | const P2PConnectParams& | |
| result_callback | PgosDataCallback<P2PConnectResult> | The result callback after the API execution ends, and it will be called in an ASYNCHRONOUS CHILD THREAD. |
Return: pgos::pstring
The connection id
P2PClose
Close a p2p connection.
/**
* Close a p2p connection.
*
* @param connection_id The id of the connection to close
* @return The result of the operation. If the connection id is valid, the result will be OK.
*/
PgosResult P2PClose(
const pgos::pstring& connection_id);
Parameters:
| Param | Type | Description |
|---|---|---|
| connection_id | const std::string& | The id of the connection to close |
Return: PgosResult
The result of the operation. If the connection id is valid, the result will be OK.
AcceptP2PConnect
Accept a p2p connection request that another player initiated.
/**
* Accept a p2p connection request that another player initiated.
*
* @param connection_id The id of the connection to accept
* @param result_callback The result callback after the API execution ends, and it will be called in an ASYNCHRONOUS CHILD THREAD.
*/
void AcceptP2PConnect(
const pgos::pstring& connection_id,
PgosDataCallback<P2PConnectResult> result_callback);
Parameters:
| Param | Type | Description |
|---|---|---|
| connection_id | const std::string& | The id of the connection to accept |
| result_callback | PgosDataCallback<P2PConnectResult> | The result callback after the API execution ends, and it will be called in an ASYNCHRONOUS CHILD THREAD. |
Return: void
RejectP2PConnect
Reject a p2p connection request that another player initiated.
/**
* Reject a p2p connection request that another player initiated.
*
* @param connection_id The id of the connection to reject
* @return The result of the operation. If the connection id is valid, the result will be OK.
*/
PgosResult RejectP2PConnect(
const pgos::pstring& connection_id);
Parameters:
| Param | Type | Description |
|---|---|---|
| connection_id | const std::string& | The id of the connection to reject |
Return: PgosResult
The result of the operation. If the connection id is valid, the result will be OK.
SendP2PMessage
Send a message to the peer player.
/**
* Send a message to the peer player.
*
* @return The result of the operation. If the connection is ready, the result will be OK.
*/
PgosResult SendP2PMessage(
const SendP2PMessageParams& params);
Parameters:
| Param | Type | Description |
|---|---|---|
| params | const SendP2PMessageParams& |
Return: PgosResult
The result of the operation. If the connection is ready, the result will be OK.
GetP2PConnectionInfoByPlayerId
Get all the p2p connection info by the peer player id. Since multiple p2p connections may be established with the same player, this function will return all the p2p connection info.
/**
* Get all the p2p connection info by the peer player id.
* Since multiple p2p connections may be established with the same player, this function will return all the p2p connection info.
*
* @param player_id The id of the peer player
* @return The p2p connection info.
*/
pgos::pvector<P2PConnectionInfo> GetP2PConnectionInfoByPlayerId(
const pgos::pstring& player_id);
Parameters:
| Param | Type | Description |
|---|---|---|
| player_id | const std::string& | The id of the peer player |
Return: pgos::pvector<P2PConnectionInfo>
The p2p connection info.
GetP2PConnectionInfoByConnectionId
Get the p2p connection info by the connection id. Only one p2p connection info, if exist, will be returned.
/**
* Get the p2p connection info by the connection id.
* Only one p2p connection info, if exist, will be returned.
*
* @param connection_id The id of the connection
* @param dst The p2p connection info that will be filled
* @return If the connection id is valid, the result will be true.
*/
bool GetP2PConnectionInfoByConnectionId(
const pgos::pstring& connection_id,
P2PConnectionInfo& dst);
Parameters:
| Param | Type | Description |
|---|---|---|
| connection_id | const std::string& | The id of the connection |
| dst | P2PConnectionInfo& | The p2p connection info that will be filled |
Return: bool
If the connection id is valid, the result will be true.
GetAllP2PConnectionInfo
Get all the p2p connection info.
/**
* Get all the p2p connection info.
*
* @return The list of p2p connection info.
*/
pgos::pvector<P2PConnectionInfo> GetAllP2PConnectionInfo();
Parameters:
(No parameters)
Return: pgos::pvector<P2PConnectionInfo>
The list of p2p connection info.
Event Details
SetOnP2PConnectRequest
When a p2p connection request is received, this event will be triggered.
/**
* OnSetOnP2PConnectRequest:
* When a p2p connection request is received, this event will be triggered.
*
* @param event Event result.
*/
void SetOnP2PConnectRequest(
const P2PConnectRequestEvt& event);
Parameters:
| Param | Type | Description |
|---|---|---|
| event | const P2PConnectRequestEvt& | Event result. |
SetOnP2PMessage
When a p2p message is received, this event will be triggered.
/**
* OnSetOnP2PMessage:
* When a p2p message is received, this event will be triggered.
*
* @param event Event result.
*/
void SetOnP2PMessage(
const P2PMessageEvt& event);
Parameters:
| Param | Type | Description |
|---|---|---|
| event | const P2PMessageEvt& | Event result. |
SetOnP2PConnectionClosed
When a p2p connection is closed, this event will be triggered.
/**
* OnSetOnP2PConnectionClosed:
* When a p2p connection is closed, this event will be triggered.
*
* @param event Event result.
*/
void SetOnP2PConnectionClosed(
const P2PConnectionClosedEvt& event);
Parameters:
| Param | Type | Description |
|---|---|---|
| event | const P2PConnectionClosedEvt& | Event result. |