Skip to content

Commit aefbf8d

Browse files
committed
Fix bug
1 parent a7a53f5 commit aefbf8d

File tree

7 files changed

+65
-55
lines changed

7 files changed

+65
-55
lines changed

out/bi_web/include/fantasy.hpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class HelloWorldClient final {
114114
frpc::DateTime date_time,
115115
std::function<
116116
void(std::string, Info, uint64_t, std::optional<std::string>)> cb) {
117-
auto req_id = m_req_id.fetch_add(1);
117+
auto req_id = frpc::createUuid();
118118
auto snd_bufs =
119119
makeRequestPacket<HelloWorldClientHelloWorldServer::hello_world>(
120120
req_id,
@@ -140,7 +140,7 @@ class HelloWorldClient final {
140140
void(std::string, Info, uint64_t, std::optional<std::string>)> cb,
141141
const std::chrono::milliseconds& timeout,
142142
std::function<void()> timeout_cb) {
143-
auto req_id = m_req_id.fetch_add(1);
143+
auto req_id = frpc::createUuid();
144144
auto snd_bufs =
145145
makeRequestPacket<HelloWorldClientHelloWorldServer::hello_world>(
146146
req_id,
@@ -393,7 +393,8 @@ class HelloWorldClient final {
393393

394394
private:
395395
template <HelloWorldClientHelloWorldServer type, typename T>
396-
std::vector<zmq::message_t> makeRequestPacket(uint64_t req_id, T&& t) {
396+
std::vector<zmq::message_t> makeRequestPacket(const std::string& req_id,
397+
T&& t) {
397398
auto header = std::make_tuple(req_id, type);
398399
auto buffer = frpc::pack<decltype(header)>(header);
399400
auto packet = frpc::pack<T>(std::forward<T>(t));
@@ -404,7 +405,7 @@ class HelloWorldClient final {
404405
return snd_bufs;
405406
}
406407

407-
void callTimeoutCallback(uint64_t req_id) {
408+
void callTimeoutCallback(const std::string& req_id) {
408409
std::unique_lock lk(m_mtx);
409410
if (m_timeout_cb.find(req_id) == m_timeout_cb.end())
410411
return;
@@ -422,7 +423,7 @@ class HelloWorldClient final {
422423
}
423424
try {
424425
using FrpcHeader =
425-
std::tuple<uint64_t, HelloWorldClientHelloWorldServer>;
426+
std::tuple<std::string, HelloWorldClientHelloWorldServer>;
426427
auto [req_id, req_type] =
427428
frpc::unpack<FrpcHeader>(recv_bufs[0].data(),
428429
recv_bufs[0].size());
@@ -467,9 +468,8 @@ class HelloWorldClient final {
467468
std::unique_ptr<frpc::BiChannel> m_channel;
468469
std::function<void(std::string)> m_error;
469470
std::mutex m_mtx;
470-
std::unordered_map<uint64_t, std::any> m_cb;
471-
std::unordered_map<uint64_t, std::function<void()>> m_timeout_cb;
472-
std::atomic_uint64_t m_req_id{0};
471+
std::unordered_map<std::string, std::any> m_cb;
472+
std::unordered_map<std::string, std::function<void()>> m_timeout_cb;
473473
};
474474

475475
struct HelloWorldServerHandler {
@@ -663,7 +663,7 @@ class HelloWorldServer final {
663663
}
664664
try {
665665
using FrpcHeader =
666-
std::tuple<uint64_t, HelloWorldClientHelloWorldServer>;
666+
std::tuple<std::string, HelloWorldClientHelloWorldServer>;
667667
[[maybe_unused]] auto [req_id, req_type] =
668668
frpc::unpack<FrpcHeader>(recv_bufs[1].data(),
669669
recv_bufs[1].size());
@@ -1304,7 +1304,7 @@ class StreamClient final {
13041304
}
13051305

13061306
auto hello_world() {
1307-
auto req_id = m_req_id.fetch_add(1);
1307+
auto req_id = frpc::createUuid();
13081308
auto header =
13091309
std::make_tuple(req_id, StreamClientStreamServer::hello_world);
13101310

@@ -1383,7 +1383,7 @@ class StreamClient final {
13831383
}
13841384
try {
13851385
using FrpcHeader =
1386-
std::tuple<uint64_t, StreamClientStreamServer, bool>;
1386+
std::tuple<std::string, StreamClientStreamServer, bool>;
13871387
auto [req_id, req_type, is_close] =
13881388
frpc::unpack<FrpcHeader>(recv_bufs[0].data(),
13891389
recv_bufs[0].size());
@@ -1424,9 +1424,8 @@ class StreamClient final {
14241424
std::unique_ptr<frpc::BiChannel> m_channel;
14251425
std::function<void(std::string)> m_error;
14261426
std::mutex m_mtx;
1427-
std::unordered_map<uint64_t, std::any> m_cb;
1428-
std::unordered_map<uint64_t, std::function<void()>> m_close_cb;
1429-
std::atomic_uint64_t m_req_id{0};
1427+
std::unordered_map<std::string, std::any> m_cb;
1428+
std::unordered_map<std::string, std::function<void()>> m_close_cb;
14301429
std::unique_ptr<frpc::ContextPool> m_pool_ptr;
14311430
};
14321431

@@ -1570,7 +1569,8 @@ class StreamServer final {
15701569
return;
15711570
}
15721571
try {
1573-
using FrpcHeader = std::tuple<uint64_t, StreamClientStreamServer>;
1572+
using FrpcHeader =
1573+
std::tuple<std::string, StreamClientStreamServer>;
15741574
auto [req_id, req_type] =
15751575
frpc::unpack<FrpcHeader>(recv_bufs[1].data(),
15761576
recv_bufs[1].size());
@@ -1696,7 +1696,7 @@ class StreamServer final {
16961696
std::unique_ptr<frpc::BiChannel> m_channel;
16971697
std::mutex m_mtx;
16981698
std::unique_ptr<frpc::ContextPool> m_pool_ptr;
1699-
std::unordered_map<uint64_t, std::any> m_channel_mapping;
1699+
std::unordered_map<std::string, std::any> m_channel_mapping;
17001700
};
17011701

17021702
#endif

out/bi_web/include/impl/utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ struct ChannelConfig {
6565
std::size_t channel_size{50000};
6666
};
6767

68-
inline std::string uniqueAddr() {
68+
inline std::string createUuid() {
6969
uuid_t uuid;
7070
char s[37];
7171
uuid_generate_random(uuid);
7272
uuid_unparse(uuid, s);
73-
return "inproc://" + std::string(s);
73+
return std::string(s);
74+
}
75+
76+
inline std::string uniqueAddr() {
77+
return "inproc://" + createUuid();
7478
}
7579

7680
template <typename>

out/include/fantasy.hpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class HelloWorldClient final {
114114
frpc::DateTime date_time,
115115
std::function<
116116
void(std::string, Info, uint64_t, std::optional<std::string>)> cb) {
117-
auto req_id = m_req_id.fetch_add(1);
117+
auto req_id = frpc::createUuid();
118118
auto snd_bufs =
119119
makeRequestPacket<HelloWorldClientHelloWorldServer::hello_world>(
120120
req_id,
@@ -140,7 +140,7 @@ class HelloWorldClient final {
140140
void(std::string, Info, uint64_t, std::optional<std::string>)> cb,
141141
const std::chrono::milliseconds& timeout,
142142
std::function<void()> timeout_cb) {
143-
auto req_id = m_req_id.fetch_add(1);
143+
auto req_id = frpc::createUuid();
144144
auto snd_bufs =
145145
makeRequestPacket<HelloWorldClientHelloWorldServer::hello_world>(
146146
req_id,
@@ -393,7 +393,8 @@ class HelloWorldClient final {
393393

394394
private:
395395
template <HelloWorldClientHelloWorldServer type, typename T>
396-
std::vector<zmq::message_t> makeRequestPacket(uint64_t req_id, T&& t) {
396+
std::vector<zmq::message_t> makeRequestPacket(const std::string& req_id,
397+
T&& t) {
397398
auto header = std::make_tuple(req_id, type);
398399
auto buffer = frpc::pack<decltype(header)>(header);
399400
auto packet = frpc::pack<T>(std::forward<T>(t));
@@ -404,7 +405,7 @@ class HelloWorldClient final {
404405
return snd_bufs;
405406
}
406407

407-
void callTimeoutCallback(uint64_t req_id) {
408+
void callTimeoutCallback(const std::string& req_id) {
408409
std::unique_lock lk(m_mtx);
409410
if (m_timeout_cb.find(req_id) == m_timeout_cb.end())
410411
return;
@@ -422,7 +423,7 @@ class HelloWorldClient final {
422423
}
423424
try {
424425
using FrpcHeader =
425-
std::tuple<uint64_t, HelloWorldClientHelloWorldServer>;
426+
std::tuple<std::string, HelloWorldClientHelloWorldServer>;
426427
auto [req_id, req_type] =
427428
frpc::unpack<FrpcHeader>(recv_bufs[0].data(),
428429
recv_bufs[0].size());
@@ -467,9 +468,8 @@ class HelloWorldClient final {
467468
std::unique_ptr<frpc::BiChannel> m_channel;
468469
std::function<void(std::string)> m_error;
469470
std::mutex m_mtx;
470-
std::unordered_map<uint64_t, std::any> m_cb;
471-
std::unordered_map<uint64_t, std::function<void()>> m_timeout_cb;
472-
std::atomic_uint64_t m_req_id{0};
471+
std::unordered_map<std::string, std::any> m_cb;
472+
std::unordered_map<std::string, std::function<void()>> m_timeout_cb;
473473
};
474474

475475
struct HelloWorldServerHandler {
@@ -663,7 +663,7 @@ class HelloWorldServer final {
663663
}
664664
try {
665665
using FrpcHeader =
666-
std::tuple<uint64_t, HelloWorldClientHelloWorldServer>;
666+
std::tuple<std::string, HelloWorldClientHelloWorldServer>;
667667
[[maybe_unused]] auto [req_id, req_type] =
668668
frpc::unpack<FrpcHeader>(recv_bufs[1].data(),
669669
recv_bufs[1].size());
@@ -1304,7 +1304,7 @@ class StreamClient final {
13041304
}
13051305

13061306
auto hello_world() {
1307-
auto req_id = m_req_id.fetch_add(1);
1307+
auto req_id = frpc::createUuid();
13081308
auto header =
13091309
std::make_tuple(req_id, StreamClientStreamServer::hello_world);
13101310

@@ -1383,7 +1383,7 @@ class StreamClient final {
13831383
}
13841384
try {
13851385
using FrpcHeader =
1386-
std::tuple<uint64_t, StreamClientStreamServer, bool>;
1386+
std::tuple<std::string, StreamClientStreamServer, bool>;
13871387
auto [req_id, req_type, is_close] =
13881388
frpc::unpack<FrpcHeader>(recv_bufs[0].data(),
13891389
recv_bufs[0].size());
@@ -1424,9 +1424,8 @@ class StreamClient final {
14241424
std::unique_ptr<frpc::BiChannel> m_channel;
14251425
std::function<void(std::string)> m_error;
14261426
std::mutex m_mtx;
1427-
std::unordered_map<uint64_t, std::any> m_cb;
1428-
std::unordered_map<uint64_t, std::function<void()>> m_close_cb;
1429-
std::atomic_uint64_t m_req_id{0};
1427+
std::unordered_map<std::string, std::any> m_cb;
1428+
std::unordered_map<std::string, std::function<void()>> m_close_cb;
14301429
std::unique_ptr<frpc::ContextPool> m_pool_ptr;
14311430
};
14321431

@@ -1570,7 +1569,8 @@ class StreamServer final {
15701569
return;
15711570
}
15721571
try {
1573-
using FrpcHeader = std::tuple<uint64_t, StreamClientStreamServer>;
1572+
using FrpcHeader =
1573+
std::tuple<std::string, StreamClientStreamServer>;
15741574
auto [req_id, req_type] =
15751575
frpc::unpack<FrpcHeader>(recv_bufs[1].data(),
15761576
recv_bufs[1].size());
@@ -1696,7 +1696,7 @@ class StreamServer final {
16961696
std::unique_ptr<frpc::BiChannel> m_channel;
16971697
std::mutex m_mtx;
16981698
std::unique_ptr<frpc::ContextPool> m_pool_ptr;
1699-
std::unordered_map<uint64_t, std::any> m_channel_mapping;
1699+
std::unordered_map<std::string, std::any> m_channel_mapping;
17001700
};
17011701

17021702
#endif

out/include/impl/utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ struct ChannelConfig {
6565
std::size_t channel_size{50000};
6666
};
6767

68-
inline std::string uniqueAddr() {
68+
inline std::string createUuid() {
6969
uuid_t uuid;
7070
char s[37];
7171
uuid_generate_random(uuid);
7272
uuid_unparse(uuid, s);
73-
return "inproc://" + std::string(s);
73+
return std::string(s);
74+
}
75+
76+
inline std::string uniqueAddr() {
77+
return "inproc://" + createUuid();
7478
}
7579

7680
template <typename>

template/cpp/bi.inja

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public:
5454
{% for func in value.definitions %}
5555

5656
void {{func.func_name}}({{_format_args(func.inputs)}}, std::function<void({{_format_args_type(func.outputs)}})> cb) {
57-
auto req_id = m_req_id.fetch_add(1);
57+
auto req_id = frpc::createUuid();
5858
auto snd_bufs = makeRequestPacket<{{value.caller}}{{value.callee}}::{{func.func_name}}>(
5959
req_id,
6060
std::make_tuple({{_format_args_name_and_move(func.inputs)}}));
@@ -69,7 +69,7 @@ public:
6969
std::function<void({{_format_args_type(func.outputs)}})> cb,
7070
const std::chrono::milliseconds& timeout,
7171
std::function<void()> timeout_cb) {
72-
auto req_id = m_req_id.fetch_add(1);
72+
auto req_id = frpc::createUuid();
7373
auto snd_bufs = makeRequestPacket<{{value.caller}}{{value.callee}}::{{func.func_name}}>(
7474
req_id,
7575
std::make_tuple({{_format_args_name_and_move(func.inputs)}}));
@@ -176,7 +176,7 @@ public:
176176

177177
private:
178178
template <{{value.caller}}{{value.callee}} type, typename T>
179-
std::vector<zmq::message_t> makeRequestPacket(uint64_t req_id, T&& t) {
179+
std::vector<zmq::message_t> makeRequestPacket(const std::string& req_id, T&& t) {
180180
auto header = std::make_tuple(req_id, type);
181181
auto buffer = frpc::pack<decltype(header)>(header);
182182
auto packet = frpc::pack<T>(std::forward<T>(t));
@@ -187,7 +187,7 @@ private:
187187
return snd_bufs;
188188
}
189189

190-
void callTimeoutCallback(uint64_t req_id) {
190+
void callTimeoutCallback(const std::string& req_id) {
191191
std::unique_lock lk(m_mtx);
192192
if (m_timeout_cb.find(req_id) == m_timeout_cb.end())
193193
return;
@@ -204,7 +204,7 @@ private:
204204
return;
205205
}
206206
try {
207-
using FrpcHeader = std::tuple<uint64_t, {{value.caller}}{{value.callee}}>;
207+
using FrpcHeader = std::tuple<std::string, {{value.caller}}{{value.callee}}>;
208208
auto [req_id, req_type] = frpc::unpack<FrpcHeader>(recv_bufs[0].data(), recv_bufs[0].size());
209209
std::unique_lock lk(m_mtx);
210210
if (m_cb.find(req_id) == m_cb.end())
@@ -237,10 +237,9 @@ private:
237237
std::unique_ptr<frpc::BiChannel> m_channel;
238238
std::function<void(std::string)> m_error;
239239
std::mutex m_mtx;
240-
std::unordered_map<uint64_t, std::any> m_cb;
241-
std::unordered_map<uint64_t, std::function<void()>> m_timeout_cb;
242-
std::atomic_uint64_t m_req_id{0};
243-
};
240+
std::unordered_map<std::string, std::any> m_cb;
241+
std::unordered_map<std::string, std::function<void()>> m_timeout_cb;
242+
};
244243

245244
struct {{value.callee}}Handler {
246245
{% for func in value.definitions %}
@@ -372,7 +371,7 @@ private:
372371
return;
373372
}
374373
try {
375-
using FrpcHeader = std::tuple<uint64_t, {{value.caller}}{{value.callee}}>;
374+
using FrpcHeader = std::tuple<std::string, {{value.caller}}{{value.callee}}>;
376375
[[maybe_unused]] auto [req_id, req_type] = frpc::unpack<FrpcHeader>(recv_bufs[1].data(), recv_bufs[1].size());
377376
switch(req_type) {
378377
{% for func in value.definitions %}

template/cpp/bi_stream.inja

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public:
7171
{% for func in value.definitions %}
7272

7373
auto {{func.func_name}}() {
74-
auto req_id = m_req_id.fetch_add(1);
74+
auto req_id = frpc::createUuid();
7575
auto header = std::make_tuple(req_id, {{value.caller}}{{value.callee}}::{{func.func_name}});
7676

7777
auto buffer = frpc::pack<decltype(header)>(header);
@@ -132,7 +132,7 @@ private:
132132
return;
133133
}
134134
try {
135-
using FrpcHeader = std::tuple<uint64_t, {{value.caller}}{{value.callee}}, bool>;
135+
using FrpcHeader = std::tuple<std::string, {{value.caller}}{{value.callee}}, bool>;
136136
auto [req_id, req_type, is_close] = frpc::unpack<FrpcHeader>(recv_bufs[0].data(), recv_bufs[0].size());
137137
if (is_close) {
138138
std::unique_lock lk(m_mtx);
@@ -171,9 +171,8 @@ private:
171171
std::unique_ptr<frpc::BiChannel> m_channel;
172172
std::function<void(std::string)> m_error;
173173
std::mutex m_mtx;
174-
std::unordered_map<uint64_t, std::any> m_cb;
175-
std::unordered_map<uint64_t, std::function<void()>> m_close_cb;
176-
std::atomic_uint64_t m_req_id{0};
174+
std::unordered_map<std::string, std::any> m_cb;
175+
std::unordered_map<std::string, std::function<void()>> m_close_cb;
177176
std::unique_ptr<frpc::ContextPool> m_pool_ptr;
178177
};
179178

@@ -284,7 +283,7 @@ private:
284283
return;
285284
}
286285
try {
287-
using FrpcHeader = std::tuple<uint64_t, {{value.caller}}{{value.callee}}>;
286+
using FrpcHeader = std::tuple<std::string, {{value.caller}}{{value.callee}}>;
288287
auto [req_id, req_type] = frpc::unpack<FrpcHeader>(recv_bufs[1].data(), recv_bufs[1].size());
289288
switch(req_type) {
290289
{% for func in value.definitions %}
@@ -375,7 +374,7 @@ private:
375374
std::unique_ptr<frpc::BiChannel> m_channel;
376375
std::mutex m_mtx;
377376
std::unique_ptr<frpc::ContextPool> m_pool_ptr;
378-
std::unordered_map<uint64_t, std::any> m_channel_mapping;
377+
std::unordered_map<std::string, std::any> m_channel_mapping;
379378
};
380379

381380
#endif

0 commit comments

Comments
 (0)