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