当前位置:网站首页>Brief introduction to libevent

Brief introduction to libevent

2022-07-06 15:15:00 Work hard to become a c++ Engineer

libevent library
        Open source . Streamlining . Cross platform (Windows、Linux、Maxos、unix). Focus on network communication .
Source package installation :
        ./configure                    Check the installation environment   Generate makefile
        make                                 Generate .o And executables
        sudo  make  install        The necessary resources cp To the specified directory of the system .
        Get into sample Catalog , function demo Verify library installation and usage .
        Compile using the library .c when , Need to add -levent Options .
        Library name libevent.so--> /usr/local/lib    Check it out .
characteristic :
        be based on “ event ” Asynchronous communication model .--- Callback .
libevent frame :
      1.  gen build event_base        ( LEGO base )
                struct event_base *event_base_new(void);
                struct event_base *event *base=event_base_new();
        2.  establish   things Pieces of event
                    Routine events event    -->event_new();
                    bufferevent -->bufferevnet_socket_new();
         3. Put the event   Add to base On
                    int event_add(struct event *ev,const struct timeval *tv);
         4. Loop listening events meet
                    int event_base_dispatch(struct event_base *base);
                                event_base_dispatch(base);
       5. Release event_base
                    event_base_free(base);
Create an event :
struct event *ev;
struct event *evennt_new(struct event_base *base,evutil_socket_t fd,short what,event_callback_fn cb;void *arg);
        base:event_base_new() Return value .
        fd : Bound to the event Upper   File descriptor .
        what: The corresponding event (r、w、e)
                EV_READ             once    Read events 
                EV_WRTIE            once   Write events 
                EV_PERSIST         Continue to trigger . combination event_base_dispatch Function USES , take effect .
        cb: Once the event meets the listening conditions , Function of callback .
        typedef void(*event_callback_fn)(evutil_socket_t fd, short,void *)
        arg: The parameters of the callback function .
         Return value : Successfully created  event

Add events to event_base
int event_add(struct event *ev,const struct timeval *tv);
     ev: event_new() The return value of .
     tv:NULL

from event_base Take off the event                 【 understand 】
int event_del(struct event *ev);
    ev:event_new() The return value of .

Destruction event :       
int event_free(struct event *ev);
    ev:event_new() The return value of .

Pending and non pending :
        Non pending : Not eligible to be dealt with
        outstanding : Qualified to be dealt with , But it hasn't been processed yet
        event_new--->event----> Non pending --->event_add---> outstanding --->dispatch()&& The listening event is triggered --> Activation state
                ——> Execute callback function ---> Processing state ---> Non pending event_add && EV_PERSIST----> outstanding --->event_del---> Non pending
Events with buffers bufferevent
       
 #include<event2/buffereevent.h>
establish 、 The destruction bufferevent 
  
struct bufferent *ev;
struct bufferevent *bufferevent_socket_new(struct event_base *base,evutil_socket_t fd,enum bufferevent_options options);
        base:    event_base
        fd:     Package to bufferevent Internal fd
        options:    BEV_OPT_CLOSE_ON_FREE
 return : Successfully created bufferevent Event object .
void bufferevent_socket_free(struct bufferevent *ev);
to bufferevent Set callback :
 contrast event:event_new(fd,callback);            event_add() ---  Hang up event_base On .
                    bufferevent_socket_new(fd)        bufferevent_setcb(callback)

void bufferevent_setcb(struct bufferevent*bufev, bufferevent_data_cb reacb, bufferevent_data_cb writecb, bufferevent_data_cb eventcb , void *cbarg );

bufev:bufferevent_socket_new() Return value 
readcb: Set up bufferevent  Degree buffer , Corresponding callback  read_cb{     bufferevent_read()   Reading data    }
writecb: Set up bufferevent  Write buffer , Corresponding callback write_cb{    } --- To the caller , Send write success notification .     Sure NULL

eventcb: Set event callback .     Also can be transmitted NULL
            typedef void(*bufferevent_data_cb)(struct bufferevent *bev,void*ctx);
            void event_cb(struct bufferevent *bev, short events, void *cbarg)
            {
                .....
            }
cbarg: The parameters used by the above callback function .
read     Callback function type :
            typedef void(*bufferevent_data_cb)(struct bufferevent *bev,void*ctx);
            void read_cb(struct bufferevent *bev,void *cbarg)
            {
                .....
                bufferevent_read();    ---read()
            }
bufferevent_read() Prototypes of functions :
            size_t bufferevent_read(struct bufferevent *bev, void *buf, size_t bufsize);

write  Callback function type :
            int bufferevent_write(struct bufferevent *bufev, const void*data,size_t size);
start-up 、 close bufferevent Of   buffer :
 void bufferevent_enable(struct bufferevent *bufev, short events);     start-up 
      events:    EV_READ、EV_WRITE、EV_READ|EV_WRITE
       Default 、write  buffer  enable、read The buffer is disable
      bufferent_enable(event,EV_READ);            -- Turn on the read buffer 

Create a listening server :
socket();bind();listen();accept();
struct evconnlistener *listner
struct evconnlistener *evconnlistener_new_bind(
    struct event_base *base,
    evconnlistener_cb cb,
    void *ptr,
    unsigned flags,
    int backlog,
    const struct sockaddr *sa,
    int socklen);

base:event_base
cb: Callback function .     Once called back , Explain that it should be completed internally with the client , Data reading and writing operations , communicate .
ptr: The parameters of the callback function 
flags:LEV_OPT_CLOSE_ON_FREE |  LEV_OPT_REUSEABLE
backlog:listen() 2 ginseng .    -1 Table max 
sa: The server's own address structure 
socklen: The size of the server's own address structure .
 Return value : Successfully created listener .
Connect to the client :
socket();    connect();
int bufferevent_socket_connect(struct bufferevent *bev, struct sockaddr *address, int addrlen);
        bev:bufferevent  Event object ( Encapsulates the fd)
        address、len: Equate to connect() ginseng 2/3

Server side libevent establish TCP Connect :
  • 1. establish event_base
  • 2. establish bufferevent Event object .bufferevent_socket_new();
  • 3. Use bufferevent_setcb() Function gives bufferevent Of read、write、event Set the callback function .
  • 4. When the monitored event is satisfied ,read_cb Will be called , Inside it bufferevent_read();    read
  • 5. Use evconnlistener_new_bind Create a listening server , Set its callback function , When a client successfully connects , This callback function will be called .
  • 6. encapsulation listner_cb() Inside the function . Complete communication with the client .
  • 7. Set the read buffer 、 Write buffered   Enable state enable、disable
  • 8. Start cycle event_base_dispath();
  • 9. Release the connection
client libevent establish TCP Connect :
  • 1. establish event_base
  • 2. Use bufferevent_socket_new() Create a that communicates with the tracking server bufferevnet Event object
  • 3. Use bufferevent_socket_connect() Connect   The server
  • 4. Use bufferevent_setcb() to bufferevnet Object's read、write、event Set callback
  • 5. Set up bufferenet Object's read / write buffer enable/disable
  • 7. Start cycle event_base_dispath();
  • 8. Release the connection

web——HTTP Request protocol :
  • 1.getline() obtain http The first line of the agreement .
  • 2. Split... From the first line     GET、 file name 、 Protocol version . Get the file name requested by the user .
  • 3. Judge whether the file exists .stat()
  • 4. Determine file or directory .
  • 5. It's a document --open--read -- Write to the browser
  • 6. First write http Reply protocol header :http/1.1 200 ok
  • 7. Write file data .

 

原网站

版权声明
本文为[Work hard to become a c++ Engineer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131321385708.html