I am using grpc in server streaming mode and i will make a request from
client using a RPC (startStreaming). Then i am blocking the request and
using the writer(stroring it) from the request i am keep on sending, using
the stored writer. My requirement is server should keep on streaming the
message when the stream starts. It is working fine for me, *but is it
advisable*. My sample code is
//Function where i blocks the stream call
::grpc::Status ProtocolService::subscribe(::grpc::ServerContext* context,
const ::grpcservices::mcuinterface::SubscribeRequest* request, ::grpc::
ServerWriter< ::grpcservices::mcuinterface::MCUStreamingPackets>* writer)
{
std::shared_ptr<IDataWriter> l_writer = std::make_shared<GrpcDataWriter
>(writer);
uint16_t clientId = request->clientid();
m_dataRelayService->setDataWriter(l_writer,clientId);
//Message received successfully
//TODO Log the data
std::cout << "Client Subscribed with ID: " << clientId << std::endl;
//Every client which calls the subscribe function will set their respective
data writer and
//waits here for the channel not to get closed and continues the streaming
m_continueStreaming[clientId] = true;
while(m_continueStreaming.at(clientId))
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));//50 ms
}
//The client is uncubscribed so remove the client from the map
m_continueStreaming.erase(clientId);
return ::grpc::Status::OK;
}
//function where i sends the data using writer
for(const auto& writer: m_writers)
{
std::shared_ptr<GrpcDataWriter> l_writer = std::dynamic_pointer_cast<
GrpcDataWriter>(writer.second);
l_writer->setData(packet);
l_writer->writeData();
}
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/70b4add3-ca64-41e0-a385-e40fa7809e1en%40googlegroups.com.