#include <windows.h>
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const char* PIPE_NAME = "\\\\.\\pipe\\PilgrimagePipe";
int main()
{
setlocale(LC_ALL, "Russian");
srand((unsigned)time(nullptr) ^ (unsigned)GetCurrentProcessId());
char buffer[256];
DWORD bytesRead;
DWORD bytesWritten;
HANDLE hPipe;
int pilgrimId = (int)GetCurrentProcessId();
int patienceMs = 2000 + rand() % 5001; // 2000..7000
if (!WaitNamedPipeA(PIPE_NAME, NMPWAIT_WAIT_FOREVER))
{
cout << "Server is not available." << endl;
return 1;
}
hPipe = CreateFileA(
PIPE_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hPipe == INVALID_HANDLE_VALUE)
{
cout << "Failed to connect to server." << endl;
return 1;
}
cout << "Connected to server." << endl;
cout << "Pilgrim id: " << pilgrimId
<< ", patience: " << patienceMs << " ms" << endl;
string request = to_string(pilgrimId) + " " + to_string(patienceMs);
BOOL writeOk = WriteFile(
hPipe,
request.c_str(),
(DWORD)request.size(),
&bytesWritten,
NULL
);
if (!writeOk)
{
cout << "Failed to write to server." << endl;
CloseHandle(hPipe);
return 1;
}
cout << "Sent: " << request << endl;
BOOL readOk = ReadFile(
hPipe,
buffer,
sizeof(buffer) - 1,
&bytesRead,
NULL
);
if (!readOk || bytesRead == 0)
{
cout << "Failed to read from server. GLE=" << GetLastError() << endl;
CloseHandle(hPipe);
return 1;
}
buffer[bytesRead] = '\0';
string response = buffer;
cout << "Response from server: " << response << endl;
CloseHandle(hPipe);
return 0;
}