-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathServer.cpp
executable file
·243 lines (226 loc) · 5.99 KB
/
Server.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
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
/*
* RDPShare: Windows Desktop Sharing remote control interface
*
* Copyright 2016 Simul Piscator
*
* RDPShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RDPShare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RDPShare. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Server.h"
#include <Winsock2.h>
#include <Windows.h>
#include <process.h>
#include "Utils.h"
#include <mutex>
static struct WSAInit
{
WSAInit()
{
WSADATA ignored;
::WSAStartup(MAKEWORD(2, 2), &ignored);
}
~WSAInit()
{
::WSACleanup();
}
} sWSAInit;
struct Server::Private
{
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
Server* this_ = (Server*)::GetWindowLongPtrA(hwnd, GWLP_USERDATA);
switch (msg)
{
case WM_CREATE:
::SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)((CREATESTRUCT*)lparam)->lpCreateParams);
return 0;
case WM_USER:
this_->OnAsyncMessage(int(wparam), (void*)lparam);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
Private( Server* pSelf)
: mpSelf(pSelf), mMessageThread(0), mServerThread(0), mListeningSocket(-1)
{
WNDCLASSA c = { 0 };
c.lpfnWndProc = &WindowProc;
c.hInstance = ::GetModuleHandleA(nullptr);
c.lpszClassName = "ServerMessageWindow";
::RegisterClassA(&c);
mHwnd = ::CreateWindowA(c.lpszClassName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, c.hInstance, mpSelf);
}
~Private()
{
}
void InitAddress(unsigned long ip, unsigned short port);
static void Send(SOCKET s, const std::string&);
static std::string Receive(SOCKET s);
static unsigned int WINAPI ThreadProc(LPVOID);
unsigned int WaitForServerThread();
Server* mpSelf;
std::mutex mMutex;
HWND mHwnd;
sockaddr_in mAddress;
unsigned int mMessageThread;
HANDLE mServerThread;
SOCKET mListeningSocket;
};
Server::Server(unsigned long ip, unsigned short port)
: p(new Private(this))
{
p->InitAddress(ip, port);
}
Server::~Server()
{
Terminate();
p->WaitForServerThread();
delete p;
}
void
Server::Private::InitAddress(unsigned long ip, unsigned short port)
{
::memset(&mAddress, 0, sizeof(mAddress));
mAddress.sin_family = AF_INET;
mAddress.sin_addr.s_addr = ip;
mAddress.sin_port = htons(port);
}
DWORD
Server::Run(unsigned long ip, unsigned short port)
{
p->mMessageThread = ::GetCurrentThreadId();
p->InitAddress(ip, port);
WSA_SucceedOrDie r;
SOCKET ls = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
r = ::bind(ls, (sockaddr*)&p->mAddress, sizeof(p->mAddress));
r = ::listen(ls, SOMAXCONN);
p->mListeningSocket = ls;
p->mServerThread = (HANDLE)::_beginthreadex(nullptr, 0, &Private::ThreadProc, p, 0, nullptr);
BOOL_SucceedOrDie b = p->mServerThread != 0;
MSG msg;
while (::GetMessageA(&msg, NULL, 0, 0) > 0)
{
std::lock_guard<std::mutex> lock(p->mMutex);
::TranslateMessage(&msg);
::DispatchMessageA(&msg);
}
Terminate();
return p->WaitForServerThread();
}
unsigned int
Server::Private::WaitForServerThread()
{
DWORD exitCode = 0;
HANDLE hThread = ::InterlockedExchangePointer(&mServerThread, 0);
if (hThread)
{
BOOL_SucceedOrDie b = BOOL(WAIT_OBJECT_0 == ::WaitForSingleObject(hThread, INFINITE));
b = ::GetExitCodeThread(hThread, &exitCode);
::CloseHandle(hThread);
}
return exitCode;
}
void
Server::Terminate()
{
SOCKET s = ::InterlockedExchange(&p->mListeningSocket,-1);
if ( s != -1)
WSA_SucceedOrDie r = ::closesocket(s);
}
void
Server::Private::Send(SOCKET s, const std::string& data)
{
int remaining = static_cast<int>(data.length());
const char* p = data.c_str();
while (remaining > 0)
{
fd_set writefds;
FD_ZERO(&writefds);
FD_SET(s, &writefds);
timeval t = { 1, 0 };
WSA_SucceedOrDie r = ::select(static_cast<int>(s) + 1, nullptr, &writefds, nullptr, &t);
if (FD_ISSET(s, &writefds))
r = ::send(s, p, remaining, 0);
else
r = remaining;
p += r;
remaining -= r;
}
}
std::string
Server::Private::Receive(SOCKET s)
{
std::string data;
char c = -1;
while (c)
{
c = 0;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(s, &readfds);
timeval t = { 20, 0 };
WSA_SucceedOrDie r = ::select(static_cast<int>(s) + 1, &readfds, nullptr, nullptr, &t);
if (FD_ISSET(s, &readfds))
r = ::recv(s, &c, 1, 0);
if (c == '\n')
c = 0;
if (c && c != '\r')
data += c;
}
return data;
}
unsigned int WINAPI
Server::Private::ThreadProc(LPVOID data)
{
Server::Private* this_ = static_cast<Server::Private*>(data);
unsigned int result = 0;
try
{
while (this_->mListeningSocket != -1)
{
SOCKET s = ::accept(this_->mListeningSocket, nullptr, nullptr);
if (s != -1)
{
std::lock_guard<std::mutex> lock(this_->mMutex);
std::string request = Receive(s);
Send(s, this_->mpSelf->OnRequest(request));
WSA_SucceedOrDie r = ::closesocket(s);
}
}
}
catch( WSA_SucceedOrDie)
{
result = ::WSAGetLastError();
}
catch (BOOL_SucceedOrDie)
{
result = ::GetLastError();
}
::PostThreadMessageA(this_->mMessageThread, WM_QUIT, 0, 0);
return result;
}
std::string
Server::Request( const std::string& command )
{
SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
WSA_SucceedOrDie r = ::connect(s, (sockaddr*)&p->mAddress, sizeof(p->mAddress));
Private::Send(s,command + "\r\n");
std::string data = Private::Receive(s);
r = ::closesocket(s);
return data;
}
void
Server::AsyncMessage(int msg, void* arg)
{
BOOL_SucceedOrDie b = ::PostMessageA(p->mHwnd, WM_USER, msg, LPARAM(arg));
}