| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 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 BLIMP_NET_STREAM_PACKET_READER_H_ | |
| 6 #define BLIMP_NET_STREAM_PACKET_READER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "blimp/net/blimp_net_export.h" | |
| 14 #include "blimp/net/packet_reader.h" | |
| 15 #include "net/base/completion_callback.h" | |
| 16 #include "net/base/net_errors.h" | |
| 17 | |
| 18 namespace net { | |
| 19 class GrowableIOBuffer; | |
| 20 class StreamSocket; | |
| 21 } // namespace net | |
| 22 | |
| 23 namespace blimp { | |
| 24 | |
| 25 // Reads opaque length-prefixed packets of bytes from a StreamSocket. | |
| 26 // The header segment is 32-bit, encoded in network byte order. | |
| 27 // The body segment length is specified in the header (capped at | |
| 28 // kMaxPacketPayloadSizeBytes). | |
| 29 class BLIMP_NET_EXPORT StreamPacketReader : public PacketReader { | |
| 30 public: | |
| 31 // |socket|: The socket to read packets from. The caller must ensure |socket| | |
| 32 // is valid while the reader is in-use (see ReadPacket below). | |
| 33 explicit StreamPacketReader(net::StreamSocket* socket); | |
| 34 | |
| 35 ~StreamPacketReader() override; | |
| 36 | |
| 37 // PacketReader implementation. | |
| 38 void ReadPacket(const scoped_refptr<net::GrowableIOBuffer>& buf, | |
| 39 const net::CompletionCallback& cb) override; | |
| 40 | |
| 41 private: | |
| 42 enum class ReadState { | |
| 43 IDLE, | |
| 44 HEADER, | |
| 45 PAYLOAD, | |
| 46 }; | |
| 47 | |
| 48 friend std::ostream& operator<<(std::ostream& out, const ReadState state); | |
| 49 | |
| 50 // State machine implementation. | |
| 51 // |result| - the result value of the most recent network operation. | |
| 52 // See comments for ReadPacket() for documentation on return values. | |
| 53 int DoReadLoop(int result); | |
| 54 | |
| 55 // Reads the header and parses it when, done, to get the payload size. | |
| 56 int DoReadHeader(int result); | |
| 57 | |
| 58 // Reads payload bytes until the payload is complete. | |
| 59 int DoReadPayload(int result); | |
| 60 | |
| 61 // Executes a socket read. | |
| 62 // Returns a positive value indicating the number of bytes read on success. | |
| 63 // Returns a negative net::Error value if the socket was closed or an error | |
| 64 // occurred. | |
| 65 int DoRead(net::IOBuffer* buf, int buf_len); | |
| 66 | |
| 67 // Processes an asynchronous header or payload read, and invokes |callback_| | |
| 68 // on packet read completion. | |
| 69 void OnReadComplete(int result); | |
| 70 | |
| 71 ReadState read_state_; | |
| 72 | |
| 73 net::StreamSocket* socket_; | |
| 74 | |
| 75 // The size of the payload, in bytes. | |
| 76 size_t payload_size_; | |
| 77 | |
| 78 scoped_refptr<net::GrowableIOBuffer> header_buffer_; | |
| 79 scoped_refptr<net::GrowableIOBuffer> payload_buffer_; | |
| 80 net::CompletionCallback callback_; | |
| 81 | |
| 82 base::WeakPtrFactory<StreamPacketReader> weak_factory_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(StreamPacketReader); | |
| 85 }; | |
| 86 | |
| 87 } // namespace blimp | |
| 88 | |
| 89 #endif // BLIMP_NET_STREAM_PACKET_READER_H_ | |
| OLD | NEW |