linux windows 宏定义

///设置多线程和同步
#ifdef WIN32
#define WaitThread(x)	{WaitForSingleObject(x, INFINITE);CloseHandle(x);}
#define Notify()		{SetEvent(m_hEventNotify);}
#define DoSend()		{SetEvent(m_hEventSend);}
#else
#define WaitThread(x)	pthread_join(x, NULL)
#define Notify()		{pthread_mutex_lock(m_pMutexNotify);pthread_cond_signal(m_pCondNotify);pthread_mutex_unlock(m_pMutexNotify);}
#define DoSend()		{/*pthread_mutex_lock(&m_lockSend); */pthread_cond_signal(&m_condSend);/*pthread_mutex_unlock(&m_lockSend);*/}
#endif

///设置阻塞
#ifdef WIN32
    unsigned long flag = 1;
    ioctlsocket(m_hSocket, FIONBIO, &flag);
#else
    int flag = 1;
    flag = fcntl(m_hSocket, F_GETFL, 0);
    flag |= O_NONBLOCK;
    fcntl(m_hSocket, F_SETFL, flag);
#endif

// socket 底层的bug。直接屏蔽。
#ifdef WIN32
#pragma warning(disable:4389 4548 4296)
#endif

//多线程回调函数格式不一致
#ifdef WIN32
DWORD WINAPI CQueryManager::LogQueryThreadProc(void* pParam)
#else
#include <sys/time.h>
void* CQueryManager::LogQueryThreadProc(void* pParam)

//时间差异	
#ifdef WIN32
#define millisecondSleep(milliseconds)	Sleep((milliseconds))
#else
#include <unistd.h>
#define millisecondSleep(milliseconds)	usleep(1000*(milliseconds))
#endif

// int64
#ifndef WIN32
typedef long long __int64;
#endif

//时间函数
#ifdef WIN32
    localtime_s(result, timep);
#else
    localtime_r(timep, result);
#endif

    // 分别获得本地时间和GMT时间
#ifdef WIN32
    localtime_s(&local, &now);
    gmtime_s(&gmt, &now);
#else
    localtime_r(&now, &local);
    gmtime_r(&now, &gmt);
#endif

//创建目录
bool CreateDir(const char* szDir)
{
#ifdef WIN32
    return 0 == mkdir(szDir);
#else
    return 0 == mkdir(szDir, 0777);
#endif
}


#ifdef WIN32
#define millisecondSleep(milliseconds)	Sleep((milliseconds))
#else
#include <unistd.h>
#define millisecondSleep(milliseconds)	usleep(1000*(milliseconds))
#endif

//
#ifdef WIN32
#else
    signal(SIGTERM, sig_kill);
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &sa, 0);
#endif

#ifdef WIN32
#else
	if (bDaemon)
	{
		printf("Enter daemon mode\n");
		init_daemon();
	}

	signal(SIGPIPE, SIG_IGN);
#endif

//
#ifdef WIN32
    CloseHandle(m_hEventNotify);
#else
    pthread_mutex_destroy(&m_mutexNotify);
    pthread_cond_destroy(&m_condNotify);
#endif

//拼接字符串
#ifndef WIN32
#define _vsnprintf  vsnprintf
#endif

//错误码错误码 和 socket阻塞
#ifdef WIN32
        int nErr = WSAGetLastError();
        if (nErr != WSAEWOULDBLOCK)
#else
        int nErr = errno;
        if (nErr != EAGAIN)
#endif

//关于SOCKET的掩藏差别的类型定义
#ifdef WIN32
#define socklen_t	int
#define iovec		WSABUF
#define iov_base	buf
#define iov_len		len
#else
typedef int			SOCKET;
#define closesocket(x)	::close(x)
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif
#define SD_RECEIVE		SHUT_RD
#define SD_SEND			SHUT_WR
#define SD_BOTH			SHUT_RDWR
#endif

//关于线程的掩藏差别的一些类型定义

#ifdef WIN32
#define thread_return_type	DWORD __stdcall
#define millisecondSleep(milliseconds)	Sleep(milliseconds)
#define threadHandle		HANDLE
#else
#include <pthread.h>
#define thread_return_type	void*
#include <unistd.h>
#define millisecondSleep(milliseconds)	usleep(1000*(milliseconds))
#define threadHandle		pthread_t
typedef void* ( *PTHREAD_START_ROUTINE)(  void* lpThreadParameter   );
typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;
#endif

/
#ifdef WIN32
	int result = fstat(file->_file, &fileStat);
#else
	int result = fstat(fileno(file), &fileStat);
#endif


#ifndef __WINDOWS_DEFINE_H__
#define __WINDOWS_DEFINE_H__

/**********************************************************************************
/ windows和linux编程上的一些差异

1.linux下g++要求每个文件结尾需要有个空行,否则会有警告

2.头文件的防止多次包含采用#ifndef...#define...#endif的方式可以通用,
vc.net中使用#pragma once, g++认为过时

3.新的ISO标准c++中for(int i = 0; i < 10; i++){...}变量i的作用域是限于for循环内的,
不可以在for循环体外引用i,g++使用了新的标准,vc则可以在循环体外访问循环变量

4.vc编译器默认的数据对齐方式和g++不同,vc是8字节对齐的。为了保证数据格式在两个平台上
都保持相同,需要指定编译时候的数据对齐方式,用#pragma pack(x)编译指令来确定对齐方式

**********************************************************************************/

#ifdef WIN32
#pragma warning( disable: 4049 )  /* more than 64k source lines */
#pragma warning( disable: 4100 ) /* unreferenced arguments in x86 call */
#pragma warning( disable: 4211 )  /* redefine extent to static */
#pragma warning( disable: 4232 )  /* dllimport identity*/
#pragma warning( disable: 4786 ) //identifier was truncated to '255' characters in the debug information
#pragma warning( disable: 4668 )
#ifndef NTDDI_WIN7SP1
#define NTDDI_WIN7SP1 0x06010100
#endif
#include <winsock2.h>
#include <windows.h>
#else//WIN32

typedef long long         __int64;

typedef long				LONG;
typedef short				SHORT;
typedef unsigned long		DWORD;
typedef int					BOOL;
typedef int					HMODULE;
typedef unsigned char		BYTE;
typedef unsigned char		byte;
typedef unsigned short		WORD;
#define DWORD_PTR			DWORD
typedef unsigned short		WCHAR;
typedef unsigned int		UINT;
//typedef unsigned int		size_t;
typedef const char*			LPCTSTR;
typedef char*				LPTSTR;
typedef const char*			LPCSTR;
typedef char*				LPSTR;
typedef char				TCHAR;
typedef void*				LPVOID;

// Standard constants
#undef FALSE
#undef TRUE
#undef NULL

#define FALSE   0
#define TRUE    1
#define NULL    0

#define _ULONGLONG_
#ifndef _ULONGLONG_


typedef __int64           LONGLONG;

typedef unsigned __int64  ULONGLONG;

typedef LONGLONG          *PLONGLONG;

typedef ULONGLONG         *PULONGLONG;

#endif // _ULONGLONG_


typedef struct tagRECT
{
    LONG    left;
    LONG    top;
    LONG    right;
    LONG    bottom;
} RECT, *PRECT, *LPRECT;

typedef struct tagSIZE
{
    LONG        cx;
    LONG        cy;
} SIZE, *PSIZE, *LPSIZE;

typedef struct tagPOINT
{
    LONG  x;
    LONG  y;
} POINT, *PPOINT, *LPPOINT;

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

#define MAKEWORD(a, b)      ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) | ((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8))
#define MAKELONG(a, b)      ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16))
#define LOWORD(l)           ((WORD)((DWORD_PTR)(l) & 0xffff))
#define HIWORD(l)           ((WORD)((DWORD_PTR)(l) >> 16))
#define LOBYTE(w)           ((BYTE)((DWORD_PTR)(w) & 0xff))
#define HIBYTE(w)           ((BYTE)((DWORD_PTR)(w) >> 8))


#ifndef MAX_PATH
#define MAX_PATH  256
#endif

#ifndef MAX_STRING
#define MAX_STRING    256
#endif

#endif //WIN32

#ifndef ASSERT
    #ifdef _DEBUG
        #ifdef WIN32
            #define ASSERT(f) if(!(f))_asm { int 3 };
        #else
            #include <cassert>
            #define ASSERT(x) if(!(x))TRACE("ASSERT %s:%d!!!\n", __FILE__, __LINE__);
        #endif//WIN32
    #else
        #define ASSERT(x) ((void)0);
    #endif //_DEBUG
#endif//ASSERT

#ifdef WIN32
#define atoi64(x)	_atoi64(x)
#else
#define atoi64(x)	atoll(x)
#endif

#endif //__WINDOWS_DEFINE_H__


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值