OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ | 11 #ifndef WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ |
12 #define WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ | 12 #define WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 #include <string> | 15 #include <string> |
16 | 16 |
| 17 #include "webrtc/api/udptransportinterface.h" |
| 18 #include "webrtc/base/asyncpacketsocket.h" // For PacketOptions. |
17 #include "webrtc/base/optional.h" | 19 #include "webrtc/base/optional.h" |
18 #include "webrtc/base/thread_checker.h" | 20 #include "webrtc/base/thread_checker.h" |
19 #include "webrtc/p2p/base/packettransportinterface.h" | 21 #include "webrtc/p2p/base/packettransportinterface.h" |
20 | 22 |
21 namespace rtc { | 23 namespace rtc { |
22 class AsyncPacketSocket; | 24 class AsyncPacketSocket; |
23 class PhysicalSocketServer; | 25 struct PacketTime; |
| 26 struct SentPacket; |
24 class SocketAddress; | 27 class SocketAddress; |
25 class SocketServer; | |
26 class Thread; | |
27 } | 28 } |
28 | 29 |
29 namespace cricket { | 30 namespace cricket { |
30 | 31 |
31 class UdpTransportChannel : public rtc::PacketTransportInterface { | 32 // Implementation of UdpTransportInterface. |
| 33 // Used by OrtcFactory. |
| 34 class UdpTransport : public webrtc::UdpTransportInterface, |
| 35 public rtc::PacketTransportInterface { |
32 public: | 36 public: |
33 enum class State { INIT, CONNECTING, CONNECTED, FAILED }; | 37 // |transport_name| is only used for identification/logging. |
34 explicit UdpTransportChannel(const std::string& transport_name); | 38 // |socket| must be non-null. |
35 UdpTransportChannel(const std::string& transport_name, rtc::SocketServer* ss); | 39 UdpTransport(const std::string& transport_name, |
36 ~UdpTransportChannel(); | 40 std::unique_ptr<rtc::AsyncPacketSocket> socket); |
| 41 ~UdpTransport(); |
37 | 42 |
| 43 // Overrides of UdpTransportInterface, used by the API consumer. |
| 44 rtc::SocketAddress GetLocalAddress() const override; |
| 45 bool SetRemoteAddress(const rtc::SocketAddress& addr) override; |
| 46 rtc::SocketAddress GetRemoteAddress() const override; |
| 47 |
| 48 // Overrides of PacketTransportInterface, used by webrtc internally. |
38 const std::string debug_name() const override { return transport_name_; } | 49 const std::string debug_name() const override { return transport_name_; } |
39 | 50 |
40 bool receiving() const override { | 51 bool receiving() const override { |
41 // TODO(johan): Implement method and signal. | 52 // TODO(johan): Implement method and signal. |
42 return true; | 53 return true; |
43 } | 54 } |
44 | 55 |
45 bool writable() const override; | 56 bool writable() const override; |
46 | 57 |
47 int SendPacket(const char* data, | 58 int SendPacket(const char* data, |
48 size_t len, | 59 size_t len, |
49 const rtc::PacketOptions& options, | 60 const rtc::PacketOptions& options, |
50 int flags) override; | 61 int flags) override; |
51 | 62 |
52 int SetOption(rtc::Socket::Option opt, int value) override { return 0; } | 63 int SetOption(rtc::Socket::Option opt, int value) override { return 0; } |
53 | 64 |
54 int GetError() override { return send_error_; } | 65 int GetError() override { return send_error_; } |
55 | 66 |
56 State state() const { | |
57 RTC_DCHECK_RUN_ON(&network_thread_checker_); | |
58 return state_; | |
59 } | |
60 | |
61 // Start() makes UdpTransportChannel transition from state INIT to CONNECTING. | |
62 // It creates the local UDP socket and binds it to a port. | |
63 // Consider checking state() after calling Start(). | |
64 void Start(); | |
65 | |
66 void SetRemoteParameters(const rtc::SocketAddress& remote); | |
67 | |
68 // Returned optional does not have a value if in the INIT or FAILED state. | |
69 // Consider checking state() before calling local_parameters(). | |
70 rtc::Optional<rtc::SocketAddress> local_parameters() const { | |
71 return local_parameters_; | |
72 } | |
73 | |
74 private: | 67 private: |
75 void OnSocketReadPacket(rtc::AsyncPacketSocket* socket, | 68 void OnSocketReadPacket(rtc::AsyncPacketSocket* socket, |
76 const char* data, | 69 const char* data, |
77 size_t len, | 70 size_t len, |
78 const rtc::SocketAddress& remote_addr, | 71 const rtc::SocketAddress& remote_addr, |
79 const rtc::PacketTime& packet_time); | 72 const rtc::PacketTime& packet_time); |
80 void OnSocketSentPacket(rtc::AsyncPacketSocket* socket, | 73 void OnSocketSentPacket(rtc::AsyncPacketSocket* socket, |
81 const rtc::SentPacket& packet); | 74 const rtc::SentPacket& packet); |
82 void SetState(State state); // Set State and Signal. | |
83 bool IsLocalConsistent(); | 75 bool IsLocalConsistent(); |
84 void UpdateState(); | |
85 std::string transport_name_; | 76 std::string transport_name_; |
86 rtc::SocketServer* socket_server_; | |
87 State state_ = State::INIT; | |
88 int send_error_ = 0; | 77 int send_error_ = 0; |
89 std::unique_ptr<rtc::AsyncPacketSocket> socket_; | 78 std::unique_ptr<rtc::AsyncPacketSocket> socket_; |
90 rtc::Optional<rtc::SocketAddress> local_parameters_; | 79 // If not set, will be an "nil" address ("IsNil" returns true). |
91 rtc::Optional<rtc::SocketAddress> remote_parameters_; | 80 rtc::SocketAddress remote_address_; |
92 rtc::ThreadChecker network_thread_checker_; | 81 rtc::ThreadChecker network_thread_checker_; |
93 }; | 82 }; |
94 } // namespace cricket | 83 } // namespace cricket |
95 | 84 |
96 #endif // WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ | 85 #endif // WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ |
OLD | NEW |