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