添加udp group

This commit is contained in:
zcy 2022-02-14 00:31:34 +08:00
parent 7ff82302cb
commit fdb5f15fef
4 changed files with 134 additions and 7 deletions

View File

@ -1,3 +1,133 @@
#include "udp_group_libevent.h" #include "udp_group_libevent.h"
#include "udp_group_libevent.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <event.h>
#include <event2/listener.h>
#ifdef __cplusplus
}
#endif
#define MSG_LEN 1024
static void read_cb(evutil_socket_t fd, short event, void* arg) {
UdpGroupLibevent* parent = (UdpGroupLibevent*)arg;
if (parent == nullptr)
return;
char buf[MSG_LEN];
int len;
int addr_len = sizeof(struct sockaddr);
struct sockaddr_in cli_addr;
memset(buf, 0, sizeof(buf));
len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&cli_addr, &addr_len);
if (len == -1) {
perror("recvfrom");
}
else if (len == 0) {
printf("Connection Closed\n");
}
else {
buf[len] = '\0';
printf("recv[%s:%d]\n", buf, len);
// sendto(fd, buf, len, 0, (struct sockaddr*)&cli_addr, addr_len);
if (parent->OnReadHandle() != nullptr) {
UdpGroupLibevent::OnReadDataHandle p = parent->OnReadHandle();
if (p)
p(buf, len, cli_addr);
}
}
}
int UdpGroupLibevent::bind_socket(struct event* ev, const char* ip, uint16_t port) {
int sock_fd;
int flag = 1;
struct sockaddr_in local_addr;
if ((nullptr == ip) || (nullptr == ev)) {
m_status = FAIL;
return -1;
}
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return -1;
}
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&flag, sizeof(flag)) < 0) {
perror("setsockopt");
return 1;
}
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(port);
local_addr.sin_addr.s_addr = inet_addr(ip);
if (bind(sock_fd, (struct sockaddr*)&local_addr, sizeof(struct sockaddr)) < 0) {
perror("bind");
return -1;
}
else {
printf("bind success, port[%d]\n", port);
}
event_set(ev, sock_fd, EV_READ | EV_PERSIST, &read_cb, (void*)this);
if (event_add(ev, NULL) == -1) {
perror("event_set");
}
m_sock_fd = sock_fd;
return 0;
}
UdpGroupLibevent::OnReadDataHandle UdpGroupLibevent::OnReadHandle() {
return this->mOnRead;
}
void UdpGroupLibevent::SetOnReadHandle(UdpGroupLibevent::OnReadDataHandle p) {
this->mOnRead = p;
}
void UdpGroupLibevent::SendTo(const char* dat, uint32_t len, std::string ip, int port) {
struct sockaddr_in dest_addr;
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port);
dest_addr.sin_addr.s_addr = inet_addr(ip.c_str());
int addr_len = sizeof(struct sockaddr);
if (m_sock_fd > 0) {
sendto(m_sock_fd, (const char*)dat, len, 0, (struct sockaddr*)&dest_addr, addr_len);
}
}
int UdpGroupLibevent::SocketFD() {
return this->mSocketFD;
}
UdpGroupLibevent::
UdpGroupLibevent(std::string ip, uint32_t port)
{
m_status = STOP;
m_bind_ip = ip;
m_port = port;
if (event_init() == NULL) {
printf("event_init() failed\n");
}
m_event = new struct event;
if (0 > bind_socket(m_event, ip.c_str(), port)) {
return;
}
m_thread = new std::thread([this]() {
event_dispatch();
}
);
m_status = RUNNING;
}

View File

@ -1,6 +1,4 @@
#pragma once #pragma once
#include <string> #include <string>
#ifndef _WIN32_WINNT #ifndef _WIN32_WINNT
@ -28,14 +26,14 @@ class UdpGroupLibevent {
public: public:
typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle; typedef std::function<void(const char* dat, int len, struct sockaddr_in)> OnReadDataHandle;
UdpGroupLibevent(std::string ip, uint32_t port); UdpGroupLibevent(std::string ip, uint32_t port);
OnReadDataHandle* OnReadHandle(); OnReadDataHandle OnReadHandle();
typedef enum { typedef enum {
RUNNING, RUNNING,
STOP, STOP,
FAIL FAIL
}STATUS; }STATUS;
friend void read_cb(int, short, void*); friend void read_cb(int, short, void*);
void SetOnReadHandle(OnReadDataHandle*); void SetOnReadHandle(OnReadDataHandle);
void SendTo(const char* dat, uint32_t len, std::string ip, int port); void SendTo(const char* dat, uint32_t len, std::string ip, int port);
int SocketFD(); int SocketFD();
private: private:
@ -49,7 +47,7 @@ private:
STATUS m_status; // STATUS m_status; //
std::thread* m_thread; // 当前线程 std::thread* m_thread; // 当前线程
intptr_t mSocketFD; // 操作系统原生socket intptr_t mSocketFD; // 操作系统原生socket
OnReadDataHandle* mOnRead; OnReadDataHandle mOnRead;
}; };

View File

@ -15,7 +15,7 @@ extern "C" {
#endif #endif
#define MSG_LEN 1024 #define MSG_LEN 1024
void read_cb(evutil_socket_t fd, short event, void* arg) { static void read_cb(evutil_socket_t fd, short event, void* arg) {
UdpDataGramLibevent* parent = (UdpDataGramLibevent*)arg; UdpDataGramLibevent* parent = (UdpDataGramLibevent*)arg;
if (parent == nullptr) if (parent == nullptr)
return; return;

View File

@ -19,7 +19,6 @@
#include <thread> #include <thread>
#include <map> #include <map>
#include <functional> #include <functional>
#include <functional>
class UdpDataGramLibevent { class UdpDataGramLibevent {