[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] Notification when client disconnects during http-request



Hi,

I'm building a HTTP-server to which clients can do requests but it is
not certain what they request for are available at the time they are
asking. Hence client requests may have to idle until there is data to
send as reply to the request, i.e. long-polling.

Is there some way to get notified if a client disconnects during this
idle period?

I've added some example code below using a timeout to simulate the
arrival of data. I've tried to add a callback that should(?) be called
when a connection closes but it is not called until the timeout occurs
and a reply is tried to be sent to that request.

Running the application below, connecting using for instance wget and
terminate the wget request within the 10 seconds interval, yields the
result below:
>server started...
>new request! start 10 sec reply timeout...
>timeout! send reply...
>connection closed...

But what I'm trying to achieve is something more like:
>server started...
>new request! start 10 sec reply timeout...
>connection closed...

I've browsed the source code (using 2.0.2-alpha) and as far as I can
see it looks like the `evcon' isn't used internally by the http
implementation? Am I doing something wrong? Do I need tweak any socket
options, such as keep alive probes?

I've been using event-buffers before and I have always been able to
detect when the "other side" disconnects.

many thanks,
Jonas Romfelt
______________________________________________________

#include <time.h>
#include <event.h>
#include <event2/http.h>
#include <event2/http_struct.h>

struct event timeout;
struct event_base *base;
struct evhttp *server;

void on_close(struct evhttp_connection *c, void *p)
{
  printf("connection closed...\n");
}

void on_timeout(int fd, short ev, void *arg)
{
  struct evhttp_request *req = (struct evhttp_request *)arg;
  printf("timeout! send reply...\n");
  evhttp_send_reply(req, HTTP_OK, "OK", NULL);
}

static void _gencb(struct evhttp_request *req, void *arg)
{
  struct timeval tv;

  printf("new request! start 10 sec reply timeout...\n");
  tv.tv_usec = 0;
  tv.tv_sec = 10;
  event_assign(&timeout, base, -1, 0, on_timeout, req);
  event_add(&timeout, &tv);

  //  evhttp_request_own(req);  // do we have to request ownership?
  evhttp_connection_set_closecb(req->evcon, on_close, NULL);
}

int main(int argc, char **argv)
{
  base = event_base_new();
  server = evhttp_new(base);
  evhttp_bind_socket(server, "0.0.0.0", 8888);
  evhttp_set_gencb(server, _gencb, NULL);
  printf("server started...\n");
  event_base_dispatch(base);
  return 0;
}
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.