Skip to content

Commit 5d814b4

Browse files
committed
chore: give better names to files
Give better names to files.
1 parent a13671a commit 5d814b4

File tree

10 files changed

+193
-189
lines changed

10 files changed

+193
-189
lines changed
Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
1-
// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]>
2-
//
3-
// SPDX-License-Identifier: MIT
4-
5-
#include <boost/cobalt.hpp>
6-
7-
#include <boost/asio/as_tuple.hpp>
8-
#include <boost/asio/experimental/concurrent_channel.hpp>
9-
#include <boost/asio/redirect_error.hpp>
10-
#include <boost/asio/this_coro.hpp>
11-
12-
namespace cobalt = boost::cobalt;
13-
using boost::system::error_code;
14-
15-
template <typename Signature>
16-
using cchannel = boost::asio::experimental::concurrent_channel<Signature>;
17-
18-
// this is a function doing some CPU heavy work that should be offloaded onto a
19-
// thread
20-
inline cobalt::promise<int> cpu_intense_work(int a, int b) { co_return a + b; }
21-
22-
// this channel is used to send a response to completed work
23-
using response_channel = cchannel<void(std::exception_ptr, int)>;
24-
// this channel is used to send a request to a working thread
25-
using function_handler = void(error_code, int, int, response_channel* res);
26-
27-
using request_channel = cchannel<function_handler>;
28-
29-
// the worker wrapper
30-
inline cobalt::thread worker(request_channel& work) {
31-
while (work.is_open()) {
32-
auto [ec, a, b, respond_to] =
33-
co_await work.async_receive(boost::asio::as_tuple(cobalt::use_op));
34-
if (ec) // done, ignore. in our code this is only triggered by closing the
35-
// channel
36-
break;
37-
38-
// to emulate this being like awaiting on the same thread, we also deliver
39-
// an exception.
40-
std::exception_ptr ep;
41-
int res = 0;
42-
try {
43-
res = co_await cpu_intense_work(a, b);
44-
} catch (...) {
45-
// this way exception get sent to the awaiting coro as if it was a call.
46-
ep = std::current_exception();
47-
}
48-
// send the response. If the channel is closed, the program will terminate!
49-
co_await respond_to->async_send(
50-
ep, res, boost::asio::redirect_error(cobalt::use_op, ec));
51-
}
52-
}
53-
54-
inline cobalt::promise<void> work(request_channel& rc,
55-
int min_a,
56-
int max_a,
57-
int b) {
58-
response_channel res{co_await cobalt::this_coro::executor};
59-
for (int a = min_a; a <= max_a; a++) {
60-
// the following two calls offload the work to another thread.
61-
co_await rc.async_send(error_code{}, a, b, &res, cobalt::use_op);
62-
int c = co_await res.async_receive(
63-
cobalt::use_op); // may throw if working thread has an exception
64-
printf("The CPU intensive result of adding %d to %d, is %d\n", a, b, c);
65-
}
66-
}
67-
68-
cobalt::main co_main(int, char**) {
69-
// a very simple thread pool
70-
std::vector<cobalt::thread> thrs;
71-
const std::size_t n = 4u;
72-
73-
request_channel rc{co_await cobalt::this_coro::executor};
74-
for (auto i = 0u; i < n; i++) thrs.push_back(worker(rc));
75-
76-
try {
77-
// this is an over simplification, but emulated multiple pieces of
78-
// code in the single threaded environment offloading work to the thread.
79-
co_await cobalt::join(
80-
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
81-
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
82-
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
83-
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
84-
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
85-
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18));
86-
87-
} catch (std::exception& e) {
88-
printf("Completed with exception %s\n", e.what());
89-
}
90-
// closing the channel will cause the threads to complete
91-
rc.close();
92-
// wait them so they don't leak.
93-
co_await cobalt::join(thrs);
94-
co_return 0;
95-
}
1+
// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <boost/cobalt.hpp>
6+
7+
#include <boost/asio/as_tuple.hpp>
8+
#include <boost/asio/experimental/concurrent_channel.hpp>
9+
#include <boost/asio/redirect_error.hpp>
10+
#include <boost/asio/this_coro.hpp>
11+
12+
namespace cobalt = boost::cobalt;
13+
using boost::system::error_code;
14+
15+
template <typename Signature>
16+
using cchannel = boost::asio::experimental::concurrent_channel<Signature>;
17+
18+
// this is a function doing some CPU heavy work that should be offloaded onto a
19+
// thread
20+
inline cobalt::promise<int> cpu_intense_work(int a, int b) { co_return a + b; }
21+
22+
// this channel is used to send a response to completed work
23+
using response_channel = cchannel<void(std::exception_ptr, int)>;
24+
// this channel is used to send a request to a working thread
25+
using function_handler = void(error_code, int, int, response_channel* res);
26+
27+
using request_channel = cchannel<function_handler>;
28+
29+
// the worker wrapper
30+
inline cobalt::thread worker(request_channel& work) {
31+
while (work.is_open()) {
32+
auto [ec, a, b, respond_to] =
33+
co_await work.async_receive(boost::asio::as_tuple(cobalt::use_op));
34+
if (ec) // done, ignore. in our code this is only triggered by closing the
35+
// channel
36+
break;
37+
38+
// to emulate this being like awaiting on the same thread, we also deliver
39+
// an exception.
40+
std::exception_ptr ep;
41+
int res = 0;
42+
try {
43+
res = co_await cpu_intense_work(a, b);
44+
} catch (...) {
45+
// this way exception get sent to the awaiting coro as if it was a call.
46+
ep = std::current_exception();
47+
}
48+
// send the response. If the channel is closed, the program will terminate!
49+
co_await respond_to->async_send(
50+
ep, res, boost::asio::redirect_error(cobalt::use_op, ec));
51+
}
52+
}
53+
54+
inline cobalt::promise<void> work(request_channel& rc,
55+
int min_a,
56+
int max_a,
57+
int b) {
58+
response_channel res{co_await cobalt::this_coro::executor};
59+
for (int a = min_a; a <= max_a; a++) {
60+
// the following two calls offload the work to another thread.
61+
co_await rc.async_send(error_code{}, a, b, &res, cobalt::use_op);
62+
int c = co_await res.async_receive(
63+
cobalt::use_op); // may throw if working thread has an exception
64+
printf("The CPU intensive result of adding %d to %d, is %d\n", a, b, c);
65+
}
66+
}
67+
68+
cobalt::main co_main(int, char**) {
69+
// a very simple thread pool
70+
std::vector<cobalt::thread> thrs;
71+
const std::size_t n = 4u;
72+
73+
request_channel rc{co_await cobalt::this_coro::executor};
74+
for (auto i = 0u; i < n; i++) thrs.push_back(worker(rc));
75+
76+
try {
77+
// this is an over simplification, but emulated multiple pieces of
78+
// code in the single threaded environment offloading work to the thread.
79+
co_await cobalt::join(
80+
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
81+
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
82+
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
83+
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
84+
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18),
85+
work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18));
86+
87+
} catch (std::exception& e) {
88+
printf("Completed with exception %s\n", e.what());
89+
}
90+
// closing the channel will cause the threads to complete
91+
rc.close();
92+
// wait them so they don't leak.
93+
co_await cobalt::join(thrs);
94+
co_return 0;
95+
}

sources/swarm/library/sources/crypt/blowfish_cipher.h

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,72 @@
77

88
#include <openssl/blowfish.h>
99

10-
namespace swarm {} // namespace swarm
10+
#include <cassert>
11+
#include <cstddef>
12+
#include <cstdint>
13+
#include <span>
1114

12-
#endif // BLOWFISH_CIPHER_H
15+
#include <common_macro.h>
16+
17+
#if defined(_MSC_VER)
18+
#pragma warning(push)
19+
#pragma warning(disable : 4996)
20+
#elif defined(__clang__) && defined(__has_warning)
21+
#if __has_warning("-Wdeprecated-declarations")
22+
#pragma clang diagnostic push
23+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
24+
#endif
25+
#endif
26+
27+
namespace swarm {
28+
29+
using ConstBytesView = std::span<const uint8_t>;
30+
using BytesView = std::span<uint8_t>;
31+
32+
class BlowfishCipher {
33+
public:
34+
BlowfishCipher() = default;
35+
36+
explicit BlowfishCipher(BytesView key) noexcept {
37+
BF_set_key(&bf_key_, key.size(), key.data());
38+
}
39+
40+
F_INLINE auto Encrypt(ConstBytesView in, BytesView out) noexcept -> void {
41+
assert(out.size() >= in.size());
42+
assert(in.size() % 8 == 0);
43+
for (size_t i = 0; i < in.size(); i += 8) {
44+
BF_ecb_encrypt(&in[i], &out[i], &bf_key_, BF_ENCRYPT);
45+
}
46+
}
47+
48+
F_INLINE auto EncryptInplace(BytesView in_out) noexcept -> void {
49+
Encrypt(in_out, in_out);
50+
}
51+
52+
F_INLINE auto Decrypt(ConstBytesView in, BytesView out) noexcept -> void {
53+
assert(out.size() >= in.size());
54+
assert(in.size() % 8 == 0);
55+
for (size_t i = 0; i < in.size(); i += 8) {
56+
BF_ecb_encrypt(&in[i], &out[i], &bf_key_, BF_DECRYPT);
57+
}
58+
}
59+
60+
F_INLINE auto DecryptInplace(BytesView in_out) noexcept -> void {
61+
Decrypt(in_out, in_out);
62+
}
63+
64+
private:
65+
BF_KEY bf_key_{};
66+
};
67+
68+
} // namespace swarm
69+
70+
#if defined(_MSC_VER)
71+
#pragma warning(pop)
72+
#elif defined(__clang__) && defined(__has_warning)
73+
#if __has_warning("-Wdeprecated-declarations")
74+
#pragma clang diagnostic pop
75+
#endif
76+
#endif
77+
78+
#endif // BLOWFISH_CIPHER_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-FileCopyrightText: © 2023 Melg Eight <[email protected]>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <common_macro.h>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#ifndef COMMON_MACRO_H
6+
#define COMMON_MACRO_H
7+
8+
#ifndef F_INLINE
9+
#if defined(_MSC_VER)
10+
#define F_INLINE __forceinline
11+
#elif defined(__GNUC__) && __GNUC__ > 3
12+
#define F_INLINE inline __attribute__((__always_inline__))
13+
#else
14+
#define F_INLINE inline
15+
#endif
16+
#endif
17+
18+
#endif // COMMON_MACRO_H

sources/swarm/library/sources/json/placeholder_function.cpp renamed to sources/swarm/library/sources/utils/hex_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5-
#include <placeholder_function.h>
5+
#include <hex_view.h>
66

77
#include <array>
88
#include <cstdint>

sources/swarm/library/sources/json/placeholder_function.h renamed to sources/swarm/library/sources/utils/hex_view.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5-
#ifndef PLACEHOLDER_FUNCTION_H
6-
#define PLACEHOLDER_FUNCTION_H
5+
#ifndef HEX_VIEW_H
6+
#define HEX_VIEW_H
77

88
#include <span>
99
#include <string>
@@ -17,4 +17,4 @@ auto HexAsciiViewFrom(std::span<const std::byte> data) noexcept -> std::string;
1717

1818
} // namespace swarm
1919

20-
#endif // PLACEHOLDER_FUNCTION_H
20+
#endif // HEX_VIEW_H

0 commit comments

Comments
 (0)