15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/* 25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle 35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2004--2005, Google Inc. 45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without 65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met: 75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 1. Redistributions of source code must retain the above copyright notice, 95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * this list of conditions and the following disclaimer. 105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 2. Redistributions in binary form must reproduce the above copyright notice, 115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * this list of conditions and the following disclaimer in the documentation 125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * and/or other materials provided with the distribution. 135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 3. The name of the author may not be used to endorse or promote products 145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * derived from this software without specific prior written permission. 155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifndef TALK_BASE_ASYNCPACKETSOCKET_H_ 295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define TALK_BASE_ASYNCPACKETSOCKET_H_ 305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 313a6035d31f360cb7a9420b7934176494b09721b3mallinath@webrtc.org#include "talk/base/dscp.h" 325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/sigslot.h" 335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/socket.h" 34d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org#include "talk/base/timeutils.h" 355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace talk_base { 375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 38d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org// This structure will have the information about when packet is actually 39d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org// received by socket. 40d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.orgstruct PacketTime { 41fbb1efa61417540336cf52976d4ff4394bfa795bwu@webrtc.org PacketTime() : timestamp(-1), not_before(-1) {} 42d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org PacketTime(int64 timestamp, int64 not_before) 43d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org : timestamp(timestamp), not_before(not_before) { 44d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org } 45d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org 46d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org int64 timestamp; // Receive time after socket delivers the data. 47d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org int64 not_before; // Earliest possible time the data could have arrived, 48d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org // indicating the potential error in the |timestamp| value, 49d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org // in case the system, is busy. For example, the time of 50d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org // the last select() call. 51d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org // If unknown, this value will be set to zero. 52d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org}; 53d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org 54d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.orginline PacketTime CreatePacketTime(int64 not_before) { 55d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org return PacketTime(TimeMicros(), not_before); 56d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org} 57d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org 585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Provides the ability to receive packets asynchronously. Sends are not 595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// buffered since it is acceptable to drop packets under high load. 605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass AsyncPacketSocket : public sigslot::has_slots<> { 615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public: 625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org enum State { 635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org STATE_CLOSED, 645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org STATE_BINDING, 655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org STATE_BOUND, 665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org STATE_CONNECTING, 675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org STATE_CONNECTED 685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org }; 695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org AsyncPacketSocket() { } 715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual ~AsyncPacketSocket() { } 725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Returns current local address. Address may be set to NULL if the 745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // socket is not bound yet (GetState() returns STATE_BINDING). 755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual SocketAddress GetLocalAddress() const = 0; 765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Returns remote address. Returns zeroes if this is not a client TCP socket. 785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual SocketAddress GetRemoteAddress() const = 0; 795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Send a packet. 813a6035d31f360cb7a9420b7934176494b09721b3mallinath@webrtc.org virtual int Send(const void *pv, size_t cb, DiffServCodePoint dscp) = 0; 823a6035d31f360cb7a9420b7934176494b09721b3mallinath@webrtc.org virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr, 833a6035d31f360cb7a9420b7934176494b09721b3mallinath@webrtc.org DiffServCodePoint) = 0; 845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Close the socket. 865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual int Close() = 0; 875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Returns current state of the socket. 895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual State GetState() const = 0; 905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Get/set options. 925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual int GetOption(Socket::Option opt, int* value) = 0; 935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual int SetOption(Socket::Option opt, int value) = 0; 945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Get/Set current error. 965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // TODO: Remove SetError(). 975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual int GetError() const = 0; 985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org virtual void SetError(int error) = 0; 995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Emitted each time a packet is read. Used only for UDP and 1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // connected TCP sockets. 102d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org sigslot::signal5<AsyncPacketSocket*, const char*, size_t, 103d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org const SocketAddress&, 104d3ecbb30dc2684653d61e8ec88a5382aecf62773wu@webrtc.org const PacketTime&> SignalReadPacket; 1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Emitted when the socket is currently able to send. 1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend; 1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Emitted after address for the socket is allocated, i.e. binding 1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // is finished. State of the socket is changed from BINDING to BOUND 1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // (for UDP and server TCP sockets) or CONNECTING (for client TCP 1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // sockets). 1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady; 1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Emitted for client TCP sockets when state is changed from 1165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // CONNECTING to CONNECTED. 1175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org sigslot::signal1<AsyncPacketSocket*> SignalConnect; 1185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Emitted for client TCP sockets when state is changed from 1205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // CONNECTED to CLOSED. 1215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org sigslot::signal2<AsyncPacketSocket*, int> SignalClose; 1225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Used only for listening TCP sockets. 1245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection; 1255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private: 1275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket); 1285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}; 1295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} // namespace talk_base 1315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif // TALK_BASE_ASYNCPACKETSOCKET_H_ 133