Using Sever streaming and blocking the stream RPC for continuous streaming

13 views
Skip to first unread message

Pragadeesh nagaraj

unread,
Sep 9, 2024, 9:23:03 AMSep 9
to grpc.io
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();

}

Reply all
Reply to author
Forward
0 new messages