Home | History | Annotate | Download | only in base
      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 WEBRTC_BASE_ASYNCUDPSOCKET_H_
     12 #define WEBRTC_BASE_ASYNCUDPSOCKET_H_
     13 
     14 #include "webrtc/base/asyncpacketsocket.h"
     15 #include "webrtc/base/scoped_ptr.h"
     16 #include "webrtc/base/socketfactory.h"
     17 
     18 namespace rtc {
     19 
     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 
     36   SocketAddress GetLocalAddress() const override;
     37   SocketAddress GetRemoteAddress() const override;
     38   int Send(const void* pv,
     39            size_t cb,
     40            const rtc::PacketOptions& options) override;
     41   int SendTo(const void* pv,
     42              size_t cb,
     43              const SocketAddress& addr,
     44              const rtc::PacketOptions& options) override;
     45   int Close() override;
     46 
     47   State GetState() const override;
     48   int GetOption(Socket::Option opt, int* value) override;
     49   int SetOption(Socket::Option opt, int value) override;
     50   int GetError() const override;
     51   void SetError(int error) override;
     52 
     53  private:
     54   // Called when the underlying socket is ready to be read from.
     55   void OnReadEvent(AsyncSocket* socket);
     56   // Called when the underlying socket is ready to send.
     57   void OnWriteEvent(AsyncSocket* socket);
     58 
     59   scoped_ptr<AsyncSocket> socket_;
     60   char* buf_;
     61   size_t size_;
     62 };
     63 
     64 }  // namespace rtc
     65 
     66 #endif  // WEBRTC_BASE_ASYNCUDPSOCKET_H_
     67