socket_server.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  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. #ifndef RTC_BASE_SOCKET_SERVER_H_
  11. #define RTC_BASE_SOCKET_SERVER_H_
  12. #include <memory>
  13. #include "rtc_base/socket_factory.h"
  14. namespace rtc {
  15. class Thread;
  16. // Needs to be forward declared because there's a circular dependency between
  17. // NetworkMonitor and Thread.
  18. // TODO(deadbeef): Fix this.
  19. class NetworkBinderInterface;
  20. // Provides the ability to wait for activity on a set of sockets. The Thread
  21. // class provides a nice wrapper on a socket server.
  22. //
  23. // The server is also a socket factory. The sockets it creates will be
  24. // notified of asynchronous I/O from this server's Wait method.
  25. class SocketServer : public SocketFactory {
  26. public:
  27. static const int kForever = -1;
  28. static std::unique_ptr<SocketServer> CreateDefault();
  29. // When the socket server is installed into a Thread, this function is
  30. // called to allow the socket server to use the thread's message queue for
  31. // any messaging that it might need to perform.
  32. virtual void SetMessageQueue(Thread* queue) {}
  33. // Sleeps until:
  34. // 1) cms milliseconds have elapsed (unless cms == kForever)
  35. // 2) WakeUp() is called
  36. // While sleeping, I/O is performed if process_io is true.
  37. virtual bool Wait(int cms, bool process_io) = 0;
  38. // Causes the current wait (if one is in progress) to wake up.
  39. virtual void WakeUp() = 0;
  40. // A network binder will bind the created sockets to a network.
  41. // It is only used in PhysicalSocketServer.
  42. void set_network_binder(NetworkBinderInterface* binder) {
  43. network_binder_ = binder;
  44. }
  45. NetworkBinderInterface* network_binder() const { return network_binder_; }
  46. private:
  47. NetworkBinderInterface* network_binder_ = nullptr;
  48. };
  49. } // namespace rtc
  50. #endif // RTC_BASE_SOCKET_SERVER_H_