cast_transport.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_
  5. #define COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_
  6. #include <string>
  7. #include "base/containers/queue.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/threading/thread_checker.h"
  12. #include "components/cast_channel/cast_channel_enum.h"
  13. #include "components/cast_channel/logger.h"
  14. #include "net/base/completion_once_callback.h"
  15. #include "net/base/ip_endpoint.h"
  16. #include "third_party/openscreen/src/cast/common/channel/proto/cast_channel.pb.h"
  17. namespace net {
  18. class DrainableIOBuffer;
  19. class DrainableIOBuffer;
  20. class GrowableIOBuffer;
  21. class IOBuffer;
  22. class Socket;
  23. } // namespace net
  24. namespace cast_channel {
  25. using ::cast::channel::CastMessage;
  26. class MessageFramer;
  27. class CastTransport {
  28. public:
  29. virtual ~CastTransport() {}
  30. // Object to be informed of incoming messages and read errors.
  31. class Delegate {
  32. public:
  33. virtual ~Delegate() {}
  34. // Called once Transport is successfully initialized and started.
  35. // Owned read delegates are Start()ed automatically.
  36. virtual void Start() = 0;
  37. // An error occurred on the channel.
  38. // The caller is responsible for closing |socket| if an error occurred.
  39. virtual void OnError(ChannelError error_state) = 0;
  40. // A message was received on the channel.
  41. virtual void OnMessage(const CastMessage& message) = 0;
  42. };
  43. // Sends a CastMessage to |socket_|.
  44. // |message|: The message to send.
  45. // |callback|: Callback to be invoked when the write operation has finished.
  46. // Virtual for testing.
  47. virtual void SendMessage(const CastMessage& message,
  48. net::CompletionOnceCallback callback) = 0;
  49. // Initializes the reading state machine and starts reading from the
  50. // underlying socket.
  51. // Virtual for testing.
  52. virtual void Start() = 0;
  53. // Changes the delegate for processing read events. Pending reads remain
  54. // in-flight.
  55. // Ownership of the pointee of |delegate| is assumed by the transport.
  56. // Prior delegates are deleted automatically.
  57. virtual void SetReadDelegate(std::unique_ptr<Delegate> delegate) = 0;
  58. };
  59. // Manager class for reading and writing messages to/from a socket.
  60. class CastTransportImpl : public CastTransport {
  61. public:
  62. using ChannelError = ::cast_channel::ChannelError;
  63. // Interface to read/write data from a socket to ease unit-testing.
  64. class Channel {
  65. public:
  66. virtual ~Channel() {}
  67. virtual void Read(net::IOBuffer* buffer,
  68. int bytes,
  69. net::CompletionOnceCallback callback) = 0;
  70. virtual void Write(net::IOBuffer* buffer,
  71. int bytes,
  72. net::CompletionOnceCallback callback) = 0;
  73. };
  74. // Adds a CastMessage read/write layer to a socket.
  75. // Message read events are propagated to the owner via |read_delegate|.
  76. // |vlog_prefix| sets the prefix used for all VLOGged output.
  77. // |channel| and |logger| must all out-live the
  78. // CastTransportImpl instance.
  79. // |read_delegate| is owned by this CastTransportImpl object.
  80. CastTransportImpl(Channel* channel,
  81. int channel_id,
  82. const net::IPEndPoint& ip_endpoint_,
  83. scoped_refptr<Logger> logger);
  84. CastTransportImpl(const CastTransportImpl&) = delete;
  85. CastTransportImpl& operator=(const CastTransportImpl&) = delete;
  86. ~CastTransportImpl() override;
  87. // CastTransport interface.
  88. void SendMessage(const CastMessage& message,
  89. net::CompletionOnceCallback callback) override;
  90. void Start() override;
  91. void SetReadDelegate(std::unique_ptr<Delegate> delegate) override;
  92. private:
  93. // Holds a message to be written to the socket. |callback| is invoked when the
  94. // message is fully written or an error occurrs.
  95. struct WriteRequest {
  96. explicit WriteRequest(const std::string& namespace_,
  97. const std::string& payload,
  98. net::CompletionOnceCallback callback);
  99. WriteRequest(WriteRequest&& other);
  100. ~WriteRequest();
  101. // Namespace of the serialized message.
  102. std::string message_namespace;
  103. // Write completion callback, invoked when the operation has completed or
  104. // failed.
  105. net::CompletionOnceCallback callback;
  106. // Buffer with outgoing data.
  107. scoped_refptr<net::DrainableIOBuffer> io_buffer;
  108. };
  109. static bool IsTerminalReadState(ReadState read_state);
  110. static bool IsTerminalWriteState(WriteState write_state);
  111. void SetReadState(ReadState read_state);
  112. void SetWriteState(WriteState write_state);
  113. void SetErrorState(ChannelError error_state);
  114. // Terminates all in-flight write callbacks with error code ERR_FAILED.
  115. void FlushWriteQueue();
  116. // Main method that performs write flow state transitions.
  117. void OnWriteResult(int result);
  118. // Each of the below Do* method is executed in the corresponding
  119. // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
  120. // DowriteComplete is called, and so on.
  121. int DoWrite();
  122. int DoWriteComplete(int result);
  123. int DoWriteCallback();
  124. int DoWriteHandleError(int result);
  125. // Main method that performs write flow state transitions.
  126. void OnReadResult(int result);
  127. // Each of the below Do* method is executed in the corresponding
  128. // write state. For example when read state is READ_STATE_READ_COMPLETE
  129. // DoReadComplete is called, and so on.
  130. int DoRead();
  131. int DoReadComplete(int result);
  132. int DoReadCallback();
  133. int DoReadHandleError(int result);
  134. // Indicates that the transport object is started and may receive and send
  135. // messages.
  136. bool started_;
  137. // Queue of pending writes. The message at the front of the queue is the one
  138. // being written.
  139. base::queue<WriteRequest> write_queue_;
  140. // Buffer used for read operations. Reused for every read.
  141. scoped_refptr<net::GrowableIOBuffer> read_buffer_;
  142. // Constructs and parses the wire representation of message frames.
  143. std::unique_ptr<MessageFramer> framer_;
  144. // Last message received on the socket.
  145. std::unique_ptr<CastMessage> current_message_;
  146. // Channel used for I/O operations.
  147. const raw_ptr<Channel> channel_;
  148. // Methods for communicating message receipt and error status to client code.
  149. std::unique_ptr<Delegate> delegate_;
  150. // Write flow state machine state.
  151. WriteState write_state_;
  152. // Read flow state machine state.
  153. ReadState read_state_;
  154. // The last error encountered by the channel.
  155. ChannelError error_state_;
  156. // Connection metadata for logging purposes.
  157. // Socket ID assigned by ApiResourceManager.
  158. int channel_id_;
  159. // IP address of the remote end.
  160. const net::IPEndPoint ip_endpoint_;
  161. // Accumulates details of events and errors, for debugging purposes.
  162. scoped_refptr<Logger> logger_;
  163. SEQUENCE_CHECKER(sequence_checker_);
  164. };
  165. } // namespace cast_channel
  166. #endif // COMPONENTS_CAST_CHANNEL_CAST_TRANSPORT_H_