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

Re: [Libevent-users] Send big file with libevent http module



Hi everyone,

I read the documentation about evbuffer_add_file_segment() and
evbuffer_file_segment_new(). I was septic at start because I didn't
know the meaning of these functions. After looking at the code,
I understood them. Actually, it was exactly what I need.

I am pasting the change I made. Maybe it will be useful for others
developers.

So, I change my bad copy/paste loop:

---------------------------
while(1)
{
   src_read = read(fd, srcbuf, sizeof(srcbuf));
   if (src_read < 0)
   {
       log_err("read() failed");
       ret = -1;
       goto close_file;
   }
   else if (src_read == 0)
   {
       /* end */
       break;
   }

   if (evbuffer_add(msg->body, srcbuf, src_read) != 0)
   {
       log_err("evbuffer_add() failed");
       ret = -1;
       goto close_file;
   }
}
---------------------------

by something like this:

---------------------------
fd = open(filename, O_RDONLY);
if (fd == -1)
{
   log_err("open(%s) failed: %m", filename);
   return -1;
}

seg = evbuffer_file_segment_new(fd, 0, -1, EVBUF_FS_CLOSE_ON_FREE);
if (seg == NULL) {
   log_err("evbuffer_file_segment_new() failed");
   close(fd);
   return -1;
}

if (evbuffer_add_file_segment(msg->body, seg, 0, -1) != 0)
{
   log_err("evbuffer_add_file_segment() failed");
   ret = -1;
   goto ex_cleanup;
}

ret = 0;

ex_cleanup:

if (seg != NULL)
   evbuffer_file_segment_free(seg);

return ret;
---------------------------

That's all !

Thanks for the help guys.

Regards,
Anthony.

On Wed, 1 Feb 2017 00:57:26 +0300
Azat Khuzhin <a3at.mail@xxxxxxxxx> wrote:

> 
> To make sendfile() works in libevent you should use draining to fd[1]
> since you need encode the file (or add headers). So indeed you can
> read file by chunks and add it to output buffer
> (evbuffer_add_file_segment()) if it's length is not greater them 100KB
> (or smth like this)
> 
> [1] https://github.com/libevent/libevent/blob/ffbce578c40a06491ce6585ab2d82ebb69a52d30/buffer.c#L3191
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.