fix: resolve inconsistency between doc and code (#1021)

* fix: resolve inconsistent between doc and code

* chore: fix typo

* chore: refine doc
This commit is contained in:
Jiarui Li 2022-10-19 14:21:37 -04:00 committed by GitHub
parent c19bd55eeb
commit f99dc0fe34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 4 deletions

View File

@ -284,16 +284,16 @@ Finally, let's look at our bidirectional streaming RPC `RouteChat()`.
```cpp ```cpp
Status RouteChat(ServerContext* context, Status RouteChat(ServerContext* context,
ServerReaderWriter<RouteNote, RouteNote>* stream) override { ServerReaderWriter<RouteNote, RouteNote>* stream) override {
std::vector<RouteNote> received_notes;
RouteNote note; RouteNote note;
while (stream->Read(&note)) { while (stream->Read(&note)) {
for (const RouteNote& n : received_notes) { std::unique_lock<std::mutex> lock(mu_);
for (const RouteNote& n : received_notes_) {
if (n.location().latitude() == note.location().latitude() && if (n.location().latitude() == note.location().latitude() &&
n.location().longitude() == note.location().longitude()) { n.location().longitude() == note.location().longitude()) {
stream->Write(n); stream->Write(n);
} }
} }
received_notes.push_back(note); received_notes_.push_back(note);
} }
return Status::OK; return Status::OK;
@ -307,6 +307,9 @@ get the other's messages in the order they were written, both the client and
server can read and write in any order — the streams operate completely server can read and write in any order — the streams operate completely
independently. independently.
Note that since `received_notes_` is an instance variable and can be accessed by
multiple threads, we use a mutex lock here to guarantee exclusive access.
#### Starting the server #### Starting the server
Once we've implemented all our methods, we also need to start up a gRPC server Once we've implemented all our methods, we also need to start up a gRPC server