Commit ae1e90a3 authored by Cristian Lumezanu's avatar Cristian Lumezanu Committed by Facebook GitHub Bot

Change move constructor to populate member fields early

Summary: The AsyncSocket move constructor creates a new socket and then populates member fields, such as `byteEventHelper_` and `preReceivedData_`, with the respective values from the old socket. This happens **after** any callbacks in the AsyncSocket constructor (e.g., installed with ConstructorCallback). These callbacks may need the updated values. This diffs offers the possibility to populate new socket member variables early, by adding them to the initializer list of the AsyncSocket constructor used in the move constructor.

Reviewed By: bschlinker

Differential Revision: D31318333

fbshipit-source-id: 731d24ab4c7525e6f28c3aea4d04c9c5e98409ea
parent 580d5f19
...@@ -588,15 +588,24 @@ AsyncSocket::AsyncSocket( ...@@ -588,15 +588,24 @@ AsyncSocket::AsyncSocket(
} }
AsyncSocket::AsyncSocket(AsyncSocket* oldAsyncSocket) AsyncSocket::AsyncSocket(AsyncSocket* oldAsyncSocket)
: AsyncSocket( : zeroCopyBufId_(oldAsyncSocket->getZeroCopyBufId()),
oldAsyncSocket->getEventBase(), state_(oldAsyncSocket->state_),
oldAsyncSocket->detachNetworkSocket(), fd_(oldAsyncSocket->detachNetworkSocket()),
oldAsyncSocket->getZeroCopyBufId(), addr_(oldAsyncSocket->addr_),
&oldAsyncSocket->addr_) { eventBase_(oldAsyncSocket->getEventBase()),
appBytesWritten_ = oldAsyncSocket->appBytesWritten_; writeTimeout_(this, eventBase_),
rawBytesWritten_ = oldAsyncSocket->rawBytesWritten_; ioHandler_(this, eventBase_, fd_),
byteEventHelper_ = std::move(oldAsyncSocket->byteEventHelper_); immediateReadHandler_(this),
preReceivedData_ = std::move(oldAsyncSocket->preReceivedData_); appBytesWritten_(oldAsyncSocket->appBytesWritten_),
rawBytesWritten_(oldAsyncSocket->rawBytesWritten_),
preReceivedData_(std::move(oldAsyncSocket->preReceivedData_)),
byteEventHelper_(std::move(oldAsyncSocket->byteEventHelper_)) {
VLOG(5) << "move AsyncSocket(" << oldAsyncSocket << "->" << this
<< ", evb=" << eventBase_ << ", fd=" << fd_
<< ", zeroCopyBufId=" << zeroCopyBufId_ << ")";
init();
disableTransparentFunctions(fd_, noTransparentTls_, noTSocks_);
setCloseOnExec();
// inform lifecycle observers to give them an opportunity to unsubscribe from // inform lifecycle observers to give them an opportunity to unsubscribe from
// events for the old socket and subscribe to the new socket; we do not move // events for the old socket and subscribe to the new socket; we do not move
...@@ -619,7 +628,6 @@ void AsyncSocket::init() { ...@@ -619,7 +628,6 @@ void AsyncSocket::init() {
eventBase_->dcheckIsInEventBaseThread(); eventBase_->dcheckIsInEventBaseThread();
} }
shutdownFlags_ = 0; shutdownFlags_ = 0;
state_ = StateEnum::UNINIT;
eventFlags_ = EventHandler::NONE; eventFlags_ = EventHandler::NONE;
sendTimeout_ = 0; sendTimeout_ = 0;
maxReadsPerEvent_ = 16; maxReadsPerEvent_ = 16;
...@@ -630,9 +638,7 @@ void AsyncSocket::init() { ...@@ -630,9 +638,7 @@ void AsyncSocket::init() {
writeReqHead_ = nullptr; writeReqHead_ = nullptr;
writeReqTail_ = nullptr; writeReqTail_ = nullptr;
wShutdownSocketSet_.reset(); wShutdownSocketSet_.reset();
appBytesWritten_ = 0;
appBytesReceived_ = 0; appBytesReceived_ = 0;
rawBytesWritten_ = 0;
totalAppBytesScheduledForWrite_ = 0; totalAppBytesScheduledForWrite_ = 0;
sendMsgParamCallback_ = &defaultSendMsgParamsCallback; sendMsgParamCallback_ = &defaultSendMsgParamsCallback;
} }
......
...@@ -1547,7 +1547,7 @@ class AsyncSocket : public AsyncTransport { ...@@ -1547,7 +1547,7 @@ class AsyncSocket : public AsyncTransport {
std::unordered_map<uint32_t, folly::IOBuf*> idZeroCopyBufPtrMap_; std::unordered_map<uint32_t, folly::IOBuf*> idZeroCopyBufPtrMap_;
std::unordered_map<folly::IOBuf*, IOBufInfo> idZeroCopyBufInfoMap_; std::unordered_map<folly::IOBuf*, IOBufInfo> idZeroCopyBufInfoMap_;
StateEnum state_; ///< StateEnum describing current state StateEnum state_{StateEnum::UNINIT}; ///< StateEnum describing current state
uint8_t shutdownFlags_; ///< Shutdown state (ShutdownFlags) uint8_t shutdownFlags_; ///< Shutdown state (ShutdownFlags)
uint16_t eventFlags_; ///< EventBase::HandlerFlags settings uint16_t eventFlags_; ///< EventBase::HandlerFlags settings
NetworkSocket fd_; ///< The socket file descriptor NetworkSocket fd_; ///< The socket file descriptor
...@@ -1575,8 +1575,8 @@ class AsyncSocket : public AsyncTransport { ...@@ -1575,8 +1575,8 @@ class AsyncSocket : public AsyncTransport {
WriteRequest* writeReqTail_; ///< End of WriteRequest chain WriteRequest* writeReqTail_; ///< End of WriteRequest chain
std::weak_ptr<ShutdownSocketSet> wShutdownSocketSet_; std::weak_ptr<ShutdownSocketSet> wShutdownSocketSet_;
size_t appBytesReceived_; ///< Num of bytes received from socket size_t appBytesReceived_; ///< Num of bytes received from socket
size_t appBytesWritten_; ///< Num of bytes written to socket size_t appBytesWritten_{0}; ///< Num of bytes written to socket
size_t rawBytesWritten_; ///< Num of (raw) bytes written to socket size_t rawBytesWritten_{0}; ///< Num of (raw) bytes written to socket
// The total num of bytes passed to AsyncSocket's write functions. It doesn't // The total num of bytes passed to AsyncSocket's write functions. It doesn't
// include failed writes, but it does include buffered writes. // include failed writes, but it does include buffered writes.
size_t totalAppBytesScheduledForWrite_; size_t totalAppBytesScheduledForWrite_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment