-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainNamedPipeServer.cpp
More file actions
79 lines (71 loc) · 2.03 KB
/
Copy pathmainNamedPipeServer.cpp
File metadata and controls
79 lines (71 loc) · 2.03 KB
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
// NamedPipeServer.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "stdafx.h"
#include <iostream>
#include <mutex>
#include <sstream>
using namespace std;
void pipeWorkerMessageHandler(
void* context,
w32::CHandle& handle,
CIOBuffer& input,
CIOBuffer& output)
{
std::mutex* pmutex = (std::mutex*) context;
DWORD threadId = GetCurrentThreadId();
Sleep(1000); //simulate work
LPWSTR text = (LPWSTR)input.Ptr();
{
lock_guard< std::mutex> lock(*pmutex);
std::wcout << L"Thread " << threadId << L" recieved: " << text << std::endl;
}
wstringstream stream;
stream << L"Message answered by thread " << threadId;
wstring message = stream.str();
DWORD bytes = (message.size() + 1 ) * sizeof(wstring::value_type);
memcpy(
output.Ptr(),
(void*)message.c_str(),
bytes);
output.SetOffset(bytes);
}
int main()
{
try
{
std::mutex g_mutex;
wstring name(L"\\\\.\\Pipe\\Named2Pipe");
CNamedPipeServer server(name,
pipeWorkerMessageHandler,
&g_mutex,
256,
256,
10);
/*
cout << "Make a choice:" << endl;
cout << "==============" << endl;
cout << "s: start serving" << endl;
cout << "q: quit" << endl << endl;
cout << "Choice: "; */
char choice = 0;
do
{
choice = getchar();
if (choice == 'q') {
cout << "Initiating shutdown" << endl;
server.Shutdown();
cout << "Waiting until everything is shutdown" << endl;
server.WaitUntilFinished(INFINITE);
cout << "Shutdown finished" << endl;
}
else if (choice == 's') {
cout << "Starting server connections" << endl;
server.StartServing();
}
} while (choice != 'q');
}
catch (exception& ex)
{
cout << ex.what() << endl;
}
}