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 #ifndef WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET_MANAGER_POSIX_H_
12 #define WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET_MANAGER_POSIX_H_
13 
14 #include <sys/types.h>
15 #include <unistd.h>
16 
17 #include <list>
18 #include <map>
19 
20 #include "webrtc/base/platform_thread.h"
21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
22 #include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h"
23 #include "webrtc/test/channel_transport/udp_socket_wrapper.h"
24 
25 namespace webrtc {
26 
27 class ConditionVariableWrapper;
28 
29 namespace test {
30 
31 class UdpSocketPosix;
32 class UdpSocketManagerPosixImpl;
33 #define MAX_NUMBER_OF_SOCKET_MANAGERS_LINUX 8
34 
35 class UdpSocketManagerPosix : public UdpSocketManager
36 {
37 public:
38     UdpSocketManagerPosix();
39     virtual ~UdpSocketManagerPosix();
40 
41     bool Init(int32_t id, uint8_t& numOfWorkThreads) override;
42 
43     bool Start() override;
44     bool Stop() override;
45 
46     bool AddSocket(UdpSocketWrapper* s) override;
47     bool RemoveSocket(UdpSocketWrapper* s) override;
48 
49 private:
50     int32_t _id;
51     CriticalSectionWrapper* _critSect;
52     uint8_t _numberOfSocketMgr;
53     uint8_t _incSocketMgrNextTime;
54     uint8_t _nextSocketMgrToAssign;
55     UdpSocketManagerPosixImpl* _socketMgr[MAX_NUMBER_OF_SOCKET_MANAGERS_LINUX];
56 };
57 
58 class UdpSocketManagerPosixImpl
59 {
60 public:
61     UdpSocketManagerPosixImpl();
62     virtual ~UdpSocketManagerPosixImpl();
63 
64     virtual bool Start();
65     virtual bool Stop();
66 
67     virtual bool AddSocket(UdpSocketWrapper* s);
68     virtual bool RemoveSocket(UdpSocketWrapper* s);
69 
70 protected:
71     static bool Run(void* obj);
72     bool Process();
73     void UpdateSocketMap();
74 
75 private:
76     typedef std::list<UdpSocketWrapper*> SocketList;
77     typedef std::list<SOCKET> FdList;
78     rtc::PlatformThread _thread;
79     CriticalSectionWrapper* _critSectList;
80 
81     fd_set _readFds;
82 
83     std::map<SOCKET, UdpSocketPosix*> _socketMap;
84     SocketList _addList;
85     FdList _removeList;
86 };
87 
88 }  // namespace test
89 }  // namespace webrtc
90 
91 #endif  // WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET_MANAGER_POSIX_H_
92