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#ifndef WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET_MANAGER_POSIX_H_
12af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#define WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET_MANAGER_POSIX_H_
13af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
14af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <sys/types.h>
15af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <unistd.h>
16af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
17083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org#include <list>
1852c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org#include <map>
1952c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org
20af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
21af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/system_wrappers/interface/thread_wrapper.h"
22af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h"
23af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/test/channel_transport/udp_socket_wrapper.h"
24af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
25af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgnamespace webrtc {
26af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
27af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgclass ConditionVariableWrapper;
28af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
29af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgnamespace test {
30af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
3152c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.orgclass UdpSocketPosix;
32af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgclass UdpSocketManagerPosixImpl;
33af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#define MAX_NUMBER_OF_SOCKET_MANAGERS_LINUX 8
34af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
35af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgclass UdpSocketManagerPosix : public UdpSocketManager
36af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
37af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgpublic:
38af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UdpSocketManagerPosix();
39af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    virtual ~UdpSocketManagerPosix();
40af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
4177c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org    virtual bool Init(int32_t id, uint8_t& numOfWorkThreads) OVERRIDE;
42af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
4377c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org    virtual int32_t ChangeUniqueId(const int32_t id) OVERRIDE;
44af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
4577c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org    virtual bool Start() OVERRIDE;
4677c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org    virtual bool Stop() OVERRIDE;
47af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
4877c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org    virtual bool AddSocket(UdpSocketWrapper* s) OVERRIDE;
4977c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org    virtual bool RemoveSocket(UdpSocketWrapper* s) OVERRIDE;
50af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgprivate:
5191cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.org    int32_t _id;
52af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    CriticalSectionWrapper* _critSect;
5391cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.org    uint8_t _numberOfSocketMgr;
5491cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.org    uint8_t _incSocketMgrNextTime;
5591cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.org    uint8_t _nextSocketMgrToAssign;
56af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UdpSocketManagerPosixImpl* _socketMgr[MAX_NUMBER_OF_SOCKET_MANAGERS_LINUX];
57af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org};
58af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
59af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgclass UdpSocketManagerPosixImpl
60af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
61af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgpublic:
62af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UdpSocketManagerPosixImpl();
63af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    virtual ~UdpSocketManagerPosixImpl();
64af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
65af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    virtual bool Start();
66af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    virtual bool Stop();
67af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
68af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    virtual bool AddSocket(UdpSocketWrapper* s);
69af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    virtual bool RemoveSocket(UdpSocketWrapper* s);
70af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
71af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgprotected:
72af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    static bool Run(ThreadObj obj);
73af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    bool Process();
74af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    void UpdateSocketMap();
75af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
76af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgprivate:
77083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    typedef std::list<UdpSocketWrapper*> SocketList;
78083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    typedef std::list<SOCKET> FdList;
79af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    ThreadWrapper* _thread;
80af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    CriticalSectionWrapper* _critSectList;
81af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
82af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    fd_set _readFds;
83af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
8452c5c708de6f5bcf9c2a58cece45013aa5efacd7pbos@webrtc.org    std::map<SOCKET, UdpSocketPosix*> _socketMap;
85083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    SocketList _addList;
86083049f2945b2924203b25c8428300d40d994f35henrike@webrtc.org    FdList _removeList;
87af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org};
88af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
89af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}  // namespace test
90af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}  // namespace webrtc
91af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
92af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#endif  // WEBRTC_TEST_CHANNEL_TRANSPORT_UDP_SOCKET_MANAGER_POSIX_H_
93