| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/test/channel_transport/udp_socket_posix.h" | 11 #include "webrtc/test/channel_transport/udp_socket_posix.h" |
| 12 | 12 |
| 13 #include <errno.h> | 13 #include <errno.h> |
| 14 #include <fcntl.h> | 14 #include <fcntl.h> |
| 15 #include <netdb.h> | 15 #include <netdb.h> |
| 16 #include <stdio.h> | 16 #include <stdio.h> |
| 17 #include <string.h> | 17 #include <string.h> |
| 18 #include <sys/ioctl.h> | 18 #include <sys/ioctl.h> |
| 19 #include <sys/types.h> | 19 #include <sys/types.h> |
| 20 #include <time.h> | 20 #include <time.h> |
| 21 #include <unistd.h> | 21 #include <unistd.h> |
| 22 | 22 |
| 23 #include "webrtc/system_wrappers/include/trace.h" | 23 #include "webrtc/system_wrappers/include/trace.h" |
| 24 #include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h" | 24 #include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h" |
| 25 #include "webrtc/test/channel_transport/udp_socket_wrapper.h" | 25 #include "webrtc/test/channel_transport/udp_socket_wrapper.h" |
| 26 | 26 |
| 27 namespace webrtc { | 27 namespace webrtc { |
| 28 namespace test { | 28 namespace test { |
| 29 UdpSocketPosix::UdpSocketPosix(const int32_t id, UdpSocketManager* mgr, | 29 UdpSocketPosix::UdpSocketPosix(const int32_t id, UdpSocketManager* mgr, |
| 30 bool ipV6Enable) : _id(id) | 30 bool ipV6Enable) |
| 31 : _id(id), |
| 32 _closeBlockingCompletedCond(true, false), |
| 33 _readyForDeletionCond(true, false) |
| 31 { | 34 { |
| 32 WEBRTC_TRACE(kTraceMemory, kTraceTransport, id, | 35 WEBRTC_TRACE(kTraceMemory, kTraceTransport, id, |
| 33 "UdpSocketPosix::UdpSocketPosix()"); | 36 "UdpSocketPosix::UdpSocketPosix()"); |
| 34 | 37 |
| 35 _wantsIncoming = false; | 38 _wantsIncoming = false; |
| 36 _mgr = mgr; | 39 _mgr = mgr; |
| 37 | 40 |
| 38 _obj = NULL; | 41 _obj = NULL; |
| 39 _incomingCb = NULL; | 42 _incomingCb = NULL; |
| 40 _readyForDeletionCond = ConditionVariableWrapper::CreateConditionVariable(); | |
| 41 _closeBlockingCompletedCond = | |
| 42 ConditionVariableWrapper::CreateConditionVariable(); | |
| 43 _cs = CriticalSectionWrapper::CreateCriticalSection(); | |
| 44 _readyForDeletion = false; | 43 _readyForDeletion = false; |
| 45 _closeBlockingActive = false; | 44 _closeBlockingActive = false; |
| 46 _closeBlockingCompleted= false; | 45 _closeBlockingCompleted = false; |
| 47 if(ipV6Enable) | 46 if(ipV6Enable) |
| 48 { | 47 { |
| 49 _socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); | 48 _socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
| 50 } | 49 } |
| 51 else { | 50 else { |
| 52 _socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 51 _socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 53 } | 52 } |
| 54 | 53 |
| 55 // Set socket to nonblocking mode. | 54 // Set socket to nonblocking mode. |
| 56 int enable_non_blocking = 1; | 55 int enable_non_blocking = 1; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 } | 67 } |
| 69 } | 68 } |
| 70 | 69 |
| 71 UdpSocketPosix::~UdpSocketPosix() | 70 UdpSocketPosix::~UdpSocketPosix() |
| 72 { | 71 { |
| 73 if(_socket != INVALID_SOCKET) | 72 if(_socket != INVALID_SOCKET) |
| 74 { | 73 { |
| 75 close(_socket); | 74 close(_socket); |
| 76 _socket = INVALID_SOCKET; | 75 _socket = INVALID_SOCKET; |
| 77 } | 76 } |
| 78 if(_readyForDeletionCond) | |
| 79 { | |
| 80 delete _readyForDeletionCond; | |
| 81 } | |
| 82 | |
| 83 if(_closeBlockingCompletedCond) | |
| 84 { | |
| 85 delete _closeBlockingCompletedCond; | |
| 86 } | |
| 87 | |
| 88 if(_cs) | |
| 89 { | |
| 90 delete _cs; | |
| 91 } | |
| 92 } | 77 } |
| 93 | 78 |
| 94 bool UdpSocketPosix::SetCallback(CallbackObj obj, IncomingSocketCallback cb) | 79 bool UdpSocketPosix::SetCallback(CallbackObj obj, IncomingSocketCallback cb) |
| 95 { | 80 { |
| 96 _obj = obj; | 81 _obj = obj; |
| 97 _incomingCb = cb; | 82 _incomingCb = cb; |
| 98 | 83 |
| 99 WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id, | 84 WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id, |
| 100 "UdpSocketPosix(%p)::SetCallback", this); | 85 "UdpSocketPosix(%p)::SetCallback", this); |
| 101 | 86 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 _incomingCb(_obj, buf, retval, &from); | 205 _incomingCb(_obj, buf, retval, &from); |
| 221 } | 206 } |
| 222 break; | 207 break; |
| 223 } | 208 } |
| 224 } | 209 } |
| 225 | 210 |
| 226 bool UdpSocketPosix::WantsIncoming() { return _wantsIncoming; } | 211 bool UdpSocketPosix::WantsIncoming() { return _wantsIncoming; } |
| 227 | 212 |
| 228 void UdpSocketPosix::CloseBlocking() | 213 void UdpSocketPosix::CloseBlocking() |
| 229 { | 214 { |
| 230 _cs->Enter(); | 215 rtc::CritScope lock(&_cs); |
| 231 _closeBlockingActive = true; | 216 _closeBlockingActive = true; |
| 232 if(!CleanUp()) | 217 if(!CleanUp()) |
| 233 { | 218 { |
| 234 _closeBlockingActive = false; | 219 _closeBlockingActive = false; |
| 235 _cs->Leave(); | |
| 236 return; | 220 return; |
| 237 } | 221 } |
| 238 | 222 |
| 239 while(!_readyForDeletion) | 223 if(!_readyForDeletion) |
| 240 { | 224 { |
| 241 _readyForDeletionCond->SleepCS(*_cs); | 225 _cs.Leave(); |
| 226 _readyForDeletionCond.Wait(rtc::Event::kForever); |
| 227 _cs.Enter(); |
| 242 } | 228 } |
| 243 _closeBlockingCompleted = true; | 229 _closeBlockingCompleted = true; |
| 244 _closeBlockingCompletedCond->Wake(); | 230 _closeBlockingCompletedCond.Set(); |
| 245 _cs->Leave(); | |
| 246 } | 231 } |
| 247 | 232 |
| 248 void UdpSocketPosix::ReadyForDeletion() | 233 void UdpSocketPosix::ReadyForDeletion() |
| 249 { | 234 { |
| 250 _cs->Enter(); | 235 rtc::CritScope lock(&_cs); |
| 251 if(!_closeBlockingActive) | 236 if(!_closeBlockingActive) |
| 252 { | 237 { |
| 253 _cs->Leave(); | |
| 254 return; | 238 return; |
| 255 } | 239 } |
| 240 |
| 256 close(_socket); | 241 close(_socket); |
| 257 _socket = INVALID_SOCKET; | 242 _socket = INVALID_SOCKET; |
| 258 _readyForDeletion = true; | 243 _readyForDeletion = true; |
| 259 _readyForDeletionCond->Wake(); | 244 _readyForDeletionCond.Set(); |
| 260 while(!_closeBlockingCompleted) | 245 if(!_closeBlockingCompleted) |
| 261 { | 246 { |
| 262 _closeBlockingCompletedCond->SleepCS(*_cs); | 247 _cs.Leave(); |
| 248 _closeBlockingCompletedCond.Wait(rtc::Event::kForever); |
| 249 _cs.Enter(); |
| 263 } | 250 } |
| 264 _cs->Leave(); | |
| 265 } | 251 } |
| 266 | 252 |
| 267 bool UdpSocketPosix::CleanUp() | 253 bool UdpSocketPosix::CleanUp() |
| 268 { | 254 { |
| 269 _wantsIncoming = false; | 255 _wantsIncoming = false; |
| 270 | 256 |
| 271 if (_socket == INVALID_SOCKET) | 257 if (_socket == INVALID_SOCKET) |
| 272 { | 258 { |
| 273 return false; | 259 return false; |
| 274 } | 260 } |
| 275 | 261 |
| 276 WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id, | 262 WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id, |
| 277 "calling UdpSocketManager::RemoveSocket()..."); | 263 "calling UdpSocketManager::RemoveSocket()..."); |
| 278 _mgr->RemoveSocket(this); | 264 _mgr->RemoveSocket(this); |
| 279 // After this, the socket should may be or will be as deleted. Return | 265 // After this, the socket should may be or will be as deleted. Return |
| 280 // immediately. | 266 // immediately. |
| 281 return true; | 267 return true; |
| 282 } | 268 } |
| 283 | 269 |
| 284 } // namespace test | 270 } // namespace test |
| 285 } // namespace webrtc | 271 } // namespace webrtc |
| OLD | NEW |