Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, 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_ASYNCPACKETSOCKET_H_
     29 #define TALK_BASE_ASYNCPACKETSOCKET_H_
     30 
     31 #include "talk/base/sigslot.h"
     32 #include "talk/base/socket.h"
     33 
     34 namespace talk_base {
     35 
     36 // Provides the ability to receive packets asynchronously. Sends are not
     37 // buffered since it is acceptable to drop packets under high load.
     38 class AsyncPacketSocket : public sigslot::has_slots<> {
     39  public:
     40   AsyncPacketSocket() { }
     41   virtual ~AsyncPacketSocket() { }
     42 
     43   // Returns current local address. If port or IP address is not
     44   // assigned yet, then they set to 0 in the result and |allocated| is
     45   // set to false. Otherwise |allocated| is set to true.
     46   virtual SocketAddress GetLocalAddress(bool* allocated) const = 0;
     47 
     48   // Returns remote address. Returns zeroes if this is not a client TCP socket.
     49   virtual SocketAddress GetRemoteAddress() const = 0;
     50 
     51   // Send a packet.
     52   virtual int Send(const void *pv, size_t cb) = 0;
     53   virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
     54 
     55   // Close the socket.
     56   virtual int Close() = 0;
     57 
     58   // Returns current state of the socket.
     59   virtual Socket::ConnState GetState() const = 0;
     60 
     61   // Get/set options.
     62   virtual int GetOption(Socket::Option opt, int* value) = 0;
     63   virtual int SetOption(Socket::Option opt, int value) = 0;
     64 
     65   // Get/Set current error.
     66   // TODO: Do we really need SetError() here?
     67   virtual int GetError() const = 0;
     68   virtual void SetError(int error) = 0;
     69 
     70   // Emitted after address for the socket is allocated.
     71   sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
     72 
     73   // Emitted each time a packet is read. Used only for UDP and
     74   // connected TCP sockets.
     75   sigslot::signal4<AsyncPacketSocket*, const char*, size_t,
     76                    const SocketAddress&> SignalReadPacket;
     77 
     78   // Used only for connected TCP sockets.
     79   sigslot::signal1<AsyncPacketSocket*> SignalConnect;
     80   sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
     81 
     82   // Used only for listening TCP sockets.
     83   sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
     84 
     85  private:
     86   DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
     87 };
     88 
     89 }  // namespace talk_base
     90 
     91 #endif  // TALK_BASE_ASYNCPACKETSOCKET_H_
     92