Ibevent using the sample timer under Windows

Source: Internet
Author: User


One main loop each server has a main loop, and in the process of starting the main loop, the following functions are used:
Create successfully returns an event with default settings base//Create failed return nullstruct event_base *event_base_new (void);//event_base release void Event_base_free (struct event_base *base);//Specify a event_base and start the event loop//This function is implemented internally as an ongoing loop//This function returns 0 for successful exit//This function returns 1 indicates an unhandled error exists int Event_ Base_dispatch (struct event_base *base);

The code examples are as follows:

int main () {//creates event base for default settings, failed to return nullstruct event_base* base = Event_base_new ();//Turn On time loop Event_base_dispatch (base) ;//Release Event_base_free (base); return 0;}


The two-register event libevent is event-driven and now requires registering events to the main loop.


1 Event Types
Timeout # define Ev_timeout      0x01//Event related file descriptor can read # define Ev_read         0x02//Event related file descriptor can write a # define Ev_write        0x04//is used for signal detection # define ev_signal       0x08//used to specify event as Persistent#define ev_persist      0x10//for specifying event Will be triggered by the edge (Edge-triggered can refer to http://name5566.com/3818.html) #define Ev_et  

2 callback function type
Event callback function//parameter evutil_socket_t---event associated file descriptor//parameter short---the current condition (i.e. the condition defined above)//parameter void*---it as the event_new function ARG parameter typedef void (*EVENT_CALLBACK_FN) (evutil_socket_t, short, void *);

3 Event-related actions
Create event//Base---Use this event event_base//what---Specify the various conditions that the event cares about (that is, the conditions defined above)//FD---File descriptor//CB---Event-related callback letter Number//ARG---User-defined data//function execution failure returns NULLSTRUCT event *event_new (struct event_base *base, evutil_socket_t FD, short    -What, Ev ENT_CALLBACK_FN CB,    void *arg); Release event (really frees memory, corresponds to event_new use)//can be used to release the event//assigned by Event_new If the event is in pending or the active state is released there will be no problem void Event_fre E (struct event *event);  Cleanup event (not really free memory)//can be used for already initialized, pending, active event//This function will convert event to non-pending, non-active state of//function return 0 for success-1 for failed int Event_del (struct event *event);

4 Event Registration
Used to register event//TV with Event_base to specify a time-out, null to indicate no timeout time//function returned 0 for success-1 for failure int Event_add (struct event *ev, const struct Timeval *TV);

The code examples are as follows:
Event callback function void CallBack (evutil_socket_t fd, short ev, void *args) {printf ("%s\n", (char*) args);} int main () {///creates event base for default settings, failed to return nullstruct event_base* base = Event_base_new ();//Create incident struct event* ev = Event_new (bas E, NULL, Ev_timeout, CallBack, (char*) "TIMEOUT");//Register Event const struct Timeval outtime = {5, 0};event_add (EV, &outtime); Turn on time loop Event_base_dispatch (base);//release Event_base_free (base); return 0;}


Three other configurations and optimizations need to initialize the network under Win32, code examples are as follows:
Event callback function void CallBack (evutil_socket_t fd, short ev, void *args) {printf ("%s\n", (char*) args);} int main () {//initialize network word wversionrequested = Makeword (2, 2);        Wsadata Wsadata;            WSAStartup (wversionrequested, &wsadata);//create event base with default settings, fail back nullstruct event_base* base = Event_base_new ();  /create event struct event* ev = event_new (base, NULL, Ev_timeout, CallBack, (char*) "TIMEOUT");//Register Event const struct Timeval outtime = {5, 0};event_add (EV, &outtime);//Open time loop event_base_dispatch (base);//release Event_base_free (base); return 0;}

Optimize the code structure and the final code is as follows:
#include <stdlib.h> #include <stdio.h> #include <Winsock2.h> #include <event2/event.h># Include <event2/util.h>//initialize network void Initnetwork () {WORD wversionrequested = Makeword (2, 2);    Wsadata Wsadata;        


Ibevent using the sample timer under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.