-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetagent_c.cpp
More file actions
152 lines (129 loc) · 4.09 KB
/
netagent_c.cpp
File metadata and controls
152 lines (129 loc) · 4.09 KB
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
#include "netagent.h"
NetClient::NetClient() {};
int NetClient::init(std::string server) {
err = 0;
WSADATA wsaData;
ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
// Validate the parameters
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cout << "WSAStartup failed with error:" << iResult << std::endl;
err = getConnectionStatus_ERR;
return -1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(server.c_str(), DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
std::cout << "getaddrinfo failed with error:" << iResult << std::endl;
WSACleanup();
err = getConnectionStatus_NO_HOST;
return -1;
}
// 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) {
std::cout << "socket failed with error: " << WSAGetLastError() << std::endl;
WSACleanup();
err = getConnectionStatus_ERR;
return -1;
}
// set timeout to 1sec
int ReceiveTimeout = 1000;
int e = setsockopt(ConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&ReceiveTimeout, sizeof(int));
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
err = getConnectionStatus_ERR;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
#ifdef CONSOLE_LOGGING_NETWORKING
std::cout << "Unable to connect to server!\n";
#endif
WSACleanup();
err = getConnectionStatus_ERR;
return -1;
}
unsigned long ul = 1;
int nRet = ioctlsocket(ConnectSocket, FIONBIO, &ul);
if (nRet == SOCKET_ERROR) {
std::cout << "ConnectSocket failed to put the socket into non-blocking mode\n";
// Failed to put the socket into non-blocking mode
return -1;
}
#ifdef CONSOLE_LOGGING_NETWORKING
std::cout << "ConnectSocket in non-blocking mode\n";
#endif
// TCP_NODELAY
/*
DWORD tr = TRUE;
setsockopt(ConnectSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&tr, sizeof(tr));
*/
return 0;
}
NetClient::NetClient(std::string server) {
init(server);
}
NetClient::~NetClient() {
closeConnection();
}
void NetClient::closeConnection() {
// cleanup
closesocket(ConnectSocket);
WSACleanup();
}
int NetClient::sendData(const char *data, int len) {
int iResult = send(ConnectSocket, data, len, 0);
if (iResult == SOCKET_ERROR) {
std::cout << "send failed with error: " << WSAGetLastError() << std::endl;
closesocket(ConnectSocket);
WSACleanup();
err = getConnectionStatus_ERR;
return -1;
}
#ifdef CONSOLE_LOGGING_NETWORKING
std::cout << "Bytes Sent: " << iResult << std::endl;
#endif
return 0;
}
int NetClient::recieveData() {
int iResult = recv(ConnectSocket, recvbuf, MAX_BUF_LEN, 0);
if (iResult == -1 && WSAGetLastError() == WSAEWOULDBLOCK)
return recieveData_NO_NEW_DATA;
if (iResult > 0) {
recvbuflen = iResult;
return recieveData_OK;
}
if (iResult == 0) {
#ifdef CONSOLE_LOGGING_NETWORKING
std::cout << "Connection closed\n";
#endif
err = getConnectionStatus_ERR;
return recieveData_ERR;
}
#ifdef CONSOLE_LOGGING_NETWORKING
std::cout << "recv failed with error: " << WSAGetLastError() << std::endl;
#endif
err = getConnectionStatus_ERR;
return recieveData_ERR;
}
int NetClient::getConnectionStatus() {
return err;
}