OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
6 #define COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
7 | |
8 #include <queue> | |
9 #include <string> | |
10 | |
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 "components/cast_channel/proto/logging.pb.h" | |
18 #include "net/base/completion_callback.h" | |
19 #include "net/base/ip_endpoint.h" | |
20 | |
21 namespace net { | |
22 class DrainableIOBuffer; | |
23 class DrainableIOBuffer; | |
24 class GrowableIOBuffer; | |
25 class Socket; | |
26 } // namespace net | |
27 | |
28 namespace cast_channel { | |
29 class CastMessage; | |
30 class MessageFramer; | |
31 | |
32 class CastTransport { | |
33 public: | |
34 virtual ~CastTransport() {} | |
35 | |
36 // Object to be informed of incoming messages and read errors. | |
37 class Delegate { | |
38 public: | |
39 using ChannelError = ::cast_channel::ChannelError; | |
40 | |
41 virtual ~Delegate() {} | |
42 | |
43 // Called once Transport is successfully initialized and started. | |
44 // Owned read delegates are Start()ed automatically. | |
45 virtual void Start() = 0; | |
46 | |
47 // An error occurred on the channel. | |
48 // The caller is responsible for closing |socket| if an error occurred. | |
49 virtual void OnError(ChannelError error_state) = 0; | |
50 | |
51 // A message was received on the channel. | |
52 virtual void OnMessage(const CastMessage& message) = 0; | |
53 }; | |
54 | |
55 // Sends a CastMessage to |socket_|. | |
56 // |message|: The message to send. | |
57 // |callback|: Callback to be invoked when the write operation has finished. | |
58 // Virtual for testing. | |
59 virtual void SendMessage(const CastMessage& message, | |
60 const net::CompletionCallback& callback) = 0; | |
61 | |
62 // Initializes the reading state machine and starts reading from the | |
63 // underlying socket. | |
64 // Virtual for testing. | |
65 virtual void Start() = 0; | |
66 | |
67 // Changes the delegate for processing read events. Pending reads remain | |
68 // in-flight. | |
69 // Ownership of the pointee of |delegate| is assumed by the transport. | |
70 // Prior delegates are deleted automatically. | |
71 virtual void SetReadDelegate(std::unique_ptr<Delegate> delegate) = 0; | |
72 }; | |
73 | |
74 // Manager class for reading and writing messages to/from a socket. | |
75 class CastTransportImpl : public CastTransport { | |
76 public: | |
77 using ChannelAuthType = ::cast_channel::ChannelAuthType; | |
78 using ChannelError = ::cast_channel::ChannelError; | |
79 | |
80 // Adds a CastMessage read/write layer to a socket. | |
81 // Message read events are propagated to the owner via |read_delegate|. | |
82 // |vlog_prefix| sets the prefix used for all VLOGged output. | |
83 // |socket| and |logger| must all out-live the | |
84 // CastTransportImpl instance. | |
85 // |read_delegate| is owned by this CastTransportImpl object. | |
86 CastTransportImpl(net::Socket* socket, | |
87 int channel_id, | |
88 const net::IPEndPoint& ip_endpoint_, | |
89 ChannelAuthType channel_auth_, | |
90 scoped_refptr<Logger> logger); | |
91 | |
92 ~CastTransportImpl() override; | |
93 | |
94 // CastTransport interface. | |
95 void SendMessage(const CastMessage& message, | |
96 const net::CompletionCallback& callback) override; | |
97 void Start() override; | |
98 void SetReadDelegate(std::unique_ptr<Delegate> delegate) override; | |
99 | |
100 private: | |
101 // Internal write states. | |
102 enum WriteState { | |
103 WRITE_STATE_UNKNOWN, | |
104 WRITE_STATE_WRITE, | |
105 WRITE_STATE_WRITE_COMPLETE, | |
106 WRITE_STATE_DO_CALLBACK, | |
107 WRITE_STATE_HANDLE_ERROR, | |
108 WRITE_STATE_ERROR, | |
109 WRITE_STATE_IDLE, | |
110 }; | |
111 | |
112 // Internal read states. | |
113 enum ReadState { | |
114 READ_STATE_UNKNOWN, | |
115 READ_STATE_READ, | |
116 READ_STATE_READ_COMPLETE, | |
117 READ_STATE_DO_CALLBACK, | |
118 READ_STATE_HANDLE_ERROR, | |
119 READ_STATE_ERROR, | |
120 }; | |
121 | |
122 // Holds a message to be written to the socket. |callback| is invoked when the | |
123 // message is fully written or an error occurrs. | |
124 struct WriteRequest { | |
125 explicit WriteRequest(const std::string& namespace_, | |
126 const std::string& payload, | |
127 const net::CompletionCallback& callback); | |
128 WriteRequest(const WriteRequest& other); | |
129 ~WriteRequest(); | |
130 | |
131 // Namespace of the serialized message. | |
132 std::string message_namespace; | |
133 // Write completion callback, invoked when the operation has completed or | |
134 // failed. | |
135 net::CompletionCallback callback; | |
136 // Buffer with outgoing data. | |
137 scoped_refptr<net::DrainableIOBuffer> io_buffer; | |
138 }; | |
139 | |
140 static proto::ReadState ReadStateToProto(CastTransportImpl::ReadState state); | |
141 static proto::WriteState WriteStateToProto( | |
142 CastTransportImpl::WriteState state); | |
143 static proto::ErrorState ErrorStateToProto(ChannelError state); | |
144 static bool IsTerminalReadState(ReadState read_state); | |
145 static bool IsTerminalWriteState(WriteState write_state); | |
146 | |
147 void SetReadState(ReadState read_state); | |
148 void SetWriteState(WriteState write_state); | |
149 void SetErrorState(ChannelError error_state); | |
150 | |
151 // Terminates all in-flight write callbacks with error code ERR_FAILED. | |
152 void FlushWriteQueue(); | |
153 | |
154 // Main method that performs write flow state transitions. | |
155 void OnWriteResult(int result); | |
156 | |
157 // Each of the below Do* method is executed in the corresponding | |
158 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE | |
159 // DowriteComplete is called, and so on. | |
160 int DoWrite(); | |
161 int DoWriteComplete(int result); | |
162 int DoWriteCallback(); | |
163 int DoWriteHandleError(int result); | |
164 | |
165 // Main method that performs write flow state transitions. | |
166 void OnReadResult(int result); | |
167 | |
168 // Each of the below Do* method is executed in the corresponding | |
169 // write state. For example when read state is READ_STATE_READ_COMPLETE | |
170 // DoReadComplete is called, and so on. | |
171 int DoRead(); | |
172 int DoReadComplete(int result); | |
173 int DoReadCallback(); | |
174 int DoReadHandleError(int result); | |
175 | |
176 // Indicates that the transport object is started and may receive and send | |
177 // messages. | |
178 bool started_; | |
179 | |
180 // Queue of pending writes. The message at the front of the queue is the one | |
181 // being written. | |
182 std::queue<WriteRequest> write_queue_; | |
183 | |
184 // Buffer used for read operations. Reused for every read. | |
185 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
186 | |
187 // Constructs and parses the wire representation of message frames. | |
188 std::unique_ptr<MessageFramer> framer_; | |
189 | |
190 // Last message received on the socket. | |
191 std::unique_ptr<CastMessage> current_message_; | |
192 | |
193 // Socket used for I/O operations. | |
194 net::Socket* const socket_; | |
195 | |
196 // Methods for communicating message receipt and error status to client code. | |
197 std::unique_ptr<Delegate> delegate_; | |
198 | |
199 // Write flow state machine state. | |
200 WriteState write_state_; | |
201 | |
202 // Read flow state machine state. | |
203 ReadState read_state_; | |
204 | |
205 // The last error encountered by the channel. | |
206 ChannelError error_state_; | |
207 | |
208 // Connection metadata for logging purposes. | |
209 // Socket ID assigned by ApiResourceManager. | |
210 int channel_id_; | |
211 | |
212 // IP address of the remote end. | |
213 const net::IPEndPoint ip_endpoint_; | |
214 | |
215 // Authentication level for the connection. | |
216 ChannelAuthType channel_auth_; | |
217 | |
218 // Accumulates details of events and errors, for debugging purposes. | |
219 scoped_refptr<Logger> logger_; | |
220 | |
221 SEQUENCE_CHECKER(sequence_checker_); | |
222 | |
223 DISALLOW_COPY_AND_ASSIGN(CastTransportImpl); | |
224 }; | |
225 } // namespace cast_channel | |
226 | |
227 #endif // COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_ | |
OLD | NEW |