-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.cpp
124 lines (105 loc) · 3.01 KB
/
socket.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
#include "pch.h"
#include "socket.h"
int Socket::send(std::string msg) {
if (done)
return SOCKET_ERROR;
int iResult = ::send(ConnectSocket, msg.c_str(), (int) msg.length(), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n, closing socket", WSAGetLastError());
close();
}
return iResult;
}
void Socket::setCallback(std::function<int(std::string)> _callback) {
callback = _callback;
for (int i = 0; i < callbackStack.size(); i++) {
callback(callbackStack[i]);
}
callbackStack.clear();
}
void Socket::listener() {
char recvbuf[512];
do {
int iResult = recv(ConnectSocket, recvbuf, 512, 0);
if (iResult > 0) {
recvbuf[iResult] = '\0';
if (callback != nullptr) {
callback(std::string(recvbuf));
} else {
callbackStack.push_back(std::string(recvbuf));
}
} else if (iResult == 0) {
printf("connection with server closed, closing socket\n");
close();
} else {
printf("recv failed with error %d, closing socket\n", WSAGetLastError());
close();
break;
}
} while (!done);
}
void Socket::close() {
done = true;
}
Socket::Socket(std::string ip, std::function<int(std::string)> _callback) {
callback = _callback;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
close();
}
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
printf("%s\n", ip.c_str());
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(ip.c_str(), DEFAULT_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
close();
return;
}
// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr=result;
// Create a SOCKET for connecting to server
ConnectSocket = INVALID_SOCKET;
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
close();
return;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("Unable to connect to server!\n");
close();
}
// Should really try the next address returned by getaddrinfo
// if the connect call failed
// But for this simple example we just free the resources
// returned by getaddrinfo and print an error message
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
close();
}
if (!closed())
listenerHandler = std::thread(&Socket::listener, this);
};
Socket::~Socket() {
done = true;
if (listenerHandler.joinable())
listenerHandler.join();
closesocket(ConnectSocket);
WSACleanup();
};