RejectedSoftware Forums

Sign up

Making a tcp proxy using vibed

Hi,

Anyone know what I'm doing wrong here?

This program crashes if the client closes the connection. Error message is:
Task terminated with unhandled exception: Trying to acquire a TCP connection that is currently owned.

import vibe.d;

shared static this() {

listenTCP(2500, (conn) {
	auto remoteconn = connectTCP("localhost", 2525);

	runTask(() {
		remoteconn.write(conn);
	});

	conn.write(remoteconn);
});

}

Re: Making a tcp proxy using vibed

A TCPConnection is a task-owned resource, if you need to use the resource you'll have to do task-messaging which requires some stream utilities (see vibe.stream.*). For this purpose, I think a task pipe would be what you're looking for.

The way you'd use it isn't too far off from what you had in mind, I think it should end up being something like this (untested) solution:

import vibe.d;

shared static this() {
	listenTCP(2500, (conn) {
		auto pipe = new TaskPipe;
		
		runTask({ 
			
			auto remoteconn = connectTCP("localhost", 2525);
		
			pipe.write(remoteconn);
			
		});

		conn.write(pipe);
		
	});
}

Re: Making a tcp proxy using vibed

On Wed, 23 Jul 2014 20:54:04 GMT, Etienne Cimon wrote:

A TCPConnection is a task-owned resource, if you need to use the resource you'll have to do task-messaging which requires some stream utilities (see vibe.stream.*). For this purpose, I think a task pipe would be what you're looking for.

Thanks, that almost works, now I just need that copying in the other direction too. Perhaps you can tell whats wrong with my original code. Its something I'd normally use Go or my own event based io in D but I'm trying to use vibed.
`
runTask(() {

ubyte[8192] buf;
logInfo("We're here");
while (true) {
	yield();
	try {
		conn.read(buf);
	} catch (Exception e) {
		logWarn("Source connection dropped read! ", e);
		break;
	}
	try {
		remoteconn.write(buf);
	} catch (Exception e) {
		logWarn("Sink connection dropped write! ", e);
		break;
	}
}

});
ubyte[8192] buf;
while (true) {

try {
	remoteconn.read(buf);
} catch (Exception e) {
	logWarn("Sink connection dropped read! ", e);
	break;
}
try {
	conn.write(buf);
} catch (Exception e) {
	logWarn("Source connection dropped write! ", e);
	break;
}

}
`

Re: Making a tcp proxy using vibed

On Wed, 23 Jul 2014 20:00:13 GMT, Rory wrote:

Hi,

Anyone know what I'm doing wrong here?

This program crashes if the client closes the connection. Error message is:
Task terminated with unhandled exception: Trying to acquire a TCP connection that is currently owned.

import vibe.d;

shared static this() {

listenTCP(2500, (conn) {
	auto remoteconn = connectTCP("localhost", 2525);

	runTask(() {
		remoteconn.write(conn);
	});

	conn.write(remoteconn);
});

}

That is actually supposed to work like that, AFAICS. Do you have a call stack for the assertion? Otherwise I'll write a quick reproduction case to see what goes wrong.