Home | History | Annotate | Download | only in channel_transport
      1 /*
      2  *  Copyright (c) 2012 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_TEST_CHANNEL_TRANSPORT_UDP_SOCKET2_WINDOWS_H_
     12 #define WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET2_WINDOWS_H_
     13 
     14 // Disable deprication warning from traffic.h
     15 #pragma warning(disable : 4995)
     16 
     17 // Don't change include order for these header files.
     18 #include <Winsock2.h>
     19 #include <Ntddndis.h>
     20 #include <traffic.h>
     21 
     22 #include "webrtc/system_wrappers/interface/atomic32.h"
     23 #include "webrtc/system_wrappers/interface/condition_variable_wrapper.h"
     24 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     25 #include "webrtc/system_wrappers/interface/event_wrapper.h"
     26 #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
     27 #include "webrtc/system_wrappers/interface/trace.h"
     28 #include "webrtc/test/channel_transport/udp_socket2_manager_win.h"
     29 #include "webrtc/test/channel_transport/udp_socket_wrapper.h"
     30 
     31 namespace webrtc {
     32 namespace test {
     33 
     34 class UdpSocket2ManagerWindows;
     35 class TrafficControlWindows;
     36 struct PerIoContext;
     37 
     38 class UdpSocket2Windows : public UdpSocketWrapper
     39 {
     40 public:
     41     UdpSocket2Windows(const int32_t id, UdpSocketManager* mgr,
     42                       bool ipV6Enable = false, bool disableGQOS = false);
     43     virtual ~UdpSocket2Windows();
     44 
     45     virtual int32_t ChangeUniqueId(const int32_t id) OVERRIDE;
     46 
     47     virtual bool ValidHandle() OVERRIDE;
     48 
     49     virtual bool SetCallback(CallbackObj, IncomingSocketCallback) OVERRIDE;
     50 
     51     virtual bool Bind(const SocketAddress& name) OVERRIDE;
     52     virtual bool SetSockopt(int32_t level, int32_t optname,
     53                             const int8_t* optval, int32_t optlen) OVERRIDE;
     54 
     55     virtual bool StartReceiving(const uint32_t receiveBuffers) OVERRIDE;
     56     virtual inline bool StartReceiving() OVERRIDE {return StartReceiving(8);}
     57     virtual bool StopReceiving() OVERRIDE;
     58 
     59     virtual int32_t SendTo(const int8_t* buf, int32_t len,
     60                            const SocketAddress& to) OVERRIDE;
     61 
     62     virtual void CloseBlocking() OVERRIDE;
     63 
     64     SOCKET GetFd() { return _socket;}
     65 
     66     virtual bool SetQos(int32_t serviceType, int32_t tokenRate,
     67                         int32_t bucketSize, int32_t peekBandwith,
     68                         int32_t minPolicedSize, int32_t maxSduSize,
     69                         const SocketAddress &stRemName,
     70                         int32_t overrideDSCP = 0) OVERRIDE;
     71 
     72     virtual int32_t SetTOS(const int32_t serviceType) OVERRIDE;
     73     virtual int32_t SetPCP(const int32_t pcp) OVERRIDE;
     74 
     75     virtual uint32_t ReceiveBuffers() OVERRIDE {return _receiveBuffers.Value();}
     76 
     77 protected:
     78     void IOCompleted(PerIoContext* pIOContext, uint32_t ioSize, uint32_t error);
     79 
     80     int32_t PostRecv();
     81     // Use pIoContext to post a new WSARecvFrom(..).
     82     int32_t PostRecv(PerIoContext* pIoContext);
     83 
     84 private:
     85     friend class UdpSocket2WorkerWindows;
     86 
     87     // Set traffic control (TC) flow adding it the interface that matches this
     88     // sockets address.
     89     // A filter is created and added to the flow.
     90     // The flow consists of:
     91     // (1) QoS send and receive information (flow specifications).
     92     // (2) A DS object (for specifying exact DSCP value).
     93     // (3) Possibly a traffic object (for specifying exact 802.1p priority (PCP)
     94     //     value).
     95     //
     96     // dscp values:
     97     // -1   don't change the current dscp value.
     98     // 0    don't add any flow to TC, unless pcp is specified.
     99     // 1-63 Add a flow to TC with the specified dscp value.
    100     // pcp values:
    101     // -2  Don't add pcp info to the flow, (3) will not be added.
    102     // -1  Don't change the current value.
    103     // 0-7 Add pcp info to the flow with the specified value,
    104     //     (3) will be added.
    105     //
    106     // If both dscp and pcp are -1 no flow will be created or added to TC.
    107     // If dscp is 0 and pcp is 0-7 (1), (2) and (3) will be created.
    108     // Note: input parameter values are assumed to be in valid range, checks
    109     // must be done by caller.
    110     int32_t SetTrafficControl(int32_t dscp, int32_t pcp,
    111                               const struct sockaddr_in* name,
    112                               FLOWSPEC* send = NULL,
    113                               FLOWSPEC* recv = NULL);
    114     int32_t CreateFlowSpec(int32_t serviceType,
    115                            int32_t tokenRate,
    116                            int32_t bucketSize,
    117                            int32_t peekBandwith,
    118                            int32_t minPolicedSize,
    119                            int32_t maxSduSize, FLOWSPEC *f);
    120 
    121     int32_t _id;
    122     RWLockWrapper* _ptrCbRWLock;
    123     IncomingSocketCallback _incomingCb;
    124     CallbackObj _obj;
    125     bool _qos;
    126 
    127     SocketAddress _remoteAddr;
    128     SOCKET _socket;
    129     int32_t _iProtocol;
    130     UdpSocket2ManagerWindows* _mgr;
    131 
    132     CriticalSectionWrapper* _pCrit;
    133     Atomic32 _outstandingCalls;
    134     Atomic32 _outstandingCallComplete;
    135     volatile bool _terminate;
    136     volatile bool _addedToMgr;
    137 
    138     CriticalSectionWrapper* _ptrDeleteCrit;
    139     ConditionVariableWrapper* _ptrDeleteCond;
    140     bool _safeTodelete;
    141 
    142     RWLockWrapper* _ptrDestRWLock;
    143     bool _outstandingCallsDisabled;
    144     bool NewOutstandingCall();
    145     void OutstandingCallCompleted();
    146     void DisableNewOutstandingCalls();
    147     void WaitForOutstandingCalls();
    148 
    149     void RemoveSocketFromManager();
    150 
    151     // RWLockWrapper is used as a reference counter for the socket. Write lock
    152     // is used for creating and deleting socket. Read lock is used for
    153     // accessing the socket.
    154     RWLockWrapper* _ptrSocketRWLock;
    155     bool AquireSocket();
    156     void ReleaseSocket();
    157     bool InvalidateSocket();
    158 
    159     // Traffic control handles and structure pointers.
    160     HANDLE _clientHandle;
    161     HANDLE _flowHandle;
    162     HANDLE _filterHandle;
    163     PTC_GEN_FLOW _flow;
    164     // TrafficControlWindows implements TOS and PCP.
    165     TrafficControlWindows* _gtc;
    166     // Holds the current pcp value. Can be -2 or 0 - 7.
    167     int _pcp;
    168 
    169     Atomic32 _receiveBuffers;
    170 };
    171 
    172 }  // namespace test
    173 }  // namespace webrtc
    174 
    175 #endif  // WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET2_WINDOWS_H_
    176