proxygen
BlockingSocket Class Reference

#include <BlockingSocket.h>

Inheritance diagram for BlockingSocket:
folly::AsyncSocket::ConnectCallback folly::AsyncReader::ReadCallback folly::AsyncWriter::WriteCallback

Public Member Functions

 BlockingSocket (int fd)
 
 BlockingSocket (folly::SocketAddress address, std::shared_ptr< folly::SSLContext > sslContext)
 
 BlockingSocket (folly::AsyncSocket::UniquePtr socket)
 
void enableTFO ()
 
void setAddress (folly::SocketAddress address)
 
void open (std::chrono::milliseconds timeout=std::chrono::milliseconds::zero())
 
void close ()
 
void closeWithReset ()
 
int32_t write (uint8_t const *buf, size_t len)
 
void flush ()
 
int32_t readAll (uint8_t *buf, size_t len)
 
int32_t read (uint8_t *buf, size_t len)
 
int getSocketFD () const
 
folly::AsyncSocketgetSocket ()
 
folly::AsyncSSLSocketgetSSLSocket ()
 
- Public Member Functions inherited from folly::AsyncSocket::ConnectCallback
virtual ~ConnectCallback ()=default
 
- Public Member Functions inherited from folly::AsyncReader::ReadCallback
virtual ~ReadCallback ()=default
 
virtual bool isBufferMovable () noexcept
 
virtual size_t maxBufferSize () const
 
virtual void readBufferAvailable (std::unique_ptr< IOBuf >) noexcept
 
- Public Member Functions inherited from folly::AsyncWriter::WriteCallback
virtual ~WriteCallback ()=default
 

Private Member Functions

void connectSuccess () noexceptoverride
 
void connectErr (const folly::AsyncSocketException &ex) noexceptoverride
 
void getReadBuffer (void **bufReturn, size_t *lenReturn) override
 
void readDataAvailable (size_t len) noexceptoverride
 
void readEOF () noexceptoverride
 
void readErr (const folly::AsyncSocketException &ex) noexceptoverride
 
void writeSuccess () noexceptoverride
 
void writeErr (size_t, const folly::AsyncSocketException &ex) noexceptoverride
 
int32_t readHelper (uint8_t *buf, size_t len, bool all)
 

Private Attributes

folly::EventBase eventBase_
 
folly::AsyncSocket::UniquePtr sock_
 
folly::Optional< folly::AsyncSocketExceptionerr_
 
uint8_treadBuf_ {nullptr}
 
size_t readLen_ {0}
 
folly::SocketAddress address_
 

Detailed Description

Definition at line 23 of file BlockingSocket.h.

Constructor & Destructor Documentation

BlockingSocket::BlockingSocket ( int  fd)
inlineexplicit

Definition at line 27 of file BlockingSocket.h.

28  : sock_(new folly::AsyncSocket(&eventBase_, fd)) {}
folly::AsyncSocket::UniquePtr sock_
folly::EventBase eventBase_
BlockingSocket::BlockingSocket ( folly::SocketAddress  address,
std::shared_ptr< folly::SSLContext sslContext 
)
inline

Definition at line 30 of file BlockingSocket.h.

33  : sock_(
34  sslContext ? new folly::AsyncSSLSocket(sslContext, &eventBase_)
36  address_(address) {}
folly::SocketAddress address_
folly::AsyncSocket::UniquePtr sock_
folly::EventBase eventBase_
BlockingSocket::BlockingSocket ( folly::AsyncSocket::UniquePtr  socket)
inlineexplicit

Definition at line 38 of file BlockingSocket.h.

References eventBase_, and sock_.

39  : sock_(std::move(socket)) {
40  sock_->attachEventBase(&eventBase_);
41  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
folly::AsyncSocket::UniquePtr sock_
folly::EventBase eventBase_

Member Function Documentation

void BlockingSocket::close ( )
inline

Definition at line 60 of file BlockingSocket.h.

References sock_.

Referenced by main(), and folly::TEST().

60  {
61  sock_->close();
62  }
folly::AsyncSocket::UniquePtr sock_
void BlockingSocket::closeWithReset ( )
inline

Definition at line 63 of file BlockingSocket.h.

References sock_.

63  {
64  sock_->closeWithReset();
65  }
folly::AsyncSocket::UniquePtr sock_
void BlockingSocket::connectErr ( const folly::AsyncSocketException ex)
inlineoverrideprivatevirtualnoexcept

connectErr() will be invoked if the connection attempt fails.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 107 of file BlockingSocket.h.

107  {
108  err_ = ex;
109  }
folly::Optional< folly::AsyncSocketException > err_
void BlockingSocket::connectSuccess ( )
inlineoverrideprivatevirtualnoexcept

connectSuccess() will be invoked when the connection has been successfully established.

Implements folly::AsyncSocket::ConnectCallback.

Definition at line 106 of file BlockingSocket.h.

106 {}
void BlockingSocket::enableTFO ( )
inline

Definition at line 43 of file BlockingSocket.h.

References sock_.

43  {
44  sock_->enableTFO();
45  }
folly::AsyncSocket::UniquePtr sock_
void BlockingSocket::flush ( )
inline

Definition at line 76 of file BlockingSocket.h.

76 {}
void BlockingSocket::getReadBuffer ( void **  bufReturn,
size_t *  lenReturn 
)
inlineoverrideprivatevirtual

When data becomes available, getReadBuffer() will be invoked to get the buffer into which data should be read.

This method allows the ReadCallback to delay buffer allocation until data becomes available. This allows applications to manage large numbers of idle connections, without having to maintain a separate read buffer for each idle connection.

It is possible that in some cases, getReadBuffer() may be called multiple times before readDataAvailable() is invoked. In this case, the data will be written to the buffer returned from the most recent call to readDataAvailable(). If the previous calls to readDataAvailable() returned different buffers, the ReadCallback is responsible for ensuring that they are not leaked.

If getReadBuffer() throws an exception, returns a nullptr buffer, or returns a 0 length, the ReadCallback will be uninstalled and its readError() method will be invoked.

getReadBuffer() is not allowed to change the transport state before it returns. (For example, it should never uninstall the read callback, or set a different read callback.)

Parameters
bufReturngetReadBuffer() should update *bufReturn to contain the address of the read buffer. This parameter will never be nullptr.
lenReturngetReadBuffer() should update *lenReturn to contain the maximum number of bytes that may be written to the read buffer. This parameter will never be nullptr.

Implements folly::AsyncReader::ReadCallback.

Definition at line 110 of file BlockingSocket.h.

References readBuf_, and readLen_.

110  {
111  *bufReturn = readBuf_;
112  *lenReturn = readLen_;
113  }
uint8_t * readBuf_
folly::AsyncSocket* BlockingSocket::getSocket ( )
inline

Definition at line 90 of file BlockingSocket.h.

References sock_.

90  {
91  return sock_.get();
92  }
folly::AsyncSocket::UniquePtr sock_
int BlockingSocket::getSocketFD ( ) const
inline

Definition at line 86 of file BlockingSocket.h.

References sock_.

86  {
87  return sock_->getFd();
88  }
folly::AsyncSocket::UniquePtr sock_
folly::AsyncSSLSocket* BlockingSocket::getSSLSocket ( )
inline

Definition at line 94 of file BlockingSocket.h.

References sock_.

94  {
95  return dynamic_cast<folly::AsyncSSLSocket*>(sock_.get());
96  }
folly::AsyncSocket::UniquePtr sock_
void BlockingSocket::open ( std::chrono::milliseconds  timeout = std::chrono::milliseconds::zero())
inline

Definition at line 51 of file BlockingSocket.h.

References address_, err_, eventBase_, folly::Optional< Value >::hasValue(), folly::EventBase::loop(), sock_, and folly::Optional< Value >::value().

Referenced by main().

52  {
53  sock_->connect(this, address_, timeout.count());
54  eventBase_.loop();
55  if (err_.hasValue()) {
56  throw err_.value();
57  }
58  }
folly::Optional< folly::AsyncSocketException > err_
folly::SocketAddress address_
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
folly::AsyncSocket::UniquePtr sock_
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
folly::EventBase eventBase_
int32_t BlockingSocket::read ( uint8_t buf,
size_t  len 
)
inline

Definition at line 82 of file BlockingSocket.h.

References readHelper().

Referenced by main().

82  {
83  return readHelper(buf, len, false);
84  }
int32_t readHelper(uint8_t *buf, size_t len, bool all)
int32_t BlockingSocket::readAll ( uint8_t buf,
size_t  len 
)
inline

Definition at line 78 of file BlockingSocket.h.

References readHelper().

Referenced by folly::TEST().

78  {
79  return readHelper(buf, len, true);
80  }
int32_t readHelper(uint8_t *buf, size_t len, bool all)
void BlockingSocket::readDataAvailable ( size_t  len)
inlineoverrideprivatevirtualnoexcept

readDataAvailable() will be invoked when data has been successfully read into the buffer returned by the last call to getReadBuffer().

The read callback remains installed after readDataAvailable() returns. It must be explicitly uninstalled to stop receiving read events. getReadBuffer() will be called at least once before each call to readDataAvailable(). getReadBuffer() will also be called before any call to readEOF().

Parameters
lenThe number of bytes placed in the buffer.

Implements folly::AsyncReader::ReadCallback.

Definition at line 114 of file BlockingSocket.h.

References readBuf_, and readLen_.

114  {
115  readBuf_ += len;
116  readLen_ -= len;
117  if (readLen_ == 0) {
118  sock_->setReadCB(nullptr);
119  }
120  }
uint8_t * readBuf_
folly::AsyncSocket::UniquePtr sock_
void BlockingSocket::readEOF ( )
inlineoverrideprivatevirtualnoexcept

readEOF() will be invoked when the transport is closed.

The read callback will be automatically uninstalled immediately before readEOF() is invoked.

Implements folly::AsyncReader::ReadCallback.

Definition at line 121 of file BlockingSocket.h.

121 {}
void BlockingSocket::readErr ( const folly::AsyncSocketException ex)
inlineoverrideprivatevirtualnoexcept

readError() will be invoked if an error occurs reading from the transport.

The read callback will be automatically uninstalled immediately before readError() is invoked.

Parameters
exAn exception describing the error that occurred.

Implements folly::AsyncReader::ReadCallback.

Definition at line 122 of file BlockingSocket.h.

122  {
123  err_ = ex;
124  }
folly::Optional< folly::AsyncSocketException > err_
int32_t BlockingSocket::readHelper ( uint8_t buf,
size_t  len,
bool  all 
)
inlineprivate

Definition at line 132 of file BlockingSocket.h.

References folly::Optional< Value >::hasValue(), folly::EventBase::loopOnce(), readBuf_, readLen_, folly::AsyncSocketException::UNKNOWN, and folly::Optional< Value >::value().

Referenced by read(), and readAll().

132  {
133  if (!sock_->good()) {
134  return 0;
135  }
136 
137  readBuf_ = buf;
138  readLen_ = len;
139  sock_->setReadCB(this);
140  while (!err_ && sock_->good() && readLen_ > 0) {
142  if (!all) {
143  break;
144  }
145  }
146  sock_->setReadCB(nullptr);
147  if (err_.hasValue()) {
148  throw err_.value();
149  }
150  if (all && readLen_ > 0) {
153  }
154  return len - readLen_;
155  }
folly::Optional< folly::AsyncSocketException > err_
uint8_t * readBuf_
bool loopOnce(int flags=0)
Definition: EventBase.cpp:271
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
folly::AsyncSocket::UniquePtr sock_
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
folly::EventBase eventBase_
Composed all(Predicate pred=Predicate())
Definition: Base.h:786
void BlockingSocket::setAddress ( folly::SocketAddress  address)
inline

Definition at line 47 of file BlockingSocket.h.

References address_.

Referenced by main().

47  {
48  address_ = address;
49  }
folly::SocketAddress address_
int32_t BlockingSocket::write ( uint8_t const *  buf,
size_t  len 
)
inline

Definition at line 67 of file BlockingSocket.h.

References err_, eventBase_, folly::Optional< Value >::hasValue(), folly::EventBase::loop(), sock_, and folly::Optional< Value >::value().

Referenced by main(), and folly::TEST().

67  {
68  sock_->write(this, buf, len);
69  eventBase_.loop();
70  if (err_.hasValue()) {
71  throw err_.value();
72  }
73  return len;
74  }
folly::Optional< folly::AsyncSocketException > err_
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept
Definition: Optional.h:300
folly::AsyncSocket::UniquePtr sock_
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
folly::EventBase eventBase_
void BlockingSocket::writeErr ( size_t  bytesWritten,
const folly::AsyncSocketException ex 
)
inlineoverrideprivatevirtualnoexcept

writeError() will be invoked if an error occurs writing the data.

Parameters
bytesWrittenThe number of bytes that were successfull
exAn exception describing the error that occurred.

Implements folly::AsyncWriter::WriteCallback.

Definition at line 126 of file BlockingSocket.h.

128  {
129  err_ = ex;
130  }
folly::Optional< folly::AsyncSocketException > err_
void BlockingSocket::writeSuccess ( )
inlineoverrideprivatevirtualnoexcept

writeSuccess() will be invoked when all of the data has been successfully written.

Note that this mainly signals that the buffer containing the data to write is no longer needed and may be freed or re-used. It does not guarantee that the data has been fully transmitted to the remote endpoint. For example, on socket-based transports, writeSuccess() only indicates that the data has been given to the kernel for eventual transmission.

Implements folly::AsyncWriter::WriteCallback.

Definition at line 125 of file BlockingSocket.h.

125 {}

Member Data Documentation

folly::SocketAddress BlockingSocket::address_
private

Definition at line 104 of file BlockingSocket.h.

Referenced by open(), and setAddress().

folly::Optional<folly::AsyncSocketException> BlockingSocket::err_
private

Definition at line 101 of file BlockingSocket.h.

Referenced by open(), and write().

folly::EventBase BlockingSocket::eventBase_
private

Definition at line 99 of file BlockingSocket.h.

Referenced by BlockingSocket(), open(), and write().

uint8_t* BlockingSocket::readBuf_ {nullptr}
private

Definition at line 102 of file BlockingSocket.h.

Referenced by getReadBuffer(), readDataAvailable(), and readHelper().

size_t BlockingSocket::readLen_ {0}
private

Definition at line 103 of file BlockingSocket.h.

Referenced by getReadBuffer(), readDataAvailable(), and readHelper().


The documentation for this class was generated from the following file: