说明:
1.channel.h
/*
* channle.h
*
* Created on: Apr 29, 2014
* Author: root
*/
#ifndef CHANNLE_H_
#define CHANNLE_H_
typedef void (*timer_event_handler_pt)(channel_t *channel);
typedef void (*read_event_handler_pt)(channel_t *channel);
typedef void (*write_event_handler_pt)(channel_t *channel);
struct channel_t {
int m_sockfd; //监听的fd
int m_events; //事件类型
int m_revents; //接收的事件类型
write_event_handler_pt write_event_handler; // 写事件
timer_event_handler_pt timer_event_handler; // 时间时间
read_event_handler_pt read_event_handler; // 读事件
};
#endif /* CHANNLE_H_ */
declear.h
/*
* declear.h
*
* Created on: Apr 28, 2014
* Author: root
*/
#ifndef DECLEAR_H_
#define DECLEAR_H_
#define MAX_EVENT 500
#define MAX_CONNECTIONS 1024
#include <errno.h>
#include <cstddef>
#include <sys/epoll.h>
#include <vector>
#include <iostream>
#include "opeation.h"
class Epoll;
class EventLoop;
struct channel_t;
class ChannelCallBack;
#endif /* DECLEAR_H_ */
epoll.h
/*
* epoll.h
*
* Created on: Apr 28, 2014
* Author: root
*/
#ifndef EPOLL_H_
#define EPOLL_H_
#include "declear.h"
#include "channel.h"
using namespace std;
class Epoll {
public:
Epoll();
virtual ~Epoll();
/**
* 添加监听对象
*/
void AddEvent(channel_t* channel);
/**
* 把epoll_wait事件添加到vector
*/
void ProcessEvents(Opeation* op,vector<channel_t*>* list_channels);
void CallBackPoll(channel_t* channel);
private:
int epollfd;
struct epoll_event events[MAX_EVENT];
};
#endif /* EPOLL_H_ */
epoll.cpp
/*
* epoll.cpp
*
* Created on: Apr 28, 2014
* Author: root
*/
#include "epoll.h"
#include "channel.h"
using namespace std;
Epoll::Epoll() {
epollfd = ::epoll_create(MAX_CONNECTIONS);
if (epollfd <= 0)
cout << "epoll_create error, errno:" << epollfd << endl;
}
Epoll::~Epoll() {
}
void Epoll::CallBackPoll(channel_t* channel)
{
int m_revents = channel->m_revents;
if(m_revents & EPOLLIN)
{
channel->read_event_handler(channel);
}
if(m_revents & EPOLLOUT)
{
channel->write_event_handler(channel);
}
}
/**
* 把epoll_wait事件添加到vector
*/
void Epoll::ProcessEvents(Opeation* pa,vector<channel_t*>* list_channels)
{
int fds = ::epoll_wait(epollfd, events, MAX_EVENT, pa->timeout);
if(fds == -1)
{
cout << "epoll_wait error, errno:" << errno << endl;
return;
}
for(int i = 0; i < fds; i++)
{
channel_t* channel = static_cast<channel_t*>(events[i].data.ptr);
channel->m_revents = events[i].events;
if(pa->flags == fast)
{
this->CallBackPoll(channel);
}
else
{
list_channels->push_back(channel);
}
}
}
/**
* 添加监听对象
*/
void Epoll::AddEvent(channel_t* channel)
{
struct epoll_event ev;
ev.data.ptr = channel;
ev.events = channel->m_events;
int fd = channel->m_sockfd;
::epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
}
eventloop.h
/*
* eventloop.h
*
* Created on: Apr 28, 2014
* Author: root
*/
#ifndef EVENTLOOP_H_
#define EVENTLOOP_H_
#include "declear.h"
class EventLoop {
public:
EventLoop();
virtual ~EventLoop();
void EventProcessInit();
/**
* 添加监听对象
*/
void AddEvent(channel_t* pChannel);
/**
* 处理事件集合对象
*/
void Loop(Opeation* op);
private:
/**
* 是否退出
*/
bool quit;
/**
* 调用EPOLL接口
*/
Epoll* poller;
};
#endif /* EVENTLOOP_H_ */
eventloop.cpp
/*
* eventloop.cpp
*
* Created on: Apr 28, 2014
* Author: root
*/
#include "eventloop.h"
#include "channel.h"
#include "epoll.h"
#include <vector>
using namespace std;
EventLoop::EventLoop()
:quit(false)
,poller(new Epoll()) // Memory Leak !!!
{
}
EventLoop::~EventLoop()
{}
/**
* 处理事件集合对象
*/
void EventLoop::Loop(Opeation* op)
{
while(!quit)
{
vector<channel_t*> list_channel;
poller->ProcessEvents(op,&list_channel);
if(list_channel.size() > 0 )
{
vector<channel_t*>::iterator it;
for(it = list_channel.begin(); it != list_channel.end(); ++it)
{
poller->CallBackPoll(*it);
}
}
}
}
/**
* 添加监听对象
*/
void EventLoop::AddEvent(channel_t* channel)
{
poller->AddEvent(channel);
}
opeation.h
/*
* opeation.h
*
* Created on: Apr 28, 2014
* Author: root
*/
#ifndef OPEATION_H_
#define OPEATION_H_
enum Flags {
fast = 0 ,
queue = 1
};
struct Opeation {
enum Flags flags;
int timeout;
};
#endif /* OPEATION_H_ */
server.h
#ifndef SERVER_H_
#define SERVER_H_
#include "declear.h"
class Server {
public:
Server();
virtual ~Server();
void run();
};
#endif /* SERVER_H_ */
server.cpp
/*
* server.cpp
*
* Created on: Apr 28, 2014
* Author: root
*/
#include "server.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <errno.h>
#include <string.h> //for bzero
#include <iostream>
#include <vector>
#include "channel.h"
#include "eventloop.h"
#define MAX_LISTENFD 1024
using namespace std;
Server::Server() {
// TODO Auto-generated constructor stub
}
Server::~Server() {
// TODO Auto-generated destructor stub
}
int createAndListen()
{
int on = 1;
int _listenfd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in servaddr;
fcntl(_listenfd, F_SETFL, O_NONBLOCK); //no-block io
setsockopt(_listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(11111);
if(-1 == bind(_listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)))
{
cout << "bind error, errno:" << errno << endl;
}
if(-1 == listen(_listenfd, MAX_LISTENFD))
{
cout << "listen error, errno:" << errno << endl;
}
return _listenfd;
}
void handleRead(channel_t* channel)
{
// TODO Auto-generated constructor stub
cout << "server::handleread..........." << errno << endl;
}
void handleWrite(channel_t* channel)
{
// TODO Auto-generated constructor stub
}
void handleTimer(channel_t* channel)
{
}
void Server::run() {
int listenfd = createAndListen();
channel_t channel;
channel.m_sockfd = listenfd;
channel.m_events = 1;
channel.read_event_handler = handleRead;
channel.timer_event_handler = handleTimer;
channel.write_event_handler = handleWrite;
EventLoop* loop = new EventLoop();
loop->AddEvent(&channel);
Opeation* op = new Opeation();
op->timeout = 500;
op->flags = fast;
loop->Loop(op);
}