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 | #include "config.h" |
27 | #include "LibWebRTCSocketClient.h" |
28 | |
29 | #if USE(LIBWEBRTC) |
30 | |
31 | #include "Connection.h" |
32 | #include "DataReference.h" |
33 | #include "NetworkRTCProvider.h" |
34 | #include "WebRTCSocketMessages.h" |
35 | #include <WebCore/SharedBuffer.h> |
36 | #include <wtf/Function.h> |
37 | |
38 | namespace WebKit { |
39 | |
40 | LibWebRTCSocketClient::LibWebRTCSocketClient(uint64_t identifier, NetworkRTCProvider& rtcProvider, std::unique_ptr<rtc::AsyncPacketSocket>&& socket, Type type) |
41 | : m_identifier(identifier) |
42 | , m_type(type) |
43 | , m_rtcProvider(rtcProvider) |
44 | , m_socket(WTFMove(socket)) |
45 | { |
46 | ASSERT(m_socket); |
47 | |
48 | m_socket->SignalReadPacket.connect(this, &LibWebRTCSocketClient::signalReadPacket); |
49 | m_socket->SignalSentPacket.connect(this, &LibWebRTCSocketClient::signalSentPacket); |
50 | m_socket->SignalClose.connect(this, &LibWebRTCSocketClient::signalClose); |
51 | |
52 | switch (type) { |
53 | case Type::ServerConnectionTCP: |
54 | return; |
55 | case Type::ClientTCP: |
56 | m_socket->SignalConnect.connect(this, &LibWebRTCSocketClient::signalConnect); |
57 | m_socket->SignalAddressReady.connect(this, &LibWebRTCSocketClient::signalAddressReady); |
58 | return; |
59 | case Type::ServerTCP: |
60 | m_socket->SignalConnect.connect(this, &LibWebRTCSocketClient::signalConnect); |
61 | m_socket->SignalNewConnection.connect(this, &LibWebRTCSocketClient::signalNewConnection); |
62 | signalAddressReady(); |
63 | return; |
64 | case Type::UDP: |
65 | m_socket->SignalConnect.connect(this, &LibWebRTCSocketClient::signalConnect); |
66 | signalAddressReady(); |
67 | return; |
68 | } |
69 | } |
70 | |
71 | void LibWebRTCSocketClient::sendTo(const WebCore::SharedBuffer& buffer, const rtc::SocketAddress& socketAddress, const rtc::PacketOptions& options) |
72 | { |
73 | m_socket->SendTo(reinterpret_cast<const uint8_t*>(buffer.data()), buffer.size(), socketAddress, options); |
74 | } |
75 | |
76 | void LibWebRTCSocketClient::close() |
77 | { |
78 | ASSERT(m_socket); |
79 | m_socket->Close(); |
80 | m_rtcProvider.takeSocket(m_identifier); |
81 | } |
82 | |
83 | void LibWebRTCSocketClient::setOption(int option, int value) |
84 | { |
85 | ASSERT(m_socket); |
86 | m_socket->SetOption(static_cast<rtc::Socket::Option>(option), value); |
87 | } |
88 | |
89 | void LibWebRTCSocketClient::signalReadPacket(rtc::AsyncPacketSocket* socket, const char* value, size_t length, const rtc::SocketAddress& address, const rtc::PacketTime& packetTime) |
90 | { |
91 | ASSERT_UNUSED(socket, m_socket.get() == socket); |
92 | auto buffer = WebCore::SharedBuffer::create(value, length); |
93 | m_rtcProvider.sendFromMainThread([identifier = m_identifier, buffer = WTFMove(buffer), address = RTCNetwork::isolatedCopy(address), packetTime](IPC::Connection& connection) { |
94 | IPC::DataReference data(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size()); |
95 | connection.send(Messages::WebRTCSocket::SignalReadPacket(data, RTCNetwork::IPAddress(address.ipaddr()), address.port(), packetTime), identifier); |
96 | }); |
97 | } |
98 | |
99 | void LibWebRTCSocketClient::signalSentPacket(rtc::AsyncPacketSocket* socket, const rtc::SentPacket& sentPacket) |
100 | { |
101 | ASSERT_UNUSED(socket, m_socket.get() == socket); |
102 | m_rtcProvider.sendFromMainThread([identifier = m_identifier, sentPacket](IPC::Connection& connection) { |
103 | connection.send(Messages::WebRTCSocket::SignalSentPacket(sentPacket.packet_id, sentPacket.send_time_ms), identifier); |
104 | }); |
105 | } |
106 | |
107 | void LibWebRTCSocketClient::signalNewConnection(rtc::AsyncPacketSocket* socket, rtc::AsyncPacketSocket* newSocket) |
108 | { |
109 | ASSERT_UNUSED(socket, m_socket.get() == socket); |
110 | m_rtcProvider.newConnection(*this, std::unique_ptr<rtc::AsyncPacketSocket>(newSocket)); |
111 | } |
112 | |
113 | void LibWebRTCSocketClient::signalAddressReady(rtc::AsyncPacketSocket* socket, const rtc::SocketAddress& address) |
114 | { |
115 | ASSERT_UNUSED(socket, m_socket.get() == socket); |
116 | m_rtcProvider.sendFromMainThread([identifier = m_identifier, address = RTCNetwork::isolatedCopy(address)](IPC::Connection& connection) { |
117 | connection.send(Messages::WebRTCSocket::SignalAddressReady(RTCNetwork::SocketAddress(address)), identifier); |
118 | }); |
119 | } |
120 | |
121 | void LibWebRTCSocketClient::signalAddressReady() |
122 | { |
123 | signalAddressReady(m_socket.get(), m_socket->GetLocalAddress()); |
124 | } |
125 | |
126 | void LibWebRTCSocketClient::signalConnect(rtc::AsyncPacketSocket* socket) |
127 | { |
128 | ASSERT_UNUSED(socket, m_socket.get() == socket); |
129 | m_rtcProvider.sendFromMainThread([identifier = m_identifier](IPC::Connection& connection) { |
130 | connection.send(Messages::WebRTCSocket::SignalConnect(), identifier); |
131 | }); |
132 | } |
133 | |
134 | void LibWebRTCSocketClient::signalClose(rtc::AsyncPacketSocket* socket, int error) |
135 | { |
136 | ASSERT_UNUSED(socket, m_socket.get() == socket); |
137 | m_rtcProvider.sendFromMainThread([identifier = m_identifier, error](IPC::Connection& connection) { |
138 | connection.send(Messages::WebRTCSocket::SignalClose(error), identifier); |
139 | }); |
140 | // We want to remove 'this' from the socket map now but we will destroy it asynchronously |
141 | // so that the socket parameter of signalClose remains alive as the caller of signalClose may actually being using it afterwards. |
142 | m_rtcProvider.callOnRTCNetworkThread([socket = m_rtcProvider.takeSocket(m_identifier)] { }); |
143 | } |
144 | |
145 | } // namespace WebKit |
146 | |
147 | #endif // USE(LIBWEBRTC) |
148 | |