1 /*
2  *  Copyright 2017 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 P2P_BASE_PACKET_TRANSPORT_INTERNAL_H_
12 #define P2P_BASE_PACKET_TRANSPORT_INTERNAL_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "p2p/base/port.h"
19 #include "rtc_base/async_packet_socket.h"
20 #include "rtc_base/network_route.h"
21 #include "rtc_base/socket.h"
22 #include "rtc_base/system/rtc_export.h"
23 #include "rtc_base/third_party/sigslot/sigslot.h"
24 
25 namespace rtc {
26 struct PacketOptions;
27 struct SentPacket;
28 
29 class RTC_EXPORT PacketTransportInternal : public sigslot::has_slots<> {
30  public:
31   virtual const std::string& transport_name() const = 0;
32 
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 
57   // TODO(pthatcher): Once Chrome's MockPacketTransportInterface implements
58   // this, remove the default implementation.
59   virtual bool GetOption(rtc::Socket::Option opt, int* value);
60 
61   // Returns the most recent error that occurred on this channel.
62   virtual int GetError() = 0;
63 
64   // Returns the current network route with transport overhead.
65   // TODO(zhihuang): Make it pure virtual once the Chrome/remoting is updated.
66   virtual absl::optional<NetworkRoute> network_route() const;
67 
68   // 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,
85                    // TODO(bugs.webrtc.org/9584): Change to passing the int64_t
86                    // timestamp by value.
87                    const int64_t&,
88                    int>
89       SignalReadPacket;
90 
91   // Signalled each time a packet is sent on this channel.
92   sigslot::signal2<PacketTransportInternal*, const rtc::SentPacket&>
93       SignalSentPacket;
94 
95   // Signalled when the current network route has changed.
96   sigslot::signal1<absl::optional<rtc::NetworkRoute>> SignalNetworkRouteChanged;
97 
98   // Signalled when the transport is closed.
99   sigslot::signal1<PacketTransportInternal*> SignalClosed;
100 
101  protected:
102   PacketTransportInternal();
103   ~PacketTransportInternal() override;
104 };
105 
106 }  // namespace rtc
107 
108 #endif  // P2P_BASE_PACKET_TRANSPORT_INTERNAL_H_
109