-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathipc_mqueue.cpp
More file actions
107 lines (82 loc) · 2.35 KB
/
ipc_mqueue.cpp
File metadata and controls
107 lines (82 loc) · 2.35 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
101
102
103
104
105
106
107
// Revision 3: Message queue module updated
#include "ipc_mqueue.h"
#include "security.h"
#include "logger.h"
#include <windows.h>
#include <iostream>
#define PIPE_NAME L"\\\\.\\pipe\\MyMsgQueue"
//
// =======================
// MESSAGE QUEUE SEND
// =======================
//
void mqSend() {
HANDLE pipe;
// Try connecting until receiver is ready
while (true) {
pipe = CreateFileW(
PIPE_NAME,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (pipe != INVALID_HANDLE_VALUE)
break; // Connected successfully
Sleep(200); // Wait for receiver
}
std::string msg;
std::cout << "Enter message: ";
std::cin.ignore();
std::getline(std::cin, msg);
msg = encryptData(msg);
DWORD written;
BOOL ok = WriteFile(pipe, msg.c_str(), msg.size(), &written, NULL);
if (!ok) {
std::cout << "Failed to send message.\n";
} else {
std::cout << "Message sent.\n";
logEvent("[MSG QUEUE] Sent encrypted message.");
}
CloseHandle(pipe);
}
//
// =======================
// MESSAGE QUEUE RECEIVE
// =======================
//
void mqReceive() {
HANDLE pipe = CreateNamedPipeW(
PIPE_NAME,
PIPE_ACCESS_INBOUND,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, 256, 256, 0, NULL
);
if (pipe == INVALID_HANDLE_VALUE) {
std::cout << "Failed to create queue.\n";
return;
}
std::cout << "Waiting for sender...\n";
BOOL connected = ConnectNamedPipe(pipe, NULL);
if (!connected) {
if (GetLastError() != ERROR_PIPE_CONNECTED) {
std::cout << "Failed to connect.\n";
CloseHandle(pipe);
return;
}
}
char buffer[256] = {0};
DWORD bytesRead = 0;
BOOL ok = ReadFile(pipe, buffer, sizeof(buffer), &bytesRead, NULL);
if (!ok || bytesRead == 0) {
std::cout << "No data received.\n";
CloseHandle(pipe);
return;
}
std::string decrypted = decryptData(std::string(buffer, bytesRead));
std::cout << "Received: " << decrypted << "\n";
logEvent("[MSG QUEUE] Received & decrypted message.");
CloseHandle(pipe);
}