1af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org/*
2af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *
4af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  Use of this source code is governed by a BSD-style license
5af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  that can be found in the LICENSE file in the root of the source
6af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  tree. An additional intellectual property rights grant can be found
7af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  in the file PATENTS.  All contributing project authors may
8af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org */
10af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
11af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/test/channel_transport/udp_socket_manager_posix.h"
12af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
1396001c8e03bf364c81f5f86a2fadb371934d0383pbos@webrtc.org#include <stdio.h>
14af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <strings.h>
15af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <sys/time.h>
16af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <sys/types.h>
17af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <time.h>
18af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <unistd.h>
19af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
20c9f8871d3a2d7366efd9b89a2dded9bfefeb8440hta@webrtc.org#include "webrtc/system_wrappers/interface/sleep.h"
21af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/system_wrappers/interface/trace.h"
22af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/test/channel_transport/udp_socket_posix.h"
23af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
24af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgnamespace webrtc {
25af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgnamespace test {
26af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
27af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgUdpSocketManagerPosix::UdpSocketManagerPosix()
28af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    : UdpSocketManager(),
29af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _id(-1),
30af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _critSect(CriticalSectionWrapper::CreateCriticalSection()),
31af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _numberOfSocketMgr(-1),
32af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _incSocketMgrNextTime(0),
33af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _nextSocketMgrToAssign(0),
34af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _socketMgr()
35af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
36af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
37af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
3891cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.orgbool UdpSocketManagerPosix::Init(int32_t id, uint8_t& numOfWorkThreads) {
39af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    CriticalSectionScoped cs(_critSect);
40af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if ((_id != -1) || (_numOfWorkThreads != 0)) {
41af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        assert(_id != -1);
42af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        assert(_numOfWorkThreads != 0);
43af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        return false;
44af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
45af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
46af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _id = id;
47af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _numberOfSocketMgr = numOfWorkThreads;
48af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _numOfWorkThreads = numOfWorkThreads;
49af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
50af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(MAX_NUMBER_OF_SOCKET_MANAGERS_LINUX < _numberOfSocketMgr)
51af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
52af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _numberOfSocketMgr = MAX_NUMBER_OF_SOCKET_MANAGERS_LINUX;
53af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
54af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    for(int i = 0;i < _numberOfSocketMgr; i++)
55af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
56af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _socketMgr[i] = new UdpSocketManagerPosixImpl();
57af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
58af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return true;
59af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
60af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
61af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
62af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgUdpSocketManagerPosix::~UdpSocketManagerPosix()
63af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
64af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    Stop();
65af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id,
66af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix(%d)::UdpSocketManagerPosix()",
67af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 _numberOfSocketMgr);
68af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
69af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    for(int i = 0;i < _numberOfSocketMgr; i++)
70af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
71af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        delete _socketMgr[i];
72af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
73af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    delete _critSect;
74af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
75af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
7691cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.orgint32_t UdpSocketManagerPosix::ChangeUniqueId(const int32_t id)
77af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
78af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _id = id;
79af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return 0;
80af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
81af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
82af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosix::Start()
83af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
84af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id,
85af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix(%d)::Start()",
86af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 _numberOfSocketMgr);
87af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
88af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Enter();
89af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    bool retVal = true;
90af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    for(int i = 0;i < _numberOfSocketMgr && retVal; i++)
91af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
92af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        retVal = _socketMgr[i]->Start();
93af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
94af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(!retVal)
95af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
96af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        WEBRTC_TRACE(
97af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceError,
98af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceTransport,
99af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _id,
100af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            "UdpSocketManagerPosix(%d)::Start() error starting socket managers",
101af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _numberOfSocketMgr);
102af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
103af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Leave();
104af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return retVal;
105af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
106af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
107af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosix::Stop()
108af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
109af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id,
110af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix(%d)::Stop()",_numberOfSocketMgr);
111af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
112af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Enter();
113af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    bool retVal = true;
114af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    for(int i = 0; i < _numberOfSocketMgr && retVal; i++)
115af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
116af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        retVal = _socketMgr[i]->Stop();
117af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
118af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(!retVal)
119af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
120af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        WEBRTC_TRACE(
121af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceError,
122af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceTransport,
123af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _id,
124af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            "UdpSocketManagerPosix(%d)::Stop() there are still active socket "
125af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            "managers",
126af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _numberOfSocketMgr);
127af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
128af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Leave();
129af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return retVal;
130af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
131af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
132af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosix::AddSocket(UdpSocketWrapper* s)
133af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
134af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id,
135af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix(%d)::AddSocket()",_numberOfSocketMgr);
136af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
137af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Enter();
138af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    bool retVal = _socketMgr[_nextSocketMgrToAssign]->AddSocket(s);
139af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(!retVal)
140af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
141af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        WEBRTC_TRACE(
142af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceError,
143af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceTransport,
144af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _id,
145af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            "UdpSocketManagerPosix(%d)::AddSocket() failed to add socket to\
146af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org manager",
147af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _numberOfSocketMgr);
148af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
149af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
150af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // Distribute sockets on UdpSocketManagerPosixImpls in a round-robin
151af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // fashion.
152af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(_incSocketMgrNextTime == 0)
153af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
154af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _incSocketMgrNextTime++;
155af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    } else {
156af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _incSocketMgrNextTime = 0;
157af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _nextSocketMgrToAssign++;
158af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if(_nextSocketMgrToAssign >= _numberOfSocketMgr)
159af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
160af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _nextSocketMgrToAssign = 0;
161af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
162af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
163af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Leave();
164af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return retVal;
165af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
166af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
167af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosix::RemoveSocket(UdpSocketWrapper* s)
168af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
169af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceDebug, kTraceTransport, _id,
170af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix(%d)::RemoveSocket()",
171af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 _numberOfSocketMgr);
172af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
173af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Enter();
174af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    bool retVal = false;
175af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    for(int i = 0;i < _numberOfSocketMgr && (retVal == false); i++)
176af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
177af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        retVal = _socketMgr[i]->RemoveSocket(s);
178af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
179af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(!retVal)
180af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
181af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        WEBRTC_TRACE(
182af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceError,
183af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            kTraceTransport,
184af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _id,
185af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            "UdpSocketManagerPosix(%d)::RemoveSocket() failed to remove socket\
186af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org from manager",
187af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _numberOfSocketMgr);
188af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
189af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSect->Leave();
190af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return retVal;
191af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
192af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
193af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
194af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgUdpSocketManagerPosixImpl::UdpSocketManagerPosixImpl()
195af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
196af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList = CriticalSectionWrapper::CreateCriticalSection();
197af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _thread = ThreadWrapper::CreateThread(UdpSocketManagerPosixImpl::Run, this,
198af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                          kRealtimePriority,
199af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                          "UdpSocketManagerPosixImplThread");
200af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    FD_ZERO(&_readFds);
201af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceMemory,  kTraceTransport, -1,
202af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix created");
203af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
204af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
205af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgUdpSocketManagerPosixImpl::~UdpSocketManagerPosixImpl()
206af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
207af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(_thread != NULL)
208af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
209af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        delete _thread;
210af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
211af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
212af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (_critSectList != NULL)
213af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
214af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        UpdateSocketMap();
215af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
216af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _critSectList->Enter();
21752c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        for (std::map<SOCKET, UdpSocketPosix*>::iterator it =
21852c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org                 _socketMap.begin();
21952c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org             it != _socketMap.end();
22052c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org             ++it) {
22152c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org          delete it->second;
222af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
22352c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        _socketMap.clear();
224af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _critSectList->Leave();
225af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
226af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        delete _critSectList;
227af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
228af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
229af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceMemory,  kTraceTransport, -1,
230af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketManagerPosix deleted");
231af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
232af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
233af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosixImpl::Start()
234af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
235af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    unsigned int id = 0;
236af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (_thread == NULL)
237af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
238af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        return false;
239af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
240af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
241af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceStateInfo,  kTraceTransport, -1,
242af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "Start UdpSocketManagerPosix");
243af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return _thread->Start(id);
244af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
245af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
246af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosixImpl::Stop()
247af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
248af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (_thread == NULL)
249af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
250af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        return true;
251af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
252af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
253af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceStateInfo,  kTraceTransport, -1,
254af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "Stop UdpSocketManagerPosix");
255af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return _thread->Stop();
256af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
257af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
258af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosixImpl::Process()
259af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
260af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    bool doSelect = false;
261af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // Timeout = 1 second.
262af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    struct timeval timeout;
263af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    timeout.tv_sec = 0;
264af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    timeout.tv_usec = 10000;
265af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
266af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    FD_ZERO(&_readFds);
267af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
268af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UpdateSocketMap();
269af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
27052c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org    SOCKET maxFd = 0;
27152c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org    for (std::map<SOCKET, UdpSocketPosix*>::iterator it = _socketMap.begin();
27252c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org         it != _socketMap.end();
27352c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org         ++it) {
27452c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      doSelect = true;
27552c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      if (it->first > maxFd)
27652c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        maxFd = it->first;
27752c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      FD_SET(it->first, &_readFds);
278af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
279af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
280af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    int num = 0;
281af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (doSelect)
282af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
283af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        num = select(maxFd+1, &_readFds, NULL, NULL, &timeout);
284af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
285af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if (num == SOCKET_ERROR)
286af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
287af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            // Timeout = 10 ms.
288c9f8871d3a2d7366efd9b89a2dded9bfefeb8440hta@webrtc.org            SleepMs(10);
289af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            return true;
290af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
291af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }else
292af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
293af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        // Timeout = 10 ms.
294c9f8871d3a2d7366efd9b89a2dded9bfefeb8440hta@webrtc.org        SleepMs(10);
295af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        return true;
296af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
297af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
29852c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org    for (std::map<SOCKET, UdpSocketPosix*>::iterator it = _socketMap.begin();
29952c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org         it != _socketMap.end();
30052c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org         ++it) {
30152c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      if (FD_ISSET(it->first, &_readFds)) {
30252c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        it->second->HasIncoming();
30352c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        --num;
30452c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      }
305af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
30652c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org
307af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return true;
308af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
309af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
310af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosixImpl::Run(ThreadObj obj)
311af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
312af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UdpSocketManagerPosixImpl* mgr =
313af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        static_cast<UdpSocketManagerPosixImpl*>(obj);
314af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return mgr->Process();
315af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
316af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
317af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosixImpl::AddSocket(UdpSocketWrapper* s)
318af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
319af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UdpSocketPosix* sl = static_cast<UdpSocketPosix*>(s);
320af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(sl->GetFd() == INVALID_SOCKET || !(sl->GetFd() < FD_SETSIZE))
321af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
322af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        return false;
323af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
324af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList->Enter();
325083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    _addList.push_back(s);
326af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList->Leave();
327af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return true;
328af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
329af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
330af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketManagerPosixImpl::RemoveSocket(UdpSocketWrapper* s)
331af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
332af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // Put in remove list if this is the correct UdpSocketManagerPosixImpl.
333af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList->Enter();
334af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
335af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // If the socket is in the add list it's safe to remove and delete it.
336083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    for (SocketList::iterator iter = _addList.begin();
337083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org         iter != _addList.end(); ++iter) {
338083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org        UdpSocketPosix* addSocket = static_cast<UdpSocketPosix*>(*iter);
339af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        unsigned int addFD = addSocket->GetFd();
340af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        unsigned int removeFD = static_cast<UdpSocketPosix*>(s)->GetFd();
341af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if(removeFD == addFD)
342af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
343083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org            _removeList.push_back(removeFD);
344af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            _critSectList->Leave();
345af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            return true;
346af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
347af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
348af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
349af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // Checking the socket map is safe since all Erase and Insert calls to this
350af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // map are also protected by _critSectList.
35152c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org    if (_socketMap.find(static_cast<UdpSocketPosix*>(s)->GetFd()) !=
35252c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        _socketMap.end()) {
353083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org      _removeList.push_back(static_cast<UdpSocketPosix*>(s)->GetFd());
35452c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      _critSectList->Leave();
35552c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org      return true;
356af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
357af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList->Leave();
358af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return false;
359af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
360af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
361af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgvoid UdpSocketManagerPosixImpl::UpdateSocketMap()
362af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
363af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // Remove items in remove list.
364af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList->Enter();
365083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    for (FdList::iterator iter = _removeList.begin();
366083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org         iter != _removeList.end(); ++iter) {
367af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        UdpSocketPosix* deleteSocket = NULL;
368083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org        SOCKET removeFD = *iter;
369af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
370af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        // If the socket is in the add list it hasn't been added to the socket
371af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        // map yet. Just remove the socket from the add list.
372083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org        for (SocketList::iterator iter = _addList.begin();
373083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org             iter != _addList.end(); ++iter) {
374083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org            UdpSocketPosix* addSocket = static_cast<UdpSocketPosix*>(*iter);
37552c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org            SOCKET addFD = addSocket->GetFd();
376af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            if(removeFD == addFD)
377af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            {
378af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                deleteSocket = addSocket;
379083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org                _addList.erase(iter);
380af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                break;
381af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            }
382af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
383af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
384af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        // Find and remove socket from _socketMap.
38552c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        std::map<SOCKET, UdpSocketPosix*>::iterator it =
38652c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org            _socketMap.find(removeFD);
38752c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        if(it != _socketMap.end())
388af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
38952c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org          deleteSocket = it->second;
39052c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org          _socketMap.erase(it);
391af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
392af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if(deleteSocket)
393af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
394af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            deleteSocket->ReadyForDeletion();
395af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            delete deleteSocket;
396af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
397af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
398083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    _removeList.clear();
399af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
400af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    // Add sockets from add list.
401083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    for (SocketList::iterator iter = _addList.begin();
402083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org         iter != _addList.end(); ++iter) {
403083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org        UdpSocketPosix* s = static_cast<UdpSocketPosix*>(*iter);
40452c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org        if(s) {
40552c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org          _socketMap[s->GetFd()] = s;
406af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
407af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
408083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    _addList.clear();
409af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _critSectList->Leave();
410af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
411af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
412af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}  // namespace test
413af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}  // namespace webrtc
414