-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathServerSend.cpp
71 lines (63 loc) · 1.45 KB
/
ServerSend.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
#include "ServerSend.h"
ServerSend::ServerSend(Server* s)
{
server = s;
running = false;
}
ServerSend::~ServerSend(void)
{
stop();
}
void
ServerSend::start()
{
if(!running)
{
sender = new Thread(this);
sender->start("ServerSend");
running = true;
}
}
void
ServerSend::stop()
{
if(running)
{
sender->stop();
}
running = false;
}
void
ServerSend::run()
{
while(server->isConnected())
{
EnterCriticalSection(&server->sendQueueLock);
if(!server->sendQueue.empty())
{
int result = 0;
wstring msg = server->sendQueue.front();
server->sendQueue.pop();
LeaveCriticalSection(&server->sendQueueLock);
size_t allocateSize = (msg.length() + 1)*sizeof(wchar_t);
char *szMessage = (char*)malloc(allocateSize);
size_t charsConverted;
wcstombs_s(&charsConverted, szMessage, allocateSize, msg.c_str(), msg.length() + 1);
result = send(server->serverSocket, szMessage, static_cast<int>(charsConverted), 0);
free(szMessage);
DebugPrint(L"Capture-ServerSend: Allocated: %i, Converted: %i, Sent: %i", allocateSize, charsConverted, result);
if(result == SOCKET_ERROR)
{
DebugPrint(L"Capture-ServerSend: Socket Error %i: %s", WSAGetLastError(), szMessage);
if(WSAGetLastError() == WSAENOTCONN)
{
server->setConnected(false);
}
}
} else {
LeaveCriticalSection(&server->sendQueueLock);
Sleep(100);
}
}
running = false;
}