This commit is contained in:
aixianling
2025-01-09 17:45:40 +08:00
commit 5c9f1dae4a
3482 changed files with 1146531 additions and 0 deletions

View 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) {
}

View 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_;
};

View 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

View 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

View 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();
}

View 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);
};