| 1 |
UDP 服务端类 |
| 2 |
|
| 3 |
```c++ |
| 4 |
|
| 5 |
class UdpServer { |
| 6 |
|
| 7 |
// 返回所在的事件循环 |
| 8 |
const EventLoopPtr& loop(); |
| 9 |
|
| 10 |
// 创建套接字 |
| 11 |
int createsocket(int port, const char* host = "0.0.0.0"); |
| 12 |
|
| 13 |
// 关闭套接字 |
| 14 |
void closesocket(); |
| 15 |
|
| 16 |
// 开始运行 |
| 17 |
void start(bool wait_threads_started = true); |
| 18 |
|
| 19 |
// 停止运行 |
| 20 |
void stop(bool wait_threads_stopped = true); |
| 21 |
|
| 22 |
// 发送 |
| 23 |
int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL); |
| 24 |
int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL); |
| 25 |
int sendto(const std::string& str, struct sockaddr* peeraddr = NULL); |
| 26 |
|
| 27 |
// 设置KCP |
| 28 |
void setKcp(kcp_setting_t* setting); |
| 29 |
|
| 30 |
// 消息回调 |
| 31 |
std::function<void(const TSocketChannelPtr&, Buffer*)> onMessage; |
| 32 |
|
| 33 |
// 写完成回调 |
| 34 |
std::function<void(const TSocketChannelPtr&, Buffer*)> onWriteComplete; |
| 35 |
}; |
| 36 |
|
| 37 |
``` |
| 38 |
|
| 39 |
测试代码见 [evpp/UdpServer_test.cpp](../../evpp/UdpServer_test.cpp) |