On Fri, 11 Oct 2013 19:34:50 +0200, Sönke Ludwig wrote:

Am 11.10.2013 14:27, schrieb Matthew Fong:

I'm currently writing a socks server. It works well, except the proxying itself. I'm currently using the following as a way to test the protocol stuff works (it does), but it obviously doesnt scale:

while (con.connected && sockscon.connected)
{
	try
	{
		while (con.dataAvailableForRead)
		{
			sockscon.write(con, con.leastSize);
			yield();
		}
		while (sockscon.dataAvailableForRead)
		{
			con.write(sockscon, sockscon.leastSize);
			yield();
		}
		yield();
		sleep(1.msecs);
	}
	catch
	{
		break;
	}						
}

How would I go about efficiently proxying one tcp connection into another?

The Probably the best way is to use different tasks to handle the read
and write directions:

void proxyConnection(TCPConnection con, TCPConnection sockscon)
{
	scope (exit) {
		// make sure that everything gets closed in case of
		// some exception that doesn't result in a disconnect
		if (con.connected) con.close();
		if (sockscon.connected) sockscon.close();
	}

	auto t = runTask({
		try sockscon.write(con); catch {}
	});

	try con.write(sockscon); catch {}
	t.join(); // wait for the task to finish
}

Thanks! I tried it and it works well. I didn't try it before because I thought accessing the same TCPConnection from different tasks wouldn't be a good idea.