blob: 18744f4504bc6db372a5af7b7f4eb0bab66c5560 [file] [log] [blame]
kmarshall90e226972015-11-05 21:55:531// 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_WRITER_H_
6#define BLIMP_NET_STREAM_PACKET_WRITER_H_
7
8#include <string>
9
10#include "base/macros.h"
11#include "base/memory/ref_counted.h"
12#include "base/threading/thread_checker.h"
13#include "blimp/net/blimp_net_export.h"
14#include "blimp/net/packet_writer.h"
15#include "net/base/completion_callback.h"
16#include "net/base/net_errors.h"
17
18namespace net {
19class DrainableIOBuffer;
20class StreamSocket;
21} // namespace net
22
23namespace blimp {
24
25// Writes opaque length-prefixed packets to a StreamSocket.
26// The header segment is 32-bit, encoded in network byte order.
27// The body segment length is specified in the header (should be capped at
28// kMaxPacketPayloadSizeBytes).
29class BLIMP_NET_EXPORT StreamPacketWriter : public PacketWriter {
30 public:
31 // |socket|: The socket to write packets to. The caller must ensure |socket|
32 // is valid while the reader is in-use (see ReadPacket below).
kmarshall2c9854582016-08-12 00:17:1233 explicit StreamPacketWriter(net::StreamSocket* socket);
kmarshall90e226972015-11-05 21:55:5334
35 ~StreamPacketWriter() override;
36
37 // PacketWriter implementation.
kmarshallb62c5692016-03-25 04:44:4038 void WritePacket(const scoped_refptr<net::DrainableIOBuffer>& data,
kmarshall840a0fe2015-12-03 00:27:3039 const net::CompletionCallback& callback) override;
kmarshall90e226972015-11-05 21:55:5340
41 private:
42 enum class WriteState {
43 IDLE,
44 HEADER,
45 PAYLOAD,
46 };
47
48 friend std::ostream& operator<<(std::ostream& out, const WriteState state);
49
50 // State machine implementation.
51 // |result| - the result value of the most recent network operation.
52 // See comments for WritePacket() for documentation on return values.
53 int DoWriteLoop(int result);
54
55 int DoWriteHeader(int result);
56
57 int DoWritePayload(int result);
58
59 // Callback function to be invoked on asynchronous write completion.
60 // Invokes |callback_| on packet write completion or on error.
61 void OnWriteComplete(int result);
62
kmarshall5c74a3e2016-06-08 18:50:3263 // Executes a socket write.
64 // Returns a positive value indicating the number of bytes written
65 // on success.
66 // Returns a negative net::Error value if the socket was closed or an error
67 // occurred.
68 int DoWrite(net::IOBuffer* buf, int buf_len);
69
kmarshall90e226972015-11-05 21:55:5370 WriteState write_state_;
71
72 net::StreamSocket* socket_;
73
74 scoped_refptr<net::DrainableIOBuffer> payload_buffer_;
75 scoped_refptr<net::DrainableIOBuffer> header_buffer_;
76 net::CompletionCallback callback_;
kmarshall90e226972015-11-05 21:55:5377
78 base::WeakPtrFactory<StreamPacketWriter> weak_factory_;
79
80 DISALLOW_COPY_AND_ASSIGN(StreamPacketWriter);
81};
82
83} // namespace blimp
84
85#endif // BLIMP_NET_STREAM_PACKET_WRITER_H_