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
ServerPP.h
Go to the documentation of this file.
1
6
7#ifndef _SERVER_H
8#define _SERVER_H
9
10#include "Channel.h"
11#include <unordered_map>
12#include <unordered_set>
13#include <sstream>
14#include <iostream>
15#include <KISS.h>
16#include "ClientPP.h"
17#include <filesystem>
18
26
27class ServerPP {
28public:
41 ServerPP(std::string address, int port, std::string separator, IP_Type ipType = IP_Type::IPv4, bool useCiphering = true, const std::string& publicKeyFile = "", const std::string& privateKeyFile = "");
42
47
52 void start();
53
58 void stop();
59
72 void plug(std::shared_ptr<Channel> channel, ChannelType type);
73
80 void unplug(const std::string& key);
81
85 std::vector<std::string> getChannelKeys();
86
94 void setChannelState(const std::string& key, const ChannelState& state);
95
102 void kicked(const std::string& clientID);
103
104private:
109 std::unique_ptr<KeyExchangeInterface> kxInterface;
110
117 std::unordered_map<std::string, std::pair<ChannelType, std::shared_ptr<Channel>>> channels;
118
123 std::unordered_map<std::string, std::stop_source> stopSources;
124
131 std::unordered_map<std::string, std::shared_ptr<ClientPP>> clients;
132
136 std::stop_source stopTag;
137
139 Server *serverTCP, *serverUDP;
140
142 std::string address;
143
145 int port;
146
148 IP_Type ipType;
149
153 std::string separator;
154
158 bool useCiphering;
159
161 std::jthread runThread;
162
164 std::vector<std::jthread> channelsThreads;
165
172 void process(ServerEvent& event, const ConnType& connType);
173
178 void run(std::stop_token st);
179
180};
181
182#endif //_SERVER_H
Project NSCpp.
ChannelType
Can be used with | operator to combine them.
Definition Channel.h:20
ChannelState
Channel's state, tells if the channel can receive data.
Definition Channel.h:17
Project NSCpp.
~ServerPP()
Destructor of the ServerPP class.
void setChannelState(const std::string &key, const ChannelState &state)
Update a channel state through the server.
void start()
Create the servers (TCP and UDP) with the given parameters and start the run method.
void stop()
Stop the server, disconnect properly every clients and clear everything.
void unplug(const std::string &key)
Unplug a channel based on its key.
std::vector< std::string > getChannelKeys()
Return a list which contains every plugged channel's keys.
void plug(std::shared_ptr< Channel > channel, ChannelType type)
Plug a new channel to the server and start it in a jthread.
void kicked(const std::string &clientID)
Callback used to tell the server you kicked a client.
ServerPP(std::string address, int port, std::string separator, IP_Type ipType=IP_Type::IPv4, bool useCiphering=true, const std::string &publicKeyFile="", const std::string &privateKeyFile="")
Constructor of the ServerPP.