OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 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_P2P_BASE_UDPTRANSPORTCHANNEL_H_ | |
12 #define WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ | |
13 | |
14 #include <memory> | |
15 #include <string> | |
16 | |
17 #include "webrtc/base/optional.h" | |
18 #include "webrtc/base/thread_checker.h" | |
19 #include "webrtc/p2p/base/packettransportinterface.h" | |
20 | |
21 namespace rtc { | |
22 class AsyncPacketSocket; | |
23 class PhysicalSocketServer; | |
24 class SocketAddress; | |
25 class SocketServer; | |
26 class Thread; | |
27 } | |
28 | |
29 namespace cricket { | |
30 | |
31 class UdpTransportChannel : public rtc::PacketTransportInterface { | |
32 public: | |
33 enum class State { INIT, CONNECTING, CONNECTED, FAILED }; | |
34 explicit UdpTransportChannel(const std::string& transport_name); | |
35 UdpTransportChannel(const std::string& transport_name, rtc::SocketServer* ss); | |
36 ~UdpTransportChannel(); | |
37 | |
38 const std::string debug_name() const override { return transport_name_; } | |
39 | |
40 bool receiving() const override { | |
41 // TODO(johan): Implement method and signal. | |
42 return true; | |
43 } | |
44 | |
45 bool writable() const override; | |
46 | |
47 int SendPacket(const char* data, | |
48 size_t len, | |
49 const rtc::PacketOptions& options, | |
50 int flags) override; | |
51 | |
52 int SetOption(rtc::Socket::Option opt, int value) override { return 0; } | |
53 | |
54 int GetError() override { return send_error_; } | |
55 | |
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: | |
75 void OnSocketReadPacket(rtc::AsyncPacketSocket* socket, | |
76 const char* data, | |
77 size_t len, | |
78 const rtc::SocketAddress& remote_addr, | |
79 const rtc::PacketTime& packet_time); | |
80 void OnSocketSentPacket(rtc::AsyncPacketSocket* socket, | |
81 const rtc::SentPacket& packet); | |
82 void SetState(State state); // Set State and Signal. | |
83 bool IsLocalConsistent(); | |
84 void UpdateState(); | |
85 std::string transport_name_; | |
86 rtc::SocketServer* socket_server_; | |
87 State state_ = State::INIT; | |
88 int send_error_ = 0; | |
89 std::unique_ptr<rtc::AsyncPacketSocket> socket_; | |
90 rtc::Optional<rtc::SocketAddress> local_parameters_; | |
91 rtc::Optional<rtc::SocketAddress> remote_parameters_; | |
92 rtc::ThreadChecker network_thread_checker_; | |
93 }; | |
94 } // namespace cricket | |
95 | |
96 #endif // WEBRTC_P2P_BASE_UDPTRANSPORTCHANNEL_H_ | |
OLD | NEW |