Diarkis C++Client Library API Document
DiarkisP2PBase Class Reference

Base class that wraps the IDiarkisP2P class for easy use of P2P functionality. More...

#include <DiarkisP2PBase.h>

Collaboration diagram for DiarkisP2PBase:

Public Member Functions

 DiarkisP2PBase ()
 DiarkisP2PBase constructor. More...
 
virtual ~DiarkisP2PBase ()
 DiarkisP2PBase destructor. More...
 
virtual void SetupP2P (const std::shared_ptr< IDiarkisUdp > &udp, const std::shared_ptr< LoggerFactory > &loggerFactory, const char *uid)
 Set up DirectMessage with UDP protocol. More...
 
virtual void ConnectClient (const char *uid, const char **address, size_t addressCount, size_t timeoutInMilliseconds=5000, bool bBuffering=true)
 Attempts to make a P2P connection to the specified user ID. More...
 
virtual void ConnectClient (const std::string &uid, const std::vector< std::string > &address, size_t timeoutInMilliseconds=5000, bool bBuffering=true)
 Same as ConnectClient() More...
 
virtual void SetEncryptionKeys (bool bEncryption, const uint8_t *key, size_t keySize, const uint8_t *iv, size_t ivSize, const uint8_t *mkey, size_t mkeySize)
 Set the encryption key. More...
 
virtual void SetEncryptionKeys (bool bEncryption, const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv, const std::vector< uint8_t > &mkey)
 Same as SetEncryptionKeys() More...
 
virtual uint16_t SendUnicast (const uint8_t *payload, size_t payloadSize, RudpType type, const char *uid)
 Sends a message to one peer. More...
 
virtual uint16_t SendUnicast (const uint8_t *payload, size_t payloadSize, RudpType type, const std::string &uid)
 Same as SendUnicast() More...
 
virtual uint16_t SendUnicast (const std::vector< uint8_t > &payload, RudpType type, const std::string &uid)
 Same as SendUnicast() More...
 
virtual uint16_t SendMulticast (const uint8_t *payload, size_t payloadSize, RudpType type, const char **uids, size_t uidsLen)
 Sends a message to multiple peers. More...
 
virtual uint16_t SendMulticast (const uint8_t *payload, size_t payloadSize, RudpType type, const std::vector< std::string > &uid)
 Same as SendMulticast() More...
 
virtual uint16_t SendMulticast (const std::vector< uint8_t > &payload, RudpType type, const std::vector< std::string > &uid)
 Same as SendMulticast() More...
 
virtual uint16_t SendBroadcast (const uint8_t *payload, size_t payloadSize, RudpType type)
 Sends a message to all peers. More...
 
virtual uint16_t SendBroadcast (const std::vector< uint8_t > &payload, RudpType type)
 Same as SendBroadcast() More...
 
void Disconnect ()
 Disconnects the P2P connection to all peers. More...
 
bool Disconnect (const char *uid)
 Disconnect P2P connection with the specified user. More...
 
void Update ()
 Function to update the status of the connection and other information. Must be called periodically. More...
 
void SetSchedulerAddNotifyEvent (std::function< void()> notifyEventFunc)
 Sets the process to be called when a P2P-related event is added to the scheduler. More...
 
uint16_t GetConnectedNum ()
 Get the number of peers connected by P2P. More...
 
void GetPeerUids (Diarkis::StdVector< Diarkis::StdString > &output)
 Get the user id of peers connected by P2P. More...
 

Protected Member Functions

virtual void OnP2PReady (const DiarkisReadyEventArgs &args)
 Callback event called when a P2P connection succeeds. More...
 
virtual void OnP2PFail (const DiarkisFailEventArgs &args)
 Callback event called when a hole punch fails. More...
 
virtual void OnP2PDisconnect (const DiarkisP2PDisconnectEventArgs &args)
 Callback event called when a P2P connection is disconnected. More...
 
virtual void OnP2PMessage (const DiarkisMessageEventArgs &args)
 Callback event called when a message is received by P2P. More...
 
virtual void OnP2PException (const DiarkisExceptionEventArgs &args)
 Callback event called when sending or receiving fails in P2P. More...
 
void RegisterPeer (const Diarkis::StdString &uid, const std::shared_ptr< Peer > &p2p)
 Register an IDiarkisP2P instance. More...
 
uint64_t UnregisterPeer (const Diarkis::StdString &uid)
 Unregister an IDiarkisP2P instance. More...
 

Protected Attributes

Diarkis::StdUnorderedMap< Diarkis::StdString, std::shared_ptr< Peer > > p2pList_
 List of P2P connections. key:UID, value:pointer to IDiarkisP2P. More...
 
std::recursive_mutex p2pListMutex_
 P2P list Mutex. More...
 
std::shared_ptr< IDiarkisUdpudp_
 Pointer to the IDiarkisUdp class. More...
 
std::shared_ptr< LoggerFactoryloggerFactory_
 Pointer to logger class. More...
 
std::shared_ptr< IDiarkisLoggerlogger_
 Pointer to logger class. More...
 
std::function< void()> schedulerAddNotifyEventFunc_
 Callback function called when P2P-related events are added. More...
 
Diarkis::StdString ownGlobalAddress_
 My Global IP address. More...
 
Diarkis::StdString myUid_
 My user ID. More...
 

Detailed Description

Base class that wraps the IDiarkisP2P class for easy use of P2P functionality.

Constructor & Destructor Documentation

◆ DiarkisP2PBase()

DiarkisP2PBase::DiarkisP2PBase ( )

DiarkisP2PBase constructor.

◆ ~DiarkisP2PBase()

DiarkisP2PBase::~DiarkisP2PBase ( )
virtual

DiarkisP2PBase destructor.

Here is the call graph for this function:

Member Function Documentation

◆ SetupP2P()

void DiarkisP2PBase::SetupP2P ( const std::shared_ptr< IDiarkisUdp > &  udp,
const std::shared_ptr< LoggerFactory > &  loggerFactory,
const char *  uid 
)
virtual

Set up DirectMessage with UDP protocol.

Register loggers and callback functions.

Parameters
[in]udpPointer to IDiarkisUdp class
[in]loggerFactoryPointer to logger class
[in]uidYour user ID

◆ ConnectClient() [1/2]

void DiarkisP2PBase::ConnectClient ( const char *  uid,
const char **  address,
size_t  addressCount,
size_t  timeoutInMilliseconds = 5000,
bool  bBuffering = true 
)
virtual

Attempts to make a P2P connection to the specified user ID.

If the connection successes, a callback event registered with GetReadyEvent() (e.g. OnP2PReady) will be triggered.
If the connection fails, a callback event registered with GetFailEvent() (e.g. OnP2PFail) will be triggered.

Parameters
[in]uidYour user ID
[in]addressList of endpoints to connect to
[in]addressCountLength of the address
[in]timeoutInMillisecondsTimeout time for hole punch
[in]bBufferingWhether buffering is performed when sending or receiving packets
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConnectClient() [2/2]

void DiarkisP2PBase::ConnectClient ( const std::string &  uid,
const std::vector< std::string > &  address,
size_t  timeoutInMilliseconds = 5000,
bool  bBuffering = true 
)
virtual

Same as ConnectClient()

Here is the call graph for this function:

◆ SetEncryptionKeys() [1/2]

void DiarkisP2PBase::SetEncryptionKeys ( bool  bEncryption,
const uint8_t *  key,
size_t  keySize,
const uint8_t *  iv,
size_t  ivSize,
const uint8_t *  mkey,
size_t  mkeySize 
)
virtual

Set the encryption key.

Parameters
[in]bEncryptionWhether to encrypt or not
[in]keyEncryption key
[in]keySizeLength of the key
[in]ivInitialization vector
[in]ivSizeLength of the iv
[in]mkeyMessage authentication code
[in]mkeySizeLength of the mkey
Here is the caller graph for this function:

◆ SetEncryptionKeys() [2/2]

void DiarkisP2PBase::SetEncryptionKeys ( bool  bEncryption,
const std::vector< uint8_t > &  key,
const std::vector< uint8_t > &  iv,
const std::vector< uint8_t > &  mkey 
)
virtual

Same as SetEncryptionKeys()

Here is the call graph for this function:

◆ SendUnicast() [1/3]

uint16_t DiarkisP2PBase::SendUnicast ( const uint8_t *  payload,
size_t  payloadSize,
RudpType  type,
const char *  uid 
)
virtual

Sends a message to one peer.

When a message is received A callback event (e.g. OnP2PMessage) registered with GetMessageEvent() is triggered.

Parameters
[in]payloadData to be sent
[in]payloadSizeLength of the payload
[in]typeRUDP type to be used
[in]uidUser ID of the peer to which the data is sent
Returns
Number of peers sent
Here is the caller graph for this function:

◆ SendUnicast() [2/3]

uint16_t DiarkisP2PBase::SendUnicast ( const uint8_t *  payload,
size_t  payloadSize,
RudpType  type,
const std::string &  uid 
)
virtual

Same as SendUnicast()

Here is the call graph for this function:

◆ SendUnicast() [3/3]

uint16_t DiarkisP2PBase::SendUnicast ( const std::vector< uint8_t > &  payload,
RudpType  type,
const std::string &  uid 
)
virtual

Same as SendUnicast()

Here is the call graph for this function:

◆ SendMulticast() [1/3]

uint16_t DiarkisP2PBase::SendMulticast ( const uint8_t *  payload,
size_t  payloadSize,
RudpType  type,
const char **  uids,
size_t  uidsLen 
)
virtual

Sends a message to multiple peers.

When a message is received A callback event (e.g. OnP2PMessage) registered with GetMessageEvent() is triggered.

Parameters
[in]payloadData to be sent
[in]payloadSizeLength of the payload
[in]typeRUDP type to be used
[in]uidsList of user ID of the peer to which the data is sent
[in]uidsLenLength of the uids
Returns
Number of peers sent
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendMulticast() [2/3]

uint16_t DiarkisP2PBase::SendMulticast ( const uint8_t *  payload,
size_t  payloadSize,
RudpType  type,
const std::vector< std::string > &  uid 
)
virtual

Same as SendMulticast()

Here is the call graph for this function:

◆ SendMulticast() [3/3]

uint16_t DiarkisP2PBase::SendMulticast ( const std::vector< uint8_t > &  payload,
RudpType  type,
const std::vector< std::string > &  uid 
)
virtual

Same as SendMulticast()

Here is the call graph for this function:

◆ SendBroadcast() [1/2]

uint16_t DiarkisP2PBase::SendBroadcast ( const uint8_t *  payload,
size_t  payloadSize,
RudpType  type 
)
virtual

Sends a message to all peers.

When a message is received A callback event (e.g. OnP2PMessage) registered with GetMessageEvent() is triggered.

Parameters
[in]payloadData to be sent
[in]payloadSizeLength of the payload
[in]typeRUDP type to be used
Returns
Number of peers sent
Here is the caller graph for this function:

◆ SendBroadcast() [2/2]

uint16_t DiarkisP2PBase::SendBroadcast ( const std::vector< uint8_t > &  payload,
RudpType  type 
)
virtual

Same as SendBroadcast()

Here is the call graph for this function:

◆ Disconnect() [1/2]

void DiarkisP2PBase::Disconnect ( )

Disconnects the P2P connection to all peers.

Callback events registered with GetDisconnectEvent() (e.g. OnP2PDisconnect) will be triggered for disconnected peers.

Here is the caller graph for this function:

◆ Disconnect() [2/2]

bool DiarkisP2PBase::Disconnect ( const char *  uid)

Disconnect P2P connection with the specified user.

Callback events registered with GetDisconnectEvent() (e.g. OnP2PDisconnect) will be triggered.

Parameters
[in]uidUser ID of the user to disconnect.
Returns
Whether the disconnection process was executed or not.
Here is the call graph for this function:

◆ Update()

void DiarkisP2PBase::Update ( )

Function to update the status of the connection and other information. Must be called periodically.

◆ SetSchedulerAddNotifyEvent()

void DiarkisP2PBase::SetSchedulerAddNotifyEvent ( std::function< void()>  notifyEventFunc)

Sets the process to be called when a P2P-related event is added to the scheduler.

◆ GetConnectedNum()

uint16_t DiarkisP2PBase::GetConnectedNum ( )

Get the number of peers connected by P2P.

◆ GetPeerUids()

void DiarkisP2PBase::GetPeerUids ( Diarkis::StdVector< Diarkis::StdString > &  output)

Get the user id of peers connected by P2P.

◆ OnP2PReady()

void DiarkisP2PBase::OnP2PReady ( const DiarkisReadyEventArgs args)
protectedvirtual

Callback event called when a P2P connection succeeds.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnP2PFail()

void DiarkisP2PBase::OnP2PFail ( const DiarkisFailEventArgs args)
protectedvirtual

Callback event called when a hole punch fails.

Triggered when a hole punch is not completed within the specified time.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnP2PDisconnect()

void DiarkisP2PBase::OnP2PDisconnect ( const DiarkisP2PDisconnectEventArgs args)
protectedvirtual

Callback event called when a P2P connection is disconnected.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnP2PMessage()

void DiarkisP2PBase::OnP2PMessage ( const DiarkisMessageEventArgs args)
protectedvirtual

Callback event called when a message is received by P2P.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnP2PException()

void DiarkisP2PBase::OnP2PException ( const DiarkisExceptionEventArgs args)
protectedvirtual

Callback event called when sending or receiving fails in P2P.

It is triggered when the socket is closed and no transmission or reception is possible.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterPeer()

void DiarkisP2PBase::RegisterPeer ( const Diarkis::StdString uid,
const std::shared_ptr< Peer > &  p2p 
)
protected

Register an IDiarkisP2P instance.

Parameters
[in]uidUID of the peer
[in]p2pPointer to IDiarkisP2P instance
Here is the caller graph for this function:

◆ UnregisterPeer()

uint64_t DiarkisP2PBase::UnregisterPeer ( const Diarkis::StdString uid)
protected

Unregister an IDiarkisP2P instance.

Parameters
[in]uidUID of the peer
Returns
Number of deleted instances
Here is the caller graph for this function:

Member Data Documentation

◆ p2pList_

Diarkis::StdUnorderedMap<Diarkis::StdString, std::shared_ptr<Peer> > DiarkisP2PBase::p2pList_
protected

List of P2P connections. key:UID, value:pointer to IDiarkisP2P.

◆ p2pListMutex_

std::recursive_mutex DiarkisP2PBase::p2pListMutex_
protected

P2P list Mutex.

◆ udp_

std::shared_ptr<IDiarkisUdp> DiarkisP2PBase::udp_
protected

Pointer to the IDiarkisUdp class.

◆ loggerFactory_

std::shared_ptr<LoggerFactory> DiarkisP2PBase::loggerFactory_
protected

Pointer to logger class.

◆ logger_

std::shared_ptr<IDiarkisLogger> DiarkisP2PBase::logger_
protected

Pointer to logger class.

◆ schedulerAddNotifyEventFunc_

std::function<void()> DiarkisP2PBase::schedulerAddNotifyEventFunc_
protected

Callback function called when P2P-related events are added.

◆ ownGlobalAddress_

Diarkis::StdString DiarkisP2PBase::ownGlobalAddress_
protected

My Global IP address.

◆ myUid_

Diarkis::StdString DiarkisP2PBase::myUid_
protected

My user ID.


The documentation for this class was generated from the following files: