-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.cpp
121 lines (95 loc) · 2.93 KB
/
example.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
#include "win-pipe.h"
#include <array>
#include <chrono>
#include <iostream>
void run_receiver();
void receiver_callback1(uint8_t* data, size_t size);
void receiver_callback2(uint8_t* data, size_t size);
void run_sender();
int main(int argc, void** argv)
{
if (argc < 2) {
std::cout << "Specify sender/receiver." << std::endl;
return EXIT_FAILURE;
}
const char* arg1 = reinterpret_cast<const char*>(argv[1]);
if (strcmp(arg1, "receiver") == 0)
run_receiver();
else if (strcmp(arg1, "sender") == 0)
run_sender();
else {
std::cout << "Unrecognized arg, must be sender/receiver." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void run_receiver()
{
std::cout << "Type callback to change behavior. Type exit to quit." << std::endl;
int current_callback = 0;
win_pipe::callback_t callbacks[2] { receiver_callback1, receiver_callback2 };
std::unordered_map<int, win_pipe::receiver> map;
map[0] = win_pipe::receiver { "win-pipe_example", callbacks[current_callback] };
std::string op;
while (true) {
std::getline(std::cin, op);
if (op == "callback") {
current_callback++;
map[0].set_callback(callbacks[current_callback % 2]);
}
else if (op == "exit")
break;
}
}
void receiver_callback1(uint8_t* data, [[maybe_unused]] size_t size)
{
using namespace std::chrono;
static int count = 0;
int oldCount = count;
count++;
auto end = high_resolution_clock::now();
if (oldCount % 2 == 0)
{
auto* start = reinterpret_cast<decltype(end)*>(data);
auto latency = duration_cast<nanoseconds>(end - *start);
std::cout << "latency: " << latency.count() << "ns\n";
}
else
{
auto* message = reinterpret_cast<const char*>(data);
std::cout << message << std::endl;
}
}
void receiver_callback2([[maybe_unused]] uint8_t* data, size_t size)
{
using namespace std::chrono;
static int count = 0;
int oldCount = count;
count++;
auto end = high_resolution_clock::now();
if (oldCount % 2 == 0)
{
auto* start = reinterpret_cast<decltype(end)*>(data);
auto latency = duration_cast<nanoseconds>(end - *start);
std::cout << "latency: " << latency.count() << "ns\n";
}
else
{
std::cout << "received a message " << size << " bytes long!\n";
}
}
void run_sender()
{
using namespace std::chrono;
std::cout << "Send messages to the receiver! Type exit to quit." << std::endl;
auto sender = win_pipe::sender { "win-pipe_example" };
std::string message;
while (true) {
std::getline(std::cin, message);
auto start = high_resolution_clock::now();
sender.send(&start, sizeof(decltype(start)));
sender.send(message.c_str(), (DWORD)message.length() + 1);
if (message == "exit")
break;
}
}