private void PipeRemoteReceiveCallback(IAsyncResult ar) { if (_closed) { return; } try { var session = (AsyncSession)ar.AsyncState; int bytesRead = session.Remote.EndReceive(ar); _totalRead += bytesRead; _tcprelay.UpdateInboundCounter(_server, bytesRead); if (bytesRead > 0) { lastActivity = DateTime.Now; int bytesToSend = -1; lock (_decryptionLock) { try { _encryptor.Decrypt(_remoteRecvBuffer, bytesRead, _remoteSendBuffer, out bytesToSend); } catch (CryptoErrorException) { Logger.Error("decryption error"); Close(); return; } } if (bytesToSend == 0) { // need more to decrypt Logger.Debug("Need more to decrypt"); session.Remote.BeginReceive(_remoteRecvBuffer, 0, RecvSize, SocketFlags.None, PipeRemoteReceiveCallback, session); return; } Logger.Debug($"start sending {bytesToSend}"); _connection.BeginSend(_remoteSendBuffer, 0, bytesToSend, SocketFlags.None, PipeConnectionSendCallback, new object[] { session, bytesToSend }); IStrategy strategy = _controller.GetCurrentStrategy(); strategy?.UpdateLastRead(_server); } else { _connection.Shutdown(SocketShutdown.Send); _connectionShutdown = true; CheckClose(); } } catch (Exception e) { Logger.LogUsefulException(e); Close(); } }
private void PipeRemoteReceiveCallback(IAsyncResult ar) { if (closed) { return; } try { int bytesRead = remote.EndReceive(ar); totalRead += bytesRead; tcprelay.UpdateInboundCounter(bytesRead); if (bytesRead > 0) { lastActivity = DateTime.Now; int bytesToSend; lock (decryptionLock) { if (closed) { return; } encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend); } Logging.Debug(remote, bytesToSend, "TCP Relay", "@PipeRemoteReceiveCallback() (download)"); connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null); IStrategy strategy = controller.GetCurrentStrategy(); if (strategy != null) { strategy.UpdateLastRead(server); } } else { connection.Shutdown(SocketShutdown.Send); connectionShutdown = true; CheckClose(); //if (totalRead == 0) //{ // // closed before anything received, reports as failure // // disable this feature // controller.GetCurrentStrategy().SetFailure(this.server); //} } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } }
private void PipeRemoteReceiveCallback(IAsyncResult ar) { if (_closed) { return; } try { if (remote == null) { return; } int bytesRead = remote.EndReceive(ar); _totalRead += bytesRead; _tcprelay.UpdateInboundCounter(server, bytesRead); if (bytesRead > 0) { lastActivity = DateTime.Now; int bytesToSend; lock (_decryptionLock) { if (_closed) { return; } encryptor.Decrypt(_remoteRecvBuffer, bytesRead, _remoteSendBuffer, out bytesToSend); } connection.BeginSend(_remoteSendBuffer, 0, bytesToSend, SocketFlags.None, new AsyncCallback(PipeConnectionSendCallback), null); IStrategy strategy = controller.GetCurrentStrategy(); strategy?.UpdateLastRead(server); } else { connection.Shutdown(SocketShutdown.Send); _connectionShutdown = true; CheckClose(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } }