1af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org/*
2af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org *  Copyright (c) 2012 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_wrapper.h"
12af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
13af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <stdlib.h>
14af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include <string.h>
15af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
16af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/system_wrappers/interface/event_wrapper.h"
17af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/system_wrappers/interface/trace.h"
18af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#include "webrtc/test/channel_transport/udp_socket_manager_wrapper.h"
19af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
20af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#if defined(_WIN32)
21af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    #include "webrtc/test/channel_transport/udp_socket2_win.h"
22af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#else
23af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    #include "webrtc/test/channel_transport/udp_socket_posix.h"
24af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#endif
25af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
26af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
27af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgnamespace webrtc {
28af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgnamespace test {
29af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
30af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketWrapper::_initiated = false;
31af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
32af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org// Temporary Android hack. The value 1024 is taken from
33af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org// <ndk>/build/platforms/android-1.5/arch-arm/usr/include/linux/posix_types.h
34af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org// TODO (tomasl): can we remove this now?
35af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#ifndef FD_SETSIZE
36af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#define FD_SETSIZE 1024
37af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#endif
38af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
39af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgUdpSocketWrapper::UdpSocketWrapper()
40af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    : _wantsIncoming(false),
41af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _deleteEvent(NULL)
42af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
43af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
44af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
45af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgUdpSocketWrapper::~UdpSocketWrapper()
46af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
47af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if(_deleteEvent)
48af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
49af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _deleteEvent->Set();
50af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org      _deleteEvent = NULL;
51af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
52af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
53af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
54af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgvoid UdpSocketWrapper::SetEventToNull()
55af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
56af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (_deleteEvent)
57af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
58af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _deleteEvent = NULL;
59af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
60af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
61af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
6291cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.orgUdpSocketWrapper* UdpSocketWrapper::CreateSocket(const int32_t id,
63af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                                 UdpSocketManager* mgr,
64af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                                 CallbackObj obj,
65af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                                 IncomingSocketCallback cb,
66af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                                 bool ipV6Enable,
67af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                                                 bool disableGQOS)
68af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
69af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
70af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    WEBRTC_TRACE(kTraceMemory, kTraceTransport, id,
71af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                 "UdpSocketWrapper::CreateSocket");
72af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
73af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    UdpSocketWrapper* s = 0;
74af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
75af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#ifdef _WIN32
76af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (!_initiated)
77af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
78af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        WSADATA wsaData;
79af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        WORD wVersionRequested = MAKEWORD( 2, 2 );
8091cab7186d535be98c837285ce24918188e9c8c3pbos@webrtc.org        int32_t err = WSAStartup( wVersionRequested, &wsaData);
81af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if (err != 0)
82af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
83af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            WEBRTC_TRACE(
84af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                kTraceError,
85af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                kTraceTransport,
86af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                id,
87af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                "UdpSocketWrapper::CreateSocket failed to initialize sockets\
88af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org WSAStartup error:%d",
89af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                err);
90af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            return NULL;
91af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
92af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
93af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _initiated = true;
94af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
95af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
96af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    s = new UdpSocket2Windows(id, mgr, ipV6Enable, disableGQOS);
97af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
98af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#else
99af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (!_initiated)
100af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
101af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        _initiated = true;
102af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
103af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    s = new UdpSocketPosix(id, mgr, ipV6Enable);
104af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (s)
105af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
106af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        UdpSocketPosix* sl = static_cast<UdpSocketPosix*>(s);
107af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if (sl->GetFd() != INVALID_SOCKET && sl->GetFd() < FD_SETSIZE)
108af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
109af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            // ok
110af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        } else
111af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
112af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            WEBRTC_TRACE(
113af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                kTraceError,
114af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                kTraceTransport,
115af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                id,
116af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                "UdpSocketWrapper::CreateSocket failed to initialize socket");
117af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            delete s;
118af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            s = NULL;
119af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
120af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
121af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org#endif
122af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    if (s)
123af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    {
124af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        s->_deleteEvent = NULL;
125af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        if (!s->SetCallback(obj, cb))
126af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        {
127af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            WEBRTC_TRACE(
128af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                kTraceError,
129af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                kTraceTransport,
130af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                id,
131af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org                "UdpSocketWrapper::CreateSocket failed to ser callback");
132af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org            return(NULL);
133af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org        }
134af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    }
135af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return s;
136af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
137af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
138af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketWrapper::StartReceiving()
139af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
140af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _wantsIncoming = true;
141af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return true;
142af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
143af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
14477c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.orgbool UdpSocketWrapper::StartReceiving(const uint32_t /*receiveBuffers*/) {
14577c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org  return StartReceiving();
14677c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org}
14777c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org
148af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.orgbool UdpSocketWrapper::StopReceiving()
149af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org{
150af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    _wantsIncoming = false;
151af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org    return true;
152af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}
153af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org
15477c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.orgint32_t UdpSocketWrapper::SetPCP(const int32_t /*pcp*/) { return -1; }
15577c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org
15677c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.orguint32_t UdpSocketWrapper::ReceiveBuffers() { return 0; }
15777c6d714d2f8f700170218784553df1a48f17885pbos@webrtc.org
158af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}  // namespace test
159af6aa7b49ba5fbfd09f901ae15b5278121d10f6fpwestin@webrtc.org}  // namespace webrtc
160