libEvent使用另外一条线程发送数据

网上一直没有找到在另外一条线程发送数据资料,不知道是不是我的想法不对,总之,自己写了一份server端的代码。直接上代码

server.h:

#pragma once

#include "event.h"
#include "event2/util.h"
#include "event2/listener.h"
#include "AX_Thread.h"
#include "AX_Mutex.h"
#include <queue>
#include <map>

#define SAFE_DELETE(x) if(x){delete x;x=NULL;}
#define SAFE_DELETE_A(x) if(x){delete[] x;x=NULL;}

typedef struct Send_Data
{
 bufferevent* event;
 int len;
 char* data;
}Send_Data;

typedef std::map<bufferevent*, int> EventSocketMap;
typedef std::map<int, bufferevent*> SocketEventMap;
typedef std::map<bufferevent*, bool> SocketEventReadyMap;
typedef std::queue<Send_Data> SendDataList;

class CServer
{
public:
 CServer(void);
 ~CServer(void);

 virtual int Listen(char* ip, int port);
 virtual int Close(void);
 virtual int Heartbeat();
 virtual int Send(int id, char *pBuf, unsigned int nBufLen);

 virtual int event_dispath_msg();

 int SetRecvTPBuffSize(int size);
 int SetSelectTimeout(long sec, long usec);

private:
 void init();
 void run();
 static void* TcpSendThread(void* param);
 static void listener_cb(evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sock, int socklen, void *arg);
 static void socket_read_cb(bufferevent *bev, void *arg);
 static void socket_write_cb(bufferevent *bev, void *arg);
 static void socket_event_cb(bufferevent *bev, short events, void *arg);
 
private:
 EventSocketMap _EventSocketMap;
 SocketEventMap _SocketEventMap;
 SocketEventReadyMap _SocketEventReadyMap;
 event_base* _base;
 evconnlistener* _listener;
 bool _bRun;
 AX_hthread_t _SendThread;
 AX_Mutex _mutex;
 SendDataList _data_list;
 int _tpRecvBuffSize;
 char* _buffer;
};


server.cpp:

#include "Server.h"
#include "event2/thread.h"

CServer::CServer(void)
{
 _base = NULL;
 _listener = NULL;
 _bRun = false;
 _buffer = NULL;
 _tpRecvBuffSize = 0;
 init();
}

CServer::~CServer(void)
{

}

void CServer::init()
{
 WSAData wsa;
 if (::WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  return;

 _EventSocketMap.clear();
 _SocketEventMap.clear();
 //使libEvent支持多线程
 evthread_use_windows_threads();
}

//socket发送线程
void CServer::run()
{
 while(_bRun)
 { <

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值