On Sun, 25 Nov 2012 16:24:33 GMT, Martin Drasar wrote:

One more thing to add - responses from server (i.e. data from netcat were already written to and read from the stream) with flush set to true go through immediately.
So the problem is only with client initiating the request/response round.

I'm not sure what libevent exactly does internally, but the cause turns out to be the blocking stdin.readln() call. The following version of the client correctly sends the two strings one by one. However, replacing the sleep() with foreach(i; 0 .. 1_000_000_000){} will case both strings to be sent at once after a delay. Looks like an asynchronous way to access stdin needs to be added to vibe.

import vibe.vibe;

void main()
{
	auto conn = connectTcp("127.0.0.1", 1234);
	string[] lines = ["hello", "world"];
	while(lines.length > 0){
		conn.write(lines[0], true);
		lines = lines[1 .. $];
		sleep(dur!"seconds"(5));
	}
	conn.close();
}

If you need to read from stdin, you could do something like this for now and read in a separate thread:

import vibe.vibe;
import core.sync.mutex;
import core.thread;
import std.stdio;

__gshared core.sync.mutex.Mutex linesMutex;
__gshared string[] lines;

void stdinReader()
{
	while(true){
		auto str = stdin.readln();
		synchronized(linesMutex) lines ~= str;
	}
}

void main()
{
	linesMutex = new core.sync.mutex.Mutex;

	(new Thread(&stdinReader)).start();

	auto conn = connectTcp("127.0.0.1", 1234);
	while(true){
		string line;
		while(true){
			synchronized(linesMutex){
				if( lines.length ){
					line = lines[0];
					lines = lines[1 .. $];
					break;
				}
			}
			sleep(dur!"msecs"(10));
		}

		conn.write(line);
	}
}

Not pretty because of the spinning wait, but should work.