tcp udp 改协程
This commit is contained in:
@@ -58,6 +58,11 @@ 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);
|
||||
@@ -138,11 +143,20 @@ 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;
|
||||
@@ -156,10 +170,20 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user