-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathBackgroundService.cpp
More file actions
100 lines (83 loc) · 2.97 KB
/
BackgroundService.cpp
File metadata and controls
100 lines (83 loc) · 2.97 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "BackgroundService.h"
#include <iostream>
// Global constants definition
const std::string PIPE_NAME = "\\\\.\\pipe\\HelloWorldService";
const std::string HELLO_WORLD_RESPONSE = "Hello world";
HelloWorldService::HelloWorldService() : m_running(false) {}
HelloWorldService::~HelloWorldService() {
Stop();
}
bool HelloWorldService::Start() {
if (m_running) {
return false;
}
m_running = true;
m_serviceThread = std::thread(&HelloWorldService::ServiceLoop, this);
std::cout << "Hello World Service started. Listening on pipe: " << PIPE_NAME << std::endl;
return true;
}
void HelloWorldService::Stop() {
if (m_running) {
m_running = false;
if (m_serviceThread.joinable()) {
m_serviceThread.join();
}
std::cout << "Hello World Service stopped." << std::endl;
}
}
bool HelloWorldService::IsRunning() const {
return m_running;
}
void HelloWorldService::ServiceLoop() {
while (m_running) {
// Create named pipe
HANDLE hPipe = CreateNamedPipeA(
PIPE_NAME.c_str(),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1, // Max instances
1024, // Output buffer size
1024, // Input buffer size
0, // Default timeout
nullptr // Security attributes
);
if (hPipe == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create named pipe. Error: " << GetLastError() << std::endl;
continue;
}
// Wait for client connection
std::cout << "Waiting for client connection..." << std::endl;
if (ConnectNamedPipe(hPipe, nullptr) || GetLastError() == ERROR_PIPE_CONNECTED) {
std::cout << "Client connected!" << std::endl;
HandleClient(hPipe);
}
else {
std::cerr << "Failed to connect to client. Error: " << GetLastError() << std::endl;
}
CloseHandle(hPipe);
}
}
void HelloWorldService::HandleClient(HANDLE hPipe) {
char buffer[1024];
DWORD bytesRead;
DWORD bytesWritten;
// Read request from client
if (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &bytesRead, nullptr)) {
buffer[bytesRead] = '\0';
std::cout << "Received request: " << buffer << std::endl;
// Send "Hello world" response
const char* response = HELLO_WORLD_RESPONSE.c_str();
if (WriteFile(hPipe, response, static_cast<DWORD>(HELLO_WORLD_RESPONSE.length()), &bytesWritten, nullptr)) {
std::cout << "Sent response: " << response << std::endl;
}
else {
std::cerr << "Failed to send response. Error: " << GetLastError() << std::endl;
}
}
else {
std::cerr << "Failed to read from client. Error: " << GetLastError() << std::endl;
}
// Flush the pipe to allow the client to read the pipe's contents before disconnecting
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
}