Skip to content

Commit

Permalink
Made ID common in socket_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zitrax committed Jan 1, 2024
1 parent a6a43bc commit 237d4a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
19 changes: 7 additions & 12 deletions src/socket_test/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "common.hpp" // NOLINT(misc-include-cleaner)
#include "logger.hpp"

class Connection {
class Connection : public socket_test::ID {
public:
explicit Connection(asio::io_context& io_context)
: m_resolver(io_context), m_socket(io_context) {}
Expand All @@ -30,33 +30,28 @@ class Connection {
auto results = co_await m_resolver.async_resolve("127.0.0.1", "8080",
asio::use_awaitable);

zit::logger()->debug("[{}] resolved {}", m_id, results.begin()->endpoint());
zit::logger()->debug("[{}] resolved {}", id(), results.begin()->endpoint());

auto result = *results.begin();
co_await m_socket.async_connect(result, asio::use_awaitable);

zit::logger()->info("[{}] {} connected to server {}", m_id,
zit::logger()->info("[{}] {} connected to server {}", id(),
m_socket.local_endpoint(), result.endpoint());

co_await m_socket.async_write_some(
asio::buffer(fmt::format("Hello {}\n", m_id)), asio::use_awaitable);
asio::buffer(fmt::format("Hello {}\n", id())), asio::use_awaitable);

zit::logger()->info("[{}] Sent hello from {}", m_id,
zit::logger()->info("[{}] Sent hello from {}", id(),
m_socket.local_endpoint());
}

private:
// This is supposedly fixed in newer versions of clang-tidy (but not yet in
// the one I use) See https://github.com/llvm/llvm-project/issues/47384
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static unsigned m_counter;
asio::ip::tcp::resolver m_resolver;
asio::ip::tcp::socket m_socket;
unsigned m_id{m_counter++};
};

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
unsigned Connection::m_counter = 0;
unsigned socket_test::ID::m_counter = 0;

int main() {
try {
Expand All @@ -73,7 +68,7 @@ int main() {
zit::logger()->debug("Creating connection {}", i);
connections.emplace_back(std::make_unique<Connection>(io_context));
zit::logger()->debug("Spawning connection {}", i);
co_spawn(io_context, connections.back()->connect(), rethrow);
co_spawn(io_context, connections.back()->connect(), socket_test::rethrow);
zit::logger()->debug("Spawned connection {}", i);
}
io_context.run();
Expand Down
16 changes: 16 additions & 0 deletions src/socket_test/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct fmt::formatter<asio::ip::tcp::endpoint> {

// NOLINTEND(readability-convert-member-functions-to-static)

namespace socket_test {

/**
* Completion handler rethrowing instead of ignoring exceptions
*/
Expand All @@ -25,3 +27,17 @@ const auto rethrow = [](const std::exception_ptr& e) {
std::rethrow_exception(e);
}
};

class ID {
public:
[[nodiscard]] auto id() const { return m_id; }

private:
// This is supposedly fixed in newer versions of clang-tidy (but not yet in
// the one I use) See https://github.com/llvm/llvm-project/issues/47384
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static unsigned m_counter;
unsigned m_id{m_counter++};
};

} // namespace socket_test
20 changes: 7 additions & 13 deletions src/socket_test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
#include <asio/use_awaitable.hpp>
#include <csignal>
#include <exception>
#include <istream>
#include <memory>
#include <string>
#include <vector>
#include "common.hpp" // NOLINT(misc-include-cleaner)
#include "logger.hpp"

#include <iostream>

class Connection {
class Connection : public socket_test::ID {
public:
explicit Connection(asio::io_context& io_context,
asio::ip::tcp::acceptor& acceptor)
Expand All @@ -30,14 +29,14 @@ class Connection {
asio::awaitable<void> listen() {
m_acceptor.listen();
while (true) {
zit::logger()->info("[{}] Listening for incoming connections on {}", m_id,
zit::logger()->info("[{}] Listening for incoming connections on {}", id(),
m_acceptor.local_endpoint());

// Await incoming connection
co_await m_acceptor.async_accept(m_socket, asio::use_awaitable);

// Handle incoming connection
zit::logger()->info("[{}] Accepted connection from {}", m_id,
zit::logger()->info("[{}] Accepted connection from {}", id(),
m_socket.remote_endpoint());

// Read incoming data
Expand All @@ -52,24 +51,19 @@ class Connection {

// Print message

zit::logger()->info("[{}] Received message: '{}' from {}", m_id, message,
zit::logger()->info("[{}] Received message: '{}' from {}", id(), message,
m_socket.remote_endpoint());
m_socket.close();
}
}

private:
// This is supposedly fixed in newer versions of clang-tidy (but not yet in
// the one I use) See https://github.com/llvm/llvm-project/issues/47384
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static unsigned m_counter;
asio::ip::tcp::acceptor& m_acceptor;
asio::ip::tcp::socket m_socket;
unsigned m_id{m_counter++};
};

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
unsigned Connection::m_counter = 0;
unsigned socket_test::ID::m_counter = 0;

int main() {
try {
Expand All @@ -89,7 +83,7 @@ int main() {
for (int i = 0; i < 2; i++) {
connections.emplace_back(
std::make_unique<Connection>(io_context, acceptor));
co_spawn(io_context, connections.back()->listen(), rethrow);
co_spawn(io_context, connections.back()->listen(), socket_test::rethrow);
}
io_context.run();
zit::logger()->info("Shutting down server");
Expand Down

0 comments on commit 237d4a4

Please sign in to comment.