Chromium Code Reviews| Index: blimp/net/grpc_engine_stream.cc |
| diff --git a/blimp/net/grpc_engine_stream.cc b/blimp/net/grpc_engine_stream.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1c5c999a95a71c115cc17de4b592ec18350eb1f |
| --- /dev/null |
| +++ b/blimp/net/grpc_engine_stream.cc |
| @@ -0,0 +1,54 @@ |
| +// 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. |
| + |
| +#include "base/bind_helpers.h" |
| +#include "blimp/net/grpc_engine_stream.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +#include "base/bind_helpers.h" |
| + |
| +namespace blimp { |
| + |
| +GrpcEngineStream::GrpcEngineStream( |
| + const std::string& ip_address, |
|
Kevin M
2016/10/31 21:33:25
Pass around validated IPEndpoints internally.
|
| + const net::CompletionCallback& connection_callback) |
| + : GrpcStream(connection_callback), ip_address_(ip_address) { |
| + grpc::ServerBuilder builder; |
| + builder.AddListeningPort(ip_address_, grpc::InsecureServerCredentials()); |
| + builder.RegisterService(&service_); |
| + completion_queue_ = builder.AddCompletionQueue(); |
| + server_ = builder.BuildAndStart(); |
| + stream_ = base::MakeUnique< |
| + grpc::ServerAsyncReaderWriter<HeliumWrapper, HeliumWrapper>>(&context_); |
| + service_.RequestStream(&context_, stream_.get(), completion_queue_.get(), |
| + completion_queue_.get(), |
| + reinterpret_cast<void*>(GrpcConnectTag())); |
| + StartCompletionQueueThread(completion_queue_.get()); |
| + DVLOG(3) << "Starting engine stream @ " << ip_address; |
| +} |
| + |
| +void GrpcEngineStream::SendMessage( |
| + std::unique_ptr<HeliumWrapper> helium_message, |
| + const HeliumMessageSentCb& sent_cb) { |
| + DVLOG(3) << "Sending: " << helium_message->ByteSize() << " bytes"; |
| + DCHECK(helium_message->serialized_helium_message().size() > 0); |
| + stream_->Write(*helium_message.get(), |
| + reinterpret_cast<void*>(GrpcWriteTag(sent_cb))); |
| +} |
| + |
| +void GrpcEngineStream::ReceiveMessage( |
| + const HeliumMessageReceivedCb& received_cb) { |
| + DVLOG(3) << "Receiving (waiting for callback)"; |
| + auto tag = GrpcReadTag(received_cb); |
| + stream_->Read(tag->GetReceivedMsg(), reinterpret_cast<void*>(tag)); |
| +} |
| + |
| +GrpcEngineStream::~GrpcEngineStream() { |
| + LOG(INFO) << "Engine stream shutting down."; |
| + server_->Shutdown(); |
| + |
| + completion_queue_->Shutdown(); |
| + completion_queue_.release(); |
| +} |
| +} // namespace blimp |