-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKiNet_windows.hpp
267 lines (235 loc) · 9.52 KB
/
KiNet_windows.hpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#pragma once
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0503
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <stdexcept>
#include <string>
#define KiNet_DEFAULT_BUFLEN 512
#define KiNet_DEFAULT_PORT "27015"
namespace kinet
{
enum class Status
{
no_error = 0,
ip_error = 1,
binding_error = 2,
port_error = 3,
send_error = 4,
receive_error = 5,
connect_error = 6,
};
namespace client {
class Socket;
}
namespace server {
class Socket;
}
class WSAData
{
private:
static bool initialised;
protected:
WSAData() noexcept {
if (!initialised) {
::WSADATA data;
auto err = ::WSAStartup(MAKEWORD(2, 2), &data);
if (err) {
// handle failure
std::string error = "WSAData couldn't start! Error Code: " + std::to_string(err);
throw std::runtime_error(error.c_str());
} else {
initialised = true;
}
}
}
~WSAData() noexcept {
::WSACleanup();
}
};
bool WSAData::initialised = false;
namespace client
{
class Socket : private WSAData
{
public:
void connect(std::string IP,std::string PORT)
{
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo(IP.c_str(), PORT.c_str(), &hints, &result);
if ( iResult != 0 )
{
//printf("getaddrinfo failed with error: %d\n", iResult);
std::string error = "getaddrinfo failed with error:" + std::to_string(iResult);
throw std::runtime_error(error.c_str());
WSACleanup();
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ; ptr=ptr->ai_next)
{
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET)
{
//printf("socket failed with error: %ld\n", WSAGetLastError());
std::string error = "getaddrinfo failed with error:" + WSAGetLastError();
throw std::runtime_error(error.c_str());
WSACleanup();
}
// Connect to server.
iResult = ::connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET)
{
std::string error = "Couldn't connect to server!";
throw std::runtime_error(error.c_str());
WSACleanup();
}
}
void send(std::string msg) //bit buffer
{
iResult = ::send( ConnectSocket, msg.c_str(), static_cast<int>(msg.size() + 1), 0 );
}
void receive(std::string& msg) //bit buffer
{
msg.reserve(KiNet_DEFAULT_BUFLEN); // msg.capacity() should == size
msg.resize(KiNet_DEFAULT_BUFLEN); // forces the string to allocate memory and makes sure it's usable
iResult = recv(ClientSocket, &msg[0], static_cast<int>(msg.capacity()), 0);
}
private:
// static kinet::WSAData wsa;
//WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = new char[5000];
char recvbuf[KiNet_DEFAULT_BUFLEN];
int iResult;
int recvbuflen = KiNet_DEFAULT_BUFLEN;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
};
}
namespace server
{
class Socket : private WSAData
{
public:
struct client
{
friend class Socket;
SOCKET ss;
bool connected;
private:
client() = default;
};
client clients[10000]; //list
u_int clients_connected = 0;
void start(std::string IP, std::string PORT)
{
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(IP.c_str(), PORT.c_str(), &hints, &result);
if ( iResult != 0 ) {
//printf("getaddrinfo failed with error: %d\n", iResult);
std::string error = "getaddrinfo failed with error:" + iResult;
throw std::runtime_error(error.c_str());
WSACleanup();
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
//printf("socket failed with error: %ld\n", WSAGetLastError());
std::string error = "socket failed with error:" + WSAGetLastError();
throw std::runtime_error(error.c_str());
freeaddrinfo(result);
WSACleanup();
}
// Setup the TCP listening socket
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
//printf("bind failed with error: %d\n", WSAGetLastError());
std::string error = "bind failed with error:" + WSAGetLastError();
throw std::runtime_error(error.c_str());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
}
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
//printf("listen failed with error: %d\n", WSAGetLastError());
std::string error = "listen failed with error:" + WSAGetLastError();
throw std::runtime_error(error.c_str());
closesocket(ListenSocket);
WSACleanup();
}
freeaddrinfo(result);
}
int accept()
{
ClientSocket = ::accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
//printf("accept failed with error: %d\n", WSAGetLastError());
std::string error = "accept failed with error:" + WSAGetLastError();
throw std::runtime_error(error.c_str());
closesocket(ListenSocket);
WSACleanup();
}
clients[clients_connected++].ss = ClientSocket;
clients[clients_connected].connected = true;
return clients_connected;
}
void disconnect(int id)
{
clients[id].connected = false;
}
void send_all(std::string msg)
{
for(int i = 0; i < clients_connected; i++)
iResult = ::send(clients[i].ss, msg.c_str(), static_cast<int>(msg.size() + 1), 0 );
}
void send_to_id(std::string msg,int id)
{
iResult = ::send(clients[id].ss, msg.c_str(), static_cast<int>(msg.size() + 1), 0 );
}
void receive_from_id(std::string& msg, int id) {
msg.reserve(KiNet_DEFAULT_BUFLEN); // msg.capacity() should == size
msg.resize(KiNet_DEFAULT_BUFLEN); // forces the string to allocate memory and makes sure it's usable
iResult = recv(clients[id].ss, &msg[0], static_cast<int>(msg.capacity()), 0);
}
private:
//static kinet::WSAData wsa;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
int iSendResult;
};
}
}