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
Channel.h
Go to the documentation of this file.
1
7
8#ifndef _CHANNEL_H
9#define _CHANNEL_H
10
11#include <NSC.h>
12#include "ClientPP.h"
13#include <queue>
14#include <shared_mutex>
15
18
20enum class ChannelType {
24 DATA = 1 << 0,
26 METADATA = 1 << 1,
28 GLOBAL = 1 << 2
29};
30
33 return static_cast<ChannelType>(static_cast<int>(a) | static_cast<int>(b));
34}
35
37inline bool hasType(ChannelType channelType, ChannelType flag) {
38 return (static_cast<int>(channelType) & static_cast<int>(flag)) != 0;
39}
40
41class Channel {
42public:
48 Channel(const std::string& key);
49
55 void setKey(const std::string& key);
56
60 const std::string& getKey();
61
69
75 void push(ClientData data);
76
82
86 bool available();
87
92 virtual void run(std::stop_token st) = 0;
93
94protected:
98 std::string key;
99
103 std::queue<ClientData> dataQueue;
104
109
110private:
111 std::shared_mutex mtx; // .lock_shared() for read-only access | .lock() to write access
112};
113
114#endif //_CHANNEL_H
ChannelType
Can be used with | operator to combine them.
Definition Channel.h:20
@ GLOBAL
Bypass the key system and get every message send to the server.
Definition Channel.h:28
@ SLEEPING
Sleeping Channel (can't receive any message)
Definition Channel.h:22
@ METADATA
Receive METADATA on the communication (Conn., Disconn.)
Definition Channel.h:26
@ DATA
Receive the message send by a client.
Definition Channel.h:24
bool hasType(ChannelType channelType, ChannelType flag)
Let you verify if a Channel have a given type easily.
Definition Channel.h:37
ChannelState
Channel's state, tells if the channel can receive data.
Definition Channel.h:17
@ Inactive
Definition Channel.h:17
@ Active
Definition Channel.h:17
ChannelType operator|(ChannelType a, ChannelType b)
Used to combine types.
Definition Channel.h:32
Project NSCpp.
std::queue< ClientData > dataQueue
This queue is used to store (and read) the data send to the Channel by the Server.
Definition Channel.h:103
std::string key
This key must be unique on the server on which it is plugged.
Definition Channel.h:98
ClientData pull()
Pop the first data from the queue and return it.
virtual void run(std::stop_token st)=0
This method is run in a Thread and handle the Channel.
const std::string & getKey()
Return the Channel's key.
void push(ClientData data)
Add the given data at the end of the queue.
ChannelState state
Control if the channel is active or not.
Definition Channel.h:108
void setState(ChannelState state)
Change the state of the server.
bool available()
Return true if data is available inside the dataQueue, false otherwise.
void setKey(const std::string &key)
Defined the Channel's key.
Channel(const std::string &key)
Constructor of the Channel's class.
Structure which hold the Client's Data after an event.
Definition ClientPP.h:43