tcp udp 改协程成功
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#ifndef yC_SERIAL_H
|
||||
#define yC_SERIAL_H
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
@@ -157,7 +156,4 @@ namespace Psc::serial {
|
||||
int get_buffer_byte_size();
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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 <ucoro/awaitable.hpp>
|
||||
|
||||
@@ -13,6 +17,135 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Psc::asio_socket {
|
||||
|
||||
class TCP_Client_Coro : public TCP_Client {
|
||||
public:
|
||||
[[nodiscard]] ucoro::awaitable<bool> connect_coro()
|
||||
{
|
||||
auto started = start_connect();
|
||||
tick();
|
||||
co_return started;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro()
|
||||
{
|
||||
tick();
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<std::string> read_coro()
|
||||
{
|
||||
co_return read();
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> send_coro(std::string data)
|
||||
{
|
||||
send(data);
|
||||
co_return;
|
||||
}
|
||||
};
|
||||
|
||||
class TCP_Server_Coro : public TCP_Server {
|
||||
public:
|
||||
[[nodiscard]] ucoro::awaitable<bool> listen_coro(std::string ip, std::uint32_t port)
|
||||
{
|
||||
co_return listen(std::move(ip), port);
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<bool> listen_coro(Sockaddr_In address)
|
||||
{
|
||||
co_return listen(address);
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro()
|
||||
{
|
||||
tick();
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> flush_clients_coro()
|
||||
{
|
||||
flush_clients();
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> write_to_all_clients_coro(std::string data)
|
||||
{
|
||||
write_to_all_clients(data);
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<std::vector<Read_Info>> read_from_all_clients_coro()
|
||||
{
|
||||
co_return read_from_all_clients();
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<std::shared_ptr<TCP_Connect>> accept_coro() const
|
||||
{
|
||||
co_return accept();
|
||||
}
|
||||
};
|
||||
|
||||
class UDP_Client_Coro : public UDP_Client {
|
||||
public:
|
||||
[[nodiscard]] ucoro::awaitable<bool> connect_coro()
|
||||
{
|
||||
co_return connect();
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<std::string> read_coro()
|
||||
{
|
||||
co_return read();
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro()
|
||||
{
|
||||
tick();
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> send_coro(std::string data)
|
||||
{
|
||||
send(data);
|
||||
co_return;
|
||||
}
|
||||
};
|
||||
|
||||
class UDP_Server_Coro : public UDP_Server {
|
||||
public:
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro()
|
||||
{
|
||||
tick();
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<std::vector<Read_Info>> read_coro()
|
||||
{
|
||||
co_return read();
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> reply_last_peer_coro(std::string data)
|
||||
{
|
||||
reply_last_peer(data);
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> send_to_coro(std::string ip, uint16_t port, std::string data)
|
||||
{
|
||||
send_to(ip, port, data);
|
||||
co_return;
|
||||
}
|
||||
|
||||
[[nodiscard]] ucoro::awaitable<void> 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 {
|
||||
|
||||
@@ -115,17 +115,6 @@ void TCP_Client::tick() {
|
||||
}
|
||||
}
|
||||
|
||||
ucoro::awaitable<bool> TCP_Client::connect_coro() {
|
||||
auto started = start_connect();
|
||||
tick();
|
||||
co_return started;
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> 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<std::string> 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<void> TCP_Client::send_coro(std::string data) {
|
||||
send(data);
|
||||
co_return;
|
||||
}
|
||||
|
||||
std::string TCP_Client::to_string() {
|
||||
return "TCP_Client:[" + dest_address.to_string() + "]";
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "ASIO_Utils.h"
|
||||
#include <ucoro/awaitable.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <array>
|
||||
@@ -22,10 +21,6 @@ public:
|
||||
void tick();
|
||||
std::string read();
|
||||
void send(const std::string& data);
|
||||
[[nodiscard]] ucoro::awaitable<bool> connect_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro();
|
||||
[[nodiscard]] ucoro::awaitable<std::string> read_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> send_coro(std::string data);
|
||||
std::string to_string() override;
|
||||
|
||||
enum State {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "TCP_Server.h"
|
||||
|
||||
#include <array>
|
||||
#include <utility>
|
||||
|
||||
namespace Psc::asio_socket {
|
||||
|
||||
@@ -24,11 +23,6 @@ void TCP_Server::tick() {
|
||||
}
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> 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<bool> TCP_Server::listen_coro(std::string ip, std::uint32_t port) {
|
||||
co_return listen(std::move(ip), port);
|
||||
}
|
||||
|
||||
ucoro::awaitable<bool> 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_Connect> TCP_Server::accept() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ucoro::awaitable<std::shared_ptr<TCP_Connect>> 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<void> 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<void> TCP_Server::write_to_all_clients_coro(std::string data) {
|
||||
write_to_all_clients(data);
|
||||
co_return;
|
||||
}
|
||||
|
||||
std::vector<TCP_Server::Read_Info> TCP_Server::read_from_all_clients() {
|
||||
std::vector<Read_Info> ret;
|
||||
std::array<char, 16 * 1024> buffer{};
|
||||
@@ -298,10 +270,6 @@ std::vector<TCP_Server::Read_Info> TCP_Server::read_from_all_clients() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ucoro::awaitable<std::vector<TCP_Server::Read_Info>> TCP_Server::read_from_all_clients_coro() {
|
||||
co_return read_from_all_clients();
|
||||
}
|
||||
|
||||
std::vector<Socket_FD> TCP_Server::client_fds() {
|
||||
std::vector<Socket_FD> ret;
|
||||
for (const auto& [fd, conn] : tcp_clients) {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "ASIO_Utils.h"
|
||||
#include "Core/Statistics/Statistics.h"
|
||||
#include <ucoro/awaitable.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
@@ -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<bool> listen_coro(std::string ip, std::uint32_t port);
|
||||
[[nodiscard]] ucoro::awaitable<bool> listen_coro(Sockaddr_In address);
|
||||
std::string to_string() override;
|
||||
void write_to_all_clients(const std::string& data);
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> flush_clients_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> 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_Info> read_from_all_clients();
|
||||
[[nodiscard]] ucoro::awaitable<std::vector<Read_Info>> read_from_all_clients_coro();
|
||||
std::vector<Socket_FD> client_fds();
|
||||
std::vector<std::shared_ptr<TCP_Connect>> get_all_clients();
|
||||
void flush_clients();
|
||||
[[nodiscard]] std::shared_ptr<TCP_Connect> accept() const;
|
||||
[[nodiscard]] ucoro::awaitable<std::shared_ptr<TCP_Connect>> accept_coro() const;
|
||||
|
||||
protected:
|
||||
mutable asio::io_context io_context;
|
||||
|
||||
@@ -53,10 +53,6 @@ bool UDP_Client::connect() {
|
||||
return true;
|
||||
}
|
||||
|
||||
ucoro::awaitable<bool> UDP_Client::connect_coro() {
|
||||
co_return connect();
|
||||
}
|
||||
|
||||
std::string UDP_Client::read() {
|
||||
std::string ret(static_cast<size_t>(read_chunk_size), '\0');
|
||||
std::size_t out_len = ret.size();
|
||||
@@ -65,10 +61,6 @@ std::string UDP_Client::read() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ucoro::awaitable<std::string> 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<void> 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<void> 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<size_t>(read_chunk_size), 0);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "ASIO_Utils.h"
|
||||
#include <ucoro/awaitable.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
@@ -18,10 +17,6 @@ public:
|
||||
std::string read();
|
||||
void tick();
|
||||
void send(const std::string& data);
|
||||
[[nodiscard]] ucoro::awaitable<bool> connect_coro();
|
||||
[[nodiscard]] ucoro::awaitable<std::string> read_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> tick_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> send_coro(std::string data);
|
||||
void close();
|
||||
std::string to_string();
|
||||
|
||||
|
||||
@@ -58,11 +58,6 @@ void UDP_Server::tick() {
|
||||
start_write();
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> 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<size_t>(read_chunk_size), 0);
|
||||
@@ -143,20 +138,11 @@ std::vector<UDP_Server::Read_Info> UDP_Server::read() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ucoro::awaitable<std::vector<UDP_Server::Read_Info>> 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<uint16_t>(last_peer.port), data);
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> 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<void> 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<uint16_t>(client.port), msg);
|
||||
}
|
||||
}
|
||||
|
||||
ucoro::awaitable<void> UDP_Server::write_to_all_clients_coro(std::string msg) {
|
||||
write_to_all_clients(msg);
|
||||
co_return;
|
||||
}
|
||||
|
||||
} // namespace Psc::asio_socket
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "ASIO_Utils.h"
|
||||
#include <ucoro/awaitable.hpp>
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
@@ -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<void> tick_coro();
|
||||
[[nodiscard]] ucoro::awaitable<void> reply_last_peer_coro(std::string data);
|
||||
[[nodiscard]] ucoro::awaitable<void> send_to_coro(std::string ip, uint16_t port, std::string data);
|
||||
[[nodiscard]] ucoro::awaitable<void> write_to_all_clients_coro(std::string msg);
|
||||
|
||||
struct Read_Info {
|
||||
Sockaddr_In peer;
|
||||
@@ -33,7 +28,6 @@ public:
|
||||
};
|
||||
|
||||
std::vector<Read_Info> read();
|
||||
[[nodiscard]] ucoro::awaitable<std::vector<Read_Info>> read_coro();
|
||||
|
||||
enum State {
|
||||
Not_Created,
|
||||
|
||||
@@ -5,4 +5,4 @@
|
||||
#include "TCP_Server.h"
|
||||
#include "UDP_Client.h"
|
||||
#include "UDP_Server.h"
|
||||
|
||||
#include "Socket_Coro.h"
|
||||
|
||||
Reference in New Issue
Block a user