async_udp_socket.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_UDP_SOCKET_H_
  11. #define RTC_BASE_ASYNC_UDP_SOCKET_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include "rtc_base/async_packet_socket.h"
  15. #include "rtc_base/async_socket.h"
  16. #include "rtc_base/socket.h"
  17. #include "rtc_base/socket_address.h"
  18. #include "rtc_base/socket_factory.h"
  19. namespace rtc {
  20. // Provides the ability to receive packets asynchronously. Sends are not
  21. // buffered since it is acceptable to drop packets under high load.
  22. class AsyncUDPSocket : public AsyncPacketSocket {
  23. public:
  24. // Binds |socket| and creates AsyncUDPSocket for it. Takes ownership
  25. // of |socket|. Returns null if bind() fails (|socket| is destroyed
  26. // in that case).
  27. static AsyncUDPSocket* Create(AsyncSocket* socket,
  28. const SocketAddress& bind_address);
  29. // Creates a new socket for sending asynchronous UDP packets using an
  30. // asynchronous socket from the given factory.
  31. static AsyncUDPSocket* Create(SocketFactory* factory,
  32. const SocketAddress& bind_address);
  33. explicit AsyncUDPSocket(AsyncSocket* socket);
  34. ~AsyncUDPSocket() override;
  35. SocketAddress GetLocalAddress() const override;
  36. SocketAddress GetRemoteAddress() const override;
  37. int Send(const void* pv,
  38. size_t cb,
  39. const rtc::PacketOptions& options) override;
  40. int SendTo(const void* pv,
  41. size_t cb,
  42. const SocketAddress& addr,
  43. const rtc::PacketOptions& options) override;
  44. int Close() override;
  45. State GetState() const override;
  46. int GetOption(Socket::Option opt, int* value) override;
  47. int SetOption(Socket::Option opt, int value) override;
  48. int GetError() const override;
  49. void SetError(int error) override;
  50. private:
  51. // Called when the underlying socket is ready to be read from.
  52. void OnReadEvent(AsyncSocket* socket);
  53. // Called when the underlying socket is ready to send.
  54. void OnWriteEvent(AsyncSocket* socket);
  55. std::unique_ptr<AsyncSocket> socket_;
  56. char* buf_;
  57. size_t size_;
  58. };
  59. } // namespace rtc
  60. #endif // RTC_BASE_ASYNC_UDP_SOCKET_H_