forked from metin2/server
Refactored directory structure, added game files from TMP
This commit is contained in:
20
src/libthecore/CMakeLists.txt
Normal file
20
src/libthecore/CMakeLists.txt
Normal file
@ -0,0 +1,20 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
project(libthecore CXX)
|
||||
|
||||
# Find source files
|
||||
file(GLOB SOURCES
|
||||
"src/*.cpp"
|
||||
"include/*.h"
|
||||
)
|
||||
|
||||
# Include header files
|
||||
include_directories("include")
|
||||
|
||||
# Create shared library
|
||||
add_library(${PROJECT_NAME} STATIC ${SOURCES})
|
||||
|
||||
# Find dependencies
|
||||
find_package(Boost REQUIRED)
|
||||
include_directories(${Boost_INCLUDE_DIRS})
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES})
|
57
src/libthecore/include/buffer.h
Normal file
57
src/libthecore/include/buffer.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Filename: buffer.h
|
||||
* Description: Buffer ó<><C3B3> <20><><EFBFBD><EFBFBD>
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (aka. <20><><EFBFBD><EFBFBD>, Cronan), <20>ۿ<EFBFBD><DBBF><EFBFBD> (aka. myevan, <20><><EFBFBD>ڷ<EFBFBD>)
|
||||
*/
|
||||
#ifndef __INC_LIBTHECORE_BUFFER_H__
|
||||
#define __INC_LIBTHECORE_BUFFER_H__
|
||||
|
||||
#define SAFE_BUFFER_DELETE(buf) { if(buf != NULL) { buffer_delete(buf); buf = NULL; } }
|
||||
|
||||
typedef struct buffer BUFFER;
|
||||
typedef struct buffer * LPBUFFER;
|
||||
|
||||
struct buffer
|
||||
{
|
||||
struct buffer * next;
|
||||
|
||||
char * write_point;
|
||||
int write_point_pos;
|
||||
|
||||
const char * read_point;
|
||||
int length;
|
||||
|
||||
char * mem_data;
|
||||
int mem_size;
|
||||
|
||||
int flag;
|
||||
};
|
||||
|
||||
extern LPBUFFER buffer_new(int size); // <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
extern void buffer_delete(LPBUFFER buffer); // <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
extern void buffer_reset(LPBUFFER buffer); // <20><><EFBFBD><EFBFBD> <20><><EFBFBD>̵<EFBFBD><CCB5><EFBFBD> <20>ʱ<EFBFBD>ȭ
|
||||
|
||||
extern DWORD buffer_size(LPBUFFER buffer); // <20><><EFBFBD>ۿ<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
extern int buffer_has_space(LPBUFFER buffer); // <20><> <20><> <20>ִ<EFBFBD> <20><><EFBFBD≯<EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
|
||||
extern void buffer_write (LPBUFFER& buffer, const void* src, int length); // <20><><EFBFBD>ۿ<EFBFBD> <20><><EFBFBD><EFBFBD>.
|
||||
extern void buffer_read(LPBUFFER buffer, void * buf, int bytes); // <20><><EFBFBD>ۿ<EFBFBD><DBBF><EFBFBD> <20>д´<D0B4>.
|
||||
extern BYTE buffer_get_byte(LPBUFFER buffer);
|
||||
extern WORD buffer_get_word(LPBUFFER buffer);
|
||||
extern DWORD buffer_get_dword(LPBUFFER buffer);
|
||||
|
||||
// buffer_proceed <20>Լ<EFBFBD><D4BC><EFBFBD> buffer_peek<65><6B><EFBFBD><EFBFBD> <20>б<EFBFBD><D0B1><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>Ƽ<DEBE> <20><> <20>ʿ䰡
|
||||
// <20><><EFBFBD><EFBFBD> <20><> ó<><C3B3><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><EFBFBD> ó<><C3B3><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ٰ<EFBFBD> <20>뺸<EFBFBD>ؾ<EFBFBD> <20><> <20><> <20><><EFBFBD><EFBFBD>.
|
||||
// (buffer_read, buffer_get_* <20>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>쿡<EFBFBD><ECBFA1> <20>˾Ƽ<CBBE> ó<><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> peek<65><6B><EFBFBD><EFBFBD> ó<><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// <20><><EFBFBD><EFBFBD> <20><EFBFBD><D7B7><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>)
|
||||
extern const void * buffer_read_peek(LPBUFFER buffer); // <20>д<EFBFBD> <20><>ġ<EFBFBD><C4A1> <20><><EFBFBD><EFBFBD>
|
||||
extern void buffer_read_proceed(LPBUFFER buffer, int length); // length<74><68>ŭ<EFBFBD><C5AD> ó<><C3B3><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> write_peek<65><6B><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><>ġ<EFBFBD><C4A1> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><EFBFBD> <20>質 <20>뺸<EFBFBD><EBBAB8> <20><>
|
||||
// buffer_write_proceed<65><64> <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>.
|
||||
extern void * buffer_write_peek(LPBUFFER buffer); // <20><><EFBFBD><EFBFBD> <20><>ġ<EFBFBD><C4A1> <20><><EFBFBD><EFBFBD>
|
||||
extern void buffer_write_proceed(LPBUFFER buffer, int length); // length<74><68> <20><><EFBFBD><EFBFBD> <20><>Ų<EFBFBD><C5B2>.
|
||||
|
||||
extern void buffer_adjust_size(LPBUFFER & buffer, int add_size); // add_size<7A><65>ŭ <20>߰<EFBFBD><DFB0><EFBFBD> ũ<>⸦ Ȯ<><C8AE>
|
||||
#endif
|
26
src/libthecore/include/heart.h
Normal file
26
src/libthecore/include/heart.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef __INC_LIBTHECORE_HEART_H__
|
||||
#define __INC_LIBTHECORE_HEART_H__
|
||||
|
||||
typedef struct heart HEART;
|
||||
typedef struct heart * LPHEART;
|
||||
|
||||
typedef void (*HEARTFUNC) (LPHEART heart, int pulse);
|
||||
|
||||
struct heart
|
||||
{
|
||||
HEARTFUNC func;
|
||||
|
||||
struct timeval before_sleep;
|
||||
struct timeval opt_time;
|
||||
struct timeval last_time;
|
||||
|
||||
int passes_per_sec;
|
||||
int pulse;
|
||||
};
|
||||
|
||||
extern LPHEART heart_new(int opt_usec, HEARTFUNC func);
|
||||
extern void heart_delete(LPHEART ht);
|
||||
extern int heart_idle(LPHEART ht); // <20><> pulse<73><65> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>.
|
||||
extern void heart_beat(LPHEART ht, int pulses);
|
||||
|
||||
#endif
|
39
src/libthecore/include/log.h
Normal file
39
src/libthecore/include/log.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef __INC_LIBTHECORE_LOG_H__
|
||||
#define __INC_LIBTHECORE_LOG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
extern int log_init(void);
|
||||
extern void log_destroy(void);
|
||||
extern void log_rotate(void);
|
||||
|
||||
// <20>α<EFBFBD> <20><><EFBFBD><EFBFBD> ó<><C3B3> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> bitvector<6F><72> ó<><C3B3><EFBFBD>ȴ<EFBFBD>)
|
||||
extern void log_set_level(unsigned int level);
|
||||
extern void log_unset_level(unsigned int level);
|
||||
|
||||
// <20>α<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>ŭ <20><><EFBFBD><EFBFBD><EFBFBD>ϴ°<CFB4><C2B0><EFBFBD> <20><><EFBFBD><EFBFBD> <20>Լ<EFBFBD>
|
||||
extern void log_set_expiration_days(unsigned int days);
|
||||
extern int log_get_expiration_days(void);
|
||||
|
||||
#ifndef __WIN32__
|
||||
extern void _sys_err(const char *func, int line, const char *format, ...);
|
||||
#else
|
||||
extern void _sys_err(const char *func, int line, const char *format, ...);
|
||||
#endif
|
||||
extern void sys_log_header(const char *header);
|
||||
extern void sys_log(unsigned int lv, const char *format, ...);
|
||||
extern void pt_log(const char *format, ...);
|
||||
|
||||
#ifndef __WIN32__
|
||||
#define sys_err(fmt, args...) _sys_err(__FUNCTION__, __LINE__, fmt, ##args)
|
||||
#else
|
||||
#define sys_err(fmt, ...) _sys_err(__FUNCTION__, __LINE__, fmt, __VA_ARGS__)
|
||||
#endif // __WIN32__
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // __INC_LOG_H__
|
40
src/libthecore/include/main.h
Normal file
40
src/libthecore/include/main.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef __INC_LIBTHECORE_MAIN_H__
|
||||
#define __INC_LIBTHECORE_MAIN_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifdef __LIBTHECORE__
|
||||
extern volatile int tics;
|
||||
extern volatile int shutdowned;
|
||||
#endif
|
||||
#include "heart.h"
|
||||
|
||||
extern LPHEART thecore_heart;
|
||||
|
||||
enum ENUM_PROFILER
|
||||
{
|
||||
PF_IDLE,
|
||||
PF_HEARTBEAT,
|
||||
NUM_PF
|
||||
};
|
||||
|
||||
extern unsigned int thecore_profiler[NUM_PF];
|
||||
|
||||
extern int thecore_init(int fps, HEARTFUNC heartbeat_func);
|
||||
extern int thecore_idle(void);
|
||||
extern void thecore_shutdown(void);
|
||||
extern void thecore_destroy(void);
|
||||
extern int thecore_pulse(void);
|
||||
extern float thecore_time(void);
|
||||
extern float thecore_pulse_per_second(void);
|
||||
extern int thecore_is_shutdowned(void);
|
||||
|
||||
extern void thecore_tick(void); // tics <20><><EFBFBD><EFBFBD>
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
16
src/libthecore/include/signals.h
Normal file
16
src/libthecore/include/signals.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __INC_LIBTHECORE_SIGNAL_H__
|
||||
#define __INC_LIBTHECORE_SIGNAL_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
extern void signal_setup();
|
||||
extern void signal_timer_disable();
|
||||
extern void signal_timer_enable(int timeout_seconds);
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
42
src/libthecore/include/stdafx.h
Normal file
42
src/libthecore/include/stdafx.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef __INC_LIBTHECORE_STDAFX_H__
|
||||
#define __INC_LIBTHECORE_STDAFX_H__
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdarg>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <dirent.h>
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <sys/signal.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <bsd/string.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "typedef.h"
|
||||
#include "heart.h"
|
||||
#include "buffer.h"
|
||||
#include "signals.h"
|
||||
#include "log.h"
|
||||
#include "main.h"
|
||||
#include "utils.h"
|
||||
|
||||
#endif // __INC_LIBTHECORE_STDAFX_H__
|
58
src/libthecore/include/typedef.h
Normal file
58
src/libthecore/include/typedef.h
Normal file
@ -0,0 +1,58 @@
|
||||
#ifndef __INC_LIBTHECORE_TYPEDEF_H__
|
||||
#define __INC_LIBTHECORE_TYPEDEF_H__
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
typedef uint64_t QWORD;
|
||||
typedef uint8_t UBYTE;
|
||||
typedef int8_t sbyte;
|
||||
typedef uint16_t sh_int;
|
||||
|
||||
#ifndef __WIN32__
|
||||
|
||||
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
|
||||
|
||||
struct timezone
|
||||
{
|
||||
int tz_minuteswest; /* minutes west of Greenwich */
|
||||
int tz_dsttime; /* type of dst correction */
|
||||
};
|
||||
|
||||
typedef SOCKET socket_t;
|
||||
|
||||
#if !defined(_W64)
|
||||
#if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
|
||||
#define _W64 __w64
|
||||
#else
|
||||
#define _W64
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef _WIN64
|
||||
typedef __int64 ssize_t;
|
||||
#else
|
||||
typedef _W64 int ssize_t;
|
||||
#endif
|
||||
|
||||
// Fixed-size integer types
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#endif
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
#endif
|
||||
|
||||
#endif // __INC_LIBTHECORE_TYPEDEF_H__
|
127
src/libthecore/include/utils.h
Normal file
127
src/libthecore/include/utils.h
Normal file
@ -0,0 +1,127 @@
|
||||
#ifndef __INC_LIBTHECORE_UTILS_H__
|
||||
#define __INC_LIBTHECORE_UTILS_H__
|
||||
|
||||
// Random number generator
|
||||
#include "effolkronium/random.hpp"
|
||||
using Random = effolkronium::random_static;
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#define LOWER(c) (((c)>='A' && (c) <= 'Z') ? ((c)+('a'-'A')) : (c))
|
||||
#define UPPER(c) (((c)>='a' && (c) <= 'z') ? ((c)+('A'-'a')) : (c))
|
||||
|
||||
#define str_cmp strcasecmp
|
||||
|
||||
#define core_dump() core_dump_unix(__FILE__, __LINE__)
|
||||
extern void core_dump_unix(const char *who, WORD line); // <20>ھ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
|
||||
#define TOKEN(string) if (!str_cmp(token_string, string))
|
||||
// src = <20><>ū : <20><>
|
||||
extern void parse_token(char * src, char * token, char * value);
|
||||
|
||||
extern void trim_and_lower(const char * src, char * dest, size_t dest_size);
|
||||
|
||||
// a<><61> b<><62> <20>ð<EFBFBD><C3B0><EFBFBD> <20><EFBFBD> <20><><EFBFBD>̳<EFBFBD><CCB3><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
extern struct timeval * timediff(const struct timeval *a, const struct timeval *b);
|
||||
|
||||
// a<><61> <20>ð<EFBFBD><C3B0><EFBFBD> b<><62> <20>ð<EFBFBD><C3B0><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
extern struct timeval * timeadd(struct timeval *a, struct timeval *b);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD> <20>ð<EFBFBD> curr_tm<74><6D><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> days<79><73> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
extern struct tm * tm_calc(const struct tm *curr_tm, int days);
|
||||
|
||||
extern void thecore_sleep(struct timeval * timeout); // timeout<75><74>ŭ <20><><EFBFBD>μ<EFBFBD><CEBC><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
|
||||
extern float get_float_time();
|
||||
extern DWORD get_dword_time();
|
||||
|
||||
extern char * time_str(time_t ct);
|
||||
|
||||
std::string GetSocketHost(const sockaddr * address);
|
||||
in_port_t GetSocketPort(const sockaddr * address);
|
||||
|
||||
#define CREATE(result, type, number) do { \
|
||||
if (!((result) = (type *) calloc ((number), sizeof(type)))) { \
|
||||
sys_err("calloc failed [%d] %s", errno, strerror(errno)); \
|
||||
abort(); } } while(0)
|
||||
|
||||
#define RECREATE(result,type,number) do { \
|
||||
if (!((result) = (type *) realloc ((result), sizeof(type) * (number)))) { \
|
||||
sys_err("realloc failed [%d] %s", errno, strerror(errno)); \
|
||||
abort(); } } while(0)
|
||||
|
||||
// Next <20><> Prev <20><> <20>ִ<EFBFBD> <20><><EFBFBD><EFBFBD>Ʈ<EFBFBD><C6AE> <20>߰<EFBFBD>
|
||||
#define INSERT_TO_TW_LIST(item, head, prev, next) \
|
||||
if (!(head)) \
|
||||
{ \
|
||||
head = item; \
|
||||
(head)->prev = (head)->next = NULL; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
(head)->prev = item; \
|
||||
(item)->next = head; \
|
||||
(item)->prev = NULL; \
|
||||
head = item; \
|
||||
}
|
||||
|
||||
#define REMOVE_FROM_TW_LIST(item, head, prev, next) \
|
||||
if ((item) == (head)) \
|
||||
{ \
|
||||
if (((head) = (item)->next)) \
|
||||
(head)->prev = NULL; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
if ((item)->next) \
|
||||
(item)->next->prev = (item)->prev; \
|
||||
\
|
||||
if ((item)->prev) \
|
||||
(item)->prev->next = (item)->next; \
|
||||
}
|
||||
|
||||
|
||||
#define INSERT_TO_LIST(item, head, next) \
|
||||
(item)->next = (head); \
|
||||
(head) = (item); \
|
||||
|
||||
#define REMOVE_FROM_LIST(item, head, next) \
|
||||
if ((item) == (head)) \
|
||||
head = (item)->next; \
|
||||
else \
|
||||
{ \
|
||||
temp = head; \
|
||||
\
|
||||
while (temp && (temp->next != (item))) \
|
||||
temp = temp->next; \
|
||||
\
|
||||
if (temp) \
|
||||
temp->next = (item)->next; \
|
||||
} \
|
||||
|
||||
#ifndef MAKEFOURCC
|
||||
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
|
||||
((DWORD)(BYTE) (ch0 ) | ((DWORD)(BYTE) (ch1) << 8) | \
|
||||
((DWORD)(BYTE) (ch2) << 16) | ((DWORD)(BYTE) (ch3) << 24))
|
||||
#endif // defined(MAKEFOURCC)
|
||||
|
||||
|
||||
// _countof for gcc/g++
|
||||
#if !defined(_countof)
|
||||
#if !defined(__cplusplus)
|
||||
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
|
||||
#else
|
||||
extern "C++"
|
||||
{
|
||||
template <typename _CountofType, size_t _SizeOfArray>
|
||||
char (*__countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
|
||||
#define _countof(_Array) sizeof(*__countof_helper(_Array))
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __WIN32__
|
||||
extern void gettimeofday(struct timeval* t, struct timezone* dummy);
|
||||
#endif
|
||||
|
||||
#endif // __INC_UTILS_H__
|
294
src/libthecore/src/buffer.cpp
Normal file
294
src/libthecore/src/buffer.cpp
Normal file
@ -0,0 +1,294 @@
|
||||
/*
|
||||
* Filename: buffer.c
|
||||
* Description: Buffer ó<><C3B3> <20><><EFBFBD><EFBFBD>
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (aka. <20><><EFBFBD><EFBFBD>, Cronan)
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
|
||||
static LPBUFFER normalized_buffer_pool[32] = { NULL, };
|
||||
|
||||
#define DEFAULT_POOL_SIZE 8192
|
||||
|
||||
// internal function forward
|
||||
void buffer_realloc(LPBUFFER& buffer, int length);
|
||||
|
||||
static int buffer_get_pool_index(int size) {
|
||||
int i;
|
||||
for (i = 0; i < 32; ++i) {
|
||||
if ((1 << i) >= size) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1; // too big... not pooled
|
||||
}
|
||||
static int buffer_get_exac_pool_index(int size) {
|
||||
int i;
|
||||
for (i = 0; i < 32; ++i) {
|
||||
if ((1 << i) == size) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1; // too big... not pooled
|
||||
}
|
||||
// <20><><EFBFBD><EFBFBD> buffer pool <20><><EFBFBD><EFBFBD>.
|
||||
static void buffer_pool_free ()
|
||||
{
|
||||
for (int i = 31; i >= 0; i--)
|
||||
{
|
||||
if (normalized_buffer_pool[i] != NULL)
|
||||
{
|
||||
LPBUFFER next;
|
||||
for (LPBUFFER p = normalized_buffer_pool[i]; p != NULL; p = next)
|
||||
{
|
||||
next = p->next;
|
||||
free(p->mem_data);
|
||||
free(p);
|
||||
}
|
||||
normalized_buffer_pool[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
// n<><6E><EFBFBD><EFBFBD> ū buffer pool <20>ϳ<EFBFBD><CFB3><EFBFBD> <20><><EFBFBD><EFBFBD>.
|
||||
static bool buffer_larger_pool_free (int n)
|
||||
{
|
||||
for (int i = 31; i > n; i--)
|
||||
{
|
||||
if (normalized_buffer_pool[i] != NULL)
|
||||
{
|
||||
LPBUFFER buffer = normalized_buffer_pool[i];
|
||||
LPBUFFER next = buffer->next;
|
||||
free(buffer->mem_data);
|
||||
free(buffer);
|
||||
normalized_buffer_pool[i] = next;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool safe_create(char** pdata, int number)
|
||||
{
|
||||
if (!((*pdata) = (char *) calloc (number, sizeof(char))))
|
||||
{
|
||||
sys_err("calloc failed [%d] %s", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
LPBUFFER buffer_new(int size)
|
||||
{
|
||||
if (size < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
LPBUFFER buffer = NULL;
|
||||
int pool_index = buffer_get_pool_index(size);
|
||||
if (pool_index >= 0) {
|
||||
BUFFER** buffer_pool = normalized_buffer_pool + pool_index;
|
||||
size = 1 << pool_index;
|
||||
|
||||
if (*buffer_pool) {
|
||||
buffer = *buffer_pool;
|
||||
*buffer_pool = buffer->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer == NULL)
|
||||
{
|
||||
CREATE(buffer, BUFFER, 1);
|
||||
buffer->mem_size = size;
|
||||
// buffer_new<65><77><EFBFBD><EFBFBD> calloc failed<65><64> <20><><EFBFBD><EFBFBD> <20><EFBFBD><DFBB>Ͽ<EFBFBD>(<28><>Ű<EFBFBD><C5B0> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ӽſ<D3BD><C5BF><EFBFBD> <20>ַ<EFBFBD> <20><EFBFBD>),
|
||||
// calloc<6F><63> <20><><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD>, buffer pool<6F><6C> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ٽ<EFBFBD> <20>õ<EFBFBD><C3B5>Ѵ<EFBFBD>.
|
||||
if (!safe_create(&buffer->mem_data, size))
|
||||
{
|
||||
// <20>ʿ<EFBFBD><CABF><EFBFBD> buffer<65><72><EFBFBD><EFBFBD> ū buffer pool<6F><6C><EFBFBD><EFBFBD> <20>ϳ<EFBFBD><CFB3><EFBFBD> <20><><EFBFBD><EFBFBD>.
|
||||
if (!buffer_larger_pool_free(pool_index))
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD> pool<6F><6C> <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>.
|
||||
buffer_pool_free();
|
||||
CREATE(buffer->mem_data, char, size);
|
||||
sys_err ("buffer pool free success.");
|
||||
}
|
||||
}
|
||||
assert(buffer != NULL);
|
||||
assert(buffer->mem_size == size);
|
||||
assert(buffer->mem_data != NULL);
|
||||
|
||||
buffer_reset(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void buffer_delete(LPBUFFER buffer)
|
||||
{
|
||||
if (buffer == NULL) {
|
||||
return;
|
||||
}
|
||||
buffer_reset(buffer);
|
||||
|
||||
int size = buffer->mem_size;
|
||||
|
||||
int pool_index = buffer_get_exac_pool_index(size);
|
||||
if (pool_index >= 0) {
|
||||
BUFFER** buffer_pool = normalized_buffer_pool + pool_index;
|
||||
buffer->next = *buffer_pool;
|
||||
*buffer_pool = buffer;
|
||||
}
|
||||
else {
|
||||
free(buffer->mem_data);
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
DWORD buffer_size(LPBUFFER buffer)
|
||||
{
|
||||
return (buffer->length);
|
||||
}
|
||||
|
||||
void buffer_reset(LPBUFFER buffer)
|
||||
{
|
||||
buffer->read_point = buffer->mem_data;
|
||||
buffer->write_point = buffer->mem_data;
|
||||
buffer->write_point_pos = 0;
|
||||
buffer->length = 0;
|
||||
buffer->next = NULL;
|
||||
buffer->flag = 0;
|
||||
}
|
||||
|
||||
void buffer_write(LPBUFFER& buffer, const void *src, int length)
|
||||
{
|
||||
if (buffer->write_point_pos + length >= buffer->mem_size)
|
||||
buffer_realloc(buffer, buffer->mem_size + length + std::min(10240, length));
|
||||
|
||||
memcpy(buffer->write_point, src, length);
|
||||
buffer_write_proceed(buffer, length);
|
||||
}
|
||||
|
||||
void buffer_read(LPBUFFER buffer, void * buf, int bytes)
|
||||
{
|
||||
memcpy(buf, buffer->read_point, bytes);
|
||||
buffer_read_proceed(buffer, bytes);
|
||||
}
|
||||
|
||||
BYTE buffer_byte(LPBUFFER buffer)
|
||||
{
|
||||
BYTE val = *(BYTE *) buffer->read_point;
|
||||
buffer_read_proceed(buffer, sizeof(BYTE));
|
||||
return val;
|
||||
}
|
||||
|
||||
WORD buffer_word(LPBUFFER buffer)
|
||||
{
|
||||
WORD val = *(WORD *) buffer->read_point;
|
||||
buffer_read_proceed(buffer, sizeof(WORD));
|
||||
return val;
|
||||
}
|
||||
|
||||
DWORD buffer_dword(LPBUFFER buffer)
|
||||
{
|
||||
DWORD val = *(DWORD *) buffer->read_point;
|
||||
buffer_read_proceed(buffer, sizeof(DWORD));
|
||||
return val;
|
||||
}
|
||||
|
||||
const void * buffer_read_peek(LPBUFFER buffer)
|
||||
{
|
||||
return (const void *) buffer->read_point;
|
||||
}
|
||||
|
||||
void buffer_read_proceed(LPBUFFER buffer, int length)
|
||||
{
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
if (length < 0)
|
||||
sys_err("buffer_proceed: length argument lower than zero (length: %d)", length);
|
||||
else if (length > buffer->length)
|
||||
{
|
||||
sys_err("buffer_proceed: length argument bigger than buffer (length: %d, buffer: %d)", length, buffer->length);
|
||||
length = buffer->length;
|
||||
}
|
||||
|
||||
// ó<><C3B3><EFBFBD><EFBFBD> <20><><EFBFBD>̰<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD>̺<EFBFBD><CCBA><EFBFBD> <20>۴ٸ<DBB4>, <20><><EFBFBD>۸<EFBFBD> <20><><EFBFBD>ܵξ<DCB5><CEBE><EFBFBD> <20>Ѵ<EFBFBD>.
|
||||
if (length < buffer->length)
|
||||
{
|
||||
// write_point <20><> pos <20><> <20>״<EFBFBD><D7B4><EFBFBD> <20>ΰ<EFBFBD> read_point <20><> <20><><EFBFBD><EFBFBD> <20><>Ų<EFBFBD><C5B2>.
|
||||
if (buffer->read_point + length - buffer->mem_data > buffer->mem_size)
|
||||
{
|
||||
sys_err("buffer_read_proceed: buffer overflow! length %d read_point %d", length, buffer->read_point - buffer->mem_data);
|
||||
abort();
|
||||
}
|
||||
|
||||
buffer->read_point += length;
|
||||
buffer->length -= length;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer_reset(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void * buffer_write_peek(LPBUFFER buffer)
|
||||
{
|
||||
return (buffer->write_point);
|
||||
}
|
||||
|
||||
void buffer_write_proceed(LPBUFFER buffer, int length)
|
||||
{
|
||||
buffer->length += length;
|
||||
buffer->write_point += length;
|
||||
buffer->write_point_pos += length;
|
||||
}
|
||||
|
||||
int buffer_has_space(LPBUFFER buffer)
|
||||
{
|
||||
return (buffer->mem_size - buffer->write_point_pos);
|
||||
}
|
||||
|
||||
void buffer_adjust_size(LPBUFFER& buffer, int add_size)
|
||||
{
|
||||
if (buffer->mem_size >= buffer->write_point_pos + add_size)
|
||||
return;
|
||||
|
||||
sys_log(0, "buffer_adjust %d current %d/%d", add_size, buffer->length, buffer->mem_size);
|
||||
buffer_realloc(buffer, buffer->mem_size + add_size);
|
||||
}
|
||||
|
||||
void buffer_realloc(LPBUFFER& buffer, int length)
|
||||
{
|
||||
int i, read_point_pos;
|
||||
LPBUFFER temp;
|
||||
|
||||
assert(length >= 0 && "buffer_realloc: length is lower than zero");
|
||||
|
||||
if (buffer->mem_size >= length)
|
||||
return;
|
||||
|
||||
// i <20><> <20><><EFBFBD><EFBFBD> <20>Ҵ<EFBFBD><D2B4><EFBFBD> ũ<><C5A9><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>ũ<EFBFBD><C5A9><EFBFBD><EFBFBD> <20><>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
// <20><EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD> ũ<>⸦ <20><><EFBFBD>Ѵ<EFBFBD>.
|
||||
i = length - buffer->mem_size;
|
||||
|
||||
if (i <= 0)
|
||||
return;
|
||||
|
||||
temp = buffer_new (length);
|
||||
sys_log(0, "reallocating buffer to %d, current %d", temp->mem_size, buffer->mem_size);
|
||||
memcpy(temp->mem_data, buffer->mem_data, buffer->mem_size);
|
||||
|
||||
read_point_pos = buffer->read_point - buffer->mem_data;
|
||||
|
||||
// write_point <20><> read_point <20><> <20><> <20><><EFBFBD><EFBFBD> <20><>Ų<EFBFBD><C5B2>.
|
||||
temp->write_point = temp->mem_data + buffer->write_point_pos;
|
||||
temp->write_point_pos = buffer->write_point_pos;
|
||||
temp->read_point = temp->mem_data + read_point_pos;
|
||||
temp->flag = buffer->flag;
|
||||
temp->next = NULL;
|
||||
temp->length = buffer->length;
|
||||
|
||||
buffer_delete(buffer);
|
||||
buffer = temp;
|
||||
}
|
94
src/libthecore/src/heart.cpp
Normal file
94
src/libthecore/src/heart.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Filename: heart.c
|
||||
* Description: fps <20><> <20>ѹ<EFBFBD><D1B9><EFBFBD> ȣ<><C8A3><EFBFBD>Ǵ<EFBFBD> "<22><><EFBFBD><EFBFBD>" <20>̴<EFBFBD>.
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD> aka. Cronan
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
|
||||
extern volatile int num_events_called;
|
||||
|
||||
LPHEART heart_new(int opt_usec, HEARTFUNC func)
|
||||
{
|
||||
LPHEART ht;
|
||||
|
||||
if (!func)
|
||||
{
|
||||
sys_err("no function defined");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CREATE(ht, HEART, 1);
|
||||
|
||||
ht->func = func;
|
||||
ht->opt_time.tv_sec = 0;
|
||||
ht->opt_time.tv_usec = opt_usec;
|
||||
ht->passes_per_sec = 1000000 / opt_usec;
|
||||
gettimeofday(&ht->last_time, (struct timezone *) 0);
|
||||
gettimeofday(&ht->before_sleep, (struct timezone *) 0);
|
||||
return (ht);
|
||||
}
|
||||
|
||||
void heart_delete(LPHEART ht)
|
||||
{
|
||||
free(ht);
|
||||
}
|
||||
|
||||
int heart_idle(LPHEART ht)
|
||||
{
|
||||
struct timeval now, process_time, timeout, temp_time;
|
||||
int missed_pulse;
|
||||
|
||||
gettimeofday(&ht->before_sleep, (struct timezone *) 0);
|
||||
process_time = *timediff(&ht->before_sleep, &ht->last_time);
|
||||
|
||||
/*
|
||||
* If we were asleep for more than one pass, count missed pulses and sleep
|
||||
* until we're resynchronized with the next upcoming pulse.
|
||||
*/
|
||||
if (process_time.tv_sec == 0 && process_time.tv_usec < ht->opt_time.tv_usec)
|
||||
{
|
||||
missed_pulse = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
missed_pulse = process_time.tv_sec * ht->passes_per_sec;
|
||||
missed_pulse += process_time.tv_usec / ht->opt_time.tv_usec;
|
||||
}
|
||||
|
||||
// <20>ٺ<EFBFBD><D9BA><EFBFBD> pulse<73><65> <20><><EFBFBD>ƴµ<C6B4> <20><> <20>ð<EFBFBD><C3B0><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...
|
||||
// <20><EFBFBD> fps <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Ʋ<><C6B2><EFBFBD><EFBFBD><EFBFBD>ִµ<D6B4>, <20><>Ȯ<EFBFBD><C8AE> <20><><EFBFBD>ߴ<EFBFBD> <20><> <20>߿<EFBFBD><DFBF><EFBFBD><EFBFBD><EFBFBD> <20>ʾ<EFBFBD>.
|
||||
if (missed_pulse > 0)
|
||||
{
|
||||
gettimeofday(&ht->last_time, (struct timezone *) 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Calculate the time we should wake up */
|
||||
temp_time = *timediff(&ht->opt_time, &process_time);
|
||||
ht->last_time = *timeadd(&ht->before_sleep, &temp_time);
|
||||
|
||||
/* Now keep sleeping until that time has come */
|
||||
gettimeofday(&now, (struct timezone *) 0);
|
||||
timeout = *timediff(&ht->last_time, &now);
|
||||
|
||||
thecore_sleep(&timeout);
|
||||
}
|
||||
|
||||
++missed_pulse;
|
||||
|
||||
if (missed_pulse <= 0)
|
||||
{
|
||||
sys_err("missed_pulse is not positive! (%d)", missed_pulse);
|
||||
missed_pulse = 1;
|
||||
}
|
||||
|
||||
if (missed_pulse > (30 * ht->passes_per_sec))
|
||||
{
|
||||
sys_err("losing %d seconds. (lag occured)", missed_pulse / ht->passes_per_sec);
|
||||
missed_pulse = 30 * ht->passes_per_sec;
|
||||
}
|
||||
|
||||
return missed_pulse;
|
||||
}
|
469
src/libthecore/src/log.cpp
Normal file
469
src/libthecore/src/log.cpp
Normal file
@ -0,0 +1,469 @@
|
||||
/*
|
||||
* Filename: log.c
|
||||
* Description: local log file <20><><EFBFBD><EFBFBD>
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD> aka. Cronan
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
|
||||
#ifndef __WIN32__
|
||||
#define SYSLOG_FILENAME "syslog"
|
||||
#define SYSERR_FILENAME "syserr"
|
||||
#define PTS_FILENAME "PTS"
|
||||
#else
|
||||
#define SYSLOG_FILENAME "syslog.txt"
|
||||
#define SYSERR_FILENAME "syserr.txt"
|
||||
#define PTS_FILENAME "PTS.txt"
|
||||
#endif
|
||||
|
||||
typedef struct log_file_s * LPLOGFILE;
|
||||
typedef struct log_file_s LOGFILE;
|
||||
|
||||
struct log_file_s
|
||||
{
|
||||
char* filename;
|
||||
FILE* fp;
|
||||
|
||||
int last_hour;
|
||||
int last_day;
|
||||
};
|
||||
|
||||
LPLOGFILE log_file_sys = NULL;
|
||||
LPLOGFILE log_file_err = NULL;
|
||||
LPLOGFILE log_file_pt = NULL;
|
||||
static char log_dir[32] = { 0, };
|
||||
unsigned int log_keep_days = 3;
|
||||
|
||||
// Internal functions
|
||||
LPLOGFILE log_file_init(const char* filename, const char *openmode);
|
||||
void log_file_destroy(LPLOGFILE logfile);
|
||||
void log_file_rotate(LPLOGFILE logfile);
|
||||
void log_file_check(LPLOGFILE logfile);
|
||||
void log_file_set_dir(const char *dir);
|
||||
|
||||
static unsigned int log_level_bits = 7;
|
||||
|
||||
void log_set_level(unsigned int bit)
|
||||
{
|
||||
log_level_bits |= bit;
|
||||
}
|
||||
|
||||
void log_unset_level(unsigned int bit)
|
||||
{
|
||||
log_level_bits &= ~bit;
|
||||
}
|
||||
|
||||
void log_set_expiration_days(unsigned int days)
|
||||
{
|
||||
log_keep_days = days;
|
||||
}
|
||||
|
||||
int log_get_expiration_days(void)
|
||||
{
|
||||
return log_keep_days;
|
||||
}
|
||||
|
||||
int log_init(void)
|
||||
{
|
||||
log_file_set_dir("./log");
|
||||
|
||||
do
|
||||
{
|
||||
log_file_sys = log_file_init(SYSLOG_FILENAME, "a+");
|
||||
if( NULL == log_file_sys ) break;
|
||||
|
||||
log_file_err = log_file_init(SYSERR_FILENAME, "a+");
|
||||
if( NULL == log_file_err ) break;
|
||||
|
||||
log_file_pt = log_file_init(PTS_FILENAME, "w");
|
||||
if( NULL == log_file_pt ) break;
|
||||
|
||||
return true;
|
||||
}
|
||||
while( false );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void log_destroy(void)
|
||||
{
|
||||
log_file_destroy(log_file_sys);
|
||||
log_file_destroy(log_file_err);
|
||||
log_file_destroy(log_file_pt);
|
||||
|
||||
log_file_sys = NULL;
|
||||
log_file_err = NULL;
|
||||
log_file_pt = NULL;
|
||||
}
|
||||
|
||||
void log_rotate(void)
|
||||
{
|
||||
log_file_check(log_file_sys);
|
||||
log_file_check(log_file_err);
|
||||
log_file_check(log_file_pt);
|
||||
|
||||
log_file_rotate(log_file_sys);
|
||||
}
|
||||
|
||||
#ifndef __WIN32__
|
||||
void _sys_err(const char *func, int line, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
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<><6E> <20><><EFBFBD>̱<EFBFBD> <20><><EFBFBD><EFBFBD>..
|
||||
int len;
|
||||
|
||||
if (!log_file_err)
|
||||
return;
|
||||
|
||||
time_s[strlen(time_s) - 1] = '\0';
|
||||
len = snprintf(buf, 1024, "SYSERR: %-15.15s.%ld :: %s: ", time_s + 4, tv.tv_usec, func);
|
||||
buf[1025] = '\0';
|
||||
|
||||
if (len < 1024)
|
||||
{
|
||||
va_start(args, format);
|
||||
vsnprintf(buf + len, 1024 - len, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
strcat(buf, "\n");
|
||||
|
||||
// log_file_err <20><> <20><><EFBFBD><EFBFBD>
|
||||
fputs(buf, log_file_err->fp);
|
||||
fflush(log_file_err->fp);
|
||||
|
||||
// log_file_sys <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
fputs(buf, log_file_sys->fp);
|
||||
fflush(log_file_sys->fp);
|
||||
}
|
||||
#else
|
||||
void _sys_err(const char *func, int line, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
time_t ct = time(0);
|
||||
char *time_s = asctime(localtime(&ct));
|
||||
|
||||
char buf[1024 + 2]; // \n<><6E> <20><><EFBFBD>̱<EFBFBD> <20><><EFBFBD><EFBFBD>..
|
||||
int len;
|
||||
|
||||
if (!log_file_err)
|
||||
return;
|
||||
|
||||
time_s[strlen(time_s) - 1] = '\0';
|
||||
len = snprintf(buf, 1024, "SYSERR: %-15.15s :: %s: ", time_s + 4, func);
|
||||
buf[1025] = '\0';
|
||||
|
||||
if (len < 1024)
|
||||
{
|
||||
va_start(args, format);
|
||||
vsnprintf(buf + len, 1024 - len, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
strcat(buf, "\n");
|
||||
|
||||
// log_file_err <20><> <20><><EFBFBD><EFBFBD>
|
||||
fputs(buf, log_file_err->fp);
|
||||
fflush(log_file_err->fp);
|
||||
|
||||
// log_file_sys <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
fputs(buf, log_file_sys->fp);
|
||||
fflush(log_file_sys->fp);
|
||||
|
||||
fputs(buf, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
#endif
|
||||
|
||||
static char sys_log_header_string[33] = "";
|
||||
|
||||
void sys_log_header(const char *header)
|
||||
{
|
||||
strncpy(sys_log_header_string, header, 32);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (log_file_sys)
|
||||
{
|
||||
time_t ct = time(0);
|
||||
char *time_s = asctime(localtime(&ct));
|
||||
|
||||
fprintf(log_file_sys->fp, sys_log_header_string);
|
||||
|
||||
time_s[strlen(time_s) - 1] = '\0';
|
||||
fprintf(log_file_sys->fp, "%-15.15s.%ld :: ", time_s + 4, tv.tv_usec );
|
||||
|
||||
va_start(args, format);
|
||||
vfprintf(log_file_sys->fp, format, args);
|
||||
va_end(args);
|
||||
|
||||
fputc('\n', log_file_sys->fp);
|
||||
fflush(log_file_sys->fp);
|
||||
}
|
||||
|
||||
#ifndef __WIN32__
|
||||
// log_level<65><6C> 1 <20>̻<EFBFBD><CCBB><EFBFBD> <20><><EFBFBD>쿡<EFBFBD><ECBFA1> <20><EFBFBD>Ʈ<EFBFBD><C6AE> <20><><EFBFBD>찡 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> stdout<75><74><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>.
|
||||
if (log_level_bits > 1)
|
||||
{
|
||||
#endif
|
||||
fprintf(stdout, sys_log_header_string);
|
||||
|
||||
va_start(args, format);
|
||||
vfprintf(stdout, format, args);
|
||||
va_end(args);
|
||||
|
||||
fputc('\n', stdout);
|
||||
fflush(stdout);
|
||||
#ifndef __WIN32__
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void pt_log(const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
if (!log_file_pt)
|
||||
return;
|
||||
|
||||
va_start(args, format);
|
||||
vfprintf(log_file_pt->fp, format, args);
|
||||
va_end(args);
|
||||
|
||||
fputc('\n', log_file_pt->fp);
|
||||
fflush(log_file_pt->fp);
|
||||
}
|
||||
|
||||
LPLOGFILE log_file_init(const char * filename, const char * openmode)
|
||||
{
|
||||
LPLOGFILE logfile;
|
||||
FILE * fp;
|
||||
struct tm curr_tm;
|
||||
time_t time_s;
|
||||
|
||||
time_s = time(0);
|
||||
curr_tm = *localtime(&time_s);
|
||||
|
||||
fp = fopen(filename, openmode);
|
||||
|
||||
if (!fp)
|
||||
return NULL;
|
||||
|
||||
logfile = (LPLOGFILE) malloc(sizeof(LOGFILE));
|
||||
logfile->filename = strdup(filename);
|
||||
logfile->fp = fp;
|
||||
logfile->last_hour = curr_tm.tm_hour;
|
||||
logfile->last_day = curr_tm.tm_mday;
|
||||
|
||||
return (logfile);
|
||||
}
|
||||
|
||||
void log_file_destroy(LPLOGFILE logfile)
|
||||
{
|
||||
if (logfile == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (logfile->filename)
|
||||
{
|
||||
free(logfile->filename);
|
||||
logfile->filename = NULL;
|
||||
}
|
||||
|
||||
if (logfile->fp)
|
||||
{
|
||||
fclose(logfile->fp);
|
||||
logfile->fp = NULL;
|
||||
}
|
||||
|
||||
free(logfile);
|
||||
}
|
||||
|
||||
void log_file_check(LPLOGFILE logfile)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD> <20>ٽ<EFBFBD> <20><><EFBFBD><EFBFBD>.
|
||||
if (stat(logfile->filename, &sb) != 0 && errno == ENOENT)
|
||||
{
|
||||
fclose(logfile->fp);
|
||||
logfile->fp = fopen(logfile->filename, "a+");
|
||||
}
|
||||
}
|
||||
|
||||
void log_file_delete_old(const char *filename)
|
||||
{
|
||||
struct stat sb;
|
||||
int num1, num2;
|
||||
char buf[32];
|
||||
char system_cmd[512];
|
||||
struct tm new_tm;
|
||||
|
||||
if (stat(filename, &sb) == -1)
|
||||
{
|
||||
perror("log_file_delete_old: stat");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!S_ISDIR(sb.st_mode))
|
||||
return;
|
||||
|
||||
new_tm = *tm_calc(NULL, -log_keep_days);
|
||||
sprintf(buf, "%04d%02d%02d", new_tm.tm_year + 1900, new_tm.tm_mon + 1, new_tm.tm_mday);
|
||||
num1 = atoi(buf);
|
||||
#ifndef __WIN32__
|
||||
{
|
||||
struct dirent ** namelist;
|
||||
int n;
|
||||
|
||||
n = scandir(filename, &namelist, 0, alphasort);
|
||||
|
||||
if (n < 0)
|
||||
perror("scandir");
|
||||
else
|
||||
{
|
||||
char name[MAXNAMLEN+1];
|
||||
|
||||
while (n-- > 0)
|
||||
{
|
||||
strncpy(name, namelist[n]->d_name, 255);
|
||||
name[255] = '\0';
|
||||
|
||||
free(namelist[n]);
|
||||
|
||||
if (*name == '.')
|
||||
continue;
|
||||
|
||||
if (!isdigit(*name))
|
||||
continue;
|
||||
|
||||
num2 = atoi(name);
|
||||
|
||||
if (num2 <= num1)
|
||||
{
|
||||
snprintf(system_cmd, sizeof(system_cmd), "rm -rf %s/%s", filename, name);
|
||||
system(system_cmd);
|
||||
|
||||
sys_log(0, "%s: SYSTEM_CMD: %s", __FUNCTION__, system_cmd);
|
||||
fprintf(stderr, "%s: SYSTEM_CMD: %s %s:%d %s:%d\n", __FUNCTION__, system_cmd, buf, num1, name, num2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(namelist);
|
||||
}
|
||||
#else
|
||||
{
|
||||
WIN32_FIND_DATA fdata;
|
||||
HANDLE hFind;
|
||||
if ((hFind = FindFirstFile(filename, &fdata)) != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
do
|
||||
{
|
||||
if (!isdigit(*fdata.cFileName))
|
||||
continue;
|
||||
|
||||
num2 = atoi(fdata.cFileName);
|
||||
|
||||
if (num2 <= num1)
|
||||
{
|
||||
sprintf(system_cmd, "del %s\\%s", filename, fdata.cFileName);
|
||||
system(system_cmd);
|
||||
|
||||
sys_log(0, "SYSTEM_CMD: %s", system_cmd);
|
||||
}
|
||||
}
|
||||
while (FindNextFile(hFind, &fdata));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void log_file_rotate(LPLOGFILE logfile)
|
||||
{
|
||||
struct tm curr_tm;
|
||||
time_t time_s;
|
||||
char dir[256];
|
||||
char system_cmd[512];
|
||||
|
||||
time_s = time(0);
|
||||
curr_tm = *localtime(&time_s);
|
||||
|
||||
if (curr_tm.tm_mday != logfile->last_day)
|
||||
{
|
||||
struct tm new_tm;
|
||||
new_tm = *tm_calc(&curr_tm, -3);
|
||||
|
||||
#ifndef __WIN32__
|
||||
sprintf(system_cmd, "rm -rf %s/%04d%02d%02d", log_dir, new_tm.tm_year + 1900, new_tm.tm_mon + 1, new_tm.tm_mday);
|
||||
#else
|
||||
sprintf(system_cmd, "del %s\\%04d%02d%02d", log_dir, new_tm.tm_year + 1900, new_tm.tm_mon + 1, new_tm.tm_mday);
|
||||
#endif
|
||||
system(system_cmd);
|
||||
|
||||
sys_log(0, "SYSTEM_CMD: %s", system_cmd);
|
||||
|
||||
logfile->last_day = curr_tm.tm_mday;
|
||||
}
|
||||
|
||||
if (curr_tm.tm_hour != logfile->last_hour)
|
||||
{
|
||||
struct stat stat_buf;
|
||||
snprintf(dir, sizeof(dir), "%s/%04d%02d%02d", log_dir, curr_tm.tm_year + 1900, curr_tm.tm_mon + 1, curr_tm.tm_mday);
|
||||
|
||||
if (stat(dir, &stat_buf) != 0 || S_ISDIR(stat_buf.st_mode))
|
||||
{
|
||||
#ifdef __WIN32__
|
||||
CreateDirectory(dir, NULL);
|
||||
#else
|
||||
mkdir(dir, S_IRWXU);
|
||||
#endif
|
||||
}
|
||||
|
||||
sys_log(0, "SYSTEM: LOG ROTATE (%04d-%02d-%02d %d)",
|
||||
curr_tm.tm_year + 1900, curr_tm.tm_mon + 1, curr_tm.tm_mday, logfile->last_hour);
|
||||
|
||||
// <20>α<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ݰ<EFBFBD>
|
||||
fclose(logfile->fp);
|
||||
|
||||
// <20>ű<EFBFBD><C5B1><EFBFBD>.
|
||||
#ifndef __WIN32__
|
||||
snprintf(system_cmd, sizeof(system_cmd), "mv %s %s/%s.%02d", logfile->filename, dir, logfile->filename, logfile->last_hour);
|
||||
#else
|
||||
snprintf(system_cmd, sizeof(system_cmd), "move %s %s\\%s.%02d", logfile->filename, dir, logfile->filename, logfile->last_hour);
|
||||
#endif
|
||||
system(system_cmd);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ð<EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
logfile->last_hour = curr_tm.tm_hour;
|
||||
|
||||
// <20>α<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ٽ<EFBFBD> <20><><EFBFBD><EFBFBD>.
|
||||
logfile->fp = fopen(logfile->filename, "a+");
|
||||
}
|
||||
}
|
||||
|
||||
void log_file_set_dir(const char *dir)
|
||||
{
|
||||
strcpy(log_dir, dir);
|
||||
log_file_delete_old(log_dir);
|
||||
}
|
120
src/libthecore/src/main.cpp
Normal file
120
src/libthecore/src/main.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Filename: main.c
|
||||
* Description: <20><><EFBFBD>̺귯<CCBA><EAB7AF> <20>ʱ<EFBFBD>ȭ/<2F><><EFBFBD><EFBFBD> <20><>
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD> aka. Cronan
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
#include "memory.h"
|
||||
|
||||
LPHEART thecore_heart = NULL;
|
||||
|
||||
volatile int shutdowned = false;
|
||||
volatile int tics = 0;
|
||||
unsigned int thecore_profiler[NUM_PF];
|
||||
|
||||
static int pid_init(void)
|
||||
{
|
||||
#ifdef __WIN32__
|
||||
return true;
|
||||
#else
|
||||
FILE* fp;
|
||||
if ((fp = fopen("pid", "w")))
|
||||
{
|
||||
fprintf(fp, "%d", getpid());
|
||||
fclose(fp);
|
||||
sys_err("\nStart of pid: %d\n", getpid());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("pid_init(): could not open file for writing. (filename: ./pid)");
|
||||
sys_err("\nError writing pid file\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void pid_deinit(void)
|
||||
{
|
||||
#ifdef __WIN32__
|
||||
return;
|
||||
#else
|
||||
remove("./pid");
|
||||
sys_err("\nEnd of pid\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
int thecore_init(int fps, HEARTFUNC heartbeat_func)
|
||||
{
|
||||
#if defined(__WIN32__) || defined(__linux__)
|
||||
srand(time(0));
|
||||
#else
|
||||
srandom(time(0) + getpid() + getuid());
|
||||
srandomdev();
|
||||
#endif
|
||||
signal_setup();
|
||||
|
||||
if (!log_init() || !pid_init())
|
||||
return false;
|
||||
|
||||
thecore_heart = heart_new(1000000 / fps, heartbeat_func);
|
||||
return true;
|
||||
}
|
||||
|
||||
void thecore_shutdown()
|
||||
{
|
||||
shutdowned = true;
|
||||
}
|
||||
|
||||
int thecore_idle(void)
|
||||
{
|
||||
thecore_tick();
|
||||
|
||||
if (shutdowned)
|
||||
return 0;
|
||||
|
||||
int pulses;
|
||||
DWORD t = get_dword_time();
|
||||
|
||||
if (!(pulses = heart_idle(thecore_heart)))
|
||||
{
|
||||
thecore_profiler[PF_IDLE] += (get_dword_time() - t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
thecore_profiler[PF_IDLE] += (get_dword_time() - t);
|
||||
return pulses;
|
||||
}
|
||||
|
||||
void thecore_destroy(void)
|
||||
{
|
||||
pid_deinit();
|
||||
log_destroy();
|
||||
}
|
||||
|
||||
int thecore_pulse(void)
|
||||
{
|
||||
return (thecore_heart->pulse);
|
||||
}
|
||||
|
||||
float thecore_pulse_per_second(void)
|
||||
{
|
||||
return ((float) thecore_heart->passes_per_sec);
|
||||
}
|
||||
|
||||
float thecore_time(void)
|
||||
{
|
||||
return ((float) thecore_heart->pulse / (float) thecore_heart->passes_per_sec);
|
||||
}
|
||||
|
||||
int thecore_is_shutdowned(void)
|
||||
{
|
||||
return shutdowned;
|
||||
}
|
||||
|
||||
void thecore_tick(void)
|
||||
{
|
||||
++tics;
|
||||
}
|
91
src/libthecore/src/signals.cpp
Normal file
91
src/libthecore/src/signals.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Filename: signal.c
|
||||
* Description: <20>ñ׳<C3B1> <20><><EFBFBD><EFBFBD> <20>Լ<EFBFBD>.
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD> aka. Cronan
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
|
||||
#if defined(__WIN32__) || defined(__linux__)
|
||||
void signal_setup() {}
|
||||
void signal_timer_disable() {}
|
||||
void signal_timer_enable(int timeout_seconds) {}
|
||||
#elif __FreeBSD__
|
||||
#define RETSIGTYPE void
|
||||
|
||||
RETSIGTYPE reap(int sig)
|
||||
{
|
||||
while (waitpid(-1, NULL, WNOHANG) > 0);
|
||||
signal(SIGCHLD, reap);
|
||||
}
|
||||
|
||||
|
||||
RETSIGTYPE checkpointing(int sig)
|
||||
{
|
||||
if (!tics)
|
||||
{
|
||||
sys_err("CHECKPOINT shutdown: tics did not updated.");
|
||||
abort();
|
||||
}
|
||||
else
|
||||
tics = 0;
|
||||
}
|
||||
|
||||
|
||||
RETSIGTYPE hupsig(int sig)
|
||||
{
|
||||
shutdowned = true;
|
||||
sys_err("SIGHUP, SIGINT, SIGTERM signal has been received. shutting down.");
|
||||
}
|
||||
|
||||
RETSIGTYPE usrsig(int sig)
|
||||
{
|
||||
core_dump();
|
||||
}
|
||||
|
||||
void signal_timer_disable(void)
|
||||
{
|
||||
struct itimerval itime;
|
||||
struct timeval interval;
|
||||
|
||||
interval.tv_sec = 0;
|
||||
interval.tv_usec = 0;
|
||||
|
||||
itime.it_interval = interval;
|
||||
itime.it_value = interval;
|
||||
|
||||
setitimer(ITIMER_VIRTUAL, &itime, NULL);
|
||||
}
|
||||
|
||||
void signal_timer_enable(int sec)
|
||||
{
|
||||
struct itimerval itime;
|
||||
struct timeval interval;
|
||||
|
||||
interval.tv_sec = sec;
|
||||
interval.tv_usec = 0;
|
||||
|
||||
itime.it_interval = interval;
|
||||
itime.it_value = interval;
|
||||
|
||||
setitimer(ITIMER_VIRTUAL, &itime, NULL);
|
||||
}
|
||||
|
||||
void signal_setup(void)
|
||||
{
|
||||
signal_timer_enable(30);
|
||||
|
||||
signal(SIGVTALRM, checkpointing);
|
||||
|
||||
/* just to be on the safe side: */
|
||||
signal(SIGHUP, hupsig);
|
||||
signal(SIGCHLD, reap);
|
||||
signal(SIGINT, hupsig);
|
||||
signal(SIGTERM, hupsig);
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
signal(SIGALRM, SIG_IGN);
|
||||
signal(SIGUSR1, usrsig);
|
||||
}
|
||||
|
||||
#endif
|
346
src/libthecore/src/utils.cpp
Normal file
346
src/libthecore/src/utils.cpp
Normal file
@ -0,0 +1,346 @@
|
||||
/*
|
||||
* Filename: utils.c
|
||||
* Description: <20><><EFBFBD><EFBFBD> <20><>ƿ<EFBFBD><C6BF>Ƽ
|
||||
*
|
||||
* Author: <20><><EFBFBD><EFBFBD> aka. Cronan
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
#include <common/length.h>
|
||||
|
||||
static struct timeval null_time = { 0, 0 };
|
||||
|
||||
struct timeval * timediff(const struct timeval *a, const struct timeval *b)
|
||||
{
|
||||
static struct timeval rslt;
|
||||
|
||||
if (a->tv_sec < b->tv_sec)
|
||||
return &null_time;
|
||||
else if (a->tv_sec == b->tv_sec)
|
||||
{
|
||||
if (a->tv_usec < b->tv_usec)
|
||||
return &null_time;
|
||||
else
|
||||
{
|
||||
rslt.tv_sec = 0;
|
||||
rslt.tv_usec = a->tv_usec - b->tv_usec;
|
||||
return &rslt;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ /* a->tv_sec > b->tv_sec */
|
||||
rslt.tv_sec = a->tv_sec - b->tv_sec;
|
||||
|
||||
if (a->tv_usec < b->tv_usec)
|
||||
{
|
||||
rslt.tv_usec = a->tv_usec + 1000000 - b->tv_usec;
|
||||
rslt.tv_sec--;
|
||||
} else
|
||||
rslt.tv_usec = a->tv_usec - b->tv_usec;
|
||||
|
||||
return &rslt;
|
||||
}
|
||||
}
|
||||
|
||||
struct timeval * timeadd(struct timeval *a, struct timeval *b)
|
||||
{
|
||||
static struct timeval rslt;
|
||||
|
||||
rslt.tv_sec = a->tv_sec + b->tv_sec;
|
||||
rslt.tv_usec = a->tv_usec + b->tv_usec;
|
||||
|
||||
while (rslt.tv_usec >= 1000000)
|
||||
{
|
||||
rslt.tv_usec -= 1000000;
|
||||
rslt.tv_sec++;
|
||||
}
|
||||
|
||||
return &rslt;
|
||||
}
|
||||
|
||||
|
||||
char *time_str(time_t ct)
|
||||
{
|
||||
static char * time_s;
|
||||
|
||||
time_s = asctime(localtime(&ct));
|
||||
|
||||
time_s[strlen(time_s) - 6] = '\0';
|
||||
time_s += 4;
|
||||
|
||||
return (time_s);
|
||||
}
|
||||
|
||||
void trim_and_lower(const char * src, char * dest, size_t dest_size)
|
||||
{
|
||||
const char * tmp = src;
|
||||
size_t len = 0;
|
||||
|
||||
if (!dest || dest_size == 0)
|
||||
return;
|
||||
|
||||
if (!src)
|
||||
{
|
||||
*dest = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
// <20>տ<EFBFBD> <20><>ĭ <20>dz<EFBFBD> <20>ٱ<EFBFBD>
|
||||
while (*tmp)
|
||||
{
|
||||
if (!isspace(*tmp))
|
||||
break;
|
||||
|
||||
tmp++;
|
||||
}
|
||||
|
||||
// \0 Ȯ<><C8AE>
|
||||
--dest_size;
|
||||
|
||||
while (*tmp && len < dest_size)
|
||||
{
|
||||
*(dest++) = LOWER(*tmp); // LOWER<45><52> <20><>ũ<EFBFBD>ζ<EFBFBD> ++ <20><><EFBFBD><EFBFBD> <20>ȵ<EFBFBD>
|
||||
++tmp;
|
||||
++len;
|
||||
}
|
||||
|
||||
*dest = '\0';
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
// <20>ڿ<EFBFBD> <20><>ĭ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
--dest;
|
||||
|
||||
while (*dest && isspace(*dest) && len--)
|
||||
*(dest--) = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/* "Name : <20><><EFBFBD><EFBFBD>" <20><> <20><><EFBFBD><EFBFBD> "<22><EFBFBD> : <20><>" <20><><EFBFBD><EFBFBD> <20>̷<EFBFBD><CCB7><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD><EFBFBD><EFBFBD>
|
||||
<20><EFBFBD><D7B8><EFBFBD> token <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD> value <20><> <20><><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD>. */
|
||||
void parse_token(char *src, char *token, char *value)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
for (tmp = src; *tmp && *tmp != ':'; tmp++)
|
||||
{
|
||||
if (isspace(*tmp))
|
||||
continue;
|
||||
|
||||
*(token++) = LOWER(*tmp);
|
||||
}
|
||||
|
||||
*token = '\0';
|
||||
|
||||
for (tmp += 2; *tmp; tmp++)
|
||||
{
|
||||
if (*tmp == '\n' || *tmp == '\r')
|
||||
continue;
|
||||
|
||||
*(value++) = *tmp;
|
||||
}
|
||||
|
||||
*value = '\0';
|
||||
}
|
||||
|
||||
|
||||
struct tm * tm_calc(const struct tm * curr_tm, int days)
|
||||
{
|
||||
bool yoon = false;
|
||||
static struct tm new_tm;
|
||||
int monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
if (!curr_tm)
|
||||
{
|
||||
time_t time_s = time(0);
|
||||
new_tm = *localtime(&time_s);
|
||||
}
|
||||
else
|
||||
memcpy(&new_tm, curr_tm, sizeof(struct tm));
|
||||
|
||||
if (new_tm.tm_mon == 1)
|
||||
{
|
||||
if (!((new_tm.tm_year + 1900) % 4))
|
||||
{
|
||||
if (!((new_tm.tm_year + 1900) % 100))
|
||||
{
|
||||
if (!((new_tm.tm_year + 1900) % 400))
|
||||
yoon = true;
|
||||
}
|
||||
else
|
||||
yoon = true;
|
||||
}
|
||||
|
||||
if (yoon)
|
||||
new_tm.tm_mday += 1;
|
||||
}
|
||||
|
||||
if (yoon)
|
||||
monthdays[1] = 29;
|
||||
else
|
||||
monthdays[1] = 28;
|
||||
|
||||
new_tm.tm_mday += days;
|
||||
|
||||
if (new_tm.tm_mday <= 0)
|
||||
{
|
||||
new_tm.tm_mon--;
|
||||
|
||||
if (new_tm.tm_mon < 0)
|
||||
{
|
||||
new_tm.tm_year--;
|
||||
new_tm.tm_mon = 11;
|
||||
}
|
||||
|
||||
new_tm.tm_mday = monthdays[new_tm.tm_mon] + new_tm.tm_mday;
|
||||
}
|
||||
else if (new_tm.tm_mday > monthdays[new_tm.tm_mon])
|
||||
{
|
||||
new_tm.tm_mon++;
|
||||
|
||||
if (new_tm.tm_mon > 11)
|
||||
{
|
||||
new_tm.tm_year++;
|
||||
new_tm.tm_mon = 0;
|
||||
}
|
||||
|
||||
new_tm.tm_mday = monthdays[new_tm.tm_mon] - new_tm.tm_mday;
|
||||
}
|
||||
|
||||
return (&new_tm);
|
||||
}
|
||||
|
||||
#ifndef __WIN32__
|
||||
void thecore_sleep(struct timeval* timeout)
|
||||
{
|
||||
if (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, timeout) < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
sys_err("select sleep %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void thecore_msleep(DWORD dwMillisecond)
|
||||
{
|
||||
static struct timeval tv_sleep;
|
||||
tv_sleep.tv_sec = dwMillisecond / 1000;
|
||||
tv_sleep.tv_usec = dwMillisecond * 1000;
|
||||
thecore_sleep(&tv_sleep);
|
||||
}
|
||||
|
||||
void core_dump_unix(const char *who, WORD line)
|
||||
{
|
||||
sys_err("*** Dumping Core %s:%d ***", who, line);
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
if (fork() == 0)
|
||||
abort();
|
||||
}
|
||||
|
||||
/*
|
||||
uint64_t rdtsc()
|
||||
{
|
||||
uint64_t x;
|
||||
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
|
||||
return x;
|
||||
}
|
||||
*/
|
||||
|
||||
#else
|
||||
|
||||
void thecore_sleep(struct timeval* timeout)
|
||||
{
|
||||
Sleep(timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
|
||||
}
|
||||
|
||||
void thecore_msleep(DWORD dwMillisecond)
|
||||
{
|
||||
Sleep(dwMillisecond);
|
||||
}
|
||||
|
||||
void gettimeofday(struct timeval* t, struct timezone* dummy)
|
||||
{
|
||||
DWORD millisec = GetTickCount();
|
||||
|
||||
t->tv_sec = (millisec / 1000);
|
||||
t->tv_usec = (millisec % 1000) * 1000;
|
||||
}
|
||||
|
||||
void core_dump_unix(const char *who, WORD line)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
float get_float_time()
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
tv.tv_sec -= 1057699978;
|
||||
return ((float) tv.tv_sec + ((float) tv.tv_usec / 1000000.0f));
|
||||
}
|
||||
|
||||
static DWORD get_boot_sec()
|
||||
{
|
||||
static struct timeval tv_boot = {0L, 0L};
|
||||
|
||||
if (tv_boot.tv_sec == 0)
|
||||
gettimeofday(&tv_boot, NULL);
|
||||
|
||||
return tv_boot.tv_sec;
|
||||
}
|
||||
|
||||
DWORD get_dword_time()
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
//tv.tv_sec -= 1057699978;
|
||||
tv.tv_sec -= get_boot_sec();
|
||||
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
|
||||
}
|
||||
|
||||
std::string GetSocketHost(const sockaddr * address) {
|
||||
sockaddr_in* peer;
|
||||
sockaddr_in6* peer6;
|
||||
char host[MAX_HOST_LENGTH + 1];
|
||||
|
||||
switch (address->sa_family) {
|
||||
case AF_INET:
|
||||
peer = (sockaddr_in*) address;
|
||||
return inet_ntop(AF_INET, &(peer->sin_addr), host, INET_ADDRSTRLEN);
|
||||
|
||||
case AF_INET6:
|
||||
peer6 = (sockaddr_in6*) address;
|
||||
return inet_ntop(AF_INET, &(peer6->sin6_addr), host, INET6_ADDRSTRLEN);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
in_port_t GetSocketPort(const sockaddr * address) {
|
||||
sockaddr_in* peer;
|
||||
sockaddr_in6* peer6;
|
||||
|
||||
switch (address->sa_family) {
|
||||
case AF_INET:
|
||||
peer = (sockaddr_in*) address;
|
||||
return peer->sin_port;
|
||||
|
||||
case AF_INET6:
|
||||
peer6 = (sockaddr_in6*) address;
|
||||
return peer6->sin6_port;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user