00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #define _WINSOCKAPI_
00034 #include <windows.h>
00035 #include <winsock2.h>
00036 #include <ws2tcpip.h>
00037 #include <wspiapi.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <stdarg.h>
00041 #include <string.h>
00042 #include <signal.h>
00043 #include <time.h>
00044 #include <sys/types.h>
00045 #include <sys/timeb.h>
00046
00047 #if defined(DLL_IMPORT)
00048 #define LIBTYPE __declspec( dllexport )
00049 #elif defined(DLL_EXPORT)
00050 #define LIBTYPE __declspec( dllimport )
00051 #else
00052 #define LIBTYPE
00053 #endif
00054
00055 typedef int bool_t;
00056 typedef signed __int8 int8_t;
00057 typedef signed __int16 int16_t;
00058 typedef signed __int32 int32_t;
00059 typedef signed __int64 int64_t;
00060 typedef unsigned __int8 uint8_t;
00061 typedef unsigned __int16 uint16_t;
00062 typedef unsigned __int32 uint32_t;
00063 typedef unsigned __int64 uint64_t;
00064 typedef HANDLE pthread_t;
00065 typedef HANDLE pthread_mutex_t;
00066
00067 #define socklen_t int
00068 #define close closesocket
00069 #define sock_getlasterror WSAGetLastError()
00070 #define sock_getlasterror_socktimeout (WSAGetLastError() == WSAETIMEDOUT)
00071 #define va_copy(x, y) x = y
00072 #define atoll _atoi64
00073 #define strdup _strdup
00074 #define strcasecmp _stricmp
00075 #define snprintf _snprintf
00076 #define fseeko _fseeki64
00077 #define ftello _ftelli64
00078 #define THREAD_FUNC_PREFIX DWORD WINAPI
00079 #define SIGPIPE SIGABRT
00080
00081 static inline int msleep(unsigned int ms)
00082 {
00083 Sleep(ms);
00084 return 0;
00085 }
00086
00087 static inline int sleep(unsigned int sec)
00088 {
00089 Sleep(sec * 1000);
00090 return 0;
00091 }
00092
00093 static inline uint64_t getcurrenttime(void)
00094 {
00095 struct timeb tb;
00096 ftime(&tb);
00097 return ((uint64_t)tb.time * 1000) + tb.millitm;
00098 }
00099
00100 static inline int setsocktimeout(int s, int level, int optname, uint64_t timeout)
00101 {
00102 int t = (int)timeout;
00103 return setsockopt(s, level, optname, (char *)&t, sizeof(t));
00104 }
00105
00106 static inline int pthread_create(pthread_t *tid, void *attr, LPTHREAD_START_ROUTINE start, void *arg)
00107 {
00108 *tid = CreateThread(NULL, 0, start, arg, 0, NULL);
00109 if (!*tid) {
00110 return (int)GetLastError();
00111 }
00112 return 0;
00113 }
00114
00115 static inline int pthread_join(pthread_t tid, void **value_ptr)
00116 {
00117 while (1) {
00118 DWORD ExitCode = 0;
00119 if (!GetExitCodeThread(tid, &ExitCode)) {
00120 return (int)GetLastError();
00121 }
00122 if (ExitCode != STILL_ACTIVE) {
00123 return 0;
00124 }
00125 }
00126 }
00127
00128 static inline void pthread_mutex_init(pthread_mutex_t *mutex, void *attr)
00129 {
00130 *mutex = CreateMutex(NULL, FALSE, NULL);
00131 }
00132
00133 static inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00134 {
00135 WaitForSingleObject(*mutex, INFINITE);
00136 }
00137
00138 static inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00139 {
00140 ReleaseMutex(*mutex);
00141 }
00142
00143
00144
00145
00146
00147
00148 static inline void console_vprintf(const char *fmt, va_list ap)
00149 {
00150 UINT cp = GetConsoleOutputCP();
00151 SetConsoleOutputCP(CP_UTF8);
00152 vprintf(fmt, ap);
00153 SetConsoleOutputCP(cp);
00154 }
00155
00156 static inline void console_printf(const char *fmt, ...)
00157 {
00158 va_list ap;
00159 va_start(ap, fmt);
00160 console_vprintf(fmt, ap);
00161 va_end(ap);
00162 }