| 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_QUIC_DATA_WRITER_H_ |    5 #ifndef NET_QUIC_QUIC_DATA_WRITER_H_ | 
|    6 #define NET_QUIC_QUIC_DATA_WRITER_H_ |    6 #define NET_QUIC_QUIC_DATA_WRITER_H_ | 
|    7  |    7  | 
|    8 #include <string> |    8 #include <string> | 
|    9  |    9  | 
|   10 #include "base/basictypes.h" |   10 #include "base/basictypes.h" | 
|   11 #include "base/logging.h" |   11 #include "base/logging.h" | 
|   12 #include "base/port.h" |   12 #include "base/port.h" | 
|   13 #include "base/string_piece.h" |   13 #include "base/string_piece.h" | 
|   14 #include "net/base/net_export.h" |   14 #include "net/base/net_export.h" | 
|   15 #include "net/quic/quic_protocol.h" |   15 #include "net/quic/quic_protocol.h" | 
|   16 #include "net/quic/uint128.h" |   16 #include "net/quic/uint128.h" | 
|   17  |   17  | 
|   18 namespace net { |   18 namespace net { | 
|   19  |   19  | 
|   20 // This class provides facilities for packing QUIC data. |   20 // This class provides facilities for packing QUIC data. | 
|   21 // |   21 // | 
|   22 // The QuicDataWriter supports appending primitive values (int, string, etc) |   22 // The QuicDataWriter supports appending primitive values (int, string, etc) | 
|   23 // to a frame instance.  The QuicDataWriter grows its internal memory buffer |   23 // to a frame instance.  The QuicDataWriter grows its internal memory buffer | 
|   24 // dynamically to hold the sequence of primitive values.   The internal memory |   24 // dynamically to hold the sequence of primitive values.   The internal memory | 
|   25 // buffer is exposed as the "data" of the QuicDataWriter. |   25 // buffer is exposed as the "data" of the QuicDataWriter. | 
|   26 class NET_EXPORT_PRIVATE QuicDataWriter { |   26 class NET_EXPORT_PRIVATE QuicDataWriter { | 
|   27  public: |   27  public: | 
|   28   explicit QuicDataWriter(size_t length); |   28   explicit QuicDataWriter(size_t length); | 
 |   29  | 
|   29   ~QuicDataWriter(); |   30   ~QuicDataWriter(); | 
|   30  |   31  | 
|   31   // Returns the size of the QuicDataWriter's data. |   32   // Returns the size of the QuicDataWriter's data. | 
|   32   size_t length() const { return length_; } |   33   size_t length() const { return length_; } | 
|   33  |   34  | 
|   34   // Takes the buffer from the QuicDataWriter. |   35   // Takes the buffer from the QuicDataWriter. | 
|   35   // TODO(rch): move non-trivial methods into .cc file. |   36   char* take(); | 
|   36   char* take() { |  | 
|   37     char* rv = buffer_; |  | 
|   38     buffer_ = NULL; |  | 
|   39     capacity_ = 0; |  | 
|   40     length_ = 0; |  | 
|   41     return rv; |  | 
|   42   } |  | 
|   43  |   37  | 
|   44   // Methods for adding to the payload.  These values are appended to the end |   38   // Methods for adding to the payload.  These values are appended to the end | 
|   45   // of the QuicDataWriter payload. Note - binary integers are written in |   39   // of the QuicDataWriter payload. Note - binary integers are written in | 
|   46   // host byte order (little endian) not network byte order (big endian). |   40   // host byte order (little endian) not network byte order (big endian). | 
|   47   bool WriteUInt8(uint8 value) { |   41   bool WriteUInt8(uint8 value); | 
|   48     return WriteBytes(&value, sizeof(value)); |   42   bool WriteUInt16(uint16 value); | 
|   49   } |   43   bool WriteUInt32(uint32 value); | 
|   50   bool WriteUInt16(uint16 value) { |   44   bool WriteUInt48(uint64 value); | 
|   51     return WriteBytes(&value, sizeof(value)); |   45   bool WriteUInt64(uint64 value); | 
|   52   } |   46   bool WriteUInt128(uint128 value); | 
|   53   bool WriteUInt32(uint32 value) { |   47   bool WriteStringPiece16(base::StringPiece val); | 
|   54     return WriteBytes(&value, sizeof(value)); |  | 
|   55   } |  | 
|   56   bool WriteUInt48(uint64 value) { |  | 
|   57     uint32 hi = value >> 32; |  | 
|   58     uint32 lo = value & GG_UINT64_C(0x00000000FFFFFFFF); |  | 
|   59     return WriteUInt32(lo) && WriteUInt16(hi); |  | 
|   60   } |  | 
|   61   bool WriteUInt64(uint64 value) { |  | 
|   62     return WriteBytes(&value, sizeof(value)); |  | 
|   63   } |  | 
|   64   bool WriteUInt128(uint128 value) { |  | 
|   65     return WriteUInt64(value.lo) && WriteUInt64(value.hi); |  | 
|   66   } |  | 
|   67   bool WriteStringPiece16(base::StringPiece val) { |  | 
|   68     if (val.length() > std::numeric_limits<uint16>::max()) { |  | 
|   69       return false; |  | 
|   70     } |  | 
|   71     if (!WriteUInt16(val.size())) { |  | 
|   72       return false; |  | 
|   73     } |  | 
|   74     return WriteBytes(val.data(), val.size()); |  | 
|   75   } |  | 
|   76  |  | 
|   77   bool AdvancePointer(uint32 len); |  | 
|   78  |  | 
|   79   bool WriteBytes(const void* data, uint32 data_len); |   48   bool WriteBytes(const void* data, uint32 data_len); | 
|   80  |   49  | 
|   81   static void WriteUint64ToBuffer(uint64 value, char* buffer); |   50   static void WriteUint64ToBuffer(uint64 value, char* buffer); | 
|   82   static void WriteUint128ToBuffer(uint128 value, char* buffer); |   51   static void WriteUint128ToBuffer(uint128 value, char* buffer); | 
|   83  |   52  | 
|   84   size_t capacity() const { |   53   size_t capacity() const { | 
|   85     return capacity_; |   54     return capacity_; | 
|   86   } |   55   } | 
|   87  |   56  | 
|   88  protected: |   57  protected: | 
|   89   const char* end_of_payload() const { return buffer_ + length_; } |   58   const char* end_of_payload() const { return buffer_ + length_; } | 
|   90  |   59  | 
|   91  private: |   60  private: | 
|   92   // Returns the location that the data should be written at, or NULL if there |   61   // Returns the location that the data should be written at, or NULL if there | 
|   93   // is not enough room. Call EndWrite with the returned offset and the given |   62   // is not enough room. Call EndWrite with the returned offset and the given | 
|   94   // length to pad out for the next write. |   63   // length to pad out for the next write. | 
|   95   char* BeginWrite(size_t length); |   64   char* BeginWrite(size_t length); | 
|   96  |   65  | 
|   97   char* buffer_; |   66   char* buffer_; | 
|   98   size_t capacity_;  // Allocation size of payload (or -1 if buffer is const). |   67   size_t capacity_;  // Allocation size of payload (or -1 if buffer is const). | 
|   99   size_t length_;    // Current length of the buffer. |   68   size_t length_;    // Current length of the buffer. | 
|  100 }; |   69 }; | 
|  101  |   70  | 
|  102 }  // namespace net |   71 }  // namespace net | 
|  103  |   72  | 
|  104 #endif  // NET_QUIC_QUIC_DATA_WRITER_H_ |   73 #endif  // NET_QUIC_QUIC_DATA_WRITER_H_ | 
| OLD | NEW |