nim_duilib/examples/proto_debuger/udp_libevent.h

58 lines
1.3 KiB
C
Raw Normal View History

2022-02-09 23:31:01 +08:00
#pragma once
#include <string>
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#include <WinSock2.h>
#endif
#ifdef linux
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#define EVENT__HAVE_PTHREADS
#endif
#include <stdint.h>
#include <iostream>
#include <mutex>
#include <thread>
#include <map>
#include <functional>
2022-02-14 23:48:50 +08:00
#include <functional>
2022-02-09 23:31:01 +08:00
class UdpDataGramLibevent {
public:
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle;
UdpDataGramLibevent(std::string ip, uint32_t port);
2022-02-14 23:48:50 +08:00
UdpDataGramLibevent(std::string ip, uint32_t port, std::string group_addr);
2022-02-11 14:26:25 +08:00
OnReadDataHandle OnReadHandle();
2022-02-09 23:31:01 +08:00
typedef enum {
RUNNING,
STOP,
FAIL
}STATUS;
friend void read_cb(int, short, void*);
2022-02-11 14:26:25 +08:00
void SetOnReadHandle(OnReadDataHandle);
2022-02-09 23:31:01 +08:00
void SendTo(const char* dat, uint32_t len, std::string ip, int port);
int SocketFD();
private:
int bind_socket(struct event* ev, const char* ip, uint16_t port);
2022-02-14 23:48:50 +08:00
int bind_socket_with_group(struct event* ev, const char* ip, uint16_t port, const char* udp_group);
uint32_t m_port; //
std::string m_bind_ip; //
2022-02-09 23:31:01 +08:00
uint16_t m_backlog;
int m_sock_fd;
2022-02-14 23:48:50 +08:00
struct sockaddr_in m_bind_addr; //
2022-02-09 23:31:01 +08:00
struct event* m_event;
STATUS m_status; //
2022-02-14 23:48:50 +08:00
std::thread* m_thread; //
intptr_t mSocketFD; // socket
2022-02-11 14:26:25 +08:00
OnReadDataHandle mOnRead;
2022-02-09 23:31:01 +08:00
};