| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_PROXY_SERVER_H_ |
| 12 | #define RTC_BASE_PROXY_SERVER_H_ |
| 13 |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 |
| 17 | #include "absl/memory/memory.h" |
| 18 | #include "rtc_base/memory/fifo_buffer.h" |
| 19 | #include "rtc_base/server_socket_adapters.h" |
| 20 | #include "rtc_base/socket.h" |
| 21 | #include "rtc_base/socket_address.h" |
| 22 |
| 23 | namespace rtc { |
| 24 |
| 25 | class SocketFactory; |
| 26 |
| 27 | // ProxyServer is a base class that allows for easy construction of proxy |
| 28 | // servers. With its helper class ProxyBinding, it contains all the necessary |
| 29 | // logic for receiving and bridging connections. The specific client-server |
| 30 | // proxy protocol is implemented by an instance of the AsyncProxyServerSocket |
| 31 | // class; children of ProxyServer implement WrapSocket appropriately to return |
| 32 | // the correct protocol handler. |
| 33 |
| 34 | class ProxyBinding : public sigslot::has_slots<> { |
| 35 | public: |
| 36 | ProxyBinding(AsyncProxyServerSocket* in_socket, Socket* out_socket); |
| 37 | ~ProxyBinding() override; |
| 38 |
| 39 | ProxyBinding(const ProxyBinding&) = delete; |
| 40 | ProxyBinding& operator=(const ProxyBinding&) = delete; |
| 41 |
| 42 | sigslot::signal1<ProxyBinding*> SignalDestroyed; |
| 43 |
| 44 | private: |
| 45 | void OnConnectRequest(AsyncProxyServerSocket* socket, |
| 46 | const SocketAddress& addr); |
| 47 | void OnInternalRead(Socket* socket); |
| 48 | void OnInternalWrite(Socket* socket); |
| 49 | void OnInternalClose(Socket* socket, int err); |
| 50 | void OnExternalConnect(Socket* socket); |
| 51 | void OnExternalRead(Socket* socket); |
| 52 | void OnExternalWrite(Socket* socket); |
| 53 | void OnExternalClose(Socket* socket, int err); |
| 54 |
| 55 | static void Read(Socket* socket, FifoBuffer* buffer); |
| 56 | static void Write(Socket* socket, FifoBuffer* buffer); |
| 57 | void Destroy(); |
| 58 |
| 59 | static const int kBufferSize = 4096; |
| 60 | std::unique_ptr<AsyncProxyServerSocket> int_socket_; |
| 61 | std::unique_ptr<Socket> ext_socket_; |
| 62 | bool connected_; |
| 63 | FifoBuffer out_buffer_; |
| 64 | FifoBuffer in_buffer_; |
| 65 | }; |
| 66 |
| 67 | class ProxyServer : public sigslot::has_slots<> { |
| 68 | public: |
| 69 | ProxyServer(SocketFactory* int_factory, |
| 70 | const SocketAddress& int_addr, |
| 71 | SocketFactory* ext_factory, |
| 72 | const SocketAddress& ext_ip); |
| 73 | ~ProxyServer() override; |
| 74 |
| 75 | ProxyServer(const ProxyServer&) = delete; |
| 76 | ProxyServer& operator=(const ProxyServer&) = delete; |
| 77 |
| 78 | // Returns the address to which the proxy server is bound |
| 79 | SocketAddress GetServerAddress(); |
| 80 |
| 81 | protected: |
| 82 | void OnAcceptEvent(Socket* socket); |
| 83 | virtual AsyncProxyServerSocket* WrapSocket(Socket* socket) = 0; |
| 84 |
| 85 | private: |
| 86 | SocketFactory* ext_factory_; |
| 87 | SocketAddress ext_ip_; |
| 88 | std::unique_ptr<Socket> server_socket_; |
| 89 | std::vector<std::unique_ptr<ProxyBinding>> bindings_; |
| 90 | }; |
| 91 |
| 92 | } // namespace rtc |
| 93 |
| 94 | #endif // RTC_BASE_PROXY_SERVER_H_ |
| 95 |