Line data Source code
1 : #include "source/common/network/raw_buffer_socket.h" 2 : 3 : #include "source/common/api/os_sys_calls_impl.h" 4 : #include "source/common/common/assert.h" 5 : #include "source/common/common/empty_string.h" 6 : #include "source/common/http/headers.h" 7 : 8 : namespace Envoy { 9 : namespace Network { 10 : 11 2370 : void RawBufferSocket::setTransportSocketCallbacks(TransportSocketCallbacks& callbacks) { 12 2370 : ASSERT(!callbacks_); 13 2370 : callbacks_ = &callbacks; 14 2370 : } 15 : 16 2376 : IoResult RawBufferSocket::doRead(Buffer::Instance& buffer) { 17 2376 : PostIoAction action = PostIoAction::KeepOpen; 18 2376 : uint64_t bytes_read = 0; 19 2376 : bool end_stream = false; 20 2376 : absl::optional<Api::IoError::IoErrorCode> err = absl::nullopt; 21 4370 : do { 22 4370 : Api::IoCallUint64Result result = callbacks_->ioHandle().read(buffer, absl::nullopt); 23 : 24 4370 : if (result.ok()) { 25 2865 : ENVOY_CONN_LOG(trace, "read returns: {}", callbacks_->connection(), result.return_value_); 26 2865 : if (result.return_value_ == 0) { 27 : // Remote close. 28 872 : end_stream = true; 29 872 : break; 30 872 : } 31 1993 : bytes_read += result.return_value_; 32 1993 : if (callbacks_->shouldDrainReadBuffer()) { 33 0 : callbacks_->setTransportSocketIsReadable(); 34 0 : break; 35 0 : } 36 1993 : } else { 37 : // Remote error (might be no data). 38 1505 : ENVOY_CONN_LOG(trace, "read error: {}, code: {}", callbacks_->connection(), 39 1505 : result.err_->getErrorDetails(), static_cast<int>(result.err_->getErrorCode())); 40 1505 : if (result.err_->getErrorCode() != Api::IoError::IoErrorCode::Again) { 41 24 : action = PostIoAction::Close; 42 24 : err = result.err_->getErrorCode(); 43 24 : } 44 1505 : break; 45 1505 : } 46 4370 : } while (true); 47 : 48 0 : return {action, bytes_read, end_stream, err}; 49 2376 : } 50 : 51 7953 : IoResult RawBufferSocket::doWrite(Buffer::Instance& buffer, bool end_stream) { 52 7953 : PostIoAction action; 53 7953 : uint64_t bytes_written = 0; 54 7953 : absl::optional<Api::IoError::IoErrorCode> err = absl::nullopt; 55 7953 : ASSERT(!shutdown_ || buffer.length() == 0); 56 12148 : do { 57 12148 : if (buffer.length() == 0) { 58 7885 : if (end_stream && !shutdown_) { 59 : // Ignore the result. This can only fail if the connection failed. In that case, the 60 : // error will be detected on the next read, and dealt with appropriately. 61 252 : callbacks_->ioHandle().shutdown(ENVOY_SHUT_WR); 62 252 : shutdown_ = true; 63 252 : } 64 7885 : action = PostIoAction::KeepOpen; 65 7885 : break; 66 7885 : } 67 4263 : Api::IoCallUint64Result result = callbacks_->ioHandle().write(buffer); 68 : 69 4263 : if (result.ok()) { 70 4196 : ENVOY_CONN_LOG(trace, "write returns: {}", callbacks_->connection(), result.return_value_); 71 4196 : bytes_written += result.return_value_; 72 4196 : } else { 73 67 : ENVOY_CONN_LOG(trace, "write error: {}, code: {}", callbacks_->connection(), 74 67 : result.err_->getErrorDetails(), static_cast<int>(result.err_->getErrorCode())); 75 68 : if (result.err_->getErrorCode() == Api::IoError::IoErrorCode::Again) { 76 4 : action = PostIoAction::KeepOpen; 77 65 : } else { 78 64 : err = result.err_->getErrorCode(); 79 64 : action = PostIoAction::Close; 80 64 : } 81 67 : break; 82 67 : } 83 4263 : } while (true); 84 : 85 0 : return {action, bytes_written, false, err}; 86 7953 : } 87 : 88 98 : std::string RawBufferSocket::protocol() const { return EMPTY_STRING; } 89 1380 : absl::string_view RawBufferSocket::failureReason() const { return EMPTY_STRING; } 90 : 91 1954 : void RawBufferSocket::onConnected() { callbacks_->raiseEvent(ConnectionEvent::Connected); } 92 : 93 : TransportSocketPtr 94 : RawBufferSocketFactory::createTransportSocket(TransportSocketOptionsConstSharedPtr, 95 242 : Upstream::HostDescriptionConstSharedPtr) const { 96 242 : return std::make_unique<RawBufferSocket>(); 97 242 : } 98 : 99 1042 : TransportSocketPtr RawBufferSocketFactory::createDownstreamTransportSocket() const { 100 1042 : return std::make_unique<RawBufferSocket>(); 101 1042 : } 102 : 103 58 : bool RawBufferSocketFactory::implementsSecureTransport() const { return false; } 104 : 105 : } // namespace Network 106 : } // namespace Envoy