OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ | 5 #ifndef NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ |
6 #define NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ | 6 #define NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 static CryptoHandshakeMessage* ParseMessage(base::StringPiece in); | 49 static CryptoHandshakeMessage* ParseMessage(base::StringPiece in); |
50 | 50 |
51 // Set callbacks to be called from the framer. A visitor must be set, or | 51 // Set callbacks to be called from the framer. A visitor must be set, or |
52 // else the framer will crash. It is acceptable for the visitor to do | 52 // else the framer will crash. It is acceptable for the visitor to do |
53 // nothing. If this is called multiple times, only the last visitor | 53 // nothing. If this is called multiple times, only the last visitor |
54 // will be used. |visitor| will be owned by the framer. | 54 // will be used. |visitor| will be owned by the framer. |
55 void set_visitor(CryptoFramerVisitorInterface* visitor) { | 55 void set_visitor(CryptoFramerVisitorInterface* visitor) { |
56 visitor_ = visitor; | 56 visitor_ = visitor; |
57 } | 57 } |
58 | 58 |
59 QuicErrorCode error() const { | 59 QuicErrorCode error() const { return error_; } |
60 return error_; | |
61 } | |
62 | 60 |
63 // Processes input data, which must be delivered in order. Returns | 61 // Processes input data, which must be delivered in order. Returns |
64 // false if there was an error, and true otherwise. | 62 // false if there was an error, and true otherwise. |
65 bool ProcessInput(base::StringPiece input); | 63 bool ProcessInput(base::StringPiece input); |
66 | 64 |
67 // Returns the number of bytes of buffered input data remaining to be | 65 // Returns the number of bytes of buffered input data remaining to be |
68 // parsed. | 66 // parsed. |
69 size_t InputBytesRemaining() const { | 67 size_t InputBytesRemaining() const { return buffer_.length(); } |
70 return buffer_.length(); | |
71 } | |
72 | 68 |
73 // Returns a new QuicData owned by the caller that contains a serialized | 69 // Returns a new QuicData owned by the caller that contains a serialized |
74 // |message|, or NULL if there was an error. | 70 // |message|, or NULL if there was an error. |
75 static QuicData* ConstructHandshakeMessage( | 71 static QuicData* ConstructHandshakeMessage( |
76 const CryptoHandshakeMessage& message); | 72 const CryptoHandshakeMessage& message); |
77 | 73 |
78 private: | 74 private: |
79 // Clears per-message state. Does not clear the visitor. | 75 // Clears per-message state. Does not clear the visitor. |
80 void Clear(); | 76 void Clear(); |
81 | 77 |
82 void set_error(QuicErrorCode error) { | 78 void set_error(QuicErrorCode error) { error_ = error; } |
83 error_ = error; | |
84 } | |
85 | 79 |
86 // Represents the current state of the parsing state machine. | 80 // Represents the current state of the parsing state machine. |
87 enum CryptoFramerState { | 81 enum CryptoFramerState { |
88 STATE_READING_TAG, | 82 STATE_READING_TAG, |
89 STATE_READING_NUM_ENTRIES, | 83 STATE_READING_NUM_ENTRIES, |
90 STATE_READING_TAGS_AND_LENGTHS, | 84 STATE_READING_TAGS_AND_LENGTHS, |
91 STATE_READING_VALUES | 85 STATE_READING_VALUES |
92 }; | 86 }; |
93 | 87 |
94 // Visitor to invoke when messages are parsed. | 88 // Visitor to invoke when messages are parsed. |
95 CryptoFramerVisitorInterface* visitor_; | 89 CryptoFramerVisitorInterface* visitor_; |
96 // Last error. | 90 // Last error. |
97 QuicErrorCode error_; | 91 QuicErrorCode error_; |
98 // Remaining unparsed data. | 92 // Remaining unparsed data. |
99 std::string buffer_; | 93 std::string buffer_; |
100 // Current state of the parsing. | 94 // Current state of the parsing. |
101 CryptoFramerState state_; | 95 CryptoFramerState state_; |
102 // The message currently being parsed. | 96 // The message currently being parsed. |
103 CryptoHandshakeMessage message_; | 97 CryptoHandshakeMessage message_; |
104 // Number of entires in the message currently being parsed. | 98 // Number of entires in the message currently being parsed. |
105 uint16 num_entries_; | 99 uint16 num_entries_; |
106 // tags_and_lengths_ contains the tags that are currently being parsed and | 100 // tags_and_lengths_ contains the tags that are currently being parsed and |
107 // their lengths. | 101 // their lengths. |
108 std::vector<std::pair<CryptoTag, size_t> > tags_and_lengths_; | 102 std::vector<std::pair<QuicTag, size_t> > tags_and_lengths_; |
109 // Cumulative length of all values in the message currently being parsed. | 103 // Cumulative length of all values in the message currently being parsed. |
110 size_t values_len_; | 104 size_t values_len_; |
111 }; | 105 }; |
112 | 106 |
113 } // namespace net | 107 } // namespace net |
114 | 108 |
115 #endif // NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ | 109 #endif // NET_QUIC_CRYPTO_CRYPTO_FRAMER_H_ |
OLD | NEW |