2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
21 #include <folly/io/async/SSLContext.h>
22 #include <folly/io/async/AsyncServerSocket.h>
29 class AsyncSSLServerSocket : public DelayedDestruction,
30 private AsyncServerSocket::AcceptCallback {
32 class SSLAcceptCallback {
34 virtual ~SSLAcceptCallback() {}
37 * connectionAccepted() is called whenever a new client connection is
40 * The SSLAcceptCallback will remain installed after connectionAccepted()
43 * @param sock The newly accepted client socket. The
45 * assumes ownership of this socket, and is responsible
46 * for closing it when done.
48 virtual void connectionAccepted(
49 const std::shared_ptr<AsyncSSLSocket> &sock)
53 * acceptError() is called if an error occurs while accepting.
55 * The SSLAcceptCallback will remain installed even after an accept error.
56 * If the callback wants to uninstall itself and stop trying to accept new
57 * connections, it must explicit call setAcceptCallback(nullptr).
59 * @param ex An exception representing the error.
61 virtual void acceptError(const std::exception& ex) noexcept = 0;
65 * Create a new TAsyncSSLServerSocket with the specified EventBase.
67 * @param eventBase The EventBase to use for driving the asynchronous I/O.
68 * If this parameter is nullptr, attachEventBase() must be
69 * called before this socket can begin accepting
70 * connections. All TAsyncSSLSocket objects accepted by
71 * this server socket will be attached to this EventBase
72 * when they are created.
74 explicit AsyncSSLServerSocket(
75 const std::shared_ptr<folly::SSLContext>& ctx,
76 EventBase* eventBase = nullptr);
81 * destroy() must be called to destroy the socket. The normal destructor is
82 * private, and should not be invoked directly. This prevents callers from
83 * deleting a TAsyncSSLServerSocket while it is invoking a callback.
85 virtual void destroy();
87 virtual void bind(const folly::SocketAddress& address) {
88 serverSocket_->bind(address);
90 virtual void bind(uint16_t port) {
91 serverSocket_->bind(port);
93 void getAddress(folly::SocketAddress* addressReturn) {
94 serverSocket_->getAddress(addressReturn);
96 virtual void listen(int backlog) {
97 serverSocket_->listen(backlog);
101 * Helper function to create a shared_ptr<TAsyncSSLServerSocket>.
103 * This passes in the correct destructor object, since TAsyncSSLServerSocket's
104 * destructor is protected and cannot be invoked directly.
106 static std::shared_ptr<AsyncSSLServerSocket> newSocket(
107 const std::shared_ptr<folly::SSLContext>& ctx,
109 return std::shared_ptr<AsyncSSLServerSocket>(
110 new AsyncSSLServerSocket(ctx, evb),
115 * Set the accept callback.
117 * This method may only be invoked from the EventBase's loop thread.
119 * @param callback The callback to invoke when a new socket
120 * connection is accepted and a new TAsyncSSLSocket is
123 * Throws TTransportException on error.
125 void setSSLAcceptCallback(SSLAcceptCallback* callback);
127 SSLAcceptCallback *getSSLAcceptCallback() const {
131 void attachEventBase(EventBase* eventBase);
132 void detachEventBase();
135 * Returns the EventBase that the handler is currently attached to.
137 EventBase* getEventBase() const {
143 * Protected destructor.
145 * Invoke destroy() instead to destroy the TAsyncSSLServerSocket.
147 virtual ~AsyncSSLServerSocket();
150 virtual void connectionAccepted(int fd,
151 const folly::SocketAddress& clientAddr)
153 virtual void acceptError(const std::exception& ex) noexcept;
155 EventBase* eventBase_;
156 AsyncServerSocket* serverSocket_;
158 std::shared_ptr<folly::SSLContext> ctx_;
159 // The accept callback
160 SSLAcceptCallback* sslCallback_;