Skip to content

Instantly share code, notes, and snippets.

@gkres
Created December 11, 2012 14:11
Show Gist options
  • Save gkres/4258844 to your computer and use it in GitHub Desktop.
Save gkres/4258844 to your computer and use it in GitHub Desktop.
evhttpclient
#include <iostream>
#include <map>
#include <ev.h>
#include <evhttpclient.h>
using namespace std;
void response_cb(ResponseInfo *response, void *requestData, void *clientData)
{
if(response == NULL)
{
cout << "Error." << endl;
}
if(response->timeout)
{
cout << "Timeout." << endl << endl;
}
cout << "Got response" << endl;
}
void timer_cb(struct ev_loop *loop, struct ev_timer *timer, int revents)
{
cout << "Making GET." << endl;
EvHttpClient *client = (EvHttpClient *) timer->data;
int get = client->makeGet(response_cb, "", map<string, string>(), NULL);
if(get < 0)
{
cout << "Error making request." << endl;
}
}
void timer_cb2(struct ev_loop *loop, struct ev_timer *timer, int revents)
{
EvHttpClient *client = (EvHttpClient *) timer->data;
delete(client);
cout << "Deleted EvHttpClient instance." << endl;
}
int main()
{
struct ev_loop *loop = ev_default_loop(0);
EvHttpClient *client = new EvHttpClient(loop, "http://www.greenhondacivicsunite.com/", 10, NULL,1);
struct ev_timer timer, timer2;
timer.data = (void *) client;
ev_timer_init(&timer, timer_cb, 1, 5);
ev_timer_start(loop, &timer);
// Set up a second timer to delete the instance
timer2.data = (void *) client;
ev_timer_init(&timer2, timer_cb2, 3, 0);
ev_timer_start(loop, &timer2);
ev_loop(loop, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment