Dart client with possibility to cancel C++ server stream

53 Views Asked by At

I've got server written in C++ and Dart (Flutter) client. I've got service with continuos server stream. Trying to implement client, where it will possible to cancel and restart server stream with changed some request params. I've tried different server implementations and curently using one based on CallbackService and ServerWriteReactor. Dart implementation is based on ResponseStream and StreamSubscription functionality. The code i use to listen to stream data in dart:

call = _client.getTestParamsStream(TestID(id: _id));
subscription = call.listen((data) {}, onError: (error) {}, onDone: () {});

Trying to cancel server stream using:

call.cancel();

Server is implemented this way:

class TestService final : public CallbackService {
 private:
  int i = 0;

 public:
  ::grpc::ServerWriteReactor<TestParams> *GetTestParamsStream(::grpc::CallbackServerContext *context,
                                                                              const TestID *request) {
    class Lister : public ::grpc::ServerWriteReactor<TestParams> {
     public:
      Lister() {
        NextWrite();
      }
      void OnCancel() override {
      }
      void OnDone() override {
        delete this;
      }
      void OnWriteDone(bool ok) override {
      }
      void NextWrite() {
          ...
          StartWrite(&response);
        }
      };
    };
    return new Lister(request->id());
  }
};

Cancel from client side seems to work, OnCancel() callback is called on server side, but is not sychronized with writting functionality and i got crash on client side (server is trying to send data after cancelling). Is it appropriate approach to such issue? Maybe it should be done using another way? Cannot find any appropriate examples showing working solution.

0

There are 0 best solutions below