跳到主要内容

Client P2PConnection API Reference

P2P Connection.

API List

APIDescription
DetectNatTypeDetect the NAT type of the current player.
P2PConnectInitiate a p2p connection request to another player.
P2PCloseClose a p2p connection.
AcceptP2PConnectAccept a p2p connection request that another player initiated.
RejectP2PConnectReject a p2p connection request that another player initiated.
SendP2PMessageSend a message to the peer player.
GetP2PConnectionInfoByPlayerIdGet 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.
GetP2PConnectionInfoByConnectionIdGet the p2p connection info by the connection id. Only one p2p connection info, if exist, will be returned.
GetAllP2PConnectionInfoGet all the p2p connection info.

Event List

EventDescription
SetOnP2PConnectRequestWhen a p2p connection request is received, this event will be triggered.
SetOnP2PMessageWhen a p2p message is received, this event will be triggered.
SetOnP2PConnectionClosedWhen 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:

ParamTypeDescription
result_callbackPgosDataCallback<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:

ParamTypeDescription
paramsconst P2PConnectParams&
result_callbackPgosDataCallback<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:

ParamTypeDescription
connection_idconst 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:

ParamTypeDescription
connection_idconst std::string&The id of the connection to accept
result_callbackPgosDataCallback<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:

ParamTypeDescription
connection_idconst 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:

ParamTypeDescription
paramsconst 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:

ParamTypeDescription
player_idconst 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:

ParamTypeDescription
connection_idconst std::string&The id of the connection
dstP2PConnectionInfo&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:

ParamTypeDescription
eventconst 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:

ParamTypeDescription
eventconst 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:

ParamTypeDescription
eventconst P2PConnectionClosedEvt&Event result.