-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
138 lines (123 loc) · 2.96 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
#include <windows.h>
#include <shared_mutex>
#include <thread>
#include <sstream>
#include <unordered_map>
#include <string>
#include "common.h"
#include "server.h"
#include "serialization.h"
using namespace std;
int main()
{
log("I am the server");
Store store;
while (TRUE) {
const DWORD pipeMode{ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT };
const HANDLE h{ CreateNamedPipe(
pipeName,
PIPE_ACCESS_DUPLEX,
pipeMode,
PIPE_UNLIMITED_INSTANCES,
bufSize,
bufSize,
0,
nullptr
) };
if (h == INVALID_HANDLE_VALUE) {
throw GetLastError();
}
const bool success = ConnectNamedPipe(h, nullptr);
if (!success) {
cout << "0x" << hex << GetLastError() << endl;
throw GetLastError();
}
log("Connected to a client");
// create a separate thread so can handle clients asynchronously
thread{[h, &store]() {
read(h, store);
FlushFileBuffers(h);
const bool dSuccess = DisconnectNamedPipe(h);
if (!dSuccess) {
cout << "0x" << hex << GetLastError() << endl;
throw GetLastError();
}
CloseHandle(h);
}}.detach();
}
}
void read(HANDLE h, Store &store) {
const auto action{ readHeader(h) };
switch (action.type) {
case Type::Send: {
log("Saving data for key " + action.key);
{
// only one thread can write at a time
lock_guard<shared_mutex> lg{ store.lock };
store.records[action.key] = readData(h);
}
log("Saved data for key " + action.key);
saveSuccess(h);
break;
}
case Type::Get: {
log("Getting data for key " + action.key);
{
// any number of threads can read
shared_lock<shared_mutex> lg{ store.lock };
returnData(h, store.records.at(action.key));
}
log("Got data for key " + action.key);
break;
}
}
}
void saveSuccess(HANDLE h) {
DWORD bytesWritten{ 0 };
const bool success = WriteFile(
h,
&saveSuccessStatus[0],
saveSuccessStatus.size(),
&bytesWritten,
nullptr
);
FlushFileBuffers(h);
if (!success) {
cout << "0x" << hex << GetLastError() << endl;
throw GetLastError();
}
}
void returnData(HANDLE h, string data) {
DWORD bytesWritten{ 0 };
const bool success = WriteFile(
h,
&data[0],
data.size(),
&bytesWritten,
nullptr
);
FlushFileBuffers(h);
if (!success) {
cout << "0x" << hex << GetLastError() << endl;
throw GetLastError();
}
}
string readData(HANDLE h) {
vector<char> buf(bufSize);
DWORD bytesRead;
const bool readSuccess = ReadFile(h, buf.data(), buf.size(), &bytesRead, nullptr);
if (!readSuccess) {
throw GetLastError();
}
return string{ buf.data(), bytesRead };
}
Action readHeader(HANDLE h) {
vector<char> buf(bufSize);
DWORD bytesRead;
const bool readSuccess = ReadFile(h, buf.data(), buf.size(), &bytesRead, nullptr);
if (!readSuccess) {
throw GetLastError();
}
auto action = deserialize<Action>(string{ buf.data(), bytesRead });
return action;
}