zoukankan      html  css  js  c++  java
  • R2学习记录

    Setting up the Libevent library
    1.重写log行为

    #include <event2/event.h>
    #include <stdio.h>
    static void discard_cb(int severity, const char *msg) {
        /* This callback does nothing. */
    }
    static FILE *logfile = NULL;
    static void write_to_file_cb(int severity, const char *msg) {
        const char *s;
        if (!logfile)
            return;
        switch (severity) {
            case _EVENT_LOG_DEBUG: 
                s = "debug";
            case _EVENT_LOG_MSG:
                s = "msg";
            case _EVENT_LOG_WARN: 
                s = "warn";
            case _EVENT_LOG_ERR:
                s = "error";
            default:    /* never reached */
                s = "?";
        }
        fprintf(logfile, "[%s] %s
    ", s, msg);
    }
    
    /* Turn off all logging from Libevent. */
    void suppress_logging(void) {
        event_set_log_callback(discard_cb);
    }
    /* Redirect all Libevent log messages to the C stdio file 'f'. */
    void set_logfile(FILE *f) {
        logfile = f;
        event_set_log_callback(write_to_file_cb);
    }

    2.重写异常退出动作:

    typedef void (*event_fatal_cb)(int err);
    void event_set_fatal_callback(event_fatal_cb cb);

    3.重写内存管理方式:

    void event_set_mem_functions(void *(*malloc_fn)(size_t sz),
    void *(*realloc_fn)(void *ptr, size_t sz),
    void (*free_fn)(void *ptr));

    4.锁和线程
    libevent默认支持win和类unix的线程库, 如果想自己实现线程库的话需要实现:
    Locks
    locking
    unlocking
    lock allocation
    lock destruction
    Conditions
    condition variable creation
    condition variable destruction
    waiting on a condition variable
    signaling/broadcasting to a condition variable
    Threads
    thread ID detection

    #define EVTHREAD_WRITE     0x04
    #define EVTHREAD_READ    0x08    
    #define EVTHREAD_TRY    0x10
    
    #define EVTHREAD_LOCKTYPE_RECURSIVE 1
    #define EVTHREAD_LOCKTYPE_READWRITE 2
    
    #define EVTHREAD_LOCK_API_VERSION 1
    
    struct evthread_lock_callbacks {
            int lock_api_version;
            unsigned supported_locktypes;
            void *(*alloc)(unsigned locktype);
            void (*free)(void *lock, unsigned locktype);
            int (*lock)(unsigned mode, void *lock);
            int (*unlock)(unsigned mode, void *lock);
    };
    int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *);
    void evthread_set_id_callback(unsigned long (*id_fn)(void));
    struct evthread_condition_callbacks {
            int condition_api_version;
            void *(*alloc_condition)(unsigned condtype);
            void (*free_condition)(void *cond);
            int (*signal_condition)(void *cond, int broadcast);
            int (*wait_condition)(void *cond, void *lock,
                            const struct timeval *timeout);
    };
    int evthread_set_condition_callbacks(
            const struct evthread_condition_callbacks *);
  • 相关阅读:
    跨站请求伪造 CSRF
    通过反射动态调用外部托管Dll
    Effective TestStand Operator Interfaces
    DataTable转成实体列表 和 DataRow转成实体类
    NI LabVIEW 编程规范
    TestStand 界面重置【小技巧】
    TestStand 基本知识[1]--基本配置
    泛型 List转换成DataTable
    如何修改数据库的服务器排序规则
    3个N加上各种运算符号结果等于6(纯属娱乐)C#
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/9795337.html
Copyright © 2011-2022 走看看