Commit 7d9dbc59 authored by Claire (Yue) Zhang's avatar Claire (Yue) Zhang Committed by Facebook GitHub Bot

Change AsyncSocket::UniquePtr destructor to ReleasableDestuctor

Summary: Adding a new ReleasableDestructor and use it in `folly::AsyncSocket::UniquePtr`.

Reviewed By: yfeldblum

Differential Revision: D25352913

fbshipit-source-id: b85204688706cb82a323e37063c9a6a390ec2ee8
parent a484d190
...@@ -75,7 +75,7 @@ class AsyncSSLSocketConnector; ...@@ -75,7 +75,7 @@ class AsyncSSLSocketConnector;
*/ */
class AsyncSSLSocket : public AsyncSocket { class AsyncSSLSocket : public AsyncSocket {
public: public:
typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr; typedef std::unique_ptr<AsyncSSLSocket, ReleasableDestructor> UniquePtr;
using X509_deleter = folly::static_function_deleter<X509, &X509_free>; using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
class HandshakeCB { class HandshakeCB {
...@@ -290,9 +290,8 @@ class AsyncSSLSocket : public AsyncSocket { ...@@ -290,9 +290,8 @@ class AsyncSSLSocket : public AsyncSocket {
NetworkSocket fd, NetworkSocket fd,
bool server = true, bool server = true,
bool deferSecurityNegotiation = false) { bool deferSecurityNegotiation = false) {
return std::shared_ptr<AsyncSSLSocket>( return std::shared_ptr<AsyncSSLSocket>(AsyncSSLSocket::UniquePtr(
new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation), new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation)));
Destructor());
} }
/** /**
...@@ -302,8 +301,8 @@ class AsyncSSLSocket : public AsyncSocket { ...@@ -302,8 +301,8 @@ class AsyncSSLSocket : public AsyncSocket {
const std::shared_ptr<folly::SSLContext>& ctx, const std::shared_ptr<folly::SSLContext>& ctx,
EventBase* evb, EventBase* evb,
bool deferSecurityNegotiation = false) { bool deferSecurityNegotiation = false) {
return std::shared_ptr<AsyncSSLSocket>( return std::shared_ptr<AsyncSSLSocket>(AsyncSSLSocket::UniquePtr(
new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation), Destructor()); new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation)));
} }
#if FOLLY_OPENSSL_HAS_SNI #if FOLLY_OPENSSL_HAS_SNI
...@@ -344,9 +343,8 @@ class AsyncSSLSocket : public AsyncSocket { ...@@ -344,9 +343,8 @@ class AsyncSSLSocket : public AsyncSocket {
EventBase* evb, EventBase* evb,
const std::string& serverName, const std::string& serverName,
bool deferSecurityNegotiation = false) { bool deferSecurityNegotiation = false) {
return std::shared_ptr<AsyncSSLSocket>( return std::shared_ptr<AsyncSSLSocket>(AsyncSSLSocket::UniquePtr(
new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation), new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation)));
Destructor());
} }
#endif // FOLLY_OPENSSL_HAS_SNI #endif // FOLLY_OPENSSL_HAS_SNI
......
...@@ -76,7 +76,31 @@ namespace folly { ...@@ -76,7 +76,31 @@ namespace folly {
class AsyncSocket : public AsyncTransport { class AsyncSocket : public AsyncTransport {
public: public:
using UniquePtr = std::unique_ptr<AsyncSocket, Destructor>; /**
* Use ReleasableDestructor with AsyncSocket to enable transferring the
* ownership of the socket owned by smart pointers.
*/
class ReleasableDestructor : public DelayedDestruction::Destructor {
public:
void operator()(DelayedDestruction* dd) const {
if (!released_) {
dd->destroy();
}
}
/**
* Release the object managed by smart pointers. This is used when the
* object ownership is transferred to another smart pointer or manually
* managed by the caller. The original object must be properly deleted at
* the end of its life cycle to avoid resource leaks.
*/
void release() { released_ = true; }
private:
bool released_{false};
};
using UniquePtr = std::unique_ptr<AsyncSocket, ReleasableDestructor>;
class ConnectCallback { class ConnectCallback {
public: public:
...@@ -304,7 +328,7 @@ class AsyncSocket : public AsyncTransport { ...@@ -304,7 +328,7 @@ class AsyncSocket : public AsyncTransport {
* destructor is protected and cannot be invoked directly. * destructor is protected and cannot be invoked directly.
*/ */
static UniquePtr newSocket(EventBase* evb) { static UniquePtr newSocket(EventBase* evb) {
return UniquePtr{new AsyncSocket(evb), Destructor()}; return UniquePtr{new AsyncSocket(evb)};
} }
/** /**
...@@ -315,8 +339,8 @@ class AsyncSocket : public AsyncTransport { ...@@ -315,8 +339,8 @@ class AsyncSocket : public AsyncTransport {
const folly::SocketAddress& address, const folly::SocketAddress& address,
uint32_t connectTimeout = 0, uint32_t connectTimeout = 0,
bool useZeroCopy = false) { bool useZeroCopy = false) {
return UniquePtr{new AsyncSocket(evb, address, connectTimeout, useZeroCopy), return UniquePtr{
Destructor()}; new AsyncSocket(evb, address, connectTimeout, useZeroCopy)};
} }
/** /**
...@@ -329,15 +353,14 @@ class AsyncSocket : public AsyncTransport { ...@@ -329,15 +353,14 @@ class AsyncSocket : public AsyncTransport {
uint32_t connectTimeout = 0, uint32_t connectTimeout = 0,
bool useZeroCopy = false) { bool useZeroCopy = false) {
return UniquePtr{ return UniquePtr{
new AsyncSocket(evb, ip, port, connectTimeout, useZeroCopy), new AsyncSocket(evb, ip, port, connectTimeout, useZeroCopy)};
Destructor()};
} }
/** /**
* Helper function to create an AsyncSocket. * Helper function to create an AsyncSocket.
*/ */
static UniquePtr newSocket(EventBase* evb, NetworkSocket fd) { static UniquePtr newSocket(EventBase* evb, NetworkSocket fd) {
return UniquePtr{new AsyncSocket(evb, fd), Destructor()}; return UniquePtr{new AsyncSocket(evb, fd)};
} }
/** /**
......
...@@ -2694,7 +2694,8 @@ TEST(AsyncSSLSocketTest, TTLSDisabled) { ...@@ -2694,7 +2694,8 @@ TEST(AsyncSSLSocketTest, TTLSDisabled) {
class MockAsyncTFOSSLSocket : public AsyncSSLSocket { class MockAsyncTFOSSLSocket : public AsyncSSLSocket {
public: public:
using UniquePtr = std::unique_ptr<MockAsyncTFOSSLSocket, Destructor>; using UniquePtr =
std::unique_ptr<MockAsyncTFOSSLSocket, ReleasableDestructor>;
explicit MockAsyncTFOSSLSocket( explicit MockAsyncTFOSSLSocket(
std::shared_ptr<folly::SSLContext> sslCtx, std::shared_ptr<folly::SSLContext> sslCtx,
......
...@@ -26,7 +26,7 @@ namespace test { ...@@ -26,7 +26,7 @@ namespace test {
class MockAsyncSocket : public AsyncSocket { class MockAsyncSocket : public AsyncSocket {
public: public:
typedef std::unique_ptr<MockAsyncSocket, Destructor> UniquePtr; typedef std::unique_ptr<MockAsyncSocket, ReleasableDestructor> UniquePtr;
explicit MockAsyncSocket(EventBase* base) : AsyncSocket(base) {} explicit MockAsyncSocket(EventBase* base) : AsyncSocket(base) {}
......
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