henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_ASYNC_UDP_SOCKET_H_ |
| 12 | #define RTC_BASE_ASYNC_UDP_SOCKET_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <memory> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 17 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/async_packet_socket.h" |
| 19 | #include "rtc_base/async_socket.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/socket_address.h" |
| 22 | #include "rtc_base/socket_factory.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 72 | #endif // RTC_BASE_ASYNC_UDP_SOCKET_H_ |