网络开源框架之libevent使用实例

【网络开源框架之libevent使用实例】一身转战三千里,一剑曾当百万师。这篇文章主要讲述网络开源框架之libevent使用实例相关的知识,希望能为你提供帮助。

#pragma once
#include "CLibEventData.h"
#include "LibEventFunction.h"
#include "LibUserFunction.h"
class CLibEvent

public:
CLibEvent(void);
~CLibEvent(void);
private:
//当前服务器对象
Server m_Server;
public:
bool StartServer(int port, short workernum, unsigned int connnum, int read_timeout, int write_timeout);
void StopServer();
private:
void LoadFuns();
static void DoAccept(struct evconnlistener *listener, evutil_socket_t fd,struct sockaddr *sa, int socklen, void *user_data);
static void DoError(struct bufferevent *bev, short error, void *ctx);
static void CloseConn(Conn *pConn,int nFunID);
static void CloseConn(Conn *pConn);
static void DoRead(struct bufferevent *bev, void *ctx);
static DWORD WINAPI ThreadServer(LPVOID lPVOID);
static DWORD WINAPI ThreadWorkers(LPVOID lPVOID);
;



#include "StdAfx.h"
#include "LibEvent.h"

#include < string>
#include < iostream>
using namespace std;

#include < assert.h>
#include < signal.h>

#include < WinSock2.h>
#pragma comment (lib,"ws2_32.lib")
#pragma comment (lib,"wsock32.lib")

#include "LibEventFunction.h"

#ifdef _DEBUG
#pragma comment (lib,"libevent.lib")
#else
#pragma comment (lib,"libevent.lib")
#endif

CLibEvent::CLibEvent(void)

LoadFuns();
ZeroMemory(& m_Server,sizeof(m_Server));
WSADATA WSAData;
WSAStartup(0x0201, & WSAData);


CLibEvent::~CLibEvent(void)

WSACleanup();


bool CLibEvent::StartServer(int port, short workernum, unsigned int connnum, int read_timeout, int write_timeout)

m_Server.bStart=false;
m_Server.nCurrentWorker=0;
m_Server.nPort=port;
m_Server.workernum=workernum;
m_Server.connnum=connnum;
m_Server.read_timeout=read_timeout;
m_Server.write_timeout=write_timeout;
evthread_use_windows_threads();
m_Server.pBase=event_base_new();
if (m_Server.pBase==NULL)

return false;

struct sockaddr_in sin;
memset(& sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(m_Server.nPort);
m_Server.pListener=evconnlistener_new_bind(m_Server.pBase,DoAccept,(void*)& m_Server,LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE,-1,(struct sockaddr*)& sin,sizeof(sin));
if (m_Server.pListener==NULL)

return false;

CLibEventFunction::RegistConnectedFunc(CLibUserFunction::Connect);
CLibEventFunction::RegistDisconnectedFunc(CLibUserFunction::DisConnect);
CLibEventFunction::RegistStx(emStx);
m_Server.pWorker=new Worker[workernum];
for (int i=0; i< workernum; i++)

m_Server.pWorker[i].pWokerbase=event_base_new();
if (m_Server.pWorker[i].pWokerbase== NULL)

delete []m_Server.pWorker;
return false;

//初始化连接对象

m_Server.pWorker[i].pListConn=new ConnList();
if (m_Server.pWorker[i].pListConn==NULL)

return false;

m_Server.pWorker[i].pListConn-> plistConn=new Conn[m_Server.connnum+1];
m_Server.pWorker[i].pListConn-> head=& m_Server.pWorker[i].pListConn-> plistConn[0];
m_Server.pWorker[i].pListConn-> tail=& m_Server.pWorker[i].pListConn-> plistConn[m_Server.connnum];
for (int j=0; j< m_Server.connnum; j++)
m_Server.

    推荐阅读