Here is my main code:
class SockServer
{
void listen()
{
listenTCP(9991, (conn){ handleConn(conn); });
}
void handleConn(TCPConnection client)
{
scope(exit) client.close();
ubyte[] pack = NetUtil.readPacket(client);
Cmd cmd = NetUtil.deserialObj!(Cmd)(pack);
NetUtil.printObj(cmd);
if(cmd.cmdId==Cmds.Connect)
{
handleConnect(client,cmd);
}
}
void handleConnect(TCPConnection client,ref Cmd cmd)
{
auto remote = connectTCP(cmd.connect.ip,cast(ushort)cmd.connect.port);
if(remote is null) return;
scope(exit)remote.close();
forward(client,remote);
}
static void forward(TCPConnection client,TCPConnection remote)
{
auto wtask = runTask({
remote.write(client);
});
client.write(remote);
// wait for the tasks to finish
wtask.join();
}
}