-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.cc
100 lines (83 loc) · 3.26 KB
/
server.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <optional>
#include <string>
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/grpc_security_constants.h>
#include <grpc/impl/slice_type.h>
#include <grpc/status.h>
namespace lisp {
namespace lisp_grpc {
extern "C" {
grpc_call_details* create_new_grpc_call_details() {
grpc_call_details* details = new grpc_call_details();
grpc_call_details_init(details);
return details;
}
char* grpc_call_method(grpc_call_details* call_details) {
return grpc_slice_to_c_string(call_details->method);
}
void delete_grpc_call_details(grpc_call_details* call_details) {
grpc_call_details_destroy(call_details);
delete call_details;
}
grpc_call* lisp_grpc_server_request_call(
grpc_server* server, grpc_call_details* details,
grpc_metadata_array* request_metadata,
grpc_completion_queue* cq_bound_to_call,
grpc_completion_queue* cq_for_notification,
void* tag) {
grpc_call* internal_call = nullptr;
grpc_call_error error = grpc_server_request_call(server,
&internal_call,
details,
request_metadata,
cq_bound_to_call,
cq_for_notification,
tag);
if (error != GRPC_CALL_OK) {
return nullptr;
}
grpc_event event = grpc_completion_queue_pluck(cq_bound_to_call,
tag,
gpr_inf_future(GPR_CLOCK_MONOTONIC),
nullptr);
if (event.success == 0)
return nullptr;
return internal_call;
}
grpc_server* grpc_run_server(grpc_server* server,
grpc_server_credentials* server_creds){
// Start a server and release a server credentials object
grpc_server_start(server);
grpc_server_credentials_release(server_creds);
return server;
}
void* register_method(grpc_server* server, const char* method_name, const char* server_address){
return grpc_server_register_method(server,
const_cast<char*>(("/"+std::string(method_name)).c_str()),
server_address,
{}, 0);
}
grpc_server* start_server(grpc_completion_queue* cq,
grpc_server_credentials* server_creds,
const char* server_address) {
// create the server
grpc_server* server = grpc_server_create(nullptr, nullptr);
grpc_server_register_completion_queue(server, cq, nullptr);
grpc_server_add_http2_port(server, server_address, server_creds);
return server;
}
void shutdown_server(grpc_server* server, grpc_completion_queue* cq, void* tag) {
if (server == nullptr) return;
grpc_server_shutdown_and_notify(server, cq, tag);
grpc_completion_queue_pluck(cq,
tag,
gpr_inf_future(GPR_CLOCK_MONOTONIC),
nullptr);
grpc_server_destroy(server);
}
// Creates a grpc_call* given a 'channel', which manages the
}
} // namespace lisp_grpc
} // namespace lisp