server.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <grpc/grpc.h>
  24. #include <grpc/grpc_security.h>
  25. #ifndef _WIN32
  26. /* This is for _exit() below, which is temporary. */
  27. #include <unistd.h>
  28. #endif
  29. #include <grpc/support/alloc.h>
  30. #include <grpc/support/log.h>
  31. #include <grpc/support/time.h>
  32. #include "src/core/lib/gprpp/host_port.h"
  33. #include "src/core/lib/profiling/timers.h"
  34. #include "test/core/end2end/data/ssl_test_data.h"
  35. #include "test/core/util/cmdline.h"
  36. #include "test/core/util/grpc_profiler.h"
  37. #include "test/core/util/port.h"
  38. #include "test/core/util/test_config.h"
  39. static grpc_completion_queue* cq;
  40. static grpc_server* server;
  41. static grpc_call* call;
  42. static grpc_call_details call_details;
  43. static grpc_metadata_array request_metadata_recv;
  44. static grpc_metadata_array initial_metadata_send;
  45. static grpc_byte_buffer* payload_buffer = nullptr;
  46. /* Used to drain the terminal read in unary calls. */
  47. static grpc_byte_buffer* terminal_buffer = nullptr;
  48. static grpc_op read_op;
  49. static grpc_op metadata_send_op;
  50. static grpc_op write_op;
  51. static grpc_op status_op[2];
  52. static int was_cancelled = 2;
  53. static grpc_op unary_ops[6];
  54. static int got_sigint = 0;
  55. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  56. typedef enum {
  57. FLING_SERVER_NEW_REQUEST = 1,
  58. FLING_SERVER_READ_FOR_UNARY,
  59. FLING_SERVER_BATCH_OPS_FOR_UNARY,
  60. FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING,
  61. FLING_SERVER_READ_FOR_STREAMING,
  62. FLING_SERVER_WRITE_FOR_STREAMING,
  63. FLING_SERVER_SEND_STATUS_FOR_STREAMING
  64. } fling_server_tags;
  65. typedef struct {
  66. gpr_refcount pending_ops;
  67. uint32_t flags;
  68. } call_state;
  69. static void request_call(void) {
  70. grpc_metadata_array_init(&request_metadata_recv);
  71. GPR_ASSERT(GRPC_CALL_OK ==
  72. grpc_server_request_call(server, &call, &call_details,
  73. &request_metadata_recv, cq, cq,
  74. tag(FLING_SERVER_NEW_REQUEST)));
  75. }
  76. static void handle_unary_method(void) {
  77. grpc_op* op;
  78. grpc_call_error error;
  79. grpc_metadata_array_init(&initial_metadata_send);
  80. op = unary_ops;
  81. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  82. op->data.send_initial_metadata.count = 0;
  83. op++;
  84. op->op = GRPC_OP_RECV_MESSAGE;
  85. op->data.recv_message.recv_message = &terminal_buffer;
  86. op++;
  87. op->op = GRPC_OP_SEND_MESSAGE;
  88. if (payload_buffer == nullptr) {
  89. gpr_log(GPR_INFO, "NULL payload buffer !!!");
  90. }
  91. op->data.send_message.send_message = payload_buffer;
  92. op++;
  93. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  94. op->data.send_status_from_server.status = GRPC_STATUS_OK;
  95. op->data.send_status_from_server.trailing_metadata_count = 0;
  96. op->data.send_status_from_server.status_details = nullptr;
  97. op++;
  98. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  99. op->data.recv_close_on_server.cancelled = &was_cancelled;
  100. op++;
  101. error = grpc_call_start_batch(call, unary_ops,
  102. static_cast<size_t>(op - unary_ops),
  103. tag(FLING_SERVER_BATCH_OPS_FOR_UNARY), nullptr);
  104. GPR_ASSERT(GRPC_CALL_OK == error);
  105. }
  106. static void send_initial_metadata(void) {
  107. grpc_call_error error;
  108. void* tagarg = tag(FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING);
  109. grpc_metadata_array_init(&initial_metadata_send);
  110. metadata_send_op.op = GRPC_OP_SEND_INITIAL_METADATA;
  111. metadata_send_op.data.send_initial_metadata.count = 0;
  112. error = grpc_call_start_batch(call, &metadata_send_op, 1, tagarg, nullptr);
  113. GPR_ASSERT(GRPC_CALL_OK == error);
  114. }
  115. static void start_read_op(int t) {
  116. grpc_call_error error;
  117. /* Starting read at server */
  118. read_op.op = GRPC_OP_RECV_MESSAGE;
  119. read_op.data.recv_message.recv_message = &payload_buffer;
  120. error = grpc_call_start_batch(call, &read_op, 1, tag(t), nullptr);
  121. GPR_ASSERT(GRPC_CALL_OK == error);
  122. }
  123. static void start_write_op(void) {
  124. grpc_call_error error;
  125. void* tagarg = tag(FLING_SERVER_WRITE_FOR_STREAMING);
  126. /* Starting write at server */
  127. write_op.op = GRPC_OP_SEND_MESSAGE;
  128. if (payload_buffer == nullptr) {
  129. gpr_log(GPR_INFO, "NULL payload buffer !!!");
  130. }
  131. write_op.data.send_message.send_message = payload_buffer;
  132. error = grpc_call_start_batch(call, &write_op, 1, tagarg, nullptr);
  133. GPR_ASSERT(GRPC_CALL_OK == error);
  134. }
  135. static void start_send_status(void) {
  136. grpc_call_error error;
  137. void* tagarg = tag(FLING_SERVER_SEND_STATUS_FOR_STREAMING);
  138. status_op[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  139. status_op[0].data.send_status_from_server.status = GRPC_STATUS_OK;
  140. status_op[0].data.send_status_from_server.trailing_metadata_count = 0;
  141. status_op[0].data.send_status_from_server.status_details = nullptr;
  142. status_op[1].op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  143. status_op[1].data.recv_close_on_server.cancelled = &was_cancelled;
  144. error = grpc_call_start_batch(call, status_op, 2, tagarg, nullptr);
  145. GPR_ASSERT(GRPC_CALL_OK == error);
  146. }
  147. /* We have some sort of deadlock, so let's not exit gracefully for now.
  148. When that is resolved, please remove the #include <unistd.h> above. */
  149. static void sigint_handler(int /*x*/) { _exit(0); }
  150. int main(int argc, char** argv) {
  151. grpc_event ev;
  152. call_state* s;
  153. std::string addr_buf;
  154. gpr_cmdline* cl;
  155. grpc_completion_queue* shutdown_cq;
  156. int shutdown_started = 0;
  157. int shutdown_finished = 0;
  158. int secure = 0;
  159. const char* addr = nullptr;
  160. char* fake_argv[1];
  161. gpr_timers_set_log_filename("latency_trace.fling_server.txt");
  162. GPR_ASSERT(argc >= 1);
  163. fake_argv[0] = argv[0];
  164. grpc_test_init(1, fake_argv);
  165. grpc_init();
  166. srand(static_cast<unsigned>(clock()));
  167. cl = gpr_cmdline_create("fling server");
  168. gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
  169. gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
  170. gpr_cmdline_parse(cl, argc, argv);
  171. gpr_cmdline_destroy(cl);
  172. if (addr == nullptr) {
  173. addr_buf = grpc_core::JoinHostPort("::", grpc_pick_unused_port_or_die());
  174. addr = addr_buf.c_str();
  175. }
  176. gpr_log(GPR_INFO, "creating server on: %s", addr);
  177. cq = grpc_completion_queue_create_for_next(nullptr);
  178. grpc_server_credentials* creds;
  179. if (secure) {
  180. grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
  181. test_server1_cert};
  182. creds = grpc_ssl_server_credentials_create(nullptr, &pem_key_cert_pair, 1,
  183. 0, nullptr);
  184. } else {
  185. creds = grpc_insecure_server_credentials_create();
  186. }
  187. server = grpc_server_create(nullptr, nullptr);
  188. GPR_ASSERT(grpc_server_add_http2_port(server, addr, creds));
  189. grpc_server_credentials_release(creds);
  190. grpc_server_register_completion_queue(server, cq, nullptr);
  191. grpc_server_start(server);
  192. addr = nullptr;
  193. addr_buf.clear();
  194. grpc_call_details_init(&call_details);
  195. request_call();
  196. grpc_profiler_start("server.prof");
  197. signal(SIGINT, sigint_handler);
  198. while (!shutdown_finished) {
  199. if (got_sigint && !shutdown_started) {
  200. gpr_log(GPR_INFO, "Shutting down due to SIGINT");
  201. shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  202. grpc_server_shutdown_and_notify(server, shutdown_cq, tag(1000));
  203. GPR_ASSERT(grpc_completion_queue_pluck(
  204. shutdown_cq, tag(1000),
  205. grpc_timeout_seconds_to_deadline(5), nullptr)
  206. .type == GRPC_OP_COMPLETE);
  207. grpc_completion_queue_destroy(shutdown_cq);
  208. grpc_completion_queue_shutdown(cq);
  209. shutdown_started = 1;
  210. }
  211. ev = grpc_completion_queue_next(
  212. cq,
  213. gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
  214. gpr_time_from_micros(1000000, GPR_TIMESPAN)),
  215. nullptr);
  216. s = static_cast<call_state*>(ev.tag);
  217. switch (ev.type) {
  218. case GRPC_OP_COMPLETE:
  219. switch (reinterpret_cast<intptr_t>(s)) {
  220. case FLING_SERVER_NEW_REQUEST:
  221. if (call != nullptr) {
  222. if (0 == grpc_slice_str_cmp(call_details.method,
  223. "/Reflector/reflectStream")) {
  224. /* Received streaming call. Send metadata here. */
  225. start_read_op(FLING_SERVER_READ_FOR_STREAMING);
  226. send_initial_metadata();
  227. } else {
  228. /* Received unary call. Can do all ops in one batch. */
  229. start_read_op(FLING_SERVER_READ_FOR_UNARY);
  230. }
  231. } else {
  232. GPR_ASSERT(shutdown_started);
  233. }
  234. /* request_call();
  235. */
  236. break;
  237. case FLING_SERVER_READ_FOR_STREAMING:
  238. if (payload_buffer != nullptr) {
  239. /* Received payload from client. */
  240. start_write_op();
  241. } else {
  242. /* Received end of stream from client. */
  243. start_send_status();
  244. }
  245. break;
  246. case FLING_SERVER_WRITE_FOR_STREAMING:
  247. /* Write completed at server */
  248. grpc_byte_buffer_destroy(payload_buffer);
  249. payload_buffer = nullptr;
  250. start_read_op(FLING_SERVER_READ_FOR_STREAMING);
  251. break;
  252. case FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING:
  253. /* Metadata send completed at server */
  254. break;
  255. case FLING_SERVER_SEND_STATUS_FOR_STREAMING:
  256. /* Send status and close completed at server */
  257. grpc_call_unref(call);
  258. if (!shutdown_started) request_call();
  259. break;
  260. case FLING_SERVER_READ_FOR_UNARY:
  261. /* Finished payload read for unary. Start all reamaining
  262. * unary ops in a batch.
  263. */
  264. handle_unary_method();
  265. break;
  266. case FLING_SERVER_BATCH_OPS_FOR_UNARY:
  267. /* Finished unary call. */
  268. grpc_byte_buffer_destroy(payload_buffer);
  269. payload_buffer = nullptr;
  270. grpc_call_unref(call);
  271. if (!shutdown_started) request_call();
  272. break;
  273. }
  274. break;
  275. case GRPC_QUEUE_SHUTDOWN:
  276. GPR_ASSERT(shutdown_started);
  277. shutdown_finished = 1;
  278. break;
  279. case GRPC_QUEUE_TIMEOUT:
  280. break;
  281. }
  282. }
  283. grpc_profiler_stop();
  284. grpc_call_details_destroy(&call_details);
  285. grpc_server_destroy(server);
  286. grpc_completion_queue_destroy(cq);
  287. grpc_shutdown();
  288. return 0;
  289. }