blob: a5321835a983299b2c9e5a6c6d8317fc7af8266a [file] [log] [blame]
deadbeef5bd5ca32017-02-10 11:31:50 -08001/*
deadbeefe814a0d2017-02-25 18:15:09 -08002 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
deadbeef5bd5ca32017-02-10 11:31:50 -08003 *
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_PACKET_TRANSPORT_INTERNAL_H_
12#define P2P_BASE_PACKET_TRANSPORT_INTERNAL_H_
deadbeef5bd5ca32017-02-10 11:31:50 -080013
14#include <string>
15#include <vector>
16
Danil Chapovalov00c71832018-06-15 15:58:38 +020017#include "absl/types/optional.h"
Zhi Huang942bc2e2017-11-13 13:26:07 -080018#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/async_packet_socket.h"
20#include "rtc_base/network_route.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/socket.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020022#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020023#include "rtc_base/third_party/sigslot/sigslot.h"
deadbeef5bd5ca32017-02-10 11:31:50 -080024
deadbeef5bd5ca32017-02-10 11:31:50 -080025namespace rtc {
26struct PacketOptions;
deadbeef5bd5ca32017-02-10 11:31:50 -080027struct SentPacket;
28
Bjorn A Mellem34cd4852019-05-24 10:13:10 -070029class RTC_EXPORT PacketTransportInternal : public sigslot::has_slots<> {
deadbeef5bd5ca32017-02-10 11:31:50 -080030 public:
Zhi Huang942bc2e2017-11-13 13:26:07 -080031 virtual const std::string& transport_name() const = 0;
deadbeef5bd5ca32017-02-10 11:31:50 -080032
33 // The transport has been established.
34 virtual bool writable() const = 0;
35
36 // The transport has received a packet in the last X milliseconds, here X is
37 // configured by each implementation.
38 virtual bool receiving() const = 0;
39
40 // Attempts to send the given packet.
41 // The return value is < 0 on failure. The return value in failure case is not
42 // descriptive. Depending on failure cause and implementation details
43 // GetError() returns an descriptive errno.h error value.
44 // This mimics posix socket send() or sendto() behavior.
45 // TODO(johan): Reliable, meaningful, consistent error codes for all
46 // implementations would be nice.
47 // TODO(johan): Remove the default argument once channel code is updated.
48 virtual int SendPacket(const char* data,
49 size_t len,
50 const rtc::PacketOptions& options,
51 int flags = 0) = 0;
52
53 // Sets a socket option. Note that not all options are
54 // supported by all transport types.
55 virtual int SetOption(rtc::Socket::Option opt, int value) = 0;
56
deadbeefe814a0d2017-02-25 18:15:09 -080057 // TODO(pthatcher): Once Chrome's MockPacketTransportInterface implements
deadbeef5bd5ca32017-02-10 11:31:50 -080058 // this, remove the default implementation.
Steve Anton33f69db2017-10-30 10:01:15 -070059 virtual bool GetOption(rtc::Socket::Option opt, int* value);
deadbeef5bd5ca32017-02-10 11:31:50 -080060
61 // Returns the most recent error that occurred on this channel.
62 virtual int GetError() = 0;
63
Zhi Huang942bc2e2017-11-13 13:26:07 -080064 // Returns the current network route with transport overhead.
65 // TODO(zhihuang): Make it pure virtual once the Chrome/remoting is updated.
Danil Chapovalov00c71832018-06-15 15:58:38 +020066 virtual absl::optional<NetworkRoute> network_route() const;
Zhi Huang942bc2e2017-11-13 13:26:07 -080067
deadbeef5bd5ca32017-02-10 11:31:50 -080068 // Emitted when the writable state, represented by |writable()|, changes.
69 sigslot::signal1<PacketTransportInternal*> SignalWritableState;
70
71 // Emitted when the PacketTransportInternal is ready to send packets. "Ready
72 // to send" is more sensitive than the writable state; a transport may be
73 // writable, but temporarily not able to send packets. For example, the
74 // underlying transport's socket buffer may be full, as indicated by
75 // SendPacket's return code and/or GetError.
76 sigslot::signal1<PacketTransportInternal*> SignalReadyToSend;
77
78 // Emitted when receiving state changes to true.
79 sigslot::signal1<PacketTransportInternal*> SignalReceivingState;
80
81 // Signalled each time a packet is received on this channel.
82 sigslot::signal5<PacketTransportInternal*,
83 const char*,
84 size_t,
Niels Möllere6933812018-11-05 13:01:41 +010085 // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
86 // timestamp by value.
87 const int64_t&,
deadbeef5bd5ca32017-02-10 11:31:50 -080088 int>
89 SignalReadPacket;
90
91 // Signalled each time a packet is sent on this channel.
92 sigslot::signal2<PacketTransportInternal*, const rtc::SentPacket&>
93 SignalSentPacket;
deadbeef225bfc02017-04-06 21:47:33 -070094
Zhi Huang942bc2e2017-11-13 13:26:07 -080095 // Signalled when the current network route has changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +020096 sigslot::signal1<absl::optional<rtc::NetworkRoute>> SignalNetworkRouteChanged;
Zhi Huang942bc2e2017-11-13 13:26:07 -080097
deadbeef225bfc02017-04-06 21:47:33 -070098 protected:
Steve Anton33f69db2017-10-30 10:01:15 -070099 PacketTransportInternal();
100 ~PacketTransportInternal() override;
deadbeef5bd5ca32017-02-10 11:31:50 -0800101};
102
103} // namespace rtc
104
Steve Anton10542f22019-01-11 09:11:00 -0800105#endif // P2P_BASE_PACKET_TRANSPORT_INTERNAL_H_