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

[Libevent-users] Libevent does not echo properly when there is a delay



I am trying to figure out if there is a trick to sending data on a socket using libevent.

Based on the following code, I built a version of an echo server, but with a threaded delay. (the original simply writes back to libevent and works fine). My real-world use case is that I need to send messages to the server, do a lot of processing, and then send the result back... say 10-30 seconds later (could be hours in some cases). Basically, I am trying to replace socket-send with the libevent equivalent and socket-send works fine so I am not sure if this is necessary.

http://www.wangafu.net/~nickm/libevent-book/Ref8_listener.html

So here is my code. For brevity's sake, I have only included the libevent-related code; not the threading code or other stuff. When debugging, a new connection is set up, the string buffer is filled properly, and debugging reveals that the writes go successfully.

http://pastebin.com/g02S2RTi

But I only receive the echo from the send-before-last. I send from the client numbers to validate this and when I send a 1 from the client, I receive nothing from the server via echo... even though the server is definitely writing to the buffer using evbuffer_add ( I have also tried this using bufferevent_write_buffer).

From the client when I send a 2, I then receive the 1 from the previous send. It's like my writes are being cached.... I have turned off nagle.

So, my question is: Does libevent cache sends using the following method?

evbuffer_add( outputBuffer, buffer, length );

Is there a way to flush this cache? Is there some other method to mark the cache as finished or complete? Can I force a send? It never sends on it's own... I have even put in delays. Replacing evbuffer_add with "send" works perfectly every time.