Skip to content

Commit

Permalink
fix: Timestamp accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
asciphx committed Sep 6, 2024
1 parent 2898279 commit 8d23656
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions fc/include/app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace fc {
//Maybe the input bandwidth of the server is much greater than the output bandwidth
App& file_type(const std::vector<std::string_view>& line = { "html","htm","ico","css","js","srt",
"json","svg","png","jpg","jpeg","gif","txt","wasm","mp4","webm","mp3","wav","aac","mkv","vtt" });
// keep-alive time seconds = idle + (intvl * probes) >> 1, RESk_A is the total time written. time seconds default = 6s
App& set_keep_alive(int idle, int intvl = 1, unsigned char probes = 10);
// keep-alive time seconds = idle + intvl * probes, RESk_A is the total time written. time seconds default = 6s
App& set_keep_alive(int idle = 1, int intvl = 1, unsigned char probes = 5);
//std::string ssl_ciphers, std::string ssl_key = "./server.key", std::string ssl_cert = "./server.crt"
App& set_ssl(std::string ssl_ciphers, std::string ssl_key = "./server.key", std::string ssl_cert = "./server.crt");
void http_serve(int port = 8080, std::string ip = "", int nthreads = std::thread::hardware_concurrency());
Expand Down
21 changes: 8 additions & 13 deletions fc/include/hh/tcp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,10 @@ namespace fc {
struct timespec timeout; memset(&timeout, 0, sizeof(timeout)); timeout.tv_nsec = 10000;
#endif
// Main loop.
int bigsize = REScore + ids; int64_t sj = RES_TIME_T;
int64_t sj = RES_TIME_T;
do {
if (RES_TP > t) {
loop_timer.tick();
if (nthreads > 1) {
if (this->idex > bigsize) { bigsize += RESmaxEVENTS; std::this_thread::yield(); }
}
t = RES_TP;
loop_timer.tick(); t = RES_TP;
}
#if __linux__ || _WIN32
this->n_events = epoll_wait(this->epoll_fd, this->kevents, RESmaxEVENTS, 1);
Expand All @@ -101,15 +97,13 @@ namespace fc {
#endif
if (_likely(this->n_events == 0)) {
if (RES_TIME_T > sj) {
if (this->idex > bigsize) bigsize += RESmaxEVENTS << 1;
#if __cplusplus >= _cpp20_date
#if __cplusplus >= _cpp20_date && _WIN32
for (auto ider = clients.begin(); ider != clients.end(); ++ider) {
if (ider->second.on == 0) { ider->second.on = 2; Task<void> v = std::move(ider->second._); if (v) v.operator()(); }
// if (ider->second.on == 1 && RES_TIME_T - ider->second.hrt >= k_A[0]) { ider->second.on = 2; if(ider->second._) ider->second._.operator()(); }
}
#endif
sj = RES_TIME_T + k_A[0];
if (bigsize > REScore)bigsize -= RESmaxEVENTS;
}
} else {
epoll_event* kevents;
Expand All @@ -135,7 +129,7 @@ namespace fc {
#if __cplusplus < _cpp20_date
if (ro->_) ro->_ = ro->_.resume_with(std::move([](co&& sink) { throw fiber_exception(std::move(sink), ""); return std::move(sink); }));
#else
++ro->idx; epoll_del(this->event_fd); ro->on = 0; fc::Task<void> v = std::move(ro->_); if (v) v.operator()();
++ro->idx; epoll_del(this->event_fd); ro->on = 2; fc::Task<void> v = std::move(ro->_); if (v) v.operator()();
#endif
//} else {
// std::cout << "FATAL ERROR: Error on server socket " << this->event_fd << std::endl; RESquit_signal_catched = false;
Expand Down Expand Up @@ -193,7 +187,7 @@ namespace fc {
});
#else
Task<void> magic = handler(socket_fd, *this->in_addr, k_a, this->loop_timer, fib, this->epoll_fd, ap, this->idex, this);
fib->_ = std::move(magic); this->loop_timer.add_s(k_a + 1, [fib, idx] { if (fib->idx == idx && fib->on && fib->_) { fib->_(); } });
fib->_ = std::move(magic); this->loop_timer.add_s(k_a + 1, [fib, idx] { if (fib->idx == idx && fib->_) { fib->_(); } });
#endif
} while (RESon);
} else if (ro->_)ro->_.operator()();// Data available on existing sockets. Wake up the fiber associated with event_fd.
Expand All @@ -211,24 +205,25 @@ namespace fc {
static void shutdown_handler(int sig) { RESquit_signal_catched = 0; }
static void start_server(std::string ip, int port, int socktype, int n, std::function<_CTX_FUNC> conn_handler, int* k_a, void* ap,
std::string ssl_key_path = "", std::string ssl_cert_path = "", std::string ssl_ciphers = "") { // Start the winsock DLL
time(&RES_TIME_T); RES_NOW = localtime(&RES_TIME_T); RES_NOW->tm_isdst = 0; int k_A = k_a[0] + ((k_a[1] * k_a[2]) >> 1);
time(&RES_TIME_T); RES_NOW = localtime(&RES_TIME_T); RES_NOW->tm_isdst = 0; int k_A = k_a[0] + k_a[1] * k_a[2];
#ifdef _WIN32
SetConsoleOutputCP(65001); setlocale(LC_CTYPE, ".UTF8"); WSADATA w; int err = WSAStartup(MAKEWORD(2, 2), &w);
if (err != 0) { std::cerr << "WSAStartup failed with error: " << err << std::endl; return; } // Setup quit signals
signal(SIGINT, shutdown_handler); signal(SIGTERM, shutdown_handler); signal(SIGABRT, shutdown_handler);
std::thread date_thread([]() { while (RESquit_signal_catched) { fc::REStop_h.tick(), std::this_thread::sleep_for(std::chrono::milliseconds(1)); } });
#else
struct sigaction act; memset(&act, 0, sizeof(act)); act.sa_handler = shutdown_handler;
sigaction(SIGINT, &act, 0); sigaction(SIGTERM, &act, 0); sigaction(SIGQUIT, &act, 0);
// Ignore sigpipe signal. Otherwise sendfile causes crashes if the
// client closes the connection during the response transfer.
std::thread date_thread([]() { while (RESquit_signal_catched) { fc::REStop_h.tick(), std::this_thread::sleep_for(std::chrono::milliseconds(4)); } });
#endif
RESmaxEVENTS = n > 32 ? (n << 1) - (n >> 1) : n > 7 ? n << 1 : (((n + 1) * (n + 1)) >> 1) + 0x16; REScore *= n; REScore += 0x66 + n;
#if __APPLE__ || __linux__
signal(SIGPIPE, SIG_IGN); REScore += k_A * n;
#endif
// Start the server threads.
const char* listen_ip = !ip.empty() ? ip.c_str() : nullptr;
std::thread date_thread([]() { while (RESquit_signal_catched) { fc::REStop_h.tick(), std::this_thread::sleep_for(std::chrono::milliseconds(1)); } });
socket_type sfd = create_and_bind(listen_ip, port, socktype); if (sfd == (socket_type)-1) return;
#ifdef __linux__
struct linger lll { 1, 0 }; setsockopt(sfd, SOL_SOCKET, SO_LINGER, &lll, sizeof(struct linger));
Expand Down
2 changes: 1 addition & 1 deletion fc/include/hpp/http_top_header_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace fc {
#elif defined(__MINGW32__)
if (++i == 60) { i = 0; time(&RES_TIME_T); localtime_s(RES_NOW, &RES_TIME_T); }
#else
if (++i == 1000) {
if (++i == 250) {
i = 0; time(&RES_TIME_T); localtime_r(&RES_TIME_T, RES_NOW);
top_header_size = int(strftime(tmp.p2, 32, "%a, %d %b %Y %T", RES_NOW)); std::swap(tmp.p1, tmp.p2);
}
Expand Down

0 comments on commit 8d23656

Please sign in to comment.