diff options
author | kmarshall <kmarshall@chromium.org> | 2015-11-05 13:55:53 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-05 21:56:35 +0000 |
commit | 90e22697d8d163452c067e88c2bae8b476380077 (patch) | |
tree | a4660eecb68cebf2cc0d3c4d154efa62b1a96451 /blimp/net/stream_packet_reader.h | |
parent | 738501d6098043ccd3cd84f6ba6a47cbdd3034d0 (diff) | |
download | chromium_src-90e22697d8d163452c067e88c2bae8b476380077.zip chromium_src-90e22697d8d163452c067e88c2bae8b476380077.tar.gz chromium_src-90e22697d8d163452c067e88c2bae8b476380077.tar.bz2 |
Change BlimpTransport to vend BlimpConnections.
This removes the dependency StreamSocket from BlimpTransport.
Make PacketReader and PacketWriter stream/datagram agnostic,
moving framing logic to the new classes StreamPacketReader and
StreamPacketWriter.
Add channel reader/writer objects and fields to BlimpConnection,
and create a specialization of BlimpConnection for receiving
ownership of StreamSocket.
R=wez@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1430963002
Cr-Commit-Position: refs/heads/master@{#358142}
Diffstat (limited to 'blimp/net/stream_packet_reader.h')
-rw-r--r-- | blimp/net/stream_packet_reader.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/blimp/net/stream_packet_reader.h b/blimp/net/stream_packet_reader.h new file mode 100644 index 0000000..4468638 --- /dev/null +++ b/blimp/net/stream_packet_reader.h @@ -0,0 +1,81 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BLIMP_NET_STREAM_PACKET_READER_H_ +#define BLIMP_NET_STREAM_PACKET_READER_H_ + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" +#include "blimp/net/blimp_net_export.h" +#include "blimp/net/packet_reader.h" +#include "net/base/completion_callback.h" +#include "net/base/net_errors.h" + +namespace net { +class GrowableIOBuffer; +class StreamSocket; +} // namespace net + +namespace blimp { + +// Reads opaque length-prefixed packets of bytes from a StreamSocket. +// The header segment is 32-bit, encoded in network byte order. +// The body segment length is specified in the header (capped at +// kMaxPacketPayloadSizeBytes). +class BLIMP_NET_EXPORT StreamPacketReader : public PacketReader { + public: + // |socket|: The socket to read packets from. The caller must ensure |socket| + // is valid while the reader is in-use (see ReadPacket below). + explicit StreamPacketReader(net::StreamSocket* socket); + + ~StreamPacketReader() override; + + // PacketReader implementation. + int ReadPacket(const scoped_refptr<net::GrowableIOBuffer>& buf, + const net::CompletionCallback& cb) override; + + private: + enum class ReadState { + IDLE, + HEADER, + PAYLOAD, + }; + + friend std::ostream& operator<<(std::ostream& out, const ReadState state); + + // State machine implementation. + // |result| - the result value of the most recent network operation. + // See comments for ReadPacket() for documentation on return values. + int DoReadLoop(int result); + + // Reads the header and parses it when, done, to get the payload size. + int DoReadHeader(int result); + + // Reads payload bytes until the payload is complete. + int DoReadPayload(int result); + + // Processes an asynchronous header or payload read, and invokes |callback_| + // on packet read completion. + void OnReadComplete(int result); + + ReadState read_state_; + + net::StreamSocket* socket_; + + // The size of the payload, in bytes. + size_t payload_size_; + + scoped_refptr<net::GrowableIOBuffer> header_buffer_; + scoped_refptr<net::GrowableIOBuffer> payload_buffer_; + net::CompletionCallback callback_; + + base::WeakPtrFactory<StreamPacketReader> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(StreamPacketReader); +}; + +} // namespace blimp + +#endif // BLIMP_NET_STREAM_PACKET_READER_H_ |