OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_GRPC_STREAM_DELEGATE_H_ |
| 6 #define BLIMP_NET_GRPC_STREAM_DELEGATE_H_ |
| 7 |
| 8 #include <list> |
| 9 |
| 10 #include "base/macros.h" |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/command_line.h" |
| 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread.h" |
| 17 |
| 18 #include "blimp/common/proto/helium_service.grpc.pb.h" |
| 19 #include "blimp/helium/stream.h" |
| 20 #include "net/base/completion_callback.h" |
| 21 |
| 22 namespace blimp { |
| 23 |
| 24 using helium::Stream; |
| 25 using helium::Result; |
| 26 |
| 27 class GrpcStream : public Stream { |
| 28 public: |
| 29 explicit GrpcStream(const net::CompletionCallback& connection_callback); |
| 30 |
| 31 ~GrpcStream() override; |
| 32 |
| 33 protected: |
| 34 class GrpcTag { |
| 35 public: |
| 36 static GrpcTag* Connect(const net::CompletionCallback& connection_cb); |
| 37 static GrpcTag* Write(const Stream::SendMessageCallback& callback); |
| 38 static GrpcTag* Read(const Stream::ReceiveMessageCallback& callback); |
| 39 static GrpcTag* WritesDone(); |
| 40 |
| 41 ~GrpcTag(); |
| 42 |
| 43 HeliumWrapper* GetReceivedMsg() const; |
| 44 |
| 45 private: |
| 46 GrpcTag(); |
| 47 |
| 48 void ApplyCallbackOnCbThread(bool ok); |
| 49 |
| 50 enum class TagType { |
| 51 // Invalid tag type. |
| 52 UNKNOWN = 0, |
| 53 // Tag used during initial connection. |
| 54 CONNECT = 1, |
| 55 // Tag used during waiting for a read to finish. |
| 56 READ = 2, |
| 57 // Tag used during waiting for a single write to finish. |
| 58 WRITE = 3, |
| 59 // Tag used when a sequence of writes has finished. |
| 60 WRITES_DONE = 4 |
| 61 }; |
| 62 |
| 63 TagType tag_type_; |
| 64 |
| 65 // For initial connection. |
| 66 net::CompletionCallback connection_cb_; |
| 67 |
| 68 // For sending. |
| 69 Stream::SendMessageCallback sent_cb_; |
| 70 |
| 71 // For receiving. |
| 72 std::unique_ptr<HeliumWrapper> received_msg_; |
| 73 Stream::ReceiveMessageCallback received_cb_; |
| 74 |
| 75 friend class GrpcStream; |
| 76 }; |
| 77 |
| 78 GrpcStream::GrpcTag* GrpcConnectTag(); |
| 79 GrpcStream::GrpcTag* GrpcReadTag(const Stream::ReceiveMessageCallback& receive
d_cb); |
| 80 GrpcStream::GrpcTag* GrpcWriteTag(const Stream::SendMessageCallback& sent_cb); |
| 81 GrpcStream::GrpcTag* GrpcWritesDoneTag(); |
| 82 |
| 83 void StartCompletionQueueThread(grpc::CompletionQueue* completion_q); |
| 84 |
| 85 private: |
| 86 static void CompletionQueueThread( |
| 87 grpc::CompletionQueue* completion_q, |
| 88 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner); |
| 89 |
| 90 // TODO(perumaal): Use ThreadChecker. |
| 91 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; |
| 92 net::CompletionCallback connection_callback_; |
| 93 std::unique_ptr<base::Thread> grpc_thread_; |
| 94 base::WeakPtrFactory<GrpcStream> weak_factory_; |
| 95 }; |
| 96 |
| 97 } // namespace blimp |
| 98 |
| 99 #endif // BLIMP_NET_GRPC_STREAM_DELEGATE_H_ |
OLD | NEW |