forked from redis/hiredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiredis_w32.h
118 lines (103 loc) · 2.02 KB
/
hiredis_w32.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef __HIREDIS_W32_H__
#define __HIREDIS_W32_H__
#include <windows.h>
#include <time.h>
#include "sds.h"
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
#define strncasecmp strnicmp
#define strcasecmp stricmp
/**
*
*/
struct timezone
{
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
/**
*
*
*/
static int gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
/*converting file time to unix epoch*/
tmpres /= 10; /*convert into microseconds*/
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
if (NULL != tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
/**
*
*
*/
static int inet_aton(const char *address, struct in_addr *sock)
{
int s;
s = inet_addr(address);
if (s == INADDR_NONE) {
return(0);
}
sock->s_addr = s;
return(1);
}
/**
*
*
*/
static sds get_last_wsock_err()
{
sds msg = NULL;
char* buf = NULL;
LPTSTR lpMsgBuf;
unsigned long last_err = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
last_err,
0,
&lpMsgBuf,
0, NULL
);
if (sizeof(TCHAR) == sizeof(char)) {
size_t size = strlen(lpMsgBuf);
buf = malloc(size + 1);
strcpy(buf, lpMsgBuf);
}
else {
size_t size = wcstombs(NULL, lpMsgBuf, 0);
buf = malloc(size + 1);
wcstombs(buf, lpMsgBuf, size + 1);
}
msg = sdsnew(buf);
LocalFree(lpMsgBuf);
free(buf);
return(msg);
}
#endif