• libevent


    libevent doc example

    #include <event2/event.h>
    
    void cb_func(evutil_socket_t fd, short what, void *arg)
    {
            const char *data = arg;
            printf("Got an event on socket %d:%s%s%s%s [%s]",
                (int) fd,
                (what&EV_TIMEOUT) ? " timeout" : "",
                (what&EV_READ)    ? " read" : "",
                (what&EV_WRITE)   ? " write" : "",
                (what&EV_SIGNAL)  ? " signal" : "",
                data);
    }
    
    void main_loop(evutil_socket_t fd1, evutil_socket_t fd2)
    {
            struct event *ev1, *ev2;
            struct timeval five_seconds = {5,0};
            struct event_base *base = event_base_new();
    
            /* The caller has already set up fd1, fd2 somehow, and make them
               nonblocking. */
    
            ev1 = event_new(base, fd1, EV_TIMEOUT|EV_READ|EV_PERSIST, cb_func,
               (char*)"Reading event");
            ev2 = event_new(base, fd2, EV_WRITE|EV_PERSIST, cb_func,
               (char*)"Writing event");
    
            event_add(ev1, &five_seconds);
            event_add(ev2, NULL);
            event_base_dispatch(base);
    }

    The event flags

    EV_TIMEOUT

    This flag indicates an event that becomes active after a timeout elapses.

    The EV_TIMEOUT flag is ignored when constructing an event: you
    can either set a timeout when you add the event, or not.  It is
    set in the 'what' argument to the callback function when a timeout
    has occurred.
    EV_READ

    This flag indicates an event that becomes active when the provided file descriptor is ready for reading.

    EV_WRITE

    This flag indicates an event that becomes active when the provided file descriptor is ready for writing.

    EV_SIGNAL

    Used to implement signal detection. See "Constructing signal events" below.

    EV_PERSIST

    Indicates that the event is persistent. See "About Event Persistence" below.

    EV_ET

    Indicates that the event should be edge-triggered, if the underlying event_base backend supports edge-triggered events. This affects the semantics of EV_READ and EV_WRITE.

    若想将event当作参数传给回调函数,则可用使用event_self_cbarg函数(在2.1.1版本才引入)

    #include <event2/event.h>
    
    static int n_calls = 0;
    
    void cb_func(evutil_socket_t fd, short what, void *arg)
    {
        struct event *me = arg;
    
        printf("cb_func called %d times so far.
    ", ++n_calls);
    
        if (n_calls > 100)
           event_del(me);
    }
    
    void run(struct event_base *base)
    {
        struct timeval one_sec = { 1, 0 };
        struct event *ev;
        /* We're going to set up a repeating timer to get called called 100
           times. */
        ev = event_new(base, -1, EV_PERSIST, cb_func, event_self_cbarg());
        event_add(ev, &one_sec);
        event_base_dispatch(base);
    }

    reference: http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html

  • 相关阅读:
    九省联考2018 解题报告
    「PKUSC2018」最大前缀和(状压dp)
    「雅礼集训 2017 Day2」解题报告
    UVA10829 L-Gap Substrings(后缀数组+ST表)
    [BZOJ2738]矩阵乘法(整体二分+二维树状数组)
    「雅礼集训 2017 Day1」 解题报告
    LeetCode 190. Reverse Bits (算32次即可)
    LeetCode 437. Path Sum III (STL map前缀和)
    LeetCode 744. Find Smallest Letter Greater Than Target (时间复杂度O(n))
    LeetCode 1. Two Sum (c++ stl map)
  • 原文地址:https://www.cnblogs.com/elaron/p/5703334.html
Copyright © 2020-2023  润新知