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 #include "webrtc/test/channel_transport/udp_socket_wrapper.h" 12 13 #include <stdlib.h> 14 #include <string.h> 15 16 #include "webrtc/system_wrappers/include/event_wrapper.h" 17 #include "webrtc/system_wrappers/include/trace.h" 18 #include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h" 19 20 #if defined(_WIN32) 21 #include "webrtc/test/channel_transport/udp_socket2_win.h" 22 #else 23 #include "webrtc/test/channel_transport/udp_socket_posix.h" 24 #endif 25 26 27 namespace webrtc { 28 namespace test { 29 30 bool UdpSocketWrapper::_initiated = false; 31 32 // Temporary Android hack. The value 1024 is taken from 33 // <ndk>/build/platforms/android-1.5/arch-arm/usr/include/linux/posix_types.h 34 // TODO (tomasl): can we remove this now? 35 #ifndef FD_SETSIZE 36 #define FD_SETSIZE 1024 37 #endif 38 39 UdpSocketWrapper::UdpSocketWrapper() 40 : _wantsIncoming(false), 41 _deleteEvent(NULL) 42 { 43 } 44 45 UdpSocketWrapper::~UdpSocketWrapper() 46 { 47 if(_deleteEvent) 48 { 49 _deleteEvent->Set(); 50 _deleteEvent = NULL; 51 } 52 } 53 54 void UdpSocketWrapper::SetEventToNull() 55 { 56 if (_deleteEvent) 57 { 58 _deleteEvent = NULL; 59 } 60 } 61 62 UdpSocketWrapper* UdpSocketWrapper::CreateSocket(const int32_t id, 63 UdpSocketManager* mgr, 64 CallbackObj obj, 65 IncomingSocketCallback cb, 66 bool ipV6Enable, 67 bool disableGQOS) 68 69 { 70 WEBRTC_TRACE(kTraceMemory, kTraceTransport, id, 71 "UdpSocketWrapper::CreateSocket"); 72 73 UdpSocketWrapper* s = 0; 74 75 #ifdef _WIN32 76 if (!_initiated) 77 { 78 WSADATA wsaData; 79 WORD wVersionRequested = MAKEWORD( 2, 2 ); 80 int32_t err = WSAStartup( wVersionRequested, &wsaData); 81 if (err != 0) 82 { 83 WEBRTC_TRACE( 84 kTraceError, 85 kTraceTransport, 86 id, 87 "UdpSocketWrapper::CreateSocket failed to initialize sockets\ 88 WSAStartup error:%d", 89 err); 90 return NULL; 91 } 92 93 _initiated = true; 94 } 95 96 s = new UdpSocket2Windows(id, mgr, ipV6Enable, disableGQOS); 97 98 #else 99 if (!_initiated) 100 { 101 _initiated = true; 102 } 103 s = new UdpSocketPosix(id, mgr, ipV6Enable); 104 if (s) 105 { 106 UdpSocketPosix* sl = static_cast<UdpSocketPosix*>(s); 107 if (sl->GetFd() != INVALID_SOCKET && sl->GetFd() < FD_SETSIZE) 108 { 109 // ok 110 } else 111 { 112 WEBRTC_TRACE( 113 kTraceError, 114 kTraceTransport, 115 id, 116 "UdpSocketWrapper::CreateSocket failed to initialize socket"); 117 delete s; 118 s = NULL; 119 } 120 } 121 #endif 122 if (s) 123 { 124 s->_deleteEvent = NULL; 125 if (!s->SetCallback(obj, cb)) 126 { 127 WEBRTC_TRACE( 128 kTraceError, 129 kTraceTransport, 130 id, 131 "UdpSocketWrapper::CreateSocket failed to ser callback"); 132 return(NULL); 133 } 134 } 135 return s; 136 } 137 138 bool UdpSocketWrapper::StartReceiving() 139 { 140 _wantsIncoming = true; 141 return true; 142 } 143 144 bool UdpSocketWrapper::StartReceiving(const uint32_t /*receiveBuffers*/) { 145 return StartReceiving(); 146 } 147 148 bool UdpSocketWrapper::StopReceiving() 149 { 150 _wantsIncoming = false; 151 return true; 152 } 153 154 int32_t UdpSocketWrapper::SetPCP(const int32_t /*pcp*/) { return -1; } 155 156 uint32_t UdpSocketWrapper::ReceiveBuffers() { return 0; } 157 158 } // namespace test 159 } // namespace webrtc 160