Home | History | Annotate | Download | only in channel_transport
      1 /*
      2  *  Copyright (c) 2011 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 #include "webrtc/test/channel_transport/traffic_control_win.h"
     12 
     13 #include <assert.h>
     14 
     15 #include "webrtc/system_wrappers/include/trace.h"
     16 
     17 namespace webrtc {
     18 namespace test {
     19 
     20 TrafficControlWindows* TrafficControlWindows::instance = NULL;
     21 uint32_t TrafficControlWindows::refCounter = 0;
     22 
     23 TrafficControlWindows::TrafficControlWindows(const int32_t id)
     24 {
     25 }
     26 
     27 TrafficControlWindows* TrafficControlWindows::GetInstance(
     28     const int32_t id)
     29 {
     30     if(instance != NULL)
     31     {
     32         WEBRTC_TRACE(
     33             kTraceDebug,
     34             kTraceTransport,
     35             id,
     36             "TrafficControlWindows - Returning already created object");
     37         refCounter++;
     38         return instance;
     39     }
     40 
     41     WEBRTC_TRACE(kTraceMemory, kTraceTransport, id,
     42                  "TrafficControlWindows - Creating new object");
     43     instance = new TrafficControlWindows(id);
     44     if(instance == NULL)
     45     {
     46         WEBRTC_TRACE(kTraceMemory, kTraceTransport, id,
     47                      "TrafficControlWindows - Error allocating memory");
     48         return NULL;
     49     }
     50 
     51     instance->tcRegister = NULL;
     52     instance->tcDeregister = NULL;
     53 
     54     instance->tcEnumerate = NULL;
     55     instance->tcOpenInterface = NULL;
     56     instance->tcCloseInterface = NULL;
     57 
     58     instance->tcAddFlow = NULL;
     59     instance->tcDeleteFlow = NULL;
     60 
     61     instance->tcAddFilter = NULL;
     62     instance->tcDeleteFilter = NULL;
     63 
     64     HMODULE trafficLib = LoadLibrary(TEXT("traffic.dll"));
     65     if(trafficLib == NULL)
     66     {
     67         WEBRTC_TRACE(
     68             kTraceWarning,
     69             kTraceTransport,
     70             id,
     71             "TrafficControlWindows - No QOS support, LoadLibrary returned NULL,\
     72  last error: %d\n",
     73             GetLastError());
     74         delete instance;
     75         instance = NULL;
     76         return NULL;
     77     }
     78 
     79     instance->tcRegister = (registerFn)GetProcAddress(trafficLib,
     80                                                       "TcRegisterClient");
     81     instance->tcDeregister = (deregisterFn)GetProcAddress(trafficLib,
     82                                                           "TcDeregisterClient");
     83     instance->tcEnumerate = (enumerateFn)GetProcAddress(
     84         trafficLib,
     85         "TcEnumerateInterfaces");
     86     instance->tcOpenInterface = (openInterfaceFn)GetProcAddress(
     87         trafficLib,
     88         "TcOpenInterfaceW");
     89     instance->tcCloseInterface = (closeInterfaceFn)GetProcAddress(
     90         trafficLib,
     91         "TcCloseInterface");
     92     instance->tcAddFlow = (flowAddFn)GetProcAddress(trafficLib,
     93                                                     "TcAddFlow");
     94     instance->tcDeleteFlow = (flowDeleteFn)GetProcAddress(trafficLib,
     95                                                           "TcDeleteFlow");
     96 
     97     instance->tcAddFilter = (filterAddFn)GetProcAddress(trafficLib,
     98                                                         "TcAddFilter");
     99     instance->tcDeleteFilter = (filterDeleteFn)GetProcAddress(trafficLib,
    100                                                               "TcDeleteFilter");
    101 
    102     if(instance->tcRegister       == NULL ||
    103        instance->tcDeregister     == NULL ||
    104        instance->tcEnumerate      == NULL ||
    105        instance->tcOpenInterface  == NULL ||
    106        instance->tcCloseInterface == NULL ||
    107        instance->tcAddFlow        == NULL ||
    108        instance->tcAddFilter      == NULL ||
    109        instance->tcDeleteFlow     == NULL ||
    110        instance->tcDeleteFilter   == NULL)
    111     {
    112         delete instance;
    113         instance = NULL;
    114         WEBRTC_TRACE(
    115             kTraceError,
    116             kTraceTransport,
    117             id,
    118             "TrafficControlWindows - Could not find function pointer for\
    119  traffic control functions");
    120         WEBRTC_TRACE(
    121             kTraceError,
    122             kTraceTransport,
    123             id,
    124             "Tcregister    : %x, tcDeregister: %x, tcEnumerate: %x,\
    125  tcOpenInterface: %x, tcCloseInterface: %x, tcAddFlow: %x, tcAddFilter: %x,\
    126  tcDeleteFlow: %x, tcDeleteFilter: %x",
    127             instance->tcRegister,
    128             instance->tcDeregister,
    129             instance->tcEnumerate,
    130             instance->tcOpenInterface,
    131             instance->tcCloseInterface,
    132             instance->tcAddFlow,
    133             instance->tcAddFilter,
    134             instance->tcDeleteFlow,
    135             instance->tcDeleteFilter );
    136         return NULL;
    137     }
    138     refCounter++;
    139     return instance;
    140 }
    141 
    142 void TrafficControlWindows::Release(TrafficControlWindows* gtc)
    143 {
    144     if (0 == refCounter)
    145     {
    146         WEBRTC_TRACE(kTraceError, kTraceTransport, -1,
    147                      "TrafficControlWindows - Cannot release, refCounter is 0");
    148         return;
    149     }
    150     if (NULL == gtc)
    151     {
    152         WEBRTC_TRACE(kTraceDebug, kTraceTransport, -1,
    153                      "TrafficControlWindows - Not releasing, gtc is NULL");
    154         return;
    155     }
    156 
    157     WEBRTC_TRACE(kTraceDebug, kTraceTransport, -1,
    158                  "TrafficControlWindows - Releasing object");
    159     refCounter--;
    160     if ((0 == refCounter) && instance)
    161     {
    162         WEBRTC_TRACE(kTraceMemory, kTraceTransport, -1,
    163                      "TrafficControlWindows - Deleting object");
    164         delete instance;
    165         instance = NULL;
    166     }
    167 }
    168 
    169 ULONG TrafficControlWindows::TcRegisterClient(
    170     ULONG TciVersion,
    171     HANDLE ClRegCtx,
    172     PTCI_CLIENT_FUNC_LIST ClientHandlerList,
    173     PHANDLE pClientHandle)
    174 {
    175     assert(tcRegister != NULL);
    176 
    177     return tcRegister(TciVersion, ClRegCtx, ClientHandlerList, pClientHandle);
    178 }
    179 
    180 ULONG TrafficControlWindows::TcDeregisterClient(HANDLE clientHandle)
    181 {
    182     assert(tcDeregister != NULL);
    183 
    184     return tcDeregister(clientHandle);
    185 }
    186 
    187 
    188 ULONG TrafficControlWindows::TcEnumerateInterfaces(
    189     HANDLE ClientHandle,
    190     PULONG pBufferSize,
    191     PTC_IFC_DESCRIPTOR interfaceBuffer)
    192 {
    193     assert(tcEnumerate != NULL);
    194 
    195     return tcEnumerate(ClientHandle, pBufferSize, interfaceBuffer);
    196 }
    197 
    198 
    199 ULONG TrafficControlWindows::TcOpenInterfaceW(LPWSTR pInterfaceName,
    200                                               HANDLE ClientHandle,
    201                                               HANDLE ClIfcCtx,
    202                                               PHANDLE pIfcHandle)
    203 {
    204     assert(tcOpenInterface != NULL);
    205 
    206     return tcOpenInterface(pInterfaceName, ClientHandle, ClIfcCtx, pIfcHandle);
    207 
    208 }
    209 
    210 ULONG TrafficControlWindows::TcCloseInterface(HANDLE IfcHandle)
    211 {
    212     assert(tcCloseInterface != NULL);
    213 
    214     return tcCloseInterface(IfcHandle);
    215 }
    216 
    217 ULONG TrafficControlWindows::TcAddFlow(HANDLE IfcHandle, HANDLE ClFlowCtx,
    218                                        ULONG  Flags, PTC_GEN_FLOW pGenericFlow,
    219                                        PHANDLE pFlowHandle)
    220 {
    221     assert(tcAddFlow != NULL);
    222     return tcAddFlow(IfcHandle, ClFlowCtx, Flags, pGenericFlow, pFlowHandle);
    223 }
    224 
    225 ULONG TrafficControlWindows::TcAddFilter(HANDLE FlowHandle,
    226                                          PTC_GEN_FILTER pGenericFilter,
    227                                          PHANDLE pFilterHandle)
    228 {
    229     assert(tcAddFilter != NULL);
    230     return tcAddFilter(FlowHandle, pGenericFilter, pFilterHandle);
    231 }
    232 
    233 ULONG TrafficControlWindows::TcDeleteFlow(HANDLE FlowHandle)
    234 {
    235     assert(tcDeleteFlow != NULL);
    236     return tcDeleteFlow(FlowHandle);
    237 
    238 }
    239 
    240 ULONG TrafficControlWindows::TcDeleteFilter(HANDLE FilterHandle)
    241 {
    242     assert(tcDeleteFilter != NULL);
    243     return tcDeleteFilter(FilterHandle);
    244 }
    245 
    246 void MyClNotifyHandler(HANDLE ClRegCtx, HANDLE ClIfcCtx, ULONG Event,
    247                        HANDLE SubCode, ULONG BufSize, PVOID Buffer)
    248 {
    249 }
    250 
    251 }  // namespace test
    252 }  // namespace webrtc
    253