1 // Copyright 2014 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.
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_
10 #include "base/containers/queue.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequence_checker.h"
14 #include "base/threading/thread_checker.h"
15 #include "components/cast_channel/cast_channel_enum.h"
16 #include "components/cast_channel/logger.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/completion_once_callback.h"
19 #include "net/base/ip_endpoint.h"
22 class DrainableIOBuffer;
23 class DrainableIOBuffer;
24 class GrowableIOBuffer;
29 namespace cast_channel {
35 virtual ~CastTransport() {}
37 // Object to be informed of incoming messages and read errors.
40 virtual ~Delegate() {}
42 // Called once Transport is successfully initialized and started.
43 // Owned read delegates are Start()ed automatically.
44 virtual void Start() = 0;
46 // An error occurred on the channel.
47 // The caller is responsible for closing |socket| if an error occurred.
48 virtual void OnError(ChannelError error_state) = 0;
50 // A message was received on the channel.
51 virtual void OnMessage(const CastMessage& message) = 0;
54 // Sends a CastMessage to |socket_|.
55 // |message|: The message to send.
56 // |callback|: Callback to be invoked when the write operation has finished.
57 // Virtual for testing.
58 virtual void SendMessage(const CastMessage& message,
59 const net::CompletionCallback& callback) = 0;
61 // Initializes the reading state machine and starts reading from the
63 // Virtual for testing.
64 virtual void Start() = 0;
66 // Changes the delegate for processing read events. Pending reads remain
68 // Ownership of the pointee of |delegate| is assumed by the transport.
69 // Prior delegates are deleted automatically.
70 virtual void SetReadDelegate(std::unique_ptr<Delegate> delegate) = 0;
73 // Manager class for reading and writing messages to/from a socket.
74 class CastTransportImpl : public CastTransport {
76 using ChannelError = ::cast_channel::ChannelError;
78 // Interface to read/write data from a socket to ease unit-testing.
82 virtual void Read(net::IOBuffer* buffer,
84 net::CompletionOnceCallback callback) = 0;
85 virtual void Write(net::IOBuffer* buffer,
87 net::CompletionOnceCallback callback) = 0;
90 // Adds a CastMessage read/write layer to a socket.
91 // Message read events are propagated to the owner via |read_delegate|.
92 // |vlog_prefix| sets the prefix used for all VLOGged output.
93 // |channel| and |logger| must all out-live the
94 // CastTransportImpl instance.
95 // |read_delegate| is owned by this CastTransportImpl object.
96 CastTransportImpl(Channel* channel,
98 const net::IPEndPoint& ip_endpoint_,
99 scoped_refptr<Logger> logger);
101 ~CastTransportImpl() override;
103 // CastTransport interface.
104 void SendMessage(const CastMessage& message,
105 const net::CompletionCallback& callback) override;
106 void Start() override;
107 void SetReadDelegate(std::unique_ptr<Delegate> delegate) override;
110 // Holds a message to be written to the socket. |callback| is invoked when the
111 // message is fully written or an error occurrs.
112 struct WriteRequest {
113 explicit WriteRequest(const std::string& namespace_,
114 const std::string& payload,
115 const net::CompletionCallback& callback);
116 WriteRequest(const WriteRequest& other);
119 // Namespace of the serialized message.
120 std::string message_namespace;
121 // Write completion callback, invoked when the operation has completed or
123 net::CompletionCallback callback;
124 // Buffer with outgoing data.
125 scoped_refptr<net::DrainableIOBuffer> io_buffer;
128 static bool IsTerminalReadState(ReadState read_state);
129 static bool IsTerminalWriteState(WriteState write_state);
131 void SetReadState(ReadState read_state);
132 void SetWriteState(WriteState write_state);
133 void SetErrorState(ChannelError error_state);
135 // Terminates all in-flight write callbacks with error code ERR_FAILED.
136 void FlushWriteQueue();
138 // Main method that performs write flow state transitions.
139 void OnWriteResult(int result);
141 // Each of the below Do* method is executed in the corresponding
142 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
143 // DowriteComplete is called, and so on.
145 int DoWriteComplete(int result);
146 int DoWriteCallback();
147 int DoWriteHandleError(int result);
149 // Main method that performs write flow state transitions.
150 void OnReadResult(int result);
152 // Each of the below Do* method is executed in the corresponding
153 // write state. For example when read state is READ_STATE_READ_COMPLETE
154 // DoReadComplete is called, and so on.
156 int DoReadComplete(int result);
157 int DoReadCallback();
158 int DoReadHandleError(int result);
160 // Indicates that the transport object is started and may receive and send
164 // Queue of pending writes. The message at the front of the queue is the one
166 base::queue<WriteRequest> write_queue_;
168 // Buffer used for read operations. Reused for every read.
169 scoped_refptr<net::GrowableIOBuffer> read_buffer_;
171 // Constructs and parses the wire representation of message frames.
172 std::unique_ptr<MessageFramer> framer_;
174 // Last message received on the socket.
175 std::unique_ptr<CastMessage> current_message_;
177 // Channel used for I/O operations.
178 Channel* const channel_;
180 // Methods for communicating message receipt and error status to client code.
181 std::unique_ptr<Delegate> delegate_;
183 // Write flow state machine state.
184 WriteState write_state_;
186 // Read flow state machine state.
187 ReadState read_state_;
189 // The last error encountered by the channel.
190 ChannelError error_state_;
192 // Connection metadata for logging purposes.
193 // Socket ID assigned by ApiResourceManager.
196 // IP address of the remote end.
197 const net::IPEndPoint ip_endpoint_;
199 // Accumulates details of events and errors, for debugging purposes.
200 scoped_refptr<Logger> logger_;
202 SEQUENCE_CHECKER(sequence_checker_);
204 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl);
206 } // namespace cast_channel
208 #endif // COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_