Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to build helloworld server code #55

Closed
bharat76 opened this issue Dec 1, 2022 · 6 comments
Closed

Is there a way to build helloworld server code #55

bharat76 opened this issue Dec 1, 2022 · 6 comments

Comments

@bharat76
Copy link

bharat76 commented Dec 1, 2022

I tried extensively with following code. Get compilation error as in this post.
any help is appreciated. Thanks you @Tradias

Code

    15  #include "zprobe.grpc.pb.h"
    16
    17  #include <agrpc/asio_grpc.hpp>
    18  #include <boost/asio/co_spawn.hpp>
    19  #include <boost/asio/detached.hpp>
    20  #include <boost/asio/signal_set.hpp>
    21  #include <grpcpp/server.h>
    22  #include <grpcpp/server_builder.h>
    23
    24  #include <optional>
    25  #include <thread>
    26  namespace asio = boost::asio;
    27
    28
    29  // begin-snippet: server-side-helloworld
    30  // ---------------------------------------------------
    31  // Server-side hello world which handles exactly one request from the client before shutting down.
    32  // ---------------------------------------------------
    33  // end-snippet
    34  int main(int argc, const char** argv)
    35  {
    36      const auto port = argc >= 2 ? argv[1] : "50051";
    37      const auto host = std::string("0.0.0.0:") + port;
    38
    39      std::unique_ptr<grpc::Server> server;
    40
    41      grpc::ServerBuilder builder;
    42      agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
    43      builder.AddListeningPort(host, grpc::InsecureServerCredentials());
    44      zprobe::ProbeService::AsyncService service;
    45      builder.RegisterService(&service);
    46      server = builder.BuildAndStart();
    47
    48      asio::co_spawn(
    49          grpc_context,
    50          [&]() -> asio::awaitable<void>
    51          {
    52              grpc::ServerContext server_context;
    53              helloworld::HelloRequest request;
    54              grpc::ServerAsyncResponseWriter<helloworld::HelloReply> writer{&server_context};
    55              co_await agrpc::request(&helloworld::Greeter::AsyncService::RequestSayHello, service, server_context,
    56                                      request, writer, asio::use_awaitable);
    57              helloworld::HelloReply response;
    58              response.set_message("Hello " + request.name());
    59              co_await agrpc::finish(writer, response, grpc::Status::OK, asio::use_awaitable);
    60          },
    61          asio::detached);
    62
    63      grpc_context.run();
    64
    65      server->Shutdown();
    66  }```

**CMAKE -- Success**
 cmake  .. "-DCMAKE_TOOLCHAIN_FILE=~/tools/vcpkg/scripts/buildsystems/vcpkg.cmake"  "-DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR"

**CMakeLists.txt**
```target_link_libraries(zprobe
  PUBLIC zprobe_grpc_proto
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF}
  asio-grpc::asio-grpc-standalone-asio)

ERROR on make

[ 83%] Building CXX object CMakeFiles/zprobe.dir/grpc_asio_server.cpp.o
/home/bbhushan/work/zprobe/grpc_asio_server.cpp:26:29: error: ‘namespace asio = boost::boost::asio;’ conflicts with a previous declaration
   26 | namespace asio = boost::asio;
      |                             ^
In file included from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/asio/execution/allocator.hpp:19,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/asio/execution.hpp:18,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/asio/any_io_executor.hpp:22,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/detail/asio_forward.hpp:24,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/detail/default_completion_token.hpp:18,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/default_completion_token.hpp:19,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/alarm.hpp:18,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/asio_grpc.hpp:33,
                 from /home/bbhushan/work/zprobe/grpc_asio_server.cpp:17:
/home/bbhushan/tools/vcpkg/installed/x64-linux/include/asio/detail/type_traits.hpp:51:11: note: previous declaration ‘namespace asio { }’
   51 | namespace asio {
      |           ^~~~
/home/bbhushan/work/zprobe/grpc_asio_server.cpp: In function ‘int main(int, const char**)’:
/home/bbhushan/work/zprobe/grpc_asio_server.cpp:48:11: error: ‘co_spawn’ is not a member of ‘asio’
   48 |     asio::co_spawn(
      |           ^~~~~~~~
/home/bbhushan/work/zprobe/grpc_asio_server.cpp:50:24: error: ‘awaitable’ in namespace ‘asio’ does not name a template type
   50 |         [&]() -> asio::awaitable<void>
      |                        ^~~~~~~~~
/home/bbhushan/work/zprobe/grpc_asio_server.cpp:50:33: error: expected ‘{’ before ‘<’ token
   50 |         [&]() -> asio::awaitable<void>
      |                                 ^
/home/bbhushan/work/zprobe/grpc_asio_server.cpp:50:34: error: expected primary-expression before ‘void’
   50 |         [&]() -> asio::awaitable<void>
      |                                  ^~~~
/home/bbhushan/work/zprobe/grpc_asio_server.cpp:61:15: error: ‘detached’ is not a member of ‘asio’; did you mean ‘boost::asio::detached’?
   61 |         asio::detached);
      |               ^~~~~~~~
In file included from /home/bbhushan/work/zprobe/grpc_asio_server.cpp:19:
/home/bbhushan/tools/vcpkg/installed/x64-linux/include/boost/asio/detached.hpp:103:22: note: ‘boost::asio::detached’ declared here
  103 | constexpr detached_t detached;
      |                      ^~~~~~~~
make[2]: *** [CMakeFiles/zprobe.dir/build.make:76: CMakeFiles/zprobe.dir/grpc_asio_server.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/zprobe.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
@Tradias
Copy link
Owner

Tradias commented Dec 1, 2022

Your example uses Boost.Asio, to make it use standalone Asio instead you will have to make some changes:

-    18  #include <boost/asio/co_spawn.hpp>
-    19  #include <boost/asio/detached.hpp>
-    20  #include <boost/asio/signal_set.hpp>
+    18  #include <asio/co_spawn.hpp>
+    19  #include <asio/detached.hpp>
-    26  namespace asio = boost::asio;

And of course the request that you actually want to handle. This comes from your zprobe.proto. In other words, these lines should be adjusted:

                  helloworld::HelloRequest request;
                  grpc::ServerAsyncResponseWriter<helloworld::HelloReply> writer{&server_context};
                  co_await agrpc::request(&helloworld::Greeter::AsyncService::RequestSayHello, service, server_context,
                                          request, writer, asio::use_awaitable);
                  helloworld::HelloReply response;
                  response.set_message("Hello " + request.name());

You should also be able to simplify your CMake file:

target_link_libraries(zprobe
  PUBLIC zprobe_grpc_proto
  asio-grpc::asio-grpc-standalone-asio)

@bharat76
Copy link
Author

bharat76 commented Dec 2, 2022

The following is the error after I follow above.
(It is lack of my understanding. You may please help me with this.)

I added following to CMakeLists.txt

add_definitions(-DASIO_STANDALONE)
add_definitions(-DASIO_HAS_CO_AWAIT)
add_definitions(-DASIO_SEPARATE_COMPILATION)
add_definitions(-DASIO_HAS_STD_COROUTINE)

Then I get the following error.

[ 83%] Building CXX object CMakeFiles/zprobe.dir/grpc_asio_server.cpp.o
In file included from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/asio/co_spawn.hpp:22,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/detail/asio_forward.hpp:45,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/detail/default_completion_token.hpp:18,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/default_completion_token.hpp:19,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/alarm.hpp:18,
                 from /home/bbhushan/tools/vcpkg/installed/x64-linux/include/agrpc/asio_grpc.hpp:33,
                 from /home/bbhushan/work/zprobe/grpc_asio_server.cpp:17:
/home/bbhushan/tools/vcpkg/installed/x64-linux/include/asio/awaitable.hpp:23:11: fatal error: coroutine: No such file or directory
   23 | # include <coroutine>
      |

@Tradias
Copy link
Owner

Tradias commented Dec 2, 2022

I have created a minimal version of the helloworld example with standalone Asio and vcpkg here: https://github.com/Tradias/example-vcpkg-grpc/tree/asio-grpc-55. I hope that helps.

@bharat76
Copy link
Author

bharat76 commented Dec 3, 2022

@Tradias I think the example repo does not use ASIO?

@Tradias
Copy link
Owner

Tradias commented Dec 3, 2022

It uses standalone ASIO, just like in your code :), see https://github.com/Tradias/example-vcpkg-grpc/blob/asio-grpc-55/src/main.cpp

@bharat76
Copy link
Author

bharat76 commented Dec 4, 2022

Thanks @Tradias. It worked.

@bharat76 bharat76 closed this as completed Dec 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants