WIP: rewrite the network stack to use libevent

This commit is contained in:
2022-03-09 21:30:51 +02:00
parent d2f43a8620
commit 64596d344c
43 changed files with 415 additions and 2149 deletions

View File

@ -1,95 +0,0 @@
#ifndef __INC_LIBTHECORE_FDWATCH_H__
#define __INC_LIBTHECORE_FDWATCH_H__
#if defined(WIN32) || defined(__linux__)
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,
};
struct fdwatch
{
fd_set rfd_set;
fd_set wfd_set;
socket_t* select_fds;
int* select_rfdidx;
int nselect_fds;
fd_set working_rfd_set;
fd_set working_wfd_set;
int nfiles;
void** fd_data;
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
extern "C"
{
#endif /* __cplusplus */
extern LPFDWATCH fdwatch_new(int nfiles);
extern void fdwatch_clear_fd(LPFDWATCH fdw, socket_t fd);
extern void fdwatch_delete(LPFDWATCH fdw);
extern int fdwatch_check_fd(LPFDWATCH fdw, socket_t fd);
extern int fdwatch_check_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx);
extern void fdwatch_clear_event(LPFDWATCH fdw, socket_t fd, unsigned int event_idx);
extern void fdwatch_add_fd(LPFDWATCH fdw, socket_t fd, void* client_data, int rw, int oneshot);
extern int fdwatch(LPFDWATCH fdw, struct timeval *timeout);
extern void * fdwatch_get_client_data(LPFDWATCH fdw, unsigned int event_idx);
extern void fdwatch_del_fd(LPFDWATCH fdw, socket_t fd);
extern int fdwatch_get_buffer_size(LPFDWATCH fdw, socket_t fd);
extern int fdwatch_get_ident(LPFDWATCH fdw, unsigned int event_idx);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,45 +0,0 @@
/*
* Filename: socket.c
* Description: <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>Լ<EFBFBD> <20><><EFBFBD><EFBFBD>.
*
* Author: <20><><EFBFBD><EFBFBD> (server), myevan (Client)
*/
#ifndef __INC_LIBTHECORE_SOCKET_H__
#define __INC_LIBTHECORE_SOCKET_H__
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
#ifdef __WIN32__
typedef int socklen_t;
#else
#define INVALID_SOCKET -1
#endif
extern int socket_read(socket_t desc, char* read_point, size_t space_left);
extern int socket_write(socket_t desc, const char *data, size_t length);
extern int socket_udp_read(socket_t desc, char * read_point, size_t space_left, struct sockaddr * from, socklen_t * fromlen);
extern int socket_tcp_bind(const char * ip, int port);
extern int socket_udp_bind(const char * ip, int port);
extern socket_t socket_accept(socket_t s, struct sockaddr_in *peer);
extern void socket_close(socket_t s);
extern socket_t socket_connect(const char* host, WORD port);
extern void socket_nonblock(socket_t s);
extern void socket_block(socket_t s);
extern void socket_dontroute(socket_t s);
extern void socket_lingeroff(socket_t s);
extern void socket_lingeron(socket_t s);
extern void socket_sndbuf(socket_t s, unsigned int opt);
extern void socket_rcvbuf(socket_t s, unsigned int opt);
#ifdef __cplusplus
};
#endif
#endif

View File

@ -1,100 +1,16 @@
#ifndef __INC_LIBTHECORE_STDAFX_H__
#define __INC_LIBTHECORE_STDAFX_H__
#if defined(__GNUC__)
#define INLINE __inline__
#elif defined(_MSC_VER)
#define INLINE inline
#endif
#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <tchar.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <conio.h>
#include <process.h>
#include <limits.h>
#include <math.h>
#include <locale.h>
#include <io.h>
#include <direct.h>
#include <fcntl.h>
#include "xdirent.h"
#include "xgetopt.h"
#define S_ISDIR(m) (m & _S_IFDIR)
#define snprintf _snprintf
struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
#define __USE_SELECT__
#define PATH_MAX _MAX_PATH
// C runtime library adjustments
#define strncat(dst, src, size) strcat_s(dst, size, src)
#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)
#define strncasecmp(s1, s2, n) strnicmp(s1, s2, n)
#define atoll(str) _atoi64(str)
#define localtime_r(timet, result) localtime_s(result, timet)
#define strtok_r(s, delim, ptrptr) strtok_s(s, delim, ptrptr)
#include <boost/typeof/typeof.hpp>
#define typeof(t) BOOST_TYPEOF(t)
// dummy declaration of non-supported signals
#define SIGUSR1 30 /* user defined signal 1 */
#define SIGUSR2 31 /* user defined signal 2 */
inline void usleep(unsigned long usec) {
::Sleep(usec / 1000);
}
inline unsigned sleep(unsigned sec) {
::Sleep(sec * 1000);
return 0;
}
inline double rint(double x)
{
return ::floor(x+.5);
}
#else
#ifndef __FreeBSD__
#define __USE_SELECT__
#ifdef __CYGWIN__
#define _POSIX_SOURCE 1
#endif
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <cerrno>
#include <cstdlib>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <cassert>
#include <cctype>
#include <climits>
#include <dirent.h>
#include <sys/time.h>
@ -112,28 +28,10 @@ inline double rint(double x)
#include <pthread.h>
#include <semaphore.h>
#ifdef __FreeBSD__
#include <sys/event.h>
#endif
#endif
#ifndef false
#define false 0
#define true (!false)
#endif
#ifndef FALSE
#define FALSE false
#define TRUE (!FALSE)
#endif
#include "typedef.h"
#include "heart.h"
#include "fdwatch.h"
#include "socket.h"
#include "buffer.h"
#include "signal.h"
#include "signals.h"
#include "log.h"
#include "main.h"
#include "utils.h"

View File

@ -1,27 +1,23 @@
#ifndef __INC_LIBTHECORE_TYPEDEF_H__
#define __INC_LIBTHECORE_TYPEDEF_H__
typedef unsigned long int QWORD;
typedef unsigned char UBYTE;
typedef signed char sbyte;
typedef unsigned short sh_int;
#include <cstdint>
typedef uint64_t QWORD;
typedef uint8_t UBYTE;
typedef int8_t sbyte;
typedef uint16_t sh_int;
#ifndef __WIN32__
#ifndef __cplusplus
typedef unsigned char bool;
#endif
typedef unsigned int DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef long LONG;
typedef unsigned long ULONG;
typedef int INT;
typedef unsigned int UINT;
typedef int socket_t;
typedef uint32_t DWORD;
typedef uint32_t BOOL;
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef int32_t LONG;
typedef uint32_t ULONG;
typedef int32_t INT;
typedef uint32_t UINT;
#else