init
This commit is contained in:
18
Gateway/gateway/event/base_event.cc
Normal file
18
Gateway/gateway/event/base_event.cc
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "StdAfx.h"
|
||||
#include "base_event.h"
|
||||
|
||||
BaseEvent::BaseEvent(GameClientMgr* cli_mgr)
|
||||
:cli_mgr_(cli_mgr) {
|
||||
}
|
||||
|
||||
BaseEvent::~BaseEvent()
|
||||
{
|
||||
}
|
||||
|
||||
bool BaseEvent::init(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void BaseEvent::RunOne(void) {
|
||||
|
||||
}
|
||||
16
Gateway/gateway/event/base_event.h
Normal file
16
Gateway/gateway/event/base_event.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
class BaseEvent
|
||||
{
|
||||
public:
|
||||
BaseEvent(GameClientMgr* cli_mgr);
|
||||
~BaseEvent();
|
||||
virtual bool init(void);
|
||||
virtual void RunOne(void);
|
||||
virtual void AddReadFd(SOCKET fd, void *ptr = NULL) {}
|
||||
virtual void DelFd(SOCKET fd, void *ptr = NULL) {}
|
||||
virtual void AddWriteFd(SOCKET fd, void *ptr = NULL) {}
|
||||
|
||||
protected:
|
||||
GameClientMgr* cli_mgr_;
|
||||
};
|
||||
|
||||
52
Gateway/gateway/event/epoll_event.cc
Normal file
52
Gateway/gateway/event/epoll_event.cc
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "StdAfx.h"
|
||||
#include "epoll_event.h"
|
||||
|
||||
#ifndef _MSC_VER
|
||||
EpollEvent::EpollEvent(GameClientMgr* cli_mgr) : BaseEvent(cli_mgr) {
|
||||
epollfd_ = 0;
|
||||
listen_fd_ = 0;
|
||||
}
|
||||
|
||||
|
||||
EpollEvent::~EpollEvent() {
|
||||
if (epollfd_) {
|
||||
close(epollfd_);
|
||||
}
|
||||
}
|
||||
bool EpollEvent::init(void) {
|
||||
epollfd_ = epoll_create(EPOLLEVENTS);
|
||||
return true;
|
||||
}
|
||||
void EpollEvent::RunOne(void) {
|
||||
int ret = epoll_wait(epollfd_, events_, EPOLLEVENTS, 0);
|
||||
for (int i = 0; i < ret; i++) {
|
||||
if (Client *ptr = (Client*)events_[i].data.ptr)
|
||||
{
|
||||
if (events_[i].events & EPOLLIN) {
|
||||
cli_mgr_->HandleReadEvent(ptr->GetFd(), ptr);
|
||||
} else if (events_[i].events & EPOLLOUT) {
|
||||
cli_mgr_->HandleWriteEvent(ptr->GetFd(), ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EpollEvent::AddReadFd(SOCKET fd, void *ptr) {
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.ptr = ptr;
|
||||
epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev);
|
||||
}
|
||||
|
||||
void EpollEvent::DelFd(SOCKET fd, void *ptr) {
|
||||
epoll_ctl(epollfd_, EPOLL_CTL_DEL, fd, NULL);
|
||||
}
|
||||
|
||||
void EpollEvent::AddWriteFd(SOCKET fd, void *ptr) {
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN | EPOLLOUT;
|
||||
ev.data.ptr = ptr;
|
||||
epoll_ctl(epollfd_, EPOLL_CTL_MOD, fd, &ev);
|
||||
}
|
||||
|
||||
#endif
|
||||
22
Gateway/gateway/event/epoll_event.h
Normal file
22
Gateway/gateway/event/epoll_event.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#ifndef _MSC_VER
|
||||
#include "base_event.h"
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#define EPOLLEVENTS 1024
|
||||
class EpollEvent :
|
||||
public BaseEvent {
|
||||
public:
|
||||
EpollEvent(GameClientMgr* cli_mgr);
|
||||
~EpollEvent();
|
||||
virtual bool init(void);
|
||||
virtual void RunOne(void);
|
||||
virtual void AddReadFd(SOCKET fd, void *ptr = NULL);
|
||||
virtual void DelFd(SOCKET fd, void *ptr = NULL);
|
||||
virtual void AddWriteFd(SOCKET fd, void *ptr = NULL);
|
||||
private:
|
||||
SOCKET epollfd_;
|
||||
SOCKET listen_fd_;
|
||||
struct epoll_event events_[EPOLLEVENTS];
|
||||
};
|
||||
#endif
|
||||
13
Gateway/gateway/event/select_event.cc
Normal file
13
Gateway/gateway/event/select_event.cc
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "StdAfx.h"
|
||||
#include "select_event.h"
|
||||
SelectEvent::SelectEvent(GameClientMgr* cli_mgr) : BaseEvent(cli_mgr){
|
||||
|
||||
}
|
||||
|
||||
SelectEvent::~SelectEvent() {
|
||||
|
||||
}
|
||||
|
||||
void SelectEvent::RunOne(void) {
|
||||
cli_mgr_->ProssClient();
|
||||
}
|
||||
11
Gateway/gateway/event/select_event.h
Normal file
11
Gateway/gateway/event/select_event.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "base_event.h"
|
||||
#include <map>
|
||||
|
||||
class SelectEvent : public BaseEvent {
|
||||
public:
|
||||
typedef std::map<SOCKET, void*> FdPtrMap;
|
||||
SelectEvent(GameClientMgr* cli_mgr);
|
||||
~SelectEvent();
|
||||
virtual void RunOne(void);
|
||||
};
|
||||
Reference in New Issue
Block a user