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 "net/base/completion_callback.h" | |
20 | |
21 namespace blimp { | |
22 | |
23 // Callbacks are always invoked in the IO thread. | |
24 class GrpcStream { | |
Kevin M
2016/10/31 21:33:26
Why doesn't this implement HeliumStream?
| |
25 public: | |
26 explicit GrpcStream(const net::CompletionCallback& connection_callback); | |
27 | |
28 // TODO(perumaal): Currently the result is an 'int', but in the future it will | |
29 // be HeliumResult. | |
30 using HeliumMessageReceivedCb = | |
31 base::Callback<void(std::unique_ptr<HeliumWrapper>, int)>; | |
32 | |
33 using HeliumMessageSentCb = base::Callback<void(int)>; | |
34 | |
35 // TODO(perumaal): Convert to HeliumStream/HeliumResult next. | |
36 virtual void SendMessage(std::unique_ptr<HeliumWrapper> helium_message, | |
37 const HeliumMessageSentCb& callback) = 0; | |
38 | |
39 virtual void ReceiveMessage(const HeliumMessageReceivedCb& received_cb) = 0; | |
40 | |
41 virtual ~GrpcStream(); | |
42 | |
43 protected: | |
44 class GrpcTag { | |
45 public: | |
46 static GrpcTag* Connect(const net::CompletionCallback& connection_cb); | |
47 static GrpcTag* Write(const GrpcStream::HeliumMessageSentCb& callback); | |
48 static GrpcTag* Read(const GrpcStream::HeliumMessageReceivedCb& callback); | |
49 static GrpcTag* WritesDone(); | |
50 | |
51 ~GrpcTag(); | |
52 | |
53 HeliumWrapper* GetReceivedMsg() const; | |
54 | |
55 private: | |
56 GrpcTag(); | |
57 | |
58 void ApplyCallbackOnCbThread(bool ok); | |
59 | |
60 enum class TagType { | |
61 // Invalid tag type. | |
62 UNKNOWN = 0, | |
63 // Tag used during initial connection. | |
64 CONNECT = 1, | |
65 // Tag used during waiting for a read to finish. | |
66 READ = 2, | |
67 // Tag used during waiting for a single write to finish. | |
68 WRITE = 3, | |
69 // Tag used when a sequence of writes has finished. | |
70 WRITES_DONE = 4 | |
71 }; | |
72 | |
73 TagType tag_type_; | |
74 | |
75 // For initial connection. | |
76 net::CompletionCallback connection_cb_; | |
77 | |
78 // For sending. | |
79 GrpcStream::HeliumMessageSentCb sent_cb_; | |
80 | |
81 // For receiving. | |
82 std::unique_ptr<HeliumWrapper> received_msg_; | |
83 GrpcStream::HeliumMessageReceivedCb received_cb_; | |
84 | |
85 friend class GrpcStream; | |
86 }; | |
87 | |
88 GrpcStream::GrpcTag* GrpcConnectTag(); | |
89 GrpcStream::GrpcTag* GrpcReadTag(const HeliumMessageReceivedCb& received_cb); | |
90 GrpcStream::GrpcTag* GrpcWriteTag(const HeliumMessageSentCb& sent_cb); | |
91 GrpcStream::GrpcTag* GrpcWritesDoneTag(); | |
92 | |
93 void StartCompletionQueueThread(grpc::CompletionQueue* completion_q); | |
94 | |
95 private: | |
96 static void CompletionQueueThread( | |
97 grpc::CompletionQueue* completion_q, | |
98 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner); | |
99 | |
Kevin M
2016/10/31 21:33:25
Use a ThreadChecker?
| |
100 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; | |
101 net::CompletionCallback connection_callback_; | |
102 std::unique_ptr<base::Thread> grpc_thread_; | |
103 base::WeakPtrFactory<GrpcStream> weak_factory_; | |
104 }; | |
105 | |
106 } // namespace blimp | |
107 | |
108 #endif // BLIMP_NET_GRPC_STREAM_DELEGATE_H_ | |
OLD | NEW |