44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
// UDP_Server.h
|
|
#pragma once
|
|
#include "Socket.h"
|
|
namespace Psc::socket {
|
|
class UDP_Server {
|
|
public:
|
|
void set_bind_address(const Sockaddr_In& addr) { bind_address = addr; }
|
|
void set_bind_address(const std::string& ip, std::uint32_t port) {
|
|
bind_address = Sockaddr_In(ip, port);
|
|
}
|
|
[[nodiscard]] std::string to_string() const {
|
|
return bind_address.to_string();
|
|
}
|
|
void create();
|
|
bool bind();
|
|
|
|
enum State {
|
|
Not_Created,
|
|
Not_Set_Field,
|
|
Not_Bind,
|
|
Working,
|
|
};
|
|
State state = Not_Created;
|
|
|
|
void tick();
|
|
void close();
|
|
// 回复到最近一次 read() 的对端(如果你想做 echo/server 常用)
|
|
void reply_last_peer(const std::string& data);
|
|
|
|
// 直接发给指定地址(需要你自己提供 ip/port)
|
|
void send_to(const std::string& ip, uint16_t port, const std::string& data);
|
|
void write_to_all_clients(const std::string & msg);
|
|
Sockaddr_In bind_address;
|
|
std::vector<Sockaddr_In> clients;
|
|
Socket_FD socket_fd = -1;
|
|
bool bound = false;
|
|
int read_chunk_size = 1024 * 1024;
|
|
Sockaddr_In last_peer{};
|
|
bool has_last_peer = false;
|
|
size_t buffer_size = 4096 * 10;
|
|
};
|
|
|
|
} // namespace Psc::socket
|