-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_network.cpp
168 lines (133 loc) · 4.36 KB
/
win_network.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/****************************************************************
xerxes engine
win_network.cpp
****************************************************************/
/* network.cpp by AegisKnight, aka Chad Austin */
#include "win_network.h"
static void InitializeNetwork()
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 0), &wsadata);
}
////////////////////////////////////////////////////////////////////////////////
// NetworkException ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
NetworkException::NetworkException(const char* message)
: m_message(message)
{
}
////////////////////////////////////////////////////////////////////////////////
const char*
NetworkException::what() const throw()
{
return m_message.c_str();
}
////////////////////////////////////////////////////////////////////////////////
// ServerSocket ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
ServerSocket::ServerSocket(int port)
{
InitializeNetwork();
// create the socket
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET) {
throw NetworkException("socket() failed");
}
// make it nonblocking
unsigned long ul = 1;
if (ioctlsocket(m_socket, FIONBIO, &ul) != 0) {
closesocket(m_socket);
throw NetworkException("ioctlsocket() failed");
}
// bind the socket to an address
sockaddr_in name;
memset(&name, 0, sizeof(name));
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = htons(port);
if (bind(m_socket, (sockaddr*)&name, sizeof(name)) != 0) {
closesocket(m_socket);
throw NetworkException("bind() failed");
}
// now listen on it
if (listen(m_socket, 1) != 0) {
closesocket(m_socket);
throw NetworkException("listen() failed");
}
}
////////////////////////////////////////////////////////////////////////////////
ServerSocket::~ServerSocket()
{
closesocket(m_socket);
}
////////////////////////////////////////////////////////////////////////////////
Socket*
ServerSocket::accept()
{
if (m_socket == INVALID_SOCKET) {
return NULL;
}
sockaddr_in addr;
int addrlen = sizeof(addr);
SOCKET s = ::accept(m_socket, (sockaddr*)&addr, &addrlen);
if (s == INVALID_SOCKET) {
return NULL;
}
return new Socket(s);
}
////////////////////////////////////////////////////////////////////////////////
// Socket //////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Socket::Socket(const char* address, int port)
{
InitializeNetwork();
// resolve address
hostent* hostptr = gethostbyname(address);
if (hostptr == NULL) {
throw NetworkException("gethostbyname() failed");
}
// create socket
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET) {
throw NetworkException("socket() failed");
}
// connect
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr = *(in_addr*)(hostptr->h_addr);
addr.sin_port = htons(port);
if (connect(m_socket, (sockaddr*)&addr, sizeof(addr)) != 0) {
closesocket(m_socket);
throw NetworkException("connect() failed");
}
// set nonblocking
u_long argp = 1L;
int result = ioctlsocket(m_socket, FIONBIO, &argp);
// set tcp_nodelay
char argc = 1;
setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, &argc, 1);
}
////////////////////////////////////////////////////////////////////////////////
Socket::~Socket()
{
closesocket(m_socket);
}
////////////////////////////////////////////////////////////////////////////////
int
Socket::write(int size, const void* bytes)
{
return send(m_socket, (char*)bytes, size, 0);
}
////////////////////////////////////////////////////////////////////////////////
int
Socket::read(int size, void* bytes)
{
return recv(m_socket, (char*)bytes, size, 0);
}
////////////////////////////////////////////////////////////////////////////////
Socket::Socket(SOCKET socket)
: m_socket(socket)
{
}
////////////////////////////////////////////////////////////////////////////////