54 lines
1.2 KiB
C
54 lines
1.2 KiB
C
|
#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>
|
|||
|
#include <functional>
|
|||
|
|
|||
|
|
|||
|
class UdpDataGramLibevent {
|
|||
|
public:
|
|||
|
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle;
|
|||
|
UdpDataGramLibevent(std::string ip, uint32_t port);
|
|||
|
OnReadDataHandle* OnReadHandle();
|
|||
|
typedef enum {
|
|||
|
RUNNING,
|
|||
|
STOP,
|
|||
|
FAIL
|
|||
|
}STATUS;
|
|||
|
friend void read_cb(int, short, void*);
|
|||
|
void SetOnReadHandle(OnReadDataHandle*);
|
|||
|
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);
|
|||
|
uint32_t m_port; // <20><><EFBFBD><EFBFBD><EFBFBD>˿ں<CBBF>
|
|||
|
std::string m_bind_ip; // <20>˿ں<CBBF>
|
|||
|
uint16_t m_backlog;
|
|||
|
int m_sock_fd;
|
|||
|
struct sockaddr_in m_bind_addr; // <20><EFBFBD>ַ
|
|||
|
struct event* m_event;
|
|||
|
STATUS m_status; //
|
|||
|
std::thread* m_thread; // <20><>ǰ<EFBFBD>߳<EFBFBD>
|
|||
|
intptr_t mSocketFD; // <20><><EFBFBD><EFBFBD>ϵͳԭ<CDB3><D4AD>socket
|
|||
|
OnReadDataHandle* mOnRead;
|
|||
|
};
|
|||
|
|
|||
|
|