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_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 | |
18 namespace net { | |
19 class DrainableIOBuffer; | |
20 class StreamSocket; | |
21 } // namespace net | |
22 | |
23 namespace 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). | |
29 class 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). | |
33 explicit StreamPacketWriter(net::StreamSocket* socket); | |
34 | |
35 ~StreamPacketWriter() override; | |
36 | |
37 // PacketWriter implementation. | |
38 void WritePacket(const scoped_refptr<net::DrainableIOBuffer>& data, | |
39 const net::CompletionCallback& callback) override; | |
40 | |
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 | |
63 // 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 | |
70 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_; | |
77 | |
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_ | |
OLD | NEW |