tcp udp 改协程
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "TCP_Server.h"
|
||||
|
||||
#include <array>
|
||||
#include <utility>
|
||||
|
||||
namespace Psc::asio_socket {
|
||||
|
||||
@@ -23,6 +24,11 @@ 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;
|
||||
@@ -54,6 +60,14 @@ 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) {
|
||||
@@ -121,6 +135,10 @@ 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;
|
||||
|
||||
@@ -240,6 +258,11 @@ 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) {
|
||||
@@ -250,6 +273,11 @@ 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{};
|
||||
@@ -270,6 +298,10 @@ 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) {
|
||||
|
||||
Reference in New Issue
Block a user