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_client_stream.h" |
| 7 #include "content/public/browser/browser_thread.h" |
| 8 |
| 9 #include <grpc++/channel.h> |
| 10 #include <grpc++/client_context.h> |
| 11 #include <grpc++/create_channel.h> |
| 12 #include <grpc++/security/credentials.h> |
| 13 #include <grpc/grpc.h> |
| 14 |
| 15 namespace blimp { |
| 16 |
| 17 GrpcClientStream::GrpcClientStream( |
| 18 const std::string& ip_address, |
| 19 const net::CompletionCallback& connection_callback) |
| 20 : GrpcStream(connection_callback), |
| 21 ip_address_(ip_address), |
| 22 completion_queue_(base::MakeUnique<grpc::CompletionQueue>()) { |
| 23 stub_ = HeliumService::NewStub( |
| 24 grpc::CreateChannel(ip_address, grpc::InsecureChannelCredentials())); |
| 25 |
| 26 stream_ = |
| 27 stub_->AsyncStream(&context_, completion_queue_.get(), GrpcConnectTag()); |
| 28 StartCompletionQueueThread(completion_queue_.get()); |
| 29 DVLOG(3) << "Starting client stream @ " << ip_address; |
| 30 } |
| 31 |
| 32 void GrpcClientStream::SendMessage( |
| 33 std::unique_ptr<HeliumWrapper> helium_message, |
| 34 const Stream::SendMessageCallback& sent_cb) { |
| 35 DVLOG(3) << "Sending: " << helium_message->ByteSize() << " bytes"; |
| 36 DCHECK(helium_message->serialized_helium_message().size() > 0); |
| 37 stream_->Write(*helium_message.get(), |
| 38 reinterpret_cast<void*>(GrpcWriteTag(sent_cb))); |
| 39 } |
| 40 |
| 41 void GrpcClientStream::ReceiveMessage( |
| 42 const Stream::ReceiveMessageCallback& received_cb) { |
| 43 auto tag = GrpcReadTag(received_cb); |
| 44 DVLOG(3) << "Receiving (waiting for callback)"; |
| 45 stream_->Read(tag->GetReceivedMsg(), reinterpret_cast<void*>(tag)); |
| 46 } |
| 47 |
| 48 GrpcClientStream::~GrpcClientStream() { |
| 49 completion_queue_->Shutdown(); |
| 50 completion_queue_.release(); |
| 51 } |
| 52 |
| 53 } // namespace blimp |
OLD | NEW |