Database now runs on linux

This commit is contained in:
2022-03-05 19:59:39 +02:00
parent f4f90b2533
commit 90ef09c331
131 changed files with 467 additions and 446 deletions

View File

@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 2.8)
project(libthecore C)
project(libthecore CXX)
# Find source files
file(GLOB SOURCES
"src/*.c"
"src/*.cpp"
"include/*.h"
)

View File

@ -1,41 +1,7 @@
#ifndef __INC_LIBTHECORE_FDWATCH_H__
#define __INC_LIBTHECORE_FDWATCH_H__
#ifndef __WIN32__
typedef struct fdwatch FDWATCH;
typedef struct fdwatch * LPFDWATCH;
enum EFdwatch
{
FDW_NONE = 0,
FDW_READ = 1,
FDW_WRITE = 2,
FDW_WRITE_ONESHOT = 4,
FDW_EOF = 8,
};
typedef struct kevent KEVENT;
typedef struct kevent * LPKEVENT;
typedef int KQUEUE;
struct fdwatch
{
KQUEUE kq;
int nfiles;
LPKEVENT kqevents;
int nkqevents;
LPKEVENT kqrevents;
int * fd_event_idx;
void ** fd_data;
int * fd_rw;
};
#else
#if defined(WIN32) || defined(__linux__)
typedef struct fdwatch FDWATCH;
typedef struct fdwatch * LPFDWATCH;
@ -68,6 +34,40 @@
int* fd_rw;
};
#else
typedef struct fdwatch FDWATCH;
typedef struct fdwatch* LPFDWATCH;
enum EFdwatch
{
FDW_NONE = 0,
FDW_READ = 1,
FDW_WRITE = 2,
FDW_WRITE_ONESHOT = 4,
FDW_EOF = 8,
};
typedef struct kevent KEVENT;
typedef struct kevent* LPKEVENT;
typedef int KQUEUE;
struct fdwatch
{
KQUEUE kq;
int nfiles;
LPKEVENT kqevents;
int nkqevents;
LPKEVENT kqrevents;
int* fd_event_idx;
void** fd_data;
int* fd_rw;
};
#endif // WIN32
#ifdef __cplusplus

View File

@ -47,7 +47,7 @@ struct timespec
// C runtime library adjustments
#define strlcat(dst, src, size) strcat_s(dst, size, src)
#define strlcpy(dst, src, size) strncpy_s(dst, size, src, _TRUNCATE)
#define strncpy(dst, src, size) strncpy_s(dst, size, src, _TRUNCATE)
#define strtoull(str, endptr, base) _strtoui64(str, endptr, base)
#define strtof(str, endptr) (float)strtod(str, endptr)
#define strcasecmp(s1, s2) stricmp(s1, s2)

View File

@ -113,6 +113,12 @@ void _sys_err(const char *func, int line, const char *format, ...)
time_t ct = time(0);
char *time_s = asctime(localtime(&ct));
struct timeval tv;
int nMiliSec = 0;
gettimeofday(&tv, NULL);
char buf[1024 + 2]; // \n을 붙이기 위해..
int len;
@ -120,7 +126,7 @@ void _sys_err(const char *func, int line, const char *format, ...)
return;
time_s[strlen(time_s) - 1] = '\0';
len = snprintf(buf, 1024, "SYSERR: %-15.15s :: %s: ", time_s + 4, func);
len = snprintf(buf, 1024, "SYSERR: %-15.15s.%d :: %s: ", time_s + 4, tv.tv_usec, func);
buf[1025] = '\0';
if (len < 1024)
@ -190,6 +196,10 @@ void sys_log(unsigned int bit, const char *format, ...)
{
va_list args;
struct timeval tv;
int nMiliSec = 0;
gettimeofday(&tv, NULL);
if (bit != 0 && !(log_level_bits & bit))
return;
@ -201,7 +211,7 @@ void sys_log(unsigned int bit, const char *format, ...)
fprintf(log_file_sys->fp, sys_log_header_string);
time_s[strlen(time_s) - 1] = '\0';
fprintf(log_file_sys->fp, "%-15.15s :: ", time_s + 4);
fprintf(log_file_sys->fp, "%-15.15s.%d :: ", time_s + 4, tv.tv_usec );
va_start(args, format);
vfprintf(log_file_sys->fp, format, args);

View File

@ -50,7 +50,7 @@ static void pid_deinit(void)
int thecore_init(int fps, HEARTFUNC heartbeat_func)
{
#ifdef __WIN32__
#if defined(__WIN32__) || defined(__linux__)
srand(time(0));
#else
srandom(time(0) + getpid() + getuid());

View File

@ -7,7 +7,7 @@
#define __LIBTHECORE__
#include "stdafx.h"
#ifdef __WIN32__
#if defined(__WIN32__) || defined(__linux__)
void signal_setup() {}
void signal_timer_disable() {}
void signal_timer_enable(int timeout_seconds) {}

View File

@ -157,14 +157,16 @@ int socket_bind(const char * ip, int port, int protocol)
}
socket_reuse(s);
#ifndef __WIN32__
socket_lingeroff(s);
#else
#ifndef __linux__
#if defined(WIN32)
// Winsock2: SO_DONTLINGER, SO_KEEPALIVE, SO_LINGER, and SO_OOBINLINE are
// not supported on sockets of type SOCK_DGRAM
if (protocol == SOCK_STREAM) {
socket_lingeroff(s);
}
#else
socket_lingeroff(s);
#endif
#endif
memset(&sa, 0, sizeof(sa));
@ -183,8 +185,9 @@ int socket_bind(const char * ip, int port, int protocol)
sys_err("bind: %s", strerror(errno));
return -1;
}
#ifndef __linux__
socket_nonblock(s);
#endif
if (protocol == SOCK_STREAM)
{
@ -235,9 +238,10 @@ socket_t socket_accept(socket_t s, struct sockaddr_in *peer)
socket_close(s);
return -1;
}
#ifndef __linux__
socket_nonblock(desc);
socket_lingeroff(desc);
#endif
return (desc);
}
@ -386,7 +390,7 @@ void socket_dontroute(socket_t s)
void socket_lingeroff(socket_t s)
{
#ifdef __WIN32__
#if defined(__WIN32__)
int linger;
linger = 0;
#else
@ -405,7 +409,7 @@ void socket_lingeroff(socket_t s)
void socket_lingeron(socket_t s)
{
#ifdef __WIN32__
#if defined(__WIN32__)
int linger;
linger = 0;
#else
@ -487,6 +491,8 @@ void socket_timeout(socket_t s, long sec, long usec)
socklen_t optlen = sizeof(rcvopt);
sndopt = rcvopt = (sec * 1000) + (usec / 1000);
#endif
#ifndef __linux__
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*) &rcvopt, optlen) < 0)
{
sys_err("setsockopt: timeout: %s", strerror(errno));
@ -500,9 +506,10 @@ void socket_timeout(socket_t s, long sec, long usec)
socket_close(s);
return;
}
#endif
optlen = sizeof(sndopt);
#ifndef __linux__
if (setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*) &sndopt, optlen) < 0)
{
sys_err("setsockopt: timeout: %s", strerror(errno));
@ -516,6 +523,7 @@ void socket_timeout(socket_t s, long sec, long usec)
socket_close(s);
return;
}
#endif
#ifndef __WIN32__
sys_log(1, "SYSTEM: %d: TIMEOUT RCV: %d.%d, SND: %d.%d", s, rcvopt.tv_sec, rcvopt.tv_usec, sndopt.tv_sec, sndopt.tv_usec);