1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if USE(LIBWEBRTC)
29
30#include <WebCore/LibWebRTCProvider.h>
31#include <webrtc/rtc_base/asyncpacketsocket.h>
32#include <wtf/Deque.h>
33#include <wtf/Forward.h>
34
35namespace IPC {
36class Connection;
37class DataReference;
38class Decoder;
39}
40
41namespace WebCore {
42class SharedBuffer;
43}
44
45namespace WebKit {
46
47class LibWebRTCSocketFactory;
48
49class LibWebRTCSocket final : public rtc::AsyncPacketSocket {
50 WTF_MAKE_FAST_ALLOCATED;
51public:
52 enum class Type { UDP, ServerTCP, ClientTCP, ServerConnectionTCP };
53
54 LibWebRTCSocket(LibWebRTCSocketFactory&, uint64_t identifier, Type, const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress);
55 ~LibWebRTCSocket();
56
57 uint64_t identifier() const { return m_identifier; }
58 const rtc::SocketAddress& localAddress() const { return m_localAddress; }
59 const rtc::SocketAddress& remoteAddress() const { return m_remoteAddress; }
60
61 void setError(int error) { m_error = error; }
62 void setState(State state) { m_state = state; }
63
64private:
65 bool willSend(size_t);
66
67 friend class WebRTCSocket;
68 void signalReadPacket(const WebCore::SharedBuffer&, rtc::SocketAddress&&, int64_t);
69 void signalSentPacket(int, int64_t);
70 void signalAddressReady(const rtc::SocketAddress&);
71 void signalConnect();
72 void signalClose(int);
73 void signalNewConnection(rtc::AsyncPacketSocket*);
74
75 // AsyncPacketSocket API
76 int GetError() const final { return m_error; }
77 void SetError(int error) final { setError(error); }
78 rtc::SocketAddress GetLocalAddress() const final;
79 rtc::SocketAddress GetRemoteAddress() const final;
80 int Send(const void *pv, size_t cb, const rtc::PacketOptions& options) final { return SendTo(pv, cb, m_remoteAddress, options); }
81 int SendTo(const void *, size_t, const rtc::SocketAddress&, const rtc::PacketOptions&) final;
82 int Close() final;
83 State GetState() const final { return m_state; }
84 int GetOption(rtc::Socket::Option, int*) final;
85 int SetOption(rtc::Socket::Option, int) final;
86
87 static void sendOnMainThread(Function<void(IPC::Connection&)>&&);
88
89 LibWebRTCSocketFactory& m_factory;
90 uint64_t m_identifier { 0 };
91 Type m_type;
92 rtc::SocketAddress m_localAddress;
93 rtc::SocketAddress m_remoteAddress;
94
95 int m_error { 0 };
96 State m_state { STATE_BINDING };
97
98 static const unsigned MAX_SOCKET_OPTION { rtc::Socket::OPT_RTP_SENDTIME_EXTN_ID + 1 };
99 Optional<int> m_options[MAX_SOCKET_OPTION];
100
101 Deque<size_t> m_beingSentPacketSizes;
102 size_t m_availableSendingBytes { 65536 };
103 bool m_shouldSignalReadyToSend { false };
104};
105
106} // namespace WebKit
107
108#endif // USE(LIBWEBRTC)
109