2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <folly/wangle/bootstrap/ServerBootstrap-inl.h>
19 #include <folly/io/async/AsyncServerSocket.h>
20 #include <folly/io/async/EventBaseManager.h>
21 #include <folly/io/async/AsyncUDPServerSocket.h>
25 class ServerSocketFactory {
27 virtual std::shared_ptr<AsyncSocketBase> newSocket(
28 int port, SocketAddress address, int backlog,
29 bool reuse, ServerSocketConfig& config) = 0;
31 virtual void stopSocket(
32 std::shared_ptr<AsyncSocketBase>& socket) = 0;
34 virtual void removeAcceptCB(std::shared_ptr<AsyncSocketBase> sock, Acceptor *callback, EventBase* base) = 0;
35 virtual void addAcceptCB(std::shared_ptr<AsyncSocketBase> sock, Acceptor* callback, EventBase* base) = 0 ;
36 virtual ~ServerSocketFactory() = default;
39 class AsyncServerSocketFactory : public ServerSocketFactory {
41 std::shared_ptr<AsyncSocketBase> newSocket(
42 int port, SocketAddress address, int backlog, bool reuse,
43 ServerSocketConfig& config) {
45 auto socket = folly::AsyncServerSocket::newSocket();
46 socket->setReusePortEnabled(reuse);
47 socket->attachEventBase(EventBaseManager::get()->getEventBase());
51 socket->bind(address);
54 socket->listen(config.acceptBacklog);
55 socket->startAccepting();
60 virtual void stopSocket(
61 std::shared_ptr<AsyncSocketBase>& s) {
62 auto socket = std::dynamic_pointer_cast<AsyncServerSocket>(s);
64 socket->stopAccepting();
65 socket->detachEventBase();
68 virtual void removeAcceptCB(std::shared_ptr<AsyncSocketBase> s,
69 Acceptor *callback, EventBase* base) {
70 auto socket = std::dynamic_pointer_cast<AsyncServerSocket>(s);
72 socket->removeAcceptCallback(callback, base);
75 virtual void addAcceptCB(std::shared_ptr<AsyncSocketBase> s,
76 Acceptor* callback, EventBase* base) {
77 auto socket = std::dynamic_pointer_cast<AsyncServerSocket>(s);
79 socket->addAcceptCallback(callback, base);
83 class AsyncUDPServerSocketFactory : public ServerSocketFactory {
85 std::shared_ptr<AsyncSocketBase> newSocket(
86 int port, SocketAddress address, int backlog, bool reuse,
87 ServerSocketConfig& config) {
89 auto socket = std::make_shared<AsyncUDPServerSocket>(
90 EventBaseManager::get()->getEventBase());
91 socket->setReusePort(reuse);
93 SocketAddress addressr("::1", port);
94 socket->bind(addressr);
96 socket->bind(address);
103 virtual void stopSocket(
104 std::shared_ptr<AsyncSocketBase>& s) {
105 auto socket = std::dynamic_pointer_cast<AsyncUDPServerSocket>(s);
110 virtual void removeAcceptCB(std::shared_ptr<AsyncSocketBase> s,
111 Acceptor *callback, EventBase* base) {
114 virtual void addAcceptCB(std::shared_ptr<AsyncSocketBase> s,
115 Acceptor* callback, EventBase* base) {
116 auto socket = std::dynamic_pointer_cast<AsyncUDPServerSocket>(s);
118 socket->addListener(base, callback);