mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
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.
325 lines
10 KiB
325 lines
10 KiB
10 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2015 gRPC authors.
|
||
10 years ago
|
*
|
||
8 years ago
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
10 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
10 years ago
|
*
|
||
8 years ago
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
10 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
#include <grpc/grpc.h>
|
||
10 years ago
|
#include <grpc/grpc_security.h>
|
||
10 years ago
|
|
||
|
#include <signal.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <time.h>
|
||
10 years ago
|
#ifndef _WIN32
|
||
|
/* This is for _exit() below, which is temporary. */
|
||
10 years ago
|
#include <unistd.h>
|
||
10 years ago
|
#endif
|
||
10 years ago
|
|
||
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/cmdline.h>
|
||
|
#include <grpc/support/host_port.h>
|
||
|
#include <grpc/support/log.h>
|
||
|
#include <grpc/support/time.h>
|
||
9 years ago
|
#include "src/core/lib/profiling/timers.h"
|
||
10 years ago
|
#include "test/core/end2end/data/ssl_test_data.h"
|
||
9 years ago
|
#include "test/core/util/grpc_profiler.h"
|
||
9 years ago
|
#include "test/core/util/port.h"
|
||
9 years ago
|
#include "test/core/util/test_config.h"
|
||
10 years ago
|
|
||
|
static grpc_completion_queue *cq;
|
||
|
static grpc_server *server;
|
||
10 years ago
|
static grpc_call *call;
|
||
|
static grpc_call_details call_details;
|
||
|
static grpc_metadata_array request_metadata_recv;
|
||
|
static grpc_metadata_array initial_metadata_send;
|
||
|
static grpc_byte_buffer *payload_buffer = NULL;
|
||
10 years ago
|
/* Used to drain the terminal read in unary calls. */
|
||
|
static grpc_byte_buffer *terminal_buffer = NULL;
|
||
10 years ago
|
|
||
|
static grpc_op read_op;
|
||
|
static grpc_op metadata_send_op;
|
||
|
static grpc_op write_op;
|
||
|
static grpc_op status_op[2];
|
||
|
static int was_cancelled = 2;
|
||
10 years ago
|
static grpc_op unary_ops[6];
|
||
10 years ago
|
static int got_sigint = 0;
|
||
10 years ago
|
|
||
9 years ago
|
static void *tag(intptr_t t) { return (void *)t; }
|
||
10 years ago
|
|
||
9 years ago
|
typedef enum {
|
||
10 years ago
|
FLING_SERVER_NEW_REQUEST = 1,
|
||
|
FLING_SERVER_READ_FOR_UNARY,
|
||
|
FLING_SERVER_BATCH_OPS_FOR_UNARY,
|
||
|
FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING,
|
||
|
FLING_SERVER_READ_FOR_STREAMING,
|
||
|
FLING_SERVER_WRITE_FOR_STREAMING,
|
||
|
FLING_SERVER_SEND_STATUS_FOR_STREAMING
|
||
|
} fling_server_tags;
|
||
|
|
||
9 years ago
|
typedef struct {
|
||
10 years ago
|
gpr_refcount pending_ops;
|
||
9 years ago
|
uint32_t flags;
|
||
10 years ago
|
} call_state;
|
||
|
|
||
9 years ago
|
static void request_call(void) {
|
||
|
grpc_metadata_array_init(&request_metadata_recv);
|
||
8 years ago
|
GPR_ASSERT(GRPC_CALL_OK ==
|
||
|
grpc_server_request_call(server, &call, &call_details,
|
||
|
&request_metadata_recv, cq, cq,
|
||
|
tag(FLING_SERVER_NEW_REQUEST)));
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void handle_unary_method(void) {
|
||
10 years ago
|
grpc_op *op;
|
||
9 years ago
|
grpc_call_error error;
|
||
10 years ago
|
|
||
9 years ago
|
grpc_metadata_array_init(&initial_metadata_send);
|
||
10 years ago
|
|
||
|
op = unary_ops;
|
||
|
op->op = GRPC_OP_SEND_INITIAL_METADATA;
|
||
|
op->data.send_initial_metadata.count = 0;
|
||
|
op++;
|
||
|
op->op = GRPC_OP_RECV_MESSAGE;
|
||
8 years ago
|
op->data.recv_message.recv_message = &terminal_buffer;
|
||
10 years ago
|
op++;
|
||
|
op->op = GRPC_OP_SEND_MESSAGE;
|
||
9 years ago
|
if (payload_buffer == NULL) {
|
||
|
gpr_log(GPR_INFO, "NULL payload buffer !!!");
|
||
|
}
|
||
8 years ago
|
op->data.send_message.send_message = payload_buffer;
|
||
10 years ago
|
op++;
|
||
|
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
|
||
|
op->data.send_status_from_server.status = GRPC_STATUS_OK;
|
||
|
op->data.send_status_from_server.trailing_metadata_count = 0;
|
||
8 years ago
|
op->data.send_status_from_server.status_details = NULL;
|
||
10 years ago
|
op++;
|
||
|
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
|
||
|
op->data.recv_close_on_server.cancelled = &was_cancelled;
|
||
|
op++;
|
||
10 years ago
|
|
||
9 years ago
|
error = grpc_call_start_batch(call, unary_ops, (size_t)(op - unary_ops),
|
||
|
tag(FLING_SERVER_BATCH_OPS_FOR_UNARY), NULL);
|
||
|
GPR_ASSERT(GRPC_CALL_OK == error);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void send_initial_metadata(void) {
|
||
9 years ago
|
grpc_call_error error;
|
||
9 years ago
|
void *tagarg = tag(FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING);
|
||
|
grpc_metadata_array_init(&initial_metadata_send);
|
||
10 years ago
|
metadata_send_op.op = GRPC_OP_SEND_INITIAL_METADATA;
|
||
|
metadata_send_op.data.send_initial_metadata.count = 0;
|
||
9 years ago
|
error = grpc_call_start_batch(call, &metadata_send_op, 1, tagarg, NULL);
|
||
9 years ago
|
|
||
9 years ago
|
GPR_ASSERT(GRPC_CALL_OK == error);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void start_read_op(int t) {
|
||
9 years ago
|
grpc_call_error error;
|
||
10 years ago
|
/* Starting read at server */
|
||
|
read_op.op = GRPC_OP_RECV_MESSAGE;
|
||
8 years ago
|
read_op.data.recv_message.recv_message = &payload_buffer;
|
||
9 years ago
|
error = grpc_call_start_batch(call, &read_op, 1, tag(t), NULL);
|
||
|
GPR_ASSERT(GRPC_CALL_OK == error);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void start_write_op(void) {
|
||
9 years ago
|
grpc_call_error error;
|
||
9 years ago
|
void *tagarg = tag(FLING_SERVER_WRITE_FOR_STREAMING);
|
||
10 years ago
|
/* Starting write at server */
|
||
|
write_op.op = GRPC_OP_SEND_MESSAGE;
|
||
9 years ago
|
if (payload_buffer == NULL) {
|
||
|
gpr_log(GPR_INFO, "NULL payload buffer !!!");
|
||
|
}
|
||
8 years ago
|
write_op.data.send_message.send_message = payload_buffer;
|
||
9 years ago
|
error = grpc_call_start_batch(call, &write_op, 1, tagarg, NULL);
|
||
|
GPR_ASSERT(GRPC_CALL_OK == error);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
static void start_send_status(void) {
|
||
9 years ago
|
grpc_call_error error;
|
||
9 years ago
|
void *tagarg = tag(FLING_SERVER_SEND_STATUS_FOR_STREAMING);
|
||
10 years ago
|
status_op[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER;
|
||
|
status_op[0].data.send_status_from_server.status = GRPC_STATUS_OK;
|
||
|
status_op[0].data.send_status_from_server.trailing_metadata_count = 0;
|
||
8 years ago
|
status_op[0].data.send_status_from_server.status_details = NULL;
|
||
10 years ago
|
status_op[1].op = GRPC_OP_RECV_CLOSE_ON_SERVER;
|
||
|
status_op[1].data.recv_close_on_server.cancelled = &was_cancelled;
|
||
|
|
||
9 years ago
|
error = grpc_call_start_batch(call, status_op, 2, tagarg, NULL);
|
||
|
GPR_ASSERT(GRPC_CALL_OK == error);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
/* We have some sort of deadlock, so let's not exit gracefully for now.
|
||
|
When that is resolved, please remove the #include <unistd.h> above. */
|
||
9 years ago
|
static void sigint_handler(int x) { _exit(0); }
|
||
10 years ago
|
|
||
9 years ago
|
int main(int argc, char **argv) {
|
||
10 years ago
|
grpc_event ev;
|
||
10 years ago
|
call_state *s;
|
||
|
char *addr_buf = NULL;
|
||
|
gpr_cmdline *cl;
|
||
8 years ago
|
grpc_completion_queue *shutdown_cq;
|
||
10 years ago
|
int shutdown_started = 0;
|
||
|
int shutdown_finished = 0;
|
||
10 years ago
|
|
||
|
int secure = 0;
|
||
7 years ago
|
const char *addr = NULL;
|
||
10 years ago
|
|
||
|
char *fake_argv[1];
|
||
|
|
||
9 years ago
|
gpr_timers_set_log_filename("latency_trace.fling_server.txt");
|
||
|
|
||
9 years ago
|
GPR_ASSERT(argc >= 1);
|
||
10 years ago
|
fake_argv[0] = argv[0];
|
||
9 years ago
|
grpc_test_init(1, fake_argv);
|
||
|
|
||
|
grpc_init();
|
||
|
srand((unsigned)clock());
|
||
|
|
||
|
cl = gpr_cmdline_create("fling server");
|
||
|
gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
|
||
|
gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
|
||
|
gpr_cmdline_parse(cl, argc, argv);
|
||
|
gpr_cmdline_destroy(cl);
|
||
|
|
||
|
if (addr == NULL) {
|
||
|
gpr_join_host_port(&addr_buf, "::", grpc_pick_unused_port_or_die());
|
||
|
addr = addr_buf;
|
||
|
}
|
||
|
gpr_log(GPR_INFO, "creating server on: %s", addr);
|
||
|
|
||
8 years ago
|
cq = grpc_completion_queue_create_for_next(NULL);
|
||
9 years ago
|
if (secure) {
|
||
|
grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
|
||
|
test_server1_cert};
|
||
|
grpc_server_credentials *ssl_creds = grpc_ssl_server_credentials_create(
|
||
|
NULL, &pem_key_cert_pair, 1, 0, NULL);
|
||
|
server = grpc_server_create(NULL, NULL);
|
||
|
GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, ssl_creds));
|
||
|
grpc_server_credentials_release(ssl_creds);
|
||
|
} else {
|
||
|
server = grpc_server_create(NULL, NULL);
|
||
|
GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr));
|
||
|
}
|
||
|
grpc_server_register_completion_queue(server, cq, NULL);
|
||
|
grpc_server_start(server);
|
||
|
|
||
|
gpr_free(addr_buf);
|
||
9 years ago
|
addr = addr_buf = NULL;
|
||
|
|
||
9 years ago
|
grpc_call_details_init(&call_details);
|
||
|
|
||
|
request_call();
|
||
|
|
||
|
grpc_profiler_start("server.prof");
|
||
|
signal(SIGINT, sigint_handler);
|
||
|
while (!shutdown_finished) {
|
||
|
if (got_sigint && !shutdown_started) {
|
||
|
gpr_log(GPR_INFO, "Shutting down due to SIGINT");
|
||
8 years ago
|
|
||
8 years ago
|
shutdown_cq = grpc_completion_queue_create_for_pluck(NULL);
|
||
8 years ago
|
grpc_server_shutdown_and_notify(server, shutdown_cq, tag(1000));
|
||
|
|
||
|
GPR_ASSERT(
|
||
|
grpc_completion_queue_pluck(shutdown_cq, tag(1000),
|
||
|
grpc_timeout_seconds_to_deadline(5), NULL)
|
||
|
.type == GRPC_OP_COMPLETE);
|
||
|
grpc_completion_queue_destroy(shutdown_cq);
|
||
|
|
||
9 years ago
|
grpc_completion_queue_shutdown(cq);
|
||
|
shutdown_started = 1;
|
||
|
}
|
||
|
ev = grpc_completion_queue_next(
|
||
|
cq, gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
|
||
|
gpr_time_from_micros(1000000, GPR_TIMESPAN)),
|
||
|
NULL);
|
||
7 years ago
|
s = static_cast<call_state *>(ev.tag);
|
||
9 years ago
|
switch (ev.type) {
|
||
|
case GRPC_OP_COMPLETE:
|
||
9 years ago
|
switch ((intptr_t)s) {
|
||
9 years ago
|
case FLING_SERVER_NEW_REQUEST:
|
||
|
if (call != NULL) {
|
||
8 years ago
|
if (0 == grpc_slice_str_cmp(call_details.method,
|
||
|
"/Reflector/reflectStream")) {
|
||
9 years ago
|
/* Received streaming call. Send metadata here. */
|
||
|
start_read_op(FLING_SERVER_READ_FOR_STREAMING);
|
||
|
send_initial_metadata();
|
||
|
} else {
|
||
|
/* Received unary call. Can do all ops in one batch. */
|
||
|
start_read_op(FLING_SERVER_READ_FOR_UNARY);
|
||
|
}
|
||
|
} else {
|
||
|
GPR_ASSERT(shutdown_started);
|
||
|
}
|
||
|
/* request_call();
|
||
|
*/
|
||
|
break;
|
||
|
case FLING_SERVER_READ_FOR_STREAMING:
|
||
|
if (payload_buffer != NULL) {
|
||
|
/* Received payload from client. */
|
||
|
start_write_op();
|
||
|
} else {
|
||
|
/* Received end of stream from client. */
|
||
|
start_send_status();
|
||
|
}
|
||
|
break;
|
||
|
case FLING_SERVER_WRITE_FOR_STREAMING:
|
||
|
/* Write completed at server */
|
||
|
grpc_byte_buffer_destroy(payload_buffer);
|
||
|
payload_buffer = NULL;
|
||
|
start_read_op(FLING_SERVER_READ_FOR_STREAMING);
|
||
|
break;
|
||
|
case FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING:
|
||
|
/* Metadata send completed at server */
|
||
|
break;
|
||
|
case FLING_SERVER_SEND_STATUS_FOR_STREAMING:
|
||
|
/* Send status and close completed at server */
|
||
8 years ago
|
grpc_call_unref(call);
|
||
9 years ago
|
if (!shutdown_started) request_call();
|
||
|
break;
|
||
|
case FLING_SERVER_READ_FOR_UNARY:
|
||
|
/* Finished payload read for unary. Start all reamaining
|
||
|
* unary ops in a batch.
|
||
|
*/
|
||
|
handle_unary_method();
|
||
|
break;
|
||
|
case FLING_SERVER_BATCH_OPS_FOR_UNARY:
|
||
|
/* Finished unary call. */
|
||
|
grpc_byte_buffer_destroy(payload_buffer);
|
||
|
payload_buffer = NULL;
|
||
8 years ago
|
grpc_call_unref(call);
|
||
9 years ago
|
if (!shutdown_started) request_call();
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
case GRPC_QUEUE_SHUTDOWN:
|
||
|
GPR_ASSERT(shutdown_started);
|
||
|
shutdown_finished = 1;
|
||
|
break;
|
||
|
case GRPC_QUEUE_TIMEOUT:
|
||
|
break;
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
|
grpc_profiler_stop();
|
||
|
grpc_call_details_destroy(&call_details);
|
||
10 years ago
|
|
||
9 years ago
|
grpc_server_destroy(server);
|
||
|
grpc_completion_queue_destroy(cq);
|
||
|
grpc_shutdown();
|
||
10 years ago
|
return 0;
|
||
10 years ago
|
}
|