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 | |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 12 | #include "net/quic/core/quic_error_codes.h" |
| 13 | #include "net/quic/core/quic_types.h" |
jri | 3ad08a48 | 2017-06-03 02:24:54 | [diff] [blame] | 14 | #include "net/quic/platform/api/quic_export.h" |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 15 | #include "net/quic/quartc/quartc_stream_interface.h" |
| 16 | |
| 17 | namespace net { |
| 18 | |
Fan Yang | 2a1699a | 2017-07-25 16:44:55 | [diff] [blame^] | 19 | // Structure holding stats exported by a QuartcSession. |
| 20 | struct QUIC_EXPORT_PRIVATE QuartcSessionStats { |
| 21 | // Bandwidth estimate in bits per second. |
| 22 | int64_t bandwidth_estimate_bits_per_second; |
| 23 | }; |
| 24 | |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 25 | // Given a PacketTransport, provides a way to send and receive separate streams |
| 26 | // of reliable, in-order, encrypted data. For example, this can build on top of |
| 27 | // a WebRTC IceTransport for sending and receiving data over QUIC. |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 28 | class QUIC_EXPORT_PRIVATE QuartcSessionInterface { |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 29 | public: |
| 30 | virtual ~QuartcSessionInterface() {} |
| 31 | |
| 32 | virtual void StartCryptoHandshake() = 0; |
| 33 | |
| 34 | // Only needed when using SRTP with QuicTransport |
| 35 | // Key Exporter interface from RFC 5705 |
| 36 | // Arguments are: |
| 37 | // label -- the exporter label. |
| 38 | // part of the RFC defining each exporter usage (IN) |
| 39 | // context/context_len -- a context to bind to for this connection; |
| 40 | // optional, can be NULL, 0 (IN) |
| 41 | // use_context -- whether to use the context value |
| 42 | // (needed to distinguish no context from |
| 43 | // zero-length ones). |
| 44 | // result -- where to put the computed value |
| 45 | // result_len -- the length of the computed value |
| 46 | virtual bool ExportKeyingMaterial(const std::string& label, |
| 47 | const uint8_t* context, |
| 48 | size_t context_len, |
| 49 | bool used_context, |
| 50 | uint8_t* result, |
| 51 | size_t result_len) = 0; |
| 52 | |
| 53 | // For forward-compatibility. More parameters could be added through the |
| 54 | // struct without changing the API. |
| 55 | struct OutgoingStreamParameters {}; |
| 56 | |
| 57 | virtual QuartcStreamInterface* CreateOutgoingStream( |
| 58 | const OutgoingStreamParameters& params) = 0; |
| 59 | |
jri | 3c1d5ca | 2017-06-02 04:52:23 | [diff] [blame] | 60 | // If the given stream is still open, sends a reset frame to cancel it. |
| 61 | // Note: This method cancels a stream by QuicStreamId rather than by pointer |
| 62 | // (or by a method on QuartcStreamInterface) because QuartcSession (and not |
| 63 | // the caller) owns the streams. Streams may finish and be deleted before the |
| 64 | // caller tries to cancel them, rendering the caller's pointers invalid. |
| 65 | virtual void CancelStream(QuicStreamId stream_id) = 0; |
| 66 | |
Fan Yang | 2a1699a | 2017-07-25 16:44:55 | [diff] [blame^] | 67 | // Gets stats associated with this Quartc session. |
| 68 | virtual QuartcSessionStats GetStats() = 0; |
| 69 | |
zhihuang | 33e7a9c | 2016-10-31 20:04:38 | [diff] [blame] | 70 | // Send and receive packets, like a virtual UDP socket. For example, this |
| 71 | // could be implemented by WebRTC's IceTransport. |
| 72 | class PacketTransport { |
| 73 | public: |
| 74 | virtual ~PacketTransport() {} |
| 75 | |
| 76 | // Called by the QuartcPacketWriter to check if the underneath transport is |
| 77 | // writable. True if packets written are expected to be sent. False if |
| 78 | // packets will be dropped. |
| 79 | virtual bool CanWrite() = 0; |
| 80 | |
| 81 | // Called by the QuartcPacketWriter when writing packets to the network. |
| 82 | // Return the number of written bytes. Return 0 if the write is blocked. |
| 83 | virtual int Write(const char* buffer, size_t buf_len) = 0; |
| 84 | }; |
| 85 | |
| 86 | // Called when CanWrite() changes from false to true. |
| 87 | virtual void OnTransportCanWrite() = 0; |
| 88 | |
| 89 | // Called when a packet has been received and should be handled by the |
| 90 | // QuicConnection. |
| 91 | virtual bool OnTransportReceived(const char* data, size_t data_len) = 0; |
| 92 | |
| 93 | // Callbacks called by the QuartcSession to notify the user of the |
| 94 | // QuartcSession of certain events. |
| 95 | class Delegate { |
| 96 | public: |
| 97 | virtual ~Delegate() {} |
| 98 | |
| 99 | // Called when the crypto handshake is complete. |
| 100 | virtual void OnCryptoHandshakeComplete() = 0; |
| 101 | |
| 102 | // Called when a new stream is received from the remote endpoint. |
| 103 | virtual void OnIncomingStream(QuartcStreamInterface* stream) = 0; |
| 104 | |
| 105 | // Called when the connection is closed. This means all of the streams will |
| 106 | // be closed and no new streams can be created. |
| 107 | // TODO(zhihuang): Create mapping from integer error code to WebRTC error |
| 108 | // code. |
| 109 | virtual void OnConnectionClosed(int error_code, bool from_remote) = 0; |
| 110 | |
| 111 | // TODO(zhihuang): Add proof verification. |
| 112 | }; |
| 113 | |
| 114 | // The |delegate| is not owned by QuartcSession. |
| 115 | virtual void SetDelegate(Delegate* delegate) = 0; |
| 116 | }; |
| 117 | |
| 118 | } // namespace net |
| 119 | |
| 120 | #endif // NET_QUIC_QUARTC_QUARTC_SESSION_INTERFACE_H_ |