proxygen
ServerSocketFactory.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #pragma once
18 
23 
24 namespace wangle {
25 
27  public:
28  virtual std::shared_ptr<folly::AsyncSocketBase> newSocket(
29  folly::SocketAddress address, int backlog,
30  bool reuse, const ServerSocketConfig& config) = 0;
31 
32  virtual void removeAcceptCB(
33  std::shared_ptr<folly::AsyncSocketBase> sock,
34  Acceptor *callback,
35  folly::EventBase* base) = 0;
36 
37  virtual void addAcceptCB(
38  std::shared_ptr<folly::AsyncSocketBase> sock,
39  Acceptor* callback,
40  folly::EventBase* base) = 0 ;
41 
42  virtual ~ServerSocketFactory() = default;
43 };
44 
46  public:
47  std::shared_ptr<folly::AsyncSocketBase> newSocket(
48  folly::SocketAddress address, int /*backlog*/, bool reuse,
49  const ServerSocketConfig& config) override {
50 
52  std::shared_ptr<folly::AsyncServerSocket> socket(
53  new folly::AsyncServerSocket(evb),
55  socket->setMaxNumMessagesInQueue(
57  socket->setReusePortEnabled(reuse);
58  if (config.enableTCPFastOpen) {
59  socket->setTFOEnabled(true, config.fastOpenQueueSize);
60  }
61  socket->bind(address);
62 
63  socket->listen(config.acceptBacklog);
64  socket->startAccepting();
65 
66  return socket;
67  }
68 
69  void removeAcceptCB(std::shared_ptr<folly::AsyncSocketBase> s,
70  Acceptor *callback, folly::EventBase* base) override {
71  auto socket = std::dynamic_pointer_cast<folly::AsyncServerSocket>(s);
72  CHECK(socket);
73  socket->removeAcceptCallback(callback, base);
74  }
75 
76  void addAcceptCB(std::shared_ptr<folly::AsyncSocketBase> s,
77  Acceptor* callback, folly::EventBase* base) override {
78  auto socket = std::dynamic_pointer_cast<folly::AsyncServerSocket>(s);
79  CHECK(socket);
80  socket->addAcceptCallback(callback, base);
81  }
82 
84  public:
86  folly::EventBase* evb = socket->getEventBase();
87  if (evb) {
89  socket->destroy();
90  });
91  } else {
92  socket->destroy();
93  }
94  }
95  };
96 };
97 
99  public:
100  std::shared_ptr<folly::AsyncSocketBase> newSocket(
101  folly::SocketAddress address, int /*backlog*/, bool reuse,
102  const ServerSocketConfig& /*config*/) override {
103 
105  std::shared_ptr<folly::AsyncUDPServerSocket> socket(
108  socket->setReusePort(reuse);
109  socket->bind(address);
110  socket->listen();
111 
112  return socket;
113  }
114 
115  void removeAcceptCB(std::shared_ptr<folly::AsyncSocketBase> /*s*/,
116  Acceptor* /*callback*/,
117  folly::EventBase* /*base*/) override {
118  }
119 
120  void addAcceptCB(std::shared_ptr<folly::AsyncSocketBase> s,
121  Acceptor* callback, folly::EventBase* base) override {
122  auto socket = std::dynamic_pointer_cast<folly::AsyncUDPServerSocket>(s);
123  DCHECK(socket);
124  socket->addListener(base, callback);
125  }
126 
128  public:
131  [socket]() {
132  delete socket;
133  }
134  );
135  }
136  };
137 };
138 
139 } // namespace wangle
void addAcceptCB(std::shared_ptr< folly::AsyncSocketBase > s, Acceptor *callback, folly::EventBase *base) override
EventBase * getEventBase() const
std::shared_ptr< folly::AsyncSocketBase > newSocket(folly::SocketAddress address, int, bool reuse, const ServerSocketConfig &config) override
virtual void removeAcceptCB(std::shared_ptr< folly::AsyncSocketBase > sock, Acceptor *callback, folly::EventBase *base)=0
EventBase * getEventBase() const override
bool runImmediatelyOrRunInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:804
void operator()(folly::AsyncServerSocket *socket) const
virtual void addAcceptCB(std::shared_ptr< folly::AsyncSocketBase > sock, Acceptor *callback, folly::EventBase *base)=0
static EventBaseManager * get()
AHArrayT::Config config
void removeAcceptCB(std::shared_ptr< folly::AsyncSocketBase >, Acceptor *, folly::EventBase *) override
void addAcceptCB(std::shared_ptr< folly::AsyncSocketBase > s, Acceptor *callback, folly::EventBase *base) override
void operator()(folly::AsyncUDPServerSocket *socket) const
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
std::shared_ptr< folly::AsyncSocketBase > newSocket(folly::SocketAddress address, int, bool reuse, const ServerSocketConfig &) override
static set< string > s
EventBase * getEventBase() const override
void removeAcceptCB(std::shared_ptr< folly::AsyncSocketBase > s, Acceptor *callback, folly::EventBase *base) override
virtual ~ServerSocketFactory()=default
virtual std::shared_ptr< folly::AsyncSocketBase > newSocket(folly::SocketAddress address, int backlog, bool reuse, const ServerSocketConfig &config)=0