knightonline/server/shared/stdafx.h

204 lines
4.6 KiB
C++

#pragma once
#include <queue>
#ifdef _WIN32
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define THREADCALL WINAPI
#define STRCASECMP _stricmp
#define I64FMT "%016I64X"
#define I64FMTD "%I64u"
#define SI64FMTD "%I64d"
#else
// Linux includes
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <dirent.h>
#define THREADCALL
#define STRCASECMP strcasecmp
#define Sleep(ms) usleep((ms) * 1000)
#define CreateDirectory(path, attr) mkdir(path, 0755)
#define _snprintf snprintf
#define I64FMT "%016lX"
#define I64FMTD "%lu"
#define SI64FMTD "%ld"
// Windows types for Linux (define before any system headers)
typedef int BOOL;
#ifndef DWORD
typedef unsigned int DWORD; // Use int to match ODBC expectation
#endif
typedef int SOCKET;
typedef int HANDLE;
typedef void* LPVOID;
typedef unsigned long* LPDWORD;
#ifndef TCHAR
typedef char TCHAR;
#endif
#define TRUE 1
#define FALSE 0
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define INVALID_HANDLE_VALUE -1
// Windows socket functions mapping to Linux
#define WSASocket(af, type, protocol, lpProtocolInfo, g, dwFlags) socket(af, type, protocol)
#define WSAAccept(s, addr, addrlen, lpfnCondition, dwCallbackData) accept(s, addr, addrlen)
#define WSAGetLastError() errno
#define WSAStartup(ver, data) 0
#define WSACleanup() 0
#define closesocket(s) close(s)
#define ioctlsocket(s, cmd, argp) ioctl(s, cmd, argp)
#define SD_BOTH SHUT_RDWR
#define FIONBIO 1
#define MAKEWORD(low, high) ((unsigned short)(((unsigned char)(low)) | ((unsigned short)((unsigned char)(high))) << 8))
// Additional socket constants
#define WSAEWOULDBLOCK EWOULDBLOCK
#define WSAECONNRESET ECONNRESET
#define WSAENOTCONN ENOTCONN
// Windows system functions
#define GetSystemInfo(si) memset(si, 0, sizeof(*si))
#define GetQueuedCompletionStatus(cp, len, key, ol, timeout) false
#define CreateIoCompletionPort(fd, cp, key, threads) -1
#define PostQueuedCompletionStatus(cp, bytes, key, ol) false
// System info structure
typedef struct {
unsigned long dwNumberOfProcessors;
} SYSTEM_INFO;
// WSA Data structure
typedef struct {
unsigned short wVersion;
unsigned short wHighVersion;
char szDescription[257];
char szSystemStatus[129];
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char* lpVendorInfo;
} WSADATA;
// Additional types
typedef unsigned long ULONG_PTR;
// OVERLAPPED structure equivalent for Linux (using epoll)
typedef struct {
unsigned long Internal;
unsigned long InternalHigh;
union {
struct {
unsigned long Offset;
unsigned long OffsetHigh;
};
void* Pointer;
};
HANDLE hEvent;
} OVERLAPPED, *LPOVERLAPPED;
#define CONTAINING_RECORD(address, type, field) \
((type *)((char*)(address) - (char*)&(((type *)0)->field)))
#endif
#if defined(_DEBUG) || defined(DEBUG)
# include <cassert>
# include "DebugUtils.h"
# define ASSERT assert
# define TRACE FormattedDebugString
// Enables tracing to stdout.
// Preferable with the VS debugger (is thrown in the "output" window), but
// it can be spammy otherwise (especially if you don't need it enabled).
# define USE_SQL_TRACE
// Ensure both typically used debug defines behave as intended
# ifndef DEBUG
# define DEBUG
# endif
# ifndef _DEBUG
# define _DEBUG
# endif
#else
# define ASSERT
# define TRACE
#endif
#ifdef _WIN32
// Ignore the warning "nonstandard extension used: enum '<enum name>' used in qualified name"
// Sometimes it's necessary to avoid collisions, but aside from that, specifying the enumeration helps make code intent clearer.
#pragma warning(disable: 4482)
#endif
#define STR(str) #str
#define STRINGIFY(str) STR(str)
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
class Guard
{
public:
Guard(std::recursive_mutex& mutex) : target(mutex) { target.lock(); }
Guard(std::recursive_mutex* mutex) : target(*mutex) { target.lock(); }
~Guard() { target.unlock(); }
protected:
std::recursive_mutex& target;
};
#ifdef _WIN32
#define sleep(ms) Sleep(ms)
#else
#define sleep(ms) usleep((ms) * 1000)
#endif
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
// define compiler-specific types
#include "types.h"
#include <random>
#include <memory>
#include <map>
#include <list>
#include <vector>
#include "tstring.h"
#include "globals.h"
#include "Atomic.h"
#include "Thread.h"
#include "Network.h"
#include "TimeThread.h"