rch | 7b8e0afb1 | 2017-05-03 01:07:22 | [diff] [blame] | 1 | // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_QUIC_QUARTC_QUARTC_SESSION_INTERFACE_H_ |
| 6 | #define NET_QUIC_QUARTC_QUARTC_SESSION_INTERFACE_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
skvlad | 6bb90e79 | 2016-11-02 20:08:37 | [diff] [blame] | 10 | #include <string> |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 11 | |
Yixin Wang | 1f24e405 | 2017-11-21 17:48:10 | [diff] [blame] | 12 | #include "net/quic/core/quic_bandwidth.h" |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 13 | #include "net/quic/core/quic_error_codes.h" |
Yixin Wang | 1f24e405 | 2017-11-21 17:48:10 | [diff] [blame] | 14 | #include "net/quic/core/quic_time.h" |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 15 | #include "net/quic/core/quic_types.h" |
jri | 3ad08a48 | 2017-06-03 02:24:54 | [diff] [blame] | 16 | #include "net/quic/platform/api/quic_export.h" |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 17 | #include "net/quic/quartc/quartc_stream_interface.h" |
| 18 | |
| 19 | namespace net { |
| 20 | |
Fan Yang | 2a1699a | 2017-07-25 16:44:55 | [diff] [blame] | 21 | // Structure holding stats exported by a QuartcSession. |
| 22 | struct QUIC_EXPORT_PRIVATE QuartcSessionStats { |
| 23 | // Bandwidth estimate in bits per second. |
Yixin Wang | 1f24e405 | 2017-11-21 17:48:10 | [diff] [blame] | 24 | QuicBandwidth bandwidth_estimate = QuicBandwidth::Zero(); |
| 25 | |
| 26 | // Smoothed round-trip time. |
| 27 | QuicTime::Delta smoothed_rtt = QuicTime::Delta::Zero(); |
Fan Yang | 2a1699a | 2017-07-25 16:44:55 | [diff] [blame] | 28 | }; |
| 29 | |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 30 | // Given a PacketTransport, provides a way to send and receive separate streams |
| 31 | // of reliable, in-order, encrypted data. For example, this can build on top of |
| 32 | // a WebRTC IceTransport for sending and receiving data over QUIC. |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 33 | class QUIC_EXPORT_PRIVATE QuartcSessionInterface { |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 34 | public: |
| 35 | virtual ~QuartcSessionInterface() {} |
| 36 | |
| 37 | virtual void StartCryptoHandshake() = 0; |
| 38 | |
| 39 | // Only needed when using SRTP with QuicTransport |
| 40 | // Key Exporter interface from RFC 5705 |
| 41 | // Arguments are: |
| 42 | // label -- the exporter label. |
| 43 | // part of the RFC defining each exporter usage (IN) |
| 44 | // context/context_len -- a context to bind to for this connection; |
| 45 | // optional, can be NULL, 0 (IN) |
| 46 | // use_context -- whether to use the context value |
| 47 | // (needed to distinguish no context from |
| 48 | // zero-length ones). |
| 49 | // result -- where to put the computed value |
| 50 | // result_len -- the length of the computed value |
| 51 | virtual bool ExportKeyingMaterial(const std::string& label, |
| 52 | const uint8_t* context, |
| 53 | size_t context_len, |
| 54 | bool used_context, |
| 55 | uint8_t* result, |
| 56 | size_t result_len) = 0; |
| 57 | |
Victor Vasiliev | 1229670 | 2017-10-17 10:32:47 | [diff] [blame] | 58 | // Closes the connection with the given human-readable error details. |
| 59 | // The connection closes with the QUIC_CONNECTION_CANCELLED error code to |
| 60 | // indicate the application closed it. |
| 61 | // |
| 62 | // Informs the peer that the connection has been closed. This prevents the |
| 63 | // peer from waiting until the connection times out. |
| 64 | // |
| 65 | // Cleans up the underlying QuicConnection's state. Closing the connection |
| 66 | // makes it safe to delete the QuartcSession. |
| 67 | virtual void CloseConnection(const std::string& error_details) = 0; |
| 68 | |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 69 | // For forward-compatibility. More parameters could be added through the |
| 70 | // struct without changing the API. |
| 71 | struct OutgoingStreamParameters {}; |
| 72 | |
| 73 | virtual QuartcStreamInterface* CreateOutgoingStream( |
| 74 | const OutgoingStreamParameters& params) = 0; |
| 75 | |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 76 | // If the given stream is still open, sends a reset frame to cancel it. |
| 77 | // Note: This method cancels a stream by QuicStreamId rather than by pointer |
| 78 | // (or by a method on QuartcStreamInterface) because QuartcSession (and not |
| 79 | // the caller) owns the streams. Streams may finish and be deleted before the |
| 80 | // caller tries to cancel them, rendering the caller's pointers invalid. |
| 81 | virtual void CancelStream(QuicStreamId stream_id) = 0; |
| 82 | |
Michael Warres | 74ee3ce | 2017-10-09 15:26:37 | [diff] [blame] | 83 | // This method verifies if a stream is still open and stream pointer can be |
| 84 | // used. When true is returned, the interface pointer is good for making a |
| 85 | // call immediately on the same thread, but may be rendered invalid by ANY |
| 86 | // other QUIC activity. |
| 87 | virtual bool IsOpenStream(QuicStreamId stream_id) = 0; |
| 88 | |
Fan Yang | 2a1699a | 2017-07-25 16:44:55 | [diff] [blame] | 89 | // Gets stats associated with this Quartc session. |
| 90 | virtual QuartcSessionStats GetStats() = 0; |
| 91 | |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 92 | // Send and receive packets, like a virtual UDP socket. For example, this |
| 93 | // could be implemented by WebRTC's IceTransport. |
| 94 | class PacketTransport { |
| 95 | public: |
| 96 | virtual ~PacketTransport() {} |
| 97 | |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 98 | // Called by the QuartcPacketWriter when writing packets to the network. |
| 99 | // Return the number of written bytes. Return 0 if the write is blocked. |
| 100 | virtual int Write(const char* buffer, size_t buf_len) = 0; |
| 101 | }; |
| 102 | |
| 103 | // Called when CanWrite() changes from false to true. |
| 104 | virtual void OnTransportCanWrite() = 0; |
| 105 | |
| 106 | // Called when a packet has been received and should be handled by the |
| 107 | // QuicConnection. |
| 108 | virtual bool OnTransportReceived(const char* data, size_t data_len) = 0; |
| 109 | |
| 110 | // Callbacks called by the QuartcSession to notify the user of the |
| 111 | // QuartcSession of certain events. |
| 112 | class Delegate { |
| 113 | public: |
| 114 | virtual ~Delegate() {} |
| 115 | |
| 116 | // Called when the crypto handshake is complete. |
| 117 | virtual void OnCryptoHandshakeComplete() = 0; |
| 118 | |
| 119 | // Called when a new stream is received from the remote endpoint. |
| 120 | virtual void OnIncomingStream(QuartcStreamInterface* stream) = 0; |
| 121 | |
| 122 | // Called when the connection is closed. This means all of the streams will |
| 123 | // be closed and no new streams can be created. |
| 124 | // TODO(zhihuang): Create mapping from integer error code to WebRTC error |
| 125 | // code. |
| 126 | virtual void OnConnectionClosed(int error_code, bool from_remote) = 0; |
| 127 | |
| 128 | // TODO(zhihuang): Add proof verification. |
| 129 | }; |
| 130 | |
| 131 | // The |delegate| is not owned by QuartcSession. |
| 132 | virtual void SetDelegate(Delegate* delegate) = 0; |
| 133 | }; |
| 134 | |
| 135 | } // namespace net |
| 136 | |
| 137 | #endif // NET_QUIC_QUARTC_QUARTC_SESSION_INTERFACE_H_ |