Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1484)

Unified Diff: blimp/net/grpc_stream.h

Issue 2462183002: GRPC Stream implementation of HeliumStream
Patch Set: Implement helium::Stream Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/net/grpc_stream.h
diff --git a/blimp/net/grpc_stream.h b/blimp/net/grpc_stream.h
new file mode 100644
index 0000000000000000000000000000000000000000..cea60ae1dcbe930cb86f1641b05c0b8cf9ae11c8
--- /dev/null
+++ b/blimp/net/grpc_stream.h
@@ -0,0 +1,99 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BLIMP_NET_GRPC_STREAM_DELEGATE_H_
+#define BLIMP_NET_GRPC_STREAM_DELEGATE_H_
+
+#include <list>
+
+#include "base/macros.h"
+
+#include "base/callback.h"
+#include "base/command_line.h"
+#include "base/memory/ptr_util.h"
+#include "base/synchronization/lock.h"
+#include "base/threading/thread.h"
+
+#include "blimp/common/proto/helium_service.grpc.pb.h"
+#include "blimp/helium/stream.h"
+#include "net/base/completion_callback.h"
+
+namespace blimp {
+
+using helium::Stream;
+using helium::Result;
+
+class GrpcStream : public Stream {
+ public:
+ explicit GrpcStream(const net::CompletionCallback& connection_callback);
+
+ ~GrpcStream() override;
+
+ protected:
+ class GrpcTag {
+ public:
+ static GrpcTag* Connect(const net::CompletionCallback& connection_cb);
+ static GrpcTag* Write(const Stream::SendMessageCallback& callback);
+ static GrpcTag* Read(const Stream::ReceiveMessageCallback& callback);
+ static GrpcTag* WritesDone();
+
+ ~GrpcTag();
+
+ HeliumWrapper* GetReceivedMsg() const;
+
+ private:
+ GrpcTag();
+
+ void ApplyCallbackOnCbThread(bool ok);
+
+ enum class TagType {
+ // Invalid tag type.
+ UNKNOWN = 0,
+ // Tag used during initial connection.
+ CONNECT = 1,
+ // Tag used during waiting for a read to finish.
+ READ = 2,
+ // Tag used during waiting for a single write to finish.
+ WRITE = 3,
+ // Tag used when a sequence of writes has finished.
+ WRITES_DONE = 4
+ };
+
+ TagType tag_type_;
+
+ // For initial connection.
+ net::CompletionCallback connection_cb_;
+
+ // For sending.
+ Stream::SendMessageCallback sent_cb_;
+
+ // For receiving.
+ std::unique_ptr<HeliumWrapper> received_msg_;
+ Stream::ReceiveMessageCallback received_cb_;
+
+ friend class GrpcStream;
+ };
+
+ GrpcStream::GrpcTag* GrpcConnectTag();
+ GrpcStream::GrpcTag* GrpcReadTag(const Stream::ReceiveMessageCallback& received_cb);
+ GrpcStream::GrpcTag* GrpcWriteTag(const Stream::SendMessageCallback& sent_cb);
+ GrpcStream::GrpcTag* GrpcWritesDoneTag();
+
+ void StartCompletionQueueThread(grpc::CompletionQueue* completion_q);
+
+ private:
+ static void CompletionQueueThread(
+ grpc::CompletionQueue* completion_q,
+ scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner);
+
+ // TODO(perumaal): Use ThreadChecker.
+ scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_;
+ net::CompletionCallback connection_callback_;
+ std::unique_ptr<base::Thread> grpc_thread_;
+ base::WeakPtrFactory<GrpcStream> weak_factory_;
+};
+
+} // namespace blimp
+
+#endif // BLIMP_NET_GRPC_STREAM_DELEGATE_H_

Powered by Google App Engine
This is Rietveld 408576698