OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2012 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_MEDIA_SCTP_SCTPTRANSPORT_H_ | |
12 #define WEBRTC_MEDIA_SCTP_SCTPTRANSPORT_H_ | |
13 | |
14 #include <errno.h> | |
15 | |
16 #include <memory> // for unique_ptr. | |
17 #include <set> | |
18 #include <string> | |
19 #include <vector> | |
20 | |
21 #include "webrtc/base/asyncinvoker.h" | |
22 #include "webrtc/base/constructormagic.h" | |
23 #include "webrtc/base/copyonwritebuffer.h" | |
24 #include "webrtc/base/sigslot.h" | |
25 #include "webrtc/base/thread.h" | |
26 // For SendDataParams/ReceiveDataParams. | |
27 // TODO(deadbeef): Use something else for SCTP. It's confusing that we use an | |
28 // SSRC field for SID. | |
29 #include "webrtc/media/base/mediachannel.h" | |
pthatcher1
2016/12/23 01:39:31
Just go ahead and rename it to .sid and make the
Taylor Brandstetter
2016/12/23 06:29:05
I changed it to a union type. I'll leave moving th
| |
30 #include "webrtc/media/sctp/sctptransportinternal.h" | |
31 #include "webrtc/p2p/base/transportchannel.h" | |
32 | |
33 // Defined by "usrsctplib/usrsctp.h" | |
34 struct sockaddr_conn; | |
35 struct sctp_assoc_change; | |
36 struct sctp_stream_reset_event; | |
37 // Defined by <sys/socket.h> | |
38 struct socket; | |
39 namespace cricket { | |
40 | |
41 // Holds data to be passed on to a channel. | |
42 struct SctpInboundPacket; | |
43 | |
44 // From channel calls, data flows like this: | |
45 // [network thread (although it can in princple be another thread)] | |
46 // 1. SctpTransport::SendData(data) | |
47 // 2. usrsctp_sendv(data) | |
48 // [network thread returns; sctp thread then calls the following] | |
49 // 3. OnSctpOutboundPacket(wrapped_data) | |
50 // [sctp thread returns having async invoked on the network thread] | |
51 // 4. SctpTransport::OnPacketFromSctpToNetwork(wrapped_data) | |
52 // 5. TransportChannel::SendPacket(wrapped_data) | |
53 // 6. ... across network ... a packet is sent back ... | |
54 // 7. SctpTransport::OnPacketReceived(wrapped_data) | |
55 // 8. usrsctp_conninput(wrapped_data) | |
56 // [network thread returns; sctp thread then calls the following] | |
57 // 9. OnSctpInboundData(data) | |
58 // [sctp thread returns having async invoked on the network thread] | |
59 // 10. SctpTransport::OnInboundPacketFromSctpToChannel(inboundpacket) | |
60 // 11. SctpTransport::OnDataFromSctpToChannel(data) | |
61 // 12. SctpTransport::SignalDataReceived(data) | |
62 // [from the same thread, methods registered/connected to | |
63 // SctpTransport are called with the recieved data] | |
64 class SctpTransport : public SctpTransportInternal, | |
65 public sigslot::has_slots<> { | |
66 public: | |
67 // |network_thread| is where packets will be processed and callbacks from | |
68 // this transport will be posted, and is the only thread on which public | |
69 // methods can be called. | |
70 // |channel| is required (must not be null). | |
71 SctpTransport(rtc::Thread* network_thread, | |
72 cricket::TransportChannel* channel); | |
73 ~SctpTransport() override; | |
74 | |
75 // SctpTransportInternal overrides (see sctptransportinternal.h for comments). | |
76 void SetTransportChannel(cricket::TransportChannel* channel) override; | |
77 bool Start(int local_port, int remote_port) override; | |
78 bool OpenStream(int sid) override; | |
79 bool ResetStream(int sid) override; | |
80 bool SendData(const SendDataParams& params, | |
81 const rtc::CopyOnWriteBuffer& payload, | |
82 SendDataResult* result = nullptr) override; | |
83 bool ReadyToSendData() override; | |
84 void set_debug_name_for_testing(const char* debug_name) override { | |
85 debug_name_ = debug_name; | |
86 } | |
87 | |
88 // Exposed to allow Post call from c-callbacks. | |
89 // TODO(deadbeef): Remove this or at least make it return a const pointer. | |
90 rtc::Thread* network_thread() const { return network_thread_; } | |
91 | |
92 private: | |
93 void ConnectTransportChannelSignals(); | |
94 void DisconnectTransportChannelSignals(); | |
95 | |
96 // Creates the socket and connects. | |
97 bool Connect(); | |
98 | |
99 // Returns false when opening the socket failed. | |
100 bool OpenSctpSocket(); | |
101 // Helpet method to set socket options. | |
102 bool ConfigureSctpSocket(); | |
103 // Sets |sock_ |to nullptr. | |
104 void CloseSctpSocket(); | |
105 | |
106 // Sends a SCTP_RESET_STREAM for all streams in closing_ssids_. | |
107 bool SendQueuedStreamResets(); | |
108 | |
109 // Sets the "ready to send" flag and fires signal if needed. | |
110 void SetReadyToSendData(); | |
111 | |
112 // Callbacks from DTLS channel. | |
113 void OnWritableState(rtc::PacketTransportInterface* transport); | |
114 virtual void OnPacketRead(rtc::PacketTransportInterface* transport, | |
115 const char* data, | |
116 size_t len, | |
117 const rtc::PacketTime& packet_time, | |
118 int flags); | |
119 | |
120 // Methods related to usrsctp callbacks. | |
121 void OnSendThresholdCallback(); | |
122 sockaddr_conn GetSctpSockAddr(int port); | |
123 | |
124 // Called using |invoker_| to send packet on the network. | |
125 void OnPacketFromSctpToNetwork(const rtc::CopyOnWriteBuffer& buffer); | |
126 // Called using |invoker_| to decide what to do with the packet. | |
127 // The |flags| parameter is used by SCTP to distinguish notification packets | |
128 // from other types of packets. | |
129 void OnInboundPacketFromSctpToChannel(const rtc::CopyOnWriteBuffer& buffer, | |
130 ReceiveDataParams params, | |
131 int flags); | |
132 void OnDataFromSctpToChannel(const ReceiveDataParams& params, | |
133 const rtc::CopyOnWriteBuffer& buffer); | |
134 void OnNotificationFromSctp(const rtc::CopyOnWriteBuffer& buffer); | |
135 void OnNotificationAssocChange(const sctp_assoc_change& change); | |
136 | |
137 void OnStreamResetEvent(const struct sctp_stream_reset_event* evt); | |
138 | |
139 // Responsible for marshalling incoming data to the channels listeners, and | |
140 // outgoing data to the network interface. | |
141 rtc::Thread* network_thread_; | |
142 // Helps pass inbound/outbound packets asynchronously to the network thread. | |
143 rtc::AsyncInvoker invoker_; | |
144 // Underlying DTLS channel. | |
145 TransportChannel* transport_channel_; | |
146 bool was_ever_writable_ = false; | |
147 int local_port_ = kSctpDefaultPort; | |
148 int remote_port_ = kSctpDefaultPort; | |
149 struct socket* sock_ = nullptr; // The socket created by usrsctp_socket(...). | |
150 | |
151 // Has Start been called? Don't create SCTP socket until it has. | |
152 bool started_ = false; | |
153 // Are we ready to queue data (SCTP socket created, and not blocked due to | |
154 // congestion control)? Different than |transport_channel_|'s "ready to | |
155 // send". | |
156 bool ready_to_send_data_ = false; | |
157 | |
158 typedef std::set<uint32_t> StreamSet; | |
159 // When a data channel opens a stream, it goes into open_streams_. When we | |
160 // want to close it, the stream's ID goes into queued_reset_streams_. When | |
161 // we actually transmit a RE-CONFIG chunk with that stream ID, the ID goes | |
162 // into sent_reset_streams_. When we get a response RE-CONFIG chunk back | |
163 // acknowledging the reset, we remove the stream ID from | |
164 // sent_reset_streams_. We use sent_reset_streams_ to differentiate | |
165 // between acknowledgment RE-CONFIG and peer-initiated RE-CONFIGs. | |
166 StreamSet open_streams_; | |
167 StreamSet queued_reset_streams_; | |
168 StreamSet sent_reset_streams_; | |
169 | |
170 // A static human-readable name for debugging messages. | |
171 const char* debug_name_ = "SctpTransport"; | |
172 // Hides usrsctp interactions from this header file. | |
173 class UsrSctpWrapper; | |
174 | |
175 RTC_DISALLOW_COPY_AND_ASSIGN(SctpTransport); | |
176 }; | |
177 | |
178 class SctpTransportFactory : public SctpTransportInternalFactory { | |
179 public: | |
180 explicit SctpTransportFactory(rtc::Thread* network_thread) | |
181 : network_thread_(network_thread) {} | |
182 | |
183 std::unique_ptr<SctpTransportInternal> CreateSctpTransport( | |
184 TransportChannel* channel) override { | |
185 return std::unique_ptr<SctpTransportInternal>( | |
186 new SctpTransport(network_thread_, channel)); | |
187 } | |
188 | |
189 private: | |
190 rtc::Thread* network_thread_; | |
191 }; | |
192 | |
193 } // namespace cricket | |
194 | |
195 #endif // WEBRTC_MEDIA_SCTP_SCTPTRANSPORT_H_ | |
OLD | NEW |