Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2010, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_BASE_ASYNCSOCKET_H_
     29 #define TALK_BASE_ASYNCSOCKET_H_
     30 
     31 #include "talk/base/common.h"
     32 #include "talk/base/sigslot.h"
     33 #include "talk/base/socket.h"
     34 
     35 namespace talk_base {
     36 
     37 // TODO: Remove Socket and rename AsyncSocket to Socket.
     38 
     39 // Provides the ability to perform socket I/O asynchronously.
     40 class AsyncSocket : public Socket {
     41  public:
     42   AsyncSocket();
     43   virtual ~AsyncSocket();
     44 
     45   virtual AsyncSocket* Accept(SocketAddress* paddr) = 0;
     46 
     47   sigslot::signal1<AsyncSocket*> SignalReadEvent;        // ready to read
     48   sigslot::signal1<AsyncSocket*> SignalWriteEvent;       // ready to write
     49   sigslot::signal1<AsyncSocket*> SignalConnectEvent;     // connected
     50   sigslot::signal2<AsyncSocket*, int> SignalCloseEvent;  // closed
     51 };
     52 
     53 class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
     54  public:
     55   // The adapted socket may explicitly be NULL, and later assigned using Attach.
     56   // However, subclasses which support detached mode must override any methods
     57   // that will be called during the detached period (usually GetState()), to
     58   // avoid dereferencing a null pointer.
     59   explicit AsyncSocketAdapter(AsyncSocket* socket);
     60   virtual ~AsyncSocketAdapter();
     61   void Attach(AsyncSocket* socket);
     62   virtual SocketAddress GetLocalAddress() const {
     63     return socket_->GetLocalAddress();
     64   }
     65   virtual SocketAddress GetRemoteAddress() const {
     66     return socket_->GetRemoteAddress();
     67   }
     68   virtual int Bind(const SocketAddress& addr) {
     69     return socket_->Bind(addr);
     70   }
     71   virtual int Connect(const SocketAddress& addr) {
     72     return socket_->Connect(addr);
     73   }
     74   virtual int Send(const void* pv, size_t cb) {
     75     return socket_->Send(pv, cb);
     76   }
     77   virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
     78     return socket_->SendTo(pv, cb, addr);
     79   }
     80   virtual int Recv(void* pv, size_t cb) {
     81     return socket_->Recv(pv, cb);
     82   }
     83   virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
     84     return socket_->RecvFrom(pv, cb, paddr);
     85   }
     86   virtual int Listen(int backlog) {
     87     return socket_->Listen(backlog);
     88   }
     89   virtual AsyncSocket* Accept(SocketAddress* paddr) {
     90     return socket_->Accept(paddr);
     91   }
     92   virtual int Close() {
     93     return socket_->Close();
     94   }
     95   virtual int GetError() const {
     96     return socket_->GetError();
     97   }
     98   virtual void SetError(int error) {
     99     return socket_->SetError(error);
    100   }
    101   virtual ConnState GetState() const {
    102     return socket_->GetState();
    103   }
    104   virtual int EstimateMTU(uint16* mtu) {
    105     return socket_->EstimateMTU(mtu);
    106   }
    107   virtual int GetOption(Option opt, int* value) {
    108     return socket_->GetOption(opt, value);
    109   }
    110   virtual int SetOption(Option opt, int value) {
    111     return socket_->SetOption(opt, value);
    112   }
    113 
    114  protected:
    115   virtual void OnConnectEvent(AsyncSocket* socket) {
    116     SignalConnectEvent(this);
    117   }
    118   virtual void OnReadEvent(AsyncSocket* socket) {
    119     SignalReadEvent(this);
    120   }
    121   virtual void OnWriteEvent(AsyncSocket* socket) {
    122     SignalWriteEvent(this);
    123   }
    124   virtual void OnCloseEvent(AsyncSocket* socket, int err) {
    125     SignalCloseEvent(this, err);
    126   }
    127 
    128   AsyncSocket* socket_;
    129 };
    130 
    131 }  // namespace talk_base
    132 
    133 #endif  // TALK_BASE_ASYNCSOCKET_H_
    134