| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #ifndef HV_UDP_SERVER_HPP_
- #define HV_UDP_SERVER_HPP_
- #include "hsocket.h"
- #include "EventLoopThreadPool.h"
- #include "Callback.h"
- #include "Channel.h"
- namespace hv {
- class UdpServer {
- public:
- UdpServer() {
- #if WITH_KCP
- enable_kcp = false;
- #endif
- }
- virtual ~UdpServer() {
- }
- const EventLoopPtr& loop() {
- return loop_thread.loop();
- }
- //@retval >=0 bindfd, <0 error
- int createsocket(int port, const char* host = "0.0.0.0") {
- hio_t* io = hloop_create_udp_server(loop_thread.hloop(), host, port);
- if (io == NULL) return -1;
- channel.reset(new SocketChannel(io));
- return channel->fd();
- }
- void closesocket() {
- if (channel) {
- channel->close();
- channel = NULL;
- }
- }
- int startRecv() {
- assert(channel != NULL);
- channel->onread = [this](Buffer* buf) {
- if (onMessage) {
- onMessage(channel, buf);
- }
- };
- channel->onwrite = [this](Buffer* buf) {
- if (onWriteComplete) {
- onWriteComplete(channel, buf);
- }
- };
- #if WITH_KCP
- if (enable_kcp) {
- hio_set_kcp(channel->io(), &kcp_setting);
- }
- #endif
- return channel->startRead();
- }
- void start(bool wait_threads_started = true) {
- loop_thread.start(wait_threads_started, std::bind(&UdpServer::startRecv, this));
- }
- void stop(bool wait_threads_stopped = true) {
- loop_thread.stop(wait_threads_stopped);
- }
- int sendto(const void* data, int size, struct sockaddr* peeraddr = NULL) {
- if (channel == NULL) return -1;
- std::lock_guard<std::mutex> locker(sendto_mutex);
- if (peeraddr) hio_set_peeraddr(channel->io(), peeraddr, SOCKADDR_LEN(peeraddr));
- return channel->write(data, size);
- }
- int sendto(Buffer* buf, struct sockaddr* peeraddr = NULL) {
- return sendto(buf->data(), buf->size(), peeraddr);
- }
- int sendto(const std::string& str, struct sockaddr* peeraddr = NULL) {
- return sendto(str.data(), str.size(), peeraddr);
- }
- public:
- SocketChannelPtr channel;
- #if WITH_KCP
- bool enable_kcp;
- kcp_setting_t kcp_setting;
- #endif
- // Callback
- MessageCallback onMessage;
- WriteCompleteCallback onWriteComplete;
- private:
- std::mutex sendto_mutex;
- EventLoopThread loop_thread;
- };
- }
- #endif // HV_UDP_SERVER_HPP_
|