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