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/dscp.h" 32 #include "talk/base/sigslot.h" 33 #include "talk/base/socket.h" 34 #include "talk/base/timeutils.h" 35 36 namespace talk_base { 37 38 // This structure will have the information about when packet is actually 39 // received by socket. 40 struct PacketTime { 41 PacketTime() : timestamp(-1), not_before(-1) {} 42 PacketTime(int64 timestamp, int64 not_before) 43 : timestamp(timestamp), not_before(not_before) { 44 } 45 46 int64 timestamp; // Receive time after socket delivers the data. 47 int64 not_before; // Earliest possible time the data could have arrived, 48 // indicating the potential error in the |timestamp| value, 49 // in case the system, is busy. For example, the time of 50 // the last select() call. 51 // If unknown, this value will be set to zero. 52 }; 53 54 inline PacketTime CreatePacketTime(int64 not_before) { 55 return PacketTime(TimeMicros(), not_before); 56 } 57 58 // Provides the ability to receive packets asynchronously. Sends are not 59 // buffered since it is acceptable to drop packets under high load. 60 class AsyncPacketSocket : public sigslot::has_slots<> { 61 public: 62 enum State { 63 STATE_CLOSED, 64 STATE_BINDING, 65 STATE_BOUND, 66 STATE_CONNECTING, 67 STATE_CONNECTED 68 }; 69 70 AsyncPacketSocket() { } 71 virtual ~AsyncPacketSocket() { } 72 73 // Returns current local address. Address may be set to NULL if the 74 // socket is not bound yet (GetState() returns STATE_BINDING). 75 virtual SocketAddress GetLocalAddress() const = 0; 76 77 // Returns remote address. Returns zeroes if this is not a client TCP socket. 78 virtual SocketAddress GetRemoteAddress() const = 0; 79 80 // Send a packet. 81 virtual int Send(const void *pv, size_t cb, DiffServCodePoint dscp) = 0; 82 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr, 83 DiffServCodePoint) = 0; 84 85 // Close the socket. 86 virtual int Close() = 0; 87 88 // Returns current state of the socket. 89 virtual State GetState() const = 0; 90 91 // Get/set options. 92 virtual int GetOption(Socket::Option opt, int* value) = 0; 93 virtual int SetOption(Socket::Option opt, int value) = 0; 94 95 // Get/Set current error. 96 // TODO: Remove SetError(). 97 virtual int GetError() const = 0; 98 virtual void SetError(int error) = 0; 99 100 // Emitted each time a packet is read. Used only for UDP and 101 // connected TCP sockets. 102 sigslot::signal5<AsyncPacketSocket*, const char*, size_t, 103 const SocketAddress&, 104 const PacketTime&> SignalReadPacket; 105 106 // Emitted when the socket is currently able to send. 107 sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend; 108 109 // Emitted after address for the socket is allocated, i.e. binding 110 // is finished. State of the socket is changed from BINDING to BOUND 111 // (for UDP and server TCP sockets) or CONNECTING (for client TCP 112 // sockets). 113 sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady; 114 115 // Emitted for client TCP sockets when state is changed from 116 // CONNECTING to CONNECTED. 117 sigslot::signal1<AsyncPacketSocket*> SignalConnect; 118 119 // Emitted for client TCP sockets when state is changed from 120 // CONNECTED to CLOSED. 121 sigslot::signal2<AsyncPacketSocket*, int> SignalClose; 122 123 // Used only for listening TCP sockets. 124 sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection; 125 126 private: 127 DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket); 128 }; 129 130 } // namespace talk_base 131 132 #endif // TALK_BASE_ASYNCPACKETSOCKET_H_ 133