基于多反应堆的高并发服务器【C/C++/Reactor】(下)重构Channel类

一、C语言

  • Channel.h
#pragma once
#include <stdbool.h>
// 定义函数指针
typedef int(*handleFunc)(void* arg);
 
// 定义文件描述符的读写事件
enum FDEvent {
    TimeOut = 0x01,
    ReadEvent = 0x02,
    WriteEvent = 0x04
};
 
struct Channel {
    // 文件描述符
    int fd;
    // 事件
    int events;
    // 回调函数
    handleFunc readCallback;// 读回调
    handleFunc writeCallback;// 写回调
    handleFunc destroyCallback;// 销毁回调
    // 回调函数的参数
    void* arg;
};
 
// 初始化一个Channel 
struct Channel* channelInit(int fd, int events, handleFunc readFunc, handleFunc writeFunc,handleFunc destroyFunc, void* arg);
 
// 修改fd的写事件(检测 or 不检测)
void writeEventEnable(struct Channel* channel, bool flag);
 
// 判断是否需要检测文件描述符的写事件
bool isWriteEventEnable(struct Channel* channel);
  • Channel.c
#include "Channel.h"
#include <stdlib.h>
 
struct Channel* channelInit(int fd, int events, handleFunc readFunc, 
       handleFunc writeFunc, handleFunc destroyFunc, void* arg) {
    struct Channel* channel = (struct Channel*)malloc(sizeof(struct Channel));
    channel->fd = fd;
    channel->events = events;
    channel->readCallback = readFunc;
    channel->writeCallback = writeFunc;
    channel->destroyCallback = destroyFunc;
    channel->arg = arg;
    return channel;
}
 
void writeEventEnable(struct Channel* channel, bool flag) {
    if(flag) {
        channel->events |= WriteEvent;
    }else{
        channel->events = channel->events & ~WriteEvent;
    }
}
 
bool isWriteEventEnable(struct Channel* channel) {
    return channel->events & WriteEvent;
}

二、C++

  • Channel.h
#pragma once
#include <stdbool.h>
// 定义函数指针
// typedef int(*handleFunc)(void* arg);
using handleFunc = int(*)(void* arg);
 
// 定义文件描述符的读写事件 (强类型枚举)
enum class FDEvent {
    TimeOut = 0x01,
    ReadEvent = 0x02,
    WriteEvent = 0x04
};
 
class Channel 
{
public:
    Channel(int fd, int events, handleFunc readFunc, handleFunc writeFunc,handleFunc destroyFunc, void* arg);
    // 回调函数
    handleFunc readCallback;// 读回调
    handleFunc writeCallback;// 写回调
    handleFunc destroyCallback;// 销毁回调
    // 修改fd的写事件(检测 or 不检测)
    void writeEventEnable(bool flag);
    // 判断是否需要检测文件描述符的写事件
    bool isWriteEventEnable();
    // 取出私有成员的值
    inline int getEvent() {
        return m_events;
    }
    inline int getSocket() {
        return m_fd;
    }
    inline const void* getArg() {
        return m_arg;
    }
private:
    // 文件描述符
    int m_fd;
    // 事件
    int m_events;
    // 回调函数的参数
    void* m_arg;
};
 
  • Channel.cpp 
#include "Channel.h"
#include <stdlib.h>

Channel::Channel(int fd, int events, handleFunc readFunc, 
       handleFunc writeFunc, handleFunc destroyFunc, void* arg) {
    m_fd = fd;
    m_events = events;
    m_arg = arg;
    readCallback = readFunc;
    writeCallback = writeFunc;
    destroyCallback = destroyFunc;
}
 
void Channel::writeEventEnable(bool flag) {
    if(flag) {
        m_events |= static_cast<int>(FDEvent::WriteEvent);
    }else{
        m_events = m_events & ~static_cast<int>(FDEvent::WriteEvent);
    }
}
 
bool Channel::isWriteEventEnable() {
    return m_events & static_cast<int>(FDEvent::WriteEvent);
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 05:24:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 05:24:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 05:24:03       87 阅读
  4. Python语言-面向对象

    2024-01-17 05:24:03       96 阅读

热门阅读

  1. 24校招,得物测试开发工程师一面

    2024-01-17 05:24:03       57 阅读
  2. sklearn快速实现python机器学习算法

    2024-01-17 05:24:03       45 阅读
  3. TCP服务器和客户端的创建步骤

    2024-01-17 05:24:03       51 阅读
  4. 1.1 面试经典 150 题-合并两个有序数组

    2024-01-17 05:24:03       59 阅读
  5. 设计模式——组合模式

    2024-01-17 05:24:03       49 阅读
  6. js 什么是外边距重叠怎么解决

    2024-01-17 05:24:03       43 阅读