mirror of
https://gitlab.com/niansa/libcrosscoro.git
synced 2025-03-06 20:53:32 +01:00
rename tcp_scheduler to tcp_server (#27)
This commit is contained in:
parent
6faafa0688
commit
290cb85aca
6 changed files with 25 additions and 25 deletions
|
@ -22,7 +22,7 @@ set(LIBCORO_SOURCE_FILES
|
||||||
inc/coro/net/ip_address.hpp src/net/ip_address.cpp
|
inc/coro/net/ip_address.hpp src/net/ip_address.cpp
|
||||||
inc/coro/net/socket.hpp
|
inc/coro/net/socket.hpp
|
||||||
inc/coro/net/tcp_client.hpp src/net/tcp_client.cpp
|
inc/coro/net/tcp_client.hpp src/net/tcp_client.cpp
|
||||||
inc/coro/net/tcp_scheduler.hpp src/net/tcp_scheduler.cpp
|
inc/coro/net/tcp_server.hpp src/net/tcp_server.cpp
|
||||||
|
|
||||||
inc/coro/awaitable.hpp
|
inc/coro/awaitable.hpp
|
||||||
inc/coro/coro.hpp
|
inc/coro/coro.hpp
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#include "coro/net/ip_address.hpp"
|
#include "coro/net/ip_address.hpp"
|
||||||
#include "coro/net/socket.hpp"
|
#include "coro/net/socket.hpp"
|
||||||
#include "coro/net/tcp_client.hpp"
|
#include "coro/net/tcp_client.hpp"
|
||||||
#include "coro/net/tcp_scheduler.hpp"
|
#include "coro/net/tcp_server.hpp"
|
||||||
|
|
||||||
#include "coro/awaitable.hpp"
|
#include "coro/awaitable.hpp"
|
||||||
#include "coro/event.hpp"
|
#include "coro/event.hpp"
|
||||||
|
|
|
@ -11,10 +11,10 @@
|
||||||
|
|
||||||
namespace coro::net
|
namespace coro::net
|
||||||
{
|
{
|
||||||
class tcp_scheduler : public io_scheduler
|
class tcp_server : public io_scheduler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using on_connection_t = std::function<task<void>(tcp_scheduler&, net::socket)>;
|
using on_connection_t = std::function<task<void>(tcp_server&, net::socket)>;
|
||||||
|
|
||||||
struct options
|
struct options
|
||||||
{
|
{
|
||||||
|
@ -25,21 +25,21 @@ public:
|
||||||
io_scheduler::options io_options{};
|
io_scheduler::options io_options{};
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit tcp_scheduler(
|
explicit tcp_server(
|
||||||
options opts =
|
options opts =
|
||||||
options{
|
options{
|
||||||
net::ip_address::from_string("0.0.0.0"),
|
net::ip_address::from_string("0.0.0.0"),
|
||||||
8080,
|
8080,
|
||||||
128,
|
128,
|
||||||
[](tcp_scheduler&, net::socket) -> task<void> { co_return; },
|
[](tcp_server&, net::socket) -> task<void> { co_return; },
|
||||||
io_scheduler::options{9, 2, io_scheduler::thread_strategy_t::spawn}});
|
io_scheduler::options{9, 2, io_scheduler::thread_strategy_t::spawn}});
|
||||||
|
|
||||||
tcp_scheduler(const tcp_scheduler&) = delete;
|
tcp_server(const tcp_server&) = delete;
|
||||||
tcp_scheduler(tcp_scheduler&&) = delete;
|
tcp_server(tcp_server&&) = delete;
|
||||||
auto operator=(const tcp_scheduler&) -> tcp_scheduler& = delete;
|
auto operator=(const tcp_server&) -> tcp_server& = delete;
|
||||||
auto operator=(tcp_scheduler&&) -> tcp_scheduler& = delete;
|
auto operator=(tcp_server&&) -> tcp_server& = delete;
|
||||||
|
|
||||||
~tcp_scheduler() override;
|
~tcp_server() override;
|
||||||
|
|
||||||
auto empty() const -> bool { return size() == 0; }
|
auto empty() const -> bool { return size() == 0; }
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#include "coro/net/tcp_scheduler.hpp"
|
#include "coro/net/tcp_server.hpp"
|
||||||
|
|
||||||
namespace coro::net
|
namespace coro::net
|
||||||
{
|
{
|
||||||
|
|
||||||
tcp_scheduler::tcp_scheduler(options opts)
|
tcp_server::tcp_server(options opts)
|
||||||
: io_scheduler(std::move(opts.io_options)),
|
: io_scheduler(std::move(opts.io_options)),
|
||||||
m_opts(std::move(opts)),
|
m_opts(std::move(opts)),
|
||||||
m_accept_socket(net::socket::make_accept_socket(
|
m_accept_socket(net::socket::make_accept_socket(
|
||||||
|
@ -20,12 +20,12 @@ tcp_scheduler::tcp_scheduler(options opts)
|
||||||
schedule(make_accept_task());
|
schedule(make_accept_task());
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_scheduler::~tcp_scheduler()
|
tcp_server::~tcp_server()
|
||||||
{
|
{
|
||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto tcp_scheduler::shutdown(shutdown_t wait_for_tasks) -> void
|
auto tcp_server::shutdown(shutdown_t wait_for_tasks) -> void
|
||||||
{
|
{
|
||||||
if (m_accept_new_connections.exchange(false, std::memory_order::release))
|
if (m_accept_new_connections.exchange(false, std::memory_order::release))
|
||||||
{
|
{
|
||||||
|
@ -40,7 +40,7 @@ auto tcp_scheduler::shutdown(shutdown_t wait_for_tasks) -> void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto tcp_scheduler::make_accept_task() -> coro::task<void>
|
auto tcp_server::make_accept_task() -> coro::task<void>
|
||||||
{
|
{
|
||||||
sockaddr_in client{};
|
sockaddr_in client{};
|
||||||
constexpr const int len = sizeof(struct sockaddr_in);
|
constexpr const int len = sizeof(struct sockaddr_in);
|
|
@ -4,7 +4,7 @@ project(libcoro_test)
|
||||||
set(LIBCORO_TEST_SOURCE_FILES
|
set(LIBCORO_TEST_SOURCE_FILES
|
||||||
net/test_dns_client.cpp
|
net/test_dns_client.cpp
|
||||||
net/test_ip_address.cpp
|
net/test_ip_address.cpp
|
||||||
net/test_tcp_scheduler.cpp
|
net/test_tcp_server.cpp
|
||||||
|
|
||||||
bench.cpp
|
bench.cpp
|
||||||
test_event.cpp
|
test_event.cpp
|
||||||
|
|
|
@ -2,17 +2,17 @@
|
||||||
|
|
||||||
#include <coro/coro.hpp>
|
#include <coro/coro.hpp>
|
||||||
|
|
||||||
TEST_CASE("tcp_scheduler no on connection throws")
|
TEST_CASE("tcp_server no on connection throws")
|
||||||
{
|
{
|
||||||
REQUIRE_THROWS(coro::net::tcp_scheduler{coro::net::tcp_scheduler::options{.on_connection = nullptr}});
|
REQUIRE_THROWS(coro::net::tcp_server{coro::net::tcp_server::options{.on_connection = nullptr}});
|
||||||
}
|
}
|
||||||
|
|
||||||
static auto tcp_scheduler_echo_server(
|
static auto tcp_server_echo(
|
||||||
const std::variant<coro::net::hostname, coro::net::ip_address> address,
|
const std::variant<coro::net::hostname, coro::net::ip_address> address,
|
||||||
const std::string msg
|
const std::string msg
|
||||||
) -> void
|
) -> void
|
||||||
{
|
{
|
||||||
auto on_connection = [&msg](coro::net::tcp_scheduler& scheduler, coro::net::socket sock) -> coro::task<void> {
|
auto on_connection = [&msg](coro::net::tcp_server& scheduler, coro::net::socket sock) -> coro::task<void> {
|
||||||
std::string in(64, '\0');
|
std::string in(64, '\0');
|
||||||
|
|
||||||
auto [rstatus, rbytes] = co_await scheduler.read(sock, std::span<char>{in.data(), in.size()});
|
auto [rstatus, rbytes] = co_await scheduler.read(sock, std::span<char>{in.data(), in.size()});
|
||||||
|
@ -28,7 +28,7 @@ static auto tcp_scheduler_echo_server(
|
||||||
co_return;
|
co_return;
|
||||||
};
|
};
|
||||||
|
|
||||||
coro::net::tcp_scheduler scheduler{coro::net::tcp_scheduler::options{
|
coro::net::tcp_server scheduler{coro::net::tcp_server::options{
|
||||||
.address = coro::net::ip_address::from_string("0.0.0.0"),
|
.address = coro::net::ip_address::from_string("0.0.0.0"),
|
||||||
.port = 8080,
|
.port = 8080,
|
||||||
.backlog = 128,
|
.backlog = 128,
|
||||||
|
@ -82,10 +82,10 @@ static auto tcp_scheduler_echo_server(
|
||||||
REQUIRE(scheduler.empty());
|
REQUIRE(scheduler.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("tcp_scheduler echo server")
|
TEST_CASE("tcp_server echo server")
|
||||||
{
|
{
|
||||||
const std::string msg{"Hello from client"};
|
const std::string msg{"Hello from client"};
|
||||||
|
|
||||||
tcp_scheduler_echo_server(coro::net::ip_address::from_string("127.0.0.1"), msg);
|
tcp_server_echo(coro::net::ip_address::from_string("127.0.0.1"), msg);
|
||||||
tcp_scheduler_echo_server(coro::net::hostname{"localhost"}, msg);
|
tcp_server_echo(coro::net::hostname{"localhost"}, msg);
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue