You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. // Copyright (c) 2021 Ximalaya Speech Team (Xiang Lyu)
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef GRPC_GRPC_CLIENT_H_
  15. #define GRPC_GRPC_CLIENT_H_
  16. #include <grpc/grpc.h>
  17. #include <grpcpp/channel.h>
  18. #include <grpcpp/client_context.h>
  19. #include <grpcpp/create_channel.h>
  20. #include <iostream>
  21. #include <memory>
  22. #include <string>
  23. #include <thread>
  24. #include "grpc/wenet.grpc.pb.h"
  25. #include "utils/wn_utils.h"S
  26. namespace wenet {
  27. using grpc::Channel;
  28. using grpc::ClientContext;
  29. using grpc::ClientReaderWriter;
  30. using wenet::ASR;
  31. using wenet::Request;
  32. using wenet::Response;
  33. class GrpcClient {
  34. public:
  35. GrpcClient(const std::string& host, int port, int nbest,
  36. bool continuous_decoding);
  37. void SendBinaryData(const void* data, size_t size);
  38. void ReadLoopFunc();
  39. void Join();
  40. bool done() const { return done_; }
  41. private:
  42. void Connect();
  43. std::string host_;
  44. int port_;
  45. std::shared_ptr<Channel> channel_{nullptr};
  46. std::unique_ptr<ASR::Stub> stub_{nullptr};
  47. std::shared_ptr<ClientContext> context_{nullptr};
  48. std::unique_ptr<ClientReaderWriter<Request, Response>> stream_{nullptr};
  49. std::shared_ptr<Request> request_{nullptr};
  50. std::shared_ptr<Response> response_{nullptr};
  51. int nbest_ = 1;
  52. bool continuous_decoding_ = false;
  53. bool done_ = false;
  54. std::unique_ptr<std::thread> t_{nullptr};
  55. WENET_DISALLOW_COPY_AND_ASSIGN(GrpcClient);
  56. };
  57. } // namespace wenet
  58. #endif // GRPC_GRPC_CLIENT_H_