6
votes

I am trying to do something similar as below (i.e. sending data from client to server using streaming grpc call). Code reference is taken from grpc example given on the official site for explanation purposes:

Client Side Code:

ClientContext context;
context.AddMetadata("authorization", "abcd");
context.set_deadline(...);

std::unique_ptr<ClientWriter<RequestObjectClass>> writer(stub_->grpcCall(&context, &response));
writer->WaitForInitialMetadata();

// Setting request parameters
request.set...(...);
request.set...(...);
request.set...(...);
request.set...(...);

if (!writer->Write(request)) {
    Status status = writer->Finish();
    if (status.error_code() == UNAUTHENTICATED) {
        std::cout << "UNAUTHORIZED" << std::endl;
        break;
    }

    // Broken stream.
    throw Exception("Broken Stream");
}

writer->WritesDone();
Status status = writer->Finish();
if (status.ok()) {
    std::cout << "RPC succeeded." << std::endl;
} else {
    std::cout << "RPC failed." << std::endl;
}

Server Side Code:

std::multimap<grpc::string_ref, grpc::string_ref> metadata = context->client_metadata();
auto auth = metadata.find("authorization");

if (auth == metadata.end()) {
    return Status(StatusCode::UNAUTHENTICATED, "UNAUTHORIZED");
}

I get "Broken stream" exception because the Status had code "DEADLINE_EXCEEDED" and details as "Deadline Exceeded". My deadline timeout for ClientContext is system_clock::now() + 5 seconds. What am I doing wrong?

1

1 Answers

0
votes

I just removed this line "writer->WaitForInitialMetadata();" and it started working. I think this line makes client wait for some metadata from server. Not sure though.