NSCpp 2.0
NSCpp is a modern C++20 server/client framework designed to simplify network programming in C++. With robust support for both IPv4 and IPv6, as well as TCP and UDP communication protocols.
 
Loading...
Searching...
No Matches
ClientPP Class Reference

#include <ClientPP.h>

Public Member Functions

 ClientPP (Client clientInfo)
 Constructor of the REMOTE client.
 
 ClientPP (const std::string &address, int port, const std::string &separator, IP_Type ipType=IP_Type::IPv4, bool useCiphering=true)
 Constructor of the LOCAL client.
 
 ~ClientPP ()
 Destructor of the ClientPP class.
 
void send (const std::string &data, ConnType connType=ConnType::TCP)
 This method can be used by both LOCAL and REMOTE client to exchange with the Server/Clients.
 
const std::string & getID ()
 Return the Client's identity string.
 
void kick ()
 Kick the client by closing its socket.
 
const Client & getClientStruct ()
 Return a reference to the client structure.
 
void start ()
 Connect the client and start the run loop.
 
void addDataCallback (const std::string &key, std::function< void(std::vector< std::string >)> callback)
 Add a callback for the DataReceived event.
 
void addDisconnectionCallback (std::function< void(void)> callback)
 Add a callback for the Disconnection event.
 
bool getHaveSecret ()
 Return the haveSecret boolean value.
 
void stop ()
 Stop the client by terminating its run loop and disconnect it from the server.
 
void setSharedSecret (const std::string &PK)
 Let you set the public key to compute the shared secret.
 
std::string decrypt (const std::string &cipher)
 Return the decrypted string of the given cipher.
 
void setKeysPaths (const std::string &PKFile, const std::string &SKFile)
 Set the public and private keys for the [LOCAL] client.
 
void setServerPKPath (const std::string &SPKFile)
 Set the server's public key for the [LOCAL] client.
 

Protected Member Functions

void run (std::stop_token st)
 Main loop of the client, it listens to the events and process them.
 
void init ()
 Init the different informations of the client.
 

Protected Attributes

std::shared_ptr< SymmetricCipherInterface > symmetricCipher
 Have the information of the symmetricKey used for the communication between the Server and the Client.
 
std::unique_ptr< KeyExchangeInterface > kxInterface
 Key used for the ECDH symmetricKey exchange.
 
Client * clientTCP
 TCP client pointer.
 
Client * clientUDP
 UDP client pointer.
 
ClientType type
 Information of the client's type REMOTE or LOCAL.
 
std::string ID
 String "address:port".
 
bool isDisconnected
 Boolean which is used to stop using the client when kicked.
 
std::stop_source stopTag
 This parameter is used to stop the run loop.
 
bool haveSecret
 Tells if the secret has been computed.
 
bool useCiphering
 Tells if the client use ciphering.
 
std::jthread runThread
 Run thread.
 
std::string address
 Server's address.
 
int port
 Server's port.
 
IP_Type ipType
 Session's IP type (IPv4 or IPv6)
 
std::unordered_map< std::string, std::function< void(std::vector< std::string >)> > dataRcvCB
 This map register the callback for the DataReceived's events.
 
std::vector< std::function< void(void)> > disconnCB
 This map register the callback for the Disconnection's events.
 
std::string PKFile
 Paths to the public and private keys.
 
std::string SKFile
 
std::string ServerPKFile
 Path to the server Public Key file.
 

Constructor & Destructor Documentation

◆ ClientPP() [1/2]

ClientPP::ClientPP ( Client clientInfo)

Constructor of the REMOTE client.

Parameters
clientInfoClient structure with the necessary information.

◆ ClientPP() [2/2]

ClientPP::ClientPP ( const std::string & address,
int port,
const std::string & separator,
IP_Type ipType = IP_Type::IPv4,
bool useCiphering = true )

Constructor of the LOCAL client.

Parameters
addressServer's address.
portServer's port.
separatorSeparator used during this session.
ipTypeIP type (IPv4 or IPv6) of this connection.
useCipheringIndication about the usage of ciphering in this session.

◆ ~ClientPP()

ClientPP::~ClientPP ( )

Destructor of the ClientPP class.

Member Function Documentation

◆ addDataCallback()

void ClientPP::addDataCallback ( const std::string & key,
std::function< void(std::vector< std::string >)> callback )

Add a callback for the DataReceived event.

Each time a message with the given key as prefix is received, the callback will be called with the received data as parameter.

Key is unique, but you can create a callback which will propagate the information.

Parameters
keyPrefix which will trigger the callback method. Should be unique.
callbackCallback of the following form void(vector<string>).

◆ addDisconnectionCallback()

void ClientPP::addDisconnectionCallback ( std::function< void(void)> callback)

Add a callback for the Disconnection event.

Called when a disconnection happen.

Parameters
callbackCallback of the following form void(void).

◆ decrypt()

std::string ClientPP::decrypt ( const std::string & cipher)

Return the decrypted string of the given cipher.

Parameters
cipherCipher to decrypt.

◆ getClientStruct()

const Client & ClientPP::getClientStruct ( )

Return a reference to the client structure.

◆ getHaveSecret()

bool ClientPP::getHaveSecret ( )

Return the haveSecret boolean value.

haveSecret is true when the shared secret has been compute.

It does not tell you that the secret is the same between the server and the client.

◆ getID()

const std::string & ClientPP::getID ( )

Return the Client's identity string.

Which is "address:port".

Warning
Is an ID only locally, client-side or server-side, not accross the network.

◆ init()

void ClientPP::init ( )
protected

Init the different informations of the client.

Its ID and KISS classes when needed.

◆ kick()

void ClientPP::kick ( )

Kick the client by closing its socket.

◆ run()

void ClientPP::run ( std::stop_token st)
protected

Main loop of the client, it listens to the events and process them.

Parameters
stStop token used to stop the loop of the thread.

◆ send()

void ClientPP::send ( const std::string & data,
ConnType connType = ConnType::TCP )

This method can be used by both LOCAL and REMOTE client to exchange with the Server/Clients.

Warning
For REMOTE clients, don't use this method for UDP communications.
See also
sendUDP to answer with UDP.
Parameters
dataData to send to the peer (client or server)
connTypeType of connection to be used for the message, could be ConnType::TCP or ConnType::UDP (default : ConnType::TCP)

◆ setKeysPaths()

void ClientPP::setKeysPaths ( const std::string & PKFile,
const std::string & SKFile )

Set the public and private keys for the [LOCAL] client.

Warning
Only available for the LOCAL client.
Parameters
PKFilePath to the public key's file.
SKFilePath to the secret key's file.

◆ setServerPKPath()

void ClientPP::setServerPKPath ( const std::string & SPKFile)

Set the server's public key for the [LOCAL] client.

Warning
Only available for the LOCAL client.
Parameters
SPKFilePath to the server's public key file

◆ setSharedSecret()

void ClientPP::setSharedSecret ( const std::string & PK)

Let you set the public key to compute the shared secret.

The given string will be considered as the peer's public key and use to compute the shared secret.

◆ start()

void ClientPP::start ( )

Connect the client and start the run loop.

Method for the LOCAL client which handles the connection and start the run method in a jthread.

◆ stop()

void ClientPP::stop ( )

Stop the client by terminating its run loop and disconnect it from the server.

Warning
Only available on LOCAL clients.

Also, properly clear clientTCP and clientUDP.

Member Data Documentation

◆ address

std::string ClientPP::address
protected

Server's address.

◆ clientTCP

Client* ClientPP::clientTCP
protected

TCP client pointer.

◆ clientUDP

Client* ClientPP::clientUDP
protected

UDP client pointer.

◆ dataRcvCB

std::unordered_map<std::string, std::function<void(std::vector<std::string>)> > ClientPP::dataRcvCB
protected

This map register the callback for the DataReceived's events.

"" is the default callback, called when no key is detected, if it does not exist the message is just ignored.

◆ disconnCB

std::vector<std::function<void(void)> > ClientPP::disconnCB
protected

This map register the callback for the Disconnection's events.

◆ haveSecret

bool ClientPP::haveSecret
protected

Tells if the secret has been computed.

◆ ID

std::string ClientPP::ID
protected

String "address:port".

◆ ipType

IP_Type ClientPP::ipType
protected

Session's IP type (IPv4 or IPv6)

◆ isDisconnected

bool ClientPP::isDisconnected
protected

Boolean which is used to stop using the client when kicked.

◆ kxInterface

std::unique_ptr<KeyExchangeInterface> ClientPP::kxInterface
protected

Key used for the ECDH symmetricKey exchange.

Both the public key and the private key are stored inside the KXInterface from KISS.

◆ PKFile

std::string ClientPP::PKFile
protected

Paths to the public and private keys.

◆ port

int ClientPP::port
protected

Server's port.

◆ runThread

std::jthread ClientPP::runThread
protected

Run thread.

◆ ServerPKFile

std::string ClientPP::ServerPKFile
protected

Path to the server Public Key file.

◆ SKFile

std::string ClientPP::SKFile
protected

◆ stopTag

std::stop_source ClientPP::stopTag
protected

This parameter is used to stop the run loop.

◆ symmetricCipher

std::shared_ptr<SymmetricCipherInterface> ClientPP::symmetricCipher
protected

Have the information of the symmetricKey used for the communication between the Server and the Client.

Server-side : keep the information for the server.

◆ type

ClientType ClientPP::type
protected

Information of the client's type REMOTE or LOCAL.

◆ useCiphering

bool ClientPP::useCiphering
protected

Tells if the client use ciphering.


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