grpc / grpc/grpc.io

Asynchronous-API tutorial

Open
#782 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
479
Forks
545
Avg merge
2d 7h
Merged PRs (30d)
5

Description

I have been trying for months to figure out how to implement a Async Bidirectional Streaming Server. The documentation and examples make no mention of it at all.

I am making best guesses by looking at various githubs that google leads me to and nothing is working. Can we please get an example of a async bidirectional streaming call, and how to use ServerAsyncReaderWriter?

I made a frankenstein that looks like the following, but I have no indication of what to do once GOT_EVENT case is hit and have no idea where to find it.

// We must not throw from a thread
try
{
// TODO - I don't know what any of the stuff in this chunk is or is doing!
// I think we make a call to the RPC method and wait for others to stream to it?
::grpc::ServerContext context;
void * ourOneAndOnlyTag = reinterpret_cast(1); ///< Identifies the call we are going to make. I assume we can only handle one client

::grpc::ServerAsyncReaderWriter
stream(&context);

m_service.RequestPortalMessageStream(&context, &stream, m_completionQueue.get(), m_completionQueue.get(), ourOneAndOnlyTag);

bool keepGoing = false;
do
{
void* tag = nullptr;
bool ok = false;
const std::chrono::time_point deadline(std::chrono::system_clock::now() +
std::chrono::seconds(1));

grpc::CompletionQueue::NextStatus nextStatus = m_completionQueue->AsyncNext(&tag, &ok, deadline);
switch(nextStatus)
{
case grpc::CompletionQueue::NextStatus::TIMEOUT:
{
keepGoing = true;
break;
}
case grpc::CompletionQueue::NextStatus::GOT_EVENT:
{
keepGoing = true;

// TODO I'm making guesses as to what to do here.
if(ok)
{
// No idea what to do here. This gets hit when I connect and when I send message, from a grpc client
// How do I know someone connected?
// How do I know a message was sent?
// How do I get the message?
// How do I send a message back?
// How do I hang up?
// Can more than one client call me, seeing how I had to make the RPC call myself?
}

break;
}
case grpc::CompletionQueue::NextStatus::SHUTDOWN:
{
keepGoing = false;
break;
}
}
} while(keepGoing);

// Completion queue was shutdown
}
catch(std::exception& e)
{
QString errorMessage(
QString("An std::exception was caught in the listening thread. Exception message: %1").arg(e.what()));
m_backPointer->onImplError(errorMessage);
}
catch(...)
{
QString errorMessage("An exception of unknown type, was caught in the listening thread.");
m_backPointer->onImplError(errorMessage);
}`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.