Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RpcTestClient.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <cstdint>
7 
8 #include <grpc/grpc.h>
9 #include <grpc++/grpc++.h>
10 
11 #include "inexor/io/Logging.hpp"
12 
13 namespace inexor { namespace rpc {
14 
16 template<typename DATA_TYPE, typename SERVICE_TYPE>
18 {
19 private:
20  DATA_TYPE make_test_msg()
21  {
22  DATA_TYPE t;
23  //t.set__masterport(25);
24  return t;
25  }
26 
27 
28 public:
29 
30  void Start(std::string port)
31  {
32  std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel("localhost:"+port, grpc::InsecureChannelCredentials()));
33  std::unique_ptr<typename SERVICE_TYPE::Stub> stub_(SERVICE_TYPE::NewStub(channel));
34  grpc::ClientContext context;
35  grpc::CompletionQueue cq;
36 
37  std::unique_ptr<grpc::ClientAsyncReaderWriter<DATA_TYPE, DATA_TYPE> > stream(stub_->AsyncSynchronize(&context, &cq, (void *)2));
38 
40  void *tag;
41  bool ok = false;
42  cq.Next(&tag, &ok);
43  // ASSERT(ok && tag == (void *)2)
45 
46  constexpr uintptr_t writetag = 4;
47  constexpr uintptr_t readtag = 3;
48 
49  DATA_TYPE receivedvalue;
50 
51  stream->Write(make_test_msg(), (void *)writetag);
52  stream->Read(&receivedvalue, (void *)readtag);
53 
54  for(int i = 0; i <1000; i++)
55  {
56  cq.Next(&tag, &ok);
57  if(!ok)
58  {
59  Log.sync->info("[Client] Not okay: for = {}", ((tag == (void *)writetag) ? "writer" : "reader"));
60  return;
61  }
62  if(tag == (void *)writetag)
63  {
64  Log.sync->info("[Client] Sent testmessage");
65  }
66  else if(tag == (void *)readtag)
67  {
68  Log.sync->info("[Client] Received. Index: {}", receivedvalue.key_case());
69  stream->Read(&receivedvalue, (void *)readtag);
70  }
71  // stream->WritesDone();
72  }
73  }
74 };
75 
76 } } // ns inexor::rpc
Temporary test client.
Definition: RpcTestClient.hpp:17
DATA_TYPE make_test_msg()
Definition: RpcTestClient.hpp:20
static Logger sync
Logger for synchronization messages.
Definition: Logging.hpp:96
Logging stuff including the ingame console logging functionality.
inexor::util::log_manager Log
Definition: Logging.cpp:241
void t(T x, const char *cmp)
Definition: utilTest.cpp:52
Legacy file system streams.
Definition: stream.hpp:22
char string[MAXSTRLEN]
cube-strings (char arrays of fixed size).
Definition: cube_types.hpp:18
void Start(std::string port)
Definition: RpcTestClient.hpp:30