forked from AdrianEddy/AIMPSoundcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpServer.cpp
82 lines (68 loc) · 2.26 KB
/
TcpServer.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
#include "TcpServer.h"
#include <winsock2.h>
#include <process.h>
#pragma comment(lib,"ws2_32.lib")
TcpServer::TcpServer(int port, RequestFunc callback) : m_port(port), m_callback(callback), m_deleteOnFinish(true) {
}
void TcpServer::ThreadFunc(void *arg) {
TcpServer *parent = static_cast<TcpServer *>(arg);
if (!parent)
return;
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
OutputDebugString(L"WSAStartup failed\n");
if (parent->m_deleteOnFinish)
delete parent;
return;
}
SOCKET s;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
OutputDebugString(L"Could not create socket\n");
if (parent->m_deleteOnFinish)
delete parent;
return;
}
struct sockaddr_in server, client;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(parent->m_port);
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) {
OutputDebugString(L"bind failed\n");
if (parent->m_deleteOnFinish)
delete parent;
return;
}
listen(s, 3);
int c = sizeof(struct sockaddr_in);
do {
SOCKET new_socket = accept(s, (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET) {
OutputDebugString(L"accept failed\n");
if (parent->m_deleteOnFinish)
delete parent;
return;
}
char request[2048];
memset(request, 0, sizeof(request));
recv(new_socket, request, sizeof(request), 0);
std::string response;
if (parent->m_callback(parent, request, response)) {
if (!response.empty()) {
send(new_socket, response.c_str(), response.size(), 0);
}
closesocket(new_socket);
closesocket(s);
WSACleanup();
if (parent->m_deleteOnFinish) {
delete parent;
}
break;
}
} while (true);
}
bool TcpServer::Start() {
HANDLE hThread = (HANDLE)_beginthread(ThreadFunc, 0, this);
return hThread != INVALID_HANDLE_VALUE;
}
TcpServer::~TcpServer() {
}