public void Start([NotNull] Lazy <IPEndPoint> localFunc, [NotNull] Lazy <IPEndPoint> remoteFunc) { var remote = remoteFunc.Value; var local = localFunc.Value; try { MainSocket.Bind(local); MainSocket.Listen(10); while (true) { // Console.WriteLine("{0} : Starting forwarding with new tunnel", remote.Address); var source = MainSocket.Accept(); var destination = new TcpForwarderSlim(); var state = new State(source, destination.MainSocket); destination.Connect(remote, source); source.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state); // Console.WriteLine("{0} : Request Forwarded", remote.Address); } } catch (SocketException) { // Console.WriteLine("{0} : Closing tunnel", remote.Address); } }
public PortForwardingWrapper([NotNull] Lazy <IPEndPoint> localFunc, [NotNull] Lazy <IPEndPoint> remoteFunc) { _cancellationToken = new CancellationTokenSource(); _forwarder = new TcpForwarderSlim(); Task.Factory.StartNew(_ => _forwarder.Start(localFunc, remoteFunc), _cancellationToken); }