async_socket.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_ASYNC_SOCKET_H_
  11. #define RTC_BASE_ASYNC_SOCKET_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include "rtc_base/socket.h"
  15. #include "rtc_base/socket_address.h"
  16. #include "rtc_base/third_party/sigslot/sigslot.h"
  17. namespace rtc {
  18. // TODO: Remove Socket and rename AsyncSocket to Socket.
  19. // Provides the ability to perform socket I/O asynchronously.
  20. class AsyncSocket : public Socket {
  21. public:
  22. AsyncSocket();
  23. ~AsyncSocket() override;
  24. AsyncSocket* Accept(SocketAddress* paddr) override = 0;
  25. // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
  26. // access concurrently from different thread.
  27. // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
  28. // but at the same time the SocketDispatcher maybe signaling the read event.
  29. // ready to read
  30. sigslot::signal1<AsyncSocket*, sigslot::multi_threaded_local> SignalReadEvent;
  31. // ready to write
  32. sigslot::signal1<AsyncSocket*, sigslot::multi_threaded_local>
  33. SignalWriteEvent;
  34. sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected
  35. sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed
  36. };
  37. class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
  38. public:
  39. // The adapted socket may explicitly be null, and later assigned using Attach.
  40. // However, subclasses which support detached mode must override any methods
  41. // that will be called during the detached period (usually GetState()), to
  42. // avoid dereferencing a null pointer.
  43. explicit AsyncSocketAdapter(AsyncSocket* socket);
  44. ~AsyncSocketAdapter() override;
  45. void Attach(AsyncSocket* socket);
  46. SocketAddress GetLocalAddress() const override;
  47. SocketAddress GetRemoteAddress() const override;
  48. int Bind(const SocketAddress& addr) override;
  49. int Connect(const SocketAddress& addr) override;
  50. int Send(const void* pv, size_t cb) override;
  51. int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
  52. int Recv(void* pv, size_t cb, int64_t* timestamp) override;
  53. int RecvFrom(void* pv,
  54. size_t cb,
  55. SocketAddress* paddr,
  56. int64_t* timestamp) override;
  57. int Listen(int backlog) override;
  58. AsyncSocket* Accept(SocketAddress* paddr) override;
  59. int Close() override;
  60. int GetError() const override;
  61. void SetError(int error) override;
  62. ConnState GetState() const override;
  63. int GetOption(Option opt, int* value) override;
  64. int SetOption(Option opt, int value) override;
  65. protected:
  66. virtual void OnConnectEvent(AsyncSocket* socket);
  67. virtual void OnReadEvent(AsyncSocket* socket);
  68. virtual void OnWriteEvent(AsyncSocket* socket);
  69. virtual void OnCloseEvent(AsyncSocket* socket, int err);
  70. AsyncSocket* socket_;
  71. };
  72. } // namespace rtc
  73. #endif // RTC_BASE_ASYNC_SOCKET_H_