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) { Logging.Error("decryption error"); Close(); return; } } if (bytesToSend == 0) { // need more to decrypt Logging.Debug("Need more to decrypt"); session.Remote.BeginReceive(_remoteRecvBuffer, 0, RecvSize, SocketFlags.None, PipeRemoteReceiveCallback, session); return; } Logging.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) { Logging.LogUsefulException(e); Close(); } }
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; lock (_decryptionLock) { _encryptor.Decrypt(RemoteRecvBuffer, bytesRead, _remoteSendBuffer, out bytesToSend); } Connection.BeginSend(_remoteSendBuffer, 0, bytesToSend, SocketFlags.None, PipeConnectionSendCallback, session); IStrategy strategy = _controller.GetCurrentStrategy(); strategy?.UpdateLastRead(_server); } else { Connection.Shutdown(SocketShutdown.Send); _connectionShutdown = true; CheckClose(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } }