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.h
Go to the documentation of this file.
1
6
7
8#ifndef _CLIENT_H
9#define _CLIENT_H
10
11#include <NSC.h>
12#include "GroupManager.h"
13#include "Serializer.h"
14#include <string>
15#include <KISS.h>
16#include <memory>
17#include <vector>
18#include <iostream>
19#include <sstream>
20
32
33enum class ClientType {
38};
39
40class ClientPP;
41
43typedef struct ClientData {
45 EventType type;
47 ConnType connType;
49 IP_Type ipType;
51 SOCKET socket;
53 SIN addr;
55 std::shared_ptr<ClientPP> client;
57 std::vector<std::string> data;
59
61template <typename T>
62auto createDataCallback(T* ptr, void (T::* method)(std::vector<std::string>)) {
63 return [ptr, method](std::vector<std::string> v) {
64 (ptr->*method)(v);
65 };
66}
67
69template <typename T>
70auto createDisconnectionCallback(T* ptr, void (T::* method)()) {
71 return [ptr, method]() {
72 (ptr->*method)();
73 };
74}
75
81void sendUDP(SOCKET* serverSocket, SIN* addr, IP_Type ipType, std::string message);
82
86std::string domainNameResolution(std::string domainName);
87
88class ClientPP {
89public:
94 ClientPP(Client clientInfo);
95
104 ClientPP(const std::string& address, int port, const std::string& separator, IP_Type ipType = IP_Type::IPv4, bool useCiphering = true);
105
110
118 void send(const std::string& data, ConnType connType = ConnType::TCP);
119
125 const std::string& getID();
126
130 void kick();
131
135 const Client& getClientStruct();
136
141 void start();
142
150 void addDataCallback(const std::string& key, std::function<void(std::vector<std::string>)> callback);
151
157 void addDisconnectionCallback(std::function<void(void)> callback);
158
165
171 void stop();
172
177 void setSharedSecret(const std::string& PK);
178
183 std::string decrypt(const std::string& cipher);
184
191 void setKeysPaths(const std::string& PKFile, const std::string& SKFile);
192
198 void setServerPKPath(const std::string& SPKFile);
199
200protected:
205 std::shared_ptr<SymmetricCipherInterface> symmetricCipher;
206
211 std::unique_ptr<KeyExchangeInterface> kxInterface;
212
214 Client* clientTCP;
215
217 Client* clientUDP;
218
223
227 std::string ID;
228
233
237 std::stop_source stopTag;
238
243 void run(std::stop_token st);
244
249 void init();
250
255
260
262 std::jthread runThread;
263
265 std::string address;
267 int port;
269 IP_Type ipType;
270
275 std::unordered_map<std::string, std::function<void(std::vector<std::string>)>> dataRcvCB;
276
280 std::vector<std::function<void(void)>> disconnCB;
281
285 std::string PKFile, SKFile;
286
290 std::string ServerPKFile;
291};
292
293#endif //_CLIENT_H
auto createDisconnectionCallback(T *ptr, void(T::*method)())
Template which can be used to create Disconnection callbacks from class' methods.
Definition ClientPP.h:70
void sendUDP(SOCKET *serverSocket, SIN *addr, IP_Type ipType, std::string message)
Function to send an UDP message to a client.
std::string domainNameResolution(std::string domainName)
Wrapper of the resolveDomainName function of NSC, with strings.
auto createDataCallback(T *ptr, void(T::*method)(std::vector< std::string >))
Template which can be used to create DataReceived callbacks from class' methods.
Definition ClientPP.h:62
ClientType
Definition ClientPP.h:33
@ LOCAL
Client-Side.
Definition ClientPP.h:35
@ REMOTE
Server-Side.
Definition ClientPP.h:37
Definition ClientPP.h:88
bool isDisconnected
Boolean which is used to stop using the client when kicked.
Definition ClientPP.h:232
void addDisconnectionCallback(std::function< void(void)> callback)
Add a callback for the Disconnection event.
bool haveSecret
Tells if the secret has been computed.
Definition ClientPP.h:254
ClientType type
Information of the client's type REMOTE or LOCAL.
Definition ClientPP.h:222
void addDataCallback(const std::string &key, std::function< void(std::vector< std::string >)> callback)
Add a callback for the DataReceived event.
void setServerPKPath(const std::string &SPKFile)
Set the server's public key for the [LOCAL] client.
int port
Server's port.
Definition ClientPP.h:267
void init()
Init the different informations of the client.
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.
~ClientPP()
Destructor of the ClientPP class.
bool getHaveSecret()
Return the haveSecret boolean value.
Client * clientTCP
TCP client pointer.
Definition ClientPP.h:214
void setSharedSecret(const std::string &PK)
Let you set the public key to compute the shared secret.
void start()
Connect the client and start the run loop.
IP_Type ipType
Session's IP type (IPv4 or IPv6)
Definition ClientPP.h:269
const Client & getClientStruct()
Return a reference to the client structure.
std::shared_ptr< SymmetricCipherInterface > symmetricCipher
Have the information of the symmetricKey used for the communication between the Server and the Client...
Definition ClientPP.h:205
std::string ServerPKFile
Path to the server Public Key file.
Definition ClientPP.h:290
std::string SKFile
Definition ClientPP.h:285
std::string PKFile
Paths to the public and private keys.
Definition ClientPP.h:285
std::vector< std::function< void(void)> > disconnCB
This map register the callback for the Disconnection's events.
Definition ClientPP.h:280
Client * clientUDP
UDP client pointer.
Definition ClientPP.h:217
std::stop_source stopTag
This parameter is used to stop the run loop.
Definition ClientPP.h:237
const std::string & getID()
Return the Client's identity string.
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(Client clientInfo)
Constructor of the REMOTE client.
std::unordered_map< std::string, std::function< void(std::vector< std::string >)> > dataRcvCB
This map register the callback for the DataReceived's events.
Definition ClientPP.h:275
void setKeysPaths(const std::string &PKFile, const std::string &SKFile)
Set the public and private keys for the [LOCAL] client.
std::unique_ptr< KeyExchangeInterface > kxInterface
Key used for the ECDH symmetricKey exchange.
Definition ClientPP.h:211
std::string decrypt(const std::string &cipher)
Return the decrypted string of the given cipher.
void run(std::stop_token st)
Main loop of the client, it listens to the events and process them.
bool useCiphering
Tells if the client use ciphering.
Definition ClientPP.h:259
void kick()
Kick the client by closing its socket.
void stop()
Stop the client by terminating its run loop and disconnect it from the server.
std::string address
Server's address.
Definition ClientPP.h:265
std::jthread runThread
Run thread.
Definition ClientPP.h:262
std::string ID
String "address:port".
Definition ClientPP.h:227
Structure which hold the Client's Data after an event.
Definition ClientPP.h:43
std::shared_ptr< ClientPP > client
Shared pointer of the client which produced the event (nullptr for UDP).
Definition ClientPP.h:55
EventType type
Type of the event : Connection, Disconnection, DataReceived.
Definition ClientPP.h:45
std::vector< std::string > data
Vector with the string obtained after a split on the separator.
Definition ClientPP.h:57
IP_Type ipType
IP type : IPv4 or IPv6.
Definition ClientPP.h:49
SIN addr
Client's address.
Definition ClientPP.h:53
SOCKET socket
In TCP -> Client's socket; in UDP -> Server's socket.
Definition ClientPP.h:51
ConnType connType
Connection type of this event : TCP or UDP.
Definition ClientPP.h:47