forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxWSA.cpp
105 lines (78 loc) · 1.74 KB
/
xWSA.cpp
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
#include "xWSA.h"
#include "swap.h"
#ifndef FOR_RING0
#include <ws2tcpip.h>
class xWSA_base
{
private:
xWSA_base();
~xWSA_base();
friend static void xWSA_init();
};
xWSA_base::xWSA_base()
{
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2,2), &wsaData))
throw std::runtime_error("WSAStartup失败");
}
xWSA_base::~xWSA_base()
{
WSACleanup();
}
static void xWSA_init()
{
static xWSA_base xwb;
}
xWSA::xWSA()
{
xWSA_init();
}
sockaddr_in AddrInfo(const char* host,const char* ports)
{
ADDRINFOA* res;
ADDRINFOA hints;
memset(&hints, 0, sizeof(hints));
xWSA wsa;
if(GetAddrInfoA(host,ports,&hints,&res))
{
throw std::runtime_error("GetAddrInfo失败");
}
sockaddr_in addrto;
memcpy(&addrto,res->ai_addr,sizeof(addrto));
FreeAddrInfoA(res);
return addrto;
}
sockaddr_in AddrInfo(const wchar_t* host,const wchar_t* ports)
{
ADDRINFOW* res;
ADDRINFOW hints;
memset(&hints,0,sizeof(hints));
xWSA wsa;
if(GetAddrInfoW(host,ports,&hints,&res))
{
throw std::runtime_error("GetAddrInfo失败");
}
sockaddr_in addrto;
memcpy(&addrto,res->ai_addr,sizeof(addrto));
FreeAddrInfoW(res);
return addrto;
}
sockaddr_in AddrInfo(const unsigned long host,const unsigned short ports)
{
sockaddr_in addrto;
memset(&addrto,0,sizeof(addrto));
addrto.sin_family = AF_INET;
addrto.sin_addr.s_addr = htonl(host);
addrto.sin_port = htons(ports);
return addrto;
}
xmsg IpString(const sockaddr_in& addr)
{
return xmsg()
<< (int)addr.sin_addr.s_net << '.'
<< (int)addr.sin_addr.s_host << '.'
<< (int)addr.sin_addr.s_lh << '.'
<< (int)addr.sin_addr.s_impno << ':'
<< (int)htons(addr.sin_port);
}
#endif //#ifndef FOR_RING0