Chromium Code Reviews| 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 #include "base/bind_helpers.h" | |
| 6 #include "blimp/net/grpc_engine_stream.h" | |
| 7 #include "content/public/browser/browser_thread.h" | |
| 8 | |
| 9 #include "base/bind_helpers.h" | |
| 10 | |
| 11 namespace blimp { | |
| 12 | |
| 13 GrpcEngineStream::GrpcEngineStream( | |
| 14 const std::string& ip_address, | |
|
Kevin M
2016/10/31 21:33:25
Pass around validated IPEndpoints internally.
| |
| 15 const net::CompletionCallback& connection_callback) | |
| 16 : GrpcStream(connection_callback), ip_address_(ip_address) { | |
| 17 grpc::ServerBuilder builder; | |
| 18 builder.AddListeningPort(ip_address_, grpc::InsecureServerCredentials()); | |
| 19 builder.RegisterService(&service_); | |
| 20 completion_queue_ = builder.AddCompletionQueue(); | |
| 21 server_ = builder.BuildAndStart(); | |
| 22 stream_ = base::MakeUnique< | |
| 23 grpc::ServerAsyncReaderWriter<HeliumWrapper, HeliumWrapper>>(&context_); | |
| 24 service_.RequestStream(&context_, stream_.get(), completion_queue_.get(), | |
| 25 completion_queue_.get(), | |
| 26 reinterpret_cast<void*>(GrpcConnectTag())); | |
| 27 StartCompletionQueueThread(completion_queue_.get()); | |
| 28 DVLOG(3) << "Starting engine stream @ " << ip_address; | |
| 29 } | |
| 30 | |
| 31 void GrpcEngineStream::SendMessage( | |
| 32 std::unique_ptr<HeliumWrapper> helium_message, | |
| 33 const HeliumMessageSentCb& sent_cb) { | |
| 34 DVLOG(3) << "Sending: " << helium_message->ByteSize() << " bytes"; | |
| 35 DCHECK(helium_message->serialized_helium_message().size() > 0); | |
| 36 stream_->Write(*helium_message.get(), | |
| 37 reinterpret_cast<void*>(GrpcWriteTag(sent_cb))); | |
| 38 } | |
| 39 | |
| 40 void GrpcEngineStream::ReceiveMessage( | |
| 41 const HeliumMessageReceivedCb& received_cb) { | |
| 42 DVLOG(3) << "Receiving (waiting for callback)"; | |
| 43 auto tag = GrpcReadTag(received_cb); | |
| 44 stream_->Read(tag->GetReceivedMsg(), reinterpret_cast<void*>(tag)); | |
| 45 } | |
| 46 | |
| 47 GrpcEngineStream::~GrpcEngineStream() { | |
| 48 LOG(INFO) << "Engine stream shutting down."; | |
| 49 server_->Shutdown(); | |
| 50 | |
| 51 completion_queue_->Shutdown(); | |
| 52 completion_queue_.release(); | |
| 53 } | |
| 54 } // namespace blimp | |
| OLD | NEW |