Single async client process talking to multiple async servers in c++

23 views
Skip to first unread message

siddhesh...@gmail.com

unread,
Oct 23, 2018, 4:58:51 AM10/23/18
to grpc.io
Hi,

Is there an example showing how a single async client process can talk to multiple async servers in c++.

it looks like one has to create separate channel and hence stub for each server.

Also a second thread needs to be created so that it can asynchronously process the responses coming from second server.

    // Instantiate the client. It requires a channel, out of which the actual RPCs
    // are created. This channel models a connection to an endpoint (in this case,
    // localhost at port 50051). We indicate that the channel isn't authenticated
    // (use of InsecureChannelCredentials()).
    GreeterClient greeter(grpc::CreateChannel(
            "localhost:50051", grpc::InsecureChannelCredentials()));

    GreeterClient greeter1(grpc::CreateChannel(
            "localhost:60051", grpc::InsecureChannelCredentials()));                                 ================> channel for the second server.

    // Spawn reader thread that loops indefinitely
    std::thread thread_ = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter);

    std::thread thread_ = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter1); ================> process messages coming for the second server.

Is this is right way OR same can be done using single thread to handle messages coming from two server.
In other words, can same channel/stub & completion queue be used for two different servers and 
is there a way to demux the messages coming from different servers on single channel ?

Thanks,
Siddhesh.


Reply all
Reply to author
Forward
0 new messages