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