93#include<jsoncpp/json/json.h>
109#include<openssl/sha.h>
113#include<sys/socket.h>
115#include<openssl/bio.h>
116#include<openssl/evp.h>
117#include<openssl/buffer.h>
122#include<condition_variable>
124#include<unordered_map>
125#include <openssl/ssl.h>
126#include <openssl/err.h>
127#include<openssl/crypto.h>
136#include <sys/eventfd.h>
137#include <sys/timerfd.h>
151#include <unordered_set>
154#include <netinet/tcp.h>
156#include <sys/prctl.h>
157#include <sys/syscall.h>
170 inline constexpr std::string_view
version=
"0.7.0";
207 : capacity_(capacity_pow2),
208 mask_(capacity_pow2 - 1),
209 buffer_(capacity_pow2),
213 static_assert(std::is_nothrow_move_constructible_v<T>,
214 "MPSCQueue requires a nothrow-move-constructible value type");
216 if (capacity_ < 2 || (capacity_ & mask_) != 0) {
218 throw std::invalid_argument(
"MPSCQueue capacity must be power of two and >= 2");
222 for (std::size_t i = 0; i < capacity_; ++i) {
223 buffer_[i].seq.store(i, std::memory_order_relaxed);
232 while (discard_one()) {}
239 bool push(T&& v)
noexcept(std::is_nothrow_move_constructible_v<T>) {
240 return emplace_impl(std::move(v));
247 return emplace_impl(std::move(copy));
254 bool pop(T& out)
noexcept(std::is_nothrow_move_assignable_v<T> &&
255 std::is_nothrow_move_constructible_v<T>)
257 const std::size_t head = head_.load(std::memory_order_relaxed);
258 Slot& slot = buffer_[head & mask_];
259 const std::size_t seq = slot.seq.load(std::memory_order_acquire);
260 const std::intptr_t dif =
static_cast<std::intptr_t
>(seq) -
static_cast<std::intptr_t
>(head + 1);
268 out = std::move(*slot.ptr());
275 slot.seq.store(head + capacity_, std::memory_order_release);
277 head_.store(head + 1, std::memory_order_relaxed);
286 const std::size_t t = tail_.load(std::memory_order_acquire);
287 const std::size_t h = head_.load(std::memory_order_acquire);
288 return (t >= h) ? (t - h) : 0;
296 return tail_.load(std::memory_order_acquire) !=
297 head_.load(std::memory_order_acquire);
302 std::atomic<std::size_t> seq;
303 typename std::aligned_storage<
sizeof(T),
alignof(T)>::type storage;
304 bool has_value =
false;
306 T* ptr() noexcept {
return reinterpret_cast<T*
>(&storage); }
307 const T* ptr() const noexcept {
return reinterpret_cast<const T*
>(&storage); }
310 void construct(U&& v)
noexcept(std::is_nothrow_constructible_v<T, U&&>) {
311 ::new (
static_cast<void*
>(&storage)) T(std::forward<U>(v));
315 void destroy() noexcept {
324 bool emplace_impl(U&& v)
noexcept(std::is_nothrow_constructible_v<T, U&&>) {
325 std::size_t pos = tail_.load(std::memory_order_relaxed);
328 Slot& slot = buffer_[pos & mask_];
329 const std::size_t seq = slot.seq.load(std::memory_order_acquire);
330 const std::intptr_t dif =
static_cast<std::intptr_t
>(seq) -
static_cast<std::intptr_t
>(pos);
334 if (tail_.compare_exchange_weak(
336 std::memory_order_relaxed,
337 std::memory_order_relaxed))
340 slot.construct(std::forward<U>(v));
342 slot.seq.store(pos + 1, std::memory_order_release);
346 }
else if (dif < 0) {
351 pos = tail_.load(std::memory_order_relaxed);
356 bool discard_one() noexcept {
357 const std::size_t head = head_.load(std::memory_order_relaxed);
358 Slot& slot = buffer_[head & mask_];
359 const std::size_t seq = slot.seq.load(std::memory_order_acquire);
360 if (
static_cast<std::intptr_t
>(seq) -
static_cast<std::intptr_t
>(head + 1) != 0)
363 slot.seq.store(head + capacity_, std::memory_order_release);
364 head_.store(head + 1, std::memory_order_relaxed);
369 const std::size_t capacity_;
370 const std::size_t mask_;
371 std::vector<Slot> buffer_;
374 alignas(64) std::atomic<std::size_t> head_;
377 alignas(64) std::atomic<std::size_t> tail_;
402 static bool createDir(
const std::string & ddir,
const mode_t &mode=0775);
411 static bool copy(
const std::string &sourceFile,
const std::string &objectFile);
420 static bool createFile(
const std::string &filePath,
const mode_t &mode=0666);
465 static std::mutex
l1;
466 static std::unordered_map<std::string,FileThreadLock>
fl2;
473 bool memoryLockOwnedByCurrentThread();
474 void markMemoryLockOwned();
475 void releaseMemoryLocks();
478 std::vector<std::string> data;
479 std::vector<std::string> backUp;
480 char *data_binary=
nullptr;
481 char *backUp_binary=
nullptr;
485 size_t multiple_backup=0;
488 std::mutex memoryOwnerMutex;
489 std::thread::id memoryLockOwner{};
490 bool memoryLocked=
false;
493 std::string fileName;
495 std::string fileNameTemp;
496 std::atomic<bool> flag{
false};
500 uint64_t totalLines=0;
545 bool openFile(
const std::string &fileName,
const bool &create=
true,
const int &multiple=0,
const size_t &size=0,
const mode_t &mode=0666);
562 bool isOpen(){
return flag.load(std::memory_order_acquire);}
631 int findC(
const std::string &targetString,
const int linePos=1);
667 bool chgLineC(
const std::string &data,
const int &linePos=0);
687 std::string&
readC(std::string &data,
const int &linePos,
const int &num);
710 bool readC(
char *data,
const size_t &pos,
const size_t &size);
721 bool writeC(
const char *data,
const size_t &pos,
const size_t &size);
742 int find(
const std::string &targetString,
const int linePos=1);
752 bool appendLine(
const std::string &data,
const int &linePos=0);
778 bool chgLine(
const std::string &data,
const int &linePos=0);
788 bool readLine(std::string &data,
const int linePos);
798 std::string&
read(std::string &data,
const int &linePos,
const int &num);
821 bool read(
char *data,
const size_t &pos,
const size_t &size);
832 bool write(
const char *data,
const size_t &pos,
const size_t &size);
1003 return Duration(dayy,hourr,minn,secc,msecc);
1018 msecc=dayy*24*60*60*1000+hourr*60*60*1000+minn*60*1000+secc*1000+msecc-b.
day*24*60*60*1000-b.
hour*60*60*1000-b.
min*60*1000-b.
sec*1000-b.
msec;
1047 return Duration(dayy,hourr,minn,secc,msecc);
1057 double k=
day+total/86400000.0000;
1067 double k=
day*24+
hour+total/36000000.0000;
1077 double k=
day*24*60+
hour*60+
min+total/60000.0000;
1087 double k=
day*24*60*60+
hour*60*60+
min*60+
sec+total/1000.0000;
1156 #define ISO8086A "yyyy-mm-ddThh:mi:ss"
1160 #define ISO8086B "yyyy-mm-ddThh:mi:ss.sss"
1174 static std::string &toPGtimeFormat();
1175 static std::chrono::system_clock::time_point strToTimePoint(
const std::string &timeStr,
const std::string &format=
ISO8086A);
1176 static std::string& timePointToStr(
const std::chrono::system_clock::time_point &tp,std::string &timeStr,
const std::string &format=
ISO8086A);
1229 std::chrono::steady_clock::time_point start;
1230 std::chrono::steady_clock::time_point end;
1271 std::string timeFormat;
1272 std::string contentFormat;
1273 std::atomic<bool> consumerGuard{
true};
1274 std::atomic<bool> logWakePending{
false};
1275 std::atomic<uint64_t> droppedLogs{0};
1276 std::mutex queueMutex;
1277 std::condition_variable queueCV;
1279 std::thread consumerThread;
1301 LogFile(
const size_t &logQueue_cap=8192):logQueue(logQueue_cap)
1304 consumerThread = std::thread([
this]()->
void
1306 std::string content;
1307 content.reserve(1024);
1312 while(this->logQueue.
pop(content))
1315 time+=contentFormat;
1319 this->logWakePending.store(
false,std::memory_order_release);
1322 this->logWakePending.store(
true,std::memory_order_release);
1325 if (!this->consumerGuard.load(std::memory_order_acquire))
1327 std::unique_lock<std::mutex> lock(this->queueMutex);
1328 this->queueCV.wait(lock,[
this] {
1329 return !this->consumerGuard.load(std::memory_order_acquire)||
1330 this->logWakePending.load(std::memory_order_acquire);
1343 bool openFile(
const std::string &fileName,
const std::string &timeFormat=
ISO8086A,
const std::string &contentFormat=
" ");
1412 static bool encryptSymmetric(
const unsigned char *before,
const size_t &length,
const unsigned char *passwd,
const unsigned char *iv,
unsigned char *after);
1423 static bool decryptSymmetric(
const unsigned char *before,
const size_t &length,
const unsigned char *passwd,
const unsigned char *iv,
unsigned char *after);
1436 static std::string&
sha1(
const std::string &ori_str,std::string &result);
1449 static std::string&
sha11(
const std::string &ori_str,std::string &result);
1464 static std::string&
bitOutput(
char input,std::string &result);
1472 static std::string&
bitOutput(
const std::string &input,std::string &result);
1489 static unsigned long&
bitStrToNumber(
const std::string &input,
unsigned long &result);
1499 static unsigned long&
bitToNumber(
const std::string &input,
unsigned long &result);
1507 static char&
toBit(
const std::string &input,
char &result);
1515 static std::string&
toBit(
const std::string &input,std::string &result);
1650 static size_t get_split_str(
const std::string_view& ori_str,std::string_view &str,
const std::string_view &a,
const std::string_view &b,
const size_t &pos=0);
1661 static std::string_view&
get_value_str(
const std::string_view& ori_str,std::string_view &str,
const std::string& name);
1670 static std::string_view&
get_value_header(
const std::string_view& ori_str,std::string_view &str,
const std::string& name);
1680 static std::string_view&
get_location_str(
const std::string_view& ori_str,std::string_view &str);
1690 static std::string_view&
getLocPara(
const std::string_view &url,std::string_view &locPara);
1699 static std::string_view&
getPara(
const std::string_view &url,std::string_view ¶);
1718 static size_t get_split_str(
const std::string_view& ori_str,std::string &str,
const std::string_view &a,
const std::string_view &b,
const size_t &pos=0);
1729 static std::string&
get_value_str(
const std::string& ori_str,std::string &str,
const std::string& name);
1738 static std::string&
get_value_header(
const std::string& ori_str,std::string &str,
const std::string& name);
1758 static std::string&
getLocPara(
const std::string &url,std::string &locPara);
1767 static std::string&
getPara(
const std::string &url,std::string ¶);
1778 static std::string&
getIP(
const std::string &url,std::string &IP);
1789 static int&
getPort(
const std::string &url,
int &port);
1799 static std::string
createHeader(
const std::string& first,
const std::string& second);
1820 template<
class... Args>
1821 static std::string
createHeader(
const std::string& first,
const std::string& second,Args... args)
1823 std::string cf=first+
": "+second+
"\r\n"+
createHeader(args...);
1860 static int&
toInt(
const std::string_view&ori_str,
int &result,
const int &i=-1);
1868 static int&
str16toInt(
const std::string_view&ori_str,
int &result,
const int &i=-1);
1877 static long&
toLong(
const std::string_view&ori_str,
long &result,
const long &i=-1);
1886 static float&
toFloat(
const std::string&ori_str,
float &result,
const float &i=-1);
1895 static double&
toDouble(
const std::string&ori_str,
double &result,
const double &i=-1);
1903 static bool&
toBool(
const std::string_view&ori_str,
bool &result);
1914 static std::string&
strto16(
const std::string &ori_str,std::string &result);
1996 static int getValue(
const std::string &oriStr,std::string& result,
const std::string &type=
"value",
const std::string &name=
"a",
const int &num=0);
2019 template<
class T1,
class T2>
2024 if constexpr (std::is_integral_v<T2>) {
2025 root[first] = Json::Value(
static_cast<Json::Int64
>(second));
2027 root[first] = second;
2029 Json::StreamWriterBuilder builder;
2030 std::string jsonString=Json::writeString(builder,root);
2044 template<
class T1,
class T2,
class... Args>
2049 if constexpr (std::is_integral_v<T2>) {
2050 root[first] = Json::Value(
static_cast<Json::Int64
>(second));
2052 root[first] = second;
2055 Json::StreamWriterBuilder builder;
2056 std::string jsonString=Json::writeString(builder,root);
2057 jsonString=jsonString.erase(jsonString.length()-2);
2059 return jsonString+
","+kk;
2072 Json::Value root(Json::arrayValue);
2074 Json::StreamWriterBuilder builder;
2075 std::string jsonString=Json::writeString(builder,root);
2087 template<
class T,
class... Args>
2090 Json::Value root(Json::arrayValue);
2093 Json::StreamWriterBuilder builder;
2094 std::string jsonString=Json::writeString(builder,root);
2095 jsonString=jsonString.erase(jsonString.length()-2);
2097 return jsonString+
","+kk;
2106 static std::string
jsonAdd(
const std::string &a,
const std::string &b);
2120 static std::string&
jsonToUTF8(
const std::string &input,std::string &output);
2207 std::deque<std::chrono::steady_clock::time_point>
history;
2264 std::unordered_map<int, ConnectionState>
conns;
2390 ConnectionLimiter(
const int& maxConn = 20,
const int& idleTimeout = 60) : maxConnections(maxConn),connectionTimeout(idleTimeout){}
2428 void setPathLimit(
const std::string &path,
const int ×,
const int &secs);
2475 void clearIP(
const std::string &ip,
const int &fd);
2520 void banIP(
const std::string &ip,
int banSeconds,
const std::string &reasonCN,
const std::string &reasonEN);
2533 bool allow(
RateState &st,
const RateLimitType &type,
const int ×,
const int &secs,
const std::chrono::steady_clock::time_point &now);
2537 int connectionTimeout;
2543 std::unordered_map<std::string, IPInformation> table;
2544 std::unordered_map<std::string, std::pair<int,int>> pathConfig;
2546 std::unordered_map<std::string,std::chrono::steady_clock::time_point> blacklist;
2547 inline void logSecurity(
const std::string &msgCN,
const std::string &msgEN);
2679 int sendData(
const char *
data,
const uint64_t &length,
const bool &block=
true);
2747 std::string serverIP=
"";
2751 SSL_CTX *ctx=
nullptr;
2758 void closeAndUnCreate();
2759 bool initCTX(
const char *ca,
const char *cert=
"",
const char *key=
"",
const char *passwd=
"");
2773 TcpClient(
const bool &TLS=
false,
const char *ca=
"",
const char *cert=
"",
const char *key=
"",
const char *passwd=
"");
2781 bool connect(
const std::string &ip,
const int &port);
2795 void resetCTX(
const bool &TLS=
false,
const char *ca=
"",
const char *cert=
"",
const char *key=
"",
const char *passwd=
"");
2847 HttpClient(
const bool &TLS=
false,
const char *ca=
"",
const char *cert=
"",
const char *key=
"",
const char *passwd=
""):
TcpClient(TLS,ca,cert,key,passwd){}
2861 bool getRequest(
const std::string &url,
const std::string &
header=
"",
const std::string &header1=
"Connection: keep-alive",
const int &
sec=-1);
2875 bool postRequest(
const std::string &url,
const std::string &
body=
"",
const std::string &
header=
"",
const std::string &header1=
"Connection: keep-alive",
const int &
sec=-1);
2890 bool getRequestFromFD(
const int &
fd,SSL *
ssl,
const std::string &url,
const std::string &
header=
"",
const std::string &header1=
"Connection: keep-alive",
const int &
sec=2);
2906 bool postRequestFromFD(
const int &
fd,SSL *
ssl,
const std::string &url,
const std::string &
body=
"",
const std::string &
header=
"",
const std::string &header1=
"Connection: keep-alive",
const int &
sec=2);
2932 std::function<bool(
const int &fd)> fc=[](
const int &)->
bool
2934 std::function<void(
const int &fd)> fcEnd=[](
const int &)->
void
2936 std::function<bool(
const int &fd)> fcTimeOut=[](
const int &)->
bool
2938 std::atomic<bool> flag1{
true};
2939 std::atomic<bool> flag2{
false};
2941 std::atomic<bool> flag3{
false};
2943 std::thread listenerThread;
2945 std::mutex callbackMutex;
2946 std::mutex countdownMutex;
2964 bool isListen(){
return flag2.load(std::memory_order_acquire);}
2974 void setFunction(std::function<
bool(
const int &fd)> fc){std::lock_guard<std::mutex> lock(callbackMutex);this->fc=std::move(fc);}
2982 void setEndFunction(std::function<
void(
const int &fd)> fcEnd){std::lock_guard<std::mutex> lock(callbackMutex);this->fcEnd=std::move(fcEnd);};
2992 void setTimeOutFunction(std::function<
bool(
const int &fd)> fcTimeOut){std::lock_guard<std::mutex> lock(callbackMutex);this->fcTimeOut=std::move(fcTimeOut);};
3012 std::lock_guard<std::mutex> lock(countdownMutex);
3014 flag3.store(
true,std::memory_order_release);
3033 {std::cout<<
"收到: "<<message<<std::endl;
return true;};
3052 WebSocketClient(
const bool &TLS=
false,
const char *ca=
"",
const char *cert=
"",
const char *key=
"",
const char *passwd=
""):
TcpClient(TLS,ca,cert,key,passwd){}
3070 bool connect(
const std::string &url,
const int &min=20);
3093 bool sendMessage(
const std::string &message,
const std::string &type=
"0001");
3103 void close(
const std::string &closeCodeAndMessage,
const bool &wait=
true);
3121 void close(
const short &code=1000,
const std::string &message=
"bye",
const bool &wait=
true);
3199 std::unordered_map<std::string,std::any>
ctx;
3253 bool sendBack(
const std::string &
data,
const std::string &
header=
"",
const std::string &code=
"200 OK",
const std::string &header1=
"");
3263 const std::string &contentType=
"text/plain; charset=utf-8",
3264 const std::string &extraHeaders=
"");
3270 const std::string &extraHeaders=
"");
3277 bool redirect(
const std::string &location,
const std::string &code=
"302 Found");
3289 bool sendBack(
const char *
data,
const size_t &length,
const char *
header=
"\0",
const char *code=
"200 OK\0",
const char *header1=
"\0",
const size_t &header_length=50);
3355 std::unordered_map<std::string,std::any>
ctx;
3382 std::unordered_map<std::string,std::any>
ctx;
3653 std::function<void(
const int &fd)> closeFun=[](
const int &)->
void
3657 std::function<void(TcpFDHandler &k,TcpInformation &inf)> securitySendBackFun=[](TcpFDHandler &,TcpInformation &)->
void
3659 std::function<bool(TcpFDHandler &k,TcpInformation &inf)> globalSolveFun=[](TcpFDHandler &,TcpInformation &)->
bool
3661 std::unordered_map<std::string,std::vector<std::function<int(TcpFDHandler &k,TcpInformation &inf)>>> solveFun;
3662 std::function<int(TcpFDHandler &k,TcpInformation &inf)> parseKey=[](TcpFDHandler &,TcpInformation &inf)->
int
3663 {inf.ctx[
"key"]=inf.data;
return 1;};
3665 std::atomic<int> port{-1};
3666 std::atomic<bool> flag{
false};
3667 std::atomic<bool> flag2{
true};
3669 void epolll(
const int &evsNum,
const int &listenFD);
3671 virtual void handler_netevent(
const int &fd);
3672 virtual void handler_workerevent(WorkerMessage message);
3673 virtual void handleHeartbeat()=0;
3674 virtual void onConnectionClosed(
const int &fd) {(void)fd;}
3753 bool setTLS(
const char *cert,
const char *key,
const char *passwd,
const char *ca);
3761 bool setTLS(
const char *cert,
const char *key,
const char *passwd=
"");
3771 bool setTLS(
const char *cert,
const char *key,
const char *passwd,
const char *ca,
3807 auto [it, inserted] = solveFun.try_emplace(key);
3808 it->second.push_back(std::move(fc));
3881 void setCloseFun(std::function<
void(
const int &fd)> closeFun){this->closeFun=closeFun;}
3980 bool isListen(){
return flag.load(std::memory_order_acquire);}
3985 int getListenPort() const noexcept{
return port.load(std::memory_order_acquire);}
4016 {inf.
ctx[
"key"]=inf.
loc;
return 1;};
4019 std::unordered_map<int,HttpRequestInformation> httpinf;
4023 void handler_netevent(
const int &fd)
override;
4025 void handleHeartbeat()
override {}
4119 auto [it, inserted] = solveFun.try_emplace(key);
4120 it->second.push_back(std::move(fc));
4205 bool sendMessage(
const std::string &msg,
const std::string &type=
"0001");
4216 std::unordered_map<int,WebSocketFDInformation> wbclientfd;
4217 std::mutex websocketRegistryMutex;
4218 std::unordered_map<int,uint64_t> websocketConnections;
4219 std::unordered_set<int> websocketClosing;
4239 void handler_netevent(
const int &fd)
override;
4243 void closeAck(
const int &fd,
const std::string &closeCodeAndMessage);
4244 void closeAck(
const int &fd,
const short &code=1000,
const std::string &message=
"bye");
4246 void handleHeartbeat()
override;
4247 void onConnectionClosed(
const int &fd)
override;
4248 bool sendMessageForConnection(
const int &fd,
const uint64_t connection,
const std::string &msg,
const std::string &type);
4249 bool closeWithoutLock(
const int &fd,
const std::string &closeCodeAndMessage);
4250 bool closeWithoutLock(
const int &fd,
const short &code=1000,
const std::string &message=
"bye");
4351 auto [it, inserted] = solveFun.try_emplace(key);
4352 it->second.push_back(std::move(fc));
4393 bool closeFD(
const int &fd,
const std::string &closeCodeAndMessage);
4413 bool closeFD(
const int &fd,
const short &code=1000,
const std::string &message=
"bye");
4437 bool sendMessage(
const int &fd,
const std::string &msg,
const std::string &type=
"0001");
4482 void sendMessage(
const std::string &msg,
const std::string &type=
"0001");
4553 int sendData(
const std::string &
data,
const std::string &ip,
const int &port,
const bool &block=
true);
4575 int sendData(
const char *
data,
const uint64_t &length,
const std::string &ip,
const int &port,
const bool &block=
true);
4590 int recvData(std::string &
data,
const uint64_t &length,std::string &ip,
int &port);
4685 [[noreturn]]
static void signalterminated() noexcept;
4738 struct semid_ds *buf;
4739 unsigned short *arry;
4745 csemp(
const csemp &) =
delete;
4746 csemp &operator=(
const csemp &) =
delete;
4764 bool init(key_t key,
unsigned short value = 1,
short sem_flg = SEM_UNDO);
4811 #define MAX_PROCESS_NAME 100
4815 #define MAX_PROCESS_INF 1000
4819 #define SHARED_MEMORY_KEY 0x5095
4823 #define SHARED_MEMORY_LOCK_KEY 0x5095
4879 bool join(
const char *name,
const char *argv0=
"",
const char *argv1=
"",
const char *argv2=
"");
4914 static void prepareForkedChild(
const bool terminateWithParent=
false)
noexcept
4917 sigemptyset(&emptySet);
4918 pthread_sigmask(SIG_SETMASK,&emptySet,
nullptr);
4919 struct sigaction defaultAction{};
4920 sigemptyset(&defaultAction.sa_mask);
4921 defaultAction.sa_handler=SIG_DFL;
4922 for(
const int childSignal:{SIGTERM,SIGINT,SIGHUP,SIGQUIT,SIGCHLD,SIGPIPE})
4923 sigaction(childSignal,&defaultAction,
nullptr);
4925 if(terminateWithParent)
4927 prctl(PR_SET_PDEATHSIG,SIGTERM);
4932 (void)terminateWithParent;
4958 template<
class... Args>
4959 static bool startProcess(
const std::string &name,
const int &sec=-1,Args ...args)
4961 std::vector<const char *> paramList={args...,
nullptr};
4971 prepareForkedChild();
4972 execv(name.c_str(),
const_cast<char* const*
>(paramList.data()));
4985 prepareForkedChild(
true);
4991 prepareForkedChild(
true);
4992 execv(name.c_str(),
const_cast<char* const*
>(paramList.data()));
4998 while(waitpid(pid,&sts,0)<0&&errno==EINTR) {}
5027 template<
class Fn,
class... Args>
5028 static typename std::enable_if<!std::is_convertible<Fn, std::string>::value,
bool>::type
5040 prepareForkedChild();
5041 auto f=std::bind(std::forward<Fn>(fn),std::forward<Args>(args)...);
5043 _exit(EXIT_SUCCESS);
5055 prepareForkedChild(
true);
5061 prepareForkedChild(
true);
5062 auto f=std::bind(std::forward<Fn>(fn),std::forward<Args>(args)...);
5064 _exit(EXIT_SUCCESS);
5069 while(waitpid(pid,&sts,0)<0&&errno==EINTR) {}
5079 using Task = std::function<void()>;
5120 throw std::invalid_argument(
"WorkerPool requires at least one worker");
5121 for (
size_t i = 0; i < n; ++i) {
5122 threads_.emplace_back([
this] {
5148 std::lock_guard<std::mutex> lk(mtx_);
5149 if (stop_||tasks_.size()>=maxPendingTasks_)
5151 tasks_.push(std::move(task));
5152 pending=tasks_.size();
5153 pendingTaskCount_.store(pending,std::memory_order_relaxed);
5155 size_t peak=peakPendingTaskCount_.load(std::memory_order_relaxed);
5156 while(peak<pending&&!peakPendingTaskCount_.compare_exchange_weak(
5157 peak,pending,std::memory_order_relaxed,std::memory_order_relaxed)) {}
5174 std::call_once(stopOnce_,[
this,drain] {
5176 std::lock_guard<std::mutex> lk(mtx_);
5180 tasks_=std::queue<Task>();
5181 pendingTaskCount_.store(0,std::memory_order_relaxed);
5185 for (
auto &t : threads_)
5187 if (t.joinable()) t.join();
5195 return pendingTaskCount_.load(std::memory_order_relaxed);
5204 return peakPendingTaskCount_.load(std::memory_order_relaxed);
5224 std::unique_lock<std::mutex> lk(mtx_);
5225 cv_.wait(lk, [
this] {
5226 return stop_ || !tasks_.empty();
5228 if (stop_ && tasks_.empty())
5232 task = std::move(tasks_.front());
5234 pendingTaskCount_.store(tasks_.size(),std::memory_order_relaxed);
5248 std::vector<std::thread> threads_;
5249 std::queue<Task> tasks_;
5250 mutable std::mutex mtx_;
5251 std::condition_variable cv_;
5253 const size_t maxPendingTasks_;
5254 std::atomic<size_t> pendingTaskCount_{0};
5255 std::atomic<size_t> peakPendingTaskCount_{0};
5256 std::once_flag stopOnce_;
负责二进制数据,字符串之间的转化
定义 sttnet.h:1455
static unsigned long & bitToNumber(const std::string &input, unsigned long &result)
将字符串转换为二进制,再转换为对应数值。
static unsigned long & bitStrToNumber(const std::string &input, unsigned long &result)
将 "01" 字符串(二进制字符串)转换为无符号整数。
static std::string & toBit(const std::string &input, std::string &result)
将任意长度的 "01" 字符串压缩为二进制数据,每 8 位为一个字节。
static char & bitOutput_bit(char input, const int pos, char &result)
获取字符 input 的从左向右第 pos 位(二进制)并返回 '1' 或 '0'。
static std::string & bitOutput(char input, std::string &result)
将单个字符转换为其对应的 8 位二进制字符串。
static std::string & bitOutput(const std::string &input, std::string &result)
将字符串中的每个字符依次转换为二进制位,并拼接为一个整体字符串。
static char & toBit(const std::string &input, char &result)
将最多 8 位的 "01" 字符串压缩成 1 个字节(char)。
负责加密,解密和哈希
定义 sttnet.h:1400
static std::string & sha11(const std::string &ori_str, std::string &result)
计算输入字符串的 SHA-1 哈希值,并以十六进制字符串形式返回。
static bool encryptSymmetric(const unsigned char *before, const size_t &length, const unsigned char *passwd, const unsigned char *iv, unsigned char *after)
AES-256-CBC模式对称加密函数
static bool decryptSymmetric(const unsigned char *before, const size_t &length, const unsigned char *passwd, const unsigned char *iv, unsigned char *after)
AES-256-CBC模式对称解密函数
static std::string & sha1(const std::string &ori_str, std::string &result)
计算输入字符串的 SHA-1 哈希值(原始二进制形式)。
数据编码解码,掩码处理等
定义 sttnet.h:1920
static std::string & generateMask_4(std::string &mask)
生成一个 32 位(4 字节)的随机掩码。
static std::string & transfer_websocket_key(std::string &str)
生成 WebSocket 握手响应中的 Sec-WebSocket-Accept 字段值。
static std::string base64_encode(const std::string &input)
对字符串进行 Base64 编码。
static std::string base64_decode(const std::string &input)
对 Base64 编码的字符串进行解码。
static std::string & maskCalculate(std::string &data, const std::string &mask)
使用给定的 4 字节掩码对字符串进行异或操作(XOR Masking)。
负责Http字符串和URL解析 包括从 URL 或请求报文中提取参数、IP、端口、请求头字段等功能。
定义 sttnet.h:1632
static std::string & get_value_str(const std::string &ori_str, std::string &str, const std::string &name)
从 URL 查询参数中提取指定 key 的值。
static size_t get_split_str(const std::string_view &ori_str, std::string &str, const std::string_view &a, const std::string_view &b, const size_t &pos=0)
从原始字符串中提取两个标记之间的子串。
static std::string & getPara(const std::string &url, std::string ¶)
获取 URL 中的查询参数字符串(包括 ?)。
static int & getPort(const std::string &url, int &port)
从 URL 中提取端口号。
static std::string_view & getPara(const std::string_view &url, std::string_view ¶)
获取 URL 中的查询参数字符串(包括 ?)。
static size_t get_split_str(const std::string_view &ori_str, std::string_view &str, const std::string_view &a, const std::string_view &b, const size_t &pos=0)
从原始字符串中提取两个标记之间的子串。
static std::string & get_location_str(const std::string &ori_str, std::string &str)
提取 URL 中 path 和 query 部分。
static std::string_view & get_location_str(const std::string_view &ori_str, std::string_view &str)
提取 URL 中 path 和 query 部分。
static std::string & getIP(const std::string &url, std::string &IP)
从 URL 中提取主机 IP 或域名。
static std::string_view & get_value_str(const std::string_view &ori_str, std::string_view &str, const std::string &name)
从 URL 查询参数中提取指定 key 的值。
static std::string_view & getLocPara(const std::string_view &url, std::string_view &locPara)
提取 URL 的 path 部分(不含 query)。
static std::string createHeader(const std::string &first, const std::string &second)
创建一个 HTTP 请求头字段字符串。
static std::string & getLocPara(const std::string &url, std::string &locPara)
提取 URL 的 path 部分(不含 query)。
static std::string_view & get_value_header(const std::string_view &ori_str, std::string_view &str, const std::string &name)
从 HTTP 请求头中提取指定字段的值。
static std::string & get_value_header(const std::string &ori_str, std::string &str, const std::string &name)
从 HTTP 请求头中提取指定字段的值。
json数据操作类
定义 sttnet.h:1982
static Json::Value toJsonArray(const std::string &str)
解析 JSON 字符串为 Json::Value。
static std::string createArray(T first)
创建只包含一个元素的 JSON 数组字符串。
定义 sttnet.h:2070
static std::string & jsonFormatify(const std::string &a, std::string &b)
将格式化后的 JSON 字符串去除缩进、空格等变成紧凑格式。
static std::string jsonAdd(const std::string &a, const std::string &b)
将两个 JSON 字符串拼接为一个有效的 JSON(适用于对象或数组拼接)。
static std::string toString(const Json::Value &val)
将 Json::Value 序列化为紧凑 JSON 文本。
static std::string & jsonToUTF8(const std::string &input, std::string &output)
将 JSON 字符串中的 Unicode 转义序列转换为 UTF-8 字符。
static int getValue(const std::string &oriStr, std::string &result, const std::string &type="value", const std::string &name="a", const int &num=0)
提取 JSON 字符串中指定字段的值或嵌套结构。
static std::string createArray(T first, Args... args)
创建多个元素组成的 JSON 数组字符串(递归变参模板)。
定义 sttnet.h:2088
static std::string createJson(T1 first, T2 second, Args... args)
创建多个键值对组成的 JSON 字符串(递归变参模板)。
定义 sttnet.h:2045
static std::string createJson(T1 first, T2 second)
创建仅包含一个键值对的 JSON 字符串。
定义 sttnet.h:2020
负责大小端字节序转换
定义 sttnet.h:1557
static unsigned long & htonl_ntohl_64(unsigned long &data)
将 64 位无符号整数的字节序反转(大端 <-> 小端)。
负责字符串和数字的转化
定义 sttnet.h:1850
static std::string & strto16(const std::string &ori_str, std::string &result)
将普通字符串转化为对应的十六进制表示字符串(hex string)。
static int & toInt(const std::string_view &ori_str, int &result, const int &i=-1)
string转化为int类型
static bool & toBool(const std::string_view &ori_str, bool &result)
string转化为bool类型
static float & toFloat(const std::string &ori_str, float &result, const float &i=-1)
string转化为float类型
static int & str16toInt(const std::string_view &ori_str, int &result, const int &i=-1)
16进制数字的字符串表示转化为10进制int类型数字
static double & toDouble(const std::string &ori_str, double &result, const double &i=-1)
string转化为double类型
static long & toLong(const std::string_view &ori_str, long &result, const long &i=-1)
string转化为long类型
负责浮点数精度处理
定义 sttnet.h:1577
static double & getPreciesDouble(double &number, const int &bit)
将 double 数值保留指定位数的小数,并直接修改原值。
static float & getValidFloat(float &number, const int &bit)
根据数值动态调整小数精度,保留指定数量的有效数字。
static std::string & getPreciesFloat(const float &number, const int &bit, std::string &str)
将浮点数格式化为指定小数位数的字符串表示。
static std::string & getPreciesDouble(const double &number, const int &bit, std::string &str)
将双精度浮点数格式化为指定小数位数的字符串表示。
static float & getPreciesFloat(float &number, const int &bit)
将 float 数值保留指定位数的小数,并直接修改原值。
随机数,字符串生成相关
定义 sttnet.h:1521
static std::string & getRandomStr_base64(std::string &str, const int &length)
生成一个规定长度的“Base64 字符集内的伪随机字符串”,并在末尾用 '=' 补齐至符合 Base64 字符串格式
static std::string & generateMask_4(std::string &mask)
生成一个 32 位(4 字节)的随机掩码。
static long getRandomNumber(const long &a, const long &b)
生成一个随机整数
负责websocket协议有关字符串的操作
定义 sttnet.h:1831
static std::string & transfer_websocket_key(std::string &str)
生成 WebSocket 握手响应中的 Sec-WebSocket-Accept 字段值。
bool openFile(const std::string &fileName, const bool &create=true, const int &multiple=0, const size_t &size=0, const mode_t &mode=0666)
打开文件
bool write(const char *data, const size_t &pos, const size_t &size)
写数据块
bool lockMemory()
把数据从磁盘读入内存
bool closeFile(const bool &del=false)
关闭已打开了的文件
bool deleteLineC(const int &linePos=0)
删除行
bool readLineC(std::string &data, const int linePos)
读取单行
std::string getFileName()
获取打开的文件名字
定义 sttnet.h:572
bool unlockMemory(const bool &rec=false)
把数据从内存写入磁盘
size_t getSize1()
获取二进制打开的文件在内存中的大小
定义 sttnet.h:599
std::string & readAllC(std::string &data)
读取全部
bool chgLineC(const std::string &data, const int &linePos=0)
修改行
bool deleteLine(const int &linePos=0)
删除行
bool isOpen()
判断对象是否打开了文件
定义 sttnet.h:562
bool writeC(const char *data, const size_t &pos, const size_t &size)
写数据块
int find(const std::string &targetString, const int linePos=1)
查找行
~File()
析构函数
定义 sttnet.h:557
uint64_t getFileLine()
获取打开的文件的行数
定义 sttnet.h:581
std::string & read(std::string &data, const int &linePos, const int &num)
读取行
std::mutex che
定义 sttnet.h:468
bool appendLineC(const std::string &data, const int &linePos=0)
插入行
std::string & readC(std::string &data, const int &linePos, const int &num)
读取行
static std::mutex l1
定义 sttnet.h:465
int findC(const std::string &targetString, const int linePos=1)
查找行
std::string & readAll(std::string &data)
读取全部
size_t getFileSize()
获取二进制打开的文件的大小
定义 sttnet.h:590
bool readLine(std::string &data, const int linePos)
读取单行
bool read(char *data, const size_t &pos, const size_t &size)
读取数据块
bool chgLine(const std::string &data, const int &linePos=0)
修改行
bool isBinary()
判断对象是否以二进制模式打开文件
定义 sttnet.h:567
static std::unordered_map< std::string, FileThreadLock > fl2
定义 sttnet.h:466
bool appendLine(const std::string &data, const int &linePos=0)
插入行
bool readC(char *data, const size_t &pos, const size_t &size)
读取数据块
bool openFile(const std::string &fileName, const std::string &timeFormat=ISO8086A, const std::string &contentFormat=" ")
打开一个日志文件
~LogFile()
析构函数 写完日志 关闭消费者线程
bool closeFile(const bool &del=false)
关闭对象打开的日志文件
bool deleteLogByTime(const std::string &date1="1", const std::string &date2="2")
删除指定时间区间内的日志
void writeLog(const std::string &data)
写一行日志
bool isOpen()
获取对象是否打开日志文件的状态
定义 sttnet.h:1348
LogFile(const size_t &logQueue_cap=8192)
定义 sttnet.h:1301
std::string getFileName()
获取对象打开的文件名
定义 sttnet.h:1353
uint64_t getDroppedLogCount() const noexcept
返回日志队列满时累计丢弃的日志条数。
定义 sttnet.h:1369
用独立、可 join 的 Reactor 线程监听单个 Linux 文件描述符。
定义 sttnet.h:2928
void setFunction(std::function< bool(const int &fd)> fc)
设置epoll触发后的处理函数 注册一个回调函数
定义 sttnet.h:2974
void waitAndQuit(const time::Duration &t=time::Duration{0, 0, 0, 10, 10})
开始退出epoll倒计时,直到套接字有新的消息 如果套接字倒计时结束还没有新的消息,那么退出epoll
定义 sttnet.h:3010
void endListenWithSignal()
发送结束epoll的信号
void setEndFunction(std::function< void(const int &fd)> fcEnd)
设置epoll退出前的回调函数 注册一个回调函数
定义 sttnet.h:2982
void startListen(const int &fd, const bool &flag=true, const time::Duration &dt=time::Duration{0, 0, 20, 0, 0})
开始监听
bool isListen()
返回epoll监听状态
定义 sttnet.h:2964
bool endListen()
结束epoll监听 会阻塞直到epoll退出完成
~EpollSingle()
EpollSingle的析构函数 调用 endListen() 唤醒并 join 监听线程。
定义 sttnet.h:3020
void setTimeOutFunction(std::function< bool(const int &fd)> fcTimeOut)
设置epoll超时后出发的回调函数 注册一个回调函数
定义 sttnet.h:2992
bool getRequest(const std::string &url, const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=-1)
发送一个GET请求到服务器
HttpClient(const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
HttpClient类的构造函数
定义 sttnet.h:2847
std::string body
服务器返回响应体
定义 sttnet.h:2920
std::string header
服务器返回响应头
定义 sttnet.h:2916
bool postRequest(const std::string &url, const std::string &body="", const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=-1)
发送一个POST请求到服务器
bool isReturn()
获取服务器返回响应状态
定义 sttnet.h:2912
bool getRequestFromFD(const int &fd, SSL *ssl, const std::string &url, const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=2)
从tcp套接字发送一个GET请求到服务器
bool postRequestFromFD(const int &fd, SSL *ssl, const std::string &url, const std::string &body="", const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=2)
发送一个POST请求到服务器
解析,响应Http/https请求的操作类 仅传入套接字,然后使用这个类进行Http的操作
定义 sttnet.h:3222
bool sendBack(const std::string &data, const std::string &header="", const std::string &code="200 OK", const std::string &header1="")
发送Http/Https响应
int solveRequest(TcpFDInf &TcpInf, HttpRequestInformation &HttpInf, const unsigned long &buffer_size, const int ×=1, const unsigned long &max_header_size=64UL *1024UL)
解析Http/Https请求
bool redirect(const std::string &location, const std::string &code="302 Found")
发送 HTTP 重定向响应。
bool sendBack(const char *data, const size_t &length, const char *header="\0", const char *code="200 OK\0", const char *header1="\0", const size_t &header_length=50)
发送Http/Https响应
void setFD(const int &fd, SSL *ssl=nullptr, const bool &flag1=false, const bool &flag2=true)
初始化对象,传入套接字等参数
定义 sttnet.h:3231
bool sendJson(const Json::Value &data, const std::string &code="200 OK", const std::string &extraHeaders="")
序列化 Json::Value 并发送 application/json UTF-8 响应。
bool sendText(const std::string &data, const std::string &code="200 OK", const std::string &contentType="text/plain; charset=utf-8", const std::string &extraHeaders="")
发送文本响应,自动补充 Content-Type 和 Content-Length。
void setGlobalSolveFunction(std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc)
设置全局备用函数
定义 sttnet.h:4090
void setSecuritySendBackFun(std::function< void(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc)
设置违反信息安全策略时候的返回函数
定义 sttnet.h:4081
bool startListen(const int &port, const int &threads=8)
打开Http服务器监听程序
定义 sttnet.h:4144
void setFunction(const std::string &key, std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc)
设置key对应的收到客户端消息后的回调函数
定义 sttnet.h:4117
void putTask(const std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> &fun, HttpServerFDHandler &k, HttpRequestInformation &inf)
把一个任务放入工作线程池由工作线程完成
void setGetKeyFunction(std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> parseKeyFun)
设置解析出key的回调函数
定义 sttnet.h:4137
bool close(const int &fd) override
关闭某个套接字的连接
~HttpServer()
析构函数
定义 sttnet.h:4153
bool close() override
关闭监听和所有已连接的套接字
HttpServer(const unsigned long long &maxFD=1000000, const int &buffer_size=256, const size_t &finishQueue_cap=65536, const bool &security_open=true, const int &connectionNumLimit=10, const int &connectionSecs=1, const int &connectionTimes=3, const int &requestSecs=1, const int &requestTimes=20, const int &checkFrequency=30, const int &connectionTimeout=30)
构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。
定义 sttnet.h:4058
bool close()
如果对象有套接字连接,关闭和释放这个连接和套接字,并且重新新建一个套接字。
int getServerPort()
返回已连接的客户端的端口 return 已连接的服务端的端口
定义 sttnet.h:2815
bool isConnect()
返回对象的连接状态
定义 sttnet.h:2820
std::string getServerIP()
返回已连接的服务端的ip return 已连接的服务端的ip
定义 sttnet.h:2810
bool connect(const std::string &ip, const int &port)
向服务端发起tcp连接
void resetCTX(const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
重新或第一次设置TLS加密参数
~TcpClient()
TcpClient的析构函数,会关闭释放套接字和其连接
定义 sttnet.h:2804
TcpClient(const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
TcpClient类的构造函数
tcp协议的套接字操作类
定义 sttnet.h:2565
bool flag1
定义 sttnet.h:2568
int recvData(char *data, const uint64_t &length)
从已连接的套接字中接收一次数据到char*容器
int recvDataByLength(char *data, const uint64_t &length, const int &sec=2)
从已连接的套接字中阻塞接收指定长度的数据到char*容器
int getFD()
获取该对象的套接字
定义 sttnet.h:2606
SSL * ssl
定义 sttnet.h:2570
std::function< void()> queuedCloseFunction
定义 sttnet.h:2573
int recvDataByLength(std::string &data, const uint64_t &length, const int &sec=2)
从已连接的套接字中阻塞接收指定长度的数据到字符串
bool flag2
定义 sttnet.h:2569
SSL * getSSL()
获取该对象的加密SSL句柄
定义 sttnet.h:2611
void blockSet(const int &sec=-1)
设置对象中的套接字为阻塞模式
int sendData(const char *data, const uint64_t &length, const bool &block=true)
向已连接的套接字发送指定长度的二进制数据。
void close(const bool &cle=true)
关闭对象
bool isConnect()
判断对象是否有套接字绑定
定义 sttnet.h:2634
bool multiUseSet()
设置对象中的套接字为SO_REUSEADDR模式
void unblockSet()
设置对象中的套接字为非阻塞模式
std::function< int(std::string)> queuedSendFunction
定义 sttnet.h:2572
int recvData(std::string &data, const uint64_t &length)
从已连接的套接字中接收一次数据到string字符串容器
bool flag3
如果sendData的block=true,如果发送过程中连接断开,这个标志位会置为true
定义 sttnet.h:2578
void setFD(const int &fd, SSL *ssl, const bool &flag1=false, const bool &flag2=false, const int &sec=-1)
传入套接字初始化对象
void setTransportFunctions(std::function< int(std::string)> sendFunction, std::function< void()> closeFunction={})
由服务端 Reactor 注入异步发送和关闭通道。
定义 sttnet.h:2597
int sendData(const std::string &data, const bool &block=true)
向已连接的套接字发送字符串数据。
uint64_t connection_obj_fd
定义 sttnet.h:3619
std::atomic< uint64_t > metricSendReadyQueueOverflows
定义 sttnet.h:3642
virtual ~TcpServer()
TcpServer 类的析构函数
定义 sttnet.h:3997
ServerMetricsSnapshot getMetrics() const noexcept
获取服务器运行指标快照。
定义 sttnet.h:3946
std::atomic< bool > workerWakePending
定义 sttnet.h:3595
std::atomic< bool > gracefulDrainRequested
定义 sttnet.h:3602
std::atomic< uint64_t > metricActiveConnections
定义 sttnet.h:3629
bool hasPendingReactorWork()
void setPathStrategy(const stt::security::RateLimitType &type)
设置“path 级请求限流”所使用的策略。
定义 sttnet.h:3863
std::unordered_map< int, std::weak_ptr< ConnectionWriteState > > writeRegistry
定义 sttnet.h:3610
std::atomic< uint64_t > metricIdleTimeoutChecks
定义 sttnet.h:3649
int getListenPort() const noexcept
获取当前实际监听端口。
定义 sttnet.h:3985
std::atomic< uint64_t > metricClosedConnections
定义 sttnet.h:3630
int enqueueWrite(const std::shared_ptr< ConnectionWriteState > &state, std::string data)
stt::system::WorkerPool * workpool
定义 sttnet.h:3571
std::condition_variable gracefulShutdownCV
定义 sttnet.h:3604
bool unblock
定义 sttnet.h:3585
std::atomic< uint64_t > metricReactorWakeups
定义 sttnet.h:3644
WriteFlushResult flushConnectionWrites(TcpFDInf &connection)
size_t writeBudgetPerEvent
定义 sttnet.h:3621
std::atomic< uint64_t > metricReactorWakeupsCoalesced
定义 sttnet.h:3645
void prepareHandler(TcpFDHandler &handler, const int &fd)
void redrawTLS()
撤销TLS加密,ca证书等
void reportReactorStartup(const bool success)
std::unordered_map< int, TcpFDInf > clientfd
定义 sttnet.h:3577
virtual bool close(const int &fd)
关闭某个套接字的连接
std::atomic< uint64_t > metricBatchedWriteSyscalls
定义 sttnet.h:3647
void applyAcceptedSocketOptions(const int &acceptedFD) const noexcept
void setMaxPendingWriteBytes(const size_t bytes)
设置每连接待发送数据高水位。
定义 sttnet.h:3887
void putTask(const std::function< int(TcpFDHandler &k, TcpInformation &inf)> &fun, TcpFDHandler &k, TcpInformation &inf)
把一个任务放入工作线程池由工作线程完成
std::condition_variable reactorStartupCV
定义 sttnet.h:3599
WriteFlushResult
定义 sttnet.h:3676
@ WaitWrite
定义 sttnet.h:3676
@ Reschedule
定义 sttnet.h:3676
@ Drained
定义 sttnet.h:3676
@ WaitRead
定义 sttnet.h:3676
bool updateConnectionEvents(const int &epollFD, TcpFDInf &connection, const bool &wantWrite)
std::deque< SendReadyMessage > timeoutCandidates
定义 sttnet.h:3612
TcpServer(const unsigned long long &maxFD=1000000, const int &buffer_size=256, const size_t &finishQueue_cap=65536, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionSecs=1, const int &connectionTimes=6, const int &requestSecs=1, const int &requestTimes=40, const int &checkFrequency=60, const int &connectionTimeout=60)
构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。
定义 sttnet.h:3729
size_t sendReadyBudgetPerWake
定义 sttnet.h:3625
void advanceGracefulDrain()
int connectionTimes
定义 sttnet.h:3615
std::mutex writeRegistryMutex
定义 sttnet.h:3609
size_t gracefulShutdownTimeoutMs
定义 sttnet.h:3626
std::atomic< uint64_t > metricWorkerTaskRejections
定义 sttnet.h:3643
void setMaxHttpHeaderBytes(const size_t bytes)
设置 HTTP 请求和 WebSocket 握手头上限。
定义 sttnet.h:3899
bool setTLS(const char *cert, const char *key, const char *passwd, const char *ca)
以兼容模式启用双向 TLS(mTLS)。
std::atomic< uint64_t > metricWorkerQueueOverflows
定义 sttnet.h:3641
int connectionSecs
定义 sttnet.h:3614
void notifyReactor() noexcept
std::atomic< uint64_t > metricQueuedWriteBytes
定义 sttnet.h:3635
std::mutex tlsContextMutex
定义 sttnet.h:3588
void setRequestStrategy(const stt::security::RateLimitType &type)
设置“IP 级请求限流”所使用的策略。
定义 sttnet.h:3855
std::atomic< uint64_t > metricBatchedWriteBuffers
定义 sttnet.h:3648
void clearReactorQueues()
unsigned long long maxFD
定义 sttnet.h:3573
ServerSocketOptions socketOptions
定义 sttnet.h:3627
bool setTLS(const char *cert, const char *key, const char *passwd="")
启用普通单向 TLS,适用于常见 HTTPS/WSS 服务。
std::atomic< uint64_t > metricIdleTimeoutCloses
定义 sttnet.h:3650
std::atomic< uint64_t > metricSentBytes
定义 sttnet.h:3636
std::atomic< uint64_t > metricRejectedConnections
定义 sttnet.h:3631
std::deque< SendReadyMessage > overflowSendReadyQueue
定义 sttnet.h:3608
system::MPSCQueue< WorkerMessage > finishQueue
定义 sttnet.h:3569
void setConnectStrategy(const stt::security::RateLimitType &type)
设置“连接速率限流”所使用的策略。
定义 sttnet.h:3848
system::MPSCQueue< SendReadyMessage > sendReadyQueue
定义 sttnet.h:3570
void drainReactorWork(const int &epollFD)
std::atomic< uint64_t > metricWriteOverflows
定义 sttnet.h:3640
std::atomic< bool > flag1
定义 sttnet.h:3578
security::ConnectionLimiter connectionLimiter
定义 sttnet.h:3574
void setGracefulShutdownTimeout(const size_t milliseconds)
设置 close()/stopListen() 等待在途请求和发送队列排空的最长时间。
定义 sttnet.h:3940
std::deque< WorkerMessage > overflowFinishQueue
定义 sttnet.h:3606
unsigned long buffer_size
定义 sttnet.h:3572
int serverType
定义 sttnet.h:3613
std::thread reactorThread
定义 sttnet.h:3597
void scheduleBufferedRead(const int &fd, const uint64_t connection)
void prepareQueuedHandler(TcpFDHandler &handler, const int &fd)
void setReactorMessageBudgets(const size_t workerCompletions, const size_t sendReadyNotifications)
设置单次 Reactor 唤醒处理的跨线程消息预算。
定义 sttnet.h:3912
std::atomic< uint64_t > metricWriteSyscalls
定义 sttnet.h:3646
bool security_open
定义 sttnet.h:3591
std::atomic< uint64_t > metricGracefulShutdownTimeouts
定义 sttnet.h:3651
size_t drainSendReady(const int &epollFD, const size_t budget)
SSL_CTX * ctx
定义 sttnet.h:3586
void setGlobalSolveFunction(std::function< bool(TcpFDHandler &k, TcpInformation &inf)> fc)
设置全局备用函数
定义 sttnet.h:3794
bool reactorStartupSuccess
定义 sttnet.h:3601
void requestCloseAfterFlush(const int &fd, const uint64_t expectedConnection=0)
std::atomic< uint64_t > metricPeakPendingWorkerTasks
定义 sttnet.h:3639
int requestSecs
定义 sttnet.h:3616
bool isListen()
返回对象的监听状态
定义 sttnet.h:3980
void setPathLimit(const std::string &path, const int ×, const int &secs)
设置某个路径的额外限流规则(path 级)。
定义 sttnet.h:3877
void setMaxPendingWorkerTasks(const size_t tasks)
设置 WorkerPool 尚未开始执行的任务上限。
定义 sttnet.h:3905
std::atomic< int > workerEventFD
定义 sttnet.h:3594
bool setTLS(const char *cert, const char *key, const char *passwd, const char *ca, TLSClientAuthMode clientAuth)
启用 TLS 并显式选择客户端证书校验模式。
SSL * getSSL(const int &fd)
查询和服务端的连接,传入套接字,返回加密的SSL句柄
std::atomic< uint64_t > metricAcceptedConnections
定义 sttnet.h:3628
std::mutex reactorStartupMutex
定义 sttnet.h:3598
void publishSendReady(const std::shared_ptr< ConnectionWriteState > &state)
std::mutex gracefulShutdownMutex
定义 sttnet.h:3603
size_t drainWorkerResults(const size_t budget)
void setGetKeyFunction(std::function< int(TcpFDHandler &k, TcpInformation &inf)> parseKeyFun)
设置解析出key的回调函数
定义 sttnet.h:3819
void setSecuritySendBackFun(std::function< void(TcpFDHandler &k, TcpInformation &inf)> fc)
设置违反信息安全策略时候的返回函数
定义 sttnet.h:3785
std::recursive_mutex lifecycleMutex
定义 sttnet.h:3596
bool startListen(const int &port, const int &threads=8)
打开Tcp服务器监听程序
std::atomic< uint64_t > metricTLSHandshakeFailures
定义 sttnet.h:3633
std::mutex overflowFinishMutex
定义 sttnet.h:3605
size_t maxPendingWriteBytes
定义 sttnet.h:3620
size_t maxPendingWorkerTasks
定义 sttnet.h:3623
std::deque< SendReadyMessage > bufferedReadQueue
定义 sttnet.h:3611
size_t maxHttpHeaderBytes
定义 sttnet.h:3622
std::atomic< uint64_t > metricParsedHttpRequests
定义 sttnet.h:3634
std::atomic< uint64_t > metricPendingWriteBytes
定义 sttnet.h:3637
void setSocketOptions(const ServerSocketOptions &options)
设置新接收 TCP 连接的常用套接字参数。
定义 sttnet.h:3922
std::atomic< uint64_t > metricAcceptErrors
定义 sttnet.h:3632
size_t workerCompletionBudgetPerWake
定义 sttnet.h:3624
std::mutex overflowSendReadyMutex
定义 sttnet.h:3607
void setFunction(const std::string &key, std::function< int(TcpFDHandler &k, TcpInformation &inf)> fc)
设置key对应的收到客户端消息后的回调函数
定义 sttnet.h:3805
int requestTimes
定义 sttnet.h:3617
std::atomic< uint64_t > metricPeakPendingWriteBytes
定义 sttnet.h:3638
bool reactorStartupComplete
定义 sttnet.h:3600
void requestQueuedClose(const std::shared_ptr< ConnectionWriteState > &state)
int checkFrequency
定义 sttnet.h:3618
void publishWorkerResult(WorkerMessage message)
virtual bool close()
关闭监听和所有已连接的套接字
void setCloseFun(std::function< void(const int &fd)> closeFun)
设置关闭tcp连接之后调用的函数
定义 sttnet.h:3881
void handleSendReady(const int &epollFD, SendReadyMessage message)
void setWriteBudgetPerEvent(const size_t bytes)
设置 Reactor 单次处理一个连接的最大发送字节数。
定义 sttnet.h:3893
bool createFD(const bool &flag1=false, const int &sec=-1)
销毁原来的套接字,重新创建一个客户端
~UdpClient()
析构函数,对象生命结束会会关闭套接字
定义 sttnet.h:4629
UdpClient(const bool &flag1=false, const int &sec=-1)
构造函数
UDP操作的类 传入套接字进行UDP协议的操作
定义 sttnet.h:4495
int recvData(std::string &data, const uint64_t &length, std::string &ip, int &port)
接收一次数据到string字符串容器
int sendData(const char *data, const uint64_t &length, const std::string &ip, const int &port, const bool &block=true)
向目标发送指定长度的二进制数据。
bool multiUseSet()
设置SO_REUSEADDR模式
void unblockSet()
设置为非阻塞模式
void close(const bool &cle=true)
置空对象,关闭套接字
int sendData(const std::string &data, const std::string &ip, const int &port, const bool &block=true)
向目标发送字符串数据。
bool flag2
定义 sttnet.h:4499
int getFD()
返回fd
定义 sttnet.h:4527
bool flag1
定义 sttnet.h:4498
void blockSet(const int &sec=-1)
设置为阻塞模式
int recvData(char *data, const uint64_t &length, std::string &ip, int &port)
接收一次数据到char*容器
void setFD(const int &fd, const bool &flag1=false, const int &sec=-1, const bool &flag2=false)
设置fd
bool createFD(const int &port, const bool &flag1=false, const int &sec=-1, const bool &flag2=true)
销毁原来的套接字,重新创建一个服务端
UdpServer(const int &port, const bool &flag1=false, const int &sec=-1, const bool &flag2=true)
构造函数
~UdpServer()
析构函数,对象生命结束会会关闭套接字
定义 sttnet.h:4656
std::string getServerPort()
如果连接到了服务器,以字符串返回服务器端口。
定义 sttnet.h:3143
std::string getServerIp()
如果连接到了服务器 返回服务器ip
定义 sttnet.h:3137
WebSocketClient(const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
WebSocketClient类的构造函数
定义 sttnet.h:3052
int getServerPortNumber()
如果连接到了服务器,以整数返回服务器端口。
定义 sttnet.h:3148
void close(const short &code=1000, const std::string &message="bye", const bool &wait=true)
发送关闭帧并关闭 WebSocket 连接(标准方式)
void setFunction(std::function< bool(const std::string &message, WebSocketClient &k)> fc)
设置收到服务端消息后的回调函数 注册一个回调函数
定义 sttnet.h:3063
void close(const std::string &closeCodeAndMessage, const bool &wait=true)
发送关闭帧并关闭 WebSocket 连接(简化方式)
std::string getUrl()
如果连接到了服务器 返回url
定义 sttnet.h:3132
~WebSocketClient()
WebSocketClient类的析构函数,销毁对象时候会优雅退出断开连接
bool connect(const std::string &url, const int &min=20)
连接到websocket服务器
bool sendMessage(const std::string &message, const std::string &type="0001")
发送 WebSocket 消息
bool isConnect()
返回连接状态
定义 sttnet.h:3127
WebSocket协议的操作类 仅传入套接字,然后使用这个类进行WebSocket的操作
定义 sttnet.h:4163
bool sendMessage(const std::string &msg, const std::string &type="0001")
发送一条websocket信息
int getMessage(TcpFDInf &Tcpinf, WebSocketFDInformation &Websocketinf, const unsigned long &buffer_size, const int &ii=1)
获取一条websocket消息
void setFD(const int &fd, SSL *ssl=nullptr, const bool &flag1=false, const bool &flag2=true)
初始化对象,传入套接字等参数
定义 sttnet.h:4172
void setTimeOutTime(const int &seca)
设置心跳时间
定义 sttnet.h:4375
bool close() override
关闭监听和所有连接
void setGlobalSolveFunction(std::function< bool(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> fc)
设置全局备用函数
定义 sttnet.h:4313
bool close(const int &fd) override
关闭某个套接字的连接
bool closeFD(const int &fd, const short &code=1000, const std::string &message="bye")
发送关闭帧关闭对应套接字的 WebSocket 连接(标准方式)
void setSecuritySendBackFun(std::function< void(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> fc)
设置违反信息安全策略时候的返回函数
定义 sttnet.h:4304
void setGetKeyFunction(std::function< int(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> parseKeyFun)
设置解析出key的回调函数
定义 sttnet.h:4369
WebSocketServer(const unsigned long long &maxFD=1000000, const int &buffer_size=256, const size_t &finishQueue_cap=65536, const bool &security_open=true, const int &connectionNumLimit=5, const int &connectionSecs=10, const int &connectionTimes=3, const int &requestSecs=1, const int &requestTimes=10, const int &checkFrequency=60, const int &connectionTimeout=120)
构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。
定义 sttnet.h:4281
void putTask(const std::function< int(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> &fun, WebSocketServerFDHandler &k, WebSocketFDInformation &inf)
把一个任务放入工作线程池由工作线程完成
void setJudgeFunction(std::function< bool(WebSocketFDInformation &k)> fcc)
设置websocket握手阶段的检查函数,只有检查通过才执行后续握手 注册一个回调函数
定义 sttnet.h:4332
void setFunction(const std::string &key, std::function< int(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> fc)
设置key对应的收到客户端消息后的回调函数
定义 sttnet.h:4349
bool startListen(const int &port, const int &threads=8)
打开Websocket服务器监听程序
定义 sttnet.h:4454
void setStartFunction(std::function< bool(WebSocketServerFDHandler &k, WebSocketFDInformation &inf)> fccc)
设置websocket连接成功后就执行的回调函数 注册一个回调函数
定义 sttnet.h:4322
bool sendMessage(const int &fd, const std::string &msg, const std::string &type="0001")
发送 WebSocket 消息给某一个客户端
void sendMessage(const std::string &msg, const std::string &type="0001")
广播发送 WebSocket 消息
~WebSocketServer()
WebSocketServer的析构函数
定义 sttnet.h:4487
bool closeFD(const int &fd, const std::string &closeCodeAndMessage)
发送关闭帧关闭对应套接字的 WebSocket 连接(简化方式)
void setHBTimeOutTime(const int &secb)
设置发送心跳后的等待时间
定义 sttnet.h:4381
统一的连接与请求安全裁决器(IP 级 + fd 级,多策略限流 + 黑名单)。
定义 sttnet.h:2382
void setRequestStrategy(const RateLimitType &type)
设置“IP 级请求限流”所使用的策略。
DefenseDecision allowRequest(const std::string &ip, const int &fd, const std::string_view &path, const int ×, const int &secs)
对已建立连接的一次请求进行安全裁决。
ConnectionLimiter(const int &maxConn=20, const int &idleTimeout=60)
构造函数。
定义 sttnet.h:2390
bool connectionDetect(const std::string &ip, const int &fd)
检测并清理僵尸连接(fd 级)。
DefenseDecision allowConnect(const std::string &ip, const int &fd, const int ×, const int &secs)
对新建立的连接进行安全裁决(IP 级)。
void setPathStrategy(const RateLimitType &type)
设置“path 级请求限流”所使用的策略。
void clearIP(const std::string &ip, const int &fd)
在连接断开时回收对应 fd 的状态。
void setPathLimit(const std::string &path, const int ×, const int &secs)
设置某个路径的额外限流规则(path 级)。
void banIP(const std::string &ip, int banSeconds, const std::string &reasonCN, const std::string &reasonEN)
立即将指定 IP 加入黑名单(直接封禁)。
bool isBanned(const std::string &ip) const
判断某ip是否被封禁
void setConnectStrategy(const RateLimitType &type)
设置“连接速率限流”所使用的策略。
void unbanIP(const std::string &ip)
手动解除某个 IP 的黑名单。
负责进程心跳监控,调度的类 用于监控服务进程,保证服务进程持续有效运行 进程结束后,0x5095这一块共享内存和信号量都没有删掉 目前只支持最多三个参数的进程加入监控 应该自己手动在程序编写加入心跳监控...
定义 sttnet.h:4864
bool join(const char *name, const char *argv0="", const char *argv1="", const char *argv2="")
把进程加入到心跳系统
bool deleteFromHBS()
把当前进程从心跳系统中删除
static bool HBCheck(const int &sec)
检查心跳监控系统 如果上一次心跳更新的时间和现在的时候相差大于等于sec秒,则杀死进程 先发送信号15杀死进程 如果8秒后进程还存在 则发送信号9强制杀死
static void list()
输出心跳监控系统的所有进程的信息
Lock-free bounded MPSC queue (Multi-Producer Single-Consumer) 无锁有界多生产者单消费者队列(环形缓冲)
定义 sttnet.h:204
MPSCQueue & operator=(const MPSCQueue &)=delete
bool push(const T &v)
定义 sttnet.h:243
MPSCQueue(std::size_t capacity_pow2)
定义 sttnet.h:206
~MPSCQueue()
定义 sttnet.h:230
MPSCQueue(const MPSCQueue &)=delete
bool push(T &&v) noexcept(std::is_nothrow_move_constructible_v< T >)
Try push (non-blocking). Returns false if queue is full. 尝试入队(非阻塞),队列满则返回 false
定义 sttnet.h:239
std::size_t approx_size() const noexcept
Approximate size (may be inaccurate under concurrency) 近似长度(并发下可能不精确)
定义 sttnet.h:285
bool pop(T &out) noexcept(std::is_nothrow_move_assignable_v< T > &&std::is_nothrow_move_constructible_v< T >)
Try pop (single consumer). Returns false if empty. 尝试出队(单消费者),空则返回 false
定义 sttnet.h:254
bool possibly_nonempty() const noexcept
判断队列是否可能包含数据。
定义 sttnet.h:295
进程管理的静态工具类
定义 sttnet.h:4912
static std::enable_if<!std::is_convertible< Fn, std::string >::value, bool >::type startProcess(Fn &&fn, const int &sec=-1, Args &&...args)
通过函数创建子进程(可选择是否定时重启)
定义 sttnet.h:5029
static bool startProcess(const std::string &name, const int &sec=-1, Args ...args)
启动一个新进程(可选择是否定时重启)
定义 sttnet.h:4959
初始化服务系统的类
定义 sttnet.h:4674
static bool blockTerminationSignals()
在创建任何工作线程前阻塞 SIGTERM 和 SIGINT。
static void setLogFile(file::LogFile *logfile=nullptr, const std::string &language="")
设置日志系统的日志文件对象 传入的日志文件对象如果是没初始化的空的对象,系统自动在程序目录下生成server_log文件夹并且根据当前时间生成一个日志文件记录服务程序的网络通信 如果传入的日志文件对象是...
static void setExceptionHandling()
设置系统的信号
static file::LogFile * logfile
系统的日志系统的读写日志对象的指针
定义 sttnet.h:4679
static int waitForTerminationSignal()
同步等待 SIGTERM/SIGINT,返回收到的信号,失败返回 -1。
static void init(file::LogFile *logfile=nullptr, const std::string &language="")
执行setExceptionHandling和setLogFile两个函数,完成初始化信号和日志系统
static std::string language
系统的日志系统的语言选择,默认为English
定义 sttnet.h:4683
固定大小的工作线程池
定义 sttnet.h:5106
~WorkerPool()
析构函数
定义 sttnet.h:5133
size_t peakPendingTasks() const noexcept
返回等待任务数的历史峰值。
定义 sttnet.h:5202
void stop(const bool drain=true)
停止线程池并等待所有线程退出
定义 sttnet.h:5172
size_t maxPendingTasks() const noexcept
返回等待任务队列容量。
定义 sttnet.h:5199
WorkerPool(size_t n, size_t maxPendingTasks=65536)
构造函数,创建指定数量的工作线程
定义 sttnet.h:5116
bool submit(Task task)
向线程池提交一个任务
定义 sttnet.h:5144
size_t pendingTasks() const
返回当前等待执行的任务数(瞬时快照)。
定义 sttnet.h:5193
封装 System V 信号量的同步工具类。
定义 sttnet.h:4728
bool init(key_t key, unsigned short value=1, short sem_flg=SEM_UNDO)
初始化信号量。
bool post(short value=1)
V 操作(释放),尝试将信号量值加上 value。
bool wait(short value=-1)
P 操作(等待),尝试将信号量值减去 value。
csemp()
构造函数,初始化内部状态。
定义 sttnet.h:4752
时间操作、运算、计时的类
定义 sttnet.h:1170
static Duration & calculateTime(const std::string &time1, const std::string &time2, Duration &result, const std::string &format1=ISO8086A, const std::string &format2=ISO8086A)
计算两个用字符串表示的时间相减的差值
static bool convertFormat(std::string &timeStr, const std::string &oldFormat, const std::string &newFormat=ISO8086A)
转化时间字符串的格式
static std::string & getTime(std::string &timeStr, const std::string &format=ISO8086A)
获取当前时间
Duration getDt()
获取上一次计时的时间
定义 sttnet.h:1253
bool isStart()
返回本对象计时状态
定义 sttnet.h:1258
static std::string & calculateTime(const std::string &time1, const Duration &time2, std::string &result, const std::string &am, const std::string &format1=ISO8086A, const std::string &format2=ISO8086A)
一个用字符串表示的时间加上或者减去一段时间
Duration checkTime()
计时过程中检查时间
static bool compareTime(const std::string &time1, const std::string &time2, const std::string &format1=ISO8086A, const std::string &format2=ISO8086A)
比较两个时间字符串表示的时间的大小
@ ESTABLISHED
定义 sttnet.h:3388
@ HANDSHAKING
定义 sttnet.h:3387
TLSClientAuthMode
TLS 服务端对客户端证书的校验模式。
定义 sttnet.h:3393
@ Required
定义 sttnet.h:3396
@ Optional
定义 sttnet.h:3395
涉及信息安全的api
定义 sttnet.h:2128
RateLimitType
限流算法类型(策略)。
定义 sttnet.h:2174
@ Cooldown
定义 sttnet.h:2175
@ SlidingWindow
定义 sttnet.h:2177
@ FixedWindow
定义 sttnet.h:2176
@ TokenBucket
定义 sttnet.h:2178
DefenseDecision
安全裁决结果(由 ConnectionLimiter 返回)。
定义 sttnet.h:2288
系统的设置,进程的控制,心跳监控等
定义 sttnet.h:173
std::function< void()> Task
定义 sttnet.h:5079
std::ostream & operator<<(std::ostream &os, const Duration &a)
将 Duration 对象以可读格式输出到流中。
std::chrono::duration< uint64_t > Seconds
定义 sttnet.h:1152
std::chrono::duration< uint64_t, std::milli > Milliseconds
定义 sttnet.h:1151
constexpr int version_minor
定义 sttnet.h:168
constexpr int version_major
定义 sttnet.h:167
constexpr std::string_view version
定义 sttnet.h:170
constexpr int version_patch
定义 sttnet.h:169
FileThreadLock(const std::string &loc, const int &threads)
这个结构体的构造函数
定义 sttnet.h:453
int threads
记录文件正在被多少个线程使用
定义 sttnet.h:443
std::mutex lock
此文件的锁
定义 sttnet.h:447
std::string loc
文件路径
定义 sttnet.h:439
单个服务端连接的有界异步发送状态。
定义 sttnet.h:3456
std::deque< std::string > queue
定义 sttnet.h:3458
size_t queued_bytes
定义 sttnet.h:3460
std::mutex mutex
定义 sttnet.h:3457
size_t max_queued_bytes
定义 sttnet.h:3461
uint64_t connection_obj_fd
定义 sttnet.h:3463
bool overflowed
定义 sttnet.h:3467
bool close_after_flush
定义 sttnet.h:3466
bool closed
定义 sttnet.h:3468
size_t front_offset
定义 sttnet.h:3459
bool notification_pending
定义 sttnet.h:3464
bool close_requested
定义 sttnet.h:3465
跨线程通知 Reactor 某连接有待发数据的轻量消息。
定义 sttnet.h:3473
uint64_t connection_obj_fd
定义 sttnet.h:3475
TcpServer 运行指标的无锁快照。
定义 sttnet.h:3405
uint64_t accepted_connections
定义 sttnet.h:3406
uint64_t write_overflows
定义 sttnet.h:3418
uint64_t active_connections
定义 sttnet.h:3407
uint64_t write_syscalls
定义 sttnet.h:3424
uint64_t worker_task_rejections
定义 sttnet.h:3421
uint64_t tls_handshake_failures
定义 sttnet.h:3411
uint64_t reactor_wakeups
定义 sttnet.h:3422
uint64_t queued_write_bytes
定义 sttnet.h:3413
uint64_t sent_bytes
定义 sttnet.h:3414
uint64_t graceful_shutdown_timeouts
定义 sttnet.h:3429
uint64_t parsed_http_requests
定义 sttnet.h:3412
uint64_t idle_timeout_closes
定义 sttnet.h:3428
uint64_t closed_connections
定义 sttnet.h:3408
uint64_t batched_write_buffers
定义 sttnet.h:3426
uint64_t rejected_connections
定义 sttnet.h:3409
uint64_t accept_errors
定义 sttnet.h:3410
uint64_t worker_queue_overflows
定义 sttnet.h:3419
uint64_t peak_pending_worker_tasks
定义 sttnet.h:3417
uint64_t pending_write_bytes
定义 sttnet.h:3415
uint64_t idle_timeout_checks
定义 sttnet.h:3427
uint64_t batched_write_syscalls
定义 sttnet.h:3425
uint64_t reactor_wakeups_coalesced
定义 sttnet.h:3423
uint64_t send_ready_queue_overflows
定义 sttnet.h:3420
uint64_t peak_pending_write_bytes
定义 sttnet.h:3416
新连接的常用 TCP 套接字调优参数。
定义 sttnet.h:3437
int fast_open_queue
定义 sttnet.h:3447
bool tcp_no_delay
定义 sttnet.h:3438
bool keep_alive
定义 sttnet.h:3439
int receive_buffer_bytes
定义 sttnet.h:3441
int keep_alive_probe_count
定义 sttnet.h:3445
int send_buffer_bytes
定义 sttnet.h:3442
int keep_alive_idle_seconds
定义 sttnet.h:3443
int keep_alive_interval_seconds
定义 sttnet.h:3444
int defer_accept_seconds
定义 sttnet.h:3446
int listen_backlog
定义 sttnet.h:3448
bool reuse_port
定义 sttnet.h:3440
保存底层基础Tcp通道信息的结构体
定义 sttnet.h:3482
int status
当前fd的接收状态,用于保存接收处理机逻辑
定义 sttnet.h:3510
bool write_waiting_for_read
定义 sttnet.h:3539
std::queue< std::any > pendindQueue
等待处理的队列
定义 sttnet.h:3506
std::string ip
客户端ip
定义 sttnet.h:3494
bool write_interest
定义 sttnet.h:3538
unsigned long p_buffer_now
接收空间位置指针
定义 sttnet.h:3530
std::shared_ptr< ConnectionWriteState > write_state
定义 sttnet.h:3537
SSL * ssl
如果加密了,存放加密句柄
定义 sttnet.h:3518
char * buffer
接收空间指针
定义 sttnet.h:3526
int fd
套接字fd
定义 sttnet.h:3486
size_t active_workers
定义 sttnet.h:3534
bool closing
定义 sttnet.h:3536
unsigned long buffer_capacity
定义 sttnet.h:3532
TLSState tls_state
tls状态
定义 sttnet.h:3522
std::string port
客户端端口
定义 sttnet.h:3498
std::string_view data
保存收到的客户端传来的数据
定义 sttnet.h:3514
int FDStatus
记录当前处理状态机到第几步了
定义 sttnet.h:3502
uint64_t connection_obj_fd
连接对象fd
定义 sttnet.h:3490
工作现场完成任务后压入完成队列的数据结构
定义 sttnet.h:3546
std::shared_ptr< void > request
定义 sttnet.h:3558
int fd
底层套接字
定义 sttnet.h:3550
uint64_t connection_obj_fd
定义 sttnet.h:3552
int ret
返回值 -2:失败并且要求关闭连接 -1:失败但不需要关闭连接 1:成功
定义 sttnet.h:3556
单个连接(fd)的安全与限流状态。
定义 sttnet.h:2230
RateState requestRate
定义 sttnet.h:2232
std::unordered_map< std::string, RateState > pathRate
定义 sttnet.h:2233
std::chrono::steady_clock::time_point lastActivity
定义 sttnet.h:2234
单一限流器的运行状态(可复用于多种限流策略)。
定义 sttnet.h:2200
std::deque< std::chrono::steady_clock::time_point > history
定义 sttnet.h:2207
int violations
定义 sttnet.h:2203
std::chrono::steady_clock::time_point lastRefill
定义 sttnet.h:2211
double tokens
定义 sttnet.h:2210
int counter
定义 sttnet.h:2202
std::chrono::steady_clock::time_point lastTime
定义 sttnet.h:2204
char argv0[20]
进程第一个参数
定义 sttnet.h:4845
char argv1[20]
进程第二个参数
定义 sttnet.h:4849
pid_t pid
进程id
定义 sttnet.h:4833
time_t lastTime
进程最后一次心跳时间,是时间戳
定义 sttnet.h:4837
char name[MAX_PROCESS_NAME]
进程名字
定义 sttnet.h:4841
char argv2[20]
进程第三个参数
定义 sttnet.h:4853
表示时间间隔的结构体,支持天、小时、分钟、秒和毫秒粒度。
定义 sttnet.h:855
bool operator<=(const Duration &b)
判断当前时间间隔是否小于等于另一个时间间隔。
定义 sttnet.h:950
bool operator>(const Duration &b)
判断当前时间间隔是否大于另一个时间间隔。
定义 sttnet.h:886
Duration operator+(const Duration &b)
将两个时间间隔相加。
定义 sttnet.h:966
Duration(long long a, int b, int c, int d, int e)
构造函数,传入天,时,分,秒,毫秒
定义 sttnet.h:879
int msec
毫秒
定义 sttnet.h:875
bool operator>=(const Duration &b)
判断当前时间间隔是否大于等于另一个时间间隔。
定义 sttnet.h:934
bool operator==(const Duration &b)
判断当前时间间隔是否等于另一个时间间隔。
定义 sttnet.h:918
double convertToHour()
将当前时间间隔转换为以“小时”为单位的浮点数表示。
定义 sttnet.h:1063
long long day
天
定义 sttnet.h:859
double convertToDay()
将当前时间间隔转换为以“天”为单位的浮点数表示。
定义 sttnet.h:1053
Duration operator-(const Duration &b)
计算两个时间间隔的差值(当前对象减去参数 b)。
定义 sttnet.h:1010
bool operator<(const Duration &b)
判断当前时间间隔是否小于另一个时间间隔。
定义 sttnet.h:902
double convertToSec()
将当前时间间隔转换为以“秒”为单位的浮点数表示。
定义 sttnet.h:1083
Duration recoverForm(const long long &t)
从给定的毫秒数恢复为标准的天-时-分-秒-毫秒格式。
定义 sttnet.h:1104
long long convertToMsec()
将当前时间间隔转换为总毫秒数。
定义 sttnet.h:1093
double convertToMin()
将当前时间间隔转换为以“分钟”为单位的浮点数表示。
定义 sttnet.h:1073
#define ISO8086A
STTNet 历史本地时间文本格式宏:"yyyy-mm-ddThh:mi:ss"(不携带时区)
定义 sttnet.h:1156
#define MAX_PROCESS_NAME
定义MAX_PROCESS_NAME这个宏为100,意思是进程信息中的进程名字长度不超过100个字节
定义 sttnet.h:4811