private void OnRequested(IAsyncResult r)
{
try {
if (_closed)
return; //SSH切断があったときは非同期受信から戻ってくるが、EndAcceptを呼んでもObjectDisposedExceptionになるだけ
Socket local = _bindedLocalSocket.EndAccept(r);
//Port Forwarding
Channel newChannel;
lock (_connection) {
newChannel = _connection.ForwardPort(
channel => {
return new Channel(_profile.SSHHost, local.RemoteEndPoint.ToString(), _id, channel, local);
},
_profile.DestinationHost,
_profile.DestinationPort,
"localhost", // FIXME
0 // FIXME
);
var isReady = newChannel.WaitReady();
if (!isReady) {
// failed to port forwarding
Debug.WriteLine("failed to port forwarding");
// the accepted socket will already be closed (at the time of the channel's close)
return;
}
newChannel.StartAsyncReceive();
}
}
catch (Exception ex) {
if (!_closed) {
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
Util.InterThreadWarning(ex.Message);
}
}
finally {
if (!_closed) {
_bindedLocalSocket.BeginAccept(new AsyncCallback(OnRequested), null);
Debug.WriteLine("BeginAccept again");
}
}
}