diff --git a/Core/Serial/Serial.h b/Core/Serial/Serial.h index cd0e4a0..69dccfc 100644 --- a/Core/Serial/Serial.h +++ b/Core/Serial/Serial.h @@ -1,5 +1,4 @@ -#ifndef yC_SERIAL_H -#define yC_SERIAL_H +#pragma once #include #include #include @@ -157,7 +156,4 @@ namespace Psc::serial { int get_buffer_byte_size(); }; -} -#endif - - +} \ No newline at end of file diff --git a/Core/Serial/Serial_p.h b/Core/Serial/Serial_p.h index 2815623..83157dc 100644 --- a/Core/Serial/Serial_p.h +++ b/Core/Serial/Serial_p.h @@ -1,5 +1,5 @@ -#ifndef yC_SERIAL_P_H -#define yC_SERIAL_P_H +#pragma once + #include "../../Core/spdlog/export.h" #include "Serial.h" @@ -11,5 +11,3 @@ void serial_log(Log_Type& log_type, const std::string& MSG); serial_log(log_type, MSG); - -#endif diff --git a/Core/socket/Socket_Coro.h b/Core/socket/Socket_Coro.h index 7d9efde..e68c504 100644 --- a/Core/socket/Socket_Coro.h +++ b/Core/socket/Socket_Coro.h @@ -1,6 +1,10 @@ #pragma once #include "ASIO_Utils.h" +#include "TCP_Client.h" +#include "TCP_Server.h" +#include "UDP_Client.h" +#include "UDP_Server.h" #include @@ -13,6 +17,135 @@ #include #include +namespace Psc::asio_socket { + +class TCP_Client_Coro : public TCP_Client { +public: + [[nodiscard]] ucoro::awaitable connect_coro() + { + auto started = start_connect(); + tick(); + co_return started; + } + + [[nodiscard]] ucoro::awaitable tick_coro() + { + tick(); + co_return; + } + + [[nodiscard]] ucoro::awaitable read_coro() + { + co_return read(); + } + + [[nodiscard]] ucoro::awaitable send_coro(std::string data) + { + send(data); + co_return; + } +}; + +class TCP_Server_Coro : public TCP_Server { +public: + [[nodiscard]] ucoro::awaitable listen_coro(std::string ip, std::uint32_t port) + { + co_return listen(std::move(ip), port); + } + + [[nodiscard]] ucoro::awaitable listen_coro(Sockaddr_In address) + { + co_return listen(address); + } + + [[nodiscard]] ucoro::awaitable tick_coro() + { + tick(); + co_return; + } + + [[nodiscard]] ucoro::awaitable flush_clients_coro() + { + flush_clients(); + co_return; + } + + [[nodiscard]] ucoro::awaitable write_to_all_clients_coro(std::string data) + { + write_to_all_clients(data); + co_return; + } + + [[nodiscard]] ucoro::awaitable> read_from_all_clients_coro() + { + co_return read_from_all_clients(); + } + + [[nodiscard]] ucoro::awaitable> accept_coro() const + { + co_return accept(); + } +}; + +class UDP_Client_Coro : public UDP_Client { +public: + [[nodiscard]] ucoro::awaitable connect_coro() + { + co_return connect(); + } + + [[nodiscard]] ucoro::awaitable read_coro() + { + co_return read(); + } + + [[nodiscard]] ucoro::awaitable tick_coro() + { + tick(); + co_return; + } + + [[nodiscard]] ucoro::awaitable send_coro(std::string data) + { + send(data); + co_return; + } +}; + +class UDP_Server_Coro : public UDP_Server { +public: + [[nodiscard]] ucoro::awaitable tick_coro() + { + tick(); + co_return; + } + + [[nodiscard]] ucoro::awaitable> read_coro() + { + co_return read(); + } + + [[nodiscard]] ucoro::awaitable reply_last_peer_coro(std::string data) + { + reply_last_peer(data); + co_return; + } + + [[nodiscard]] ucoro::awaitable send_to_coro(std::string ip, uint16_t port, std::string data) + { + send_to(ip, port, data); + co_return; + } + + [[nodiscard]] ucoro::awaitable write_to_all_clients_coro(std::string msg) + { + write_to_all_clients(msg); + co_return; + } +}; + +} // namespace Psc::asio_socket + namespace Psc::asio_socket::coro { struct TCP_Read_Result { diff --git a/Core/socket/TCP_Client.cpp b/Core/socket/TCP_Client.cpp index 62a48ab..3e370ce 100644 --- a/Core/socket/TCP_Client.cpp +++ b/Core/socket/TCP_Client.cpp @@ -115,17 +115,6 @@ void TCP_Client::tick() { } } -ucoro::awaitable TCP_Client::connect_coro() { - auto started = start_connect(); - tick(); - co_return started; -} - -ucoro::awaitable TCP_Client::tick_coro() { - tick(); - co_return; -} - void TCP_Client::start_read() { if (read_pending || state != Connected || !socket || !socket->is_open()) return; @@ -180,21 +169,12 @@ std::string TCP_Client::read() { return ret; } -ucoro::awaitable TCP_Client::read_coro() { - co_return read(); -} - void TCP_Client::send(const std::string& data) { if (data.empty()) return; send_buffer.write_best_effort(data.data(), data.size()); if (state == Connected) start_write(); } -ucoro::awaitable TCP_Client::send_coro(std::string data) { - send(data); - co_return; -} - std::string TCP_Client::to_string() { return "TCP_Client:[" + dest_address.to_string() + "]"; } diff --git a/Core/socket/TCP_Client.h b/Core/socket/TCP_Client.h index 49bdc24..c8eb65a 100644 --- a/Core/socket/TCP_Client.h +++ b/Core/socket/TCP_Client.h @@ -1,7 +1,6 @@ #pragma once #include "ASIO_Utils.h" -#include #include #include @@ -22,10 +21,6 @@ public: void tick(); std::string read(); void send(const std::string& data); - [[nodiscard]] ucoro::awaitable connect_coro(); - [[nodiscard]] ucoro::awaitable tick_coro(); - [[nodiscard]] ucoro::awaitable read_coro(); - [[nodiscard]] ucoro::awaitable send_coro(std::string data); std::string to_string() override; enum State { diff --git a/Core/socket/TCP_Server.cpp b/Core/socket/TCP_Server.cpp index 48d4df1..558d0d3 100644 --- a/Core/socket/TCP_Server.cpp +++ b/Core/socket/TCP_Server.cpp @@ -1,7 +1,6 @@ #include "TCP_Server.h" #include -#include namespace Psc::asio_socket { @@ -24,11 +23,6 @@ void TCP_Server::tick() { } } -ucoro::awaitable TCP_Server::tick_coro() { - tick(); - co_return; -} - TCP_Server & TCP_Server::set_tcp_no_delay(bool value) { no_delay = value; return *this; @@ -60,14 +54,6 @@ bool TCP_Server::listen(const std::string& ip, std::uint32_t port) { return listen(Sockaddr_In(ip, port)); } -ucoro::awaitable TCP_Server::listen_coro(std::string ip, std::uint32_t port) { - co_return listen(std::move(ip), port); -} - -ucoro::awaitable TCP_Server::listen_coro(Sockaddr_In addr) { - co_return listen(addr); -} - bool TCP_Server::listen(const Sockaddr_In& addr) { if (!acceptor) create(); if (addr.ip.empty() || addr.port == 0) { @@ -135,10 +121,6 @@ std::shared_ptr TCP_Server::accept() const { return nullptr; } -ucoro::awaitable> TCP_Server::accept_coro() const { - co_return accept(); -} - void TCP_Server::start_accept() { if (accept_pending || !acceptor || state != Working || !acceptor->is_open()) return; @@ -258,11 +240,6 @@ void TCP_Server::flush_clients() { } } -ucoro::awaitable TCP_Server::flush_clients_coro() { - flush_clients(); - co_return; -} - void TCP_Server::write_to_all_clients(const std::string& data) { if (data.empty()) return; for (auto& [_, conn] : tcp_clients) { @@ -273,11 +250,6 @@ void TCP_Server::write_to_all_clients(const std::string& data) { } } -ucoro::awaitable TCP_Server::write_to_all_clients_coro(std::string data) { - write_to_all_clients(data); - co_return; -} - std::vector TCP_Server::read_from_all_clients() { std::vector ret; std::array buffer{}; @@ -298,10 +270,6 @@ std::vector TCP_Server::read_from_all_clients() { return ret; } -ucoro::awaitable> TCP_Server::read_from_all_clients_coro() { - co_return read_from_all_clients(); -} - std::vector TCP_Server::client_fds() { std::vector ret; for (const auto& [fd, conn] : tcp_clients) { diff --git a/Core/socket/TCP_Server.h b/Core/socket/TCP_Server.h index d8464be..319460e 100644 --- a/Core/socket/TCP_Server.h +++ b/Core/socket/TCP_Server.h @@ -2,7 +2,6 @@ #include "ASIO_Utils.h" #include "Core/Statistics/Statistics.h" -#include #include #include @@ -43,13 +42,8 @@ public: TCP_Server& set_recv_system_buffer_size(size_t size); bool listen(const std::string& ip, std::uint32_t port); bool listen(const Sockaddr_In& address); - [[nodiscard]] ucoro::awaitable listen_coro(std::string ip, std::uint32_t port); - [[nodiscard]] ucoro::awaitable listen_coro(Sockaddr_In address); std::string to_string() override; void write_to_all_clients(const std::string& data); - [[nodiscard]] ucoro::awaitable tick_coro(); - [[nodiscard]] ucoro::awaitable flush_clients_coro(); - [[nodiscard]] ucoro::awaitable write_to_all_clients_coro(std::string data); struct Read_Info { Accept_Info info; @@ -67,12 +61,10 @@ public: State state = Not_Created; std::vector read_from_all_clients(); - [[nodiscard]] ucoro::awaitable> read_from_all_clients_coro(); std::vector client_fds(); std::vector> get_all_clients(); void flush_clients(); [[nodiscard]] std::shared_ptr accept() const; - [[nodiscard]] ucoro::awaitable> accept_coro() const; protected: mutable asio::io_context io_context; diff --git a/Core/socket/UDP_Client.cpp b/Core/socket/UDP_Client.cpp index cbcb7cd..da5211e 100644 --- a/Core/socket/UDP_Client.cpp +++ b/Core/socket/UDP_Client.cpp @@ -53,10 +53,6 @@ bool UDP_Client::connect() { return true; } -ucoro::awaitable UDP_Client::connect_coro() { - co_return connect(); -} - std::string UDP_Client::read() { std::string ret(static_cast(read_chunk_size), '\0'); std::size_t out_len = ret.size(); @@ -65,10 +61,6 @@ std::string UDP_Client::read() { return ret; } -ucoro::awaitable UDP_Client::read_coro() { - co_return read(); -} - void UDP_Client::tick() { if (!connected && !connect_pending && !dest_address.ip.empty() && dest_address.port != 0) { connect(); @@ -87,11 +79,6 @@ void UDP_Client::tick() { } } -ucoro::awaitable UDP_Client::tick_coro() { - tick(); - co_return; -} - void UDP_Client::send(const std::string& data) { if (data.empty()) return; send_buffer.write(data.data(), data.size()); @@ -99,11 +86,6 @@ void UDP_Client::send(const std::string& data) { if (connected) start_write(); } -ucoro::awaitable UDP_Client::send_coro(std::string data) { - send(data); - co_return; -} - void UDP_Client::start_read() { if (read_pending || !connected || !socket || !socket->is_open()) return; if (recv_storage.empty()) recv_storage.assign(static_cast(read_chunk_size), 0); diff --git a/Core/socket/UDP_Client.h b/Core/socket/UDP_Client.h index 2e50c6c..089afb7 100644 --- a/Core/socket/UDP_Client.h +++ b/Core/socket/UDP_Client.h @@ -1,7 +1,6 @@ #pragma once #include "ASIO_Utils.h" -#include #include #include @@ -18,10 +17,6 @@ public: std::string read(); void tick(); void send(const std::string& data); - [[nodiscard]] ucoro::awaitable connect_coro(); - [[nodiscard]] ucoro::awaitable read_coro(); - [[nodiscard]] ucoro::awaitable tick_coro(); - [[nodiscard]] ucoro::awaitable send_coro(std::string data); void close(); std::string to_string(); diff --git a/Core/socket/UDP_Server.cpp b/Core/socket/UDP_Server.cpp index 1aabe5b..13906cd 100644 --- a/Core/socket/UDP_Server.cpp +++ b/Core/socket/UDP_Server.cpp @@ -58,11 +58,6 @@ void UDP_Server::tick() { start_write(); } -ucoro::awaitable UDP_Server::tick_coro() { - tick(); - co_return; -} - void UDP_Server::start_read() { if (read_pending || !bound || !socket || !socket->is_open()) return; if (recv_storage.empty()) recv_storage.assign(static_cast(read_chunk_size), 0); @@ -143,20 +138,11 @@ std::vector UDP_Server::read() { return ret; } -ucoro::awaitable> UDP_Server::read_coro() { - co_return read(); -} - void UDP_Server::reply_last_peer(const std::string& data) { if (!has_last_peer) return; send_to(last_peer.ip, static_cast(last_peer.port), data); } -ucoro::awaitable UDP_Server::reply_last_peer_coro(std::string data) { - reply_last_peer(data); - co_return; -} - void UDP_Server::send_to(const std::string& ip, uint16_t port, const std::string& data) { if (data.empty()) return; if (!bound && !bind()) return; @@ -170,20 +156,10 @@ void UDP_Server::send_to(const std::string& ip, uint16_t port, const std::string start_write(); } -ucoro::awaitable UDP_Server::send_to_coro(std::string ip, uint16_t port, std::string data) { - send_to(ip, port, data); - co_return; -} - void UDP_Server::write_to_all_clients(const std::string& msg) { for (const auto& client : clients) { send_to(client.ip, static_cast(client.port), msg); } } -ucoro::awaitable UDP_Server::write_to_all_clients_coro(std::string msg) { - write_to_all_clients(msg); - co_return; -} - } // namespace Psc::asio_socket diff --git a/Core/socket/UDP_Server.h b/Core/socket/UDP_Server.h index d9e523b..4a66c9d 100644 --- a/Core/socket/UDP_Server.h +++ b/Core/socket/UDP_Server.h @@ -1,7 +1,6 @@ #pragma once #include "ASIO_Utils.h" -#include #include #include @@ -22,10 +21,6 @@ public: void reply_last_peer(const std::string& data); void send_to(const std::string& ip, uint16_t port, const std::string& data); void write_to_all_clients(const std::string& msg); - [[nodiscard]] ucoro::awaitable tick_coro(); - [[nodiscard]] ucoro::awaitable reply_last_peer_coro(std::string data); - [[nodiscard]] ucoro::awaitable send_to_coro(std::string ip, uint16_t port, std::string data); - [[nodiscard]] ucoro::awaitable write_to_all_clients_coro(std::string msg); struct Read_Info { Sockaddr_In peer; @@ -33,7 +28,6 @@ public: }; std::vector read(); - [[nodiscard]] ucoro::awaitable> read_coro(); enum State { Not_Created, diff --git a/Core/socket/export.h b/Core/socket/export.h index b7cc4fb..b7787aa 100644 --- a/Core/socket/export.h +++ b/Core/socket/export.h @@ -5,4 +5,4 @@ #include "TCP_Server.h" #include "UDP_Client.h" #include "UDP_Server.h" - +#include "Socket_Coro.h"