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