aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-09-22 12:33:20 -0700
committerGravatar Craig Tiller <ctiller@google.com>2015-09-22 12:33:20 -0700
commita82950e68318a6aab6fe894fa39f7fa616c4647b (patch)
tree7d02bd1e9e1cbae1f14ad4ad1e06d3ae81a96dfe /src/core
parent8af4c337181322cc4fb396199c90f574cfb4163f (diff)
clang-format all core files
Diffstat (limited to 'src/core')
-rw-r--r--src/core/census/aggregation.h21
-rw-r--r--src/core/census/context.c5
-rw-r--r--src/core/census/context.h13
-rw-r--r--src/core/census/grpc_context.c24
-rw-r--r--src/core/census/grpc_filter.c194
-rw-r--r--src/core/census/initialize.c42
-rw-r--r--src/core/census/operation.c30
-rw-r--r--src/core/census/tracing.c15
-rw-r--r--src/core/channel/channel_args.c251
-rw-r--r--src/core/channel/channel_args.h25
-rw-r--r--src/core/channel/channel_stack.c187
-rw-r--r--src/core/channel/channel_stack.h88
-rw-r--r--src/core/channel/client_channel.c1031
-rw-r--r--src/core/channel/client_channel.h22
-rw-r--r--src/core/channel/compress_filter.c451
-rw-r--r--src/core/channel/connected_channel.c118
-rw-r--r--src/core/channel/connected_channel.h3
-rw-r--r--src/core/channel/context.h8
-rw-r--r--src/core/channel/http_client_filter.c313
-rw-r--r--src/core/channel/http_server_filter.c382
-rw-r--r--src/core/channel/noop_filter.c88
-rw-r--r--src/core/client_config/client_config.c50
-rw-r--r--src/core/client_config/client_config.h13
-rw-r--r--src/core/client_config/connector.c28
-rw-r--r--src/core/client_config/connector.h34
-rw-r--r--src/core/client_config/lb_policies/pick_first.c461
-rw-r--r--src/core/client_config/lb_policies/pick_first.h2
-rw-r--r--src/core/client_config/lb_policies/round_robin.c726
-rw-r--r--src/core/client_config/lb_policies/round_robin.h2
-rw-r--r--src/core/client_config/lb_policy.c86
-rw-r--r--src/core/client_config/lb_policy.h63
-rw-r--r--src/core/client_config/lb_policy_factory.c22
-rw-r--r--src/core/client_config/lb_policy_factory.h23
-rw-r--r--src/core/client_config/lb_policy_registry.c58
-rw-r--r--src/core/client_config/lb_policy_registry.h9
-rw-r--r--src/core/client_config/resolver.c67
-rw-r--r--src/core/client_config/resolver.h41
-rw-r--r--src/core/client_config/resolver_factory.c32
-rw-r--r--src/core/client_config/resolver_factory.h28
-rw-r--r--src/core/client_config/resolver_registry.c126
-rw-r--r--src/core/client_config/resolver_registry.h11
-rw-r--r--src/core/client_config/resolvers/dns_resolver.c284
-rw-r--r--src/core/client_config/resolvers/dns_resolver.h2
-rw-r--r--src/core/client_config/resolvers/sockaddr_resolver.c422
-rw-r--r--src/core/client_config/resolvers/sockaddr_resolver.h6
-rw-r--r--src/core/client_config/resolvers/zookeeper_resolver.c627
-rw-r--r--src/core/client_config/resolvers/zookeeper_resolver.h2
-rw-r--r--src/core/client_config/subchannel.c863
-rw-r--r--src/core/client_config/subchannel.h53
-rw-r--r--src/core/client_config/subchannel_factory.c21
-rw-r--r--src/core/client_config/subchannel_factory.h23
-rw-r--r--src/core/client_config/subchannel_factory_decorators/add_channel_arg.c9
-rw-r--r--src/core/client_config/subchannel_factory_decorators/add_channel_arg.h3
-rw-r--r--src/core/client_config/subchannel_factory_decorators/merge_channel_args.c62
-rw-r--r--src/core/client_config/subchannel_factory_decorators/merge_channel_args.h3
-rw-r--r--src/core/client_config/uri_parser.c316
-rw-r--r--src/core/client_config/uri_parser.h7
-rw-r--r--src/core/compression/algorithm.c103
-rw-r--r--src/core/compression/message_compress.c207
-rw-r--r--src/core/compression/message_compress.h6
-rw-r--r--src/core/debug/trace.c137
-rw-r--r--src/core/debug/trace.h6
-rw-r--r--src/core/httpcli/format_request.c112
-rw-r--r--src/core/httpcli/format_request.h6
-rw-r--r--src/core/httpcli/httpcli.c310
-rw-r--r--src/core/httpcli/httpcli.h57
-rw-r--r--src/core/httpcli/httpcli_security_connector.c199
-rw-r--r--src/core/httpcli/parser.c256
-rw-r--r--src/core/httpcli/parser.h14
-rw-r--r--src/core/iomgr/alarm.c390
-rw-r--r--src/core/iomgr/alarm.h11
-rw-r--r--src/core/iomgr/alarm_heap.c152
-rw-r--r--src/core/iomgr/alarm_heap.h17
-rw-r--r--src/core/iomgr/alarm_internal.h11
-rw-r--r--src/core/iomgr/closure.c56
-rw-r--r--src/core/iomgr/closure.h19
-rw-r--r--src/core/iomgr/endpoint.c47
-rw-r--r--src/core/iomgr/endpoint.h43
-rw-r--r--src/core/iomgr/endpoint_pair.h6
-rw-r--r--src/core/iomgr/endpoint_pair_posix.c35
-rw-r--r--src/core/iomgr/endpoint_pair_windows.c54
-rw-r--r--src/core/iomgr/exec_ctx.c28
-rw-r--r--src/core/iomgr/exec_ctx.h9
-rw-r--r--src/core/iomgr/fd_posix.c557
-rw-r--r--src/core/iomgr/fd_posix.h44
-rw-r--r--src/core/iomgr/iocp_windows.c216
-rw-r--r--src/core/iomgr/iocp_windows.h14
-rw-r--r--src/core/iomgr/iomgr.c159
-rw-r--r--src/core/iomgr/iomgr.h4
-rw-r--r--src/core/iomgr/iomgr_internal.h11
-rw-r--r--src/core/iomgr/iomgr_posix.c18
-rw-r--r--src/core/iomgr/iomgr_posix.h4
-rw-r--r--src/core/iomgr/iomgr_windows.c32
-rw-r--r--src/core/iomgr/pollset.h14
-rw-r--r--src/core/iomgr/pollset_multipoller_with_epoll.c289
-rw-r--r--src/core/iomgr/pollset_multipoller_with_poll_posix.c250
-rw-r--r--src/core/iomgr/pollset_posix.c685
-rw-r--r--src/core/iomgr/pollset_posix.h52
-rw-r--r--src/core/iomgr/pollset_set.h12
-rw-r--r--src/core/iomgr/pollset_set_posix.c144
-rw-r--r--src/core/iomgr/pollset_set_posix.h9
-rw-r--r--src/core/iomgr/pollset_set_windows.c28
-rw-r--r--src/core/iomgr/pollset_set_windows.h5
-rw-r--r--src/core/iomgr/pollset_windows.c160
-rw-r--r--src/core/iomgr/pollset_windows.h6
-rw-r--r--src/core/iomgr/resolve_address.h17
-rw-r--r--src/core/iomgr/resolve_address_posix.c177
-rw-r--r--src/core/iomgr/resolve_address_windows.c141
-rw-r--r--src/core/iomgr/sockaddr_utils.c271
-rw-r--r--src/core/iomgr/sockaddr_utils.h24
-rw-r--r--src/core/iomgr/sockaddr_win32.h2
-rw-r--r--src/core/iomgr/socket_utils_common_posix.c234
-rw-r--r--src/core/iomgr/socket_utils_linux.c7
-rw-r--r--src/core/iomgr/socket_utils_posix.c38
-rw-r--r--src/core/iomgr/socket_utils_posix.h21
-rw-r--r--src/core/iomgr/socket_windows.c56
-rw-r--r--src/core/iomgr/socket_windows.h14
-rw-r--r--src/core/iomgr/tcp_client.h6
-rw-r--r--src/core/iomgr/tcp_client_posix.c361
-rw-r--r--src/core/iomgr/tcp_client_windows.c207
-rw-r--r--src/core/iomgr/tcp_posix.c576
-rw-r--r--src/core/iomgr/tcp_posix.h3
-rw-r--r--src/core/iomgr/tcp_server.h17
-rw-r--r--src/core/iomgr/tcp_server_posix.c595
-rw-r--r--src/core/iomgr/tcp_server_windows.c537
-rw-r--r--src/core/iomgr/tcp_windows.c428
-rw-r--r--src/core/iomgr/tcp_windows.h4
-rw-r--r--src/core/iomgr/time_averaged_stats.c44
-rw-r--r--src/core/iomgr/time_averaged_stats.h12
-rw-r--r--src/core/iomgr/udp_server.c455
-rw-r--r--src/core/iomgr/udp_server.h15
-rw-r--r--src/core/iomgr/wakeup_fd_eventfd.c48
-rw-r--r--src/core/iomgr/wakeup_fd_nospecial.c9
-rw-r--r--src/core/iomgr/wakeup_fd_pipe.c69
-rw-r--r--src/core/iomgr/wakeup_fd_posix.c51
-rw-r--r--src/core/iomgr/wakeup_fd_posix.h30
-rw-r--r--src/core/iomgr/workqueue.h21
-rw-r--r--src/core/iomgr/workqueue_posix.c140
-rw-r--r--src/core/iomgr/workqueue_posix.h3
-rw-r--r--src/core/json/json.c41
-rw-r--r--src/core/json/json.h13
-rw-r--r--src/core/json/json_common.h3
-rw-r--r--src/core/json/json_reader.c1257
-rw-r--r--src/core/json/json_reader.h56
-rw-r--r--src/core/json/json_string.c355
-rw-r--r--src/core/json/json_writer.c385
-rw-r--r--src/core/json/json_writer.h31
-rw-r--r--src/core/profiling/basic_timers.c106
-rw-r--r--src/core/profiling/stap_timers.c28
-rw-r--r--src/core/profiling/timers.h94
-rw-r--r--src/core/security/base64.c311
-rw-r--r--src/core/security/base64.h8
-rw-r--r--src/core/security/client_auth_filter.c441
-rw-r--r--src/core/security/credentials.c1761
-rw-r--r--src/core/security/credentials.h169
-rw-r--r--src/core/security/credentials_metadata.c105
-rw-r--r--src/core/security/credentials_posix.c21
-rw-r--r--src/core/security/credentials_win32.c21
-rw-r--r--src/core/security/google_default_credentials.c240
-rw-r--r--src/core/security/handshake.c346
-rw-r--r--src/core/security/handshake.h7
-rw-r--r--src/core/security/json_token.c540
-rw-r--r--src/core/security/json_token.h37
-rw-r--r--src/core/security/jwt_verifier.c1171
-rw-r--r--src/core/security/jwt_verifier.h48
-rw-r--r--src/core/security/secure_endpoint.c520
-rw-r--r--src/core/security/secure_endpoint.h4
-rw-r--r--src/core/security/security_connector.c919
-rw-r--r--src/core/security/security_connector.h103
-rw-r--r--src/core/security/security_context.c396
-rw-r--r--src/core/security/security_context.h43
-rw-r--r--src/core/security/server_auth_filter.c281
-rw-r--r--src/core/security/server_secure_chttp2.c335
-rw-r--r--src/core/statistics/census_init.c16
-rw-r--r--src/core/statistics/census_interface.h15
-rw-r--r--src/core/statistics/census_log.c591
-rw-r--r--src/core/statistics/census_log.h18
-rw-r--r--src/core/statistics/census_rpc_stats.c339
-rw-r--r--src/core/statistics/census_rpc_stats.h70
-rw-r--r--src/core/statistics/census_tracing.c309
-rw-r--r--src/core/statistics/census_tracing.h47
-rw-r--r--src/core/statistics/hash_table.c376
-rw-r--r--src/core/statistics/hash_table.h39
-rw-r--r--src/core/statistics/window_stats.c370
-rw-r--r--src/core/statistics/window_stats.h25
-rw-r--r--src/core/support/alloc.c52
-rw-r--r--src/core/support/cmdline.c388
-rw-r--r--src/core/support/cpu_iphone.c12
-rw-r--r--src/core/support/cpu_linux.c40
-rw-r--r--src/core/support/cpu_posix.c37
-rw-r--r--src/core/support/cpu_windows.c12
-rw-r--r--src/core/support/env.h9
-rw-r--r--src/core/support/env_linux.c16
-rw-r--r--src/core/support/env_posix.c16
-rw-r--r--src/core/support/env_win32.c21
-rw-r--r--src/core/support/file.c72
-rw-r--r--src/core/support/file.h10
-rw-r--r--src/core/support/file_posix.c52
-rw-r--r--src/core/support/file_win32.c33
-rw-r--r--src/core/support/histogram.c276
-rw-r--r--src/core/support/host_port.c121
-rw-r--r--src/core/support/log.c26
-rw-r--r--src/core/support/log_android.c36
-rw-r--r--src/core/support/log_linux.c58
-rw-r--r--src/core/support/log_posix.c73
-rw-r--r--src/core/support/log_win32.c95
-rw-r--r--src/core/support/murmur_hash.c44
-rw-r--r--src/core/support/murmur_hash.h2
-rw-r--r--src/core/support/slice.c429
-rw-r--r--src/core/support/slice_buffer.c288
-rw-r--r--src/core/support/stack_lockfree.c114
-rw-r--r--src/core/support/stack_lockfree.h8
-rw-r--r--src/core/support/string.c350
-rw-r--r--src/core/support/string.h42
-rw-r--r--src/core/support/string_posix.c54
-rw-r--r--src/core/support/string_win32.c81
-rw-r--r--src/core/support/string_win32.h4
-rw-r--r--src/core/support/subprocess_posix.c98
-rw-r--r--src/core/support/sync.c124
-rw-r--r--src/core/support/sync_posix.c90
-rw-r--r--src/core/support/sync_win32.c122
-rw-r--r--src/core/support/thd.c31
-rw-r--r--src/core/support/thd_posix.c66
-rw-r--r--src/core/support/thd_win32.c107
-rw-r--r--src/core/support/time.c457
-rw-r--r--src/core/support/time_posix.c107
-rw-r--r--src/core/support/time_precise.h46
-rw-r--r--src/core/support/time_win32.c56
-rw-r--r--src/core/support/tls_pthread.c6
-rw-r--r--src/core/surface/byte_buffer.c87
-rw-r--r--src/core/surface/byte_buffer_queue.c91
-rw-r--r--src/core/surface/byte_buffer_queue.h18
-rw-r--r--src/core/surface/byte_buffer_reader.c95
-rw-r--r--src/core/surface/call.c2600
-rw-r--r--src/core/surface/call.h171
-rw-r--r--src/core/surface/call_details.c14
-rw-r--r--src/core/surface/call_log_batch.c124
-rw-r--r--src/core/surface/channel.c428
-rw-r--r--src/core/surface/channel.h34
-rw-r--r--src/core/surface/channel_connectivity.c189
-rw-r--r--src/core/surface/channel_create.c199
-rw-r--r--src/core/surface/completion_queue.c437
-rw-r--r--src/core/surface/completion_queue.h30
-rw-r--r--src/core/surface/event_string.c50
-rw-r--r--src/core/surface/event_string.h2
-rw-r--r--src/core/surface/init.c139
-rw-r--r--src/core/surface/init.h4
-rw-r--r--src/core/surface/init_secure.c8
-rw-r--r--src/core/surface/init_unsecure.c5
-rw-r--r--src/core/surface/lame_client.c173
-rw-r--r--src/core/surface/metadata_array.c12
-rw-r--r--src/core/surface/secure_channel_create.c327
-rw-r--r--src/core/surface/server.c1560
-rw-r--r--src/core/surface/server.h21
-rw-r--r--src/core/surface/server_chttp2.c128
-rw-r--r--src/core/surface/server_create.c11
-rw-r--r--src/core/surface/version.c4
-rw-r--r--src/core/transport/chttp2/alpn.c26
-rw-r--r--src/core/transport/chttp2/alpn.h6
-rw-r--r--src/core/transport/chttp2/bin_encoder.c214
-rw-r--r--src/core/transport/chttp2/bin_encoder.h8
-rw-r--r--src/core/transport/chttp2/frame.h3
-rw-r--r--src/core/transport/chttp2/frame_data.c215
-rw-r--r--src/core/transport/chttp2/frame_data.h21
-rw-r--r--src/core/transport/chttp2/frame_goaway.c218
-rw-r--r--src/core/transport/chttp2/frame_goaway.h22
-rw-r--r--src/core/transport/chttp2/frame_ping.c82
-rw-r--r--src/core/transport/chttp2/frame_ping.h13
-rw-r--r--src/core/transport/chttp2/frame_rst_stream.c78
-rw-r--r--src/core/transport/chttp2/frame_rst_stream.h13
-rw-r--r--src/core/transport/chttp2/frame_settings.c353
-rw-r--r--src/core/transport/chttp2/frame_settings.h32
-rw-r--r--src/core/transport/chttp2/frame_window_update.c107
-rw-r--r--src/core/transport/chttp2/frame_window_update.h15
-rw-r--r--src/core/transport/chttp2/hpack_parser.c1994
-rw-r--r--src/core/transport/chttp2/hpack_parser.h30
-rw-r--r--src/core/transport/chttp2/hpack_table.c362
-rw-r--r--src/core/transport/chttp2/hpack_table.h18
-rw-r--r--src/core/transport/chttp2/http2_errors.h3
-rw-r--r--src/core/transport/chttp2/huffsyms.c130
-rw-r--r--src/core/transport/chttp2/huffsyms.h3
-rw-r--r--src/core/transport/chttp2/incoming_metadata.c200
-rw-r--r--src/core/transport/chttp2/incoming_metadata.h35
-rw-r--r--src/core/transport/chttp2/internal.h247
-rw-r--r--src/core/transport/chttp2/parsing.c1155
-rw-r--r--src/core/transport/chttp2/status_conversion.c36
-rw-r--r--src/core/transport/chttp2/status_conversion.h10
-rw-r--r--src/core/transport/chttp2/stream_encoder.c925
-rw-r--r--src/core/transport/chttp2/stream_encoder.h17
-rw-r--r--src/core/transport/chttp2/stream_lists.c488
-rw-r--r--src/core/transport/chttp2/stream_map.c215
-rw-r--r--src/core/transport/chttp2/stream_map.h26
-rw-r--r--src/core/transport/chttp2/timeout_encoding.c258
-rw-r--r--src/core/transport/chttp2/timeout_encoding.h4
-rw-r--r--src/core/transport/chttp2/varint.c56
-rw-r--r--src/core/transport/chttp2/varint.h6
-rw-r--r--src/core/transport/chttp2/writing.c309
-rw-r--r--src/core/transport/chttp2_transport.c1721
-rw-r--r--src/core/transport/chttp2_transport.h8
-rw-r--r--src/core/transport/connectivity_state.c139
-rw-r--r--src/core/transport/connectivity_state.h25
-rw-r--r--src/core/transport/metadata.c867
-rw-r--r--src/core/transport/metadata.h84
-rw-r--r--src/core/transport/stream_op.c451
-rw-r--r--src/core/transport/stream_op.h82
-rw-r--r--src/core/transport/transport.c148
-rw-r--r--src/core/transport/transport.h56
-rw-r--r--src/core/transport/transport_impl.h25
-rw-r--r--src/core/transport/transport_op_string.c208
-rw-r--r--src/core/tsi/fake_transport_security.c670
-rw-r--r--src/core/tsi/fake_transport_security.h10
-rw-r--r--src/core/tsi/ssl_transport_security.c2096
-rw-r--r--src/core/tsi/ssl_transport_security.h33
-rw-r--r--src/core/tsi/transport_security.c323
-rw-r--r--src/core/tsi/transport_security.h89
-rw-r--r--src/core/tsi/transport_security_interface.h110
316 files changed, 25855 insertions, 30329 deletions
diff --git a/src/core/census/aggregation.h b/src/core/census/aggregation.h
index 779615f0c9..e9bc6ada96 100644
--- a/src/core/census/aggregation.h
+++ b/src/core/census/aggregation.h
@@ -37,31 +37,30 @@
#define GRPC_INTERNAL_CORE_CENSUS_AGGREGATION_H
/** Structure used to describe an aggregation type. */
-struct census_aggregation_ops
-{
+struct census_aggregation_ops {
/* Create a new aggregation. The pointer returned can be used in future calls
to clone(), free(), record(), data() and reset(). */
- void *(*create) (const void *create_arg);
+ void *(*create)(const void *create_arg);
/* Make a copy of an aggregation created by create() */
- void *(*clone) (const void *aggregation);
+ void *(*clone)(const void *aggregation);
/* Destroy an aggregation created by create() */
- void (*free) (void *aggregation);
+ void (*free)(void *aggregation);
/* Record a new value against aggregation. */
- void (*record) (void *aggregation, double value);
+ void (*record)(void *aggregation, double value);
/* Return current aggregation data. The caller must cast this object into
the correct type for the aggregation result. The object returned can be
freed by using free_data(). */
- void *(*data) (const void *aggregation);
+ void *(*data)(const void *aggregation);
/* free data returned by data() */
- void (*free_data) (void *data);
+ void (*free_data)(void *data);
/* Reset an aggregation to default (zero) values. */
- void (*reset) (void *aggregation);
+ void (*reset)(void *aggregation);
/* Merge 'from' aggregation into 'to'. Both aggregations must be compatible */
- void (*merge) (void *to, const void *from);
+ void (*merge)(void *to, const void *from);
/* Fill buffer with printable string version of aggregation contents. For
debugging only. Returns the number of bytes added to buffer (a value == n
implies the buffer was of insufficient size). */
- size_t (*print) (const void *aggregation, char *buffer, size_t n);
+ size_t (*print)(const void *aggregation, char *buffer, size_t n);
};
#endif /* GRPC_INTERNAL_CORE_CENSUS_AGGREGATION_H */
diff --git a/src/core/census/context.c b/src/core/census/context.c
index 0a9f129d56..cab58b653c 100644
--- a/src/core/census/context.c
+++ b/src/core/census/context.c
@@ -39,9 +39,8 @@
/* Placeholder implementation only. */
-size_t
-census_context_serialize (const census_context * context, char *buffer, size_t buf_size)
-{
+size_t census_context_serialize(const census_context *context, char *buffer,
+ size_t buf_size) {
/* TODO(aveitch): implement serialization */
return 0;
}
diff --git a/src/core/census/context.h b/src/core/census/context.h
index 4da979dc33..d9907d4da7 100644
--- a/src/core/census/context.h
+++ b/src/core/census/context.h
@@ -38,13 +38,12 @@
/* census_context is the in-memory representation of information needed to
* maintain tracing, RPC statistics and resource usage information. */
-struct census_context
-{
- gpr_uint64 op_id; /* Operation identifier - unique per-context */
- gpr_uint64 trace_id; /* Globally unique trace identifier */
- /* TODO(aveitch) Add census tags:
- const census_tag_set *tags;
- */
+struct census_context {
+ gpr_uint64 op_id; /* Operation identifier - unique per-context */
+ gpr_uint64 trace_id; /* Globally unique trace identifier */
+ /* TODO(aveitch) Add census tags:
+ const census_tag_set *tags;
+ */
};
#endif /* GRPC_INTERNAL_CORE_CENSUS_CONTEXT_H */
diff --git a/src/core/census/grpc_context.c b/src/core/census/grpc_context.c
index 4520c68010..429f3ec9db 100644
--- a/src/core/census/grpc_context.c
+++ b/src/core/census/grpc_context.c
@@ -35,21 +35,15 @@
#include <grpc/grpc.h>
#include "src/core/surface/call.h"
-void
-grpc_census_call_set_context (grpc_call * call, census_context * context)
-{
- if (census_enabled () == CENSUS_FEATURE_NONE)
- {
- return;
- }
- if (context != NULL)
- {
- grpc_call_context_set (call, GRPC_CONTEXT_TRACING, context, NULL);
- }
+void grpc_census_call_set_context(grpc_call *call, census_context *context) {
+ if (census_enabled() == CENSUS_FEATURE_NONE) {
+ return;
+ }
+ if (context != NULL) {
+ grpc_call_context_set(call, GRPC_CONTEXT_TRACING, context, NULL);
+ }
}
-census_context *
-grpc_census_call_get_context (grpc_call * call)
-{
- return (census_context *) grpc_call_context_get (call, GRPC_CONTEXT_TRACING);
+census_context *grpc_census_call_get_context(grpc_call *call) {
+ return (census_context *)grpc_call_context_get(call, GRPC_CONTEXT_TRACING);
}
diff --git a/src/core/census/grpc_filter.c b/src/core/census/grpc_filter.c
index 485987a295..1830af3feb 100644
--- a/src/core/census/grpc_filter.c
+++ b/src/core/census/grpc_filter.c
@@ -46,8 +46,7 @@
#include <grpc/support/slice.h>
#include <grpc/support/time.h>
-typedef struct call_data
-{
+typedef struct call_data {
census_op_id op_id;
census_context *ctxt;
gpr_timespec start_ts;
@@ -58,154 +57,139 @@ typedef struct call_data
grpc_closure *on_done_recv;
} call_data;
-typedef struct channel_data
-{
- grpc_mdstr *path_str; /* pointer to meta data str with key == ":path" */
+typedef struct channel_data {
+ grpc_mdstr *path_str; /* pointer to meta data str with key == ":path" */
} channel_data;
-static void
-extract_and_annotate_method_tag (grpc_stream_op_buffer * sopb, call_data * calld, channel_data * chand)
-{
+static void extract_and_annotate_method_tag(grpc_stream_op_buffer *sopb,
+ call_data *calld,
+ channel_data *chand) {
grpc_linked_mdelem *m;
size_t i;
- for (i = 0; i < sopb->nops; i++)
- {
- grpc_stream_op *op = &sopb->ops[i];
- if (op->type != GRPC_OP_METADATA)
- continue;
- for (m = op->data.metadata.list.head; m != NULL; m = m->next)
- {
- if (m->md->key == chand->path_str)
- {
- gpr_log (GPR_DEBUG, "%s", (const char *) GPR_SLICE_START_PTR (m->md->value->slice));
- /* Add method tag here */
- }
- }
+ for (i = 0; i < sopb->nops; i++) {
+ grpc_stream_op *op = &sopb->ops[i];
+ if (op->type != GRPC_OP_METADATA) continue;
+ for (m = op->data.metadata.list.head; m != NULL; m = m->next) {
+ if (m->md->key == chand->path_str) {
+ gpr_log(GPR_DEBUG, "%s",
+ (const char *)GPR_SLICE_START_PTR(m->md->value->slice));
+ /* Add method tag here */
+ }
}
+ }
}
-static void
-client_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void client_mutate_op(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- if (op->send_ops)
- {
- extract_and_annotate_method_tag (op->send_ops, calld, chand);
- }
+ if (op->send_ops) {
+ extract_and_annotate_method_tag(op->send_ops, calld, chand);
+ }
}
-static void
-client_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- client_mutate_op (elem, op);
- grpc_call_next_op (exec_ctx, elem, op);
+static void client_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ client_mutate_op(elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
-static void
-server_on_done_recv (grpc_exec_ctx * exec_ctx, void *ptr, int success)
-{
+static void server_on_done_recv(grpc_exec_ctx *exec_ctx, void *ptr,
+ int success) {
grpc_call_element *elem = ptr;
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- if (success)
- {
- extract_and_annotate_method_tag (calld->recv_ops, calld, chand);
- }
- calld->on_done_recv->cb (exec_ctx, calld->on_done_recv->cb_arg, success);
+ if (success) {
+ extract_and_annotate_method_tag(calld->recv_ops, calld, chand);
+ }
+ calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
}
-static void
-server_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void server_mutate_op(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
- if (op->recv_ops)
- {
- /* substitute our callback for the op callback */
- calld->recv_ops = op->recv_ops;
- calld->on_done_recv = op->on_done_recv;
- op->on_done_recv = calld->on_done_recv;
- }
+ if (op->recv_ops) {
+ /* substitute our callback for the op callback */
+ calld->recv_ops = op->recv_ops;
+ calld->on_done_recv = op->on_done_recv;
+ op->on_done_recv = calld->on_done_recv;
+ }
}
-static void
-server_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void server_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
- GPR_ASSERT ((calld->op_id.upper != 0) || (calld->op_id.lower != 0));
- server_mutate_op (elem, op);
- grpc_call_next_op (exec_ctx, elem, op);
+ GPR_ASSERT((calld->op_id.upper != 0) || (calld->op_id.lower != 0));
+ server_mutate_op(elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
-static void
-client_init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void client_init_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *d = elem->call_data;
- GPR_ASSERT (d != NULL);
- d->start_ts = gpr_now (GPR_CLOCK_REALTIME);
- if (initial_op)
- client_mutate_op (elem, initial_op);
+ GPR_ASSERT(d != NULL);
+ d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
+ if (initial_op) client_mutate_op(elem, initial_op);
}
-static void
-client_destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
call_data *d = elem->call_data;
- GPR_ASSERT (d != NULL);
+ GPR_ASSERT(d != NULL);
/* TODO(hongyu): record rpc client stats and census_rpc_end_op here */
}
-static void
-server_init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void server_init_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *d = elem->call_data;
- GPR_ASSERT (d != NULL);
- d->start_ts = gpr_now (GPR_CLOCK_REALTIME);
+ GPR_ASSERT(d != NULL);
+ d->start_ts = gpr_now(GPR_CLOCK_REALTIME);
/* TODO(hongyu): call census_tracing_start_op here. */
- grpc_closure_init (d->on_done_recv, server_on_done_recv, elem);
- if (initial_op)
- server_mutate_op (elem, initial_op);
+ grpc_closure_init(d->on_done_recv, server_on_done_recv, elem);
+ if (initial_op) server_mutate_op(elem, initial_op);
}
-static void
-server_destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
call_data *d = elem->call_data;
- GPR_ASSERT (d != NULL);
+ GPR_ASSERT(d != NULL);
/* TODO(hongyu): record rpc server stats and census_tracing_end_op here */
}
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
channel_data *chand = elem->channel_data;
- GPR_ASSERT (chand != NULL);
- chand->path_str = grpc_mdstr_from_string (mdctx, ":path", 0);
+ GPR_ASSERT(chand != NULL);
+ chand->path_str = grpc_mdstr_from_string(mdctx, ":path", 0);
}
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
channel_data *chand = elem->channel_data;
- GPR_ASSERT (chand != NULL);
- if (chand->path_str != NULL)
- {
- GRPC_MDSTR_UNREF (chand->path_str);
- }
+ GPR_ASSERT(chand != NULL);
+ if (chand->path_str != NULL) {
+ GRPC_MDSTR_UNREF(chand->path_str);
+ }
}
const grpc_channel_filter grpc_client_census_filter = {
- client_start_transport_op, grpc_channel_next_op,
- sizeof (call_data), client_init_call_elem,
- client_destroy_call_elem, sizeof (channel_data),
- init_channel_elem, destroy_channel_elem,
- grpc_call_next_get_peer, "census-client"
-};
+ client_start_transport_op, grpc_channel_next_op,
+ sizeof(call_data), client_init_call_elem,
+ client_destroy_call_elem, sizeof(channel_data),
+ init_channel_elem, destroy_channel_elem,
+ grpc_call_next_get_peer, "census-client"};
const grpc_channel_filter grpc_server_census_filter = {
- server_start_transport_op, grpc_channel_next_op,
- sizeof (call_data), server_init_call_elem,
- server_destroy_call_elem, sizeof (channel_data),
- init_channel_elem, destroy_channel_elem,
- grpc_call_next_get_peer, "census-server"
-};
+ server_start_transport_op, grpc_channel_next_op,
+ sizeof(call_data), server_init_call_elem,
+ server_destroy_call_elem, sizeof(channel_data),
+ init_channel_elem, destroy_channel_elem,
+ grpc_call_next_get_peer, "census-server"};
diff --git a/src/core/census/initialize.c b/src/core/census/initialize.c
index dab8354168..8d60f790eb 100644
--- a/src/core/census/initialize.c
+++ b/src/core/census/initialize.c
@@ -35,39 +35,23 @@
static int features_enabled = CENSUS_FEATURE_NONE;
-int
-census_initialize (int features)
-{
- if (features_enabled != CENSUS_FEATURE_NONE)
- {
- return 1;
- }
- if (features != CENSUS_FEATURE_NONE)
- {
- return 1;
- }
- else
- {
- features_enabled = features;
- return 0;
- }
+int census_initialize(int features) {
+ if (features_enabled != CENSUS_FEATURE_NONE) {
+ return 1;
+ }
+ if (features != CENSUS_FEATURE_NONE) {
+ return 1;
+ } else {
+ features_enabled = features;
+ return 0;
+ }
}
-void
-census_shutdown (void)
-{
- features_enabled = CENSUS_FEATURE_NONE;
-}
+void census_shutdown(void) { features_enabled = CENSUS_FEATURE_NONE; }
-int
-census_supported (void)
-{
+int census_supported(void) {
/* TODO(aveitch): improve this as we implement features... */
return CENSUS_FEATURE_NONE;
}
-int
-census_enabled (void)
-{
- return features_enabled;
-}
+int census_enabled(void) { return features_enabled; }
diff --git a/src/core/census/operation.c b/src/core/census/operation.c
index b409c17d48..118eb0a47a 100644
--- a/src/core/census/operation.c
+++ b/src/core/census/operation.c
@@ -34,34 +34,30 @@
/* TODO(aveitch): These are all placeholder implementations. */
-census_timestamp
-census_start_rpc_op_timestamp (void)
-{
+census_timestamp census_start_rpc_op_timestamp(void) {
census_timestamp ct;
/* TODO(aveitch): assumes gpr_timespec implementation of census_timestamp. */
- ct.ts = gpr_now (GPR_CLOCK_MONOTONIC);
+ ct.ts = gpr_now(GPR_CLOCK_MONOTONIC);
return ct;
}
-census_context *
-census_start_client_rpc_op (const census_context * context, gpr_int64 rpc_name_id, const census_rpc_name_info * rpc_name_info, const char *peer, int trace_mask, const census_timestamp * start_time)
-{
+census_context *census_start_client_rpc_op(
+ const census_context *context, gpr_int64 rpc_name_id,
+ const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
+ const census_timestamp *start_time) {
return NULL;
}
-census_context *
-census_start_server_rpc_op (const char *buffer, gpr_int64 rpc_name_id, const census_rpc_name_info * rpc_name_info, const char *peer, int trace_mask, census_timestamp * start_time)
-{
+census_context *census_start_server_rpc_op(
+ const char *buffer, gpr_int64 rpc_name_id,
+ const census_rpc_name_info *rpc_name_info, const char *peer, int trace_mask,
+ census_timestamp *start_time) {
return NULL;
}
-census_context *
-census_start_op (census_context * context, const char *family, const char *name, int trace_mask)
-{
+census_context *census_start_op(census_context *context, const char *family,
+ const char *name, int trace_mask) {
return NULL;
}
-void
-census_end_op (census_context * context, int status)
-{
-}
+void census_end_op(census_context *context, int status) {}
diff --git a/src/core/census/tracing.c b/src/core/census/tracing.c
index 272efd4ca0..ae38773c0a 100644
--- a/src/core/census/tracing.c
+++ b/src/core/census/tracing.c
@@ -35,18 +35,11 @@
/* TODO(aveitch): These are all placeholder implementations. */
-int
-census_trace_mask (const census_context * context)
-{
+int census_trace_mask(const census_context *context) {
return CENSUS_TRACE_MASK_NONE;
}
-void
-census_set_trace_mask (int trace_mask)
-{
-}
+void census_set_trace_mask(int trace_mask) {}
-void
-census_trace_print (census_context * context, gpr_uint32 type, const char *buffer, size_t n)
-{
-}
+void census_trace_print(census_context *context, gpr_uint32 type,
+ const char *buffer, size_t n) {}
diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c
index 4207a97114..487db1119a 100644
--- a/src/core/channel/channel_args.c
+++ b/src/core/channel/channel_args.c
@@ -41,206 +41,169 @@
#include <string.h>
-static grpc_arg
-copy_arg (const grpc_arg * src)
-{
+static grpc_arg copy_arg(const grpc_arg *src) {
grpc_arg dst;
dst.type = src->type;
- dst.key = gpr_strdup (src->key);
- switch (dst.type)
- {
+ dst.key = gpr_strdup(src->key);
+ switch (dst.type) {
case GRPC_ARG_STRING:
- dst.value.string = gpr_strdup (src->value.string);
+ dst.value.string = gpr_strdup(src->value.string);
break;
case GRPC_ARG_INTEGER:
dst.value.integer = src->value.integer;
break;
case GRPC_ARG_POINTER:
dst.value.pointer = src->value.pointer;
- dst.value.pointer.p = src->value.pointer.copy ? src->value.pointer.copy (src->value.pointer.p) : src->value.pointer.p;
+ dst.value.pointer.p = src->value.pointer.copy
+ ? src->value.pointer.copy(src->value.pointer.p)
+ : src->value.pointer.p;
break;
- }
+ }
return dst;
}
-grpc_channel_args *
-grpc_channel_args_copy_and_add (const grpc_channel_args * src, const grpc_arg * to_add, size_t num_to_add)
-{
- grpc_channel_args *dst = gpr_malloc (sizeof (grpc_channel_args));
+grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src,
+ const grpc_arg *to_add,
+ size_t num_to_add) {
+ grpc_channel_args *dst = gpr_malloc(sizeof(grpc_channel_args));
size_t i;
size_t src_num_args = (src == NULL) ? 0 : src->num_args;
- if (!src && !to_add)
- {
- dst->num_args = 0;
- dst->args = NULL;
- return dst;
- }
+ if (!src && !to_add) {
+ dst->num_args = 0;
+ dst->args = NULL;
+ return dst;
+ }
dst->num_args = src_num_args + num_to_add;
- dst->args = gpr_malloc (sizeof (grpc_arg) * dst->num_args);
- for (i = 0; i < src_num_args; i++)
- {
- dst->args[i] = copy_arg (&src->args[i]);
- }
- for (i = 0; i < num_to_add; i++)
- {
- dst->args[i + src_num_args] = copy_arg (&to_add[i]);
- }
+ dst->args = gpr_malloc(sizeof(grpc_arg) * dst->num_args);
+ for (i = 0; i < src_num_args; i++) {
+ dst->args[i] = copy_arg(&src->args[i]);
+ }
+ for (i = 0; i < num_to_add; i++) {
+ dst->args[i + src_num_args] = copy_arg(&to_add[i]);
+ }
return dst;
}
-grpc_channel_args *
-grpc_channel_args_copy (const grpc_channel_args * src)
-{
- return grpc_channel_args_copy_and_add (src, NULL, 0);
+grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src) {
+ return grpc_channel_args_copy_and_add(src, NULL, 0);
}
-grpc_channel_args *
-grpc_channel_args_merge (const grpc_channel_args * a, const grpc_channel_args * b)
-{
- return grpc_channel_args_copy_and_add (a, b->args, b->num_args);
+grpc_channel_args *grpc_channel_args_merge(const grpc_channel_args *a,
+ const grpc_channel_args *b) {
+ return grpc_channel_args_copy_and_add(a, b->args, b->num_args);
}
-void
-grpc_channel_args_destroy (grpc_channel_args * a)
-{
+void grpc_channel_args_destroy(grpc_channel_args *a) {
size_t i;
- for (i = 0; i < a->num_args; i++)
- {
- switch (a->args[i].type)
- {
- case GRPC_ARG_STRING:
- gpr_free (a->args[i].value.string);
- break;
- case GRPC_ARG_INTEGER:
- break;
- case GRPC_ARG_POINTER:
- if (a->args[i].value.pointer.destroy)
- {
- a->args[i].value.pointer.destroy (a->args[i].value.pointer.p);
- }
- break;
- }
- gpr_free (a->args[i].key);
+ for (i = 0; i < a->num_args; i++) {
+ switch (a->args[i].type) {
+ case GRPC_ARG_STRING:
+ gpr_free(a->args[i].value.string);
+ break;
+ case GRPC_ARG_INTEGER:
+ break;
+ case GRPC_ARG_POINTER:
+ if (a->args[i].value.pointer.destroy) {
+ a->args[i].value.pointer.destroy(a->args[i].value.pointer.p);
+ }
+ break;
}
- gpr_free (a->args);
- gpr_free (a);
+ gpr_free(a->args[i].key);
+ }
+ gpr_free(a->args);
+ gpr_free(a);
}
-int
-grpc_channel_args_is_census_enabled (const grpc_channel_args * a)
-{
+int grpc_channel_args_is_census_enabled(const grpc_channel_args *a) {
size_t i;
- if (a == NULL)
- return 0;
- for (i = 0; i < a->num_args; i++)
- {
- if (0 == strcmp (a->args[i].key, GRPC_ARG_ENABLE_CENSUS))
- {
- return a->args[i].value.integer != 0;
- }
+ if (a == NULL) return 0;
+ for (i = 0; i < a->num_args; i++) {
+ if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_CENSUS)) {
+ return a->args[i].value.integer != 0;
}
+ }
return 0;
}
-grpc_compression_algorithm
-grpc_channel_args_get_compression_algorithm (const grpc_channel_args * a)
-{
+grpc_compression_algorithm grpc_channel_args_get_compression_algorithm(
+ const grpc_channel_args *a) {
size_t i;
- if (a == NULL)
- return 0;
- for (i = 0; i < a->num_args; ++i)
- {
- if (a->args[i].type == GRPC_ARG_INTEGER && !strcmp (GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key))
- {
- return (grpc_compression_algorithm) a->args[i].value.integer;
- break;
- }
+ if (a == NULL) return 0;
+ for (i = 0; i < a->num_args; ++i) {
+ if (a->args[i].type == GRPC_ARG_INTEGER &&
+ !strcmp(GRPC_COMPRESSION_ALGORITHM_ARG, a->args[i].key)) {
+ return (grpc_compression_algorithm)a->args[i].value.integer;
+ break;
}
+ }
return GRPC_COMPRESS_NONE;
}
-grpc_channel_args *
-grpc_channel_args_set_compression_algorithm (grpc_channel_args * a, grpc_compression_algorithm algorithm)
-{
+grpc_channel_args *grpc_channel_args_set_compression_algorithm(
+ grpc_channel_args *a, grpc_compression_algorithm algorithm) {
grpc_arg tmp;
tmp.type = GRPC_ARG_INTEGER;
tmp.key = GRPC_COMPRESSION_ALGORITHM_ARG;
tmp.value.integer = algorithm;
- return grpc_channel_args_copy_and_add (a, &tmp, 1);
+ return grpc_channel_args_copy_and_add(a, &tmp, 1);
}
/** Returns 1 if the argument for compression algorithm's enabled states bitset
* was found in \a a, returning the arg's value in \a states. Otherwise, returns
* 0. */
-static int
-find_compression_algorithm_states_bitset (const grpc_channel_args * a, int **states_arg)
-{
- if (a != NULL)
- {
- size_t i;
- for (i = 0; i < a->num_args; ++i)
- {
- if (a->args[i].type == GRPC_ARG_INTEGER && !strcmp (GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key))
- {
- *states_arg = &a->args[i].value.integer;
- return 1; /* GPR_TRUE */
- }
- }
+static int find_compression_algorithm_states_bitset(const grpc_channel_args *a,
+ int **states_arg) {
+ if (a != NULL) {
+ size_t i;
+ for (i = 0; i < a->num_args; ++i) {
+ if (a->args[i].type == GRPC_ARG_INTEGER &&
+ !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) {
+ *states_arg = &a->args[i].value.integer;
+ return 1; /* GPR_TRUE */
+ }
}
- return 0; /* GPR_FALSE */
+ }
+ return 0; /* GPR_FALSE */
}
-grpc_channel_args *
-grpc_channel_args_compression_algorithm_set_state (grpc_channel_args ** a, grpc_compression_algorithm algorithm, int state)
-{
+grpc_channel_args *grpc_channel_args_compression_algorithm_set_state(
+ grpc_channel_args **a, grpc_compression_algorithm algorithm, int state) {
int *states_arg;
grpc_channel_args *result = *a;
- const int states_arg_found = find_compression_algorithm_states_bitset (*a, &states_arg);
+ const int states_arg_found =
+ find_compression_algorithm_states_bitset(*a, &states_arg);
- if (states_arg_found)
- {
- if (state != 0)
- {
- GPR_BITSET ((unsigned *) states_arg, algorithm);
- }
- else
- {
- GPR_BITCLEAR ((unsigned *) states_arg, algorithm);
- }
+ if (states_arg_found) {
+ if (state != 0) {
+ GPR_BITSET((unsigned *)states_arg, algorithm);
+ } else {
+ GPR_BITCLEAR((unsigned *)states_arg, algorithm);
}
- else
- {
- /* create a new arg */
- grpc_arg tmp;
- tmp.type = GRPC_ARG_INTEGER;
- tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG;
- /* all enabled by default */
- tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
- if (state != 0)
- {
- GPR_BITSET ((unsigned *) &tmp.value.integer, algorithm);
- }
- else
- {
- GPR_BITCLEAR ((unsigned *) &tmp.value.integer, algorithm);
- }
- result = grpc_channel_args_copy_and_add (*a, &tmp, 1);
- grpc_channel_args_destroy (*a);
- *a = result;
+ } else {
+ /* create a new arg */
+ grpc_arg tmp;
+ tmp.type = GRPC_ARG_INTEGER;
+ tmp.key = GRPC_COMPRESSION_ALGORITHM_STATE_ARG;
+ /* all enabled by default */
+ tmp.value.integer = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
+ if (state != 0) {
+ GPR_BITSET((unsigned *)&tmp.value.integer, algorithm);
+ } else {
+ GPR_BITCLEAR((unsigned *)&tmp.value.integer, algorithm);
}
+ result = grpc_channel_args_copy_and_add(*a, &tmp, 1);
+ grpc_channel_args_destroy(*a);
+ *a = result;
+ }
return result;
}
-int
-grpc_channel_args_compression_algorithm_get_states (const grpc_channel_args * a)
-{
+int grpc_channel_args_compression_algorithm_get_states(
+ const grpc_channel_args *a) {
int *states_arg;
- if (find_compression_algorithm_states_bitset (a, &states_arg))
- {
- return *states_arg;
- }
- else
- {
- return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */
- }
+ if (find_compression_algorithm_states_bitset(a, &states_arg)) {
+ return *states_arg;
+ } else {
+ return (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; /* All algs. enabled */
+ }
}
diff --git a/src/core/channel/channel_args.h b/src/core/channel/channel_args.h
index bcfd8c46a0..f9e7b05860 100644
--- a/src/core/channel/channel_args.h
+++ b/src/core/channel/channel_args.h
@@ -38,29 +38,34 @@
#include <grpc/grpc.h>
/* Copy some arguments */
-grpc_channel_args *grpc_channel_args_copy (const grpc_channel_args * src);
+grpc_channel_args *grpc_channel_args_copy(const grpc_channel_args *src);
/** Copy some arguments and add the to_add parameter in the end.
If to_add is NULL, it is equivalent to call grpc_channel_args_copy. */
-grpc_channel_args *grpc_channel_args_copy_and_add (const grpc_channel_args * src, const grpc_arg * to_add, size_t num_to_add);
+grpc_channel_args *grpc_channel_args_copy_and_add(const grpc_channel_args *src,
+ const grpc_arg *to_add,
+ size_t num_to_add);
/** Copy args from a then args from b into a new channel args */
-grpc_channel_args *grpc_channel_args_merge (const grpc_channel_args * a, const grpc_channel_args * b);
+grpc_channel_args *grpc_channel_args_merge(const grpc_channel_args *a,
+ const grpc_channel_args *b);
/** Destroy arguments created by grpc_channel_args_copy */
-void grpc_channel_args_destroy (grpc_channel_args * a);
+void grpc_channel_args_destroy(grpc_channel_args *a);
/** Reads census_enabled settings from channel args. Returns 1 if census_enabled
* is specified in channel args, otherwise returns 0. */
-int grpc_channel_args_is_census_enabled (const grpc_channel_args * a);
+int grpc_channel_args_is_census_enabled(const grpc_channel_args *a);
/** Returns the compression algorithm set in \a a. */
-grpc_compression_algorithm grpc_channel_args_get_compression_algorithm (const grpc_channel_args * a);
+grpc_compression_algorithm grpc_channel_args_get_compression_algorithm(
+ const grpc_channel_args *a);
/** Returns a channel arg instance with compression enabled. If \a a is
* non-NULL, its args are copied. N.B. GRPC_COMPRESS_NONE disables compression
* for the channel. */
-grpc_channel_args *grpc_channel_args_set_compression_algorithm (grpc_channel_args * a, grpc_compression_algorithm algorithm);
+grpc_channel_args *grpc_channel_args_set_compression_algorithm(
+ grpc_channel_args *a, grpc_compression_algorithm algorithm);
/** Sets the support for the given compression algorithm. By default, all
* compression algorithms are enabled. It's an error to disable an algorithm set
@@ -69,13 +74,15 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm (grpc_channel_arg
* Returns an instance will the updated algorithm states. The \a a pointer is
* modified to point to the returned instance (which may be different from the
* input value of \a a). */
-grpc_channel_args *grpc_channel_args_compression_algorithm_set_state (grpc_channel_args ** a, grpc_compression_algorithm algorithm, int enabled);
+grpc_channel_args *grpc_channel_args_compression_algorithm_set_state(
+ grpc_channel_args **a, grpc_compression_algorithm algorithm, int enabled);
/** Returns the bitset representing the support state (true for enabled, false
* for disabled) for compression algorithms.
*
* The i-th bit of the returned bitset corresponds to the i-th entry in the
* grpc_compression_algorithm enum. */
-int grpc_channel_args_compression_algorithm_get_states (const grpc_channel_args * a);
+int grpc_channel_args_compression_algorithm_get_states(
+ const grpc_channel_args *a);
#endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H */
diff --git a/src/core/channel/channel_stack.c b/src/core/channel/channel_stack.c
index 67ab2c535f..abd7f719e7 100644
--- a/src/core/channel/channel_stack.c
+++ b/src/core/channel/channel_stack.c
@@ -59,20 +59,21 @@ int grpc_trace_channel = 0;
#define ROUND_UP_TO_ALIGNMENT_SIZE(x) \
(((x) + GPR_MAX_ALIGNMENT - 1u) & ~(GPR_MAX_ALIGNMENT - 1u))
-size_t
-grpc_channel_stack_size (const grpc_channel_filter ** filters, size_t filter_count)
-{
+size_t grpc_channel_stack_size(const grpc_channel_filter **filters,
+ size_t filter_count) {
/* always need the header, and size for the channel elements */
- size_t size = ROUND_UP_TO_ALIGNMENT_SIZE (sizeof (grpc_channel_stack)) + ROUND_UP_TO_ALIGNMENT_SIZE (filter_count * sizeof (grpc_channel_element));
+ size_t size =
+ ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(grpc_channel_stack)) +
+ ROUND_UP_TO_ALIGNMENT_SIZE(filter_count * sizeof(grpc_channel_element));
size_t i;
- GPR_ASSERT ((GPR_MAX_ALIGNMENT & (GPR_MAX_ALIGNMENT - 1)) == 0 && "GPR_MAX_ALIGNMENT must be a power of two");
+ GPR_ASSERT((GPR_MAX_ALIGNMENT & (GPR_MAX_ALIGNMENT - 1)) == 0 &&
+ "GPR_MAX_ALIGNMENT must be a power of two");
/* add the size for each filter */
- for (i = 0; i < filter_count; i++)
- {
- size += ROUND_UP_TO_ALIGNMENT_SIZE (filters[i]->sizeof_channel_data);
- }
+ for (i = 0; i < filter_count; i++) {
+ size += ROUND_UP_TO_ALIGNMENT_SIZE(filters[i]->sizeof_channel_data);
+ }
return size;
}
@@ -85,142 +86,142 @@ grpc_channel_stack_size (const grpc_channel_filter ** filters, size_t filter_cou
((grpc_call_element *)((char *)(stk) + \
ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(grpc_call_stack))))
-grpc_channel_element *
-grpc_channel_stack_element (grpc_channel_stack * channel_stack, size_t index)
-{
- return CHANNEL_ELEMS_FROM_STACK (channel_stack) + index;
+grpc_channel_element *grpc_channel_stack_element(
+ grpc_channel_stack *channel_stack, size_t index) {
+ return CHANNEL_ELEMS_FROM_STACK(channel_stack) + index;
}
-grpc_channel_element *
-grpc_channel_stack_last_element (grpc_channel_stack * channel_stack)
-{
- return grpc_channel_stack_element (channel_stack, channel_stack->count - 1);
+grpc_channel_element *grpc_channel_stack_last_element(
+ grpc_channel_stack *channel_stack) {
+ return grpc_channel_stack_element(channel_stack, channel_stack->count - 1);
}
-grpc_call_element *
-grpc_call_stack_element (grpc_call_stack * call_stack, size_t index)
-{
- return CALL_ELEMS_FROM_STACK (call_stack) + index;
+grpc_call_element *grpc_call_stack_element(grpc_call_stack *call_stack,
+ size_t index) {
+ return CALL_ELEMS_FROM_STACK(call_stack) + index;
}
-void
-grpc_channel_stack_init (grpc_exec_ctx * exec_ctx, const grpc_channel_filter ** filters, size_t filter_count, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, grpc_channel_stack * stack)
-{
- size_t call_size = ROUND_UP_TO_ALIGNMENT_SIZE (sizeof (grpc_call_stack)) + ROUND_UP_TO_ALIGNMENT_SIZE (filter_count * sizeof (grpc_call_element));
+void grpc_channel_stack_init(grpc_exec_ctx *exec_ctx,
+ const grpc_channel_filter **filters,
+ size_t filter_count, grpc_channel *master,
+ const grpc_channel_args *args,
+ grpc_mdctx *metadata_context,
+ grpc_channel_stack *stack) {
+ size_t call_size =
+ ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(grpc_call_stack)) +
+ ROUND_UP_TO_ALIGNMENT_SIZE(filter_count * sizeof(grpc_call_element));
grpc_channel_element *elems;
char *user_data;
size_t i;
stack->count = filter_count;
- elems = CHANNEL_ELEMS_FROM_STACK (stack);
- user_data = ((char *) elems) + ROUND_UP_TO_ALIGNMENT_SIZE (filter_count * sizeof (grpc_channel_element));
+ elems = CHANNEL_ELEMS_FROM_STACK(stack);
+ user_data =
+ ((char *)elems) +
+ ROUND_UP_TO_ALIGNMENT_SIZE(filter_count * sizeof(grpc_channel_element));
/* init per-filter data */
- for (i = 0; i < filter_count; i++)
- {
- elems[i].filter = filters[i];
- elems[i].channel_data = user_data;
- elems[i].filter->init_channel_elem (exec_ctx, &elems[i], master, args, metadata_context, i == 0, i == (filter_count - 1));
- user_data += ROUND_UP_TO_ALIGNMENT_SIZE (filters[i]->sizeof_channel_data);
- call_size += ROUND_UP_TO_ALIGNMENT_SIZE (filters[i]->sizeof_call_data);
- }
-
- GPR_ASSERT (user_data > (char *) stack);
- GPR_ASSERT ((gpr_uintptr) (user_data - (char *) stack) == grpc_channel_stack_size (filters, filter_count));
+ for (i = 0; i < filter_count; i++) {
+ elems[i].filter = filters[i];
+ elems[i].channel_data = user_data;
+ elems[i].filter->init_channel_elem(exec_ctx, &elems[i], master, args,
+ metadata_context, i == 0,
+ i == (filter_count - 1));
+ user_data += ROUND_UP_TO_ALIGNMENT_SIZE(filters[i]->sizeof_channel_data);
+ call_size += ROUND_UP_TO_ALIGNMENT_SIZE(filters[i]->sizeof_call_data);
+ }
+
+ GPR_ASSERT(user_data > (char *)stack);
+ GPR_ASSERT((gpr_uintptr)(user_data - (char *)stack) ==
+ grpc_channel_stack_size(filters, filter_count));
stack->call_stack_size = call_size;
}
-void
-grpc_channel_stack_destroy (grpc_exec_ctx * exec_ctx, grpc_channel_stack * stack)
-{
- grpc_channel_element *channel_elems = CHANNEL_ELEMS_FROM_STACK (stack);
+void grpc_channel_stack_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_channel_stack *stack) {
+ grpc_channel_element *channel_elems = CHANNEL_ELEMS_FROM_STACK(stack);
size_t count = stack->count;
size_t i;
/* destroy per-filter data */
- for (i = 0; i < count; i++)
- {
- channel_elems[i].filter->destroy_channel_elem (exec_ctx, &channel_elems[i]);
- }
+ for (i = 0; i < count; i++) {
+ channel_elems[i].filter->destroy_channel_elem(exec_ctx, &channel_elems[i]);
+ }
}
-void
-grpc_call_stack_init (grpc_exec_ctx * exec_ctx, grpc_channel_stack * channel_stack, const void *transport_server_data, grpc_transport_stream_op * initial_op, grpc_call_stack * call_stack)
-{
- grpc_channel_element *channel_elems = CHANNEL_ELEMS_FROM_STACK (channel_stack);
+void grpc_call_stack_init(grpc_exec_ctx *exec_ctx,
+ grpc_channel_stack *channel_stack,
+ const void *transport_server_data,
+ grpc_transport_stream_op *initial_op,
+ grpc_call_stack *call_stack) {
+ grpc_channel_element *channel_elems = CHANNEL_ELEMS_FROM_STACK(channel_stack);
size_t count = channel_stack->count;
grpc_call_element *call_elems;
char *user_data;
size_t i;
call_stack->count = count;
- call_elems = CALL_ELEMS_FROM_STACK (call_stack);
- user_data = ((char *) call_elems) + ROUND_UP_TO_ALIGNMENT_SIZE (count * sizeof (grpc_call_element));
+ call_elems = CALL_ELEMS_FROM_STACK(call_stack);
+ user_data = ((char *)call_elems) +
+ ROUND_UP_TO_ALIGNMENT_SIZE(count * sizeof(grpc_call_element));
/* init per-filter data */
- for (i = 0; i < count; i++)
- {
- call_elems[i].filter = channel_elems[i].filter;
- call_elems[i].channel_data = channel_elems[i].channel_data;
- call_elems[i].call_data = user_data;
- call_elems[i].filter->init_call_elem (exec_ctx, &call_elems[i], transport_server_data, initial_op);
- user_data += ROUND_UP_TO_ALIGNMENT_SIZE (call_elems[i].filter->sizeof_call_data);
- }
+ for (i = 0; i < count; i++) {
+ call_elems[i].filter = channel_elems[i].filter;
+ call_elems[i].channel_data = channel_elems[i].channel_data;
+ call_elems[i].call_data = user_data;
+ call_elems[i].filter->init_call_elem(exec_ctx, &call_elems[i],
+ transport_server_data, initial_op);
+ user_data +=
+ ROUND_UP_TO_ALIGNMENT_SIZE(call_elems[i].filter->sizeof_call_data);
+ }
}
-void
-grpc_call_stack_destroy (grpc_exec_ctx * exec_ctx, grpc_call_stack * stack)
-{
- grpc_call_element *elems = CALL_ELEMS_FROM_STACK (stack);
+void grpc_call_stack_destroy(grpc_exec_ctx *exec_ctx, grpc_call_stack *stack) {
+ grpc_call_element *elems = CALL_ELEMS_FROM_STACK(stack);
size_t count = stack->count;
size_t i;
/* destroy per-filter data */
- for (i = 0; i < count; i++)
- {
- elems[i].filter->destroy_call_elem (exec_ctx, &elems[i]);
- }
+ for (i = 0; i < count; i++) {
+ elems[i].filter->destroy_call_elem(exec_ctx, &elems[i]);
+ }
}
-void
-grpc_call_next_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+void grpc_call_next_op(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
grpc_call_element *next_elem = elem + 1;
- next_elem->filter->start_transport_stream_op (exec_ctx, next_elem, op);
+ next_elem->filter->start_transport_stream_op(exec_ctx, next_elem, op);
}
-char *
-grpc_call_next_get_peer (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+char *grpc_call_next_get_peer(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
grpc_call_element *next_elem = elem + 1;
- return next_elem->filter->get_peer (exec_ctx, next_elem);
+ return next_elem->filter->get_peer(exec_ctx, next_elem);
}
-void
-grpc_channel_next_op (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op)
-{
+void grpc_channel_next_op(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
+ grpc_transport_op *op) {
grpc_channel_element *next_elem = elem + 1;
- next_elem->filter->start_transport_op (exec_ctx, next_elem, op);
+ next_elem->filter->start_transport_op(exec_ctx, next_elem, op);
}
-grpc_channel_stack *
-grpc_channel_stack_from_top_element (grpc_channel_element * elem)
-{
- return (grpc_channel_stack *) ((char *) (elem) - ROUND_UP_TO_ALIGNMENT_SIZE (sizeof (grpc_channel_stack)));
+grpc_channel_stack *grpc_channel_stack_from_top_element(
+ grpc_channel_element *elem) {
+ return (grpc_channel_stack *)((char *)(elem)-ROUND_UP_TO_ALIGNMENT_SIZE(
+ sizeof(grpc_channel_stack)));
}
-grpc_call_stack *
-grpc_call_stack_from_top_element (grpc_call_element * elem)
-{
- return (grpc_call_stack *) ((char *) (elem) - ROUND_UP_TO_ALIGNMENT_SIZE (sizeof (grpc_call_stack)));
+grpc_call_stack *grpc_call_stack_from_top_element(grpc_call_element *elem) {
+ return (grpc_call_stack *)((char *)(elem)-ROUND_UP_TO_ALIGNMENT_SIZE(
+ sizeof(grpc_call_stack)));
}
-void
-grpc_call_element_send_cancel (grpc_exec_ctx * exec_ctx, grpc_call_element * cur_elem)
-{
+void grpc_call_element_send_cancel(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *cur_elem) {
grpc_transport_stream_op op;
- memset (&op, 0, sizeof (op));
+ memset(&op, 0, sizeof(op));
op.cancel_with_status = GRPC_STATUS_CANCELLED;
- grpc_call_next_op (exec_ctx, cur_elem, &op);
+ grpc_call_next_op(exec_ctx, cur_elem, &op);
}
diff --git a/src/core/channel/channel_stack.h b/src/core/channel/channel_stack.h
index 0bfe326976..6732cc3018 100644
--- a/src/core/channel/channel_stack.h
+++ b/src/core/channel/channel_stack.h
@@ -61,15 +61,17 @@ typedef struct grpc_call_element grpc_call_element;
4. a name, which is useful when debugging
Members are laid out in approximate frequency of use order. */
-typedef struct
-{
+typedef struct {
/* Called to eg. send/receive data on a call.
See grpc_call_next_op on how to call the next element in the stack */
- void (*start_transport_stream_op) (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op);
+ void (*start_transport_stream_op)(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op);
/* Called to handle channel level operations - e.g. new calls, or transport
closure.
See grpc_channel_next_op on how to call the next element in the stack */
- void (*start_transport_op) (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op);
+ void (*start_transport_op)(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_transport_op *op);
/* sizeof(per call data) */
size_t sizeof_call_data;
@@ -81,10 +83,12 @@ typedef struct
on a client; if it is non-NULL, then it points to memory owned by the
transport and is on the server. Most filters want to ignore this
argument. */
- void (*init_call_elem) (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op);
+ void (*init_call_elem)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op);
/* Destroy per call data.
The filter does not need to do any chaining */
- void (*destroy_call_elem) (grpc_exec_ctx * exec_ctx, grpc_call_element * elem);
+ void (*destroy_call_elem)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem);
/* sizeof(per channel data) */
size_t sizeof_channel_data;
@@ -94,13 +98,17 @@ typedef struct
is_first, is_last designate this elements position in the stack, and are
useful for asserting correct configuration by upper layer code.
The filter does not need to do any chaining */
- void (*init_channel_elem) (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, int is_first, int is_last);
+ void (*init_channel_elem)(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
+ grpc_channel *master, const grpc_channel_args *args,
+ grpc_mdctx *metadata_context, int is_first,
+ int is_last);
/* Destroy per channel data.
The filter does not need to do any chaining */
- void (*destroy_channel_elem) (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem);
+ void (*destroy_channel_elem)(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem);
/* Implement grpc_call_get_peer() */
- char *(*get_peer) (grpc_exec_ctx * exec_ctx, grpc_call_element * elem);
+ char *(*get_peer)(grpc_exec_ctx *exec_ctx, grpc_call_element *elem);
/* The name of this filter */
const char *name;
@@ -108,8 +116,7 @@ typedef struct
/* A channel_element tracks its filter and the filter requested memory within
a channel allocation */
-struct grpc_channel_element
-{
+struct grpc_channel_element {
const grpc_channel_filter *filter;
void *channel_data;
};
@@ -117,8 +124,7 @@ struct grpc_channel_element
/* A call_element tracks its filter, the filter requested memory within
a channel allocation, and the filter requested memory within a call
allocation */
-struct grpc_call_element
-{
+struct grpc_call_element {
const grpc_channel_filter *filter;
void *channel_data;
void *call_data;
@@ -126,8 +132,7 @@ struct grpc_call_element
/* A channel stack tracks a set of related filters for one channel, and
guarantees they live within a single malloc() allocation */
-typedef struct
-{
+typedef struct {
size_t count;
/* Memory required for a call stack (computed at channel stack
initialization) */
@@ -136,48 +141,63 @@ typedef struct
/* A call stack tracks a set of related filters for one call, and guarantees
they live within a single malloc() allocation */
-typedef struct
-{
- size_t count;
-} grpc_call_stack;
+typedef struct { size_t count; } grpc_call_stack;
/* Get a channel element given a channel stack and its index */
-grpc_channel_element *grpc_channel_stack_element (grpc_channel_stack * stack, size_t i);
+grpc_channel_element *grpc_channel_stack_element(grpc_channel_stack *stack,
+ size_t i);
/* Get the last channel element in a channel stack */
-grpc_channel_element *grpc_channel_stack_last_element (grpc_channel_stack * stack);
+grpc_channel_element *grpc_channel_stack_last_element(
+ grpc_channel_stack *stack);
/* Get a call stack element given a call stack and an index */
-grpc_call_element *grpc_call_stack_element (grpc_call_stack * stack, size_t i);
+grpc_call_element *grpc_call_stack_element(grpc_call_stack *stack, size_t i);
/* Determine memory required for a channel stack containing a set of filters */
-size_t grpc_channel_stack_size (const grpc_channel_filter ** filters, size_t filter_count);
+size_t grpc_channel_stack_size(const grpc_channel_filter **filters,
+ size_t filter_count);
/* Initialize a channel stack given some filters */
-void grpc_channel_stack_init (grpc_exec_ctx * exec_ctx, const grpc_channel_filter ** filters, size_t filter_count, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, grpc_channel_stack * stack);
+void grpc_channel_stack_init(grpc_exec_ctx *exec_ctx,
+ const grpc_channel_filter **filters,
+ size_t filter_count, grpc_channel *master,
+ const grpc_channel_args *args,
+ grpc_mdctx *metadata_context,
+ grpc_channel_stack *stack);
/* Destroy a channel stack */
-void grpc_channel_stack_destroy (grpc_exec_ctx * exec_ctx, grpc_channel_stack * stack);
+void grpc_channel_stack_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_channel_stack *stack);
/* Initialize a call stack given a channel stack. transport_server_data is
expected to be NULL on a client, or an opaque transport owned pointer on the
server. */
-void grpc_call_stack_init (grpc_exec_ctx * exec_ctx, grpc_channel_stack * channel_stack, const void *transport_server_data, grpc_transport_stream_op * initial_op, grpc_call_stack * call_stack);
+void grpc_call_stack_init(grpc_exec_ctx *exec_ctx,
+ grpc_channel_stack *channel_stack,
+ const void *transport_server_data,
+ grpc_transport_stream_op *initial_op,
+ grpc_call_stack *call_stack);
/* Destroy a call stack */
-void grpc_call_stack_destroy (grpc_exec_ctx * exec_ctx, grpc_call_stack * stack);
+void grpc_call_stack_destroy(grpc_exec_ctx *exec_ctx, grpc_call_stack *stack);
/* Call the next operation in a call stack */
-void grpc_call_next_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op);
+void grpc_call_next_op(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ grpc_transport_stream_op *op);
/* Call the next operation (depending on call directionality) in a channel
stack */
-void grpc_channel_next_op (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op);
+void grpc_channel_next_op(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
+ grpc_transport_op *op);
/* Pass through a request to get_peer to the next child element */
-char *grpc_call_next_get_peer (grpc_exec_ctx * exec_ctx, grpc_call_element * elem);
+char *grpc_call_next_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem);
/* Given the top element of a channel stack, get the channel stack itself */
-grpc_channel_stack *grpc_channel_stack_from_top_element (grpc_channel_element * elem);
+grpc_channel_stack *grpc_channel_stack_from_top_element(
+ grpc_channel_element *elem);
/* Given the top element of a call stack, get the call stack itself */
-grpc_call_stack *grpc_call_stack_from_top_element (grpc_call_element * elem);
+grpc_call_stack *grpc_call_stack_from_top_element(grpc_call_element *elem);
-void grpc_call_log_op (char *file, int line, gpr_log_severity severity, grpc_call_element * elem, grpc_transport_stream_op * op);
+void grpc_call_log_op(char *file, int line, gpr_log_severity severity,
+ grpc_call_element *elem, grpc_transport_stream_op *op);
-void grpc_call_element_send_cancel (grpc_exec_ctx * exec_ctx, grpc_call_element * cur_elem);
+void grpc_call_element_send_cancel(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *cur_elem);
extern int grpc_trace_channel;
diff --git a/src/core/channel/client_channel.c b/src/core/channel/client_channel.c
index 97f097d301..ce03ad5eb0 100644
--- a/src/core/channel/client_channel.c
+++ b/src/core/channel/client_channel.c
@@ -51,8 +51,7 @@
typedef struct call_data call_data;
-typedef struct
-{
+typedef struct {
/** metadata context for this channel */
grpc_mdctx *mdctx;
/** resolver for this channel */
@@ -90,16 +89,14 @@ typedef struct
to watch for state changes from the lb_policy. When a state change is seen,
we
update the channel, and create a new watcher */
-typedef struct
-{
+typedef struct {
channel_data *chand;
grpc_closure on_changed;
grpc_connectivity_state state;
grpc_lb_policy *lb_policy;
} lb_policy_connectivity_watcher;
-typedef enum
-{
+typedef enum {
CALL_CREATED,
CALL_WAITING_FOR_SEND,
CALL_WAITING_FOR_CONFIG,
@@ -109,8 +106,7 @@ typedef enum
CALL_CANCELLED
} call_state;
-struct call_data
-{
+struct call_data {
/* owning element */
grpc_call_element *elem;
@@ -127,406 +123,361 @@ struct call_data
grpc_linked_mdelem details;
};
-static grpc_closure *
-merge_into_waiting_op (grpc_call_element * elem, grpc_transport_stream_op * new_op)
- GRPC_MUST_USE_RESULT;
+static grpc_closure *merge_into_waiting_op(grpc_call_element *elem,
+ grpc_transport_stream_op *new_op)
+ GRPC_MUST_USE_RESULT;
- static void handle_op_after_cancellation (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void handle_op_after_cancellation(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- if (op->send_ops)
- {
- grpc_stream_ops_unref_owned_objects (op->send_ops->ops, op->send_ops->nops);
- op->on_done_send->cb (exec_ctx, op->on_done_send->cb_arg, 0);
- }
- if (op->recv_ops)
- {
- char status[GPR_LTOA_MIN_BUFSIZE];
- grpc_metadata_batch mdb;
- gpr_ltoa (GRPC_STATUS_CANCELLED, status);
- calld->status.md = grpc_mdelem_from_strings (chand->mdctx, "grpc-status", status);
- calld->details.md = grpc_mdelem_from_strings (chand->mdctx, "grpc-message", "Cancelled");
- calld->status.prev = calld->details.next = NULL;
- calld->status.next = &calld->details;
- calld->details.prev = &calld->status;
- mdb.list.head = &calld->status;
- mdb.list.tail = &calld->details;
- mdb.garbage.head = mdb.garbage.tail = NULL;
- mdb.deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
- grpc_sopb_add_metadata (op->recv_ops, mdb);
- *op->recv_state = GRPC_STREAM_CLOSED;
- op->on_done_recv->cb (exec_ctx, op->on_done_recv->cb_arg, 1);
- }
- if (op->on_consumed)
- {
- op->on_consumed->cb (exec_ctx, op->on_consumed->cb_arg, 0);
- }
+ if (op->send_ops) {
+ grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops);
+ op->on_done_send->cb(exec_ctx, op->on_done_send->cb_arg, 0);
+ }
+ if (op->recv_ops) {
+ char status[GPR_LTOA_MIN_BUFSIZE];
+ grpc_metadata_batch mdb;
+ gpr_ltoa(GRPC_STATUS_CANCELLED, status);
+ calld->status.md =
+ grpc_mdelem_from_strings(chand->mdctx, "grpc-status", status);
+ calld->details.md =
+ grpc_mdelem_from_strings(chand->mdctx, "grpc-message", "Cancelled");
+ calld->status.prev = calld->details.next = NULL;
+ calld->status.next = &calld->details;
+ calld->details.prev = &calld->status;
+ mdb.list.head = &calld->status;
+ mdb.list.tail = &calld->details;
+ mdb.garbage.head = mdb.garbage.tail = NULL;
+ mdb.deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
+ grpc_sopb_add_metadata(op->recv_ops, mdb);
+ *op->recv_state = GRPC_STREAM_CLOSED;
+ op->on_done_recv->cb(exec_ctx, op->on_done_recv->cb_arg, 1);
+ }
+ if (op->on_consumed) {
+ op->on_consumed->cb(exec_ctx, op->on_consumed->cb_arg, 0);
+ }
}
-typedef struct
-{
+typedef struct {
grpc_closure closure;
grpc_call_element *elem;
} waiting_call;
-static void perform_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op, int continuation);
+static void perform_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op,
+ int continuation);
-static void
-continue_with_pick (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void continue_with_pick(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
waiting_call *wc = arg;
call_data *calld = wc->elem->call_data;
- perform_transport_stream_op (exec_ctx, wc->elem, &calld->waiting_op, 1);
- gpr_free (wc);
+ perform_transport_stream_op(exec_ctx, wc->elem, &calld->waiting_op, 1);
+ gpr_free(wc);
}
-static void
-add_to_lb_policy_wait_queue_locked_state_config (grpc_call_element * elem)
-{
+static void add_to_lb_policy_wait_queue_locked_state_config(
+ grpc_call_element *elem) {
channel_data *chand = elem->channel_data;
- waiting_call *wc = gpr_malloc (sizeof (*wc));
- grpc_closure_init (&wc->closure, continue_with_pick, wc);
+ waiting_call *wc = gpr_malloc(sizeof(*wc));
+ grpc_closure_init(&wc->closure, continue_with_pick, wc);
wc->elem = elem;
- grpc_closure_list_add (&chand->waiting_for_config_closures, &wc->closure, 1);
+ grpc_closure_list_add(&chand->waiting_for_config_closures, &wc->closure, 1);
}
-static int
-is_empty (void *p, int len)
-{
+static int is_empty(void *p, int len) {
char *ptr = p;
int i;
- for (i = 0; i < len; i++)
- {
- if (ptr[i] != 0)
- return 0;
- }
+ for (i = 0; i < len; i++) {
+ if (ptr[i] != 0) return 0;
+ }
return 1;
}
-static void
-started_call (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void started_call(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
call_data *calld = arg;
grpc_transport_stream_op op;
int have_waiting;
- gpr_mu_lock (&calld->mu_state);
- if (calld->state == CALL_CANCELLED && calld->subchannel_call != NULL)
- {
- memset (&op, 0, sizeof (op));
- op.cancel_with_status = GRPC_STATUS_CANCELLED;
- gpr_mu_unlock (&calld->mu_state);
- grpc_subchannel_call_process_op (exec_ctx, calld->subchannel_call, &op);
- }
- else if (calld->state == CALL_WAITING_FOR_CALL)
- {
- have_waiting = !is_empty (&calld->waiting_op, sizeof (calld->waiting_op));
- if (calld->subchannel_call != NULL)
- {
- calld->state = CALL_ACTIVE;
- gpr_mu_unlock (&calld->mu_state);
- if (have_waiting)
- {
- grpc_subchannel_call_process_op (exec_ctx, calld->subchannel_call, &calld->waiting_op);
- }
- }
- else
- {
- calld->state = CALL_CANCELLED;
- gpr_mu_unlock (&calld->mu_state);
- if (have_waiting)
- {
- handle_op_after_cancellation (exec_ctx, calld->elem, &calld->waiting_op);
- }
- }
- }
- else
- {
- GPR_ASSERT (calld->state == CALL_CANCELLED);
- gpr_mu_unlock (&calld->mu_state);
- }
+ gpr_mu_lock(&calld->mu_state);
+ if (calld->state == CALL_CANCELLED && calld->subchannel_call != NULL) {
+ memset(&op, 0, sizeof(op));
+ op.cancel_with_status = GRPC_STATUS_CANCELLED;
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, &op);
+ } else if (calld->state == CALL_WAITING_FOR_CALL) {
+ have_waiting = !is_empty(&calld->waiting_op, sizeof(calld->waiting_op));
+ if (calld->subchannel_call != NULL) {
+ calld->state = CALL_ACTIVE;
+ gpr_mu_unlock(&calld->mu_state);
+ if (have_waiting) {
+ grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call,
+ &calld->waiting_op);
+ }
+ } else {
+ calld->state = CALL_CANCELLED;
+ gpr_mu_unlock(&calld->mu_state);
+ if (have_waiting) {
+ handle_op_after_cancellation(exec_ctx, calld->elem, &calld->waiting_op);
+ }
+ }
+ } else {
+ GPR_ASSERT(calld->state == CALL_CANCELLED);
+ gpr_mu_unlock(&calld->mu_state);
+ }
}
-static void
-picked_target (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void picked_target(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
call_data *calld = arg;
grpc_pollset *pollset;
- if (calld->picked_channel == NULL)
- {
- /* treat this like a cancellation */
- calld->waiting_op.cancel_with_status = GRPC_STATUS_UNAVAILABLE;
- perform_transport_stream_op (exec_ctx, calld->elem, &calld->waiting_op, 1);
- }
- else
- {
- gpr_mu_lock (&calld->mu_state);
- if (calld->state == CALL_CANCELLED)
- {
- gpr_mu_unlock (&calld->mu_state);
- handle_op_after_cancellation (exec_ctx, calld->elem, &calld->waiting_op);
- }
- else
- {
- GPR_ASSERT (calld->state == CALL_WAITING_FOR_PICK);
- calld->state = CALL_WAITING_FOR_CALL;
- pollset = calld->waiting_op.bind_pollset;
- gpr_mu_unlock (&calld->mu_state);
- grpc_closure_init (&calld->async_setup_task, started_call, calld);
- grpc_subchannel_create_call (exec_ctx, calld->picked_channel, pollset, &calld->subchannel_call, &calld->async_setup_task);
- }
- }
+ if (calld->picked_channel == NULL) {
+ /* treat this like a cancellation */
+ calld->waiting_op.cancel_with_status = GRPC_STATUS_UNAVAILABLE;
+ perform_transport_stream_op(exec_ctx, calld->elem, &calld->waiting_op, 1);
+ } else {
+ gpr_mu_lock(&calld->mu_state);
+ if (calld->state == CALL_CANCELLED) {
+ gpr_mu_unlock(&calld->mu_state);
+ handle_op_after_cancellation(exec_ctx, calld->elem, &calld->waiting_op);
+ } else {
+ GPR_ASSERT(calld->state == CALL_WAITING_FOR_PICK);
+ calld->state = CALL_WAITING_FOR_CALL;
+ pollset = calld->waiting_op.bind_pollset;
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_closure_init(&calld->async_setup_task, started_call, calld);
+ grpc_subchannel_create_call(exec_ctx, calld->picked_channel, pollset,
+ &calld->subchannel_call,
+ &calld->async_setup_task);
+ }
+ }
}
-static grpc_closure *
-merge_into_waiting_op (grpc_call_element * elem, grpc_transport_stream_op * new_op)
-{
+static grpc_closure *merge_into_waiting_op(grpc_call_element *elem,
+ grpc_transport_stream_op *new_op) {
call_data *calld = elem->call_data;
grpc_closure *consumed_op = NULL;
grpc_transport_stream_op *waiting_op = &calld->waiting_op;
- GPR_ASSERT ((waiting_op->send_ops != NULL) + (new_op->send_ops != NULL) <= 1);
- GPR_ASSERT ((waiting_op->recv_ops != NULL) + (new_op->recv_ops != NULL) <= 1);
- if (new_op->send_ops != NULL)
- {
- waiting_op->send_ops = new_op->send_ops;
- waiting_op->is_last_send = new_op->is_last_send;
- waiting_op->on_done_send = new_op->on_done_send;
- }
- if (new_op->recv_ops != NULL)
- {
- waiting_op->recv_ops = new_op->recv_ops;
- waiting_op->recv_state = new_op->recv_state;
- waiting_op->on_done_recv = new_op->on_done_recv;
- }
- if (new_op->on_consumed != NULL)
- {
- if (waiting_op->on_consumed != NULL)
- {
- consumed_op = waiting_op->on_consumed;
- }
- waiting_op->on_consumed = new_op->on_consumed;
- }
- if (new_op->cancel_with_status != GRPC_STATUS_OK)
- {
- waiting_op->cancel_with_status = new_op->cancel_with_status;
- }
+ GPR_ASSERT((waiting_op->send_ops != NULL) + (new_op->send_ops != NULL) <= 1);
+ GPR_ASSERT((waiting_op->recv_ops != NULL) + (new_op->recv_ops != NULL) <= 1);
+ if (new_op->send_ops != NULL) {
+ waiting_op->send_ops = new_op->send_ops;
+ waiting_op->is_last_send = new_op->is_last_send;
+ waiting_op->on_done_send = new_op->on_done_send;
+ }
+ if (new_op->recv_ops != NULL) {
+ waiting_op->recv_ops = new_op->recv_ops;
+ waiting_op->recv_state = new_op->recv_state;
+ waiting_op->on_done_recv = new_op->on_done_recv;
+ }
+ if (new_op->on_consumed != NULL) {
+ if (waiting_op->on_consumed != NULL) {
+ consumed_op = waiting_op->on_consumed;
+ }
+ waiting_op->on_consumed = new_op->on_consumed;
+ }
+ if (new_op->cancel_with_status != GRPC_STATUS_OK) {
+ waiting_op->cancel_with_status = new_op->cancel_with_status;
+ }
return consumed_op;
}
-static char *
-cc_get_peer (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
grpc_subchannel_call *subchannel_call;
char *result;
- gpr_mu_lock (&calld->mu_state);
- if (calld->state == CALL_ACTIVE)
- {
- subchannel_call = calld->subchannel_call;
- GRPC_SUBCHANNEL_CALL_REF (subchannel_call, "get_peer");
- gpr_mu_unlock (&calld->mu_state);
- result = grpc_subchannel_call_get_peer (exec_ctx, subchannel_call);
- GRPC_SUBCHANNEL_CALL_UNREF (exec_ctx, subchannel_call, "get_peer");
- return result;
- }
- else
- {
- gpr_mu_unlock (&calld->mu_state);
- return grpc_channel_get_target (chand->master);
- }
+ gpr_mu_lock(&calld->mu_state);
+ if (calld->state == CALL_ACTIVE) {
+ subchannel_call = calld->subchannel_call;
+ GRPC_SUBCHANNEL_CALL_REF(subchannel_call, "get_peer");
+ gpr_mu_unlock(&calld->mu_state);
+ result = grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
+ GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, subchannel_call, "get_peer");
+ return result;
+ } else {
+ gpr_mu_unlock(&calld->mu_state);
+ return grpc_channel_get_target(chand->master);
+ }
}
-static void
-perform_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op, int continuation)
-{
+static void perform_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op,
+ int continuation) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
grpc_subchannel_call *subchannel_call;
grpc_lb_policy *lb_policy;
grpc_transport_stream_op op2;
- GPR_ASSERT (elem->filter == &grpc_client_channel_filter);
- GRPC_CALL_LOG_OP (GPR_INFO, elem, op);
+ GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
+ GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
- gpr_mu_lock (&calld->mu_state);
- switch (calld->state)
- {
+ gpr_mu_lock(&calld->mu_state);
+ switch (calld->state) {
case CALL_ACTIVE:
- GPR_ASSERT (!continuation);
+ GPR_ASSERT(!continuation);
subchannel_call = calld->subchannel_call;
- gpr_mu_unlock (&calld->mu_state);
- grpc_subchannel_call_process_op (exec_ctx, subchannel_call, op);
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_subchannel_call_process_op(exec_ctx, subchannel_call, op);
break;
case CALL_CANCELLED:
- gpr_mu_unlock (&calld->mu_state);
- handle_op_after_cancellation (exec_ctx, elem, op);
+ gpr_mu_unlock(&calld->mu_state);
+ handle_op_after_cancellation(exec_ctx, elem, op);
break;
case CALL_WAITING_FOR_SEND:
- GPR_ASSERT (!continuation);
- grpc_exec_ctx_enqueue (exec_ctx, merge_into_waiting_op (elem, op), 1);
- if (!calld->waiting_op.send_ops && calld->waiting_op.cancel_with_status == GRPC_STATUS_OK)
- {
- gpr_mu_unlock (&calld->mu_state);
- break;
- }
+ GPR_ASSERT(!continuation);
+ grpc_exec_ctx_enqueue(exec_ctx, merge_into_waiting_op(elem, op), 1);
+ if (!calld->waiting_op.send_ops &&
+ calld->waiting_op.cancel_with_status == GRPC_STATUS_OK) {
+ gpr_mu_unlock(&calld->mu_state);
+ break;
+ }
*op = calld->waiting_op;
- memset (&calld->waiting_op, 0, sizeof (calld->waiting_op));
+ memset(&calld->waiting_op, 0, sizeof(calld->waiting_op));
continuation = 1;
- /* fall through */
+ /* fall through */
case CALL_WAITING_FOR_CONFIG:
case CALL_WAITING_FOR_PICK:
case CALL_WAITING_FOR_CALL:
- if (!continuation)
- {
- if (op->cancel_with_status != GRPC_STATUS_OK)
- {
- calld->state = CALL_CANCELLED;
- op2 = calld->waiting_op;
- memset (&calld->waiting_op, 0, sizeof (calld->waiting_op));
- if (op->on_consumed)
- {
- calld->waiting_op.on_consumed = op->on_consumed;
- op->on_consumed = NULL;
- }
- else if (op2.on_consumed)
- {
- calld->waiting_op.on_consumed = op2.on_consumed;
- op2.on_consumed = NULL;
- }
- gpr_mu_unlock (&calld->mu_state);
- handle_op_after_cancellation (exec_ctx, elem, op);
- handle_op_after_cancellation (exec_ctx, elem, &op2);
- }
- else
- {
- grpc_exec_ctx_enqueue (exec_ctx, merge_into_waiting_op (elem, op), 1);
- gpr_mu_unlock (&calld->mu_state);
- }
- break;
- }
- /* fall through */
+ if (!continuation) {
+ if (op->cancel_with_status != GRPC_STATUS_OK) {
+ calld->state = CALL_CANCELLED;
+ op2 = calld->waiting_op;
+ memset(&calld->waiting_op, 0, sizeof(calld->waiting_op));
+ if (op->on_consumed) {
+ calld->waiting_op.on_consumed = op->on_consumed;
+ op->on_consumed = NULL;
+ } else if (op2.on_consumed) {
+ calld->waiting_op.on_consumed = op2.on_consumed;
+ op2.on_consumed = NULL;
+ }
+ gpr_mu_unlock(&calld->mu_state);
+ handle_op_after_cancellation(exec_ctx, elem, op);
+ handle_op_after_cancellation(exec_ctx, elem, &op2);
+ } else {
+ grpc_exec_ctx_enqueue(exec_ctx, merge_into_waiting_op(elem, op), 1);
+ gpr_mu_unlock(&calld->mu_state);
+ }
+ break;
+ }
+ /* fall through */
case CALL_CREATED:
- if (op->cancel_with_status != GRPC_STATUS_OK)
- {
- calld->state = CALL_CANCELLED;
- gpr_mu_unlock (&calld->mu_state);
- handle_op_after_cancellation (exec_ctx, elem, op);
- }
- else
- {
- calld->waiting_op = *op;
-
- if (op->send_ops == NULL)
- {
- /* need to have some send ops before we can select the
- lb target */
- calld->state = CALL_WAITING_FOR_SEND;
- gpr_mu_unlock (&calld->mu_state);
- }
- else
- {
- gpr_mu_lock (&chand->mu_config);
- lb_policy = chand->lb_policy;
- if (lb_policy)
- {
- grpc_transport_stream_op *op = &calld->waiting_op;
- grpc_pollset *bind_pollset = op->bind_pollset;
- grpc_metadata_batch *initial_metadata = &op->send_ops->ops[0].data.metadata;
- GRPC_LB_POLICY_REF (lb_policy, "pick");
- gpr_mu_unlock (&chand->mu_config);
- calld->state = CALL_WAITING_FOR_PICK;
-
- GPR_ASSERT (op->bind_pollset);
- GPR_ASSERT (op->send_ops);
- GPR_ASSERT (op->send_ops->nops >= 1);
- GPR_ASSERT (op->send_ops->ops[0].type == GRPC_OP_METADATA);
- gpr_mu_unlock (&calld->mu_state);
-
- grpc_closure_init (&calld->async_setup_task, picked_target, calld);
- grpc_lb_policy_pick (exec_ctx, lb_policy, bind_pollset, initial_metadata, &calld->picked_channel, &calld->async_setup_task);
-
- GRPC_LB_POLICY_UNREF (exec_ctx, lb_policy, "pick");
- }
- else if (chand->resolver != NULL)
- {
- calld->state = CALL_WAITING_FOR_CONFIG;
- add_to_lb_policy_wait_queue_locked_state_config (elem);
- if (!chand->started_resolving && chand->resolver != NULL)
- {
- GRPC_CHANNEL_INTERNAL_REF (chand->master, "resolver");
- chand->started_resolving = 1;
- grpc_resolver_next (exec_ctx, chand->resolver, &chand->incoming_configuration, &chand->on_config_changed);
- }
- gpr_mu_unlock (&chand->mu_config);
- gpr_mu_unlock (&calld->mu_state);
- }
- else
- {
- calld->state = CALL_CANCELLED;
- gpr_mu_unlock (&chand->mu_config);
- gpr_mu_unlock (&calld->mu_state);
- handle_op_after_cancellation (exec_ctx, elem, op);
- }
- }
- }
+ if (op->cancel_with_status != GRPC_STATUS_OK) {
+ calld->state = CALL_CANCELLED;
+ gpr_mu_unlock(&calld->mu_state);
+ handle_op_after_cancellation(exec_ctx, elem, op);
+ } else {
+ calld->waiting_op = *op;
+
+ if (op->send_ops == NULL) {
+ /* need to have some send ops before we can select the
+ lb target */
+ calld->state = CALL_WAITING_FOR_SEND;
+ gpr_mu_unlock(&calld->mu_state);
+ } else {
+ gpr_mu_lock(&chand->mu_config);
+ lb_policy = chand->lb_policy;
+ if (lb_policy) {
+ grpc_transport_stream_op *op = &calld->waiting_op;
+ grpc_pollset *bind_pollset = op->bind_pollset;
+ grpc_metadata_batch *initial_metadata =
+ &op->send_ops->ops[0].data.metadata;
+ GRPC_LB_POLICY_REF(lb_policy, "pick");
+ gpr_mu_unlock(&chand->mu_config);
+ calld->state = CALL_WAITING_FOR_PICK;
+
+ GPR_ASSERT(op->bind_pollset);
+ GPR_ASSERT(op->send_ops);
+ GPR_ASSERT(op->send_ops->nops >= 1);
+ GPR_ASSERT(op->send_ops->ops[0].type == GRPC_OP_METADATA);
+ gpr_mu_unlock(&calld->mu_state);
+
+ grpc_closure_init(&calld->async_setup_task, picked_target, calld);
+ grpc_lb_policy_pick(exec_ctx, lb_policy, bind_pollset,
+ initial_metadata, &calld->picked_channel,
+ &calld->async_setup_task);
+
+ GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick");
+ } else if (chand->resolver != NULL) {
+ calld->state = CALL_WAITING_FOR_CONFIG;
+ add_to_lb_policy_wait_queue_locked_state_config(elem);
+ if (!chand->started_resolving && chand->resolver != NULL) {
+ GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver");
+ chand->started_resolving = 1;
+ grpc_resolver_next(exec_ctx, chand->resolver,
+ &chand->incoming_configuration,
+ &chand->on_config_changed);
+ }
+ gpr_mu_unlock(&chand->mu_config);
+ gpr_mu_unlock(&calld->mu_state);
+ } else {
+ calld->state = CALL_CANCELLED;
+ gpr_mu_unlock(&chand->mu_config);
+ gpr_mu_unlock(&calld->mu_state);
+ handle_op_after_cancellation(exec_ctx, elem, op);
+ }
+ }
+ }
break;
- }
+ }
}
-static void
-cc_start_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- perform_transport_stream_op (exec_ctx, elem, op, 0);
+static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ perform_transport_stream_op(exec_ctx, elem, op, 0);
}
-static void watch_lb_policy (grpc_exec_ctx * exec_ctx, channel_data * chand, grpc_lb_policy * lb_policy, grpc_connectivity_state current_state);
+static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
+ grpc_lb_policy *lb_policy,
+ grpc_connectivity_state current_state);
-static void
-on_lb_policy_state_changed_locked (grpc_exec_ctx * exec_ctx, lb_policy_connectivity_watcher * w)
-{
+static void on_lb_policy_state_changed_locked(
+ grpc_exec_ctx *exec_ctx, lb_policy_connectivity_watcher *w) {
/* check if the notification is for a stale policy */
- if (w->lb_policy != w->chand->lb_policy)
- return;
+ if (w->lb_policy != w->chand->lb_policy) return;
- grpc_connectivity_state_set (exec_ctx, &w->chand->state_tracker, w->state, "lb_changed");
- if (w->state != GRPC_CHANNEL_FATAL_FAILURE)
- {
- watch_lb_policy (exec_ctx, w->chand, w->lb_policy, w->state);
- }
+ grpc_connectivity_state_set(exec_ctx, &w->chand->state_tracker, w->state,
+ "lb_changed");
+ if (w->state != GRPC_CHANNEL_FATAL_FAILURE) {
+ watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
+ }
}
-static void
-on_lb_policy_state_changed (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
lb_policy_connectivity_watcher *w = arg;
- gpr_mu_lock (&w->chand->mu_config);
- on_lb_policy_state_changed_locked (exec_ctx, w);
- gpr_mu_unlock (&w->chand->mu_config);
+ gpr_mu_lock(&w->chand->mu_config);
+ on_lb_policy_state_changed_locked(exec_ctx, w);
+ gpr_mu_unlock(&w->chand->mu_config);
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, w->chand->master, "watch_lb_policy");
- gpr_free (w);
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->chand->master, "watch_lb_policy");
+ gpr_free(w);
}
-static void
-watch_lb_policy (grpc_exec_ctx * exec_ctx, channel_data * chand, grpc_lb_policy * lb_policy, grpc_connectivity_state current_state)
-{
- lb_policy_connectivity_watcher *w = gpr_malloc (sizeof (*w));
- GRPC_CHANNEL_INTERNAL_REF (chand->master, "watch_lb_policy");
+static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
+ grpc_lb_policy *lb_policy,
+ grpc_connectivity_state current_state) {
+ lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
+ GRPC_CHANNEL_INTERNAL_REF(chand->master, "watch_lb_policy");
w->chand = chand;
- grpc_closure_init (&w->on_changed, on_lb_policy_state_changed, w);
+ grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
w->state = current_state;
w->lb_policy = lb_policy;
- grpc_lb_policy_notify_on_state_change (exec_ctx, lb_policy, &w->state, &w->on_changed);
+ grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
+ &w->on_changed);
}
-static void
-cc_on_config_changed (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void cc_on_config_changed(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
channel_data *chand = arg;
grpc_lb_policy *lb_policy = NULL;
grpc_lb_policy *old_lb_policy;
@@ -534,313 +485,295 @@ cc_on_config_changed (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
int exit_idle = 0;
- if (chand->incoming_configuration != NULL)
- {
- lb_policy = grpc_client_config_get_lb_policy (chand->incoming_configuration);
- if (lb_policy != NULL)
- {
- GRPC_LB_POLICY_REF (lb_policy, "channel");
- GRPC_LB_POLICY_REF (lb_policy, "config_change");
- state = grpc_lb_policy_check_connectivity (exec_ctx, lb_policy);
- }
-
- grpc_client_config_unref (exec_ctx, chand->incoming_configuration);
+ if (chand->incoming_configuration != NULL) {
+ lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration);
+ if (lb_policy != NULL) {
+ GRPC_LB_POLICY_REF(lb_policy, "channel");
+ GRPC_LB_POLICY_REF(lb_policy, "config_change");
+ state = grpc_lb_policy_check_connectivity(exec_ctx, lb_policy);
}
+ grpc_client_config_unref(exec_ctx, chand->incoming_configuration);
+ }
+
chand->incoming_configuration = NULL;
- gpr_mu_lock (&chand->mu_config);
+ gpr_mu_lock(&chand->mu_config);
old_lb_policy = chand->lb_policy;
chand->lb_policy = lb_policy;
- if (lb_policy != NULL || chand->resolver == NULL /* disconnected */ )
- {
- grpc_exec_ctx_enqueue_list (exec_ctx, &chand->waiting_for_config_closures);
- }
- if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives)
- {
- GRPC_LB_POLICY_REF (lb_policy, "exit_idle");
- exit_idle = 1;
- chand->exit_idle_when_lb_policy_arrives = 0;
- }
-
- if (iomgr_success && chand->resolver)
- {
- grpc_resolver *resolver = chand->resolver;
- GRPC_RESOLVER_REF (resolver, "channel-next");
- grpc_connectivity_state_set (exec_ctx, &chand->state_tracker, state, "new_lb+resolver");
- if (lb_policy != NULL)
- {
- watch_lb_policy (exec_ctx, chand, lb_policy, state);
- }
- gpr_mu_unlock (&chand->mu_config);
- GRPC_CHANNEL_INTERNAL_REF (chand->master, "resolver");
- grpc_resolver_next (exec_ctx, resolver, &chand->incoming_configuration, &chand->on_config_changed);
- GRPC_RESOLVER_UNREF (exec_ctx, resolver, "channel-next");
- }
- else
- {
- old_resolver = chand->resolver;
- chand->resolver = NULL;
- grpc_connectivity_state_set (exec_ctx, &chand->state_tracker, GRPC_CHANNEL_FATAL_FAILURE, "resolver_gone");
- gpr_mu_unlock (&chand->mu_config);
- if (old_resolver != NULL)
- {
- grpc_resolver_shutdown (exec_ctx, old_resolver);
- GRPC_RESOLVER_UNREF (exec_ctx, old_resolver, "channel");
- }
- }
-
- if (exit_idle)
- {
- grpc_lb_policy_exit_idle (exec_ctx, lb_policy);
- GRPC_LB_POLICY_UNREF (exec_ctx, lb_policy, "exit_idle");
- }
-
- if (old_lb_policy != NULL)
- {
- grpc_lb_policy_shutdown (exec_ctx, old_lb_policy);
- GRPC_LB_POLICY_UNREF (exec_ctx, old_lb_policy, "channel");
- }
-
- if (lb_policy != NULL)
- {
- GRPC_LB_POLICY_UNREF (exec_ctx, lb_policy, "config_change");
- }
-
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, chand->master, "resolver");
+ if (lb_policy != NULL || chand->resolver == NULL /* disconnected */) {
+ grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures);
+ }
+ if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
+ GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
+ exit_idle = 1;
+ chand->exit_idle_when_lb_policy_arrives = 0;
+ }
+
+ if (iomgr_success && chand->resolver) {
+ grpc_resolver *resolver = chand->resolver;
+ GRPC_RESOLVER_REF(resolver, "channel-next");
+ grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state,
+ "new_lb+resolver");
+ if (lb_policy != NULL) {
+ watch_lb_policy(exec_ctx, chand, lb_policy, state);
+ }
+ gpr_mu_unlock(&chand->mu_config);
+ GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver");
+ grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration,
+ &chand->on_config_changed);
+ GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel-next");
+ } else {
+ old_resolver = chand->resolver;
+ chand->resolver = NULL;
+ grpc_connectivity_state_set(exec_ctx, &chand->state_tracker,
+ GRPC_CHANNEL_FATAL_FAILURE, "resolver_gone");
+ gpr_mu_unlock(&chand->mu_config);
+ if (old_resolver != NULL) {
+ grpc_resolver_shutdown(exec_ctx, old_resolver);
+ GRPC_RESOLVER_UNREF(exec_ctx, old_resolver, "channel");
+ }
+ }
+
+ if (exit_idle) {
+ grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
+ GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
+ }
+
+ if (old_lb_policy != NULL) {
+ grpc_lb_policy_shutdown(exec_ctx, old_lb_policy);
+ GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
+ }
+
+ if (lb_policy != NULL) {
+ GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
+ }
+
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->master, "resolver");
}
-static void
-cc_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op)
-{
+static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem,
+ grpc_transport_op *op) {
grpc_lb_policy *lb_policy = NULL;
channel_data *chand = elem->channel_data;
grpc_resolver *destroy_resolver = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, op->on_consumed, 1);
-
- GPR_ASSERT (op->set_accept_stream == NULL);
- GPR_ASSERT (op->bind_pollset == NULL);
-
- gpr_mu_lock (&chand->mu_config);
- if (op->on_connectivity_state_change != NULL)
- {
- grpc_connectivity_state_notify_on_state_change (exec_ctx, &chand->state_tracker, op->connectivity_state, op->on_connectivity_state_change);
- op->on_connectivity_state_change = NULL;
- op->connectivity_state = NULL;
- }
-
- if (!is_empty (op, sizeof (*op)))
- {
- lb_policy = chand->lb_policy;
- if (lb_policy)
- {
- GRPC_LB_POLICY_REF (lb_policy, "broadcast");
- }
- }
-
- if (op->disconnect && chand->resolver != NULL)
- {
- grpc_connectivity_state_set (exec_ctx, &chand->state_tracker, GRPC_CHANNEL_FATAL_FAILURE, "disconnect");
- destroy_resolver = chand->resolver;
- chand->resolver = NULL;
- if (chand->lb_policy != NULL)
- {
- grpc_lb_policy_shutdown (exec_ctx, chand->lb_policy);
- GRPC_LB_POLICY_UNREF (exec_ctx, chand->lb_policy, "channel");
- chand->lb_policy = NULL;
- }
- }
- gpr_mu_unlock (&chand->mu_config);
-
- if (destroy_resolver)
- {
- grpc_resolver_shutdown (exec_ctx, destroy_resolver);
- GRPC_RESOLVER_UNREF (exec_ctx, destroy_resolver, "channel");
- }
-
- if (lb_policy)
- {
- grpc_lb_policy_broadcast (exec_ctx, lb_policy, op);
- GRPC_LB_POLICY_UNREF (exec_ctx, lb_policy, "broadcast");
- }
+ grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, 1);
+
+ GPR_ASSERT(op->set_accept_stream == NULL);
+ GPR_ASSERT(op->bind_pollset == NULL);
+
+ gpr_mu_lock(&chand->mu_config);
+ if (op->on_connectivity_state_change != NULL) {
+ grpc_connectivity_state_notify_on_state_change(
+ exec_ctx, &chand->state_tracker, op->connectivity_state,
+ op->on_connectivity_state_change);
+ op->on_connectivity_state_change = NULL;
+ op->connectivity_state = NULL;
+ }
+
+ if (!is_empty(op, sizeof(*op))) {
+ lb_policy = chand->lb_policy;
+ if (lb_policy) {
+ GRPC_LB_POLICY_REF(lb_policy, "broadcast");
+ }
+ }
+
+ if (op->disconnect && chand->resolver != NULL) {
+ grpc_connectivity_state_set(exec_ctx, &chand->state_tracker,
+ GRPC_CHANNEL_FATAL_FAILURE, "disconnect");
+ destroy_resolver = chand->resolver;
+ chand->resolver = NULL;
+ if (chand->lb_policy != NULL) {
+ grpc_lb_policy_shutdown(exec_ctx, chand->lb_policy);
+ GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
+ chand->lb_policy = NULL;
+ }
+ }
+ gpr_mu_unlock(&chand->mu_config);
+
+ if (destroy_resolver) {
+ grpc_resolver_shutdown(exec_ctx, destroy_resolver);
+ GRPC_RESOLVER_UNREF(exec_ctx, destroy_resolver, "channel");
+ }
+
+ if (lb_policy) {
+ grpc_lb_policy_broadcast(exec_ctx, lb_policy, op);
+ GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "broadcast");
+ }
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *calld = elem->call_data;
/* TODO(ctiller): is there something useful we can do here? */
- GPR_ASSERT (initial_op == NULL);
+ GPR_ASSERT(initial_op == NULL);
- GPR_ASSERT (elem->filter == &grpc_client_channel_filter);
- GPR_ASSERT (server_transport_data == NULL);
- gpr_mu_init (&calld->mu_state);
+ GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
+ GPR_ASSERT(server_transport_data == NULL);
+ gpr_mu_init(&calld->mu_state);
calld->elem = elem;
calld->state = CALL_CREATED;
- calld->deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
+ calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
call_data *calld = elem->call_data;
grpc_subchannel_call *subchannel_call;
/* if the call got activated, we need to destroy the child stack also, and
remove it from the in-flight requests tracked by the child_entry we
picked */
- gpr_mu_lock (&calld->mu_state);
- switch (calld->state)
- {
+ gpr_mu_lock(&calld->mu_state);
+ switch (calld->state) {
case CALL_ACTIVE:
subchannel_call = calld->subchannel_call;
- gpr_mu_unlock (&calld->mu_state);
- GRPC_SUBCHANNEL_CALL_UNREF (exec_ctx, subchannel_call, "client_channel");
+ gpr_mu_unlock(&calld->mu_state);
+ GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, subchannel_call, "client_channel");
break;
case CALL_CREATED:
case CALL_CANCELLED:
- gpr_mu_unlock (&calld->mu_state);
+ gpr_mu_unlock(&calld->mu_state);
break;
case CALL_WAITING_FOR_PICK:
case CALL_WAITING_FOR_CONFIG:
case CALL_WAITING_FOR_CALL:
case CALL_WAITING_FOR_SEND:
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
break;
- }
+ }
}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args,
+ grpc_mdctx *metadata_context, int is_first,
+ int is_last) {
channel_data *chand = elem->channel_data;
- memset (chand, 0, sizeof (*chand));
+ memset(chand, 0, sizeof(*chand));
- GPR_ASSERT (is_last);
- GPR_ASSERT (elem->filter == &grpc_client_channel_filter);
+ GPR_ASSERT(is_last);
+ GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
- gpr_mu_init (&chand->mu_config);
+ gpr_mu_init(&chand->mu_config);
chand->mdctx = metadata_context;
chand->master = master;
- grpc_pollset_set_init (&chand->pollset_set);
- grpc_closure_init (&chand->on_config_changed, cc_on_config_changed, chand);
+ grpc_pollset_set_init(&chand->pollset_set);
+ grpc_closure_init(&chand->on_config_changed, cc_on_config_changed, chand);
- grpc_connectivity_state_init (&chand->state_tracker, GRPC_CHANNEL_IDLE, "client_channel");
+ grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
+ "client_channel");
}
/* Destructor for channel_data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
channel_data *chand = elem->channel_data;
- if (chand->resolver != NULL)
- {
- grpc_resolver_shutdown (exec_ctx, chand->resolver);
- GRPC_RESOLVER_UNREF (exec_ctx, chand->resolver, "channel");
- }
- if (chand->lb_policy != NULL)
- {
- GRPC_LB_POLICY_UNREF (exec_ctx, chand->lb_policy, "channel");
- }
- grpc_connectivity_state_destroy (exec_ctx, &chand->state_tracker);
- grpc_pollset_set_destroy (&chand->pollset_set);
- gpr_mu_destroy (&chand->mu_config);
+ if (chand->resolver != NULL) {
+ grpc_resolver_shutdown(exec_ctx, chand->resolver);
+ GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
+ }
+ if (chand->lb_policy != NULL) {
+ GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
+ }
+ grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
+ grpc_pollset_set_destroy(&chand->pollset_set);
+ gpr_mu_destroy(&chand->mu_config);
}
const grpc_channel_filter grpc_client_channel_filter = {
- cc_start_transport_stream_op,
- cc_start_transport_op,
- sizeof (call_data),
- init_call_elem,
- destroy_call_elem,
- sizeof (channel_data),
- init_channel_elem,
- destroy_channel_elem,
- cc_get_peer,
- "client-channel",
+ cc_start_transport_stream_op,
+ cc_start_transport_op,
+ sizeof(call_data),
+ init_call_elem,
+ destroy_call_elem,
+ sizeof(channel_data),
+ init_channel_elem,
+ destroy_channel_elem,
+ cc_get_peer,
+ "client-channel",
};
-void
-grpc_client_channel_set_resolver (grpc_exec_ctx * exec_ctx, grpc_channel_stack * channel_stack, grpc_resolver * resolver)
-{
+void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx,
+ grpc_channel_stack *channel_stack,
+ grpc_resolver *resolver) {
/* post construction initialization: set the transport setup pointer */
- grpc_channel_element *elem = grpc_channel_stack_last_element (channel_stack);
+ grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
channel_data *chand = elem->channel_data;
- gpr_mu_lock (&chand->mu_config);
- GPR_ASSERT (!chand->resolver);
+ gpr_mu_lock(&chand->mu_config);
+ GPR_ASSERT(!chand->resolver);
chand->resolver = resolver;
- GRPC_RESOLVER_REF (resolver, "channel");
- if (!grpc_closure_list_empty (chand->waiting_for_config_closures) || chand->exit_idle_when_lb_policy_arrives)
- {
- chand->started_resolving = 1;
- GRPC_CHANNEL_INTERNAL_REF (chand->master, "resolver");
- grpc_resolver_next (exec_ctx, resolver, &chand->incoming_configuration, &chand->on_config_changed);
- }
- gpr_mu_unlock (&chand->mu_config);
+ GRPC_RESOLVER_REF(resolver, "channel");
+ if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
+ chand->exit_idle_when_lb_policy_arrives) {
+ chand->started_resolving = 1;
+ GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver");
+ grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration,
+ &chand->on_config_changed);
+ }
+ gpr_mu_unlock(&chand->mu_config);
}
-grpc_connectivity_state
-grpc_client_channel_check_connectivity_state (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, int try_to_connect)
-{
+grpc_connectivity_state grpc_client_channel_check_connectivity_state(
+ grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
channel_data *chand = elem->channel_data;
grpc_connectivity_state out;
- gpr_mu_lock (&chand->mu_config);
- out = grpc_connectivity_state_check (&chand->state_tracker);
- if (out == GRPC_CHANNEL_IDLE && try_to_connect)
- {
- if (chand->lb_policy != NULL)
- {
- grpc_lb_policy_exit_idle (exec_ctx, chand->lb_policy);
- }
- else
- {
- chand->exit_idle_when_lb_policy_arrives = 1;
- if (!chand->started_resolving && chand->resolver != NULL)
- {
- GRPC_CHANNEL_INTERNAL_REF (chand->master, "resolver");
- chand->started_resolving = 1;
- grpc_resolver_next (exec_ctx, chand->resolver, &chand->incoming_configuration, &chand->on_config_changed);
- }
- }
- }
- gpr_mu_unlock (&chand->mu_config);
+ gpr_mu_lock(&chand->mu_config);
+ out = grpc_connectivity_state_check(&chand->state_tracker);
+ if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
+ if (chand->lb_policy != NULL) {
+ grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
+ } else {
+ chand->exit_idle_when_lb_policy_arrives = 1;
+ if (!chand->started_resolving && chand->resolver != NULL) {
+ GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver");
+ chand->started_resolving = 1;
+ grpc_resolver_next(exec_ctx, chand->resolver,
+ &chand->incoming_configuration,
+ &chand->on_config_changed);
+ }
+ }
+ }
+ gpr_mu_unlock(&chand->mu_config);
return out;
}
-void
-grpc_client_channel_watch_connectivity_state (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_connectivity_state * state, grpc_closure * on_complete)
-{
+void grpc_client_channel_watch_connectivity_state(
+ grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
+ grpc_connectivity_state *state, grpc_closure *on_complete) {
channel_data *chand = elem->channel_data;
- gpr_mu_lock (&chand->mu_config);
- grpc_connectivity_state_notify_on_state_change (exec_ctx, &chand->state_tracker, state, on_complete);
- gpr_mu_unlock (&chand->mu_config);
+ gpr_mu_lock(&chand->mu_config);
+ grpc_connectivity_state_notify_on_state_change(
+ exec_ctx, &chand->state_tracker, state, on_complete);
+ gpr_mu_unlock(&chand->mu_config);
}
-grpc_pollset_set *
-grpc_client_channel_get_connecting_pollset_set (grpc_channel_element * elem)
-{
+grpc_pollset_set *grpc_client_channel_get_connecting_pollset_set(
+ grpc_channel_element *elem) {
channel_data *chand = elem->channel_data;
return &chand->pollset_set;
}
-void
-grpc_client_channel_add_interested_party (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_pollset * pollset)
-{
+void grpc_client_channel_add_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem,
+ grpc_pollset *pollset) {
channel_data *chand = elem->channel_data;
- grpc_pollset_set_add_pollset (exec_ctx, &chand->pollset_set, pollset);
+ grpc_pollset_set_add_pollset(exec_ctx, &chand->pollset_set, pollset);
}
-void
-grpc_client_channel_del_interested_party (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_pollset * pollset)
-{
+void grpc_client_channel_del_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem,
+ grpc_pollset *pollset) {
channel_data *chand = elem->channel_data;
- grpc_pollset_set_del_pollset (exec_ctx, &chand->pollset_set, pollset);
+ grpc_pollset_set_del_pollset(exec_ctx, &chand->pollset_set, pollset);
}
diff --git a/src/core/channel/client_channel.h b/src/core/channel/client_channel.h
index 5af9794727..5103f07a43 100644
--- a/src/core/channel/client_channel.h
+++ b/src/core/channel/client_channel.h
@@ -49,15 +49,25 @@ extern const grpc_channel_filter grpc_client_channel_filter;
/* post-construction initializer to let the client channel know which
transport setup it should cancel upon destruction, or initiate when it needs
a connection */
-void grpc_client_channel_set_resolver (grpc_exec_ctx * exec_ctx, grpc_channel_stack * channel_stack, grpc_resolver * resolver);
+void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx,
+ grpc_channel_stack *channel_stack,
+ grpc_resolver *resolver);
-grpc_connectivity_state grpc_client_channel_check_connectivity_state (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, int try_to_connect);
+grpc_connectivity_state grpc_client_channel_check_connectivity_state(
+ grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect);
-void grpc_client_channel_watch_connectivity_state (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_connectivity_state * state, grpc_closure * on_complete);
+void grpc_client_channel_watch_connectivity_state(
+ grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
+ grpc_connectivity_state *state, grpc_closure *on_complete);
-grpc_pollset_set *grpc_client_channel_get_connecting_pollset_set (grpc_channel_element * elem);
+grpc_pollset_set *grpc_client_channel_get_connecting_pollset_set(
+ grpc_channel_element *elem);
-void grpc_client_channel_add_interested_party (grpc_exec_ctx * exec_ctx, grpc_channel_element * channel, grpc_pollset * pollset);
-void grpc_client_channel_del_interested_party (grpc_exec_ctx * exec_ctx, grpc_channel_element * channel, grpc_pollset * pollset);
+void grpc_client_channel_add_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *channel,
+ grpc_pollset *pollset);
+void grpc_client_channel_del_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *channel,
+ grpc_pollset *pollset);
#endif /* GRPC_INTERNAL_CORE_CHANNEL_CLIENT_CHANNEL_H */
diff --git a/src/core/channel/compress_filter.c b/src/core/channel/compress_filter.c
index 6acdc72075..f8dbe8c817 100644
--- a/src/core/channel/compress_filter.c
+++ b/src/core/channel/compress_filter.c
@@ -44,14 +44,13 @@
#include "src/core/compression/message_compress.h"
#include "src/core/support/string.h"
-typedef struct call_data
-{
+typedef struct call_data {
gpr_slice_buffer slices; /**< Buffers up input slices to be compressed */
grpc_linked_mdelem compression_algorithm_storage;
grpc_linked_mdelem accept_encoding_storage;
- gpr_uint32 remaining_slice_bytes;
- /**< Input data to be read, as per BEGIN_MESSAGE */
- int written_initial_metadata; /**< Already processed initial md? */
+ gpr_uint32 remaining_slice_bytes;
+ /**< Input data to be read, as per BEGIN_MESSAGE */
+ int written_initial_metadata; /**< Already processed initial md? */
/** Compression algorithm we'll try to use. It may be given by incoming
* metadata, or by the channel's default compression settings. */
grpc_compression_algorithm compression_algorithm;
@@ -59,8 +58,7 @@ typedef struct call_data
int has_compression_algorithm;
} call_data;
-typedef struct channel_data
-{
+typedef struct channel_data {
/** Metadata key for the incoming (requested) compression algorithm */
grpc_mdstr *mdstr_request_compression_algorithm_key;
/** Metadata key for the outgoing (used) compression algorithm */
@@ -82,62 +80,59 @@ typedef struct channel_data
* larger than the raw input).
*
* Returns 1 if the data was actually compress and 0 otherwise. */
-static int
-compress_send_sb (grpc_compression_algorithm algorithm, gpr_slice_buffer * slices)
-{
+static int compress_send_sb(grpc_compression_algorithm algorithm,
+ gpr_slice_buffer *slices) {
int did_compress;
gpr_slice_buffer tmp;
- gpr_slice_buffer_init (&tmp);
- did_compress = grpc_msg_compress (algorithm, slices, &tmp);
- if (did_compress)
- {
- gpr_slice_buffer_swap (slices, &tmp);
- }
- gpr_slice_buffer_destroy (&tmp);
+ gpr_slice_buffer_init(&tmp);
+ did_compress = grpc_msg_compress(algorithm, slices, &tmp);
+ if (did_compress) {
+ gpr_slice_buffer_swap(slices, &tmp);
+ }
+ gpr_slice_buffer_destroy(&tmp);
return did_compress;
}
/** For each \a md element from the incoming metadata, filter out the entry for
* "grpc-encoding", using its value to populate the call data's
* compression_algorithm field. */
-static grpc_mdelem *
-compression_md_filter (void *user_data, grpc_mdelem * md)
-{
+static grpc_mdelem *compression_md_filter(void *user_data, grpc_mdelem *md) {
grpc_call_element *elem = user_data;
call_data *calld = elem->call_data;
channel_data *channeld = elem->channel_data;
- if (md->key == channeld->mdstr_request_compression_algorithm_key)
- {
- const char *md_c_str = grpc_mdstr_as_c_string (md->value);
- if (!grpc_compression_algorithm_parse (md_c_str, strlen (md_c_str), &calld->compression_algorithm))
- {
- gpr_log (GPR_ERROR, "Invalid compression algorithm: '%s' (unknown). Ignoring.", md_c_str);
- calld->compression_algorithm = GRPC_COMPRESS_NONE;
- }
- if (grpc_compression_options_is_algorithm_enabled (&channeld->compression_options, calld->compression_algorithm) == 0)
- {
- gpr_log (GPR_ERROR, "Invalid compression algorithm: '%s' (previously disabled). " "Ignoring.", md_c_str);
- calld->compression_algorithm = GRPC_COMPRESS_NONE;
- }
- calld->has_compression_algorithm = 1;
- return NULL;
+ if (md->key == channeld->mdstr_request_compression_algorithm_key) {
+ const char *md_c_str = grpc_mdstr_as_c_string(md->value);
+ if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str),
+ &calld->compression_algorithm)) {
+ gpr_log(GPR_ERROR,
+ "Invalid compression algorithm: '%s' (unknown). Ignoring.",
+ md_c_str);
+ calld->compression_algorithm = GRPC_COMPRESS_NONE;
+ }
+ if (grpc_compression_options_is_algorithm_enabled(
+ &channeld->compression_options, calld->compression_algorithm) ==
+ 0) {
+ gpr_log(GPR_ERROR,
+ "Invalid compression algorithm: '%s' (previously disabled). "
+ "Ignoring.",
+ md_c_str);
+ calld->compression_algorithm = GRPC_COMPRESS_NONE;
}
+ calld->has_compression_algorithm = 1;
+ return NULL;
+ }
return md;
}
-static int
-skip_compression (channel_data * channeld, call_data * calld)
-{
- if (calld->has_compression_algorithm)
- {
- if (calld->compression_algorithm == GRPC_COMPRESS_NONE)
- {
- return 1;
- }
- return 0; /* we have an actual call-specific algorithm */
+static int skip_compression(channel_data *channeld, call_data *calld) {
+ if (calld->has_compression_algorithm) {
+ if (calld->compression_algorithm == GRPC_COMPRESS_NONE) {
+ return 1;
}
+ return 0; /* we have an actual call-specific algorithm */
+ }
/* no per-call compression override */
return channeld->default_compression_algorithm == GRPC_COMPRESS_NONE;
}
@@ -146,127 +141,126 @@ skip_compression (channel_data * channeld, call_data * calld)
* the associated GRPC_OP_BEGIN_MESSAGE accordingly (new compressed length,
* flags indicating compression is in effect) and replaces \a send_ops with it.
* */
-static void
-finish_compressed_sopb (grpc_stream_op_buffer * send_ops, grpc_call_element * elem)
-{
+static void finish_compressed_sopb(grpc_stream_op_buffer *send_ops,
+ grpc_call_element *elem) {
size_t i;
call_data *calld = elem->call_data;
- int new_slices_added = 0; /* GPR_FALSE */
+ int new_slices_added = 0; /* GPR_FALSE */
grpc_metadata_batch metadata;
grpc_stream_op_buffer new_send_ops;
- grpc_sopb_init (&new_send_ops);
-
- for (i = 0; i < send_ops->nops; i++)
- {
- grpc_stream_op *sop = &send_ops->ops[i];
- switch (sop->type)
- {
- case GRPC_OP_BEGIN_MESSAGE:
- GPR_ASSERT (calld->slices.length <= GPR_UINT32_MAX);
- grpc_sopb_add_begin_message (&new_send_ops, (gpr_uint32) calld->slices.length, sop->data.begin_message.flags | GRPC_WRITE_INTERNAL_COMPRESS);
- break;
- case GRPC_OP_SLICE:
- /* Once we reach the slices section of the original buffer, simply add
- * all the new (compressed) slices. We obviously want to do this only
- * once, hence the "new_slices_added" guard. */
- if (!new_slices_added)
- {
- size_t j;
- for (j = 0; j < calld->slices.count; ++j)
- {
- grpc_sopb_add_slice (&new_send_ops, gpr_slice_ref (calld->slices.slices[j]));
- }
- new_slices_added = 1; /* GPR_TRUE */
- }
- break;
- case GRPC_OP_METADATA:
- /* move the metadata to the new buffer. */
- grpc_metadata_batch_move (&metadata, &sop->data.metadata);
- grpc_sopb_add_metadata (&new_send_ops, metadata);
- break;
- case GRPC_NO_OP:
- break;
- }
+ grpc_sopb_init(&new_send_ops);
+
+ for (i = 0; i < send_ops->nops; i++) {
+ grpc_stream_op *sop = &send_ops->ops[i];
+ switch (sop->type) {
+ case GRPC_OP_BEGIN_MESSAGE:
+ GPR_ASSERT(calld->slices.length <= GPR_UINT32_MAX);
+ grpc_sopb_add_begin_message(
+ &new_send_ops, (gpr_uint32)calld->slices.length,
+ sop->data.begin_message.flags | GRPC_WRITE_INTERNAL_COMPRESS);
+ break;
+ case GRPC_OP_SLICE:
+ /* Once we reach the slices section of the original buffer, simply add
+ * all the new (compressed) slices. We obviously want to do this only
+ * once, hence the "new_slices_added" guard. */
+ if (!new_slices_added) {
+ size_t j;
+ for (j = 0; j < calld->slices.count; ++j) {
+ grpc_sopb_add_slice(&new_send_ops,
+ gpr_slice_ref(calld->slices.slices[j]));
+ }
+ new_slices_added = 1; /* GPR_TRUE */
+ }
+ break;
+ case GRPC_OP_METADATA:
+ /* move the metadata to the new buffer. */
+ grpc_metadata_batch_move(&metadata, &sop->data.metadata);
+ grpc_sopb_add_metadata(&new_send_ops, metadata);
+ break;
+ case GRPC_NO_OP:
+ break;
}
- grpc_sopb_swap (send_ops, &new_send_ops);
- grpc_sopb_destroy (&new_send_ops);
+ }
+ grpc_sopb_swap(send_ops, &new_send_ops);
+ grpc_sopb_destroy(&new_send_ops);
}
/** Filter's "main" function, called for any incoming grpc_transport_stream_op
* instance that holds a non-zero number of send operations, accesible to this
* function in \a send_ops. */
-static void
-process_send_ops (grpc_call_element * elem, grpc_stream_op_buffer * send_ops)
-{
+static void process_send_ops(grpc_call_element *elem,
+ grpc_stream_op_buffer *send_ops) {
call_data *calld = elem->call_data;
channel_data *channeld = elem->channel_data;
size_t i;
int did_compress = 0;
/* In streaming calls, we need to reset the previously accumulated slices */
- gpr_slice_buffer_reset_and_unref (&calld->slices);
- for (i = 0; i < send_ops->nops; ++i)
- {
- grpc_stream_op *sop = &send_ops->ops[i];
- switch (sop->type)
- {
- case GRPC_OP_BEGIN_MESSAGE:
- /* buffer up slices until we've processed all the expected ones (as
- * given by GRPC_OP_BEGIN_MESSAGE) */
- calld->remaining_slice_bytes = sop->data.begin_message.length;
- if (sop->data.begin_message.flags & GRPC_WRITE_NO_COMPRESS)
- {
- calld->has_compression_algorithm = 1; /* GPR_TRUE */
- calld->compression_algorithm = GRPC_COMPRESS_NONE;
- }
- break;
- case GRPC_OP_METADATA:
- if (!calld->written_initial_metadata)
- {
- /* Parse incoming request for compression. If any, it'll be available
- * at calld->compression_algorithm */
- grpc_metadata_batch_filter (&(sop->data.metadata), compression_md_filter, elem);
- if (!calld->has_compression_algorithm)
- {
- /* If no algorithm was found in the metadata and we aren't
- * exceptionally skipping compression, fall back to the channel
- * default */
- calld->compression_algorithm = channeld->default_compression_algorithm;
- calld->has_compression_algorithm = 1; /* GPR_TRUE */
- }
- /* hint compression algorithm */
- grpc_metadata_batch_add_tail (&(sop->data.metadata), &calld->compression_algorithm_storage, GRPC_MDELEM_REF (channeld->mdelem_compression_algorithms[calld->compression_algorithm]));
-
- /* convey supported compression algorithms */
- grpc_metadata_batch_add_tail (&(sop->data.metadata), &calld->accept_encoding_storage, GRPC_MDELEM_REF (channeld->mdelem_accept_encoding));
-
- calld->written_initial_metadata = 1; /* GPR_TRUE */
- }
- break;
- case GRPC_OP_SLICE:
- if (skip_compression (channeld, calld))
- continue;
- GPR_ASSERT (calld->remaining_slice_bytes > 0);
- /* Increase input ref count, gpr_slice_buffer_add takes ownership. */
- gpr_slice_buffer_add (&calld->slices, gpr_slice_ref (sop->data.slice));
- GPR_ASSERT (GPR_SLICE_LENGTH (sop->data.slice) >= calld->remaining_slice_bytes);
- calld->remaining_slice_bytes -= (gpr_uint32) GPR_SLICE_LENGTH (sop->data.slice);
- if (calld->remaining_slice_bytes == 0)
- {
- did_compress = compress_send_sb (calld->compression_algorithm, &calld->slices);
- }
- break;
- case GRPC_NO_OP:
- break;
- }
+ gpr_slice_buffer_reset_and_unref(&calld->slices);
+ for (i = 0; i < send_ops->nops; ++i) {
+ grpc_stream_op *sop = &send_ops->ops[i];
+ switch (sop->type) {
+ case GRPC_OP_BEGIN_MESSAGE:
+ /* buffer up slices until we've processed all the expected ones (as
+ * given by GRPC_OP_BEGIN_MESSAGE) */
+ calld->remaining_slice_bytes = sop->data.begin_message.length;
+ if (sop->data.begin_message.flags & GRPC_WRITE_NO_COMPRESS) {
+ calld->has_compression_algorithm = 1; /* GPR_TRUE */
+ calld->compression_algorithm = GRPC_COMPRESS_NONE;
+ }
+ break;
+ case GRPC_OP_METADATA:
+ if (!calld->written_initial_metadata) {
+ /* Parse incoming request for compression. If any, it'll be available
+ * at calld->compression_algorithm */
+ grpc_metadata_batch_filter(&(sop->data.metadata),
+ compression_md_filter, elem);
+ if (!calld->has_compression_algorithm) {
+ /* If no algorithm was found in the metadata and we aren't
+ * exceptionally skipping compression, fall back to the channel
+ * default */
+ calld->compression_algorithm =
+ channeld->default_compression_algorithm;
+ calld->has_compression_algorithm = 1; /* GPR_TRUE */
+ }
+ /* hint compression algorithm */
+ grpc_metadata_batch_add_tail(
+ &(sop->data.metadata), &calld->compression_algorithm_storage,
+ GRPC_MDELEM_REF(channeld->mdelem_compression_algorithms
+ [calld->compression_algorithm]));
+
+ /* convey supported compression algorithms */
+ grpc_metadata_batch_add_tail(
+ &(sop->data.metadata), &calld->accept_encoding_storage,
+ GRPC_MDELEM_REF(channeld->mdelem_accept_encoding));
+
+ calld->written_initial_metadata = 1; /* GPR_TRUE */
+ }
+ break;
+ case GRPC_OP_SLICE:
+ if (skip_compression(channeld, calld)) continue;
+ GPR_ASSERT(calld->remaining_slice_bytes > 0);
+ /* Increase input ref count, gpr_slice_buffer_add takes ownership. */
+ gpr_slice_buffer_add(&calld->slices, gpr_slice_ref(sop->data.slice));
+ GPR_ASSERT(GPR_SLICE_LENGTH(sop->data.slice) >=
+ calld->remaining_slice_bytes);
+ calld->remaining_slice_bytes -=
+ (gpr_uint32)GPR_SLICE_LENGTH(sop->data.slice);
+ if (calld->remaining_slice_bytes == 0) {
+ did_compress =
+ compress_send_sb(calld->compression_algorithm, &calld->slices);
+ }
+ break;
+ case GRPC_NO_OP:
+ break;
}
+ }
/* Modify the send_ops stream_op_buffer depending on whether compression was
* carried out */
- if (did_compress)
- {
- finish_compressed_sopb (send_ops, elem);
- }
+ if (did_compress) {
+ finish_compressed_sopb(send_ops, elem);
+ }
}
/* Called either:
@@ -274,52 +268,49 @@ process_send_ops (grpc_call_element * elem, grpc_stream_op_buffer * send_ops)
- a network event (or similar) from below, to receive something
op contains type and call direction information, in addition to the data
that is being sent or received. */
-static void
-compress_start_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- if (op->send_ops && op->send_ops->nops > 0)
- {
- process_send_ops (elem, op->send_ops);
- }
+static void compress_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ if (op->send_ops && op->send_ops->nops > 0) {
+ process_send_ops(elem, op->send_ops);
+ }
/* pass control down the stack */
- grpc_call_next_op (exec_ctx, elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
/* initialize members */
- gpr_slice_buffer_init (&calld->slices);
+ gpr_slice_buffer_init(&calld->slices);
calld->has_compression_algorithm = 0;
- calld->written_initial_metadata = 0; /* GPR_FALSE */
-
- if (initial_op)
- {
- if (initial_op->send_ops && initial_op->send_ops->nops > 0)
- {
- process_send_ops (elem, initial_op->send_ops);
- }
+ calld->written_initial_metadata = 0; /* GPR_FALSE */
+
+ if (initial_op) {
+ if (initial_op->send_ops && initial_op->send_ops->nops > 0) {
+ process_send_ops(elem, initial_op->send_ops);
}
+ }
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
- gpr_slice_buffer_destroy (&calld->slices);
+ gpr_slice_buffer_destroy(&calld->slices);
}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
channel_data *channeld = elem->channel_data;
grpc_compression_algorithm algo_idx;
const char *supported_algorithms_names[GRPC_COMPRESS_ALGORITHMS_COUNT - 1];
@@ -327,72 +318,82 @@ init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_c
char *accept_encoding_str;
size_t accept_encoding_str_len;
- grpc_compression_options_init (&channeld->compression_options);
- channeld->compression_options.enabled_algorithms_bitset = (gpr_uint32) grpc_channel_args_compression_algorithm_get_states (args);
+ grpc_compression_options_init(&channeld->compression_options);
+ channeld->compression_options.enabled_algorithms_bitset =
+ (gpr_uint32)grpc_channel_args_compression_algorithm_get_states(args);
- channeld->default_compression_algorithm = grpc_channel_args_get_compression_algorithm (args);
+ channeld->default_compression_algorithm =
+ grpc_channel_args_get_compression_algorithm(args);
/* Make sure the default isn't disabled. */
- GPR_ASSERT (grpc_compression_options_is_algorithm_enabled (&channeld->compression_options, channeld->default_compression_algorithm));
- channeld->compression_options.default_compression_algorithm = channeld->default_compression_algorithm;
-
- channeld->mdstr_request_compression_algorithm_key = grpc_mdstr_from_string (mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, 0);
-
- channeld->mdstr_outgoing_compression_algorithm_key = grpc_mdstr_from_string (mdctx, "grpc-encoding", 0);
-
- channeld->mdstr_compression_capabilities_key = grpc_mdstr_from_string (mdctx, "grpc-accept-encoding", 0);
-
- for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx)
- {
- char *algorithm_name;
- /* skip disabled algorithms */
- if (grpc_compression_options_is_algorithm_enabled (&channeld->compression_options, algo_idx) == 0)
- {
- continue;
- }
- GPR_ASSERT (grpc_compression_algorithm_name (algo_idx, &algorithm_name) != 0);
- channeld->mdelem_compression_algorithms[algo_idx] = grpc_mdelem_from_metadata_strings (mdctx, GRPC_MDSTR_REF (channeld->mdstr_outgoing_compression_algorithm_key), grpc_mdstr_from_string (mdctx, algorithm_name, 0));
- if (algo_idx > 0)
- {
- supported_algorithms_names[supported_algorithms_idx++] = algorithm_name;
- }
+ GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(
+ &channeld->compression_options, channeld->default_compression_algorithm));
+ channeld->compression_options.default_compression_algorithm =
+ channeld->default_compression_algorithm;
+
+ channeld->mdstr_request_compression_algorithm_key =
+ grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, 0);
+
+ channeld->mdstr_outgoing_compression_algorithm_key =
+ grpc_mdstr_from_string(mdctx, "grpc-encoding", 0);
+
+ channeld->mdstr_compression_capabilities_key =
+ grpc_mdstr_from_string(mdctx, "grpc-accept-encoding", 0);
+
+ for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) {
+ char *algorithm_name;
+ /* skip disabled algorithms */
+ if (grpc_compression_options_is_algorithm_enabled(
+ &channeld->compression_options, algo_idx) == 0) {
+ continue;
+ }
+ GPR_ASSERT(grpc_compression_algorithm_name(algo_idx, &algorithm_name) != 0);
+ channeld->mdelem_compression_algorithms[algo_idx] =
+ grpc_mdelem_from_metadata_strings(
+ mdctx,
+ GRPC_MDSTR_REF(channeld->mdstr_outgoing_compression_algorithm_key),
+ grpc_mdstr_from_string(mdctx, algorithm_name, 0));
+ if (algo_idx > 0) {
+ supported_algorithms_names[supported_algorithms_idx++] = algorithm_name;
}
+ }
/* TODO(dgq): gpr_strjoin_sep could be made to work with statically allocated
* arrays, as to avoid the heap allocs */
- accept_encoding_str = gpr_strjoin_sep (supported_algorithms_names, supported_algorithms_idx, ",", &accept_encoding_str_len);
+ accept_encoding_str =
+ gpr_strjoin_sep(supported_algorithms_names, supported_algorithms_idx, ",",
+ &accept_encoding_str_len);
- channeld->mdelem_accept_encoding = grpc_mdelem_from_metadata_strings (mdctx, GRPC_MDSTR_REF (channeld->mdstr_compression_capabilities_key), grpc_mdstr_from_string (mdctx, accept_encoding_str, 0));
- gpr_free (accept_encoding_str);
+ channeld->mdelem_accept_encoding = grpc_mdelem_from_metadata_strings(
+ mdctx, GRPC_MDSTR_REF(channeld->mdstr_compression_capabilities_key),
+ grpc_mdstr_from_string(mdctx, accept_encoding_str, 0));
+ gpr_free(accept_encoding_str);
- GPR_ASSERT (!is_last);
+ GPR_ASSERT(!is_last);
}
/* Destructor for channel data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
channel_data *channeld = elem->channel_data;
grpc_compression_algorithm algo_idx;
- GRPC_MDSTR_UNREF (channeld->mdstr_request_compression_algorithm_key);
- GRPC_MDSTR_UNREF (channeld->mdstr_outgoing_compression_algorithm_key);
- GRPC_MDSTR_UNREF (channeld->mdstr_compression_capabilities_key);
- for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx)
- {
- GRPC_MDELEM_UNREF (channeld->mdelem_compression_algorithms[algo_idx]);
- }
- GRPC_MDELEM_UNREF (channeld->mdelem_accept_encoding);
+ GRPC_MDSTR_UNREF(channeld->mdstr_request_compression_algorithm_key);
+ GRPC_MDSTR_UNREF(channeld->mdstr_outgoing_compression_algorithm_key);
+ GRPC_MDSTR_UNREF(channeld->mdstr_compression_capabilities_key);
+ for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) {
+ GRPC_MDELEM_UNREF(channeld->mdelem_compression_algorithms[algo_idx]);
+ }
+ GRPC_MDELEM_UNREF(channeld->mdelem_accept_encoding);
}
const grpc_channel_filter grpc_compress_filter = {
- compress_start_transport_stream_op,
- grpc_channel_next_op,
- sizeof (call_data),
- init_call_elem,
- destroy_call_elem,
- sizeof (channel_data),
- init_channel_elem,
- destroy_channel_elem,
- grpc_call_next_get_peer,
- "compress"
-};
+ compress_start_transport_stream_op,
+ grpc_channel_next_op,
+ sizeof(call_data),
+ init_call_elem,
+ destroy_call_elem,
+ sizeof(channel_data),
+ init_channel_elem,
+ destroy_channel_elem,
+ grpc_call_next_get_peer,
+ "compress"};
diff --git a/src/core/channel/connected_channel.c b/src/core/channel/connected_channel.c
index bdb75474ff..ea701bc284 100644
--- a/src/core/channel/connected_channel.c
+++ b/src/core/channel/connected_channel.c
@@ -46,15 +46,11 @@
#define MAX_BUFFER_LENGTH 8192
-typedef struct connected_channel_channel_data
-{
+typedef struct connected_channel_channel_data {
grpc_transport *transport;
} channel_data;
-typedef struct connected_channel_call_data
-{
- void *unused;
-} call_data;
+typedef struct connected_channel_call_data { void *unused; } call_data;
/* We perform a small hack to locate transport data alongside the connected
channel data in call allocations, to allow everything to be pulled in minimal
@@ -65,95 +61,95 @@ typedef struct connected_channel_call_data
/* Intercept a call operation and either push it directly up or translate it
into transport stream operations */
-static void
-con_start_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void con_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- GPR_ASSERT (elem->filter == &grpc_connected_channel_filter);
- GRPC_CALL_LOG_OP (GPR_INFO, elem, op);
+ GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
+ GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
- grpc_transport_perform_stream_op (exec_ctx, chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA (calld), op);
+ grpc_transport_perform_stream_op(exec_ctx, chand->transport,
+ TRANSPORT_STREAM_FROM_CALL_DATA(calld), op);
}
-static void
-con_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op)
-{
+static void con_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem,
+ grpc_transport_op *op) {
channel_data *chand = elem->channel_data;
- grpc_transport_perform_op (exec_ctx, chand->transport, op);
+ grpc_transport_perform_op(exec_ctx, chand->transport, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
int r;
- GPR_ASSERT (elem->filter == &grpc_connected_channel_filter);
- r = grpc_transport_init_stream (exec_ctx, chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA (calld), server_transport_data, initial_op);
- GPR_ASSERT (r == 0);
+ GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
+ r = grpc_transport_init_stream(exec_ctx, chand->transport,
+ TRANSPORT_STREAM_FROM_CALL_DATA(calld),
+ server_transport_data, initial_op);
+ GPR_ASSERT(r == 0);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- GPR_ASSERT (elem->filter == &grpc_connected_channel_filter);
- grpc_transport_destroy_stream (exec_ctx, chand->transport, TRANSPORT_STREAM_FROM_CALL_DATA (calld));
+ GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
+ grpc_transport_destroy_stream(exec_ctx, chand->transport,
+ TRANSPORT_STREAM_FROM_CALL_DATA(calld));
}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
- channel_data *cd = (channel_data *) elem->channel_data;
- GPR_ASSERT (is_last);
- GPR_ASSERT (elem->filter == &grpc_connected_channel_filter);
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
+ channel_data *cd = (channel_data *)elem->channel_data;
+ GPR_ASSERT(is_last);
+ GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
cd->transport = NULL;
}
/* Destructor for channel_data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
- channel_data *cd = (channel_data *) elem->channel_data;
- GPR_ASSERT (elem->filter == &grpc_connected_channel_filter);
- grpc_transport_destroy (exec_ctx, cd->transport);
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
+ channel_data *cd = (channel_data *)elem->channel_data;
+ GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
+ grpc_transport_destroy(exec_ctx, cd->transport);
}
-static char *
-con_get_peer (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static char *con_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
channel_data *chand = elem->channel_data;
- return grpc_transport_get_peer (exec_ctx, chand->transport);
+ return grpc_transport_get_peer(exec_ctx, chand->transport);
}
const grpc_channel_filter grpc_connected_channel_filter = {
- con_start_transport_stream_op,
- con_start_transport_op,
- sizeof (call_data),
- init_call_elem,
- destroy_call_elem,
- sizeof (channel_data),
- init_channel_elem,
- destroy_channel_elem,
- con_get_peer,
- "connected",
+ con_start_transport_stream_op,
+ con_start_transport_op,
+ sizeof(call_data),
+ init_call_elem,
+ destroy_call_elem,
+ sizeof(channel_data),
+ init_channel_elem,
+ destroy_channel_elem,
+ con_get_peer,
+ "connected",
};
-void
-grpc_connected_channel_bind_transport (grpc_channel_stack * channel_stack, grpc_transport * transport)
-{
+void grpc_connected_channel_bind_transport(grpc_channel_stack *channel_stack,
+ grpc_transport *transport) {
/* Assumes that the connected channel filter is always the last filter
in a channel stack */
- grpc_channel_element *elem = grpc_channel_stack_last_element (channel_stack);
- channel_data *cd = (channel_data *) elem->channel_data;
- GPR_ASSERT (elem->filter == &grpc_connected_channel_filter);
- GPR_ASSERT (cd->transport == NULL);
+ grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
+ channel_data *cd = (channel_data *)elem->channel_data;
+ GPR_ASSERT(elem->filter == &grpc_connected_channel_filter);
+ GPR_ASSERT(cd->transport == NULL);
cd->transport = transport;
/* HACK(ctiller): increase call stack size for the channel to make space
@@ -162,5 +158,5 @@ grpc_connected_channel_bind_transport (grpc_channel_stack * channel_stack, grpc_
This is only "safe" because call stacks place no additional data after
the last call element, and the last call element MUST be the connected
channel. */
- channel_stack->call_stack_size += grpc_transport_stream_size (transport);
+ channel_stack->call_stack_size += grpc_transport_stream_size(transport);
}
diff --git a/src/core/channel/connected_channel.h b/src/core/channel/connected_channel.h
index 67bcd6c911..eac6eb7ebe 100644
--- a/src/core/channel/connected_channel.h
+++ b/src/core/channel/connected_channel.h
@@ -43,6 +43,7 @@ extern const grpc_channel_filter grpc_connected_channel_filter;
/* Post construction fixup: set the transport in the connected channel.
Must be called before any call stack using this filter is used. */
-void grpc_connected_channel_bind_transport (grpc_channel_stack * channel_stack, grpc_transport * transport);
+void grpc_connected_channel_bind_transport(grpc_channel_stack* channel_stack,
+ grpc_transport* transport);
#endif /* GRPC_INTERNAL_CORE_CHANNEL_CONNECTED_CHANNEL_H */
diff --git a/src/core/channel/context.h b/src/core/channel/context.h
index 076dc7189b..ac5796b9ef 100644
--- a/src/core/channel/context.h
+++ b/src/core/channel/context.h
@@ -35,17 +35,15 @@
#define GRPC_INTERNAL_CORE_CHANNEL_CONTEXT_H
/* Call object context pointers */
-typedef enum
-{
+typedef enum {
GRPC_CONTEXT_SECURITY = 0,
GRPC_CONTEXT_TRACING,
GRPC_CONTEXT_COUNT
} grpc_context_index;
-typedef struct
-{
+typedef struct {
void *value;
- void (*destroy) (void *);
+ void (*destroy)(void *);
} grpc_call_context_element;
#endif /* GRPC_INTERNAL_CORE_CHANNEL_CONTEXT_H */
diff --git a/src/core/channel/http_client_filter.c b/src/core/channel/http_client_filter.c
index 3421bdb7b1..721ed4da39 100644
--- a/src/core/channel/http_client_filter.c
+++ b/src/core/channel/http_client_filter.c
@@ -37,8 +37,7 @@
#include <grpc/support/string_util.h>
#include "src/core/support/string.h"
-typedef struct call_data
-{
+typedef struct call_data {
grpc_linked_mdelem method;
grpc_linked_mdelem scheme;
grpc_linked_mdelem authority;
@@ -58,8 +57,7 @@ typedef struct call_data
grpc_closure hc_on_recv;
} call_data;
-typedef struct channel_data
-{
+typedef struct channel_data {
grpc_mdelem *te_trailers;
grpc_mdelem *method;
grpc_mdelem *scheme;
@@ -69,255 +67,222 @@ typedef struct channel_data
grpc_mdelem *user_agent;
} channel_data;
-typedef struct
-{
+typedef struct {
grpc_call_element *elem;
grpc_exec_ctx *exec_ctx;
} client_recv_filter_args;
-static grpc_mdelem *
-client_recv_filter (void *user_data, grpc_mdelem * md)
-{
+static grpc_mdelem *client_recv_filter(void *user_data, grpc_mdelem *md) {
client_recv_filter_args *a = user_data;
grpc_call_element *elem = a->elem;
channel_data *channeld = elem->channel_data;
- if (md == channeld->status)
- {
- return NULL;
- }
- else if (md->key == channeld->status->key)
- {
- grpc_call_element_send_cancel (a->exec_ctx, elem);
- return NULL;
- }
- else if (md->key == channeld->content_type->key)
- {
- return NULL;
- }
+ if (md == channeld->status) {
+ return NULL;
+ } else if (md->key == channeld->status->key) {
+ grpc_call_element_send_cancel(a->exec_ctx, elem);
+ return NULL;
+ } else if (md->key == channeld->content_type->key) {
+ return NULL;
+ }
return md;
}
-static void
-hc_on_recv (grpc_exec_ctx * exec_ctx, void *user_data, int success)
-{
+static void hc_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
grpc_call_element *elem = user_data;
call_data *calld = elem->call_data;
size_t i;
size_t nops = calld->recv_ops->nops;
grpc_stream_op *ops = calld->recv_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- client_recv_filter_args a;
- if (op->type != GRPC_OP_METADATA)
- continue;
- calld->got_initial_metadata = 1;
- a.elem = elem;
- a.exec_ctx = exec_ctx;
- grpc_metadata_batch_filter (&op->data.metadata, client_recv_filter, &a);
- }
- calld->on_done_recv->cb (exec_ctx, calld->on_done_recv->cb_arg, success);
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ client_recv_filter_args a;
+ if (op->type != GRPC_OP_METADATA) continue;
+ calld->got_initial_metadata = 1;
+ a.elem = elem;
+ a.exec_ctx = exec_ctx;
+ grpc_metadata_batch_filter(&op->data.metadata, client_recv_filter, &a);
+ }
+ calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
}
-static grpc_mdelem *
-client_strip_filter (void *user_data, grpc_mdelem * md)
-{
+static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) {
grpc_call_element *elem = user_data;
channel_data *channeld = elem->channel_data;
/* eat the things we'd like to set ourselves */
- if (md->key == channeld->method->key)
- return NULL;
- if (md->key == channeld->scheme->key)
- return NULL;
- if (md->key == channeld->te_trailers->key)
- return NULL;
- if (md->key == channeld->content_type->key)
- return NULL;
- if (md->key == channeld->user_agent->key)
- return NULL;
+ if (md->key == channeld->method->key) return NULL;
+ if (md->key == channeld->scheme->key) return NULL;
+ if (md->key == channeld->te_trailers->key) return NULL;
+ if (md->key == channeld->content_type->key) return NULL;
+ if (md->key == channeld->user_agent->key) return NULL;
return md;
}
-static void
-hc_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void hc_mutate_op(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
channel_data *channeld = elem->channel_data;
size_t i;
- if (op->send_ops && !calld->sent_initial_metadata)
- {
- size_t nops = op->send_ops->nops;
- grpc_stream_op *ops = op->send_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- if (op->type != GRPC_OP_METADATA)
- continue;
- calld->sent_initial_metadata = 1;
- grpc_metadata_batch_filter (&op->data.metadata, client_strip_filter, elem);
- /* Send : prefixed headers, which have to be before any application
- layer headers. */
- grpc_metadata_batch_add_head (&op->data.metadata, &calld->method, GRPC_MDELEM_REF (channeld->method));
- grpc_metadata_batch_add_head (&op->data.metadata, &calld->scheme, GRPC_MDELEM_REF (channeld->scheme));
- grpc_metadata_batch_add_tail (&op->data.metadata, &calld->te_trailers, GRPC_MDELEM_REF (channeld->te_trailers));
- grpc_metadata_batch_add_tail (&op->data.metadata, &calld->content_type, GRPC_MDELEM_REF (channeld->content_type));
- grpc_metadata_batch_add_tail (&op->data.metadata, &calld->user_agent, GRPC_MDELEM_REF (channeld->user_agent));
- break;
- }
+ if (op->send_ops && !calld->sent_initial_metadata) {
+ size_t nops = op->send_ops->nops;
+ grpc_stream_op *ops = op->send_ops->ops;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ if (op->type != GRPC_OP_METADATA) continue;
+ calld->sent_initial_metadata = 1;
+ grpc_metadata_batch_filter(&op->data.metadata, client_strip_filter, elem);
+ /* Send : prefixed headers, which have to be before any application
+ layer headers. */
+ grpc_metadata_batch_add_head(&op->data.metadata, &calld->method,
+ GRPC_MDELEM_REF(channeld->method));
+ grpc_metadata_batch_add_head(&op->data.metadata, &calld->scheme,
+ GRPC_MDELEM_REF(channeld->scheme));
+ grpc_metadata_batch_add_tail(&op->data.metadata, &calld->te_trailers,
+ GRPC_MDELEM_REF(channeld->te_trailers));
+ grpc_metadata_batch_add_tail(&op->data.metadata, &calld->content_type,
+ GRPC_MDELEM_REF(channeld->content_type));
+ grpc_metadata_batch_add_tail(&op->data.metadata, &calld->user_agent,
+ GRPC_MDELEM_REF(channeld->user_agent));
+ break;
}
+ }
- if (op->recv_ops && !calld->got_initial_metadata)
- {
- /* substitute our callback for the higher callback */
- calld->recv_ops = op->recv_ops;
- calld->on_done_recv = op->on_done_recv;
- op->on_done_recv = &calld->hc_on_recv;
- }
+ if (op->recv_ops && !calld->got_initial_metadata) {
+ /* substitute our callback for the higher callback */
+ calld->recv_ops = op->recv_ops;
+ calld->on_done_recv = op->on_done_recv;
+ op->on_done_recv = &calld->hc_on_recv;
+ }
}
-static void
-hc_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- GRPC_CALL_LOG_OP (GPR_INFO, elem, op);
- hc_mutate_op (elem, op);
- grpc_call_next_op (exec_ctx, elem, op);
+static void hc_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
+ hc_mutate_op(elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *calld = elem->call_data;
calld->sent_initial_metadata = 0;
calld->got_initial_metadata = 0;
calld->on_done_recv = NULL;
- grpc_closure_init (&calld->hc_on_recv, hc_on_recv, elem);
- if (initial_op)
- hc_mutate_op (elem, initial_op);
+ grpc_closure_init(&calld->hc_on_recv, hc_on_recv, elem);
+ if (initial_op) hc_mutate_op(elem, initial_op);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
-}
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {}
-static const char *
-scheme_from_args (const grpc_channel_args * args)
-{
+static const char *scheme_from_args(const grpc_channel_args *args) {
unsigned i;
- if (args != NULL)
- {
- for (i = 0; i < args->num_args; ++i)
- {
- if (args->args[i].type == GRPC_ARG_STRING && strcmp (args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0)
- {
- return args->args[i].value.string;
- }
- }
+ if (args != NULL) {
+ for (i = 0; i < args->num_args; ++i) {
+ if (args->args[i].type == GRPC_ARG_STRING &&
+ strcmp(args->args[i].key, GRPC_ARG_HTTP2_SCHEME) == 0) {
+ return args->args[i].value.string;
+ }
}
+ }
return "http";
}
-static grpc_mdstr *
-user_agent_from_args (grpc_mdctx * mdctx, const grpc_channel_args * args)
-{
+static grpc_mdstr *user_agent_from_args(grpc_mdctx *mdctx,
+ const grpc_channel_args *args) {
gpr_strvec v;
size_t i;
int is_first = 1;
char *tmp;
grpc_mdstr *result;
- gpr_strvec_init (&v);
+ gpr_strvec_init(&v);
- for (i = 0; args && i < args->num_args; i++)
- {
- if (0 == strcmp (args->args[i].key, GRPC_ARG_PRIMARY_USER_AGENT_STRING))
- {
- if (args->args[i].type != GRPC_ARG_STRING)
- {
- gpr_log (GPR_ERROR, "Channel argument '%s' should be a string", GRPC_ARG_PRIMARY_USER_AGENT_STRING);
- }
- else
- {
- if (!is_first)
- gpr_strvec_add (&v, gpr_strdup (" "));
- is_first = 0;
- gpr_strvec_add (&v, gpr_strdup (args->args[i].value.string));
- }
- }
+ for (i = 0; args && i < args->num_args; i++) {
+ if (0 == strcmp(args->args[i].key, GRPC_ARG_PRIMARY_USER_AGENT_STRING)) {
+ if (args->args[i].type != GRPC_ARG_STRING) {
+ gpr_log(GPR_ERROR, "Channel argument '%s' should be a string",
+ GRPC_ARG_PRIMARY_USER_AGENT_STRING);
+ } else {
+ if (!is_first) gpr_strvec_add(&v, gpr_strdup(" "));
+ is_first = 0;
+ gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string));
+ }
}
+ }
- gpr_asprintf (&tmp, "%sgrpc-c/%s (%s)", is_first ? "" : " ", grpc_version_string (), GPR_PLATFORM_STRING);
+ gpr_asprintf(&tmp, "%sgrpc-c/%s (%s)", is_first ? "" : " ",
+ grpc_version_string(), GPR_PLATFORM_STRING);
is_first = 0;
- gpr_strvec_add (&v, tmp);
+ gpr_strvec_add(&v, tmp);
- for (i = 0; args && i < args->num_args; i++)
- {
- if (0 == strcmp (args->args[i].key, GRPC_ARG_SECONDARY_USER_AGENT_STRING))
- {
- if (args->args[i].type != GRPC_ARG_STRING)
- {
- gpr_log (GPR_ERROR, "Channel argument '%s' should be a string", GRPC_ARG_SECONDARY_USER_AGENT_STRING);
- }
- else
- {
- if (!is_first)
- gpr_strvec_add (&v, gpr_strdup (" "));
- is_first = 0;
- gpr_strvec_add (&v, gpr_strdup (args->args[i].value.string));
- }
- }
+ for (i = 0; args && i < args->num_args; i++) {
+ if (0 == strcmp(args->args[i].key, GRPC_ARG_SECONDARY_USER_AGENT_STRING)) {
+ if (args->args[i].type != GRPC_ARG_STRING) {
+ gpr_log(GPR_ERROR, "Channel argument '%s' should be a string",
+ GRPC_ARG_SECONDARY_USER_AGENT_STRING);
+ } else {
+ if (!is_first) gpr_strvec_add(&v, gpr_strdup(" "));
+ is_first = 0;
+ gpr_strvec_add(&v, gpr_strdup(args->args[i].value.string));
+ }
}
+ }
- tmp = gpr_strvec_flatten (&v, NULL);
- gpr_strvec_destroy (&v);
- result = grpc_mdstr_from_string (mdctx, tmp, 0);
- gpr_free (tmp);
+ tmp = gpr_strvec_flatten(&v, NULL);
+ gpr_strvec_destroy(&v);
+ result = grpc_mdstr_from_string(mdctx, tmp, 0);
+ gpr_free(tmp);
return result;
}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * channel_args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *channel_args,
+ grpc_mdctx *mdctx, int is_first, int is_last) {
/* grab pointers to our data from the channel element */
channel_data *channeld = elem->channel_data;
/* The first and the last filters tend to be implemented differently to
handle the case that there's no 'next' filter to call on the up or down
path */
- GPR_ASSERT (!is_last);
+ GPR_ASSERT(!is_last);
/* initialize members */
- channeld->te_trailers = grpc_mdelem_from_strings (mdctx, "te", "trailers");
- channeld->method = grpc_mdelem_from_strings (mdctx, ":method", "POST");
- channeld->scheme = grpc_mdelem_from_strings (mdctx, ":scheme", scheme_from_args (channel_args));
- channeld->content_type = grpc_mdelem_from_strings (mdctx, "content-type", "application/grpc");
- channeld->status = grpc_mdelem_from_strings (mdctx, ":status", "200");
- channeld->user_agent = grpc_mdelem_from_metadata_strings (mdctx, grpc_mdstr_from_string (mdctx, "user-agent", 0), user_agent_from_args (mdctx, channel_args));
+ channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
+ channeld->method = grpc_mdelem_from_strings(mdctx, ":method", "POST");
+ channeld->scheme = grpc_mdelem_from_strings(mdctx, ":scheme",
+ scheme_from_args(channel_args));
+ channeld->content_type =
+ grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
+ channeld->status = grpc_mdelem_from_strings(mdctx, ":status", "200");
+ channeld->user_agent = grpc_mdelem_from_metadata_strings(
+ mdctx, grpc_mdstr_from_string(mdctx, "user-agent", 0),
+ user_agent_from_args(mdctx, channel_args));
}
/* Destructor for channel data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
/* grab pointers to our data from the channel element */
channel_data *channeld = elem->channel_data;
- GRPC_MDELEM_UNREF (channeld->te_trailers);
- GRPC_MDELEM_UNREF (channeld->method);
- GRPC_MDELEM_UNREF (channeld->scheme);
- GRPC_MDELEM_UNREF (channeld->content_type);
- GRPC_MDELEM_UNREF (channeld->status);
- GRPC_MDELEM_UNREF (channeld->user_agent);
+ GRPC_MDELEM_UNREF(channeld->te_trailers);
+ GRPC_MDELEM_UNREF(channeld->method);
+ GRPC_MDELEM_UNREF(channeld->scheme);
+ GRPC_MDELEM_UNREF(channeld->content_type);
+ GRPC_MDELEM_UNREF(channeld->status);
+ GRPC_MDELEM_UNREF(channeld->user_agent);
}
const grpc_channel_filter grpc_http_client_filter = {
- hc_start_transport_op, grpc_channel_next_op, sizeof (call_data),
- init_call_elem, destroy_call_elem, sizeof (channel_data),
- init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer,
- "http-client"
-};
+ hc_start_transport_op, grpc_channel_next_op, sizeof(call_data),
+ init_call_elem, destroy_call_elem, sizeof(channel_data),
+ init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer,
+ "http-client"};
diff --git a/src/core/channel/http_server_filter.c b/src/core/channel/http_server_filter.c
index 6f776b8431..02a351d7b1 100644
--- a/src/core/channel/http_server_filter.c
+++ b/src/core/channel/http_server_filter.c
@@ -37,8 +37,7 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-typedef struct call_data
-{
+typedef struct call_data {
gpr_uint8 got_initial_metadata;
gpr_uint8 seen_path;
gpr_uint8 seen_post;
@@ -58,8 +57,7 @@ typedef struct call_data
grpc_closure hs_on_recv;
} call_data;
-typedef struct channel_data
-{
+typedef struct channel_data {
grpc_mdelem *te_trailers;
grpc_mdelem *method_post;
grpc_mdelem *http_scheme;
@@ -76,268 +74,234 @@ typedef struct channel_data
grpc_mdctx *mdctx;
} channel_data;
-typedef struct
-{
+typedef struct {
grpc_call_element *elem;
grpc_exec_ctx *exec_ctx;
} server_filter_args;
-static grpc_mdelem *
-server_filter (void *user_data, grpc_mdelem * md)
-{
+static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
server_filter_args *a = user_data;
grpc_call_element *elem = a->elem;
channel_data *channeld = elem->channel_data;
call_data *calld = elem->call_data;
/* Check if it is one of the headers we care about. */
- if (md == channeld->te_trailers || md == channeld->method_post || md == channeld->http_scheme || md == channeld->https_scheme || md == channeld->grpc_scheme || md == channeld->content_type)
- {
- /* swallow it */
- if (md == channeld->method_post)
- {
- calld->seen_post = 1;
- }
- else if (md->key == channeld->http_scheme->key)
- {
- calld->seen_scheme = 1;
- }
- else if (md == channeld->te_trailers)
- {
- calld->seen_te_trailers = 1;
- }
- /* TODO(klempner): Track that we've seen all the headers we should
- require */
- return NULL;
+ if (md == channeld->te_trailers || md == channeld->method_post ||
+ md == channeld->http_scheme || md == channeld->https_scheme ||
+ md == channeld->grpc_scheme || md == channeld->content_type) {
+ /* swallow it */
+ if (md == channeld->method_post) {
+ calld->seen_post = 1;
+ } else if (md->key == channeld->http_scheme->key) {
+ calld->seen_scheme = 1;
+ } else if (md == channeld->te_trailers) {
+ calld->seen_te_trailers = 1;
}
- else if (md->key == channeld->content_type->key)
- {
- if (strncmp (grpc_mdstr_as_c_string (md->value), "application/grpc+", 17) == 0)
- {
- /* Although the C implementation doesn't (currently) generate them,
- any custom +-suffix is explicitly valid. */
- /* TODO(klempner): We should consider preallocating common values such
- as +proto or +json, or at least stashing them if we see them. */
- /* TODO(klempner): Should we be surfacing this to application code? */
- }
- else
- {
- /* TODO(klempner): We're currently allowing this, but we shouldn't
- see it without a proxy so log for now. */
- gpr_log (GPR_INFO, "Unexpected content-type %s", channeld->content_type->key);
- }
- return NULL;
+ /* TODO(klempner): Track that we've seen all the headers we should
+ require */
+ return NULL;
+ } else if (md->key == channeld->content_type->key) {
+ if (strncmp(grpc_mdstr_as_c_string(md->value), "application/grpc+", 17) ==
+ 0) {
+ /* Although the C implementation doesn't (currently) generate them,
+ any custom +-suffix is explicitly valid. */
+ /* TODO(klempner): We should consider preallocating common values such
+ as +proto or +json, or at least stashing them if we see them. */
+ /* TODO(klempner): Should we be surfacing this to application code? */
+ } else {
+ /* TODO(klempner): We're currently allowing this, but we shouldn't
+ see it without a proxy so log for now. */
+ gpr_log(GPR_INFO, "Unexpected content-type %s",
+ channeld->content_type->key);
}
- else if (md->key == channeld->te_trailers->key || md->key == channeld->method_post->key || md->key == channeld->http_scheme->key)
- {
- gpr_log (GPR_ERROR, "Invalid %s: header: '%s'", grpc_mdstr_as_c_string (md->key), grpc_mdstr_as_c_string (md->value));
- /* swallow it and error everything out. */
- /* TODO(klempner): We ought to generate more descriptive error messages
- on the wire here. */
- grpc_call_element_send_cancel (a->exec_ctx, elem);
+ return NULL;
+ } else if (md->key == channeld->te_trailers->key ||
+ md->key == channeld->method_post->key ||
+ md->key == channeld->http_scheme->key) {
+ gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
+ grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value));
+ /* swallow it and error everything out. */
+ /* TODO(klempner): We ought to generate more descriptive error messages
+ on the wire here. */
+ grpc_call_element_send_cancel(a->exec_ctx, elem);
+ return NULL;
+ } else if (md->key == channeld->path_key) {
+ if (calld->seen_path) {
+ gpr_log(GPR_ERROR, "Received :path twice");
return NULL;
}
- else if (md->key == channeld->path_key)
- {
- if (calld->seen_path)
- {
- gpr_log (GPR_ERROR, "Received :path twice");
- return NULL;
- }
- calld->seen_path = 1;
- return md;
- }
- else if (md->key == channeld->authority_key)
- {
- calld->seen_authority = 1;
- return md;
- }
- else if (md->key == channeld->host_key)
- {
- /* translate host to :authority since :authority may be
- omitted */
- grpc_mdelem *authority = grpc_mdelem_from_metadata_strings (channeld->mdctx, GRPC_MDSTR_REF (channeld->authority_key),
- GRPC_MDSTR_REF (md->value));
- GRPC_MDELEM_UNREF (md);
- calld->seen_authority = 1;
- return authority;
- }
- else
- {
- return md;
- }
+ calld->seen_path = 1;
+ return md;
+ } else if (md->key == channeld->authority_key) {
+ calld->seen_authority = 1;
+ return md;
+ } else if (md->key == channeld->host_key) {
+ /* translate host to :authority since :authority may be
+ omitted */
+ grpc_mdelem *authority = grpc_mdelem_from_metadata_strings(
+ channeld->mdctx, GRPC_MDSTR_REF(channeld->authority_key),
+ GRPC_MDSTR_REF(md->value));
+ GRPC_MDELEM_UNREF(md);
+ calld->seen_authority = 1;
+ return authority;
+ } else {
+ return md;
+ }
}
-static void
-hs_on_recv (grpc_exec_ctx * exec_ctx, void *user_data, int success)
-{
+static void hs_on_recv(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
grpc_call_element *elem = user_data;
call_data *calld = elem->call_data;
- if (success)
- {
- size_t i;
- size_t nops = calld->recv_ops->nops;
- grpc_stream_op *ops = calld->recv_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- server_filter_args a;
- if (op->type != GRPC_OP_METADATA)
- continue;
- calld->got_initial_metadata = 1;
- a.elem = elem;
- a.exec_ctx = exec_ctx;
- grpc_metadata_batch_filter (&op->data.metadata, server_filter, &a);
- /* Have we seen the required http2 transport headers?
- (:method, :scheme, content-type, with :path and :authority covered
- at the channel level right now) */
- if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers && calld->seen_path && calld->seen_authority)
- {
- /* do nothing */
- }
- else
- {
- if (!calld->seen_path)
- {
- gpr_log (GPR_ERROR, "Missing :path header");
- }
- if (!calld->seen_authority)
- {
- gpr_log (GPR_ERROR, "Missing :authority header");
- }
- if (!calld->seen_post)
- {
- gpr_log (GPR_ERROR, "Missing :method header");
- }
- if (!calld->seen_scheme)
- {
- gpr_log (GPR_ERROR, "Missing :scheme header");
- }
- if (!calld->seen_te_trailers)
- {
- gpr_log (GPR_ERROR, "Missing te trailers header");
- }
- /* Error this call out */
- success = 0;
- grpc_call_element_send_cancel (exec_ctx, elem);
- }
- }
+ if (success) {
+ size_t i;
+ size_t nops = calld->recv_ops->nops;
+ grpc_stream_op *ops = calld->recv_ops->ops;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ server_filter_args a;
+ if (op->type != GRPC_OP_METADATA) continue;
+ calld->got_initial_metadata = 1;
+ a.elem = elem;
+ a.exec_ctx = exec_ctx;
+ grpc_metadata_batch_filter(&op->data.metadata, server_filter, &a);
+ /* Have we seen the required http2 transport headers?
+ (:method, :scheme, content-type, with :path and :authority covered
+ at the channel level right now) */
+ if (calld->seen_post && calld->seen_scheme && calld->seen_te_trailers &&
+ calld->seen_path && calld->seen_authority) {
+ /* do nothing */
+ } else {
+ if (!calld->seen_path) {
+ gpr_log(GPR_ERROR, "Missing :path header");
+ }
+ if (!calld->seen_authority) {
+ gpr_log(GPR_ERROR, "Missing :authority header");
+ }
+ if (!calld->seen_post) {
+ gpr_log(GPR_ERROR, "Missing :method header");
+ }
+ if (!calld->seen_scheme) {
+ gpr_log(GPR_ERROR, "Missing :scheme header");
+ }
+ if (!calld->seen_te_trailers) {
+ gpr_log(GPR_ERROR, "Missing te trailers header");
+ }
+ /* Error this call out */
+ success = 0;
+ grpc_call_element_send_cancel(exec_ctx, elem);
+ }
}
- calld->on_done_recv->cb (exec_ctx, calld->on_done_recv->cb_arg, success);
+ }
+ calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
}
-static void
-hs_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void hs_mutate_op(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
channel_data *channeld = elem->channel_data;
size_t i;
- if (op->send_ops && !calld->sent_status)
- {
- size_t nops = op->send_ops->nops;
- grpc_stream_op *ops = op->send_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- if (op->type != GRPC_OP_METADATA)
- continue;
- calld->sent_status = 1;
- grpc_metadata_batch_add_head (&op->data.metadata, &calld->status, GRPC_MDELEM_REF (channeld->status_ok));
- grpc_metadata_batch_add_tail (&op->data.metadata, &calld->content_type, GRPC_MDELEM_REF (channeld->content_type));
- break;
- }
+ if (op->send_ops && !calld->sent_status) {
+ size_t nops = op->send_ops->nops;
+ grpc_stream_op *ops = op->send_ops->ops;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ if (op->type != GRPC_OP_METADATA) continue;
+ calld->sent_status = 1;
+ grpc_metadata_batch_add_head(&op->data.metadata, &calld->status,
+ GRPC_MDELEM_REF(channeld->status_ok));
+ grpc_metadata_batch_add_tail(&op->data.metadata, &calld->content_type,
+ GRPC_MDELEM_REF(channeld->content_type));
+ break;
}
+ }
- if (op->recv_ops && !calld->got_initial_metadata)
- {
- /* substitute our callback for the higher callback */
- calld->recv_ops = op->recv_ops;
- calld->on_done_recv = op->on_done_recv;
- op->on_done_recv = &calld->hs_on_recv;
- }
+ if (op->recv_ops && !calld->got_initial_metadata) {
+ /* substitute our callback for the higher callback */
+ calld->recv_ops = op->recv_ops;
+ calld->on_done_recv = op->on_done_recv;
+ op->on_done_recv = &calld->hs_on_recv;
+ }
}
-static void
-hs_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- GRPC_CALL_LOG_OP (GPR_INFO, elem, op);
- hs_mutate_op (elem, op);
- grpc_call_next_op (exec_ctx, elem, op);
+static void hs_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
+ hs_mutate_op(elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
/* initialize members */
- memset (calld, 0, sizeof (*calld));
- grpc_closure_init (&calld->hs_on_recv, hs_on_recv, elem);
- if (initial_op)
- hs_mutate_op (elem, initial_op);
+ memset(calld, 0, sizeof(*calld));
+ grpc_closure_init(&calld->hs_on_recv, hs_on_recv, elem);
+ if (initial_op) hs_mutate_op(elem, initial_op);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
-}
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
/* grab pointers to our data from the channel element */
channel_data *channeld = elem->channel_data;
/* The first and the last filters tend to be implemented differently to
handle the case that there's no 'next' filter to call on the up or down
path */
- GPR_ASSERT (!is_first);
- GPR_ASSERT (!is_last);
+ GPR_ASSERT(!is_first);
+ GPR_ASSERT(!is_last);
/* initialize members */
- channeld->te_trailers = grpc_mdelem_from_strings (mdctx, "te", "trailers");
- channeld->status_ok = grpc_mdelem_from_strings (mdctx, ":status", "200");
- channeld->status_not_found = grpc_mdelem_from_strings (mdctx, ":status", "404");
- channeld->method_post = grpc_mdelem_from_strings (mdctx, ":method", "POST");
- channeld->http_scheme = grpc_mdelem_from_strings (mdctx, ":scheme", "http");
- channeld->https_scheme = grpc_mdelem_from_strings (mdctx, ":scheme", "https");
- channeld->grpc_scheme = grpc_mdelem_from_strings (mdctx, ":scheme", "grpc");
- channeld->path_key = grpc_mdstr_from_string (mdctx, ":path", 0);
- channeld->authority_key = grpc_mdstr_from_string (mdctx, ":authority", 0);
- channeld->host_key = grpc_mdstr_from_string (mdctx, "host", 0);
- channeld->content_type = grpc_mdelem_from_strings (mdctx, "content-type", "application/grpc");
+ channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
+ channeld->status_ok = grpc_mdelem_from_strings(mdctx, ":status", "200");
+ channeld->status_not_found =
+ grpc_mdelem_from_strings(mdctx, ":status", "404");
+ channeld->method_post = grpc_mdelem_from_strings(mdctx, ":method", "POST");
+ channeld->http_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "http");
+ channeld->https_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "https");
+ channeld->grpc_scheme = grpc_mdelem_from_strings(mdctx, ":scheme", "grpc");
+ channeld->path_key = grpc_mdstr_from_string(mdctx, ":path", 0);
+ channeld->authority_key = grpc_mdstr_from_string(mdctx, ":authority", 0);
+ channeld->host_key = grpc_mdstr_from_string(mdctx, "host", 0);
+ channeld->content_type =
+ grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
channeld->mdctx = mdctx;
}
/* Destructor for channel data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
/* grab pointers to our data from the channel element */
channel_data *channeld = elem->channel_data;
- GRPC_MDELEM_UNREF (channeld->te_trailers);
- GRPC_MDELEM_UNREF (channeld->status_ok);
- GRPC_MDELEM_UNREF (channeld->status_not_found);
- GRPC_MDELEM_UNREF (channeld->method_post);
- GRPC_MDELEM_UNREF (channeld->http_scheme);
- GRPC_MDELEM_UNREF (channeld->https_scheme);
- GRPC_MDELEM_UNREF (channeld->grpc_scheme);
- GRPC_MDELEM_UNREF (channeld->content_type);
- GRPC_MDSTR_UNREF (channeld->path_key);
- GRPC_MDSTR_UNREF (channeld->authority_key);
- GRPC_MDSTR_UNREF (channeld->host_key);
+ GRPC_MDELEM_UNREF(channeld->te_trailers);
+ GRPC_MDELEM_UNREF(channeld->status_ok);
+ GRPC_MDELEM_UNREF(channeld->status_not_found);
+ GRPC_MDELEM_UNREF(channeld->method_post);
+ GRPC_MDELEM_UNREF(channeld->http_scheme);
+ GRPC_MDELEM_UNREF(channeld->https_scheme);
+ GRPC_MDELEM_UNREF(channeld->grpc_scheme);
+ GRPC_MDELEM_UNREF(channeld->content_type);
+ GRPC_MDSTR_UNREF(channeld->path_key);
+ GRPC_MDSTR_UNREF(channeld->authority_key);
+ GRPC_MDSTR_UNREF(channeld->host_key);
}
const grpc_channel_filter grpc_http_server_filter = {
- hs_start_transport_op, grpc_channel_next_op, sizeof (call_data),
- init_call_elem, destroy_call_elem, sizeof (channel_data),
- init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer,
- "http-server"
-};
+ hs_start_transport_op, grpc_channel_next_op, sizeof(call_data),
+ init_call_elem, destroy_call_elem, sizeof(channel_data),
+ init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer,
+ "http-server"};
diff --git a/src/core/channel/noop_filter.c b/src/core/channel/noop_filter.c
index 7e566642f9..91b30d61ca 100644
--- a/src/core/channel/noop_filter.c
+++ b/src/core/channel/noop_filter.c
@@ -34,31 +34,25 @@
#include "src/core/channel/noop_filter.h"
#include <grpc/support/log.h>
-typedef struct call_data
-{
- int unused; /* C89 requires at least one struct element */
+typedef struct call_data {
+ int unused; /* C89 requires at least one struct element */
} call_data;
-typedef struct channel_data
-{
- int unused; /* C89 requires at least one struct element */
+typedef struct channel_data {
+ int unused; /* C89 requires at least one struct element */
} channel_data;
/* used to silence 'variable not used' warnings */
-static void
-ignore_unused (void *ignored)
-{
-}
+static void ignore_unused(void *ignored) {}
-static void
-noop_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void noop_mutate_op(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
channel_data *channeld = elem->channel_data;
- ignore_unused (calld);
- ignore_unused (channeld);
+ ignore_unused(calld);
+ ignore_unused(channeld);
/* do nothing */
}
@@ -68,19 +62,19 @@ noop_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
- a network event (or similar) from below, to receive something
op contains type and call direction information, in addition to the data
that is being sent or received. */
-static void
-noop_start_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- noop_mutate_op (elem, op);
+static void noop_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ noop_mutate_op(elem, op);
/* pass control down the stack */
- grpc_call_next_op (exec_ctx, elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
channel_data *channeld = elem->channel_data;
@@ -88,51 +82,47 @@ init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *
/* initialize members */
calld->unused = channeld->unused;
- if (initial_op)
- noop_mutate_op (elem, initial_op);
+ if (initial_op) noop_mutate_op(elem, initial_op);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
-}
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
/* grab pointers to our data from the channel element */
channel_data *channeld = elem->channel_data;
/* The first and the last filters tend to be implemented differently to
handle the case that there's no 'next' filter to call on the up or down
path */
- GPR_ASSERT (!is_first);
- GPR_ASSERT (!is_last);
+ GPR_ASSERT(!is_first);
+ GPR_ASSERT(!is_last);
/* initialize members */
channeld->unused = 0;
}
/* Destructor for channel data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
/* grab pointers to our data from the channel element */
channel_data *channeld = elem->channel_data;
- ignore_unused (channeld);
+ ignore_unused(channeld);
}
-const grpc_channel_filter grpc_no_op_filter = { noop_start_transport_stream_op,
- grpc_channel_next_op,
- sizeof (call_data),
- init_call_elem,
- destroy_call_elem,
- sizeof (channel_data),
- init_channel_elem,
- destroy_channel_elem,
- grpc_call_next_get_peer,
- "no-op"
-};
+const grpc_channel_filter grpc_no_op_filter = {noop_start_transport_stream_op,
+ grpc_channel_next_op,
+ sizeof(call_data),
+ init_call_elem,
+ destroy_call_elem,
+ sizeof(channel_data),
+ init_channel_elem,
+ destroy_channel_elem,
+ grpc_call_next_get_peer,
+ "no-op"};
diff --git a/src/core/client_config/client_config.c b/src/core/client_config/client_config.c
index fc6448201f..6ecffb3854 100644
--- a/src/core/client_config/client_config.c
+++ b/src/core/client_config/client_config.c
@@ -37,50 +37,36 @@
#include <grpc/support/alloc.h>
-struct grpc_client_config
-{
+struct grpc_client_config {
gpr_refcount refs;
grpc_lb_policy *lb_policy;
};
-grpc_client_config *
-grpc_client_config_create ()
-{
- grpc_client_config *c = gpr_malloc (sizeof (*c));
- memset (c, 0, sizeof (*c));
- gpr_ref_init (&c->refs, 1);
+grpc_client_config *grpc_client_config_create() {
+ grpc_client_config *c = gpr_malloc(sizeof(*c));
+ memset(c, 0, sizeof(*c));
+ gpr_ref_init(&c->refs, 1);
return c;
}
-void
-grpc_client_config_ref (grpc_client_config * c)
-{
- gpr_ref (&c->refs);
-}
+void grpc_client_config_ref(grpc_client_config *c) { gpr_ref(&c->refs); }
-void
-grpc_client_config_unref (grpc_exec_ctx * exec_ctx, grpc_client_config * c)
-{
- if (gpr_unref (&c->refs))
- {
- GRPC_LB_POLICY_UNREF (exec_ctx, c->lb_policy, "client_config");
- gpr_free (c);
- }
+void grpc_client_config_unref(grpc_exec_ctx *exec_ctx, grpc_client_config *c) {
+ if (gpr_unref(&c->refs)) {
+ GRPC_LB_POLICY_UNREF(exec_ctx, c->lb_policy, "client_config");
+ gpr_free(c);
+ }
}
-void
-grpc_client_config_set_lb_policy (grpc_client_config * c, grpc_lb_policy * lb_policy)
-{
- GPR_ASSERT (c->lb_policy == NULL);
- if (lb_policy)
- {
- GRPC_LB_POLICY_REF (lb_policy, "client_config");
- }
+void grpc_client_config_set_lb_policy(grpc_client_config *c,
+ grpc_lb_policy *lb_policy) {
+ GPR_ASSERT(c->lb_policy == NULL);
+ if (lb_policy) {
+ GRPC_LB_POLICY_REF(lb_policy, "client_config");
+ }
c->lb_policy = lb_policy;
}
-grpc_lb_policy *
-grpc_client_config_get_lb_policy (grpc_client_config * c)
-{
+grpc_lb_policy *grpc_client_config_get_lb_policy(grpc_client_config *c) {
return c->lb_policy;
}
diff --git a/src/core/client_config/client_config.h b/src/core/client_config/client_config.h
index c6afafe023..04bf036b00 100644
--- a/src/core/client_config/client_config.h
+++ b/src/core/client_config/client_config.h
@@ -40,11 +40,14 @@
grpc_resolver */
typedef struct grpc_client_config grpc_client_config;
-grpc_client_config *grpc_client_config_create ();
-void grpc_client_config_ref (grpc_client_config * client_config);
-void grpc_client_config_unref (grpc_exec_ctx * exec_ctx, grpc_client_config * client_config);
+grpc_client_config *grpc_client_config_create();
+void grpc_client_config_ref(grpc_client_config *client_config);
+void grpc_client_config_unref(grpc_exec_ctx *exec_ctx,
+ grpc_client_config *client_config);
-void grpc_client_config_set_lb_policy (grpc_client_config * client_config, grpc_lb_policy * lb_policy);
-grpc_lb_policy *grpc_client_config_get_lb_policy (grpc_client_config * client_config);
+void grpc_client_config_set_lb_policy(grpc_client_config *client_config,
+ grpc_lb_policy *lb_policy);
+grpc_lb_policy *grpc_client_config_get_lb_policy(
+ grpc_client_config *client_config);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_CLIENT_CONFIG_H */
diff --git a/src/core/client_config/connector.c b/src/core/client_config/connector.c
index 209c3f1767..1603ffb8be 100644
--- a/src/core/client_config/connector.c
+++ b/src/core/client_config/connector.c
@@ -33,26 +33,22 @@
#include "src/core/client_config/connector.h"
-void
-grpc_connector_ref (grpc_connector * connector)
-{
- connector->vtable->ref (connector);
+void grpc_connector_ref(grpc_connector* connector) {
+ connector->vtable->ref(connector);
}
-void
-grpc_connector_unref (grpc_exec_ctx * exec_ctx, grpc_connector * connector)
-{
- connector->vtable->unref (exec_ctx, connector);
+void grpc_connector_unref(grpc_exec_ctx* exec_ctx, grpc_connector* connector) {
+ connector->vtable->unref(exec_ctx, connector);
}
-void
-grpc_connector_connect (grpc_exec_ctx * exec_ctx, grpc_connector * connector, const grpc_connect_in_args * in_args, grpc_connect_out_args * out_args, grpc_closure * notify)
-{
- connector->vtable->connect (exec_ctx, connector, in_args, out_args, notify);
+void grpc_connector_connect(grpc_exec_ctx* exec_ctx, grpc_connector* connector,
+ const grpc_connect_in_args* in_args,
+ grpc_connect_out_args* out_args,
+ grpc_closure* notify) {
+ connector->vtable->connect(exec_ctx, connector, in_args, out_args, notify);
}
-void
-grpc_connector_shutdown (grpc_exec_ctx * exec_ctx, grpc_connector * connector)
-{
- connector->vtable->shutdown (exec_ctx, connector);
+void grpc_connector_shutdown(grpc_exec_ctx* exec_ctx,
+ grpc_connector* connector) {
+ connector->vtable->shutdown(exec_ctx, connector);
}
diff --git a/src/core/client_config/connector.h b/src/core/client_config/connector.h
index 3abee64008..e9b8be4b53 100644
--- a/src/core/client_config/connector.h
+++ b/src/core/client_config/connector.h
@@ -41,13 +41,11 @@
typedef struct grpc_connector grpc_connector;
typedef struct grpc_connector_vtable grpc_connector_vtable;
-struct grpc_connector
-{
+struct grpc_connector {
const grpc_connector_vtable *vtable;
};
-typedef struct
-{
+typedef struct {
/** set of pollsets interested in this connection */
grpc_pollset_set *interested_parties;
/** address to connect to */
@@ -59,8 +57,7 @@ typedef struct
const grpc_channel_args *channel_args;
} grpc_connect_in_args;
-typedef struct
-{
+typedef struct {
/** the connected transport */
grpc_transport *transport;
/** any additional filters (owned by the caller of connect) */
@@ -68,21 +65,26 @@ typedef struct
size_t num_filters;
} grpc_connect_out_args;
-struct grpc_connector_vtable
-{
- void (*ref) (grpc_connector * connector);
- void (*unref) (grpc_exec_ctx * exec_ctx, grpc_connector * connector);
+struct grpc_connector_vtable {
+ void (*ref)(grpc_connector *connector);
+ void (*unref)(grpc_exec_ctx *exec_ctx, grpc_connector *connector);
/** Implementation of grpc_connector_shutdown */
- void (*shutdown) (grpc_exec_ctx * exec_ctx, grpc_connector * connector);
+ void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_connector *connector);
/** Implementation of grpc_connector_connect */
- void (*connect) (grpc_exec_ctx * exec_ctx, grpc_connector * connector, const grpc_connect_in_args * in_args, grpc_connect_out_args * out_args, grpc_closure * notify);
+ void (*connect)(grpc_exec_ctx *exec_ctx, grpc_connector *connector,
+ const grpc_connect_in_args *in_args,
+ grpc_connect_out_args *out_args, grpc_closure *notify);
};
-void grpc_connector_ref (grpc_connector * connector);
-void grpc_connector_unref (grpc_exec_ctx * exec_ctx, grpc_connector * connector);
+void grpc_connector_ref(grpc_connector *connector);
+void grpc_connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *connector);
/** Connect using the connector: max one outstanding call at a time */
-void grpc_connector_connect (grpc_exec_ctx * exec_ctx, grpc_connector * connector, const grpc_connect_in_args * in_args, grpc_connect_out_args * out_args, grpc_closure * notify);
+void grpc_connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *connector,
+ const grpc_connect_in_args *in_args,
+ grpc_connect_out_args *out_args,
+ grpc_closure *notify);
/** Cancel any pending connection */
-void grpc_connector_shutdown (grpc_exec_ctx * exec_ctx, grpc_connector * connector);
+void grpc_connector_shutdown(grpc_exec_ctx *exec_ctx,
+ grpc_connector *connector);
#endif
diff --git a/src/core/client_config/lb_policies/pick_first.c b/src/core/client_config/lb_policies/pick_first.c
index 75e6c01839..5fa1ee4418 100644
--- a/src/core/client_config/lb_policies/pick_first.c
+++ b/src/core/client_config/lb_policies/pick_first.c
@@ -39,16 +39,14 @@
#include <grpc/support/alloc.h>
#include "src/core/transport/connectivity_state.h"
-typedef struct pending_pick
-{
+typedef struct pending_pick {
struct pending_pick *next;
grpc_pollset *pollset;
grpc_subchannel **target;
grpc_closure *on_complete;
} pending_pick;
-typedef struct
-{
+typedef struct {
/** base policy: must be first */
grpc_lb_policy base;
/** all our subchannels */
@@ -78,303 +76,284 @@ typedef struct
grpc_connectivity_state_tracker state_tracker;
} pick_first_lb_policy;
-static void
-del_interested_parties_locked (grpc_exec_ctx * exec_ctx, pick_first_lb_policy * p)
-{
+static void del_interested_parties_locked(grpc_exec_ctx *exec_ctx,
+ pick_first_lb_policy *p) {
pending_pick *pp;
- for (pp = p->pending_picks; pp; pp = pp->next)
- {
- grpc_subchannel_del_interested_party (exec_ctx, p->subchannels[p->checking_subchannel], pp->pollset);
- }
+ for (pp = p->pending_picks; pp; pp = pp->next) {
+ grpc_subchannel_del_interested_party(
+ exec_ctx, p->subchannels[p->checking_subchannel], pp->pollset);
+ }
}
-static void
-add_interested_parties_locked (grpc_exec_ctx * exec_ctx, pick_first_lb_policy * p)
-{
+static void add_interested_parties_locked(grpc_exec_ctx *exec_ctx,
+ pick_first_lb_policy *p) {
pending_pick *pp;
- for (pp = p->pending_picks; pp; pp = pp->next)
- {
- grpc_subchannel_add_interested_party (exec_ctx, p->subchannels[p->checking_subchannel], pp->pollset);
- }
+ for (pp = p->pending_picks; pp; pp = pp->next) {
+ grpc_subchannel_add_interested_party(
+ exec_ctx, p->subchannels[p->checking_subchannel], pp->pollset);
+ }
}
-void
-pf_destroy (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
+void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
size_t i;
- GPR_ASSERT (p->pending_picks == NULL);
- for (i = 0; i < p->num_subchannels; i++)
- {
- GRPC_SUBCHANNEL_UNREF (exec_ctx, p->subchannels[i], "pick_first");
- }
- grpc_connectivity_state_destroy (exec_ctx, &p->state_tracker);
- gpr_free (p->subchannels);
- gpr_mu_destroy (&p->mu);
- gpr_free (p);
+ GPR_ASSERT(p->pending_picks == NULL);
+ for (i = 0; i < p->num_subchannels; i++) {
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first");
+ }
+ grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
+ gpr_free(p->subchannels);
+ gpr_mu_destroy(&p->mu);
+ gpr_free(p);
}
-void
-pf_shutdown (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
+void pf_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
pending_pick *pp;
- gpr_mu_lock (&p->mu);
- del_interested_parties_locked (exec_ctx, p);
+ gpr_mu_lock(&p->mu);
+ del_interested_parties_locked(exec_ctx, p);
p->shutdown = 1;
pp = p->pending_picks;
p->pending_picks = NULL;
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
- gpr_mu_unlock (&p->mu);
- while (pp != NULL)
- {
- pending_pick *next = pp->next;
- *pp->target = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, pp->on_complete, 1);
- gpr_free (pp);
- pp = next;
- }
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
+ gpr_mu_unlock(&p->mu);
+ while (pp != NULL) {
+ pending_pick *next = pp->next;
+ *pp->target = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
+ gpr_free(pp);
+ pp = next;
+ }
}
-static void
-start_picking (grpc_exec_ctx * exec_ctx, pick_first_lb_policy * p)
-{
+static void start_picking(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p) {
p->started_picking = 1;
p->checking_subchannel = 0;
p->checking_connectivity = GRPC_CHANNEL_IDLE;
- GRPC_LB_POLICY_REF (&p->base, "pick_first_connectivity");
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[p->checking_subchannel], &p->checking_connectivity, &p->connectivity_changed);
+ GRPC_LB_POLICY_REF(&p->base, "pick_first_connectivity");
+ grpc_subchannel_notify_on_state_change(
+ exec_ctx, p->subchannels[p->checking_subchannel],
+ &p->checking_connectivity, &p->connectivity_changed);
}
-void
-pf_exit_idle (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
- gpr_mu_lock (&p->mu);
- if (!p->started_picking)
- {
- start_picking (exec_ctx, p);
- }
- gpr_mu_unlock (&p->mu);
+void pf_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
+ gpr_mu_lock(&p->mu);
+ if (!p->started_picking) {
+ start_picking(exec_ctx, p);
+ }
+ gpr_mu_unlock(&p->mu);
}
-void
-pf_pick (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol, grpc_pollset * pollset, grpc_metadata_batch * initial_metadata, grpc_subchannel ** target, grpc_closure * on_complete)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
+void pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
+ grpc_pollset *pollset, grpc_metadata_batch *initial_metadata,
+ grpc_subchannel **target, grpc_closure *on_complete) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
pending_pick *pp;
- gpr_mu_lock (&p->mu);
- if (p->selected)
- {
- gpr_mu_unlock (&p->mu);
- *target = p->selected;
- grpc_exec_ctx_enqueue (exec_ctx, on_complete, 1);
- }
- else
- {
- if (!p->started_picking)
- {
- start_picking (exec_ctx, p);
- }
- grpc_subchannel_add_interested_party (exec_ctx, p->subchannels[p->checking_subchannel], pollset);
- pp = gpr_malloc (sizeof (*pp));
- pp->next = p->pending_picks;
- pp->pollset = pollset;
- pp->target = target;
- pp->on_complete = on_complete;
- p->pending_picks = pp;
- gpr_mu_unlock (&p->mu);
+ gpr_mu_lock(&p->mu);
+ if (p->selected) {
+ gpr_mu_unlock(&p->mu);
+ *target = p->selected;
+ grpc_exec_ctx_enqueue(exec_ctx, on_complete, 1);
+ } else {
+ if (!p->started_picking) {
+ start_picking(exec_ctx, p);
}
+ grpc_subchannel_add_interested_party(
+ exec_ctx, p->subchannels[p->checking_subchannel], pollset);
+ pp = gpr_malloc(sizeof(*pp));
+ pp->next = p->pending_picks;
+ pp->pollset = pollset;
+ pp->target = target;
+ pp->on_complete = on_complete;
+ p->pending_picks = pp;
+ gpr_mu_unlock(&p->mu);
+ }
}
-static void
-pf_connectivity_changed (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
pick_first_lb_policy *p = arg;
pending_pick *pp;
- gpr_mu_lock (&p->mu);
+ gpr_mu_lock(&p->mu);
- if (p->shutdown)
- {
- gpr_mu_unlock (&p->mu);
- GRPC_LB_POLICY_UNREF (exec_ctx, &p->base, "pick_first_connectivity");
- return;
+ if (p->shutdown) {
+ gpr_mu_unlock(&p->mu);
+ GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
+ return;
+ } else if (p->selected != NULL) {
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ p->checking_connectivity, "selected_changed");
+ if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE) {
+ grpc_subchannel_notify_on_state_change(exec_ctx, p->selected,
+ &p->checking_connectivity,
+ &p->connectivity_changed);
+ } else {
+ GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
}
- else if (p->selected != NULL)
- {
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, p->checking_connectivity, "selected_changed");
- if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE)
- {
- grpc_subchannel_notify_on_state_change (exec_ctx, p->selected, &p->checking_connectivity, &p->connectivity_changed);
- }
- else
- {
- GRPC_LB_POLICY_UNREF (exec_ctx, &p->base, "pick_first_connectivity");
- }
- }
- else
- {
- loop:
- switch (p->checking_connectivity)
- {
- case GRPC_CHANNEL_READY:
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_READY, "connecting_ready");
- p->selected = p->subchannels[p->checking_subchannel];
- while ((pp = p->pending_picks))
- {
- p->pending_picks = pp->next;
- *pp->target = p->selected;
- grpc_subchannel_del_interested_party (exec_ctx, p->selected, pp->pollset);
- grpc_exec_ctx_enqueue (exec_ctx, pp->on_complete, 1);
- gpr_free (pp);
- }
- grpc_subchannel_notify_on_state_change (exec_ctx, p->selected, &p->checking_connectivity, &p->connectivity_changed);
- break;
- case GRPC_CHANNEL_TRANSIENT_FAILURE:
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, "connecting_transient_failure");
- del_interested_parties_locked (exec_ctx, p);
- p->checking_subchannel = (p->checking_subchannel + 1) % p->num_subchannels;
- p->checking_connectivity = grpc_subchannel_check_connectivity (p->subchannels[p->checking_subchannel]);
- add_interested_parties_locked (exec_ctx, p);
- if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE)
- {
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[p->checking_subchannel], &p->checking_connectivity, &p->connectivity_changed);
- }
- else
- {
- goto loop;
- }
- break;
- case GRPC_CHANNEL_CONNECTING:
- case GRPC_CHANNEL_IDLE:
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING, "connecting_changed");
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[p->checking_subchannel], &p->checking_connectivity, &p->connectivity_changed);
- break;
- case GRPC_CHANNEL_FATAL_FAILURE:
- del_interested_parties_locked (exec_ctx, p);
- GPR_SWAP (grpc_subchannel *, p->subchannels[p->checking_subchannel], p->subchannels[p->num_subchannels - 1]);
- p->num_subchannels--;
- GRPC_SUBCHANNEL_UNREF (exec_ctx, p->subchannels[p->num_subchannels], "pick_first");
- if (p->num_subchannels == 0)
- {
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_FATAL_FAILURE, "no_more_channels");
- while ((pp = p->pending_picks))
- {
- p->pending_picks = pp->next;
- *pp->target = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, pp->on_complete, 1);
- gpr_free (pp);
- }
- GRPC_LB_POLICY_UNREF (exec_ctx, &p->base, "pick_first_connectivity");
- }
- else
- {
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, "subchannel_failed");
- p->checking_subchannel %= p->num_subchannels;
- p->checking_connectivity = grpc_subchannel_check_connectivity (p->subchannels[p->checking_subchannel]);
- add_interested_parties_locked (exec_ctx, p);
- goto loop;
- }
- }
+ } else {
+ loop:
+ switch (p->checking_connectivity) {
+ case GRPC_CHANNEL_READY:
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_READY, "connecting_ready");
+ p->selected = p->subchannels[p->checking_subchannel];
+ while ((pp = p->pending_picks)) {
+ p->pending_picks = pp->next;
+ *pp->target = p->selected;
+ grpc_subchannel_del_interested_party(exec_ctx, p->selected,
+ pp->pollset);
+ grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
+ gpr_free(pp);
+ }
+ grpc_subchannel_notify_on_state_change(exec_ctx, p->selected,
+ &p->checking_connectivity,
+ &p->connectivity_changed);
+ break;
+ case GRPC_CHANNEL_TRANSIENT_FAILURE:
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_TRANSIENT_FAILURE,
+ "connecting_transient_failure");
+ del_interested_parties_locked(exec_ctx, p);
+ p->checking_subchannel =
+ (p->checking_subchannel + 1) % p->num_subchannels;
+ p->checking_connectivity = grpc_subchannel_check_connectivity(
+ p->subchannels[p->checking_subchannel]);
+ add_interested_parties_locked(exec_ctx, p);
+ if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
+ grpc_subchannel_notify_on_state_change(
+ exec_ctx, p->subchannels[p->checking_subchannel],
+ &p->checking_connectivity, &p->connectivity_changed);
+ } else {
+ goto loop;
+ }
+ break;
+ case GRPC_CHANNEL_CONNECTING:
+ case GRPC_CHANNEL_IDLE:
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_CONNECTING,
+ "connecting_changed");
+ grpc_subchannel_notify_on_state_change(
+ exec_ctx, p->subchannels[p->checking_subchannel],
+ &p->checking_connectivity, &p->connectivity_changed);
+ break;
+ case GRPC_CHANNEL_FATAL_FAILURE:
+ del_interested_parties_locked(exec_ctx, p);
+ GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel],
+ p->subchannels[p->num_subchannels - 1]);
+ p->num_subchannels--;
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels],
+ "pick_first");
+ if (p->num_subchannels == 0) {
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_FATAL_FAILURE,
+ "no_more_channels");
+ while ((pp = p->pending_picks)) {
+ p->pending_picks = pp->next;
+ *pp->target = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
+ gpr_free(pp);
+ }
+ GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
+ } else {
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_TRANSIENT_FAILURE,
+ "subchannel_failed");
+ p->checking_subchannel %= p->num_subchannels;
+ p->checking_connectivity = grpc_subchannel_check_connectivity(
+ p->subchannels[p->checking_subchannel]);
+ add_interested_parties_locked(exec_ctx, p);
+ goto loop;
+ }
}
+ }
- gpr_mu_unlock (&p->mu);
+ gpr_mu_unlock(&p->mu);
}
-static void
-pf_broadcast (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol, grpc_transport_op * op)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
+static void pf_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
+ grpc_transport_op *op) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
size_t i;
size_t n;
grpc_subchannel **subchannels;
- gpr_mu_lock (&p->mu);
+ gpr_mu_lock(&p->mu);
n = p->num_subchannels;
- subchannels = gpr_malloc (n * sizeof (*subchannels));
- for (i = 0; i < n; i++)
- {
- subchannels[i] = p->subchannels[i];
- GRPC_SUBCHANNEL_REF (subchannels[i], "pf_broadcast");
- }
- gpr_mu_unlock (&p->mu);
+ subchannels = gpr_malloc(n * sizeof(*subchannels));
+ for (i = 0; i < n; i++) {
+ subchannels[i] = p->subchannels[i];
+ GRPC_SUBCHANNEL_REF(subchannels[i], "pf_broadcast");
+ }
+ gpr_mu_unlock(&p->mu);
- for (i = 0; i < n; i++)
- {
- grpc_subchannel_process_transport_op (exec_ctx, subchannels[i], op);
- GRPC_SUBCHANNEL_UNREF (exec_ctx, subchannels[i], "pf_broadcast");
- }
- gpr_free (subchannels);
+ for (i = 0; i < n; i++) {
+ grpc_subchannel_process_transport_op(exec_ctx, subchannels[i], op);
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pf_broadcast");
+ }
+ gpr_free(subchannels);
}
-static grpc_connectivity_state
-pf_check_connectivity (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
+static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *pol) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
grpc_connectivity_state st;
- gpr_mu_lock (&p->mu);
- st = grpc_connectivity_state_check (&p->state_tracker);
- gpr_mu_unlock (&p->mu);
+ gpr_mu_lock(&p->mu);
+ st = grpc_connectivity_state_check(&p->state_tracker);
+ gpr_mu_unlock(&p->mu);
return st;
}
-void
-pf_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol, grpc_connectivity_state * current, grpc_closure * notify)
-{
- pick_first_lb_policy *p = (pick_first_lb_policy *) pol;
- gpr_mu_lock (&p->mu);
- grpc_connectivity_state_notify_on_state_change (exec_ctx, &p->state_tracker, current, notify);
- gpr_mu_unlock (&p->mu);
+void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
+ grpc_connectivity_state *current,
+ grpc_closure *notify) {
+ pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
+ gpr_mu_lock(&p->mu);
+ grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
+ current, notify);
+ gpr_mu_unlock(&p->mu);
}
static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = {
- pf_destroy,
- pf_shutdown,
- pf_pick,
- pf_exit_idle,
- pf_broadcast,
- pf_check_connectivity,
- pf_notify_on_state_change
-};
+ pf_destroy,
+ pf_shutdown,
+ pf_pick,
+ pf_exit_idle,
+ pf_broadcast,
+ pf_check_connectivity,
+ pf_notify_on_state_change};
-static void
-pick_first_factory_ref (grpc_lb_policy_factory * factory)
-{
-}
+static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {}
-static void
-pick_first_factory_unref (grpc_lb_policy_factory * factory)
-{
-}
+static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {}
-static grpc_lb_policy *
-create_pick_first (grpc_lb_policy_factory * factory, grpc_lb_policy_args * args)
-{
- pick_first_lb_policy *p = gpr_malloc (sizeof (*p));
- GPR_ASSERT (args->num_subchannels > 0);
- memset (p, 0, sizeof (*p));
- grpc_lb_policy_init (&p->base, &pick_first_lb_policy_vtable);
- p->subchannels = gpr_malloc (sizeof (grpc_subchannel *) * args->num_subchannels);
+static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory,
+ grpc_lb_policy_args *args) {
+ pick_first_lb_policy *p = gpr_malloc(sizeof(*p));
+ GPR_ASSERT(args->num_subchannels > 0);
+ memset(p, 0, sizeof(*p));
+ grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable);
+ p->subchannels =
+ gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels);
p->num_subchannels = args->num_subchannels;
- grpc_connectivity_state_init (&p->state_tracker, GRPC_CHANNEL_IDLE, "pick_first");
- memcpy (p->subchannels, args->subchannels, sizeof (grpc_subchannel *) * args->num_subchannels);
- grpc_closure_init (&p->connectivity_changed, pf_connectivity_changed, p);
- gpr_mu_init (&p->mu);
+ grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
+ "pick_first");
+ memcpy(p->subchannels, args->subchannels,
+ sizeof(grpc_subchannel *) * args->num_subchannels);
+ grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p);
+ gpr_mu_init(&p->mu);
return &p->base;
}
static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = {
- pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
- "pick_first"
-};
+ pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
+ "pick_first"};
static grpc_lb_policy_factory pick_first_lb_policy_factory = {
- &pick_first_factory_vtable
-};
+ &pick_first_factory_vtable};
-grpc_lb_policy_factory *
-grpc_pick_first_lb_factory_create ()
-{
+grpc_lb_policy_factory *grpc_pick_first_lb_factory_create() {
return &pick_first_lb_policy_factory;
}
diff --git a/src/core/client_config/lb_policies/pick_first.h b/src/core/client_config/lb_policies/pick_first.h
index 72a4e7c32c..3ca53ad42a 100644
--- a/src/core/client_config/lb_policies/pick_first.h
+++ b/src/core/client_config/lb_policies/pick_first.h
@@ -38,6 +38,6 @@
/** Returns a load balancing factory for the pick first policy, which picks up
* the first subchannel from \a subchannels to succesfully connect */
-grpc_lb_policy_factory *grpc_pick_first_lb_factory_create ();
+grpc_lb_policy_factory *grpc_pick_first_lb_factory_create();
#endif
diff --git a/src/core/client_config/lb_policies/round_robin.c b/src/core/client_config/lb_policies/round_robin.c
index 5f8976396b..479c376724 100644
--- a/src/core/client_config/lb_policies/round_robin.c
+++ b/src/core/client_config/lb_policies/round_robin.c
@@ -43,8 +43,7 @@ int grpc_lb_round_robin_trace = 0;
/** List of entities waiting for a pick.
*
* Once a pick is available, \a target is updated and \a on_complete called. */
-typedef struct pending_pick
-{
+typedef struct pending_pick {
struct pending_pick *next;
grpc_pollset *pollset;
grpc_subchannel **target;
@@ -52,21 +51,18 @@ typedef struct pending_pick
} pending_pick;
/** List of subchannels in a connectivity READY state */
-typedef struct ready_list
-{
+typedef struct ready_list {
grpc_subchannel *subchannel;
struct ready_list *next;
struct ready_list *prev;
} ready_list;
-typedef struct
-{
+typedef struct {
size_t subchannel_idx; /**< Index over p->subchannels */
- void *p; /**< round_robin_lb_policy instance */
+ void *p; /**< round_robin_lb_policy instance */
} connectivity_changed_cb_arg;
-typedef struct
-{
+typedef struct {
/** base policy: must be first */
grpc_lb_policy base;
@@ -110,264 +106,224 @@ typedef struct
*
* Note that this function does *not* advance p->ready_list_last_pick. Use \a
* advance_last_picked_locked() for that. */
-static ready_list *
-peek_next_connected_locked (const round_robin_lb_policy * p)
-{
+static ready_list *peek_next_connected_locked(const round_robin_lb_policy *p) {
ready_list *selected;
selected = p->ready_list_last_pick->next;
- while (selected != NULL)
- {
- if (selected == &p->ready_list)
- {
- GPR_ASSERT (selected->subchannel == NULL);
- /* skip dummy root */
- selected = selected->next;
- }
- else
- {
- GPR_ASSERT (selected->subchannel != NULL);
- return selected;
- }
+ while (selected != NULL) {
+ if (selected == &p->ready_list) {
+ GPR_ASSERT(selected->subchannel == NULL);
+ /* skip dummy root */
+ selected = selected->next;
+ } else {
+ GPR_ASSERT(selected->subchannel != NULL);
+ return selected;
}
+ }
return NULL;
}
/** Advance the \a ready_list picking head. */
-static void
-advance_last_picked_locked (round_robin_lb_policy * p)
-{
- if (p->ready_list_last_pick->next != NULL)
- { /* non-empty list */
+static void advance_last_picked_locked(round_robin_lb_policy *p) {
+ if (p->ready_list_last_pick->next != NULL) { /* non-empty list */
+ p->ready_list_last_pick = p->ready_list_last_pick->next;
+ if (p->ready_list_last_pick == &p->ready_list) {
+ /* skip dummy root */
p->ready_list_last_pick = p->ready_list_last_pick->next;
- if (p->ready_list_last_pick == &p->ready_list)
- {
- /* skip dummy root */
- p->ready_list_last_pick = p->ready_list_last_pick->next;
- }
- }
- else
- { /* should be an empty list */
- GPR_ASSERT (p->ready_list_last_pick == &p->ready_list);
- }
-
- if (grpc_lb_round_robin_trace)
- {
- gpr_log (GPR_DEBUG, "[READYLIST] ADVANCED LAST PICK. NOW AT NODE %p (SC %p)", p->ready_list_last_pick, p->ready_list_last_pick->subchannel);
}
+ } else { /* should be an empty list */
+ GPR_ASSERT(p->ready_list_last_pick == &p->ready_list);
+ }
+
+ if (grpc_lb_round_robin_trace) {
+ gpr_log(GPR_DEBUG, "[READYLIST] ADVANCED LAST PICK. NOW AT NODE %p (SC %p)",
+ p->ready_list_last_pick, p->ready_list_last_pick->subchannel);
+ }
}
/** Prepends (relative to the root at p->ready_list) the connected subchannel \a
* csc to the list of ready subchannels. */
-static ready_list *
-add_connected_sc_locked (round_robin_lb_policy * p, grpc_subchannel * csc)
-{
- ready_list *new_elem = gpr_malloc (sizeof (ready_list));
+static ready_list *add_connected_sc_locked(round_robin_lb_policy *p,
+ grpc_subchannel *csc) {
+ ready_list *new_elem = gpr_malloc(sizeof(ready_list));
new_elem->subchannel = csc;
- if (p->ready_list.prev == NULL)
- {
- /* first element */
- new_elem->next = &p->ready_list;
- new_elem->prev = &p->ready_list;
- p->ready_list.next = new_elem;
- p->ready_list.prev = new_elem;
- }
- else
- {
- new_elem->next = &p->ready_list;
- new_elem->prev = p->ready_list.prev;
- p->ready_list.prev->next = new_elem;
- p->ready_list.prev = new_elem;
- }
- if (grpc_lb_round_robin_trace)
- {
- gpr_log (GPR_DEBUG, "[READYLIST] ADDING NODE %p (SC %p)", new_elem, csc);
- }
+ if (p->ready_list.prev == NULL) {
+ /* first element */
+ new_elem->next = &p->ready_list;
+ new_elem->prev = &p->ready_list;
+ p->ready_list.next = new_elem;
+ p->ready_list.prev = new_elem;
+ } else {
+ new_elem->next = &p->ready_list;
+ new_elem->prev = p->ready_list.prev;
+ p->ready_list.prev->next = new_elem;
+ p->ready_list.prev = new_elem;
+ }
+ if (grpc_lb_round_robin_trace) {
+ gpr_log(GPR_DEBUG, "[READYLIST] ADDING NODE %p (SC %p)", new_elem, csc);
+ }
return new_elem;
}
/** Removes \a node from the list of connected subchannels */
-static void
-remove_disconnected_sc_locked (round_robin_lb_policy * p, ready_list * node)
-{
- if (node == NULL)
- {
- return;
- }
- if (node == p->ready_list_last_pick)
- {
- /* If removing the lastly picked node, reset the last pick pointer to the
- * dummy root of the list */
- p->ready_list_last_pick = &p->ready_list;
- }
+static void remove_disconnected_sc_locked(round_robin_lb_policy *p,
+ ready_list *node) {
+ if (node == NULL) {
+ return;
+ }
+ if (node == p->ready_list_last_pick) {
+ /* If removing the lastly picked node, reset the last pick pointer to the
+ * dummy root of the list */
+ p->ready_list_last_pick = &p->ready_list;
+ }
/* removing last item */
- if (node->next == &p->ready_list && node->prev == &p->ready_list)
- {
- GPR_ASSERT (p->ready_list.next == node);
- GPR_ASSERT (p->ready_list.prev == node);
- p->ready_list.next = NULL;
- p->ready_list.prev = NULL;
- }
- else
- {
- node->prev->next = node->next;
- node->next->prev = node->prev;
- }
-
- if (grpc_lb_round_robin_trace)
- {
- gpr_log (GPR_DEBUG, "[READYLIST] REMOVED NODE %p (SC %p)", node, node->subchannel);
- }
+ if (node->next == &p->ready_list && node->prev == &p->ready_list) {
+ GPR_ASSERT(p->ready_list.next == node);
+ GPR_ASSERT(p->ready_list.prev == node);
+ p->ready_list.next = NULL;
+ p->ready_list.prev = NULL;
+ } else {
+ node->prev->next = node->next;
+ node->next->prev = node->prev;
+ }
+
+ if (grpc_lb_round_robin_trace) {
+ gpr_log(GPR_DEBUG, "[READYLIST] REMOVED NODE %p (SC %p)", node,
+ node->subchannel);
+ }
node->next = NULL;
node->prev = NULL;
node->subchannel = NULL;
- gpr_free (node);
+ gpr_free(node);
}
-static void
-del_interested_parties_locked (grpc_exec_ctx * exec_ctx, round_robin_lb_policy * p, const size_t subchannel_idx)
-{
+static void del_interested_parties_locked(grpc_exec_ctx *exec_ctx,
+ round_robin_lb_policy *p,
+ const size_t subchannel_idx) {
pending_pick *pp;
- for (pp = p->pending_picks; pp; pp = pp->next)
- {
- grpc_subchannel_del_interested_party (exec_ctx, p->subchannels[subchannel_idx], pp->pollset);
- }
+ for (pp = p->pending_picks; pp; pp = pp->next) {
+ grpc_subchannel_del_interested_party(
+ exec_ctx, p->subchannels[subchannel_idx], pp->pollset);
+ }
}
-void
-rr_destroy (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
+void rr_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
size_t i;
ready_list *elem;
- for (i = 0; i < p->num_subchannels; i++)
- {
- del_interested_parties_locked (exec_ctx, p, i);
- }
- for (i = 0; i < p->num_subchannels; i++)
- {
- GRPC_SUBCHANNEL_UNREF (exec_ctx, p->subchannels[i], "round_robin");
- }
- gpr_free (p->connectivity_changed_cbs);
- gpr_free (p->subchannel_connectivity);
-
- grpc_connectivity_state_destroy (exec_ctx, &p->state_tracker);
- gpr_free (p->subchannels);
- gpr_mu_destroy (&p->mu);
+ for (i = 0; i < p->num_subchannels; i++) {
+ del_interested_parties_locked(exec_ctx, p, i);
+ }
+ for (i = 0; i < p->num_subchannels; i++) {
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "round_robin");
+ }
+ gpr_free(p->connectivity_changed_cbs);
+ gpr_free(p->subchannel_connectivity);
+
+ grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
+ gpr_free(p->subchannels);
+ gpr_mu_destroy(&p->mu);
elem = p->ready_list.next;
- while (elem != NULL && elem != &p->ready_list)
- {
- ready_list *tmp;
- tmp = elem->next;
- elem->next = NULL;
- elem->prev = NULL;
- elem->subchannel = NULL;
- gpr_free (elem);
- elem = tmp;
- }
- gpr_free (p->subchannel_index_to_readylist_node);
- gpr_free (p->cb_args);
- gpr_free (p);
+ while (elem != NULL && elem != &p->ready_list) {
+ ready_list *tmp;
+ tmp = elem->next;
+ elem->next = NULL;
+ elem->prev = NULL;
+ elem->subchannel = NULL;
+ gpr_free(elem);
+ elem = tmp;
+ }
+ gpr_free(p->subchannel_index_to_readylist_node);
+ gpr_free(p->cb_args);
+ gpr_free(p);
}
-void
-rr_shutdown (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
+void rr_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
size_t i;
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
pending_pick *pp;
- gpr_mu_lock (&p->mu);
+ gpr_mu_lock(&p->mu);
- for (i = 0; i < p->num_subchannels; i++)
- {
- del_interested_parties_locked (exec_ctx, p, i);
- }
+ for (i = 0; i < p->num_subchannels; i++) {
+ del_interested_parties_locked(exec_ctx, p, i);
+ }
p->shutdown = 1;
- while ((pp = p->pending_picks))
- {
- p->pending_picks = pp->next;
- *pp->target = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, pp->on_complete, 0);
- gpr_free (pp);
- }
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
- gpr_mu_unlock (&p->mu);
+ while ((pp = p->pending_picks)) {
+ p->pending_picks = pp->next;
+ *pp->target = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 0);
+ gpr_free(pp);
+ }
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
+ gpr_mu_unlock(&p->mu);
}
-static void
-start_picking (grpc_exec_ctx * exec_ctx, round_robin_lb_policy * p)
-{
+static void start_picking(grpc_exec_ctx *exec_ctx, round_robin_lb_policy *p) {
size_t i;
p->started_picking = 1;
- for (i = 0; i < p->num_subchannels; i++)
- {
- p->subchannel_connectivity[i] = GRPC_CHANNEL_IDLE;
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[i], &p->subchannel_connectivity[i], &p->connectivity_changed_cbs[i]);
- GRPC_LB_POLICY_REF (&p->base, "round_robin_connectivity");
- }
+ for (i = 0; i < p->num_subchannels; i++) {
+ p->subchannel_connectivity[i] = GRPC_CHANNEL_IDLE;
+ grpc_subchannel_notify_on_state_change(exec_ctx, p->subchannels[i],
+ &p->subchannel_connectivity[i],
+ &p->connectivity_changed_cbs[i]);
+ GRPC_LB_POLICY_REF(&p->base, "round_robin_connectivity");
+ }
}
-void
-rr_exit_idle (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
- gpr_mu_lock (&p->mu);
- if (!p->started_picking)
- {
- start_picking (exec_ctx, p);
- }
- gpr_mu_unlock (&p->mu);
+void rr_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
+ gpr_mu_lock(&p->mu);
+ if (!p->started_picking) {
+ start_picking(exec_ctx, p);
+ }
+ gpr_mu_unlock(&p->mu);
}
-void
-rr_pick (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol, grpc_pollset * pollset, grpc_metadata_batch * initial_metadata, grpc_subchannel ** target, grpc_closure * on_complete)
-{
+void rr_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
+ grpc_pollset *pollset, grpc_metadata_batch *initial_metadata,
+ grpc_subchannel **target, grpc_closure *on_complete) {
size_t i;
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
pending_pick *pp;
ready_list *selected;
- gpr_mu_lock (&p->mu);
- if ((selected = peek_next_connected_locked (p)))
- {
- gpr_mu_unlock (&p->mu);
- *target = selected->subchannel;
- if (grpc_lb_round_robin_trace)
- {
- gpr_log (GPR_DEBUG, "[RR PICK] TARGET <-- SUBCHANNEL %p (NODE %p)", selected->subchannel, selected);
- }
- /* only advance the last picked pointer if the selection was used */
- advance_last_picked_locked (p);
- on_complete->cb (exec_ctx, on_complete->cb_arg, 1);
+ gpr_mu_lock(&p->mu);
+ if ((selected = peek_next_connected_locked(p))) {
+ gpr_mu_unlock(&p->mu);
+ *target = selected->subchannel;
+ if (grpc_lb_round_robin_trace) {
+ gpr_log(GPR_DEBUG, "[RR PICK] TARGET <-- SUBCHANNEL %p (NODE %p)",
+ selected->subchannel, selected);
+ }
+ /* only advance the last picked pointer if the selection was used */
+ advance_last_picked_locked(p);
+ on_complete->cb(exec_ctx, on_complete->cb_arg, 1);
+ } else {
+ if (!p->started_picking) {
+ start_picking(exec_ctx, p);
}
- else
- {
- if (!p->started_picking)
- {
- start_picking (exec_ctx, p);
- }
- for (i = 0; i < p->num_subchannels; i++)
- {
- grpc_subchannel_add_interested_party (exec_ctx, p->subchannels[i], pollset);
- }
- pp = gpr_malloc (sizeof (*pp));
- pp->next = p->pending_picks;
- pp->pollset = pollset;
- pp->target = target;
- pp->on_complete = on_complete;
- p->pending_picks = pp;
- gpr_mu_unlock (&p->mu);
+ for (i = 0; i < p->num_subchannels; i++) {
+ grpc_subchannel_add_interested_party(exec_ctx, p->subchannels[i],
+ pollset);
}
+ pp = gpr_malloc(sizeof(*pp));
+ pp->next = p->pending_picks;
+ pp->pollset = pollset;
+ pp->target = target;
+ pp->on_complete = on_complete;
+ p->pending_picks = pp;
+ gpr_mu_unlock(&p->mu);
+ }
}
-static void
-rr_connectivity_changed (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
connectivity_changed_cb_arg *cb_arg = arg;
round_robin_lb_policy *p = cb_arg->p;
/* index over p->subchannels of this cb's subchannel */
@@ -380,194 +336,198 @@ rr_connectivity_changed (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
/* connectivity state of this cb's subchannel */
grpc_connectivity_state *this_connectivity;
- gpr_mu_lock (&p->mu);
+ gpr_mu_lock(&p->mu);
this_connectivity = &p->subchannel_connectivity[this_idx];
- if (p->shutdown)
- {
- unref = 1;
- }
- else
- {
- switch (*this_connectivity)
- {
- case GRPC_CHANNEL_READY:
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_READY, "connecting_ready");
- /* add the newly connected subchannel to the list of connected ones.
- * Note that it goes to the "end of the line". */
- p->subchannel_index_to_readylist_node[this_idx] = add_connected_sc_locked (p, p->subchannels[this_idx]);
- /* at this point we know there's at least one suitable subchannel. Go
- * ahead and pick one and notify the pending suitors in
- * p->pending_picks. This preemtively replicates rr_pick()'s actions. */
- selected = peek_next_connected_locked (p);
- if (p->pending_picks != NULL)
- {
- /* if the selected subchannel is going to be used for the pending
- * picks, update the last picked pointer */
- advance_last_picked_locked (p);
- }
- while ((pp = p->pending_picks))
- {
- p->pending_picks = pp->next;
- *pp->target = selected->subchannel;
- if (grpc_lb_round_robin_trace)
- {
- gpr_log (GPR_DEBUG, "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)", selected->subchannel, selected);
- }
- grpc_subchannel_del_interested_party (exec_ctx, selected->subchannel, pp->pollset);
- grpc_exec_ctx_enqueue (exec_ctx, pp->on_complete, 1);
- gpr_free (pp);
- }
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[this_idx], this_connectivity, &p->connectivity_changed_cbs[this_idx]);
- break;
- case GRPC_CHANNEL_CONNECTING:
- case GRPC_CHANNEL_IDLE:
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, *this_connectivity, "connecting_changed");
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[this_idx], this_connectivity, &p->connectivity_changed_cbs[this_idx]);
- break;
- case GRPC_CHANNEL_TRANSIENT_FAILURE:
- del_interested_parties_locked (exec_ctx, p, this_idx);
- /* renew state notification */
- grpc_subchannel_notify_on_state_change (exec_ctx, p->subchannels[this_idx], this_connectivity, &p->connectivity_changed_cbs[this_idx]);
-
- /* remove from ready list if still present */
- if (p->subchannel_index_to_readylist_node[this_idx] != NULL)
- {
- remove_disconnected_sc_locked (p, p->subchannel_index_to_readylist_node[this_idx]);
- p->subchannel_index_to_readylist_node[this_idx] = NULL;
- }
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, "connecting_transient_failure");
- break;
- case GRPC_CHANNEL_FATAL_FAILURE:
- del_interested_parties_locked (exec_ctx, p, this_idx);
- if (p->subchannel_index_to_readylist_node[this_idx] != NULL)
- {
- remove_disconnected_sc_locked (p, p->subchannel_index_to_readylist_node[this_idx]);
- p->subchannel_index_to_readylist_node[this_idx] = NULL;
- }
-
- GPR_SWAP (grpc_subchannel *, p->subchannels[this_idx], p->subchannels[p->num_subchannels - 1]);
- p->num_subchannels--;
- GRPC_SUBCHANNEL_UNREF (exec_ctx, p->subchannels[p->num_subchannels], "round_robin");
-
- if (p->num_subchannels == 0)
- {
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_FATAL_FAILURE, "no_more_channels");
- while ((pp = p->pending_picks))
- {
- p->pending_picks = pp->next;
- *pp->target = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, pp->on_complete, 1);
- gpr_free (pp);
- }
- unref = 1;
- }
- else
- {
- grpc_connectivity_state_set (exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, "subchannel_failed");
- }
- } /* switch */
- } /* !unref */
-
- gpr_mu_unlock (&p->mu);
-
- if (unref)
- {
- GRPC_LB_POLICY_UNREF (exec_ctx, &p->base, "round_robin_connectivity");
- }
+ if (p->shutdown) {
+ unref = 1;
+ } else {
+ switch (*this_connectivity) {
+ case GRPC_CHANNEL_READY:
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_READY, "connecting_ready");
+ /* add the newly connected subchannel to the list of connected ones.
+ * Note that it goes to the "end of the line". */
+ p->subchannel_index_to_readylist_node[this_idx] =
+ add_connected_sc_locked(p, p->subchannels[this_idx]);
+ /* at this point we know there's at least one suitable subchannel. Go
+ * ahead and pick one and notify the pending suitors in
+ * p->pending_picks. This preemtively replicates rr_pick()'s actions. */
+ selected = peek_next_connected_locked(p);
+ if (p->pending_picks != NULL) {
+ /* if the selected subchannel is going to be used for the pending
+ * picks, update the last picked pointer */
+ advance_last_picked_locked(p);
+ }
+ while ((pp = p->pending_picks)) {
+ p->pending_picks = pp->next;
+ *pp->target = selected->subchannel;
+ if (grpc_lb_round_robin_trace) {
+ gpr_log(GPR_DEBUG,
+ "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)",
+ selected->subchannel, selected);
+ }
+ grpc_subchannel_del_interested_party(exec_ctx, selected->subchannel,
+ pp->pollset);
+ grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
+ gpr_free(pp);
+ }
+ grpc_subchannel_notify_on_state_change(
+ exec_ctx, p->subchannels[this_idx], this_connectivity,
+ &p->connectivity_changed_cbs[this_idx]);
+ break;
+ case GRPC_CHANNEL_CONNECTING:
+ case GRPC_CHANNEL_IDLE:
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ *this_connectivity, "connecting_changed");
+ grpc_subchannel_notify_on_state_change(
+ exec_ctx, p->subchannels[this_idx], this_connectivity,
+ &p->connectivity_changed_cbs[this_idx]);
+ break;
+ case GRPC_CHANNEL_TRANSIENT_FAILURE:
+ del_interested_parties_locked(exec_ctx, p, this_idx);
+ /* renew state notification */
+ grpc_subchannel_notify_on_state_change(
+ exec_ctx, p->subchannels[this_idx], this_connectivity,
+ &p->connectivity_changed_cbs[this_idx]);
+
+ /* remove from ready list if still present */
+ if (p->subchannel_index_to_readylist_node[this_idx] != NULL) {
+ remove_disconnected_sc_locked(
+ p, p->subchannel_index_to_readylist_node[this_idx]);
+ p->subchannel_index_to_readylist_node[this_idx] = NULL;
+ }
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_TRANSIENT_FAILURE,
+ "connecting_transient_failure");
+ break;
+ case GRPC_CHANNEL_FATAL_FAILURE:
+ del_interested_parties_locked(exec_ctx, p, this_idx);
+ if (p->subchannel_index_to_readylist_node[this_idx] != NULL) {
+ remove_disconnected_sc_locked(
+ p, p->subchannel_index_to_readylist_node[this_idx]);
+ p->subchannel_index_to_readylist_node[this_idx] = NULL;
+ }
+
+ GPR_SWAP(grpc_subchannel *, p->subchannels[this_idx],
+ p->subchannels[p->num_subchannels - 1]);
+ p->num_subchannels--;
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels],
+ "round_robin");
+
+ if (p->num_subchannels == 0) {
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_FATAL_FAILURE,
+ "no_more_channels");
+ while ((pp = p->pending_picks)) {
+ p->pending_picks = pp->next;
+ *pp->target = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
+ gpr_free(pp);
+ }
+ unref = 1;
+ } else {
+ grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
+ GRPC_CHANNEL_TRANSIENT_FAILURE,
+ "subchannel_failed");
+ }
+ } /* switch */
+ } /* !unref */
+
+ gpr_mu_unlock(&p->mu);
+
+ if (unref) {
+ GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "round_robin_connectivity");
+ }
}
-static void
-rr_broadcast (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol, grpc_transport_op * op)
-{
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
+static void rr_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
+ grpc_transport_op *op) {
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
size_t i;
size_t n;
grpc_subchannel **subchannels;
- gpr_mu_lock (&p->mu);
+ gpr_mu_lock(&p->mu);
n = p->num_subchannels;
- subchannels = gpr_malloc (n * sizeof (*subchannels));
- for (i = 0; i < n; i++)
- {
- subchannels[i] = p->subchannels[i];
- GRPC_SUBCHANNEL_REF (subchannels[i], "rr_broadcast");
- }
- gpr_mu_unlock (&p->mu);
-
- for (i = 0; i < n; i++)
- {
- grpc_subchannel_process_transport_op (exec_ctx, subchannels[i], op);
- GRPC_SUBCHANNEL_UNREF (exec_ctx, subchannels[i], "rr_broadcast");
- }
- gpr_free (subchannels);
+ subchannels = gpr_malloc(n * sizeof(*subchannels));
+ for (i = 0; i < n; i++) {
+ subchannels[i] = p->subchannels[i];
+ GRPC_SUBCHANNEL_REF(subchannels[i], "rr_broadcast");
+ }
+ gpr_mu_unlock(&p->mu);
+
+ for (i = 0; i < n; i++) {
+ grpc_subchannel_process_transport_op(exec_ctx, subchannels[i], op);
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "rr_broadcast");
+ }
+ gpr_free(subchannels);
}
-static grpc_connectivity_state
-rr_check_connectivity (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol)
-{
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
+static grpc_connectivity_state rr_check_connectivity(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *pol) {
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
grpc_connectivity_state st;
- gpr_mu_lock (&p->mu);
- st = grpc_connectivity_state_check (&p->state_tracker);
- gpr_mu_unlock (&p->mu);
+ gpr_mu_lock(&p->mu);
+ st = grpc_connectivity_state_check(&p->state_tracker);
+ gpr_mu_unlock(&p->mu);
return st;
}
-static void
-rr_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_lb_policy * pol, grpc_connectivity_state * current, grpc_closure * notify)
-{
- round_robin_lb_policy *p = (round_robin_lb_policy *) pol;
- gpr_mu_lock (&p->mu);
- grpc_connectivity_state_notify_on_state_change (exec_ctx, &p->state_tracker, current, notify);
- gpr_mu_unlock (&p->mu);
+static void rr_notify_on_state_change(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *pol,
+ grpc_connectivity_state *current,
+ grpc_closure *notify) {
+ round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
+ gpr_mu_lock(&p->mu);
+ grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
+ current, notify);
+ gpr_mu_unlock(&p->mu);
}
static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = {
- rr_destroy,
- rr_shutdown,
- rr_pick,
- rr_exit_idle,
- rr_broadcast,
- rr_check_connectivity,
- rr_notify_on_state_change
-};
-
-static void
-round_robin_factory_ref (grpc_lb_policy_factory * factory)
-{
-}
+ rr_destroy,
+ rr_shutdown,
+ rr_pick,
+ rr_exit_idle,
+ rr_broadcast,
+ rr_check_connectivity,
+ rr_notify_on_state_change};
-static void
-round_robin_factory_unref (grpc_lb_policy_factory * factory)
-{
-}
+static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {}
+
+static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {}
-static grpc_lb_policy *
-create_round_robin (grpc_lb_policy_factory * factory, grpc_lb_policy_args * args)
-{
+static grpc_lb_policy *create_round_robin(grpc_lb_policy_factory *factory,
+ grpc_lb_policy_args *args) {
size_t i;
- round_robin_lb_policy *p = gpr_malloc (sizeof (*p));
- GPR_ASSERT (args->num_subchannels > 0);
- memset (p, 0, sizeof (*p));
- grpc_lb_policy_init (&p->base, &round_robin_lb_policy_vtable);
- p->subchannels = gpr_malloc (sizeof (grpc_subchannel *) * args->num_subchannels);
+ round_robin_lb_policy *p = gpr_malloc(sizeof(*p));
+ GPR_ASSERT(args->num_subchannels > 0);
+ memset(p, 0, sizeof(*p));
+ grpc_lb_policy_init(&p->base, &round_robin_lb_policy_vtable);
+ p->subchannels =
+ gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels);
p->num_subchannels = args->num_subchannels;
- grpc_connectivity_state_init (&p->state_tracker, GRPC_CHANNEL_IDLE, "round_robin");
- memcpy (p->subchannels, args->subchannels, sizeof (grpc_subchannel *) * args->num_subchannels);
-
- gpr_mu_init (&p->mu);
- p->connectivity_changed_cbs = gpr_malloc (sizeof (grpc_closure) * args->num_subchannels);
- p->subchannel_connectivity = gpr_malloc (sizeof (grpc_connectivity_state) * args->num_subchannels);
-
- p->cb_args = gpr_malloc (sizeof (connectivity_changed_cb_arg) * args->num_subchannels);
- for (i = 0; i < args->num_subchannels; i++)
- {
- p->cb_args[i].subchannel_idx = i;
- p->cb_args[i].p = p;
- grpc_closure_init (&p->connectivity_changed_cbs[i], rr_connectivity_changed, &p->cb_args[i]);
- }
+ grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
+ "round_robin");
+ memcpy(p->subchannels, args->subchannels,
+ sizeof(grpc_subchannel *) * args->num_subchannels);
+
+ gpr_mu_init(&p->mu);
+ p->connectivity_changed_cbs =
+ gpr_malloc(sizeof(grpc_closure) * args->num_subchannels);
+ p->subchannel_connectivity =
+ gpr_malloc(sizeof(grpc_connectivity_state) * args->num_subchannels);
+
+ p->cb_args =
+ gpr_malloc(sizeof(connectivity_changed_cb_arg) * args->num_subchannels);
+ for (i = 0; i < args->num_subchannels; i++) {
+ p->cb_args[i].subchannel_idx = i;
+ p->cb_args[i].p = p;
+ grpc_closure_init(&p->connectivity_changed_cbs[i], rr_connectivity_changed,
+ &p->cb_args[i]);
+ }
/* The (dummy node) root of the ready list */
p->ready_list.subchannel = NULL;
@@ -575,22 +535,20 @@ create_round_robin (grpc_lb_policy_factory * factory, grpc_lb_policy_args * args
p->ready_list.next = NULL;
p->ready_list_last_pick = &p->ready_list;
- p->subchannel_index_to_readylist_node = gpr_malloc (sizeof (grpc_subchannel *) * args->num_subchannels);
- memset (p->subchannel_index_to_readylist_node, 0, sizeof (grpc_subchannel *) * args->num_subchannels);
+ p->subchannel_index_to_readylist_node =
+ gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels);
+ memset(p->subchannel_index_to_readylist_node, 0,
+ sizeof(grpc_subchannel *) * args->num_subchannels);
return &p->base;
}
static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = {
- round_robin_factory_ref, round_robin_factory_unref, create_round_robin,
- "round_robin"
-};
+ round_robin_factory_ref, round_robin_factory_unref, create_round_robin,
+ "round_robin"};
static grpc_lb_policy_factory round_robin_lb_policy_factory = {
- &round_robin_factory_vtable
-};
+ &round_robin_factory_vtable};
-grpc_lb_policy_factory *
-grpc_round_robin_lb_factory_create ()
-{
+grpc_lb_policy_factory *grpc_round_robin_lb_factory_create() {
return &round_robin_lb_policy_factory;
}
diff --git a/src/core/client_config/lb_policies/round_robin.h b/src/core/client_config/lb_policies/round_robin.h
index 213995aa3f..cf1f69c85f 100644
--- a/src/core/client_config/lb_policies/round_robin.h
+++ b/src/core/client_config/lb_policies/round_robin.h
@@ -41,6 +41,6 @@ extern int grpc_lb_round_robin_trace;
#include "src/core/client_config/lb_policy_factory.h"
/** Returns a load balancing factory for the round robin policy */
-grpc_lb_policy_factory *grpc_round_robin_lb_factory_create ();
+grpc_lb_policy_factory *grpc_round_robin_lb_factory_create();
#endif
diff --git a/src/core/client_config/lb_policy.c b/src/core/client_config/lb_policy.c
index 6a9cf66887..c955186f7f 100644
--- a/src/core/client_config/lb_policy.c
+++ b/src/core/client_config/lb_policy.c
@@ -33,74 +33,66 @@
#include "src/core/client_config/lb_policy.h"
-void
-grpc_lb_policy_init (grpc_lb_policy * policy, const grpc_lb_policy_vtable * vtable)
-{
+void grpc_lb_policy_init(grpc_lb_policy *policy,
+ const grpc_lb_policy_vtable *vtable) {
policy->vtable = vtable;
- gpr_ref_init (&policy->refs, 1);
+ gpr_ref_init(&policy->refs, 1);
}
#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
-void
-grpc_lb_policy_ref (grpc_lb_policy * policy, const char *file, int line, const char *reason)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "LB_POLICY:%p ref %d -> %d %s", policy, (int) policy->refs.count, (int) policy->refs.count + 1, reason);
+void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line,
+ const char *reason) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "LB_POLICY:%p ref %d -> %d %s",
+ policy, (int)policy->refs.count, (int)policy->refs.count + 1, reason);
#else
-void
-grpc_lb_policy_ref (grpc_lb_policy * policy)
-{
+void grpc_lb_policy_ref(grpc_lb_policy *policy) {
#endif
- gpr_ref (&policy->refs);
+ gpr_ref(&policy->refs);
}
#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
-void
-grpc_lb_policy_unref (grpc_lb_policy * policy, grpc_closure_list * closure_list, const char *file, int line, const char *reason)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "LB_POLICY:%p unref %d -> %d %s", policy, (int) policy->refs.count, (int) policy->refs.count - 1, reason);
+void grpc_lb_policy_unref(grpc_lb_policy *policy,
+ grpc_closure_list *closure_list, const char *file,
+ int line, const char *reason) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "LB_POLICY:%p unref %d -> %d %s",
+ policy, (int)policy->refs.count, (int)policy->refs.count - 1, reason);
#else
-void
-grpc_lb_policy_unref (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy)
-{
+void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy) {
#endif
- if (gpr_unref (&policy->refs))
- {
- policy->vtable->destroy (exec_ctx, policy);
- }
+ if (gpr_unref(&policy->refs)) {
+ policy->vtable->destroy(exec_ctx, policy);
+ }
}
-void
-grpc_lb_policy_shutdown (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy)
-{
- policy->vtable->shutdown (exec_ctx, policy);
+void grpc_lb_policy_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy) {
+ policy->vtable->shutdown(exec_ctx, policy);
}
-void
-grpc_lb_policy_pick (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_pollset * pollset, grpc_metadata_batch * initial_metadata, grpc_subchannel ** target, grpc_closure * on_complete)
-{
- policy->vtable->pick (exec_ctx, policy, pollset, initial_metadata, target, on_complete);
+void grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ grpc_pollset *pollset,
+ grpc_metadata_batch *initial_metadata,
+ grpc_subchannel **target, grpc_closure *on_complete) {
+ policy->vtable->pick(exec_ctx, policy, pollset, initial_metadata, target,
+ on_complete);
}
-void
-grpc_lb_policy_broadcast (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_transport_op * op)
-{
- policy->vtable->broadcast (exec_ctx, policy, op);
+void grpc_lb_policy_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ grpc_transport_op *op) {
+ policy->vtable->broadcast(exec_ctx, policy, op);
}
-void
-grpc_lb_policy_exit_idle (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy)
-{
- policy->vtable->exit_idle (exec_ctx, policy);
+void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy) {
+ policy->vtable->exit_idle(exec_ctx, policy);
}
-void
-grpc_lb_policy_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_connectivity_state * state, grpc_closure * closure)
-{
- policy->vtable->notify_on_state_change (exec_ctx, policy, state, closure);
+void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *policy,
+ grpc_connectivity_state *state,
+ grpc_closure *closure) {
+ policy->vtable->notify_on_state_change(exec_ctx, policy, state, closure);
}
-grpc_connectivity_state
-grpc_lb_policy_check_connectivity (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy)
-{
- return policy->vtable->check_connectivity (exec_ctx, policy);
+grpc_connectivity_state grpc_lb_policy_check_connectivity(
+ grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy) {
+ return policy->vtable->check_connectivity(exec_ctx, policy);
}
diff --git a/src/core/client_config/lb_policy.h b/src/core/client_config/lb_policy.h
index e66dac15b0..0eefe64991 100644
--- a/src/core/client_config/lb_policy.h
+++ b/src/core/client_config/lb_policy.h
@@ -42,35 +42,41 @@
typedef struct grpc_lb_policy grpc_lb_policy;
typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
-typedef void (*grpc_lb_completion) (void *cb_arg, grpc_subchannel * subchannel, grpc_status_code status, const char *errmsg);
+typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel,
+ grpc_status_code status, const char *errmsg);
-struct grpc_lb_policy
-{
+struct grpc_lb_policy {
const grpc_lb_policy_vtable *vtable;
gpr_refcount refs;
};
-struct grpc_lb_policy_vtable
-{
- void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+struct grpc_lb_policy_vtable {
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
- void (*shutdown) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+ void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
/** implement grpc_lb_policy_pick */
- void (*pick) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_pollset * pollset, grpc_metadata_batch * initial_metadata, grpc_subchannel ** target, grpc_closure * on_complete);
+ void (*pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ grpc_pollset *pollset, grpc_metadata_batch *initial_metadata,
+ grpc_subchannel **target, grpc_closure *on_complete);
/** try to enter a READY connectivity state */
- void (*exit_idle) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+ void (*exit_idle)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
/** broadcast a transport op to all subchannels */
- void (*broadcast) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_transport_op * op);
+ void (*broadcast)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ grpc_transport_op *op);
/** check the current connectivity of the lb_policy */
- grpc_connectivity_state (*check_connectivity) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+ grpc_connectivity_state (*check_connectivity)(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *policy);
/** call notify when the connectivity state of a channel changes from *state.
Updates *state with the new state of the policy */
- void (*notify_on_state_change) (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_connectivity_state * state, grpc_closure * closure);
+ void (*notify_on_state_change)(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *policy,
+ grpc_connectivity_state *state,
+ grpc_closure *closure);
};
#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
@@ -78,33 +84,44 @@ struct grpc_lb_policy_vtable
grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
#define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \
grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
-void grpc_lb_policy_ref (grpc_lb_policy * policy, const char *file, int line, const char *reason);
-void grpc_lb_policy_unref (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, const char *file, int line, const char *reason);
+void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line,
+ const char *reason);
+void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ const char *file, int line, const char *reason);
#else
#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
#define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p))
-void grpc_lb_policy_ref (grpc_lb_policy * policy);
-void grpc_lb_policy_unref (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+void grpc_lb_policy_ref(grpc_lb_policy *policy);
+void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
#endif
/** called by concrete implementations to initialize the base struct */
-void grpc_lb_policy_init (grpc_lb_policy * policy, const grpc_lb_policy_vtable * vtable);
+void grpc_lb_policy_init(grpc_lb_policy *policy,
+ const grpc_lb_policy_vtable *vtable);
/** Start shutting down (fail any pending picks) */
-void grpc_lb_policy_shutdown (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+void grpc_lb_policy_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
/** Given initial metadata in \a initial_metadata, find an appropriate
target for this rpc, and 'return' it by calling \a on_complete after setting
\a target.
Picking can be asynchronous. Any IO should be done under \a pollset. */
-void grpc_lb_policy_pick (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_pollset * pollset, grpc_metadata_batch * initial_metadata, grpc_subchannel ** target, grpc_closure * on_complete);
+void grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ grpc_pollset *pollset,
+ grpc_metadata_batch *initial_metadata,
+ grpc_subchannel **target, grpc_closure *on_complete);
-void grpc_lb_policy_broadcast (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_transport_op * op);
+void grpc_lb_policy_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
+ grpc_transport_op *op);
-void grpc_lb_policy_exit_idle (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
-void grpc_lb_policy_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy, grpc_connectivity_state * state, grpc_closure * closure);
+void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx,
+ grpc_lb_policy *policy,
+ grpc_connectivity_state *state,
+ grpc_closure *closure);
-grpc_connectivity_state grpc_lb_policy_check_connectivity (grpc_exec_ctx * exec_ctx, grpc_lb_policy * policy);
+grpc_connectivity_state grpc_lb_policy_check_connectivity(
+ grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
#endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_H */
diff --git a/src/core/client_config/lb_policy_factory.c b/src/core/client_config/lb_policy_factory.c
index 9d202d0c36..e49de544e3 100644
--- a/src/core/client_config/lb_policy_factory.c
+++ b/src/core/client_config/lb_policy_factory.c
@@ -33,22 +33,16 @@
#include "src/core/client_config/lb_policy_factory.h"
-void
-grpc_lb_policy_factory_ref (grpc_lb_policy_factory * factory)
-{
- factory->vtable->ref (factory);
+void grpc_lb_policy_factory_ref(grpc_lb_policy_factory* factory) {
+ factory->vtable->ref(factory);
}
-void
-grpc_lb_policy_factory_unref (grpc_lb_policy_factory * factory)
-{
- factory->vtable->unref (factory);
+void grpc_lb_policy_factory_unref(grpc_lb_policy_factory* factory) {
+ factory->vtable->unref(factory);
}
-grpc_lb_policy *
-grpc_lb_policy_factory_create_lb_policy (grpc_lb_policy_factory * factory, grpc_lb_policy_args * args)
-{
- if (factory == NULL)
- return NULL;
- return factory->vtable->create_lb_policy (factory, args);
+grpc_lb_policy* grpc_lb_policy_factory_create_lb_policy(
+ grpc_lb_policy_factory* factory, grpc_lb_policy_args* args) {
+ if (factory == NULL) return NULL;
+ return factory->vtable->create_lb_policy(factory, args);
}
diff --git a/src/core/client_config/lb_policy_factory.h b/src/core/client_config/lb_policy_factory.h
index a00b5142dc..04610316ee 100644
--- a/src/core/client_config/lb_policy_factory.h
+++ b/src/core/client_config/lb_policy_factory.h
@@ -42,33 +42,32 @@ typedef struct grpc_lb_policy_factory_vtable grpc_lb_policy_factory_vtable;
/** grpc_lb_policy provides grpc_client_config objects to grpc_channel
objects */
-struct grpc_lb_policy_factory
-{
+struct grpc_lb_policy_factory {
const grpc_lb_policy_factory_vtable *vtable;
};
-typedef struct grpc_lb_policy_args
-{
+typedef struct grpc_lb_policy_args {
grpc_subchannel **subchannels;
size_t num_subchannels;
} grpc_lb_policy_args;
-struct grpc_lb_policy_factory_vtable
-{
- void (*ref) (grpc_lb_policy_factory * factory);
- void (*unref) (grpc_lb_policy_factory * factory);
+struct grpc_lb_policy_factory_vtable {
+ void (*ref)(grpc_lb_policy_factory *factory);
+ void (*unref)(grpc_lb_policy_factory *factory);
/** Implementation of grpc_lb_policy_factory_create_lb_policy */
- grpc_lb_policy *(*create_lb_policy) (grpc_lb_policy_factory * factory, grpc_lb_policy_args * args);
+ grpc_lb_policy *(*create_lb_policy)(grpc_lb_policy_factory *factory,
+ grpc_lb_policy_args *args);
/** Name for the LB policy this factory implements */
const char *name;
};
-void grpc_lb_policy_factory_ref (grpc_lb_policy_factory * factory);
-void grpc_lb_policy_factory_unref (grpc_lb_policy_factory * factory);
+void grpc_lb_policy_factory_ref(grpc_lb_policy_factory *factory);
+void grpc_lb_policy_factory_unref(grpc_lb_policy_factory *factory);
/** Create a lb_policy instance. */
-grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy (grpc_lb_policy_factory * factory, grpc_lb_policy_args * args);
+grpc_lb_policy *grpc_lb_policy_factory_create_lb_policy(
+ grpc_lb_policy_factory *factory, grpc_lb_policy_args *args);
#endif /* GRPC_INTERNAL_CORE_CONFIG_LB_POLICY_FACTORY_H */
diff --git a/src/core/client_config/lb_policy_registry.c b/src/core/client_config/lb_policy_registry.c
index 4d7cc7c128..fc302e82d7 100644
--- a/src/core/client_config/lb_policy_registry.c
+++ b/src/core/client_config/lb_policy_registry.c
@@ -42,59 +42,47 @@ static int g_number_of_lb_policies = 0;
static grpc_lb_policy_factory *g_default_lb_policy_factory;
-void
-grpc_lb_policy_registry_init (grpc_lb_policy_factory * default_factory)
-{
+void grpc_lb_policy_registry_init(grpc_lb_policy_factory *default_factory) {
g_number_of_lb_policies = 0;
g_default_lb_policy_factory = default_factory;
}
-void
-grpc_lb_policy_registry_shutdown (void)
-{
+void grpc_lb_policy_registry_shutdown(void) {
int i;
- for (i = 0; i < g_number_of_lb_policies; i++)
- {
- grpc_lb_policy_factory_unref (g_all_of_the_lb_policies[i]);
- }
+ for (i = 0; i < g_number_of_lb_policies; i++) {
+ grpc_lb_policy_factory_unref(g_all_of_the_lb_policies[i]);
+ }
}
-void
-grpc_register_lb_policy (grpc_lb_policy_factory * factory)
-{
+void grpc_register_lb_policy(grpc_lb_policy_factory *factory) {
int i;
- for (i = 0; i < g_number_of_lb_policies; i++)
- {
- GPR_ASSERT (0 != strcmp (factory->vtable->name, g_all_of_the_lb_policies[i]->vtable->name));
- }
- GPR_ASSERT (g_number_of_lb_policies != MAX_POLICIES);
- grpc_lb_policy_factory_ref (factory);
+ for (i = 0; i < g_number_of_lb_policies; i++) {
+ GPR_ASSERT(0 != strcmp(factory->vtable->name,
+ g_all_of_the_lb_policies[i]->vtable->name));
+ }
+ GPR_ASSERT(g_number_of_lb_policies != MAX_POLICIES);
+ grpc_lb_policy_factory_ref(factory);
g_all_of_the_lb_policies[g_number_of_lb_policies++] = factory;
}
-static grpc_lb_policy_factory *
-lookup_factory (const char *name)
-{
+static grpc_lb_policy_factory *lookup_factory(const char *name) {
int i;
- if (name == NULL)
- return NULL;
+ if (name == NULL) return NULL;
- for (i = 0; i < g_number_of_lb_policies; i++)
- {
- if (0 == strcmp (name, g_all_of_the_lb_policies[i]->vtable->name))
- {
- return g_all_of_the_lb_policies[i];
- }
+ for (i = 0; i < g_number_of_lb_policies; i++) {
+ if (0 == strcmp(name, g_all_of_the_lb_policies[i]->vtable->name)) {
+ return g_all_of_the_lb_policies[i];
}
+ }
return NULL;
}
-grpc_lb_policy *
-grpc_lb_policy_create (const char *name, grpc_lb_policy_args * args)
-{
- grpc_lb_policy_factory *factory = lookup_factory (name);
- grpc_lb_policy *lb_policy = grpc_lb_policy_factory_create_lb_policy (factory, args);
+grpc_lb_policy *grpc_lb_policy_create(const char *name,
+ grpc_lb_policy_args *args) {
+ grpc_lb_policy_factory *factory = lookup_factory(name);
+ grpc_lb_policy *lb_policy =
+ grpc_lb_policy_factory_create_lb_policy(factory, args);
return lb_policy;
}
diff --git a/src/core/client_config/lb_policy_registry.h b/src/core/client_config/lb_policy_registry.h
index dea0cfc0fd..96fc2a1628 100644
--- a/src/core/client_config/lb_policy_registry.h
+++ b/src/core/client_config/lb_policy_registry.h
@@ -38,16 +38,17 @@
/** Initialize the registry and set \a default_factory as the factory to be
* returned when no name is provided in a lookup */
-void grpc_lb_policy_registry_init (grpc_lb_policy_factory * default_factory);
-void grpc_lb_policy_registry_shutdown (void);
+void grpc_lb_policy_registry_init(grpc_lb_policy_factory *default_factory);
+void grpc_lb_policy_registry_shutdown(void);
/** Register a LB policy factory. */
-void grpc_register_lb_policy (grpc_lb_policy_factory * factory);
+void grpc_register_lb_policy(grpc_lb_policy_factory *factory);
/** Create a \a grpc_lb_policy instance.
*
* If \a name is NULL, the default factory from \a grpc_lb_policy_registry_init
* will be returned. */
-grpc_lb_policy *grpc_lb_policy_create (const char *name, grpc_lb_policy_args * args);
+grpc_lb_policy *grpc_lb_policy_create(const char *name,
+ grpc_lb_policy_args *args);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_LB_POLICY_REGISTRY_H */
diff --git a/src/core/client_config/resolver.c b/src/core/client_config/resolver.c
index a55daad5a2..081097eb19 100644
--- a/src/core/client_config/resolver.c
+++ b/src/core/client_config/resolver.c
@@ -33,56 +33,53 @@
#include "src/core/client_config/resolver.h"
-void
-grpc_resolver_init (grpc_resolver * resolver, const grpc_resolver_vtable * vtable)
-{
+void grpc_resolver_init(grpc_resolver *resolver,
+ const grpc_resolver_vtable *vtable) {
resolver->vtable = vtable;
- gpr_ref_init (&resolver->refs, 1);
+ gpr_ref_init(&resolver->refs, 1);
}
#ifdef GRPC_RESOLVER_REFCOUNT_DEBUG
-void
-grpc_resolver_ref (grpc_resolver * resolver, grpc_closure_list * closure_list, const char *file, int line, const char *reason)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p ref %d -> %d %s", resolver, (int) resolver->refs.count, (int) resolver->refs.count + 1, reason);
+void grpc_resolver_ref(grpc_resolver *resolver, grpc_closure_list *closure_list,
+ const char *file, int line, const char *reason) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p ref %d -> %d %s",
+ resolver, (int)resolver->refs.count, (int)resolver->refs.count + 1,
+ reason);
#else
-void
-grpc_resolver_ref (grpc_resolver * resolver)
-{
+void grpc_resolver_ref(grpc_resolver *resolver) {
#endif
- gpr_ref (&resolver->refs);
+ gpr_ref(&resolver->refs);
}
#ifdef GRPC_RESOLVER_REFCOUNT_DEBUG
-void
-grpc_resolver_unref (grpc_resolver * resolver, grpc_closure_list * closure_list, const char *file, int line, const char *reason)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p unref %d -> %d %s", resolver, (int) resolver->refs.count, (int) resolver->refs.count - 1, reason);
+void grpc_resolver_unref(grpc_resolver *resolver,
+ grpc_closure_list *closure_list, const char *file,
+ int line, const char *reason) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "RESOLVER:%p unref %d -> %d %s",
+ resolver, (int)resolver->refs.count, (int)resolver->refs.count - 1,
+ reason);
#else
-void
-grpc_resolver_unref (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver)
-{
+void grpc_resolver_unref(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) {
#endif
- if (gpr_unref (&resolver->refs))
- {
- resolver->vtable->destroy (exec_ctx, resolver);
- }
+ if (gpr_unref(&resolver->refs)) {
+ resolver->vtable->destroy(exec_ctx, resolver);
+ }
}
-void
-grpc_resolver_shutdown (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver)
-{
- resolver->vtable->shutdown (exec_ctx, resolver);
+void grpc_resolver_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) {
+ resolver->vtable->shutdown(exec_ctx, resolver);
}
-void
-grpc_resolver_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, struct sockaddr *failing_address, int failing_address_len)
-{
- resolver->vtable->channel_saw_error (exec_ctx, resolver, failing_address, failing_address_len);
+void grpc_resolver_channel_saw_error(grpc_exec_ctx *exec_ctx,
+ grpc_resolver *resolver,
+ struct sockaddr *failing_address,
+ int failing_address_len) {
+ resolver->vtable->channel_saw_error(exec_ctx, resolver, failing_address,
+ failing_address_len);
}
-void
-grpc_resolver_next (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, grpc_client_config ** target_config, grpc_closure * on_complete)
-{
- resolver->vtable->next (exec_ctx, resolver, target_config, on_complete);
+void grpc_resolver_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete) {
+ resolver->vtable->next(exec_ctx, resolver, target_config, on_complete);
}
diff --git a/src/core/client_config/resolver.h b/src/core/client_config/resolver.h
index b7de00a143..7ba0cd5bd4 100644
--- a/src/core/client_config/resolver.h
+++ b/src/core/client_config/resolver.h
@@ -43,40 +43,47 @@ typedef struct grpc_resolver_vtable grpc_resolver_vtable;
/** grpc_resolver provides grpc_client_config objects to grpc_channel
objects */
-struct grpc_resolver
-{
+struct grpc_resolver {
const grpc_resolver_vtable *vtable;
gpr_refcount refs;
};
-struct grpc_resolver_vtable
-{
- void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver);
- void (*shutdown) (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver);
- void (*channel_saw_error) (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, struct sockaddr * failing_address, int failing_address_len);
- void (*next) (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, grpc_client_config ** target_config, grpc_closure * on_complete);
+struct grpc_resolver_vtable {
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver);
+ void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver);
+ void (*channel_saw_error)(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
+ struct sockaddr *failing_address,
+ int failing_address_len);
+ void (*next)(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
+ grpc_client_config **target_config, grpc_closure *on_complete);
};
#ifdef GRPC_RESOLVER_REFCOUNT_DEBUG
#define GRPC_RESOLVER_REF(p, r) grpc_resolver_ref((p), __FILE__, __LINE__, (r))
#define GRPC_RESOLVER_UNREF(cl, p, r) \
grpc_resolver_unref((cl), (p), __FILE__, __LINE__, (r))
-void grpc_resolver_ref (grpc_resolver * policy, const char *file, int line, const char *reason);
-void grpc_resolver_unref (grpc_resolver * policy, grpc_closure_list * closure_list, const char *file, int line, const char *reason);
+void grpc_resolver_ref(grpc_resolver *policy, const char *file, int line,
+ const char *reason);
+void grpc_resolver_unref(grpc_resolver *policy, grpc_closure_list *closure_list,
+ const char *file, int line, const char *reason);
#else
#define GRPC_RESOLVER_REF(p, r) grpc_resolver_ref((p))
#define GRPC_RESOLVER_UNREF(cl, p, r) grpc_resolver_unref((cl), (p))
-void grpc_resolver_ref (grpc_resolver * policy);
-void grpc_resolver_unref (grpc_exec_ctx * exec_ctx, grpc_resolver * policy);
+void grpc_resolver_ref(grpc_resolver *policy);
+void grpc_resolver_unref(grpc_exec_ctx *exec_ctx, grpc_resolver *policy);
#endif
-void grpc_resolver_init (grpc_resolver * resolver, const grpc_resolver_vtable * vtable);
+void grpc_resolver_init(grpc_resolver *resolver,
+ const grpc_resolver_vtable *vtable);
-void grpc_resolver_shutdown (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver);
+void grpc_resolver_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver);
/** Notification that the channel has seen an error on some address.
Can be used as a hint that re-resolution is desirable soon. */
-void grpc_resolver_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, struct sockaddr *failing_address, int failing_address_len);
+void grpc_resolver_channel_saw_error(grpc_exec_ctx *exec_ctx,
+ grpc_resolver *resolver,
+ struct sockaddr *failing_address,
+ int failing_address_len);
/** Get the next client config. Called by the channel to fetch a new
configuration. Expected to set *target_config with a new configuration,
@@ -84,6 +91,8 @@ void grpc_resolver_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver *
If resolution is fatally broken, set *target_config to NULL and
schedule on_complete. */
-void grpc_resolver_next (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, grpc_client_config ** target_config, grpc_closure * on_complete);
+void grpc_resolver_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete);
#endif /* GRPC_INTERNAL_CORE_CONFIG_RESOLVER_H */
diff --git a/src/core/client_config/resolver_factory.c b/src/core/client_config/resolver_factory.c
index d4f8201918..e7e9196ac4 100644
--- a/src/core/client_config/resolver_factory.c
+++ b/src/core/client_config/resolver_factory.c
@@ -33,31 +33,23 @@
#include "src/core/client_config/resolver_factory.h"
-void
-grpc_resolver_factory_ref (grpc_resolver_factory * factory)
-{
- factory->vtable->ref (factory);
+void grpc_resolver_factory_ref(grpc_resolver_factory* factory) {
+ factory->vtable->ref(factory);
}
-void
-grpc_resolver_factory_unref (grpc_resolver_factory * factory)
-{
- factory->vtable->unref (factory);
+void grpc_resolver_factory_unref(grpc_resolver_factory* factory) {
+ factory->vtable->unref(factory);
}
/** Create a resolver instance for a name */
-grpc_resolver *
-grpc_resolver_factory_create_resolver (grpc_resolver_factory * factory, grpc_resolver_args * args)
-{
- if (factory == NULL)
- return NULL;
- return factory->vtable->create_resolver (factory, args);
+grpc_resolver* grpc_resolver_factory_create_resolver(
+ grpc_resolver_factory* factory, grpc_resolver_args* args) {
+ if (factory == NULL) return NULL;
+ return factory->vtable->create_resolver(factory, args);
}
-char *
-grpc_resolver_factory_get_default_authority (grpc_resolver_factory * factory, grpc_uri * uri)
-{
- if (factory == NULL)
- return NULL;
- return factory->vtable->get_default_authority (factory, uri);
+char* grpc_resolver_factory_get_default_authority(
+ grpc_resolver_factory* factory, grpc_uri* uri) {
+ if (factory == NULL) return NULL;
+ return factory->vtable->get_default_authority(factory, uri);
}
diff --git a/src/core/client_config/resolver_factory.h b/src/core/client_config/resolver_factory.h
index 920776886a..4c4df353f7 100644
--- a/src/core/client_config/resolver_factory.h
+++ b/src/core/client_config/resolver_factory.h
@@ -43,40 +43,40 @@ typedef struct grpc_resolver_factory_vtable grpc_resolver_factory_vtable;
/** grpc_resolver provides grpc_client_config objects to grpc_channel
objects */
-struct grpc_resolver_factory
-{
+struct grpc_resolver_factory {
const grpc_resolver_factory_vtable *vtable;
};
-typedef struct grpc_resolver_args
-{
+typedef struct grpc_resolver_args {
grpc_uri *uri;
grpc_subchannel_factory *subchannel_factory;
} grpc_resolver_args;
-struct grpc_resolver_factory_vtable
-{
- void (*ref) (grpc_resolver_factory * factory);
- void (*unref) (grpc_resolver_factory * factory);
+struct grpc_resolver_factory_vtable {
+ void (*ref)(grpc_resolver_factory *factory);
+ void (*unref)(grpc_resolver_factory *factory);
/** Implementation of grpc_resolver_factory_create_resolver */
- grpc_resolver *(*create_resolver) (grpc_resolver_factory * factory, grpc_resolver_args * args);
+ grpc_resolver *(*create_resolver)(grpc_resolver_factory *factory,
+ grpc_resolver_args *args);
/** Implementation of grpc_resolver_factory_get_default_authority */
- char *(*get_default_authority) (grpc_resolver_factory * factory, grpc_uri * uri);
+ char *(*get_default_authority)(grpc_resolver_factory *factory, grpc_uri *uri);
/** URI scheme that this factory implements */
const char *scheme;
};
-void grpc_resolver_factory_ref (grpc_resolver_factory * resolver);
-void grpc_resolver_factory_unref (grpc_resolver_factory * resolver);
+void grpc_resolver_factory_ref(grpc_resolver_factory *resolver);
+void grpc_resolver_factory_unref(grpc_resolver_factory *resolver);
/** Create a resolver instance for a name */
-grpc_resolver *grpc_resolver_factory_create_resolver (grpc_resolver_factory * factory, grpc_resolver_args * args);
+grpc_resolver *grpc_resolver_factory_create_resolver(
+ grpc_resolver_factory *factory, grpc_resolver_args *args);
/** Return a (freshly allocated with gpr_malloc) string representing
the default authority to use for this scheme. */
-char *grpc_resolver_factory_get_default_authority (grpc_resolver_factory * factory, grpc_uri * uri);
+char *grpc_resolver_factory_get_default_authority(
+ grpc_resolver_factory *factory, grpc_uri *uri);
#endif /* GRPC_INTERNAL_CORE_CONFIG_RESOLVER_FACTORY_H */
diff --git a/src/core/client_config/resolver_registry.c b/src/core/client_config/resolver_registry.c
index 1ebf43048e..89a945c2d3 100644
--- a/src/core/client_config/resolver_registry.c
+++ b/src/core/client_config/resolver_registry.c
@@ -46,112 +46,92 @@ static int g_number_of_resolvers = 0;
static char *g_default_resolver_prefix;
-void
-grpc_resolver_registry_init (const char *default_resolver_prefix)
-{
+void grpc_resolver_registry_init(const char *default_resolver_prefix) {
g_number_of_resolvers = 0;
- g_default_resolver_prefix = gpr_strdup (default_resolver_prefix);
+ g_default_resolver_prefix = gpr_strdup(default_resolver_prefix);
}
-void
-grpc_resolver_registry_shutdown (void)
-{
+void grpc_resolver_registry_shutdown(void) {
int i;
- for (i = 0; i < g_number_of_resolvers; i++)
- {
- grpc_resolver_factory_unref (g_all_of_the_resolvers[i]);
- }
- gpr_free (g_default_resolver_prefix);
+ for (i = 0; i < g_number_of_resolvers; i++) {
+ grpc_resolver_factory_unref(g_all_of_the_resolvers[i]);
+ }
+ gpr_free(g_default_resolver_prefix);
}
-void
-grpc_register_resolver_type (grpc_resolver_factory * factory)
-{
+void grpc_register_resolver_type(grpc_resolver_factory *factory) {
int i;
- for (i = 0; i < g_number_of_resolvers; i++)
- {
- GPR_ASSERT (0 != strcmp (factory->vtable->scheme, g_all_of_the_resolvers[i]->vtable->scheme));
- }
- GPR_ASSERT (g_number_of_resolvers != MAX_RESOLVERS);
- grpc_resolver_factory_ref (factory);
+ for (i = 0; i < g_number_of_resolvers; i++) {
+ GPR_ASSERT(0 != strcmp(factory->vtable->scheme,
+ g_all_of_the_resolvers[i]->vtable->scheme));
+ }
+ GPR_ASSERT(g_number_of_resolvers != MAX_RESOLVERS);
+ grpc_resolver_factory_ref(factory);
g_all_of_the_resolvers[g_number_of_resolvers++] = factory;
}
-static grpc_resolver_factory *
-lookup_factory (grpc_uri * uri)
-{
+static grpc_resolver_factory *lookup_factory(grpc_uri *uri) {
int i;
/* handling NULL uri's here simplifies grpc_resolver_create */
- if (!uri)
- return NULL;
-
- for (i = 0; i < g_number_of_resolvers; i++)
- {
- if (0 == strcmp (uri->scheme, g_all_of_the_resolvers[i]->vtable->scheme))
- {
- return g_all_of_the_resolvers[i];
- }
+ if (!uri) return NULL;
+
+ for (i = 0; i < g_number_of_resolvers; i++) {
+ if (0 == strcmp(uri->scheme, g_all_of_the_resolvers[i]->vtable->scheme)) {
+ return g_all_of_the_resolvers[i];
}
+ }
return NULL;
}
-static grpc_resolver_factory *
-resolve_factory (const char *target, grpc_uri ** uri)
-{
+static grpc_resolver_factory *resolve_factory(const char *target,
+ grpc_uri **uri) {
char *tmp;
grpc_resolver_factory *factory = NULL;
- GPR_ASSERT (uri != NULL);
- *uri = grpc_uri_parse (target, 1);
- factory = lookup_factory (*uri);
- if (factory == NULL)
- {
- if (g_default_resolver_prefix != NULL)
- {
- grpc_uri_destroy (*uri);
- gpr_asprintf (&tmp, "%s%s", g_default_resolver_prefix, target);
- *uri = grpc_uri_parse (tmp, 1);
- factory = lookup_factory (*uri);
- if (factory == NULL)
- {
- grpc_uri_destroy (grpc_uri_parse (target, 0));
- grpc_uri_destroy (grpc_uri_parse (tmp, 0));
- gpr_log (GPR_ERROR, "don't know how to resolve '%s' or '%s'", target, tmp);
- }
- gpr_free (tmp);
- }
- else
- {
- grpc_uri_destroy (grpc_uri_parse (target, 0));
- gpr_log (GPR_ERROR, "don't know how to resolve '%s'", target);
- }
+ GPR_ASSERT(uri != NULL);
+ *uri = grpc_uri_parse(target, 1);
+ factory = lookup_factory(*uri);
+ if (factory == NULL) {
+ if (g_default_resolver_prefix != NULL) {
+ grpc_uri_destroy(*uri);
+ gpr_asprintf(&tmp, "%s%s", g_default_resolver_prefix, target);
+ *uri = grpc_uri_parse(tmp, 1);
+ factory = lookup_factory(*uri);
+ if (factory == NULL) {
+ grpc_uri_destroy(grpc_uri_parse(target, 0));
+ grpc_uri_destroy(grpc_uri_parse(tmp, 0));
+ gpr_log(GPR_ERROR, "don't know how to resolve '%s' or '%s'", target,
+ tmp);
+ }
+ gpr_free(tmp);
+ } else {
+ grpc_uri_destroy(grpc_uri_parse(target, 0));
+ gpr_log(GPR_ERROR, "don't know how to resolve '%s'", target);
}
+ }
return factory;
}
-grpc_resolver *
-grpc_resolver_create (const char *target, grpc_subchannel_factory * subchannel_factory)
-{
+grpc_resolver *grpc_resolver_create(
+ const char *target, grpc_subchannel_factory *subchannel_factory) {
grpc_uri *uri = NULL;
- grpc_resolver_factory *factory = resolve_factory (target, &uri);
+ grpc_resolver_factory *factory = resolve_factory(target, &uri);
grpc_resolver *resolver;
grpc_resolver_args args;
- memset (&args, 0, sizeof (args));
+ memset(&args, 0, sizeof(args));
args.uri = uri;
args.subchannel_factory = subchannel_factory;
- resolver = grpc_resolver_factory_create_resolver (factory, &args);
- grpc_uri_destroy (uri);
+ resolver = grpc_resolver_factory_create_resolver(factory, &args);
+ grpc_uri_destroy(uri);
return resolver;
}
-char *
-grpc_get_default_authority (const char *target)
-{
+char *grpc_get_default_authority(const char *target) {
grpc_uri *uri = NULL;
- grpc_resolver_factory *factory = resolve_factory (target, &uri);
- char *authority = grpc_resolver_factory_get_default_authority (factory, uri);
- grpc_uri_destroy (uri);
+ grpc_resolver_factory *factory = resolve_factory(target, &uri);
+ char *authority = grpc_resolver_factory_get_default_authority(factory, uri);
+ grpc_uri_destroy(uri);
return authority;
}
diff --git a/src/core/client_config/resolver_registry.h b/src/core/client_config/resolver_registry.h
index d52e50fd13..5a7193b7ae 100644
--- a/src/core/client_config/resolver_registry.h
+++ b/src/core/client_config/resolver_registry.h
@@ -36,15 +36,15 @@
#include "src/core/client_config/resolver_factory.h"
-void grpc_resolver_registry_init (const char *default_prefix);
-void grpc_resolver_registry_shutdown (void);
+void grpc_resolver_registry_init(const char *default_prefix);
+void grpc_resolver_registry_shutdown(void);
/** Register a resolver type.
URI's of \a scheme will be resolved with the given resolver.
If \a priority is greater than zero, then the resolver will be eligible
to resolve names that are passed in with no scheme. Higher priority
resolvers will be tried before lower priority schemes. */
-void grpc_register_resolver_type (grpc_resolver_factory * factory);
+void grpc_register_resolver_type(grpc_resolver_factory *factory);
/** Create a resolver given \a target.
First tries to parse \a target as a URI. If this succeeds, tries
@@ -55,10 +55,11 @@ void grpc_register_resolver_type (grpc_resolver_factory * factory);
If a resolver factory was found, use it to instantiate a resolver and
return it.
If a resolver factory was not found, return NULL. */
-grpc_resolver *grpc_resolver_create (const char *target, grpc_subchannel_factory * subchannel_factory);
+grpc_resolver *grpc_resolver_create(
+ const char *target, grpc_subchannel_factory *subchannel_factory);
/** Given a target, return a (freshly allocated with gpr_malloc) string
representing the default authority to pass from a client. */
-char *grpc_get_default_authority (const char *target);
+char *grpc_get_default_authority(const char *target);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVER_REGISTRY_H */
diff --git a/src/core/client_config/resolvers/dns_resolver.c b/src/core/client_config/resolvers/dns_resolver.c
index dc8151311a..7f9dd2543f 100644
--- a/src/core/client_config/resolvers/dns_resolver.c
+++ b/src/core/client_config/resolvers/dns_resolver.c
@@ -44,8 +44,7 @@
#include "src/core/iomgr/resolve_address.h"
#include "src/core/support/string.h"
-typedef struct
-{
+typedef struct {
/** base class: must be first */
grpc_resolver base;
/** refcount */
@@ -75,175 +74,160 @@ typedef struct
grpc_client_config *resolved_config;
} dns_resolver;
-static void dns_destroy (grpc_exec_ctx * exec_ctx, grpc_resolver * r);
+static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
-static void dns_start_resolving_locked (dns_resolver * r);
-static void dns_maybe_finish_next_locked (grpc_exec_ctx * exec_ctx, dns_resolver * r);
+static void dns_start_resolving_locked(dns_resolver *r);
+static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
+ dns_resolver *r);
-static void dns_shutdown (grpc_exec_ctx * exec_ctx, grpc_resolver * r);
-static void dns_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver * r, struct sockaddr *failing_address, int failing_address_len);
-static void dns_next (grpc_exec_ctx * exec_ctx, grpc_resolver * r, grpc_client_config ** target_config, grpc_closure * on_complete);
+static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
+static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx, grpc_resolver *r,
+ struct sockaddr *failing_address,
+ int failing_address_len);
+static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete);
static const grpc_resolver_vtable dns_resolver_vtable = {
- dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next
-};
+ dns_destroy, dns_shutdown, dns_channel_saw_error, dns_next};
-static void
-dns_shutdown (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver)
-{
- dns_resolver *r = (dns_resolver *) resolver;
- gpr_mu_lock (&r->mu);
- if (r->next_completion != NULL)
- {
- *r->target_config = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, r->next_completion, 1);
- r->next_completion = NULL;
- }
- gpr_mu_unlock (&r->mu);
+static void dns_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver) {
+ dns_resolver *r = (dns_resolver *)resolver;
+ gpr_mu_lock(&r->mu);
+ if (r->next_completion != NULL) {
+ *r->target_config = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, 1);
+ r->next_completion = NULL;
+ }
+ gpr_mu_unlock(&r->mu);
}
-static void
-dns_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, struct sockaddr *sa, int len)
-{
- dns_resolver *r = (dns_resolver *) resolver;
- gpr_mu_lock (&r->mu);
- if (!r->resolving)
- {
- dns_start_resolving_locked (r);
- }
- gpr_mu_unlock (&r->mu);
+static void dns_channel_saw_error(grpc_exec_ctx *exec_ctx,
+ grpc_resolver *resolver, struct sockaddr *sa,
+ int len) {
+ dns_resolver *r = (dns_resolver *)resolver;
+ gpr_mu_lock(&r->mu);
+ if (!r->resolving) {
+ dns_start_resolving_locked(r);
+ }
+ gpr_mu_unlock(&r->mu);
}
-static void
-dns_next (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, grpc_client_config ** target_config, grpc_closure * on_complete)
-{
- dns_resolver *r = (dns_resolver *) resolver;
- gpr_mu_lock (&r->mu);
- GPR_ASSERT (!r->next_completion);
+static void dns_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete) {
+ dns_resolver *r = (dns_resolver *)resolver;
+ gpr_mu_lock(&r->mu);
+ GPR_ASSERT(!r->next_completion);
r->next_completion = on_complete;
r->target_config = target_config;
- if (r->resolved_version == 0 && !r->resolving)
- {
- dns_start_resolving_locked (r);
- }
- else
- {
- dns_maybe_finish_next_locked (exec_ctx, r);
- }
- gpr_mu_unlock (&r->mu);
+ if (r->resolved_version == 0 && !r->resolving) {
+ dns_start_resolving_locked(r);
+ } else {
+ dns_maybe_finish_next_locked(exec_ctx, r);
+ }
+ gpr_mu_unlock(&r->mu);
}
-static void
-dns_on_resolved (grpc_exec_ctx * exec_ctx, void *arg, grpc_resolved_addresses * addresses)
-{
+static void dns_on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_resolved_addresses *addresses) {
dns_resolver *r = arg;
grpc_client_config *config = NULL;
grpc_subchannel **subchannels;
grpc_subchannel_args args;
grpc_lb_policy *lb_policy;
size_t i;
- if (addresses)
- {
- grpc_lb_policy_args lb_policy_args;
- config = grpc_client_config_create ();
- subchannels = gpr_malloc (sizeof (grpc_subchannel *) * addresses->naddrs);
- for (i = 0; i < addresses->naddrs; i++)
- {
- memset (&args, 0, sizeof (args));
- args.addr = (struct sockaddr *) (addresses->addrs[i].addr);
- args.addr_len = (size_t) addresses->addrs[i].len;
- subchannels[i] = grpc_subchannel_factory_create_subchannel (exec_ctx, r->subchannel_factory, &args);
- }
- memset (&lb_policy_args, 0, sizeof (lb_policy_args));
- lb_policy_args.subchannels = subchannels;
- lb_policy_args.num_subchannels = addresses->naddrs;
- lb_policy = grpc_lb_policy_create (r->lb_policy_name, &lb_policy_args);
- grpc_client_config_set_lb_policy (config, lb_policy);
- GRPC_LB_POLICY_UNREF (exec_ctx, lb_policy, "construction");
- grpc_resolved_addresses_destroy (addresses);
- gpr_free (subchannels);
+ if (addresses) {
+ grpc_lb_policy_args lb_policy_args;
+ config = grpc_client_config_create();
+ subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs);
+ for (i = 0; i < addresses->naddrs; i++) {
+ memset(&args, 0, sizeof(args));
+ args.addr = (struct sockaddr *)(addresses->addrs[i].addr);
+ args.addr_len = (size_t)addresses->addrs[i].len;
+ subchannels[i] = grpc_subchannel_factory_create_subchannel(
+ exec_ctx, r->subchannel_factory, &args);
}
- gpr_mu_lock (&r->mu);
- GPR_ASSERT (r->resolving);
+ memset(&lb_policy_args, 0, sizeof(lb_policy_args));
+ lb_policy_args.subchannels = subchannels;
+ lb_policy_args.num_subchannels = addresses->naddrs;
+ lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args);
+ grpc_client_config_set_lb_policy(config, lb_policy);
+ GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "construction");
+ grpc_resolved_addresses_destroy(addresses);
+ gpr_free(subchannels);
+ }
+ gpr_mu_lock(&r->mu);
+ GPR_ASSERT(r->resolving);
r->resolving = 0;
- if (r->resolved_config)
- {
- grpc_client_config_unref (exec_ctx, r->resolved_config);
- }
+ if (r->resolved_config) {
+ grpc_client_config_unref(exec_ctx, r->resolved_config);
+ }
r->resolved_config = config;
r->resolved_version++;
- dns_maybe_finish_next_locked (exec_ctx, r);
- gpr_mu_unlock (&r->mu);
+ dns_maybe_finish_next_locked(exec_ctx, r);
+ gpr_mu_unlock(&r->mu);
- GRPC_RESOLVER_UNREF (exec_ctx, &r->base, "dns-resolving");
+ GRPC_RESOLVER_UNREF(exec_ctx, &r->base, "dns-resolving");
}
-static void
-dns_start_resolving_locked (dns_resolver * r)
-{
- GRPC_RESOLVER_REF (&r->base, "dns-resolving");
- GPR_ASSERT (!r->resolving);
+static void dns_start_resolving_locked(dns_resolver *r) {
+ GRPC_RESOLVER_REF(&r->base, "dns-resolving");
+ GPR_ASSERT(!r->resolving);
r->resolving = 1;
- grpc_resolve_address (r->name, r->default_port, dns_on_resolved, r);
+ grpc_resolve_address(r->name, r->default_port, dns_on_resolved, r);
}
-static void
-dns_maybe_finish_next_locked (grpc_exec_ctx * exec_ctx, dns_resolver * r)
-{
- if (r->next_completion != NULL && r->resolved_version != r->published_version)
- {
- *r->target_config = r->resolved_config;
- if (r->resolved_config)
- {
- grpc_client_config_ref (r->resolved_config);
- }
- grpc_exec_ctx_enqueue (exec_ctx, r->next_completion, 1);
- r->next_completion = NULL;
- r->published_version = r->resolved_version;
+static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
+ dns_resolver *r) {
+ if (r->next_completion != NULL &&
+ r->resolved_version != r->published_version) {
+ *r->target_config = r->resolved_config;
+ if (r->resolved_config) {
+ grpc_client_config_ref(r->resolved_config);
}
+ grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, 1);
+ r->next_completion = NULL;
+ r->published_version = r->resolved_version;
+ }
}
-static void
-dns_destroy (grpc_exec_ctx * exec_ctx, grpc_resolver * gr)
-{
- dns_resolver *r = (dns_resolver *) gr;
- gpr_mu_destroy (&r->mu);
- if (r->resolved_config)
- {
- grpc_client_config_unref (exec_ctx, r->resolved_config);
- }
- grpc_subchannel_factory_unref (exec_ctx, r->subchannel_factory);
- gpr_free (r->name);
- gpr_free (r->default_port);
- gpr_free (r->lb_policy_name);
- gpr_free (r);
+static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
+ dns_resolver *r = (dns_resolver *)gr;
+ gpr_mu_destroy(&r->mu);
+ if (r->resolved_config) {
+ grpc_client_config_unref(exec_ctx, r->resolved_config);
+ }
+ grpc_subchannel_factory_unref(exec_ctx, r->subchannel_factory);
+ gpr_free(r->name);
+ gpr_free(r->default_port);
+ gpr_free(r->lb_policy_name);
+ gpr_free(r);
}
-static grpc_resolver *
-dns_create (grpc_resolver_args * args, const char *default_port, const char *lb_policy_name)
-{
+static grpc_resolver *dns_create(grpc_resolver_args *args,
+ const char *default_port,
+ const char *lb_policy_name) {
dns_resolver *r;
const char *path = args->uri->path;
- if (0 != strcmp (args->uri->authority, ""))
- {
- gpr_log (GPR_ERROR, "authority based dns uri's not supported");
- return NULL;
- }
+ if (0 != strcmp(args->uri->authority, "")) {
+ gpr_log(GPR_ERROR, "authority based dns uri's not supported");
+ return NULL;
+ }
- if (path[0] == '/')
- ++path;
+ if (path[0] == '/') ++path;
- r = gpr_malloc (sizeof (dns_resolver));
- memset (r, 0, sizeof (*r));
- gpr_ref_init (&r->refs, 1);
- gpr_mu_init (&r->mu);
- grpc_resolver_init (&r->base, &dns_resolver_vtable);
- r->name = gpr_strdup (path);
- r->default_port = gpr_strdup (default_port);
+ r = gpr_malloc(sizeof(dns_resolver));
+ memset(r, 0, sizeof(*r));
+ gpr_ref_init(&r->refs, 1);
+ gpr_mu_init(&r->mu);
+ grpc_resolver_init(&r->base, &dns_resolver_vtable);
+ r->name = gpr_strdup(path);
+ r->default_port = gpr_strdup(default_port);
r->subchannel_factory = args->subchannel_factory;
- grpc_subchannel_factory_ref (r->subchannel_factory);
- r->lb_policy_name = gpr_strdup (lb_policy_name);
+ grpc_subchannel_factory_ref(r->subchannel_factory);
+ r->lb_policy_name = gpr_strdup(lb_policy_name);
return &r->base;
}
@@ -251,39 +235,27 @@ dns_create (grpc_resolver_args * args, const char *default_port, const char *lb_
* FACTORY
*/
-static void
-dns_factory_ref (grpc_resolver_factory * factory)
-{
-}
+static void dns_factory_ref(grpc_resolver_factory *factory) {}
-static void
-dns_factory_unref (grpc_resolver_factory * factory)
-{
-}
+static void dns_factory_unref(grpc_resolver_factory *factory) {}
-static grpc_resolver *
-dns_factory_create_resolver (grpc_resolver_factory * factory, grpc_resolver_args * args)
-{
- return dns_create (args, "https", "pick_first");
+static grpc_resolver *dns_factory_create_resolver(
+ grpc_resolver_factory *factory, grpc_resolver_args *args) {
+ return dns_create(args, "https", "pick_first");
}
-char *
-dns_factory_get_default_host_name (grpc_resolver_factory * factory, grpc_uri * uri)
-{
+char *dns_factory_get_default_host_name(grpc_resolver_factory *factory,
+ grpc_uri *uri) {
const char *path = uri->path;
- if (path[0] == '/')
- ++path;
- return gpr_strdup (path);
+ if (path[0] == '/') ++path;
+ return gpr_strdup(path);
}
static const grpc_resolver_factory_vtable dns_factory_vtable = {
- dns_factory_ref, dns_factory_unref, dns_factory_create_resolver,
- dns_factory_get_default_host_name, "dns"
-};
-static grpc_resolver_factory dns_resolver_factory = { &dns_factory_vtable };
+ dns_factory_ref, dns_factory_unref, dns_factory_create_resolver,
+ dns_factory_get_default_host_name, "dns"};
+static grpc_resolver_factory dns_resolver_factory = {&dns_factory_vtable};
-grpc_resolver_factory *
-grpc_dns_resolver_factory_create ()
-{
+grpc_resolver_factory *grpc_dns_resolver_factory_create() {
return &dns_resolver_factory;
}
diff --git a/src/core/client_config/resolvers/dns_resolver.h b/src/core/client_config/resolvers/dns_resolver.h
index bb43499149..a3ef3161a6 100644
--- a/src/core/client_config/resolvers/dns_resolver.h
+++ b/src/core/client_config/resolvers/dns_resolver.h
@@ -37,6 +37,6 @@
#include "src/core/client_config/resolver_factory.h"
/** Create a dns resolver factory */
-grpc_resolver_factory *grpc_dns_resolver_factory_create (void);
+grpc_resolver_factory *grpc_dns_resolver_factory_create(void);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_DNS_RESOLVER_H */
diff --git a/src/core/client_config/resolvers/sockaddr_resolver.c b/src/core/client_config/resolvers/sockaddr_resolver.c
index f4c958c424..0b017f06c7 100644
--- a/src/core/client_config/resolvers/sockaddr_resolver.c
+++ b/src/core/client_config/resolvers/sockaddr_resolver.c
@@ -49,8 +49,7 @@
#include "src/core/iomgr/resolve_address.h"
#include "src/core/support/string.h"
-typedef struct
-{
+typedef struct {
/** base class: must be first */
grpc_resolver base;
/** refcount */
@@ -77,314 +76,283 @@ typedef struct
grpc_client_config **target_config;
} sockaddr_resolver;
-static void sockaddr_destroy (grpc_exec_ctx * exec_ctx, grpc_resolver * r);
+static void sockaddr_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
-static void sockaddr_maybe_finish_next_locked (grpc_exec_ctx * exec_ctx, sockaddr_resolver * r);
+static void sockaddr_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
+ sockaddr_resolver *r);
-static void sockaddr_shutdown (grpc_exec_ctx * exec_ctx, grpc_resolver * r);
-static void sockaddr_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver * r, struct sockaddr *failing_address, int failing_address_len);
-static void sockaddr_next (grpc_exec_ctx * exec_ctx, grpc_resolver * r, grpc_client_config ** target_config, grpc_closure * on_complete);
+static void sockaddr_shutdown(grpc_exec_ctx *exec_ctx, grpc_resolver *r);
+static void sockaddr_channel_saw_error(grpc_exec_ctx *exec_ctx,
+ grpc_resolver *r,
+ struct sockaddr *failing_address,
+ int failing_address_len);
+static void sockaddr_next(grpc_exec_ctx *exec_ctx, grpc_resolver *r,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete);
static const grpc_resolver_vtable sockaddr_resolver_vtable = {
- sockaddr_destroy, sockaddr_shutdown, sockaddr_channel_saw_error,
- sockaddr_next
-};
-
-static void
-sockaddr_shutdown (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver)
-{
- sockaddr_resolver *r = (sockaddr_resolver *) resolver;
- gpr_mu_lock (&r->mu);
- if (r->next_completion != NULL)
- {
- *r->target_config = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, r->next_completion, 1);
- r->next_completion = NULL;
- }
- gpr_mu_unlock (&r->mu);
+ sockaddr_destroy, sockaddr_shutdown, sockaddr_channel_saw_error,
+ sockaddr_next};
+
+static void sockaddr_shutdown(grpc_exec_ctx *exec_ctx,
+ grpc_resolver *resolver) {
+ sockaddr_resolver *r = (sockaddr_resolver *)resolver;
+ gpr_mu_lock(&r->mu);
+ if (r->next_completion != NULL) {
+ *r->target_config = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, 1);
+ r->next_completion = NULL;
+ }
+ gpr_mu_unlock(&r->mu);
}
-static void
-sockaddr_channel_saw_error (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, struct sockaddr *sa, int len)
-{
-}
+static void sockaddr_channel_saw_error(grpc_exec_ctx *exec_ctx,
+ grpc_resolver *resolver,
+ struct sockaddr *sa, int len) {}
-static void
-sockaddr_next (grpc_exec_ctx * exec_ctx, grpc_resolver * resolver, grpc_client_config ** target_config, grpc_closure * on_complete)
-{
- sockaddr_resolver *r = (sockaddr_resolver *) resolver;
- gpr_mu_lock (&r->mu);
- GPR_ASSERT (!r->next_completion);
+static void sockaddr_next(grpc_exec_ctx *exec_ctx, grpc_resolver *resolver,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete) {
+ sockaddr_resolver *r = (sockaddr_resolver *)resolver;
+ gpr_mu_lock(&r->mu);
+ GPR_ASSERT(!r->next_completion);
r->next_completion = on_complete;
r->target_config = target_config;
- sockaddr_maybe_finish_next_locked (exec_ctx, r);
- gpr_mu_unlock (&r->mu);
+ sockaddr_maybe_finish_next_locked(exec_ctx, r);
+ gpr_mu_unlock(&r->mu);
}
-static void
-sockaddr_maybe_finish_next_locked (grpc_exec_ctx * exec_ctx, sockaddr_resolver * r)
-{
+static void sockaddr_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
+ sockaddr_resolver *r) {
grpc_client_config *cfg;
grpc_lb_policy *lb_policy;
grpc_lb_policy_args lb_policy_args;
grpc_subchannel **subchannels;
grpc_subchannel_args args;
- if (r->next_completion != NULL && !r->published)
- {
- size_t i;
- cfg = grpc_client_config_create ();
- subchannels = gpr_malloc (sizeof (grpc_subchannel *) * r->num_addrs);
- for (i = 0; i < r->num_addrs; i++)
- {
- memset (&args, 0, sizeof (args));
- args.addr = (struct sockaddr *) &r->addrs[i];
- args.addr_len = r->addrs_len[i];
- subchannels[i] = grpc_subchannel_factory_create_subchannel (exec_ctx, r->subchannel_factory, &args);
- }
- memset (&lb_policy_args, 0, sizeof (lb_policy_args));
- lb_policy_args.subchannels = subchannels;
- lb_policy_args.num_subchannels = r->num_addrs;
- lb_policy = grpc_lb_policy_create (r->lb_policy_name, &lb_policy_args);
- gpr_free (subchannels);
- grpc_client_config_set_lb_policy (cfg, lb_policy);
- GRPC_LB_POLICY_UNREF (exec_ctx, lb_policy, "sockaddr");
- r->published = 1;
- *r->target_config = cfg;
- grpc_exec_ctx_enqueue (exec_ctx, r->next_completion, 1);
- r->next_completion = NULL;
+ if (r->next_completion != NULL && !r->published) {
+ size_t i;
+ cfg = grpc_client_config_create();
+ subchannels = gpr_malloc(sizeof(grpc_subchannel *) * r->num_addrs);
+ for (i = 0; i < r->num_addrs; i++) {
+ memset(&args, 0, sizeof(args));
+ args.addr = (struct sockaddr *)&r->addrs[i];
+ args.addr_len = r->addrs_len[i];
+ subchannels[i] = grpc_subchannel_factory_create_subchannel(
+ exec_ctx, r->subchannel_factory, &args);
}
+ memset(&lb_policy_args, 0, sizeof(lb_policy_args));
+ lb_policy_args.subchannels = subchannels;
+ lb_policy_args.num_subchannels = r->num_addrs;
+ lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args);
+ gpr_free(subchannels);
+ grpc_client_config_set_lb_policy(cfg, lb_policy);
+ GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "sockaddr");
+ r->published = 1;
+ *r->target_config = cfg;
+ grpc_exec_ctx_enqueue(exec_ctx, r->next_completion, 1);
+ r->next_completion = NULL;
+ }
}
-static void
-sockaddr_destroy (grpc_exec_ctx * exec_ctx, grpc_resolver * gr)
-{
- sockaddr_resolver *r = (sockaddr_resolver *) gr;
- gpr_mu_destroy (&r->mu);
- grpc_subchannel_factory_unref (exec_ctx, r->subchannel_factory);
- gpr_free (r->addrs);
- gpr_free (r->addrs_len);
- gpr_free (r->lb_policy_name);
- gpr_free (r);
+static void sockaddr_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
+ sockaddr_resolver *r = (sockaddr_resolver *)gr;
+ gpr_mu_destroy(&r->mu);
+ grpc_subchannel_factory_unref(exec_ctx, r->subchannel_factory);
+ gpr_free(r->addrs);
+ gpr_free(r->addrs_len);
+ gpr_free(r->lb_policy_name);
+ gpr_free(r);
}
#ifdef GPR_POSIX_SOCKET
-static int
-parse_unix (grpc_uri * uri, struct sockaddr_storage *addr, size_t * len)
-{
- struct sockaddr_un *un = (struct sockaddr_un *) addr;
+static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr,
+ size_t *len) {
+ struct sockaddr_un *un = (struct sockaddr_un *)addr;
un->sun_family = AF_UNIX;
- strcpy (un->sun_path, uri->path);
- *len = strlen (un->sun_path) + sizeof (un->sun_family) + 1;
+ strcpy(un->sun_path, uri->path);
+ *len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
return 1;
}
-static char *
-unix_get_default_authority (grpc_resolver_factory * factory, grpc_uri * uri)
-{
- return gpr_strdup ("localhost");
+static char *unix_get_default_authority(grpc_resolver_factory *factory,
+ grpc_uri *uri) {
+ return gpr_strdup("localhost");
}
#endif
-static char *
-ip_get_default_authority (grpc_uri * uri)
-{
+static char *ip_get_default_authority(grpc_uri *uri) {
const char *path = uri->path;
- if (path[0] == '/')
- ++path;
- return gpr_strdup (path);
+ if (path[0] == '/') ++path;
+ return gpr_strdup(path);
}
-static char *
-ipv4_get_default_authority (grpc_resolver_factory * factory, grpc_uri * uri)
-{
- return ip_get_default_authority (uri);
+static char *ipv4_get_default_authority(grpc_resolver_factory *factory,
+ grpc_uri *uri) {
+ return ip_get_default_authority(uri);
}
-static char *
-ipv6_get_default_authority (grpc_resolver_factory * factory, grpc_uri * uri)
-{
- return ip_get_default_authority (uri);
+static char *ipv6_get_default_authority(grpc_resolver_factory *factory,
+ grpc_uri *uri) {
+ return ip_get_default_authority(uri);
}
-static int
-parse_ipv4 (grpc_uri * uri, struct sockaddr_storage *addr, size_t * len)
-{
+static int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr,
+ size_t *len) {
const char *host_port = uri->path;
char *host;
char *port;
int port_num;
int result = 0;
- struct sockaddr_in *in = (struct sockaddr_in *) addr;
+ struct sockaddr_in *in = (struct sockaddr_in *)addr;
- if (*host_port == '/')
- ++host_port;
- if (!gpr_split_host_port (host_port, &host, &port))
- {
- return 0;
- }
+ if (*host_port == '/') ++host_port;
+ if (!gpr_split_host_port(host_port, &host, &port)) {
+ return 0;
+ }
- memset (in, 0, sizeof (*in));
- *len = sizeof (*in);
+ memset(in, 0, sizeof(*in));
+ *len = sizeof(*in);
in->sin_family = AF_INET;
- if (inet_pton (AF_INET, host, &in->sin_addr) == 0)
- {
- gpr_log (GPR_ERROR, "invalid ipv4 address: '%s'", host);
- goto done;
- }
+ if (inet_pton(AF_INET, host, &in->sin_addr) == 0) {
+ gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host);
+ goto done;
+ }
- if (port != NULL)
- {
- if (sscanf (port, "%d", &port_num) != 1 || port_num < 0 || port_num > 65535)
- {
- gpr_log (GPR_ERROR, "invalid ipv4 port: '%s'", port);
- goto done;
- }
- in->sin_port = htons ((gpr_uint16) port_num);
- }
- else
- {
- gpr_log (GPR_ERROR, "no port given for ipv4 scheme");
+ if (port != NULL) {
+ if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 ||
+ port_num > 65535) {
+ gpr_log(GPR_ERROR, "invalid ipv4 port: '%s'", port);
goto done;
}
+ in->sin_port = htons((gpr_uint16)port_num);
+ } else {
+ gpr_log(GPR_ERROR, "no port given for ipv4 scheme");
+ goto done;
+ }
result = 1;
done:
- gpr_free (host);
- gpr_free (port);
+ gpr_free(host);
+ gpr_free(port);
return result;
}
-static int
-parse_ipv6 (grpc_uri * uri, struct sockaddr_storage *addr, size_t * len)
-{
+static int parse_ipv6(grpc_uri *uri, struct sockaddr_storage *addr,
+ size_t *len) {
const char *host_port = uri->path;
char *host;
char *port;
int port_num;
int result = 0;
- struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) addr;
+ struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
- if (*host_port == '/')
- ++host_port;
- if (!gpr_split_host_port (host_port, &host, &port))
- {
- return 0;
- }
+ if (*host_port == '/') ++host_port;
+ if (!gpr_split_host_port(host_port, &host, &port)) {
+ return 0;
+ }
- memset (in6, 0, sizeof (*in6));
- *len = sizeof (*in6);
+ memset(in6, 0, sizeof(*in6));
+ *len = sizeof(*in6);
in6->sin6_family = AF_INET6;
- if (inet_pton (AF_INET6, host, &in6->sin6_addr) == 0)
- {
- gpr_log (GPR_ERROR, "invalid ipv6 address: '%s'", host);
- goto done;
- }
+ if (inet_pton(AF_INET6, host, &in6->sin6_addr) == 0) {
+ gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host);
+ goto done;
+ }
- if (port != NULL)
- {
- if (sscanf (port, "%d", &port_num) != 1 || port_num < 0 || port_num > 65535)
- {
- gpr_log (GPR_ERROR, "invalid ipv6 port: '%s'", port);
- goto done;
- }
- in6->sin6_port = htons ((gpr_uint16) port_num);
- }
- else
- {
- gpr_log (GPR_ERROR, "no port given for ipv6 scheme");
+ if (port != NULL) {
+ if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 ||
+ port_num > 65535) {
+ gpr_log(GPR_ERROR, "invalid ipv6 port: '%s'", port);
goto done;
}
+ in6->sin6_port = htons((gpr_uint16)port_num);
+ } else {
+ gpr_log(GPR_ERROR, "no port given for ipv6 scheme");
+ goto done;
+ }
result = 1;
done:
- gpr_free (host);
- gpr_free (port);
+ gpr_free(host);
+ gpr_free(port);
return result;
}
-static void
-do_nothing (void *ignored)
-{
-}
+static void do_nothing(void *ignored) {}
-static grpc_resolver *
-sockaddr_create (grpc_resolver_args * args, const char *default_lb_policy_name, int parse (grpc_uri * uri, struct sockaddr_storage *dst, size_t * len))
-{
+static grpc_resolver *sockaddr_create(
+ grpc_resolver_args *args, const char *default_lb_policy_name,
+ int parse(grpc_uri *uri, struct sockaddr_storage *dst, size_t *len)) {
size_t i;
- int errors_found = 0; /* GPR_FALSE */
+ int errors_found = 0; /* GPR_FALSE */
sockaddr_resolver *r;
gpr_slice path_slice;
gpr_slice_buffer path_parts;
- if (0 != strcmp (args->uri->authority, ""))
- {
- gpr_log (GPR_ERROR, "authority based uri's not supported by the %s scheme", args->uri->scheme);
- return NULL;
- }
+ if (0 != strcmp(args->uri->authority, "")) {
+ gpr_log(GPR_ERROR, "authority based uri's not supported by the %s scheme",
+ args->uri->scheme);
+ return NULL;
+ }
- r = gpr_malloc (sizeof (sockaddr_resolver));
- memset (r, 0, sizeof (*r));
+ r = gpr_malloc(sizeof(sockaddr_resolver));
+ memset(r, 0, sizeof(*r));
r->lb_policy_name = NULL;
- if (0 != strcmp (args->uri->query, ""))
- {
- gpr_slice query_slice;
- gpr_slice_buffer query_parts;
-
- query_slice = gpr_slice_new (args->uri->query, strlen (args->uri->query), do_nothing);
- gpr_slice_buffer_init (&query_parts);
- gpr_slice_split (query_slice, "=", &query_parts);
- GPR_ASSERT (query_parts.count == 2);
- if (0 == gpr_slice_str_cmp (query_parts.slices[0], "lb_policy"))
- {
- r->lb_policy_name = gpr_dump_slice (query_parts.slices[1], GPR_DUMP_ASCII);
- }
- gpr_slice_buffer_destroy (&query_parts);
- gpr_slice_unref (query_slice);
- }
- if (r->lb_policy_name == NULL)
- {
- r->lb_policy_name = gpr_strdup (default_lb_policy_name);
+ if (0 != strcmp(args->uri->query, "")) {
+ gpr_slice query_slice;
+ gpr_slice_buffer query_parts;
+
+ query_slice =
+ gpr_slice_new(args->uri->query, strlen(args->uri->query), do_nothing);
+ gpr_slice_buffer_init(&query_parts);
+ gpr_slice_split(query_slice, "=", &query_parts);
+ GPR_ASSERT(query_parts.count == 2);
+ if (0 == gpr_slice_str_cmp(query_parts.slices[0], "lb_policy")) {
+ r->lb_policy_name = gpr_dump_slice(query_parts.slices[1], GPR_DUMP_ASCII);
}
+ gpr_slice_buffer_destroy(&query_parts);
+ gpr_slice_unref(query_slice);
+ }
+ if (r->lb_policy_name == NULL) {
+ r->lb_policy_name = gpr_strdup(default_lb_policy_name);
+ }
- path_slice = gpr_slice_new (args->uri->path, strlen (args->uri->path), do_nothing);
- gpr_slice_buffer_init (&path_parts);
+ path_slice =
+ gpr_slice_new(args->uri->path, strlen(args->uri->path), do_nothing);
+ gpr_slice_buffer_init(&path_parts);
- gpr_slice_split (path_slice, ",", &path_parts);
+ gpr_slice_split(path_slice, ",", &path_parts);
r->num_addrs = path_parts.count;
- r->addrs = gpr_malloc (sizeof (struct sockaddr_storage) * r->num_addrs);
- r->addrs_len = gpr_malloc (sizeof (*r->addrs_len) * r->num_addrs);
-
- for (i = 0; i < r->num_addrs; i++)
- {
- grpc_uri ith_uri = *args->uri;
- char *part_str = gpr_dump_slice (path_parts.slices[i], GPR_DUMP_ASCII);
- ith_uri.path = part_str;
- if (!parse (&ith_uri, &r->addrs[i], &r->addrs_len[i]))
- {
- errors_found = 1; /* GPR_TRUE */
- }
- gpr_free (part_str);
- if (errors_found)
- break;
+ r->addrs = gpr_malloc(sizeof(struct sockaddr_storage) * r->num_addrs);
+ r->addrs_len = gpr_malloc(sizeof(*r->addrs_len) * r->num_addrs);
+
+ for (i = 0; i < r->num_addrs; i++) {
+ grpc_uri ith_uri = *args->uri;
+ char *part_str = gpr_dump_slice(path_parts.slices[i], GPR_DUMP_ASCII);
+ ith_uri.path = part_str;
+ if (!parse(&ith_uri, &r->addrs[i], &r->addrs_len[i])) {
+ errors_found = 1; /* GPR_TRUE */
}
+ gpr_free(part_str);
+ if (errors_found) break;
+ }
- gpr_slice_buffer_destroy (&path_parts);
- gpr_slice_unref (path_slice);
- if (errors_found)
- {
- gpr_free (r);
- return NULL;
- }
+ gpr_slice_buffer_destroy(&path_parts);
+ gpr_slice_unref(path_slice);
+ if (errors_found) {
+ gpr_free(r);
+ return NULL;
+ }
- gpr_ref_init (&r->refs, 1);
- gpr_mu_init (&r->mu);
- grpc_resolver_init (&r->base, &sockaddr_resolver_vtable);
+ gpr_ref_init(&r->refs, 1);
+ gpr_mu_init(&r->mu);
+ grpc_resolver_init(&r->base, &sockaddr_resolver_vtable);
r->subchannel_factory = args->subchannel_factory;
- grpc_subchannel_factory_ref (r->subchannel_factory);
+ grpc_subchannel_factory_ref(r->subchannel_factory);
return &r->base;
}
@@ -393,15 +361,9 @@ sockaddr_create (grpc_resolver_args * args, const char *default_lb_policy_name,
* FACTORY
*/
-static void
-sockaddr_factory_ref (grpc_resolver_factory * factory)
-{
-}
+static void sockaddr_factory_ref(grpc_resolver_factory *factory) {}
-static void
-sockaddr_factory_unref (grpc_resolver_factory * factory)
-{
-}
+static void sockaddr_factory_unref(grpc_resolver_factory *factory) {}
#define DECL_FACTORY(name) \
static grpc_resolver *name##_factory_create_resolver( \
@@ -418,6 +380,6 @@ sockaddr_factory_unref (grpc_resolver_factory * factory)
}
#ifdef GPR_POSIX_SOCKET
-DECL_FACTORY (unix)
+DECL_FACTORY(unix)
#endif
- DECL_FACTORY (ipv4) DECL_FACTORY (ipv6)
+DECL_FACTORY(ipv4) DECL_FACTORY(ipv6)
diff --git a/src/core/client_config/resolvers/sockaddr_resolver.h b/src/core/client_config/resolvers/sockaddr_resolver.h
index c778812e49..1b7a18f9c2 100644
--- a/src/core/client_config/resolvers/sockaddr_resolver.h
+++ b/src/core/client_config/resolvers/sockaddr_resolver.h
@@ -38,13 +38,13 @@
#include "src/core/client_config/resolver_factory.h"
-grpc_resolver_factory *grpc_ipv4_resolver_factory_create (void);
+grpc_resolver_factory *grpc_ipv4_resolver_factory_create(void);
-grpc_resolver_factory *grpc_ipv6_resolver_factory_create (void);
+grpc_resolver_factory *grpc_ipv6_resolver_factory_create(void);
#ifdef GPR_POSIX_SOCKET
/** Create a unix resolver factory */
-grpc_resolver_factory *grpc_unix_resolver_factory_create (void);
+grpc_resolver_factory *grpc_unix_resolver_factory_create(void);
#endif
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H */
diff --git a/src/core/client_config/resolvers/zookeeper_resolver.c b/src/core/client_config/resolvers/zookeeper_resolver.c
index 3e2040c412..213d5a172f 100644
--- a/src/core/client_config/resolvers/zookeeper_resolver.c
+++ b/src/core/client_config/resolvers/zookeeper_resolver.c
@@ -50,8 +50,7 @@
/** Zookeeper session expiration time in milliseconds */
#define GRPC_ZOOKEEPER_SESSION_TIMEOUT 15000
-typedef struct
-{
+typedef struct {
/** base class: must be first */
grpc_resolver base;
/** refcount */
@@ -88,119 +87,101 @@ typedef struct
int resolved_num;
} zookeeper_resolver;
-static void zookeeper_destroy (grpc_resolver * r);
+static void zookeeper_destroy(grpc_resolver *r);
-static void zookeeper_start_resolving_locked (zookeeper_resolver * r);
-static grpc_closure *
-zookeeper_maybe_finish_next_locked (zookeeper_resolver * r)
- GRPC_MUST_USE_RESULT;
+static void zookeeper_start_resolving_locked(zookeeper_resolver *r);
+static grpc_closure *zookeeper_maybe_finish_next_locked(zookeeper_resolver *r)
+ GRPC_MUST_USE_RESULT;
- static void zookeeper_shutdown (grpc_resolver * r);
- static void zookeeper_channel_saw_error (grpc_resolver * r, struct sockaddr *failing_address, int failing_address_len);
- static void zookeeper_next (grpc_resolver * r, grpc_client_config ** target_config, grpc_closure * on_complete);
+static void zookeeper_shutdown(grpc_resolver *r);
+static void zookeeper_channel_saw_error(grpc_resolver *r,
+ struct sockaddr *failing_address,
+ int failing_address_len);
+static void zookeeper_next(grpc_resolver *r, grpc_client_config **target_config,
+ grpc_closure *on_complete);
- static const grpc_resolver_vtable zookeeper_resolver_vtable = {
- zookeeper_destroy, zookeeper_shutdown, zookeeper_channel_saw_error,
- zookeeper_next
- };
+static const grpc_resolver_vtable zookeeper_resolver_vtable = {
+ zookeeper_destroy, zookeeper_shutdown, zookeeper_channel_saw_error,
+ zookeeper_next};
-static void
-zookeeper_shutdown (grpc_resolver * resolver)
-{
- zookeeper_resolver *r = (zookeeper_resolver *) resolver;
+static void zookeeper_shutdown(grpc_resolver *resolver) {
+ zookeeper_resolver *r = (zookeeper_resolver *)resolver;
grpc_closure *call = NULL;
- gpr_mu_lock (&r->mu);
- if (r->next_completion != NULL)
- {
- *r->target_config = NULL;
- call = r->next_completion;
- r->next_completion = NULL;
- }
- zookeeper_close (r->zookeeper_handle);
- gpr_mu_unlock (&r->mu);
- if (call != NULL)
- {
- call->cb (call->cb_arg, 1);
- }
+ gpr_mu_lock(&r->mu);
+ if (r->next_completion != NULL) {
+ *r->target_config = NULL;
+ call = r->next_completion;
+ r->next_completion = NULL;
+ }
+ zookeeper_close(r->zookeeper_handle);
+ gpr_mu_unlock(&r->mu);
+ if (call != NULL) {
+ call->cb(call->cb_arg, 1);
+ }
}
-static void
-zookeeper_channel_saw_error (grpc_resolver * resolver, struct sockaddr *sa, int len)
-{
- zookeeper_resolver *r = (zookeeper_resolver *) resolver;
- gpr_mu_lock (&r->mu);
- if (r->resolving == 0)
- {
- zookeeper_start_resolving_locked (r);
- }
- gpr_mu_unlock (&r->mu);
+static void zookeeper_channel_saw_error(grpc_resolver *resolver,
+ struct sockaddr *sa, int len) {
+ zookeeper_resolver *r = (zookeeper_resolver *)resolver;
+ gpr_mu_lock(&r->mu);
+ if (r->resolving == 0) {
+ zookeeper_start_resolving_locked(r);
+ }
+ gpr_mu_unlock(&r->mu);
}
-static void
-zookeeper_next (grpc_resolver * resolver, grpc_client_config ** target_config, grpc_closure * on_complete)
-{
- zookeeper_resolver *r = (zookeeper_resolver *) resolver;
+static void zookeeper_next(grpc_resolver *resolver,
+ grpc_client_config **target_config,
+ grpc_closure *on_complete) {
+ zookeeper_resolver *r = (zookeeper_resolver *)resolver;
grpc_closure *call;
- gpr_mu_lock (&r->mu);
- GPR_ASSERT (r->next_completion == NULL);
+ gpr_mu_lock(&r->mu);
+ GPR_ASSERT(r->next_completion == NULL);
r->next_completion = on_complete;
r->target_config = target_config;
- if (r->resolved_version == 0 && r->resolving == 0)
- {
- zookeeper_start_resolving_locked (r);
- }
- else
- {
- call = zookeeper_maybe_finish_next_locked (r);
- }
- gpr_mu_unlock (&r->mu);
- if (call)
- call->cb (call->cb_arg, 1);
+ if (r->resolved_version == 0 && r->resolving == 0) {
+ zookeeper_start_resolving_locked(r);
+ } else {
+ call = zookeeper_maybe_finish_next_locked(r);
+ }
+ gpr_mu_unlock(&r->mu);
+ if (call) call->cb(call->cb_arg, 1);
}
/** Zookeeper global watcher for connection management
TODO: better connection management besides logs */
-static void
-zookeeper_global_watcher (zhandle_t * zookeeper_handle, int type, int state, const char *path, void *watcher_ctx)
-{
- if (type == ZOO_SESSION_EVENT)
- {
- if (state == ZOO_EXPIRED_SESSION_STATE)
- {
- gpr_log (GPR_ERROR, "Zookeeper session expired");
- }
- else if (state == ZOO_AUTH_FAILED_STATE)
- {
- gpr_log (GPR_ERROR, "Zookeeper authentication failed");
- }
+static void zookeeper_global_watcher(zhandle_t *zookeeper_handle, int type,
+ int state, const char *path,
+ void *watcher_ctx) {
+ if (type == ZOO_SESSION_EVENT) {
+ if (state == ZOO_EXPIRED_SESSION_STATE) {
+ gpr_log(GPR_ERROR, "Zookeeper session expired");
+ } else if (state == ZOO_AUTH_FAILED_STATE) {
+ gpr_log(GPR_ERROR, "Zookeeper authentication failed");
}
+ }
}
/** Zookeeper watcher triggered by changes to watched nodes
Once triggered, it tries to resolve again to get updated addresses */
-static void
-zookeeper_watcher (zhandle_t * zookeeper_handle, int type, int state, const char *path, void *watcher_ctx)
-{
- if (watcher_ctx != NULL)
- {
- zookeeper_resolver *r = (zookeeper_resolver *) watcher_ctx;
- if (state == ZOO_CONNECTED_STATE)
- {
- gpr_mu_lock (&r->mu);
- if (r->resolving == 0)
- {
- zookeeper_start_resolving_locked (r);
- }
- gpr_mu_unlock (&r->mu);
- }
+static void zookeeper_watcher(zhandle_t *zookeeper_handle, int type, int state,
+ const char *path, void *watcher_ctx) {
+ if (watcher_ctx != NULL) {
+ zookeeper_resolver *r = (zookeeper_resolver *)watcher_ctx;
+ if (state == ZOO_CONNECTED_STATE) {
+ gpr_mu_lock(&r->mu);
+ if (r->resolving == 0) {
+ zookeeper_start_resolving_locked(r);
+ }
+ gpr_mu_unlock(&r->mu);
}
+ }
}
/** Callback function after getting all resolved addresses
Creates a subchannel for each address */
-static void
-zookeeper_on_resolved (void *arg, grpc_resolved_addresses * addresses)
-{
+static void zookeeper_on_resolved(void *arg,
+ grpc_resolved_addresses *addresses) {
zookeeper_resolver *r = arg;
grpc_client_config *config = NULL;
grpc_subchannel **subchannels;
@@ -208,77 +189,74 @@ zookeeper_on_resolved (void *arg, grpc_resolved_addresses * addresses)
grpc_lb_policy *lb_policy;
grpc_closure *call;
size_t i;
- if (addresses != NULL)
- {
- grpc_lb_policy_args lb_policy_args;
- config = grpc_client_config_create ();
- subchannels = gpr_malloc (sizeof (grpc_subchannel *) * addresses->naddrs);
- for (i = 0; i < addresses->naddrs; i++)
- {
- memset (&args, 0, sizeof (args));
- args.addr = (struct sockaddr *) (addresses->addrs[i].addr);
- args.addr_len = addresses->addrs[i].len;
- subchannels[i] = grpc_subchannel_factory_create_subchannel (r->subchannel_factory, &args);
- }
- lb_policy_args.subchannels = subchannels;
- lb_policy_args.num_subchannels = addresses->naddrs;
- lb_policy = grpc_lb_policy_create (r->lb_policy_name, &lb_policy_args);
- grpc_client_config_set_lb_policy (config, lb_policy);
- GRPC_LB_POLICY_UNREF (lb_policy, "construction");
- grpc_resolved_addresses_destroy (addresses);
- gpr_free (subchannels);
+ if (addresses != NULL) {
+ grpc_lb_policy_args lb_policy_args;
+ config = grpc_client_config_create();
+ subchannels = gpr_malloc(sizeof(grpc_subchannel *) * addresses->naddrs);
+ for (i = 0; i < addresses->naddrs; i++) {
+ memset(&args, 0, sizeof(args));
+ args.addr = (struct sockaddr *)(addresses->addrs[i].addr);
+ args.addr_len = addresses->addrs[i].len;
+ subchannels[i] = grpc_subchannel_factory_create_subchannel(
+ r->subchannel_factory, &args);
}
- gpr_mu_lock (&r->mu);
- GPR_ASSERT (r->resolving == 1);
+ lb_policy_args.subchannels = subchannels;
+ lb_policy_args.num_subchannels = addresses->naddrs;
+ lb_policy = grpc_lb_policy_create(r->lb_policy_name, &lb_policy_args);
+ grpc_client_config_set_lb_policy(config, lb_policy);
+ GRPC_LB_POLICY_UNREF(lb_policy, "construction");
+ grpc_resolved_addresses_destroy(addresses);
+ gpr_free(subchannels);
+ }
+ gpr_mu_lock(&r->mu);
+ GPR_ASSERT(r->resolving == 1);
r->resolving = 0;
- if (r->resolved_config != NULL)
- {
- grpc_client_config_unref (r->resolved_config);
- }
+ if (r->resolved_config != NULL) {
+ grpc_client_config_unref(r->resolved_config);
+ }
r->resolved_config = config;
r->resolved_version++;
- call = zookeeper_maybe_finish_next_locked (r);
- gpr_mu_unlock (&r->mu);
+ call = zookeeper_maybe_finish_next_locked(r);
+ gpr_mu_unlock(&r->mu);
- if (call)
- call->cb (call->cb_arg, 1);
+ if (call) call->cb(call->cb_arg, 1);
- GRPC_RESOLVER_UNREF (&r->base, "zookeeper-resolving");
+ GRPC_RESOLVER_UNREF(&r->base, "zookeeper-resolving");
}
/** Callback function for each DNS resolved address */
-static void
-zookeeper_dns_resolved (void *arg, grpc_resolved_addresses * addresses)
-{
+static void zookeeper_dns_resolved(void *arg,
+ grpc_resolved_addresses *addresses) {
size_t i;
zookeeper_resolver *r = arg;
int resolve_done = 0;
- gpr_mu_lock (&r->mu);
+ gpr_mu_lock(&r->mu);
r->resolved_num++;
- r->resolved_addrs->addrs = gpr_realloc (r->resolved_addrs->addrs, sizeof (grpc_resolved_address) * (r->resolved_addrs->naddrs + addresses->naddrs));
- for (i = 0; i < addresses->naddrs; i++)
- {
- memcpy (r->resolved_addrs->addrs[i + r->resolved_addrs->naddrs].addr, addresses->addrs[i].addr, addresses->addrs[i].len);
- r->resolved_addrs->addrs[i + r->resolved_addrs->naddrs].len = addresses->addrs[i].len;
- }
+ r->resolved_addrs->addrs =
+ gpr_realloc(r->resolved_addrs->addrs,
+ sizeof(grpc_resolved_address) *
+ (r->resolved_addrs->naddrs + addresses->naddrs));
+ for (i = 0; i < addresses->naddrs; i++) {
+ memcpy(r->resolved_addrs->addrs[i + r->resolved_addrs->naddrs].addr,
+ addresses->addrs[i].addr, addresses->addrs[i].len);
+ r->resolved_addrs->addrs[i + r->resolved_addrs->naddrs].len =
+ addresses->addrs[i].len;
+ }
r->resolved_addrs->naddrs += addresses->naddrs;
- grpc_resolved_addresses_destroy (addresses);
+ grpc_resolved_addresses_destroy(addresses);
/** Wait for all addresses to be resolved */
resolve_done = (r->resolved_num == r->resolved_total);
- gpr_mu_unlock (&r->mu);
- if (resolve_done)
- {
- zookeeper_on_resolved (r, r->resolved_addrs);
- }
+ gpr_mu_unlock(&r->mu);
+ if (resolve_done) {
+ zookeeper_on_resolved(r, r->resolved_addrs);
+ }
}
/** Parses JSON format address of a zookeeper node */
-static char *
-zookeeper_parse_address (const char *value, size_t value_len)
-{
+static char *zookeeper_parse_address(const char *value, size_t value_len) {
grpc_json *json;
grpc_json *cur;
const char *host;
@@ -286,301 +264,256 @@ zookeeper_parse_address (const char *value, size_t value_len)
char *buffer;
char *address = NULL;
- buffer = gpr_malloc (value_len);
- memcpy (buffer, value, value_len);
- json = grpc_json_parse_string_with_len (buffer, value_len);
- if (json != NULL)
- {
- host = NULL;
- port = NULL;
- for (cur = json->child; cur != NULL; cur = cur->next)
- {
- if (!strcmp (cur->key, "host"))
- {
- host = cur->value;
- if (port != NULL)
- {
- break;
- }
- }
- else if (!strcmp (cur->key, "port"))
- {
- port = cur->value;
- if (host != NULL)
- {
- break;
- }
- }
- }
- if (host != NULL && port != NULL)
- {
- gpr_asprintf (&address, "%s:%s", host, port);
- }
- grpc_json_destroy (json);
+ buffer = gpr_malloc(value_len);
+ memcpy(buffer, value, value_len);
+ json = grpc_json_parse_string_with_len(buffer, value_len);
+ if (json != NULL) {
+ host = NULL;
+ port = NULL;
+ for (cur = json->child; cur != NULL; cur = cur->next) {
+ if (!strcmp(cur->key, "host")) {
+ host = cur->value;
+ if (port != NULL) {
+ break;
+ }
+ } else if (!strcmp(cur->key, "port")) {
+ port = cur->value;
+ if (host != NULL) {
+ break;
+ }
+ }
+ }
+ if (host != NULL && port != NULL) {
+ gpr_asprintf(&address, "%s:%s", host, port);
}
- gpr_free (buffer);
+ grpc_json_destroy(json);
+ }
+ gpr_free(buffer);
return address;
}
-static void
-zookeeper_get_children_node_completion (int rc, const char *value, int value_len, const struct Stat *stat, const void *arg)
-{
+static void zookeeper_get_children_node_completion(int rc, const char *value,
+ int value_len,
+ const struct Stat *stat,
+ const void *arg) {
char *address = NULL;
- zookeeper_resolver *r = (zookeeper_resolver *) arg;
+ zookeeper_resolver *r = (zookeeper_resolver *)arg;
int resolve_done = 0;
- if (rc != 0)
- {
- gpr_log (GPR_ERROR, "Error in getting a child node of %s", r->name);
- return;
- }
+ if (rc != 0) {
+ gpr_log(GPR_ERROR, "Error in getting a child node of %s", r->name);
+ return;
+ }
- address = zookeeper_parse_address (value, (size_t) value_len);
- if (address != NULL)
- {
+ address = zookeeper_parse_address(value, (size_t)value_len);
+ if (address != NULL) {
/** Further resolves address by DNS */
- grpc_resolve_address (address, NULL, zookeeper_dns_resolved, r);
- gpr_free (address);
- }
- else
- {
- gpr_log (GPR_ERROR, "Error in resolving a child node of %s", r->name);
- gpr_mu_lock (&r->mu);
- r->resolved_total--;
- resolve_done = (r->resolved_num == r->resolved_total);
- gpr_mu_unlock (&r->mu);
- if (resolve_done)
- {
- zookeeper_on_resolved (r, r->resolved_addrs);
- }
+ grpc_resolve_address(address, NULL, zookeeper_dns_resolved, r);
+ gpr_free(address);
+ } else {
+ gpr_log(GPR_ERROR, "Error in resolving a child node of %s", r->name);
+ gpr_mu_lock(&r->mu);
+ r->resolved_total--;
+ resolve_done = (r->resolved_num == r->resolved_total);
+ gpr_mu_unlock(&r->mu);
+ if (resolve_done) {
+ zookeeper_on_resolved(r, r->resolved_addrs);
}
+ }
}
-static void
-zookeeper_get_children_completion (int rc, const struct String_vector *children, const void *arg)
-{
+static void zookeeper_get_children_completion(
+ int rc, const struct String_vector *children, const void *arg) {
char *path;
int status;
int i;
- zookeeper_resolver *r = (zookeeper_resolver *) arg;
+ zookeeper_resolver *r = (zookeeper_resolver *)arg;
- if (rc != 0)
- {
- gpr_log (GPR_ERROR, "Error in getting zookeeper children of %s", r->name);
- return;
- }
+ if (rc != 0) {
+ gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name);
+ return;
+ }
- if (children->count == 0)
- {
- gpr_log (GPR_ERROR, "Error in resolving zookeeper address %s", r->name);
- return;
- }
+ if (children->count == 0) {
+ gpr_log(GPR_ERROR, "Error in resolving zookeeper address %s", r->name);
+ return;
+ }
- r->resolved_addrs = gpr_malloc (sizeof (grpc_resolved_addresses));
+ r->resolved_addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
r->resolved_addrs->addrs = NULL;
r->resolved_addrs->naddrs = 0;
r->resolved_total = children->count;
/** TODO: Replace expensive heap allocation with stack
if we can get maximum length of zookeeper path */
- for (i = 0; i < children->count; i++)
- {
- gpr_asprintf (&path, "%s/%s", r->name, children->data[i]);
- status = zoo_awget (r->zookeeper_handle, path, zookeeper_watcher, r, zookeeper_get_children_node_completion, r);
- gpr_free (path);
- if (status != 0)
- {
- gpr_log (GPR_ERROR, "Error in getting zookeeper node %s", path);
- }
+ for (i = 0; i < children->count; i++) {
+ gpr_asprintf(&path, "%s/%s", r->name, children->data[i]);
+ status = zoo_awget(r->zookeeper_handle, path, zookeeper_watcher, r,
+ zookeeper_get_children_node_completion, r);
+ gpr_free(path);
+ if (status != 0) {
+ gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", path);
}
+ }
}
-static void
-zookeeper_get_node_completion (int rc, const char *value, int value_len, const struct Stat *stat, const void *arg)
-{
+static void zookeeper_get_node_completion(int rc, const char *value,
+ int value_len,
+ const struct Stat *stat,
+ const void *arg) {
int status;
char *address = NULL;
- zookeeper_resolver *r = (zookeeper_resolver *) arg;
+ zookeeper_resolver *r = (zookeeper_resolver *)arg;
r->resolved_addrs = NULL;
r->resolved_total = 0;
r->resolved_num = 0;
- if (rc != 0)
- {
- gpr_log (GPR_ERROR, "Error in getting zookeeper node %s", r->name);
- return;
- }
+ if (rc != 0) {
+ gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name);
+ return;
+ }
/** If zookeeper node of path r->name does not have address
(i.e. service node), get its children */
- address = zookeeper_parse_address (value, (size_t) value_len);
- if (address != NULL)
- {
- r->resolved_addrs = gpr_malloc (sizeof (grpc_resolved_addresses));
- r->resolved_addrs->addrs = NULL;
- r->resolved_addrs->naddrs = 0;
- r->resolved_total = 1;
+ address = zookeeper_parse_address(value, (size_t)value_len);
+ if (address != NULL) {
+ r->resolved_addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
+ r->resolved_addrs->addrs = NULL;
+ r->resolved_addrs->naddrs = 0;
+ r->resolved_total = 1;
/** Further resolves address by DNS */
- grpc_resolve_address (address, NULL, zookeeper_dns_resolved, r);
- gpr_free (address);
- return;
- }
-
- status = zoo_awget_children (r->zookeeper_handle, r->name, zookeeper_watcher, r, zookeeper_get_children_completion, r);
- if (status != 0)
- {
- gpr_log (GPR_ERROR, "Error in getting zookeeper children of %s", r->name);
- }
+ grpc_resolve_address(address, NULL, zookeeper_dns_resolved, r);
+ gpr_free(address);
+ return;
+ }
+
+ status = zoo_awget_children(r->zookeeper_handle, r->name, zookeeper_watcher,
+ r, zookeeper_get_children_completion, r);
+ if (status != 0) {
+ gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name);
+ }
}
-static void
-zookeeper_resolve_address (zookeeper_resolver * r)
-{
+static void zookeeper_resolve_address(zookeeper_resolver *r) {
int status;
- status = zoo_awget (r->zookeeper_handle, r->name, zookeeper_watcher, r, zookeeper_get_node_completion, r);
- if (status != 0)
- {
- gpr_log (GPR_ERROR, "Error in getting zookeeper node %s", r->name);
- }
+ status = zoo_awget(r->zookeeper_handle, r->name, zookeeper_watcher, r,
+ zookeeper_get_node_completion, r);
+ if (status != 0) {
+ gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name);
+ }
}
-static void
-zookeeper_start_resolving_locked (zookeeper_resolver * r)
-{
- GRPC_RESOLVER_REF (&r->base, "zookeeper-resolving");
- GPR_ASSERT (r->resolving == 0);
+static void zookeeper_start_resolving_locked(zookeeper_resolver *r) {
+ GRPC_RESOLVER_REF(&r->base, "zookeeper-resolving");
+ GPR_ASSERT(r->resolving == 0);
r->resolving = 1;
- zookeeper_resolve_address (r);
+ zookeeper_resolve_address(r);
}
-static grpc_closure *
-zookeeper_maybe_finish_next_locked (zookeeper_resolver * r)
-{
+static grpc_closure *zookeeper_maybe_finish_next_locked(zookeeper_resolver *r) {
grpc_closure *call = NULL;
- if (r->next_completion != NULL && r->resolved_version != r->published_version)
- {
- *r->target_config = r->resolved_config;
- if (r->resolved_config != NULL)
- {
- grpc_client_config_ref (r->resolved_config);
- }
- call = r->next_completion;
- r->next_completion = NULL;
- r->published_version = r->resolved_version;
+ if (r->next_completion != NULL &&
+ r->resolved_version != r->published_version) {
+ *r->target_config = r->resolved_config;
+ if (r->resolved_config != NULL) {
+ grpc_client_config_ref(r->resolved_config);
}
+ call = r->next_completion;
+ r->next_completion = NULL;
+ r->published_version = r->resolved_version;
+ }
return call;
}
-static void
-zookeeper_destroy (grpc_resolver * gr)
-{
- zookeeper_resolver *r = (zookeeper_resolver *) gr;
- gpr_mu_destroy (&r->mu);
- if (r->resolved_config != NULL)
- {
- grpc_client_config_unref (r->resolved_config);
- }
- grpc_subchannel_factory_unref (r->subchannel_factory);
- gpr_free (r->name);
- gpr_free (r->lb_policy_name);
- gpr_free (r);
+static void zookeeper_destroy(grpc_resolver *gr) {
+ zookeeper_resolver *r = (zookeeper_resolver *)gr;
+ gpr_mu_destroy(&r->mu);
+ if (r->resolved_config != NULL) {
+ grpc_client_config_unref(r->resolved_config);
+ }
+ grpc_subchannel_factory_unref(r->subchannel_factory);
+ gpr_free(r->name);
+ gpr_free(r->lb_policy_name);
+ gpr_free(r);
}
-static grpc_resolver *
-zookeeper_create (grpc_resolver_args * args, const char *lb_policy_name)
-{
+static grpc_resolver *zookeeper_create(grpc_resolver_args *args,
+ const char *lb_policy_name) {
zookeeper_resolver *r;
size_t length;
char *path = args->uri->path;
- if (0 == strcmp (args->uri->authority, ""))
- {
- gpr_log (GPR_ERROR, "No authority specified in zookeeper uri");
- return NULL;
- }
+ if (0 == strcmp(args->uri->authority, "")) {
+ gpr_log(GPR_ERROR, "No authority specified in zookeeper uri");
+ return NULL;
+ }
/** Removes the trailing slash if exists */
- length = strlen (path);
- if (length > 1 && path[length - 1] == '/')
- {
- path[length - 1] = 0;
- }
-
- r = gpr_malloc (sizeof (zookeeper_resolver));
- memset (r, 0, sizeof (*r));
- gpr_ref_init (&r->refs, 1);
- gpr_mu_init (&r->mu);
- grpc_resolver_init (&r->base, &zookeeper_resolver_vtable);
- r->name = gpr_strdup (path);
+ length = strlen(path);
+ if (length > 1 && path[length - 1] == '/') {
+ path[length - 1] = 0;
+ }
+
+ r = gpr_malloc(sizeof(zookeeper_resolver));
+ memset(r, 0, sizeof(*r));
+ gpr_ref_init(&r->refs, 1);
+ gpr_mu_init(&r->mu);
+ grpc_resolver_init(&r->base, &zookeeper_resolver_vtable);
+ r->name = gpr_strdup(path);
r->subchannel_factory = args->subchannel_factory;
- grpc_subchannel_factory_ref (r->subchannel_factory);
+ grpc_subchannel_factory_ref(r->subchannel_factory);
- r->lb_policy_name = gpr_strdup (lb_policy_name);
+ r->lb_policy_name = gpr_strdup(lb_policy_name);
/** Initializes zookeeper client */
- zoo_set_debug_level (ZOO_LOG_LEVEL_WARN);
- r->zookeeper_handle = zookeeper_init (args->uri->authority, zookeeper_global_watcher, GRPC_ZOOKEEPER_SESSION_TIMEOUT, 0, 0, 0);
- if (r->zookeeper_handle == NULL)
- {
- gpr_log (GPR_ERROR, "Unable to connect to zookeeper server");
- return NULL;
- }
+ zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
+ r->zookeeper_handle =
+ zookeeper_init(args->uri->authority, zookeeper_global_watcher,
+ GRPC_ZOOKEEPER_SESSION_TIMEOUT, 0, 0, 0);
+ if (r->zookeeper_handle == NULL) {
+ gpr_log(GPR_ERROR, "Unable to connect to zookeeper server");
+ return NULL;
+ }
return &r->base;
}
-static void
-zookeeper_plugin_init ()
-{
- grpc_register_resolver_type (grpc_zookeeper_resolver_factory_create ());
+static void zookeeper_plugin_init() {
+ grpc_register_resolver_type(grpc_zookeeper_resolver_factory_create());
}
-void
-grpc_zookeeper_register ()
-{
- grpc_register_plugin (zookeeper_plugin_init, NULL);
+void grpc_zookeeper_register() {
+ grpc_register_plugin(zookeeper_plugin_init, NULL);
}
/*
* FACTORY
*/
-static void
-zookeeper_factory_ref (grpc_resolver_factory * factory)
-{
-}
+static void zookeeper_factory_ref(grpc_resolver_factory *factory) {}
-static void
-zookeeper_factory_unref (grpc_resolver_factory * factory)
-{
-}
+static void zookeeper_factory_unref(grpc_resolver_factory *factory) {}
-static char *
-zookeeper_factory_get_default_hostname (grpc_resolver_factory * factory, grpc_uri * uri)
-{
+static char *zookeeper_factory_get_default_hostname(
+ grpc_resolver_factory *factory, grpc_uri *uri) {
return NULL;
}
-static grpc_resolver *
-zookeeper_factory_create_resolver (grpc_resolver_factory * factory, grpc_resolver_args * args)
-{
- return zookeeper_create (args, "pick_first");
+static grpc_resolver *zookeeper_factory_create_resolver(
+ grpc_resolver_factory *factory, grpc_resolver_args *args) {
+ return zookeeper_create(args, "pick_first");
}
static const grpc_resolver_factory_vtable zookeeper_factory_vtable = {
- zookeeper_factory_ref, zookeeper_factory_unref,
- zookeeper_factory_create_resolver, zookeeper_factory_get_default_hostname,
- "zookeeper"
-};
+ zookeeper_factory_ref, zookeeper_factory_unref,
+ zookeeper_factory_create_resolver, zookeeper_factory_get_default_hostname,
+ "zookeeper"};
static grpc_resolver_factory zookeeper_resolver_factory = {
- &zookeeper_factory_vtable
-};
+ &zookeeper_factory_vtable};
-grpc_resolver_factory *
-grpc_zookeeper_resolver_factory_create ()
-{
+grpc_resolver_factory *grpc_zookeeper_resolver_factory_create() {
return &zookeeper_resolver_factory;
}
diff --git a/src/core/client_config/resolvers/zookeeper_resolver.h b/src/core/client_config/resolvers/zookeeper_resolver.h
index 86b4480b15..a6f002dd6d 100644
--- a/src/core/client_config/resolvers/zookeeper_resolver.h
+++ b/src/core/client_config/resolvers/zookeeper_resolver.h
@@ -37,6 +37,6 @@
#include "src/core/client_config/resolver_factory.h"
/** Create a zookeeper resolver factory */
-grpc_resolver_factory *grpc_zookeeper_resolver_factory_create (void);
+grpc_resolver_factory *grpc_zookeeper_resolver_factory_create(void);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_ZOOKEEPER_RESOLVER_H */
diff --git a/src/core/client_config/subchannel.c b/src/core/client_config/subchannel.c
index 8058c9508b..c7dfba9a4b 100644
--- a/src/core/client_config/subchannel.c
+++ b/src/core/client_config/subchannel.c
@@ -50,8 +50,7 @@
#define GRPC_SUBCHANNEL_RECONNECT_MAX_BACKOFF_SECONDS 120
#define GRPC_SUBCHANNEL_RECONNECT_JITTER 0.2
-typedef struct
-{
+typedef struct {
/* all fields protected by subchannel->mu */
/** refcount */
int refs;
@@ -59,16 +58,14 @@ typedef struct
grpc_subchannel *subchannel;
} connection;
-typedef struct
-{
+typedef struct {
grpc_closure closure;
size_t version;
grpc_subchannel *subchannel;
grpc_connectivity_state connectivity_state;
} state_watcher;
-typedef struct waiting_for_connect
-{
+typedef struct waiting_for_connect {
struct waiting_for_connect *next;
grpc_closure *notify;
grpc_pollset *pollset;
@@ -77,8 +74,7 @@ typedef struct waiting_for_connect
grpc_closure continuation;
} waiting_for_connect;
-struct grpc_subchannel
-{
+struct grpc_subchannel {
grpc_connector *connector;
/** non-transport related channel filters */
@@ -139,8 +135,7 @@ struct grpc_subchannel
gpr_uint32 random;
};
-struct grpc_subchannel_call
-{
+struct grpc_subchannel_call {
connection *connection;
gpr_refcount refs;
};
@@ -148,19 +143,25 @@ struct grpc_subchannel_call
#define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1))
#define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1))
-static grpc_subchannel_call *create_call (grpc_exec_ctx * exec_ctx, connection * con);
-static void connectivity_state_changed_locked (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, const char *reason);
-static grpc_connectivity_state compute_connectivity_locked (grpc_subchannel * c);
-static gpr_timespec compute_connect_deadline (grpc_subchannel * c);
-static void subchannel_connected (grpc_exec_ctx * exec_ctx, void *subchannel, int iomgr_success);
-
-static void subchannel_ref_locked (grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
-static int
-subchannel_unref_locked (grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
- GRPC_MUST_USE_RESULT;
- static void connection_ref_locked (connection * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
- static grpc_subchannel *connection_unref_locked (grpc_exec_ctx *exec_ctx, connection * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT;
- static void subchannel_destroy (grpc_exec_ctx * exec_ctx, grpc_subchannel * c);
+static grpc_subchannel_call *create_call(grpc_exec_ctx *exec_ctx,
+ connection *con);
+static void connectivity_state_changed_locked(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c,
+ const char *reason);
+static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c);
+static gpr_timespec compute_connect_deadline(grpc_subchannel *c);
+static void subchannel_connected(grpc_exec_ctx *exec_ctx, void *subchannel,
+ int iomgr_success);
+
+static void subchannel_ref_locked(
+ grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
+static int subchannel_unref_locked(
+ grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT;
+static void connection_ref_locked(connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
+static grpc_subchannel *connection_unref_locked(
+ grpc_exec_ctx *exec_ctx,
+ connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) GRPC_MUST_USE_RESULT;
+static void subchannel_destroy(grpc_exec_ctx *exec_ctx, grpc_subchannel *c);
#ifdef GRPC_SUBCHANNEL_REFCOUNT_DEBUG
#define SUBCHANNEL_REF_LOCKED(p, r) \
@@ -196,34 +197,29 @@ subchannel_unref_locked (grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
* connection implementation
*/
- static void connection_destroy (grpc_exec_ctx * exec_ctx, connection * c)
-{
- GPR_ASSERT (c->refs == 0);
- grpc_channel_stack_destroy (exec_ctx, CHANNEL_STACK_FROM_CONNECTION (c));
- gpr_free (c);
+static void connection_destroy(grpc_exec_ctx *exec_ctx, connection *c) {
+ GPR_ASSERT(c->refs == 0);
+ grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CONNECTION(c));
+ gpr_free(c);
}
-static void
-connection_ref_locked (connection * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
- REF_LOG ("CONNECTION", c);
- subchannel_ref_locked (c->subchannel REF_PASS_ARGS);
+static void connection_ref_locked(
+ connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
+ REF_LOG("CONNECTION", c);
+ subchannel_ref_locked(c->subchannel REF_PASS_ARGS);
++c->refs;
}
-static grpc_subchannel *
-connection_unref_locked (grpc_exec_ctx *exec_ctx, connection * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
+static grpc_subchannel *connection_unref_locked(
+ grpc_exec_ctx *exec_ctx, connection *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
grpc_subchannel *destroy = NULL;
- UNREF_LOG ("CONNECTION", c);
- if (subchannel_unref_locked (c->subchannel REF_PASS_ARGS))
- {
- destroy = c->subchannel;
- }
- if (--c->refs == 0 && c->subchannel->active != c)
- {
- connection_destroy (exec_ctx, c);
- }
+ UNREF_LOG("CONNECTION", c);
+ if (subchannel_unref_locked(c->subchannel REF_PASS_ARGS)) {
+ destroy = c->subchannel;
+ }
+ if (--c->refs == 0 && c->subchannel->active != c) {
+ connection_destroy(exec_ctx, c);
+ }
return destroy;
}
@@ -231,261 +227,237 @@ connection_unref_locked (grpc_exec_ctx *exec_ctx, connection * c GRPC_SUBCHANNEL
* grpc_subchannel implementation
*/
-static void
-subchannel_ref_locked (grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
- REF_LOG ("SUBCHANNEL", c);
+static void subchannel_ref_locked(
+ grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
+ REF_LOG("SUBCHANNEL", c);
++c->refs;
}
-static int
-subchannel_unref_locked (grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
- UNREF_LOG ("SUBCHANNEL", c);
+static int subchannel_unref_locked(
+ grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
+ UNREF_LOG("SUBCHANNEL", c);
return --c->refs == 0;
}
-void
-grpc_subchannel_ref (grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
- gpr_mu_lock (&c->mu);
- subchannel_ref_locked (c REF_PASS_ARGS);
- gpr_mu_unlock (&c->mu);
+void grpc_subchannel_ref(grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
+ gpr_mu_lock(&c->mu);
+ subchannel_ref_locked(c REF_PASS_ARGS);
+ gpr_mu_unlock(&c->mu);
}
-void
-grpc_subchannel_unref (grpc_exec_ctx *exec_ctx, grpc_subchannel * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
+void grpc_subchannel_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
int destroy;
- gpr_mu_lock (&c->mu);
- destroy = subchannel_unref_locked (c REF_PASS_ARGS);
- gpr_mu_unlock (&c->mu);
- if (destroy)
- subchannel_destroy (exec_ctx, c);
-}
-
-static void
-subchannel_destroy (grpc_exec_ctx * exec_ctx, grpc_subchannel * c)
-{
- if (c->active != NULL)
- {
- connection_destroy (exec_ctx, c->active);
- }
- gpr_free (c->filters);
- grpc_channel_args_destroy (c->args);
- gpr_free (c->addr);
- grpc_mdctx_unref (c->mdctx);
- grpc_connectivity_state_destroy (exec_ctx, &c->state_tracker);
- grpc_connector_unref (exec_ctx, c->connector);
- gpr_free (c);
+ gpr_mu_lock(&c->mu);
+ destroy = subchannel_unref_locked(c REF_PASS_ARGS);
+ gpr_mu_unlock(&c->mu);
+ if (destroy) subchannel_destroy(exec_ctx, c);
+}
+
+static void subchannel_destroy(grpc_exec_ctx *exec_ctx, grpc_subchannel *c) {
+ if (c->active != NULL) {
+ connection_destroy(exec_ctx, c->active);
+ }
+ gpr_free(c->filters);
+ grpc_channel_args_destroy(c->args);
+ gpr_free(c->addr);
+ grpc_mdctx_unref(c->mdctx);
+ grpc_connectivity_state_destroy(exec_ctx, &c->state_tracker);
+ grpc_connector_unref(exec_ctx, c->connector);
+ gpr_free(c);
}
-void
-grpc_subchannel_add_interested_party (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, grpc_pollset * pollset)
-{
- grpc_pollset_set_add_pollset (exec_ctx, c->pollset_set, pollset);
+void grpc_subchannel_add_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c,
+ grpc_pollset *pollset) {
+ grpc_pollset_set_add_pollset(exec_ctx, c->pollset_set, pollset);
}
-void
-grpc_subchannel_del_interested_party (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, grpc_pollset * pollset)
-{
- grpc_pollset_set_del_pollset (exec_ctx, c->pollset_set, pollset);
+void grpc_subchannel_del_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c,
+ grpc_pollset *pollset) {
+ grpc_pollset_set_del_pollset(exec_ctx, c->pollset_set, pollset);
}
-static gpr_uint32
-random_seed ()
-{
- return (gpr_uint32) (gpr_time_to_millis (gpr_now (GPR_CLOCK_MONOTONIC)));
+static gpr_uint32 random_seed() {
+ return (gpr_uint32)(gpr_time_to_millis(gpr_now(GPR_CLOCK_MONOTONIC)));
}
-grpc_subchannel *
-grpc_subchannel_create (grpc_connector * connector, grpc_subchannel_args * args)
-{
- grpc_subchannel *c = gpr_malloc (sizeof (*c));
- grpc_channel_element *parent_elem = grpc_channel_stack_last_element (grpc_channel_get_channel_stack (args->master));
- memset (c, 0, sizeof (*c));
+grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
+ grpc_subchannel_args *args) {
+ grpc_subchannel *c = gpr_malloc(sizeof(*c));
+ grpc_channel_element *parent_elem = grpc_channel_stack_last_element(
+ grpc_channel_get_channel_stack(args->master));
+ memset(c, 0, sizeof(*c));
c->refs = 1;
c->connector = connector;
- grpc_connector_ref (c->connector);
+ grpc_connector_ref(c->connector);
c->num_filters = args->filter_count;
- c->filters = gpr_malloc (sizeof (grpc_channel_filter *) * c->num_filters);
- memcpy (c->filters, args->filters, sizeof (grpc_channel_filter *) * c->num_filters);
- c->addr = gpr_malloc (args->addr_len);
- memcpy (c->addr, args->addr, args->addr_len);
+ c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters);
+ memcpy(c->filters, args->filters,
+ sizeof(grpc_channel_filter *) * c->num_filters);
+ c->addr = gpr_malloc(args->addr_len);
+ memcpy(c->addr, args->addr, args->addr_len);
c->addr_len = args->addr_len;
- c->args = grpc_channel_args_copy (args->args);
+ c->args = grpc_channel_args_copy(args->args);
c->mdctx = args->mdctx;
c->master = args->master;
- c->pollset_set = grpc_client_channel_get_connecting_pollset_set (parent_elem);
- c->random = random_seed ();
- grpc_mdctx_ref (c->mdctx);
- grpc_closure_init (&c->connected, subchannel_connected, c);
- grpc_connectivity_state_init (&c->state_tracker, GRPC_CHANNEL_IDLE, "subchannel");
- gpr_mu_init (&c->mu);
+ c->pollset_set = grpc_client_channel_get_connecting_pollset_set(parent_elem);
+ c->random = random_seed();
+ grpc_mdctx_ref(c->mdctx);
+ grpc_closure_init(&c->connected, subchannel_connected, c);
+ grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE,
+ "subchannel");
+ gpr_mu_init(&c->mu);
return c;
}
-static void
-continue_connect (grpc_exec_ctx * exec_ctx, grpc_subchannel * c)
-{
+static void continue_connect(grpc_exec_ctx *exec_ctx, grpc_subchannel *c) {
grpc_connect_in_args args;
args.interested_parties = c->pollset_set;
args.addr = c->addr;
args.addr_len = c->addr_len;
- args.deadline = compute_connect_deadline (c);
+ args.deadline = compute_connect_deadline(c);
args.channel_args = c->args;
- grpc_connector_connect (exec_ctx, c->connector, &args, &c->connecting_result, &c->connected);
+ grpc_connector_connect(exec_ctx, c->connector, &args, &c->connecting_result,
+ &c->connected);
}
-static void
-start_connect (grpc_exec_ctx * exec_ctx, grpc_subchannel * c)
-{
- c->backoff_delta = gpr_time_from_seconds (GRPC_SUBCHANNEL_INITIAL_CONNECT_BACKOFF_SECONDS, GPR_TIMESPAN);
- c->next_attempt = gpr_time_add (gpr_now (GPR_CLOCK_MONOTONIC), c->backoff_delta);
- continue_connect (exec_ctx, c);
+static void start_connect(grpc_exec_ctx *exec_ctx, grpc_subchannel *c) {
+ c->backoff_delta = gpr_time_from_seconds(
+ GRPC_SUBCHANNEL_INITIAL_CONNECT_BACKOFF_SECONDS, GPR_TIMESPAN);
+ c->next_attempt =
+ gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), c->backoff_delta);
+ continue_connect(exec_ctx, c);
}
-static void
-continue_creating_call (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void continue_creating_call(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
waiting_for_connect *w4c = arg;
- grpc_subchannel_del_interested_party (exec_ctx, w4c->subchannel, w4c->pollset);
- grpc_subchannel_create_call (exec_ctx, w4c->subchannel, w4c->pollset, w4c->target, w4c->notify);
- GRPC_SUBCHANNEL_UNREF (exec_ctx, w4c->subchannel, "waiting_for_connect");
- gpr_free (w4c);
+ grpc_subchannel_del_interested_party(exec_ctx, w4c->subchannel, w4c->pollset);
+ grpc_subchannel_create_call(exec_ctx, w4c->subchannel, w4c->pollset,
+ w4c->target, w4c->notify);
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, w4c->subchannel, "waiting_for_connect");
+ gpr_free(w4c);
}
-void
-grpc_subchannel_create_call (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, grpc_pollset * pollset, grpc_subchannel_call ** target, grpc_closure * notify)
-{
+void grpc_subchannel_create_call(grpc_exec_ctx *exec_ctx, grpc_subchannel *c,
+ grpc_pollset *pollset,
+ grpc_subchannel_call **target,
+ grpc_closure *notify) {
connection *con;
- gpr_mu_lock (&c->mu);
- if (c->active != NULL)
- {
- con = c->active;
- CONNECTION_REF_LOCKED (con, "call");
- gpr_mu_unlock (&c->mu);
-
- *target = create_call (exec_ctx, con);
- notify->cb (exec_ctx, notify->cb_arg, 1);
- }
- else
- {
- waiting_for_connect *w4c = gpr_malloc (sizeof (*w4c));
- w4c->next = c->waiting;
- w4c->notify = notify;
- w4c->pollset = pollset;
- w4c->target = target;
- w4c->subchannel = c;
- /* released when clearing w4c */
- SUBCHANNEL_REF_LOCKED (c, "waiting_for_connect");
- grpc_closure_init (&w4c->continuation, continue_creating_call, w4c);
- c->waiting = w4c;
- grpc_subchannel_add_interested_party (exec_ctx, c, pollset);
- if (!c->connecting)
- {
- c->connecting = 1;
- connectivity_state_changed_locked (exec_ctx, c, "create_call");
- /* released by connection */
- SUBCHANNEL_REF_LOCKED (c, "connecting");
- GRPC_CHANNEL_INTERNAL_REF (c->master, "connecting");
- gpr_mu_unlock (&c->mu);
-
- start_connect (exec_ctx, c);
- }
- else
- {
- gpr_mu_unlock (&c->mu);
- }
+ gpr_mu_lock(&c->mu);
+ if (c->active != NULL) {
+ con = c->active;
+ CONNECTION_REF_LOCKED(con, "call");
+ gpr_mu_unlock(&c->mu);
+
+ *target = create_call(exec_ctx, con);
+ notify->cb(exec_ctx, notify->cb_arg, 1);
+ } else {
+ waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c));
+ w4c->next = c->waiting;
+ w4c->notify = notify;
+ w4c->pollset = pollset;
+ w4c->target = target;
+ w4c->subchannel = c;
+ /* released when clearing w4c */
+ SUBCHANNEL_REF_LOCKED(c, "waiting_for_connect");
+ grpc_closure_init(&w4c->continuation, continue_creating_call, w4c);
+ c->waiting = w4c;
+ grpc_subchannel_add_interested_party(exec_ctx, c, pollset);
+ if (!c->connecting) {
+ c->connecting = 1;
+ connectivity_state_changed_locked(exec_ctx, c, "create_call");
+ /* released by connection */
+ SUBCHANNEL_REF_LOCKED(c, "connecting");
+ GRPC_CHANNEL_INTERNAL_REF(c->master, "connecting");
+ gpr_mu_unlock(&c->mu);
+
+ start_connect(exec_ctx, c);
+ } else {
+ gpr_mu_unlock(&c->mu);
}
+ }
}
-grpc_connectivity_state
-grpc_subchannel_check_connectivity (grpc_subchannel * c)
-{
+grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) {
grpc_connectivity_state state;
- gpr_mu_lock (&c->mu);
- state = grpc_connectivity_state_check (&c->state_tracker);
- gpr_mu_unlock (&c->mu);
+ gpr_mu_lock(&c->mu);
+ state = grpc_connectivity_state_check(&c->state_tracker);
+ gpr_mu_unlock(&c->mu);
return state;
}
-void
-grpc_subchannel_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, grpc_connectivity_state * state, grpc_closure * notify)
-{
+void grpc_subchannel_notify_on_state_change(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c,
+ grpc_connectivity_state *state,
+ grpc_closure *notify) {
int do_connect = 0;
- gpr_mu_lock (&c->mu);
- if (grpc_connectivity_state_notify_on_state_change (exec_ctx, &c->state_tracker, state, notify))
- {
- do_connect = 1;
- c->connecting = 1;
- /* released by connection */
- SUBCHANNEL_REF_LOCKED (c, "connecting");
- GRPC_CHANNEL_INTERNAL_REF (c->master, "connecting");
- connectivity_state_changed_locked (exec_ctx, c, "state_change");
- }
- gpr_mu_unlock (&c->mu);
-
- if (do_connect)
- {
- start_connect (exec_ctx, c);
- }
-}
-
-void
-grpc_subchannel_process_transport_op (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, grpc_transport_op * op)
-{
+ gpr_mu_lock(&c->mu);
+ if (grpc_connectivity_state_notify_on_state_change(
+ exec_ctx, &c->state_tracker, state, notify)) {
+ do_connect = 1;
+ c->connecting = 1;
+ /* released by connection */
+ SUBCHANNEL_REF_LOCKED(c, "connecting");
+ GRPC_CHANNEL_INTERNAL_REF(c->master, "connecting");
+ connectivity_state_changed_locked(exec_ctx, c, "state_change");
+ }
+ gpr_mu_unlock(&c->mu);
+
+ if (do_connect) {
+ start_connect(exec_ctx, c);
+ }
+}
+
+void grpc_subchannel_process_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c,
+ grpc_transport_op *op) {
connection *con = NULL;
grpc_subchannel *destroy;
int cancel_alarm = 0;
- gpr_mu_lock (&c->mu);
- if (c->active != NULL)
- {
- con = c->active;
- CONNECTION_REF_LOCKED (con, "transport-op");
- }
- if (op->disconnect)
- {
- c->disconnected = 1;
- connectivity_state_changed_locked (exec_ctx, c, "disconnect");
- if (c->have_alarm)
- {
- cancel_alarm = 1;
- }
+ gpr_mu_lock(&c->mu);
+ if (c->active != NULL) {
+ con = c->active;
+ CONNECTION_REF_LOCKED(con, "transport-op");
+ }
+ if (op->disconnect) {
+ c->disconnected = 1;
+ connectivity_state_changed_locked(exec_ctx, c, "disconnect");
+ if (c->have_alarm) {
+ cancel_alarm = 1;
}
- gpr_mu_unlock (&c->mu);
-
- if (con != NULL)
- {
- grpc_channel_stack *channel_stack = CHANNEL_STACK_FROM_CONNECTION (con);
- grpc_channel_element *top_elem = grpc_channel_stack_element (channel_stack, 0);
- top_elem->filter->start_transport_op (exec_ctx, top_elem, op);
-
- gpr_mu_lock (&c->mu);
- destroy = CONNECTION_UNREF_LOCKED (exec_ctx, con, "transport-op");
- gpr_mu_unlock (&c->mu);
- if (destroy)
- {
- subchannel_destroy (exec_ctx, destroy);
- }
+ }
+ gpr_mu_unlock(&c->mu);
+
+ if (con != NULL) {
+ grpc_channel_stack *channel_stack = CHANNEL_STACK_FROM_CONNECTION(con);
+ grpc_channel_element *top_elem =
+ grpc_channel_stack_element(channel_stack, 0);
+ top_elem->filter->start_transport_op(exec_ctx, top_elem, op);
+
+ gpr_mu_lock(&c->mu);
+ destroy = CONNECTION_UNREF_LOCKED(exec_ctx, con, "transport-op");
+ gpr_mu_unlock(&c->mu);
+ if (destroy) {
+ subchannel_destroy(exec_ctx, destroy);
}
+ }
- if (cancel_alarm)
- {
- grpc_alarm_cancel (exec_ctx, &c->alarm);
- }
+ if (cancel_alarm) {
+ grpc_alarm_cancel(exec_ctx, &c->alarm);
+ }
- if (op->disconnect)
- {
- grpc_connector_shutdown (exec_ctx, c->connector);
- }
+ if (op->disconnect) {
+ grpc_connector_shutdown(exec_ctx, c->connector);
+ }
}
-static void
-on_state_changed (grpc_exec_ctx * exec_ctx, void *p, int iomgr_success)
-{
+static void on_state_changed(grpc_exec_ctx *exec_ctx, void *p,
+ int iomgr_success) {
state_watcher *sw = p;
grpc_subchannel *c = sw->subchannel;
gpr_mu *mu = &c->mu;
@@ -494,59 +466,57 @@ on_state_changed (grpc_exec_ctx * exec_ctx, void *p, int iomgr_success)
grpc_channel_element *elem;
connection *destroy_connection = NULL;
- gpr_mu_lock (mu);
+ gpr_mu_lock(mu);
/* if we failed or there is a version number mismatch, just leave
this closure */
- if (!iomgr_success || sw->subchannel->active_version != sw->version)
- {
- goto done;
- }
+ if (!iomgr_success || sw->subchannel->active_version != sw->version) {
+ goto done;
+ }
- switch (sw->connectivity_state)
- {
+ switch (sw->connectivity_state) {
case GRPC_CHANNEL_CONNECTING:
case GRPC_CHANNEL_READY:
case GRPC_CHANNEL_IDLE:
/* all is still good: keep watching */
- memset (&op, 0, sizeof (op));
+ memset(&op, 0, sizeof(op));
op.connectivity_state = &sw->connectivity_state;
op.on_connectivity_state_change = &sw->closure;
- elem = grpc_channel_stack_element (CHANNEL_STACK_FROM_CONNECTION (c->active), 0);
- elem->filter->start_transport_op (exec_ctx, elem, &op);
+ elem = grpc_channel_stack_element(
+ CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
+ elem->filter->start_transport_op(exec_ctx, elem, &op);
/* early out */
- gpr_mu_unlock (mu);
+ gpr_mu_unlock(mu);
return;
case GRPC_CHANNEL_FATAL_FAILURE:
case GRPC_CHANNEL_TRANSIENT_FAILURE:
/* things have gone wrong, deactivate and enter idle */
- if (sw->subchannel->active->refs == 0)
- {
- destroy_connection = sw->subchannel->active;
- }
+ if (sw->subchannel->active->refs == 0) {
+ destroy_connection = sw->subchannel->active;
+ }
sw->subchannel->active = NULL;
- grpc_connectivity_state_set (exec_ctx, &c->state_tracker, c->disconnected ? GRPC_CHANNEL_FATAL_FAILURE : GRPC_CHANNEL_TRANSIENT_FAILURE, "connection_failed");
+ grpc_connectivity_state_set(exec_ctx, &c->state_tracker,
+ c->disconnected
+ ? GRPC_CHANNEL_FATAL_FAILURE
+ : GRPC_CHANNEL_TRANSIENT_FAILURE,
+ "connection_failed");
break;
- }
+ }
done:
- connectivity_state_changed_locked (exec_ctx, c, "transport_state_changed");
- destroy = SUBCHANNEL_UNREF_LOCKED (c, "state_watcher");
- gpr_free (sw);
- gpr_mu_unlock (mu);
- if (destroy)
- {
- subchannel_destroy (exec_ctx, c);
- }
- if (destroy_connection != NULL)
- {
- connection_destroy (exec_ctx, destroy_connection);
- }
-}
-
-static void
-publish_transport (grpc_exec_ctx * exec_ctx, grpc_subchannel * c)
-{
+ connectivity_state_changed_locked(exec_ctx, c, "transport_state_changed");
+ destroy = SUBCHANNEL_UNREF_LOCKED(c, "state_watcher");
+ gpr_free(sw);
+ gpr_mu_unlock(mu);
+ if (destroy) {
+ subchannel_destroy(exec_ctx, c);
+ }
+ if (destroy_connection != NULL) {
+ connection_destroy(exec_ctx, destroy_connection);
+ }
+}
+
+static void publish_transport(grpc_exec_ctx *exec_ctx, grpc_subchannel *c) {
size_t channel_stack_size;
connection *con;
grpc_channel_stack *stk;
@@ -560,46 +530,46 @@ publish_transport (grpc_exec_ctx * exec_ctx, grpc_subchannel * c)
/* build final filter list */
num_filters = c->num_filters + c->connecting_result.num_filters + 1;
- filters = gpr_malloc (sizeof (*filters) * num_filters);
- memcpy (filters, c->filters, sizeof (*filters) * c->num_filters);
- memcpy (filters + c->num_filters, c->connecting_result.filters, sizeof (*filters) * c->connecting_result.num_filters);
+ filters = gpr_malloc(sizeof(*filters) * num_filters);
+ memcpy(filters, c->filters, sizeof(*filters) * c->num_filters);
+ memcpy(filters + c->num_filters, c->connecting_result.filters,
+ sizeof(*filters) * c->connecting_result.num_filters);
filters[num_filters - 1] = &grpc_connected_channel_filter;
/* construct channel stack */
- channel_stack_size = grpc_channel_stack_size (filters, num_filters);
- con = gpr_malloc (sizeof (connection) + channel_stack_size);
- stk = (grpc_channel_stack *) (con + 1);
+ channel_stack_size = grpc_channel_stack_size(filters, num_filters);
+ con = gpr_malloc(sizeof(connection) + channel_stack_size);
+ stk = (grpc_channel_stack *)(con + 1);
con->refs = 0;
con->subchannel = c;
- grpc_channel_stack_init (exec_ctx, filters, num_filters, c->master, c->args, c->mdctx, stk);
- grpc_connected_channel_bind_transport (stk, c->connecting_result.transport);
- gpr_free (c->connecting_result.filters);
- memset (&c->connecting_result, 0, sizeof (c->connecting_result));
+ grpc_channel_stack_init(exec_ctx, filters, num_filters, c->master, c->args,
+ c->mdctx, stk);
+ grpc_connected_channel_bind_transport(stk, c->connecting_result.transport);
+ gpr_free(c->connecting_result.filters);
+ memset(&c->connecting_result, 0, sizeof(c->connecting_result));
/* initialize state watcher */
- sw = gpr_malloc (sizeof (*sw));
- grpc_closure_init (&sw->closure, on_state_changed, sw);
+ sw = gpr_malloc(sizeof(*sw));
+ grpc_closure_init(&sw->closure, on_state_changed, sw);
sw->subchannel = c;
sw->connectivity_state = GRPC_CHANNEL_READY;
- gpr_mu_lock (&c->mu);
+ gpr_mu_lock(&c->mu);
- if (c->disconnected)
- {
- gpr_mu_unlock (&c->mu);
- gpr_free (sw);
- gpr_free (filters);
- grpc_channel_stack_destroy (exec_ctx, stk);
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, c->master, "connecting");
- GRPC_SUBCHANNEL_UNREF (exec_ctx, c, "connecting");
- return;
- }
+ if (c->disconnected) {
+ gpr_mu_unlock(&c->mu);
+ gpr_free(sw);
+ gpr_free(filters);
+ grpc_channel_stack_destroy(exec_ctx, stk);
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, c->master, "connecting");
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, c, "connecting");
+ return;
+ }
/* publish */
- if (c->active != NULL && c->active->refs == 0)
- {
- destroy_connection = c->active;
- }
+ if (c->active != NULL && c->active->refs == 0) {
+ destroy_connection = c->active;
+ }
c->active = con;
c->active_version++;
sw->version = c->active_version;
@@ -607,202 +577,183 @@ publish_transport (grpc_exec_ctx * exec_ctx, grpc_subchannel * c)
/* watch for changes; subchannel ref for connecting is donated
to the state watcher */
- memset (&op, 0, sizeof (op));
+ memset(&op, 0, sizeof(op));
op.connectivity_state = &sw->connectivity_state;
op.on_connectivity_state_change = &sw->closure;
op.bind_pollset_set = c->pollset_set;
- SUBCHANNEL_REF_LOCKED (c, "state_watcher");
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, c->master, "connecting");
- GPR_ASSERT (!SUBCHANNEL_UNREF_LOCKED (c, "connecting"));
- elem = grpc_channel_stack_element (CHANNEL_STACK_FROM_CONNECTION (c->active), 0);
- elem->filter->start_transport_op (exec_ctx, elem, &op);
+ SUBCHANNEL_REF_LOCKED(c, "state_watcher");
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, c->master, "connecting");
+ GPR_ASSERT(!SUBCHANNEL_UNREF_LOCKED(c, "connecting"));
+ elem =
+ grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0);
+ elem->filter->start_transport_op(exec_ctx, elem, &op);
/* signal completion */
- connectivity_state_changed_locked (exec_ctx, c, "connected");
+ connectivity_state_changed_locked(exec_ctx, c, "connected");
w4c = c->waiting;
c->waiting = NULL;
- gpr_mu_unlock (&c->mu);
+ gpr_mu_unlock(&c->mu);
- while (w4c != NULL)
- {
- waiting_for_connect *next = w4c->next;
- grpc_exec_ctx_enqueue (exec_ctx, &w4c->continuation, 1);
- w4c = next;
- }
+ while (w4c != NULL) {
+ waiting_for_connect *next = w4c->next;
+ grpc_exec_ctx_enqueue(exec_ctx, &w4c->continuation, 1);
+ w4c = next;
+ }
- gpr_free (filters);
+ gpr_free(filters);
- if (destroy_connection != NULL)
- {
- connection_destroy (exec_ctx, destroy_connection);
- }
+ if (destroy_connection != NULL) {
+ connection_destroy(exec_ctx, destroy_connection);
+ }
}
/* Generate a random number between 0 and 1. */
-static double
-generate_uniform_random_number (grpc_subchannel * c)
-{
- c->random = (1103515245 * c->random + 12345) % ((gpr_uint32) 1 << 31);
- return c->random / (double) ((gpr_uint32) 1 << 31);
+static double generate_uniform_random_number(grpc_subchannel *c) {
+ c->random = (1103515245 * c->random + 12345) % ((gpr_uint32)1 << 31);
+ return c->random / (double)((gpr_uint32)1 << 31);
}
/* Update backoff_delta and next_attempt in subchannel */
-static void
-update_reconnect_parameters (grpc_subchannel * c)
-{
+static void update_reconnect_parameters(grpc_subchannel *c) {
gpr_int32 backoff_delta_millis, jitter;
- gpr_int32 max_backoff_millis = GRPC_SUBCHANNEL_RECONNECT_MAX_BACKOFF_SECONDS * 1000;
+ gpr_int32 max_backoff_millis =
+ GRPC_SUBCHANNEL_RECONNECT_MAX_BACKOFF_SECONDS * 1000;
double jitter_range;
- backoff_delta_millis = (gpr_int32) (gpr_time_to_millis (c->backoff_delta) * GRPC_SUBCHANNEL_RECONNECT_BACKOFF_MULTIPLIER);
- if (backoff_delta_millis > max_backoff_millis)
- {
- backoff_delta_millis = max_backoff_millis;
- }
- c->backoff_delta = gpr_time_from_millis (backoff_delta_millis, GPR_TIMESPAN);
- c->next_attempt = gpr_time_add (gpr_now (GPR_CLOCK_MONOTONIC), c->backoff_delta);
+ backoff_delta_millis =
+ (gpr_int32)(gpr_time_to_millis(c->backoff_delta) *
+ GRPC_SUBCHANNEL_RECONNECT_BACKOFF_MULTIPLIER);
+ if (backoff_delta_millis > max_backoff_millis) {
+ backoff_delta_millis = max_backoff_millis;
+ }
+ c->backoff_delta = gpr_time_from_millis(backoff_delta_millis, GPR_TIMESPAN);
+ c->next_attempt =
+ gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), c->backoff_delta);
jitter_range = GRPC_SUBCHANNEL_RECONNECT_JITTER * backoff_delta_millis;
- jitter = (gpr_int32) ((2 * generate_uniform_random_number (c) - 1) * jitter_range);
- c->next_attempt = gpr_time_add (c->next_attempt, gpr_time_from_millis (jitter, GPR_TIMESPAN));
+ jitter =
+ (gpr_int32)((2 * generate_uniform_random_number(c) - 1) * jitter_range);
+ c->next_attempt =
+ gpr_time_add(c->next_attempt, gpr_time_from_millis(jitter, GPR_TIMESPAN));
}
-static void
-on_alarm (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+static void on_alarm(grpc_exec_ctx *exec_ctx, void *arg, int iomgr_success) {
grpc_subchannel *c = arg;
- gpr_mu_lock (&c->mu);
+ gpr_mu_lock(&c->mu);
c->have_alarm = 0;
- if (c->disconnected)
- {
- iomgr_success = 0;
- }
- connectivity_state_changed_locked (exec_ctx, c, "alarm");
- gpr_mu_unlock (&c->mu);
- if (iomgr_success)
- {
- update_reconnect_parameters (c);
- continue_connect (exec_ctx, c);
- }
- else
- {
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, c->master, "connecting");
- GRPC_SUBCHANNEL_UNREF (exec_ctx, c, "connecting");
- }
-}
-
-static void
-subchannel_connected (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_success)
-{
+ if (c->disconnected) {
+ iomgr_success = 0;
+ }
+ connectivity_state_changed_locked(exec_ctx, c, "alarm");
+ gpr_mu_unlock(&c->mu);
+ if (iomgr_success) {
+ update_reconnect_parameters(c);
+ continue_connect(exec_ctx, c);
+ } else {
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, c->master, "connecting");
+ GRPC_SUBCHANNEL_UNREF(exec_ctx, c, "connecting");
+ }
+}
+
+static void subchannel_connected(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_success) {
grpc_subchannel *c = arg;
- if (c->connecting_result.transport != NULL)
- {
- publish_transport (exec_ctx, c);
- }
- else
- {
- gpr_timespec now = gpr_now (GPR_CLOCK_MONOTONIC);
- gpr_mu_lock (&c->mu);
- GPR_ASSERT (!c->have_alarm);
- c->have_alarm = 1;
- connectivity_state_changed_locked (exec_ctx, c, "connect_failed");
- grpc_alarm_init (exec_ctx, &c->alarm, c->next_attempt, on_alarm, c, now);
- gpr_mu_unlock (&c->mu);
- }
-}
-
-static gpr_timespec
-compute_connect_deadline (grpc_subchannel * c)
-{
- gpr_timespec current_deadline = gpr_time_add (c->next_attempt, c->backoff_delta);
- gpr_timespec min_deadline = gpr_time_add (gpr_now (GPR_CLOCK_MONOTONIC),
- gpr_time_from_seconds (GRPC_SUBCHANNEL_MIN_CONNECT_TIMEOUT_SECONDS,
- GPR_TIMESPAN));
- return gpr_time_cmp (current_deadline, min_deadline) > 0 ? current_deadline : min_deadline;
-}
-
-static grpc_connectivity_state
-compute_connectivity_locked (grpc_subchannel * c)
-{
- if (c->disconnected)
- {
- return GRPC_CHANNEL_FATAL_FAILURE;
- }
- if (c->connecting)
- {
- if (c->have_alarm)
- {
- return GRPC_CHANNEL_TRANSIENT_FAILURE;
- }
- return GRPC_CHANNEL_CONNECTING;
- }
- if (c->active)
- {
- return GRPC_CHANNEL_READY;
+ if (c->connecting_result.transport != NULL) {
+ publish_transport(exec_ctx, c);
+ } else {
+ gpr_timespec now = gpr_now(GPR_CLOCK_MONOTONIC);
+ gpr_mu_lock(&c->mu);
+ GPR_ASSERT(!c->have_alarm);
+ c->have_alarm = 1;
+ connectivity_state_changed_locked(exec_ctx, c, "connect_failed");
+ grpc_alarm_init(exec_ctx, &c->alarm, c->next_attempt, on_alarm, c, now);
+ gpr_mu_unlock(&c->mu);
+ }
+}
+
+static gpr_timespec compute_connect_deadline(grpc_subchannel *c) {
+ gpr_timespec current_deadline =
+ gpr_time_add(c->next_attempt, c->backoff_delta);
+ gpr_timespec min_deadline = gpr_time_add(
+ gpr_now(GPR_CLOCK_MONOTONIC),
+ gpr_time_from_seconds(GRPC_SUBCHANNEL_MIN_CONNECT_TIMEOUT_SECONDS,
+ GPR_TIMESPAN));
+ return gpr_time_cmp(current_deadline, min_deadline) > 0 ? current_deadline
+ : min_deadline;
+}
+
+static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) {
+ if (c->disconnected) {
+ return GRPC_CHANNEL_FATAL_FAILURE;
+ }
+ if (c->connecting) {
+ if (c->have_alarm) {
+ return GRPC_CHANNEL_TRANSIENT_FAILURE;
}
+ return GRPC_CHANNEL_CONNECTING;
+ }
+ if (c->active) {
+ return GRPC_CHANNEL_READY;
+ }
return GRPC_CHANNEL_IDLE;
}
-static void
-connectivity_state_changed_locked (grpc_exec_ctx * exec_ctx, grpc_subchannel * c, const char *reason)
-{
- grpc_connectivity_state current = compute_connectivity_locked (c);
- grpc_connectivity_state_set (exec_ctx, &c->state_tracker, current, reason);
+static void connectivity_state_changed_locked(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *c,
+ const char *reason) {
+ grpc_connectivity_state current = compute_connectivity_locked(c);
+ grpc_connectivity_state_set(exec_ctx, &c->state_tracker, current, reason);
}
/*
* grpc_subchannel_call implementation
*/
-void
-grpc_subchannel_call_ref (grpc_subchannel_call * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
- gpr_ref (&c->refs);
-}
-
-void
-grpc_subchannel_call_unref (grpc_exec_ctx *exec_ctx, grpc_subchannel_call * c GRPC_SUBCHANNEL_REF_EXTRA_ARGS)
-{
- if (gpr_unref (&c->refs))
- {
- gpr_mu *mu = &c->connection->subchannel->mu;
- grpc_subchannel *destroy;
- grpc_call_stack_destroy (exec_ctx, SUBCHANNEL_CALL_TO_CALL_STACK (c));
- gpr_mu_lock (mu);
- destroy = CONNECTION_UNREF_LOCKED (exec_ctx, c->connection, "call");
- gpr_mu_unlock (mu);
- gpr_free (c);
- if (destroy != NULL)
- {
- subchannel_destroy (exec_ctx, destroy);
- }
+void grpc_subchannel_call_ref(
+ grpc_subchannel_call *c GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
+ gpr_ref(&c->refs);
+}
+
+void grpc_subchannel_call_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_call *c
+ GRPC_SUBCHANNEL_REF_EXTRA_ARGS) {
+ if (gpr_unref(&c->refs)) {
+ gpr_mu *mu = &c->connection->subchannel->mu;
+ grpc_subchannel *destroy;
+ grpc_call_stack_destroy(exec_ctx, SUBCHANNEL_CALL_TO_CALL_STACK(c));
+ gpr_mu_lock(mu);
+ destroy = CONNECTION_UNREF_LOCKED(exec_ctx, c->connection, "call");
+ gpr_mu_unlock(mu);
+ gpr_free(c);
+ if (destroy != NULL) {
+ subchannel_destroy(exec_ctx, destroy);
}
+ }
}
-char *
-grpc_subchannel_call_get_peer (grpc_exec_ctx * exec_ctx, grpc_subchannel_call * call)
-{
- grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK (call);
- grpc_call_element *top_elem = grpc_call_stack_element (call_stack, 0);
- return top_elem->filter->get_peer (exec_ctx, top_elem);
+char *grpc_subchannel_call_get_peer(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_call *call) {
+ grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
+ grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
+ return top_elem->filter->get_peer(exec_ctx, top_elem);
}
-void
-grpc_subchannel_call_process_op (grpc_exec_ctx * exec_ctx, grpc_subchannel_call * call, grpc_transport_stream_op * op)
-{
- grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK (call);
- grpc_call_element *top_elem = grpc_call_stack_element (call_stack, 0);
- top_elem->filter->start_transport_stream_op (exec_ctx, top_elem, op);
+void grpc_subchannel_call_process_op(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_call *call,
+ grpc_transport_stream_op *op) {
+ grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call);
+ grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0);
+ top_elem->filter->start_transport_stream_op(exec_ctx, top_elem, op);
}
-static grpc_subchannel_call *
-create_call (grpc_exec_ctx * exec_ctx, connection * con)
-{
- grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION (con);
- grpc_subchannel_call *call = gpr_malloc (sizeof (grpc_subchannel_call) + chanstk->call_stack_size);
- grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK (call);
+static grpc_subchannel_call *create_call(grpc_exec_ctx *exec_ctx,
+ connection *con) {
+ grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con);
+ grpc_subchannel_call *call =
+ gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size);
+ grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call);
call->connection = con;
- gpr_ref_init (&call->refs, 1);
- grpc_call_stack_init (exec_ctx, chanstk, NULL, NULL, callstk);
+ gpr_ref_init(&call->refs, 1);
+ grpc_call_stack_init(exec_ctx, chanstk, NULL, NULL, callstk);
return call;
}
diff --git a/src/core/client_config/subchannel.h b/src/core/client_config/subchannel.h
index b87d6f55ff..c9e5861d9c 100644
--- a/src/core/client_config/subchannel.h
+++ b/src/core/client_config/subchannel.h
@@ -64,37 +64,59 @@ typedef struct grpc_subchannel_args grpc_subchannel_args;
#define GRPC_SUBCHANNEL_REF_EXTRA_ARGS
#endif
-void grpc_subchannel_ref (grpc_subchannel * channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
-void grpc_subchannel_unref (grpc_exec_ctx *exec_ctx, grpc_subchannel * channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
-void grpc_subchannel_call_ref (grpc_subchannel_call * call GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
-void grpc_subchannel_call_unref (grpc_exec_ctx *exec_ctx, grpc_subchannel_call * call GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
+void grpc_subchannel_ref(
+ grpc_subchannel *channel GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
+void grpc_subchannel_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *channel
+ GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
+void grpc_subchannel_call_ref(
+ grpc_subchannel_call *call GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
+void grpc_subchannel_call_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_call *call
+ GRPC_SUBCHANNEL_REF_EXTRA_ARGS);
/** construct a call (possibly asynchronously) */
-void grpc_subchannel_create_call (grpc_exec_ctx * exec_ctx, grpc_subchannel * subchannel, grpc_pollset * pollset, grpc_subchannel_call ** target, grpc_closure * notify);
+void grpc_subchannel_create_call(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *subchannel,
+ grpc_pollset *pollset,
+ grpc_subchannel_call **target,
+ grpc_closure *notify);
/** process a transport level op */
-void grpc_subchannel_process_transport_op (grpc_exec_ctx * exec_ctx, grpc_subchannel * subchannel, grpc_transport_op * op);
+void grpc_subchannel_process_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *subchannel,
+ grpc_transport_op *op);
/** poll the current connectivity state of a channel */
-grpc_connectivity_state grpc_subchannel_check_connectivity (grpc_subchannel * channel);
+grpc_connectivity_state grpc_subchannel_check_connectivity(
+ grpc_subchannel *channel);
/** call notify when the connectivity state of a channel changes from *state.
Updates *state with the new state of the channel */
-void grpc_subchannel_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_subchannel * channel, grpc_connectivity_state * state, grpc_closure * notify);
+void grpc_subchannel_notify_on_state_change(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *channel,
+ grpc_connectivity_state *state,
+ grpc_closure *notify);
/** express interest in \a channel's activities through \a pollset. */
-void grpc_subchannel_add_interested_party (grpc_exec_ctx * exec_ctx, grpc_subchannel * channel, grpc_pollset * pollset);
+void grpc_subchannel_add_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *channel,
+ grpc_pollset *pollset);
/** stop following \a channel's activity through \a pollset. */
-void grpc_subchannel_del_interested_party (grpc_exec_ctx * exec_ctx, grpc_subchannel * channel, grpc_pollset * pollset);
+void grpc_subchannel_del_interested_party(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel *channel,
+ grpc_pollset *pollset);
/** continue processing a transport op */
-void grpc_subchannel_call_process_op (grpc_exec_ctx * exec_ctx, grpc_subchannel_call * subchannel_call, grpc_transport_stream_op * op);
+void grpc_subchannel_call_process_op(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_call *subchannel_call,
+ grpc_transport_stream_op *op);
/** continue querying for peer */
-char *grpc_subchannel_call_get_peer (grpc_exec_ctx * exec_ctx, grpc_subchannel_call * subchannel_call);
+char *grpc_subchannel_call_get_peer(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_call *subchannel_call);
-struct grpc_subchannel_args
-{
+struct grpc_subchannel_args {
/** Channel filters for this channel - wrapped factories will likely
want to mutate this */
const grpc_channel_filter **filters;
@@ -112,6 +134,7 @@ struct grpc_subchannel_args
};
/** create a subchannel given a connector */
-grpc_subchannel *grpc_subchannel_create (grpc_connector * connector, grpc_subchannel_args * args);
+grpc_subchannel *grpc_subchannel_create(grpc_connector *connector,
+ grpc_subchannel_args *args);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_SUBCHANNEL_H */
diff --git a/src/core/client_config/subchannel_factory.c b/src/core/client_config/subchannel_factory.c
index f60cd02421..2c64219e8b 100644
--- a/src/core/client_config/subchannel_factory.c
+++ b/src/core/client_config/subchannel_factory.c
@@ -33,20 +33,17 @@
#include "src/core/client_config/subchannel_factory.h"
-void
-grpc_subchannel_factory_ref (grpc_subchannel_factory * factory)
-{
- factory->vtable->ref (factory);
+void grpc_subchannel_factory_ref(grpc_subchannel_factory* factory) {
+ factory->vtable->ref(factory);
}
-void
-grpc_subchannel_factory_unref (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * factory)
-{
- factory->vtable->unref (exec_ctx, factory);
+void grpc_subchannel_factory_unref(grpc_exec_ctx* exec_ctx,
+ grpc_subchannel_factory* factory) {
+ factory->vtable->unref(exec_ctx, factory);
}
-grpc_subchannel *
-grpc_subchannel_factory_create_subchannel (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * factory, grpc_subchannel_args * args)
-{
- return factory->vtable->create_subchannel (exec_ctx, factory, args);
+grpc_subchannel* grpc_subchannel_factory_create_subchannel(
+ grpc_exec_ctx* exec_ctx, grpc_subchannel_factory* factory,
+ grpc_subchannel_args* args) {
+ return factory->vtable->create_subchannel(exec_ctx, factory, args);
}
diff --git a/src/core/client_config/subchannel_factory.h b/src/core/client_config/subchannel_factory.h
index 7586a74754..c6d8cc90be 100644
--- a/src/core/client_config/subchannel_factory.h
+++ b/src/core/client_config/subchannel_factory.h
@@ -42,22 +42,25 @@ typedef struct grpc_subchannel_factory_vtable grpc_subchannel_factory_vtable;
/** Constructor for new configured channels.
Creating decorators around this type is encouraged to adapt behavior. */
-struct grpc_subchannel_factory
-{
+struct grpc_subchannel_factory {
const grpc_subchannel_factory_vtable *vtable;
};
-struct grpc_subchannel_factory_vtable
-{
- void (*ref) (grpc_subchannel_factory * factory);
- void (*unref) (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * factory);
- grpc_subchannel *(*create_subchannel) (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * factory, grpc_subchannel_args * args);
+struct grpc_subchannel_factory_vtable {
+ void (*ref)(grpc_subchannel_factory *factory);
+ void (*unref)(grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *factory);
+ grpc_subchannel *(*create_subchannel)(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_factory *factory,
+ grpc_subchannel_args *args);
};
-void grpc_subchannel_factory_ref (grpc_subchannel_factory * factory);
-void grpc_subchannel_factory_unref (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * factory);
+void grpc_subchannel_factory_ref(grpc_subchannel_factory *factory);
+void grpc_subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_factory *factory);
/** Create a new grpc_subchannel */
-grpc_subchannel *grpc_subchannel_factory_create_subchannel (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * factory, grpc_subchannel_args * args);
+grpc_subchannel *grpc_subchannel_factory_create_subchannel(
+ grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *factory,
+ grpc_subchannel_args *args);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_SUBCHANNEL_FACTORY_H */
diff --git a/src/core/client_config/subchannel_factory_decorators/add_channel_arg.c b/src/core/client_config/subchannel_factory_decorators/add_channel_arg.c
index 220f00106b..585e465fa4 100644
--- a/src/core/client_config/subchannel_factory_decorators/add_channel_arg.c
+++ b/src/core/client_config/subchannel_factory_decorators/add_channel_arg.c
@@ -34,11 +34,10 @@
#include "src/core/client_config/subchannel_factory_decorators/add_channel_arg.h"
#include "src/core/client_config/subchannel_factory_decorators/merge_channel_args.h"
-grpc_subchannel_factory *
-grpc_subchannel_factory_add_channel_arg (grpc_subchannel_factory * input, const grpc_arg * arg)
-{
+grpc_subchannel_factory *grpc_subchannel_factory_add_channel_arg(
+ grpc_subchannel_factory *input, const grpc_arg *arg) {
grpc_channel_args args;
args.num_args = 1;
- args.args = (grpc_arg *) arg;
- return grpc_subchannel_factory_merge_channel_args (input, &args);
+ args.args = (grpc_arg *)arg;
+ return grpc_subchannel_factory_merge_channel_args(input, &args);
}
diff --git a/src/core/client_config/subchannel_factory_decorators/add_channel_arg.h b/src/core/client_config/subchannel_factory_decorators/add_channel_arg.h
index 38f9c48d7a..76a535ebed 100644
--- a/src/core/client_config/subchannel_factory_decorators/add_channel_arg.h
+++ b/src/core/client_config/subchannel_factory_decorators/add_channel_arg.h
@@ -39,7 +39,8 @@
/** Takes a subchannel factory, returns a new one that mutates incoming
channel_args by adding a new argument; ownership of input, arg is retained
by the caller. */
-grpc_subchannel_factory *grpc_subchannel_factory_add_channel_arg (grpc_subchannel_factory * input, const grpc_arg * arg);
+grpc_subchannel_factory *grpc_subchannel_factory_add_channel_arg(
+ grpc_subchannel_factory *input, const grpc_arg *arg);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_SUBCHANNEL_FACTORY_DECORATORS_ADD_CHANNEL_ARG_H \
*/
diff --git a/src/core/client_config/subchannel_factory_decorators/merge_channel_args.c b/src/core/client_config/subchannel_factory_decorators/merge_channel_args.c
index 442862a4b8..cd25fdcf0f 100644
--- a/src/core/client_config/subchannel_factory_decorators/merge_channel_args.c
+++ b/src/core/client_config/subchannel_factory_decorators/merge_channel_args.c
@@ -35,58 +35,52 @@
#include <grpc/support/alloc.h>
#include "src/core/channel/channel_args.h"
-typedef struct
-{
+typedef struct {
grpc_subchannel_factory base;
gpr_refcount refs;
grpc_subchannel_factory *wrapped;
grpc_channel_args *merge_args;
} merge_args_factory;
-static void
-merge_args_factory_ref (grpc_subchannel_factory * scf)
-{
- merge_args_factory *f = (merge_args_factory *) scf;
- gpr_ref (&f->refs);
+static void merge_args_factory_ref(grpc_subchannel_factory *scf) {
+ merge_args_factory *f = (merge_args_factory *)scf;
+ gpr_ref(&f->refs);
}
-static void
-merge_args_factory_unref (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * scf)
-{
- merge_args_factory *f = (merge_args_factory *) scf;
- if (gpr_unref (&f->refs))
- {
- grpc_subchannel_factory_unref (exec_ctx, f->wrapped);
- grpc_channel_args_destroy (f->merge_args);
- gpr_free (f);
- }
+static void merge_args_factory_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_factory *scf) {
+ merge_args_factory *f = (merge_args_factory *)scf;
+ if (gpr_unref(&f->refs)) {
+ grpc_subchannel_factory_unref(exec_ctx, f->wrapped);
+ grpc_channel_args_destroy(f->merge_args);
+ gpr_free(f);
+ }
}
-static grpc_subchannel *
-merge_args_factory_create_subchannel (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * scf, grpc_subchannel_args * args)
-{
- merge_args_factory *f = (merge_args_factory *) scf;
- grpc_channel_args *final_args = grpc_channel_args_merge (args->args, f->merge_args);
+static grpc_subchannel *merge_args_factory_create_subchannel(
+ grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *scf,
+ grpc_subchannel_args *args) {
+ merge_args_factory *f = (merge_args_factory *)scf;
+ grpc_channel_args *final_args =
+ grpc_channel_args_merge(args->args, f->merge_args);
grpc_subchannel *s;
args->args = final_args;
- s = grpc_subchannel_factory_create_subchannel (exec_ctx, f->wrapped, args);
- grpc_channel_args_destroy (final_args);
+ s = grpc_subchannel_factory_create_subchannel(exec_ctx, f->wrapped, args);
+ grpc_channel_args_destroy(final_args);
return s;
}
static const grpc_subchannel_factory_vtable merge_args_factory_vtable = {
- merge_args_factory_ref, merge_args_factory_unref,
- merge_args_factory_create_subchannel
-};
+ merge_args_factory_ref, merge_args_factory_unref,
+ merge_args_factory_create_subchannel};
-grpc_subchannel_factory *
-grpc_subchannel_factory_merge_channel_args (grpc_subchannel_factory * input, const grpc_channel_args * args)
-{
- merge_args_factory *f = gpr_malloc (sizeof (*f));
+grpc_subchannel_factory *grpc_subchannel_factory_merge_channel_args(
+ grpc_subchannel_factory *input, const grpc_channel_args *args) {
+ merge_args_factory *f = gpr_malloc(sizeof(*f));
f->base.vtable = &merge_args_factory_vtable;
- gpr_ref_init (&f->refs, 1);
- grpc_subchannel_factory_ref (input);
+ gpr_ref_init(&f->refs, 1);
+ grpc_subchannel_factory_ref(input);
f->wrapped = input;
- f->merge_args = grpc_channel_args_copy (args);
+ f->merge_args = grpc_channel_args_copy(args);
return &f->base;
}
diff --git a/src/core/client_config/subchannel_factory_decorators/merge_channel_args.h b/src/core/client_config/subchannel_factory_decorators/merge_channel_args.h
index 5155509a47..a9e1691871 100644
--- a/src/core/client_config/subchannel_factory_decorators/merge_channel_args.h
+++ b/src/core/client_config/subchannel_factory_decorators/merge_channel_args.h
@@ -39,7 +39,8 @@
/** Takes a subchannel factory, returns a new one that mutates incoming
channel_args by adding a new argument; ownership of input, args is retained
by the caller. */
-grpc_subchannel_factory *grpc_subchannel_factory_merge_channel_args (grpc_subchannel_factory * input, const grpc_channel_args * args);
+grpc_subchannel_factory *grpc_subchannel_factory_merge_channel_args(
+ grpc_subchannel_factory *input, const grpc_channel_args *args);
#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_SUBCHANNEL_FACTORY_DECORATORS_MERGE_CHANNEL_ARGS_H \
*/
diff --git a/src/core/client_config/uri_parser.c b/src/core/client_config/uri_parser.c
index 4f4cc5b9c4..df9f32d403 100644
--- a/src/core/client_config/uri_parser.c
+++ b/src/core/client_config/uri_parser.c
@@ -42,35 +42,31 @@
/** a size_t default value... maps to all 1's */
#define NOT_SET (~(size_t)0)
-static grpc_uri *
-bad_uri (const char *uri_text, size_t pos, const char *section, int suppress_errors)
-{
+static grpc_uri *bad_uri(const char *uri_text, size_t pos, const char *section,
+ int suppress_errors) {
char *line_prefix;
size_t pfx_len;
- if (!suppress_errors)
- {
- gpr_asprintf (&line_prefix, "bad uri.%s: '", section);
- pfx_len = strlen (line_prefix) + pos;
- gpr_log (GPR_ERROR, "%s%s'", line_prefix, uri_text);
- gpr_free (line_prefix);
+ if (!suppress_errors) {
+ gpr_asprintf(&line_prefix, "bad uri.%s: '", section);
+ pfx_len = strlen(line_prefix) + pos;
+ gpr_log(GPR_ERROR, "%s%s'", line_prefix, uri_text);
+ gpr_free(line_prefix);
- line_prefix = gpr_malloc (pfx_len + 1);
- memset (line_prefix, ' ', pfx_len);
- line_prefix[pfx_len] = 0;
- gpr_log (GPR_ERROR, "%s^ here", line_prefix);
- gpr_free (line_prefix);
- }
+ line_prefix = gpr_malloc(pfx_len + 1);
+ memset(line_prefix, ' ', pfx_len);
+ line_prefix[pfx_len] = 0;
+ gpr_log(GPR_ERROR, "%s^ here", line_prefix);
+ gpr_free(line_prefix);
+ }
return NULL;
}
/** Returns a copy of \a src[begin, end) */
-static char *
-copy_component (const char *src, size_t begin, size_t end)
-{
- char *out = gpr_malloc (end - begin + 1);
- memcpy (out, src + begin, end - begin);
+static char *copy_component(const char *src, size_t begin, size_t end) {
+ char *out = gpr_malloc(end - begin + 1);
+ memcpy(out, src + begin, end - begin);
out[end - begin] = 0;
return out;
}
@@ -78,77 +74,66 @@ copy_component (const char *src, size_t begin, size_t end)
/** Returns how many chars to advance if \a uri_text[i] begins a valid \a pchar
* production. If \a uri_text[i] introduces an invalid \a pchar (such as percent
* sign not followed by two hex digits), NOT_SET is returned. */
-static size_t
-parse_pchar (const char *uri_text, size_t i)
-{
+static size_t parse_pchar(const char *uri_text, size_t i) {
/* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* pct-encoded = "%" HEXDIG HEXDIG
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "=" */
char c = uri_text[i];
- if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || (c == '-' || c == '.' || c == '_' || c == '~') || /* unreserved */
- (c == '!' || c == '$' || c == '&' || c == '\'' || c == '$' || c == '&' || c == '(' || c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || c == '=') /* sub-delims */ )
- {
- return 1;
+ if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) ||
+ ((c >= '0') && (c <= '9')) ||
+ (c == '-' || c == '.' || c == '_' || c == '~') || /* unreserved */
+ (c == '!' || c == '$' || c == '&' || c == '\'' || c == '$' || c == '&' ||
+ c == '(' || c == ')' || c == '*' || c == '+' || c == ',' || c == ';' ||
+ c == '=') /* sub-delims */) {
+ return 1;
+ }
+ if (c == '%') { /* pct-encoded */
+ size_t j;
+ if (uri_text[i + 1] == 0 || uri_text[i + 2] == 0) {
+ return NOT_SET;
}
- if (c == '%')
- { /* pct-encoded */
- size_t j;
- if (uri_text[i + 1] == 0 || uri_text[i + 2] == 0)
- {
- return NOT_SET;
- }
- for (j = i + 1; j < 2; j++)
- {
- c = uri_text[j];
- if (!(((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'))))
- {
- return NOT_SET;
- }
- }
- return 2;
+ for (j = i + 1; j < 2; j++) {
+ c = uri_text[j];
+ if (!(((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) ||
+ ((c >= 'A') && (c <= 'F')))) {
+ return NOT_SET;
+ }
}
+ return 2;
+ }
return 0;
}
/* *( pchar / "?" / "/" ) */
-static int
-parse_fragment_or_query (const char *uri_text, size_t * i)
-{
+static int parse_fragment_or_query(const char *uri_text, size_t *i) {
char c;
- while ((c = uri_text[*i]) != 0)
- {
- const size_t advance = parse_pchar (uri_text, *i); /* pchar */
- switch (advance)
- {
- case 0: /* uri_text[i] isn't in pchar */
- /* maybe it's ? or / */
- if (uri_text[*i] == '?' || uri_text[*i] == '/')
- {
- (*i)++;
- break;
- }
- else
- {
- return 1;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
- default:
- (*i) += advance;
- break;
- case NOT_SET: /* uri_text[i] introduces an invalid URI */
- return 0;
- }
+ while ((c = uri_text[*i]) != 0) {
+ const size_t advance = parse_pchar(uri_text, *i); /* pchar */
+ switch (advance) {
+ case 0: /* uri_text[i] isn't in pchar */
+ /* maybe it's ? or / */
+ if (uri_text[*i] == '?' || uri_text[*i] == '/') {
+ (*i)++;
+ break;
+ } else {
+ return 1;
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
+ default:
+ (*i) += advance;
+ break;
+ case NOT_SET: /* uri_text[i] introduces an invalid URI */
+ return 0;
}
+ }
/* *i is the first uri_text position past the \a query production, maybe \0 */
return 1;
}
-grpc_uri *
-grpc_uri_parse (const char *uri_text, int suppress_errors)
-{
+grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors) {
grpc_uri *uri;
size_t scheme_begin = 0;
size_t scheme_end = NOT_SET;
@@ -162,127 +147,96 @@ grpc_uri_parse (const char *uri_text, int suppress_errors)
size_t fragment_end = NOT_SET;
size_t i;
- for (i = scheme_begin; uri_text[i] != 0; i++)
- {
- if (uri_text[i] == ':')
- {
- scheme_end = i;
- break;
- }
- if (uri_text[i] >= 'a' && uri_text[i] <= 'z')
- continue;
- if (uri_text[i] >= 'A' && uri_text[i] <= 'Z')
- continue;
- if (i != scheme_begin)
- {
- if (uri_text[i] >= '0' && uri_text[i] <= '9')
- continue;
- if (uri_text[i] == '+')
- continue;
- if (uri_text[i] == '-')
- continue;
- if (uri_text[i] == '.')
- continue;
- }
+ for (i = scheme_begin; uri_text[i] != 0; i++) {
+ if (uri_text[i] == ':') {
+ scheme_end = i;
break;
}
- if (scheme_end == NOT_SET)
- {
- return bad_uri (uri_text, i, "scheme", suppress_errors);
+ if (uri_text[i] >= 'a' && uri_text[i] <= 'z') continue;
+ if (uri_text[i] >= 'A' && uri_text[i] <= 'Z') continue;
+ if (i != scheme_begin) {
+ if (uri_text[i] >= '0' && uri_text[i] <= '9') continue;
+ if (uri_text[i] == '+') continue;
+ if (uri_text[i] == '-') continue;
+ if (uri_text[i] == '.') continue;
}
-
- if (uri_text[scheme_end + 1] == '/' && uri_text[scheme_end + 2] == '/')
- {
- authority_begin = scheme_end + 3;
- for (i = authority_begin; uri_text[i] != 0 && authority_end == NOT_SET; i++)
- {
- if (uri_text[i] == '/' || uri_text[i] == '?' || uri_text[i] == '#')
- {
- authority_end = i;
- }
- }
- if (authority_end == NOT_SET && uri_text[i] == 0)
- {
- authority_end = i;
- }
- if (authority_end == NOT_SET)
- {
- return bad_uri (uri_text, i, "authority", suppress_errors);
- }
- /* TODO(ctiller): parse the authority correctly */
- path_begin = authority_end;
+ break;
+ }
+ if (scheme_end == NOT_SET) {
+ return bad_uri(uri_text, i, "scheme", suppress_errors);
+ }
+
+ if (uri_text[scheme_end + 1] == '/' && uri_text[scheme_end + 2] == '/') {
+ authority_begin = scheme_end + 3;
+ for (i = authority_begin; uri_text[i] != 0 && authority_end == NOT_SET;
+ i++) {
+ if (uri_text[i] == '/' || uri_text[i] == '?' || uri_text[i] == '#') {
+ authority_end = i;
+ }
}
- else
- {
- path_begin = scheme_end + 1;
+ if (authority_end == NOT_SET && uri_text[i] == 0) {
+ authority_end = i;
}
-
- for (i = path_begin; uri_text[i] != 0; i++)
- {
- if (uri_text[i] == '?' || uri_text[i] == '#')
- {
- path_end = i;
- break;
- }
+ if (authority_end == NOT_SET) {
+ return bad_uri(uri_text, i, "authority", suppress_errors);
}
- if (path_end == NOT_SET && uri_text[i] == 0)
- {
+ /* TODO(ctiller): parse the authority correctly */
+ path_begin = authority_end;
+ } else {
+ path_begin = scheme_end + 1;
+ }
+
+ for (i = path_begin; uri_text[i] != 0; i++) {
+ if (uri_text[i] == '?' || uri_text[i] == '#') {
path_end = i;
+ break;
}
- if (path_end == NOT_SET)
- {
- return bad_uri (uri_text, i, "path", suppress_errors);
- }
-
- if (uri_text[i] == '?')
- {
- query_begin = ++i;
- if (!parse_fragment_or_query (uri_text, &i))
- {
- return bad_uri (uri_text, i, "query", suppress_errors);
- }
- else if (uri_text[i] != 0 && uri_text[i] != '#')
- {
- /* We must be at the end or at the beginning of a fragment */
- return bad_uri (uri_text, i, "query", suppress_errors);
- }
- query_end = i;
+ }
+ if (path_end == NOT_SET && uri_text[i] == 0) {
+ path_end = i;
+ }
+ if (path_end == NOT_SET) {
+ return bad_uri(uri_text, i, "path", suppress_errors);
+ }
+
+ if (uri_text[i] == '?') {
+ query_begin = ++i;
+ if (!parse_fragment_or_query(uri_text, &i)) {
+ return bad_uri(uri_text, i, "query", suppress_errors);
+ } else if (uri_text[i] != 0 && uri_text[i] != '#') {
+ /* We must be at the end or at the beginning of a fragment */
+ return bad_uri(uri_text, i, "query", suppress_errors);
}
- if (uri_text[i] == '#')
- {
- fragment_begin = ++i;
- if (!parse_fragment_or_query (uri_text, &i))
- {
- return bad_uri (uri_text, i - fragment_end, "fragment", suppress_errors);
- }
- else if (uri_text[i] != 0)
- {
- /* We must be at the end */
- return bad_uri (uri_text, i, "fragment", suppress_errors);
- }
- fragment_end = i;
+ query_end = i;
+ }
+ if (uri_text[i] == '#') {
+ fragment_begin = ++i;
+ if (!parse_fragment_or_query(uri_text, &i)) {
+ return bad_uri(uri_text, i - fragment_end, "fragment", suppress_errors);
+ } else if (uri_text[i] != 0) {
+ /* We must be at the end */
+ return bad_uri(uri_text, i, "fragment", suppress_errors);
}
+ fragment_end = i;
+ }
- uri = gpr_malloc (sizeof (*uri));
- memset (uri, 0, sizeof (*uri));
- uri->scheme = copy_component (uri_text, scheme_begin, scheme_end);
- uri->authority = copy_component (uri_text, authority_begin, authority_end);
- uri->path = copy_component (uri_text, path_begin, path_end);
- uri->query = copy_component (uri_text, query_begin, query_end);
- uri->fragment = copy_component (uri_text, fragment_begin, fragment_end);
+ uri = gpr_malloc(sizeof(*uri));
+ memset(uri, 0, sizeof(*uri));
+ uri->scheme = copy_component(uri_text, scheme_begin, scheme_end);
+ uri->authority = copy_component(uri_text, authority_begin, authority_end);
+ uri->path = copy_component(uri_text, path_begin, path_end);
+ uri->query = copy_component(uri_text, query_begin, query_end);
+ uri->fragment = copy_component(uri_text, fragment_begin, fragment_end);
return uri;
}
-void
-grpc_uri_destroy (grpc_uri * uri)
-{
- if (!uri)
- return;
- gpr_free (uri->scheme);
- gpr_free (uri->authority);
- gpr_free (uri->path);
- gpr_free (uri->query);
- gpr_free (uri->fragment);
- gpr_free (uri);
+void grpc_uri_destroy(grpc_uri *uri) {
+ if (!uri) return;
+ gpr_free(uri->scheme);
+ gpr_free(uri->authority);
+ gpr_free(uri->path);
+ gpr_free(uri->query);
+ gpr_free(uri->fragment);
+ gpr_free(uri);
}
diff --git a/src/core/client_config/uri_parser.h b/src/core/client_config/uri_parser.h
index 1eb26e7ca6..b8daa13bd4 100644
--- a/src/core/client_config/uri_parser.h
+++ b/src/core/client_config/uri_parser.h
@@ -34,8 +34,7 @@
#ifndef GRPC_INTERNAL_CORE_CLIENT_CONFIG_URI_PARSER_H
#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_URI_PARSER_H
-typedef struct
-{
+typedef struct {
char *scheme;
char *authority;
char *path;
@@ -44,9 +43,9 @@ typedef struct
} grpc_uri;
/** parse a uri, return NULL on failure */
-grpc_uri *grpc_uri_parse (const char *uri_text, int suppress_errors);
+grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors);
/** destroy a uri */
-void grpc_uri_destroy (grpc_uri * uri);
+void grpc_uri_destroy(grpc_uri *uri);
#endif
diff --git a/src/core/compression/algorithm.c b/src/core/compression/algorithm.c
index 2627761d90..8adde13b1e 100644
--- a/src/core/compression/algorithm.c
+++ b/src/core/compression/algorithm.c
@@ -37,41 +37,30 @@
#include <grpc/compression.h>
#include <grpc/support/useful.h>
-int
-grpc_compression_algorithm_parse (const char *name, size_t name_length, grpc_compression_algorithm * algorithm)
-{
+int grpc_compression_algorithm_parse(const char *name, size_t name_length,
+ grpc_compression_algorithm *algorithm) {
/* we use strncmp not only because it's safer (even though in this case it
* doesn't matter, given that we are comparing against string literals, but
* because this way we needn't have "name" nil-terminated (useful for slice
* data, for example) */
- if (name_length == 0)
- {
- return 0;
- }
- if (strncmp (name, "identity", name_length) == 0)
- {
- *algorithm = GRPC_COMPRESS_NONE;
- }
- else if (strncmp (name, "gzip", name_length) == 0)
- {
- *algorithm = GRPC_COMPRESS_GZIP;
- }
- else if (strncmp (name, "deflate", name_length) == 0)
- {
- *algorithm = GRPC_COMPRESS_DEFLATE;
- }
- else
- {
- return 0;
- }
+ if (name_length == 0) {
+ return 0;
+ }
+ if (strncmp(name, "identity", name_length) == 0) {
+ *algorithm = GRPC_COMPRESS_NONE;
+ } else if (strncmp(name, "gzip", name_length) == 0) {
+ *algorithm = GRPC_COMPRESS_GZIP;
+ } else if (strncmp(name, "deflate", name_length) == 0) {
+ *algorithm = GRPC_COMPRESS_DEFLATE;
+ } else {
+ return 0;
+ }
return 1;
}
-int
-grpc_compression_algorithm_name (grpc_compression_algorithm algorithm, char **name)
-{
- switch (algorithm)
- {
+int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
+ char **name) {
+ switch (algorithm) {
case GRPC_COMPRESS_NONE:
*name = "identity";
break;
@@ -83,17 +72,15 @@ grpc_compression_algorithm_name (grpc_compression_algorithm algorithm, char **na
break;
default:
return 0;
- }
+ }
return 1;
}
/* TODO(dgq): Add the ability to specify parameters to the individual
* compression algorithms */
-grpc_compression_algorithm
-grpc_compression_algorithm_for_level (grpc_compression_level level)
-{
- switch (level)
- {
+grpc_compression_algorithm grpc_compression_algorithm_for_level(
+ grpc_compression_level level) {
+ switch (level) {
case GRPC_COMPRESS_LEVEL_NONE:
return GRPC_COMPRESS_NONE;
case GRPC_COMPRESS_LEVEL_LOW:
@@ -102,45 +89,39 @@ grpc_compression_algorithm_for_level (grpc_compression_level level)
return GRPC_COMPRESS_DEFLATE;
default:
/* we shouldn't be making it here */
- abort ();
- }
+ abort();
+ }
}
-grpc_compression_level
-grpc_compression_level_for_algorithm (grpc_compression_algorithm algorithm)
-{
+grpc_compression_level grpc_compression_level_for_algorithm(
+ grpc_compression_algorithm algorithm) {
grpc_compression_level clevel;
- for (clevel = GRPC_COMPRESS_LEVEL_NONE; clevel < GRPC_COMPRESS_LEVEL_COUNT; ++clevel)
- {
- if (grpc_compression_algorithm_for_level (clevel) == algorithm)
- {
- return clevel;
- }
+ for (clevel = GRPC_COMPRESS_LEVEL_NONE; clevel < GRPC_COMPRESS_LEVEL_COUNT;
+ ++clevel) {
+ if (grpc_compression_algorithm_for_level(clevel) == algorithm) {
+ return clevel;
}
- abort ();
+ }
+ abort();
}
-void
-grpc_compression_options_init (grpc_compression_options * opts)
-{
+void grpc_compression_options_init(grpc_compression_options *opts) {
opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
opts->default_compression_algorithm = GRPC_COMPRESS_NONE;
}
-void
-grpc_compression_options_enable_algorithm (grpc_compression_options * opts, grpc_compression_algorithm algorithm)
-{
- GPR_BITSET (&opts->enabled_algorithms_bitset, algorithm);
+void grpc_compression_options_enable_algorithm(
+ grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
+ GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm);
}
-void
-grpc_compression_options_disable_algorithm (grpc_compression_options * opts, grpc_compression_algorithm algorithm)
-{
- GPR_BITCLEAR (&opts->enabled_algorithms_bitset, algorithm);
+void grpc_compression_options_disable_algorithm(
+ grpc_compression_options *opts, grpc_compression_algorithm algorithm) {
+ GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm);
}
-int
-grpc_compression_options_is_algorithm_enabled (const grpc_compression_options * opts, grpc_compression_algorithm algorithm)
-{
- return GPR_BITGET (opts->enabled_algorithms_bitset, algorithm);
+int grpc_compression_options_is_algorithm_enabled(
+ const grpc_compression_options *opts,
+ grpc_compression_algorithm algorithm) {
+ return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm);
}
diff --git a/src/core/compression/message_compress.c b/src/core/compression/message_compress.c
index ac079f8724..209c1f0ff1 100644
--- a/src/core/compression/message_compress.c
+++ b/src/core/compression/message_compress.c
@@ -42,176 +42,153 @@
#define OUTPUT_BLOCK_SIZE 1024
-static int
-zlib_body (z_stream * zs, gpr_slice_buffer * input, gpr_slice_buffer * output, int (*flate) (z_stream * zs, int flush))
-{
+static int zlib_body(z_stream* zs, gpr_slice_buffer* input,
+ gpr_slice_buffer* output,
+ int (*flate)(z_stream* zs, int flush)) {
int r;
int flush;
size_t i;
- gpr_slice outbuf = gpr_slice_malloc (OUTPUT_BLOCK_SIZE);
- const uInt uint_max = ~(uInt) 0;
+ gpr_slice outbuf = gpr_slice_malloc(OUTPUT_BLOCK_SIZE);
+ const uInt uint_max = ~(uInt)0;
- GPR_ASSERT (GPR_SLICE_LENGTH (outbuf) <= uint_max);
- zs->avail_out = (uInt) GPR_SLICE_LENGTH (outbuf);
- zs->next_out = GPR_SLICE_START_PTR (outbuf);
+ GPR_ASSERT(GPR_SLICE_LENGTH(outbuf) <= uint_max);
+ zs->avail_out = (uInt)GPR_SLICE_LENGTH(outbuf);
+ zs->next_out = GPR_SLICE_START_PTR(outbuf);
flush = Z_NO_FLUSH;
- for (i = 0; i < input->count; i++)
- {
- if (i == input->count - 1)
- flush = Z_FINISH;
- GPR_ASSERT (GPR_SLICE_LENGTH (input->slices[i]) <= uint_max);
- zs->avail_in = (uInt) GPR_SLICE_LENGTH (input->slices[i]);
- zs->next_in = GPR_SLICE_START_PTR (input->slices[i]);
- do
- {
- if (zs->avail_out == 0)
- {
- gpr_slice_buffer_add_indexed (output, outbuf);
- outbuf = gpr_slice_malloc (OUTPUT_BLOCK_SIZE);
- GPR_ASSERT (GPR_SLICE_LENGTH (outbuf) <= uint_max);
- zs->avail_out = (uInt) GPR_SLICE_LENGTH (outbuf);
- zs->next_out = GPR_SLICE_START_PTR (outbuf);
- }
- r = flate (zs, flush);
- if (r == Z_STREAM_ERROR)
- {
- gpr_log (GPR_INFO, "zlib: stream error");
- goto error;
- }
- }
- while (zs->avail_out == 0);
- if (zs->avail_in)
- {
- gpr_log (GPR_INFO, "zlib: not all input consumed");
- goto error;
- }
+ for (i = 0; i < input->count; i++) {
+ if (i == input->count - 1) flush = Z_FINISH;
+ GPR_ASSERT(GPR_SLICE_LENGTH(input->slices[i]) <= uint_max);
+ zs->avail_in = (uInt)GPR_SLICE_LENGTH(input->slices[i]);
+ zs->next_in = GPR_SLICE_START_PTR(input->slices[i]);
+ do {
+ if (zs->avail_out == 0) {
+ gpr_slice_buffer_add_indexed(output, outbuf);
+ outbuf = gpr_slice_malloc(OUTPUT_BLOCK_SIZE);
+ GPR_ASSERT(GPR_SLICE_LENGTH(outbuf) <= uint_max);
+ zs->avail_out = (uInt)GPR_SLICE_LENGTH(outbuf);
+ zs->next_out = GPR_SLICE_START_PTR(outbuf);
+ }
+ r = flate(zs, flush);
+ if (r == Z_STREAM_ERROR) {
+ gpr_log(GPR_INFO, "zlib: stream error");
+ goto error;
+ }
+ } while (zs->avail_out == 0);
+ if (zs->avail_in) {
+ gpr_log(GPR_INFO, "zlib: not all input consumed");
+ goto error;
}
+ }
- GPR_ASSERT (outbuf.refcount);
+ GPR_ASSERT(outbuf.refcount);
outbuf.data.refcounted.length -= zs->avail_out;
- gpr_slice_buffer_add_indexed (output, outbuf);
+ gpr_slice_buffer_add_indexed(output, outbuf);
return 1;
error:
- gpr_slice_unref (outbuf);
+ gpr_slice_unref(outbuf);
return 0;
}
-static int
-zlib_compress (gpr_slice_buffer * input, gpr_slice_buffer * output, int gzip)
-{
+static int zlib_compress(gpr_slice_buffer* input, gpr_slice_buffer* output,
+ int gzip) {
z_stream zs;
int r;
size_t i;
size_t count_before = output->count;
size_t length_before = output->length;
- memset (&zs, 0, sizeof (zs));
- r = deflateInit2 (&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0), 8, Z_DEFAULT_STRATEGY);
- if (r != Z_OK)
- {
- gpr_log (GPR_ERROR, "deflateInit2 returns %d", r);
- return 0;
- }
- r = zlib_body (&zs, input, output, deflate) && output->length < input->length;
- if (!r)
- {
- for (i = count_before; i < output->count; i++)
- {
- gpr_slice_unref (output->slices[i]);
- }
- output->count = count_before;
- output->length = length_before;
+ memset(&zs, 0, sizeof(zs));
+ r = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | (gzip ? 16 : 0),
+ 8, Z_DEFAULT_STRATEGY);
+ if (r != Z_OK) {
+ gpr_log(GPR_ERROR, "deflateInit2 returns %d", r);
+ return 0;
+ }
+ r = zlib_body(&zs, input, output, deflate) && output->length < input->length;
+ if (!r) {
+ for (i = count_before; i < output->count; i++) {
+ gpr_slice_unref(output->slices[i]);
}
- deflateEnd (&zs);
+ output->count = count_before;
+ output->length = length_before;
+ }
+ deflateEnd(&zs);
return r;
}
-static int
-zlib_decompress (gpr_slice_buffer * input, gpr_slice_buffer * output, int gzip)
-{
+static int zlib_decompress(gpr_slice_buffer* input, gpr_slice_buffer* output,
+ int gzip) {
z_stream zs;
int r;
size_t i;
size_t count_before = output->count;
size_t length_before = output->length;
- memset (&zs, 0, sizeof (zs));
- r = inflateInit2 (&zs, 15 | (gzip ? 16 : 0));
- if (r != Z_OK)
- {
- gpr_log (GPR_ERROR, "inflateInit2 returns %d", r);
- return 0;
+ memset(&zs, 0, sizeof(zs));
+ r = inflateInit2(&zs, 15 | (gzip ? 16 : 0));
+ if (r != Z_OK) {
+ gpr_log(GPR_ERROR, "inflateInit2 returns %d", r);
+ return 0;
+ }
+ r = zlib_body(&zs, input, output, inflate);
+ if (!r) {
+ for (i = count_before; i < output->count; i++) {
+ gpr_slice_unref(output->slices[i]);
}
- r = zlib_body (&zs, input, output, inflate);
- if (!r)
- {
- for (i = count_before; i < output->count; i++)
- {
- gpr_slice_unref (output->slices[i]);
- }
- output->count = count_before;
- output->length = length_before;
- }
- inflateEnd (&zs);
+ output->count = count_before;
+ output->length = length_before;
+ }
+ inflateEnd(&zs);
return r;
}
-static int
-copy (gpr_slice_buffer * input, gpr_slice_buffer * output)
-{
+static int copy(gpr_slice_buffer* input, gpr_slice_buffer* output) {
size_t i;
- for (i = 0; i < input->count; i++)
- {
- gpr_slice_buffer_add (output, gpr_slice_ref (input->slices[i]));
- }
+ for (i = 0; i < input->count; i++) {
+ gpr_slice_buffer_add(output, gpr_slice_ref(input->slices[i]));
+ }
return 1;
}
-int
-compress_inner (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output)
-{
- switch (algorithm)
- {
+int compress_inner(grpc_compression_algorithm algorithm,
+ gpr_slice_buffer* input, gpr_slice_buffer* output) {
+ switch (algorithm) {
case GRPC_COMPRESS_NONE:
/* the fallback path always needs to be send uncompressed: we simply
rely on that here */
return 0;
case GRPC_COMPRESS_DEFLATE:
- return zlib_compress (input, output, 0);
+ return zlib_compress(input, output, 0);
case GRPC_COMPRESS_GZIP:
- return zlib_compress (input, output, 1);
+ return zlib_compress(input, output, 1);
case GRPC_COMPRESS_ALGORITHMS_COUNT:
break;
- }
- gpr_log (GPR_ERROR, "invalid compression algorithm %d", algorithm);
+ }
+ gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
return 0;
}
-int
-grpc_msg_compress (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output)
-{
- if (!compress_inner (algorithm, input, output))
- {
- copy (input, output);
- return 0;
- }
+int grpc_msg_compress(grpc_compression_algorithm algorithm,
+ gpr_slice_buffer* input, gpr_slice_buffer* output) {
+ if (!compress_inner(algorithm, input, output)) {
+ copy(input, output);
+ return 0;
+ }
return 1;
}
-int
-grpc_msg_decompress (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output)
-{
- switch (algorithm)
- {
+int grpc_msg_decompress(grpc_compression_algorithm algorithm,
+ gpr_slice_buffer* input, gpr_slice_buffer* output) {
+ switch (algorithm) {
case GRPC_COMPRESS_NONE:
- return copy (input, output);
+ return copy(input, output);
case GRPC_COMPRESS_DEFLATE:
- return zlib_decompress (input, output, 0);
+ return zlib_decompress(input, output, 0);
case GRPC_COMPRESS_GZIP:
- return zlib_decompress (input, output, 1);
+ return zlib_decompress(input, output, 1);
case GRPC_COMPRESS_ALGORITHMS_COUNT:
break;
- }
- gpr_log (GPR_ERROR, "invalid compression algorithm %d", algorithm);
+ }
+ gpr_log(GPR_ERROR, "invalid compression algorithm %d", algorithm);
return 0;
}
diff --git a/src/core/compression/message_compress.h b/src/core/compression/message_compress.h
index c052adda94..14652004b8 100644
--- a/src/core/compression/message_compress.h
+++ b/src/core/compression/message_compress.h
@@ -40,11 +40,13 @@
/* compress 'input' to 'output' using 'algorithm'.
On success, appends compressed slices to output and returns 1.
On failure, appends uncompressed slices to output and returns 0. */
-int grpc_msg_compress (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output);
+int grpc_msg_compress(grpc_compression_algorithm algorithm,
+ gpr_slice_buffer* input, gpr_slice_buffer* output);
/* decompress 'input' to 'output' using 'algorithm'.
On success, appends slices to output and returns 1.
On failure, output is unchanged, and returns 0. */
-int grpc_msg_decompress (grpc_compression_algorithm algorithm, gpr_slice_buffer * input, gpr_slice_buffer * output);
+int grpc_msg_decompress(grpc_compression_algorithm algorithm,
+ gpr_slice_buffer* input, gpr_slice_buffer* output);
#endif /* GRPC_INTERNAL_CORE_COMPRESSION_MESSAGE_COMPRESS_H */
diff --git a/src/core/debug/trace.c b/src/core/debug/trace.c
index d43d999762..3b35d81cd8 100644
--- a/src/core/debug/trace.c
+++ b/src/core/debug/trace.c
@@ -40,18 +40,15 @@
#include <grpc/support/log.h>
#include "src/core/support/env.h"
-typedef struct tracer
-{
+typedef struct tracer {
const char *name;
int *flag;
struct tracer *next;
} tracer;
static tracer *tracers;
-void
-grpc_register_tracer (const char *name, int *flag)
-{
- tracer *t = gpr_malloc (sizeof (*t));
+void grpc_register_tracer(const char *name, int *flag) {
+ tracer *t = gpr_malloc(sizeof(*t));
t->name = name;
t->flag = flag;
t->next = tracers;
@@ -59,107 +56,81 @@ grpc_register_tracer (const char *name, int *flag)
tracers = t;
}
-static void
-add (const char *beg, const char *end, char ***ss, size_t * ns)
-{
+static void add(const char *beg, const char *end, char ***ss, size_t *ns) {
size_t n = *ns;
size_t np = n + 1;
char *s;
size_t len;
- GPR_ASSERT (end >= beg);
- len = (size_t) (end - beg);
- s = gpr_malloc (len + 1);
- memcpy (s, beg, len);
+ GPR_ASSERT(end >= beg);
+ len = (size_t)(end - beg);
+ s = gpr_malloc(len + 1);
+ memcpy(s, beg, len);
s[len] = 0;
- *ss = gpr_realloc (*ss, sizeof (char **) * np);
+ *ss = gpr_realloc(*ss, sizeof(char **) * np);
(*ss)[n] = s;
*ns = np;
}
-static void
-split (const char *s, char ***ss, size_t * ns)
-{
- const char *c = strchr (s, ',');
- if (c == NULL)
- {
- add (s, s + strlen (s), ss, ns);
- }
- else
- {
- add (s, c, ss, ns);
- split (c + 1, ss, ns);
- }
+static void split(const char *s, char ***ss, size_t *ns) {
+ const char *c = strchr(s, ',');
+ if (c == NULL) {
+ add(s, s + strlen(s), ss, ns);
+ } else {
+ add(s, c, ss, ns);
+ split(c + 1, ss, ns);
+ }
}
-static void
-parse (const char *s)
-{
+static void parse(const char *s) {
char **strings = NULL;
size_t nstrings = 0;
size_t i;
- split (s, &strings, &nstrings);
+ split(s, &strings, &nstrings);
- for (i = 0; i < nstrings; i++)
- {
- grpc_tracer_set_enabled (strings[i], 1);
- }
+ for (i = 0; i < nstrings; i++) {
+ grpc_tracer_set_enabled(strings[i], 1);
+ }
- for (i = 0; i < nstrings; i++)
- {
- gpr_free (strings[i]);
- }
- gpr_free (strings);
+ for (i = 0; i < nstrings; i++) {
+ gpr_free(strings[i]);
+ }
+ gpr_free(strings);
}
-void
-grpc_tracer_init (const char *env_var)
-{
- char *e = gpr_getenv (env_var);
- if (e != NULL)
- {
- parse (e);
- gpr_free (e);
- }
+void grpc_tracer_init(const char *env_var) {
+ char *e = gpr_getenv(env_var);
+ if (e != NULL) {
+ parse(e);
+ gpr_free(e);
+ }
}
-void
-grpc_tracer_shutdown (void)
-{
- while (tracers)
- {
- tracer *t = tracers;
- tracers = t->next;
- gpr_free (t);
- }
+void grpc_tracer_shutdown(void) {
+ while (tracers) {
+ tracer *t = tracers;
+ tracers = t->next;
+ gpr_free(t);
+ }
}
-int
-grpc_tracer_set_enabled (const char *name, int enabled)
-{
+int grpc_tracer_set_enabled(const char *name, int enabled) {
tracer *t;
- if (0 == strcmp (name, "all"))
- {
- for (t = tracers; t; t = t->next)
- {
- *t->flag = 1;
- }
+ if (0 == strcmp(name, "all")) {
+ for (t = tracers; t; t = t->next) {
+ *t->flag = 1;
+ }
+ } else {
+ int found = 0;
+ for (t = tracers; t; t = t->next) {
+ if (0 == strcmp(name, t->name)) {
+ *t->flag = enabled;
+ found = 1;
+ }
}
- else
- {
- int found = 0;
- for (t = tracers; t; t = t->next)
- {
- if (0 == strcmp (name, t->name))
- {
- *t->flag = enabled;
- found = 1;
- }
- }
- if (!found)
- {
- gpr_log (GPR_ERROR, "Unknown trace var: '%s'", name);
- return 0; /* early return */
- }
+ if (!found) {
+ gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
+ return 0; /* early return */
}
+ }
return 1;
}
diff --git a/src/core/debug/trace.h b/src/core/debug/trace.h
index d11074d7c6..dc5875976e 100644
--- a/src/core/debug/trace.h
+++ b/src/core/debug/trace.h
@@ -36,8 +36,8 @@
#include <grpc/support/port_platform.h>
-void grpc_register_tracer (const char *name, int *flag);
-void grpc_tracer_init (const char *env_var_name);
-void grpc_tracer_shutdown (void);
+void grpc_register_tracer(const char *name, int *flag);
+void grpc_tracer_init(const char *env_var_name);
+void grpc_tracer_shutdown(void);
#endif /* GRPC_INTERNAL_CORE_DEBUG_TRACE_H */
diff --git a/src/core/httpcli/format_request.c b/src/core/httpcli/format_request.c
index 367fef9e32..6189fce86b 100644
--- a/src/core/httpcli/format_request.c
+++ b/src/core/httpcli/format_request.c
@@ -43,86 +43,78 @@
#include <grpc/support/string_util.h>
#include <grpc/support/useful.h>
-static void
-fill_common_header (const grpc_httpcli_request * request, gpr_strvec * buf)
-{
+static void fill_common_header(const grpc_httpcli_request *request,
+ gpr_strvec *buf) {
size_t i;
- gpr_strvec_add (buf, gpr_strdup (request->path));
- gpr_strvec_add (buf, gpr_strdup (" HTTP/1.0\r\n"));
+ gpr_strvec_add(buf, gpr_strdup(request->path));
+ gpr_strvec_add(buf, gpr_strdup(" HTTP/1.0\r\n"));
/* just in case some crazy server really expects HTTP/1.1 */
- gpr_strvec_add (buf, gpr_strdup ("Host: "));
- gpr_strvec_add (buf, gpr_strdup (request->host));
- gpr_strvec_add (buf, gpr_strdup ("\r\n"));
- gpr_strvec_add (buf, gpr_strdup ("Connection: close\r\n"));
- gpr_strvec_add (buf, gpr_strdup ("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"));
+ gpr_strvec_add(buf, gpr_strdup("Host: "));
+ gpr_strvec_add(buf, gpr_strdup(request->host));
+ gpr_strvec_add(buf, gpr_strdup("\r\n"));
+ gpr_strvec_add(buf, gpr_strdup("Connection: close\r\n"));
+ gpr_strvec_add(buf,
+ gpr_strdup("User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"));
/* user supplied headers */
- for (i = 0; i < request->hdr_count; i++)
- {
- gpr_strvec_add (buf, gpr_strdup (request->hdrs[i].key));
- gpr_strvec_add (buf, gpr_strdup (": "));
- gpr_strvec_add (buf, gpr_strdup (request->hdrs[i].value));
- gpr_strvec_add (buf, gpr_strdup ("\r\n"));
- }
+ for (i = 0; i < request->hdr_count; i++) {
+ gpr_strvec_add(buf, gpr_strdup(request->hdrs[i].key));
+ gpr_strvec_add(buf, gpr_strdup(": "));
+ gpr_strvec_add(buf, gpr_strdup(request->hdrs[i].value));
+ gpr_strvec_add(buf, gpr_strdup("\r\n"));
+ }
}
-gpr_slice
-grpc_httpcli_format_get_request (const grpc_httpcli_request * request)
-{
+gpr_slice grpc_httpcli_format_get_request(const grpc_httpcli_request *request) {
gpr_strvec out;
char *flat;
size_t flat_len;
- gpr_strvec_init (&out);
- gpr_strvec_add (&out, gpr_strdup ("GET "));
- fill_common_header (request, &out);
- gpr_strvec_add (&out, gpr_strdup ("\r\n"));
+ gpr_strvec_init(&out);
+ gpr_strvec_add(&out, gpr_strdup("GET "));
+ fill_common_header(request, &out);
+ gpr_strvec_add(&out, gpr_strdup("\r\n"));
- flat = gpr_strvec_flatten (&out, &flat_len);
- gpr_strvec_destroy (&out);
+ flat = gpr_strvec_flatten(&out, &flat_len);
+ gpr_strvec_destroy(&out);
- return gpr_slice_new (flat, flat_len, gpr_free);
+ return gpr_slice_new(flat, flat_len, gpr_free);
}
-gpr_slice
-grpc_httpcli_format_post_request (const grpc_httpcli_request * request, const char *body_bytes, size_t body_size)
-{
+gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request,
+ const char *body_bytes,
+ size_t body_size) {
gpr_strvec out;
char *tmp;
size_t out_len;
size_t i;
- gpr_strvec_init (&out);
+ gpr_strvec_init(&out);
- gpr_strvec_add (&out, gpr_strdup ("POST "));
- fill_common_header (request, &out);
- if (body_bytes)
- {
- gpr_uint8 has_content_type = 0;
- for (i = 0; i < request->hdr_count; i++)
- {
- if (strcmp (request->hdrs[i].key, "Content-Type") == 0)
- {
- has_content_type = 1;
- break;
- }
- }
- if (!has_content_type)
- {
- gpr_strvec_add (&out, gpr_strdup ("Content-Type: text/plain\r\n"));
- }
- gpr_asprintf (&tmp, "Content-Length: %lu\r\n", (unsigned long) body_size);
- gpr_strvec_add (&out, tmp);
+ gpr_strvec_add(&out, gpr_strdup("POST "));
+ fill_common_header(request, &out);
+ if (body_bytes) {
+ gpr_uint8 has_content_type = 0;
+ for (i = 0; i < request->hdr_count; i++) {
+ if (strcmp(request->hdrs[i].key, "Content-Type") == 0) {
+ has_content_type = 1;
+ break;
+ }
}
- gpr_strvec_add (&out, gpr_strdup ("\r\n"));
- tmp = gpr_strvec_flatten (&out, &out_len);
- gpr_strvec_destroy (&out);
-
- if (body_bytes)
- {
- tmp = gpr_realloc (tmp, out_len + body_size);
- memcpy (tmp + out_len, body_bytes, body_size);
- out_len += body_size;
+ if (!has_content_type) {
+ gpr_strvec_add(&out, gpr_strdup("Content-Type: text/plain\r\n"));
}
+ gpr_asprintf(&tmp, "Content-Length: %lu\r\n", (unsigned long)body_size);
+ gpr_strvec_add(&out, tmp);
+ }
+ gpr_strvec_add(&out, gpr_strdup("\r\n"));
+ tmp = gpr_strvec_flatten(&out, &out_len);
+ gpr_strvec_destroy(&out);
+
+ if (body_bytes) {
+ tmp = gpr_realloc(tmp, out_len + body_size);
+ memcpy(tmp + out_len, body_bytes, body_size);
+ out_len += body_size;
+ }
- return gpr_slice_new (tmp, out_len, gpr_free);
+ return gpr_slice_new(tmp, out_len, gpr_free);
}
diff --git a/src/core/httpcli/format_request.h b/src/core/httpcli/format_request.h
index 2238c0da50..c8dc8f7d4e 100644
--- a/src/core/httpcli/format_request.h
+++ b/src/core/httpcli/format_request.h
@@ -37,7 +37,9 @@
#include "src/core/httpcli/httpcli.h"
#include <grpc/support/slice.h>
-gpr_slice grpc_httpcli_format_get_request (const grpc_httpcli_request * request);
-gpr_slice grpc_httpcli_format_post_request (const grpc_httpcli_request * request, const char *body_bytes, size_t body_size);
+gpr_slice grpc_httpcli_format_get_request(const grpc_httpcli_request *request);
+gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request,
+ const char *body_bytes,
+ size_t body_size);
#endif /* GRPC_INTERNAL_CORE_HTTPCLI_FORMAT_REQUEST_H */
diff --git a/src/core/httpcli/httpcli.c b/src/core/httpcli/httpcli.c
index e2763e0044..a87f1aa87b 100644
--- a/src/core/httpcli/httpcli.c
+++ b/src/core/httpcli/httpcli.c
@@ -46,8 +46,7 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-typedef struct
-{
+typedef struct {
gpr_slice request_text;
grpc_httpcli_parser parser;
grpc_resolved_addresses *addresses;
@@ -72,230 +71,211 @@ typedef struct
static grpc_httpcli_get_override g_get_override = NULL;
static grpc_httpcli_post_override g_post_override = NULL;
-static void
-plaintext_handshake (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * endpoint, const char *host, void (*on_done) (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * endpoint))
-{
- on_done (exec_ctx, arg, endpoint);
+static void plaintext_handshake(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_endpoint *endpoint, const char *host,
+ void (*on_done)(grpc_exec_ctx *exec_ctx,
+ void *arg,
+ grpc_endpoint *endpoint)) {
+ on_done(exec_ctx, arg, endpoint);
}
-const grpc_httpcli_handshaker grpc_httpcli_plaintext = { "http",
- plaintext_handshake
-};
+const grpc_httpcli_handshaker grpc_httpcli_plaintext = {"http",
+ plaintext_handshake};
-void
-grpc_httpcli_context_init (grpc_httpcli_context * context)
-{
- grpc_pollset_set_init (&context->pollset_set);
+void grpc_httpcli_context_init(grpc_httpcli_context *context) {
+ grpc_pollset_set_init(&context->pollset_set);
}
-void
-grpc_httpcli_context_destroy (grpc_httpcli_context * context)
-{
- grpc_pollset_set_destroy (&context->pollset_set);
+void grpc_httpcli_context_destroy(grpc_httpcli_context *context) {
+ grpc_pollset_set_destroy(&context->pollset_set);
}
-static void next_address (grpc_exec_ctx * exec_ctx, internal_request * req);
+static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req);
-static void
-finish (grpc_exec_ctx * exec_ctx, internal_request * req, int success)
-{
- grpc_pollset_set_del_pollset (exec_ctx, &req->context->pollset_set, req->pollset);
- req->on_response (exec_ctx, req->user_data, success ? &req->parser.r : NULL);
- grpc_httpcli_parser_destroy (&req->parser);
- if (req->addresses != NULL)
- {
- grpc_resolved_addresses_destroy (req->addresses);
- }
- if (req->ep != NULL)
- {
- grpc_endpoint_destroy (exec_ctx, req->ep);
- }
- gpr_slice_unref (req->request_text);
- gpr_free (req->host);
- grpc_iomgr_unregister_object (&req->iomgr_obj);
- gpr_slice_buffer_destroy (&req->incoming);
- gpr_slice_buffer_destroy (&req->outgoing);
- gpr_free (req);
+static void finish(grpc_exec_ctx *exec_ctx, internal_request *req,
+ int success) {
+ grpc_pollset_set_del_pollset(exec_ctx, &req->context->pollset_set,
+ req->pollset);
+ req->on_response(exec_ctx, req->user_data, success ? &req->parser.r : NULL);
+ grpc_httpcli_parser_destroy(&req->parser);
+ if (req->addresses != NULL) {
+ grpc_resolved_addresses_destroy(req->addresses);
+ }
+ if (req->ep != NULL) {
+ grpc_endpoint_destroy(exec_ctx, req->ep);
+ }
+ gpr_slice_unref(req->request_text);
+ gpr_free(req->host);
+ grpc_iomgr_unregister_object(&req->iomgr_obj);
+ gpr_slice_buffer_destroy(&req->incoming);
+ gpr_slice_buffer_destroy(&req->outgoing);
+ gpr_free(req);
}
-static void on_read (grpc_exec_ctx * exec_ctx, void *user_data, int success);
+static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success);
-static void
-do_read (grpc_exec_ctx * exec_ctx, internal_request * req)
-{
- grpc_endpoint_read (exec_ctx, req->ep, &req->incoming, &req->on_read);
+static void do_read(grpc_exec_ctx *exec_ctx, internal_request *req) {
+ grpc_endpoint_read(exec_ctx, req->ep, &req->incoming, &req->on_read);
}
-static void
-on_read (grpc_exec_ctx * exec_ctx, void *user_data, int success)
-{
+static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
internal_request *req = user_data;
size_t i;
- for (i = 0; i < req->incoming.count; i++)
- {
- if (GPR_SLICE_LENGTH (req->incoming.slices[i]))
- {
- req->have_read_byte = 1;
- if (!grpc_httpcli_parser_parse (&req->parser, req->incoming.slices[i]))
- {
- finish (exec_ctx, req, 0);
- return;
- }
- }
+ for (i = 0; i < req->incoming.count; i++) {
+ if (GPR_SLICE_LENGTH(req->incoming.slices[i])) {
+ req->have_read_byte = 1;
+ if (!grpc_httpcli_parser_parse(&req->parser, req->incoming.slices[i])) {
+ finish(exec_ctx, req, 0);
+ return;
+ }
}
+ }
- if (success)
- {
- do_read (exec_ctx, req);
- }
- else if (!req->have_read_byte)
- {
- next_address (exec_ctx, req);
- }
- else
- {
- finish (exec_ctx, req, grpc_httpcli_parser_eof (&req->parser));
- }
+ if (success) {
+ do_read(exec_ctx, req);
+ } else if (!req->have_read_byte) {
+ next_address(exec_ctx, req);
+ } else {
+ finish(exec_ctx, req, grpc_httpcli_parser_eof(&req->parser));
+ }
}
-static void
-on_written (grpc_exec_ctx * exec_ctx, internal_request * req)
-{
- do_read (exec_ctx, req);
+static void on_written(grpc_exec_ctx *exec_ctx, internal_request *req) {
+ do_read(exec_ctx, req);
}
-static void
-done_write (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void done_write(grpc_exec_ctx *exec_ctx, void *arg, int success) {
internal_request *req = arg;
- if (success)
- {
- on_written (exec_ctx, req);
- }
- else
- {
- next_address (exec_ctx, req);
- }
+ if (success) {
+ on_written(exec_ctx, req);
+ } else {
+ next_address(exec_ctx, req);
+ }
}
-static void
-start_write (grpc_exec_ctx * exec_ctx, internal_request * req)
-{
- gpr_slice_ref (req->request_text);
- gpr_slice_buffer_add (&req->outgoing, req->request_text);
- grpc_endpoint_write (exec_ctx, req->ep, &req->outgoing, &req->done_write);
+static void start_write(grpc_exec_ctx *exec_ctx, internal_request *req) {
+ gpr_slice_ref(req->request_text);
+ gpr_slice_buffer_add(&req->outgoing, req->request_text);
+ grpc_endpoint_write(exec_ctx, req->ep, &req->outgoing, &req->done_write);
}
-static void
-on_handshake_done (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * ep)
-{
+static void on_handshake_done(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_endpoint *ep) {
internal_request *req = arg;
- if (!ep)
- {
- next_address (exec_ctx, req);
- return;
- }
+ if (!ep) {
+ next_address(exec_ctx, req);
+ return;
+ }
req->ep = ep;
- start_write (exec_ctx, req);
+ start_write(exec_ctx, req);
}
-static void
-on_connected (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void on_connected(grpc_exec_ctx *exec_ctx, void *arg, int success) {
internal_request *req = arg;
- if (!req->ep)
- {
- next_address (exec_ctx, req);
- return;
- }
- req->handshaker->handshake (exec_ctx, req, req->ep, req->host, on_handshake_done);
+ if (!req->ep) {
+ next_address(exec_ctx, req);
+ return;
+ }
+ req->handshaker->handshake(exec_ctx, req, req->ep, req->host,
+ on_handshake_done);
}
-static void
-next_address (grpc_exec_ctx * exec_ctx, internal_request * req)
-{
+static void next_address(grpc_exec_ctx *exec_ctx, internal_request *req) {
grpc_resolved_address *addr;
- if (req->next_address == req->addresses->naddrs)
- {
- finish (exec_ctx, req, 0);
- return;
- }
+ if (req->next_address == req->addresses->naddrs) {
+ finish(exec_ctx, req, 0);
+ return;
+ }
addr = &req->addresses->addrs[req->next_address++];
- grpc_closure_init (&req->connected, on_connected, req);
- grpc_tcp_client_connect (exec_ctx, &req->connected, &req->ep, &req->context->pollset_set, (struct sockaddr *) &addr->addr, addr->len, req->deadline);
+ grpc_closure_init(&req->connected, on_connected, req);
+ grpc_tcp_client_connect(
+ exec_ctx, &req->connected, &req->ep, &req->context->pollset_set,
+ (struct sockaddr *)&addr->addr, addr->len, req->deadline);
}
-static void
-on_resolved (grpc_exec_ctx * exec_ctx, void *arg, grpc_resolved_addresses * addresses)
-{
+static void on_resolved(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_resolved_addresses *addresses) {
internal_request *req = arg;
- if (!addresses)
- {
- finish (exec_ctx, req, 0);
- return;
- }
+ if (!addresses) {
+ finish(exec_ctx, req, 0);
+ return;
+ }
req->addresses = addresses;
req->next_address = 0;
- next_address (exec_ctx, req);
+ next_address(exec_ctx, req);
}
-static void
-internal_request_begin (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data, const char *name, gpr_slice request_text)
-{
- internal_request *req = gpr_malloc (sizeof (internal_request));
- memset (req, 0, sizeof (*req));
+static void internal_request_begin(
+ grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
+ grpc_pollset *pollset, const grpc_httpcli_request *request,
+ gpr_timespec deadline, grpc_httpcli_response_cb on_response,
+ void *user_data, const char *name, gpr_slice request_text) {
+ internal_request *req = gpr_malloc(sizeof(internal_request));
+ memset(req, 0, sizeof(*req));
req->request_text = request_text;
- grpc_httpcli_parser_init (&req->parser);
+ grpc_httpcli_parser_init(&req->parser);
req->on_response = on_response;
req->user_data = user_data;
req->deadline = deadline;
- req->handshaker = request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
+ req->handshaker =
+ request->handshaker ? request->handshaker : &grpc_httpcli_plaintext;
req->context = context;
req->pollset = pollset;
- grpc_closure_init (&req->on_read, on_read, req);
- grpc_closure_init (&req->done_write, done_write, req);
- gpr_slice_buffer_init (&req->incoming);
- gpr_slice_buffer_init (&req->outgoing);
- grpc_iomgr_register_object (&req->iomgr_obj, name);
- req->host = gpr_strdup (request->host);
+ grpc_closure_init(&req->on_read, on_read, req);
+ grpc_closure_init(&req->done_write, done_write, req);
+ gpr_slice_buffer_init(&req->incoming);
+ gpr_slice_buffer_init(&req->outgoing);
+ grpc_iomgr_register_object(&req->iomgr_obj, name);
+ req->host = gpr_strdup(request->host);
- grpc_pollset_set_add_pollset (exec_ctx, &req->context->pollset_set, req->pollset);
- grpc_resolve_address (request->host, req->handshaker->default_port, on_resolved, req);
+ grpc_pollset_set_add_pollset(exec_ctx, &req->context->pollset_set,
+ req->pollset);
+ grpc_resolve_address(request->host, req->handshaker->default_port,
+ on_resolved, req);
}
-void
-grpc_httpcli_get (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data)
-{
+void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
+ grpc_pollset *pollset,
+ const grpc_httpcli_request *request,
+ gpr_timespec deadline,
+ grpc_httpcli_response_cb on_response, void *user_data) {
char *name;
- if (g_get_override && g_get_override (exec_ctx, request, deadline, on_response, user_data))
- {
- return;
- }
- gpr_asprintf (&name, "HTTP:GET:%s:%s", request->host, request->path);
- internal_request_begin (exec_ctx, context, pollset, request, deadline, on_response, user_data, name, grpc_httpcli_format_get_request (request));
- gpr_free (name);
+ if (g_get_override &&
+ g_get_override(exec_ctx, request, deadline, on_response, user_data)) {
+ return;
+ }
+ gpr_asprintf(&name, "HTTP:GET:%s:%s", request->host, request->path);
+ internal_request_begin(exec_ctx, context, pollset, request, deadline,
+ on_response, user_data, name,
+ grpc_httpcli_format_get_request(request));
+ gpr_free(name);
}
-void
-grpc_httpcli_post (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data)
-{
+void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
+ grpc_pollset *pollset,
+ const grpc_httpcli_request *request,
+ const char *body_bytes, size_t body_size,
+ gpr_timespec deadline,
+ grpc_httpcli_response_cb on_response, void *user_data) {
char *name;
- if (g_post_override && g_post_override (exec_ctx, request, body_bytes, body_size, deadline, on_response, user_data))
- {
- return;
- }
- gpr_asprintf (&name, "HTTP:POST:%s:%s", request->host, request->path);
- internal_request_begin (exec_ctx, context, pollset, request, deadline, on_response, user_data, name, grpc_httpcli_format_post_request (request, body_bytes, body_size));
- gpr_free (name);
+ if (g_post_override &&
+ g_post_override(exec_ctx, request, body_bytes, body_size, deadline,
+ on_response, user_data)) {
+ return;
+ }
+ gpr_asprintf(&name, "HTTP:POST:%s:%s", request->host, request->path);
+ internal_request_begin(
+ exec_ctx, context, pollset, request, deadline, on_response, user_data,
+ name, grpc_httpcli_format_post_request(request, body_bytes, body_size));
+ gpr_free(name);
}
-void
-grpc_httpcli_set_override (grpc_httpcli_get_override get, grpc_httpcli_post_override post)
-{
+void grpc_httpcli_set_override(grpc_httpcli_get_override get,
+ grpc_httpcli_post_override post) {
g_get_override = get;
g_post_override = post;
}
diff --git a/src/core/httpcli/httpcli.h b/src/core/httpcli/httpcli.h
index 9c489721b6..6469c2f03e 100644
--- a/src/core/httpcli/httpcli.h
+++ b/src/core/httpcli/httpcli.h
@@ -47,8 +47,7 @@
#define GRPC_HTTPCLI_MAX_HEADER_LENGTH 4096
/* A single header to be passed in a request */
-typedef struct grpc_httpcli_header
-{
+typedef struct grpc_httpcli_header {
char *key;
char *value;
} grpc_httpcli_header;
@@ -56,23 +55,23 @@ typedef struct grpc_httpcli_header
/* Tracks in-progress http requests
TODO(ctiller): allow caching and capturing multiple requests for the
same content and combining them */
-typedef struct grpc_httpcli_context
-{
+typedef struct grpc_httpcli_context {
grpc_pollset_set pollset_set;
} grpc_httpcli_context;
-typedef struct
-{
+typedef struct {
const char *default_port;
- void (*handshake) (grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint * endpoint, const char *host, void (*on_done) (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * endpoint));
+ void (*handshake)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint,
+ const char *host,
+ void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_endpoint *endpoint));
} grpc_httpcli_handshaker;
extern const grpc_httpcli_handshaker grpc_httpcli_plaintext;
extern const grpc_httpcli_handshaker grpc_httpcli_ssl;
/* A request */
-typedef struct grpc_httpcli_request
-{
+typedef struct grpc_httpcli_request {
/* The host name to connect to */
char *host;
/* The path of the resource to fetch */
@@ -87,8 +86,7 @@ typedef struct grpc_httpcli_request
} grpc_httpcli_request;
/* A response */
-typedef struct grpc_httpcli_response
-{
+typedef struct grpc_httpcli_response {
/* HTTP status code */
int status;
/* Headers: count and key/values */
@@ -100,10 +98,12 @@ typedef struct grpc_httpcli_response
} grpc_httpcli_response;
/* Callback for grpc_httpcli_get and grpc_httpcli_post. */
-typedef void (*grpc_httpcli_response_cb) (grpc_exec_ctx * exec_ctx, void *user_data, const grpc_httpcli_response * response);
+typedef void (*grpc_httpcli_response_cb)(grpc_exec_ctx *exec_ctx,
+ void *user_data,
+ const grpc_httpcli_response *response);
-void grpc_httpcli_context_init (grpc_httpcli_context * context);
-void grpc_httpcli_context_destroy (grpc_httpcli_context * context);
+void grpc_httpcli_context_init(grpc_httpcli_context *context);
+void grpc_httpcli_context_destroy(grpc_httpcli_context *context);
/* Asynchronously perform a HTTP GET.
'context' specifies the http context under which to do the get
@@ -115,7 +115,11 @@ void grpc_httpcli_context_destroy (grpc_httpcli_context * context);
'deadline' contains a deadline for the request (or gpr_inf_future)
'on_response' is a callback to report results to (and 'user_data' is a user
supplied pointer to pass to said call) */
-void grpc_httpcli_get (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
+void grpc_httpcli_get(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
+ grpc_pollset *pollset,
+ const grpc_httpcli_request *request,
+ gpr_timespec deadline,
+ grpc_httpcli_response_cb on_response, void *user_data);
/* Asynchronously perform a HTTP POST.
'context' specifies the http context under which to do the post
@@ -132,12 +136,25 @@ void grpc_httpcli_get (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context,
'on_response' is a callback to report results to (and 'user_data' is a user
supplied pointer to pass to said call)
Does not support ?var1=val1&var2=val2 in the path. */
-void grpc_httpcli_post (grpc_exec_ctx * exec_ctx, grpc_httpcli_context * context, grpc_pollset * pollset, const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
+void grpc_httpcli_post(grpc_exec_ctx *exec_ctx, grpc_httpcli_context *context,
+ grpc_pollset *pollset,
+ const grpc_httpcli_request *request,
+ const char *body_bytes, size_t body_size,
+ gpr_timespec deadline,
+ grpc_httpcli_response_cb on_response, void *user_data);
/* override functions return 1 if they handled the request, 0 otherwise */
-typedef int (*grpc_httpcli_get_override) (grpc_exec_ctx * exec_ctx, const grpc_httpcli_request * request, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
-typedef int (*grpc_httpcli_post_override) (grpc_exec_ctx * exec_ctx, const grpc_httpcli_request * request, const char *body_bytes, size_t body_size, gpr_timespec deadline, grpc_httpcli_response_cb on_response, void *user_data);
-
-void grpc_httpcli_set_override (grpc_httpcli_get_override get, grpc_httpcli_post_override post);
+typedef int (*grpc_httpcli_get_override)(grpc_exec_ctx *exec_ctx,
+ const grpc_httpcli_request *request,
+ gpr_timespec deadline,
+ grpc_httpcli_response_cb on_response,
+ void *user_data);
+typedef int (*grpc_httpcli_post_override)(
+ grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
+ const char *body_bytes, size_t body_size, gpr_timespec deadline,
+ grpc_httpcli_response_cb on_response, void *user_data);
+
+void grpc_httpcli_set_override(grpc_httpcli_get_override get,
+ grpc_httpcli_post_override post);
#endif /* GRPC_INTERNAL_CORE_HTTPCLI_HTTPCLI_H */
diff --git a/src/core/httpcli/httpcli_security_connector.c b/src/core/httpcli/httpcli_security_connector.c
index a9b9a5991e..fc6699c918 100644
--- a/src/core/httpcli/httpcli_security_connector.c
+++ b/src/core/httpcli/httpcli_security_connector.c
@@ -42,146 +42,147 @@
#include <grpc/support/string_util.h>
#include "src/core/tsi/ssl_transport_security.h"
-typedef struct
-{
+typedef struct {
grpc_channel_security_connector base;
tsi_ssl_handshaker_factory *handshaker_factory;
char *secure_peer_name;
} grpc_httpcli_ssl_channel_security_connector;
-static void
-httpcli_ssl_destroy (grpc_security_connector * sc)
-{
- grpc_httpcli_ssl_channel_security_connector *c = (grpc_httpcli_ssl_channel_security_connector *) sc;
- if (c->handshaker_factory != NULL)
- {
- tsi_ssl_handshaker_factory_destroy (c->handshaker_factory);
- }
- if (c->secure_peer_name != NULL)
- gpr_free (c->secure_peer_name);
- gpr_free (sc);
+static void httpcli_ssl_destroy(grpc_security_connector *sc) {
+ grpc_httpcli_ssl_channel_security_connector *c =
+ (grpc_httpcli_ssl_channel_security_connector *)sc;
+ if (c->handshaker_factory != NULL) {
+ tsi_ssl_handshaker_factory_destroy(c->handshaker_factory);
+ }
+ if (c->secure_peer_name != NULL) gpr_free(c->secure_peer_name);
+ gpr_free(sc);
}
-static void
-httpcli_ssl_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- grpc_httpcli_ssl_channel_security_connector *c = (grpc_httpcli_ssl_channel_security_connector *) sc;
+static void httpcli_ssl_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ grpc_httpcli_ssl_channel_security_connector *c =
+ (grpc_httpcli_ssl_channel_security_connector *)sc;
tsi_result result = TSI_OK;
tsi_handshaker *handshaker;
- if (c->handshaker_factory == NULL)
- {
- cb (exec_ctx, user_data, GRPC_SECURITY_ERROR, nonsecure_endpoint, NULL);
- return;
- }
- result = tsi_ssl_handshaker_factory_create_handshaker (c->handshaker_factory, c->secure_peer_name, &handshaker);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshaker creation failed with error %s.", tsi_result_to_string (result));
- cb (exec_ctx, user_data, GRPC_SECURITY_ERROR, nonsecure_endpoint, NULL);
- }
- else
- {
- grpc_do_security_handshake (exec_ctx, handshaker, sc, nonsecure_endpoint, cb, user_data);
- }
+ if (c->handshaker_factory == NULL) {
+ cb(exec_ctx, user_data, GRPC_SECURITY_ERROR, nonsecure_endpoint, NULL);
+ return;
+ }
+ result = tsi_ssl_handshaker_factory_create_handshaker(
+ c->handshaker_factory, c->secure_peer_name, &handshaker);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.",
+ tsi_result_to_string(result));
+ cb(exec_ctx, user_data, GRPC_SECURITY_ERROR, nonsecure_endpoint, NULL);
+ } else {
+ grpc_do_security_handshake(exec_ctx, handshaker, sc, nonsecure_endpoint, cb,
+ user_data);
+ }
}
-static grpc_security_status
-httpcli_ssl_check_peer (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data)
-{
- grpc_httpcli_ssl_channel_security_connector *c = (grpc_httpcli_ssl_channel_security_connector *) sc;
+static grpc_security_status httpcli_ssl_check_peer(grpc_security_connector *sc,
+ tsi_peer peer,
+ grpc_security_check_cb cb,
+ void *user_data) {
+ grpc_httpcli_ssl_channel_security_connector *c =
+ (grpc_httpcli_ssl_channel_security_connector *)sc;
grpc_security_status status = GRPC_SECURITY_OK;
/* Check the peer name. */
- if (c->secure_peer_name != NULL && !tsi_ssl_peer_matches_name (&peer, c->secure_peer_name))
- {
- gpr_log (GPR_ERROR, "Peer name %s is not in peer certificate", c->secure_peer_name);
- status = GRPC_SECURITY_ERROR;
- }
- tsi_peer_destruct (&peer);
+ if (c->secure_peer_name != NULL &&
+ !tsi_ssl_peer_matches_name(&peer, c->secure_peer_name)) {
+ gpr_log(GPR_ERROR, "Peer name %s is not in peer certificate",
+ c->secure_peer_name);
+ status = GRPC_SECURITY_ERROR;
+ }
+ tsi_peer_destruct(&peer);
return status;
}
static grpc_security_connector_vtable httpcli_ssl_vtable = {
- httpcli_ssl_destroy, httpcli_ssl_do_handshake, httpcli_ssl_check_peer
-};
+ httpcli_ssl_destroy, httpcli_ssl_do_handshake, httpcli_ssl_check_peer};
-static grpc_security_status
-httpcli_ssl_channel_security_connector_create (const unsigned char *pem_root_certs, size_t pem_root_certs_size, const char *secure_peer_name, grpc_channel_security_connector ** sc)
-{
+static grpc_security_status httpcli_ssl_channel_security_connector_create(
+ const unsigned char *pem_root_certs, size_t pem_root_certs_size,
+ const char *secure_peer_name, grpc_channel_security_connector **sc) {
tsi_result result = TSI_OK;
grpc_httpcli_ssl_channel_security_connector *c;
- if (secure_peer_name != NULL && pem_root_certs == NULL)
- {
- gpr_log (GPR_ERROR, "Cannot assert a secure peer name without a trust root.");
- return GRPC_SECURITY_ERROR;
- }
+ if (secure_peer_name != NULL && pem_root_certs == NULL) {
+ gpr_log(GPR_ERROR,
+ "Cannot assert a secure peer name without a trust root.");
+ return GRPC_SECURITY_ERROR;
+ }
- c = gpr_malloc (sizeof (grpc_httpcli_ssl_channel_security_connector));
- memset (c, 0, sizeof (grpc_httpcli_ssl_channel_security_connector));
+ c = gpr_malloc(sizeof(grpc_httpcli_ssl_channel_security_connector));
+ memset(c, 0, sizeof(grpc_httpcli_ssl_channel_security_connector));
- gpr_ref_init (&c->base.base.refcount, 1);
+ gpr_ref_init(&c->base.base.refcount, 1);
c->base.base.is_client_side = 1;
c->base.base.vtable = &httpcli_ssl_vtable;
- if (secure_peer_name != NULL)
- {
- c->secure_peer_name = gpr_strdup (secure_peer_name);
- }
- result = tsi_create_ssl_client_handshaker_factory (NULL, 0, NULL, 0, pem_root_certs, pem_root_certs_size, NULL, NULL, NULL, 0, &c->handshaker_factory);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string (result));
- httpcli_ssl_destroy (&c->base.base);
- *sc = NULL;
- return GRPC_SECURITY_ERROR;
- }
+ if (secure_peer_name != NULL) {
+ c->secure_peer_name = gpr_strdup(secure_peer_name);
+ }
+ result = tsi_create_ssl_client_handshaker_factory(
+ NULL, 0, NULL, 0, pem_root_certs, pem_root_certs_size, NULL, NULL, NULL,
+ 0, &c->handshaker_factory);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
+ tsi_result_to_string(result));
+ httpcli_ssl_destroy(&c->base.base);
+ *sc = NULL;
+ return GRPC_SECURITY_ERROR;
+ }
*sc = &c->base;
return GRPC_SECURITY_OK;
}
/* handshaker */
-typedef struct
-{
- void (*func) (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * endpoint);
+typedef struct {
+ void (*func)(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *endpoint);
void *arg;
} on_done_closure;
-static void
-on_secure_transport_setup_done (grpc_exec_ctx * exec_ctx, void *rp, grpc_security_status status, grpc_endpoint * wrapped_endpoint, grpc_endpoint * secure_endpoint)
-{
+static void on_secure_transport_setup_done(grpc_exec_ctx *exec_ctx, void *rp,
+ grpc_security_status status,
+ grpc_endpoint *wrapped_endpoint,
+ grpc_endpoint *secure_endpoint) {
on_done_closure *c = rp;
- if (status != GRPC_SECURITY_OK)
- {
- gpr_log (GPR_ERROR, "Secure transport setup failed with error %d.", status);
- c->func (exec_ctx, c->arg, NULL);
- }
- else
- {
- c->func (exec_ctx, c->arg, secure_endpoint);
- }
- gpr_free (c);
+ if (status != GRPC_SECURITY_OK) {
+ gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
+ c->func(exec_ctx, c->arg, NULL);
+ } else {
+ c->func(exec_ctx, c->arg, secure_endpoint);
+ }
+ gpr_free(c);
}
-static void
-ssl_handshake (grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint * tcp, const char *host, void (*on_done) (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * endpoint))
-{
+static void ssl_handshake(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_endpoint *tcp, const char *host,
+ void (*on_done)(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_endpoint *endpoint)) {
grpc_channel_security_connector *sc = NULL;
const unsigned char *pem_root_certs = NULL;
- on_done_closure *c = gpr_malloc (sizeof (*c));
- size_t pem_root_certs_size = grpc_get_default_ssl_roots (&pem_root_certs);
- if (pem_root_certs == NULL || pem_root_certs_size == 0)
- {
- gpr_log (GPR_ERROR, "Could not get default pem root certs.");
- on_done (exec_ctx, arg, NULL);
- gpr_free (c);
- return;
- }
+ on_done_closure *c = gpr_malloc(sizeof(*c));
+ size_t pem_root_certs_size = grpc_get_default_ssl_roots(&pem_root_certs);
+ if (pem_root_certs == NULL || pem_root_certs_size == 0) {
+ gpr_log(GPR_ERROR, "Could not get default pem root certs.");
+ on_done(exec_ctx, arg, NULL);
+ gpr_free(c);
+ return;
+ }
c->func = on_done;
c->arg = arg;
- GPR_ASSERT (httpcli_ssl_channel_security_connector_create (pem_root_certs, pem_root_certs_size, host, &sc) == GRPC_SECURITY_OK);
- grpc_security_connector_do_handshake (exec_ctx, &sc->base, tcp, on_secure_transport_setup_done, c);
- GRPC_SECURITY_CONNECTOR_UNREF (&sc->base, "httpcli");
+ GPR_ASSERT(httpcli_ssl_channel_security_connector_create(
+ pem_root_certs, pem_root_certs_size, host, &sc) ==
+ GRPC_SECURITY_OK);
+ grpc_security_connector_do_handshake(exec_ctx, &sc->base, tcp,
+ on_secure_transport_setup_done, c);
+ GRPC_SECURITY_CONNECTOR_UNREF(&sc->base, "httpcli");
}
-const grpc_httpcli_handshaker grpc_httpcli_ssl = { "https", ssl_handshake };
+const grpc_httpcli_handshaker grpc_httpcli_ssl = {"https", ssl_handshake};
diff --git a/src/core/httpcli/parser.c b/src/core/httpcli/parser.c
index 3c9211efba..404906d5ae 100644
--- a/src/core/httpcli/parser.c
+++ b/src/core/httpcli/parser.c
@@ -39,220 +39,178 @@
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
-static int
-handle_response_line (grpc_httpcli_parser * parser)
-{
+static int handle_response_line(grpc_httpcli_parser *parser) {
gpr_uint8 *beg = parser->cur_line;
gpr_uint8 *cur = beg;
gpr_uint8 *end = beg + parser->cur_line_length;
- if (cur == end || *cur++ != 'H')
- goto error;
- if (cur == end || *cur++ != 'T')
- goto error;
- if (cur == end || *cur++ != 'T')
- goto error;
- if (cur == end || *cur++ != 'P')
- goto error;
- if (cur == end || *cur++ != '/')
- goto error;
- if (cur == end || *cur++ != '1')
- goto error;
- if (cur == end || *cur++ != '.')
- goto error;
- if (cur == end || *cur < '0' || *cur++ > '1')
- goto error;
- if (cur == end || *cur++ != ' ')
- goto error;
- if (cur == end || *cur < '1' || *cur++ > '9')
- goto error;
- if (cur == end || *cur < '0' || *cur++ > '9')
- goto error;
- if (cur == end || *cur < '0' || *cur++ > '9')
- goto error;
- parser->r.status = (cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0');
- if (cur == end || *cur++ != ' ')
- goto error;
+ if (cur == end || *cur++ != 'H') goto error;
+ if (cur == end || *cur++ != 'T') goto error;
+ if (cur == end || *cur++ != 'T') goto error;
+ if (cur == end || *cur++ != 'P') goto error;
+ if (cur == end || *cur++ != '/') goto error;
+ if (cur == end || *cur++ != '1') goto error;
+ if (cur == end || *cur++ != '.') goto error;
+ if (cur == end || *cur < '0' || *cur++ > '1') goto error;
+ if (cur == end || *cur++ != ' ') goto error;
+ if (cur == end || *cur < '1' || *cur++ > '9') goto error;
+ if (cur == end || *cur < '0' || *cur++ > '9') goto error;
+ if (cur == end || *cur < '0' || *cur++ > '9') goto error;
+ parser->r.status =
+ (cur[-3] - '0') * 100 + (cur[-2] - '0') * 10 + (cur[-1] - '0');
+ if (cur == end || *cur++ != ' ') goto error;
/* we don't really care about the status code message */
return 1;
error:
- gpr_log (GPR_ERROR, "Failed parsing response line");
+ gpr_log(GPR_ERROR, "Failed parsing response line");
return 0;
}
-static char *
-buf2str (void *buffer, size_t length)
-{
- char *out = gpr_malloc (length + 1);
- memcpy (out, buffer, length);
+static char *buf2str(void *buffer, size_t length) {
+ char *out = gpr_malloc(length + 1);
+ memcpy(out, buffer, length);
out[length] = 0;
return out;
}
-static int
-add_header (grpc_httpcli_parser * parser)
-{
+static int add_header(grpc_httpcli_parser *parser) {
gpr_uint8 *beg = parser->cur_line;
gpr_uint8 *cur = beg;
gpr_uint8 *end = beg + parser->cur_line_length;
- grpc_httpcli_header hdr = { NULL, NULL };
+ grpc_httpcli_header hdr = {NULL, NULL};
- GPR_ASSERT (cur != end);
+ GPR_ASSERT(cur != end);
- if (*cur == ' ' || *cur == '\t')
- {
- gpr_log (GPR_ERROR, "Continued header lines not supported yet");
- goto error;
- }
-
- while (cur != end && *cur != ':')
- {
- cur++;
- }
- if (cur == end)
- {
- gpr_log (GPR_ERROR, "Didn't find ':' in header string");
- goto error;
- }
- GPR_ASSERT (cur >= beg);
- hdr.key = buf2str (beg, (size_t) (cur - beg));
- cur++; /* skip : */
-
- while (cur != end && (*cur == ' ' || *cur == '\t'))
- {
- cur++;
- }
- GPR_ASSERT (end - cur >= 2);
- hdr.value = buf2str (cur, (size_t) (end - cur) - 2);
+ if (*cur == ' ' || *cur == '\t') {
+ gpr_log(GPR_ERROR, "Continued header lines not supported yet");
+ goto error;
+ }
- if (parser->r.hdr_count == parser->hdr_capacity)
- {
- parser->hdr_capacity = GPR_MAX (parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2);
- parser->r.hdrs = gpr_realloc (parser->r.hdrs, parser->hdr_capacity * sizeof (*parser->r.hdrs));
- }
+ while (cur != end && *cur != ':') {
+ cur++;
+ }
+ if (cur == end) {
+ gpr_log(GPR_ERROR, "Didn't find ':' in header string");
+ goto error;
+ }
+ GPR_ASSERT(cur >= beg);
+ hdr.key = buf2str(beg, (size_t)(cur - beg));
+ cur++; /* skip : */
+
+ while (cur != end && (*cur == ' ' || *cur == '\t')) {
+ cur++;
+ }
+ GPR_ASSERT(end - cur >= 2);
+ hdr.value = buf2str(cur, (size_t)(end - cur) - 2);
+
+ if (parser->r.hdr_count == parser->hdr_capacity) {
+ parser->hdr_capacity =
+ GPR_MAX(parser->hdr_capacity + 1, parser->hdr_capacity * 3 / 2);
+ parser->r.hdrs = gpr_realloc(
+ parser->r.hdrs, parser->hdr_capacity * sizeof(*parser->r.hdrs));
+ }
parser->r.hdrs[parser->r.hdr_count++] = hdr;
return 1;
error:
- gpr_free (hdr.key);
- gpr_free (hdr.value);
+ gpr_free(hdr.key);
+ gpr_free(hdr.value);
return 0;
}
-static int
-finish_line (grpc_httpcli_parser * parser)
-{
- switch (parser->state)
- {
+static int finish_line(grpc_httpcli_parser *parser) {
+ switch (parser->state) {
case GRPC_HTTPCLI_INITIAL_RESPONSE:
- if (!handle_response_line (parser))
- {
- return 0;
- }
+ if (!handle_response_line(parser)) {
+ return 0;
+ }
parser->state = GRPC_HTTPCLI_HEADERS;
break;
case GRPC_HTTPCLI_HEADERS:
- if (parser->cur_line_length == 2)
- {
- parser->state = GRPC_HTTPCLI_BODY;
- break;
- }
- if (!add_header (parser))
- {
- return 0;
- }
+ if (parser->cur_line_length == 2) {
+ parser->state = GRPC_HTTPCLI_BODY;
+ break;
+ }
+ if (!add_header(parser)) {
+ return 0;
+ }
break;
case GRPC_HTTPCLI_BODY:
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
- }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
+ }
parser->cur_line_length = 0;
return 1;
}
-static int
-addbyte (grpc_httpcli_parser * parser, gpr_uint8 byte)
-{
- switch (parser->state)
- {
+static int addbyte(grpc_httpcli_parser *parser, gpr_uint8 byte) {
+ switch (parser->state) {
case GRPC_HTTPCLI_INITIAL_RESPONSE:
case GRPC_HTTPCLI_HEADERS:
- if (parser->cur_line_length >= GRPC_HTTPCLI_MAX_HEADER_LENGTH)
- {
- gpr_log (GPR_ERROR, "HTTP client max line length (%d) exceeded", GRPC_HTTPCLI_MAX_HEADER_LENGTH);
- return 0;
- }
+ if (parser->cur_line_length >= GRPC_HTTPCLI_MAX_HEADER_LENGTH) {
+ gpr_log(GPR_ERROR, "HTTP client max line length (%d) exceeded",
+ GRPC_HTTPCLI_MAX_HEADER_LENGTH);
+ return 0;
+ }
parser->cur_line[parser->cur_line_length] = byte;
parser->cur_line_length++;
- if (parser->cur_line_length >= 2 && parser->cur_line[parser->cur_line_length - 2] == '\r' && parser->cur_line[parser->cur_line_length - 1] == '\n')
- {
- return finish_line (parser);
- }
- else
- {
- return 1;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ if (parser->cur_line_length >= 2 &&
+ parser->cur_line[parser->cur_line_length - 2] == '\r' &&
+ parser->cur_line[parser->cur_line_length - 1] == '\n') {
+ return finish_line(parser);
+ } else {
+ return 1;
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
case GRPC_HTTPCLI_BODY:
- if (parser->r.body_length == parser->body_capacity)
- {
- parser->body_capacity = GPR_MAX (8, parser->body_capacity * 3 / 2);
- parser->r.body = gpr_realloc ((void *) parser->r.body, parser->body_capacity);
- }
- parser->r.body[parser->r.body_length] = (char) byte;
+ if (parser->r.body_length == parser->body_capacity) {
+ parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2);
+ parser->r.body =
+ gpr_realloc((void *)parser->r.body, parser->body_capacity);
+ }
+ parser->r.body[parser->r.body_length] = (char)byte;
parser->r.body_length++;
return 1;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
return 0;
}
-void
-grpc_httpcli_parser_init (grpc_httpcli_parser * parser)
-{
- memset (parser, 0, sizeof (*parser));
+void grpc_httpcli_parser_init(grpc_httpcli_parser *parser) {
+ memset(parser, 0, sizeof(*parser));
parser->state = GRPC_HTTPCLI_INITIAL_RESPONSE;
parser->r.status = 500;
}
-void
-grpc_httpcli_parser_destroy (grpc_httpcli_parser * parser)
-{
+void grpc_httpcli_parser_destroy(grpc_httpcli_parser *parser) {
size_t i;
- gpr_free (parser->r.body);
- for (i = 0; i < parser->r.hdr_count; i++)
- {
- gpr_free (parser->r.hdrs[i].key);
- gpr_free (parser->r.hdrs[i].value);
- }
- gpr_free (parser->r.hdrs);
+ gpr_free(parser->r.body);
+ for (i = 0; i < parser->r.hdr_count; i++) {
+ gpr_free(parser->r.hdrs[i].key);
+ gpr_free(parser->r.hdrs[i].value);
+ }
+ gpr_free(parser->r.hdrs);
}
-int
-grpc_httpcli_parser_parse (grpc_httpcli_parser * parser, gpr_slice slice)
-{
+int grpc_httpcli_parser_parse(grpc_httpcli_parser *parser, gpr_slice slice) {
size_t i;
- for (i = 0; i < GPR_SLICE_LENGTH (slice); i++)
- {
- if (!addbyte (parser, GPR_SLICE_START_PTR (slice)[i]))
- {
- return 0;
- }
+ for (i = 0; i < GPR_SLICE_LENGTH(slice); i++) {
+ if (!addbyte(parser, GPR_SLICE_START_PTR(slice)[i])) {
+ return 0;
}
+ }
return 1;
}
-int
-grpc_httpcli_parser_eof (grpc_httpcli_parser * parser)
-{
+int grpc_httpcli_parser_eof(grpc_httpcli_parser *parser) {
return parser->state == GRPC_HTTPCLI_BODY;
}
diff --git a/src/core/httpcli/parser.h b/src/core/httpcli/parser.h
index 9f7e04824c..40b28f0ee8 100644
--- a/src/core/httpcli/parser.h
+++ b/src/core/httpcli/parser.h
@@ -38,15 +38,13 @@
#include <grpc/support/port_platform.h>
#include <grpc/support/slice.h>
-typedef enum
-{
+typedef enum {
GRPC_HTTPCLI_INITIAL_RESPONSE,
GRPC_HTTPCLI_HEADERS,
GRPC_HTTPCLI_BODY
} grpc_httpcli_parser_state;
-typedef struct
-{
+typedef struct {
grpc_httpcli_parser_state state;
grpc_httpcli_response r;
@@ -57,10 +55,10 @@ typedef struct
size_t cur_line_length;
} grpc_httpcli_parser;
-void grpc_httpcli_parser_init (grpc_httpcli_parser * parser);
-void grpc_httpcli_parser_destroy (grpc_httpcli_parser * parser);
+void grpc_httpcli_parser_init(grpc_httpcli_parser* parser);
+void grpc_httpcli_parser_destroy(grpc_httpcli_parser* parser);
-int grpc_httpcli_parser_parse (grpc_httpcli_parser * parser, gpr_slice slice);
-int grpc_httpcli_parser_eof (grpc_httpcli_parser * parser);
+int grpc_httpcli_parser_parse(grpc_httpcli_parser* parser, gpr_slice slice);
+int grpc_httpcli_parser_eof(grpc_httpcli_parser* parser);
#endif /* GRPC_INTERNAL_CORE_HTTPCLI_PARSER_H */
diff --git a/src/core/iomgr/alarm.c b/src/core/iomgr/alarm.c
index 31de551151..0ba5361606 100644
--- a/src/core/iomgr/alarm.c
+++ b/src/core/iomgr/alarm.c
@@ -48,8 +48,7 @@
#define MIN_QUEUE_WINDOW_DURATION 0.01
#define MAX_QUEUE_WINDOW_DURATION 1
-typedef struct
-{
+typedef struct {
gpr_mu mu;
grpc_time_averaged_stats stats;
/* All and only alarms with deadlines <= this will be in the heap. */
@@ -73,140 +72,127 @@ static shard_type g_shards[NUM_SHARDS];
/* Protected by g_mu */
static shard_type *g_shard_queue[NUM_SHARDS];
-static int run_some_expired_alarms (grpc_exec_ctx * exec_ctx, gpr_timespec now, gpr_timespec * next, int success);
+static int run_some_expired_alarms(grpc_exec_ctx *exec_ctx, gpr_timespec now,
+ gpr_timespec *next, int success);
-static gpr_timespec
-compute_min_deadline (shard_type * shard)
-{
- return grpc_alarm_heap_is_empty (&shard->heap) ? shard->queue_deadline_cap : grpc_alarm_heap_top (&shard->heap)->deadline;
+static gpr_timespec compute_min_deadline(shard_type *shard) {
+ return grpc_alarm_heap_is_empty(&shard->heap)
+ ? shard->queue_deadline_cap
+ : grpc_alarm_heap_top(&shard->heap)->deadline;
}
-void
-grpc_alarm_list_init (gpr_timespec now)
-{
+void grpc_alarm_list_init(gpr_timespec now) {
gpr_uint32 i;
- gpr_mu_init (&g_mu);
- gpr_mu_init (&g_checker_mu);
+ gpr_mu_init(&g_mu);
+ gpr_mu_init(&g_checker_mu);
g_clock_type = now.clock_type;
- for (i = 0; i < NUM_SHARDS; i++)
- {
- shard_type *shard = &g_shards[i];
- gpr_mu_init (&shard->mu);
- grpc_time_averaged_stats_init (&shard->stats, 1.0 / ADD_DEADLINE_SCALE, 0.1, 0.5);
- shard->queue_deadline_cap = now;
- shard->shard_queue_index = i;
- grpc_alarm_heap_init (&shard->heap);
- shard->list.next = shard->list.prev = &shard->list;
- shard->min_deadline = compute_min_deadline (shard);
- g_shard_queue[i] = shard;
- }
+ for (i = 0; i < NUM_SHARDS; i++) {
+ shard_type *shard = &g_shards[i];
+ gpr_mu_init(&shard->mu);
+ grpc_time_averaged_stats_init(&shard->stats, 1.0 / ADD_DEADLINE_SCALE, 0.1,
+ 0.5);
+ shard->queue_deadline_cap = now;
+ shard->shard_queue_index = i;
+ grpc_alarm_heap_init(&shard->heap);
+ shard->list.next = shard->list.prev = &shard->list;
+ shard->min_deadline = compute_min_deadline(shard);
+ g_shard_queue[i] = shard;
+ }
}
-void
-grpc_alarm_list_shutdown (grpc_exec_ctx *exec_ctx)
-{
+void grpc_alarm_list_shutdown(grpc_exec_ctx *exec_ctx) {
int i;
- run_some_expired_alarms (exec_ctx, gpr_inf_future (g_clock_type), NULL, 0);
- for (i = 0; i < NUM_SHARDS; i++)
- {
- shard_type *shard = &g_shards[i];
- gpr_mu_destroy (&shard->mu);
- grpc_alarm_heap_destroy (&shard->heap);
- }
- gpr_mu_destroy (&g_mu);
- gpr_mu_destroy (&g_checker_mu);
+ run_some_expired_alarms(exec_ctx, gpr_inf_future(g_clock_type), NULL, 0);
+ for (i = 0; i < NUM_SHARDS; i++) {
+ shard_type *shard = &g_shards[i];
+ gpr_mu_destroy(&shard->mu);
+ grpc_alarm_heap_destroy(&shard->heap);
+ }
+ gpr_mu_destroy(&g_mu);
+ gpr_mu_destroy(&g_checker_mu);
}
/* This is a cheap, but good enough, pointer hash for sharding the tasks: */
-static size_t
-shard_idx (const grpc_alarm * info)
-{
- size_t x = (size_t) info;
+static size_t shard_idx(const grpc_alarm *info) {
+ size_t x = (size_t)info;
return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) & (NUM_SHARDS - 1);
}
-static double
-ts_to_dbl (gpr_timespec ts)
-{
- return (double) ts.tv_sec + 1e-9 * ts.tv_nsec;
+static double ts_to_dbl(gpr_timespec ts) {
+ return (double)ts.tv_sec + 1e-9 * ts.tv_nsec;
}
-static gpr_timespec
-dbl_to_ts (double d)
-{
+static gpr_timespec dbl_to_ts(double d) {
gpr_timespec ts;
- ts.tv_sec = (time_t) d;
- ts.tv_nsec = (int) (1e9 * (d - (double) ts.tv_sec));
+ ts.tv_sec = (time_t)d;
+ ts.tv_nsec = (int)(1e9 * (d - (double)ts.tv_sec));
ts.clock_type = GPR_TIMESPAN;
return ts;
}
-static void
-list_join (grpc_alarm * head, grpc_alarm * alarm)
-{
+static void list_join(grpc_alarm *head, grpc_alarm *alarm) {
alarm->next = head;
alarm->prev = head->prev;
alarm->next->prev = alarm->prev->next = alarm;
}
-static void
-list_remove (grpc_alarm * alarm)
-{
+static void list_remove(grpc_alarm *alarm) {
alarm->next->prev = alarm->prev;
alarm->prev->next = alarm->next;
}
-static void
-swap_adjacent_shards_in_queue (gpr_uint32 first_shard_queue_index)
-{
+static void swap_adjacent_shards_in_queue(gpr_uint32 first_shard_queue_index) {
shard_type *temp;
temp = g_shard_queue[first_shard_queue_index];
- g_shard_queue[first_shard_queue_index] = g_shard_queue[first_shard_queue_index + 1];
+ g_shard_queue[first_shard_queue_index] =
+ g_shard_queue[first_shard_queue_index + 1];
g_shard_queue[first_shard_queue_index + 1] = temp;
- g_shard_queue[first_shard_queue_index]->shard_queue_index = first_shard_queue_index;
- g_shard_queue[first_shard_queue_index + 1]->shard_queue_index = first_shard_queue_index + 1;
+ g_shard_queue[first_shard_queue_index]->shard_queue_index =
+ first_shard_queue_index;
+ g_shard_queue[first_shard_queue_index + 1]->shard_queue_index =
+ first_shard_queue_index + 1;
}
-static void
-note_deadline_change (shard_type * shard)
-{
- while (shard->shard_queue_index > 0 && gpr_time_cmp (shard->min_deadline, g_shard_queue[shard->shard_queue_index - 1]->min_deadline) < 0)
- {
- swap_adjacent_shards_in_queue (shard->shard_queue_index - 1);
- }
- while (shard->shard_queue_index < NUM_SHARDS - 1 && gpr_time_cmp (shard->min_deadline, g_shard_queue[shard->shard_queue_index + 1]->min_deadline) > 0)
- {
- swap_adjacent_shards_in_queue (shard->shard_queue_index);
- }
+static void note_deadline_change(shard_type *shard) {
+ while (shard->shard_queue_index > 0 &&
+ gpr_time_cmp(
+ shard->min_deadline,
+ g_shard_queue[shard->shard_queue_index - 1]->min_deadline) < 0) {
+ swap_adjacent_shards_in_queue(shard->shard_queue_index - 1);
+ }
+ while (shard->shard_queue_index < NUM_SHARDS - 1 &&
+ gpr_time_cmp(
+ shard->min_deadline,
+ g_shard_queue[shard->shard_queue_index + 1]->min_deadline) > 0) {
+ swap_adjacent_shards_in_queue(shard->shard_queue_index);
+ }
}
-void
-grpc_alarm_init (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm, gpr_timespec deadline, grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg, gpr_timespec now)
-{
+void grpc_alarm_init(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm,
+ gpr_timespec deadline, grpc_iomgr_cb_func alarm_cb,
+ void *alarm_cb_arg, gpr_timespec now) {
int is_first_alarm = 0;
- shard_type *shard = &g_shards[shard_idx (alarm)];
- GPR_ASSERT (deadline.clock_type == g_clock_type);
- GPR_ASSERT (now.clock_type == g_clock_type);
- grpc_closure_init (&alarm->closure, alarm_cb, alarm_cb_arg);
+ shard_type *shard = &g_shards[shard_idx(alarm)];
+ GPR_ASSERT(deadline.clock_type == g_clock_type);
+ GPR_ASSERT(now.clock_type == g_clock_type);
+ grpc_closure_init(&alarm->closure, alarm_cb, alarm_cb_arg);
alarm->deadline = deadline;
alarm->triggered = 0;
/* TODO(ctiller): check deadline expired */
- gpr_mu_lock (&shard->mu);
- grpc_time_averaged_stats_add_sample (&shard->stats, ts_to_dbl (gpr_time_sub (deadline, now)));
- if (gpr_time_cmp (deadline, shard->queue_deadline_cap) < 0)
- {
- is_first_alarm = grpc_alarm_heap_add (&shard->heap, alarm);
- }
- else
- {
- alarm->heap_index = INVALID_HEAP_INDEX;
- list_join (&shard->list, alarm);
- }
- gpr_mu_unlock (&shard->mu);
+ gpr_mu_lock(&shard->mu);
+ grpc_time_averaged_stats_add_sample(&shard->stats,
+ ts_to_dbl(gpr_time_sub(deadline, now)));
+ if (gpr_time_cmp(deadline, shard->queue_deadline_cap) < 0) {
+ is_first_alarm = grpc_alarm_heap_add(&shard->heap, alarm);
+ } else {
+ alarm->heap_index = INVALID_HEAP_INDEX;
+ list_join(&shard->list, alarm);
+ }
+ gpr_mu_unlock(&shard->mu);
/* Deadline may have decreased, we need to adjust the master queue. Note
that there is a potential racy unlocked region here. There could be a
@@ -219,42 +205,34 @@ grpc_alarm_init (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm, gpr_timespec dead
trigger the new alarm because the min_deadline hadn't yet been reduced.
In that case, the alarm will simply have to wait for the next
grpc_alarm_check. */
- if (is_first_alarm)
- {
- gpr_mu_lock (&g_mu);
- if (gpr_time_cmp (deadline, shard->min_deadline) < 0)
- {
- gpr_timespec old_min_deadline = g_shard_queue[0]->min_deadline;
- shard->min_deadline = deadline;
- note_deadline_change (shard);
- if (shard->shard_queue_index == 0 && gpr_time_cmp (deadline, old_min_deadline) < 0)
- {
- grpc_kick_poller ();
- }
- }
- gpr_mu_unlock (&g_mu);
+ if (is_first_alarm) {
+ gpr_mu_lock(&g_mu);
+ if (gpr_time_cmp(deadline, shard->min_deadline) < 0) {
+ gpr_timespec old_min_deadline = g_shard_queue[0]->min_deadline;
+ shard->min_deadline = deadline;
+ note_deadline_change(shard);
+ if (shard->shard_queue_index == 0 &&
+ gpr_time_cmp(deadline, old_min_deadline) < 0) {
+ grpc_kick_poller();
+ }
}
+ gpr_mu_unlock(&g_mu);
+ }
}
-void
-grpc_alarm_cancel (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm)
-{
- shard_type *shard = &g_shards[shard_idx (alarm)];
- gpr_mu_lock (&shard->mu);
- if (!alarm->triggered)
- {
- grpc_exec_ctx_enqueue (exec_ctx, &alarm->closure, 0);
- alarm->triggered = 1;
- if (alarm->heap_index == INVALID_HEAP_INDEX)
- {
- list_remove (alarm);
- }
- else
- {
- grpc_alarm_heap_remove (&shard->heap, alarm);
- }
+void grpc_alarm_cancel(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm) {
+ shard_type *shard = &g_shards[shard_idx(alarm)];
+ gpr_mu_lock(&shard->mu);
+ if (!alarm->triggered) {
+ grpc_exec_ctx_enqueue(exec_ctx, &alarm->closure, 0);
+ alarm->triggered = 1;
+ if (alarm->heap_index == INVALID_HEAP_INDEX) {
+ list_remove(alarm);
+ } else {
+ grpc_alarm_heap_remove(&shard->heap, alarm);
}
- gpr_mu_unlock (&shard->mu);
+ }
+ gpr_mu_unlock(&shard->mu);
}
/* This is called when the queue is empty and "now" has reached the
@@ -262,126 +240,114 @@ grpc_alarm_cancel (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm)
for alarms that fall at or under it. Returns true if the queue is no
longer empty.
REQUIRES: shard->mu locked */
-static int
-refill_queue (shard_type * shard, gpr_timespec now)
-{
+static int refill_queue(shard_type *shard, gpr_timespec now) {
/* Compute the new queue window width and bound by the limits: */
- double computed_deadline_delta = grpc_time_averaged_stats_update_average (&shard->stats) * ADD_DEADLINE_SCALE;
- double deadline_delta = GPR_CLAMP (computed_deadline_delta, MIN_QUEUE_WINDOW_DURATION,
- MAX_QUEUE_WINDOW_DURATION);
+ double computed_deadline_delta =
+ grpc_time_averaged_stats_update_average(&shard->stats) *
+ ADD_DEADLINE_SCALE;
+ double deadline_delta =
+ GPR_CLAMP(computed_deadline_delta, MIN_QUEUE_WINDOW_DURATION,
+ MAX_QUEUE_WINDOW_DURATION);
grpc_alarm *alarm, *next;
/* Compute the new cap and put all alarms under it into the queue: */
- shard->queue_deadline_cap = gpr_time_add (gpr_time_max (now, shard->queue_deadline_cap), dbl_to_ts (deadline_delta));
- for (alarm = shard->list.next; alarm != &shard->list; alarm = next)
- {
- next = alarm->next;
-
- if (gpr_time_cmp (alarm->deadline, shard->queue_deadline_cap) < 0)
- {
- list_remove (alarm);
- grpc_alarm_heap_add (&shard->heap, alarm);
- }
+ shard->queue_deadline_cap = gpr_time_add(
+ gpr_time_max(now, shard->queue_deadline_cap), dbl_to_ts(deadline_delta));
+ for (alarm = shard->list.next; alarm != &shard->list; alarm = next) {
+ next = alarm->next;
+
+ if (gpr_time_cmp(alarm->deadline, shard->queue_deadline_cap) < 0) {
+ list_remove(alarm);
+ grpc_alarm_heap_add(&shard->heap, alarm);
}
- return !grpc_alarm_heap_is_empty (&shard->heap);
+ }
+ return !grpc_alarm_heap_is_empty(&shard->heap);
}
/* This pops the next non-cancelled alarm with deadline <= now from the queue,
or returns NULL if there isn't one.
REQUIRES: shard->mu locked */
-static grpc_alarm *
-pop_one (shard_type * shard, gpr_timespec now)
-{
+static grpc_alarm *pop_one(shard_type *shard, gpr_timespec now) {
grpc_alarm *alarm;
- for (;;)
- {
- if (grpc_alarm_heap_is_empty (&shard->heap))
- {
- if (gpr_time_cmp (now, shard->queue_deadline_cap) < 0)
- return NULL;
- if (!refill_queue (shard, now))
- return NULL;
- }
- alarm = grpc_alarm_heap_top (&shard->heap);
- if (gpr_time_cmp (alarm->deadline, now) > 0)
- return NULL;
- alarm->triggered = 1;
- grpc_alarm_heap_pop (&shard->heap);
- return alarm;
+ for (;;) {
+ if (grpc_alarm_heap_is_empty(&shard->heap)) {
+ if (gpr_time_cmp(now, shard->queue_deadline_cap) < 0) return NULL;
+ if (!refill_queue(shard, now)) return NULL;
}
+ alarm = grpc_alarm_heap_top(&shard->heap);
+ if (gpr_time_cmp(alarm->deadline, now) > 0) return NULL;
+ alarm->triggered = 1;
+ grpc_alarm_heap_pop(&shard->heap);
+ return alarm;
+ }
}
/* REQUIRES: shard->mu unlocked */
-static size_t
-pop_alarms (grpc_exec_ctx * exec_ctx, shard_type * shard, gpr_timespec now, gpr_timespec * new_min_deadline, int success)
-{
+static size_t pop_alarms(grpc_exec_ctx *exec_ctx, shard_type *shard,
+ gpr_timespec now, gpr_timespec *new_min_deadline,
+ int success) {
size_t n = 0;
grpc_alarm *alarm;
- gpr_mu_lock (&shard->mu);
- while ((alarm = pop_one (shard, now)))
- {
- grpc_exec_ctx_enqueue (exec_ctx, &alarm->closure, success);
- n++;
- }
- *new_min_deadline = compute_min_deadline (shard);
- gpr_mu_unlock (&shard->mu);
+ gpr_mu_lock(&shard->mu);
+ while ((alarm = pop_one(shard, now))) {
+ grpc_exec_ctx_enqueue(exec_ctx, &alarm->closure, success);
+ n++;
+ }
+ *new_min_deadline = compute_min_deadline(shard);
+ gpr_mu_unlock(&shard->mu);
return n;
}
-static int
-run_some_expired_alarms (grpc_exec_ctx * exec_ctx, gpr_timespec now, gpr_timespec * next, int success)
-{
+static int run_some_expired_alarms(grpc_exec_ctx *exec_ctx, gpr_timespec now,
+ gpr_timespec *next, int success) {
size_t n = 0;
/* TODO(ctiller): verify that there are any alarms (atomically) here */
- if (gpr_mu_trylock (&g_checker_mu))
- {
- gpr_mu_lock (&g_mu);
-
- while (gpr_time_cmp (g_shard_queue[0]->min_deadline, now) < 0)
- {
- gpr_timespec new_min_deadline;
-
- /* For efficiency, we pop as many available alarms as we can from the
- shard. This may violate perfect alarm deadline ordering, but that
- shouldn't be a big deal because we don't make ordering guarantees. */
- n += pop_alarms (exec_ctx, g_shard_queue[0], now, &new_min_deadline, success);
-
- /* An grpc_alarm_init() on the shard could intervene here, adding a new
- alarm that is earlier than new_min_deadline. However,
- grpc_alarm_init() will block on the master_lock before it can call
- set_min_deadline, so this one will complete first and then the AddAlarm
- will reduce the min_deadline (perhaps unnecessarily). */
- g_shard_queue[0]->min_deadline = new_min_deadline;
- note_deadline_change (g_shard_queue[0]);
- }
-
- if (next)
- {
- *next = gpr_time_min (*next, g_shard_queue[0]->min_deadline);
- }
-
- gpr_mu_unlock (&g_mu);
- gpr_mu_unlock (&g_checker_mu);
+ if (gpr_mu_trylock(&g_checker_mu)) {
+ gpr_mu_lock(&g_mu);
+
+ while (gpr_time_cmp(g_shard_queue[0]->min_deadline, now) < 0) {
+ gpr_timespec new_min_deadline;
+
+ /* For efficiency, we pop as many available alarms as we can from the
+ shard. This may violate perfect alarm deadline ordering, but that
+ shouldn't be a big deal because we don't make ordering guarantees. */
+ n += pop_alarms(exec_ctx, g_shard_queue[0], now, &new_min_deadline,
+ success);
+
+ /* An grpc_alarm_init() on the shard could intervene here, adding a new
+ alarm that is earlier than new_min_deadline. However,
+ grpc_alarm_init() will block on the master_lock before it can call
+ set_min_deadline, so this one will complete first and then the AddAlarm
+ will reduce the min_deadline (perhaps unnecessarily). */
+ g_shard_queue[0]->min_deadline = new_min_deadline;
+ note_deadline_change(g_shard_queue[0]);
}
- return (int) n;
+ if (next) {
+ *next = gpr_time_min(*next, g_shard_queue[0]->min_deadline);
+ }
+
+ gpr_mu_unlock(&g_mu);
+ gpr_mu_unlock(&g_checker_mu);
+ }
+
+ return (int)n;
}
-int
-grpc_alarm_check (grpc_exec_ctx * exec_ctx, gpr_timespec now, gpr_timespec * next)
-{
- GPR_ASSERT (now.clock_type == g_clock_type);
- return run_some_expired_alarms (exec_ctx, now, next, gpr_time_cmp (now, gpr_inf_future (now.clock_type)) != 0);
+int grpc_alarm_check(grpc_exec_ctx *exec_ctx, gpr_timespec now,
+ gpr_timespec *next) {
+ GPR_ASSERT(now.clock_type == g_clock_type);
+ return run_some_expired_alarms(
+ exec_ctx, now, next,
+ gpr_time_cmp(now, gpr_inf_future(now.clock_type)) != 0);
}
-gpr_timespec
-grpc_alarm_list_next_timeout (void)
-{
+gpr_timespec grpc_alarm_list_next_timeout(void) {
gpr_timespec out;
- gpr_mu_lock (&g_mu);
+ gpr_mu_lock(&g_mu);
out = g_shard_queue[0]->min_deadline;
- gpr_mu_unlock (&g_mu);
+ gpr_mu_unlock(&g_mu);
return out;
}
diff --git a/src/core/iomgr/alarm.h b/src/core/iomgr/alarm.h
index dc4ce29b46..94f9bc1355 100644
--- a/src/core/iomgr/alarm.h
+++ b/src/core/iomgr/alarm.h
@@ -39,10 +39,9 @@
#include <grpc/support/port_platform.h>
#include <grpc/support/time.h>
-typedef struct grpc_alarm
-{
+typedef struct grpc_alarm {
gpr_timespec deadline;
- gpr_uint32 heap_index; /* INVALID_HEAP_INDEX if not in heap */
+ gpr_uint32 heap_index; /* INVALID_HEAP_INDEX if not in heap */
int triggered;
struct grpc_alarm *next;
struct grpc_alarm *prev;
@@ -55,7 +54,9 @@ typedef struct grpc_alarm
and application code should check the status to determine how it was
invoked. The application callback is also responsible for maintaining
information about when to free up any user-level state. */
-void grpc_alarm_init (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm, gpr_timespec deadline, grpc_iomgr_cb_func alarm_cb, void *alarm_cb_arg, gpr_timespec now);
+void grpc_alarm_init(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm,
+ gpr_timespec deadline, grpc_iomgr_cb_func alarm_cb,
+ void *alarm_cb_arg, gpr_timespec now);
/* Note that there is no alarm destroy function. This is because the
alarm is a one-time occurrence with a guarantee that the callback will
@@ -83,6 +84,6 @@ void grpc_alarm_init (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm, gpr_timespec
matches this aim.
Requires: cancel() must happen after add() on a given alarm */
-void grpc_alarm_cancel (grpc_exec_ctx * exec_ctx, grpc_alarm * alarm);
+void grpc_alarm_cancel(grpc_exec_ctx *exec_ctx, grpc_alarm *alarm);
#endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_H */
diff --git a/src/core/iomgr/alarm_heap.c b/src/core/iomgr/alarm_heap.c
index 12e1631476..769142e425 100644
--- a/src/core/iomgr/alarm_heap.c
+++ b/src/core/iomgr/alarm_heap.c
@@ -43,18 +43,14 @@
position. This functor is called each time immediately after modifying a
value in the underlying container, with the offset of the modified element as
its argument. */
-static void
-adjust_upwards (grpc_alarm ** first, gpr_uint32 i, grpc_alarm * t)
-{
- while (i > 0)
- {
- gpr_uint32 parent = (gpr_uint32) (((int) i - 1) / 2);
- if (gpr_time_cmp (first[parent]->deadline, t->deadline) >= 0)
- break;
- first[i] = first[parent];
- first[i]->heap_index = i;
- i = parent;
- }
+static void adjust_upwards(grpc_alarm **first, gpr_uint32 i, grpc_alarm *t) {
+ while (i > 0) {
+ gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2);
+ if (gpr_time_cmp(first[parent]->deadline, t->deadline) >= 0) break;
+ first[i] = first[parent];
+ first[i]->heap_index = i;
+ i = parent;
+ }
first[i] = t;
t->heap_index = i;
}
@@ -62,24 +58,24 @@ adjust_upwards (grpc_alarm ** first, gpr_uint32 i, grpc_alarm * t)
/* Adjusts a heap so as to move a hole at position i farther away from the root,
until a suitable position is found for element t. Then, copies t into that
position. */
-static void
-adjust_downwards (grpc_alarm ** first, gpr_uint32 i, gpr_uint32 length, grpc_alarm * t)
-{
- for (;;)
- {
- gpr_uint32 left_child = 1u + 2u * i;
- gpr_uint32 right_child;
- gpr_uint32 next_i;
- if (left_child >= length)
- break;
- right_child = left_child + 1;
- next_i = right_child < length && gpr_time_cmp (first[left_child]->deadline, first[right_child]->deadline) < 0 ? right_child : left_child;
- if (gpr_time_cmp (t->deadline, first[next_i]->deadline) >= 0)
- break;
- first[i] = first[next_i];
- first[i]->heap_index = i;
- i = next_i;
- }
+static void adjust_downwards(grpc_alarm **first, gpr_uint32 i,
+ gpr_uint32 length, grpc_alarm *t) {
+ for (;;) {
+ gpr_uint32 left_child = 1u + 2u * i;
+ gpr_uint32 right_child;
+ gpr_uint32 next_i;
+ if (left_child >= length) break;
+ right_child = left_child + 1;
+ next_i = right_child < length &&
+ gpr_time_cmp(first[left_child]->deadline,
+ first[right_child]->deadline) < 0
+ ? right_child
+ : left_child;
+ if (gpr_time_cmp(t->deadline, first[next_i]->deadline) >= 0) break;
+ first[i] = first[next_i];
+ first[i]->heap_index = i;
+ i = next_i;
+ }
first[i] = t;
t->heap_index = i;
}
@@ -87,88 +83,66 @@ adjust_downwards (grpc_alarm ** first, gpr_uint32 i, gpr_uint32 length, grpc_ala
#define SHRINK_MIN_ELEMS 8
#define SHRINK_FULLNESS_FACTOR 2
-static void
-maybe_shrink (grpc_alarm_heap * heap)
-{
- if (heap->alarm_count >= 8 && heap->alarm_count <= heap->alarm_capacity / SHRINK_FULLNESS_FACTOR / 2)
- {
- heap->alarm_capacity = heap->alarm_count * SHRINK_FULLNESS_FACTOR;
- heap->alarms = gpr_realloc (heap->alarms, heap->alarm_capacity * sizeof (grpc_alarm *));
- }
+static void maybe_shrink(grpc_alarm_heap *heap) {
+ if (heap->alarm_count >= 8 &&
+ heap->alarm_count <= heap->alarm_capacity / SHRINK_FULLNESS_FACTOR / 2) {
+ heap->alarm_capacity = heap->alarm_count * SHRINK_FULLNESS_FACTOR;
+ heap->alarms =
+ gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *));
+ }
}
-static void
-note_changed_priority (grpc_alarm_heap * heap, grpc_alarm * alarm)
-{
+static void note_changed_priority(grpc_alarm_heap *heap, grpc_alarm *alarm) {
gpr_uint32 i = alarm->heap_index;
- gpr_uint32 parent = (gpr_uint32) (((int) i - 1) / 2);
- if (gpr_time_cmp (heap->alarms[parent]->deadline, alarm->deadline) < 0)
- {
- adjust_upwards (heap->alarms, i, alarm);
- }
- else
- {
- adjust_downwards (heap->alarms, i, heap->alarm_count, alarm);
- }
+ gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2);
+ if (gpr_time_cmp(heap->alarms[parent]->deadline, alarm->deadline) < 0) {
+ adjust_upwards(heap->alarms, i, alarm);
+ } else {
+ adjust_downwards(heap->alarms, i, heap->alarm_count, alarm);
+ }
}
-void
-grpc_alarm_heap_init (grpc_alarm_heap * heap)
-{
- memset (heap, 0, sizeof (*heap));
+void grpc_alarm_heap_init(grpc_alarm_heap *heap) {
+ memset(heap, 0, sizeof(*heap));
}
-void
-grpc_alarm_heap_destroy (grpc_alarm_heap * heap)
-{
- gpr_free (heap->alarms);
-}
+void grpc_alarm_heap_destroy(grpc_alarm_heap *heap) { gpr_free(heap->alarms); }
-int
-grpc_alarm_heap_add (grpc_alarm_heap * heap, grpc_alarm * alarm)
-{
- if (heap->alarm_count == heap->alarm_capacity)
- {
- heap->alarm_capacity = GPR_MAX (heap->alarm_capacity + 1, heap->alarm_capacity * 3 / 2);
- heap->alarms = gpr_realloc (heap->alarms, heap->alarm_capacity * sizeof (grpc_alarm *));
- }
+int grpc_alarm_heap_add(grpc_alarm_heap *heap, grpc_alarm *alarm) {
+ if (heap->alarm_count == heap->alarm_capacity) {
+ heap->alarm_capacity =
+ GPR_MAX(heap->alarm_capacity + 1, heap->alarm_capacity * 3 / 2);
+ heap->alarms =
+ gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *));
+ }
alarm->heap_index = heap->alarm_count;
- adjust_upwards (heap->alarms, heap->alarm_count, alarm);
+ adjust_upwards(heap->alarms, heap->alarm_count, alarm);
heap->alarm_count++;
return alarm->heap_index == 0;
}
-void
-grpc_alarm_heap_remove (grpc_alarm_heap * heap, grpc_alarm * alarm)
-{
+void grpc_alarm_heap_remove(grpc_alarm_heap *heap, grpc_alarm *alarm) {
gpr_uint32 i = alarm->heap_index;
- if (i == heap->alarm_count - 1)
- {
- heap->alarm_count--;
- maybe_shrink (heap);
- return;
- }
+ if (i == heap->alarm_count - 1) {
+ heap->alarm_count--;
+ maybe_shrink(heap);
+ return;
+ }
heap->alarms[i] = heap->alarms[heap->alarm_count - 1];
heap->alarms[i]->heap_index = i;
heap->alarm_count--;
- maybe_shrink (heap);
- note_changed_priority (heap, heap->alarms[i]);
+ maybe_shrink(heap);
+ note_changed_priority(heap, heap->alarms[i]);
}
-int
-grpc_alarm_heap_is_empty (grpc_alarm_heap * heap)
-{
+int grpc_alarm_heap_is_empty(grpc_alarm_heap *heap) {
return heap->alarm_count == 0;
}
-grpc_alarm *
-grpc_alarm_heap_top (grpc_alarm_heap * heap)
-{
+grpc_alarm *grpc_alarm_heap_top(grpc_alarm_heap *heap) {
return heap->alarms[0];
}
-void
-grpc_alarm_heap_pop (grpc_alarm_heap * heap)
-{
- grpc_alarm_heap_remove (heap, grpc_alarm_heap_top (heap));
+void grpc_alarm_heap_pop(grpc_alarm_heap *heap) {
+ grpc_alarm_heap_remove(heap, grpc_alarm_heap_top(heap));
}
diff --git a/src/core/iomgr/alarm_heap.h b/src/core/iomgr/alarm_heap.h
index 1970897356..91d6ee3ca2 100644
--- a/src/core/iomgr/alarm_heap.h
+++ b/src/core/iomgr/alarm_heap.h
@@ -36,23 +36,22 @@
#include "src/core/iomgr/alarm.h"
-typedef struct
-{
+typedef struct {
grpc_alarm **alarms;
gpr_uint32 alarm_count;
gpr_uint32 alarm_capacity;
} grpc_alarm_heap;
/* return 1 if the new alarm is the first alarm in the heap */
-int grpc_alarm_heap_add (grpc_alarm_heap * heap, grpc_alarm * alarm);
+int grpc_alarm_heap_add(grpc_alarm_heap *heap, grpc_alarm *alarm);
-void grpc_alarm_heap_init (grpc_alarm_heap * heap);
-void grpc_alarm_heap_destroy (grpc_alarm_heap * heap);
+void grpc_alarm_heap_init(grpc_alarm_heap *heap);
+void grpc_alarm_heap_destroy(grpc_alarm_heap *heap);
-void grpc_alarm_heap_remove (grpc_alarm_heap * heap, grpc_alarm * alarm);
-grpc_alarm *grpc_alarm_heap_top (grpc_alarm_heap * heap);
-void grpc_alarm_heap_pop (grpc_alarm_heap * heap);
+void grpc_alarm_heap_remove(grpc_alarm_heap *heap, grpc_alarm *alarm);
+grpc_alarm *grpc_alarm_heap_top(grpc_alarm_heap *heap);
+void grpc_alarm_heap_pop(grpc_alarm_heap *heap);
-int grpc_alarm_heap_is_empty (grpc_alarm_heap * heap);
+int grpc_alarm_heap_is_empty(grpc_alarm_heap *heap);
#endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_HEAP_H */
diff --git a/src/core/iomgr/alarm_internal.h b/src/core/iomgr/alarm_internal.h
index 398124d83b..31d840e6f9 100644
--- a/src/core/iomgr/alarm_internal.h
+++ b/src/core/iomgr/alarm_internal.h
@@ -49,14 +49,15 @@
with high probability at least one thread in the system will see an update
at any time slice. */
-int grpc_alarm_check (grpc_exec_ctx * exec_ctx, gpr_timespec now, gpr_timespec * next);
-void grpc_alarm_list_init (gpr_timespec now);
-void grpc_alarm_list_shutdown (grpc_exec_ctx * exec_ctx);
+int grpc_alarm_check(grpc_exec_ctx* exec_ctx, gpr_timespec now,
+ gpr_timespec* next);
+void grpc_alarm_list_init(gpr_timespec now);
+void grpc_alarm_list_shutdown(grpc_exec_ctx* exec_ctx);
-gpr_timespec grpc_alarm_list_next_timeout (void);
+gpr_timespec grpc_alarm_list_next_timeout(void);
/* the following must be implemented by each iomgr implementation */
-void grpc_kick_poller (void);
+void grpc_kick_poller(void);
#endif /* GRPC_INTERNAL_CORE_IOMGR_ALARM_INTERNAL_H */
diff --git a/src/core/iomgr/closure.c b/src/core/iomgr/closure.c
index 4c1a1233d6..3265425789 100644
--- a/src/core/iomgr/closure.c
+++ b/src/core/iomgr/closure.c
@@ -33,53 +33,39 @@
#include "src/core/iomgr/closure.h"
-void
-grpc_closure_init (grpc_closure * closure, grpc_iomgr_cb_func cb, void *cb_arg)
-{
+void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
+ void *cb_arg) {
closure->cb = cb;
closure->cb_arg = cb_arg;
closure->next = NULL;
}
-void
-grpc_closure_list_add (grpc_closure_list * closure_list, grpc_closure * closure, int success)
-{
- if (closure == NULL)
- return;
+void grpc_closure_list_add(grpc_closure_list *closure_list,
+ grpc_closure *closure, int success) {
+ if (closure == NULL) return;
closure->next = NULL;
closure->success = success;
- if (closure_list->head == NULL)
- {
- closure_list->head = closure;
- }
- else
- {
- closure_list->tail->next = closure;
- }
+ if (closure_list->head == NULL) {
+ closure_list->head = closure;
+ } else {
+ closure_list->tail->next = closure;
+ }
closure_list->tail = closure;
}
-int
-grpc_closure_list_empty (grpc_closure_list closure_list)
-{
+int grpc_closure_list_empty(grpc_closure_list closure_list) {
return closure_list.head == NULL;
}
-void
-grpc_closure_list_move (grpc_closure_list * src, grpc_closure_list * dst)
-{
- if (src->head == NULL)
- {
- return;
- }
- if (dst->head == NULL)
- {
- *dst = *src;
- }
- else
- {
- dst->tail->next = src->head;
- dst->tail = src->tail;
- }
+void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst) {
+ if (src->head == NULL) {
+ return;
+ }
+ if (dst->head == NULL) {
+ *dst = *src;
+ } else {
+ dst->tail->next = src->head;
+ dst->tail = src->tail;
+ }
src->head = src->tail = NULL;
}
diff --git a/src/core/iomgr/closure.h b/src/core/iomgr/closure.h
index e76e157f6a..982ffa4e1b 100644
--- a/src/core/iomgr/closure.h
+++ b/src/core/iomgr/closure.h
@@ -43,8 +43,7 @@ typedef struct grpc_closure grpc_closure;
struct grpc_exec_ctx;
typedef struct grpc_exec_ctx grpc_exec_ctx;
-typedef struct grpc_closure_list
-{
+typedef struct grpc_closure_list {
grpc_closure *head;
grpc_closure *tail;
} grpc_closure_list;
@@ -54,11 +53,11 @@ typedef struct grpc_closure_list
* \param arg Arbitrary input.
* \param success An indication on the state of the iomgr. On false, cleanup
* actions should be taken (eg, shutdown). */
-typedef void (*grpc_iomgr_cb_func) (grpc_exec_ctx * exec_ctx, void *arg, int success);
+typedef void (*grpc_iomgr_cb_func)(grpc_exec_ctx *exec_ctx, void *arg,
+ int success);
/** A closure over a grpc_iomgr_cb_func. */
-struct grpc_closure
-{
+struct grpc_closure {
/** Bound callback. */
grpc_iomgr_cb_func cb;
@@ -75,13 +74,15 @@ struct grpc_closure
};
/** Initializes \a closure with \a cb and \a cb_arg. */
-void grpc_closure_init (grpc_closure * closure, grpc_iomgr_cb_func cb, void *cb_arg);
+void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
+ void *cb_arg);
#define GRPC_CLOSURE_LIST_INIT \
{ NULL, NULL }
-void grpc_closure_list_add (grpc_closure_list * list, grpc_closure * closure, int success);
-void grpc_closure_list_move (grpc_closure_list * src, grpc_closure_list * dst);
-int grpc_closure_list_empty (grpc_closure_list list);
+void grpc_closure_list_add(grpc_closure_list *list, grpc_closure *closure,
+ int success);
+void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst);
+int grpc_closure_list_empty(grpc_closure_list list);
#endif /* GRPC_INTERNAL_CORE_IOMGR_CLOSURE_H */
diff --git a/src/core/iomgr/endpoint.c b/src/core/iomgr/endpoint.c
index d0f272a00b..bd64707669 100644
--- a/src/core/iomgr/endpoint.c
+++ b/src/core/iomgr/endpoint.c
@@ -33,44 +33,35 @@
#include "src/core/iomgr/endpoint.h"
-void
-grpc_endpoint_read (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb)
-{
- ep->vtable->read (exec_ctx, ep, slices, cb);
+void grpc_endpoint_read(grpc_exec_ctx* exec_ctx, grpc_endpoint* ep,
+ gpr_slice_buffer* slices, grpc_closure* cb) {
+ ep->vtable->read(exec_ctx, ep, slices, cb);
}
-void
-grpc_endpoint_write (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb)
-{
- ep->vtable->write (exec_ctx, ep, slices, cb);
+void grpc_endpoint_write(grpc_exec_ctx* exec_ctx, grpc_endpoint* ep,
+ gpr_slice_buffer* slices, grpc_closure* cb) {
+ ep->vtable->write(exec_ctx, ep, slices, cb);
}
-void
-grpc_endpoint_add_to_pollset (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset * pollset)
-{
- ep->vtable->add_to_pollset (exec_ctx, ep, pollset);
+void grpc_endpoint_add_to_pollset(grpc_exec_ctx* exec_ctx, grpc_endpoint* ep,
+ grpc_pollset* pollset) {
+ ep->vtable->add_to_pollset(exec_ctx, ep, pollset);
}
-void
-grpc_endpoint_add_to_pollset_set (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset_set * pollset_set)
-{
- ep->vtable->add_to_pollset_set (exec_ctx, ep, pollset_set);
+void grpc_endpoint_add_to_pollset_set(grpc_exec_ctx* exec_ctx,
+ grpc_endpoint* ep,
+ grpc_pollset_set* pollset_set) {
+ ep->vtable->add_to_pollset_set(exec_ctx, ep, pollset_set);
}
-void
-grpc_endpoint_shutdown (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep)
-{
- ep->vtable->shutdown (exec_ctx, ep);
+void grpc_endpoint_shutdown(grpc_exec_ctx* exec_ctx, grpc_endpoint* ep) {
+ ep->vtable->shutdown(exec_ctx, ep);
}
-void
-grpc_endpoint_destroy (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep)
-{
- ep->vtable->destroy (exec_ctx, ep);
+void grpc_endpoint_destroy(grpc_exec_ctx* exec_ctx, grpc_endpoint* ep) {
+ ep->vtable->destroy(exec_ctx, ep);
}
-char *
-grpc_endpoint_get_peer (grpc_endpoint * ep)
-{
- return ep->vtable->get_peer (ep);
+char* grpc_endpoint_get_peer(grpc_endpoint* ep) {
+ return ep->vtable->get_peer(ep);
}
diff --git a/src/core/iomgr/endpoint.h b/src/core/iomgr/endpoint.h
index 1a88b852e6..cbdc947abb 100644
--- a/src/core/iomgr/endpoint.h
+++ b/src/core/iomgr/endpoint.h
@@ -46,24 +46,28 @@
typedef struct grpc_endpoint grpc_endpoint;
typedef struct grpc_endpoint_vtable grpc_endpoint_vtable;
-struct grpc_endpoint_vtable
-{
- void (*read) (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb);
- void (*write) (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb);
- void (*add_to_pollset) (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset * pollset);
- void (*add_to_pollset_set) (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset_set * pollset);
- void (*shutdown) (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep);
- void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep);
- char *(*get_peer) (grpc_endpoint * ep);
+struct grpc_endpoint_vtable {
+ void (*read)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb);
+ void (*write)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb);
+ void (*add_to_pollset)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset *pollset);
+ void (*add_to_pollset_set)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset_set *pollset);
+ void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
+ char *(*get_peer)(grpc_endpoint *ep);
};
/* When data is available on the connection, calls the callback with slices.
Callback success indicates that the endpoint can accept more reads, failure
indicates the endpoint is closed.
Valid slices may be placed into \a slices even on callback success == 0. */
-void grpc_endpoint_read (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb);
+void grpc_endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb);
-char *grpc_endpoint_get_peer (grpc_endpoint * ep);
+char *grpc_endpoint_get_peer(grpc_endpoint *ep);
/* Write slices out to the socket.
@@ -75,20 +79,23 @@ char *grpc_endpoint_get_peer (grpc_endpoint * ep);
No guarantee is made to the content of slices after a write EXCEPT that
it is a valid slice buffer.
*/
-void grpc_endpoint_write (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb);
+void grpc_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb);
/* Causes any pending read/write callbacks to run immediately with
success==0 */
-void grpc_endpoint_shutdown (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep);
-void grpc_endpoint_destroy (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep);
+void grpc_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
+void grpc_endpoint_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
/* Add an endpoint to a pollset, so that when the pollset is polled, events from
this endpoint are considered */
-void grpc_endpoint_add_to_pollset (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset * pollset);
-void grpc_endpoint_add_to_pollset_set (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset_set * pollset_set);
+void grpc_endpoint_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset *pollset);
+void grpc_endpoint_add_to_pollset_set(grpc_exec_ctx *exec_ctx,
+ grpc_endpoint *ep,
+ grpc_pollset_set *pollset_set);
-struct grpc_endpoint
-{
+struct grpc_endpoint {
const grpc_endpoint_vtable *vtable;
};
diff --git a/src/core/iomgr/endpoint_pair.h b/src/core/iomgr/endpoint_pair.h
index 31816c4916..095ec5fcc9 100644
--- a/src/core/iomgr/endpoint_pair.h
+++ b/src/core/iomgr/endpoint_pair.h
@@ -36,12 +36,12 @@
#include "src/core/iomgr/endpoint.h"
-typedef struct
-{
+typedef struct {
grpc_endpoint *client;
grpc_endpoint *server;
} grpc_endpoint_pair;
-grpc_endpoint_pair grpc_iomgr_create_endpoint_pair (const char *name, size_t read_slice_size);
+grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
+ size_t read_slice_size);
#endif /* GRPC_INTERNAL_CORE_IOMGR_ENDPOINT_PAIR_H */
diff --git a/src/core/iomgr/endpoint_pair_posix.c b/src/core/iomgr/endpoint_pair_posix.c
index 5a810fb010..deae9c6875 100644
--- a/src/core/iomgr/endpoint_pair_posix.c
+++ b/src/core/iomgr/endpoint_pair_posix.c
@@ -49,31 +49,30 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-static void
-create_sockets (int sv[2])
-{
+static void create_sockets(int sv[2]) {
int flags;
- GPR_ASSERT (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == 0);
- flags = fcntl (sv[0], F_GETFL, 0);
- GPR_ASSERT (fcntl (sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
- flags = fcntl (sv[1], F_GETFL, 0);
- GPR_ASSERT (fcntl (sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
+ GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
+ flags = fcntl(sv[0], F_GETFL, 0);
+ GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
+ flags = fcntl(sv[1], F_GETFL, 0);
+ GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
}
-grpc_endpoint_pair
-grpc_iomgr_create_endpoint_pair (const char *name, size_t read_slice_size)
-{
+grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
+ size_t read_slice_size) {
int sv[2];
grpc_endpoint_pair p;
char *final_name;
- create_sockets (sv);
+ create_sockets(sv);
- gpr_asprintf (&final_name, "%s:client", name);
- p.client = grpc_tcp_create (grpc_fd_create (sv[1], final_name), read_slice_size, "socketpair-server");
- gpr_free (final_name);
- gpr_asprintf (&final_name, "%s:server", name);
- p.server = grpc_tcp_create (grpc_fd_create (sv[0], final_name), read_slice_size, "socketpair-client");
- gpr_free (final_name);
+ gpr_asprintf(&final_name, "%s:client", name);
+ p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), read_slice_size,
+ "socketpair-server");
+ gpr_free(final_name);
+ gpr_asprintf(&final_name, "%s:server", name);
+ p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), read_slice_size,
+ "socketpair-client");
+ gpr_free(final_name);
return p;
}
diff --git a/src/core/iomgr/endpoint_pair_windows.c b/src/core/iomgr/endpoint_pair_windows.c
index 990548913a..db9d092dca 100644
--- a/src/core/iomgr/endpoint_pair_windows.c
+++ b/src/core/iomgr/endpoint_pair_windows.c
@@ -45,48 +45,52 @@
#include "src/core/iomgr/socket_windows.h"
#include <grpc/support/log.h>
-static void
-create_sockets (SOCKET sv[2])
-{
+static void create_sockets(SOCKET sv[2]) {
SOCKET svr_sock = INVALID_SOCKET;
SOCKET lst_sock = INVALID_SOCKET;
SOCKET cli_sock = INVALID_SOCKET;
SOCKADDR_IN addr;
- int addr_len = sizeof (addr);
+ int addr_len = sizeof(addr);
- lst_sock = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
- GPR_ASSERT (lst_sock != INVALID_SOCKET);
+ lst_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
+ WSA_FLAG_OVERLAPPED);
+ GPR_ASSERT(lst_sock != INVALID_SOCKET);
- memset (&addr, 0, sizeof (addr));
- addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_family = AF_INET;
- GPR_ASSERT (bind (lst_sock, (struct sockaddr *) &addr, sizeof (addr)) != SOCKET_ERROR);
- GPR_ASSERT (listen (lst_sock, SOMAXCONN) != SOCKET_ERROR);
- GPR_ASSERT (getsockname (lst_sock, (struct sockaddr *) &addr, &addr_len) != SOCKET_ERROR);
+ GPR_ASSERT(bind(lst_sock, (struct sockaddr *)&addr, sizeof(addr)) !=
+ SOCKET_ERROR);
+ GPR_ASSERT(listen(lst_sock, SOMAXCONN) != SOCKET_ERROR);
+ GPR_ASSERT(getsockname(lst_sock, (struct sockaddr *)&addr, &addr_len) !=
+ SOCKET_ERROR);
- cli_sock = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
- GPR_ASSERT (cli_sock != INVALID_SOCKET);
+ cli_sock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
+ WSA_FLAG_OVERLAPPED);
+ GPR_ASSERT(cli_sock != INVALID_SOCKET);
- GPR_ASSERT (WSAConnect (cli_sock, (struct sockaddr *) &addr, addr_len, NULL, NULL, NULL, NULL) == 0);
- svr_sock = accept (lst_sock, (struct sockaddr *) &addr, &addr_len);
- GPR_ASSERT (svr_sock != INVALID_SOCKET);
+ GPR_ASSERT(WSAConnect(cli_sock, (struct sockaddr *)&addr, addr_len, NULL,
+ NULL, NULL, NULL) == 0);
+ svr_sock = accept(lst_sock, (struct sockaddr *)&addr, &addr_len);
+ GPR_ASSERT(svr_sock != INVALID_SOCKET);
- closesocket (lst_sock);
- grpc_tcp_prepare_socket (cli_sock);
- grpc_tcp_prepare_socket (svr_sock);
+ closesocket(lst_sock);
+ grpc_tcp_prepare_socket(cli_sock);
+ grpc_tcp_prepare_socket(svr_sock);
sv[1] = cli_sock;
sv[0] = svr_sock;
}
-grpc_endpoint_pair
-grpc_iomgr_create_endpoint_pair (const char *name, size_t read_slice_size)
-{
+grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
+ size_t read_slice_size) {
SOCKET sv[2];
grpc_endpoint_pair p;
- create_sockets (sv);
- p.client = grpc_tcp_create (grpc_winsocket_create (sv[1], "endpoint:client"), "endpoint:server");
- p.server = grpc_tcp_create (grpc_winsocket_create (sv[0], "endpoint:server"), "endpoint:client");
+ create_sockets(sv);
+ p.client = grpc_tcp_create(grpc_winsocket_create(sv[1], "endpoint:client"),
+ "endpoint:server");
+ p.server = grpc_tcp_create(grpc_winsocket_create(sv[0], "endpoint:server"),
+ "endpoint:client");
return p;
}
diff --git a/src/core/iomgr/exec_ctx.c b/src/core/iomgr/exec_ctx.c
index d5a8cf4475..fe8c48a94b 100644
--- a/src/core/iomgr/exec_ctx.c
+++ b/src/core/iomgr/exec_ctx.c
@@ -33,30 +33,28 @@
#include "src/core/iomgr/exec_ctx.h"
-void
-grpc_exec_ctx_flush (grpc_exec_ctx *exec_ctx)
-{
- while (!grpc_closure_list_empty (exec_ctx->closure_list))
- {
- grpc_closure *c = closure_list->head;
- closure_list->head = closure_list->tail = NULL;
- while (c != NULL)
- {
- grpc_closure *next = c->next;
- c->cb (exec_ctx, c->cb_arg, c->success);
- c = next;
- }
+void grpc_exec_ctx_flush(grpc_exec_ctx *exec_ctx) {
+ while (!grpc_closure_list_empty(exec_ctx->closure_list)) {
+ grpc_closure *c = closure_list->head;
+ closure_list->head = closure_list->tail = NULL;
+ while (c != NULL) {
+ grpc_closure *next = c->next;
+ c->cb(exec_ctx, c->cb_arg, c->success);
+ c = next;
}
+ }
}
void grpc_exec_ctx_finish(grpc_exec_ctx *exec_ctx) {
grpc_exec_ctx_flush(exec_ctx);
}
-void grpc_exec_ctx_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure, int success) {
+void grpc_exec_ctx_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure,
+ int success) {
grpc_closure_list_add(&exec_ctx->closure_list, closure, success);
}
-void grpc_exec_ctx_enqueue_list(grpc_exec_ctx *exec_ctx, grpc_closure_list *list) {
+void grpc_exec_ctx_enqueue_list(grpc_exec_ctx *exec_ctx,
+ grpc_closure_list *list) {
grpc_closure_list_move(list, &exec_ctx->closure_list);
}
diff --git a/src/core/iomgr/exec_ctx.h b/src/core/iomgr/exec_ctx.h
index ff084abeba..0cebc1665e 100644
--- a/src/core/iomgr/exec_ctx.h
+++ b/src/core/iomgr/exec_ctx.h
@@ -40,11 +40,14 @@ struct grpc_exec_ctx {
grpc_closure_list closure_list;
};
-#define GRPC_EXEC_CTX_INIT { GRPC_CLOSURE_LIST_INIT }
+#define GRPC_EXEC_CTX_INIT \
+ { GRPC_CLOSURE_LIST_INIT }
void grpc_exec_ctx_finish(grpc_exec_ctx *exec_ctx);
void grpc_exec_ctx_flush(grpc_exec_ctx *exec_ctx);
-void grpc_exec_ctx_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure, int success);
-void grpc_exec_ctx_enqueue_list(grpc_exec_ctx *exec_ctx, grpc_closure_list *list);
+void grpc_exec_ctx_enqueue(grpc_exec_ctx *exec_ctx, grpc_closure *closure,
+ int success);
+void grpc_exec_ctx_enqueue_list(grpc_exec_ctx *exec_ctx,
+ grpc_closure_list *list);
#endif
diff --git a/src/core/iomgr/fd_posix.c b/src/core/iomgr/fd_posix.c
index 27936455c6..31e30d210b 100644
--- a/src/core/iomgr/fd_posix.c
+++ b/src/core/iomgr/fd_posix.c
@@ -45,11 +45,10 @@
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
-enum descriptor_state
-{
+enum descriptor_state {
NOT_READY = 0,
READY = 1
-}; /* or a pointer to a closure to call */
+}; /* or a pointer to a closure to call */
/* We need to keep a freelist not because of any concerns of malloc performance
* but instead so that implementations with multiple threads in (for example)
@@ -71,40 +70,35 @@ enum descriptor_state
static grpc_fd *fd_freelist = NULL;
static gpr_mu fd_freelist_mu;
-static void
-freelist_fd (grpc_fd * fd)
-{
- gpr_mu_lock (&fd_freelist_mu);
+static void freelist_fd(grpc_fd *fd) {
+ gpr_mu_lock(&fd_freelist_mu);
fd->freelist_next = fd_freelist;
fd_freelist = fd;
- grpc_iomgr_unregister_object (&fd->iomgr_object);
- gpr_mu_unlock (&fd_freelist_mu);
+ grpc_iomgr_unregister_object(&fd->iomgr_object);
+ gpr_mu_unlock(&fd_freelist_mu);
}
-static grpc_fd *
-alloc_fd (int fd)
-{
+static grpc_fd *alloc_fd(int fd) {
grpc_fd *r = NULL;
- gpr_mu_lock (&fd_freelist_mu);
- if (fd_freelist != NULL)
- {
- r = fd_freelist;
- fd_freelist = fd_freelist->freelist_next;
- }
- gpr_mu_unlock (&fd_freelist_mu);
- if (r == NULL)
- {
- r = gpr_malloc (sizeof (grpc_fd));
- gpr_mu_init (&r->set_state_mu);
- gpr_mu_init (&r->watcher_mu);
- }
-
- gpr_atm_rel_store (&r->refst, 1);
- gpr_atm_rel_store (&r->readst, NOT_READY);
- gpr_atm_rel_store (&r->writest, NOT_READY);
- gpr_atm_rel_store (&r->shutdown, 0);
+ gpr_mu_lock(&fd_freelist_mu);
+ if (fd_freelist != NULL) {
+ r = fd_freelist;
+ fd_freelist = fd_freelist->freelist_next;
+ }
+ gpr_mu_unlock(&fd_freelist_mu);
+ if (r == NULL) {
+ r = gpr_malloc(sizeof(grpc_fd));
+ gpr_mu_init(&r->set_state_mu);
+ gpr_mu_init(&r->watcher_mu);
+ }
+
+ gpr_atm_rel_store(&r->refst, 1);
+ gpr_atm_rel_store(&r->readst, NOT_READY);
+ gpr_atm_rel_store(&r->writest, NOT_READY);
+ gpr_atm_rel_store(&r->shutdown, 0);
r->fd = fd;
- r->inactive_watcher_root.next = r->inactive_watcher_root.prev = &r->inactive_watcher_root;
+ r->inactive_watcher_root.next = r->inactive_watcher_root.prev =
+ &r->inactive_watcher_root;
r->freelist_next = NULL;
r->read_watcher = r->write_watcher = NULL;
r->on_done_closure = NULL;
@@ -112,198 +106,147 @@ alloc_fd (int fd)
return r;
}
-static void
-destroy (grpc_fd * fd)
-{
- gpr_mu_destroy (&fd->set_state_mu);
- gpr_mu_destroy (&fd->watcher_mu);
- gpr_free (fd);
+static void destroy(grpc_fd *fd) {
+ gpr_mu_destroy(&fd->set_state_mu);
+ gpr_mu_destroy(&fd->watcher_mu);
+ gpr_free(fd);
}
#ifdef GRPC_FD_REF_COUNT_DEBUG
#define REF_BY(fd, n, reason) ref_by(fd, n, reason, __FILE__, __LINE__)
#define UNREF_BY(fd, n, reason) unref_by(fd, n, reason, __FILE__, __LINE__)
-static void
-ref_by (grpc_fd * fd, int n, const char *reason, const char *file, int line)
-{
- gpr_log (GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, gpr_atm_no_barrier_load (&fd->refst), gpr_atm_no_barrier_load (&fd->refst) + n, reason, file, line);
+static void ref_by(grpc_fd *fd, int n, const char *reason, const char *file,
+ int line) {
+ gpr_log(GPR_DEBUG, "FD %d %p ref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
+ gpr_atm_no_barrier_load(&fd->refst),
+ gpr_atm_no_barrier_load(&fd->refst) + n, reason, file, line);
#else
#define REF_BY(fd, n, reason) ref_by(fd, n)
#define UNREF_BY(fd, n, reason) unref_by(fd, n)
-static void
-ref_by (grpc_fd * fd, int n)
-{
+static void ref_by(grpc_fd *fd, int n) {
#endif
- GPR_ASSERT (gpr_atm_no_barrier_fetch_add (&fd->refst, n) > 0);
+ GPR_ASSERT(gpr_atm_no_barrier_fetch_add(&fd->refst, n) > 0);
}
#ifdef GRPC_FD_REF_COUNT_DEBUG
-static void
-unref_by (grpc_fd * fd, int n, const char *reason, const char *file, int line)
-{
+static void unref_by(grpc_fd *fd, int n, const char *reason, const char *file,
+ int line) {
gpr_atm old;
- gpr_log (GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n, gpr_atm_no_barrier_load (&fd->refst), gpr_atm_no_barrier_load (&fd->refst) - n, reason, file, line);
+ gpr_log(GPR_DEBUG, "FD %d %p unref %d %d -> %d [%s; %s:%d]", fd->fd, fd, n,
+ gpr_atm_no_barrier_load(&fd->refst),
+ gpr_atm_no_barrier_load(&fd->refst) - n, reason, file, line);
#else
-static void
-unref_by (grpc_fd * fd, int n)
-{
+static void unref_by(grpc_fd *fd, int n) {
gpr_atm old;
#endif
- old = gpr_atm_full_fetch_add (&fd->refst, -n);
- if (old == n)
- {
- freelist_fd (fd);
- }
- else
- {
- GPR_ASSERT (old > n);
- }
+ old = gpr_atm_full_fetch_add(&fd->refst, -n);
+ if (old == n) {
+ freelist_fd(fd);
+ } else {
+ GPR_ASSERT(old > n);
+ }
}
-void
-grpc_fd_global_init (void)
-{
- gpr_mu_init (&fd_freelist_mu);
+void grpc_fd_global_init(void) { gpr_mu_init(&fd_freelist_mu); }
+
+void grpc_fd_global_shutdown(void) {
+ gpr_mu_lock(&fd_freelist_mu);
+ gpr_mu_unlock(&fd_freelist_mu);
+ while (fd_freelist != NULL) {
+ grpc_fd *fd = fd_freelist;
+ fd_freelist = fd_freelist->freelist_next;
+ destroy(fd);
+ }
+ gpr_mu_destroy(&fd_freelist_mu);
}
-void
-grpc_fd_global_shutdown (void)
-{
- gpr_mu_lock (&fd_freelist_mu);
- gpr_mu_unlock (&fd_freelist_mu);
- while (fd_freelist != NULL)
- {
- grpc_fd *fd = fd_freelist;
- fd_freelist = fd_freelist->freelist_next;
- destroy (fd);
- }
- gpr_mu_destroy (&fd_freelist_mu);
-}
-
-grpc_fd *
-grpc_fd_create (int fd, const char *name)
-{
- grpc_fd *r = alloc_fd (fd);
- grpc_iomgr_register_object (&r->iomgr_object, name);
+grpc_fd *grpc_fd_create(int fd, const char *name) {
+ grpc_fd *r = alloc_fd(fd);
+ grpc_iomgr_register_object(&r->iomgr_object, name);
return r;
}
-int
-grpc_fd_is_orphaned (grpc_fd * fd)
-{
- return (gpr_atm_acq_load (&fd->refst) & 1) == 0;
+int grpc_fd_is_orphaned(grpc_fd *fd) {
+ return (gpr_atm_acq_load(&fd->refst) & 1) == 0;
}
-static void
-pollset_kick_locked (grpc_pollset * pollset)
-{
- gpr_mu_lock (GRPC_POLLSET_MU (pollset));
- grpc_pollset_kick (pollset, NULL);
- gpr_mu_unlock (GRPC_POLLSET_MU (pollset));
+static void pollset_kick_locked(grpc_pollset *pollset) {
+ gpr_mu_lock(GRPC_POLLSET_MU(pollset));
+ grpc_pollset_kick(pollset, NULL);
+ gpr_mu_unlock(GRPC_POLLSET_MU(pollset));
}
-static void
-maybe_wake_one_watcher_locked (grpc_fd * fd)
-{
- if (fd->inactive_watcher_root.next != &fd->inactive_watcher_root)
- {
- pollset_kick_locked (fd->inactive_watcher_root.next->pollset);
- }
- else if (fd->read_watcher)
- {
- pollset_kick_locked (fd->read_watcher->pollset);
- }
- else if (fd->write_watcher)
- {
- pollset_kick_locked (fd->write_watcher->pollset);
- }
+static void maybe_wake_one_watcher_locked(grpc_fd *fd) {
+ if (fd->inactive_watcher_root.next != &fd->inactive_watcher_root) {
+ pollset_kick_locked(fd->inactive_watcher_root.next->pollset);
+ } else if (fd->read_watcher) {
+ pollset_kick_locked(fd->read_watcher->pollset);
+ } else if (fd->write_watcher) {
+ pollset_kick_locked(fd->write_watcher->pollset);
+ }
}
-static void
-maybe_wake_one_watcher (grpc_fd * fd)
-{
- gpr_mu_lock (&fd->watcher_mu);
- maybe_wake_one_watcher_locked (fd);
- gpr_mu_unlock (&fd->watcher_mu);
+static void maybe_wake_one_watcher(grpc_fd *fd) {
+ gpr_mu_lock(&fd->watcher_mu);
+ maybe_wake_one_watcher_locked(fd);
+ gpr_mu_unlock(&fd->watcher_mu);
}
-static void
-wake_all_watchers_locked (grpc_fd * fd)
-{
+static void wake_all_watchers_locked(grpc_fd *fd) {
grpc_fd_watcher *watcher;
- for (watcher = fd->inactive_watcher_root.next; watcher != &fd->inactive_watcher_root; watcher = watcher->next)
- {
- pollset_kick_locked (watcher->pollset);
- }
- if (fd->read_watcher)
- {
- pollset_kick_locked (fd->read_watcher->pollset);
- }
- if (fd->write_watcher && fd->write_watcher != fd->read_watcher)
- {
- pollset_kick_locked (fd->write_watcher->pollset);
- }
+ for (watcher = fd->inactive_watcher_root.next;
+ watcher != &fd->inactive_watcher_root; watcher = watcher->next) {
+ pollset_kick_locked(watcher->pollset);
+ }
+ if (fd->read_watcher) {
+ pollset_kick_locked(fd->read_watcher->pollset);
+ }
+ if (fd->write_watcher && fd->write_watcher != fd->read_watcher) {
+ pollset_kick_locked(fd->write_watcher->pollset);
+ }
}
-static int
-has_watchers (grpc_fd * fd)
-{
- return fd->read_watcher != NULL || fd->write_watcher != NULL || fd->inactive_watcher_root.next != &fd->inactive_watcher_root;
+static int has_watchers(grpc_fd *fd) {
+ return fd->read_watcher != NULL || fd->write_watcher != NULL ||
+ fd->inactive_watcher_root.next != &fd->inactive_watcher_root;
}
-void
-grpc_fd_orphan (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * on_done, const char *reason)
-{
+void grpc_fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure *on_done,
+ const char *reason) {
fd->on_done_closure = on_done;
- shutdown (fd->fd, SHUT_RDWR);
- gpr_mu_lock (&fd->watcher_mu);
- REF_BY (fd, 1, reason); /* remove active status, but keep referenced */
- if (!has_watchers (fd))
- {
- fd->closed = 1;
- close (fd->fd);
- grpc_exec_ctx_enqueue (exec_ctx, fd->on_done_closure, 1);
- }
- else
- {
- wake_all_watchers_locked (fd);
- }
- gpr_mu_unlock (&fd->watcher_mu);
- UNREF_BY (fd, 2, reason); /* drop the reference */
+ shutdown(fd->fd, SHUT_RDWR);
+ gpr_mu_lock(&fd->watcher_mu);
+ REF_BY(fd, 1, reason); /* remove active status, but keep referenced */
+ if (!has_watchers(fd)) {
+ fd->closed = 1;
+ close(fd->fd);
+ grpc_exec_ctx_enqueue(exec_ctx, fd->on_done_closure, 1);
+ } else {
+ wake_all_watchers_locked(fd);
+ }
+ gpr_mu_unlock(&fd->watcher_mu);
+ UNREF_BY(fd, 2, reason); /* drop the reference */
}
/* increment refcount by two to avoid changing the orphan bit */
#ifdef GRPC_FD_REF_COUNT_DEBUG
-void
-grpc_fd_ref (grpc_fd * fd, const char *reason, const char *file, int line)
-{
- ref_by (fd, 2, reason, file, line);
+void grpc_fd_ref(grpc_fd *fd, const char *reason, const char *file, int line) {
+ ref_by(fd, 2, reason, file, line);
}
-void
-grpc_fd_unref (grpc_fd * fd, const char *reason, const char *file, int line)
-{
- unref_by (fd, 2, reason, file, line);
+void grpc_fd_unref(grpc_fd *fd, const char *reason, const char *file,
+ int line) {
+ unref_by(fd, 2, reason, file, line);
}
#else
-void
-grpc_fd_ref (grpc_fd * fd)
-{
- ref_by (fd, 2);
-}
+void grpc_fd_ref(grpc_fd *fd) { ref_by(fd, 2); }
-void
-grpc_fd_unref (grpc_fd * fd)
-{
- unref_by (fd, 2);
-}
+void grpc_fd_unref(grpc_fd *fd) { unref_by(fd, 2); }
#endif
-static void
-notify_on (grpc_exec_ctx * exec_ctx, grpc_fd * fd, gpr_atm * st, grpc_closure * closure)
-{
- switch (gpr_atm_acq_load (st))
- {
+static void notify_on(grpc_exec_ctx *exec_ctx, grpc_fd *fd, gpr_atm *st,
+ grpc_closure *closure) {
+ switch (gpr_atm_acq_load(st)) {
case NOT_READY:
/* There is no race if the descriptor is already ready, so we skip
the interlocked op in that case. As long as the app doesn't
@@ -311,195 +254,177 @@ notify_on (grpc_exec_ctx * exec_ctx, grpc_fd * fd, gpr_atm * st, grpc_closure *
oldval should never be anything other than READY or NOT_READY. We
don't
check for user error on the fast path. */
- if (gpr_atm_rel_cas (st, NOT_READY, (gpr_intptr) closure))
- {
- /* swap was successful -- the closure will run after the next
- set_ready call. NOTE: we don't have an ABA problem here,
- since we should never have concurrent calls to the same
- notify_on function. */
- maybe_wake_one_watcher (fd);
- return;
- }
- /* swap was unsuccessful due to an intervening set_ready call.
- Fall through to the READY code below */
+ if (gpr_atm_rel_cas(st, NOT_READY, (gpr_intptr)closure)) {
+ /* swap was successful -- the closure will run after the next
+ set_ready call. NOTE: we don't have an ABA problem here,
+ since we should never have concurrent calls to the same
+ notify_on function. */
+ maybe_wake_one_watcher(fd);
+ return;
+ }
+ /* swap was unsuccessful due to an intervening set_ready call.
+ Fall through to the READY code below */
case READY:
- GPR_ASSERT (gpr_atm_no_barrier_load (st) == READY);
- gpr_atm_rel_store (st, NOT_READY);
- grpc_exec_ctx_enqueue (exec_ctx, closure, !gpr_atm_acq_load (&fd->shutdown));
+ GPR_ASSERT(gpr_atm_no_barrier_load(st) == READY);
+ gpr_atm_rel_store(st, NOT_READY);
+ grpc_exec_ctx_enqueue(exec_ctx, closure,
+ !gpr_atm_acq_load(&fd->shutdown));
return;
- default: /* WAITING */
+ default: /* WAITING */
/* upcallptr was set to a different closure. This is an error! */
- gpr_log (GPR_ERROR, "User called a notify_on function with a previous callback still " "pending");
- abort ();
- }
- gpr_log (GPR_ERROR, "Corrupt memory in &st->state");
- abort ();
+ gpr_log(GPR_ERROR,
+ "User called a notify_on function with a previous callback still "
+ "pending");
+ abort();
+ }
+ gpr_log(GPR_ERROR, "Corrupt memory in &st->state");
+ abort();
}
-static void
-set_ready_locked (grpc_exec_ctx * exec_ctx, grpc_fd * fd, gpr_atm * st)
-{
- gpr_intptr state = gpr_atm_acq_load (st);
+static void set_ready_locked(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
+ gpr_atm *st) {
+ gpr_intptr state = gpr_atm_acq_load(st);
- switch (state)
- {
+ switch (state) {
case READY:
/* duplicate ready, ignore */
return;
case NOT_READY:
- if (gpr_atm_rel_cas (st, NOT_READY, READY))
- {
- /* swap was successful -- the closure will run after the next
- notify_on call. */
- return;
- }
+ if (gpr_atm_rel_cas(st, NOT_READY, READY)) {
+ /* swap was successful -- the closure will run after the next
+ notify_on call. */
+ return;
+ }
/* swap was unsuccessful due to an intervening set_ready call.
Fall through to the WAITING code below */
- state = gpr_atm_acq_load (st);
- default: /* waiting */
- GPR_ASSERT (gpr_atm_no_barrier_load (st) != READY && gpr_atm_no_barrier_load (st) != NOT_READY);
- grpc_exec_ctx_enqueue (exec_ctx, (grpc_closure *) state, !gpr_atm_acq_load (&fd->shutdown));
- gpr_atm_rel_store (st, NOT_READY);
+ state = gpr_atm_acq_load(st);
+ default: /* waiting */
+ GPR_ASSERT(gpr_atm_no_barrier_load(st) != READY &&
+ gpr_atm_no_barrier_load(st) != NOT_READY);
+ grpc_exec_ctx_enqueue(exec_ctx, (grpc_closure *)state,
+ !gpr_atm_acq_load(&fd->shutdown));
+ gpr_atm_rel_store(st, NOT_READY);
return;
- }
+ }
}
-static void
-set_ready (grpc_exec_ctx * exec_ctx, grpc_fd * fd, gpr_atm * st)
-{
+static void set_ready(grpc_exec_ctx *exec_ctx, grpc_fd *fd, gpr_atm *st) {
/* only one set_ready can be active at once (but there may be a racing
notify_on) */
- gpr_mu_lock (&fd->set_state_mu);
- set_ready_locked (exec_ctx, fd, st);
- gpr_mu_unlock (&fd->set_state_mu);
+ gpr_mu_lock(&fd->set_state_mu);
+ set_ready_locked(exec_ctx, fd, st);
+ gpr_mu_unlock(&fd->set_state_mu);
}
-void
-grpc_fd_shutdown (grpc_exec_ctx * exec_ctx, grpc_fd * fd)
-{
- gpr_mu_lock (&fd->set_state_mu);
- GPR_ASSERT (!gpr_atm_no_barrier_load (&fd->shutdown));
- gpr_atm_rel_store (&fd->shutdown, 1);
- set_ready_locked (exec_ctx, fd, &fd->readst);
- set_ready_locked (exec_ctx, fd, &fd->writest);
- gpr_mu_unlock (&fd->set_state_mu);
+void grpc_fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
+ gpr_mu_lock(&fd->set_state_mu);
+ GPR_ASSERT(!gpr_atm_no_barrier_load(&fd->shutdown));
+ gpr_atm_rel_store(&fd->shutdown, 1);
+ set_ready_locked(exec_ctx, fd, &fd->readst);
+ set_ready_locked(exec_ctx, fd, &fd->writest);
+ gpr_mu_unlock(&fd->set_state_mu);
}
-void
-grpc_fd_notify_on_read (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * closure)
-{
- notify_on (exec_ctx, fd, &fd->readst, closure);
+void grpc_fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
+ grpc_closure *closure) {
+ notify_on(exec_ctx, fd, &fd->readst, closure);
}
-void
-grpc_fd_notify_on_write (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * closure)
-{
- notify_on (exec_ctx, fd, &fd->writest, closure);
+void grpc_fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
+ grpc_closure *closure) {
+ notify_on(exec_ctx, fd, &fd->writest, closure);
}
-gpr_uint32
-grpc_fd_begin_poll (grpc_fd * fd, grpc_pollset * pollset, gpr_uint32 read_mask, gpr_uint32 write_mask, grpc_fd_watcher * watcher)
-{
+gpr_uint32 grpc_fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
+ gpr_uint32 read_mask, gpr_uint32 write_mask,
+ grpc_fd_watcher *watcher) {
gpr_uint32 mask = 0;
/* keep track of pollers that have requested our events, in case they change
*/
- GRPC_FD_REF (fd, "poll");
+ GRPC_FD_REF(fd, "poll");
- gpr_mu_lock (&fd->watcher_mu);
+ gpr_mu_lock(&fd->watcher_mu);
/* if we are shutdown, then don't add to the watcher set */
- if (gpr_atm_no_barrier_load (&fd->shutdown))
- {
- watcher->fd = NULL;
- watcher->pollset = NULL;
- gpr_mu_unlock (&fd->watcher_mu);
- GRPC_FD_UNREF (fd, "poll");
- return 0;
- }
+ if (gpr_atm_no_barrier_load(&fd->shutdown)) {
+ watcher->fd = NULL;
+ watcher->pollset = NULL;
+ gpr_mu_unlock(&fd->watcher_mu);
+ GRPC_FD_UNREF(fd, "poll");
+ return 0;
+ }
/* if there is nobody polling for read, but we need to, then start doing so */
- if (read_mask && !fd->read_watcher && (gpr_uintptr) gpr_atm_acq_load (&fd->readst) > READY)
- {
- fd->read_watcher = watcher;
- mask |= read_mask;
- }
+ if (read_mask && !fd->read_watcher &&
+ (gpr_uintptr)gpr_atm_acq_load(&fd->readst) > READY) {
+ fd->read_watcher = watcher;
+ mask |= read_mask;
+ }
/* if there is nobody polling for write, but we need to, then start doing so
*/
- if (write_mask && !fd->write_watcher && (gpr_uintptr) gpr_atm_acq_load (&fd->writest) > READY)
- {
- fd->write_watcher = watcher;
- mask |= write_mask;
- }
+ if (write_mask && !fd->write_watcher &&
+ (gpr_uintptr)gpr_atm_acq_load(&fd->writest) > READY) {
+ fd->write_watcher = watcher;
+ mask |= write_mask;
+ }
/* if not polling, remember this watcher in case we need someone to later */
- if (mask == 0)
- {
- watcher->next = &fd->inactive_watcher_root;
- watcher->prev = watcher->next->prev;
- watcher->next->prev = watcher->prev->next = watcher;
- }
+ if (mask == 0) {
+ watcher->next = &fd->inactive_watcher_root;
+ watcher->prev = watcher->next->prev;
+ watcher->next->prev = watcher->prev->next = watcher;
+ }
watcher->pollset = pollset;
watcher->fd = fd;
- gpr_mu_unlock (&fd->watcher_mu);
+ gpr_mu_unlock(&fd->watcher_mu);
return mask;
}
-void
-grpc_fd_end_poll (grpc_exec_ctx * exec_ctx, grpc_fd_watcher * watcher, int got_read, int got_write)
-{
+void grpc_fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *watcher,
+ int got_read, int got_write) {
int was_polling = 0;
int kick = 0;
grpc_fd *fd = watcher->fd;
- if (fd == NULL)
- {
- return;
- }
-
- gpr_mu_lock (&fd->watcher_mu);
- if (watcher == fd->read_watcher)
- {
- /* remove read watcher, kick if we still need a read */
- was_polling = 1;
- kick = kick || !got_read;
- fd->read_watcher = NULL;
- }
- if (watcher == fd->write_watcher)
- {
- /* remove write watcher, kick if we still need a write */
- was_polling = 1;
- kick = kick || !got_write;
- fd->write_watcher = NULL;
- }
- if (!was_polling)
- {
- /* remove from inactive list */
- watcher->next->prev = watcher->prev;
- watcher->prev->next = watcher->next;
- }
- if (kick)
- {
- maybe_wake_one_watcher_locked (fd);
- }
- if (grpc_fd_is_orphaned (fd) && !has_watchers (fd) && !fd->closed)
- {
- fd->closed = 1;
- close (fd->fd);
- grpc_exec_ctx_enqueue (exec_ctx, fd->on_done_closure, 1);
- }
- gpr_mu_unlock (&fd->watcher_mu);
-
- GRPC_FD_UNREF (fd, "poll");
+ if (fd == NULL) {
+ return;
+ }
+
+ gpr_mu_lock(&fd->watcher_mu);
+ if (watcher == fd->read_watcher) {
+ /* remove read watcher, kick if we still need a read */
+ was_polling = 1;
+ kick = kick || !got_read;
+ fd->read_watcher = NULL;
+ }
+ if (watcher == fd->write_watcher) {
+ /* remove write watcher, kick if we still need a write */
+ was_polling = 1;
+ kick = kick || !got_write;
+ fd->write_watcher = NULL;
+ }
+ if (!was_polling) {
+ /* remove from inactive list */
+ watcher->next->prev = watcher->prev;
+ watcher->prev->next = watcher->next;
+ }
+ if (kick) {
+ maybe_wake_one_watcher_locked(fd);
+ }
+ if (grpc_fd_is_orphaned(fd) && !has_watchers(fd) && !fd->closed) {
+ fd->closed = 1;
+ close(fd->fd);
+ grpc_exec_ctx_enqueue(exec_ctx, fd->on_done_closure, 1);
+ }
+ gpr_mu_unlock(&fd->watcher_mu);
+
+ GRPC_FD_UNREF(fd, "poll");
}
-void
-grpc_fd_become_readable (grpc_exec_ctx * exec_ctx, grpc_fd * fd)
-{
- set_ready (exec_ctx, fd, &fd->readst);
+void grpc_fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
+ set_ready(exec_ctx, fd, &fd->readst);
}
-void
-grpc_fd_become_writable (grpc_exec_ctx * exec_ctx, grpc_fd * fd)
-{
- set_ready (exec_ctx, fd, &fd->writest);
+void grpc_fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd) {
+ set_ready(exec_ctx, fd, &fd->writest);
}
#endif
diff --git a/src/core/iomgr/fd_posix.h b/src/core/iomgr/fd_posix.h
index 550de3753e..089aa4d717 100644
--- a/src/core/iomgr/fd_posix.h
+++ b/src/core/iomgr/fd_posix.h
@@ -42,16 +42,14 @@
typedef struct grpc_fd grpc_fd;
-typedef struct grpc_fd_watcher
-{
+typedef struct grpc_fd_watcher {
struct grpc_fd_watcher *next;
struct grpc_fd_watcher *prev;
grpc_pollset *pollset;
grpc_fd *fd;
} grpc_fd_watcher;
-struct grpc_fd
-{
+struct grpc_fd {
int fd;
/* refst format:
bit0: 1=active/0=orphaned
@@ -105,7 +103,7 @@ struct grpc_fd
/* Create a wrapped file descriptor.
Requires fd is a non-blocking file descriptor.
This takes ownership of closing fd. */
-grpc_fd *grpc_fd_create (int fd, const char *name);
+grpc_fd *grpc_fd_create(int fd, const char *name);
/* Releases fd to be asynchronously destroyed.
on_done is called when the underlying file descriptor is definitely close()d.
@@ -113,7 +111,8 @@ grpc_fd *grpc_fd_create (int fd, const char *name);
Requires: *fd initialized; no outstanding notify_on_read or
notify_on_write.
MUST NOT be called with a pollset lock taken */
-void grpc_fd_orphan (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * on_done, const char *reason);
+void grpc_fd_orphan(grpc_exec_ctx *exec_ctx, grpc_fd *fd, grpc_closure *on_done,
+ const char *reason);
/* Begin polling on an fd.
Registers that the given pollset is interested in this fd - so that if read
@@ -126,16 +125,19 @@ void grpc_fd_orphan (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * on_d
Polling strategies that do not need to alter their behavior depending on the
fd's current interest (such as epoll) do not need to call this function.
MUST NOT be called with a pollset lock taken */
-gpr_uint32 grpc_fd_begin_poll (grpc_fd * fd, grpc_pollset * pollset, gpr_uint32 read_mask, gpr_uint32 write_mask, grpc_fd_watcher * rec);
+gpr_uint32 grpc_fd_begin_poll(grpc_fd *fd, grpc_pollset *pollset,
+ gpr_uint32 read_mask, gpr_uint32 write_mask,
+ grpc_fd_watcher *rec);
/* Complete polling previously started with grpc_fd_begin_poll
MUST NOT be called with a pollset lock taken */
-void grpc_fd_end_poll (grpc_exec_ctx * exec_ctx, grpc_fd_watcher * rec, int got_read, int got_write);
+void grpc_fd_end_poll(grpc_exec_ctx *exec_ctx, grpc_fd_watcher *rec,
+ int got_read, int got_write);
/* Return 1 if this fd is orphaned, 0 otherwise */
-int grpc_fd_is_orphaned (grpc_fd * fd);
+int grpc_fd_is_orphaned(grpc_fd *fd);
/* Cause any current callbacks to error out with GRPC_CALLBACK_CANCELLED. */
-void grpc_fd_shutdown (grpc_exec_ctx * exec_ctx, grpc_fd * fd);
+void grpc_fd_shutdown(grpc_exec_ctx *exec_ctx, grpc_fd *fd);
/* Register read interest, causing read_cb to be called once when fd becomes
readable, on deadline specified by deadline, or on shutdown triggered by
@@ -150,32 +152,34 @@ void grpc_fd_shutdown (grpc_exec_ctx * exec_ctx, grpc_fd * fd);
underlying platform. This means that users must drain fd in read_cb before
calling notify_on_read again. Users are also expected to handle spurious
events, i.e read_cb is called while nothing can be readable from fd */
-void grpc_fd_notify_on_read (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * closure);
+void grpc_fd_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
+ grpc_closure *closure);
/* Exactly the same semantics as above, except based on writable events. */
-void grpc_fd_notify_on_write (grpc_exec_ctx * exec_ctx, grpc_fd * fd, grpc_closure * closure);
+void grpc_fd_notify_on_write(grpc_exec_ctx *exec_ctx, grpc_fd *fd,
+ grpc_closure *closure);
/* Notification from the poller to an fd that it has become readable or
writable.
If allow_synchronous_callback is 1, allow running the fd callback inline
in this callstack, otherwise register an asynchronous callback and return */
-void grpc_fd_become_readable (grpc_exec_ctx * exec_ctx, grpc_fd * fd);
-void grpc_fd_become_writable (grpc_exec_ctx * exec_ctx, grpc_fd * fd);
+void grpc_fd_become_readable(grpc_exec_ctx *exec_ctx, grpc_fd *fd);
+void grpc_fd_become_writable(grpc_exec_ctx *exec_ctx, grpc_fd *fd);
/* Reference counting for fds */
#ifdef GRPC_FD_REF_COUNT_DEBUG
-void grpc_fd_ref (grpc_fd * fd, const char *reason, const char *file, int line);
-void grpc_fd_unref (grpc_fd * fd, const char *reason, const char *file, int line);
+void grpc_fd_ref(grpc_fd *fd, const char *reason, const char *file, int line);
+void grpc_fd_unref(grpc_fd *fd, const char *reason, const char *file, int line);
#define GRPC_FD_REF(fd, reason) grpc_fd_ref(fd, reason, __FILE__, __LINE__)
#define GRPC_FD_UNREF(fd, reason) grpc_fd_unref(fd, reason, __FILE__, __LINE__)
#else
-void grpc_fd_ref (grpc_fd * fd);
-void grpc_fd_unref (grpc_fd * fd);
+void grpc_fd_ref(grpc_fd *fd);
+void grpc_fd_unref(grpc_fd *fd);
#define GRPC_FD_REF(fd, reason) grpc_fd_ref(fd)
#define GRPC_FD_UNREF(fd, reason) grpc_fd_unref(fd)
#endif
-void grpc_fd_global_init (void);
-void grpc_fd_global_shutdown (void);
+void grpc_fd_global_init(void);
+void grpc_fd_global_shutdown(void);
#endif /* GRPC_INTERNAL_CORE_IOMGR_FD_POSIX_H */
diff --git a/src/core/iomgr/iocp_windows.c b/src/core/iomgr/iocp_windows.c
index d934e638ac..54048910a2 100644
--- a/src/core/iomgr/iocp_windows.c
+++ b/src/core/iomgr/iocp_windows.c
@@ -56,9 +56,7 @@ static gpr_atm g_custom_events = 0;
static HANDLE g_iocp;
-static void
-do_iocp_work ()
-{
+static void do_iocp_work() {
BOOL success;
DWORD bytes = 0;
DWORD flags = 0;
@@ -66,159 +64,135 @@ do_iocp_work ()
LPOVERLAPPED overlapped;
grpc_winsocket *socket;
grpc_winsocket_callback_info *info;
- void (*f) (void *, int) = NULL;
+ void (*f)(void *, int) = NULL;
void *opaque = NULL;
- success = GetQueuedCompletionStatus (g_iocp, &bytes, &completion_key, &overlapped, INFINITE);
+ success = GetQueuedCompletionStatus(g_iocp, &bytes, &completion_key,
+ &overlapped, INFINITE);
/* success = 0 and overlapped = NULL means the deadline got attained.
Which is impossible. since our wait time is +inf */
- GPR_ASSERT (success || overlapped);
- GPR_ASSERT (completion_key && overlapped);
- if (overlapped == &g_iocp_custom_overlap)
- {
- gpr_atm_full_fetch_add (&g_custom_events, -1);
- if (completion_key == (ULONG_PTR) & g_iocp_kick_token)
- {
- /* We were awoken from a kick. */
- return;
- }
- gpr_log (GPR_ERROR, "Unknown custom completion key.");
- abort ();
+ GPR_ASSERT(success || overlapped);
+ GPR_ASSERT(completion_key && overlapped);
+ if (overlapped == &g_iocp_custom_overlap) {
+ gpr_atm_full_fetch_add(&g_custom_events, -1);
+ if (completion_key == (ULONG_PTR)&g_iocp_kick_token) {
+ /* We were awoken from a kick. */
+ return;
}
-
- socket = (grpc_winsocket *) completion_key;
- if (overlapped == &socket->write_info.overlapped)
- {
- info = &socket->write_info;
- }
- else if (overlapped == &socket->read_info.overlapped)
- {
- info = &socket->read_info;
- }
- else
- {
- gpr_log (GPR_ERROR, "Unknown IOCP operation");
- abort ();
- }
- success = WSAGetOverlappedResult (socket->socket, &info->overlapped, &bytes, FALSE, &flags);
+ gpr_log(GPR_ERROR, "Unknown custom completion key.");
+ abort();
+ }
+
+ socket = (grpc_winsocket *)completion_key;
+ if (overlapped == &socket->write_info.overlapped) {
+ info = &socket->write_info;
+ } else if (overlapped == &socket->read_info.overlapped) {
+ info = &socket->read_info;
+ } else {
+ gpr_log(GPR_ERROR, "Unknown IOCP operation");
+ abort();
+ }
+ success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
+ FALSE, &flags);
info->bytes_transfered = bytes;
- info->wsa_error = success ? 0 : WSAGetLastError ();
- GPR_ASSERT (overlapped == &info->overlapped);
- GPR_ASSERT (!info->has_pending_iocp);
- gpr_mu_lock (&socket->state_mu);
- if (info->cb)
- {
- f = info->cb;
- opaque = info->opaque;
- info->cb = NULL;
- }
- else
- {
- info->has_pending_iocp = 1;
- }
- gpr_mu_unlock (&socket->state_mu);
- if (f)
- f (opaque, 1);
+ info->wsa_error = success ? 0 : WSAGetLastError();
+ GPR_ASSERT(overlapped == &info->overlapped);
+ GPR_ASSERT(!info->has_pending_iocp);
+ gpr_mu_lock(&socket->state_mu);
+ if (info->cb) {
+ f = info->cb;
+ opaque = info->opaque;
+ info->cb = NULL;
+ } else {
+ info->has_pending_iocp = 1;
+ }
+ gpr_mu_unlock(&socket->state_mu);
+ if (f) f(opaque, 1);
}
-static void
-iocp_loop (void *p)
-{
- while (gpr_atm_acq_load (&g_custom_events) || !gpr_event_get (&g_shutdown_iocp))
- {
- do_iocp_work ();
- }
+static void iocp_loop(void *p) {
+ while (gpr_atm_acq_load(&g_custom_events) ||
+ !gpr_event_get(&g_shutdown_iocp)) {
+ do_iocp_work();
+ }
- gpr_event_set (&g_iocp_done, (void *) 1);
+ gpr_event_set(&g_iocp_done, (void *)1);
}
-void
-grpc_iocp_init (void)
-{
+void grpc_iocp_init(void) {
gpr_thd_id id;
- g_iocp = CreateIoCompletionPort (INVALID_HANDLE_VALUE, NULL, (ULONG_PTR) NULL, 0);
- GPR_ASSERT (g_iocp);
+ g_iocp =
+ CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
+ GPR_ASSERT(g_iocp);
- gpr_event_init (&g_iocp_done);
- gpr_event_init (&g_shutdown_iocp);
- gpr_thd_new (&id, iocp_loop, NULL, NULL);
+ gpr_event_init(&g_iocp_done);
+ gpr_event_init(&g_shutdown_iocp);
+ gpr_thd_new(&id, iocp_loop, NULL, NULL);
}
-void
-grpc_iocp_kick (void)
-{
+void grpc_iocp_kick(void) {
BOOL success;
- gpr_atm_full_fetch_add (&g_custom_events, 1);
- success = PostQueuedCompletionStatus (g_iocp, 0, (ULONG_PTR) & g_iocp_kick_token, &g_iocp_custom_overlap);
- GPR_ASSERT (success);
+ gpr_atm_full_fetch_add(&g_custom_events, 1);
+ success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token,
+ &g_iocp_custom_overlap);
+ GPR_ASSERT(success);
}
-void
-grpc_iocp_shutdown (void)
-{
+void grpc_iocp_shutdown(void) {
BOOL success;
- gpr_event_set (&g_shutdown_iocp, (void *) 1);
- grpc_iocp_kick ();
- gpr_event_wait (&g_iocp_done, gpr_inf_future (GPR_CLOCK_REALTIME));
- success = CloseHandle (g_iocp);
- GPR_ASSERT (success);
+ gpr_event_set(&g_shutdown_iocp, (void *)1);
+ grpc_iocp_kick();
+ gpr_event_wait(&g_iocp_done, gpr_inf_future(GPR_CLOCK_REALTIME));
+ success = CloseHandle(g_iocp);
+ GPR_ASSERT(success);
}
-void
-grpc_iocp_add_socket (grpc_winsocket * socket)
-{
+void grpc_iocp_add_socket(grpc_winsocket *socket) {
HANDLE ret;
- if (socket->added_to_iocp)
- return;
- ret = CreateIoCompletionPort ((HANDLE) socket->socket, g_iocp, (gpr_uintptr) socket, 0);
- if (!ret)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
- gpr_free (utf8_message);
- __debugbreak ();
- abort ();
- }
+ if (socket->added_to_iocp) return;
+ ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
+ (gpr_uintptr)socket, 0);
+ if (!ret) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
+ gpr_free(utf8_message);
+ __debugbreak();
+ abort();
+ }
socket->added_to_iocp = 1;
- GPR_ASSERT (ret == g_iocp);
+ GPR_ASSERT(ret == g_iocp);
}
/* Calling notify_on_read or write means either of two things:
-) The IOCP already completed in the background, and we need to call
the callback now.
-) The IOCP hasn't completed yet, and we're queuing it for later. */
-static void
-socket_notify_on_iocp (grpc_winsocket * socket, void (*cb) (void *, int), void *opaque, grpc_winsocket_callback_info * info)
-{
+static void socket_notify_on_iocp(grpc_winsocket *socket,
+ void (*cb)(void *, int), void *opaque,
+ grpc_winsocket_callback_info *info) {
int run_now = 0;
- GPR_ASSERT (!info->cb);
- gpr_mu_lock (&socket->state_mu);
- if (info->has_pending_iocp)
- {
- run_now = 1;
- info->has_pending_iocp = 0;
- }
- else
- {
- info->cb = cb;
- info->opaque = opaque;
- }
- gpr_mu_unlock (&socket->state_mu);
- if (run_now)
- cb (opaque, 1);
+ GPR_ASSERT(!info->cb);
+ gpr_mu_lock(&socket->state_mu);
+ if (info->has_pending_iocp) {
+ run_now = 1;
+ info->has_pending_iocp = 0;
+ } else {
+ info->cb = cb;
+ info->opaque = opaque;
+ }
+ gpr_mu_unlock(&socket->state_mu);
+ if (run_now) cb(opaque, 1);
}
-void
-grpc_socket_notify_on_write (grpc_winsocket * socket, void (*cb) (void *, int), void *opaque)
-{
- socket_notify_on_iocp (socket, cb, opaque, &socket->write_info);
+void grpc_socket_notify_on_write(grpc_winsocket *socket,
+ void (*cb)(void *, int), void *opaque) {
+ socket_notify_on_iocp(socket, cb, opaque, &socket->write_info);
}
-void
-grpc_socket_notify_on_read (grpc_winsocket * socket, void (*cb) (void *, int), void *opaque)
-{
- socket_notify_on_iocp (socket, cb, opaque, &socket->read_info);
+void grpc_socket_notify_on_read(grpc_winsocket *socket, void (*cb)(void *, int),
+ void *opaque) {
+ socket_notify_on_iocp(socket, cb, opaque, &socket->read_info);
}
#endif /* GPR_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/iocp_windows.h b/src/core/iomgr/iocp_windows.h
index 972378f1db..7d2dc45176 100644
--- a/src/core/iomgr/iocp_windows.h
+++ b/src/core/iomgr/iocp_windows.h
@@ -38,13 +38,15 @@
#include "src/core/iomgr/socket_windows.h"
-void grpc_iocp_init (void);
-void grpc_iocp_kick (void);
-void grpc_iocp_shutdown (void);
-void grpc_iocp_add_socket (grpc_winsocket *);
+void grpc_iocp_init(void);
+void grpc_iocp_kick(void);
+void grpc_iocp_shutdown(void);
+void grpc_iocp_add_socket(grpc_winsocket *);
-void grpc_socket_notify_on_write (grpc_winsocket *, void (*cb) (void *, int success), void *opaque);
+void grpc_socket_notify_on_write(grpc_winsocket *,
+ void (*cb)(void *, int success), void *opaque);
-void grpc_socket_notify_on_read (grpc_winsocket *, void (*cb) (void *, int success), void *opaque);
+void grpc_socket_notify_on_read(grpc_winsocket *,
+ void (*cb)(void *, int success), void *opaque);
#endif /* GRPC_INTERNAL_CORE_IOMGR_IOCP_WINDOWS_H */
diff --git a/src/core/iomgr/iomgr.c b/src/core/iomgr/iomgr.c
index 30da17fdd8..ef222416af 100644
--- a/src/core/iomgr/iomgr.c
+++ b/src/core/iomgr/iomgr.c
@@ -51,127 +51,112 @@ static gpr_cv g_rcv;
static int g_shutdown;
static grpc_iomgr_object g_root_object;
-void
-grpc_kick_poller (void)
-{
+void grpc_kick_poller(void) {
/* Empty. The background callback executor polls periodically. The activity
* the kicker is trying to draw the executor's attention to will be picked up
* either by one of the periodic wakeups or by one of the polling application
* threads. */
}
-void
-grpc_iomgr_init (void)
-{
+void grpc_iomgr_init(void) {
g_shutdown = 0;
- gpr_mu_init (&g_mu);
- gpr_cv_init (&g_rcv);
- grpc_alarm_list_init (gpr_now (GPR_CLOCK_MONOTONIC));
+ gpr_mu_init(&g_mu);
+ gpr_cv_init(&g_rcv);
+ grpc_alarm_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
g_root_object.next = g_root_object.prev = &g_root_object;
g_root_object.name = "root";
- grpc_iomgr_platform_init ();
+ grpc_iomgr_platform_init();
}
-static size_t
-count_objects (void)
-{
+static size_t count_objects(void) {
grpc_iomgr_object *obj;
size_t n = 0;
- for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next)
- {
- n++;
- }
+ for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
+ n++;
+ }
return n;
}
-static void
-dump_objects (const char *kind)
-{
+static void dump_objects(const char *kind) {
grpc_iomgr_object *obj;
- for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next)
- {
- gpr_log (GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
- }
+ for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
+ gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
+ }
}
-void
-grpc_iomgr_shutdown (void)
-{
- gpr_timespec shutdown_deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_seconds (10, GPR_TIMESPAN));
- gpr_timespec last_warning_time = gpr_now (GPR_CLOCK_REALTIME);
+void grpc_iomgr_shutdown(void) {
+ gpr_timespec shutdown_deadline = gpr_time_add(
+ gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
+ gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- gpr_mu_lock (&g_mu);
+ gpr_mu_lock(&g_mu);
g_shutdown = 1;
- while (g_root_object.next != &g_root_object)
- {
- if (gpr_time_cmp (gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), last_warning_time), gpr_time_from_seconds (1, GPR_TIMESPAN)) >= 0)
- {
- if (g_root_object.next != &g_root_object)
- {
- gpr_log (GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed", count_objects ());
- }
- last_warning_time = gpr_now (GPR_CLOCK_REALTIME);
- }
- if (grpc_alarm_check (&exec_ctx, gpr_inf_future (GPR_CLOCK_MONOTONIC), NULL))
- {
- gpr_mu_unlock (&g_mu);
- grpc_exec_ctx_finish (&exec_ctx);
- gpr_mu_lock (&g_mu);
- continue;
- }
- if (g_root_object.next != &g_root_object)
- {
- int timeout = 0;
- gpr_timespec short_deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_millis (100, GPR_TIMESPAN));
- if (gpr_cv_wait (&g_rcv, &g_mu, short_deadline))
- {
- if (gpr_time_cmp (gpr_now (GPR_CLOCK_REALTIME), shutdown_deadline) > 0)
- {
- timeout = 1;
- break;
- }
- }
- if (timeout && g_root_object.next != &g_root_object)
- {
- gpr_log (GPR_DEBUG, "Failed to free %d iomgr objects before shutdown deadline: " "memory leaks are likely", count_objects ());
- dump_objects ("LEAKED");
- break;
- }
- }
+ while (g_root_object.next != &g_root_object) {
+ if (gpr_time_cmp(
+ gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
+ gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
+ if (g_root_object.next != &g_root_object) {
+ gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed",
+ count_objects());
+ }
+ last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
+ }
+ if (grpc_alarm_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC),
+ NULL)) {
+ gpr_mu_unlock(&g_mu);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_mu_lock(&g_mu);
+ continue;
+ }
+ if (g_root_object.next != &g_root_object) {
+ int timeout = 0;
+ gpr_timespec short_deadline = gpr_time_add(
+ gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100, GPR_TIMESPAN));
+ if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
+ if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) {
+ timeout = 1;
+ break;
+ }
+ }
+ if (timeout && g_root_object.next != &g_root_object) {
+ gpr_log(GPR_DEBUG,
+ "Failed to free %d iomgr objects before shutdown deadline: "
+ "memory leaks are likely",
+ count_objects());
+ dump_objects("LEAKED");
+ break;
+ }
}
- gpr_mu_unlock (&g_mu);
+ }
+ gpr_mu_unlock(&g_mu);
- grpc_alarm_list_shutdown (&exec_ctx);
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_alarm_list_shutdown(&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
/* ensure all threads have left g_mu */
- gpr_mu_lock (&g_mu);
- gpr_mu_unlock (&g_mu);
+ gpr_mu_lock(&g_mu);
+ gpr_mu_unlock(&g_mu);
- grpc_iomgr_platform_shutdown ();
- gpr_mu_destroy (&g_mu);
- gpr_cv_destroy (&g_rcv);
+ grpc_iomgr_platform_shutdown();
+ gpr_mu_destroy(&g_mu);
+ gpr_cv_destroy(&g_rcv);
}
-void
-grpc_iomgr_register_object (grpc_iomgr_object * obj, const char *name)
-{
- obj->name = gpr_strdup (name);
- gpr_mu_lock (&g_mu);
+void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
+ obj->name = gpr_strdup(name);
+ gpr_mu_lock(&g_mu);
obj->next = &g_root_object;
obj->prev = g_root_object.prev;
obj->next->prev = obj->prev->next = obj;
- gpr_mu_unlock (&g_mu);
+ gpr_mu_unlock(&g_mu);
}
-void
-grpc_iomgr_unregister_object (grpc_iomgr_object * obj)
-{
- gpr_mu_lock (&g_mu);
+void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
+ gpr_mu_lock(&g_mu);
obj->next->prev = obj->prev;
obj->prev->next = obj->next;
- gpr_cv_signal (&g_rcv);
- gpr_mu_unlock (&g_mu);
- gpr_free (obj->name);
+ gpr_cv_signal(&g_rcv);
+ gpr_mu_unlock(&g_mu);
+ gpr_free(obj->name);
}
diff --git a/src/core/iomgr/iomgr.h b/src/core/iomgr/iomgr.h
index dc9f3c9b4e..c9ea84c605 100644
--- a/src/core/iomgr/iomgr.h
+++ b/src/core/iomgr/iomgr.h
@@ -35,9 +35,9 @@
#define GRPC_INTERNAL_CORE_IOMGR_IOMGR_H
/** Initializes the iomgr. */
-void grpc_iomgr_init (void);
+void grpc_iomgr_init(void);
/** Signals the intention to shutdown the iomgr. */
-void grpc_iomgr_shutdown (void);
+void grpc_iomgr_shutdown(void);
#endif /* GRPC_INTERNAL_CORE_IOMGR_IOMGR_H */
diff --git a/src/core/iomgr/iomgr_internal.h b/src/core/iomgr/iomgr_internal.h
index 9793734722..f266732c96 100644
--- a/src/core/iomgr/iomgr_internal.h
+++ b/src/core/iomgr/iomgr_internal.h
@@ -37,17 +37,16 @@
#include "src/core/iomgr/iomgr.h"
#include <grpc/support/sync.h>
-typedef struct grpc_iomgr_object
-{
+typedef struct grpc_iomgr_object {
char *name;
struct grpc_iomgr_object *next;
struct grpc_iomgr_object *prev;
} grpc_iomgr_object;
-void grpc_iomgr_register_object (grpc_iomgr_object * obj, const char *name);
-void grpc_iomgr_unregister_object (grpc_iomgr_object * obj);
+void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name);
+void grpc_iomgr_unregister_object(grpc_iomgr_object *obj);
-void grpc_iomgr_platform_init (void);
-void grpc_iomgr_platform_shutdown (void);
+void grpc_iomgr_platform_init(void);
+void grpc_iomgr_platform_shutdown(void);
#endif /* GRPC_INTERNAL_CORE_IOMGR_IOMGR_INTERNAL_H */
diff --git a/src/core/iomgr/iomgr_posix.c b/src/core/iomgr/iomgr_posix.c
index 5bc7b0eca3..2425e59941 100644
--- a/src/core/iomgr/iomgr_posix.c
+++ b/src/core/iomgr/iomgr_posix.c
@@ -40,19 +40,15 @@
#include "src/core/iomgr/fd_posix.h"
#include "src/core/iomgr/tcp_posix.h"
-void
-grpc_iomgr_platform_init (void)
-{
- grpc_fd_global_init ();
- grpc_pollset_global_init ();
- grpc_register_tracer ("tcp", &grpc_tcp_trace);
+void grpc_iomgr_platform_init(void) {
+ grpc_fd_global_init();
+ grpc_pollset_global_init();
+ grpc_register_tracer("tcp", &grpc_tcp_trace);
}
-void
-grpc_iomgr_platform_shutdown (void)
-{
- grpc_pollset_global_shutdown ();
- grpc_fd_global_shutdown ();
+void grpc_iomgr_platform_shutdown(void) {
+ grpc_pollset_global_shutdown();
+ grpc_fd_global_shutdown();
}
#endif /* GRPC_POSIX_SOCKET */
diff --git a/src/core/iomgr/iomgr_posix.h b/src/core/iomgr/iomgr_posix.h
index 6725523a8b..716fedb636 100644
--- a/src/core/iomgr/iomgr_posix.h
+++ b/src/core/iomgr/iomgr_posix.h
@@ -36,7 +36,7 @@
#include "src/core/iomgr/iomgr_internal.h"
-void grpc_pollset_global_init (void);
-void grpc_pollset_global_shutdown (void);
+void grpc_pollset_global_init(void);
+void grpc_pollset_global_shutdown(void);
#endif /* GRPC_INTERNAL_CORE_IOMGR_IOMGR_POSIX_H */
diff --git a/src/core/iomgr/iomgr_windows.c b/src/core/iomgr/iomgr_windows.c
index 4b2e154d0d..b49cb87e97 100644
--- a/src/core/iomgr/iomgr_windows.c
+++ b/src/core/iomgr/iomgr_windows.c
@@ -47,33 +47,25 @@
ports. All of what we're doing here is basically make sure that
Windows sockets are initialized in and out. */
-static void
-winsock_init (void)
-{
+static void winsock_init(void) {
WSADATA wsaData;
- int status = WSAStartup (MAKEWORD (2, 0), &wsaData);
- GPR_ASSERT (status == 0);
+ int status = WSAStartup(MAKEWORD(2, 0), &wsaData);
+ GPR_ASSERT(status == 0);
}
-static void
-winsock_shutdown (void)
-{
- int status = WSACleanup ();
- GPR_ASSERT (status == 0);
+static void winsock_shutdown(void) {
+ int status = WSACleanup();
+ GPR_ASSERT(status == 0);
}
-void
-grpc_iomgr_platform_init (void)
-{
- winsock_init ();
- grpc_iocp_init ();
+void grpc_iomgr_platform_init(void) {
+ winsock_init();
+ grpc_iocp_init();
}
-void
-grpc_iomgr_platform_shutdown (void)
-{
- grpc_iocp_shutdown ();
- winsock_shutdown ();
+void grpc_iomgr_platform_shutdown(void) {
+ grpc_iocp_shutdown();
+ winsock_shutdown();
}
#endif /* GRPC_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/pollset.h b/src/core/iomgr/pollset.h
index 11f0c592e6..d15553a12a 100644
--- a/src/core/iomgr/pollset.h
+++ b/src/core/iomgr/pollset.h
@@ -54,9 +54,10 @@
#include "src/core/iomgr/pollset_windows.h"
#endif
-void grpc_pollset_init (grpc_pollset * pollset);
-void grpc_pollset_shutdown (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_closure * closure);
-void grpc_pollset_destroy (grpc_pollset * pollset);
+void grpc_pollset_init(grpc_pollset *pollset);
+void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_closure *closure);
+void grpc_pollset_destroy(grpc_pollset *pollset);
/* Do some work on a pollset.
May involve invoking asynchronous callbacks, or actually polling file
@@ -76,11 +77,14 @@ void grpc_pollset_destroy (grpc_pollset * pollset);
May call grpc_closure_list_run on grpc_closure_list, without holding the
pollset
lock */
-void grpc_pollset_work (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec now, gpr_timespec deadline);
+void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_pollset_worker *worker, gpr_timespec now,
+ gpr_timespec deadline);
/* Break one polling thread out of polling work for this pollset.
If specific_worker is GRPC_POLLSET_KICK_BROADCAST, kick ALL the workers.
Otherwise, if specific_worker is non-NULL, then kick that worker. */
-void grpc_pollset_kick (grpc_pollset * pollset, grpc_pollset_worker * specific_worker);
+void grpc_pollset_kick(grpc_pollset *pollset,
+ grpc_pollset_worker *specific_worker);
#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_H */
diff --git a/src/core/iomgr/pollset_multipoller_with_epoll.c b/src/core/iomgr/pollset_multipoller_with_epoll.c
index c62a786cf6..5626b08a47 100644
--- a/src/core/iomgr/pollset_multipoller_with_epoll.c
+++ b/src/core/iomgr/pollset_multipoller_with_epoll.c
@@ -45,28 +45,24 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-typedef struct wakeup_fd_hdl
-{
+typedef struct wakeup_fd_hdl {
grpc_wakeup_fd wakeup_fd;
struct wakeup_fd_hdl *next;
} wakeup_fd_hdl;
-typedef struct
-{
+typedef struct {
grpc_pollset *pollset;
grpc_fd *fd;
grpc_closure closure;
} delayed_add;
-typedef struct
-{
+typedef struct {
int epoll_fd;
wakeup_fd_hdl *free_wakeup_fds;
} pollset_hdr;
-static void
-finally_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd)
-{
+static void finally_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_fd *fd) {
pollset_hdr *h = pollset->data.ptr;
struct epoll_event ev;
int err;
@@ -75,98 +71,90 @@ finally_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd)
/* We pretend to be polling whilst adding an fd to keep the fd from being
closed during the add. This may result in a spurious wakeup being assigned
to this pollset whilst adding, but that should be benign. */
- GPR_ASSERT (grpc_fd_begin_poll (fd, pollset, 0, 0, &watcher) == 0);
- if (watcher.fd != NULL)
- {
- ev.events = (uint32_t) (EPOLLIN | EPOLLOUT | EPOLLET);
- ev.data.ptr = fd;
- err = epoll_ctl (h->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev);
- if (err < 0)
- {
- /* FDs may be added to a pollset multiple times, so EEXIST is normal. */
- if (errno != EEXIST)
- {
- gpr_log (GPR_ERROR, "epoll_ctl add for %d failed: %s", fd->fd, strerror (errno));
- }
- }
+ GPR_ASSERT(grpc_fd_begin_poll(fd, pollset, 0, 0, &watcher) == 0);
+ if (watcher.fd != NULL) {
+ ev.events = (uint32_t)(EPOLLIN | EPOLLOUT | EPOLLET);
+ ev.data.ptr = fd;
+ err = epoll_ctl(h->epoll_fd, EPOLL_CTL_ADD, fd->fd, &ev);
+ if (err < 0) {
+ /* FDs may be added to a pollset multiple times, so EEXIST is normal. */
+ if (errno != EEXIST) {
+ gpr_log(GPR_ERROR, "epoll_ctl add for %d failed: %s", fd->fd,
+ strerror(errno));
+ }
}
- grpc_fd_end_poll (exec_ctx, &watcher, 0, 0);
+ }
+ grpc_fd_end_poll(exec_ctx, &watcher, 0, 0);
}
-static void
-perform_delayed_add (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_status)
-{
+static void perform_delayed_add(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_status) {
delayed_add *da = arg;
- if (!grpc_fd_is_orphaned (da->fd))
- {
- finally_add_fd (exec_ctx, da->pollset, da->fd);
- }
+ if (!grpc_fd_is_orphaned(da->fd)) {
+ finally_add_fd(exec_ctx, da->pollset, da->fd);
+ }
- gpr_mu_lock (&da->pollset->mu);
+ gpr_mu_lock(&da->pollset->mu);
da->pollset->in_flight_cbs--;
- if (da->pollset->shutting_down)
- {
- /* We don't care about this pollset anymore. */
- if (da->pollset->in_flight_cbs == 0 && !da->pollset->called_shutdown)
- {
- da->pollset->called_shutdown = 1;
- grpc_exec_ctx_enqueue (exec_ctx, da->pollset->shutdown_done, 1);
- }
+ if (da->pollset->shutting_down) {
+ /* We don't care about this pollset anymore. */
+ if (da->pollset->in_flight_cbs == 0 && !da->pollset->called_shutdown) {
+ da->pollset->called_shutdown = 1;
+ grpc_exec_ctx_enqueue(exec_ctx, da->pollset->shutdown_done, 1);
}
- gpr_mu_unlock (&da->pollset->mu);
+ }
+ gpr_mu_unlock(&da->pollset->mu);
- GRPC_FD_UNREF (da->fd, "delayed_add");
+ GRPC_FD_UNREF(da->fd, "delayed_add");
- gpr_free (da);
+ gpr_free(da);
}
-static void
-multipoll_with_epoll_pollset_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd, int and_unlock_pollset)
-{
- if (and_unlock_pollset)
- {
- gpr_mu_unlock (&pollset->mu);
- finally_add_fd (exec_ctx, pollset, fd);
- }
- else
- {
- delayed_add *da = gpr_malloc (sizeof (*da));
- da->pollset = pollset;
- da->fd = fd;
- GRPC_FD_REF (fd, "delayed_add");
- grpc_closure_init (&da->closure, perform_delayed_add, da);
- pollset->in_flight_cbs++;
- grpc_exec_ctx_enqueue (exec_ctx, &da->closure, 1);
- }
+static void multipoll_with_epoll_pollset_add_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset,
+ grpc_fd *fd,
+ int and_unlock_pollset) {
+ if (and_unlock_pollset) {
+ gpr_mu_unlock(&pollset->mu);
+ finally_add_fd(exec_ctx, pollset, fd);
+ } else {
+ delayed_add *da = gpr_malloc(sizeof(*da));
+ da->pollset = pollset;
+ da->fd = fd;
+ GRPC_FD_REF(fd, "delayed_add");
+ grpc_closure_init(&da->closure, perform_delayed_add, da);
+ pollset->in_flight_cbs++;
+ grpc_exec_ctx_enqueue(exec_ctx, &da->closure, 1);
+ }
}
-static void
-multipoll_with_epoll_pollset_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd, int and_unlock_pollset)
-{
+static void multipoll_with_epoll_pollset_del_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset,
+ grpc_fd *fd,
+ int and_unlock_pollset) {
pollset_hdr *h = pollset->data.ptr;
int err;
- if (and_unlock_pollset)
- {
- gpr_mu_unlock (&pollset->mu);
- }
+ if (and_unlock_pollset) {
+ gpr_mu_unlock(&pollset->mu);
+ }
/* Note that this can race with concurrent poll, but that should be fine since
* at worst it creates a spurious read event on a reused grpc_fd object. */
- err = epoll_ctl (h->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
- if (err < 0)
- {
- gpr_log (GPR_ERROR, "epoll_ctl del for %d failed: %s", fd->fd, strerror (errno));
- }
+ err = epoll_ctl(h->epoll_fd, EPOLL_CTL_DEL, fd->fd, NULL);
+ if (err < 0) {
+ gpr_log(GPR_ERROR, "epoll_ctl del for %d failed: %s", fd->fd,
+ strerror(errno));
+ }
}
/* TODO(klempner): We probably want to turn this down a bit */
#define GRPC_EPOLL_MAX_EVENTS 1000
-static void
-multipoll_with_epoll_pollset_maybe_work_and_unlock (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec deadline, gpr_timespec now)
-{
+static void multipoll_with_epoll_pollset_maybe_work_and_unlock(
+ grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, grpc_pollset_worker *worker,
+ gpr_timespec deadline, gpr_timespec now) {
struct epoll_event ep_ev[GRPC_EPOLL_MAX_EVENTS];
int ep_rv;
int poll_rv;
@@ -180,116 +168,93 @@ multipoll_with_epoll_pollset_maybe_work_and_unlock (grpc_exec_ctx * exec_ctx, gr
* here.
*/
- gpr_mu_unlock (&pollset->mu);
+ gpr_mu_unlock(&pollset->mu);
- timeout_ms = grpc_poll_deadline_to_millis_timeout (deadline, now);
+ timeout_ms = grpc_poll_deadline_to_millis_timeout(deadline, now);
- pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD (&worker->wakeup_fd);
+ pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd);
pfds[0].events = POLLIN;
pfds[0].revents = 0;
pfds[1].fd = h->epoll_fd;
pfds[1].events = POLLIN;
pfds[1].revents = 0;
- poll_rv = grpc_poll_function (pfds, 2, timeout_ms);
+ poll_rv = grpc_poll_function(pfds, 2, timeout_ms);
- if (poll_rv < 0)
- {
- if (errno != EINTR)
- {
- gpr_log (GPR_ERROR, "poll() failed: %s", strerror (errno));
- }
+ if (poll_rv < 0) {
+ if (errno != EINTR) {
+ gpr_log(GPR_ERROR, "poll() failed: %s", strerror(errno));
}
- else if (poll_rv == 0)
- {
- /* do nothing */
+ } else if (poll_rv == 0) {
+ /* do nothing */
+ } else {
+ if (pfds[0].revents) {
+ grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd);
}
- else
- {
- if (pfds[0].revents)
- {
- grpc_wakeup_fd_consume_wakeup (&worker->wakeup_fd);
- }
- if (pfds[1].revents)
- {
- do
- {
- ep_rv = epoll_wait (h->epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
- if (ep_rv < 0)
- {
- if (errno != EINTR)
- {
- gpr_log (GPR_ERROR, "epoll_wait() failed: %s", strerror (errno));
- }
- }
- else
- {
- int i;
- for (i = 0; i < ep_rv; ++i)
- {
- grpc_fd *fd = ep_ev[i].data.ptr;
- /* TODO(klempner): We might want to consider making err and pri
- * separate events */
- int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
- int read = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
- int write = ep_ev[i].events & EPOLLOUT;
- if (read || cancel)
- {
- grpc_fd_become_readable (exec_ctx, fd);
- }
- if (write || cancel)
- {
- grpc_fd_become_writable (exec_ctx, fd);
- }
- }
- }
- }
- while (ep_rv == GRPC_EPOLL_MAX_EVENTS);
- }
+ if (pfds[1].revents) {
+ do {
+ ep_rv = epoll_wait(h->epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
+ if (ep_rv < 0) {
+ if (errno != EINTR) {
+ gpr_log(GPR_ERROR, "epoll_wait() failed: %s", strerror(errno));
+ }
+ } else {
+ int i;
+ for (i = 0; i < ep_rv; ++i) {
+ grpc_fd *fd = ep_ev[i].data.ptr;
+ /* TODO(klempner): We might want to consider making err and pri
+ * separate events */
+ int cancel = ep_ev[i].events & (EPOLLERR | EPOLLHUP);
+ int read = ep_ev[i].events & (EPOLLIN | EPOLLPRI);
+ int write = ep_ev[i].events & EPOLLOUT;
+ if (read || cancel) {
+ grpc_fd_become_readable(exec_ctx, fd);
+ }
+ if (write || cancel) {
+ grpc_fd_become_writable(exec_ctx, fd);
+ }
+ }
+ }
+ } while (ep_rv == GRPC_EPOLL_MAX_EVENTS);
}
+ }
}
-static void
-multipoll_with_epoll_pollset_finish_shutdown (grpc_pollset * pollset)
-{
-}
+static void multipoll_with_epoll_pollset_finish_shutdown(
+ grpc_pollset *pollset) {}
-static void
-multipoll_with_epoll_pollset_destroy (grpc_pollset * pollset)
-{
+static void multipoll_with_epoll_pollset_destroy(grpc_pollset *pollset) {
pollset_hdr *h = pollset->data.ptr;
- close (h->epoll_fd);
- gpr_free (h);
+ close(h->epoll_fd);
+ gpr_free(h);
}
static const grpc_pollset_vtable multipoll_with_epoll_pollset = {
- multipoll_with_epoll_pollset_add_fd, multipoll_with_epoll_pollset_del_fd,
- multipoll_with_epoll_pollset_maybe_work_and_unlock,
- multipoll_with_epoll_pollset_finish_shutdown,
- multipoll_with_epoll_pollset_destroy
-};
-
-static void
-epoll_become_multipoller (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd ** fds, size_t nfds)
-{
+ multipoll_with_epoll_pollset_add_fd, multipoll_with_epoll_pollset_del_fd,
+ multipoll_with_epoll_pollset_maybe_work_and_unlock,
+ multipoll_with_epoll_pollset_finish_shutdown,
+ multipoll_with_epoll_pollset_destroy};
+
+static void epoll_become_multipoller(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset, grpc_fd **fds,
+ size_t nfds) {
size_t i;
- pollset_hdr *h = gpr_malloc (sizeof (pollset_hdr));
+ pollset_hdr *h = gpr_malloc(sizeof(pollset_hdr));
pollset->vtable = &multipoll_with_epoll_pollset;
pollset->data.ptr = h;
- h->epoll_fd = epoll_create1 (EPOLL_CLOEXEC);
- if (h->epoll_fd < 0)
- {
- /* TODO(klempner): Fall back to poll here, especially on ENOSYS */
- gpr_log (GPR_ERROR, "epoll_create1 failed: %s", strerror (errno));
- abort ();
- }
- for (i = 0; i < nfds; i++)
- {
- multipoll_with_epoll_pollset_add_fd (exec_ctx, pollset, fds[i], 0);
- }
+ h->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
+ if (h->epoll_fd < 0) {
+ /* TODO(klempner): Fall back to poll here, especially on ENOSYS */
+ gpr_log(GPR_ERROR, "epoll_create1 failed: %s", strerror(errno));
+ abort();
+ }
+ for (i = 0; i < nfds; i++) {
+ multipoll_with_epoll_pollset_add_fd(exec_ctx, pollset, fds[i], 0);
+ }
}
-grpc_platform_become_multipoller_type grpc_platform_become_multipoller = epoll_become_multipoller;
+grpc_platform_become_multipoller_type grpc_platform_become_multipoller =
+ epoll_become_multipoller;
#endif /* GPR_LINUX_MULTIPOLL_WITH_EPOLL */
diff --git a/src/core/iomgr/pollset_multipoller_with_poll_posix.c b/src/core/iomgr/pollset_multipoller_with_poll_posix.c
index b7eb6c0ab0..2a18cedb33 100644
--- a/src/core/iomgr/pollset_multipoller_with_poll_posix.c
+++ b/src/core/iomgr/pollset_multipoller_with_poll_posix.c
@@ -48,8 +48,7 @@
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
-typedef struct
-{
+typedef struct {
/* all polled fds */
size_t fd_count;
size_t fd_capacity;
@@ -60,52 +59,48 @@ typedef struct
grpc_fd **dels;
} pollset_hdr;
-static void
-multipoll_with_poll_pollset_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd, int and_unlock_pollset)
-{
+static void multipoll_with_poll_pollset_add_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset,
+ grpc_fd *fd,
+ int and_unlock_pollset) {
size_t i;
pollset_hdr *h = pollset->data.ptr;
/* TODO(ctiller): this is O(num_fds^2); maybe switch to a hash set here */
- for (i = 0; i < h->fd_count; i++)
- {
- if (h->fds[i] == fd)
- goto exit;
- }
- if (h->fd_count == h->fd_capacity)
- {
- h->fd_capacity = GPR_MAX (h->fd_capacity + 8, h->fd_count * 3 / 2);
- h->fds = gpr_realloc (h->fds, sizeof (grpc_fd *) * h->fd_capacity);
- }
+ for (i = 0; i < h->fd_count; i++) {
+ if (h->fds[i] == fd) goto exit;
+ }
+ if (h->fd_count == h->fd_capacity) {
+ h->fd_capacity = GPR_MAX(h->fd_capacity + 8, h->fd_count * 3 / 2);
+ h->fds = gpr_realloc(h->fds, sizeof(grpc_fd *) * h->fd_capacity);
+ }
h->fds[h->fd_count++] = fd;
- GRPC_FD_REF (fd, "multipoller");
+ GRPC_FD_REF(fd, "multipoller");
exit:
- if (and_unlock_pollset)
- {
- gpr_mu_unlock (&pollset->mu);
- }
+ if (and_unlock_pollset) {
+ gpr_mu_unlock(&pollset->mu);
+ }
}
-static void
-multipoll_with_poll_pollset_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd, int and_unlock_pollset)
-{
+static void multipoll_with_poll_pollset_del_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset,
+ grpc_fd *fd,
+ int and_unlock_pollset) {
/* will get removed next poll cycle */
pollset_hdr *h = pollset->data.ptr;
- if (h->del_count == h->del_capacity)
- {
- h->del_capacity = GPR_MAX (h->del_capacity + 8, h->del_count * 3 / 2);
- h->dels = gpr_realloc (h->dels, sizeof (grpc_fd *) * h->del_capacity);
- }
+ if (h->del_count == h->del_capacity) {
+ h->del_capacity = GPR_MAX(h->del_capacity + 8, h->del_count * 3 / 2);
+ h->dels = gpr_realloc(h->dels, sizeof(grpc_fd *) * h->del_capacity);
+ }
h->dels[h->del_count++] = fd;
- GRPC_FD_REF (fd, "multipoller_del");
- if (and_unlock_pollset)
- {
- gpr_mu_unlock (&pollset->mu);
- }
+ GRPC_FD_REF(fd, "multipoller_del");
+ if (and_unlock_pollset) {
+ gpr_mu_unlock(&pollset->mu);
+ }
}
-static void
-multipoll_with_poll_pollset_maybe_work_and_unlock (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec deadline, gpr_timespec now)
-{
+static void multipoll_with_poll_pollset_maybe_work_and_unlock(
+ grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, grpc_pollset_worker *worker,
+ gpr_timespec deadline, gpr_timespec now) {
int timeout;
int r;
size_t i, j, fd_count;
@@ -116,150 +111,125 @@ multipoll_with_poll_pollset_maybe_work_and_unlock (grpc_exec_ctx * exec_ctx, grp
struct pollfd *pfds;
h = pollset->data.ptr;
- timeout = grpc_poll_deadline_to_millis_timeout (deadline, now);
+ timeout = grpc_poll_deadline_to_millis_timeout(deadline, now);
/* TODO(ctiller): perform just one malloc here if we exceed the inline case */
- pfds = gpr_malloc (sizeof (*pfds) * (h->fd_count + 1));
- watchers = gpr_malloc (sizeof (*watchers) * (h->fd_count + 1));
+ pfds = gpr_malloc(sizeof(*pfds) * (h->fd_count + 1));
+ watchers = gpr_malloc(sizeof(*watchers) * (h->fd_count + 1));
fd_count = 0;
pfd_count = 1;
- pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD (&worker->wakeup_fd);
+ pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd);
pfds[0].events = POLLIN;
pfds[0].revents = POLLOUT;
- for (i = 0; i < h->fd_count; i++)
- {
- int remove = grpc_fd_is_orphaned (h->fds[i]);
- for (j = 0; !remove && j < h->del_count; j++)
- {
- if (h->fds[i] == h->dels[j])
- remove = 1;
- }
- if (remove)
- {
- GRPC_FD_UNREF (h->fds[i], "multipoller");
- }
- else
- {
- h->fds[fd_count++] = h->fds[i];
- watchers[pfd_count].fd = h->fds[i];
- pfds[pfd_count].fd = h->fds[i]->fd;
- pfds[pfd_count].revents = 0;
- pfd_count++;
- }
+ for (i = 0; i < h->fd_count; i++) {
+ int remove = grpc_fd_is_orphaned(h->fds[i]);
+ for (j = 0; !remove && j < h->del_count; j++) {
+ if (h->fds[i] == h->dels[j]) remove = 1;
}
- for (j = 0; j < h->del_count; j++)
- {
- GRPC_FD_UNREF (h->dels[j], "multipoller_del");
+ if (remove) {
+ GRPC_FD_UNREF(h->fds[i], "multipoller");
+ } else {
+ h->fds[fd_count++] = h->fds[i];
+ watchers[pfd_count].fd = h->fds[i];
+ pfds[pfd_count].fd = h->fds[i]->fd;
+ pfds[pfd_count].revents = 0;
+ pfd_count++;
}
+ }
+ for (j = 0; j < h->del_count; j++) {
+ GRPC_FD_UNREF(h->dels[j], "multipoller_del");
+ }
h->del_count = 0;
h->fd_count = fd_count;
- gpr_mu_unlock (&pollset->mu);
+ gpr_mu_unlock(&pollset->mu);
- for (i = 1; i < pfd_count; i++)
- {
- pfds[i].events = (short) grpc_fd_begin_poll (watchers[i].fd, pollset, POLLIN, POLLOUT, &watchers[i]);
- }
+ for (i = 1; i < pfd_count; i++) {
+ pfds[i].events = (short)grpc_fd_begin_poll(watchers[i].fd, pollset, POLLIN,
+ POLLOUT, &watchers[i]);
+ }
- r = grpc_poll_function (pfds, pfd_count, timeout);
+ r = grpc_poll_function(pfds, pfd_count, timeout);
- for (i = 1; i < pfd_count; i++)
- {
- grpc_fd_end_poll (exec_ctx, &watchers[i], pfds[i].revents & POLLIN, pfds[i].revents & POLLOUT);
- }
+ for (i = 1; i < pfd_count; i++) {
+ grpc_fd_end_poll(exec_ctx, &watchers[i], pfds[i].revents & POLLIN,
+ pfds[i].revents & POLLOUT);
+ }
- if (r < 0)
- {
- if (errno != EINTR)
- {
- gpr_log (GPR_ERROR, "poll() failed: %s", strerror (errno));
- }
+ if (r < 0) {
+ if (errno != EINTR) {
+ gpr_log(GPR_ERROR, "poll() failed: %s", strerror(errno));
}
- else if (r == 0)
- {
- /* do nothing */
+ } else if (r == 0) {
+ /* do nothing */
+ } else {
+ if (pfds[0].revents & POLLIN) {
+ grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd);
}
- else
- {
- if (pfds[0].revents & POLLIN)
- {
- grpc_wakeup_fd_consume_wakeup (&worker->wakeup_fd);
- }
- for (i = 1; i < pfd_count; i++)
- {
- if (watchers[i].fd == NULL)
- {
- continue;
- }
- if (pfds[i].revents & (POLLIN | POLLHUP | POLLERR))
- {
- grpc_fd_become_readable (exec_ctx, watchers[i].fd);
- }
- if (pfds[i].revents & (POLLOUT | POLLHUP | POLLERR))
- {
- grpc_fd_become_writable (exec_ctx, watchers[i].fd);
- }
- }
+ for (i = 1; i < pfd_count; i++) {
+ if (watchers[i].fd == NULL) {
+ continue;
+ }
+ if (pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) {
+ grpc_fd_become_readable(exec_ctx, watchers[i].fd);
+ }
+ if (pfds[i].revents & (POLLOUT | POLLHUP | POLLERR)) {
+ grpc_fd_become_writable(exec_ctx, watchers[i].fd);
+ }
}
+ }
- gpr_free (pfds);
- gpr_free (watchers);
+ gpr_free(pfds);
+ gpr_free(watchers);
}
-static void
-multipoll_with_poll_pollset_finish_shutdown (grpc_pollset * pollset)
-{
+static void multipoll_with_poll_pollset_finish_shutdown(grpc_pollset *pollset) {
size_t i;
pollset_hdr *h = pollset->data.ptr;
- for (i = 0; i < h->fd_count; i++)
- {
- GRPC_FD_UNREF (h->fds[i], "multipoller");
- }
- for (i = 0; i < h->del_count; i++)
- {
- GRPC_FD_UNREF (h->dels[i], "multipoller_del");
- }
+ for (i = 0; i < h->fd_count; i++) {
+ GRPC_FD_UNREF(h->fds[i], "multipoller");
+ }
+ for (i = 0; i < h->del_count; i++) {
+ GRPC_FD_UNREF(h->dels[i], "multipoller_del");
+ }
h->fd_count = 0;
h->del_count = 0;
}
-static void
-multipoll_with_poll_pollset_destroy (grpc_pollset * pollset)
-{
+static void multipoll_with_poll_pollset_destroy(grpc_pollset *pollset) {
pollset_hdr *h = pollset->data.ptr;
- multipoll_with_poll_pollset_finish_shutdown (pollset);
- gpr_free (h->fds);
- gpr_free (h->dels);
- gpr_free (h);
+ multipoll_with_poll_pollset_finish_shutdown(pollset);
+ gpr_free(h->fds);
+ gpr_free(h->dels);
+ gpr_free(h);
}
static const grpc_pollset_vtable multipoll_with_poll_pollset = {
- multipoll_with_poll_pollset_add_fd, multipoll_with_poll_pollset_del_fd,
- multipoll_with_poll_pollset_maybe_work_and_unlock,
- multipoll_with_poll_pollset_finish_shutdown,
- multipoll_with_poll_pollset_destroy
-};
-
-void
-grpc_poll_become_multipoller (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd ** fds, size_t nfds)
-{
+ multipoll_with_poll_pollset_add_fd, multipoll_with_poll_pollset_del_fd,
+ multipoll_with_poll_pollset_maybe_work_and_unlock,
+ multipoll_with_poll_pollset_finish_shutdown,
+ multipoll_with_poll_pollset_destroy};
+
+void grpc_poll_become_multipoller(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset, grpc_fd **fds,
+ size_t nfds) {
size_t i;
- pollset_hdr *h = gpr_malloc (sizeof (pollset_hdr));
+ pollset_hdr *h = gpr_malloc(sizeof(pollset_hdr));
pollset->vtable = &multipoll_with_poll_pollset;
pollset->data.ptr = h;
h->fd_count = nfds;
h->fd_capacity = nfds;
- h->fds = gpr_malloc (nfds * sizeof (grpc_fd *));
+ h->fds = gpr_malloc(nfds * sizeof(grpc_fd *));
h->del_count = 0;
h->del_capacity = 0;
h->dels = NULL;
- for (i = 0; i < nfds; i++)
- {
- h->fds[i] = fds[i];
- GRPC_FD_REF (fds[i], "multipoller");
- }
+ for (i = 0; i < nfds; i++) {
+ h->fds[i] = fds[i];
+ GRPC_FD_REF(fds[i], "multipoller");
+ }
}
#endif /* GPR_POSIX_SOCKET */
#ifdef GPR_POSIX_MULTIPOLL_WITH_POLL
-grpc_platform_become_multipoller_type grpc_platform_become_multipoller = grpc_poll_become_multipoller;
+grpc_platform_become_multipoller_type grpc_platform_become_multipoller =
+ grpc_poll_become_multipoller;
#endif
diff --git a/src/core/iomgr/pollset_posix.c b/src/core/iomgr/pollset_posix.c
index fedc0d8aed..850b51e09e 100644
--- a/src/core/iomgr/pollset_posix.c
+++ b/src/core/iomgr/pollset_posix.c
@@ -53,281 +53,234 @@
#include <grpc/support/tls.h>
#include <grpc/support/useful.h>
-GPR_TLS_DECL (g_current_thread_poller);
-GPR_TLS_DECL (g_current_thread_worker);
+GPR_TLS_DECL(g_current_thread_poller);
+GPR_TLS_DECL(g_current_thread_worker);
grpc_poll_function_type grpc_poll_function = poll;
-static void
-remove_worker (grpc_pollset * p, grpc_pollset_worker * worker)
-{
+static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
worker->prev->next = worker->next;
worker->next->prev = worker->prev;
}
-int
-grpc_pollset_has_workers (grpc_pollset * p)
-{
+int grpc_pollset_has_workers(grpc_pollset *p) {
return p->root_worker.next != &p->root_worker;
}
-static grpc_pollset_worker *
-pop_front_worker (grpc_pollset * p)
-{
- if (grpc_pollset_has_workers (p))
- {
- grpc_pollset_worker *w = p->root_worker.next;
- remove_worker (p, w);
- return w;
- }
- else
- {
- return NULL;
- }
+static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
+ if (grpc_pollset_has_workers(p)) {
+ grpc_pollset_worker *w = p->root_worker.next;
+ remove_worker(p, w);
+ return w;
+ } else {
+ return NULL;
+ }
}
-static void
-push_back_worker (grpc_pollset * p, grpc_pollset_worker * worker)
-{
+static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
worker->next = &p->root_worker;
worker->prev = worker->next->prev;
worker->prev->next = worker->next->prev = worker;
}
-static void
-push_front_worker (grpc_pollset * p, grpc_pollset_worker * worker)
-{
+static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
worker->prev = &p->root_worker;
worker->next = worker->prev->next;
worker->prev->next = worker->next->prev = worker;
}
-void
-grpc_pollset_kick (grpc_pollset * p, grpc_pollset_worker * specific_worker)
-{
+void grpc_pollset_kick(grpc_pollset *p, grpc_pollset_worker *specific_worker) {
/* pollset->mu already held */
- if (specific_worker != NULL)
- {
- if (specific_worker == GRPC_POLLSET_KICK_BROADCAST)
- {
- for (specific_worker = p->root_worker.next; specific_worker != &p->root_worker; specific_worker = specific_worker->next)
- {
- grpc_wakeup_fd_wakeup (&specific_worker->wakeup_fd);
- }
- p->kicked_without_pollers = 1;
- }
- else if (gpr_tls_get (&g_current_thread_worker) != (gpr_intptr) specific_worker)
- {
- grpc_wakeup_fd_wakeup (&specific_worker->wakeup_fd);
- }
- }
- else if (gpr_tls_get (&g_current_thread_poller) != (gpr_intptr) p)
- {
- specific_worker = pop_front_worker (p);
- if (specific_worker != NULL)
- {
- push_back_worker (p, specific_worker);
- grpc_wakeup_fd_wakeup (&specific_worker->wakeup_fd);
- }
- else
- {
- p->kicked_without_pollers = 1;
- }
- }
+ if (specific_worker != NULL) {
+ if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
+ for (specific_worker = p->root_worker.next;
+ specific_worker != &p->root_worker;
+ specific_worker = specific_worker->next) {
+ grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd);
+ }
+ p->kicked_without_pollers = 1;
+ } else if (gpr_tls_get(&g_current_thread_worker) !=
+ (gpr_intptr)specific_worker) {
+ grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd);
+ }
+ } else if (gpr_tls_get(&g_current_thread_poller) != (gpr_intptr)p) {
+ specific_worker = pop_front_worker(p);
+ if (specific_worker != NULL) {
+ push_back_worker(p, specific_worker);
+ grpc_wakeup_fd_wakeup(&specific_worker->wakeup_fd);
+ } else {
+ p->kicked_without_pollers = 1;
+ }
+ }
}
/* global state management */
-void
-grpc_pollset_global_init (void)
-{
- gpr_tls_init (&g_current_thread_poller);
- grpc_wakeup_fd_global_init ();
+void grpc_pollset_global_init(void) {
+ gpr_tls_init(&g_current_thread_poller);
+ grpc_wakeup_fd_global_init();
}
-void
-grpc_pollset_global_shutdown (void)
-{
- gpr_tls_destroy (&g_current_thread_poller);
- grpc_wakeup_fd_global_destroy ();
+void grpc_pollset_global_shutdown(void) {
+ gpr_tls_destroy(&g_current_thread_poller);
+ grpc_wakeup_fd_global_destroy();
}
/* main interface */
-static void become_basic_pollset (grpc_pollset * pollset, grpc_fd * fd_or_null);
+static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null);
-void
-grpc_pollset_init (grpc_pollset * pollset)
-{
- gpr_mu_init (&pollset->mu);
+void grpc_pollset_init(grpc_pollset *pollset) {
+ gpr_mu_init(&pollset->mu);
pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
pollset->in_flight_cbs = 0;
pollset->shutting_down = 0;
pollset->called_shutdown = 0;
pollset->idle_jobs.head = pollset->idle_jobs.tail = NULL;
- become_basic_pollset (pollset, NULL);
+ become_basic_pollset(pollset, NULL);
}
-void
-grpc_pollset_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd)
-{
- gpr_mu_lock (&pollset->mu);
- pollset->vtable->add_fd (exec_ctx, pollset, fd, 1);
+void grpc_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_fd *fd) {
+ gpr_mu_lock(&pollset->mu);
+ pollset->vtable->add_fd(exec_ctx, pollset, fd, 1);
/* the following (enabled only in debug) will reacquire and then release
our lock - meaning that if the unlocking flag passed to del_fd above is
not respected, the code will deadlock (in a way that we have a chance of
debugging) */
#ifndef NDEBUG
- gpr_mu_lock (&pollset->mu);
- gpr_mu_unlock (&pollset->mu);
+ gpr_mu_lock(&pollset->mu);
+ gpr_mu_unlock(&pollset->mu);
#endif
}
-void
-grpc_pollset_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd)
-{
- gpr_mu_lock (&pollset->mu);
- pollset->vtable->del_fd (exec_ctx, pollset, fd, 1);
+void grpc_pollset_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_fd *fd) {
+ gpr_mu_lock(&pollset->mu);
+ pollset->vtable->del_fd(exec_ctx, pollset, fd, 1);
/* the following (enabled only in debug) will reacquire and then release
our lock - meaning that if the unlocking flag passed to del_fd above is
not respected, the code will deadlock (in a way that we have a chance of
debugging) */
#ifndef NDEBUG
- gpr_mu_lock (&pollset->mu);
- gpr_mu_unlock (&pollset->mu);
+ gpr_mu_lock(&pollset->mu);
+ gpr_mu_unlock(&pollset->mu);
#endif
}
-static void
-finish_shutdown (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset)
-{
- pollset->vtable->finish_shutdown (pollset);
- grpc_exec_ctx_enqueue (exec_ctx, pollset->shutdown_done, 1);
+static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset) {
+ pollset->vtable->finish_shutdown(pollset);
+ grpc_exec_ctx_enqueue(exec_ctx, pollset->shutdown_done, 1);
}
-void
-grpc_pollset_work (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec now, gpr_timespec deadline)
-{
+void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_pollset_worker *worker, gpr_timespec now,
+ gpr_timespec deadline) {
/* pollset->mu already held */
int added_worker = 0;
int locked = 1;
/* this must happen before we (potentially) drop pollset->mu */
worker->next = worker->prev = NULL;
/* TODO(ctiller): pool these */
- grpc_wakeup_fd_init (&worker->wakeup_fd);
- if (!grpc_pollset_has_workers (pollset) && !grpc_closure_list_empty (pollset->idle_jobs))
- {
- grpc_exec_ctx_enqueue_list (exec_ctx, &pollset->idle_jobs);
- goto done;
- }
- if (grpc_alarm_check (exec_ctx, now, &deadline))
- {
- goto done;
- }
- if (pollset->shutting_down)
- {
- goto done;
- }
- if (pollset->in_flight_cbs)
- {
- /* Give do_promote priority so we don't starve it out */
- gpr_mu_unlock (&pollset->mu);
- locked = 0;
- goto done;
- }
- if (!pollset->kicked_without_pollers)
- {
- push_front_worker (pollset, worker);
- added_worker = 1;
- gpr_tls_set (&g_current_thread_poller, (gpr_intptr) pollset);
- pollset->vtable->maybe_work_and_unlock (exec_ctx, pollset, worker, deadline, now);
- locked = 0;
- gpr_tls_set (&g_current_thread_poller, 0);
- }
- else
- {
- pollset->kicked_without_pollers = 0;
- }
+ grpc_wakeup_fd_init(&worker->wakeup_fd);
+ if (!grpc_pollset_has_workers(pollset) &&
+ !grpc_closure_list_empty(pollset->idle_jobs)) {
+ grpc_exec_ctx_enqueue_list(exec_ctx, &pollset->idle_jobs);
+ goto done;
+ }
+ if (grpc_alarm_check(exec_ctx, now, &deadline)) {
+ goto done;
+ }
+ if (pollset->shutting_down) {
+ goto done;
+ }
+ if (pollset->in_flight_cbs) {
+ /* Give do_promote priority so we don't starve it out */
+ gpr_mu_unlock(&pollset->mu);
+ locked = 0;
+ goto done;
+ }
+ if (!pollset->kicked_without_pollers) {
+ push_front_worker(pollset, worker);
+ added_worker = 1;
+ gpr_tls_set(&g_current_thread_poller, (gpr_intptr)pollset);
+ pollset->vtable->maybe_work_and_unlock(exec_ctx, pollset, worker, deadline,
+ now);
+ locked = 0;
+ gpr_tls_set(&g_current_thread_poller, 0);
+ } else {
+ pollset->kicked_without_pollers = 0;
+ }
done:
- if (!locked)
- {
- grpc_exec_ctx_flush (exec_ctx);
- gpr_mu_lock (&pollset->mu);
- locked = 1;
- }
- grpc_wakeup_fd_destroy (&worker->wakeup_fd);
- if (added_worker)
- {
- remove_worker (pollset, worker);
- }
- if (pollset->shutting_down)
- {
- if (grpc_pollset_has_workers (pollset))
- {
- grpc_pollset_kick (pollset, NULL);
- }
- else if (!pollset->called_shutdown && pollset->in_flight_cbs == 0)
- {
- pollset->called_shutdown = 1;
- gpr_mu_unlock (&pollset->mu);
- finish_shutdown (exec_ctx, pollset);
- grpc_exec_ctx_flush (exec_ctx);
- /* Continuing to access pollset here is safe -- it is the caller's
- * responsibility to not destroy when it has outstanding calls to
- * grpc_pollset_work.
- * TODO(dklempner): Can we refactor the shutdown logic to avoid this? */
- gpr_mu_lock (&pollset->mu);
- }
- }
+ if (!locked) {
+ grpc_exec_ctx_flush(exec_ctx);
+ gpr_mu_lock(&pollset->mu);
+ locked = 1;
+ }
+ grpc_wakeup_fd_destroy(&worker->wakeup_fd);
+ if (added_worker) {
+ remove_worker(pollset, worker);
+ }
+ if (pollset->shutting_down) {
+ if (grpc_pollset_has_workers(pollset)) {
+ grpc_pollset_kick(pollset, NULL);
+ } else if (!pollset->called_shutdown && pollset->in_flight_cbs == 0) {
+ pollset->called_shutdown = 1;
+ gpr_mu_unlock(&pollset->mu);
+ finish_shutdown(exec_ctx, pollset);
+ grpc_exec_ctx_flush(exec_ctx);
+ /* Continuing to access pollset here is safe -- it is the caller's
+ * responsibility to not destroy when it has outstanding calls to
+ * grpc_pollset_work.
+ * TODO(dklempner): Can we refactor the shutdown logic to avoid this? */
+ gpr_mu_lock(&pollset->mu);
+ }
+ }
}
-void
-grpc_pollset_shutdown (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_closure * closure)
-{
+void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_closure *closure) {
int call_shutdown = 0;
- gpr_mu_lock (&pollset->mu);
- GPR_ASSERT (!pollset->shutting_down);
+ gpr_mu_lock(&pollset->mu);
+ GPR_ASSERT(!pollset->shutting_down);
pollset->shutting_down = 1;
- if (!pollset->called_shutdown && pollset->in_flight_cbs == 0 && !grpc_pollset_has_workers (pollset))
- {
- pollset->called_shutdown = 1;
- call_shutdown = 1;
- }
+ if (!pollset->called_shutdown && pollset->in_flight_cbs == 0 &&
+ !grpc_pollset_has_workers(pollset)) {
+ pollset->called_shutdown = 1;
+ call_shutdown = 1;
+ }
pollset->shutdown_done = closure;
- grpc_pollset_kick (pollset, GRPC_POLLSET_KICK_BROADCAST);
- gpr_mu_unlock (&pollset->mu);
+ grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
+ gpr_mu_unlock(&pollset->mu);
- if (call_shutdown)
- {
- finish_shutdown (exec_ctx, pollset);
- }
+ if (call_shutdown) {
+ finish_shutdown(exec_ctx, pollset);
+ }
}
-void
-grpc_pollset_destroy (grpc_pollset * pollset)
-{
- GPR_ASSERT (pollset->shutting_down);
- GPR_ASSERT (pollset->in_flight_cbs == 0);
- GPR_ASSERT (!grpc_pollset_has_workers (pollset));
- pollset->vtable->destroy (pollset);
- gpr_mu_destroy (&pollset->mu);
+void grpc_pollset_destroy(grpc_pollset *pollset) {
+ GPR_ASSERT(pollset->shutting_down);
+ GPR_ASSERT(pollset->in_flight_cbs == 0);
+ GPR_ASSERT(!grpc_pollset_has_workers(pollset));
+ pollset->vtable->destroy(pollset);
+ gpr_mu_destroy(&pollset->mu);
}
-int
-grpc_poll_deadline_to_millis_timeout (gpr_timespec deadline, gpr_timespec now)
-{
+int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline,
+ gpr_timespec now) {
gpr_timespec timeout;
static const int max_spin_polling_us = 10;
- if (gpr_time_cmp (deadline, gpr_inf_future (deadline.clock_type)) == 0)
- {
- return -1;
- }
- if (gpr_time_cmp (deadline, gpr_time_add (now, gpr_time_from_micros (max_spin_polling_us, GPR_TIMESPAN))) <= 0)
- {
- return 0;
- }
- timeout = gpr_time_sub (deadline, now);
- return gpr_time_to_millis (gpr_time_add (timeout, gpr_time_from_nanos (GPR_NS_PER_SEC - 1, GPR_TIMESPAN)));
+ if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) {
+ return -1;
+ }
+ if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros(
+ max_spin_polling_us,
+ GPR_TIMESPAN))) <= 0) {
+ return 0;
+ }
+ timeout = gpr_time_sub(deadline, now);
+ return gpr_time_to_millis(gpr_time_add(
+ timeout, gpr_time_from_nanos(GPR_NS_PER_SEC - 1, GPR_TIMESPAN)));
}
/*
@@ -335,17 +288,14 @@ grpc_poll_deadline_to_millis_timeout (gpr_timespec deadline, gpr_timespec now)
* descriptor via poll()
*/
-typedef struct grpc_unary_promote_args
-{
+typedef struct grpc_unary_promote_args {
const grpc_pollset_vtable *original_vtable;
grpc_pollset *pollset;
grpc_fd *fd;
grpc_closure promotion_closure;
} grpc_unary_promote_args;
-static void
-basic_do_promote (grpc_exec_ctx * exec_ctx, void *args, int success)
-{
+static void basic_do_promote(grpc_exec_ctx *exec_ctx, void *args, int success) {
grpc_unary_promote_args *up_args = args;
const grpc_pollset_vtable *original_vtable = up_args->original_vtable;
grpc_pollset *pollset = up_args->pollset;
@@ -360,143 +310,123 @@ basic_do_promote (grpc_exec_ctx * exec_ctx, void *args, int success)
* 4. The pollset may be shutting down.
*/
- gpr_mu_lock (&pollset->mu);
+ gpr_mu_lock(&pollset->mu);
/* First we need to ensure that nobody is polling concurrently */
- GPR_ASSERT (!grpc_pollset_has_workers (pollset));
+ GPR_ASSERT(!grpc_pollset_has_workers(pollset));
- gpr_free (up_args);
+ gpr_free(up_args);
/* At this point the pollset may no longer be a unary poller. In that case
* we should just call the right add function and be done. */
/* TODO(klempner): If we're not careful this could cause infinite recursion.
* That's not a problem for now because empty_pollset has a trivial poller
* and we don't have any mechanism to unbecome multipoller. */
pollset->in_flight_cbs--;
- if (pollset->shutting_down)
- {
- /* We don't care about this pollset anymore. */
- if (pollset->in_flight_cbs == 0 && !pollset->called_shutdown)
- {
- GPR_ASSERT (!grpc_pollset_has_workers (pollset));
- pollset->called_shutdown = 1;
- grpc_exec_ctx_enqueue (exec_ctx, pollset->shutdown_done, 1);
- }
- }
- else if (grpc_fd_is_orphaned (fd))
- {
- /* Don't try to add it to anything, we'll drop our ref on it below */
- }
- else if (pollset->vtable != original_vtable)
- {
- pollset->vtable->add_fd (exec_ctx, pollset, fd, 0);
- }
- else if (fd != pollset->data.ptr)
- {
- grpc_fd *fds[2];
- fds[0] = pollset->data.ptr;
- fds[1] = fd;
-
- if (fds[0] && !grpc_fd_is_orphaned (fds[0]))
- {
- grpc_platform_become_multipoller (exec_ctx, pollset, fds, GPR_ARRAY_SIZE (fds));
- GRPC_FD_UNREF (fds[0], "basicpoll");
- }
- else
- {
- /* old fd is orphaned and we haven't cleaned it up until now, so remain a
- * unary poller */
- /* Note that it is possible that fds[1] is also orphaned at this point.
- * That's okay, we'll correct it at the next add or poll. */
- if (fds[0])
- GRPC_FD_UNREF (fds[0], "basicpoll");
- pollset->data.ptr = fd;
- GRPC_FD_REF (fd, "basicpoll");
- }
- }
-
- gpr_mu_unlock (&pollset->mu);
+ if (pollset->shutting_down) {
+ /* We don't care about this pollset anymore. */
+ if (pollset->in_flight_cbs == 0 && !pollset->called_shutdown) {
+ GPR_ASSERT(!grpc_pollset_has_workers(pollset));
+ pollset->called_shutdown = 1;
+ grpc_exec_ctx_enqueue(exec_ctx, pollset->shutdown_done, 1);
+ }
+ } else if (grpc_fd_is_orphaned(fd)) {
+ /* Don't try to add it to anything, we'll drop our ref on it below */
+ } else if (pollset->vtable != original_vtable) {
+ pollset->vtable->add_fd(exec_ctx, pollset, fd, 0);
+ } else if (fd != pollset->data.ptr) {
+ grpc_fd *fds[2];
+ fds[0] = pollset->data.ptr;
+ fds[1] = fd;
+
+ if (fds[0] && !grpc_fd_is_orphaned(fds[0])) {
+ grpc_platform_become_multipoller(exec_ctx, pollset, fds,
+ GPR_ARRAY_SIZE(fds));
+ GRPC_FD_UNREF(fds[0], "basicpoll");
+ } else {
+ /* old fd is orphaned and we haven't cleaned it up until now, so remain a
+ * unary poller */
+ /* Note that it is possible that fds[1] is also orphaned at this point.
+ * That's okay, we'll correct it at the next add or poll. */
+ if (fds[0]) GRPC_FD_UNREF(fds[0], "basicpoll");
+ pollset->data.ptr = fd;
+ GRPC_FD_REF(fd, "basicpoll");
+ }
+ }
+
+ gpr_mu_unlock(&pollset->mu);
/* Matching ref in basic_pollset_add_fd */
- GRPC_FD_UNREF (fd, "basicpoll_add");
+ GRPC_FD_UNREF(fd, "basicpoll_add");
}
-static void
-basic_pollset_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd, int and_unlock_pollset)
-{
+static void basic_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_fd *fd, int and_unlock_pollset) {
grpc_unary_promote_args *up_args;
- GPR_ASSERT (fd);
- if (fd == pollset->data.ptr)
- goto exit;
-
- if (!grpc_pollset_has_workers (pollset))
- {
- /* Fast path -- no in flight cbs */
- /* TODO(klempner): Comment this out and fix any test failures or establish
- * they are due to timing issues */
- grpc_fd *fds[2];
- fds[0] = pollset->data.ptr;
- fds[1] = fd;
-
- if (fds[0] == NULL)
- {
- pollset->data.ptr = fd;
- GRPC_FD_REF (fd, "basicpoll");
- }
- else if (!grpc_fd_is_orphaned (fds[0]))
- {
- grpc_platform_become_multipoller (exec_ctx, pollset, fds, GPR_ARRAY_SIZE (fds));
- GRPC_FD_UNREF (fds[0], "basicpoll");
- }
- else
- {
- /* old fd is orphaned and we haven't cleaned it up until now, so remain a
- * unary poller */
- GRPC_FD_UNREF (fds[0], "basicpoll");
- pollset->data.ptr = fd;
- GRPC_FD_REF (fd, "basicpoll");
- }
- goto exit;
+ GPR_ASSERT(fd);
+ if (fd == pollset->data.ptr) goto exit;
+
+ if (!grpc_pollset_has_workers(pollset)) {
+ /* Fast path -- no in flight cbs */
+ /* TODO(klempner): Comment this out and fix any test failures or establish
+ * they are due to timing issues */
+ grpc_fd *fds[2];
+ fds[0] = pollset->data.ptr;
+ fds[1] = fd;
+
+ if (fds[0] == NULL) {
+ pollset->data.ptr = fd;
+ GRPC_FD_REF(fd, "basicpoll");
+ } else if (!grpc_fd_is_orphaned(fds[0])) {
+ grpc_platform_become_multipoller(exec_ctx, pollset, fds,
+ GPR_ARRAY_SIZE(fds));
+ GRPC_FD_UNREF(fds[0], "basicpoll");
+ } else {
+ /* old fd is orphaned and we haven't cleaned it up until now, so remain a
+ * unary poller */
+ GRPC_FD_UNREF(fds[0], "basicpoll");
+ pollset->data.ptr = fd;
+ GRPC_FD_REF(fd, "basicpoll");
}
+ goto exit;
+ }
/* Now we need to promote. This needs to happen when we're not polling. Since
* this may be called from poll, the wait needs to happen asynchronously. */
- GRPC_FD_REF (fd, "basicpoll_add");
+ GRPC_FD_REF(fd, "basicpoll_add");
pollset->in_flight_cbs++;
- up_args = gpr_malloc (sizeof (*up_args));
+ up_args = gpr_malloc(sizeof(*up_args));
up_args->fd = fd;
up_args->original_vtable = pollset->vtable;
up_args->pollset = pollset;
up_args->promotion_closure.cb = basic_do_promote;
up_args->promotion_closure.cb_arg = up_args;
- grpc_closure_list_add (&pollset->idle_jobs, &up_args->promotion_closure, 1);
- grpc_pollset_kick (pollset, GRPC_POLLSET_KICK_BROADCAST);
+ grpc_closure_list_add(&pollset->idle_jobs, &up_args->promotion_closure, 1);
+ grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
exit:
- if (and_unlock_pollset)
- {
- gpr_mu_unlock (&pollset->mu);
- }
+ if (and_unlock_pollset) {
+ gpr_mu_unlock(&pollset->mu);
+ }
}
-static void
-basic_pollset_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_fd * fd, int and_unlock_pollset)
-{
- GPR_ASSERT (fd);
- if (fd == pollset->data.ptr)
- {
- GRPC_FD_UNREF (pollset->data.ptr, "basicpoll");
- pollset->data.ptr = NULL;
- }
-
- if (and_unlock_pollset)
- {
- gpr_mu_unlock (&pollset->mu);
- }
+static void basic_pollset_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_fd *fd, int and_unlock_pollset) {
+ GPR_ASSERT(fd);
+ if (fd == pollset->data.ptr) {
+ GRPC_FD_UNREF(pollset->data.ptr, "basicpoll");
+ pollset->data.ptr = NULL;
+ }
+
+ if (and_unlock_pollset) {
+ gpr_mu_unlock(&pollset->mu);
+ }
}
-static void
-basic_pollset_maybe_work_and_unlock (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec deadline, gpr_timespec now)
-{
+static void basic_pollset_maybe_work_and_unlock(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset,
+ grpc_pollset_worker *worker,
+ gpr_timespec deadline,
+ gpr_timespec now) {
struct pollfd pfd[2];
grpc_fd *fd;
grpc_fd_watcher fd_watcher;
@@ -505,98 +435,77 @@ basic_pollset_maybe_work_and_unlock (grpc_exec_ctx * exec_ctx, grpc_pollset * po
nfds_t nfds;
fd = pollset->data.ptr;
- if (fd && grpc_fd_is_orphaned (fd))
- {
- GRPC_FD_UNREF (fd, "basicpoll");
- fd = pollset->data.ptr = NULL;
- }
- timeout = grpc_poll_deadline_to_millis_timeout (deadline, now);
- pfd[0].fd = GRPC_WAKEUP_FD_GET_READ_FD (&worker->wakeup_fd);
+ if (fd && grpc_fd_is_orphaned(fd)) {
+ GRPC_FD_UNREF(fd, "basicpoll");
+ fd = pollset->data.ptr = NULL;
+ }
+ timeout = grpc_poll_deadline_to_millis_timeout(deadline, now);
+ pfd[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&worker->wakeup_fd);
pfd[0].events = POLLIN;
pfd[0].revents = 0;
nfds = 1;
- if (fd)
- {
- pfd[1].fd = fd->fd;
- pfd[1].revents = 0;
- gpr_mu_unlock (&pollset->mu);
- pfd[1].events = (short) grpc_fd_begin_poll (fd, pollset, POLLIN, POLLOUT, &fd_watcher);
- if (pfd[1].events != 0)
- {
- nfds++;
- }
- }
- else
- {
- gpr_mu_unlock (&pollset->mu);
- }
+ if (fd) {
+ pfd[1].fd = fd->fd;
+ pfd[1].revents = 0;
+ gpr_mu_unlock(&pollset->mu);
+ pfd[1].events =
+ (short)grpc_fd_begin_poll(fd, pollset, POLLIN, POLLOUT, &fd_watcher);
+ if (pfd[1].events != 0) {
+ nfds++;
+ }
+ } else {
+ gpr_mu_unlock(&pollset->mu);
+ }
/* poll fd count (argument 2) is shortened by one if we have no events
to poll on - such that it only includes the kicker */
- r = grpc_poll_function (pfd, nfds, timeout);
- GRPC_TIMER_MARK (GRPC_PTAG_POLL_FINISHED, r);
-
- if (fd)
- {
- grpc_fd_end_poll (exec_ctx, &fd_watcher, pfd[1].revents & POLLIN, pfd[1].revents & POLLOUT);
- }
-
- if (r < 0)
- {
- if (errno != EINTR)
- {
- gpr_log (GPR_ERROR, "poll() failed: %s", strerror (errno));
- }
- }
- else if (r == 0)
- {
- /* do nothing */
- }
- else
- {
- if (pfd[0].revents & POLLIN)
- {
- grpc_wakeup_fd_consume_wakeup (&worker->wakeup_fd);
- }
- if (nfds > 1)
- {
- if (pfd[1].revents & (POLLIN | POLLHUP | POLLERR))
- {
- grpc_fd_become_readable (exec_ctx, fd);
- }
- if (pfd[1].revents & (POLLOUT | POLLHUP | POLLERR))
- {
- grpc_fd_become_writable (exec_ctx, fd);
- }
- }
- }
+ r = grpc_poll_function(pfd, nfds, timeout);
+ GRPC_TIMER_MARK(GRPC_PTAG_POLL_FINISHED, r);
+
+ if (fd) {
+ grpc_fd_end_poll(exec_ctx, &fd_watcher, pfd[1].revents & POLLIN,
+ pfd[1].revents & POLLOUT);
+ }
+
+ if (r < 0) {
+ if (errno != EINTR) {
+ gpr_log(GPR_ERROR, "poll() failed: %s", strerror(errno));
+ }
+ } else if (r == 0) {
+ /* do nothing */
+ } else {
+ if (pfd[0].revents & POLLIN) {
+ grpc_wakeup_fd_consume_wakeup(&worker->wakeup_fd);
+ }
+ if (nfds > 1) {
+ if (pfd[1].revents & (POLLIN | POLLHUP | POLLERR)) {
+ grpc_fd_become_readable(exec_ctx, fd);
+ }
+ if (pfd[1].revents & (POLLOUT | POLLHUP | POLLERR)) {
+ grpc_fd_become_writable(exec_ctx, fd);
+ }
+ }
+ }
}
-static void
-basic_pollset_destroy (grpc_pollset * pollset)
-{
- if (pollset->data.ptr != NULL)
- {
- GRPC_FD_UNREF (pollset->data.ptr, "basicpoll");
- pollset->data.ptr = NULL;
- }
+static void basic_pollset_destroy(grpc_pollset *pollset) {
+ if (pollset->data.ptr != NULL) {
+ GRPC_FD_UNREF(pollset->data.ptr, "basicpoll");
+ pollset->data.ptr = NULL;
+ }
}
static const grpc_pollset_vtable basic_pollset = {
- basic_pollset_add_fd, basic_pollset_del_fd,
- basic_pollset_maybe_work_and_unlock, basic_pollset_destroy,
- basic_pollset_destroy
-};
-
-static void
-become_basic_pollset (grpc_pollset * pollset, grpc_fd * fd_or_null)
-{
+ basic_pollset_add_fd, basic_pollset_del_fd,
+ basic_pollset_maybe_work_and_unlock, basic_pollset_destroy,
+ basic_pollset_destroy};
+
+static void become_basic_pollset(grpc_pollset *pollset, grpc_fd *fd_or_null) {
pollset->vtable = &basic_pollset;
pollset->data.ptr = fd_or_null;
- if (fd_or_null != NULL)
- {
- GRPC_FD_REF (fd_or_null, "basicpoll");
- }
+ if (fd_or_null != NULL) {
+ GRPC_FD_REF(fd_or_null, "basicpoll");
+ }
}
#endif /* GPR_POSIX_POLLSET */
diff --git a/src/core/iomgr/pollset_posix.h b/src/core/iomgr/pollset_posix.h
index 91cf93580f..f996dd1edf 100644
--- a/src/core/iomgr/pollset_posix.h
+++ b/src/core/iomgr/pollset_posix.h
@@ -48,15 +48,13 @@ typedef struct grpc_pollset_vtable grpc_pollset_vtable;
use the struct tag */
struct grpc_fd;
-typedef struct grpc_pollset_worker
-{
+typedef struct grpc_pollset_worker {
grpc_wakeup_fd wakeup_fd;
struct grpc_pollset_worker *next;
struct grpc_pollset_worker *prev;
} grpc_pollset_worker;
-typedef struct grpc_pollset
-{
+typedef struct grpc_pollset {
/* pollsets under posix can mutate representation as fds are added and
removed.
For example, we may choose a poll() based implementation on linux for
@@ -70,34 +68,38 @@ typedef struct grpc_pollset
int kicked_without_pollers;
grpc_closure *shutdown_done;
grpc_closure_list idle_jobs;
- union
- {
+ union {
int fd;
void *ptr;
} data;
} grpc_pollset;
-struct grpc_pollset_vtable
-{
- void (*add_fd) (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, struct grpc_fd * fd, int and_unlock_pollset);
- void (*del_fd) (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, struct grpc_fd * fd, int and_unlock_pollset);
- void (*maybe_work_and_unlock) (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec deadline, gpr_timespec now);
- void (*finish_shutdown) (grpc_pollset * pollset);
- void (*destroy) (grpc_pollset * pollset);
+struct grpc_pollset_vtable {
+ void (*add_fd)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ struct grpc_fd *fd, int and_unlock_pollset);
+ void (*del_fd)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ struct grpc_fd *fd, int and_unlock_pollset);
+ void (*maybe_work_and_unlock)(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ grpc_pollset_worker *worker,
+ gpr_timespec deadline, gpr_timespec now);
+ void (*finish_shutdown)(grpc_pollset *pollset);
+ void (*destroy)(grpc_pollset *pollset);
};
#define GRPC_POLLSET_MU(pollset) (&(pollset)->mu)
/* Add an fd to a pollset */
-void grpc_pollset_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, struct grpc_fd *fd);
+void grpc_pollset_add_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ struct grpc_fd *fd);
/* Force remove an fd from a pollset (normally they are removed on the next
poll after an fd is orphaned) */
-void grpc_pollset_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, struct grpc_fd *fd);
+void grpc_pollset_del_fd(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
+ struct grpc_fd *fd);
/* Returns the fd to listen on for kicks */
-int grpc_kick_read_fd (grpc_pollset * p);
+int grpc_kick_read_fd(grpc_pollset *p);
/* Call after polling has been kicked to leave the kicked state */
-void grpc_kick_drain (grpc_pollset * p);
+void grpc_kick_drain(grpc_pollset *p);
/* Convert a timespec to milliseconds:
- very small or negative poll times are clamped to zero to do a
@@ -106,20 +108,26 @@ void grpc_kick_drain (grpc_pollset * p);
- longer than a millisecond polls are rounded up to the next nearest
millisecond to avoid spinning
- infinite timeouts are converted to -1 */
-int grpc_poll_deadline_to_millis_timeout (gpr_timespec deadline, gpr_timespec now);
+int grpc_poll_deadline_to_millis_timeout(gpr_timespec deadline,
+ gpr_timespec now);
/* turn a pollset into a multipoller: platform specific */
-typedef void (*grpc_platform_become_multipoller_type) (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, struct grpc_fd ** fds, size_t fd_count);
+typedef void (*grpc_platform_become_multipoller_type)(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset,
+ struct grpc_fd **fds,
+ size_t fd_count);
extern grpc_platform_become_multipoller_type grpc_platform_become_multipoller;
-void grpc_poll_become_multipoller (grpc_exec_ctx * exec_ctx, grpc_pollset * pollset, struct grpc_fd **fds, size_t fd_count);
+void grpc_poll_become_multipoller(grpc_exec_ctx *exec_ctx,
+ grpc_pollset *pollset, struct grpc_fd **fds,
+ size_t fd_count);
/* Return 1 if the pollset has active threads in grpc_pollset_work (pollset must
* be locked) */
-int grpc_pollset_has_workers (grpc_pollset * pollset);
+int grpc_pollset_has_workers(grpc_pollset *pollset);
/* override to allow tests to hook poll() usage */
-typedef int (*grpc_poll_function_type) (struct pollfd *, nfds_t, int);
+typedef int (*grpc_poll_function_type)(struct pollfd *, nfds_t, int);
extern grpc_poll_function_type grpc_poll_function;
#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_POSIX_H */
diff --git a/src/core/iomgr/pollset_set.h b/src/core/iomgr/pollset_set.h
index a777c6ca23..0fdcba01a4 100644
--- a/src/core/iomgr/pollset_set.h
+++ b/src/core/iomgr/pollset_set.h
@@ -49,9 +49,13 @@
#include "src/core/iomgr/pollset_set_windows.h"
#endif
-void grpc_pollset_set_init (grpc_pollset_set * pollset_set);
-void grpc_pollset_set_destroy (grpc_pollset_set * pollset_set);
-void grpc_pollset_set_add_pollset (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_pollset * pollset);
-void grpc_pollset_set_del_pollset (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_pollset * pollset);
+void grpc_pollset_set_init(grpc_pollset_set* pollset_set);
+void grpc_pollset_set_destroy(grpc_pollset_set* pollset_set);
+void grpc_pollset_set_add_pollset(grpc_exec_ctx* exec_ctx,
+ grpc_pollset_set* pollset_set,
+ grpc_pollset* pollset);
+void grpc_pollset_set_del_pollset(grpc_exec_ctx* exec_ctx,
+ grpc_pollset_set* pollset_set,
+ grpc_pollset* pollset);
#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_H */
diff --git a/src/core/iomgr/pollset_set_posix.c b/src/core/iomgr/pollset_set_posix.c
index cb2e6aede3..c86ed3d5da 100644
--- a/src/core/iomgr/pollset_set_posix.c
+++ b/src/core/iomgr/pollset_set_posix.c
@@ -43,105 +43,93 @@
#include "src/core/iomgr/pollset_set.h"
-void
-grpc_pollset_set_init (grpc_pollset_set * pollset_set)
-{
- memset (pollset_set, 0, sizeof (*pollset_set));
- gpr_mu_init (&pollset_set->mu);
+void grpc_pollset_set_init(grpc_pollset_set *pollset_set) {
+ memset(pollset_set, 0, sizeof(*pollset_set));
+ gpr_mu_init(&pollset_set->mu);
}
-void
-grpc_pollset_set_destroy (grpc_pollset_set * pollset_set)
-{
+void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set) {
size_t i;
- gpr_mu_destroy (&pollset_set->mu);
- for (i = 0; i < pollset_set->fd_count; i++)
- {
- GRPC_FD_UNREF (pollset_set->fds[i], "pollset");
- }
- gpr_free (pollset_set->pollsets);
- gpr_free (pollset_set->fds);
+ gpr_mu_destroy(&pollset_set->mu);
+ for (i = 0; i < pollset_set->fd_count; i++) {
+ GRPC_FD_UNREF(pollset_set->fds[i], "pollset");
+ }
+ gpr_free(pollset_set->pollsets);
+ gpr_free(pollset_set->fds);
}
-void
-grpc_pollset_set_add_pollset (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_pollset * pollset)
-{
+void grpc_pollset_set_add_pollset(grpc_exec_ctx *exec_ctx,
+ grpc_pollset_set *pollset_set,
+ grpc_pollset *pollset) {
size_t i, j;
- gpr_mu_lock (&pollset_set->mu);
- if (pollset_set->pollset_count == pollset_set->pollset_capacity)
- {
- pollset_set->pollset_capacity = GPR_MAX (8, 2 * pollset_set->pollset_capacity);
- pollset_set->pollsets = gpr_realloc (pollset_set->pollsets, pollset_set->pollset_capacity * sizeof (*pollset_set->pollsets));
- }
+ gpr_mu_lock(&pollset_set->mu);
+ if (pollset_set->pollset_count == pollset_set->pollset_capacity) {
+ pollset_set->pollset_capacity =
+ GPR_MAX(8, 2 * pollset_set->pollset_capacity);
+ pollset_set->pollsets =
+ gpr_realloc(pollset_set->pollsets, pollset_set->pollset_capacity *
+ sizeof(*pollset_set->pollsets));
+ }
pollset_set->pollsets[pollset_set->pollset_count++] = pollset;
- for (i = 0, j = 0; i < pollset_set->fd_count; i++)
- {
- if (grpc_fd_is_orphaned (pollset_set->fds[i]))
- {
- GRPC_FD_UNREF (pollset_set->fds[i], "pollset");
- }
- else
- {
- grpc_pollset_add_fd (exec_ctx, pollset, pollset_set->fds[i]);
- pollset_set->fds[j++] = pollset_set->fds[i];
- }
+ for (i = 0, j = 0; i < pollset_set->fd_count; i++) {
+ if (grpc_fd_is_orphaned(pollset_set->fds[i])) {
+ GRPC_FD_UNREF(pollset_set->fds[i], "pollset");
+ } else {
+ grpc_pollset_add_fd(exec_ctx, pollset, pollset_set->fds[i]);
+ pollset_set->fds[j++] = pollset_set->fds[i];
}
+ }
pollset_set->fd_count = j;
- gpr_mu_unlock (&pollset_set->mu);
+ gpr_mu_unlock(&pollset_set->mu);
}
-void
-grpc_pollset_set_del_pollset (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_pollset * pollset)
-{
+void grpc_pollset_set_del_pollset(grpc_exec_ctx *exec_ctx,
+ grpc_pollset_set *pollset_set,
+ grpc_pollset *pollset) {
size_t i;
- gpr_mu_lock (&pollset_set->mu);
- for (i = 0; i < pollset_set->pollset_count; i++)
- {
- if (pollset_set->pollsets[i] == pollset)
- {
- pollset_set->pollset_count--;
- GPR_SWAP (grpc_pollset *, pollset_set->pollsets[i], pollset_set->pollsets[pollset_set->pollset_count]);
- break;
- }
+ gpr_mu_lock(&pollset_set->mu);
+ for (i = 0; i < pollset_set->pollset_count; i++) {
+ if (pollset_set->pollsets[i] == pollset) {
+ pollset_set->pollset_count--;
+ GPR_SWAP(grpc_pollset *, pollset_set->pollsets[i],
+ pollset_set->pollsets[pollset_set->pollset_count]);
+ break;
}
- gpr_mu_unlock (&pollset_set->mu);
+ }
+ gpr_mu_unlock(&pollset_set->mu);
}
-void
-grpc_pollset_set_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_fd * fd)
-{
+void grpc_pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset_set *pollset_set, grpc_fd *fd) {
size_t i;
- gpr_mu_lock (&pollset_set->mu);
- if (pollset_set->fd_count == pollset_set->fd_capacity)
- {
- pollset_set->fd_capacity = GPR_MAX (8, 2 * pollset_set->fd_capacity);
- pollset_set->fds = gpr_realloc (pollset_set->fds, pollset_set->fd_capacity * sizeof (*pollset_set->fds));
- }
- GRPC_FD_REF (fd, "pollset_set");
+ gpr_mu_lock(&pollset_set->mu);
+ if (pollset_set->fd_count == pollset_set->fd_capacity) {
+ pollset_set->fd_capacity = GPR_MAX(8, 2 * pollset_set->fd_capacity);
+ pollset_set->fds = gpr_realloc(
+ pollset_set->fds, pollset_set->fd_capacity * sizeof(*pollset_set->fds));
+ }
+ GRPC_FD_REF(fd, "pollset_set");
pollset_set->fds[pollset_set->fd_count++] = fd;
- for (i = 0; i < pollset_set->pollset_count; i++)
- {
- grpc_pollset_add_fd (exec_ctx, pollset_set->pollsets[i], fd);
- }
- gpr_mu_unlock (&pollset_set->mu);
+ for (i = 0; i < pollset_set->pollset_count; i++) {
+ grpc_pollset_add_fd(exec_ctx, pollset_set->pollsets[i], fd);
+ }
+ gpr_mu_unlock(&pollset_set->mu);
}
-void
-grpc_pollset_set_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_fd * fd)
-{
+void grpc_pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset_set *pollset_set, grpc_fd *fd) {
size_t i;
- gpr_mu_lock (&pollset_set->mu);
- for (i = 0; i < pollset_set->fd_count; i++)
- {
- if (pollset_set->fds[i] == fd)
- {
- pollset_set->fd_count--;
- GPR_SWAP (grpc_fd *, pollset_set->fds[i], pollset_set->fds[pollset_set->fd_count]);
- GRPC_FD_UNREF (fd, "pollset_set");
- break;
- }
+ gpr_mu_lock(&pollset_set->mu);
+ for (i = 0; i < pollset_set->fd_count; i++) {
+ if (pollset_set->fds[i] == fd) {
+ pollset_set->fd_count--;
+ GPR_SWAP(grpc_fd *, pollset_set->fds[i],
+ pollset_set->fds[pollset_set->fd_count]);
+ GRPC_FD_UNREF(fd, "pollset_set");
+ break;
}
- gpr_mu_unlock (&pollset_set->mu);
+ }
+ gpr_mu_unlock(&pollset_set->mu);
}
#endif /* GPR_POSIX_SOCKET */
diff --git a/src/core/iomgr/pollset_set_posix.h b/src/core/iomgr/pollset_set_posix.h
index 6075ae8f86..05234fb642 100644
--- a/src/core/iomgr/pollset_set_posix.h
+++ b/src/core/iomgr/pollset_set_posix.h
@@ -37,8 +37,7 @@
#include "src/core/iomgr/fd_posix.h"
#include "src/core/iomgr/pollset_posix.h"
-typedef struct grpc_pollset_set
-{
+typedef struct grpc_pollset_set {
gpr_mu mu;
size_t pollset_count;
@@ -50,7 +49,9 @@ typedef struct grpc_pollset_set
grpc_fd **fds;
} grpc_pollset_set;
-void grpc_pollset_set_add_fd (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_fd * fd);
-void grpc_pollset_set_del_fd (grpc_exec_ctx * exec_ctx, grpc_pollset_set * pollset_set, grpc_fd * fd);
+void grpc_pollset_set_add_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset_set *pollset_set, grpc_fd *fd);
+void grpc_pollset_set_del_fd(grpc_exec_ctx *exec_ctx,
+ grpc_pollset_set *pollset_set, grpc_fd *fd);
#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_WINDOWS_H */
diff --git a/src/core/iomgr/pollset_set_windows.c b/src/core/iomgr/pollset_set_windows.c
index 642e6fb983..c80dd592b0 100644
--- a/src/core/iomgr/pollset_set_windows.c
+++ b/src/core/iomgr/pollset_set_windows.c
@@ -37,24 +37,14 @@
#include "src/core/iomgr/pollset_set.h"
-void
-grpc_pollset_set_init (grpc_pollset_set * pollset_set)
-{
-}
-
-void
-grpc_pollset_set_destroy (grpc_pollset_set * pollset_set)
-{
-}
-
-void
-grpc_pollset_set_add_pollset (grpc_pollset_set * pollset_set, grpc_pollset * pollset)
-{
-}
-
-void
-grpc_pollset_set_del_pollset (grpc_pollset_set * pollset_set, grpc_pollset * pollset)
-{
-}
+void grpc_pollset_set_init(grpc_pollset_set* pollset_set) {}
+
+void grpc_pollset_set_destroy(grpc_pollset_set* pollset_set) {}
+
+void grpc_pollset_set_add_pollset(grpc_pollset_set* pollset_set,
+ grpc_pollset* pollset) {}
+
+void grpc_pollset_set_del_pollset(grpc_pollset_set* pollset_set,
+ grpc_pollset* pollset) {}
#endif /* GPR_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/pollset_set_windows.h b/src/core/iomgr/pollset_set_windows.h
index 0fb03cd27a..cada0d2b61 100644
--- a/src/core/iomgr/pollset_set_windows.h
+++ b/src/core/iomgr/pollset_set_windows.h
@@ -34,9 +34,6 @@
#ifndef GRPC_INTERNAL_CORE_IOMGR_POLLSET_SET_WINDOWS_H
#define GRPC_INTERNAL_CORE_IOMGR_POLLSET_SET_WINDOWS_H
-typedef struct grpc_pollset_set
-{
- void *unused;
-} grpc_pollset_set;
+typedef struct grpc_pollset_set { void *unused; } grpc_pollset_set;
#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_WINDOWS_H */
diff --git a/src/core/iomgr/pollset_windows.c b/src/core/iomgr/pollset_windows.c
index da0024b921..e6791d4943 100644
--- a/src/core/iomgr/pollset_windows.c
+++ b/src/core/iomgr/pollset_windows.c
@@ -42,45 +42,32 @@
#include "src/core/iomgr/pollset.h"
#include "src/core/iomgr/pollset_windows.h"
-static void
-remove_worker (grpc_pollset * p, grpc_pollset_worker * worker)
-{
+static void remove_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
worker->prev->next = worker->next;
worker->next->prev = worker->prev;
}
-static int
-has_workers (grpc_pollset * p)
-{
+static int has_workers(grpc_pollset *p) {
return p->root_worker.next != &p->root_worker;
}
-static grpc_pollset_worker *
-pop_front_worker (grpc_pollset * p)
-{
- if (has_workers (p))
- {
- grpc_pollset_worker *w = p->root_worker.next;
- remove_worker (p, w);
- return w;
- }
- else
- {
- return NULL;
- }
+static grpc_pollset_worker *pop_front_worker(grpc_pollset *p) {
+ if (has_workers(p)) {
+ grpc_pollset_worker *w = p->root_worker.next;
+ remove_worker(p, w);
+ return w;
+ } else {
+ return NULL;
+ }
}
-static void
-push_back_worker (grpc_pollset * p, grpc_pollset_worker * worker)
-{
+static void push_back_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
worker->next = &p->root_worker;
worker->prev = worker->next->prev;
worker->prev->next = worker->next->prev = worker;
}
-static void
-push_front_worker (grpc_pollset * p, grpc_pollset_worker * worker)
-{
+static void push_front_worker(grpc_pollset *p, grpc_pollset_worker *worker) {
worker->prev = &p->root_worker;
worker->next = worker->prev->next;
worker->prev->next = worker->next->prev = worker;
@@ -91,94 +78,73 @@ push_front_worker (grpc_pollset * p, grpc_pollset_worker * worker)
set of features for the sake of the rest of grpc. But grpc_pollset_work
won't actually do any polling, and return as quickly as possible. */
-void
-grpc_pollset_init (grpc_pollset * pollset)
-{
- memset (pollset, 0, sizeof (*pollset));
- gpr_mu_init (&pollset->mu);
+void grpc_pollset_init(grpc_pollset *pollset) {
+ memset(pollset, 0, sizeof(*pollset));
+ gpr_mu_init(&pollset->mu);
pollset->root_worker.next = pollset->root_worker.prev = &pollset->root_worker;
pollset->kicked_without_pollers = 0;
}
-void
-grpc_pollset_shutdown (grpc_pollset * pollset, void (*shutdown_done) (void *arg), void *shutdown_done_arg)
-{
- gpr_mu_lock (&pollset->mu);
+void grpc_pollset_shutdown(grpc_pollset *pollset,
+ void (*shutdown_done)(void *arg),
+ void *shutdown_done_arg) {
+ gpr_mu_lock(&pollset->mu);
pollset->shutting_down = 1;
- grpc_pollset_kick (pollset, GRPC_POLLSET_KICK_BROADCAST);
- gpr_mu_unlock (&pollset->mu);
- shutdown_done (shutdown_done_arg);
+ grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
+ gpr_mu_unlock(&pollset->mu);
+ shutdown_done(shutdown_done_arg);
}
-void
-grpc_pollset_destroy (grpc_pollset * pollset)
-{
- gpr_mu_destroy (&pollset->mu);
+void grpc_pollset_destroy(grpc_pollset *pollset) {
+ gpr_mu_destroy(&pollset->mu);
}
-void
-grpc_pollset_work (grpc_pollset * pollset, grpc_pollset_worker * worker, gpr_timespec now, gpr_timespec deadline)
-{
+void grpc_pollset_work(grpc_pollset *pollset, grpc_pollset_worker *worker,
+ gpr_timespec now, gpr_timespec deadline) {
int added_worker = 0;
worker->next = worker->prev = NULL;
- gpr_cv_init (&worker->cv);
- if (grpc_maybe_call_delayed_callbacks (&pollset->mu, 1 /* GPR_TRUE */ ))
- {
- goto done;
- }
- if (grpc_alarm_check (&pollset->mu, now, &deadline))
- {
- goto done;
- }
- if (!pollset->kicked_without_pollers && !pollset->shutting_down)
- {
- push_front_worker (pollset, worker);
- added_worker = 1;
- gpr_cv_wait (&worker->cv, &pollset->mu, deadline);
- }
- else
- {
- pollset->kicked_without_pollers = 0;
- }
+ gpr_cv_init(&worker->cv);
+ if (grpc_maybe_call_delayed_callbacks(&pollset->mu, 1 /* GPR_TRUE */)) {
+ goto done;
+ }
+ if (grpc_alarm_check(&pollset->mu, now, &deadline)) {
+ goto done;
+ }
+ if (!pollset->kicked_without_pollers && !pollset->shutting_down) {
+ push_front_worker(pollset, worker);
+ added_worker = 1;
+ gpr_cv_wait(&worker->cv, &pollset->mu, deadline);
+ } else {
+ pollset->kicked_without_pollers = 0;
+ }
done:
- gpr_cv_destroy (&worker->cv);
- if (added_worker)
- {
- remove_worker (pollset, worker);
- }
+ gpr_cv_destroy(&worker->cv);
+ if (added_worker) {
+ remove_worker(pollset, worker);
+ }
}
-void
-grpc_pollset_kick (grpc_pollset * p, grpc_pollset_worker * specific_worker)
-{
- if (specific_worker != NULL)
- {
- if (specific_worker == GRPC_POLLSET_KICK_BROADCAST)
- {
- for (specific_worker = p->root_worker.next; specific_worker != &p->root_worker; specific_worker = specific_worker->next)
- {
- gpr_cv_signal (&specific_worker->cv);
- }
- p->kicked_without_pollers = 1;
- }
- else
- {
- gpr_cv_signal (&specific_worker->cv);
- }
+void grpc_pollset_kick(grpc_pollset *p, grpc_pollset_worker *specific_worker) {
+ if (specific_worker != NULL) {
+ if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
+ for (specific_worker = p->root_worker.next;
+ specific_worker != &p->root_worker;
+ specific_worker = specific_worker->next) {
+ gpr_cv_signal(&specific_worker->cv);
+ }
+ p->kicked_without_pollers = 1;
+ } else {
+ gpr_cv_signal(&specific_worker->cv);
}
- else
- {
- specific_worker = pop_front_worker (p);
- if (specific_worker != NULL)
- {
- push_back_worker (p, specific_worker);
- gpr_cv_signal (&specific_worker->cv);
- }
- else
- {
- p->kicked_without_pollers = 1;
- }
+ } else {
+ specific_worker = pop_front_worker(p);
+ if (specific_worker != NULL) {
+ push_back_worker(p, specific_worker);
+ gpr_cv_signal(&specific_worker->cv);
+ } else {
+ p->kicked_without_pollers = 1;
}
+ }
}
#endif /* GPR_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/pollset_windows.h b/src/core/iomgr/pollset_windows.h
index 6da7415709..4efa5a1717 100644
--- a/src/core/iomgr/pollset_windows.h
+++ b/src/core/iomgr/pollset_windows.h
@@ -43,15 +43,13 @@
used to synchronize with the IOCP, and workers are condition variables
used to block threads until work is ready. */
-typedef struct grpc_pollset_worker
-{
+typedef struct grpc_pollset_worker {
gpr_cv cv;
struct grpc_pollset_worker *next;
struct grpc_pollset_worker *prev;
} grpc_pollset_worker;
-typedef struct grpc_pollset
-{
+typedef struct grpc_pollset {
gpr_mu mu;
int shutting_down;
int kicked_without_pollers;
diff --git a/src/core/iomgr/resolve_address.h b/src/core/iomgr/resolve_address.h
index aeee82584a..01eedffa88 100644
--- a/src/core/iomgr/resolve_address.h
+++ b/src/core/iomgr/resolve_address.h
@@ -40,14 +40,12 @@
#define GRPC_MAX_SOCKADDR_SIZE 128
-typedef struct
-{
+typedef struct {
char addr[GRPC_MAX_SOCKADDR_SIZE];
size_t len;
} grpc_resolved_address;
-typedef struct
-{
+typedef struct {
size_t naddrs;
grpc_resolved_address *addrs;
} grpc_resolved_addresses;
@@ -56,16 +54,19 @@ typedef struct
On success: addresses is the result, and the callee must call
grpc_resolved_addresses_destroy when it's done with them
On failure: addresses is NULL */
-typedef void (*grpc_resolve_cb) (grpc_exec_ctx * exec_ctx, void *arg, grpc_resolved_addresses * addresses);
+typedef void (*grpc_resolve_cb)(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_resolved_addresses *addresses);
/* Asynchronously resolve addr. Use default_port if a port isn't designated
in addr, otherwise use the port in addr. */
/* TODO(ctiller): add a timeout here */
-void grpc_resolve_address (const char *addr, const char *default_port, grpc_resolve_cb cb, void *arg);
+void grpc_resolve_address(const char *addr, const char *default_port,
+ grpc_resolve_cb cb, void *arg);
/* Destroy resolved addresses */
-void grpc_resolved_addresses_destroy (grpc_resolved_addresses * addresses);
+void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addresses);
/* Resolve addr in a blocking fashion. Returns NULL on failure. On success,
result must be freed with grpc_resolved_addresses_destroy. */
-grpc_resolved_addresses *grpc_blocking_resolve_address (const char *addr, const char *default_port);
+grpc_resolved_addresses *grpc_blocking_resolve_address(
+ const char *addr, const char *default_port);
#endif /* GRPC_INTERNAL_CORE_IOMGR_RESOLVE_ADDRESS_H */
diff --git a/src/core/iomgr/resolve_address_posix.c b/src/core/iomgr/resolve_address_posix.c
index 1017a788ff..aacd185548 100644
--- a/src/core/iomgr/resolve_address_posix.c
+++ b/src/core/iomgr/resolve_address_posix.c
@@ -51,8 +51,7 @@
#include <grpc/support/thd.h>
#include <grpc/support/time.h>
-typedef struct
-{
+typedef struct {
char *name;
char *default_port;
grpc_resolve_cb cb;
@@ -60,9 +59,8 @@ typedef struct
grpc_iomgr_object iomgr_object;
} request;
-grpc_resolved_addresses *
-grpc_blocking_resolve_address (const char *name, const char *default_port)
-{
+grpc_resolved_addresses *grpc_blocking_resolve_address(
+ const char *name, const char *default_port) {
struct addrinfo hints;
struct addrinfo *result = NULL, *resp;
char *host;
@@ -72,127 +70,114 @@ grpc_blocking_resolve_address (const char *name, const char *default_port)
grpc_resolved_addresses *addrs = NULL;
struct sockaddr_un *un;
- if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' && name[4] == ':' && name[5] != 0)
- {
- addrs = gpr_malloc (sizeof (grpc_resolved_addresses));
- addrs->naddrs = 1;
- addrs->addrs = gpr_malloc (sizeof (grpc_resolved_address));
- un = (struct sockaddr_un *) addrs->addrs->addr;
- un->sun_family = AF_UNIX;
- strcpy (un->sun_path, name + 5);
- addrs->addrs->len = strlen (un->sun_path) + sizeof (un->sun_family) + 1;
- return addrs;
- }
+ if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
+ name[4] == ':' && name[5] != 0) {
+ addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
+ addrs->naddrs = 1;
+ addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address));
+ un = (struct sockaddr_un *)addrs->addrs->addr;
+ un->sun_family = AF_UNIX;
+ strcpy(un->sun_path, name + 5);
+ addrs->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
+ return addrs;
+ }
/* parse name, splitting it into host and port parts */
- gpr_split_host_port (name, &host, &port);
- if (host == NULL)
- {
- gpr_log (GPR_ERROR, "unparseable host:port: '%s'", name);
+ gpr_split_host_port(name, &host, &port);
+ if (host == NULL) {
+ gpr_log(GPR_ERROR, "unparseable host:port: '%s'", name);
+ goto done;
+ }
+ if (port == NULL) {
+ if (default_port == NULL) {
+ gpr_log(GPR_ERROR, "no port in name '%s'", name);
goto done;
}
- if (port == NULL)
- {
- if (default_port == NULL)
- {
- gpr_log (GPR_ERROR, "no port in name '%s'", name);
- goto done;
- }
- port = gpr_strdup (default_port);
- }
+ port = gpr_strdup(default_port);
+ }
/* Call getaddrinfo */
- memset (&hints, 0, sizeof (hints));
- hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
- hints.ai_socktype = SOCK_STREAM; /* stream socket */
- hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
-
- s = getaddrinfo (host, port, &hints, &result);
- if (s != 0)
- {
- /* Retry if well-known service name is recognized */
- char *svc[][2] = { {"http", "80"}, {"https", "443"} };
- int i;
- for (i = 0; i < (int) (sizeof (svc) / sizeof (svc[0])); i++)
- {
- if (strcmp (port, svc[i][0]) == 0)
- {
- s = getaddrinfo (host, svc[i][1], &hints, &result);
- break;
- }
- }
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
+ hints.ai_socktype = SOCK_STREAM; /* stream socket */
+ hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
+
+ s = getaddrinfo(host, port, &hints, &result);
+ if (s != 0) {
+ /* Retry if well-known service name is recognized */
+ char *svc[][2] = {{"http", "80"}, {"https", "443"}};
+ int i;
+ for (i = 0; i < (int)(sizeof(svc) / sizeof(svc[0])); i++) {
+ if (strcmp(port, svc[i][0]) == 0) {
+ s = getaddrinfo(host, svc[i][1], &hints, &result);
+ break;
+ }
}
+ }
- if (s != 0)
- {
- gpr_log (GPR_ERROR, "getaddrinfo: %s", gai_strerror (s));
- goto done;
- }
+ if (s != 0) {
+ gpr_log(GPR_ERROR, "getaddrinfo: %s", gai_strerror(s));
+ goto done;
+ }
/* Success path: set addrs non-NULL, fill it in */
- addrs = gpr_malloc (sizeof (grpc_resolved_addresses));
+ addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
addrs->naddrs = 0;
- for (resp = result; resp != NULL; resp = resp->ai_next)
- {
- addrs->naddrs++;
- }
- addrs->addrs = gpr_malloc (sizeof (grpc_resolved_address) * addrs->naddrs);
+ for (resp = result; resp != NULL; resp = resp->ai_next) {
+ addrs->naddrs++;
+ }
+ addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address) * addrs->naddrs);
i = 0;
- for (resp = result; resp != NULL; resp = resp->ai_next)
- {
- memcpy (&addrs->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
- addrs->addrs[i].len = resp->ai_addrlen;
- i++;
- }
+ for (resp = result; resp != NULL; resp = resp->ai_next) {
+ memcpy(&addrs->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
+ addrs->addrs[i].len = resp->ai_addrlen;
+ i++;
+ }
done:
- gpr_free (host);
- gpr_free (port);
- if (result)
- {
- freeaddrinfo (result);
- }
+ gpr_free(host);
+ gpr_free(port);
+ if (result) {
+ freeaddrinfo(result);
+ }
return addrs;
}
/* Thread function to asynch-ify grpc_blocking_resolve_address */
-static void
-do_request_thread (void *rp)
-{
+static void do_request_thread(void *rp) {
request *r = rp;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_resolved_addresses *resolved = grpc_blocking_resolve_address (r->name, r->default_port);
+ grpc_resolved_addresses *resolved =
+ grpc_blocking_resolve_address(r->name, r->default_port);
void *arg = r->arg;
grpc_resolve_cb cb = r->cb;
- gpr_free (r->name);
- gpr_free (r->default_port);
- cb (&exec_ctx, arg, resolved);
- grpc_iomgr_unregister_object (&r->iomgr_object);
- gpr_free (r);
- grpc_exec_ctx_finish (&exec_ctx);
+ gpr_free(r->name);
+ gpr_free(r->default_port);
+ cb(&exec_ctx, arg, resolved);
+ grpc_iomgr_unregister_object(&r->iomgr_object);
+ gpr_free(r);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-void
-grpc_resolved_addresses_destroy (grpc_resolved_addresses * addrs)
-{
- gpr_free (addrs->addrs);
- gpr_free (addrs);
+void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addrs) {
+ gpr_free(addrs->addrs);
+ gpr_free(addrs);
}
-void
-grpc_resolve_address (const char *name, const char *default_port, grpc_resolve_cb cb, void *arg)
-{
- request *r = gpr_malloc (sizeof (request));
+void grpc_resolve_address(const char *name, const char *default_port,
+ grpc_resolve_cb cb, void *arg) {
+ request *r = gpr_malloc(sizeof(request));
gpr_thd_id id;
char *tmp;
- gpr_asprintf (&tmp, "resolve_address:name='%s':default_port='%s'", name, default_port);
- grpc_iomgr_register_object (&r->iomgr_object, tmp);
- gpr_free (tmp);
- r->name = gpr_strdup (name);
- r->default_port = gpr_strdup (default_port);
+ gpr_asprintf(&tmp, "resolve_address:name='%s':default_port='%s'", name,
+ default_port);
+ grpc_iomgr_register_object(&r->iomgr_object, tmp);
+ gpr_free(tmp);
+ r->name = gpr_strdup(name);
+ r->default_port = gpr_strdup(default_port);
r->cb = cb;
r->arg = arg;
- gpr_thd_new (&id, do_request_thread, r, NULL);
+ gpr_thd_new(&id, do_request_thread, r, NULL);
}
#endif
diff --git a/src/core/iomgr/resolve_address_windows.c b/src/core/iomgr/resolve_address_windows.c
index 4a85a0a7ad..fb5fd0d4f6 100644
--- a/src/core/iomgr/resolve_address_windows.c
+++ b/src/core/iomgr/resolve_address_windows.c
@@ -50,8 +50,7 @@
#include <grpc/support/thd.h>
#include <grpc/support/time.h>
-typedef struct
-{
+typedef struct {
char *name;
char *default_port;
grpc_resolve_cb cb;
@@ -59,9 +58,8 @@ typedef struct
grpc_iomgr_object iomgr_object;
} request;
-grpc_resolved_addresses *
-grpc_blocking_resolve_address (const char *name, const char *default_port)
-{
+grpc_resolved_addresses *grpc_blocking_resolve_address(
+ const char *name, const char *default_port) {
struct addrinfo hints;
struct addrinfo *result = NULL, *resp;
char *host;
@@ -71,106 +69,95 @@ grpc_blocking_resolve_address (const char *name, const char *default_port)
grpc_resolved_addresses *addrs = NULL;
/* parse name, splitting it into host and port parts */
- gpr_split_host_port (name, &host, &port);
- if (host == NULL)
- {
- gpr_log (GPR_ERROR, "unparseable host:port: '%s'", name);
+ gpr_split_host_port(name, &host, &port);
+ if (host == NULL) {
+ gpr_log(GPR_ERROR, "unparseable host:port: '%s'", name);
+ goto done;
+ }
+ if (port == NULL) {
+ if (default_port == NULL) {
+ gpr_log(GPR_ERROR, "no port in name '%s'", name);
goto done;
}
- if (port == NULL)
- {
- if (default_port == NULL)
- {
- gpr_log (GPR_ERROR, "no port in name '%s'", name);
- goto done;
- }
- port = gpr_strdup (default_port);
- }
+ port = gpr_strdup(default_port);
+ }
/* Call getaddrinfo */
- memset (&hints, 0, sizeof (hints));
- hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
- hints.ai_socktype = SOCK_STREAM; /* stream socket */
- hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
-
- s = getaddrinfo (host, port, &hints, &result);
- if (s != 0)
- {
- gpr_log (GPR_ERROR, "getaddrinfo: %s", gai_strerror (s));
- goto done;
- }
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
+ hints.ai_socktype = SOCK_STREAM; /* stream socket */
+ hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */
+
+ s = getaddrinfo(host, port, &hints, &result);
+ if (s != 0) {
+ gpr_log(GPR_ERROR, "getaddrinfo: %s", gai_strerror(s));
+ goto done;
+ }
/* Success path: set addrs non-NULL, fill it in */
- addrs = gpr_malloc (sizeof (grpc_resolved_addresses));
+ addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
addrs->naddrs = 0;
- for (resp = result; resp != NULL; resp = resp->ai_next)
- {
- addrs->naddrs++;
- }
- addrs->addrs = gpr_malloc (sizeof (grpc_resolved_address) * addrs->naddrs);
+ for (resp = result; resp != NULL; resp = resp->ai_next) {
+ addrs->naddrs++;
+ }
+ addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address) * addrs->naddrs);
i = 0;
- for (resp = result; resp != NULL; resp = resp->ai_next)
- {
- memcpy (&addrs->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
- addrs->addrs[i].len = resp->ai_addrlen;
- i++;
- }
+ for (resp = result; resp != NULL; resp = resp->ai_next) {
+ memcpy(&addrs->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
+ addrs->addrs[i].len = resp->ai_addrlen;
+ i++;
+ }
{
- for (i = 0; i < addrs->naddrs; i++)
- {
- char *buf;
- grpc_sockaddr_to_string (&buf, (struct sockaddr *) &addrs->addrs[i].addr, 0);
- gpr_free (buf);
- }
+ for (i = 0; i < addrs->naddrs; i++) {
+ char *buf;
+ grpc_sockaddr_to_string(&buf, (struct sockaddr *)&addrs->addrs[i].addr,
+ 0);
+ gpr_free(buf);
+ }
}
done:
- gpr_free (host);
- gpr_free (port);
- if (result)
- {
- freeaddrinfo (result);
- }
+ gpr_free(host);
+ gpr_free(port);
+ if (result) {
+ freeaddrinfo(result);
+ }
return addrs;
}
/* Thread function to asynch-ify grpc_blocking_resolve_address */
-static void
-do_request (void *rp)
-{
+static void do_request(void *rp) {
request *r = rp;
- grpc_resolved_addresses *resolved = grpc_blocking_resolve_address (r->name, r->default_port);
+ grpc_resolved_addresses *resolved =
+ grpc_blocking_resolve_address(r->name, r->default_port);
void *arg = r->arg;
grpc_resolve_cb cb = r->cb;
- gpr_free (r->name);
- gpr_free (r->default_port);
- grpc_iomgr_unregister_object (&r->iomgr_object);
- gpr_free (r);
- cb (arg, resolved);
+ gpr_free(r->name);
+ gpr_free(r->default_port);
+ grpc_iomgr_unregister_object(&r->iomgr_object);
+ gpr_free(r);
+ cb(arg, resolved);
}
-void
-grpc_resolved_addresses_destroy (grpc_resolved_addresses * addrs)
-{
- gpr_free (addrs->addrs);
- gpr_free (addrs);
+void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addrs) {
+ gpr_free(addrs->addrs);
+ gpr_free(addrs);
}
-void
-grpc_resolve_address (const char *name, const char *default_port, grpc_resolve_cb cb, void *arg)
-{
- request *r = gpr_malloc (sizeof (request));
+void grpc_resolve_address(const char *name, const char *default_port,
+ grpc_resolve_cb cb, void *arg) {
+ request *r = gpr_malloc(sizeof(request));
gpr_thd_id id;
const char *label;
- gpr_asprintf (&label, "resolve:%s", name);
- grpc_iomgr_register_object (&r->iomgr_object, label);
- gpr_free (label);
- r->name = gpr_strdup (name);
- r->default_port = gpr_strdup (default_port);
+ gpr_asprintf(&label, "resolve:%s", name);
+ grpc_iomgr_register_object(&r->iomgr_object, label);
+ gpr_free(label);
+ r->name = gpr_strdup(name);
+ r->default_port = gpr_strdup(default_port);
r->cb = cb;
r->arg = arg;
- gpr_thd_new (&id, do_request, r, NULL);
+ gpr_thd_new(&id, do_request, r, NULL);
}
#endif
diff --git a/src/core/iomgr/sockaddr_utils.c b/src/core/iomgr/sockaddr_utils.c
index d55d26ff04..0e4bf24549 100644
--- a/src/core/iomgr/sockaddr_utils.c
+++ b/src/core/iomgr/sockaddr_utils.c
@@ -48,119 +48,96 @@
#include "src/core/support/string.h"
-static const gpr_uint8 kV4MappedPrefix[] = { 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0xff, 0xff
-};
-
-int
-grpc_sockaddr_is_v4mapped (const struct sockaddr *addr, struct sockaddr_in *addr4_out)
-{
- GPR_ASSERT (addr != (struct sockaddr *) addr4_out);
- if (addr->sa_family == AF_INET6)
- {
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
- if (memcmp (addr6->sin6_addr.s6_addr, kV4MappedPrefix, sizeof (kV4MappedPrefix)) == 0)
- {
- if (addr4_out != NULL)
- {
- /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
- memset (addr4_out, 0, sizeof (*addr4_out));
- addr4_out->sin_family = AF_INET;
- /* s6_addr32 would be nice, but it's non-standard. */
- memcpy (&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4);
- addr4_out->sin_port = addr6->sin6_port;
- }
- return 1;
- }
+static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0xff, 0xff};
+
+int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
+ struct sockaddr_in *addr4_out) {
+ GPR_ASSERT(addr != (struct sockaddr *)addr4_out);
+ if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
+ if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix,
+ sizeof(kV4MappedPrefix)) == 0) {
+ if (addr4_out != NULL) {
+ /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
+ memset(addr4_out, 0, sizeof(*addr4_out));
+ addr4_out->sin_family = AF_INET;
+ /* s6_addr32 would be nice, but it's non-standard. */
+ memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4);
+ addr4_out->sin_port = addr6->sin6_port;
+ }
+ return 1;
}
+ }
return 0;
}
-int
-grpc_sockaddr_to_v4mapped (const struct sockaddr *addr, struct sockaddr_in6 *addr6_out)
-{
- GPR_ASSERT (addr != (struct sockaddr *) addr6_out);
- if (addr->sa_family == AF_INET)
- {
- const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
- memset (addr6_out, 0, sizeof (*addr6_out));
- addr6_out->sin6_family = AF_INET6;
- memcpy (&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
- memcpy (&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
- addr6_out->sin6_port = addr4->sin_port;
- return 1;
- }
+int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
+ struct sockaddr_in6 *addr6_out) {
+ GPR_ASSERT(addr != (struct sockaddr *)addr6_out);
+ if (addr->sa_family == AF_INET) {
+ const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
+ memset(addr6_out, 0, sizeof(*addr6_out));
+ addr6_out->sin6_family = AF_INET6;
+ memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
+ memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
+ addr6_out->sin6_port = addr4->sin_port;
+ return 1;
+ }
return 0;
}
-int
-grpc_sockaddr_is_wildcard (const struct sockaddr *addr, int *port_out)
-{
+int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) {
struct sockaddr_in addr4_normalized;
- if (grpc_sockaddr_is_v4mapped (addr, &addr4_normalized))
- {
- addr = (struct sockaddr *) &addr4_normalized;
- }
- if (addr->sa_family == AF_INET)
- {
- /* Check for 0.0.0.0 */
- const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
- if (addr4->sin_addr.s_addr != 0)
- {
- return 0;
- }
- *port_out = ntohs (addr4->sin_port);
- return 1;
- }
- else if (addr->sa_family == AF_INET6)
- {
- /* Check for :: */
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
- int i;
- for (i = 0; i < 16; i++)
- {
- if (addr6->sin6_addr.s6_addr[i] != 0)
- {
- return 0;
- }
- }
- *port_out = ntohs (addr6->sin6_port);
- return 1;
- }
- else
- {
+ if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) {
+ addr = (struct sockaddr *)&addr4_normalized;
+ }
+ if (addr->sa_family == AF_INET) {
+ /* Check for 0.0.0.0 */
+ const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
+ if (addr4->sin_addr.s_addr != 0) {
return 0;
}
+ *port_out = ntohs(addr4->sin_port);
+ return 1;
+ } else if (addr->sa_family == AF_INET6) {
+ /* Check for :: */
+ const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
+ int i;
+ for (i = 0; i < 16; i++) {
+ if (addr6->sin6_addr.s6_addr[i] != 0) {
+ return 0;
+ }
+ }
+ *port_out = ntohs(addr6->sin6_port);
+ return 1;
+ } else {
+ return 0;
+ }
}
-void
-grpc_sockaddr_make_wildcards (int port, struct sockaddr_in *wild4_out, struct sockaddr_in6 *wild6_out)
-{
- grpc_sockaddr_make_wildcard4 (port, wild4_out);
- grpc_sockaddr_make_wildcard6 (port, wild6_out);
+void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
+ struct sockaddr_in6 *wild6_out) {
+ grpc_sockaddr_make_wildcard4(port, wild4_out);
+ grpc_sockaddr_make_wildcard6(port, wild6_out);
}
-void
-grpc_sockaddr_make_wildcard4 (int port, struct sockaddr_in *wild_out)
-{
- GPR_ASSERT (port >= 0 && port < 65536);
- memset (wild_out, 0, sizeof (*wild_out));
+void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) {
+ GPR_ASSERT(port >= 0 && port < 65536);
+ memset(wild_out, 0, sizeof(*wild_out));
wild_out->sin_family = AF_INET;
- wild_out->sin_port = htons ((gpr_uint16) port);
+ wild_out->sin_port = htons((gpr_uint16)port);
}
-void
-grpc_sockaddr_make_wildcard6 (int port, struct sockaddr_in6 *wild_out)
-{
- GPR_ASSERT (port >= 0 && port < 65536);
- memset (wild_out, 0, sizeof (*wild_out));
+void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) {
+ GPR_ASSERT(port >= 0 && port < 65536);
+ memset(wild_out, 0, sizeof(*wild_out));
wild_out->sin6_family = AF_INET6;
- wild_out->sin6_port = htons ((gpr_uint16) port);
+ wild_out->sin6_port = htons((gpr_uint16)port);
}
-int
-grpc_sockaddr_to_string (char **out, const struct sockaddr *addr, int normalize)
-{
+int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
+ int normalize) {
const int save_errno = errno;
struct sockaddr_in addr_normalized;
char ntop_buf[INET6_ADDRSTRLEN];
@@ -169,101 +146,87 @@ grpc_sockaddr_to_string (char **out, const struct sockaddr *addr, int normalize)
int ret;
*out = NULL;
- if (normalize && grpc_sockaddr_is_v4mapped (addr, &addr_normalized))
- {
- addr = (const struct sockaddr *) &addr_normalized;
- }
- if (addr->sa_family == AF_INET)
- {
- const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
- ip = &addr4->sin_addr;
- port = ntohs (addr4->sin_port);
- }
- else if (addr->sa_family == AF_INET6)
- {
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
- ip = &addr6->sin6_addr;
- port = ntohs (addr6->sin6_port);
- }
- if (ip != NULL && inet_ntop (addr->sa_family, ip, ntop_buf, sizeof (ntop_buf)) != NULL)
- {
- ret = gpr_join_host_port (out, ntop_buf, port);
- }
- else
- {
- ret = gpr_asprintf (out, "(sockaddr family=%d)", addr->sa_family);
- }
+ if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
+ addr = (const struct sockaddr *)&addr_normalized;
+ }
+ if (addr->sa_family == AF_INET) {
+ const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
+ ip = &addr4->sin_addr;
+ port = ntohs(addr4->sin_port);
+ } else if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
+ ip = &addr6->sin6_addr;
+ port = ntohs(addr6->sin6_port);
+ }
+ if (ip != NULL &&
+ inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) {
+ ret = gpr_join_host_port(out, ntop_buf, port);
+ } else {
+ ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family);
+ }
/* This is probably redundant, but we wouldn't want to log the wrong error. */
errno = save_errno;
return ret;
}
-char *
-grpc_sockaddr_to_uri (const struct sockaddr *addr)
-{
+char *grpc_sockaddr_to_uri(const struct sockaddr *addr) {
char *temp;
char *result;
struct sockaddr_in addr_normalized;
- if (grpc_sockaddr_is_v4mapped (addr, &addr_normalized))
- {
- addr = (const struct sockaddr *) &addr_normalized;
- }
+ if (grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
+ addr = (const struct sockaddr *)&addr_normalized;
+ }
- switch (addr->sa_family)
- {
+ switch (addr->sa_family) {
case AF_INET:
- grpc_sockaddr_to_string (&temp, addr, 0);
- gpr_asprintf (&result, "ipv4:%s", temp);
- gpr_free (temp);
+ grpc_sockaddr_to_string(&temp, addr, 0);
+ gpr_asprintf(&result, "ipv4:%s", temp);
+ gpr_free(temp);
return result;
case AF_INET6:
- grpc_sockaddr_to_string (&temp, addr, 0);
- gpr_asprintf (&result, "ipv6:%s", temp);
- gpr_free (temp);
+ grpc_sockaddr_to_string(&temp, addr, 0);
+ gpr_asprintf(&result, "ipv6:%s", temp);
+ gpr_free(temp);
return result;
#ifdef GPR_POSIX_SOCKET
case AF_UNIX:
- gpr_asprintf (&result, "unix:%s", ((struct sockaddr_un *) addr)->sun_path);
+ gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path);
return result;
#endif
- }
+ }
return NULL;
}
-int
-grpc_sockaddr_get_port (const struct sockaddr *addr)
-{
- switch (addr->sa_family)
- {
+int grpc_sockaddr_get_port(const struct sockaddr *addr) {
+ switch (addr->sa_family) {
case AF_INET:
- return ntohs (((struct sockaddr_in *) addr)->sin_port);
+ return ntohs(((struct sockaddr_in *)addr)->sin_port);
case AF_INET6:
- return ntohs (((struct sockaddr_in6 *) addr)->sin6_port);
+ return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
case AF_UNIX:
return 1;
default:
- gpr_log (GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", addr->sa_family);
+ gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port",
+ addr->sa_family);
return 0;
- }
+ }
}
-int
-grpc_sockaddr_set_port (const struct sockaddr *addr, int port)
-{
- switch (addr->sa_family)
- {
+int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
+ switch (addr->sa_family) {
case AF_INET:
- GPR_ASSERT (port >= 0 && port < 65536);
- ((struct sockaddr_in *) addr)->sin_port = htons ((gpr_uint16) port);
+ GPR_ASSERT(port >= 0 && port < 65536);
+ ((struct sockaddr_in *)addr)->sin_port = htons((gpr_uint16)port);
return 1;
case AF_INET6:
- GPR_ASSERT (port >= 0 && port < 65536);
- ((struct sockaddr_in6 *) addr)->sin6_port = htons ((gpr_uint16) port);
+ GPR_ASSERT(port >= 0 && port < 65536);
+ ((struct sockaddr_in6 *)addr)->sin6_port = htons((gpr_uint16)port);
return 1;
default:
- gpr_log (GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", addr->sa_family);
+ gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port",
+ addr->sa_family);
return 0;
- }
+ }
}
diff --git a/src/core/iomgr/sockaddr_utils.h b/src/core/iomgr/sockaddr_utils.h
index bfbe37a28b..6f7a279900 100644
--- a/src/core/iomgr/sockaddr_utils.h
+++ b/src/core/iomgr/sockaddr_utils.h
@@ -41,30 +41,33 @@
If addr4_out is non-NULL, the inner IPv4 address will be copied here when
returning true. */
-int grpc_sockaddr_is_v4mapped (const struct sockaddr *addr, struct sockaddr_in *addr4_out);
+int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
+ struct sockaddr_in *addr4_out);
/* If addr is an AF_INET address, writes the corresponding ::ffff:0.0.0.0/96
address to addr6_out and returns true. Otherwise returns false. */
-int grpc_sockaddr_to_v4mapped (const struct sockaddr *addr, struct sockaddr_in6 *addr6_out);
+int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
+ struct sockaddr_in6 *addr6_out);
/* If addr is ::, 0.0.0.0, or ::ffff:0.0.0.0, writes the port number to
*port_out (if not NULL) and returns true, otherwise returns false. */
-int grpc_sockaddr_is_wildcard (const struct sockaddr *addr, int *port_out);
+int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out);
/* Writes 0.0.0.0:port and [::]:port to separate sockaddrs. */
-void grpc_sockaddr_make_wildcards (int port, struct sockaddr_in *wild4_out, struct sockaddr_in6 *wild6_out);
+void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
+ struct sockaddr_in6 *wild6_out);
/* Writes 0.0.0.0:port. */
-void grpc_sockaddr_make_wildcard4 (int port, struct sockaddr_in *wild_out);
+void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out);
/* Writes [::]:port. */
-void grpc_sockaddr_make_wildcard6 (int port, struct sockaddr_in6 *wild_out);
+void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out);
/* Return the IP port number of a sockaddr */
-int grpc_sockaddr_get_port (const struct sockaddr *addr);
+int grpc_sockaddr_get_port(const struct sockaddr *addr);
/* Set IP port number of a sockaddr */
-int grpc_sockaddr_set_port (const struct sockaddr *addr, int port);
+int grpc_sockaddr_set_port(const struct sockaddr *addr, int port);
/* Converts a sockaddr into a newly-allocated human-readable string.
@@ -78,8 +81,9 @@ int grpc_sockaddr_set_port (const struct sockaddr *addr, int port);
In the unlikely event of an error, returns -1 and sets *out to NULL.
The existing value of errno is always preserved. */
-int grpc_sockaddr_to_string (char **out, const struct sockaddr *addr, int normalize);
+int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
+ int normalize);
-char *grpc_sockaddr_to_uri (const struct sockaddr *addr);
+char *grpc_sockaddr_to_uri(const struct sockaddr *addr);
#endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKADDR_UTILS_H */
diff --git a/src/core/iomgr/sockaddr_win32.h b/src/core/iomgr/sockaddr_win32.h
index a5bcf9146f..fe2be99145 100644
--- a/src/core/iomgr/sockaddr_win32.h
+++ b/src/core/iomgr/sockaddr_win32.h
@@ -40,7 +40,7 @@
#ifdef __MINGW32__
/* mingw seems to be missing that definition. */
-const char *inet_ntop (int af, const void *src, char *dst, socklen_t size);
+const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
#endif
#endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKADDR_WIN32_H */
diff --git a/src/core/iomgr/socket_utils_common_posix.c b/src/core/iomgr/socket_utils_common_posix.c
index 668383ff6e..a9af594700 100644
--- a/src/core/iomgr/socket_utils_common_posix.c
+++ b/src/core/iomgr/socket_utils_common_posix.c
@@ -57,126 +57,103 @@
#include <grpc/support/sync.h>
/* set a socket to non blocking mode */
-int
-grpc_set_socket_nonblocking (int fd, int non_blocking)
-{
- int oldflags = fcntl (fd, F_GETFL, 0);
- if (oldflags < 0)
- {
- return 0;
- }
-
- if (non_blocking)
- {
- oldflags |= O_NONBLOCK;
- }
- else
- {
- oldflags &= ~O_NONBLOCK;
- }
-
- if (fcntl (fd, F_SETFL, oldflags) != 0)
- {
- return 0;
- }
+int grpc_set_socket_nonblocking(int fd, int non_blocking) {
+ int oldflags = fcntl(fd, F_GETFL, 0);
+ if (oldflags < 0) {
+ return 0;
+ }
+
+ if (non_blocking) {
+ oldflags |= O_NONBLOCK;
+ } else {
+ oldflags &= ~O_NONBLOCK;
+ }
+
+ if (fcntl(fd, F_SETFL, oldflags) != 0) {
+ return 0;
+ }
return 1;
}
-int
-grpc_set_socket_no_sigpipe_if_possible (int fd)
-{
+int grpc_set_socket_no_sigpipe_if_possible(int fd) {
#ifdef GPR_HAVE_SO_NOSIGPIPE
int val = 1;
int newval;
- socklen_t intlen = sizeof (newval);
- return 0 == setsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof (val)) && 0 == getsockopt (fd, SOL_SOCKET, SO_NOSIGPIPE, &newval, &intlen) && (newval != 0) == val;
+ socklen_t intlen = sizeof(newval);
+ return 0 == setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val)) &&
+ 0 == getsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &newval, &intlen) &&
+ (newval != 0) == val;
#else
return 1;
#endif
}
/* set a socket to close on exec */
-int
-grpc_set_socket_cloexec (int fd, int close_on_exec)
-{
- int oldflags = fcntl (fd, F_GETFD, 0);
- if (oldflags < 0)
- {
- return 0;
- }
-
- if (close_on_exec)
- {
- oldflags |= FD_CLOEXEC;
- }
- else
- {
- oldflags &= ~FD_CLOEXEC;
- }
-
- if (fcntl (fd, F_SETFD, oldflags) != 0)
- {
- return 0;
- }
+int grpc_set_socket_cloexec(int fd, int close_on_exec) {
+ int oldflags = fcntl(fd, F_GETFD, 0);
+ if (oldflags < 0) {
+ return 0;
+ }
+
+ if (close_on_exec) {
+ oldflags |= FD_CLOEXEC;
+ } else {
+ oldflags &= ~FD_CLOEXEC;
+ }
+
+ if (fcntl(fd, F_SETFD, oldflags) != 0) {
+ return 0;
+ }
return 1;
}
/* set a socket to reuse old addresses */
-int
-grpc_set_socket_reuse_addr (int fd, int reuse)
-{
+int grpc_set_socket_reuse_addr(int fd, int reuse) {
int val = (reuse != 0);
int newval;
- socklen_t intlen = sizeof (newval);
- return 0 == setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val)) && 0 == getsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &newval, &intlen) && (newval != 0) == val;
+ socklen_t intlen = sizeof(newval);
+ return 0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) &&
+ 0 == getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &newval, &intlen) &&
+ (newval != 0) == val;
}
/* disable nagle */
-int
-grpc_set_socket_low_latency (int fd, int low_latency)
-{
+int grpc_set_socket_low_latency(int fd, int low_latency) {
int val = (low_latency != 0);
int newval;
- socklen_t intlen = sizeof (newval);
- return 0 == setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)) && 0 == getsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &newval, &intlen) && (newval != 0) == val;
+ socklen_t intlen = sizeof(newval);
+ return 0 == setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) &&
+ 0 == getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &newval, &intlen) &&
+ (newval != 0) == val;
}
static gpr_once g_probe_ipv6_once = GPR_ONCE_INIT;
static int g_ipv6_loopback_available;
-static void
-probe_ipv6_once (void)
-{
- int fd = socket (AF_INET6, SOCK_STREAM, 0);
+static void probe_ipv6_once(void) {
+ int fd = socket(AF_INET6, SOCK_STREAM, 0);
g_ipv6_loopback_available = 0;
- if (fd < 0)
- {
- gpr_log (GPR_INFO, "Disabling AF_INET6 sockets because socket() failed.");
- }
- else
- {
- struct sockaddr_in6 addr;
- memset (&addr, 0, sizeof (addr));
- addr.sin6_family = AF_INET6;
- addr.sin6_addr.s6_addr[15] = 1; /* [::1]:0 */
- if (bind (fd, (struct sockaddr *) &addr, sizeof (addr)) == 0)
- {
- g_ipv6_loopback_available = 1;
- }
- else
- {
- gpr_log (GPR_INFO, "Disabling AF_INET6 sockets because ::1 is not available.");
- }
- close (fd);
+ if (fd < 0) {
+ gpr_log(GPR_INFO, "Disabling AF_INET6 sockets because socket() failed.");
+ } else {
+ struct sockaddr_in6 addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin6_family = AF_INET6;
+ addr.sin6_addr.s6_addr[15] = 1; /* [::1]:0 */
+ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
+ g_ipv6_loopback_available = 1;
+ } else {
+ gpr_log(GPR_INFO,
+ "Disabling AF_INET6 sockets because ::1 is not available.");
}
+ close(fd);
+ }
}
-int
-grpc_ipv6_loopback_available (void)
-{
- gpr_once_init (&g_probe_ipv6_once, probe_ipv6_once);
+int grpc_ipv6_loopback_available(void) {
+ gpr_once_init(&g_probe_ipv6_once, probe_ipv6_once);
return g_ipv6_loopback_available;
}
@@ -185,60 +162,47 @@ grpc_ipv6_loopback_available (void)
also speak IPv4. */
int grpc_forbid_dualstack_sockets_for_testing = 0;
-static int
-set_socket_dualstack (int fd)
-{
- if (!grpc_forbid_dualstack_sockets_for_testing)
- {
- const int off = 0;
- return 0 == setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof (off));
- }
- else
- {
- /* Force an IPv6-only socket, for testing purposes. */
- const int on = 1;
- setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on));
- return 0;
- }
+static int set_socket_dualstack(int fd) {
+ if (!grpc_forbid_dualstack_sockets_for_testing) {
+ const int off = 0;
+ return 0 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
+ } else {
+ /* Force an IPv6-only socket, for testing purposes. */
+ const int on = 1;
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
+ return 0;
+ }
}
-int
-grpc_create_dualstack_socket (const struct sockaddr *addr, int type, int protocol, grpc_dualstack_mode * dsmode)
-{
+int grpc_create_dualstack_socket(const struct sockaddr *addr, int type,
+ int protocol, grpc_dualstack_mode *dsmode) {
int family = addr->sa_family;
- if (family == AF_INET6)
- {
- int fd;
- if (grpc_ipv6_loopback_available ())
- {
- fd = socket (family, type, protocol);
- }
- else
- {
- fd = -1;
- errno = EAFNOSUPPORT;
- }
- /* Check if we've got a valid dualstack socket. */
- if (fd >= 0 && set_socket_dualstack (fd))
- {
- *dsmode = GRPC_DSMODE_DUALSTACK;
- return fd;
- }
- /* If this isn't an IPv4 address, then return whatever we've got. */
- if (!grpc_sockaddr_is_v4mapped (addr, NULL))
- {
- *dsmode = GRPC_DSMODE_IPV6;
- return fd;
- }
- /* Fall back to AF_INET. */
- if (fd >= 0)
- {
- close (fd);
- }
- family = AF_INET;
+ if (family == AF_INET6) {
+ int fd;
+ if (grpc_ipv6_loopback_available()) {
+ fd = socket(family, type, protocol);
+ } else {
+ fd = -1;
+ errno = EAFNOSUPPORT;
+ }
+ /* Check if we've got a valid dualstack socket. */
+ if (fd >= 0 && set_socket_dualstack(fd)) {
+ *dsmode = GRPC_DSMODE_DUALSTACK;
+ return fd;
+ }
+ /* If this isn't an IPv4 address, then return whatever we've got. */
+ if (!grpc_sockaddr_is_v4mapped(addr, NULL)) {
+ *dsmode = GRPC_DSMODE_IPV6;
+ return fd;
+ }
+ /* Fall back to AF_INET. */
+ if (fd >= 0) {
+ close(fd);
}
+ family = AF_INET;
+ }
*dsmode = family == AF_INET ? GRPC_DSMODE_IPV4 : GRPC_DSMODE_NONE;
- return socket (family, type, protocol);
+ return socket(family, type, protocol);
}
#endif
diff --git a/src/core/iomgr/socket_utils_linux.c b/src/core/iomgr/socket_utils_linux.c
index a21ae656fb..a87625262b 100644
--- a/src/core/iomgr/socket_utils_linux.c
+++ b/src/core/iomgr/socket_utils_linux.c
@@ -40,13 +40,12 @@
#include <sys/types.h>
#include <sys/socket.h>
-int
-grpc_accept4 (int sockfd, struct sockaddr *addr, socklen_t * addrlen, int nonblock, int cloexec)
-{
+int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
+ int nonblock, int cloexec) {
int flags = 0;
flags |= nonblock ? SOCK_NONBLOCK : 0;
flags |= cloexec ? SOCK_CLOEXEC : 0;
- return accept4 (sockfd, addr, addrlen, flags);
+ return accept4(sockfd, addr, addrlen, flags);
}
#endif
diff --git a/src/core/iomgr/socket_utils_posix.c b/src/core/iomgr/socket_utils_posix.c
index 1e32d7461f..3c56b46744 100644
--- a/src/core/iomgr/socket_utils_posix.c
+++ b/src/core/iomgr/socket_utils_posix.c
@@ -43,35 +43,27 @@
#include <grpc/support/log.h>
-int
-grpc_accept4 (int sockfd, struct sockaddr *addr, socklen_t * addrlen, int nonblock, int cloexec)
-{
+int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
+ int nonblock, int cloexec) {
int fd, flags;
- fd = accept (sockfd, addr, addrlen);
- if (fd >= 0)
- {
- if (nonblock)
- {
- flags = fcntl (fd, F_GETFL, 0);
- if (flags < 0)
- goto close_and_error;
- if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) != 0)
- goto close_and_error;
- }
- if (cloexec)
- {
- flags = fcntl (fd, F_GETFD, 0);
- if (flags < 0)
- goto close_and_error;
- if (fcntl (fd, F_SETFD, flags | FD_CLOEXEC) != 0)
- goto close_and_error;
- }
+ fd = accept(sockfd, addr, addrlen);
+ if (fd >= 0) {
+ if (nonblock) {
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0) goto close_and_error;
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) goto close_and_error;
}
+ if (cloexec) {
+ flags = fcntl(fd, F_GETFD, 0);
+ if (flags < 0) goto close_and_error;
+ if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != 0) goto close_and_error;
+ }
+ }
return fd;
close_and_error:
- close (fd);
+ close(fd);
return -1;
}
diff --git a/src/core/iomgr/socket_utils_posix.h b/src/core/iomgr/socket_utils_posix.h
index 8e0492802a..d330d1986e 100644
--- a/src/core/iomgr/socket_utils_posix.h
+++ b/src/core/iomgr/socket_utils_posix.h
@@ -38,19 +38,20 @@
#include <sys/socket.h>
/* a wrapper for accept or accept4 */
-int grpc_accept4 (int sockfd, struct sockaddr *addr, socklen_t * addrlen, int nonblock, int cloexec);
+int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
+ int nonblock, int cloexec);
/* set a socket to non blocking mode */
-int grpc_set_socket_nonblocking (int fd, int non_blocking);
+int grpc_set_socket_nonblocking(int fd, int non_blocking);
/* set a socket to close on exec */
-int grpc_set_socket_cloexec (int fd, int close_on_exec);
+int grpc_set_socket_cloexec(int fd, int close_on_exec);
/* set a socket to reuse old addresses */
-int grpc_set_socket_reuse_addr (int fd, int reuse);
+int grpc_set_socket_reuse_addr(int fd, int reuse);
/* disable nagle */
-int grpc_set_socket_low_latency (int fd, int low_latency);
+int grpc_set_socket_low_latency(int fd, int low_latency);
/* Returns true if this system can create AF_INET6 sockets bound to ::1.
The value is probed once, and cached for the life of the process.
@@ -60,20 +61,19 @@ int grpc_set_socket_low_latency (int fd, int low_latency);
and bind IPv6 sockets, but cannot connect to a getsockname() of [::]:port
without a valid loopback interface. Rather than expose this half-broken
state to library users, we turn off IPv6 sockets. */
-int grpc_ipv6_loopback_available (void);
+int grpc_ipv6_loopback_available(void);
/* Tries to set SO_NOSIGPIPE if available on this platform.
Returns 1 on success, 0 on failure.
If SO_NO_SIGPIPE is not available, returns 1. */
-int grpc_set_socket_no_sigpipe_if_possible (int fd);
+int grpc_set_socket_no_sigpipe_if_possible(int fd);
/* An enum to keep track of IPv4/IPv6 socket modes.
Currently, this information is only used when a socket is first created, but
in the future we may wish to store it alongside the fd. This would let calls
like sendto() know which family to use without asking the kernel first. */
-typedef enum grpc_dualstack_mode
-{
+typedef enum grpc_dualstack_mode {
/* Uninitialized, or a non-IP socket. */
GRPC_DSMODE_NONE,
/* AF_INET only. */
@@ -107,6 +107,7 @@ extern int grpc_forbid_dualstack_sockets_for_testing;
IPv4, so that bind() or connect() see the correct family.
Also, it's important to distinguish between DUALSTACK and IPV6 when
listening on the [::] wildcard address. */
-int grpc_create_dualstack_socket (const struct sockaddr *addr, int type, int protocol, grpc_dualstack_mode * dsmode);
+int grpc_create_dualstack_socket(const struct sockaddr *addr, int type,
+ int protocol, grpc_dualstack_mode *dsmode);
#endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_UTILS_POSIX_H */
diff --git a/src/core/iomgr/socket_windows.c b/src/core/iomgr/socket_windows.c
index 3e0cad0c7b..fafb7b6622 100644
--- a/src/core/iomgr/socket_windows.c
+++ b/src/core/iomgr/socket_windows.c
@@ -49,18 +49,16 @@
#include "src/core/iomgr/pollset_windows.h"
#include "src/core/iomgr/socket_windows.h"
-grpc_winsocket *
-grpc_winsocket_create (SOCKET socket, const char *name)
-{
+grpc_winsocket *grpc_winsocket_create(SOCKET socket, const char *name) {
char *final_name;
- grpc_winsocket *r = gpr_malloc (sizeof (grpc_winsocket));
- memset (r, 0, sizeof (grpc_winsocket));
+ grpc_winsocket *r = gpr_malloc(sizeof(grpc_winsocket));
+ memset(r, 0, sizeof(grpc_winsocket));
r->socket = socket;
- gpr_mu_init (&r->state_mu);
- gpr_asprintf (&final_name, "%s:socket=0x%p", name, r);
- grpc_iomgr_register_object (&r->iomgr_object, final_name);
- gpr_free (final_name);
- grpc_iocp_add_socket (r);
+ gpr_mu_init(&r->state_mu);
+ gpr_asprintf(&final_name, "%s:socket=0x%p", name, r);
+ grpc_iomgr_register_object(&r->iomgr_object, final_name);
+ gpr_free(final_name);
+ grpc_iocp_add_socket(r);
return r;
}
@@ -68,9 +66,7 @@ grpc_winsocket_create (SOCKET socket, const char *name)
operations to abort them. We need to do that this way because of the
various callsites of that function, which happens to be in various
mutex hold states, and that'd be unsafe to call them directly. */
-void
-grpc_winsocket_shutdown (grpc_winsocket * winsocket)
-{
+void grpc_winsocket_shutdown(grpc_winsocket *winsocket) {
/* Grab the function pointer for DisconnectEx for that specific socket.
It may change depending on the interface. */
int status;
@@ -78,27 +74,25 @@ grpc_winsocket_shutdown (grpc_winsocket * winsocket)
LPFN_DISCONNECTEX DisconnectEx;
DWORD ioctl_num_bytes;
- status = WSAIoctl (winsocket->socket, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof (guid), &DisconnectEx, sizeof (DisconnectEx), &ioctl_num_bytes, NULL, NULL);
+ status = WSAIoctl(winsocket->socket, SIO_GET_EXTENSION_FUNCTION_POINTER,
+ &guid, sizeof(guid), &DisconnectEx, sizeof(DisconnectEx),
+ &ioctl_num_bytes, NULL, NULL);
- if (status == 0)
- {
- DisconnectEx (winsocket->socket, NULL, 0, 0);
- }
- else
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "Unable to retrieve DisconnectEx pointer : %s", utf8_message);
- gpr_free (utf8_message);
- }
- closesocket (winsocket->socket);
+ if (status == 0) {
+ DisconnectEx(winsocket->socket, NULL, 0, 0);
+ } else {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "Unable to retrieve DisconnectEx pointer : %s",
+ utf8_message);
+ gpr_free(utf8_message);
+ }
+ closesocket(winsocket->socket);
}
-void
-grpc_winsocket_destroy (grpc_winsocket * winsocket)
-{
- grpc_iomgr_unregister_object (&winsocket->iomgr_object);
- gpr_mu_destroy (&winsocket->state_mu);
- gpr_free (winsocket);
+void grpc_winsocket_destroy(grpc_winsocket *winsocket) {
+ grpc_iomgr_unregister_object(&winsocket->iomgr_object);
+ gpr_mu_destroy(&winsocket->state_mu);
+ gpr_free(winsocket);
}
#endif /* GPR_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/socket_windows.h b/src/core/iomgr/socket_windows.h
index df2a730413..45bc657225 100644
--- a/src/core/iomgr/socket_windows.h
+++ b/src/core/iomgr/socket_windows.h
@@ -45,8 +45,7 @@
/* This holds the data for an outstanding read or write on a socket.
The mutex to protect the concurrent access to that data is the one
inside the winsocket wrapper. */
-typedef struct grpc_winsocket_callback_info
-{
+typedef struct grpc_winsocket_callback_info {
/* This is supposed to be a WSAOVERLAPPED, but in order to get that
definition, we need to include ws2tcpip.h, which needs to be included
from the top, otherwise it'll clash with a previous inclusion of
@@ -55,7 +54,7 @@ typedef struct grpc_winsocket_callback_info
OVERLAPPED overlapped;
/* The callback information for the pending operation. May be empty if the
caller hasn't registered a callback yet. */
- void (*cb) (void *opaque, int success);
+ void (*cb)(void *opaque, int success);
void *opaque;
/* A boolean to describe if the IO Completion Port got a notification for
that operation. This will happen if the operation completed before the
@@ -80,8 +79,7 @@ typedef struct grpc_winsocket_callback_info
operation callbacks for connect and listen. But given the scope of listen
and accept, we don't need to go to that extent and waste memory. Also, this
is closer to what happens in posix world. */
-typedef struct grpc_winsocket
-{
+typedef struct grpc_winsocket {
SOCKET socket;
grpc_winsocket_callback_info write_info;
@@ -101,13 +99,13 @@ typedef struct grpc_winsocket
/* Create a wrapped windows handle. This takes ownership of it, meaning that
it will be responsible for closing it. */
-grpc_winsocket *grpc_winsocket_create (SOCKET socket, const char *name);
+grpc_winsocket *grpc_winsocket_create(SOCKET socket, const char *name);
/* Initiate an asynchronous shutdown of the socket. Will call off any pending
operation to cancel them. */
-void grpc_winsocket_shutdown (grpc_winsocket * socket);
+void grpc_winsocket_shutdown(grpc_winsocket *socket);
/* Destroy a socket. Should only be called if there's no pending operation. */
-void grpc_winsocket_destroy (grpc_winsocket * socket);
+void grpc_winsocket_destroy(grpc_winsocket *socket);
#endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H */
diff --git a/src/core/iomgr/tcp_client.h b/src/core/iomgr/tcp_client.h
index 2e14074006..5e18e71ca2 100644
--- a/src/core/iomgr/tcp_client.h
+++ b/src/core/iomgr/tcp_client.h
@@ -44,6 +44,10 @@
NULL on failure).
interested_parties points to a set of pollsets that would be interested
in this connection being established (in order to continue their work) */
-void grpc_tcp_client_connect (grpc_exec_ctx * exec_ctx, grpc_closure * on_connect, grpc_endpoint ** endpoint, grpc_pollset_set * interested_parties, const struct sockaddr *addr, size_t addr_len, gpr_timespec deadline);
+void grpc_tcp_client_connect(grpc_exec_ctx *exec_ctx, grpc_closure *on_connect,
+ grpc_endpoint **endpoint,
+ grpc_pollset_set *interested_parties,
+ const struct sockaddr *addr, size_t addr_len,
+ gpr_timespec deadline);
#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_CLIENT_H */
diff --git a/src/core/iomgr/tcp_client_posix.c b/src/core/iomgr/tcp_client_posix.c
index 3949ce6f24..346566866a 100644
--- a/src/core/iomgr/tcp_client_posix.c
+++ b/src/core/iomgr/tcp_client_posix.c
@@ -56,8 +56,7 @@
extern int grpc_tcp_trace;
-typedef struct
-{
+typedef struct {
gpr_mu mu;
grpc_fd *fd;
gpr_timespec deadline;
@@ -70,57 +69,49 @@ typedef struct
grpc_closure *closure;
} async_connect;
-static int
-prepare_socket (const struct sockaddr *addr, int fd)
-{
- if (fd < 0)
- {
- goto error;
- }
+static int prepare_socket(const struct sockaddr *addr, int fd) {
+ if (fd < 0) {
+ goto error;
+ }
- if (!grpc_set_socket_nonblocking (fd, 1) || !grpc_set_socket_cloexec (fd, 1) || (addr->sa_family != AF_UNIX && !grpc_set_socket_low_latency (fd, 1)) || !grpc_set_socket_no_sigpipe_if_possible (fd))
- {
- gpr_log (GPR_ERROR, "Unable to configure socket %d: %s", fd, strerror (errno));
- goto error;
- }
+ if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
+ (addr->sa_family != AF_UNIX && !grpc_set_socket_low_latency(fd, 1)) ||
+ !grpc_set_socket_no_sigpipe_if_possible(fd)) {
+ gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
+ strerror(errno));
+ goto error;
+ }
return 1;
error:
- if (fd >= 0)
- {
- close (fd);
- }
+ if (fd >= 0) {
+ close(fd);
+ }
return 0;
}
-static void
-tc_on_alarm (grpc_exec_ctx * exec_ctx, void *acp, int success)
-{
+static void tc_on_alarm(grpc_exec_ctx *exec_ctx, void *acp, int success) {
int done;
async_connect *ac = acp;
- if (grpc_tcp_trace)
- {
- gpr_log (GPR_DEBUG, "CLIENT_CONNECT: %s: on_alarm: success=%d", ac->addr_str, success);
- }
- gpr_mu_lock (&ac->mu);
- if (ac->fd != NULL)
- {
- grpc_fd_shutdown (exec_ctx, ac->fd);
- }
+ if (grpc_tcp_trace) {
+ gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %s: on_alarm: success=%d", ac->addr_str,
+ success);
+ }
+ gpr_mu_lock(&ac->mu);
+ if (ac->fd != NULL) {
+ grpc_fd_shutdown(exec_ctx, ac->fd);
+ }
done = (--ac->refs == 0);
- gpr_mu_unlock (&ac->mu);
- if (done)
- {
- gpr_mu_destroy (&ac->mu);
- gpr_free (ac->addr_str);
- gpr_free (ac);
- }
+ gpr_mu_unlock(&ac->mu);
+ if (done) {
+ gpr_mu_destroy(&ac->mu);
+ gpr_free(ac->addr_str);
+ gpr_free(ac);
+ }
}
-static void
-on_writable (grpc_exec_ctx * exec_ctx, void *acp, int success)
-{
+static void on_writable(grpc_exec_ctx *exec_ctx, void *acp, int success) {
async_connect *ac = acp;
int so_error = 0;
socklen_t so_error_size;
@@ -130,107 +121,93 @@ on_writable (grpc_exec_ctx * exec_ctx, void *acp, int success)
grpc_closure *closure = ac->closure;
grpc_fd *fd;
- if (grpc_tcp_trace)
- {
- gpr_log (GPR_DEBUG, "CLIENT_CONNECT: %s: on_writable: success=%d", ac->addr_str, success);
- }
+ if (grpc_tcp_trace) {
+ gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %s: on_writable: success=%d",
+ ac->addr_str, success);
+ }
- gpr_mu_lock (&ac->mu);
- GPR_ASSERT (ac->fd);
+ gpr_mu_lock(&ac->mu);
+ GPR_ASSERT(ac->fd);
fd = ac->fd;
ac->fd = NULL;
- gpr_mu_unlock (&ac->mu);
-
- grpc_alarm_cancel (exec_ctx, &ac->alarm);
-
- gpr_mu_lock (&ac->mu);
- if (success)
- {
- do
- {
- so_error_size = sizeof (so_error);
- err = getsockopt (fd->fd, SOL_SOCKET, SO_ERROR, &so_error, &so_error_size);
- }
- while (err < 0 && errno == EINTR);
- if (err < 0)
- {
- gpr_log (GPR_ERROR, "getsockopt(ERROR): %s", strerror (errno));
- goto finish;
- }
- else if (so_error != 0)
- {
- if (so_error == ENOBUFS)
- {
- /* We will get one of these errors if we have run out of
- memory in the kernel for the data structures allocated
- when you connect a socket. If this happens it is very
- likely that if we wait a little bit then try again the
- connection will work (since other programs or this
- program will close their network connections and free up
- memory). This does _not_ indicate that there is anything
- wrong with the server we are connecting to, this is a
- local problem.
-
- If you are looking at this code, then chances are that
- your program or another program on the same computer
- opened too many network connections. The "easy" fix:
- don't do that! */
- gpr_log (GPR_ERROR, "kernel out of buffers");
- gpr_mu_unlock (&ac->mu);
- grpc_fd_notify_on_write (exec_ctx, fd, &ac->write_closure);
- return;
- }
- else
- {
- switch (so_error)
- {
- case ECONNREFUSED:
- gpr_log (GPR_ERROR, "socket error: connection refused");
- break;
- default:
- gpr_log (GPR_ERROR, "socket error: %d", so_error);
- break;
- }
- goto finish;
- }
- }
- else
- {
- grpc_pollset_set_del_fd (exec_ctx, ac->interested_parties, fd);
- *ep = grpc_tcp_create (fd, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, ac->addr_str);
- fd = NULL;
- goto finish;
- }
- }
- else
- {
- gpr_log (GPR_ERROR, "on_writable failed during connect");
+ gpr_mu_unlock(&ac->mu);
+
+ grpc_alarm_cancel(exec_ctx, &ac->alarm);
+
+ gpr_mu_lock(&ac->mu);
+ if (success) {
+ do {
+ so_error_size = sizeof(so_error);
+ err = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &so_error, &so_error_size);
+ } while (err < 0 && errno == EINTR);
+ if (err < 0) {
+ gpr_log(GPR_ERROR, "getsockopt(ERROR): %s", strerror(errno));
+ goto finish;
+ } else if (so_error != 0) {
+ if (so_error == ENOBUFS) {
+ /* We will get one of these errors if we have run out of
+ memory in the kernel for the data structures allocated
+ when you connect a socket. If this happens it is very
+ likely that if we wait a little bit then try again the
+ connection will work (since other programs or this
+ program will close their network connections and free up
+ memory). This does _not_ indicate that there is anything
+ wrong with the server we are connecting to, this is a
+ local problem.
+
+ If you are looking at this code, then chances are that
+ your program or another program on the same computer
+ opened too many network connections. The "easy" fix:
+ don't do that! */
+ gpr_log(GPR_ERROR, "kernel out of buffers");
+ gpr_mu_unlock(&ac->mu);
+ grpc_fd_notify_on_write(exec_ctx, fd, &ac->write_closure);
+ return;
+ } else {
+ switch (so_error) {
+ case ECONNREFUSED:
+ gpr_log(GPR_ERROR, "socket error: connection refused");
+ break;
+ default:
+ gpr_log(GPR_ERROR, "socket error: %d", so_error);
+ break;
+ }
+ goto finish;
+ }
+ } else {
+ grpc_pollset_set_del_fd(exec_ctx, ac->interested_parties, fd);
+ *ep = grpc_tcp_create(fd, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, ac->addr_str);
+ fd = NULL;
goto finish;
}
+ } else {
+ gpr_log(GPR_ERROR, "on_writable failed during connect");
+ goto finish;
+ }
- abort ();
+ abort();
finish:
- if (fd != NULL)
- {
- grpc_pollset_set_del_fd (exec_ctx, ac->interested_parties, fd);
- grpc_fd_orphan (exec_ctx, fd, NULL, "tcp_client_orphan");
- fd = NULL;
- }
+ if (fd != NULL) {
+ grpc_pollset_set_del_fd(exec_ctx, ac->interested_parties, fd);
+ grpc_fd_orphan(exec_ctx, fd, NULL, "tcp_client_orphan");
+ fd = NULL;
+ }
done = (--ac->refs == 0);
- gpr_mu_unlock (&ac->mu);
- if (done)
- {
- gpr_mu_destroy (&ac->mu);
- gpr_free (ac->addr_str);
- gpr_free (ac);
- }
- grpc_exec_ctx_enqueue (exec_ctx, closure, *ep != NULL);
+ gpr_mu_unlock(&ac->mu);
+ if (done) {
+ gpr_mu_destroy(&ac->mu);
+ gpr_free(ac->addr_str);
+ gpr_free(ac);
+ }
+ grpc_exec_ctx_enqueue(exec_ctx, closure, *ep != NULL);
}
-void
-grpc_tcp_client_connect (grpc_exec_ctx * exec_ctx, grpc_closure * closure, grpc_endpoint ** ep, grpc_pollset_set * interested_parties, const struct sockaddr *addr, size_t addr_len, gpr_timespec deadline)
-{
+void grpc_tcp_client_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure,
+ grpc_endpoint **ep,
+ grpc_pollset_set *interested_parties,
+ const struct sockaddr *addr, size_t addr_len,
+ gpr_timespec deadline) {
int fd;
grpc_dualstack_mode dsmode;
int err;
@@ -244,84 +221,78 @@ grpc_tcp_client_connect (grpc_exec_ctx * exec_ctx, grpc_closure * closure, grpc_
*ep = NULL;
/* Use dualstack sockets where available. */
- if (grpc_sockaddr_to_v4mapped (addr, &addr6_v4mapped))
- {
- addr = (const struct sockaddr *) &addr6_v4mapped;
- addr_len = sizeof (addr6_v4mapped);
- }
-
- fd = grpc_create_dualstack_socket (addr, SOCK_STREAM, 0, &dsmode);
- if (fd < 0)
- {
- gpr_log (GPR_ERROR, "Unable to create socket: %s", strerror (errno));
- }
- if (dsmode == GRPC_DSMODE_IPV4)
- {
- /* If we got an AF_INET socket, map the address back to IPv4. */
- GPR_ASSERT (grpc_sockaddr_is_v4mapped (addr, &addr4_copy));
- addr = (struct sockaddr *) &addr4_copy;
- addr_len = sizeof (addr4_copy);
- }
- if (!prepare_socket (addr, fd))
- {
- grpc_exec_ctx_enqueue (exec_ctx, closure, 0);
- return;
- }
-
- do
- {
- GPR_ASSERT (addr_len < ~(socklen_t) 0);
- err = connect (fd, addr, (socklen_t) addr_len);
- }
- while (err < 0 && errno == EINTR);
-
- addr_str = grpc_sockaddr_to_uri (addr);
- gpr_asprintf (&name, "tcp-client:%s", addr_str);
-
- fdobj = grpc_fd_create (fd, name);
-
- if (err >= 0)
- {
- *ep = grpc_tcp_create (fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str);
- grpc_exec_ctx_enqueue (exec_ctx, closure, 1);
- goto done;
- }
-
- if (errno != EWOULDBLOCK && errno != EINPROGRESS)
- {
- gpr_log (GPR_ERROR, "connect error to '%s': %s", addr_str, strerror (errno));
- grpc_fd_orphan (exec_ctx, fdobj, NULL, "tcp_client_connect_error");
- grpc_exec_ctx_enqueue (exec_ctx, closure, 0);
- goto done;
- }
-
- grpc_pollset_set_add_fd (exec_ctx, interested_parties, fdobj);
-
- ac = gpr_malloc (sizeof (async_connect));
+ if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
+ addr = (const struct sockaddr *)&addr6_v4mapped;
+ addr_len = sizeof(addr6_v4mapped);
+ }
+
+ fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
+ if (fd < 0) {
+ gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
+ }
+ if (dsmode == GRPC_DSMODE_IPV4) {
+ /* If we got an AF_INET socket, map the address back to IPv4. */
+ GPR_ASSERT(grpc_sockaddr_is_v4mapped(addr, &addr4_copy));
+ addr = (struct sockaddr *)&addr4_copy;
+ addr_len = sizeof(addr4_copy);
+ }
+ if (!prepare_socket(addr, fd)) {
+ grpc_exec_ctx_enqueue(exec_ctx, closure, 0);
+ return;
+ }
+
+ do {
+ GPR_ASSERT(addr_len < ~(socklen_t)0);
+ err = connect(fd, addr, (socklen_t)addr_len);
+ } while (err < 0 && errno == EINTR);
+
+ addr_str = grpc_sockaddr_to_uri(addr);
+ gpr_asprintf(&name, "tcp-client:%s", addr_str);
+
+ fdobj = grpc_fd_create(fd, name);
+
+ if (err >= 0) {
+ *ep = grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str);
+ grpc_exec_ctx_enqueue(exec_ctx, closure, 1);
+ goto done;
+ }
+
+ if (errno != EWOULDBLOCK && errno != EINPROGRESS) {
+ gpr_log(GPR_ERROR, "connect error to '%s': %s", addr_str, strerror(errno));
+ grpc_fd_orphan(exec_ctx, fdobj, NULL, "tcp_client_connect_error");
+ grpc_exec_ctx_enqueue(exec_ctx, closure, 0);
+ goto done;
+ }
+
+ grpc_pollset_set_add_fd(exec_ctx, interested_parties, fdobj);
+
+ ac = gpr_malloc(sizeof(async_connect));
ac->closure = closure;
ac->ep = ep;
ac->fd = fdobj;
ac->interested_parties = interested_parties;
ac->addr_str = addr_str;
addr_str = NULL;
- gpr_mu_init (&ac->mu);
+ gpr_mu_init(&ac->mu);
ac->refs = 2;
ac->write_closure.cb = on_writable;
ac->write_closure.cb_arg = ac;
- if (grpc_tcp_trace)
- {
- gpr_log (GPR_DEBUG, "CLIENT_CONNECT: %s: asynchronously connecting", ac->addr_str);
- }
+ if (grpc_tcp_trace) {
+ gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %s: asynchronously connecting",
+ ac->addr_str);
+ }
- gpr_mu_lock (&ac->mu);
- grpc_alarm_init (exec_ctx, &ac->alarm, gpr_convert_clock_type (deadline, GPR_CLOCK_MONOTONIC), tc_on_alarm, ac, gpr_now (GPR_CLOCK_MONOTONIC));
- grpc_fd_notify_on_write (exec_ctx, ac->fd, &ac->write_closure);
- gpr_mu_unlock (&ac->mu);
+ gpr_mu_lock(&ac->mu);
+ grpc_alarm_init(exec_ctx, &ac->alarm,
+ gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
+ tc_on_alarm, ac, gpr_now(GPR_CLOCK_MONOTONIC));
+ grpc_fd_notify_on_write(exec_ctx, ac->fd, &ac->write_closure);
+ gpr_mu_unlock(&ac->mu);
done:
- gpr_free (name);
- gpr_free (addr_str);
+ gpr_free(name);
+ gpr_free(addr_str);
}
#endif
diff --git a/src/core/iomgr/tcp_client_windows.c b/src/core/iomgr/tcp_client_windows.c
index 03a9a9c9d6..28f1bc96c8 100644
--- a/src/core/iomgr/tcp_client_windows.c
+++ b/src/core/iomgr/tcp_client_windows.c
@@ -51,9 +51,8 @@
#include "src/core/iomgr/sockaddr_utils.h"
#include "src/core/iomgr/socket_windows.h"
-typedef struct
-{
- void (*cb) (void *arg, grpc_endpoint * tcp);
+typedef struct {
+ void (*cb)(void *arg, grpc_endpoint *tcp);
void *cb_arg;
gpr_mu mu;
grpc_winsocket *socket;
@@ -63,79 +62,67 @@ typedef struct
int refs;
} async_connect;
-static void
-async_connect_unlock_and_cleanup (async_connect * ac)
-{
+static void async_connect_unlock_and_cleanup(async_connect *ac) {
int done = (--ac->refs == 0);
- gpr_mu_unlock (&ac->mu);
- if (done)
- {
- if (ac->socket != NULL)
- grpc_winsocket_destroy (ac->socket);
- gpr_mu_destroy (&ac->mu);
- gpr_free (ac->addr_name);
- gpr_free (ac);
- }
+ gpr_mu_unlock(&ac->mu);
+ if (done) {
+ if (ac->socket != NULL) grpc_winsocket_destroy(ac->socket);
+ gpr_mu_destroy(&ac->mu);
+ gpr_free(ac->addr_name);
+ gpr_free(ac);
+ }
}
-static void
-on_alarm (void *acp, int occured)
-{
+static void on_alarm(void *acp, int occured) {
async_connect *ac = acp;
- gpr_mu_lock (&ac->mu);
+ gpr_mu_lock(&ac->mu);
/* If the alarm didn't occur, it got cancelled. */
- if (ac->socket != NULL && occured)
- {
- grpc_winsocket_shutdown (ac->socket);
- }
- async_connect_unlock_and_cleanup (ac);
+ if (ac->socket != NULL && occured) {
+ grpc_winsocket_shutdown(ac->socket);
+ }
+ async_connect_unlock_and_cleanup(ac);
}
-static void
-on_connect (void *acp, int from_iocp)
-{
+static void on_connect(void *acp, int from_iocp) {
async_connect *ac = acp;
SOCKET sock = ac->socket->socket;
grpc_endpoint *ep = NULL;
grpc_winsocket_callback_info *info = &ac->socket->write_info;
- void (*cb) (void *arg, grpc_endpoint * tcp) = ac->cb;
+ void (*cb)(void *arg, grpc_endpoint *tcp) = ac->cb;
void *cb_arg = ac->cb_arg;
- grpc_alarm_cancel (&ac->alarm);
-
- gpr_mu_lock (&ac->mu);
-
- if (from_iocp)
- {
- DWORD transfered_bytes = 0;
- DWORD flags;
- BOOL wsa_success = WSAGetOverlappedResult (sock, &info->overlapped,
- &transfered_bytes, FALSE, &flags);
- GPR_ASSERT (transfered_bytes == 0);
- if (!wsa_success)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "on_connect error: %s", utf8_message);
- gpr_free (utf8_message);
- }
- else
- {
- ep = grpc_tcp_create (ac->socket, ac->addr_name);
- ac->socket = NULL;
- }
+ grpc_alarm_cancel(&ac->alarm);
+
+ gpr_mu_lock(&ac->mu);
+
+ if (from_iocp) {
+ DWORD transfered_bytes = 0;
+ DWORD flags;
+ BOOL wsa_success = WSAGetOverlappedResult(sock, &info->overlapped,
+ &transfered_bytes, FALSE, &flags);
+ GPR_ASSERT(transfered_bytes == 0);
+ if (!wsa_success) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "on_connect error: %s", utf8_message);
+ gpr_free(utf8_message);
+ } else {
+ ep = grpc_tcp_create(ac->socket, ac->addr_name);
+ ac->socket = NULL;
}
+ }
- async_connect_unlock_and_cleanup (ac);
+ async_connect_unlock_and_cleanup(ac);
/* If the connection was aborted, the callback was already called when
the deadline was met. */
- cb (cb_arg, ep);
+ cb(cb_arg, ep);
}
/* Tries to issue one async connection, then schedules both an IOCP
notification request for the connection, and one timeout alert. */
-void
-grpc_tcp_client_connect (void (*cb) (void *arg, grpc_endpoint * tcp), void *arg, grpc_pollset_set * interested_parties, const struct sockaddr *addr, int addr_len, gpr_timespec deadline)
-{
+void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *tcp),
+ void *arg, grpc_pollset_set *interested_parties,
+ const struct sockaddr *addr, int addr_len,
+ gpr_timespec deadline) {
SOCKET sock = INVALID_SOCKET;
BOOL success;
int status;
@@ -151,85 +138,79 @@ grpc_tcp_client_connect (void (*cb) (void *arg, grpc_endpoint * tcp), void *arg,
grpc_winsocket_callback_info *info;
/* Use dualstack sockets where available. */
- if (grpc_sockaddr_to_v4mapped (addr, &addr6_v4mapped))
- {
- addr = (const struct sockaddr *) &addr6_v4mapped;
- addr_len = sizeof (addr6_v4mapped);
- }
-
- sock = WSASocket (AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
- if (sock == INVALID_SOCKET)
- {
- message = "Unable to create socket: %s";
- goto failure;
- }
-
- if (!grpc_tcp_prepare_socket (sock))
- {
- message = "Unable to set socket options: %s";
- goto failure;
- }
+ if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
+ addr = (const struct sockaddr *)&addr6_v4mapped;
+ addr_len = sizeof(addr6_v4mapped);
+ }
+
+ sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
+ WSA_FLAG_OVERLAPPED);
+ if (sock == INVALID_SOCKET) {
+ message = "Unable to create socket: %s";
+ goto failure;
+ }
+
+ if (!grpc_tcp_prepare_socket(sock)) {
+ message = "Unable to set socket options: %s";
+ goto failure;
+ }
/* Grab the function pointer for ConnectEx for that specific socket.
It may change depending on the interface. */
- status = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof (guid), &ConnectEx, sizeof (ConnectEx), &ioctl_num_bytes, NULL, NULL);
+ status =
+ WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid),
+ &ConnectEx, sizeof(ConnectEx), &ioctl_num_bytes, NULL, NULL);
- if (status != 0)
- {
- message = "Unable to retrieve ConnectEx pointer: %s";
- goto failure;
- }
+ if (status != 0) {
+ message = "Unable to retrieve ConnectEx pointer: %s";
+ goto failure;
+ }
- grpc_sockaddr_make_wildcard6 (0, &local_address);
+ grpc_sockaddr_make_wildcard6(0, &local_address);
- status = bind (sock, (struct sockaddr *) &local_address, sizeof (local_address));
- if (status != 0)
- {
- message = "Unable to bind socket: %s";
- goto failure;
- }
+ status = bind(sock, (struct sockaddr *)&local_address, sizeof(local_address));
+ if (status != 0) {
+ message = "Unable to bind socket: %s";
+ goto failure;
+ }
- socket = grpc_winsocket_create (sock, "client");
+ socket = grpc_winsocket_create(sock, "client");
info = &socket->write_info;
- success = ConnectEx (sock, addr, addr_len, NULL, 0, NULL, &info->overlapped);
+ success = ConnectEx(sock, addr, addr_len, NULL, 0, NULL, &info->overlapped);
/* It wouldn't be unusual to get a success immediately. But we'll still get
an IOCP notification, so let's ignore it. */
- if (!success)
- {
- int error = WSAGetLastError ();
- if (error != ERROR_IO_PENDING)
- {
- message = "ConnectEx failed: %s";
- goto failure;
- }
+ if (!success) {
+ int error = WSAGetLastError();
+ if (error != ERROR_IO_PENDING) {
+ message = "ConnectEx failed: %s";
+ goto failure;
}
+ }
- ac = gpr_malloc (sizeof (async_connect));
+ ac = gpr_malloc(sizeof(async_connect));
ac->cb = cb;
ac->cb_arg = arg;
ac->socket = socket;
- gpr_mu_init (&ac->mu);
+ gpr_mu_init(&ac->mu);
ac->refs = 2;
- ac->addr_name = grpc_sockaddr_to_uri (addr);
+ ac->addr_name = grpc_sockaddr_to_uri(addr);
- grpc_alarm_init (&ac->alarm, deadline, on_alarm, ac, gpr_now (GPR_CLOCK_MONOTONIC));
- grpc_socket_notify_on_write (socket, on_connect, ac);
+ grpc_alarm_init(&ac->alarm, deadline, on_alarm, ac,
+ gpr_now(GPR_CLOCK_MONOTONIC));
+ grpc_socket_notify_on_write(socket, on_connect, ac);
return;
failure:
- utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, message, utf8_message);
- gpr_free (utf8_message);
- if (socket != NULL)
- {
- grpc_winsocket_destroy (socket);
- }
- else if (sock != INVALID_SOCKET)
- {
- closesocket (sock);
- }
- cb (arg, NULL);
+ utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, message, utf8_message);
+ gpr_free(utf8_message);
+ if (socket != NULL) {
+ grpc_winsocket_destroy(socket);
+ } else if (sock != INVALID_SOCKET) {
+ closesocket(sock);
+ }
+ cb(arg, NULL);
}
#endif /* GPR_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/tcp_posix.c b/src/core/iomgr/tcp_posix.c
index cd0769a48b..c98f0125f8 100644
--- a/src/core/iomgr/tcp_posix.c
+++ b/src/core/iomgr/tcp_posix.c
@@ -69,13 +69,12 @@ typedef size_t msg_iovlen_type;
int grpc_tcp_trace = 0;
-typedef struct
-{
+typedef struct {
grpc_endpoint base;
grpc_fd *em_fd;
int fd;
int finished_edge;
- msg_iovlen_type iov_size; /* Number of slices to allocate per read attempt */
+ msg_iovlen_type iov_size; /* Number of slices to allocate per read attempt */
size_t slice_size;
gpr_refcount refcount;
@@ -95,22 +94,20 @@ typedef struct
char *peer_string;
} grpc_tcp;
-static void tcp_handle_read (grpc_exec_ctx * exec_ctx, void *arg /* grpc_tcp */ , int success);
-static void tcp_handle_write (grpc_exec_ctx * exec_ctx, void *arg /* grpc_tcp */ , int success);
+static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
+ int success);
+static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
+ int success);
-static void
-tcp_shutdown (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- grpc_fd_shutdown (exec_ctx, tcp->em_fd);
+static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ grpc_fd_shutdown(exec_ctx, tcp->em_fd);
}
-static void
-tcp_free (grpc_exec_ctx * exec_ctx, grpc_tcp * tcp)
-{
- grpc_fd_orphan (exec_ctx, tcp->em_fd, NULL, "tcp_unref_orphan");
- gpr_free (tcp->peer_string);
- gpr_free (tcp);
+static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
+ grpc_fd_orphan(exec_ctx, tcp->em_fd, NULL, "tcp_unref_orphan");
+ gpr_free(tcp->peer_string);
+ gpr_free(tcp);
}
/*#define GRPC_TCP_REFCOUNT_DEBUG*/
@@ -118,94 +115,77 @@ tcp_free (grpc_exec_ctx * exec_ctx, grpc_tcp * tcp)
#define TCP_UNREF(cl, tcp, reason) \
tcp_unref((cl), (tcp), (reason), __FILE__, __LINE__)
#define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
-static void
-tcp_unref (grpc_exec_ctx *exec_ctx, grpc_tcp * tcp, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp, reason, tcp->refcount.count, tcp->refcount.count - 1);
- if (gpr_unref (&tcp->refcount))
- {
- tcp_free (exec_ctx, tcp);
- }
+static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp,
+ const char *reason, const char *file, int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp,
+ reason, tcp->refcount.count, tcp->refcount.count - 1);
+ if (gpr_unref(&tcp->refcount)) {
+ tcp_free(exec_ctx, tcp);
+ }
}
-static void
-tcp_ref (grpc_tcp * tcp, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp, reason, tcp->refcount.count, tcp->refcount.count + 1);
- gpr_ref (&tcp->refcount);
+static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
+ int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp,
+ reason, tcp->refcount.count, tcp->refcount.count + 1);
+ gpr_ref(&tcp->refcount);
}
#else
#define TCP_UNREF(cl, tcp, reason) tcp_unref((cl), (tcp))
#define TCP_REF(tcp, reason) tcp_ref((tcp))
-static void
-tcp_unref (grpc_exec_ctx * exec_ctx, grpc_tcp * tcp)
-{
- if (gpr_unref (&tcp->refcount))
- {
- tcp_free (exec_ctx, tcp);
- }
+static void tcp_unref(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
+ if (gpr_unref(&tcp->refcount)) {
+ tcp_free(exec_ctx, tcp);
+ }
}
-static void
-tcp_ref (grpc_tcp * tcp)
-{
- gpr_ref (&tcp->refcount);
-}
+static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
#endif
-static void
-tcp_destroy (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- TCP_UNREF (exec_ctx, tcp, "destroy");
+static void tcp_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ TCP_UNREF(exec_ctx, tcp, "destroy");
}
-static void
-call_read_cb (grpc_exec_ctx * exec_ctx, grpc_tcp * tcp, int success)
-{
+static void call_read_cb(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp, int success) {
grpc_closure *cb = tcp->read_cb;
- if (grpc_tcp_trace)
- {
- size_t i;
- gpr_log (GPR_DEBUG, "read: success=%d", success);
- for (i = 0; i < tcp->incoming_buffer->count; i++)
- {
- char *dump = gpr_dump_slice (tcp->incoming_buffer->slices[i],
- GPR_DUMP_HEX | GPR_DUMP_ASCII);
- gpr_log (GPR_DEBUG, "READ %p: %s", tcp, dump);
- gpr_free (dump);
- }
+ if (grpc_tcp_trace) {
+ size_t i;
+ gpr_log(GPR_DEBUG, "read: success=%d", success);
+ for (i = 0; i < tcp->incoming_buffer->count; i++) {
+ char *dump = gpr_dump_slice(tcp->incoming_buffer->slices[i],
+ GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ gpr_log(GPR_DEBUG, "READ %p: %s", tcp, dump);
+ gpr_free(dump);
}
+ }
tcp->read_cb = NULL;
tcp->incoming_buffer = NULL;
- cb->cb (exec_ctx, cb->cb_arg, success);
+ cb->cb(exec_ctx, cb->cb_arg, success);
}
#define MAX_READ_IOVEC 4
-static void
-tcp_continue_read (grpc_exec_ctx * exec_ctx, grpc_tcp * tcp)
-{
+static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
struct msghdr msg;
struct iovec iov[MAX_READ_IOVEC];
ssize_t read_bytes;
size_t i;
- GPR_ASSERT (!tcp->finished_edge);
- GPR_ASSERT (tcp->iov_size <= MAX_READ_IOVEC);
- GPR_ASSERT (tcp->incoming_buffer->count <= MAX_READ_IOVEC);
- GRPC_TIMER_BEGIN (GRPC_PTAG_HANDLE_READ, 0);
+ GPR_ASSERT(!tcp->finished_edge);
+ GPR_ASSERT(tcp->iov_size <= MAX_READ_IOVEC);
+ GPR_ASSERT(tcp->incoming_buffer->count <= MAX_READ_IOVEC);
+ GRPC_TIMER_BEGIN(GRPC_PTAG_HANDLE_READ, 0);
- while (tcp->incoming_buffer->count < (size_t) tcp->iov_size)
- {
- gpr_slice_buffer_add_indexed (tcp->incoming_buffer, gpr_slice_malloc (tcp->slice_size));
- }
- for (i = 0; i < tcp->incoming_buffer->count; i++)
- {
- iov[i].iov_base = GPR_SLICE_START_PTR (tcp->incoming_buffer->slices[i]);
- iov[i].iov_len = GPR_SLICE_LENGTH (tcp->incoming_buffer->slices[i]);
- }
+ while (tcp->incoming_buffer->count < (size_t)tcp->iov_size) {
+ gpr_slice_buffer_add_indexed(tcp->incoming_buffer,
+ gpr_slice_malloc(tcp->slice_size));
+ }
+ for (i = 0; i < tcp->incoming_buffer->count; i++) {
+ iov[i].iov_base = GPR_SLICE_START_PTR(tcp->incoming_buffer->slices[i]);
+ iov[i].iov_len = GPR_SLICE_LENGTH(tcp->incoming_buffer->slices[i]);
+ }
msg.msg_name = NULL;
msg.msg_namelen = 0;
@@ -215,106 +195,83 @@ tcp_continue_read (grpc_exec_ctx * exec_ctx, grpc_tcp * tcp)
msg.msg_controllen = 0;
msg.msg_flags = 0;
- GRPC_TIMER_BEGIN (GRPC_PTAG_RECVMSG, 0);
- do
- {
- read_bytes = recvmsg (tcp->fd, &msg, 0);
- }
- while (read_bytes < 0 && errno == EINTR);
- GRPC_TIMER_END (GRPC_PTAG_RECVMSG, 0);
-
- if (read_bytes < 0)
- {
- /* NB: After calling call_read_cb a parallel call of the read handler may
- * be running. */
- if (errno == EAGAIN)
- {
- if (tcp->iov_size > 1)
- {
- tcp->iov_size /= 2;
- }
- /* We've consumed the edge, request a new one */
- grpc_fd_notify_on_read (exec_ctx, tcp->em_fd, &tcp->read_closure);
- }
- else
- {
- /* TODO(klempner): Log interesting errors */
- gpr_slice_buffer_reset_and_unref (tcp->incoming_buffer);
- call_read_cb (exec_ctx, tcp, 0);
- TCP_UNREF (exec_ctx, tcp, "read");
- }
- }
- else if (read_bytes == 0)
- {
- /* 0 read size ==> end of stream */
- gpr_slice_buffer_reset_and_unref (tcp->incoming_buffer);
- call_read_cb (exec_ctx, tcp, 0);
- TCP_UNREF (exec_ctx, tcp, "read");
+ GRPC_TIMER_BEGIN(GRPC_PTAG_RECVMSG, 0);
+ do {
+ read_bytes = recvmsg(tcp->fd, &msg, 0);
+ } while (read_bytes < 0 && errno == EINTR);
+ GRPC_TIMER_END(GRPC_PTAG_RECVMSG, 0);
+
+ if (read_bytes < 0) {
+ /* NB: After calling call_read_cb a parallel call of the read handler may
+ * be running. */
+ if (errno == EAGAIN) {
+ if (tcp->iov_size > 1) {
+ tcp->iov_size /= 2;
+ }
+ /* We've consumed the edge, request a new one */
+ grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
+ } else {
+ /* TODO(klempner): Log interesting errors */
+ gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer);
+ call_read_cb(exec_ctx, tcp, 0);
+ TCP_UNREF(exec_ctx, tcp, "read");
}
- else
- {
- GPR_ASSERT ((size_t) read_bytes <= tcp->incoming_buffer->length);
- if ((size_t) read_bytes < tcp->incoming_buffer->length)
- {
- gpr_slice_buffer_trim_end (tcp->incoming_buffer, tcp->incoming_buffer->length - (size_t) read_bytes);
- }
- else if (tcp->iov_size < MAX_READ_IOVEC)
- {
- ++tcp->iov_size;
- }
- GPR_ASSERT ((size_t) read_bytes == tcp->incoming_buffer->length);
- call_read_cb (exec_ctx, tcp, 1);
- TCP_UNREF (exec_ctx, tcp, "read");
+ } else if (read_bytes == 0) {
+ /* 0 read size ==> end of stream */
+ gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer);
+ call_read_cb(exec_ctx, tcp, 0);
+ TCP_UNREF(exec_ctx, tcp, "read");
+ } else {
+ GPR_ASSERT((size_t)read_bytes <= tcp->incoming_buffer->length);
+ if ((size_t)read_bytes < tcp->incoming_buffer->length) {
+ gpr_slice_buffer_trim_end(
+ tcp->incoming_buffer,
+ tcp->incoming_buffer->length - (size_t)read_bytes);
+ } else if (tcp->iov_size < MAX_READ_IOVEC) {
+ ++tcp->iov_size;
}
+ GPR_ASSERT((size_t)read_bytes == tcp->incoming_buffer->length);
+ call_read_cb(exec_ctx, tcp, 1);
+ TCP_UNREF(exec_ctx, tcp, "read");
+ }
- GRPC_TIMER_END (GRPC_PTAG_HANDLE_READ, 0);
+ GRPC_TIMER_END(GRPC_PTAG_HANDLE_READ, 0);
}
-static void
-tcp_handle_read (grpc_exec_ctx * exec_ctx, void *arg /* grpc_tcp */ , int success)
-{
- grpc_tcp *tcp = (grpc_tcp *) arg;
- GPR_ASSERT (!tcp->finished_edge);
-
- if (!success)
- {
- gpr_slice_buffer_reset_and_unref (tcp->incoming_buffer);
- call_read_cb (exec_ctx, tcp, 0);
- TCP_UNREF (exec_ctx, tcp, "read");
- }
- else
- {
- tcp_continue_read (exec_ctx, tcp);
- }
+static void tcp_handle_read(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
+ int success) {
+ grpc_tcp *tcp = (grpc_tcp *)arg;
+ GPR_ASSERT(!tcp->finished_edge);
+
+ if (!success) {
+ gpr_slice_buffer_reset_and_unref(tcp->incoming_buffer);
+ call_read_cb(exec_ctx, tcp, 0);
+ TCP_UNREF(exec_ctx, tcp, "read");
+ } else {
+ tcp_continue_read(exec_ctx, tcp);
+ }
}
-static void
-tcp_read (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * incoming_buffer, grpc_closure * cb)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- GPR_ASSERT (tcp->read_cb == NULL);
+static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *incoming_buffer, grpc_closure *cb) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ GPR_ASSERT(tcp->read_cb == NULL);
tcp->read_cb = cb;
tcp->incoming_buffer = incoming_buffer;
- gpr_slice_buffer_reset_and_unref (incoming_buffer);
- TCP_REF (tcp, "read");
- if (tcp->finished_edge)
- {
- tcp->finished_edge = 0;
- grpc_fd_notify_on_read (exec_ctx, tcp->em_fd, &tcp->read_closure);
- }
- else
- {
- grpc_exec_ctx_enqueue (exec_ctx, &tcp->read_closure, 1);
- }
+ gpr_slice_buffer_reset_and_unref(incoming_buffer);
+ TCP_REF(tcp, "read");
+ if (tcp->finished_edge) {
+ tcp->finished_edge = 0;
+ grpc_fd_notify_on_read(exec_ctx, tcp->em_fd, &tcp->read_closure);
+ } else {
+ grpc_exec_ctx_enqueue(exec_ctx, &tcp->read_closure, 1);
+ }
}
-typedef enum
-{ FLUSH_DONE, FLUSH_PENDING, FLUSH_ERROR } flush_result;
+typedef enum { FLUSH_DONE, FLUSH_PENDING, FLUSH_ERROR } flush_result;
#define MAX_WRITE_IOVEC 16
-static flush_result
-tcp_flush (grpc_tcp * tcp)
-{
+static flush_result tcp_flush(grpc_tcp *tcp) {
struct msghdr msg;
struct iovec iov[MAX_WRITE_IOVEC];
msg_iovlen_type iov_size;
@@ -324,189 +281,168 @@ tcp_flush (grpc_tcp * tcp)
size_t unwind_slice_idx;
size_t unwind_byte_idx;
- for (;;)
- {
- sending_length = 0;
- unwind_slice_idx = tcp->outgoing_slice_idx;
- unwind_byte_idx = tcp->outgoing_byte_idx;
- for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count && iov_size != MAX_WRITE_IOVEC; iov_size++)
- {
- iov[iov_size].iov_base = GPR_SLICE_START_PTR (tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) + tcp->outgoing_byte_idx;
- iov[iov_size].iov_len = GPR_SLICE_LENGTH (tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) - tcp->outgoing_byte_idx;
- sending_length += iov[iov_size].iov_len;
- tcp->outgoing_slice_idx++;
- tcp->outgoing_byte_idx = 0;
- }
- GPR_ASSERT (iov_size > 0);
-
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_iov = iov;
- msg.msg_iovlen = iov_size;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_flags = 0;
-
- GRPC_TIMER_BEGIN (GRPC_PTAG_SENDMSG, 0);
- do
- {
- /* TODO(klempner): Cork if this is a partial write */
- sent_length = sendmsg (tcp->fd, &msg, SENDMSG_FLAGS);
- }
- while (sent_length < 0 && errno == EINTR);
- GRPC_TIMER_END (GRPC_PTAG_SENDMSG, 0);
-
- if (sent_length < 0)
- {
- if (errno == EAGAIN)
- {
- tcp->outgoing_slice_idx = unwind_slice_idx;
- tcp->outgoing_byte_idx = unwind_byte_idx;
- return FLUSH_PENDING;
- }
- else
- {
- /* TODO(klempner): Log some of these */
- return FLUSH_ERROR;
- }
- }
-
- GPR_ASSERT (tcp->outgoing_byte_idx == 0);
- trailing = sending_length - (size_t) sent_length;
- while (trailing > 0)
- {
- size_t slice_length;
-
- tcp->outgoing_slice_idx--;
- slice_length = GPR_SLICE_LENGTH (tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]);
- if (slice_length > trailing)
- {
- tcp->outgoing_byte_idx = slice_length - trailing;
- break;
- }
- else
- {
- trailing -= slice_length;
- }
- }
-
- if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count)
- {
- return FLUSH_DONE;
- }
- };
+ for (;;) {
+ sending_length = 0;
+ unwind_slice_idx = tcp->outgoing_slice_idx;
+ unwind_byte_idx = tcp->outgoing_byte_idx;
+ for (iov_size = 0; tcp->outgoing_slice_idx != tcp->outgoing_buffer->count &&
+ iov_size != MAX_WRITE_IOVEC;
+ iov_size++) {
+ iov[iov_size].iov_base =
+ GPR_SLICE_START_PTR(
+ tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) +
+ tcp->outgoing_byte_idx;
+ iov[iov_size].iov_len =
+ GPR_SLICE_LENGTH(
+ tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]) -
+ tcp->outgoing_byte_idx;
+ sending_length += iov[iov_size].iov_len;
+ tcp->outgoing_slice_idx++;
+ tcp->outgoing_byte_idx = 0;
+ }
+ GPR_ASSERT(iov_size > 0);
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = iov;
+ msg.msg_iovlen = iov_size;
+ msg.msg_control = NULL;
+ msg.msg_controllen = 0;
+ msg.msg_flags = 0;
+
+ GRPC_TIMER_BEGIN(GRPC_PTAG_SENDMSG, 0);
+ do {
+ /* TODO(klempner): Cork if this is a partial write */
+ sent_length = sendmsg(tcp->fd, &msg, SENDMSG_FLAGS);
+ } while (sent_length < 0 && errno == EINTR);
+ GRPC_TIMER_END(GRPC_PTAG_SENDMSG, 0);
+
+ if (sent_length < 0) {
+ if (errno == EAGAIN) {
+ tcp->outgoing_slice_idx = unwind_slice_idx;
+ tcp->outgoing_byte_idx = unwind_byte_idx;
+ return FLUSH_PENDING;
+ } else {
+ /* TODO(klempner): Log some of these */
+ return FLUSH_ERROR;
+ }
+ }
+
+ GPR_ASSERT(tcp->outgoing_byte_idx == 0);
+ trailing = sending_length - (size_t)sent_length;
+ while (trailing > 0) {
+ size_t slice_length;
+
+ tcp->outgoing_slice_idx--;
+ slice_length = GPR_SLICE_LENGTH(
+ tcp->outgoing_buffer->slices[tcp->outgoing_slice_idx]);
+ if (slice_length > trailing) {
+ tcp->outgoing_byte_idx = slice_length - trailing;
+ break;
+ } else {
+ trailing -= slice_length;
+ }
+ }
+
+ if (tcp->outgoing_slice_idx == tcp->outgoing_buffer->count) {
+ return FLUSH_DONE;
+ }
+ };
}
-static void
-tcp_handle_write (grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */ , int success)
-{
- grpc_tcp *tcp = (grpc_tcp *) arg;
+static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */,
+ int success) {
+ grpc_tcp *tcp = (grpc_tcp *)arg;
flush_result status;
grpc_closure *cb;
- if (!success)
- {
- cb = tcp->write_cb;
- tcp->write_cb = NULL;
- cb->cb (exec_ctx, cb->cb_arg, 0);
- TCP_UNREF (exec_ctx, tcp, "write");
- return;
- }
-
- GRPC_TIMER_BEGIN (GRPC_PTAG_TCP_CB_WRITE, 0);
- status = tcp_flush (tcp);
- if (status == FLUSH_PENDING)
- {
- grpc_fd_notify_on_write (exec_ctx, tcp->em_fd, &tcp->write_closure);
- }
- else
- {
- cb = tcp->write_cb;
- tcp->write_cb = NULL;
- cb->cb (exec_ctx, cb->cb_arg, status == FLUSH_DONE);
- TCP_UNREF (exec_ctx, tcp, "write");
- }
- GRPC_TIMER_END (GRPC_PTAG_TCP_CB_WRITE, 0);
+ if (!success) {
+ cb = tcp->write_cb;
+ tcp->write_cb = NULL;
+ cb->cb(exec_ctx, cb->cb_arg, 0);
+ TCP_UNREF(exec_ctx, tcp, "write");
+ return;
+ }
+
+ GRPC_TIMER_BEGIN(GRPC_PTAG_TCP_CB_WRITE, 0);
+ status = tcp_flush(tcp);
+ if (status == FLUSH_PENDING) {
+ grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure);
+ } else {
+ cb = tcp->write_cb;
+ tcp->write_cb = NULL;
+ cb->cb(exec_ctx, cb->cb_arg, status == FLUSH_DONE);
+ TCP_UNREF(exec_ctx, tcp, "write");
+ }
+ GRPC_TIMER_END(GRPC_PTAG_TCP_CB_WRITE, 0);
}
-static void
-tcp_write (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, gpr_slice_buffer * buf, grpc_closure * cb)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
+static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *buf, grpc_closure *cb) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
flush_result status;
- if (grpc_tcp_trace)
- {
- size_t i;
+ if (grpc_tcp_trace) {
+ size_t i;
- for (i = 0; i < buf->count; i++)
- {
- char *data = gpr_dump_slice (buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII);
- gpr_log (GPR_DEBUG, "WRITE %p: %s", tcp, data);
- gpr_free (data);
- }
+ for (i = 0; i < buf->count; i++) {
+ char *data =
+ gpr_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ gpr_log(GPR_DEBUG, "WRITE %p: %s", tcp, data);
+ gpr_free(data);
}
+ }
- GRPC_TIMER_BEGIN (GRPC_PTAG_TCP_WRITE, 0);
- GPR_ASSERT (tcp->write_cb == NULL);
+ GRPC_TIMER_BEGIN(GRPC_PTAG_TCP_WRITE, 0);
+ GPR_ASSERT(tcp->write_cb == NULL);
- if (buf->length == 0)
- {
- GRPC_TIMER_END (GRPC_PTAG_TCP_WRITE, 0);
- grpc_exec_ctx_enqueue (exec_ctx, cb, 1);
- return;
- }
+ if (buf->length == 0) {
+ GRPC_TIMER_END(GRPC_PTAG_TCP_WRITE, 0);
+ grpc_exec_ctx_enqueue(exec_ctx, cb, 1);
+ return;
+ }
tcp->outgoing_buffer = buf;
tcp->outgoing_slice_idx = 0;
tcp->outgoing_byte_idx = 0;
- status = tcp_flush (tcp);
- if (status == FLUSH_PENDING)
- {
- TCP_REF (tcp, "write");
- tcp->write_cb = cb;
- grpc_fd_notify_on_write (exec_ctx, tcp->em_fd, &tcp->write_closure);
- }
- else
- {
- grpc_exec_ctx_enqueue (exec_ctx, cb, status == FLUSH_DONE);
- }
+ status = tcp_flush(tcp);
+ if (status == FLUSH_PENDING) {
+ TCP_REF(tcp, "write");
+ tcp->write_cb = cb;
+ grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure);
+ } else {
+ grpc_exec_ctx_enqueue(exec_ctx, cb, status == FLUSH_DONE);
+ }
- GRPC_TIMER_END (GRPC_PTAG_TCP_WRITE, 0);
+ GRPC_TIMER_END(GRPC_PTAG_TCP_WRITE, 0);
}
-static void
-tcp_add_to_pollset (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset * pollset)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- grpc_pollset_add_fd (exec_ctx, pollset, tcp->em_fd);
+static void tcp_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset *pollset) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ grpc_pollset_add_fd(exec_ctx, pollset, tcp->em_fd);
}
-static void
-tcp_add_to_pollset_set (grpc_exec_ctx * exec_ctx, grpc_endpoint * ep, grpc_pollset_set * pollset_set)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- grpc_pollset_set_add_fd (exec_ctx, pollset_set, tcp->em_fd);
+static void tcp_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset_set *pollset_set) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ grpc_pollset_set_add_fd(exec_ctx, pollset_set, tcp->em_fd);
}
-static char *
-tcp_get_peer (grpc_endpoint * ep)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- return gpr_strdup (tcp->peer_string);
+static char *tcp_get_peer(grpc_endpoint *ep) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ return gpr_strdup(tcp->peer_string);
}
static const grpc_endpoint_vtable vtable = {
- tcp_read, tcp_write, tcp_add_to_pollset, tcp_add_to_pollset_set,
- tcp_shutdown, tcp_destroy, tcp_get_peer
-};
-
-grpc_endpoint *
-grpc_tcp_create (grpc_fd * em_fd, size_t slice_size, const char *peer_string)
-{
- grpc_tcp *tcp = (grpc_tcp *) gpr_malloc (sizeof (grpc_tcp));
+ tcp_read, tcp_write, tcp_add_to_pollset, tcp_add_to_pollset_set,
+ tcp_shutdown, tcp_destroy, tcp_get_peer};
+
+grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size,
+ const char *peer_string) {
+ grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
tcp->base.vtable = &vtable;
- tcp->peer_string = gpr_strdup (peer_string);
+ tcp->peer_string = gpr_strdup(peer_string);
tcp->fd = em_fd->fd;
tcp->read_cb = NULL;
tcp->write_cb = NULL;
@@ -515,7 +451,7 @@ grpc_tcp_create (grpc_fd * em_fd, size_t slice_size, const char *peer_string)
tcp->iov_size = 1;
tcp->finished_edge = 1;
/* paired with unref in grpc_tcp_destroy */
- gpr_ref_init (&tcp->refcount, 1);
+ gpr_ref_init(&tcp->refcount, 1);
tcp->em_fd = em_fd;
tcp->read_closure.cb = tcp_handle_read;
tcp->read_closure.cb_arg = tcp;
diff --git a/src/core/iomgr/tcp_posix.h b/src/core/iomgr/tcp_posix.h
index 4f73be4b9d..40b3ae2679 100644
--- a/src/core/iomgr/tcp_posix.h
+++ b/src/core/iomgr/tcp_posix.h
@@ -53,6 +53,7 @@ extern int grpc_tcp_trace;
/* Create a tcp endpoint given a file desciptor and a read slice size.
Takes ownership of fd. */
-grpc_endpoint *grpc_tcp_create (grpc_fd * fd, size_t read_slice_size, const char *peer_string);
+grpc_endpoint *grpc_tcp_create(grpc_fd *fd, size_t read_slice_size,
+ const char *peer_string);
#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_POSIX_H */
diff --git a/src/core/iomgr/tcp_server.h b/src/core/iomgr/tcp_server.h
index ee373a76bf..882635f638 100644
--- a/src/core/iomgr/tcp_server.h
+++ b/src/core/iomgr/tcp_server.h
@@ -40,13 +40,16 @@
typedef struct grpc_tcp_server grpc_tcp_server;
/* Called for newly connected TCP connections. */
-typedef void (*grpc_tcp_server_cb) (grpc_exec_ctx * exec_ctx, void *arg, grpc_endpoint * ep);
+typedef void (*grpc_tcp_server_cb)(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_endpoint *ep);
/* Create a server, initially not bound to any ports */
-grpc_tcp_server *grpc_tcp_server_create (void);
+grpc_tcp_server *grpc_tcp_server_create(void);
/* Start listening to bound ports */
-void grpc_tcp_server_start (grpc_exec_ctx * exec_ctx, grpc_tcp_server * server, grpc_pollset ** pollsets, size_t pollset_count, grpc_tcp_server_cb on_accept_cb, void *cb_arg);
+void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server,
+ grpc_pollset **pollsets, size_t pollset_count,
+ grpc_tcp_server_cb on_accept_cb, void *cb_arg);
/* Add a port to the server, returning port number on success, or negative
on failure.
@@ -59,15 +62,17 @@ void grpc_tcp_server_start (grpc_exec_ctx * exec_ctx, grpc_tcp_server * server,
For raw access to the underlying sockets, see grpc_tcp_server_get_fd(). */
/* TODO(ctiller): deprecate this, and make grpc_tcp_server_add_ports to handle
all of the multiple socket port matching logic in one place */
-int grpc_tcp_server_add_port (grpc_tcp_server * s, const void *addr, size_t addr_len);
+int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
+ size_t addr_len);
/* Returns the file descriptor of the Nth listening socket on this server,
or -1 if the index is out of bounds.
The file descriptor remains owned by the server, and will be cleaned
up when grpc_tcp_server_destroy is called. */
-int grpc_tcp_server_get_fd (grpc_tcp_server * s, unsigned index);
+int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index);
-void grpc_tcp_server_destroy (grpc_exec_ctx * exec_ctx, grpc_tcp_server * server, grpc_closure * closure);
+void grpc_tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *server,
+ grpc_closure *closure);
#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_SERVER_H */
diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c
index 79e8039c79..a582f4a7c3 100644
--- a/src/core/iomgr/tcp_server_posix.c
+++ b/src/core/iomgr/tcp_server_posix.c
@@ -74,13 +74,11 @@ static gpr_once s_init_max_accept_queue_size;
static int s_max_accept_queue_size;
/* one listening port */
-typedef struct
-{
+typedef struct {
int fd;
grpc_fd *emfd;
grpc_tcp_server *server;
- union
- {
+ union {
gpr_uint8 untyped[GRPC_MAX_SOCKADDR_SIZE];
struct sockaddr sockaddr;
struct sockaddr_un un;
@@ -90,20 +88,16 @@ typedef struct
grpc_closure destroyed_closure;
} server_port;
-static void
-unlink_if_unix_domain_socket (const struct sockaddr_un *un)
-{
+static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
struct stat st;
- if (stat (un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK)
- {
- unlink (un->sun_path);
- }
+ if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
+ unlink(un->sun_path);
+ }
}
/* the overall server */
-struct grpc_tcp_server
-{
+struct grpc_tcp_server {
/* Called whenever accept() succeeds on a server port. */
grpc_tcp_server_cb on_accept_cb;
void *on_accept_cb_arg;
@@ -132,321 +126,279 @@ struct grpc_tcp_server
size_t pollset_count;
};
-grpc_tcp_server *
-grpc_tcp_server_create (void)
-{
- grpc_tcp_server *s = gpr_malloc (sizeof (grpc_tcp_server));
- gpr_mu_init (&s->mu);
+grpc_tcp_server *grpc_tcp_server_create(void) {
+ grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
+ gpr_mu_init(&s->mu);
s->active_ports = 0;
s->destroyed_ports = 0;
s->shutdown = 0;
s->on_accept_cb = NULL;
s->on_accept_cb_arg = NULL;
- s->ports = gpr_malloc (sizeof (server_port) * INIT_PORT_CAP);
+ s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
s->nports = 0;
s->port_capacity = INIT_PORT_CAP;
return s;
}
-static void
-finish_shutdown (grpc_exec_ctx * exec_ctx, grpc_tcp_server * s)
-{
- grpc_exec_ctx_enqueue (exec_ctx, s->shutdown_complete, 1);
+static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
+ grpc_exec_ctx_enqueue(exec_ctx, s->shutdown_complete, 1);
- gpr_mu_destroy (&s->mu);
+ gpr_mu_destroy(&s->mu);
- gpr_free (s->ports);
- gpr_free (s);
+ gpr_free(s->ports);
+ gpr_free(s);
}
-static void
-destroyed_port (grpc_exec_ctx * exec_ctx, void *server, int success)
-{
+static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, int success) {
grpc_tcp_server *s = server;
- gpr_mu_lock (&s->mu);
+ gpr_mu_lock(&s->mu);
s->destroyed_ports++;
- if (s->destroyed_ports == s->nports)
- {
- gpr_mu_unlock (&s->mu);
- finish_shutdown (exec_ctx, s);
- }
- else
- {
- GPR_ASSERT (s->destroyed_ports < s->nports);
- gpr_mu_unlock (&s->mu);
- }
+ if (s->destroyed_ports == s->nports) {
+ gpr_mu_unlock(&s->mu);
+ finish_shutdown(exec_ctx, s);
+ } else {
+ GPR_ASSERT(s->destroyed_ports < s->nports);
+ gpr_mu_unlock(&s->mu);
+ }
}
/* called when all listening endpoints have been shutdown, so no further
events will be received on them - at this point it's safe to destroy
things */
-static void
-deactivated_all_ports (grpc_exec_ctx * exec_ctx, grpc_tcp_server * s)
-{
+static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) {
size_t i;
/* delete ALL the things */
- gpr_mu_lock (&s->mu);
-
- if (!s->shutdown)
- {
- gpr_mu_unlock (&s->mu);
- return;
- }
-
- if (s->nports)
- {
- for (i = 0; i < s->nports; i++)
- {
- server_port *sp = &s->ports[i];
- if (sp->addr.sockaddr.sa_family == AF_UNIX)
- {
- unlink_if_unix_domain_socket (&sp->addr.un);
- }
- sp->destroyed_closure.cb = destroyed_port;
- sp->destroyed_closure.cb_arg = s;
- grpc_fd_orphan (exec_ctx, sp->emfd, &sp->destroyed_closure, "tcp_listener_shutdown");
- }
- gpr_mu_unlock (&s->mu);
- }
- else
- {
- gpr_mu_unlock (&s->mu);
- finish_shutdown (exec_ctx, s);
+ gpr_mu_lock(&s->mu);
+
+ if (!s->shutdown) {
+ gpr_mu_unlock(&s->mu);
+ return;
+ }
+
+ if (s->nports) {
+ for (i = 0; i < s->nports; i++) {
+ server_port *sp = &s->ports[i];
+ if (sp->addr.sockaddr.sa_family == AF_UNIX) {
+ unlink_if_unix_domain_socket(&sp->addr.un);
+ }
+ sp->destroyed_closure.cb = destroyed_port;
+ sp->destroyed_closure.cb_arg = s;
+ grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure,
+ "tcp_listener_shutdown");
}
+ gpr_mu_unlock(&s->mu);
+ } else {
+ gpr_mu_unlock(&s->mu);
+ finish_shutdown(exec_ctx, s);
+ }
}
-void
-grpc_tcp_server_destroy (grpc_exec_ctx * exec_ctx, grpc_tcp_server * s, grpc_closure * closure)
-{
+void grpc_tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,
+ grpc_closure *closure) {
size_t i;
- gpr_mu_lock (&s->mu);
+ gpr_mu_lock(&s->mu);
- GPR_ASSERT (!s->shutdown);
+ GPR_ASSERT(!s->shutdown);
s->shutdown = 1;
s->shutdown_complete = closure;
/* shutdown all fd's */
- if (s->active_ports)
- {
- for (i = 0; i < s->nports; i++)
- {
- grpc_fd_shutdown (exec_ctx, s->ports[i].emfd);
- }
- gpr_mu_unlock (&s->mu);
- }
- else
- {
- gpr_mu_unlock (&s->mu);
- deactivated_all_ports (exec_ctx, s);
+ if (s->active_ports) {
+ for (i = 0; i < s->nports; i++) {
+ grpc_fd_shutdown(exec_ctx, s->ports[i].emfd);
}
+ gpr_mu_unlock(&s->mu);
+ } else {
+ gpr_mu_unlock(&s->mu);
+ deactivated_all_ports(exec_ctx, s);
+ }
}
/* get max listen queue size on linux */
-static void
-init_max_accept_queue_size (void)
-{
+static void init_max_accept_queue_size(void) {
int n = SOMAXCONN;
char buf[64];
- FILE *fp = fopen ("/proc/sys/net/core/somaxconn", "r");
- if (fp == NULL)
- {
- /* 2.4 kernel. */
- s_max_accept_queue_size = SOMAXCONN;
- return;
+ FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r");
+ if (fp == NULL) {
+ /* 2.4 kernel. */
+ s_max_accept_queue_size = SOMAXCONN;
+ return;
+ }
+ if (fgets(buf, sizeof buf, fp)) {
+ char *end;
+ long i = strtol(buf, &end, 10);
+ if (i > 0 && i <= INT_MAX && end && *end == 0) {
+ n = (int)i;
}
- if (fgets (buf, sizeof buf, fp))
- {
- char *end;
- long i = strtol (buf, &end, 10);
- if (i > 0 && i <= INT_MAX && end && *end == 0)
- {
- n = (int) i;
- }
- }
- fclose (fp);
+ }
+ fclose(fp);
s_max_accept_queue_size = n;
- if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE)
- {
- gpr_log (GPR_INFO, "Suspiciously small accept queue (%d) will probably lead to " "connection drops", s_max_accept_queue_size);
- }
+ if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
+ gpr_log(GPR_INFO,
+ "Suspiciously small accept queue (%d) will probably lead to "
+ "connection drops",
+ s_max_accept_queue_size);
+ }
}
-static int
-get_max_accept_queue_size (void)
-{
- gpr_once_init (&s_init_max_accept_queue_size, init_max_accept_queue_size);
+static int get_max_accept_queue_size(void) {
+ gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
return s_max_accept_queue_size;
}
/* Prepare a recently-created socket for listening. */
-static int
-prepare_socket (int fd, const struct sockaddr *addr, size_t addr_len)
-{
+static int prepare_socket(int fd, const struct sockaddr *addr,
+ size_t addr_len) {
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
- if (fd < 0)
- {
- goto error;
- }
-
- if (!grpc_set_socket_nonblocking (fd, 1) || !grpc_set_socket_cloexec (fd, 1) || (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency (fd, 1) || !grpc_set_socket_reuse_addr (fd, 1))) || !grpc_set_socket_no_sigpipe_if_possible (fd))
- {
- gpr_log (GPR_ERROR, "Unable to configure socket %d: %s", fd, strerror (errno));
- goto error;
- }
-
- GPR_ASSERT (addr_len < ~(socklen_t) 0);
- if (bind (fd, addr, (socklen_t) addr_len) < 0)
- {
- char *addr_str;
- grpc_sockaddr_to_string (&addr_str, addr, 0);
- gpr_log (GPR_ERROR, "bind addr=%s: %s", addr_str, strerror (errno));
- gpr_free (addr_str);
- goto error;
- }
-
- if (listen (fd, get_max_accept_queue_size ()) < 0)
- {
- gpr_log (GPR_ERROR, "listen: %s", strerror (errno));
- goto error;
- }
-
- sockname_len = sizeof (sockname_temp);
- if (getsockname (fd, (struct sockaddr *) &sockname_temp, &sockname_len) < 0)
- {
- goto error;
- }
-
- return grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
+ if (fd < 0) {
+ goto error;
+ }
+
+ if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
+ (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) ||
+ !grpc_set_socket_reuse_addr(fd, 1))) ||
+ !grpc_set_socket_no_sigpipe_if_possible(fd)) {
+ gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
+ strerror(errno));
+ goto error;
+ }
+
+ GPR_ASSERT(addr_len < ~(socklen_t)0);
+ if (bind(fd, addr, (socklen_t)addr_len) < 0) {
+ char *addr_str;
+ grpc_sockaddr_to_string(&addr_str, addr, 0);
+ gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
+ gpr_free(addr_str);
+ goto error;
+ }
+
+ if (listen(fd, get_max_accept_queue_size()) < 0) {
+ gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
+ goto error;
+ }
+
+ sockname_len = sizeof(sockname_temp);
+ if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
+ goto error;
+ }
+
+ return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
error:
- if (fd >= 0)
- {
- close (fd);
- }
+ if (fd >= 0) {
+ close(fd);
+ }
return -1;
}
/* event manager callback when reads are ready */
-static void
-on_read (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void on_read(grpc_exec_ctx *exec_ctx, void *arg, int success) {
server_port *sp = arg;
grpc_fd *fdobj;
size_t i;
- if (!success)
- {
- goto error;
- }
+ if (!success) {
+ goto error;
+ }
/* loop until accept4 returns EAGAIN, and then re-arm notification */
- for (;;)
- {
- struct sockaddr_storage addr;
- socklen_t addrlen = sizeof (addr);
- char *addr_str;
- char *name;
- /* Note: If we ever decide to return this address to the user, remember to
- strip off the ::ffff:0.0.0.0/96 prefix first. */
- int fd = grpc_accept4 (sp->fd, (struct sockaddr *) &addr, &addrlen, 1, 1);
- if (fd < 0)
- {
- switch (errno)
- {
- case EINTR:
- continue;
- case EAGAIN:
- grpc_fd_notify_on_read (exec_ctx, sp->emfd, &sp->read_closure);
- return;
- default:
- gpr_log (GPR_ERROR, "Failed accept4: %s", strerror (errno));
- goto error;
- }
- }
-
- grpc_set_socket_no_sigpipe_if_possible (fd);
-
- addr_str = grpc_sockaddr_to_uri ((struct sockaddr *) &addr);
- gpr_asprintf (&name, "tcp-server-connection:%s", addr_str);
-
- if (grpc_tcp_trace)
- {
- gpr_log (GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str);
- }
-
- fdobj = grpc_fd_create (fd, name);
- /* TODO(ctiller): revise this when we have server-side sharding
- of channels -- we certainly should not be automatically adding every
- incoming channel to every pollset owned by the server */
- for (i = 0; i < sp->server->pollset_count; i++)
- {
- grpc_pollset_add_fd (exec_ctx, sp->server->pollsets[i], fdobj);
- }
- sp->server->on_accept_cb (exec_ctx, sp->server->on_accept_cb_arg, grpc_tcp_create (fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str));
-
- gpr_free (name);
- gpr_free (addr_str);
+ for (;;) {
+ struct sockaddr_storage addr;
+ socklen_t addrlen = sizeof(addr);
+ char *addr_str;
+ char *name;
+ /* Note: If we ever decide to return this address to the user, remember to
+ strip off the ::ffff:0.0.0.0/96 prefix first. */
+ int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1);
+ if (fd < 0) {
+ switch (errno) {
+ case EINTR:
+ continue;
+ case EAGAIN:
+ grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure);
+ return;
+ default:
+ gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
+ goto error;
+ }
}
- abort ();
+ grpc_set_socket_no_sigpipe_if_possible(fd);
-error:
- gpr_mu_lock (&sp->server->mu);
- if (0 == --sp->server->active_ports)
- {
- gpr_mu_unlock (&sp->server->mu);
- deactivated_all_ports (exec_ctx, sp->server);
+ addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr);
+ gpr_asprintf(&name, "tcp-server-connection:%s", addr_str);
+
+ if (grpc_tcp_trace) {
+ gpr_log(GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str);
}
- else
- {
- gpr_mu_unlock (&sp->server->mu);
+
+ fdobj = grpc_fd_create(fd, name);
+ /* TODO(ctiller): revise this when we have server-side sharding
+ of channels -- we certainly should not be automatically adding every
+ incoming channel to every pollset owned by the server */
+ for (i = 0; i < sp->server->pollset_count; i++) {
+ grpc_pollset_add_fd(exec_ctx, sp->server->pollsets[i], fdobj);
}
+ sp->server->on_accept_cb(
+ exec_ctx, sp->server->on_accept_cb_arg,
+ grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str));
+
+ gpr_free(name);
+ gpr_free(addr_str);
+ }
+
+ abort();
+
+error:
+ gpr_mu_lock(&sp->server->mu);
+ if (0 == --sp->server->active_ports) {
+ gpr_mu_unlock(&sp->server->mu);
+ deactivated_all_ports(exec_ctx, sp->server);
+ } else {
+ gpr_mu_unlock(&sp->server->mu);
+ }
}
-static int
-add_socket_to_server (grpc_tcp_server * s, int fd, const struct sockaddr *addr, size_t addr_len)
-{
+static int add_socket_to_server(grpc_tcp_server *s, int fd,
+ const struct sockaddr *addr, size_t addr_len) {
server_port *sp;
int port;
char *addr_str;
char *name;
- port = prepare_socket (fd, addr, addr_len);
- if (port >= 0)
- {
- grpc_sockaddr_to_string (&addr_str, (struct sockaddr *) &addr, 1);
- gpr_asprintf (&name, "tcp-server-listener:%s", addr_str);
- gpr_mu_lock (&s->mu);
- GPR_ASSERT (!s->on_accept_cb && "must add ports before starting server");
- /* append it to the list under a lock */
- if (s->nports == s->port_capacity)
- {
- s->port_capacity *= 2;
- s->ports = gpr_realloc (s->ports, sizeof (server_port) * s->port_capacity);
- }
- sp = &s->ports[s->nports++];
- sp->server = s;
- sp->fd = fd;
- sp->emfd = grpc_fd_create (fd, name);
- memcpy (sp->addr.untyped, addr, addr_len);
- sp->addr_len = addr_len;
- GPR_ASSERT (sp->emfd);
- gpr_mu_unlock (&s->mu);
- gpr_free (addr_str);
- gpr_free (name);
+ port = prepare_socket(fd, addr, addr_len);
+ if (port >= 0) {
+ grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
+ gpr_asprintf(&name, "tcp-server-listener:%s", addr_str);
+ gpr_mu_lock(&s->mu);
+ GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
+ /* append it to the list under a lock */
+ if (s->nports == s->port_capacity) {
+ s->port_capacity *= 2;
+ s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity);
}
+ sp = &s->ports[s->nports++];
+ sp->server = s;
+ sp->fd = fd;
+ sp->emfd = grpc_fd_create(fd, name);
+ memcpy(sp->addr.untyped, addr, addr_len);
+ sp->addr_len = addr_len;
+ GPR_ASSERT(sp->emfd);
+ gpr_mu_unlock(&s->mu);
+ gpr_free(addr_str);
+ gpr_free(name);
+ }
return port;
}
-int
-grpc_tcp_server_add_port (grpc_tcp_server * s, const void *addr, size_t addr_len)
-{
+int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
+ size_t addr_len) {
int allocated_port1 = -1;
int allocated_port2 = -1;
unsigned i;
@@ -461,110 +413,99 @@ grpc_tcp_server_add_port (grpc_tcp_server * s, const void *addr, size_t addr_len
socklen_t sockname_len;
int port;
- if (((struct sockaddr *) addr)->sa_family == AF_UNIX)
- {
- unlink_if_unix_domain_socket (addr);
- }
+ if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
+ unlink_if_unix_domain_socket(addr);
+ }
/* Check if this is a wildcard port, and if so, try to keep the port the same
as some previously created listener. */
- if (grpc_sockaddr_get_port (addr) == 0)
- {
- for (i = 0; i < s->nports; i++)
- {
- sockname_len = sizeof (sockname_temp);
- if (0 == getsockname (s->ports[i].fd, (struct sockaddr *) &sockname_temp, &sockname_len))
- {
- port = grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
- if (port > 0)
- {
- allocated_addr = malloc (addr_len);
- memcpy (allocated_addr, addr, addr_len);
- grpc_sockaddr_set_port (allocated_addr, port);
- addr = allocated_addr;
- break;
- }
- }
- }
+ if (grpc_sockaddr_get_port(addr) == 0) {
+ for (i = 0; i < s->nports; i++) {
+ sockname_len = sizeof(sockname_temp);
+ if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp,
+ &sockname_len)) {
+ port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
+ if (port > 0) {
+ allocated_addr = malloc(addr_len);
+ memcpy(allocated_addr, addr, addr_len);
+ grpc_sockaddr_set_port(allocated_addr, port);
+ addr = allocated_addr;
+ break;
+ }
+ }
}
+ }
- if (grpc_sockaddr_to_v4mapped (addr, &addr6_v4mapped))
- {
- addr = (const struct sockaddr *) &addr6_v4mapped;
- addr_len = sizeof (addr6_v4mapped);
- }
+ if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
+ addr = (const struct sockaddr *)&addr6_v4mapped;
+ addr_len = sizeof(addr6_v4mapped);
+ }
/* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
- if (grpc_sockaddr_is_wildcard (addr, &port))
- {
- grpc_sockaddr_make_wildcards (port, &wild4, &wild6);
-
- /* Try listening on IPv6 first. */
- addr = (struct sockaddr *) &wild6;
- addr_len = sizeof (wild6);
- fd = grpc_create_dualstack_socket (addr, SOCK_STREAM, 0, &dsmode);
- allocated_port1 = add_socket_to_server (s, fd, addr, addr_len);
- if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK)
- {
- goto done;
- }
-
- /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
- if (port == 0 && allocated_port1 > 0)
- {
- grpc_sockaddr_set_port ((struct sockaddr *) &wild4, allocated_port1);
- }
- addr = (struct sockaddr *) &wild4;
- addr_len = sizeof (wild4);
+ if (grpc_sockaddr_is_wildcard(addr, &port)) {
+ grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
+
+ /* Try listening on IPv6 first. */
+ addr = (struct sockaddr *)&wild6;
+ addr_len = sizeof(wild6);
+ fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
+ allocated_port1 = add_socket_to_server(s, fd, addr, addr_len);
+ if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
+ goto done;
}
- fd = grpc_create_dualstack_socket (addr, SOCK_STREAM, 0, &dsmode);
- if (fd < 0)
- {
- gpr_log (GPR_ERROR, "Unable to create socket: %s", strerror (errno));
- }
- if (dsmode == GRPC_DSMODE_IPV4 && grpc_sockaddr_is_v4mapped (addr, &addr4_copy))
- {
- addr = (struct sockaddr *) &addr4_copy;
- addr_len = sizeof (addr4_copy);
+ /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
+ if (port == 0 && allocated_port1 > 0) {
+ grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1);
}
- allocated_port2 = add_socket_to_server (s, fd, addr, addr_len);
+ addr = (struct sockaddr *)&wild4;
+ addr_len = sizeof(wild4);
+ }
+
+ fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
+ if (fd < 0) {
+ gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
+ }
+ if (dsmode == GRPC_DSMODE_IPV4 &&
+ grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
+ addr = (struct sockaddr *)&addr4_copy;
+ addr_len = sizeof(addr4_copy);
+ }
+ allocated_port2 = add_socket_to_server(s, fd, addr, addr_len);
done:
- gpr_free (allocated_addr);
+ gpr_free(allocated_addr);
return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
}
-int
-grpc_tcp_server_get_fd (grpc_tcp_server * s, unsigned index)
-{
+int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index) {
return (index < s->nports) ? s->ports[index].fd : -1;
}
-void
-grpc_tcp_server_start (grpc_exec_ctx * exec_ctx, grpc_tcp_server * s, grpc_pollset ** pollsets, size_t pollset_count, grpc_tcp_server_cb on_accept_cb, void *on_accept_cb_arg)
-{
+void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,
+ grpc_pollset **pollsets, size_t pollset_count,
+ grpc_tcp_server_cb on_accept_cb,
+ void *on_accept_cb_arg) {
size_t i, j;
- GPR_ASSERT (on_accept_cb);
- gpr_mu_lock (&s->mu);
- GPR_ASSERT (!s->on_accept_cb);
- GPR_ASSERT (s->active_ports == 0);
+ GPR_ASSERT(on_accept_cb);
+ gpr_mu_lock(&s->mu);
+ GPR_ASSERT(!s->on_accept_cb);
+ GPR_ASSERT(s->active_ports == 0);
s->on_accept_cb = on_accept_cb;
s->on_accept_cb_arg = on_accept_cb_arg;
s->pollsets = pollsets;
s->pollset_count = pollset_count;
- for (i = 0; i < s->nports; i++)
- {
- for (j = 0; j < pollset_count; j++)
- {
- grpc_pollset_add_fd (exec_ctx, pollsets[j], s->ports[i].emfd);
- }
- s->ports[i].read_closure.cb = on_read;
- s->ports[i].read_closure.cb_arg = &s->ports[i];
- grpc_fd_notify_on_read (exec_ctx, s->ports[i].emfd, &s->ports[i].read_closure);
- s->active_ports++;
+ for (i = 0; i < s->nports; i++) {
+ for (j = 0; j < pollset_count; j++) {
+ grpc_pollset_add_fd(exec_ctx, pollsets[j], s->ports[i].emfd);
}
- gpr_mu_unlock (&s->mu);
+ s->ports[i].read_closure.cb = on_read;
+ s->ports[i].read_closure.cb_arg = &s->ports[i];
+ grpc_fd_notify_on_read(exec_ctx, s->ports[i].emfd,
+ &s->ports[i].read_closure);
+ s->active_ports++;
+ }
+ gpr_mu_unlock(&s->mu);
}
#endif
diff --git a/src/core/iomgr/tcp_server_windows.c b/src/core/iomgr/tcp_server_windows.c
index 9d4e0e1531..be2c0055db 100644
--- a/src/core/iomgr/tcp_server_windows.c
+++ b/src/core/iomgr/tcp_server_windows.c
@@ -55,11 +55,10 @@
#define MIN_SAFE_ACCEPT_QUEUE_SIZE 100
/* one listening port */
-typedef struct server_port
-{
+typedef struct server_port {
/* This seemingly magic number comes from AcceptEx's documentation. each
address buffer needs to have at least 16 more bytes at their end. */
- gpr_uint8 addresses[(sizeof (struct sockaddr_in6) + 16) * 2];
+ gpr_uint8 addresses[(sizeof(struct sockaddr_in6) + 16) * 2];
/* This will hold the socket for the next accept. */
SOCKET new_socket;
/* The listener winsocked. */
@@ -71,8 +70,7 @@ typedef struct server_port
} server_port;
/* the overall server */
-struct grpc_tcp_server
-{
+struct grpc_tcp_server {
/* Called whenever accept() succeeds on a server port. */
grpc_tcp_server_cb on_accept_cb;
void *on_accept_cb_arg;
@@ -88,226 +86,201 @@ struct grpc_tcp_server
size_t port_capacity;
/* shutdown callback */
- void (*shutdown_complete) (void *);
+ void (*shutdown_complete)(void *);
void *shutdown_complete_arg;
};
/* Public function. Allocates the proper data structures to hold a
grpc_tcp_server. */
-grpc_tcp_server *
-grpc_tcp_server_create (void)
-{
- grpc_tcp_server *s = gpr_malloc (sizeof (grpc_tcp_server));
- gpr_mu_init (&s->mu);
+grpc_tcp_server *grpc_tcp_server_create(void) {
+ grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
+ gpr_mu_init(&s->mu);
s->active_ports = 0;
s->on_accept_cb = NULL;
s->on_accept_cb_arg = NULL;
- s->ports = gpr_malloc (sizeof (server_port) * INIT_PORT_CAP);
+ s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
s->nports = 0;
s->port_capacity = INIT_PORT_CAP;
s->shutdown_complete = NULL;
return s;
}
-static void
-dont_care_about_shutdown_completion (void *arg)
-{
-}
+static void dont_care_about_shutdown_completion(void *arg) {}
-static void
-finish_shutdown (grpc_tcp_server * s)
-{
+static void finish_shutdown(grpc_tcp_server *s) {
size_t i;
- s->shutdown_complete (s->shutdown_complete_arg);
+ s->shutdown_complete(s->shutdown_complete_arg);
/* Now that the accepts have been aborted, we can destroy the sockets.
The IOCP won't get notified on these, so we can flag them as already
closed by the system. */
- for (i = 0; i < s->nports; i++)
- {
- server_port *sp = &s->ports[i];
- grpc_winsocket_destroy (sp->socket);
- }
- gpr_free (s->ports);
- gpr_free (s);
+ for (i = 0; i < s->nports; i++) {
+ server_port *sp = &s->ports[i];
+ grpc_winsocket_destroy(sp->socket);
+ }
+ gpr_free(s->ports);
+ gpr_free(s);
}
/* Public function. Stops and destroys a grpc_tcp_server. */
-void
-grpc_tcp_server_destroy (grpc_tcp_server * s, void (*shutdown_complete) (void *shutdown_done_arg), void *shutdown_complete_arg)
-{
+void grpc_tcp_server_destroy(grpc_tcp_server *s,
+ void (*shutdown_complete)(void *shutdown_done_arg),
+ void *shutdown_complete_arg) {
size_t i;
int immediately_done = 0;
- gpr_mu_lock (&s->mu);
+ gpr_mu_lock(&s->mu);
- s->shutdown_complete = shutdown_complete ? shutdown_complete : dont_care_about_shutdown_completion;
+ s->shutdown_complete = shutdown_complete
+ ? shutdown_complete
+ : dont_care_about_shutdown_completion;
s->shutdown_complete_arg = shutdown_complete_arg;
/* First, shutdown all fd's. This will queue abortion calls for all
of the pending accepts due to the normal operation mechanism. */
- if (s->active_ports == 0)
- {
- immediately_done = 1;
- }
- for (i = 0; i < s->nports; i++)
- {
- server_port *sp = &s->ports[i];
- sp->shutting_down = 1;
- grpc_winsocket_shutdown (sp->socket);
- }
- gpr_mu_unlock (&s->mu);
-
- if (immediately_done)
- {
- finish_shutdown (s);
- }
+ if (s->active_ports == 0) {
+ immediately_done = 1;
+ }
+ for (i = 0; i < s->nports; i++) {
+ server_port *sp = &s->ports[i];
+ sp->shutting_down = 1;
+ grpc_winsocket_shutdown(sp->socket);
+ }
+ gpr_mu_unlock(&s->mu);
+
+ if (immediately_done) {
+ finish_shutdown(s);
+ }
}
/* Prepare (bind) a recently-created socket for listening. */
-static int
-prepare_socket (SOCKET sock, const struct sockaddr *addr, int addr_len)
-{
+static int prepare_socket(SOCKET sock, const struct sockaddr *addr,
+ int addr_len) {
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
- if (sock == INVALID_SOCKET)
- goto error;
+ if (sock == INVALID_SOCKET) goto error;
- if (!grpc_tcp_prepare_socket (sock))
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "Unable to prepare socket: %s", utf8_message);
- gpr_free (utf8_message);
- goto error;
- }
-
- if (bind (sock, addr, addr_len) == SOCKET_ERROR)
- {
- char *addr_str;
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- grpc_sockaddr_to_string (&addr_str, addr, 0);
- gpr_log (GPR_ERROR, "bind addr=%s: %s", addr_str, utf8_message);
- gpr_free (utf8_message);
- gpr_free (addr_str);
- goto error;
- }
-
- if (listen (sock, SOMAXCONN) == SOCKET_ERROR)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "listen: %s", utf8_message);
- gpr_free (utf8_message);
- goto error;
- }
+ if (!grpc_tcp_prepare_socket(sock)) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "Unable to prepare socket: %s", utf8_message);
+ gpr_free(utf8_message);
+ goto error;
+ }
+
+ if (bind(sock, addr, addr_len) == SOCKET_ERROR) {
+ char *addr_str;
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ grpc_sockaddr_to_string(&addr_str, addr, 0);
+ gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, utf8_message);
+ gpr_free(utf8_message);
+ gpr_free(addr_str);
+ goto error;
+ }
- sockname_len = sizeof (sockname_temp);
- if (getsockname (sock, (struct sockaddr *) &sockname_temp, &sockname_len) == SOCKET_ERROR)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "getsockname: %s", utf8_message);
- gpr_free (utf8_message);
- goto error;
- }
+ if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "listen: %s", utf8_message);
+ gpr_free(utf8_message);
+ goto error;
+ }
+
+ sockname_len = sizeof(sockname_temp);
+ if (getsockname(sock, (struct sockaddr *)&sockname_temp, &sockname_len) ==
+ SOCKET_ERROR) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "getsockname: %s", utf8_message);
+ gpr_free(utf8_message);
+ goto error;
+ }
- return grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
+ return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
error:
- if (sock != INVALID_SOCKET)
- closesocket (sock);
+ if (sock != INVALID_SOCKET) closesocket(sock);
return -1;
}
-static void
-decrement_active_ports_and_notify (server_port * sp)
-{
+static void decrement_active_ports_and_notify(server_port *sp) {
int notify = 0;
sp->shutting_down = 0;
- gpr_mu_lock (&sp->server->mu);
- GPR_ASSERT (sp->server->active_ports > 0);
- if (0 == --sp->server->active_ports && sp->server->shutdown_complete != NULL)
- {
- notify = 1;
- }
- gpr_mu_unlock (&sp->server->mu);
- if (notify)
- {
- finish_shutdown (sp->server);
- }
+ gpr_mu_lock(&sp->server->mu);
+ GPR_ASSERT(sp->server->active_ports > 0);
+ if (0 == --sp->server->active_ports &&
+ sp->server->shutdown_complete != NULL) {
+ notify = 1;
+ }
+ gpr_mu_unlock(&sp->server->mu);
+ if (notify) {
+ finish_shutdown(sp->server);
+ }
}
/* start_accept will reference that for the IOCP notification request. */
-static void on_accept (void *arg, int from_iocp);
+static void on_accept(void *arg, int from_iocp);
/* In order to do an async accept, we need to create a socket first which
will be the one assigned to the new incoming connection. */
-static void
-start_accept (server_port * port)
-{
+static void start_accept(server_port *port) {
SOCKET sock = INVALID_SOCKET;
char *message;
char *utf8_message;
BOOL success;
- DWORD addrlen = sizeof (struct sockaddr_in6) + 16;
+ DWORD addrlen = sizeof(struct sockaddr_in6) + 16;
DWORD bytes_received = 0;
- sock = WSASocket (AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
+ sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
+ WSA_FLAG_OVERLAPPED);
- if (sock == INVALID_SOCKET)
- {
- message = "Unable to create socket: %s";
- goto failure;
- }
+ if (sock == INVALID_SOCKET) {
+ message = "Unable to create socket: %s";
+ goto failure;
+ }
- if (!grpc_tcp_prepare_socket (sock))
- {
- message = "Unable to prepare socket: %s";
- goto failure;
- }
+ if (!grpc_tcp_prepare_socket(sock)) {
+ message = "Unable to prepare socket: %s";
+ goto failure;
+ }
/* Start the "accept" asynchronously. */
- success = port->AcceptEx (port->socket->socket, sock, port->addresses, 0, addrlen, addrlen, &bytes_received, &port->socket->read_info.overlapped);
+ success = port->AcceptEx(port->socket->socket, sock, port->addresses, 0,
+ addrlen, addrlen, &bytes_received,
+ &port->socket->read_info.overlapped);
/* It is possible to get an accept immediately without delay. However, we
will still get an IOCP notification for it. So let's just ignore it. */
- if (!success)
- {
- int error = WSAGetLastError ();
- if (error != ERROR_IO_PENDING)
- {
- message = "AcceptEx failed: %s";
- goto failure;
- }
+ if (!success) {
+ int error = WSAGetLastError();
+ if (error != ERROR_IO_PENDING) {
+ message = "AcceptEx failed: %s";
+ goto failure;
}
+ }
/* We're ready to do the accept. Calling grpc_socket_notify_on_read may
immediately process an accept that happened in the meantime. */
port->new_socket = sock;
- grpc_socket_notify_on_read (port->socket, on_accept, port);
+ grpc_socket_notify_on_read(port->socket, on_accept, port);
return;
failure:
- if (port->shutting_down)
- {
- /* We are abandoning the listener port, take that into account to prevent
- occasional hangs on shutdown. The hang happens when sp->shutting_down
- change is not seen by on_accept and we proceed to trying new accept,
- but we fail there because the listening port has been closed in the
- meantime. */
- decrement_active_ports_and_notify (port);
- return;
- }
- utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, message, utf8_message);
- gpr_free (utf8_message);
- if (sock != INVALID_SOCKET)
- closesocket (sock);
+ if (port->shutting_down) {
+ /* We are abandoning the listener port, take that into account to prevent
+ occasional hangs on shutdown. The hang happens when sp->shutting_down
+ change is not seen by on_accept and we proceed to trying new accept,
+ but we fail there because the listening port has been closed in the
+ meantime. */
+ decrement_active_ports_and_notify(port);
+ return;
+ }
+ utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, message, utf8_message);
+ gpr_free(utf8_message);
+ if (sock != INVALID_SOCKET) closesocket(sock);
}
/* Event manager callback when reads are ready. */
-static void
-on_accept (void *arg, int from_iocp)
-{
+static void on_accept(void *arg, int from_iocp) {
server_port *sp = arg;
SOCKET sock = sp->new_socket;
grpc_winsocket_callback_info *info = &sp->socket->read_info;
@@ -315,7 +288,7 @@ on_accept (void *arg, int from_iocp)
struct sockaddr_storage peer_name;
char *peer_name_string;
char *fd_name;
- int peer_name_len = sizeof (peer_name);
+ int peer_name_len = sizeof(peer_name);
DWORD transfered_bytes;
DWORD flags;
BOOL wsa_success;
@@ -324,76 +297,65 @@ on_accept (void *arg, int from_iocp)
/* The general mechanism for shutting down is to queue abortion calls. While
this is necessary in the read/write case, it's useless for the accept
case. We only need to adjust the pending callback count */
- if (!from_iocp)
- {
- return;
- }
+ if (!from_iocp) {
+ return;
+ }
/* The IOCP notified us of a completed operation. Let's grab the results,
and act accordingly. */
transfered_bytes = 0;
- wsa_success = WSAGetOverlappedResult (sock, &info->overlapped, &transfered_bytes, FALSE, &flags);
- if (!wsa_success)
- {
- if (sp->shutting_down)
- {
- /* During the shutdown case, we ARE expecting an error. So that's well,
- and we can wake up the shutdown thread. */
- decrement_active_ports_and_notify (sp);
- return;
- }
- else
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "on_accept error: %s", utf8_message);
- gpr_free (utf8_message);
- closesocket (sock);
- }
+ wsa_success = WSAGetOverlappedResult(sock, &info->overlapped,
+ &transfered_bytes, FALSE, &flags);
+ if (!wsa_success) {
+ if (sp->shutting_down) {
+ /* During the shutdown case, we ARE expecting an error. So that's well,
+ and we can wake up the shutdown thread. */
+ decrement_active_ports_and_notify(sp);
+ return;
+ } else {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "on_accept error: %s", utf8_message);
+ gpr_free(utf8_message);
+ closesocket(sock);
}
- else
- {
- if (!sp->shutting_down)
- {
- peer_name_string = NULL;
- err = setsockopt (sock, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *) &sp->socket->socket, sizeof (sp->socket->socket));
- if (err)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "setsockopt error: %s", utf8_message);
- gpr_free (utf8_message);
- }
- err = getpeername (sock, (struct sockaddr *) &peer_name, &peer_name_len);
- if (!err)
- {
- peer_name_string = grpc_sockaddr_to_uri ((struct sockaddr *) &peer_name);
- }
- else
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "getpeername error: %s", utf8_message);
- gpr_free (utf8_message);
- }
- gpr_asprintf (&fd_name, "tcp_server:%s", peer_name_string);
- ep = grpc_tcp_create (grpc_winsocket_create (sock, fd_name), peer_name_string);
- gpr_free (fd_name);
- gpr_free (peer_name_string);
- }
+ } else {
+ if (!sp->shutting_down) {
+ peer_name_string = NULL;
+ err = setsockopt(sock, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
+ (char *)&sp->socket->socket, sizeof(sp->socket->socket));
+ if (err) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "setsockopt error: %s", utf8_message);
+ gpr_free(utf8_message);
+ }
+ err = getpeername(sock, (struct sockaddr *)&peer_name, &peer_name_len);
+ if (!err) {
+ peer_name_string = grpc_sockaddr_to_uri((struct sockaddr *)&peer_name);
+ } else {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "getpeername error: %s", utf8_message);
+ gpr_free(utf8_message);
+ }
+ gpr_asprintf(&fd_name, "tcp_server:%s", peer_name_string);
+ ep = grpc_tcp_create(grpc_winsocket_create(sock, fd_name),
+ peer_name_string);
+ gpr_free(fd_name);
+ gpr_free(peer_name_string);
}
+ }
/* The only time we should call our callback, is where we successfully
managed to accept a connection, and created an endpoint. */
- if (ep)
- sp->server->on_accept_cb (sp->server->on_accept_cb_arg, ep);
+ if (ep) sp->server->on_accept_cb(sp->server->on_accept_cb_arg, ep);
/* As we were notified from the IOCP of one and exactly one accept,
the former socked we created has now either been destroy or assigned
to the new connection. We need to create a new one for the next
connection. */
- start_accept (sp);
+ start_accept(sp);
}
-static int
-add_socket_to_server (grpc_tcp_server * s, SOCKET sock, const struct sockaddr *addr, int addr_len)
-{
+static int add_socket_to_server(grpc_tcp_server *s, SOCKET sock,
+ const struct sockaddr *addr, int addr_len) {
server_port *sp;
int port;
int status;
@@ -401,49 +363,46 @@ add_socket_to_server (grpc_tcp_server * s, SOCKET sock, const struct sockaddr *a
DWORD ioctl_num_bytes;
LPFN_ACCEPTEX AcceptEx;
- if (sock == INVALID_SOCKET)
- return -1;
+ if (sock == INVALID_SOCKET) return -1;
/* We need to grab the AcceptEx pointer for that port, as it may be
interface-dependent. We'll cache it to avoid doing that again. */
- status = WSAIoctl (sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof (guid), &AcceptEx, sizeof (AcceptEx), &ioctl_num_bytes, NULL, NULL);
-
- if (status != 0)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "on_connect error: %s", utf8_message);
- gpr_free (utf8_message);
- closesocket (sock);
- return -1;
- }
-
- port = prepare_socket (sock, addr, addr_len);
- if (port >= 0)
- {
- gpr_mu_lock (&s->mu);
- GPR_ASSERT (!s->on_accept_cb && "must add ports before starting server");
- /* append it to the list under a lock */
- if (s->nports == s->port_capacity)
- {
- s->port_capacity *= 2;
- s->ports = gpr_realloc (s->ports, sizeof (server_port) * s->port_capacity);
- }
- sp = &s->ports[s->nports++];
- sp->server = s;
- sp->socket = grpc_winsocket_create (sock, "listener");
- sp->shutting_down = 0;
- sp->AcceptEx = AcceptEx;
- sp->new_socket = INVALID_SOCKET;
- GPR_ASSERT (sp->socket);
- gpr_mu_unlock (&s->mu);
+ status =
+ WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid),
+ &AcceptEx, sizeof(AcceptEx), &ioctl_num_bytes, NULL, NULL);
+
+ if (status != 0) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "on_connect error: %s", utf8_message);
+ gpr_free(utf8_message);
+ closesocket(sock);
+ return -1;
+ }
+
+ port = prepare_socket(sock, addr, addr_len);
+ if (port >= 0) {
+ gpr_mu_lock(&s->mu);
+ GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
+ /* append it to the list under a lock */
+ if (s->nports == s->port_capacity) {
+ s->port_capacity *= 2;
+ s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity);
}
+ sp = &s->ports[s->nports++];
+ sp->server = s;
+ sp->socket = grpc_winsocket_create(sock, "listener");
+ sp->shutting_down = 0;
+ sp->AcceptEx = AcceptEx;
+ sp->new_socket = INVALID_SOCKET;
+ GPR_ASSERT(sp->socket);
+ gpr_mu_unlock(&s->mu);
+ }
return port;
}
-int
-grpc_tcp_server_add_port (grpc_tcp_server * s, const void *addr, int addr_len)
-{
+int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr,
+ int addr_len) {
int allocated_port = -1;
unsigned i;
SOCKET sock;
@@ -456,77 +415,71 @@ grpc_tcp_server_add_port (grpc_tcp_server * s, const void *addr, int addr_len)
/* Check if this is a wildcard port, and if so, try to keep the port the same
as some previously created listener. */
- if (grpc_sockaddr_get_port (addr) == 0)
- {
- for (i = 0; i < s->nports; i++)
- {
- sockname_len = sizeof (sockname_temp);
- if (0 == getsockname (s->ports[i].socket->socket, (struct sockaddr *) &sockname_temp, &sockname_len))
- {
- port = grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
- if (port > 0)
- {
- allocated_addr = malloc (addr_len);
- memcpy (allocated_addr, addr, addr_len);
- grpc_sockaddr_set_port (allocated_addr, port);
- addr = allocated_addr;
- break;
- }
- }
- }
+ if (grpc_sockaddr_get_port(addr) == 0) {
+ for (i = 0; i < s->nports; i++) {
+ sockname_len = sizeof(sockname_temp);
+ if (0 == getsockname(s->ports[i].socket->socket,
+ (struct sockaddr *)&sockname_temp, &sockname_len)) {
+ port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
+ if (port > 0) {
+ allocated_addr = malloc(addr_len);
+ memcpy(allocated_addr, addr, addr_len);
+ grpc_sockaddr_set_port(allocated_addr, port);
+ addr = allocated_addr;
+ break;
+ }
+ }
}
+ }
- if (grpc_sockaddr_to_v4mapped (addr, &addr6_v4mapped))
- {
- addr = (const struct sockaddr *) &addr6_v4mapped;
- addr_len = sizeof (addr6_v4mapped);
- }
+ if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
+ addr = (const struct sockaddr *)&addr6_v4mapped;
+ addr_len = sizeof(addr6_v4mapped);
+ }
/* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
- if (grpc_sockaddr_is_wildcard (addr, &port))
- {
- grpc_sockaddr_make_wildcard6 (port, &wildcard);
+ if (grpc_sockaddr_is_wildcard(addr, &port)) {
+ grpc_sockaddr_make_wildcard6(port, &wildcard);
- addr = (struct sockaddr *) &wildcard;
- addr_len = sizeof (wildcard);
- }
+ addr = (struct sockaddr *)&wildcard;
+ addr_len = sizeof(wildcard);
+ }
- sock = WSASocket (AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
- if (sock == INVALID_SOCKET)
- {
- char *utf8_message = gpr_format_message (WSAGetLastError ());
- gpr_log (GPR_ERROR, "unable to create socket: %s", utf8_message);
- gpr_free (utf8_message);
- }
+ sock = WSASocket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
+ WSA_FLAG_OVERLAPPED);
+ if (sock == INVALID_SOCKET) {
+ char *utf8_message = gpr_format_message(WSAGetLastError());
+ gpr_log(GPR_ERROR, "unable to create socket: %s", utf8_message);
+ gpr_free(utf8_message);
+ }
- allocated_port = add_socket_to_server (s, sock, addr, addr_len);
- gpr_free (allocated_addr);
+ allocated_port = add_socket_to_server(s, sock, addr, addr_len);
+ gpr_free(allocated_addr);
return allocated_port;
}
SOCKET
-grpc_tcp_server_get_socket (grpc_tcp_server * s, unsigned index)
-{
+grpc_tcp_server_get_socket(grpc_tcp_server *s, unsigned index) {
return (index < s->nports) ? s->ports[index].socket->socket : INVALID_SOCKET;
}
-void
-grpc_tcp_server_start (grpc_tcp_server * s, grpc_pollset ** pollset, size_t pollset_count, grpc_tcp_server_cb on_accept_cb, void *on_accept_cb_arg)
-{
+void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset **pollset,
+ size_t pollset_count,
+ grpc_tcp_server_cb on_accept_cb,
+ void *on_accept_cb_arg) {
size_t i;
- GPR_ASSERT (on_accept_cb);
- gpr_mu_lock (&s->mu);
- GPR_ASSERT (!s->on_accept_cb);
- GPR_ASSERT (s->active_ports == 0);
+ GPR_ASSERT(on_accept_cb);
+ gpr_mu_lock(&s->mu);
+ GPR_ASSERT(!s->on_accept_cb);
+ GPR_ASSERT(s->active_ports == 0);
s->on_accept_cb = on_accept_cb;
s->on_accept_cb_arg = on_accept_cb_arg;
- for (i = 0; i < s->nports; i++)
- {
- start_accept (s->ports + i);
- s->active_ports++;
- }
- gpr_mu_unlock (&s->mu);
+ for (i = 0; i < s->nports; i++) {
+ start_accept(s->ports + i);
+ s->active_ports++;
+ }
+ gpr_mu_unlock(&s->mu);
}
#endif /* GPR_WINSOCK_SOCKET */
diff --git a/src/core/iomgr/tcp_windows.c b/src/core/iomgr/tcp_windows.c
index 9837b026b1..ab309180eb 100644
--- a/src/core/iomgr/tcp_windows.c
+++ b/src/core/iomgr/tcp_windows.c
@@ -51,37 +51,30 @@
#include "src/core/iomgr/socket_windows.h"
#include "src/core/iomgr/tcp_client.h"
-static int
-set_non_block (SOCKET sock)
-{
+static int set_non_block(SOCKET sock) {
int status;
unsigned long param = 1;
DWORD ret;
- status = WSAIoctl (sock, FIONBIO, &param, sizeof (param), NULL, 0, &ret, NULL, NULL);
+ status =
+ WSAIoctl(sock, FIONBIO, &param, sizeof(param), NULL, 0, &ret, NULL, NULL);
return status == 0;
}
-static int
-set_dualstack (SOCKET sock)
-{
+static int set_dualstack(SOCKET sock) {
int status;
unsigned long param = 0;
- status = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *) &param, sizeof (param));
+ status = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&param,
+ sizeof(param));
return status == 0;
}
-int
-grpc_tcp_prepare_socket (SOCKET sock)
-{
- if (!set_non_block (sock))
- return 0;
- if (!set_dualstack (sock))
- return 0;
+int grpc_tcp_prepare_socket(SOCKET sock) {
+ if (!set_non_block(sock)) return 0;
+ if (!set_dualstack(sock)) return 0;
return 1;
}
-typedef struct grpc_tcp
-{
+typedef struct grpc_tcp {
/* This is our C++ class derivation emulation. */
grpc_endpoint base;
/* The one socket this endpoint is using. */
@@ -103,58 +96,46 @@ typedef struct grpc_tcp
char *peer_string;
} grpc_tcp;
-static void
-tcp_free (grpc_tcp * tcp)
-{
- grpc_winsocket_destroy (tcp->socket);
- gpr_mu_destroy (&tcp->mu);
- gpr_free (tcp->peer_string);
- gpr_free (tcp);
+static void tcp_free(grpc_tcp *tcp) {
+ grpc_winsocket_destroy(tcp->socket);
+ gpr_mu_destroy(&tcp->mu);
+ gpr_free(tcp->peer_string);
+ gpr_free(tcp);
}
/*#define GRPC_TCP_REFCOUNT_DEBUG*/
#ifdef GRPC_TCP_REFCOUNT_DEBUG
#define TCP_UNREF(tcp, reason) tcp_unref((tcp), (reason), __FILE__, __LINE__)
#define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__)
-static void
-tcp_unref (grpc_tcp * tcp, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp, reason, tcp->refcount.count, tcp->refcount.count - 1);
- if (gpr_unref (&tcp->refcount))
- {
- tcp_free (tcp);
- }
+static void tcp_unref(grpc_tcp *tcp, const char *reason, const char *file,
+ int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp,
+ reason, tcp->refcount.count, tcp->refcount.count - 1);
+ if (gpr_unref(&tcp->refcount)) {
+ tcp_free(tcp);
+ }
}
-static void
-tcp_ref (grpc_tcp * tcp, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp, reason, tcp->refcount.count, tcp->refcount.count + 1);
- gpr_ref (&tcp->refcount);
+static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file,
+ int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp,
+ reason, tcp->refcount.count, tcp->refcount.count + 1);
+ gpr_ref(&tcp->refcount);
}
#else
#define TCP_UNREF(tcp, reason) tcp_unref((tcp))
#define TCP_REF(tcp, reason) tcp_ref((tcp))
-static void
-tcp_unref (grpc_tcp * tcp)
-{
- if (gpr_unref (&tcp->refcount))
- {
- tcp_free (tcp);
- }
+static void tcp_unref(grpc_tcp *tcp) {
+ if (gpr_unref(&tcp->refcount)) {
+ tcp_free(tcp);
+ }
}
-static void
-tcp_ref (grpc_tcp * tcp)
-{
- gpr_ref (&tcp->refcount);
-}
+static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); }
#endif
/* Asynchronous callback from the IOCP, or the background thread. */
-static int
-on_read (grpc_tcp * tcp, int success)
-{
+static int on_read(grpc_tcp *tcp, int success) {
grpc_winsocket *socket = tcp->socket;
gpr_slice sub;
gpr_slice *slice = NULL;
@@ -162,56 +143,45 @@ on_read (grpc_tcp * tcp, int success)
grpc_winsocket_callback_info *info = &socket->read_info;
int do_abort = 0;
- if (success)
- {
- if (socket->read_info.wsa_error != 0)
- {
- if (socket->read_info.wsa_error != WSAECONNRESET)
- {
- char *utf8_message = gpr_format_message (info->wsa_error);
- gpr_log (GPR_ERROR, "ReadFile overlapped error: %s", utf8_message);
- gpr_free (utf8_message);
- }
- success = 0;
- gpr_slice_unref (tcp->read_slice);
- }
- else
- {
- if (info->bytes_transfered != 0)
- {
- sub = gpr_slice_sub_no_ref (tcp->read_slice, 0, info->bytes_transfered);
- gpr_slice_buffer_add (tcp->read_slices, sub);
- success = 1;
- }
- else
- {
- gpr_slice_unref (tcp->read_slice);
- success = 0;
- }
- }
+ if (success) {
+ if (socket->read_info.wsa_error != 0) {
+ if (socket->read_info.wsa_error != WSAECONNRESET) {
+ char *utf8_message = gpr_format_message(info->wsa_error);
+ gpr_log(GPR_ERROR, "ReadFile overlapped error: %s", utf8_message);
+ gpr_free(utf8_message);
+ }
+ success = 0;
+ gpr_slice_unref(tcp->read_slice);
+ } else {
+ if (info->bytes_transfered != 0) {
+ sub = gpr_slice_sub_no_ref(tcp->read_slice, 0, info->bytes_transfered);
+ gpr_slice_buffer_add(tcp->read_slices, sub);
+ success = 1;
+ } else {
+ gpr_slice_unref(tcp->read_slice);
+ success = 0;
+ }
}
+ }
return success;
}
-static void
-on_read_cb (void *tcpp, int from_iocp)
-{
+static void on_read_cb(void *tcpp, int from_iocp) {
grpc_tcp *tcp = tcpp;
grpc_closure *cb = tcp->read_cb;
- int success = on_read (tcp, from_iocp);
+ int success = on_read(tcp, from_iocp);
tcp->read_cb = NULL;
- TCP_UNREF (tcp, "read");
- if (cb)
- {
- cb->cb (cb->cb_arg, success);
- }
+ TCP_UNREF(tcp, "read");
+ if (cb) {
+ cb->cb(cb->cb_arg, success);
+ }
}
-static grpc_endpoint_op_status
-win_read (grpc_endpoint * ep, gpr_slice_buffer * read_slices, grpc_closure * cb)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
+static grpc_endpoint_op_status win_read(grpc_endpoint *ep,
+ gpr_slice_buffer *read_slices,
+ grpc_closure *cb) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
grpc_winsocket *handle = tcp->socket;
grpc_winsocket_callback_info *info = &handle->read_info;
int status;
@@ -219,97 +189,88 @@ win_read (grpc_endpoint * ep, gpr_slice_buffer * read_slices, grpc_closure * cb)
DWORD flags = 0;
WSABUF buffer;
- if (tcp->shutting_down)
- {
- return GRPC_ENDPOINT_ERROR;
- }
+ if (tcp->shutting_down) {
+ return GRPC_ENDPOINT_ERROR;
+ }
tcp->read_cb = cb;
tcp->read_slices = read_slices;
- gpr_slice_buffer_reset_and_unref (read_slices);
+ gpr_slice_buffer_reset_and_unref(read_slices);
- tcp->read_slice = gpr_slice_malloc (8192);
+ tcp->read_slice = gpr_slice_malloc(8192);
- buffer.len = GPR_SLICE_LENGTH (tcp->read_slice);
- buffer.buf = (char *) GPR_SLICE_START_PTR (tcp->read_slice);
+ buffer.len = GPR_SLICE_LENGTH(tcp->read_slice);
+ buffer.buf = (char *)GPR_SLICE_START_PTR(tcp->read_slice);
/* First let's try a synchronous, non-blocking read. */
- status = WSARecv (tcp->socket->socket, &buffer, 1, &bytes_read, &flags, NULL, NULL);
- info->wsa_error = status == 0 ? 0 : WSAGetLastError ();
+ status =
+ WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, NULL, NULL);
+ info->wsa_error = status == 0 ? 0 : WSAGetLastError();
/* Did we get data immediately ? Yay. */
- if (info->wsa_error != WSAEWOULDBLOCK)
- {
- int ok;
- info->bytes_transfered = bytes_read;
- ok = on_read (tcp, 1);
- return ok ? GRPC_ENDPOINT_DONE : GRPC_ENDPOINT_ERROR;
- }
+ if (info->wsa_error != WSAEWOULDBLOCK) {
+ int ok;
+ info->bytes_transfered = bytes_read;
+ ok = on_read(tcp, 1);
+ return ok ? GRPC_ENDPOINT_DONE : GRPC_ENDPOINT_ERROR;
+ }
- TCP_REF (tcp, "read");
+ TCP_REF(tcp, "read");
/* Otherwise, let's retry, by queuing a read. */
- memset (&tcp->socket->read_info.overlapped, 0, sizeof (OVERLAPPED));
- status = WSARecv (tcp->socket->socket, &buffer, 1, &bytes_read, &flags, &info->overlapped, NULL);
-
- if (status != 0)
- {
- int wsa_error = WSAGetLastError ();
- if (wsa_error != WSA_IO_PENDING)
- {
- int ok;
- info->wsa_error = wsa_error;
- ok = on_read (tcp, 1);
- return ok ? GRPC_ENDPOINT_DONE : GRPC_ENDPOINT_ERROR;
- }
+ memset(&tcp->socket->read_info.overlapped, 0, sizeof(OVERLAPPED));
+ status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags,
+ &info->overlapped, NULL);
+
+ if (status != 0) {
+ int wsa_error = WSAGetLastError();
+ if (wsa_error != WSA_IO_PENDING) {
+ int ok;
+ info->wsa_error = wsa_error;
+ ok = on_read(tcp, 1);
+ return ok ? GRPC_ENDPOINT_DONE : GRPC_ENDPOINT_ERROR;
}
+ }
- grpc_socket_notify_on_read (tcp->socket, on_read_cb, tcp);
+ grpc_socket_notify_on_read(tcp->socket, on_read_cb, tcp);
return GRPC_ENDPOINT_PENDING;
}
/* Asynchronous callback from the IOCP, or the background thread. */
-static void
-on_write (void *tcpp, int success)
-{
- grpc_tcp *tcp = (grpc_tcp *) tcpp;
+static void on_write(void *tcpp, int success) {
+ grpc_tcp *tcp = (grpc_tcp *)tcpp;
grpc_winsocket *handle = tcp->socket;
grpc_winsocket_callback_info *info = &handle->write_info;
grpc_closure *cb;
int do_abort = 0;
- gpr_mu_lock (&tcp->mu);
+ gpr_mu_lock(&tcp->mu);
cb = tcp->write_cb;
tcp->write_cb = NULL;
- gpr_mu_unlock (&tcp->mu);
-
- if (success)
- {
- if (info->wsa_error != 0)
- {
- if (info->wsa_error != WSAECONNRESET)
- {
- char *utf8_message = gpr_format_message (info->wsa_error);
- gpr_log (GPR_ERROR, "WSASend overlapped error: %s", utf8_message);
- gpr_free (utf8_message);
- }
- success = 0;
- }
- else
- {
- GPR_ASSERT (info->bytes_transfered == tcp->write_slices->length);
- }
+ gpr_mu_unlock(&tcp->mu);
+
+ if (success) {
+ if (info->wsa_error != 0) {
+ if (info->wsa_error != WSAECONNRESET) {
+ char *utf8_message = gpr_format_message(info->wsa_error);
+ gpr_log(GPR_ERROR, "WSASend overlapped error: %s", utf8_message);
+ gpr_free(utf8_message);
+ }
+ success = 0;
+ } else {
+ GPR_ASSERT(info->bytes_transfered == tcp->write_slices->length);
}
+ }
- TCP_UNREF (tcp, "write");
- cb->cb (cb->cb_arg, success);
+ TCP_UNREF(tcp, "write");
+ cb->cb(cb->cb_arg, success);
}
/* Initiates a write. */
-static grpc_endpoint_op_status
-win_write (grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
+static grpc_endpoint_op_status win_write(grpc_endpoint *ep,
+ gpr_slice_buffer *slices,
+ grpc_closure *cb) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
grpc_winsocket *socket = tcp->socket;
grpc_winsocket_callback_info *info = &socket->write_info;
unsigned i;
@@ -319,96 +280,82 @@ win_write (grpc_endpoint * ep, gpr_slice_buffer * slices, grpc_closure * cb)
WSABUF *allocated = NULL;
WSABUF *buffers = local_buffers;
- if (tcp->shutting_down)
- {
- return GRPC_ENDPOINT_ERROR;
- }
+ if (tcp->shutting_down) {
+ return GRPC_ENDPOINT_ERROR;
+ }
tcp->write_cb = cb;
tcp->write_slices = slices;
- if (tcp->write_slices->count > GPR_ARRAY_SIZE (local_buffers))
- {
- buffers = (WSABUF *) gpr_malloc (sizeof (WSABUF) * tcp->write_slices->count);
- allocated = buffers;
- }
+ if (tcp->write_slices->count > GPR_ARRAY_SIZE(local_buffers)) {
+ buffers = (WSABUF *)gpr_malloc(sizeof(WSABUF) * tcp->write_slices->count);
+ allocated = buffers;
+ }
- for (i = 0; i < tcp->write_slices->count; i++)
- {
- buffers[i].len = GPR_SLICE_LENGTH (tcp->write_slices->slices[i]);
- buffers[i].buf = (char *) GPR_SLICE_START_PTR (tcp->write_slices->slices[i]);
- }
+ for (i = 0; i < tcp->write_slices->count; i++) {
+ buffers[i].len = GPR_SLICE_LENGTH(tcp->write_slices->slices[i]);
+ buffers[i].buf = (char *)GPR_SLICE_START_PTR(tcp->write_slices->slices[i]);
+ }
/* First, let's try a synchronous, non-blocking write. */
- status = WSASend (socket->socket, buffers, tcp->write_slices->count, &bytes_sent, 0, NULL, NULL);
- info->wsa_error = status == 0 ? 0 : WSAGetLastError ();
+ status = WSASend(socket->socket, buffers, tcp->write_slices->count,
+ &bytes_sent, 0, NULL, NULL);
+ info->wsa_error = status == 0 ? 0 : WSAGetLastError();
/* We would kind of expect to get a WSAEWOULDBLOCK here, especially on a busy
connection that has its send queue filled up. But if we don't, then we can
avoid doing an async write operation at all. */
- if (info->wsa_error != WSAEWOULDBLOCK)
- {
- grpc_endpoint_op_status ret = GRPC_ENDPOINT_ERROR;
- if (status == 0)
- {
- ret = GRPC_ENDPOINT_DONE;
- GPR_ASSERT (bytes_sent == tcp->write_slices->length);
- }
- else
- {
- if (socket->read_info.wsa_error != WSAECONNRESET)
- {
- char *utf8_message = gpr_format_message (info->wsa_error);
- gpr_log (GPR_ERROR, "WSASend error: %s", utf8_message);
- gpr_free (utf8_message);
- }
- }
- if (allocated)
- gpr_free (allocated);
- return ret;
+ if (info->wsa_error != WSAEWOULDBLOCK) {
+ grpc_endpoint_op_status ret = GRPC_ENDPOINT_ERROR;
+ if (status == 0) {
+ ret = GRPC_ENDPOINT_DONE;
+ GPR_ASSERT(bytes_sent == tcp->write_slices->length);
+ } else {
+ if (socket->read_info.wsa_error != WSAECONNRESET) {
+ char *utf8_message = gpr_format_message(info->wsa_error);
+ gpr_log(GPR_ERROR, "WSASend error: %s", utf8_message);
+ gpr_free(utf8_message);
+ }
}
+ if (allocated) gpr_free(allocated);
+ return ret;
+ }
- TCP_REF (tcp, "write");
+ TCP_REF(tcp, "write");
/* If we got a WSAEWOULDBLOCK earlier, then we need to re-do the same
operation, this time asynchronously. */
- memset (&socket->write_info.overlapped, 0, sizeof (OVERLAPPED));
- status = WSASend (socket->socket, buffers, tcp->write_slices->count, &bytes_sent, 0, &socket->write_info.overlapped, NULL);
- if (allocated)
- gpr_free (allocated);
-
- if (status != 0)
- {
- int wsa_error = WSAGetLastError ();
- if (wsa_error != WSA_IO_PENDING)
- {
- TCP_UNREF (tcp, "write");
- return GRPC_ENDPOINT_ERROR;
- }
+ memset(&socket->write_info.overlapped, 0, sizeof(OVERLAPPED));
+ status = WSASend(socket->socket, buffers, tcp->write_slices->count,
+ &bytes_sent, 0, &socket->write_info.overlapped, NULL);
+ if (allocated) gpr_free(allocated);
+
+ if (status != 0) {
+ int wsa_error = WSAGetLastError();
+ if (wsa_error != WSA_IO_PENDING) {
+ TCP_UNREF(tcp, "write");
+ return GRPC_ENDPOINT_ERROR;
}
+ }
/* As all is now setup, we can now ask for the IOCP notification. It may
trigger the callback immediately however, but no matter. */
- grpc_socket_notify_on_write (socket, on_write, tcp);
+ grpc_socket_notify_on_write(socket, on_write, tcp);
return GRPC_ENDPOINT_PENDING;
}
-static void
-win_add_to_pollset (grpc_endpoint * ep, grpc_pollset * ps)
-{
+static void win_add_to_pollset(grpc_endpoint *ep, grpc_pollset *ps) {
grpc_tcp *tcp;
- (void) ps;
- tcp = (grpc_tcp *) ep;
- grpc_iocp_add_socket (tcp->socket);
+ (void)ps;
+ tcp = (grpc_tcp *)ep;
+ grpc_iocp_add_socket(tcp->socket);
}
-static void
-win_add_to_pollset_set (grpc_endpoint * ep, grpc_pollset_set * pss)
-{
+static void win_add_to_pollset_set(grpc_endpoint *ep, grpc_pollset_set *pss) {
grpc_tcp *tcp;
- (void) pss;
- tcp = (grpc_tcp *) ep;
- grpc_iocp_add_socket (tcp->socket);
+ (void)pss;
+ tcp = (grpc_tcp *)ep;
+ grpc_iocp_add_socket(tcp->socket);
}
/* Initiates a shutdown of the TCP endpoint. This will queue abort callbacks
@@ -417,47 +364,38 @@ win_add_to_pollset_set (grpc_endpoint * ep, grpc_pollset_set * pss)
we're not going to protect against these. However the IO Completion Port
callback will happen from another thread, so we need to protect against
concurrent access of the data structure in that regard. */
-static void
-win_shutdown (grpc_endpoint * ep)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- gpr_mu_lock (&tcp->mu);
+static void win_shutdown(grpc_endpoint *ep) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ gpr_mu_lock(&tcp->mu);
/* At that point, what may happen is that we're already inside the IOCP
callback. See the comments in on_read and on_write. */
tcp->shutting_down = 1;
- grpc_winsocket_shutdown (tcp->socket);
- gpr_mu_unlock (&tcp->mu);
+ grpc_winsocket_shutdown(tcp->socket);
+ gpr_mu_unlock(&tcp->mu);
}
-static void
-win_destroy (grpc_endpoint * ep)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- TCP_UNREF (tcp, "destroy");
+static void win_destroy(grpc_endpoint *ep) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ TCP_UNREF(tcp, "destroy");
}
-static char *
-win_get_peer (grpc_endpoint * ep)
-{
- grpc_tcp *tcp = (grpc_tcp *) ep;
- return gpr_strdup (tcp->peer_string);
+static char *win_get_peer(grpc_endpoint *ep) {
+ grpc_tcp *tcp = (grpc_tcp *)ep;
+ return gpr_strdup(tcp->peer_string);
}
static grpc_endpoint_vtable vtable = {
- win_read, win_write, win_add_to_pollset, win_add_to_pollset_set,
- win_shutdown, win_destroy, win_get_peer
-};
-
-grpc_endpoint *
-grpc_tcp_create (grpc_winsocket * socket, char *peer_string)
-{
- grpc_tcp *tcp = (grpc_tcp *) gpr_malloc (sizeof (grpc_tcp));
- memset (tcp, 0, sizeof (grpc_tcp));
+ win_read, win_write, win_add_to_pollset, win_add_to_pollset_set,
+ win_shutdown, win_destroy, win_get_peer};
+
+grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket, char *peer_string) {
+ grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp));
+ memset(tcp, 0, sizeof(grpc_tcp));
tcp->base.vtable = &vtable;
tcp->socket = socket;
- gpr_mu_init (&tcp->mu);
- gpr_ref_init (&tcp->refcount, 1);
- tcp->peer_string = gpr_strdup (peer_string);
+ gpr_mu_init(&tcp->mu);
+ gpr_ref_init(&tcp->refcount, 1);
+ tcp->peer_string = gpr_strdup(peer_string);
return &tcp->base;
}
diff --git a/src/core/iomgr/tcp_windows.h b/src/core/iomgr/tcp_windows.h
index 2b87a875ad..deb3e48293 100644
--- a/src/core/iomgr/tcp_windows.h
+++ b/src/core/iomgr/tcp_windows.h
@@ -50,8 +50,8 @@
/* Create a tcp endpoint given a winsock handle.
* Takes ownership of the handle.
*/
-grpc_endpoint *grpc_tcp_create (grpc_winsocket * socket, char *peer_string);
+grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket, char *peer_string);
-int grpc_tcp_prepare_socket (SOCKET sock);
+int grpc_tcp_prepare_socket(SOCKET sock);
#endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_WINDOWS_H */
diff --git a/src/core/iomgr/time_averaged_stats.c b/src/core/iomgr/time_averaged_stats.c
index c2d1cdf212..e075db4373 100644
--- a/src/core/iomgr/time_averaged_stats.c
+++ b/src/core/iomgr/time_averaged_stats.c
@@ -33,9 +33,9 @@
#include "src/core/iomgr/time_averaged_stats.h"
-void
-grpc_time_averaged_stats_init (grpc_time_averaged_stats * stats, double init_avg, double regress_weight, double persistence_factor)
-{
+void grpc_time_averaged_stats_init(grpc_time_averaged_stats* stats,
+ double init_avg, double regress_weight,
+ double persistence_factor) {
stats->init_avg = init_avg;
stats->regress_weight = regress_weight;
stats->persistence_factor = persistence_factor;
@@ -45,33 +45,31 @@ grpc_time_averaged_stats_init (grpc_time_averaged_stats * stats, double init_avg
stats->aggregate_weighted_avg = init_avg;
}
-void
-grpc_time_averaged_stats_add_sample (grpc_time_averaged_stats * stats, double value)
-{
+void grpc_time_averaged_stats_add_sample(grpc_time_averaged_stats* stats,
+ double value) {
stats->batch_total_value += value;
++stats->batch_num_samples;
}
-double
-grpc_time_averaged_stats_update_average (grpc_time_averaged_stats * stats)
-{
+double grpc_time_averaged_stats_update_average(
+ grpc_time_averaged_stats* stats) {
/* Start with the current batch: */
double weighted_sum = stats->batch_total_value;
double total_weight = stats->batch_num_samples;
- if (stats->regress_weight > 0)
- {
- /* Add in the regression towards init_avg_: */
- weighted_sum += stats->regress_weight * stats->init_avg;
- total_weight += stats->regress_weight;
- }
- if (stats->persistence_factor > 0)
- {
- /* Add in the persistence: */
- const double prev_sample_weight = stats->persistence_factor * stats->aggregate_total_weight;
- weighted_sum += prev_sample_weight * stats->aggregate_weighted_avg;
- total_weight += prev_sample_weight;
- }
- stats->aggregate_weighted_avg = (total_weight > 0) ? (weighted_sum / total_weight) : stats->init_avg;
+ if (stats->regress_weight > 0) {
+ /* Add in the regression towards init_avg_: */
+ weighted_sum += stats->regress_weight * stats->init_avg;
+ total_weight += stats->regress_weight;
+ }
+ if (stats->persistence_factor > 0) {
+ /* Add in the persistence: */
+ const double prev_sample_weight =
+ stats->persistence_factor * stats->aggregate_total_weight;
+ weighted_sum += prev_sample_weight * stats->aggregate_weighted_avg;
+ total_weight += prev_sample_weight;
+ }
+ stats->aggregate_weighted_avg =
+ (total_weight > 0) ? (weighted_sum / total_weight) : stats->init_avg;
stats->aggregate_total_weight = total_weight;
stats->batch_num_samples = 0;
stats->batch_total_value = 0;
diff --git a/src/core/iomgr/time_averaged_stats.h b/src/core/iomgr/time_averaged_stats.h
index c4f9a61063..4e9e3956c2 100644
--- a/src/core/iomgr/time_averaged_stats.h
+++ b/src/core/iomgr/time_averaged_stats.h
@@ -39,8 +39,7 @@
weighted mean. It is designed for batch operations where we do many adds
before updating the average. */
-typedef struct
-{
+typedef struct {
/* The initial average value. This is the reported average until the first
grpc_time_averaged_stats_update_average call. If a positive regress_weight
is used, we also regress towards this value on each update. */
@@ -76,11 +75,14 @@ typedef struct
/* See the comments on the members above for an explanation of init_avg,
regress_weight, and persistence_factor. */
-void grpc_time_averaged_stats_init (grpc_time_averaged_stats * stats, double init_avg, double regress_weight, double persistence_factor);
+void grpc_time_averaged_stats_init(grpc_time_averaged_stats* stats,
+ double init_avg, double regress_weight,
+ double persistence_factor);
/* Add a sample to the current batch. */
-void grpc_time_averaged_stats_add_sample (grpc_time_averaged_stats * stats, double value);
+void grpc_time_averaged_stats_add_sample(grpc_time_averaged_stats* stats,
+ double value);
/* Complete a batch and compute the new estimate of the average sample
value. */
-double grpc_time_averaged_stats_update_average (grpc_time_averaged_stats * stats);
+double grpc_time_averaged_stats_update_average(grpc_time_averaged_stats* stats);
#endif /* GRPC_INTERNAL_CORE_IOMGR_TIME_AVERAGED_STATS_H */
diff --git a/src/core/iomgr/udp_server.c b/src/core/iomgr/udp_server.c
index a1b6f75ede..e1a57b035f 100644
--- a/src/core/iomgr/udp_server.c
+++ b/src/core/iomgr/udp_server.c
@@ -69,13 +69,11 @@
#define INIT_PORT_CAP 2
/* one listening port */
-typedef struct
-{
+typedef struct {
int fd;
grpc_fd *emfd;
grpc_udp_server *server;
- union
- {
+ union {
gpr_uint8 untyped[GRPC_MAX_SOCKADDR_SIZE];
struct sockaddr sockaddr;
struct sockaddr_un un;
@@ -86,20 +84,16 @@ typedef struct
grpc_udp_server_read_cb read_cb;
} server_port;
-static void
-unlink_if_unix_domain_socket (const struct sockaddr_un *un)
-{
+static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) {
struct stat st;
- if (stat (un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK)
- {
- unlink (un->sun_path);
- }
+ if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
+ unlink(un->sun_path);
+ }
}
/* the overall server */
-struct grpc_udp_server
-{
+struct grpc_udp_server {
gpr_mu mu;
gpr_cv cv;
@@ -125,237 +119,203 @@ struct grpc_udp_server
size_t pollset_count;
};
-grpc_udp_server *
-grpc_udp_server_create (void)
-{
- grpc_udp_server *s = gpr_malloc (sizeof (grpc_udp_server));
- gpr_mu_init (&s->mu);
- gpr_cv_init (&s->cv);
+grpc_udp_server *grpc_udp_server_create(void) {
+ grpc_udp_server *s = gpr_malloc(sizeof(grpc_udp_server));
+ gpr_mu_init(&s->mu);
+ gpr_cv_init(&s->cv);
s->active_ports = 0;
s->destroyed_ports = 0;
s->shutdown = 0;
- s->ports = gpr_malloc (sizeof (server_port) * INIT_PORT_CAP);
+ s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
s->nports = 0;
s->port_capacity = INIT_PORT_CAP;
return s;
}
-static void
-finish_shutdown (grpc_exec_ctx * exec_ctx, grpc_udp_server * s)
-{
- grpc_exec_ctx_enqueue (exec_ctx, s->shutdown_complete, 1);
+static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_udp_server *s) {
+ grpc_exec_ctx_enqueue(exec_ctx, s->shutdown_complete, 1);
- gpr_mu_destroy (&s->mu);
- gpr_cv_destroy (&s->cv);
+ gpr_mu_destroy(&s->mu);
+ gpr_cv_destroy(&s->cv);
- gpr_free (s->ports);
- gpr_free (s);
+ gpr_free(s->ports);
+ gpr_free(s);
}
-static void
-destroyed_port (grpc_exec_ctx * exec_ctx, void *server, int success)
-{
+static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, int success) {
grpc_udp_server *s = server;
- gpr_mu_lock (&s->mu);
+ gpr_mu_lock(&s->mu);
s->destroyed_ports++;
- if (s->destroyed_ports == s->nports)
- {
- gpr_mu_unlock (&s->mu);
- finish_shutdown (exec_ctx, s);
- }
- else
- {
- gpr_mu_unlock (&s->mu);
- }
+ if (s->destroyed_ports == s->nports) {
+ gpr_mu_unlock(&s->mu);
+ finish_shutdown(exec_ctx, s);
+ } else {
+ gpr_mu_unlock(&s->mu);
+ }
}
/* called when all listening endpoints have been shutdown, so no further
events will be received on them - at this point it's safe to destroy
things */
-static void
-deactivated_all_ports (grpc_exec_ctx * exec_ctx, grpc_udp_server * s)
-{
+static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_udp_server *s) {
size_t i;
/* delete ALL the things */
- gpr_mu_lock (&s->mu);
-
- if (!s->shutdown)
- {
- gpr_mu_unlock (&s->mu);
- return;
- }
-
- if (s->nports)
- {
- for (i = 0; i < s->nports; i++)
- {
- server_port *sp = &s->ports[i];
- if (sp->addr.sockaddr.sa_family == AF_UNIX)
- {
- unlink_if_unix_domain_socket (&sp->addr.un);
- }
- sp->destroyed_closure.cb = destroyed_port;
- sp->destroyed_closure.cb_arg = s;
- grpc_fd_orphan (exec_ctx, sp->emfd, &sp->destroyed_closure, "udp_listener_shutdown");
- }
- gpr_mu_unlock (&s->mu);
- }
- else
- {
- gpr_mu_unlock (&s->mu);
- finish_shutdown (exec_ctx, s);
+ gpr_mu_lock(&s->mu);
+
+ if (!s->shutdown) {
+ gpr_mu_unlock(&s->mu);
+ return;
+ }
+
+ if (s->nports) {
+ for (i = 0; i < s->nports; i++) {
+ server_port *sp = &s->ports[i];
+ if (sp->addr.sockaddr.sa_family == AF_UNIX) {
+ unlink_if_unix_domain_socket(&sp->addr.un);
+ }
+ sp->destroyed_closure.cb = destroyed_port;
+ sp->destroyed_closure.cb_arg = s;
+ grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure,
+ "udp_listener_shutdown");
}
+ gpr_mu_unlock(&s->mu);
+ } else {
+ gpr_mu_unlock(&s->mu);
+ finish_shutdown(exec_ctx, s);
+ }
}
-void
-grpc_udp_server_destroy (grpc_exec_ctx * exec_ctx, grpc_udp_server * s, grpc_closure * on_done)
-{
+void grpc_udp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_udp_server *s,
+ grpc_closure *on_done) {
size_t i;
- gpr_mu_lock (&s->mu);
+ gpr_mu_lock(&s->mu);
- GPR_ASSERT (!s->shutdown);
+ GPR_ASSERT(!s->shutdown);
s->shutdown = 1;
s->shutdown_complete = on_done;
/* shutdown all fd's */
- if (s->active_ports)
- {
- for (i = 0; i < s->nports; i++)
- {
- grpc_fd_shutdown (exec_ctx, s->ports[i].emfd);
- }
- gpr_mu_unlock (&s->mu);
- }
- else
- {
- gpr_mu_unlock (&s->mu);
- deactivated_all_ports (exec_ctx, s);
+ if (s->active_ports) {
+ for (i = 0; i < s->nports; i++) {
+ grpc_fd_shutdown(exec_ctx, s->ports[i].emfd);
}
+ gpr_mu_unlock(&s->mu);
+ } else {
+ gpr_mu_unlock(&s->mu);
+ deactivated_all_ports(exec_ctx, s);
+ }
}
/* Prepare a recently-created socket for listening. */
-static int
-prepare_socket (int fd, const struct sockaddr *addr, size_t addr_len)
-{
+static int prepare_socket(int fd, const struct sockaddr *addr,
+ size_t addr_len) {
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
int get_local_ip;
int rc;
- if (fd < 0)
- {
- goto error;
- }
+ if (fd < 0) {
+ goto error;
+ }
- if (!grpc_set_socket_nonblocking (fd, 1) || !grpc_set_socket_cloexec (fd, 1))
- {
- gpr_log (GPR_ERROR, "Unable to configure socket %d: %s", fd, strerror (errno));
- }
+ if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1)) {
+ gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
+ strerror(errno));
+ }
get_local_ip = 1;
- rc = setsockopt (fd, IPPROTO_IP, IP_PKTINFO, &get_local_ip, sizeof (get_local_ip));
- if (rc == 0 && addr->sa_family == AF_INET6)
- {
+ rc = setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &get_local_ip,
+ sizeof(get_local_ip));
+ if (rc == 0 && addr->sa_family == AF_INET6) {
#if !TARGET_OS_IPHONE
- rc = setsockopt (fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &get_local_ip, sizeof (get_local_ip));
+ rc = setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &get_local_ip,
+ sizeof(get_local_ip));
#endif
- }
+ }
- GPR_ASSERT (addr_len < ~(socklen_t) 0);
- if (bind (fd, addr, (socklen_t) addr_len) < 0)
- {
- char *addr_str;
- grpc_sockaddr_to_string (&addr_str, addr, 0);
- gpr_log (GPR_ERROR, "bind addr=%s: %s", addr_str, strerror (errno));
- gpr_free (addr_str);
- goto error;
- }
+ GPR_ASSERT(addr_len < ~(socklen_t)0);
+ if (bind(fd, addr, (socklen_t)addr_len) < 0) {
+ char *addr_str;
+ grpc_sockaddr_to_string(&addr_str, addr, 0);
+ gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
+ gpr_free(addr_str);
+ goto error;
+ }
- sockname_len = sizeof (sockname_temp);
- if (getsockname (fd, (struct sockaddr *) &sockname_temp, &sockname_len) < 0)
- {
- goto error;
- }
+ sockname_len = sizeof(sockname_temp);
+ if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
+ goto error;
+ }
- return grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
+ return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
error:
- if (fd >= 0)
- {
- close (fd);
- }
+ if (fd >= 0) {
+ close(fd);
+ }
return -1;
}
/* event manager callback when reads are ready */
-static void
-on_read (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void on_read(grpc_exec_ctx *exec_ctx, void *arg, int success) {
server_port *sp = arg;
- if (success == 0)
- {
- gpr_mu_lock (&sp->server->mu);
- if (0 == --sp->server->active_ports)
- {
- gpr_mu_unlock (&sp->server->mu);
- deactivated_all_ports (exec_ctx, sp->server);
- }
- else
- {
- gpr_mu_unlock (&sp->server->mu);
- }
- return;
+ if (success == 0) {
+ gpr_mu_lock(&sp->server->mu);
+ if (0 == --sp->server->active_ports) {
+ gpr_mu_unlock(&sp->server->mu);
+ deactivated_all_ports(exec_ctx, sp->server);
+ } else {
+ gpr_mu_unlock(&sp->server->mu);
}
+ return;
+ }
/* Tell the registered callback that data is available to read. */
- GPR_ASSERT (sp->read_cb);
- sp->read_cb (sp->fd);
+ GPR_ASSERT(sp->read_cb);
+ sp->read_cb(sp->fd);
/* Re-arm the notification event so we get another chance to read. */
- grpc_fd_notify_on_read (exec_ctx, sp->emfd, &sp->read_closure);
+ grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure);
}
-static int
-add_socket_to_server (grpc_udp_server * s, int fd, const struct sockaddr *addr, size_t addr_len, grpc_udp_server_read_cb read_cb)
-{
+static int add_socket_to_server(grpc_udp_server *s, int fd,
+ const struct sockaddr *addr, size_t addr_len,
+ grpc_udp_server_read_cb read_cb) {
server_port *sp;
int port;
char *addr_str;
char *name;
- port = prepare_socket (fd, addr, addr_len);
- if (port >= 0)
- {
- grpc_sockaddr_to_string (&addr_str, (struct sockaddr *) &addr, 1);
- gpr_asprintf (&name, "udp-server-listener:%s", addr_str);
- gpr_mu_lock (&s->mu);
- /* append it to the list under a lock */
- if (s->nports == s->port_capacity)
- {
- s->port_capacity *= 2;
- s->ports = gpr_realloc (s->ports, sizeof (server_port) * s->port_capacity);
- }
- sp = &s->ports[s->nports++];
- sp->server = s;
- sp->fd = fd;
- sp->emfd = grpc_fd_create (fd, name);
- memcpy (sp->addr.untyped, addr, addr_len);
- sp->addr_len = addr_len;
- sp->read_cb = read_cb;
- GPR_ASSERT (sp->emfd);
- gpr_mu_unlock (&s->mu);
+ port = prepare_socket(fd, addr, addr_len);
+ if (port >= 0) {
+ grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1);
+ gpr_asprintf(&name, "udp-server-listener:%s", addr_str);
+ gpr_mu_lock(&s->mu);
+ /* append it to the list under a lock */
+ if (s->nports == s->port_capacity) {
+ s->port_capacity *= 2;
+ s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity);
}
+ sp = &s->ports[s->nports++];
+ sp->server = s;
+ sp->fd = fd;
+ sp->emfd = grpc_fd_create(fd, name);
+ memcpy(sp->addr.untyped, addr, addr_len);
+ sp->addr_len = addr_len;
+ sp->read_cb = read_cb;
+ GPR_ASSERT(sp->emfd);
+ gpr_mu_unlock(&s->mu);
+ }
return port;
}
-int
-grpc_udp_server_add_port (grpc_udp_server * s, const void *addr, size_t addr_len, grpc_udp_server_read_cb read_cb)
-{
+int grpc_udp_server_add_port(grpc_udp_server *s, const void *addr,
+ size_t addr_len, grpc_udp_server_read_cb read_cb) {
int allocated_port1 = -1;
int allocated_port2 = -1;
unsigned i;
@@ -370,117 +330,102 @@ grpc_udp_server_add_port (grpc_udp_server * s, const void *addr, size_t addr_len
socklen_t sockname_len;
int port;
- if (((struct sockaddr *) addr)->sa_family == AF_UNIX)
- {
- unlink_if_unix_domain_socket (addr);
- }
+ if (((struct sockaddr *)addr)->sa_family == AF_UNIX) {
+ unlink_if_unix_domain_socket(addr);
+ }
/* Check if this is a wildcard port, and if so, try to keep the port the same
as some previously created listener. */
- if (grpc_sockaddr_get_port (addr) == 0)
- {
- for (i = 0; i < s->nports; i++)
- {
- sockname_len = sizeof (sockname_temp);
- if (0 == getsockname (s->ports[i].fd, (struct sockaddr *) &sockname_temp, &sockname_len))
- {
- port = grpc_sockaddr_get_port ((struct sockaddr *) &sockname_temp);
- if (port > 0)
- {
- allocated_addr = malloc (addr_len);
- memcpy (allocated_addr, addr, addr_len);
- grpc_sockaddr_set_port (allocated_addr, port);
- addr = allocated_addr;
- break;
- }
- }
- }
+ if (grpc_sockaddr_get_port(addr) == 0) {
+ for (i = 0; i < s->nports; i++) {
+ sockname_len = sizeof(sockname_temp);
+ if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp,
+ &sockname_len)) {
+ port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
+ if (port > 0) {
+ allocated_addr = malloc(addr_len);
+ memcpy(allocated_addr, addr, addr_len);
+ grpc_sockaddr_set_port(allocated_addr, port);
+ addr = allocated_addr;
+ break;
+ }
+ }
}
+ }
- if (grpc_sockaddr_to_v4mapped (addr, &addr6_v4mapped))
- {
- addr = (const struct sockaddr *) &addr6_v4mapped;
- addr_len = sizeof (addr6_v4mapped);
- }
+ if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
+ addr = (const struct sockaddr *)&addr6_v4mapped;
+ addr_len = sizeof(addr6_v4mapped);
+ }
/* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
- if (grpc_sockaddr_is_wildcard (addr, &port))
- {
- grpc_sockaddr_make_wildcards (port, &wild4, &wild6);
-
- /* Try listening on IPv6 first. */
- addr = (struct sockaddr *) &wild6;
- addr_len = sizeof (wild6);
- fd = grpc_create_dualstack_socket (addr, SOCK_DGRAM, IPPROTO_UDP, &dsmode);
- allocated_port1 = add_socket_to_server (s, fd, addr, addr_len, read_cb);
- if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK)
- {
- goto done;
- }
-
- /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
- if (port == 0 && allocated_port1 > 0)
- {
- grpc_sockaddr_set_port ((struct sockaddr *) &wild4, allocated_port1);
- }
- addr = (struct sockaddr *) &wild4;
- addr_len = sizeof (wild4);
+ if (grpc_sockaddr_is_wildcard(addr, &port)) {
+ grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
+
+ /* Try listening on IPv6 first. */
+ addr = (struct sockaddr *)&wild6;
+ addr_len = sizeof(wild6);
+ fd = grpc_create_dualstack_socket(addr, SOCK_DGRAM, IPPROTO_UDP, &dsmode);
+ allocated_port1 = add_socket_to_server(s, fd, addr, addr_len, read_cb);
+ if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
+ goto done;
}
- fd = grpc_create_dualstack_socket (addr, SOCK_DGRAM, IPPROTO_UDP, &dsmode);
- if (fd < 0)
- {
- gpr_log (GPR_ERROR, "Unable to create socket: %s", strerror (errno));
+ /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
+ if (port == 0 && allocated_port1 > 0) {
+ grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1);
}
- if (dsmode == GRPC_DSMODE_IPV4 && grpc_sockaddr_is_v4mapped (addr, &addr4_copy))
- {
- addr = (struct sockaddr *) &addr4_copy;
- addr_len = sizeof (addr4_copy);
- }
- allocated_port2 = add_socket_to_server (s, fd, addr, addr_len, read_cb);
+ addr = (struct sockaddr *)&wild4;
+ addr_len = sizeof(wild4);
+ }
+
+ fd = grpc_create_dualstack_socket(addr, SOCK_DGRAM, IPPROTO_UDP, &dsmode);
+ if (fd < 0) {
+ gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
+ }
+ if (dsmode == GRPC_DSMODE_IPV4 &&
+ grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
+ addr = (struct sockaddr *)&addr4_copy;
+ addr_len = sizeof(addr4_copy);
+ }
+ allocated_port2 = add_socket_to_server(s, fd, addr, addr_len, read_cb);
done:
- gpr_free (allocated_addr);
+ gpr_free(allocated_addr);
return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
}
-int
-grpc_udp_server_get_fd (grpc_udp_server * s, unsigned index)
-{
+int grpc_udp_server_get_fd(grpc_udp_server *s, unsigned index) {
return (index < s->nports) ? s->ports[index].fd : -1;
}
-void
-grpc_udp_server_start (grpc_exec_ctx * exec_ctx, grpc_udp_server * s, grpc_pollset ** pollsets, size_t pollset_count)
-{
+void grpc_udp_server_start(grpc_exec_ctx *exec_ctx, grpc_udp_server *s,
+ grpc_pollset **pollsets, size_t pollset_count) {
size_t i, j;
- gpr_mu_lock (&s->mu);
- GPR_ASSERT (s->active_ports == 0);
+ gpr_mu_lock(&s->mu);
+ GPR_ASSERT(s->active_ports == 0);
s->pollsets = pollsets;
- for (i = 0; i < s->nports; i++)
- {
- for (j = 0; j < pollset_count; j++)
- {
- grpc_pollset_add_fd (exec_ctx, pollsets[j], s->ports[i].emfd);
- }
- s->ports[i].read_closure.cb = on_read;
- s->ports[i].read_closure.cb_arg = &s->ports[i];
- grpc_fd_notify_on_read (exec_ctx, s->ports[i].emfd, &s->ports[i].read_closure);
- s->active_ports++;
+ for (i = 0; i < s->nports; i++) {
+ for (j = 0; j < pollset_count; j++) {
+ grpc_pollset_add_fd(exec_ctx, pollsets[j], s->ports[i].emfd);
}
- gpr_mu_unlock (&s->mu);
+ s->ports[i].read_closure.cb = on_read;
+ s->ports[i].read_closure.cb_arg = &s->ports[i];
+ grpc_fd_notify_on_read(exec_ctx, s->ports[i].emfd,
+ &s->ports[i].read_closure);
+ s->active_ports++;
+ }
+ gpr_mu_unlock(&s->mu);
}
/* TODO(rjshade): Add a test for this method. */
-void
-grpc_udp_server_write (server_port * sp, const char *buffer, size_t buf_len, const struct sockaddr *peer_address)
-{
+void grpc_udp_server_write(server_port *sp, const char *buffer, size_t buf_len,
+ const struct sockaddr *peer_address) {
ssize_t rc;
- rc = sendto (sp->fd, buffer, buf_len, 0, peer_address, sizeof (peer_address));
- if (rc < 0)
- {
- gpr_log (GPR_ERROR, "Unable to send data: %s", strerror (errno));
- }
+ rc = sendto(sp->fd, buffer, buf_len, 0, peer_address, sizeof(peer_address));
+ if (rc < 0) {
+ gpr_log(GPR_ERROR, "Unable to send data: %s", strerror(errno));
+ }
}
#endif
diff --git a/src/core/iomgr/udp_server.h b/src/core/iomgr/udp_server.h
index e37d0485c1..76082d7761 100644
--- a/src/core/iomgr/udp_server.h
+++ b/src/core/iomgr/udp_server.h
@@ -40,15 +40,16 @@
typedef struct grpc_udp_server grpc_udp_server;
/* Called when data is available to read from the socket. */
-typedef void (*grpc_udp_server_read_cb) (int fd);
+typedef void (*grpc_udp_server_read_cb)(int fd);
/* Create a server, initially not bound to any ports */
-grpc_udp_server *grpc_udp_server_create (void);
+grpc_udp_server *grpc_udp_server_create(void);
/* Start listening to bound ports */
-void grpc_udp_server_start (grpc_exec_ctx * exec_ctx, grpc_udp_server * udp_server, grpc_pollset ** pollsets, size_t pollset_count);
+void grpc_udp_server_start(grpc_exec_ctx *exec_ctx, grpc_udp_server *udp_server,
+ grpc_pollset **pollsets, size_t pollset_count);
-int grpc_udp_server_get_fd (grpc_udp_server * s, unsigned index);
+int grpc_udp_server_get_fd(grpc_udp_server *s, unsigned index);
/* Add a port to the server, returning port number on success, or negative
on failure.
@@ -60,9 +61,11 @@ int grpc_udp_server_get_fd (grpc_udp_server * s, unsigned index);
/* TODO(ctiller): deprecate this, and make grpc_udp_server_add_ports to handle
all of the multiple socket port matching logic in one place */
-int grpc_udp_server_add_port (grpc_udp_server * s, const void *addr, size_t addr_len, grpc_udp_server_read_cb read_cb);
+int grpc_udp_server_add_port(grpc_udp_server *s, const void *addr,
+ size_t addr_len, grpc_udp_server_read_cb read_cb);
-void grpc_udp_server_destroy (grpc_exec_ctx * exec_ctx, grpc_udp_server * server, grpc_closure * on_done);
+void grpc_udp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_udp_server *server,
+ grpc_closure *on_done);
/* Write the contents of buffer to the underlying UDP socket. */
/*
diff --git a/src/core/iomgr/wakeup_fd_eventfd.c b/src/core/iomgr/wakeup_fd_eventfd.c
index 23b5033fcf..48eb1afb3d 100644
--- a/src/core/iomgr/wakeup_fd_eventfd.c
+++ b/src/core/iomgr/wakeup_fd_eventfd.c
@@ -42,56 +42,40 @@
#include "src/core/iomgr/wakeup_fd_posix.h"
#include <grpc/support/log.h>
-static void
-eventfd_create (grpc_wakeup_fd * fd_info)
-{
- int efd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
+static void eventfd_create(grpc_wakeup_fd* fd_info) {
+ int efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
/* TODO(klempner): Handle failure more gracefully */
- GPR_ASSERT (efd >= 0);
+ GPR_ASSERT(efd >= 0);
fd_info->read_fd = efd;
fd_info->write_fd = -1;
}
-static void
-eventfd_consume (grpc_wakeup_fd * fd_info)
-{
+static void eventfd_consume(grpc_wakeup_fd* fd_info) {
eventfd_t value;
int err;
- do
- {
- err = eventfd_read (fd_info->read_fd, &value);
- }
- while (err < 0 && errno == EINTR);
+ do {
+ err = eventfd_read(fd_info->read_fd, &value);
+ } while (err < 0 && errno == EINTR);
}
-static void
-eventfd_wakeup (grpc_wakeup_fd * fd_info)
-{
+static void eventfd_wakeup(grpc_wakeup_fd* fd_info) {
int err;
- do
- {
- err = eventfd_write (fd_info->read_fd, 1);
- }
- while (err < 0 && errno == EINTR);
+ do {
+ err = eventfd_write(fd_info->read_fd, 1);
+ } while (err < 0 && errno == EINTR);
}
-static void
-eventfd_destroy (grpc_wakeup_fd * fd_info)
-{
- if (fd_info->read_fd != 0)
- close (fd_info->read_fd);
+static void eventfd_destroy(grpc_wakeup_fd* fd_info) {
+ if (fd_info->read_fd != 0) close(fd_info->read_fd);
}
-static int
-eventfd_check_availability (void)
-{
+static int eventfd_check_availability(void) {
/* TODO(klempner): Actually check if eventfd is available */
return 1;
}
const grpc_wakeup_fd_vtable grpc_specialized_wakeup_fd_vtable = {
- eventfd_create, eventfd_consume, eventfd_wakeup, eventfd_destroy,
- eventfd_check_availability
-};
+ eventfd_create, eventfd_consume, eventfd_wakeup, eventfd_destroy,
+ eventfd_check_availability};
#endif /* GPR_LINUX_EVENTFD */
diff --git a/src/core/iomgr/wakeup_fd_nospecial.c b/src/core/iomgr/wakeup_fd_nospecial.c
index 95b819393f..78d763c103 100644
--- a/src/core/iomgr/wakeup_fd_nospecial.c
+++ b/src/core/iomgr/wakeup_fd_nospecial.c
@@ -43,14 +43,9 @@
#include "src/core/iomgr/wakeup_fd_posix.h"
#include <stddef.h>
-static int
-check_availability_invalid (void)
-{
- return 0;
-}
+static int check_availability_invalid(void) { return 0; }
const grpc_wakeup_fd_vtable grpc_specialized_wakeup_fd_vtable = {
- NULL, NULL, NULL, NULL, check_availability_invalid
-};
+ NULL, NULL, NULL, NULL, check_availability_invalid};
#endif /* GPR_POSIX_NO_SPECIAL_WAKEUP_FD */
diff --git a/src/core/iomgr/wakeup_fd_pipe.c b/src/core/iomgr/wakeup_fd_pipe.c
index 4c87e3e1d9..80de181d9d 100644
--- a/src/core/iomgr/wakeup_fd_pipe.c
+++ b/src/core/iomgr/wakeup_fd_pipe.c
@@ -44,71 +44,54 @@
#include "src/core/iomgr/socket_utils_posix.h"
#include <grpc/support/log.h>
-static void
-pipe_init (grpc_wakeup_fd * fd_info)
-{
+static void pipe_init(grpc_wakeup_fd* fd_info) {
int pipefd[2];
/* TODO(klempner): Make this nonfatal */
- GPR_ASSERT (0 == pipe (pipefd));
- GPR_ASSERT (grpc_set_socket_nonblocking (pipefd[0], 1));
- GPR_ASSERT (grpc_set_socket_nonblocking (pipefd[1], 1));
+ GPR_ASSERT(0 == pipe(pipefd));
+ GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[0], 1));
+ GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[1], 1));
fd_info->read_fd = pipefd[0];
fd_info->write_fd = pipefd[1];
}
-static void
-pipe_consume (grpc_wakeup_fd * fd_info)
-{
+static void pipe_consume(grpc_wakeup_fd* fd_info) {
char buf[128];
ssize_t r;
- for (;;)
- {
- r = read (fd_info->read_fd, buf, sizeof (buf));
- if (r > 0)
- continue;
- if (r == 0)
- return;
- switch (errno)
- {
- case EAGAIN:
- return;
- case EINTR:
- continue;
- default:
- gpr_log (GPR_ERROR, "error reading pipe: %s", strerror (errno));
- return;
- }
+ for (;;) {
+ r = read(fd_info->read_fd, buf, sizeof(buf));
+ if (r > 0) continue;
+ if (r == 0) return;
+ switch (errno) {
+ case EAGAIN:
+ return;
+ case EINTR:
+ continue;
+ default:
+ gpr_log(GPR_ERROR, "error reading pipe: %s", strerror(errno));
+ return;
}
+ }
}
-static void
-pipe_wakeup (grpc_wakeup_fd * fd_info)
-{
+static void pipe_wakeup(grpc_wakeup_fd* fd_info) {
char c = 0;
- while (write (fd_info->write_fd, &c, 1) != 1 && errno == EINTR)
+ while (write(fd_info->write_fd, &c, 1) != 1 && errno == EINTR)
;
}
-static void
-pipe_destroy (grpc_wakeup_fd * fd_info)
-{
- if (fd_info->read_fd != 0)
- close (fd_info->read_fd);
- if (fd_info->write_fd != 0)
- close (fd_info->write_fd);
+static void pipe_destroy(grpc_wakeup_fd* fd_info) {
+ if (fd_info->read_fd != 0) close(fd_info->read_fd);
+ if (fd_info->write_fd != 0) close(fd_info->write_fd);
}
-static int
-pipe_check_availability (void)
-{
+static int pipe_check_availability(void) {
/* Assume that pipes are always available. */
return 1;
}
const grpc_wakeup_fd_vtable grpc_pipe_wakeup_fd_vtable = {
- pipe_init, pipe_consume, pipe_wakeup, pipe_destroy,
- pipe_check_availability
-};
+ pipe_init, pipe_consume, pipe_wakeup, pipe_destroy,
+ pipe_check_availability};
#endif /* GPR_POSIX_WAKUP_FD */
diff --git a/src/core/iomgr/wakeup_fd_posix.c b/src/core/iomgr/wakeup_fd_posix.c
index dae3e94f65..d09fb78d12 100644
--- a/src/core/iomgr/wakeup_fd_posix.c
+++ b/src/core/iomgr/wakeup_fd_posix.c
@@ -41,53 +41,34 @@
static const grpc_wakeup_fd_vtable *wakeup_fd_vtable = NULL;
-void
-grpc_wakeup_fd_global_init (void)
-{
- if (grpc_specialized_wakeup_fd_vtable.check_availability ())
- {
- wakeup_fd_vtable = &grpc_specialized_wakeup_fd_vtable;
- }
- else
- {
- wakeup_fd_vtable = &grpc_pipe_wakeup_fd_vtable;
- }
+void grpc_wakeup_fd_global_init(void) {
+ if (grpc_specialized_wakeup_fd_vtable.check_availability()) {
+ wakeup_fd_vtable = &grpc_specialized_wakeup_fd_vtable;
+ } else {
+ wakeup_fd_vtable = &grpc_pipe_wakeup_fd_vtable;
+ }
}
-void
-grpc_wakeup_fd_global_init_force_fallback (void)
-{
+void grpc_wakeup_fd_global_init_force_fallback(void) {
wakeup_fd_vtable = &grpc_pipe_wakeup_fd_vtable;
}
-void
-grpc_wakeup_fd_global_destroy (void)
-{
- wakeup_fd_vtable = NULL;
-}
+void grpc_wakeup_fd_global_destroy(void) { wakeup_fd_vtable = NULL; }
-void
-grpc_wakeup_fd_init (grpc_wakeup_fd * fd_info)
-{
- wakeup_fd_vtable->init (fd_info);
+void grpc_wakeup_fd_init(grpc_wakeup_fd *fd_info) {
+ wakeup_fd_vtable->init(fd_info);
}
-void
-grpc_wakeup_fd_consume_wakeup (grpc_wakeup_fd * fd_info)
-{
- wakeup_fd_vtable->consume (fd_info);
+void grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd *fd_info) {
+ wakeup_fd_vtable->consume(fd_info);
}
-void
-grpc_wakeup_fd_wakeup (grpc_wakeup_fd * fd_info)
-{
- wakeup_fd_vtable->wakeup (fd_info);
+void grpc_wakeup_fd_wakeup(grpc_wakeup_fd *fd_info) {
+ wakeup_fd_vtable->wakeup(fd_info);
}
-void
-grpc_wakeup_fd_destroy (grpc_wakeup_fd * fd_info)
-{
- wakeup_fd_vtable->destroy (fd_info);
+void grpc_wakeup_fd_destroy(grpc_wakeup_fd *fd_info) {
+ wakeup_fd_vtable->destroy(fd_info);
}
#endif /* GPR_POSIX_WAKEUP_FD */
diff --git a/src/core/iomgr/wakeup_fd_posix.h b/src/core/iomgr/wakeup_fd_posix.h
index 63efde714f..fe71b5abe9 100644
--- a/src/core/iomgr/wakeup_fd_posix.h
+++ b/src/core/iomgr/wakeup_fd_posix.h
@@ -62,37 +62,35 @@
#ifndef GRPC_INTERNAL_CORE_IOMGR_WAKEUP_FD_POSIX_H
#define GRPC_INTERNAL_CORE_IOMGR_WAKEUP_FD_POSIX_H
-void grpc_wakeup_fd_global_init (void);
-void grpc_wakeup_fd_global_destroy (void);
+void grpc_wakeup_fd_global_init(void);
+void grpc_wakeup_fd_global_destroy(void);
/* Force using the fallback implementation. This is intended for testing
* purposes only.*/
-void grpc_wakeup_fd_global_init_force_fallback (void);
+void grpc_wakeup_fd_global_init_force_fallback(void);
typedef struct grpc_wakeup_fd grpc_wakeup_fd;
-typedef struct grpc_wakeup_fd_vtable
-{
- void (*init) (grpc_wakeup_fd * fd_info);
- void (*consume) (grpc_wakeup_fd * fd_info);
- void (*wakeup) (grpc_wakeup_fd * fd_info);
- void (*destroy) (grpc_wakeup_fd * fd_info);
+typedef struct grpc_wakeup_fd_vtable {
+ void (*init)(grpc_wakeup_fd* fd_info);
+ void (*consume)(grpc_wakeup_fd* fd_info);
+ void (*wakeup)(grpc_wakeup_fd* fd_info);
+ void (*destroy)(grpc_wakeup_fd* fd_info);
/* Must be called before calling any other functions */
- int (*check_availability) (void);
+ int (*check_availability)(void);
} grpc_wakeup_fd_vtable;
-struct grpc_wakeup_fd
-{
+struct grpc_wakeup_fd {
int read_fd;
int write_fd;
};
#define GRPC_WAKEUP_FD_GET_READ_FD(fd_info) ((fd_info)->read_fd)
-void grpc_wakeup_fd_init (grpc_wakeup_fd * fd_info);
-void grpc_wakeup_fd_consume_wakeup (grpc_wakeup_fd * fd_info);
-void grpc_wakeup_fd_wakeup (grpc_wakeup_fd * fd_info);
-void grpc_wakeup_fd_destroy (grpc_wakeup_fd * fd_info);
+void grpc_wakeup_fd_init(grpc_wakeup_fd* fd_info);
+void grpc_wakeup_fd_consume_wakeup(grpc_wakeup_fd* fd_info);
+void grpc_wakeup_fd_wakeup(grpc_wakeup_fd* fd_info);
+void grpc_wakeup_fd_destroy(grpc_wakeup_fd* fd_info);
/* Defined in some specialized implementation's .c file, or by
* wakeup_fd_nospecial.c if no such implementation exists. */
diff --git a/src/core/iomgr/workqueue.h b/src/core/iomgr/workqueue.h
index 0790fdb006..714536233c 100644
--- a/src/core/iomgr/workqueue.h
+++ b/src/core/iomgr/workqueue.h
@@ -52,9 +52,9 @@ struct grpc_workqueue;
typedef struct grpc_workqueue grpc_workqueue;
/** Create a work queue */
-grpc_workqueue *grpc_workqueue_create (grpc_exec_ctx *exec_ctx);
+grpc_workqueue *grpc_workqueue_create(grpc_exec_ctx *exec_ctx);
-void grpc_workqueue_flush (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue);
+void grpc_workqueue_flush(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue);
#define GRPC_WORKQUEUE_REFCOUNT_DEBUG
#ifdef GRPC_WORKQUEUE_REFCOUNT_DEBUG
@@ -62,19 +62,24 @@ void grpc_workqueue_flush (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue)
grpc_workqueue_ref((p), __FILE__, __LINE__, (r))
#define GRPC_WORKQUEUE_UNREF(cl, p, r) \
grpc_workqueue_unref((cl), (p), __FILE__, __LINE__, (r))
-void grpc_workqueue_ref (grpc_workqueue * workqueue, const char *file, int line, const char *reason);
-void grpc_workqueue_unref (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue, const char *file, int line, const char *reason);
+void grpc_workqueue_ref(grpc_workqueue *workqueue, const char *file, int line,
+ const char *reason);
+void grpc_workqueue_unref(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue,
+ const char *file, int line, const char *reason);
#else
#define GRPC_WORKQUEUE_REF(p, r) grpc_workqueue_ref((p))
#define GRPC_WORKQUEUE_UNREF(cl, p, r) grpc_workqueue_unref((cl), (p))
-void grpc_workqueue_ref (grpc_workqueue * workqueue);
-void grpc_workqueue_unref (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue);
+void grpc_workqueue_ref(grpc_workqueue *workqueue);
+void grpc_workqueue_unref(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue);
#endif
/** Bind this workqueue to a pollset */
-void grpc_workqueue_add_to_pollset (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue, grpc_pollset * pollset);
+void grpc_workqueue_add_to_pollset(grpc_exec_ctx *exec_ctx,
+ grpc_workqueue *workqueue,
+ grpc_pollset *pollset);
/** Add a work item to a workqueue */
-void grpc_workqueue_push (grpc_workqueue * workqueue, grpc_closure * closure, int success);
+void grpc_workqueue_push(grpc_workqueue *workqueue, grpc_closure *closure,
+ int success);
#endif
diff --git a/src/core/iomgr/workqueue_posix.c b/src/core/iomgr/workqueue_posix.c
index 91ac0f32e4..2e510ee5a2 100644
--- a/src/core/iomgr/workqueue_posix.c
+++ b/src/core/iomgr/workqueue_posix.c
@@ -45,110 +45,98 @@
#include "src/core/iomgr/fd_posix.h"
-static void on_readable (grpc_exec_ctx * exec_ctx, void *arg, int success);
+static void on_readable(grpc_exec_ctx *exec_ctx, void *arg, int success);
-grpc_workqueue *
-grpc_workqueue_create (grpc_exec_ctx *exec_ctx)
-{
+grpc_workqueue *grpc_workqueue_create(grpc_exec_ctx *exec_ctx) {
char name[32];
- grpc_workqueue *workqueue = gpr_malloc (sizeof (grpc_workqueue));
- gpr_ref_init (&workqueue->refs, 1);
- gpr_mu_init (&workqueue->mu);
+ grpc_workqueue *workqueue = gpr_malloc(sizeof(grpc_workqueue));
+ gpr_ref_init(&workqueue->refs, 1);
+ gpr_mu_init(&workqueue->mu);
workqueue->closure_list.head = workqueue->closure_list.tail = NULL;
- grpc_wakeup_fd_init (&workqueue->wakeup_fd);
- sprintf (name, "workqueue:%p", (void *) workqueue);
- workqueue->wakeup_read_fd = grpc_fd_create (GRPC_WAKEUP_FD_GET_READ_FD (&workqueue->wakeup_fd), name);
- grpc_closure_init (&workqueue->read_closure, on_readable, workqueue);
- grpc_fd_notify_on_read (exec_ctx, workqueue->wakeup_read_fd, &workqueue->read_closure);
+ grpc_wakeup_fd_init(&workqueue->wakeup_fd);
+ sprintf(name, "workqueue:%p", (void *)workqueue);
+ workqueue->wakeup_read_fd =
+ grpc_fd_create(GRPC_WAKEUP_FD_GET_READ_FD(&workqueue->wakeup_fd), name);
+ grpc_closure_init(&workqueue->read_closure, on_readable, workqueue);
+ grpc_fd_notify_on_read(exec_ctx, workqueue->wakeup_read_fd,
+ &workqueue->read_closure);
return workqueue;
}
-static void
-workqueue_destroy (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue)
-{
- GPR_ASSERT (grpc_closure_list_empty (workqueue->closure_list));
- grpc_fd_shutdown (exec_ctx, workqueue->wakeup_read_fd);
+static void workqueue_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_workqueue *workqueue) {
+ GPR_ASSERT(grpc_closure_list_empty(workqueue->closure_list));
+ grpc_fd_shutdown(exec_ctx, workqueue->wakeup_read_fd);
}
#ifdef GRPC_WORKQUEUE_REFCOUNT_DEBUG
-void
-grpc_workqueue_ref (grpc_workqueue * workqueue, const char *file, int line, const char *reason)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "WORKQUEUE:%p ref %d -> %d %s", workqueue, (int) workqueue->refs.count, (int) workqueue->refs.count + 1, reason);
+void grpc_workqueue_ref(grpc_workqueue *workqueue, const char *file, int line,
+ const char *reason) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "WORKQUEUE:%p ref %d -> %d %s",
+ workqueue, (int)workqueue->refs.count, (int)workqueue->refs.count + 1,
+ reason);
#else
-void
-grpc_workqueue_ref (grpc_workqueue * workqueue)
-{
+void grpc_workqueue_ref(grpc_workqueue *workqueue) {
#endif
- gpr_ref (&workqueue->refs);
+ gpr_ref(&workqueue->refs);
}
#ifdef GRPC_WORKQUEUE_REFCOUNT_DEBUG
-void
-grpc_workqueue_unref (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue, const char *file, int line, const char *reason)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "WORKQUEUE:%p unref %d -> %d %s", workqueue, (int) workqueue->refs.count, (int) workqueue->refs.count - 1, reason);
+void grpc_workqueue_unref(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue,
+ const char *file, int line, const char *reason) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "WORKQUEUE:%p unref %d -> %d %s",
+ workqueue, (int)workqueue->refs.count, (int)workqueue->refs.count - 1,
+ reason);
#else
-void
-grpc_workqueue_unref (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue)
-{
+void grpc_workqueue_unref(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue) {
#endif
- if (gpr_unref (&workqueue->refs))
- {
- workqueue_destroy (exec_ctx, workqueue);
- }
+ if (gpr_unref(&workqueue->refs)) {
+ workqueue_destroy(exec_ctx, workqueue);
+ }
}
-void
-grpc_workqueue_add_to_pollset (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue, grpc_pollset * pollset)
-{
- grpc_pollset_add_fd (exec_ctx, pollset, workqueue->wakeup_read_fd);
+void grpc_workqueue_add_to_pollset(grpc_exec_ctx *exec_ctx,
+ grpc_workqueue *workqueue,
+ grpc_pollset *pollset) {
+ grpc_pollset_add_fd(exec_ctx, pollset, workqueue->wakeup_read_fd);
}
-void
-grpc_workqueue_flush (grpc_exec_ctx * exec_ctx, grpc_workqueue * workqueue)
-{
- gpr_mu_lock (&workqueue->mu);
- grpc_closure_list_move (&exec_ctx->closure_list, &workqueue->closure_list);
- gpr_mu_unlock (&workqueue->mu);
+void grpc_workqueue_flush(grpc_exec_ctx *exec_ctx, grpc_workqueue *workqueue) {
+ gpr_mu_lock(&workqueue->mu);
+ grpc_closure_list_move(&exec_ctx->closure_list, &workqueue->closure_list);
+ gpr_mu_unlock(&workqueue->mu);
}
-static void
-on_readable (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void on_readable(grpc_exec_ctx *exec_ctx, void *arg, int success) {
grpc_workqueue *workqueue = arg;
- if (!success)
- {
- gpr_mu_destroy (&workqueue->mu);
- /* HACK: let wakeup_fd code know that we stole the fd */
- workqueue->wakeup_fd.read_fd = 0;
- grpc_wakeup_fd_destroy (&workqueue->wakeup_fd);
- grpc_fd_orphan (exec_ctx, workqueue->wakeup_read_fd, NULL, "destroy");
- gpr_free (workqueue);
- }
- else
- {
- gpr_mu_lock (&workqueue->mu);
- grpc_closure_list_move (&exec_ctx->closure_list, &workqueue->closure_list);
- grpc_wakeup_fd_consume_wakeup (&workqueue->wakeup_fd);
- gpr_mu_unlock (&workqueue->mu);
- grpc_fd_notify_on_read (exec_ctx, workqueue->wakeup_read_fd, &workqueue->read_closure);
- }
+ if (!success) {
+ gpr_mu_destroy(&workqueue->mu);
+ /* HACK: let wakeup_fd code know that we stole the fd */
+ workqueue->wakeup_fd.read_fd = 0;
+ grpc_wakeup_fd_destroy(&workqueue->wakeup_fd);
+ grpc_fd_orphan(exec_ctx, workqueue->wakeup_read_fd, NULL, "destroy");
+ gpr_free(workqueue);
+ } else {
+ gpr_mu_lock(&workqueue->mu);
+ grpc_closure_list_move(&exec_ctx->closure_list, &workqueue->closure_list);
+ grpc_wakeup_fd_consume_wakeup(&workqueue->wakeup_fd);
+ gpr_mu_unlock(&workqueue->mu);
+ grpc_fd_notify_on_read(exec_ctx, workqueue->wakeup_read_fd,
+ &workqueue->read_closure);
+ }
}
-void
-grpc_workqueue_push (grpc_workqueue * workqueue, grpc_closure * closure, int success)
-{
+void grpc_workqueue_push(grpc_workqueue *workqueue, grpc_closure *closure,
+ int success) {
closure->success = success;
closure->next = NULL;
- gpr_mu_lock (&workqueue->mu);
- if (grpc_closure_list_empty (workqueue->closure_list))
- {
- grpc_wakeup_fd_wakeup (&workqueue->wakeup_fd);
- }
- grpc_closure_list_add (&workqueue->closure_list, closure, success);
- gpr_mu_unlock (&workqueue->mu);
+ gpr_mu_lock(&workqueue->mu);
+ if (grpc_closure_list_empty(workqueue->closure_list)) {
+ grpc_wakeup_fd_wakeup(&workqueue->wakeup_fd);
+ }
+ grpc_closure_list_add(&workqueue->closure_list, closure, success);
+ gpr_mu_unlock(&workqueue->mu);
}
#endif /* GPR_POSIX_SOCKET */
diff --git a/src/core/iomgr/workqueue_posix.h b/src/core/iomgr/workqueue_posix.h
index 48c5af9cae..589034fe1b 100644
--- a/src/core/iomgr/workqueue_posix.h
+++ b/src/core/iomgr/workqueue_posix.h
@@ -36,8 +36,7 @@
struct grpc_fd;
-struct grpc_workqueue
-{
+struct grpc_workqueue {
gpr_refcount refs;
gpr_mu mu;
diff --git a/src/core/json/json.c b/src/core/json/json.c
index a44e74b1e1..96e11eebb1 100644
--- a/src/core/json/json.c
+++ b/src/core/json/json.c
@@ -37,37 +37,28 @@
#include "src/core/json/json.h"
-grpc_json *
-grpc_json_create (grpc_json_type type)
-{
- grpc_json *json = gpr_malloc (sizeof (*json));
- memset (json, 0, sizeof (*json));
+grpc_json *grpc_json_create(grpc_json_type type) {
+ grpc_json *json = gpr_malloc(sizeof(*json));
+ memset(json, 0, sizeof(*json));
json->type = type;
return json;
}
-void
-grpc_json_destroy (grpc_json * json)
-{
- while (json->child)
- {
- grpc_json_destroy (json->child);
- }
+void grpc_json_destroy(grpc_json *json) {
+ while (json->child) {
+ grpc_json_destroy(json->child);
+ }
- if (json->next)
- {
- json->next->prev = json->prev;
- }
+ if (json->next) {
+ json->next->prev = json->prev;
+ }
- if (json->prev)
- {
- json->prev->next = json->next;
- }
- else if (json->parent)
- {
- json->parent->child = json->next;
- }
+ if (json->prev) {
+ json->prev->next = json->next;
+ } else if (json->parent) {
+ json->parent->child = json->next;
+ }
- gpr_free (json);
+ gpr_free(json);
}
diff --git a/src/core/json/json.h b/src/core/json/json.h
index 37584ee262..c4df2998c3 100644
--- a/src/core/json/json.h
+++ b/src/core/json/json.h
@@ -41,8 +41,7 @@
/* A tree-like structure to hold json values. The key and value pointers
* are not owned by it.
*/
-typedef struct grpc_json
-{
+typedef struct grpc_json {
struct grpc_json *next;
struct grpc_json *prev;
struct grpc_json *child;
@@ -66,8 +65,8 @@ typedef struct grpc_json
*
* Delete the allocated tree afterward using grpc_json_destroy().
*/
-grpc_json *grpc_json_parse_string_with_len (char *input, size_t size);
-grpc_json *grpc_json_parse_string (char *input);
+grpc_json *grpc_json_parse_string_with_len(char *input, size_t size);
+grpc_json *grpc_json_parse_string(char *input);
/* This function will create a new string using gpr_realloc, and will
* deserialize the grpc_json tree into it. It'll be zero-terminated,
@@ -77,13 +76,13 @@ grpc_json *grpc_json_parse_string (char *input);
* If indent is 0, then newlines will be suppressed as well, and the
* output will be condensed at its maximum.
*/
-char *grpc_json_dump_to_string (grpc_json * json, int indent);
+char *grpc_json_dump_to_string(grpc_json *json, int indent);
/* Use these to create or delete a grpc_json object.
* Deletion is recursive. We will not attempt to free any of the strings
* in any of the objects of that tree.
*/
-grpc_json *grpc_json_create (grpc_json_type type);
-void grpc_json_destroy (grpc_json * json);
+grpc_json *grpc_json_create(grpc_json_type type);
+void grpc_json_destroy(grpc_json *json);
#endif /* GRPC_INTERNAL_CORE_JSON_JSON_H */
diff --git a/src/core/json/json_common.h b/src/core/json/json_common.h
index f9dbeb4766..481695b38b 100644
--- a/src/core/json/json_common.h
+++ b/src/core/json/json_common.h
@@ -35,8 +35,7 @@
#define GRPC_INTERNAL_CORE_JSON_JSON_COMMON_H
/* The various json types. */
-typedef enum
-{
+typedef enum {
GRPC_JSON_OBJECT,
GRPC_JSON_ARRAY,
GRPC_JSON_STRING,
diff --git a/src/core/json/json_reader.c b/src/core/json/json_reader.c
index 70dfe0efcf..8abad01252 100644
--- a/src/core/json/json_reader.c
+++ b/src/core/json/json_reader.c
@@ -37,718 +37,623 @@
#include "src/core/json/json_reader.h"
-static void
-json_reader_string_clear (grpc_json_reader * reader)
-{
- reader->vtable->string_clear (reader->userdata);
+static void json_reader_string_clear(grpc_json_reader *reader) {
+ reader->vtable->string_clear(reader->userdata);
}
-static void
-json_reader_string_add_char (grpc_json_reader * reader, gpr_uint32 c)
-{
- reader->vtable->string_add_char (reader->userdata, c);
+static void json_reader_string_add_char(grpc_json_reader *reader,
+ gpr_uint32 c) {
+ reader->vtable->string_add_char(reader->userdata, c);
}
-static void
-json_reader_string_add_utf32 (grpc_json_reader * reader, gpr_uint32 utf32)
-{
- reader->vtable->string_add_utf32 (reader->userdata, utf32);
+static void json_reader_string_add_utf32(grpc_json_reader *reader,
+ gpr_uint32 utf32) {
+ reader->vtable->string_add_utf32(reader->userdata, utf32);
}
-static gpr_uint32
-grpc_json_reader_read_char (grpc_json_reader * reader)
-{
- return reader->vtable->read_char (reader->userdata);
+static gpr_uint32 grpc_json_reader_read_char(grpc_json_reader *reader) {
+ return reader->vtable->read_char(reader->userdata);
}
-static void
-json_reader_container_begins (grpc_json_reader * reader, grpc_json_type type)
-{
- reader->vtable->container_begins (reader->userdata, type);
+static void json_reader_container_begins(grpc_json_reader *reader,
+ grpc_json_type type) {
+ reader->vtable->container_begins(reader->userdata, type);
}
-static grpc_json_type
-grpc_json_reader_container_ends (grpc_json_reader * reader)
-{
- return reader->vtable->container_ends (reader->userdata);
+static grpc_json_type grpc_json_reader_container_ends(
+ grpc_json_reader *reader) {
+ return reader->vtable->container_ends(reader->userdata);
}
-static void
-json_reader_set_key (grpc_json_reader * reader)
-{
- reader->vtable->set_key (reader->userdata);
+static void json_reader_set_key(grpc_json_reader *reader) {
+ reader->vtable->set_key(reader->userdata);
}
-static void
-json_reader_set_string (grpc_json_reader * reader)
-{
- reader->vtable->set_string (reader->userdata);
+static void json_reader_set_string(grpc_json_reader *reader) {
+ reader->vtable->set_string(reader->userdata);
}
-static int
-json_reader_set_number (grpc_json_reader * reader)
-{
- return reader->vtable->set_number (reader->userdata);
+static int json_reader_set_number(grpc_json_reader *reader) {
+ return reader->vtable->set_number(reader->userdata);
}
-static void
-json_reader_set_true (grpc_json_reader * reader)
-{
- reader->vtable->set_true (reader->userdata);
+static void json_reader_set_true(grpc_json_reader *reader) {
+ reader->vtable->set_true(reader->userdata);
}
-static void
-json_reader_set_false (grpc_json_reader * reader)
-{
- reader->vtable->set_false (reader->userdata);
+static void json_reader_set_false(grpc_json_reader *reader) {
+ reader->vtable->set_false(reader->userdata);
}
-static void
-json_reader_set_null (grpc_json_reader * reader)
-{
- reader->vtable->set_null (reader->userdata);
+static void json_reader_set_null(grpc_json_reader *reader) {
+ reader->vtable->set_null(reader->userdata);
}
/* Call this function to initialize the reader structure. */
-void
-grpc_json_reader_init (grpc_json_reader * reader, grpc_json_reader_vtable * vtable, void *userdata)
-{
- memset (reader, 0, sizeof (*reader));
+void grpc_json_reader_init(grpc_json_reader *reader,
+ grpc_json_reader_vtable *vtable, void *userdata) {
+ memset(reader, 0, sizeof(*reader));
reader->vtable = vtable;
reader->userdata = userdata;
- json_reader_string_clear (reader);
+ json_reader_string_clear(reader);
reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
}
-int
-grpc_json_reader_is_complete (grpc_json_reader * reader)
-{
- return ((reader->depth == 0) && ((reader->state == GRPC_JSON_STATE_END) || (reader->state == GRPC_JSON_STATE_VALUE_END)));
+int grpc_json_reader_is_complete(grpc_json_reader *reader) {
+ return ((reader->depth == 0) &&
+ ((reader->state == GRPC_JSON_STATE_END) ||
+ (reader->state == GRPC_JSON_STATE_VALUE_END)));
}
-grpc_json_reader_status
-grpc_json_reader_run (grpc_json_reader * reader)
-{
+grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader) {
gpr_uint32 c, success;
/* This state-machine is a strict implementation of ECMA-404 */
- for (;;)
- {
- c = grpc_json_reader_read_char (reader);
- switch (c)
- {
- /* Let's process the error cases first. */
- case GRPC_JSON_READ_CHAR_ERROR:
- return GRPC_JSON_READ_ERROR;
-
- case GRPC_JSON_READ_CHAR_EAGAIN:
- return GRPC_JSON_EAGAIN;
-
- case GRPC_JSON_READ_CHAR_EOF:
- if (grpc_json_reader_is_complete (reader))
- {
- return GRPC_JSON_DONE;
- }
- else
- {
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- /* Processing whitespaces. */
- case ' ':
- case '\t':
- case '\n':
- case '\r':
- switch (reader->state)
- {
- case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
- case GRPC_JSON_STATE_OBJECT_KEY_END:
- case GRPC_JSON_STATE_VALUE_BEGIN:
- case GRPC_JSON_STATE_VALUE_END:
- case GRPC_JSON_STATE_END:
- break;
-
- case GRPC_JSON_STATE_OBJECT_KEY_STRING:
- case GRPC_JSON_STATE_VALUE_STRING:
- if (c != ' ')
- return GRPC_JSON_PARSE_ERROR;
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_char (reader, c);
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER:
- case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
- case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
- case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
- success = (gpr_uint32) json_reader_set_number (reader);
- if (!success)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_clear (reader);
- reader->state = GRPC_JSON_STATE_VALUE_END;
- break;
-
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- /* Value, object or array terminations. */
- case ',':
- case '}':
- case ']':
- switch (reader->state)
- {
- case GRPC_JSON_STATE_OBJECT_KEY_STRING:
- case GRPC_JSON_STATE_VALUE_STRING:
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_char (reader, c);
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER:
- case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
- case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
- case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
- success = (gpr_uint32) json_reader_set_number (reader);
- if (!success)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_clear (reader);
- reader->state = GRPC_JSON_STATE_VALUE_END;
- /* The missing break here is intentional. */
-
- case GRPC_JSON_STATE_VALUE_END:
- case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
- case GRPC_JSON_STATE_VALUE_BEGIN:
- if (c == ',')
- {
- if (reader->state != GRPC_JSON_STATE_VALUE_END)
- {
- return GRPC_JSON_PARSE_ERROR;
- }
- if (reader->in_object)
- {
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
- }
- else
- {
- reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
- }
- }
- else
- {
- if (reader->depth-- == 0)
- return GRPC_JSON_PARSE_ERROR;
- if ((c == '}') && !reader->in_object)
- {
- return GRPC_JSON_PARSE_ERROR;
- }
- if ((c == '}') && (reader->state == GRPC_JSON_STATE_OBJECT_KEY_BEGIN) && !reader->container_just_begun)
- {
- return GRPC_JSON_PARSE_ERROR;
- }
- if ((c == ']') && !reader->in_array)
- return GRPC_JSON_PARSE_ERROR;
- if ((c == ']') && (reader->state == GRPC_JSON_STATE_VALUE_BEGIN) && !reader->container_just_begun)
- {
- return GRPC_JSON_PARSE_ERROR;
- }
- reader->state = GRPC_JSON_STATE_VALUE_END;
- switch (grpc_json_reader_container_ends (reader))
- {
- case GRPC_JSON_OBJECT:
- reader->in_object = 1;
- reader->in_array = 0;
- break;
- case GRPC_JSON_ARRAY:
- reader->in_object = 0;
- reader->in_array = 1;
- break;
- case GRPC_JSON_TOP_LEVEL:
- if (reader->depth != 0)
- return GRPC_JSON_INTERNAL_ERROR;
- reader->in_object = 0;
- reader->in_array = 0;
- reader->state = GRPC_JSON_STATE_END;
- break;
- default:
- return GRPC_JSON_INTERNAL_ERROR;
- }
- }
- break;
-
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- /* In-string escaping. */
- case '\\':
- switch (reader->state)
- {
- case GRPC_JSON_STATE_OBJECT_KEY_STRING:
- reader->escaped_string_was_key = 1;
- reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
- break;
-
- case GRPC_JSON_STATE_VALUE_STRING:
- reader->escaped_string_was_key = 0;
- reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
- break;
-
- /* This is the \\ case. */
- case GRPC_JSON_STATE_STRING_ESCAPE:
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_char (reader, '\\');
- if (reader->escaped_string_was_key)
- {
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
- }
- else
- {
- reader->state = GRPC_JSON_STATE_VALUE_STRING;
- }
- break;
-
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- default:
- reader->container_just_begun = 0;
- switch (reader->state)
- {
- case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
- if (c != '"')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
- break;
-
- case GRPC_JSON_STATE_OBJECT_KEY_STRING:
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- if (c == '"')
- {
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_END;
- json_reader_set_key (reader);
- json_reader_string_clear (reader);
- }
- else
- {
- if (c <= 0x001f)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_char (reader, c);
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_STRING:
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- if (c == '"')
- {
- reader->state = GRPC_JSON_STATE_VALUE_END;
- json_reader_set_string (reader);
- json_reader_string_clear (reader);
- }
- else
- {
- if (c < 32)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_char (reader, c);
- }
- break;
-
- case GRPC_JSON_STATE_OBJECT_KEY_END:
- if (c != ':')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
- break;
-
- case GRPC_JSON_STATE_VALUE_BEGIN:
- switch (c)
- {
- case 't':
- reader->state = GRPC_JSON_STATE_VALUE_TRUE_R;
- break;
-
- case 'f':
- reader->state = GRPC_JSON_STATE_VALUE_FALSE_A;
- break;
-
- case 'n':
- reader->state = GRPC_JSON_STATE_VALUE_NULL_U;
- break;
-
- case '"':
- reader->state = GRPC_JSON_STATE_VALUE_STRING;
- break;
-
- case '0':
- json_reader_string_add_char (reader, c);
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
- break;
-
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case '-':
- json_reader_string_add_char (reader, c);
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER;
- break;
-
- case '{':
- reader->container_just_begun = 1;
- json_reader_container_begins (reader, GRPC_JSON_OBJECT);
- reader->depth++;
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
- reader->in_object = 1;
- reader->in_array = 0;
- break;
-
- case '[':
- reader->container_just_begun = 1;
- json_reader_container_begins (reader, GRPC_JSON_ARRAY);
- reader->depth++;
- reader->in_object = 0;
- reader->in_array = 1;
- break;
- }
- break;
-
- case GRPC_JSON_STATE_STRING_ESCAPE:
- if (reader->escaped_string_was_key)
- {
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
- }
- else
- {
- reader->state = GRPC_JSON_STATE_VALUE_STRING;
- }
- if (reader->unicode_high_surrogate && c != 'u')
- return GRPC_JSON_PARSE_ERROR;
- switch (c)
- {
- case '"':
- case '/':
- json_reader_string_add_char (reader, c);
- break;
- case 'b':
- json_reader_string_add_char (reader, '\b');
- break;
- case 'f':
- json_reader_string_add_char (reader, '\f');
- break;
- case 'n':
- json_reader_string_add_char (reader, '\n');
- break;
- case 'r':
- json_reader_string_add_char (reader, '\r');
- break;
- case 't':
- json_reader_string_add_char (reader, '\t');
- break;
- case 'u':
- reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U1;
- reader->unicode_char = 0;
- break;
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_STRING_ESCAPE_U1:
- case GRPC_JSON_STATE_STRING_ESCAPE_U2:
- case GRPC_JSON_STATE_STRING_ESCAPE_U3:
- case GRPC_JSON_STATE_STRING_ESCAPE_U4:
- if ((c >= '0') && (c <= '9'))
- {
- c -= '0';
- }
- else if ((c >= 'A') && (c <= 'F'))
- {
- c -= 'A' - 10;
- }
- else if ((c >= 'a') && (c <= 'f'))
- {
- c -= 'a' - 10;
- }
- else
- {
- return GRPC_JSON_PARSE_ERROR;
- }
- reader->unicode_char = (gpr_uint16) (reader->unicode_char << 4);
- reader->unicode_char = (gpr_uint16) (reader->unicode_char | c);
-
- switch (reader->state)
- {
- case GRPC_JSON_STATE_STRING_ESCAPE_U1:
- reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U2;
- break;
- case GRPC_JSON_STATE_STRING_ESCAPE_U2:
- reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U3;
- break;
- case GRPC_JSON_STATE_STRING_ESCAPE_U3:
- reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U4;
- break;
- case GRPC_JSON_STATE_STRING_ESCAPE_U4:
- /* See grpc_json_writer_escape_string to have a description
- * of what's going on here.
- */
- if ((reader->unicode_char & 0xfc00) == 0xd800)
- {
- /* high surrogate utf-16 */
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- reader->unicode_high_surrogate = reader->unicode_char;
- }
- else if ((reader->unicode_char & 0xfc00) == 0xdc00)
- {
- /* low surrogate utf-16 */
- gpr_uint32 utf32;
- if (reader->unicode_high_surrogate == 0)
- return GRPC_JSON_PARSE_ERROR;
- utf32 = 0x10000;
- utf32 += (gpr_uint32) ((reader->unicode_high_surrogate - 0xd800) * 0x400);
- utf32 += (gpr_uint32) (reader->unicode_char - 0xdc00);
- json_reader_string_add_utf32 (reader, utf32);
- reader->unicode_high_surrogate = 0;
- }
- else
- {
- /* anything else */
- if (reader->unicode_high_surrogate != 0)
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_utf32 (reader, reader->unicode_char);
- }
- if (reader->escaped_string_was_key)
- {
- reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
- }
- else
- {
- reader->state = GRPC_JSON_STATE_VALUE_STRING;
- }
- break;
- default:
- return GRPC_JSON_INTERNAL_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER:
- json_reader_string_add_char (reader, c);
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- break;
- case 'e':
- case 'E':
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
- break;
- case '.':
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
- break;
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
- json_reader_string_add_char (reader, c);
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- break;
- case 'e':
- case 'E':
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
- break;
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
- if (c != '.')
- return GRPC_JSON_PARSE_ERROR;
- json_reader_string_add_char (reader, c);
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER_DOT:
- json_reader_string_add_char (reader, c);
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
- break;
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER_E:
- json_reader_string_add_char (reader, c);
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case '+':
- case '-':
- reader->state = GRPC_JSON_STATE_VALUE_NUMBER_EPM;
- break;
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
- json_reader_string_add_char (reader, c);
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- break;
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_VALUE_TRUE_R:
- if (c != 'r')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_TRUE_U;
- break;
-
- case GRPC_JSON_STATE_VALUE_TRUE_U:
- if (c != 'u')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_TRUE_E;
- break;
-
- case GRPC_JSON_STATE_VALUE_TRUE_E:
- if (c != 'e')
- return GRPC_JSON_PARSE_ERROR;
- json_reader_set_true (reader);
- reader->state = GRPC_JSON_STATE_VALUE_END;
- break;
-
- case GRPC_JSON_STATE_VALUE_FALSE_A:
- if (c != 'a')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_FALSE_L;
- break;
-
- case GRPC_JSON_STATE_VALUE_FALSE_L:
- if (c != 'l')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_FALSE_S;
- break;
-
- case GRPC_JSON_STATE_VALUE_FALSE_S:
- if (c != 's')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_FALSE_E;
- break;
-
- case GRPC_JSON_STATE_VALUE_FALSE_E:
- if (c != 'e')
- return GRPC_JSON_PARSE_ERROR;
- json_reader_set_false (reader);
- reader->state = GRPC_JSON_STATE_VALUE_END;
- break;
-
- case GRPC_JSON_STATE_VALUE_NULL_U:
- if (c != 'u')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_NULL_L1;
- break;
-
- case GRPC_JSON_STATE_VALUE_NULL_L1:
- if (c != 'l')
- return GRPC_JSON_PARSE_ERROR;
- reader->state = GRPC_JSON_STATE_VALUE_NULL_L2;
- break;
-
- case GRPC_JSON_STATE_VALUE_NULL_L2:
- if (c != 'l')
- return GRPC_JSON_PARSE_ERROR;
- json_reader_set_null (reader);
- reader->state = GRPC_JSON_STATE_VALUE_END;
- break;
-
- /* All of the VALUE_END cases are handled in the specialized case
- * above. */
- case GRPC_JSON_STATE_VALUE_END:
- switch (c)
- {
- case ',':
- case '}':
- case ']':
- return GRPC_JSON_INTERNAL_ERROR;
- break;
-
- default:
- return GRPC_JSON_PARSE_ERROR;
- }
- break;
-
- case GRPC_JSON_STATE_END:
- return GRPC_JSON_PARSE_ERROR;
- }
- }
+ for (;;) {
+ c = grpc_json_reader_read_char(reader);
+ switch (c) {
+ /* Let's process the error cases first. */
+ case GRPC_JSON_READ_CHAR_ERROR:
+ return GRPC_JSON_READ_ERROR;
+
+ case GRPC_JSON_READ_CHAR_EAGAIN:
+ return GRPC_JSON_EAGAIN;
+
+ case GRPC_JSON_READ_CHAR_EOF:
+ if (grpc_json_reader_is_complete(reader)) {
+ return GRPC_JSON_DONE;
+ } else {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ /* Processing whitespaces. */
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\r':
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
+ case GRPC_JSON_STATE_OBJECT_KEY_END:
+ case GRPC_JSON_STATE_VALUE_BEGIN:
+ case GRPC_JSON_STATE_VALUE_END:
+ case GRPC_JSON_STATE_END:
+ break;
+
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ case GRPC_JSON_STATE_VALUE_STRING:
+ if (c != ' ') return GRPC_JSON_PARSE_ERROR;
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER:
+ case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
+ case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
+ case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
+ success = (gpr_uint32)json_reader_set_number(reader);
+ if (!success) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_clear(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ /* Value, object or array terminations. */
+ case ',':
+ case '}':
+ case ']':
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ case GRPC_JSON_STATE_VALUE_STRING:
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER:
+ case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
+ case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
+ case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
+ success = (gpr_uint32)json_reader_set_number(reader);
+ if (!success) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_clear(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ /* The missing break here is intentional. */
+
+ case GRPC_JSON_STATE_VALUE_END:
+ case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
+ case GRPC_JSON_STATE_VALUE_BEGIN:
+ if (c == ',') {
+ if (reader->state != GRPC_JSON_STATE_VALUE_END) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ if (reader->in_object) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
+ }
+ } else {
+ if (reader->depth-- == 0) return GRPC_JSON_PARSE_ERROR;
+ if ((c == '}') && !reader->in_object) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ if ((c == '}') &&
+ (reader->state == GRPC_JSON_STATE_OBJECT_KEY_BEGIN) &&
+ !reader->container_just_begun) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ if ((c == ']') && !reader->in_array) return GRPC_JSON_PARSE_ERROR;
+ if ((c == ']') &&
+ (reader->state == GRPC_JSON_STATE_VALUE_BEGIN) &&
+ !reader->container_just_begun) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ switch (grpc_json_reader_container_ends(reader)) {
+ case GRPC_JSON_OBJECT:
+ reader->in_object = 1;
+ reader->in_array = 0;
+ break;
+ case GRPC_JSON_ARRAY:
+ reader->in_object = 0;
+ reader->in_array = 1;
+ break;
+ case GRPC_JSON_TOP_LEVEL:
+ if (reader->depth != 0) return GRPC_JSON_INTERNAL_ERROR;
+ reader->in_object = 0;
+ reader->in_array = 0;
+ reader->state = GRPC_JSON_STATE_END;
+ break;
+ default:
+ return GRPC_JSON_INTERNAL_ERROR;
+ }
+ }
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ /* In-string escaping. */
+ case '\\':
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ reader->escaped_string_was_key = 1;
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_STRING:
+ reader->escaped_string_was_key = 0;
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
+ break;
+
+ /* This is the \\ case. */
+ case GRPC_JSON_STATE_STRING_ESCAPE:
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, '\\');
+ if (reader->escaped_string_was_key) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ }
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ default:
+ reader->container_just_begun = 0;
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
+ if (c != '"') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ break;
+
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ if (c == '"') {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_END;
+ json_reader_set_key(reader);
+ json_reader_string_clear(reader);
+ } else {
+ if (c <= 0x001f) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_STRING:
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ if (c == '"') {
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ json_reader_set_string(reader);
+ json_reader_string_clear(reader);
+ } else {
+ if (c < 32) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ }
+ break;
+
+ case GRPC_JSON_STATE_OBJECT_KEY_END:
+ if (c != ':') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_BEGIN:
+ switch (c) {
+ case 't':
+ reader->state = GRPC_JSON_STATE_VALUE_TRUE_R;
+ break;
+
+ case 'f':
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_A;
+ break;
+
+ case 'n':
+ reader->state = GRPC_JSON_STATE_VALUE_NULL_U;
+ break;
+
+ case '"':
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ break;
+
+ case '0':
+ json_reader_string_add_char(reader, c);
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
+ break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '-':
+ json_reader_string_add_char(reader, c);
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER;
+ break;
+
+ case '{':
+ reader->container_just_begun = 1;
+ json_reader_container_begins(reader, GRPC_JSON_OBJECT);
+ reader->depth++;
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
+ reader->in_object = 1;
+ reader->in_array = 0;
+ break;
+
+ case '[':
+ reader->container_just_begun = 1;
+ json_reader_container_begins(reader, GRPC_JSON_ARRAY);
+ reader->depth++;
+ reader->in_object = 0;
+ reader->in_array = 1;
+ break;
+ }
+ break;
+
+ case GRPC_JSON_STATE_STRING_ESCAPE:
+ if (reader->escaped_string_was_key) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ }
+ if (reader->unicode_high_surrogate && c != 'u')
+ return GRPC_JSON_PARSE_ERROR;
+ switch (c) {
+ case '"':
+ case '/':
+ json_reader_string_add_char(reader, c);
+ break;
+ case 'b':
+ json_reader_string_add_char(reader, '\b');
+ break;
+ case 'f':
+ json_reader_string_add_char(reader, '\f');
+ break;
+ case 'n':
+ json_reader_string_add_char(reader, '\n');
+ break;
+ case 'r':
+ json_reader_string_add_char(reader, '\r');
+ break;
+ case 't':
+ json_reader_string_add_char(reader, '\t');
+ break;
+ case 'u':
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U1;
+ reader->unicode_char = 0;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_STRING_ESCAPE_U1:
+ case GRPC_JSON_STATE_STRING_ESCAPE_U2:
+ case GRPC_JSON_STATE_STRING_ESCAPE_U3:
+ case GRPC_JSON_STATE_STRING_ESCAPE_U4:
+ if ((c >= '0') && (c <= '9')) {
+ c -= '0';
+ } else if ((c >= 'A') && (c <= 'F')) {
+ c -= 'A' - 10;
+ } else if ((c >= 'a') && (c <= 'f')) {
+ c -= 'a' - 10;
+ } else {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ reader->unicode_char = (gpr_uint16)(reader->unicode_char << 4);
+ reader->unicode_char = (gpr_uint16)(reader->unicode_char | c);
+
+ switch (reader->state) {
+ case GRPC_JSON_STATE_STRING_ESCAPE_U1:
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U2;
+ break;
+ case GRPC_JSON_STATE_STRING_ESCAPE_U2:
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U3;
+ break;
+ case GRPC_JSON_STATE_STRING_ESCAPE_U3:
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U4;
+ break;
+ case GRPC_JSON_STATE_STRING_ESCAPE_U4:
+ /* See grpc_json_writer_escape_string to have a description
+ * of what's going on here.
+ */
+ if ((reader->unicode_char & 0xfc00) == 0xd800) {
+ /* high surrogate utf-16 */
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ reader->unicode_high_surrogate = reader->unicode_char;
+ } else if ((reader->unicode_char & 0xfc00) == 0xdc00) {
+ /* low surrogate utf-16 */
+ gpr_uint32 utf32;
+ if (reader->unicode_high_surrogate == 0)
+ return GRPC_JSON_PARSE_ERROR;
+ utf32 = 0x10000;
+ utf32 += (gpr_uint32)(
+ (reader->unicode_high_surrogate - 0xd800) * 0x400);
+ utf32 += (gpr_uint32)(reader->unicode_char - 0xdc00);
+ json_reader_string_add_utf32(reader, utf32);
+ reader->unicode_high_surrogate = 0;
+ } else {
+ /* anything else */
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_utf32(reader, reader->unicode_char);
+ }
+ if (reader->escaped_string_was_key) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ }
+ break;
+ default:
+ return GRPC_JSON_INTERNAL_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ case 'e':
+ case 'E':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
+ break;
+ case '.':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ case 'e':
+ case 'E':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
+ if (c != '.') return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_DOT:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_E:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '+':
+ case '-':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_EPM;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_TRUE_R:
+ if (c != 'r') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_TRUE_U;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_TRUE_U:
+ if (c != 'u') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_TRUE_E;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_TRUE_E:
+ if (c != 'e') return GRPC_JSON_PARSE_ERROR;
+ json_reader_set_true(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_A:
+ if (c != 'a') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_L;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_L:
+ if (c != 'l') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_S;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_S:
+ if (c != 's') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_E;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_E:
+ if (c != 'e') return GRPC_JSON_PARSE_ERROR;
+ json_reader_set_false(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NULL_U:
+ if (c != 'u') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_NULL_L1;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NULL_L1:
+ if (c != 'l') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_NULL_L2;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NULL_L2:
+ if (c != 'l') return GRPC_JSON_PARSE_ERROR;
+ json_reader_set_null(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ /* All of the VALUE_END cases are handled in the specialized case
+ * above. */
+ case GRPC_JSON_STATE_VALUE_END:
+ switch (c) {
+ case ',':
+ case '}':
+ case ']':
+ return GRPC_JSON_INTERNAL_ERROR;
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_END:
+ return GRPC_JSON_PARSE_ERROR;
+ }
}
+ }
return GRPC_JSON_INTERNAL_ERROR;
}
diff --git a/src/core/json/json_reader.h b/src/core/json/json_reader.h
index f77036b4cb..417db110cf 100644
--- a/src/core/json/json_reader.h
+++ b/src/core/json/json_reader.h
@@ -37,8 +37,7 @@
#include <grpc/support/port_platform.h>
#include "src/core/json/json_common.h"
-typedef enum
-{
+typedef enum {
GRPC_JSON_STATE_OBJECT_KEY_BEGIN,
GRPC_JSON_STATE_OBJECT_KEY_STRING,
GRPC_JSON_STATE_OBJECT_KEY_END,
@@ -69,8 +68,7 @@ typedef enum
GRPC_JSON_STATE_END
} grpc_json_reader_state;
-enum
-{
+enum {
/* The first non-unicode value is 0x110000. But let's pick
* a value high enough to start our error codes from. These
* values are safe to return from the read_char function.
@@ -82,34 +80,32 @@ enum
struct grpc_json_reader;
-typedef struct grpc_json_reader_vtable
-{
+typedef struct grpc_json_reader_vtable {
/* Clears your internal string scratchpad. */
- void (*string_clear) (void *userdata);
+ void (*string_clear)(void *userdata);
/* Adds a char to the string scratchpad. */
- void (*string_add_char) (void *userdata, gpr_uint32 c);
+ void (*string_add_char)(void *userdata, gpr_uint32 c);
/* Adds a utf32 char to the string scratchpad. */
- void (*string_add_utf32) (void *userdata, gpr_uint32 c);
+ void (*string_add_utf32)(void *userdata, gpr_uint32 c);
/* Reads a character from your input. May be utf-8, 16 or 32. */
- gpr_uint32 (*read_char) (void *userdata);
+ gpr_uint32 (*read_char)(void *userdata);
/* Starts a container of type GRPC_JSON_ARRAY or GRPC_JSON_OBJECT. */
- void (*container_begins) (void *userdata, grpc_json_type type);
+ void (*container_begins)(void *userdata, grpc_json_type type);
/* Ends the current container. Must return the type of its parent. */
- grpc_json_type (*container_ends) (void *userdata);
+ grpc_json_type (*container_ends)(void *userdata);
/* Your internal string scratchpad is an object's key. */
- void (*set_key) (void *userdata);
+ void (*set_key)(void *userdata);
/* Your internal string scratchpad is a string value. */
- void (*set_string) (void *userdata);
+ void (*set_string)(void *userdata);
/* Your internal string scratchpad is a numerical value. Return 1 if valid. */
- int (*set_number) (void *userdata);
+ int (*set_number)(void *userdata);
/* Sets the values true, false or null. */
- void (*set_true) (void *userdata);
- void (*set_false) (void *userdata);
- void (*set_null) (void *userdata);
+ void (*set_true)(void *userdata);
+ void (*set_false)(void *userdata);
+ void (*set_null)(void *userdata);
} grpc_json_reader_vtable;
-typedef struct grpc_json_reader
-{
+typedef struct grpc_json_reader {
/* That structure is fully private, and initialized by grpc_json_reader_init.
* The definition is public so you can put it on your stack.
*/
@@ -126,13 +122,12 @@ typedef struct grpc_json_reader
} grpc_json_reader;
/* The return type of the parser. */
-typedef enum
-{
- GRPC_JSON_DONE, /* The parser finished successfully. */
- GRPC_JSON_EAGAIN, /* The parser yields to get more data. */
- GRPC_JSON_READ_ERROR, /* The parser passes through a read error. */
- GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
- GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
+typedef enum {
+ GRPC_JSON_DONE, /* The parser finished successfully. */
+ GRPC_JSON_EAGAIN, /* The parser yields to get more data. */
+ GRPC_JSON_READ_ERROR, /* The parser passes through a read error. */
+ GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
+ GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
} grpc_json_reader_status;
/* Call this function to start parsing the input. It will return the following:
@@ -149,16 +144,17 @@ typedef enum
* . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid
* internal state.
*/
-grpc_json_reader_status grpc_json_reader_run (grpc_json_reader * reader);
+grpc_json_reader_status grpc_json_reader_run(grpc_json_reader *reader);
/* Call this function to initialize the reader structure. */
-void grpc_json_reader_init (grpc_json_reader * reader, grpc_json_reader_vtable * vtable, void *userdata);
+void grpc_json_reader_init(grpc_json_reader *reader,
+ grpc_json_reader_vtable *vtable, void *userdata);
/* You may call this from the read_char callback if you don't know where is the
* end of your input stream, and you'd like the json reader to hint you that it
* has completed reading its input, so you can return an EOF to it. Note that
* there might still be trailing whitespaces after that point.
*/
-int grpc_json_reader_is_complete (grpc_json_reader * reader);
+int grpc_json_reader_is_complete(grpc_json_reader *reader);
#endif /* GRPC_INTERNAL_CORE_JSON_JSON_READER_H */
diff --git a/src/core/json/json_string.c b/src/core/json/json_string.c
index ea596ee8fd..0461c2703f 100644
--- a/src/core/json/json_string.c
+++ b/src/core/json/json_string.c
@@ -52,8 +52,7 @@
* Note that this works because the act of parsing json always reduces its
* input size, and never expands it.
*/
-typedef struct
-{
+typedef struct {
grpc_json *top;
grpc_json *current_container;
grpc_json *current_value;
@@ -67,8 +66,7 @@ typedef struct
/* This json writer will put everything in a big string.
* The point is that we allocate that string in chunks of 256 bytes.
*/
-typedef struct
-{
+typedef struct {
char *output;
size_t free_space;
size_t string_len;
@@ -79,130 +77,104 @@ typedef struct
* and will enlarge it if necessary. We're only allocating chunks of 256
* bytes at a time (or multiples thereof).
*/
-static void
-json_writer_output_check (void *userdata, size_t needed)
-{
+static void json_writer_output_check(void *userdata, size_t needed) {
json_writer_userdata *state = userdata;
- if (state->free_space >= needed)
- return;
+ if (state->free_space >= needed) return;
needed -= state->free_space;
/* Round up by 256 bytes. */
needed = (needed + 0xff) & ~0xffU;
- state->output = gpr_realloc (state->output, state->allocated + needed);
+ state->output = gpr_realloc(state->output, state->allocated + needed);
state->free_space += needed;
state->allocated += needed;
}
/* These are needed by the writer's implementation. */
-static void
-json_writer_output_char (void *userdata, char c)
-{
+static void json_writer_output_char(void *userdata, char c) {
json_writer_userdata *state = userdata;
- json_writer_output_check (userdata, 1);
+ json_writer_output_check(userdata, 1);
state->output[state->string_len++] = c;
state->free_space--;
}
-static void
-json_writer_output_string_with_len (void *userdata, const char *str, size_t len)
-{
+static void json_writer_output_string_with_len(void *userdata, const char *str,
+ size_t len) {
json_writer_userdata *state = userdata;
- json_writer_output_check (userdata, len);
- memcpy (state->output + state->string_len, str, len);
+ json_writer_output_check(userdata, len);
+ memcpy(state->output + state->string_len, str, len);
state->string_len += len;
state->free_space -= len;
}
-static void
-json_writer_output_string (void *userdata, const char *str)
-{
- size_t len = strlen (str);
- json_writer_output_string_with_len (userdata, str, len);
+static void json_writer_output_string(void *userdata, const char *str) {
+ size_t len = strlen(str);
+ json_writer_output_string_with_len(userdata, str, len);
}
/* The reader asks us to clear our scratchpad. In our case, we'll simply mark
* the end of the current string, and advance our output pointer.
*/
-static void
-json_reader_string_clear (void *userdata)
-{
+static void json_reader_string_clear(void *userdata) {
json_reader_userdata *state = userdata;
- if (state->string)
- {
- GPR_ASSERT (state->string_ptr < state->input);
- *state->string_ptr++ = 0;
- }
+ if (state->string) {
+ GPR_ASSERT(state->string_ptr < state->input);
+ *state->string_ptr++ = 0;
+ }
state->string = state->string_ptr;
}
-static void
-json_reader_string_add_char (void *userdata, gpr_uint32 c)
-{
+static void json_reader_string_add_char(void *userdata, gpr_uint32 c) {
json_reader_userdata *state = userdata;
- GPR_ASSERT (state->string_ptr < state->input);
- GPR_ASSERT (c <= 0xff);
- *state->string_ptr++ = (gpr_uint8) c;
+ GPR_ASSERT(state->string_ptr < state->input);
+ GPR_ASSERT(c <= 0xff);
+ *state->string_ptr++ = (gpr_uint8)c;
}
/* We are converting a UTF-32 character into UTF-8 here,
* as described by RFC3629.
*/
-static void
-json_reader_string_add_utf32 (void *userdata, gpr_uint32 c)
-{
- if (c <= 0x7f)
- {
- json_reader_string_add_char (userdata, c);
- }
- else if (c <= 0x7ff)
- {
- gpr_uint32 b1 = 0xc0 | ((c >> 6) & 0x1f);
- gpr_uint32 b2 = 0x80 | (c & 0x3f);
- json_reader_string_add_char (userdata, b1);
- json_reader_string_add_char (userdata, b2);
- }
- else if (c <= 0xffff)
- {
- gpr_uint32 b1 = 0xe0 | ((c >> 12) & 0x0f);
- gpr_uint32 b2 = 0x80 | ((c >> 6) & 0x3f);
- gpr_uint32 b3 = 0x80 | (c & 0x3f);
- json_reader_string_add_char (userdata, b1);
- json_reader_string_add_char (userdata, b2);
- json_reader_string_add_char (userdata, b3);
- }
- else if (c <= 0x1fffff)
- {
- gpr_uint32 b1 = 0xf0 | ((c >> 18) & 0x07);
- gpr_uint32 b2 = 0x80 | ((c >> 12) & 0x3f);
- gpr_uint32 b3 = 0x80 | ((c >> 6) & 0x3f);
- gpr_uint32 b4 = 0x80 | (c & 0x3f);
- json_reader_string_add_char (userdata, b1);
- json_reader_string_add_char (userdata, b2);
- json_reader_string_add_char (userdata, b3);
- json_reader_string_add_char (userdata, b4);
- }
+static void json_reader_string_add_utf32(void *userdata, gpr_uint32 c) {
+ if (c <= 0x7f) {
+ json_reader_string_add_char(userdata, c);
+ } else if (c <= 0x7ff) {
+ gpr_uint32 b1 = 0xc0 | ((c >> 6) & 0x1f);
+ gpr_uint32 b2 = 0x80 | (c & 0x3f);
+ json_reader_string_add_char(userdata, b1);
+ json_reader_string_add_char(userdata, b2);
+ } else if (c <= 0xffff) {
+ gpr_uint32 b1 = 0xe0 | ((c >> 12) & 0x0f);
+ gpr_uint32 b2 = 0x80 | ((c >> 6) & 0x3f);
+ gpr_uint32 b3 = 0x80 | (c & 0x3f);
+ json_reader_string_add_char(userdata, b1);
+ json_reader_string_add_char(userdata, b2);
+ json_reader_string_add_char(userdata, b3);
+ } else if (c <= 0x1fffff) {
+ gpr_uint32 b1 = 0xf0 | ((c >> 18) & 0x07);
+ gpr_uint32 b2 = 0x80 | ((c >> 12) & 0x3f);
+ gpr_uint32 b3 = 0x80 | ((c >> 6) & 0x3f);
+ gpr_uint32 b4 = 0x80 | (c & 0x3f);
+ json_reader_string_add_char(userdata, b1);
+ json_reader_string_add_char(userdata, b2);
+ json_reader_string_add_char(userdata, b3);
+ json_reader_string_add_char(userdata, b4);
+ }
}
/* We consider that the input may be a zero-terminated string. So we
* can end up hitting eof before the end of the alleged string length.
*/
-static gpr_uint32
-json_reader_read_char (void *userdata)
-{
+static gpr_uint32 json_reader_read_char(void *userdata) {
gpr_uint32 r;
json_reader_userdata *state = userdata;
- if (state->remaining_input == 0)
- return GRPC_JSON_READ_CHAR_EOF;
+ if (state->remaining_input == 0) return GRPC_JSON_READ_CHAR_EOF;
r = *state->input++;
state->remaining_input--;
- if (r == 0)
- {
- state->remaining_input = 0;
- return GRPC_JSON_READ_CHAR_EOF;
- }
+ if (r == 0) {
+ state->remaining_input = 0;
+ return GRPC_JSON_READ_CHAR_EOF;
+ }
return r;
}
@@ -210,48 +182,39 @@ json_reader_read_char (void *userdata)
/* Helper function to create a new grpc_json object and link it into
* our tree-in-progress inside our opaque structure.
*/
-static grpc_json *
-json_create_and_link (void *userdata, grpc_json_type type)
-{
+static grpc_json *json_create_and_link(void *userdata, grpc_json_type type) {
json_reader_userdata *state = userdata;
- grpc_json *json = grpc_json_create (type);
+ grpc_json *json = grpc_json_create(type);
json->parent = state->current_container;
json->prev = state->current_value;
state->current_value = json;
- if (json->prev)
- {
- json->prev->next = json;
- }
- if (json->parent)
- {
- if (!json->parent->child)
- {
- json->parent->child = json;
- }
- if (json->parent->type == GRPC_JSON_OBJECT)
- {
- json->key = (char *) state->key;
- }
+ if (json->prev) {
+ json->prev->next = json;
+ }
+ if (json->parent) {
+ if (!json->parent->child) {
+ json->parent->child = json;
}
- if (!state->top)
- {
- state->top = json;
+ if (json->parent->type == GRPC_JSON_OBJECT) {
+ json->key = (char *)state->key;
}
+ }
+ if (!state->top) {
+ state->top = json;
+ }
return json;
}
-static void
-json_reader_container_begins (void *userdata, grpc_json_type type)
-{
+static void json_reader_container_begins(void *userdata, grpc_json_type type) {
json_reader_userdata *state = userdata;
grpc_json *container;
- GPR_ASSERT (type == GRPC_JSON_ARRAY || type == GRPC_JSON_OBJECT);
+ GPR_ASSERT(type == GRPC_JSON_ARRAY || type == GRPC_JSON_OBJECT);
- container = json_create_and_link (userdata, type);
+ container = json_create_and_link(userdata, type);
state->current_container = container;
state->current_value = NULL;
}
@@ -265,21 +228,18 @@ json_reader_container_begins (void *userdata, grpc_json_type type)
* Also note that if we're at the top of the tree, and the last container
* ends, we have to return GRPC_JSON_TOP_LEVEL.
*/
-static grpc_json_type
-json_reader_container_ends (void *userdata)
-{
+static grpc_json_type json_reader_container_ends(void *userdata) {
grpc_json_type container_type = GRPC_JSON_TOP_LEVEL;
json_reader_userdata *state = userdata;
- GPR_ASSERT (state->current_container);
+ GPR_ASSERT(state->current_container);
state->current_value = state->current_container;
state->current_container = state->current_container->parent;
- if (state->current_container)
- {
- container_type = state->current_container->type;
- }
+ if (state->current_container) {
+ container_type = state->current_container->type;
+ }
return container_type;
}
@@ -290,155 +250,130 @@ json_reader_container_ends (void *userdata)
* Note that in the set_number case, we're not going to try interpreting it.
* We'll keep it as a string, and leave it to the caller to evaluate it.
*/
-static void
-json_reader_set_key (void *userdata)
-{
+static void json_reader_set_key(void *userdata) {
json_reader_userdata *state = userdata;
state->key = state->string;
}
-static void
-json_reader_set_string (void *userdata)
-{
+static void json_reader_set_string(void *userdata) {
json_reader_userdata *state = userdata;
- grpc_json *json = json_create_and_link (userdata, GRPC_JSON_STRING);
- json->value = (char *) state->string;
+ grpc_json *json = json_create_and_link(userdata, GRPC_JSON_STRING);
+ json->value = (char *)state->string;
}
-static int
-json_reader_set_number (void *userdata)
-{
+static int json_reader_set_number(void *userdata) {
json_reader_userdata *state = userdata;
- grpc_json *json = json_create_and_link (userdata, GRPC_JSON_NUMBER);
- json->value = (char *) state->string;
+ grpc_json *json = json_create_and_link(userdata, GRPC_JSON_NUMBER);
+ json->value = (char *)state->string;
return 1;
}
/* The object types true, false and null are self-sufficient, and don't need
* any more information beside their type.
*/
-static void
-json_reader_set_true (void *userdata)
-{
- json_create_and_link (userdata, GRPC_JSON_TRUE);
+static void json_reader_set_true(void *userdata) {
+ json_create_and_link(userdata, GRPC_JSON_TRUE);
}
-static void
-json_reader_set_false (void *userdata)
-{
- json_create_and_link (userdata, GRPC_JSON_FALSE);
+static void json_reader_set_false(void *userdata) {
+ json_create_and_link(userdata, GRPC_JSON_FALSE);
}
-static void
-json_reader_set_null (void *userdata)
-{
- json_create_and_link (userdata, GRPC_JSON_NULL);
+static void json_reader_set_null(void *userdata) {
+ json_create_and_link(userdata, GRPC_JSON_NULL);
}
static grpc_json_reader_vtable reader_vtable = {
- json_reader_string_clear, json_reader_string_add_char,
- json_reader_string_add_utf32, json_reader_read_char,
- json_reader_container_begins, json_reader_container_ends,
- json_reader_set_key, json_reader_set_string,
- json_reader_set_number, json_reader_set_true,
- json_reader_set_false, json_reader_set_null
-};
+ json_reader_string_clear, json_reader_string_add_char,
+ json_reader_string_add_utf32, json_reader_read_char,
+ json_reader_container_begins, json_reader_container_ends,
+ json_reader_set_key, json_reader_set_string,
+ json_reader_set_number, json_reader_set_true,
+ json_reader_set_false, json_reader_set_null};
/* And finally, let's define our public API. */
-grpc_json *
-grpc_json_parse_string_with_len (char *input, size_t size)
-{
+grpc_json *grpc_json_parse_string_with_len(char *input, size_t size) {
grpc_json_reader reader;
json_reader_userdata state;
grpc_json *json = NULL;
grpc_json_reader_status status;
- if (!input)
- return NULL;
+ if (!input) return NULL;
state.top = state.current_container = state.current_value = NULL;
state.string = state.key = NULL;
- state.string_ptr = state.input = (gpr_uint8 *) input;
+ state.string_ptr = state.input = (gpr_uint8 *)input;
state.remaining_input = size;
- grpc_json_reader_init (&reader, &reader_vtable, &state);
+ grpc_json_reader_init(&reader, &reader_vtable, &state);
- status = grpc_json_reader_run (&reader);
+ status = grpc_json_reader_run(&reader);
json = state.top;
- if ((status != GRPC_JSON_DONE) && json)
- {
- grpc_json_destroy (json);
- json = NULL;
- }
+ if ((status != GRPC_JSON_DONE) && json) {
+ grpc_json_destroy(json);
+ json = NULL;
+ }
return json;
}
#define UNBOUND_JSON_STRING_LENGTH 0x7fffffff
-grpc_json *
-grpc_json_parse_string (char *input)
-{
- return grpc_json_parse_string_with_len (input, UNBOUND_JSON_STRING_LENGTH);
+grpc_json *grpc_json_parse_string(char *input) {
+ return grpc_json_parse_string_with_len(input, UNBOUND_JSON_STRING_LENGTH);
}
-static void
-json_dump_recursive (grpc_json_writer * writer, grpc_json * json, int in_object)
-{
- while (json)
- {
- if (in_object)
- grpc_json_writer_object_key (writer, json->key);
-
- switch (json->type)
- {
- case GRPC_JSON_OBJECT:
- case GRPC_JSON_ARRAY:
- grpc_json_writer_container_begins (writer, json->type);
- if (json->child)
- json_dump_recursive (writer, json->child, json->type == GRPC_JSON_OBJECT);
- grpc_json_writer_container_ends (writer, json->type);
- break;
- case GRPC_JSON_STRING:
- grpc_json_writer_value_string (writer, json->value);
- break;
- case GRPC_JSON_NUMBER:
- grpc_json_writer_value_raw (writer, json->value);
- break;
- case GRPC_JSON_TRUE:
- grpc_json_writer_value_raw_with_len (writer, "true", 4);
- break;
- case GRPC_JSON_FALSE:
- grpc_json_writer_value_raw_with_len (writer, "false", 5);
- break;
- case GRPC_JSON_NULL:
- grpc_json_writer_value_raw_with_len (writer, "null", 4);
- break;
- default:
- abort ();
- }
- json = json->next;
+static void json_dump_recursive(grpc_json_writer *writer, grpc_json *json,
+ int in_object) {
+ while (json) {
+ if (in_object) grpc_json_writer_object_key(writer, json->key);
+
+ switch (json->type) {
+ case GRPC_JSON_OBJECT:
+ case GRPC_JSON_ARRAY:
+ grpc_json_writer_container_begins(writer, json->type);
+ if (json->child)
+ json_dump_recursive(writer, json->child,
+ json->type == GRPC_JSON_OBJECT);
+ grpc_json_writer_container_ends(writer, json->type);
+ break;
+ case GRPC_JSON_STRING:
+ grpc_json_writer_value_string(writer, json->value);
+ break;
+ case GRPC_JSON_NUMBER:
+ grpc_json_writer_value_raw(writer, json->value);
+ break;
+ case GRPC_JSON_TRUE:
+ grpc_json_writer_value_raw_with_len(writer, "true", 4);
+ break;
+ case GRPC_JSON_FALSE:
+ grpc_json_writer_value_raw_with_len(writer, "false", 5);
+ break;
+ case GRPC_JSON_NULL:
+ grpc_json_writer_value_raw_with_len(writer, "null", 4);
+ break;
+ default:
+ abort();
}
+ json = json->next;
+ }
}
static grpc_json_writer_vtable writer_vtable = {
- json_writer_output_char, json_writer_output_string,
- json_writer_output_string_with_len
-};
+ json_writer_output_char, json_writer_output_string,
+ json_writer_output_string_with_len};
-char *
-grpc_json_dump_to_string (grpc_json * json, int indent)
-{
+char *grpc_json_dump_to_string(grpc_json *json, int indent) {
grpc_json_writer writer;
json_writer_userdata state;
state.output = NULL;
state.free_space = state.string_len = state.allocated = 0;
- grpc_json_writer_init (&writer, indent, &writer_vtable, &state);
+ grpc_json_writer_init(&writer, indent, &writer_vtable, &state);
- json_dump_recursive (&writer, json, 0);
+ json_dump_recursive(&writer, json, 0);
- json_writer_output_char (&state, 0);
+ json_writer_output_char(&state, 0);
return state.output;
}
diff --git a/src/core/json/json_writer.c b/src/core/json/json_writer.c
index fc49d72998..e0d02f411e 100644
--- a/src/core/json/json_writer.c
+++ b/src/core/json/json_writer.c
@@ -37,273 +37,224 @@
#include "src/core/json/json_writer.h"
-static void
-json_writer_output_char (grpc_json_writer * writer, char c)
-{
- writer->vtable->output_char (writer->userdata, c);
+static void json_writer_output_char(grpc_json_writer *writer, char c) {
+ writer->vtable->output_char(writer->userdata, c);
}
-static void
-json_writer_output_string (grpc_json_writer * writer, const char *str)
-{
- writer->vtable->output_string (writer->userdata, str);
+static void json_writer_output_string(grpc_json_writer *writer,
+ const char *str) {
+ writer->vtable->output_string(writer->userdata, str);
}
-static void
-json_writer_output_string_with_len (grpc_json_writer * writer, const char *str, size_t len)
-{
- writer->vtable->output_string_with_len (writer->userdata, str, len);
+static void json_writer_output_string_with_len(grpc_json_writer *writer,
+ const char *str, size_t len) {
+ writer->vtable->output_string_with_len(writer->userdata, str, len);
}
-void
-grpc_json_writer_init (grpc_json_writer * writer, int indent, grpc_json_writer_vtable * vtable, void *userdata)
-{
- memset (writer, 0, sizeof (*writer));
+void grpc_json_writer_init(grpc_json_writer *writer, int indent,
+ grpc_json_writer_vtable *vtable, void *userdata) {
+ memset(writer, 0, sizeof(*writer));
writer->container_empty = 1;
writer->indent = indent;
writer->vtable = vtable;
writer->userdata = userdata;
}
-static void
-json_writer_output_indent (grpc_json_writer * writer)
-{
- static const char spacesstr[] = " " " " " " " ";
+static void json_writer_output_indent(grpc_json_writer *writer) {
+ static const char spacesstr[] =
+ " "
+ " "
+ " "
+ " ";
- unsigned spaces = (unsigned) (writer->depth * writer->indent);
+ unsigned spaces = (unsigned)(writer->depth * writer->indent);
- if (writer->indent == 0)
+ if (writer->indent == 0) return;
+
+ if (writer->got_key) {
+ json_writer_output_char(writer, ' ');
return;
+ }
- if (writer->got_key)
- {
- json_writer_output_char (writer, ' ');
- return;
- }
+ while (spaces >= (sizeof(spacesstr) - 1)) {
+ json_writer_output_string_with_len(writer, spacesstr,
+ sizeof(spacesstr) - 1);
+ spaces -= (unsigned)(sizeof(spacesstr) - 1);
+ }
- while (spaces >= (sizeof (spacesstr) - 1))
- {
- json_writer_output_string_with_len (writer, spacesstr, sizeof (spacesstr) - 1);
- spaces -= (unsigned) (sizeof (spacesstr) - 1);
- }
+ if (spaces == 0) return;
- if (spaces == 0)
- return;
-
- json_writer_output_string_with_len (writer, spacesstr + sizeof (spacesstr) - 1 - spaces, spaces);
+ json_writer_output_string_with_len(
+ writer, spacesstr + sizeof(spacesstr) - 1 - spaces, spaces);
}
-static void
-json_writer_value_end (grpc_json_writer * writer)
-{
- if (writer->container_empty)
- {
- writer->container_empty = 0;
- if ((writer->indent == 0) || (writer->depth == 0))
- return;
- json_writer_output_char (writer, '\n');
- }
- else
- {
- json_writer_output_char (writer, ',');
- if (writer->indent == 0)
- return;
- json_writer_output_char (writer, '\n');
- }
+static void json_writer_value_end(grpc_json_writer *writer) {
+ if (writer->container_empty) {
+ writer->container_empty = 0;
+ if ((writer->indent == 0) || (writer->depth == 0)) return;
+ json_writer_output_char(writer, '\n');
+ } else {
+ json_writer_output_char(writer, ',');
+ if (writer->indent == 0) return;
+ json_writer_output_char(writer, '\n');
+ }
}
-static void
-json_writer_escape_utf16 (grpc_json_writer * writer, gpr_uint16 utf16)
-{
+static void json_writer_escape_utf16(grpc_json_writer *writer,
+ gpr_uint16 utf16) {
static const char hex[] = "0123456789abcdef";
- json_writer_output_string_with_len (writer, "\\u", 2);
- json_writer_output_char (writer, hex[(utf16 >> 12) & 0x0f]);
- json_writer_output_char (writer, hex[(utf16 >> 8) & 0x0f]);
- json_writer_output_char (writer, hex[(utf16 >> 4) & 0x0f]);
- json_writer_output_char (writer, hex[(utf16) & 0x0f]);
+ json_writer_output_string_with_len(writer, "\\u", 2);
+ json_writer_output_char(writer, hex[(utf16 >> 12) & 0x0f]);
+ json_writer_output_char(writer, hex[(utf16 >> 8) & 0x0f]);
+ json_writer_output_char(writer, hex[(utf16 >> 4) & 0x0f]);
+ json_writer_output_char(writer, hex[(utf16)&0x0f]);
}
-static void
-json_writer_escape_string (grpc_json_writer * writer, const char *string)
-{
- json_writer_output_char (writer, '"');
+static void json_writer_escape_string(grpc_json_writer *writer,
+ const char *string) {
+ json_writer_output_char(writer, '"');
- for (;;)
- {
- gpr_uint8 c = (gpr_uint8) * string++;
- if (c == 0)
- {
- break;
- }
- else if ((c >= 32) && (c <= 126))
- {
- if ((c == '\\') || (c == '"'))
- json_writer_output_char (writer, '\\');
- json_writer_output_char (writer, (char) c);
- }
- else if ((c < 32) || (c == 127))
- {
- switch (c)
- {
- case '\b':
- json_writer_output_string_with_len (writer, "\\b", 2);
- break;
- case '\f':
- json_writer_output_string_with_len (writer, "\\f", 2);
- break;
- case '\n':
- json_writer_output_string_with_len (writer, "\\n", 2);
- break;
- case '\r':
- json_writer_output_string_with_len (writer, "\\r", 2);
- break;
- case '\t':
- json_writer_output_string_with_len (writer, "\\t", 2);
- break;
- default:
- json_writer_escape_utf16 (writer, c);
- break;
- }
- }
- else
- {
- gpr_uint32 utf32 = 0;
- int extra = 0;
- int i;
- int valid = 1;
- if ((c & 0xe0) == 0xc0)
- {
- utf32 = c & 0x1f;
- extra = 1;
- }
- else if ((c & 0xf0) == 0xe0)
- {
- utf32 = c & 0x0f;
- extra = 2;
- }
- else if ((c & 0xf8) == 0xf0)
- {
- utf32 = c & 0x07;
- extra = 3;
- }
- else
- {
- break;
- }
- for (i = 0; i < extra; i++)
- {
- utf32 <<= 6;
- c = (gpr_uint8) (*string++);
- /* Breaks out and bail on any invalid UTF-8 sequence, including \0. */
- if ((c & 0xc0) != 0x80)
- {
- valid = 0;
- break;
- }
- utf32 |= c & 0x3f;
- }
- if (!valid)
- break;
- /* The range 0xd800 - 0xdfff is reserved by the surrogates ad vitam.
- * Any other range is technically reserved for future usage, so if we
- * don't want the software to break in the future, we have to allow
- * anything else. The first non-unicode character is 0x110000. */
- if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) || (utf32 >= 0x110000))
- break;
- if (utf32 >= 0x10000)
- {
- /* If utf32 contains a character that is above 0xffff, it needs to be
- * broken down into a utf-16 surrogate pair. A surrogate pair is first
- * a high surrogate, followed by a low surrogate. Each surrogate holds
- * 10 bits of usable data, thus allowing a total of 20 bits of data.
- * The high surrogate marker is 0xd800, while the low surrogate marker
- * is 0xdc00. The low 10 bits of each will be the usable data.
- *
- * After re-combining the 20 bits of data, one has to add 0x10000 to
- * the resulting value, in order to obtain the original character.
- * This is obviously because the range 0x0000 - 0xffff can be written
- * without any special trick.
- *
- * Since 0x10ffff is the highest allowed character, we're working in
- * the range 0x00000 - 0xfffff after we decrement it by 0x10000.
- * That range is exactly 20 bits.
- */
- utf32 -= 0x10000;
- json_writer_escape_utf16 (writer, (gpr_uint16) (0xd800 | (utf32 >> 10)));
- json_writer_escape_utf16 (writer, (gpr_uint16) (0xdc00 | (utf32 & 0x3ff)));
- }
- else
- {
- json_writer_escape_utf16 (writer, (gpr_uint16) utf32);
- }
- }
+ for (;;) {
+ gpr_uint8 c = (gpr_uint8)*string++;
+ if (c == 0) {
+ break;
+ } else if ((c >= 32) && (c <= 126)) {
+ if ((c == '\\') || (c == '"')) json_writer_output_char(writer, '\\');
+ json_writer_output_char(writer, (char)c);
+ } else if ((c < 32) || (c == 127)) {
+ switch (c) {
+ case '\b':
+ json_writer_output_string_with_len(writer, "\\b", 2);
+ break;
+ case '\f':
+ json_writer_output_string_with_len(writer, "\\f", 2);
+ break;
+ case '\n':
+ json_writer_output_string_with_len(writer, "\\n", 2);
+ break;
+ case '\r':
+ json_writer_output_string_with_len(writer, "\\r", 2);
+ break;
+ case '\t':
+ json_writer_output_string_with_len(writer, "\\t", 2);
+ break;
+ default:
+ json_writer_escape_utf16(writer, c);
+ break;
+ }
+ } else {
+ gpr_uint32 utf32 = 0;
+ int extra = 0;
+ int i;
+ int valid = 1;
+ if ((c & 0xe0) == 0xc0) {
+ utf32 = c & 0x1f;
+ extra = 1;
+ } else if ((c & 0xf0) == 0xe0) {
+ utf32 = c & 0x0f;
+ extra = 2;
+ } else if ((c & 0xf8) == 0xf0) {
+ utf32 = c & 0x07;
+ extra = 3;
+ } else {
+ break;
+ }
+ for (i = 0; i < extra; i++) {
+ utf32 <<= 6;
+ c = (gpr_uint8)(*string++);
+ /* Breaks out and bail on any invalid UTF-8 sequence, including \0. */
+ if ((c & 0xc0) != 0x80) {
+ valid = 0;
+ break;
+ }
+ utf32 |= c & 0x3f;
+ }
+ if (!valid) break;
+ /* The range 0xd800 - 0xdfff is reserved by the surrogates ad vitam.
+ * Any other range is technically reserved for future usage, so if we
+ * don't want the software to break in the future, we have to allow
+ * anything else. The first non-unicode character is 0x110000. */
+ if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) || (utf32 >= 0x110000))
+ break;
+ if (utf32 >= 0x10000) {
+ /* If utf32 contains a character that is above 0xffff, it needs to be
+ * broken down into a utf-16 surrogate pair. A surrogate pair is first
+ * a high surrogate, followed by a low surrogate. Each surrogate holds
+ * 10 bits of usable data, thus allowing a total of 20 bits of data.
+ * The high surrogate marker is 0xd800, while the low surrogate marker
+ * is 0xdc00. The low 10 bits of each will be the usable data.
+ *
+ * After re-combining the 20 bits of data, one has to add 0x10000 to
+ * the resulting value, in order to obtain the original character.
+ * This is obviously because the range 0x0000 - 0xffff can be written
+ * without any special trick.
+ *
+ * Since 0x10ffff is the highest allowed character, we're working in
+ * the range 0x00000 - 0xfffff after we decrement it by 0x10000.
+ * That range is exactly 20 bits.
+ */
+ utf32 -= 0x10000;
+ json_writer_escape_utf16(writer, (gpr_uint16)(0xd800 | (utf32 >> 10)));
+ json_writer_escape_utf16(writer,
+ (gpr_uint16)(0xdc00 | (utf32 & 0x3ff)));
+ } else {
+ json_writer_escape_utf16(writer, (gpr_uint16)utf32);
+ }
}
+ }
- json_writer_output_char (writer, '"');
+ json_writer_output_char(writer, '"');
}
-void
-grpc_json_writer_container_begins (grpc_json_writer * writer, grpc_json_type type)
-{
- if (!writer->got_key)
- json_writer_value_end (writer);
- json_writer_output_indent (writer);
- json_writer_output_char (writer, type == GRPC_JSON_OBJECT ? '{' : '[');
+void grpc_json_writer_container_begins(grpc_json_writer *writer,
+ grpc_json_type type) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_output_char(writer, type == GRPC_JSON_OBJECT ? '{' : '[');
writer->container_empty = 1;
writer->got_key = 0;
writer->depth++;
}
-void
-grpc_json_writer_container_ends (grpc_json_writer * writer, grpc_json_type type)
-{
+void grpc_json_writer_container_ends(grpc_json_writer *writer,
+ grpc_json_type type) {
if (writer->indent && !writer->container_empty)
- json_writer_output_char (writer, '\n');
+ json_writer_output_char(writer, '\n');
writer->depth--;
- if (!writer->container_empty)
- json_writer_output_indent (writer);
- json_writer_output_char (writer, type == GRPC_JSON_OBJECT ? '}' : ']');
+ if (!writer->container_empty) json_writer_output_indent(writer);
+ json_writer_output_char(writer, type == GRPC_JSON_OBJECT ? '}' : ']');
writer->container_empty = 0;
writer->got_key = 0;
}
-void
-grpc_json_writer_object_key (grpc_json_writer * writer, const char *string)
-{
- json_writer_value_end (writer);
- json_writer_output_indent (writer);
- json_writer_escape_string (writer, string);
- json_writer_output_char (writer, ':');
+void grpc_json_writer_object_key(grpc_json_writer *writer, const char *string) {
+ json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_escape_string(writer, string);
+ json_writer_output_char(writer, ':');
writer->got_key = 1;
}
-void
-grpc_json_writer_value_raw (grpc_json_writer * writer, const char *string)
-{
- if (!writer->got_key)
- json_writer_value_end (writer);
- json_writer_output_indent (writer);
- json_writer_output_string (writer, string);
+void grpc_json_writer_value_raw(grpc_json_writer *writer, const char *string) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_output_string(writer, string);
writer->got_key = 0;
}
-void
-grpc_json_writer_value_raw_with_len (grpc_json_writer * writer, const char *string, size_t len)
-{
- if (!writer->got_key)
- json_writer_value_end (writer);
- json_writer_output_indent (writer);
- json_writer_output_string_with_len (writer, string, len);
+void grpc_json_writer_value_raw_with_len(grpc_json_writer *writer,
+ const char *string, size_t len) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_output_string_with_len(writer, string, len);
writer->got_key = 0;
}
-void
-grpc_json_writer_value_string (grpc_json_writer * writer, const char *string)
-{
- if (!writer->got_key)
- json_writer_value_end (writer);
- json_writer_output_indent (writer);
- json_writer_escape_string (writer, string);
+void grpc_json_writer_value_string(grpc_json_writer *writer,
+ const char *string) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_escape_string(writer, string);
writer->got_key = 0;
}
diff --git a/src/core/json/json_writer.h b/src/core/json/json_writer.h
index d7dfe2846f..9ef04aab01 100644
--- a/src/core/json/json_writer.h
+++ b/src/core/json/json_writer.h
@@ -50,19 +50,17 @@
#include "src/core/json/json_common.h"
-typedef struct grpc_json_writer_vtable
-{
+typedef struct grpc_json_writer_vtable {
/* Adds a character to the output stream. */
- void (*output_char) (void *userdata, char);
+ void (*output_char)(void *userdata, char);
/* Adds a zero-terminated string to the output stream. */
- void (*output_string) (void *userdata, const char *str);
+ void (*output_string)(void *userdata, const char *str);
/* Adds a fixed-length string to the output stream. */
- void (*output_string_with_len) (void *userdata, const char *str, size_t len);
+ void (*output_string_with_len)(void *userdata, const char *str, size_t len);
} grpc_json_writer_vtable;
-typedef struct grpc_json_writer
-{
+typedef struct grpc_json_writer {
void *userdata;
grpc_json_writer_vtable *vtable;
int indent;
@@ -76,19 +74,24 @@ typedef struct grpc_json_writer
* use indent=0, then the output will not have any newlines either, thus
* emitting a condensed json output.
*/
-void grpc_json_writer_init (grpc_json_writer * writer, int indent, grpc_json_writer_vtable * vtable, void *userdata);
+void grpc_json_writer_init(grpc_json_writer *writer, int indent,
+ grpc_json_writer_vtable *vtable, void *userdata);
/* Signals the beginning of a container. */
-void grpc_json_writer_container_begins (grpc_json_writer * writer, grpc_json_type type);
+void grpc_json_writer_container_begins(grpc_json_writer *writer,
+ grpc_json_type type);
/* Signals the end of a container. */
-void grpc_json_writer_container_ends (grpc_json_writer * writer, grpc_json_type type);
+void grpc_json_writer_container_ends(grpc_json_writer *writer,
+ grpc_json_type type);
/* Writes down an object key for the next value. */
-void grpc_json_writer_object_key (grpc_json_writer * writer, const char *string);
+void grpc_json_writer_object_key(grpc_json_writer *writer, const char *string);
/* Sets a raw value. Useful for numbers. */
-void grpc_json_writer_value_raw (grpc_json_writer * writer, const char *string);
+void grpc_json_writer_value_raw(grpc_json_writer *writer, const char *string);
/* Sets a raw value with its length. Useful for values like true or false. */
-void grpc_json_writer_value_raw_with_len (grpc_json_writer * writer, const char *string, size_t len);
+void grpc_json_writer_value_raw_with_len(grpc_json_writer *writer,
+ const char *string, size_t len);
/* Sets a string value. It'll be escaped, and utf-8 validated. */
-void grpc_json_writer_value_string (grpc_json_writer * writer, const char *string);
+void grpc_json_writer_value_string(grpc_json_writer *writer,
+ const char *string);
#endif /* GRPC_INTERNAL_CORE_JSON_JSON_WRITER_H */
diff --git a/src/core/profiling/basic_timers.c b/src/core/profiling/basic_timers.c
index d4506857b1..2f6c88daac 100644
--- a/src/core/profiling/basic_timers.c
+++ b/src/core/profiling/basic_timers.c
@@ -44,16 +44,14 @@
#include <grpc/support/thd.h>
#include <stdio.h>
-typedef enum
-{
+typedef enum {
BEGIN = '{',
END = '}',
MARK = '.',
IMPORTANT = '!'
} marker_type;
-typedef struct grpc_timer_entry
-{
+typedef struct grpc_timer_entry {
gpr_timespec tm;
int tag;
const char *tagstr;
@@ -68,34 +66,32 @@ typedef struct grpc_timer_entry
static __thread grpc_timer_entry log[MAX_COUNT];
static __thread int count;
-static void
-log_report ()
-{
+static void log_report() {
int i;
- for (i = 0; i < count; i++)
- {
- grpc_timer_entry *entry = &(log[i]);
- printf ("GRPC_LAT_PROF %ld.%09d %p %c %d(%s) %p %s %d\n", entry->tm.tv_sec, entry->tm.tv_nsec, (void *) (gpr_intptr) gpr_thd_currentid (), entry->type, entry->tag, entry->tagstr, entry->id, entry->file, entry->line);
- }
+ for (i = 0; i < count; i++) {
+ grpc_timer_entry *entry = &(log[i]);
+ printf("GRPC_LAT_PROF %ld.%09d %p %c %d(%s) %p %s %d\n", entry->tm.tv_sec,
+ entry->tm.tv_nsec, (void *)(gpr_intptr)gpr_thd_currentid(),
+ entry->type, entry->tag, entry->tagstr, entry->id, entry->file,
+ entry->line);
+ }
/* Now clear out the log */
count = 0;
}
-static void
-grpc_timers_log_add (int tag, const char *tagstr, marker_type type, void *id, const char *file, int line)
-{
+static void grpc_timers_log_add(int tag, const char *tagstr, marker_type type,
+ void *id, const char *file, int line) {
grpc_timer_entry *entry;
/* TODO (vpai) : Improve concurrency */
- if (count == MAX_COUNT)
- {
- log_report ();
- }
+ if (count == MAX_COUNT) {
+ log_report();
+ }
entry = &log[count++];
- entry->tm = gpr_now (GPR_CLOCK_PRECISE);
+ entry->tm = gpr_now(GPR_CLOCK_PRECISE);
entry->tag = tag;
entry->tagstr = tagstr;
entry->type = type;
@@ -105,61 +101,41 @@ grpc_timers_log_add (int tag, const char *tagstr, marker_type type, void *id, co
}
/* Latency profiler API implementation. */
-void
-grpc_timer_add_mark (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- if (tag < GRPC_PTAG_IGNORE_THRESHOLD)
- {
- grpc_timers_log_add (tag, tagstr, MARK, id, file, line);
- }
+void grpc_timer_add_mark(int tag, const char *tagstr, void *id,
+ const char *file, int line) {
+ if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
+ grpc_timers_log_add(tag, tagstr, MARK, id, file, line);
+ }
}
-void
-grpc_timer_add_important_mark (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- if (tag < GRPC_PTAG_IGNORE_THRESHOLD)
- {
- grpc_timers_log_add (tag, tagstr, IMPORTANT, id, file, line);
- }
+void grpc_timer_add_important_mark(int tag, const char *tagstr, void *id,
+ const char *file, int line) {
+ if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
+ grpc_timers_log_add(tag, tagstr, IMPORTANT, id, file, line);
+ }
}
-void
-grpc_timer_begin (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- if (tag < GRPC_PTAG_IGNORE_THRESHOLD)
- {
- grpc_timers_log_add (tag, tagstr, BEGIN, id, file, line);
- }
+void grpc_timer_begin(int tag, const char *tagstr, void *id, const char *file,
+ int line) {
+ if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
+ grpc_timers_log_add(tag, tagstr, BEGIN, id, file, line);
+ }
}
-void
-grpc_timer_end (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- if (tag < GRPC_PTAG_IGNORE_THRESHOLD)
- {
- grpc_timers_log_add (tag, tagstr, END, id, file, line);
- }
+void grpc_timer_end(int tag, const char *tagstr, void *id, const char *file,
+ int line) {
+ if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
+ grpc_timers_log_add(tag, tagstr, END, id, file, line);
+ }
}
/* Basic profiler specific API functions. */
-void
-grpc_timers_global_init (void)
-{
-}
+void grpc_timers_global_init(void) {}
-void
-grpc_timers_global_destroy (void)
-{
-}
+void grpc_timers_global_destroy(void) {}
-#else /* !GRPC_BASIC_PROFILER */
-void
-grpc_timers_global_init (void)
-{
-}
+#else /* !GRPC_BASIC_PROFILER */
+void grpc_timers_global_init(void) {}
-void
-grpc_timers_global_destroy (void)
-{
-}
+void grpc_timers_global_destroy(void) {}
#endif /* GRPC_BASIC_PROFILER */
diff --git a/src/core/profiling/stap_timers.c b/src/core/profiling/stap_timers.c
index f284e32433..6868a674a9 100644
--- a/src/core/profiling/stap_timers.c
+++ b/src/core/profiling/stap_timers.c
@@ -42,28 +42,24 @@
#include "src/core/profiling/stap_probes.h"
/* Latency profiler API implementation. */
-void
-grpc_timer_add_mark (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- _STAP_ADD_MARK (tag);
+void grpc_timer_add_mark(int tag, const char *tagstr, void *id,
+ const char *file, int line) {
+ _STAP_ADD_MARK(tag);
}
-void
-grpc_timer_add_important_mark (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- _STAP_ADD_IMPORTANT_MARK (tag);
+void grpc_timer_add_important_mark(int tag, const char *tagstr, void *id,
+ const char *file, int line) {
+ _STAP_ADD_IMPORTANT_MARK(tag);
}
-void
-grpc_timer_begin (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- _STAP_TIMING_NS_BEGIN (tag);
+void grpc_timer_begin(int tag, const char *tagstr, void *id, const char *file,
+ int line) {
+ _STAP_TIMING_NS_BEGIN(tag);
}
-void
-grpc_timer_end (int tag, const char *tagstr, void *id, const char *file, int line)
-{
- _STAP_TIMING_NS_END (tag);
+void grpc_timer_end(int tag, const char *tagstr, void *id, const char *file,
+ int line) {
+ _STAP_TIMING_NS_END(tag);
}
#endif /* GRPC_STAP_PROFILER */
diff --git a/src/core/profiling/timers.h b/src/core/profiling/timers.h
index 34929d8217..a70520408c 100644
--- a/src/core/profiling/timers.h
+++ b/src/core/profiling/timers.h
@@ -35,49 +35,51 @@
#define GRPC_CORE_PROFILING_TIMERS_H
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
- void grpc_timers_global_init (void);
- void grpc_timers_global_destroy (void);
-
- void grpc_timer_add_mark (int tag, const char *tagstr, void *id, const char *file, int line);
- void grpc_timer_add_important_mark (int tag, const char *tagstr, void *id, const char *file, int line);
- void grpc_timer_begin (int tag, const char *tagstr, void *id, const char *file, int line);
- void grpc_timer_end (int tag, const char *tagstr, void *id, const char *file, int line);
-
- enum grpc_profiling_tags
- {
- /* Any GRPC_PTAG_* >= than the threshold won't generate any profiling mark. */
- GRPC_PTAG_IGNORE_THRESHOLD = 1000000,
-
- /* Re. Protos. */
- GRPC_PTAG_PROTO_SERIALIZE = 100 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_PROTO_DESERIALIZE = 101 + GRPC_PTAG_IGNORE_THRESHOLD,
-
- /* Re. sockets. */
- GRPC_PTAG_HANDLE_READ = 200 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_SENDMSG = 201 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_RECVMSG = 202 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_POLL_FINISHED = 203 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_TCP_CB_WRITE = 204 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_TCP_WRITE = 205 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_CALL_ON_DONE_RECV = 206 + GRPC_PTAG_IGNORE_THRESHOLD,
-
- /* C++ */
- GRPC_PTAG_CPP_CALL_CREATED = 300 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_CPP_PERFORM_OPS = 301 + GRPC_PTAG_IGNORE_THRESHOLD,
-
- /* Transports */
- GRPC_PTAG_HTTP2_UNLOCK = 401 + GRPC_PTAG_IGNORE_THRESHOLD,
- GRPC_PTAG_HTTP2_UNLOCK_CLEANUP = 402 + GRPC_PTAG_IGNORE_THRESHOLD,
-
- /* > 1024 Unassigned reserved. For any miscellaneous use.
- * Use addition to generate tags from this base or take advantage of the 10
- * zero'd bits for OR-ing. */
- GRPC_PTAG_OTHER_BASE = 1024
- };
+void grpc_timers_global_init(void);
+void grpc_timers_global_destroy(void);
+
+void grpc_timer_add_mark(int tag, const char *tagstr, void *id,
+ const char *file, int line);
+void grpc_timer_add_important_mark(int tag, const char *tagstr, void *id,
+ const char *file, int line);
+void grpc_timer_begin(int tag, const char *tagstr, void *id, const char *file,
+ int line);
+void grpc_timer_end(int tag, const char *tagstr, void *id, const char *file,
+ int line);
+
+enum grpc_profiling_tags {
+ /* Any GRPC_PTAG_* >= than the threshold won't generate any profiling mark. */
+ GRPC_PTAG_IGNORE_THRESHOLD = 1000000,
+
+ /* Re. Protos. */
+ GRPC_PTAG_PROTO_SERIALIZE = 100 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_PROTO_DESERIALIZE = 101 + GRPC_PTAG_IGNORE_THRESHOLD,
+
+ /* Re. sockets. */
+ GRPC_PTAG_HANDLE_READ = 200 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_SENDMSG = 201 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_RECVMSG = 202 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_POLL_FINISHED = 203 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_TCP_CB_WRITE = 204 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_TCP_WRITE = 205 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_CALL_ON_DONE_RECV = 206 + GRPC_PTAG_IGNORE_THRESHOLD,
+
+ /* C++ */
+ GRPC_PTAG_CPP_CALL_CREATED = 300 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_CPP_PERFORM_OPS = 301 + GRPC_PTAG_IGNORE_THRESHOLD,
+
+ /* Transports */
+ GRPC_PTAG_HTTP2_UNLOCK = 401 + GRPC_PTAG_IGNORE_THRESHOLD,
+ GRPC_PTAG_HTTP2_UNLOCK_CLEANUP = 402 + GRPC_PTAG_IGNORE_THRESHOLD,
+
+ /* > 1024 Unassigned reserved. For any miscellaneous use.
+ * Use addition to generate tags from this base or take advantage of the 10
+ * zero'd bits for OR-ing. */
+ GRPC_PTAG_OTHER_BASE = 1024
+};
#if !(defined(GRPC_STAP_PROFILER) + defined(GRPC_BASIC_PROFILER))
/* No profiling. No-op all the things. */
@@ -97,7 +99,7 @@ extern "C"
do { \
} while (0)
-#else /* at least one profiler requested... */
+#else /* at least one profiler requested... */
/* ... hopefully only one. */
#if defined(GRPC_STAP_PROFILER) && defined(GRPC_BASIC_PROFILER)
#error "GRPC_STAP_PROFILER and GRPC_BASIC_PROFILER are mutually exclusive."
@@ -129,16 +131,16 @@ extern "C"
#ifdef GRPC_STAP_PROFILER
/* Empty placeholder for now. */
-#endif /* GRPC_STAP_PROFILER */
+#endif /* GRPC_STAP_PROFILER */
#ifdef GRPC_BASIC_PROFILER
/* Empty placeholder for now. */
-#endif /* GRPC_BASIC_PROFILER */
+#endif /* GRPC_BASIC_PROFILER */
-#endif /* at least one profiler requested. */
+#endif /* at least one profiler requested. */
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_CORE_PROFILING_TIMERS_H */
+#endif /* GRPC_CORE_PROFILING_TIMERS_H */
diff --git a/src/core/security/base64.c b/src/core/security/base64.c
index 4d86da9453..5226d2c578 100644
--- a/src/core/security/base64.c
+++ b/src/core/security/base64.c
@@ -42,21 +42,22 @@
/* --- Constants. --- */
static const char base64_bytes[] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, 0x3E, -1, -1, -1, 0x3F,
- 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, -1, -1,
- -1, 0x7F, -1, -1, -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
- 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1,
- -1, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
- 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
- 0x31, 0x32, 0x33, -1, -1, -1, -1, -1
-};
-
-static const char base64_url_unsafe_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-static const char base64_url_safe_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, 0x3E, -1, -1, -1, 0x3F,
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, -1, -1,
+ -1, 0x7F, -1, -1, -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
+ 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1,
+ -1, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
+ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
+ 0x31, 0x32, 0x33, -1, -1, -1, -1, -1};
+
+static const char base64_url_unsafe_chars[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static const char base64_url_safe_chars[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
#define GRPC_BASE64_PAD_CHAR '='
#define GRPC_BASE64_PAD_BYTE 0x7F
@@ -65,194 +66,168 @@ static const char base64_url_safe_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi
/* --- base64 functions. --- */
-char *
-grpc_base64_encode (const void *vdata, size_t data_size, int url_safe, int multiline)
-{
+char *grpc_base64_encode(const void *vdata, size_t data_size, int url_safe,
+ int multiline) {
const unsigned char *data = vdata;
- const char *base64_chars = url_safe ? base64_url_safe_chars : base64_url_unsafe_chars;
- size_t result_projected_size = 4 * ((data_size + 3) / 3) + 2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS)) : 0) + 1;
- char *result = gpr_malloc (result_projected_size);
+ const char *base64_chars =
+ url_safe ? base64_url_safe_chars : base64_url_unsafe_chars;
+ size_t result_projected_size =
+ 4 * ((data_size + 3) / 3) +
+ 2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS))
+ : 0) +
+ 1;
+ char *result = gpr_malloc(result_projected_size);
char *current = result;
size_t num_blocks = 0;
size_t i = 0;
/* Encode each block. */
- while (data_size >= 3)
- {
- *current++ = base64_chars[(data[i] >> 2) & 0x3F];
- *current++ = base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
- *current++ = base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)];
- *current++ = base64_chars[data[i + 2] & 0x3F];
-
- data_size -= 3;
- i += 3;
- if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS))
- {
- *current++ = '\r';
- *current++ = '\n';
- num_blocks = 0;
- }
+ while (data_size >= 3) {
+ *current++ = base64_chars[(data[i] >> 2) & 0x3F];
+ *current++ =
+ base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
+ *current++ =
+ base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)];
+ *current++ = base64_chars[data[i + 2] & 0x3F];
+
+ data_size -= 3;
+ i += 3;
+ if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) {
+ *current++ = '\r';
+ *current++ = '\n';
+ num_blocks = 0;
}
+ }
/* Take care of the tail. */
- if (data_size == 2)
- {
- *current++ = base64_chars[(data[i] >> 2) & 0x3F];
- *current++ = base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
- *current++ = base64_chars[(data[i + 1] & 0x0F) << 2];
- *current++ = GRPC_BASE64_PAD_CHAR;
- }
- else if (data_size == 1)
- {
- *current++ = base64_chars[(data[i] >> 2) & 0x3F];
- *current++ = base64_chars[(data[i] & 0x03) << 4];
- *current++ = GRPC_BASE64_PAD_CHAR;
- *current++ = GRPC_BASE64_PAD_CHAR;
- }
-
- GPR_ASSERT (current >= result);
- GPR_ASSERT ((gpr_uintptr) (current - result) < result_projected_size);
+ if (data_size == 2) {
+ *current++ = base64_chars[(data[i] >> 2) & 0x3F];
+ *current++ =
+ base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
+ *current++ = base64_chars[(data[i + 1] & 0x0F) << 2];
+ *current++ = GRPC_BASE64_PAD_CHAR;
+ } else if (data_size == 1) {
+ *current++ = base64_chars[(data[i] >> 2) & 0x3F];
+ *current++ = base64_chars[(data[i] & 0x03) << 4];
+ *current++ = GRPC_BASE64_PAD_CHAR;
+ *current++ = GRPC_BASE64_PAD_CHAR;
+ }
+
+ GPR_ASSERT(current >= result);
+ GPR_ASSERT((gpr_uintptr)(current - result) < result_projected_size);
result[current - result] = '\0';
return result;
}
-gpr_slice
-grpc_base64_decode (const char *b64, int url_safe)
-{
- return grpc_base64_decode_with_len (b64, strlen (b64), url_safe);
+gpr_slice grpc_base64_decode(const char *b64, int url_safe) {
+ return grpc_base64_decode_with_len(b64, strlen(b64), url_safe);
}
-static void
-decode_one_char (const unsigned char *codes, unsigned char *result, size_t * result_offset)
-{
- gpr_uint32 packed = ((gpr_uint32) codes[0] << 2) | ((gpr_uint32) codes[1] >> 4);
- result[(*result_offset)++] = (unsigned char) packed;
+static void decode_one_char(const unsigned char *codes, unsigned char *result,
+ size_t *result_offset) {
+ gpr_uint32 packed = ((gpr_uint32)codes[0] << 2) | ((gpr_uint32)codes[1] >> 4);
+ result[(*result_offset)++] = (unsigned char)packed;
}
-static void
-decode_two_chars (const unsigned char *codes, unsigned char *result, size_t * result_offset)
-{
- gpr_uint32 packed = ((gpr_uint32) codes[0] << 10) | ((gpr_uint32) codes[1] << 4) | ((gpr_uint32) codes[2] >> 2);
- result[(*result_offset)++] = (unsigned char) (packed >> 8);
- result[(*result_offset)++] = (unsigned char) (packed);
+static void decode_two_chars(const unsigned char *codes, unsigned char *result,
+ size_t *result_offset) {
+ gpr_uint32 packed = ((gpr_uint32)codes[0] << 10) |
+ ((gpr_uint32)codes[1] << 4) | ((gpr_uint32)codes[2] >> 2);
+ result[(*result_offset)++] = (unsigned char)(packed >> 8);
+ result[(*result_offset)++] = (unsigned char)(packed);
}
-static int
-decode_group (const unsigned char *codes, size_t num_codes, unsigned char *result, size_t * result_offset)
-{
- GPR_ASSERT (num_codes <= 4);
+static int decode_group(const unsigned char *codes, size_t num_codes,
+ unsigned char *result, size_t *result_offset) {
+ GPR_ASSERT(num_codes <= 4);
/* Short end groups that may not have padding. */
- if (num_codes == 1)
- {
- gpr_log (GPR_ERROR, "Invalid group. Must be at least 2 bytes.");
- return 0;
- }
- if (num_codes == 2)
- {
- decode_one_char (codes, result, result_offset);
- return 1;
- }
- if (num_codes == 3)
- {
- decode_two_chars (codes, result, result_offset);
- return 1;
- }
+ if (num_codes == 1) {
+ gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes.");
+ return 0;
+ }
+ if (num_codes == 2) {
+ decode_one_char(codes, result, result_offset);
+ return 1;
+ }
+ if (num_codes == 3) {
+ decode_two_chars(codes, result, result_offset);
+ return 1;
+ }
/* Regular 4 byte groups with padding or not. */
- GPR_ASSERT (num_codes == 4);
- if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE)
- {
- gpr_log (GPR_ERROR, "Invalid padding detected.");
+ GPR_ASSERT(num_codes == 4);
+ if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) {
+ gpr_log(GPR_ERROR, "Invalid padding detected.");
+ return 0;
+ }
+ if (codes[2] == GRPC_BASE64_PAD_BYTE) {
+ if (codes[3] == GRPC_BASE64_PAD_BYTE) {
+ decode_one_char(codes, result, result_offset);
+ } else {
+ gpr_log(GPR_ERROR, "Invalid padding detected.");
return 0;
}
- if (codes[2] == GRPC_BASE64_PAD_BYTE)
- {
- if (codes[3] == GRPC_BASE64_PAD_BYTE)
- {
- decode_one_char (codes, result, result_offset);
- }
- else
- {
- gpr_log (GPR_ERROR, "Invalid padding detected.");
- return 0;
- }
- }
- else if (codes[3] == GRPC_BASE64_PAD_BYTE)
- {
- decode_two_chars (codes, result, result_offset);
- }
- else
- {
- /* No padding. */
- gpr_uint32 packed = ((gpr_uint32) codes[0] << 18) | ((gpr_uint32) codes[1] << 12) | ((gpr_uint32) codes[2] << 6) | codes[3];
- result[(*result_offset)++] = (unsigned char) (packed >> 16);
- result[(*result_offset)++] = (unsigned char) (packed >> 8);
- result[(*result_offset)++] = (unsigned char) (packed);
- }
+ } else if (codes[3] == GRPC_BASE64_PAD_BYTE) {
+ decode_two_chars(codes, result, result_offset);
+ } else {
+ /* No padding. */
+ gpr_uint32 packed = ((gpr_uint32)codes[0] << 18) |
+ ((gpr_uint32)codes[1] << 12) |
+ ((gpr_uint32)codes[2] << 6) | codes[3];
+ result[(*result_offset)++] = (unsigned char)(packed >> 16);
+ result[(*result_offset)++] = (unsigned char)(packed >> 8);
+ result[(*result_offset)++] = (unsigned char)(packed);
+ }
return 1;
}
-gpr_slice
-grpc_base64_decode_with_len (const char *b64, size_t b64_len, int url_safe)
-{
- gpr_slice result = gpr_slice_malloc (b64_len);
- unsigned char *current = GPR_SLICE_START_PTR (result);
+gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len,
+ int url_safe) {
+ gpr_slice result = gpr_slice_malloc(b64_len);
+ unsigned char *current = GPR_SLICE_START_PTR(result);
size_t result_size = 0;
unsigned char codes[4];
size_t num_codes = 0;
- while (b64_len--)
- {
- unsigned char c = (unsigned char) (*b64++);
- signed char code;
- if (c >= GPR_ARRAY_SIZE (base64_bytes))
- continue;
- if (url_safe)
- {
- if (c == '+' || c == '/')
- {
- gpr_log (GPR_ERROR, "Invalid character for url safe base64 %c", c);
- goto fail;
- }
- if (c == '-')
- {
- c = '+';
- }
- else if (c == '_')
- {
- c = '/';
- }
- }
- code = base64_bytes[c];
- if (code == -1)
- {
- if (c != '\r' && c != '\n')
- {
- gpr_log (GPR_ERROR, "Invalid character %c", c);
- goto fail;
- }
- }
- else
- {
- codes[num_codes++] = (unsigned char) code;
- if (num_codes == 4)
- {
- if (!decode_group (codes, num_codes, current, &result_size))
- goto fail;
- num_codes = 0;
- }
- }
+ while (b64_len--) {
+ unsigned char c = (unsigned char)(*b64++);
+ signed char code;
+ if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue;
+ if (url_safe) {
+ if (c == '+' || c == '/') {
+ gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c);
+ goto fail;
+ }
+ if (c == '-') {
+ c = '+';
+ } else if (c == '_') {
+ c = '/';
+ }
}
-
- if (num_codes != 0 && !decode_group (codes, num_codes, current, &result_size))
- {
- goto fail;
+ code = base64_bytes[c];
+ if (code == -1) {
+ if (c != '\r' && c != '\n') {
+ gpr_log(GPR_ERROR, "Invalid character %c", c);
+ goto fail;
+ }
+ } else {
+ codes[num_codes++] = (unsigned char)code;
+ if (num_codes == 4) {
+ if (!decode_group(codes, num_codes, current, &result_size)) goto fail;
+ num_codes = 0;
+ }
}
- GPR_SLICE_SET_LENGTH (result, result_size);
+ }
+
+ if (num_codes != 0 &&
+ !decode_group(codes, num_codes, current, &result_size)) {
+ goto fail;
+ }
+ GPR_SLICE_SET_LENGTH(result, result_size);
return result;
fail:
- gpr_slice_unref (result);
- return gpr_empty_slice ();
+ gpr_slice_unref(result);
+ return gpr_empty_slice();
}
diff --git a/src/core/security/base64.h b/src/core/security/base64.h
index 9d4b44c4b0..31ae982691 100644
--- a/src/core/security/base64.h
+++ b/src/core/security/base64.h
@@ -38,13 +38,15 @@
/* Encodes data using base64. It is the caller's responsability to free
the returned char * using gpr_free. Returns NULL on NULL input. */
-char *grpc_base64_encode (const void *data, size_t data_size, int url_safe, int multiline);
+char *grpc_base64_encode(const void *data, size_t data_size, int url_safe,
+ int multiline);
/* Decodes data according to the base64 specification. Returns an empty
slice in case of failure. */
-gpr_slice grpc_base64_decode (const char *b64, int url_safe);
+gpr_slice grpc_base64_decode(const char *b64, int url_safe);
/* Same as above except that the length is provided by the caller. */
-gpr_slice grpc_base64_decode_with_len (const char *b64, size_t b64_len, int url_safe);
+gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len,
+ int url_safe);
#endif /* GRPC_INTERNAL_CORE_SECURITY_BASE64_H */
diff --git a/src/core/security/client_auth_filter.c b/src/core/security/client_auth_filter.c
index 33f2e8093a..e24281bdff 100644
--- a/src/core/security/client_auth_filter.c
+++ b/src/core/security/client_auth_filter.c
@@ -49,8 +49,7 @@
#define MAX_CREDENTIALS_METADATA_COUNT 4
/* We can have a per-call credentials. */
-typedef struct
-{
+typedef struct {
grpc_credentials *creds;
grpc_mdstr *host;
grpc_mdstr *method;
@@ -68,8 +67,7 @@ typedef struct
} call_data;
/* We can have a per-channel credentials. */
-typedef struct
-{
+typedef struct {
grpc_channel_security_connector *security_connector;
grpc_mdctx *md_ctx;
grpc_mdstr *authority_string;
@@ -78,130 +76,125 @@ typedef struct
grpc_mdstr *status_key;
} channel_data;
-static void
-reset_service_url (call_data * calld)
-{
- if (calld->service_url != NULL)
- {
- gpr_free (calld->service_url);
- calld->service_url = NULL;
- }
+static void reset_service_url(call_data *calld) {
+ if (calld->service_url != NULL) {
+ gpr_free(calld->service_url);
+ calld->service_url = NULL;
+ }
}
-static void
-bubble_up_error (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_status_code status, const char *error_msg)
-{
+static void bubble_up_error(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ grpc_status_code status, const char *error_msg) {
call_data *calld = elem->call_data;
- gpr_log (GPR_ERROR, "Client side authentication failure: %s", error_msg);
- grpc_transport_stream_op_add_cancellation (&calld->op, status);
- grpc_call_next_op (exec_ctx, elem, &calld->op);
+ gpr_log(GPR_ERROR, "Client side authentication failure: %s", error_msg);
+ grpc_transport_stream_op_add_cancellation(&calld->op, status);
+ grpc_call_next_op(exec_ctx, elem, &calld->op);
}
-static void
-on_credentials_metadata (grpc_exec_ctx * exec_ctx, void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status)
-{
- grpc_call_element *elem = (grpc_call_element *) user_data;
+static void on_credentials_metadata(grpc_exec_ctx *exec_ctx, void *user_data,
+ grpc_credentials_md *md_elems,
+ size_t num_md,
+ grpc_credentials_status status) {
+ grpc_call_element *elem = (grpc_call_element *)user_data;
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
grpc_transport_stream_op *op = &calld->op;
grpc_metadata_batch *mdb;
size_t i;
- reset_service_url (calld);
- if (status != GRPC_CREDENTIALS_OK)
- {
- bubble_up_error (exec_ctx, elem, GRPC_STATUS_UNAUTHENTICATED, "Credentials failed to get metadata.");
- return;
- }
- GPR_ASSERT (num_md <= MAX_CREDENTIALS_METADATA_COUNT);
- GPR_ASSERT (op->send_ops && op->send_ops->nops > calld->op_md_idx && op->send_ops->ops[calld->op_md_idx].type == GRPC_OP_METADATA);
+ reset_service_url(calld);
+ if (status != GRPC_CREDENTIALS_OK) {
+ bubble_up_error(exec_ctx, elem, GRPC_STATUS_UNAUTHENTICATED,
+ "Credentials failed to get metadata.");
+ return;
+ }
+ GPR_ASSERT(num_md <= MAX_CREDENTIALS_METADATA_COUNT);
+ GPR_ASSERT(op->send_ops && op->send_ops->nops > calld->op_md_idx &&
+ op->send_ops->ops[calld->op_md_idx].type == GRPC_OP_METADATA);
mdb = &op->send_ops->ops[calld->op_md_idx].data.metadata;
- for (i = 0; i < num_md; i++)
- {
- grpc_metadata_batch_add_tail (mdb, &calld->md_links[i], grpc_mdelem_from_slices (chand->md_ctx, gpr_slice_ref (md_elems[i].key), gpr_slice_ref (md_elems[i].value)));
- }
- grpc_call_next_op (exec_ctx, elem, op);
+ for (i = 0; i < num_md; i++) {
+ grpc_metadata_batch_add_tail(
+ mdb, &calld->md_links[i],
+ grpc_mdelem_from_slices(chand->md_ctx, gpr_slice_ref(md_elems[i].key),
+ gpr_slice_ref(md_elems[i].value)));
+ }
+ grpc_call_next_op(exec_ctx, elem, op);
}
-void
-build_service_url (const char *url_scheme, call_data * calld)
-{
- char *service = gpr_strdup (grpc_mdstr_as_c_string (calld->method));
- char *last_slash = strrchr (service, '/');
- if (last_slash == NULL)
- {
- gpr_log (GPR_ERROR, "No '/' found in fully qualified method name");
- service[0] = '\0';
- }
- else if (last_slash == service)
- {
- /* No service part in fully qualified method name: will just be "/". */
- service[1] = '\0';
- }
- else
- {
- *last_slash = '\0';
- }
- if (url_scheme == NULL)
- url_scheme = "";
- reset_service_url (calld);
- gpr_asprintf (&calld->service_url, "%s://%s%s", url_scheme, grpc_mdstr_as_c_string (calld->host), service);
- gpr_free (service);
+void build_service_url(const char *url_scheme, call_data *calld) {
+ char *service = gpr_strdup(grpc_mdstr_as_c_string(calld->method));
+ char *last_slash = strrchr(service, '/');
+ if (last_slash == NULL) {
+ gpr_log(GPR_ERROR, "No '/' found in fully qualified method name");
+ service[0] = '\0';
+ } else if (last_slash == service) {
+ /* No service part in fully qualified method name: will just be "/". */
+ service[1] = '\0';
+ } else {
+ *last_slash = '\0';
+ }
+ if (url_scheme == NULL) url_scheme = "";
+ reset_service_url(calld);
+ gpr_asprintf(&calld->service_url, "%s://%s%s", url_scheme,
+ grpc_mdstr_as_c_string(calld->host), service);
+ gpr_free(service);
}
-static void
-send_security_metadata (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void send_security_metadata(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- grpc_client_security_context *ctx = (grpc_client_security_context *) op->context[GRPC_CONTEXT_SECURITY].value;
- grpc_credentials *channel_creds = chand->security_connector->request_metadata_creds;
- int channel_creds_has_md = (channel_creds != NULL) && grpc_credentials_has_request_metadata (channel_creds);
- int call_creds_has_md = (ctx != NULL) && (ctx->creds != NULL) && grpc_credentials_has_request_metadata (ctx->creds);
+ grpc_client_security_context *ctx =
+ (grpc_client_security_context *)op->context[GRPC_CONTEXT_SECURITY].value;
+ grpc_credentials *channel_creds =
+ chand->security_connector->request_metadata_creds;
+ int channel_creds_has_md =
+ (channel_creds != NULL) &&
+ grpc_credentials_has_request_metadata(channel_creds);
+ int call_creds_has_md = (ctx != NULL) && (ctx->creds != NULL) &&
+ grpc_credentials_has_request_metadata(ctx->creds);
- if (!channel_creds_has_md && !call_creds_has_md)
- {
- /* Skip sending metadata altogether. */
- grpc_call_next_op (exec_ctx, elem, op);
- return;
- }
+ if (!channel_creds_has_md && !call_creds_has_md) {
+ /* Skip sending metadata altogether. */
+ grpc_call_next_op(exec_ctx, elem, op);
+ return;
+ }
- if (channel_creds_has_md && call_creds_has_md)
- {
- calld->creds = grpc_composite_credentials_create (channel_creds, ctx->creds, NULL);
- if (calld->creds == NULL)
- {
- bubble_up_error (exec_ctx, elem, GRPC_STATUS_INVALID_ARGUMENT, "Incompatible credentials set on channel and call.");
- return;
- }
- }
- else
- {
- calld->creds = grpc_credentials_ref (call_creds_has_md ? ctx->creds : channel_creds);
+ if (channel_creds_has_md && call_creds_has_md) {
+ calld->creds =
+ grpc_composite_credentials_create(channel_creds, ctx->creds, NULL);
+ if (calld->creds == NULL) {
+ bubble_up_error(exec_ctx, elem, GRPC_STATUS_INVALID_ARGUMENT,
+ "Incompatible credentials set on channel and call.");
+ return;
}
+ } else {
+ calld->creds =
+ grpc_credentials_ref(call_creds_has_md ? ctx->creds : channel_creds);
+ }
- build_service_url (chand->security_connector->base.url_scheme, calld);
- calld->op = *op; /* Copy op (originates from the caller's stack). */
- GPR_ASSERT (calld->pollset);
- grpc_credentials_get_request_metadata (exec_ctx, calld->creds, calld->pollset, calld->service_url, on_credentials_metadata, elem);
+ build_service_url(chand->security_connector->base.url_scheme, calld);
+ calld->op = *op; /* Copy op (originates from the caller's stack). */
+ GPR_ASSERT(calld->pollset);
+ grpc_credentials_get_request_metadata(exec_ctx, calld->creds, calld->pollset,
+ calld->service_url,
+ on_credentials_metadata, elem);
}
-static void
-on_host_checked (grpc_exec_ctx * exec_ctx, void *user_data, grpc_security_status status)
-{
- grpc_call_element *elem = (grpc_call_element *) user_data;
+static void on_host_checked(grpc_exec_ctx *exec_ctx, void *user_data,
+ grpc_security_status status) {
+ grpc_call_element *elem = (grpc_call_element *)user_data;
call_data *calld = elem->call_data;
- if (status == GRPC_SECURITY_OK)
- {
- send_security_metadata (exec_ctx, elem, &calld->op);
- }
- else
- {
- char *error_msg;
- gpr_asprintf (&error_msg, "Invalid host %s set in :authority metadata.", grpc_mdstr_as_c_string (calld->host));
- bubble_up_error (exec_ctx, elem, GRPC_STATUS_INVALID_ARGUMENT, error_msg);
- gpr_free (error_msg);
- }
+ if (status == GRPC_SECURITY_OK) {
+ send_security_metadata(exec_ctx, elem, &calld->op);
+ } else {
+ char *error_msg;
+ gpr_asprintf(&error_msg, "Invalid host %s set in :authority metadata.",
+ grpc_mdstr_as_c_string(calld->host));
+ bubble_up_error(exec_ctx, elem, GRPC_STATUS_INVALID_ARGUMENT, error_msg);
+ gpr_free(error_msg);
+ }
}
/* Called either:
@@ -209,9 +202,9 @@ on_host_checked (grpc_exec_ctx * exec_ctx, void *user_data, grpc_security_status
- a network event (or similar) from below, to receive something
op contains type and call direction information, in addition to the data
that is being sent or received. */
-static void
-auth_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void auth_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
@@ -219,162 +212,152 @@ auth_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grp
size_t i;
grpc_client_security_context *sec_ctx = NULL;
- if (calld->security_context_set == 0)
- {
- calld->security_context_set = 1;
- GPR_ASSERT (op->context);
- if (op->context[GRPC_CONTEXT_SECURITY].value == NULL)
- {
- op->context[GRPC_CONTEXT_SECURITY].value = grpc_client_security_context_create ();
- op->context[GRPC_CONTEXT_SECURITY].destroy = grpc_client_security_context_destroy;
- }
- sec_ctx = op->context[GRPC_CONTEXT_SECURITY].value;
- GRPC_AUTH_CONTEXT_UNREF (sec_ctx->auth_context, "client auth filter");
- sec_ctx->auth_context = GRPC_AUTH_CONTEXT_REF (chand->security_connector->base.auth_context, "client_auth_filter");
+ if (calld->security_context_set == 0) {
+ calld->security_context_set = 1;
+ GPR_ASSERT(op->context);
+ if (op->context[GRPC_CONTEXT_SECURITY].value == NULL) {
+ op->context[GRPC_CONTEXT_SECURITY].value =
+ grpc_client_security_context_create();
+ op->context[GRPC_CONTEXT_SECURITY].destroy =
+ grpc_client_security_context_destroy;
}
+ sec_ctx = op->context[GRPC_CONTEXT_SECURITY].value;
+ GRPC_AUTH_CONTEXT_UNREF(sec_ctx->auth_context, "client auth filter");
+ sec_ctx->auth_context = GRPC_AUTH_CONTEXT_REF(
+ chand->security_connector->base.auth_context, "client_auth_filter");
+ }
- if (op->bind_pollset)
- {
- calld->pollset = op->bind_pollset;
- }
+ if (op->bind_pollset) {
+ calld->pollset = op->bind_pollset;
+ }
- if (op->send_ops && !calld->sent_initial_metadata)
- {
- size_t nops = op->send_ops->nops;
- grpc_stream_op *ops = op->send_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *sop = &ops[i];
- if (sop->type != GRPC_OP_METADATA)
- continue;
- calld->op_md_idx = i;
- calld->sent_initial_metadata = 1;
- for (l = sop->data.metadata.list.head; l != NULL; l = l->next)
- {
- grpc_mdelem *md = l->md;
- /* Pointer comparison is OK for md_elems created from the same context.
- */
- if (md->key == chand->authority_string)
- {
- if (calld->host != NULL)
- GRPC_MDSTR_UNREF (calld->host);
- calld->host = GRPC_MDSTR_REF (md->value);
- }
- else if (md->key == chand->path_string)
- {
- if (calld->method != NULL)
- GRPC_MDSTR_UNREF (calld->method);
- calld->method = GRPC_MDSTR_REF (md->value);
- }
- }
- if (calld->host != NULL)
- {
- grpc_security_status status;
- const char *call_host = grpc_mdstr_as_c_string (calld->host);
- calld->op = *op; /* Copy op (originates from the caller's stack). */
- status = grpc_channel_security_connector_check_call_host (exec_ctx, chand->security_connector, call_host, on_host_checked, elem);
- if (status != GRPC_SECURITY_OK)
- {
- if (status == GRPC_SECURITY_ERROR)
- {
- char *error_msg;
- gpr_asprintf (&error_msg, "Invalid host %s set in :authority metadata.", call_host);
- bubble_up_error (exec_ctx, elem, GRPC_STATUS_INVALID_ARGUMENT, error_msg);
- gpr_free (error_msg);
- }
- return; /* early exit */
- }
- }
- send_security_metadata (exec_ctx, elem, op);
- return; /* early exit */
- }
+ if (op->send_ops && !calld->sent_initial_metadata) {
+ size_t nops = op->send_ops->nops;
+ grpc_stream_op *ops = op->send_ops->ops;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *sop = &ops[i];
+ if (sop->type != GRPC_OP_METADATA) continue;
+ calld->op_md_idx = i;
+ calld->sent_initial_metadata = 1;
+ for (l = sop->data.metadata.list.head; l != NULL; l = l->next) {
+ grpc_mdelem *md = l->md;
+ /* Pointer comparison is OK for md_elems created from the same context.
+ */
+ if (md->key == chand->authority_string) {
+ if (calld->host != NULL) GRPC_MDSTR_UNREF(calld->host);
+ calld->host = GRPC_MDSTR_REF(md->value);
+ } else if (md->key == chand->path_string) {
+ if (calld->method != NULL) GRPC_MDSTR_UNREF(calld->method);
+ calld->method = GRPC_MDSTR_REF(md->value);
+ }
+ }
+ if (calld->host != NULL) {
+ grpc_security_status status;
+ const char *call_host = grpc_mdstr_as_c_string(calld->host);
+ calld->op = *op; /* Copy op (originates from the caller's stack). */
+ status = grpc_channel_security_connector_check_call_host(
+ exec_ctx, chand->security_connector, call_host, on_host_checked,
+ elem);
+ if (status != GRPC_SECURITY_OK) {
+ if (status == GRPC_SECURITY_ERROR) {
+ char *error_msg;
+ gpr_asprintf(&error_msg,
+ "Invalid host %s set in :authority metadata.",
+ call_host);
+ bubble_up_error(exec_ctx, elem, GRPC_STATUS_INVALID_ARGUMENT,
+ error_msg);
+ gpr_free(error_msg);
+ }
+ return; /* early exit */
+ }
+ }
+ send_security_metadata(exec_ctx, elem, op);
+ return; /* early exit */
}
+ }
/* pass control down the stack */
- grpc_call_next_op (exec_ctx, elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *calld = elem->call_data;
- memset (calld, 0, sizeof (*calld));
- GPR_ASSERT (!initial_op || !initial_op->send_ops);
+ memset(calld, 0, sizeof(*calld));
+ GPR_ASSERT(!initial_op || !initial_op->send_ops);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
call_data *calld = elem->call_data;
- grpc_credentials_unref (calld->creds);
- if (calld->host != NULL)
- {
- GRPC_MDSTR_UNREF (calld->host);
- }
- if (calld->method != NULL)
- {
- GRPC_MDSTR_UNREF (calld->method);
- }
- reset_service_url (calld);
+ grpc_credentials_unref(calld->creds);
+ if (calld->host != NULL) {
+ GRPC_MDSTR_UNREF(calld->host);
+ }
+ if (calld->method != NULL) {
+ GRPC_MDSTR_UNREF(calld->method);
+ }
+ reset_service_url(calld);
}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, int is_first, int is_last)
-{
- grpc_security_connector *sc = grpc_find_security_connector_in_args (args);
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args,
+ grpc_mdctx *metadata_context, int is_first,
+ int is_last) {
+ grpc_security_connector *sc = grpc_find_security_connector_in_args(args);
/* grab pointers to our data from the channel element */
channel_data *chand = elem->channel_data;
/* The first and the last filters tend to be implemented differently to
handle the case that there's no 'next' filter to call on the up or down
path */
- GPR_ASSERT (!is_last);
- GPR_ASSERT (sc != NULL);
+ GPR_ASSERT(!is_last);
+ GPR_ASSERT(sc != NULL);
/* initialize members */
- GPR_ASSERT (sc->is_client_side);
- chand->security_connector = (grpc_channel_security_connector *) GRPC_SECURITY_CONNECTOR_REF (sc, "client_auth_filter");
+ GPR_ASSERT(sc->is_client_side);
+ chand->security_connector =
+ (grpc_channel_security_connector *)GRPC_SECURITY_CONNECTOR_REF(
+ sc, "client_auth_filter");
chand->md_ctx = metadata_context;
- chand->authority_string = grpc_mdstr_from_string (chand->md_ctx, ":authority", 0);
- chand->path_string = grpc_mdstr_from_string (chand->md_ctx, ":path", 0);
- chand->error_msg_key = grpc_mdstr_from_string (chand->md_ctx, "grpc-message", 0);
- chand->status_key = grpc_mdstr_from_string (chand->md_ctx, "grpc-status", 0);
+ chand->authority_string =
+ grpc_mdstr_from_string(chand->md_ctx, ":authority", 0);
+ chand->path_string = grpc_mdstr_from_string(chand->md_ctx, ":path", 0);
+ chand->error_msg_key =
+ grpc_mdstr_from_string(chand->md_ctx, "grpc-message", 0);
+ chand->status_key = grpc_mdstr_from_string(chand->md_ctx, "grpc-status", 0);
}
/* Destructor for channel data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
/* grab pointers to our data from the channel element */
channel_data *chand = elem->channel_data;
grpc_channel_security_connector *ctx = chand->security_connector;
if (ctx != NULL)
- GRPC_SECURITY_CONNECTOR_UNREF (&ctx->base, "client_auth_filter");
- if (chand->authority_string != NULL)
- {
- GRPC_MDSTR_UNREF (chand->authority_string);
- }
- if (chand->error_msg_key != NULL)
- {
- GRPC_MDSTR_UNREF (chand->error_msg_key);
- }
- if (chand->status_key != NULL)
- {
- GRPC_MDSTR_UNREF (chand->status_key);
- }
- if (chand->path_string != NULL)
- {
- GRPC_MDSTR_UNREF (chand->path_string);
- }
+ GRPC_SECURITY_CONNECTOR_UNREF(&ctx->base, "client_auth_filter");
+ if (chand->authority_string != NULL) {
+ GRPC_MDSTR_UNREF(chand->authority_string);
+ }
+ if (chand->error_msg_key != NULL) {
+ GRPC_MDSTR_UNREF(chand->error_msg_key);
+ }
+ if (chand->status_key != NULL) {
+ GRPC_MDSTR_UNREF(chand->status_key);
+ }
+ if (chand->path_string != NULL) {
+ GRPC_MDSTR_UNREF(chand->path_string);
+ }
}
const grpc_channel_filter grpc_client_auth_filter = {
- auth_start_transport_op, grpc_channel_next_op,
- sizeof (call_data), init_call_elem,
- destroy_call_elem, sizeof (channel_data),
- init_channel_elem, destroy_channel_elem,
- grpc_call_next_get_peer, "client-auth"
-};
+ auth_start_transport_op, grpc_channel_next_op,
+ sizeof(call_data), init_call_elem,
+ destroy_call_elem, sizeof(channel_data),
+ init_channel_elem, destroy_channel_elem,
+ grpc_call_next_get_peer, "client-auth"};
diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c
index 71dfb25825..48cfed7476 100644
--- a/src/core/security/credentials.c
+++ b/src/core/security/credentials.c
@@ -52,935 +52,857 @@
/* -- Common. -- */
-struct grpc_credentials_metadata_request
-{
+struct grpc_credentials_metadata_request {
grpc_credentials *creds;
grpc_credentials_metadata_cb cb;
void *user_data;
};
static grpc_credentials_metadata_request *
-grpc_credentials_metadata_request_create (grpc_credentials * creds, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_credentials_metadata_request *r = gpr_malloc (sizeof (grpc_credentials_metadata_request));
- r->creds = grpc_credentials_ref (creds);
+grpc_credentials_metadata_request_create(grpc_credentials *creds,
+ grpc_credentials_metadata_cb cb,
+ void *user_data) {
+ grpc_credentials_metadata_request *r =
+ gpr_malloc(sizeof(grpc_credentials_metadata_request));
+ r->creds = grpc_credentials_ref(creds);
r->cb = cb;
r->user_data = user_data;
return r;
}
-static void
-grpc_credentials_metadata_request_destroy (grpc_credentials_metadata_request * r)
-{
- grpc_credentials_unref (r->creds);
- gpr_free (r);
+static void grpc_credentials_metadata_request_destroy(
+ grpc_credentials_metadata_request *r) {
+ grpc_credentials_unref(r->creds);
+ gpr_free(r);
}
-grpc_credentials *
-grpc_credentials_ref (grpc_credentials * creds)
-{
- if (creds == NULL)
- return NULL;
- gpr_ref (&creds->refcount);
+grpc_credentials *grpc_credentials_ref(grpc_credentials *creds) {
+ if (creds == NULL) return NULL;
+ gpr_ref(&creds->refcount);
return creds;
}
-void
-grpc_credentials_unref (grpc_credentials * creds)
-{
- if (creds == NULL)
- return;
- if (gpr_unref (&creds->refcount))
- {
- creds->vtable->destruct (creds);
- gpr_free (creds);
- }
+void grpc_credentials_unref(grpc_credentials *creds) {
+ if (creds == NULL) return;
+ if (gpr_unref(&creds->refcount)) {
+ creds->vtable->destruct(creds);
+ gpr_free(creds);
+ }
}
-void
-grpc_credentials_release (grpc_credentials * creds)
-{
- grpc_credentials_unref (creds);
+void grpc_credentials_release(grpc_credentials *creds) {
+ grpc_credentials_unref(creds);
}
-int
-grpc_credentials_has_request_metadata (grpc_credentials * creds)
-{
- if (creds == NULL)
- return 0;
- return creds->vtable->has_request_metadata (creds);
+int grpc_credentials_has_request_metadata(grpc_credentials *creds) {
+ if (creds == NULL) return 0;
+ return creds->vtable->has_request_metadata(creds);
}
-int
-grpc_credentials_has_request_metadata_only (grpc_credentials * creds)
-{
- if (creds == NULL)
- return 0;
- return creds->vtable->has_request_metadata_only (creds);
+int grpc_credentials_has_request_metadata_only(grpc_credentials *creds) {
+ if (creds == NULL) return 0;
+ return creds->vtable->has_request_metadata_only(creds);
}
-void
-grpc_credentials_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- if (creds == NULL || !grpc_credentials_has_request_metadata (creds) || creds->vtable->get_request_metadata == NULL)
- {
- if (cb != NULL)
- {
- cb (exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_OK);
- }
- return;
+void grpc_credentials_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ if (creds == NULL || !grpc_credentials_has_request_metadata(creds) ||
+ creds->vtable->get_request_metadata == NULL) {
+ if (cb != NULL) {
+ cb(exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_OK);
}
- creds->vtable->get_request_metadata (exec_ctx, creds, pollset, service_url, cb, user_data);
+ return;
+ }
+ creds->vtable->get_request_metadata(exec_ctx, creds, pollset, service_url, cb,
+ user_data);
}
-grpc_security_status
-grpc_credentials_create_security_connector (grpc_credentials * creds, const char *target, const grpc_channel_args * args, grpc_credentials * request_metadata_creds, grpc_channel_security_connector ** sc, grpc_channel_args ** new_args)
-{
+grpc_security_status grpc_credentials_create_security_connector(
+ grpc_credentials *creds, const char *target, const grpc_channel_args *args,
+ grpc_credentials *request_metadata_creds,
+ grpc_channel_security_connector **sc, grpc_channel_args **new_args) {
*new_args = NULL;
- if (creds == NULL || creds->vtable->create_security_connector == NULL || grpc_credentials_has_request_metadata_only (creds))
- {
- gpr_log (GPR_ERROR, "Invalid credentials for creating a security connector.");
- return GRPC_SECURITY_ERROR;
- }
- return creds->vtable->create_security_connector (creds, target, args, request_metadata_creds, sc, new_args);
+ if (creds == NULL || creds->vtable->create_security_connector == NULL ||
+ grpc_credentials_has_request_metadata_only(creds)) {
+ gpr_log(GPR_ERROR,
+ "Invalid credentials for creating a security connector.");
+ return GRPC_SECURITY_ERROR;
+ }
+ return creds->vtable->create_security_connector(
+ creds, target, args, request_metadata_creds, sc, new_args);
}
-grpc_server_credentials *
-grpc_server_credentials_ref (grpc_server_credentials * creds)
-{
- if (creds == NULL)
- return NULL;
- gpr_ref (&creds->refcount);
+grpc_server_credentials *grpc_server_credentials_ref(
+ grpc_server_credentials *creds) {
+ if (creds == NULL) return NULL;
+ gpr_ref(&creds->refcount);
return creds;
}
-void
-grpc_server_credentials_unref (grpc_server_credentials * creds)
-{
- if (creds == NULL)
- return;
- if (gpr_unref (&creds->refcount))
- {
- creds->vtable->destruct (creds);
- if (creds->processor.destroy != NULL && creds->processor.state != NULL)
- {
- creds->processor.destroy (creds->processor.state);
- }
- gpr_free (creds);
+void grpc_server_credentials_unref(grpc_server_credentials *creds) {
+ if (creds == NULL) return;
+ if (gpr_unref(&creds->refcount)) {
+ creds->vtable->destruct(creds);
+ if (creds->processor.destroy != NULL && creds->processor.state != NULL) {
+ creds->processor.destroy(creds->processor.state);
}
+ gpr_free(creds);
+ }
}
-void
-grpc_server_credentials_release (grpc_server_credentials * creds)
-{
- grpc_server_credentials_unref (creds);
+void grpc_server_credentials_release(grpc_server_credentials *creds) {
+ grpc_server_credentials_unref(creds);
}
-grpc_security_status
-grpc_server_credentials_create_security_connector (grpc_server_credentials * creds, grpc_security_connector ** sc)
-{
- if (creds == NULL || creds->vtable->create_security_connector == NULL)
- {
- gpr_log (GPR_ERROR, "Server credentials cannot create security context.");
- return GRPC_SECURITY_ERROR;
- }
- return creds->vtable->create_security_connector (creds, sc);
+grpc_security_status grpc_server_credentials_create_security_connector(
+ grpc_server_credentials *creds, grpc_security_connector **sc) {
+ if (creds == NULL || creds->vtable->create_security_connector == NULL) {
+ gpr_log(GPR_ERROR, "Server credentials cannot create security context.");
+ return GRPC_SECURITY_ERROR;
+ }
+ return creds->vtable->create_security_connector(creds, sc);
}
-void
-grpc_server_credentials_set_auth_metadata_processor (grpc_server_credentials * creds, grpc_auth_metadata_processor processor)
-{
- if (creds == NULL)
- return;
- if (creds->processor.destroy != NULL && creds->processor.state != NULL)
- {
- creds->processor.destroy (creds->processor.state);
- }
+void grpc_server_credentials_set_auth_metadata_processor(
+ grpc_server_credentials *creds, grpc_auth_metadata_processor processor) {
+ if (creds == NULL) return;
+ if (creds->processor.destroy != NULL && creds->processor.state != NULL) {
+ creds->processor.destroy(creds->processor.state);
+ }
creds->processor = processor;
}
/* -- Ssl credentials. -- */
-static void
-ssl_destruct (grpc_credentials * creds)
-{
- grpc_ssl_credentials *c = (grpc_ssl_credentials *) creds;
- if (c->config.pem_root_certs != NULL)
- gpr_free (c->config.pem_root_certs);
- if (c->config.pem_private_key != NULL)
- gpr_free (c->config.pem_private_key);
- if (c->config.pem_cert_chain != NULL)
- gpr_free (c->config.pem_cert_chain);
-}
-
-static void
-ssl_server_destruct (grpc_server_credentials * creds)
-{
- grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *) creds;
+static void ssl_destruct(grpc_credentials *creds) {
+ grpc_ssl_credentials *c = (grpc_ssl_credentials *)creds;
+ if (c->config.pem_root_certs != NULL) gpr_free(c->config.pem_root_certs);
+ if (c->config.pem_private_key != NULL) gpr_free(c->config.pem_private_key);
+ if (c->config.pem_cert_chain != NULL) gpr_free(c->config.pem_cert_chain);
+}
+
+static void ssl_server_destruct(grpc_server_credentials *creds) {
+ grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *)creds;
size_t i;
- for (i = 0; i < c->config.num_key_cert_pairs; i++)
- {
- if (c->config.pem_private_keys[i] != NULL)
- {
- gpr_free (c->config.pem_private_keys[i]);
- }
- if (c->config.pem_cert_chains[i] != NULL)
- {
- gpr_free (c->config.pem_cert_chains[i]);
- }
- }
- if (c->config.pem_private_keys != NULL)
- gpr_free (c->config.pem_private_keys);
- if (c->config.pem_private_keys_sizes != NULL)
- {
- gpr_free (c->config.pem_private_keys_sizes);
+ for (i = 0; i < c->config.num_key_cert_pairs; i++) {
+ if (c->config.pem_private_keys[i] != NULL) {
+ gpr_free(c->config.pem_private_keys[i]);
}
- if (c->config.pem_cert_chains != NULL)
- gpr_free (c->config.pem_cert_chains);
- if (c->config.pem_cert_chains_sizes != NULL)
- {
- gpr_free (c->config.pem_cert_chains_sizes);
+ if (c->config.pem_cert_chains[i] != NULL) {
+ gpr_free(c->config.pem_cert_chains[i]);
}
- if (c->config.pem_root_certs != NULL)
- gpr_free (c->config.pem_root_certs);
+ }
+ if (c->config.pem_private_keys != NULL) gpr_free(c->config.pem_private_keys);
+ if (c->config.pem_private_keys_sizes != NULL) {
+ gpr_free(c->config.pem_private_keys_sizes);
+ }
+ if (c->config.pem_cert_chains != NULL) gpr_free(c->config.pem_cert_chains);
+ if (c->config.pem_cert_chains_sizes != NULL) {
+ gpr_free(c->config.pem_cert_chains_sizes);
+ }
+ if (c->config.pem_root_certs != NULL) gpr_free(c->config.pem_root_certs);
}
-static int
-ssl_has_request_metadata (const grpc_credentials * creds)
-{
- return 0;
-}
+static int ssl_has_request_metadata(const grpc_credentials *creds) { return 0; }
-static int
-ssl_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int ssl_has_request_metadata_only(const grpc_credentials *creds) {
return 0;
}
-static grpc_security_status
-ssl_create_security_connector (grpc_credentials * creds, const char *target, const grpc_channel_args * args, grpc_credentials * request_metadata_creds, grpc_channel_security_connector ** sc, grpc_channel_args ** new_args)
-{
- grpc_ssl_credentials *c = (grpc_ssl_credentials *) creds;
+static grpc_security_status ssl_create_security_connector(
+ grpc_credentials *creds, const char *target, const grpc_channel_args *args,
+ grpc_credentials *request_metadata_creds,
+ grpc_channel_security_connector **sc, grpc_channel_args **new_args) {
+ grpc_ssl_credentials *c = (grpc_ssl_credentials *)creds;
grpc_security_status status = GRPC_SECURITY_OK;
size_t i = 0;
const char *overridden_target_name = NULL;
grpc_arg arg;
- for (i = 0; args && i < args->num_args; i++)
- {
- grpc_arg *arg = &args->args[i];
- if (strcmp (arg->key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == 0 && arg->type == GRPC_ARG_STRING)
- {
- overridden_target_name = arg->value.string;
- break;
- }
- }
- status = grpc_ssl_channel_security_connector_create (request_metadata_creds, &c->config, target, overridden_target_name, sc);
- if (status != GRPC_SECURITY_OK)
- {
- return status;
+ for (i = 0; args && i < args->num_args; i++) {
+ grpc_arg *arg = &args->args[i];
+ if (strcmp(arg->key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == 0 &&
+ arg->type == GRPC_ARG_STRING) {
+ overridden_target_name = arg->value.string;
+ break;
}
+ }
+ status = grpc_ssl_channel_security_connector_create(
+ request_metadata_creds, &c->config, target, overridden_target_name, sc);
+ if (status != GRPC_SECURITY_OK) {
+ return status;
+ }
arg.type = GRPC_ARG_STRING;
arg.key = GRPC_ARG_HTTP2_SCHEME;
arg.value.string = "https";
- *new_args = grpc_channel_args_copy_and_add (args, &arg, 1);
+ *new_args = grpc_channel_args_copy_and_add(args, &arg, 1);
return status;
}
-static grpc_security_status
-ssl_server_create_security_connector (grpc_server_credentials * creds, grpc_security_connector ** sc)
-{
- grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *) creds;
- return grpc_ssl_server_security_connector_create (&c->config, sc);
+static grpc_security_status ssl_server_create_security_connector(
+ grpc_server_credentials *creds, grpc_security_connector **sc) {
+ grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *)creds;
+ return grpc_ssl_server_security_connector_create(&c->config, sc);
}
static grpc_credentials_vtable ssl_vtable = {
- ssl_destruct, ssl_has_request_metadata, ssl_has_request_metadata_only, NULL,
- ssl_create_security_connector
-};
+ ssl_destruct, ssl_has_request_metadata, ssl_has_request_metadata_only, NULL,
+ ssl_create_security_connector};
static grpc_server_credentials_vtable ssl_server_vtable = {
- ssl_server_destruct, ssl_server_create_security_connector
-};
+ ssl_server_destruct, ssl_server_create_security_connector};
-static void
-ssl_copy_key_material (const char *input, unsigned char **output, size_t * output_size)
-{
- *output_size = strlen (input);
- *output = gpr_malloc (*output_size);
- memcpy (*output, input, *output_size);
+static void ssl_copy_key_material(const char *input, unsigned char **output,
+ size_t *output_size) {
+ *output_size = strlen(input);
+ *output = gpr_malloc(*output_size);
+ memcpy(*output, input, *output_size);
}
-static void
-ssl_build_config (const char *pem_root_certs, grpc_ssl_pem_key_cert_pair * pem_key_cert_pair, grpc_ssl_config * config)
-{
- if (pem_root_certs != NULL)
- {
- ssl_copy_key_material (pem_root_certs, &config->pem_root_certs, &config->pem_root_certs_size);
- }
- if (pem_key_cert_pair != NULL)
- {
- GPR_ASSERT (pem_key_cert_pair->private_key != NULL);
- GPR_ASSERT (pem_key_cert_pair->cert_chain != NULL);
- ssl_copy_key_material (pem_key_cert_pair->private_key, &config->pem_private_key, &config->pem_private_key_size);
- ssl_copy_key_material (pem_key_cert_pair->cert_chain, &config->pem_cert_chain, &config->pem_cert_chain_size);
- }
+static void ssl_build_config(const char *pem_root_certs,
+ grpc_ssl_pem_key_cert_pair *pem_key_cert_pair,
+ grpc_ssl_config *config) {
+ if (pem_root_certs != NULL) {
+ ssl_copy_key_material(pem_root_certs, &config->pem_root_certs,
+ &config->pem_root_certs_size);
+ }
+ if (pem_key_cert_pair != NULL) {
+ GPR_ASSERT(pem_key_cert_pair->private_key != NULL);
+ GPR_ASSERT(pem_key_cert_pair->cert_chain != NULL);
+ ssl_copy_key_material(pem_key_cert_pair->private_key,
+ &config->pem_private_key,
+ &config->pem_private_key_size);
+ ssl_copy_key_material(pem_key_cert_pair->cert_chain,
+ &config->pem_cert_chain,
+ &config->pem_cert_chain_size);
+ }
}
-static void
-ssl_build_server_config (const char *pem_root_certs, grpc_ssl_pem_key_cert_pair * pem_key_cert_pairs, size_t num_key_cert_pairs, int force_client_auth, grpc_ssl_server_config * config)
-{
+static void ssl_build_server_config(
+ const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs,
+ size_t num_key_cert_pairs, int force_client_auth,
+ grpc_ssl_server_config *config) {
size_t i;
config->force_client_auth = force_client_auth;
- if (pem_root_certs != NULL)
- {
- ssl_copy_key_material (pem_root_certs, &config->pem_root_certs, &config->pem_root_certs_size);
- }
- if (num_key_cert_pairs > 0)
- {
- GPR_ASSERT (pem_key_cert_pairs != NULL);
- config->pem_private_keys = gpr_malloc (num_key_cert_pairs * sizeof (unsigned char *));
- config->pem_cert_chains = gpr_malloc (num_key_cert_pairs * sizeof (unsigned char *));
- config->pem_private_keys_sizes = gpr_malloc (num_key_cert_pairs * sizeof (size_t));
- config->pem_cert_chains_sizes = gpr_malloc (num_key_cert_pairs * sizeof (size_t));
- }
+ if (pem_root_certs != NULL) {
+ ssl_copy_key_material(pem_root_certs, &config->pem_root_certs,
+ &config->pem_root_certs_size);
+ }
+ if (num_key_cert_pairs > 0) {
+ GPR_ASSERT(pem_key_cert_pairs != NULL);
+ config->pem_private_keys =
+ gpr_malloc(num_key_cert_pairs * sizeof(unsigned char *));
+ config->pem_cert_chains =
+ gpr_malloc(num_key_cert_pairs * sizeof(unsigned char *));
+ config->pem_private_keys_sizes =
+ gpr_malloc(num_key_cert_pairs * sizeof(size_t));
+ config->pem_cert_chains_sizes =
+ gpr_malloc(num_key_cert_pairs * sizeof(size_t));
+ }
config->num_key_cert_pairs = num_key_cert_pairs;
- for (i = 0; i < num_key_cert_pairs; i++)
- {
- GPR_ASSERT (pem_key_cert_pairs[i].private_key != NULL);
- GPR_ASSERT (pem_key_cert_pairs[i].cert_chain != NULL);
- ssl_copy_key_material (pem_key_cert_pairs[i].private_key, &config->pem_private_keys[i], &config->pem_private_keys_sizes[i]);
- ssl_copy_key_material (pem_key_cert_pairs[i].cert_chain, &config->pem_cert_chains[i], &config->pem_cert_chains_sizes[i]);
- }
+ for (i = 0; i < num_key_cert_pairs; i++) {
+ GPR_ASSERT(pem_key_cert_pairs[i].private_key != NULL);
+ GPR_ASSERT(pem_key_cert_pairs[i].cert_chain != NULL);
+ ssl_copy_key_material(pem_key_cert_pairs[i].private_key,
+ &config->pem_private_keys[i],
+ &config->pem_private_keys_sizes[i]);
+ ssl_copy_key_material(pem_key_cert_pairs[i].cert_chain,
+ &config->pem_cert_chains[i],
+ &config->pem_cert_chains_sizes[i]);
+ }
}
-grpc_credentials *
-grpc_ssl_credentials_create (const char *pem_root_certs, grpc_ssl_pem_key_cert_pair * pem_key_cert_pair, void *reserved)
-{
- grpc_ssl_credentials *c = gpr_malloc (sizeof (grpc_ssl_credentials));
- GPR_ASSERT (reserved == NULL);
- memset (c, 0, sizeof (grpc_ssl_credentials));
+grpc_credentials *grpc_ssl_credentials_create(
+ const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair,
+ void *reserved) {
+ grpc_ssl_credentials *c = gpr_malloc(sizeof(grpc_ssl_credentials));
+ GPR_ASSERT(reserved == NULL);
+ memset(c, 0, sizeof(grpc_ssl_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_SSL;
c->base.vtable = &ssl_vtable;
- gpr_ref_init (&c->base.refcount, 1);
- ssl_build_config (pem_root_certs, pem_key_cert_pair, &c->config);
+ gpr_ref_init(&c->base.refcount, 1);
+ ssl_build_config(pem_root_certs, pem_key_cert_pair, &c->config);
return &c->base;
}
-grpc_server_credentials *
-grpc_ssl_server_credentials_create (const char *pem_root_certs, grpc_ssl_pem_key_cert_pair * pem_key_cert_pairs, size_t num_key_cert_pairs, int force_client_auth, void *reserved)
-{
- grpc_ssl_server_credentials *c = gpr_malloc (sizeof (grpc_ssl_server_credentials));
- GPR_ASSERT (reserved == NULL);
- memset (c, 0, sizeof (grpc_ssl_server_credentials));
+grpc_server_credentials *grpc_ssl_server_credentials_create(
+ const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs,
+ size_t num_key_cert_pairs, int force_client_auth, void *reserved) {
+ grpc_ssl_server_credentials *c =
+ gpr_malloc(sizeof(grpc_ssl_server_credentials));
+ GPR_ASSERT(reserved == NULL);
+ memset(c, 0, sizeof(grpc_ssl_server_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_SSL;
- gpr_ref_init (&c->base.refcount, 1);
+ gpr_ref_init(&c->base.refcount, 1);
c->base.vtable = &ssl_server_vtable;
- ssl_build_server_config (pem_root_certs, pem_key_cert_pairs, num_key_cert_pairs, force_client_auth, &c->config);
+ ssl_build_server_config(pem_root_certs, pem_key_cert_pairs,
+ num_key_cert_pairs, force_client_auth, &c->config);
return &c->base;
}
/* -- Jwt credentials -- */
-static void
-jwt_reset_cache (grpc_service_account_jwt_access_credentials * c)
-{
- if (c->cached.jwt_md != NULL)
- {
- grpc_credentials_md_store_unref (c->cached.jwt_md);
- c->cached.jwt_md = NULL;
- }
- if (c->cached.service_url != NULL)
- {
- gpr_free (c->cached.service_url);
- c->cached.service_url = NULL;
- }
- c->cached.jwt_expiration = gpr_inf_past (GPR_CLOCK_REALTIME);
+static void jwt_reset_cache(grpc_service_account_jwt_access_credentials *c) {
+ if (c->cached.jwt_md != NULL) {
+ grpc_credentials_md_store_unref(c->cached.jwt_md);
+ c->cached.jwt_md = NULL;
+ }
+ if (c->cached.service_url != NULL) {
+ gpr_free(c->cached.service_url);
+ c->cached.service_url = NULL;
+ }
+ c->cached.jwt_expiration = gpr_inf_past(GPR_CLOCK_REALTIME);
}
-static void
-jwt_destruct (grpc_credentials * creds)
-{
- grpc_service_account_jwt_access_credentials *c = (grpc_service_account_jwt_access_credentials *) creds;
- grpc_auth_json_key_destruct (&c->key);
- jwt_reset_cache (c);
- gpr_mu_destroy (&c->cache_mu);
+static void jwt_destruct(grpc_credentials *creds) {
+ grpc_service_account_jwt_access_credentials *c =
+ (grpc_service_account_jwt_access_credentials *)creds;
+ grpc_auth_json_key_destruct(&c->key);
+ jwt_reset_cache(c);
+ gpr_mu_destroy(&c->cache_mu);
}
-static int
-jwt_has_request_metadata (const grpc_credentials * creds)
-{
- return 1;
-}
+static int jwt_has_request_metadata(const grpc_credentials *creds) { return 1; }
-static int
-jwt_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int jwt_has_request_metadata_only(const grpc_credentials *creds) {
return 1;
}
-static void
-jwt_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_service_account_jwt_access_credentials *c = (grpc_service_account_jwt_access_credentials *) creds;
- gpr_timespec refresh_threshold = gpr_time_from_seconds (GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS, GPR_TIMESPAN);
+static void jwt_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_service_account_jwt_access_credentials *c =
+ (grpc_service_account_jwt_access_credentials *)creds;
+ gpr_timespec refresh_threshold = gpr_time_from_seconds(
+ GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS, GPR_TIMESPAN);
/* See if we can return a cached jwt. */
grpc_credentials_md_store *jwt_md = NULL;
{
- gpr_mu_lock (&c->cache_mu);
- if (c->cached.service_url != NULL && strcmp (c->cached.service_url, service_url) == 0 && c->cached.jwt_md != NULL && (gpr_time_cmp (gpr_time_sub (c->cached.jwt_expiration, gpr_now (GPR_CLOCK_REALTIME)), refresh_threshold) > 0))
- {
- jwt_md = grpc_credentials_md_store_ref (c->cached.jwt_md);
- }
- gpr_mu_unlock (&c->cache_mu);
+ gpr_mu_lock(&c->cache_mu);
+ if (c->cached.service_url != NULL &&
+ strcmp(c->cached.service_url, service_url) == 0 &&
+ c->cached.jwt_md != NULL &&
+ (gpr_time_cmp(gpr_time_sub(c->cached.jwt_expiration,
+ gpr_now(GPR_CLOCK_REALTIME)),
+ refresh_threshold) > 0)) {
+ jwt_md = grpc_credentials_md_store_ref(c->cached.jwt_md);
+ }
+ gpr_mu_unlock(&c->cache_mu);
}
- if (jwt_md == NULL)
- {
- char *jwt = NULL;
- /* Generate a new jwt. */
- gpr_mu_lock (&c->cache_mu);
- jwt_reset_cache (c);
- jwt = grpc_jwt_encode_and_sign (&c->key, service_url, c->jwt_lifetime, NULL);
- if (jwt != NULL)
- {
- char *md_value;
- gpr_asprintf (&md_value, "Bearer %s", jwt);
- gpr_free (jwt);
- c->cached.jwt_expiration = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), c->jwt_lifetime);
- c->cached.service_url = gpr_strdup (service_url);
- c->cached.jwt_md = grpc_credentials_md_store_create (1);
- grpc_credentials_md_store_add_cstrings (c->cached.jwt_md, GRPC_AUTHORIZATION_METADATA_KEY, md_value);
- gpr_free (md_value);
- jwt_md = grpc_credentials_md_store_ref (c->cached.jwt_md);
- }
- gpr_mu_unlock (&c->cache_mu);
+ if (jwt_md == NULL) {
+ char *jwt = NULL;
+ /* Generate a new jwt. */
+ gpr_mu_lock(&c->cache_mu);
+ jwt_reset_cache(c);
+ jwt = grpc_jwt_encode_and_sign(&c->key, service_url, c->jwt_lifetime, NULL);
+ if (jwt != NULL) {
+ char *md_value;
+ gpr_asprintf(&md_value, "Bearer %s", jwt);
+ gpr_free(jwt);
+ c->cached.jwt_expiration =
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), c->jwt_lifetime);
+ c->cached.service_url = gpr_strdup(service_url);
+ c->cached.jwt_md = grpc_credentials_md_store_create(1);
+ grpc_credentials_md_store_add_cstrings(
+ c->cached.jwt_md, GRPC_AUTHORIZATION_METADATA_KEY, md_value);
+ gpr_free(md_value);
+ jwt_md = grpc_credentials_md_store_ref(c->cached.jwt_md);
}
+ gpr_mu_unlock(&c->cache_mu);
+ }
- if (jwt_md != NULL)
- {
- cb (exec_ctx, user_data, jwt_md->entries, jwt_md->num_entries, GRPC_CREDENTIALS_OK);
- grpc_credentials_md_store_unref (jwt_md);
- }
- else
- {
- cb (exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_ERROR);
- }
+ if (jwt_md != NULL) {
+ cb(exec_ctx, user_data, jwt_md->entries, jwt_md->num_entries,
+ GRPC_CREDENTIALS_OK);
+ grpc_credentials_md_store_unref(jwt_md);
+ } else {
+ cb(exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_ERROR);
+ }
}
static grpc_credentials_vtable jwt_vtable = {
- jwt_destruct, jwt_has_request_metadata, jwt_has_request_metadata_only,
- jwt_get_request_metadata, NULL
-};
+ jwt_destruct, jwt_has_request_metadata, jwt_has_request_metadata_only,
+ jwt_get_request_metadata, NULL};
grpc_credentials *
-grpc_service_account_jwt_access_credentials_create_from_auth_json_key (grpc_auth_json_key key, gpr_timespec token_lifetime)
-{
+grpc_service_account_jwt_access_credentials_create_from_auth_json_key(
+ grpc_auth_json_key key, gpr_timespec token_lifetime) {
grpc_service_account_jwt_access_credentials *c;
- if (!grpc_auth_json_key_is_valid (&key))
- {
- gpr_log (GPR_ERROR, "Invalid input for jwt credentials creation");
- return NULL;
- }
- c = gpr_malloc (sizeof (grpc_service_account_jwt_access_credentials));
- memset (c, 0, sizeof (grpc_service_account_jwt_access_credentials));
+ if (!grpc_auth_json_key_is_valid(&key)) {
+ gpr_log(GPR_ERROR, "Invalid input for jwt credentials creation");
+ return NULL;
+ }
+ c = gpr_malloc(sizeof(grpc_service_account_jwt_access_credentials));
+ memset(c, 0, sizeof(grpc_service_account_jwt_access_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_JWT;
- gpr_ref_init (&c->base.refcount, 1);
+ gpr_ref_init(&c->base.refcount, 1);
c->base.vtable = &jwt_vtable;
c->key = key;
c->jwt_lifetime = token_lifetime;
- gpr_mu_init (&c->cache_mu);
- jwt_reset_cache (c);
+ gpr_mu_init(&c->cache_mu);
+ jwt_reset_cache(c);
return &c->base;
}
-grpc_credentials *
-grpc_service_account_jwt_access_credentials_create (const char *json_key, gpr_timespec token_lifetime, void *reserved)
-{
- GPR_ASSERT (reserved == NULL);
- return grpc_service_account_jwt_access_credentials_create_from_auth_json_key (grpc_auth_json_key_create_from_string (json_key), token_lifetime);
+grpc_credentials *grpc_service_account_jwt_access_credentials_create(
+ const char *json_key, gpr_timespec token_lifetime, void *reserved) {
+ GPR_ASSERT(reserved == NULL);
+ return grpc_service_account_jwt_access_credentials_create_from_auth_json_key(
+ grpc_auth_json_key_create_from_string(json_key), token_lifetime);
}
/* -- Oauth2TokenFetcher credentials -- */
-static void
-oauth2_token_fetcher_destruct (grpc_credentials * creds)
-{
- grpc_oauth2_token_fetcher_credentials *c = (grpc_oauth2_token_fetcher_credentials *) creds;
- grpc_credentials_md_store_unref (c->access_token_md);
- gpr_mu_destroy (&c->mu);
- grpc_httpcli_context_destroy (&c->httpcli_context);
+static void oauth2_token_fetcher_destruct(grpc_credentials *creds) {
+ grpc_oauth2_token_fetcher_credentials *c =
+ (grpc_oauth2_token_fetcher_credentials *)creds;
+ grpc_credentials_md_store_unref(c->access_token_md);
+ gpr_mu_destroy(&c->mu);
+ grpc_httpcli_context_destroy(&c->httpcli_context);
}
-static int
-oauth2_token_fetcher_has_request_metadata (const grpc_credentials * creds)
-{
+static int oauth2_token_fetcher_has_request_metadata(
+ const grpc_credentials *creds) {
return 1;
}
-static int
-oauth2_token_fetcher_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int oauth2_token_fetcher_has_request_metadata_only(
+ const grpc_credentials *creds) {
return 1;
}
grpc_credentials_status
-grpc_oauth2_token_fetcher_credentials_parse_server_response (const grpc_httpcli_response * response, grpc_credentials_md_store ** token_md, gpr_timespec * token_lifetime)
-{
+grpc_oauth2_token_fetcher_credentials_parse_server_response(
+ const grpc_httpcli_response *response, grpc_credentials_md_store **token_md,
+ gpr_timespec *token_lifetime) {
char *null_terminated_body = NULL;
char *new_access_token = NULL;
grpc_credentials_status status = GRPC_CREDENTIALS_OK;
grpc_json *json = NULL;
- if (response == NULL)
- {
- gpr_log (GPR_ERROR, "Received NULL response.");
+ if (response == NULL) {
+ gpr_log(GPR_ERROR, "Received NULL response.");
+ status = GRPC_CREDENTIALS_ERROR;
+ goto end;
+ }
+
+ if (response->body_length > 0) {
+ null_terminated_body = gpr_malloc(response->body_length + 1);
+ null_terminated_body[response->body_length] = '\0';
+ memcpy(null_terminated_body, response->body, response->body_length);
+ }
+
+ if (response->status != 200) {
+ gpr_log(GPR_ERROR, "Call to http server ended with error %d [%s].",
+ response->status,
+ null_terminated_body != NULL ? null_terminated_body : "");
+ status = GRPC_CREDENTIALS_ERROR;
+ goto end;
+ } else {
+ grpc_json *access_token = NULL;
+ grpc_json *token_type = NULL;
+ grpc_json *expires_in = NULL;
+ grpc_json *ptr;
+ json = grpc_json_parse_string(null_terminated_body);
+ if (json == NULL) {
+ gpr_log(GPR_ERROR, "Could not parse JSON from %s", null_terminated_body);
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
-
- if (response->body_length > 0)
- {
- null_terminated_body = gpr_malloc (response->body_length + 1);
- null_terminated_body[response->body_length] = '\0';
- memcpy (null_terminated_body, response->body, response->body_length);
+ if (json->type != GRPC_JSON_OBJECT) {
+ gpr_log(GPR_ERROR, "Response should be a JSON object");
+ status = GRPC_CREDENTIALS_ERROR;
+ goto end;
}
-
- if (response->status != 200)
- {
- gpr_log (GPR_ERROR, "Call to http server ended with error %d [%s].", response->status, null_terminated_body != NULL ? null_terminated_body : "");
+ for (ptr = json->child; ptr; ptr = ptr->next) {
+ if (strcmp(ptr->key, "access_token") == 0) {
+ access_token = ptr;
+ } else if (strcmp(ptr->key, "token_type") == 0) {
+ token_type = ptr;
+ } else if (strcmp(ptr->key, "expires_in") == 0) {
+ expires_in = ptr;
+ }
+ }
+ if (access_token == NULL || access_token->type != GRPC_JSON_STRING) {
+ gpr_log(GPR_ERROR, "Missing or invalid access_token in JSON.");
+ status = GRPC_CREDENTIALS_ERROR;
+ goto end;
+ }
+ if (token_type == NULL || token_type->type != GRPC_JSON_STRING) {
+ gpr_log(GPR_ERROR, "Missing or invalid token_type in JSON.");
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
- else
- {
- grpc_json *access_token = NULL;
- grpc_json *token_type = NULL;
- grpc_json *expires_in = NULL;
- grpc_json *ptr;
- json = grpc_json_parse_string (null_terminated_body);
- if (json == NULL)
- {
- gpr_log (GPR_ERROR, "Could not parse JSON from %s", null_terminated_body);
- status = GRPC_CREDENTIALS_ERROR;
- goto end;
- }
- if (json->type != GRPC_JSON_OBJECT)
- {
- gpr_log (GPR_ERROR, "Response should be a JSON object");
- status = GRPC_CREDENTIALS_ERROR;
- goto end;
- }
- for (ptr = json->child; ptr; ptr = ptr->next)
- {
- if (strcmp (ptr->key, "access_token") == 0)
- {
- access_token = ptr;
- }
- else if (strcmp (ptr->key, "token_type") == 0)
- {
- token_type = ptr;
- }
- else if (strcmp (ptr->key, "expires_in") == 0)
- {
- expires_in = ptr;
- }
- }
- if (access_token == NULL || access_token->type != GRPC_JSON_STRING)
- {
- gpr_log (GPR_ERROR, "Missing or invalid access_token in JSON.");
- status = GRPC_CREDENTIALS_ERROR;
- goto end;
- }
- if (token_type == NULL || token_type->type != GRPC_JSON_STRING)
- {
- gpr_log (GPR_ERROR, "Missing or invalid token_type in JSON.");
- status = GRPC_CREDENTIALS_ERROR;
- goto end;
- }
- if (expires_in == NULL || expires_in->type != GRPC_JSON_NUMBER)
- {
- gpr_log (GPR_ERROR, "Missing or invalid expires_in in JSON.");
- status = GRPC_CREDENTIALS_ERROR;
- goto end;
- }
- gpr_asprintf (&new_access_token, "%s %s", token_type->value, access_token->value);
- token_lifetime->tv_sec = strtol (expires_in->value, NULL, 10);
- token_lifetime->tv_nsec = 0;
- token_lifetime->clock_type = GPR_TIMESPAN;
- if (*token_md != NULL)
- grpc_credentials_md_store_unref (*token_md);
- *token_md = grpc_credentials_md_store_create (1);
- grpc_credentials_md_store_add_cstrings (*token_md, GRPC_AUTHORIZATION_METADATA_KEY, new_access_token);
- status = GRPC_CREDENTIALS_OK;
+ if (expires_in == NULL || expires_in->type != GRPC_JSON_NUMBER) {
+ gpr_log(GPR_ERROR, "Missing or invalid expires_in in JSON.");
+ status = GRPC_CREDENTIALS_ERROR;
+ goto end;
}
+ gpr_asprintf(&new_access_token, "%s %s", token_type->value,
+ access_token->value);
+ token_lifetime->tv_sec = strtol(expires_in->value, NULL, 10);
+ token_lifetime->tv_nsec = 0;
+ token_lifetime->clock_type = GPR_TIMESPAN;
+ if (*token_md != NULL) grpc_credentials_md_store_unref(*token_md);
+ *token_md = grpc_credentials_md_store_create(1);
+ grpc_credentials_md_store_add_cstrings(
+ *token_md, GRPC_AUTHORIZATION_METADATA_KEY, new_access_token);
+ status = GRPC_CREDENTIALS_OK;
+ }
end:
- if (status != GRPC_CREDENTIALS_OK && (*token_md != NULL))
- {
- grpc_credentials_md_store_unref (*token_md);
- *token_md = NULL;
- }
- if (null_terminated_body != NULL)
- gpr_free (null_terminated_body);
- if (new_access_token != NULL)
- gpr_free (new_access_token);
- if (json != NULL)
- grpc_json_destroy (json);
+ if (status != GRPC_CREDENTIALS_OK && (*token_md != NULL)) {
+ grpc_credentials_md_store_unref(*token_md);
+ *token_md = NULL;
+ }
+ if (null_terminated_body != NULL) gpr_free(null_terminated_body);
+ if (new_access_token != NULL) gpr_free(new_access_token);
+ if (json != NULL) grpc_json_destroy(json);
return status;
}
-static void
-on_oauth2_token_fetcher_http_response (grpc_exec_ctx * exec_ctx, void *user_data, const grpc_httpcli_response * response)
-{
- grpc_credentials_metadata_request *r = (grpc_credentials_metadata_request *) user_data;
- grpc_oauth2_token_fetcher_credentials *c = (grpc_oauth2_token_fetcher_credentials *) r->creds;
+static void on_oauth2_token_fetcher_http_response(
+ grpc_exec_ctx *exec_ctx, void *user_data,
+ const grpc_httpcli_response *response) {
+ grpc_credentials_metadata_request *r =
+ (grpc_credentials_metadata_request *)user_data;
+ grpc_oauth2_token_fetcher_credentials *c =
+ (grpc_oauth2_token_fetcher_credentials *)r->creds;
gpr_timespec token_lifetime;
grpc_credentials_status status;
- gpr_mu_lock (&c->mu);
- status = grpc_oauth2_token_fetcher_credentials_parse_server_response (response, &c->access_token_md, &token_lifetime);
- if (status == GRPC_CREDENTIALS_OK)
- {
- c->token_expiration = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), token_lifetime);
- r->cb (exec_ctx, r->user_data, c->access_token_md->entries, c->access_token_md->num_entries, status);
- }
- else
- {
- c->token_expiration = gpr_inf_past (GPR_CLOCK_REALTIME);
- r->cb (exec_ctx, r->user_data, NULL, 0, status);
- }
- gpr_mu_unlock (&c->mu);
- grpc_credentials_metadata_request_destroy (r);
+ gpr_mu_lock(&c->mu);
+ status = grpc_oauth2_token_fetcher_credentials_parse_server_response(
+ response, &c->access_token_md, &token_lifetime);
+ if (status == GRPC_CREDENTIALS_OK) {
+ c->token_expiration =
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), token_lifetime);
+ r->cb(exec_ctx, r->user_data, c->access_token_md->entries,
+ c->access_token_md->num_entries, status);
+ } else {
+ c->token_expiration = gpr_inf_past(GPR_CLOCK_REALTIME);
+ r->cb(exec_ctx, r->user_data, NULL, 0, status);
+ }
+ gpr_mu_unlock(&c->mu);
+ grpc_credentials_metadata_request_destroy(r);
}
-static void
-oauth2_token_fetcher_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_oauth2_token_fetcher_credentials *c = (grpc_oauth2_token_fetcher_credentials *) creds;
- gpr_timespec refresh_threshold = gpr_time_from_seconds (GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS, GPR_TIMESPAN);
+static void oauth2_token_fetcher_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_oauth2_token_fetcher_credentials *c =
+ (grpc_oauth2_token_fetcher_credentials *)creds;
+ gpr_timespec refresh_threshold = gpr_time_from_seconds(
+ GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS, GPR_TIMESPAN);
grpc_credentials_md_store *cached_access_token_md = NULL;
{
- gpr_mu_lock (&c->mu);
- if (c->access_token_md != NULL && (gpr_time_cmp (gpr_time_sub (c->token_expiration, gpr_now (GPR_CLOCK_REALTIME)), refresh_threshold) > 0))
- {
- cached_access_token_md = grpc_credentials_md_store_ref (c->access_token_md);
- }
- gpr_mu_unlock (&c->mu);
- }
- if (cached_access_token_md != NULL)
- {
- cb (exec_ctx, user_data, cached_access_token_md->entries, cached_access_token_md->num_entries, GRPC_CREDENTIALS_OK);
- grpc_credentials_md_store_unref (cached_access_token_md);
- }
- else
- {
- c->fetch_func (exec_ctx, grpc_credentials_metadata_request_create (creds, cb, user_data), &c->httpcli_context, pollset, on_oauth2_token_fetcher_http_response, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), refresh_threshold));
+ gpr_mu_lock(&c->mu);
+ if (c->access_token_md != NULL &&
+ (gpr_time_cmp(
+ gpr_time_sub(c->token_expiration, gpr_now(GPR_CLOCK_REALTIME)),
+ refresh_threshold) > 0)) {
+ cached_access_token_md =
+ grpc_credentials_md_store_ref(c->access_token_md);
}
+ gpr_mu_unlock(&c->mu);
+ }
+ if (cached_access_token_md != NULL) {
+ cb(exec_ctx, user_data, cached_access_token_md->entries,
+ cached_access_token_md->num_entries, GRPC_CREDENTIALS_OK);
+ grpc_credentials_md_store_unref(cached_access_token_md);
+ } else {
+ c->fetch_func(
+ exec_ctx,
+ grpc_credentials_metadata_request_create(creds, cb, user_data),
+ &c->httpcli_context, pollset, on_oauth2_token_fetcher_http_response,
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), refresh_threshold));
+ }
}
-static void
-init_oauth2_token_fetcher (grpc_oauth2_token_fetcher_credentials * c, grpc_fetch_oauth2_func fetch_func)
-{
- memset (c, 0, sizeof (grpc_oauth2_token_fetcher_credentials));
+static void init_oauth2_token_fetcher(grpc_oauth2_token_fetcher_credentials *c,
+ grpc_fetch_oauth2_func fetch_func) {
+ memset(c, 0, sizeof(grpc_oauth2_token_fetcher_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2;
- gpr_ref_init (&c->base.refcount, 1);
- gpr_mu_init (&c->mu);
- c->token_expiration = gpr_inf_past (GPR_CLOCK_REALTIME);
+ gpr_ref_init(&c->base.refcount, 1);
+ gpr_mu_init(&c->mu);
+ c->token_expiration = gpr_inf_past(GPR_CLOCK_REALTIME);
c->fetch_func = fetch_func;
- grpc_httpcli_context_init (&c->httpcli_context);
+ grpc_httpcli_context_init(&c->httpcli_context);
}
/* -- GoogleComputeEngine credentials. -- */
static grpc_credentials_vtable compute_engine_vtable = {
- oauth2_token_fetcher_destruct, oauth2_token_fetcher_has_request_metadata,
- oauth2_token_fetcher_has_request_metadata_only,
- oauth2_token_fetcher_get_request_metadata, NULL
-};
-
-static void
-compute_engine_fetch_oauth2 (grpc_exec_ctx * exec_ctx, grpc_credentials_metadata_request * metadata_req, grpc_httpcli_context * httpcli_context, grpc_pollset * pollset, grpc_httpcli_response_cb response_cb, gpr_timespec deadline)
-{
- grpc_httpcli_header header = { "Metadata-Flavor", "Google" };
+ oauth2_token_fetcher_destruct, oauth2_token_fetcher_has_request_metadata,
+ oauth2_token_fetcher_has_request_metadata_only,
+ oauth2_token_fetcher_get_request_metadata, NULL};
+
+static void compute_engine_fetch_oauth2(
+ grpc_exec_ctx *exec_ctx, grpc_credentials_metadata_request *metadata_req,
+ grpc_httpcli_context *httpcli_context, grpc_pollset *pollset,
+ grpc_httpcli_response_cb response_cb, gpr_timespec deadline) {
+ grpc_httpcli_header header = {"Metadata-Flavor", "Google"};
grpc_httpcli_request request;
- memset (&request, 0, sizeof (grpc_httpcli_request));
+ memset(&request, 0, sizeof(grpc_httpcli_request));
request.host = GRPC_COMPUTE_ENGINE_METADATA_HOST;
request.path = GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH;
request.hdr_count = 1;
request.hdrs = &header;
- grpc_httpcli_get (exec_ctx, httpcli_context, pollset, &request, deadline, response_cb, metadata_req);
+ grpc_httpcli_get(exec_ctx, httpcli_context, pollset, &request, deadline,
+ response_cb, metadata_req);
}
-grpc_credentials *
-grpc_google_compute_engine_credentials_create (void *reserved)
-{
- grpc_oauth2_token_fetcher_credentials *c = gpr_malloc (sizeof (grpc_oauth2_token_fetcher_credentials));
- GPR_ASSERT (reserved == NULL);
- init_oauth2_token_fetcher (c, compute_engine_fetch_oauth2);
+grpc_credentials *grpc_google_compute_engine_credentials_create(
+ void *reserved) {
+ grpc_oauth2_token_fetcher_credentials *c =
+ gpr_malloc(sizeof(grpc_oauth2_token_fetcher_credentials));
+ GPR_ASSERT(reserved == NULL);
+ init_oauth2_token_fetcher(c, compute_engine_fetch_oauth2);
c->base.vtable = &compute_engine_vtable;
return &c->base;
}
/* -- GoogleRefreshToken credentials. -- */
-static void
-refresh_token_destruct (grpc_credentials * creds)
-{
- grpc_google_refresh_token_credentials *c = (grpc_google_refresh_token_credentials *) creds;
- grpc_auth_refresh_token_destruct (&c->refresh_token);
- oauth2_token_fetcher_destruct (&c->base.base);
+static void refresh_token_destruct(grpc_credentials *creds) {
+ grpc_google_refresh_token_credentials *c =
+ (grpc_google_refresh_token_credentials *)creds;
+ grpc_auth_refresh_token_destruct(&c->refresh_token);
+ oauth2_token_fetcher_destruct(&c->base.base);
}
static grpc_credentials_vtable refresh_token_vtable = {
- refresh_token_destruct, oauth2_token_fetcher_has_request_metadata,
- oauth2_token_fetcher_has_request_metadata_only,
- oauth2_token_fetcher_get_request_metadata, NULL
-};
-
-static void
-refresh_token_fetch_oauth2 (grpc_exec_ctx * exec_ctx, grpc_credentials_metadata_request * metadata_req, grpc_httpcli_context * httpcli_context, grpc_pollset * pollset, grpc_httpcli_response_cb response_cb, gpr_timespec deadline)
-{
- grpc_google_refresh_token_credentials *c = (grpc_google_refresh_token_credentials *) metadata_req->creds;
- grpc_httpcli_header header = { "Content-Type",
- "application/x-www-form-urlencoded"
- };
+ refresh_token_destruct, oauth2_token_fetcher_has_request_metadata,
+ oauth2_token_fetcher_has_request_metadata_only,
+ oauth2_token_fetcher_get_request_metadata, NULL};
+
+static void refresh_token_fetch_oauth2(
+ grpc_exec_ctx *exec_ctx, grpc_credentials_metadata_request *metadata_req,
+ grpc_httpcli_context *httpcli_context, grpc_pollset *pollset,
+ grpc_httpcli_response_cb response_cb, gpr_timespec deadline) {
+ grpc_google_refresh_token_credentials *c =
+ (grpc_google_refresh_token_credentials *)metadata_req->creds;
+ grpc_httpcli_header header = {"Content-Type",
+ "application/x-www-form-urlencoded"};
grpc_httpcli_request request;
char *body = NULL;
- gpr_asprintf (&body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING, c->refresh_token.client_id, c->refresh_token.client_secret, c->refresh_token.refresh_token);
- memset (&request, 0, sizeof (grpc_httpcli_request));
+ gpr_asprintf(&body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING,
+ c->refresh_token.client_id, c->refresh_token.client_secret,
+ c->refresh_token.refresh_token);
+ memset(&request, 0, sizeof(grpc_httpcli_request));
request.host = GRPC_GOOGLE_OAUTH2_SERVICE_HOST;
request.path = GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH;
request.hdr_count = 1;
request.hdrs = &header;
request.handshaker = &grpc_httpcli_ssl;
- grpc_httpcli_post (exec_ctx,httpcli_context, pollset, &request, body, strlen ( body), deadline, response_cb, metadata_req);
- gpr_free (body);
+ grpc_httpcli_post(exec_ctx, httpcli_context, pollset, &request, body,
+ strlen(body), deadline, response_cb, metadata_req);
+ gpr_free(body);
}
-grpc_credentials *
-grpc_refresh_token_credentials_create_from_auth_refresh_token (grpc_auth_refresh_token refresh_token)
-{
+grpc_credentials *grpc_refresh_token_credentials_create_from_auth_refresh_token(
+ grpc_auth_refresh_token refresh_token) {
grpc_google_refresh_token_credentials *c;
- if (!grpc_auth_refresh_token_is_valid (&refresh_token))
- {
- gpr_log (GPR_ERROR, "Invalid input for refresh token credentials creation");
- return NULL;
- }
- c = gpr_malloc (sizeof (grpc_google_refresh_token_credentials));
- memset (c, 0, sizeof (grpc_google_refresh_token_credentials));
- init_oauth2_token_fetcher (&c->base, refresh_token_fetch_oauth2);
+ if (!grpc_auth_refresh_token_is_valid(&refresh_token)) {
+ gpr_log(GPR_ERROR, "Invalid input for refresh token credentials creation");
+ return NULL;
+ }
+ c = gpr_malloc(sizeof(grpc_google_refresh_token_credentials));
+ memset(c, 0, sizeof(grpc_google_refresh_token_credentials));
+ init_oauth2_token_fetcher(&c->base, refresh_token_fetch_oauth2);
c->base.base.vtable = &refresh_token_vtable;
c->refresh_token = refresh_token;
return &c->base.base;
}
-grpc_credentials *
-grpc_google_refresh_token_credentials_create (const char *json_refresh_token, void *reserved)
-{
- GPR_ASSERT (reserved == NULL);
- return grpc_refresh_token_credentials_create_from_auth_refresh_token (grpc_auth_refresh_token_create_from_string (json_refresh_token));
+grpc_credentials *grpc_google_refresh_token_credentials_create(
+ const char *json_refresh_token, void *reserved) {
+ GPR_ASSERT(reserved == NULL);
+ return grpc_refresh_token_credentials_create_from_auth_refresh_token(
+ grpc_auth_refresh_token_create_from_string(json_refresh_token));
}
/* -- Metadata-only credentials. -- */
-static void
-md_only_test_destruct (grpc_credentials * creds)
-{
- grpc_md_only_test_credentials *c = (grpc_md_only_test_credentials *) creds;
- grpc_credentials_md_store_unref (c->md_store);
+static void md_only_test_destruct(grpc_credentials *creds) {
+ grpc_md_only_test_credentials *c = (grpc_md_only_test_credentials *)creds;
+ grpc_credentials_md_store_unref(c->md_store);
}
-static int
-md_only_test_has_request_metadata (const grpc_credentials * creds)
-{
+static int md_only_test_has_request_metadata(const grpc_credentials *creds) {
return 1;
}
-static int
-md_only_test_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int md_only_test_has_request_metadata_only(
+ const grpc_credentials *creds) {
return 1;
}
-static void
-on_simulated_token_fetch_done (void *user_data)
-{
- grpc_credentials_metadata_request *r = (grpc_credentials_metadata_request *) user_data;
- grpc_md_only_test_credentials *c = (grpc_md_only_test_credentials *) r->creds;
+static void on_simulated_token_fetch_done(void *user_data) {
+ grpc_credentials_metadata_request *r =
+ (grpc_credentials_metadata_request *)user_data;
+ grpc_md_only_test_credentials *c = (grpc_md_only_test_credentials *)r->creds;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- r->cb (&exec_ctx, r->user_data, c->md_store->entries, c->md_store->num_entries, GRPC_CREDENTIALS_OK);
- grpc_credentials_metadata_request_destroy (r);
- grpc_exec_ctx_finish (&exec_ctx);
-}
-
-static void
-md_only_test_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_md_only_test_credentials *c = (grpc_md_only_test_credentials *) creds;
-
- if (c->is_async)
- {
- gpr_thd_id thd_id;
- grpc_credentials_metadata_request *cb_arg = grpc_credentials_metadata_request_create (creds, cb, user_data);
- gpr_thd_new (&thd_id, on_simulated_token_fetch_done, cb_arg, NULL);
- }
- else
- {
- cb (exec_ctx, user_data, c->md_store->entries, 1, GRPC_CREDENTIALS_OK);
- }
+ r->cb(&exec_ctx, r->user_data, c->md_store->entries, c->md_store->num_entries,
+ GRPC_CREDENTIALS_OK);
+ grpc_credentials_metadata_request_destroy(r);
+ grpc_exec_ctx_finish(&exec_ctx);
+}
+
+static void md_only_test_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_md_only_test_credentials *c = (grpc_md_only_test_credentials *)creds;
+
+ if (c->is_async) {
+ gpr_thd_id thd_id;
+ grpc_credentials_metadata_request *cb_arg =
+ grpc_credentials_metadata_request_create(creds, cb, user_data);
+ gpr_thd_new(&thd_id, on_simulated_token_fetch_done, cb_arg, NULL);
+ } else {
+ cb(exec_ctx, user_data, c->md_store->entries, 1, GRPC_CREDENTIALS_OK);
+ }
}
static grpc_credentials_vtable md_only_test_vtable = {
- md_only_test_destruct, md_only_test_has_request_metadata,
- md_only_test_has_request_metadata_only, md_only_test_get_request_metadata,
- NULL
-};
-
-grpc_credentials *
-grpc_md_only_test_credentials_create (const char *md_key, const char *md_value, int is_async)
-{
- grpc_md_only_test_credentials *c = gpr_malloc (sizeof (grpc_md_only_test_credentials));
- memset (c, 0, sizeof (grpc_md_only_test_credentials));
+ md_only_test_destruct, md_only_test_has_request_metadata,
+ md_only_test_has_request_metadata_only, md_only_test_get_request_metadata,
+ NULL};
+
+grpc_credentials *grpc_md_only_test_credentials_create(const char *md_key,
+ const char *md_value,
+ int is_async) {
+ grpc_md_only_test_credentials *c =
+ gpr_malloc(sizeof(grpc_md_only_test_credentials));
+ memset(c, 0, sizeof(grpc_md_only_test_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2;
c->base.vtable = &md_only_test_vtable;
- gpr_ref_init (&c->base.refcount, 1);
- c->md_store = grpc_credentials_md_store_create (1);
- grpc_credentials_md_store_add_cstrings (c->md_store, md_key, md_value);
+ gpr_ref_init(&c->base.refcount, 1);
+ c->md_store = grpc_credentials_md_store_create(1);
+ grpc_credentials_md_store_add_cstrings(c->md_store, md_key, md_value);
c->is_async = is_async;
return &c->base;
}
/* -- Oauth2 Access Token credentials. -- */
-static void
-access_token_destruct (grpc_credentials * creds)
-{
- grpc_access_token_credentials *c = (grpc_access_token_credentials *) creds;
- grpc_credentials_md_store_unref (c->access_token_md);
+static void access_token_destruct(grpc_credentials *creds) {
+ grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds;
+ grpc_credentials_md_store_unref(c->access_token_md);
}
-static int
-access_token_has_request_metadata (const grpc_credentials * creds)
-{
+static int access_token_has_request_metadata(const grpc_credentials *creds) {
return 1;
}
-static int
-access_token_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int access_token_has_request_metadata_only(
+ const grpc_credentials *creds) {
return 1;
}
-static void
-access_token_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_access_token_credentials *c = (grpc_access_token_credentials *) creds;
- cb (exec_ctx, user_data, c->access_token_md->entries, 1, GRPC_CREDENTIALS_OK);
+static void access_token_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds;
+ cb(exec_ctx, user_data, c->access_token_md->entries, 1, GRPC_CREDENTIALS_OK);
}
static grpc_credentials_vtable access_token_vtable = {
- access_token_destruct, access_token_has_request_metadata,
- access_token_has_request_metadata_only, access_token_get_request_metadata,
- NULL
-};
-
-grpc_credentials *
-grpc_access_token_credentials_create (const char *access_token, void *reserved)
-{
- grpc_access_token_credentials *c = gpr_malloc (sizeof (grpc_access_token_credentials));
+ access_token_destruct, access_token_has_request_metadata,
+ access_token_has_request_metadata_only, access_token_get_request_metadata,
+ NULL};
+
+grpc_credentials *grpc_access_token_credentials_create(const char *access_token,
+ void *reserved) {
+ grpc_access_token_credentials *c =
+ gpr_malloc(sizeof(grpc_access_token_credentials));
char *token_md_value;
- GPR_ASSERT (reserved == NULL);
- memset (c, 0, sizeof (grpc_access_token_credentials));
+ GPR_ASSERT(reserved == NULL);
+ memset(c, 0, sizeof(grpc_access_token_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2;
c->base.vtable = &access_token_vtable;
- gpr_ref_init (&c->base.refcount, 1);
- c->access_token_md = grpc_credentials_md_store_create (1);
- gpr_asprintf (&token_md_value, "Bearer %s", access_token);
- grpc_credentials_md_store_add_cstrings (c->access_token_md, GRPC_AUTHORIZATION_METADATA_KEY, token_md_value);
- gpr_free (token_md_value);
+ gpr_ref_init(&c->base.refcount, 1);
+ c->access_token_md = grpc_credentials_md_store_create(1);
+ gpr_asprintf(&token_md_value, "Bearer %s", access_token);
+ grpc_credentials_md_store_add_cstrings(
+ c->access_token_md, GRPC_AUTHORIZATION_METADATA_KEY, token_md_value);
+ gpr_free(token_md_value);
return &c->base;
}
/* -- Fake transport security credentials. -- */
-static void
-fake_transport_security_credentials_destruct (grpc_credentials * creds)
-{
+static void fake_transport_security_credentials_destruct(
+ grpc_credentials *creds) {
/* Nothing to do here. */
}
-static void
-fake_transport_security_server_credentials_destruct (grpc_server_credentials * creds)
-{
+static void fake_transport_security_server_credentials_destruct(
+ grpc_server_credentials *creds) {
/* Nothing to do here. */
}
-static int
-fake_transport_security_has_request_metadata (const grpc_credentials * creds)
-{
+static int fake_transport_security_has_request_metadata(
+ const grpc_credentials *creds) {
return 0;
}
-static int
-fake_transport_security_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int fake_transport_security_has_request_metadata_only(
+ const grpc_credentials *creds) {
return 0;
}
-static grpc_security_status
-fake_transport_security_create_security_connector (grpc_credentials * c, const char *target, const grpc_channel_args * args, grpc_credentials * request_metadata_creds, grpc_channel_security_connector ** sc, grpc_channel_args ** new_args)
-{
- *sc = grpc_fake_channel_security_connector_create (request_metadata_creds, 1);
+static grpc_security_status fake_transport_security_create_security_connector(
+ grpc_credentials *c, const char *target, const grpc_channel_args *args,
+ grpc_credentials *request_metadata_creds,
+ grpc_channel_security_connector **sc, grpc_channel_args **new_args) {
+ *sc = grpc_fake_channel_security_connector_create(request_metadata_creds, 1);
return GRPC_SECURITY_OK;
}
static grpc_security_status
-fake_transport_security_server_create_security_connector (grpc_server_credentials * c, grpc_security_connector ** sc)
-{
- *sc = grpc_fake_server_security_connector_create ();
+fake_transport_security_server_create_security_connector(
+ grpc_server_credentials *c, grpc_security_connector **sc) {
+ *sc = grpc_fake_server_security_connector_create();
return GRPC_SECURITY_OK;
}
static grpc_credentials_vtable fake_transport_security_credentials_vtable = {
- fake_transport_security_credentials_destruct,
- fake_transport_security_has_request_metadata,
- fake_transport_security_has_request_metadata_only, NULL,
- fake_transport_security_create_security_connector
-};
-
-static grpc_server_credentials_vtable fake_transport_security_server_credentials_vtable = {
- fake_transport_security_server_credentials_destruct,
- fake_transport_security_server_create_security_connector
-};
-
-grpc_credentials *
-grpc_fake_transport_security_credentials_create (void)
-{
- grpc_credentials *c = gpr_malloc (sizeof (grpc_credentials));
- memset (c, 0, sizeof (grpc_credentials));
+ fake_transport_security_credentials_destruct,
+ fake_transport_security_has_request_metadata,
+ fake_transport_security_has_request_metadata_only, NULL,
+ fake_transport_security_create_security_connector};
+
+static grpc_server_credentials_vtable
+ fake_transport_security_server_credentials_vtable = {
+ fake_transport_security_server_credentials_destruct,
+ fake_transport_security_server_create_security_connector};
+
+grpc_credentials *grpc_fake_transport_security_credentials_create(void) {
+ grpc_credentials *c = gpr_malloc(sizeof(grpc_credentials));
+ memset(c, 0, sizeof(grpc_credentials));
c->type = GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY;
c->vtable = &fake_transport_security_credentials_vtable;
- gpr_ref_init (&c->refcount, 1);
+ gpr_ref_init(&c->refcount, 1);
return c;
}
-grpc_server_credentials *
-grpc_fake_transport_security_server_credentials_create (void)
-{
- grpc_server_credentials *c = gpr_malloc (sizeof (grpc_server_credentials));
- memset (c, 0, sizeof (grpc_server_credentials));
+grpc_server_credentials *grpc_fake_transport_security_server_credentials_create(
+ void) {
+ grpc_server_credentials *c = gpr_malloc(sizeof(grpc_server_credentials));
+ memset(c, 0, sizeof(grpc_server_credentials));
c->type = GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY;
- gpr_ref_init (&c->refcount, 1);
+ gpr_ref_init(&c->refcount, 1);
c->vtable = &fake_transport_security_server_credentials_vtable;
return c;
}
/* -- Composite credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_composite_credentials *composite_creds;
size_t creds_index;
grpc_credentials_md_store *md_elems;
@@ -990,406 +912,355 @@ typedef struct
grpc_credentials_metadata_cb cb;
} grpc_composite_credentials_metadata_context;
-static void
-composite_destruct (grpc_credentials * creds)
-{
- grpc_composite_credentials *c = (grpc_composite_credentials *) creds;
+static void composite_destruct(grpc_credentials *creds) {
+ grpc_composite_credentials *c = (grpc_composite_credentials *)creds;
size_t i;
- for (i = 0; i < c->inner.num_creds; i++)
- {
- grpc_credentials_unref (c->inner.creds_array[i]);
- }
- gpr_free (c->inner.creds_array);
+ for (i = 0; i < c->inner.num_creds; i++) {
+ grpc_credentials_unref(c->inner.creds_array[i]);
+ }
+ gpr_free(c->inner.creds_array);
}
-static int
-composite_has_request_metadata (const grpc_credentials * creds)
-{
- const grpc_composite_credentials *c = (const grpc_composite_credentials *) creds;
+static int composite_has_request_metadata(const grpc_credentials *creds) {
+ const grpc_composite_credentials *c =
+ (const grpc_composite_credentials *)creds;
size_t i;
- for (i = 0; i < c->inner.num_creds; i++)
- {
- if (grpc_credentials_has_request_metadata (c->inner.creds_array[i]))
- {
- return 1;
- }
+ for (i = 0; i < c->inner.num_creds; i++) {
+ if (grpc_credentials_has_request_metadata(c->inner.creds_array[i])) {
+ return 1;
}
+ }
return 0;
}
-static int
-composite_has_request_metadata_only (const grpc_credentials * creds)
-{
- const grpc_composite_credentials *c = (const grpc_composite_credentials *) creds;
+static int composite_has_request_metadata_only(const grpc_credentials *creds) {
+ const grpc_composite_credentials *c =
+ (const grpc_composite_credentials *)creds;
size_t i;
- for (i = 0; i < c->inner.num_creds; i++)
- {
- if (!grpc_credentials_has_request_metadata_only (c->inner.creds_array[i]))
- {
- return 0;
- }
+ for (i = 0; i < c->inner.num_creds; i++) {
+ if (!grpc_credentials_has_request_metadata_only(c->inner.creds_array[i])) {
+ return 0;
}
+ }
return 1;
}
-static void
-composite_md_context_destroy (grpc_composite_credentials_metadata_context * ctx)
-{
- grpc_credentials_md_store_unref (ctx->md_elems);
- if (ctx->service_url != NULL)
- gpr_free (ctx->service_url);
- gpr_free (ctx);
+static void composite_md_context_destroy(
+ grpc_composite_credentials_metadata_context *ctx) {
+ grpc_credentials_md_store_unref(ctx->md_elems);
+ if (ctx->service_url != NULL) gpr_free(ctx->service_url);
+ gpr_free(ctx);
}
-static void
-composite_metadata_cb (grpc_exec_ctx * exec_ctx, void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status)
-{
- grpc_composite_credentials_metadata_context *ctx = (grpc_composite_credentials_metadata_context *) user_data;
- if (status != GRPC_CREDENTIALS_OK)
- {
- ctx->cb (exec_ctx, ctx->user_data, NULL, 0, status);
- return;
- }
+static void composite_metadata_cb(grpc_exec_ctx *exec_ctx, void *user_data,
+ grpc_credentials_md *md_elems, size_t num_md,
+ grpc_credentials_status status) {
+ grpc_composite_credentials_metadata_context *ctx =
+ (grpc_composite_credentials_metadata_context *)user_data;
+ if (status != GRPC_CREDENTIALS_OK) {
+ ctx->cb(exec_ctx, ctx->user_data, NULL, 0, status);
+ return;
+ }
/* Copy the metadata in the context. */
- if (num_md > 0)
- {
- size_t i;
- for (i = 0; i < num_md; i++)
- {
- grpc_credentials_md_store_add (ctx->md_elems, md_elems[i].key, md_elems[i].value);
- }
+ if (num_md > 0) {
+ size_t i;
+ for (i = 0; i < num_md; i++) {
+ grpc_credentials_md_store_add(ctx->md_elems, md_elems[i].key,
+ md_elems[i].value);
}
+ }
/* See if we need to get some more metadata. */
- while (ctx->creds_index < ctx->composite_creds->inner.num_creds)
- {
- grpc_credentials *inner_creds = ctx->composite_creds->inner.creds_array[ctx->creds_index++];
- if (grpc_credentials_has_request_metadata (inner_creds))
- {
- grpc_credentials_get_request_metadata (exec_ctx, inner_creds, ctx->pollset, ctx->service_url, composite_metadata_cb, ctx);
- return;
- }
+ while (ctx->creds_index < ctx->composite_creds->inner.num_creds) {
+ grpc_credentials *inner_creds =
+ ctx->composite_creds->inner.creds_array[ctx->creds_index++];
+ if (grpc_credentials_has_request_metadata(inner_creds)) {
+ grpc_credentials_get_request_metadata(exec_ctx, inner_creds, ctx->pollset,
+ ctx->service_url,
+ composite_metadata_cb, ctx);
+ return;
}
+ }
/* We're done!. */
- ctx->cb (exec_ctx, ctx->user_data, ctx->md_elems->entries, ctx->md_elems->num_entries, GRPC_CREDENTIALS_OK);
- composite_md_context_destroy (ctx);
+ ctx->cb(exec_ctx, ctx->user_data, ctx->md_elems->entries,
+ ctx->md_elems->num_entries, GRPC_CREDENTIALS_OK);
+ composite_md_context_destroy(ctx);
}
-static void
-composite_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_composite_credentials *c = (grpc_composite_credentials *) creds;
+static void composite_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_composite_credentials *c = (grpc_composite_credentials *)creds;
grpc_composite_credentials_metadata_context *ctx;
- if (!grpc_credentials_has_request_metadata (creds))
- {
- cb (exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_OK);
- return;
- }
- ctx = gpr_malloc (sizeof (grpc_composite_credentials_metadata_context));
- memset (ctx, 0, sizeof (grpc_composite_credentials_metadata_context));
- ctx->service_url = gpr_strdup (service_url);
+ if (!grpc_credentials_has_request_metadata(creds)) {
+ cb(exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_OK);
+ return;
+ }
+ ctx = gpr_malloc(sizeof(grpc_composite_credentials_metadata_context));
+ memset(ctx, 0, sizeof(grpc_composite_credentials_metadata_context));
+ ctx->service_url = gpr_strdup(service_url);
ctx->user_data = user_data;
ctx->cb = cb;
ctx->composite_creds = c;
ctx->pollset = pollset;
- ctx->md_elems = grpc_credentials_md_store_create (c->inner.num_creds);
- while (ctx->creds_index < c->inner.num_creds)
- {
- grpc_credentials *inner_creds = c->inner.creds_array[ctx->creds_index++];
- if (grpc_credentials_has_request_metadata (inner_creds))
- {
- grpc_credentials_get_request_metadata (exec_ctx, inner_creds, pollset, service_url, composite_metadata_cb, ctx);
- return;
- }
- }
- GPR_ASSERT (0); /* Should have exited before. */
-}
-
-static grpc_security_status
-composite_create_security_connector (grpc_credentials * creds, const char *target, const grpc_channel_args * args, grpc_credentials * request_metadata_creds, grpc_channel_security_connector ** sc, grpc_channel_args ** new_args)
-{
- grpc_composite_credentials *c = (grpc_composite_credentials *) creds;
- if (c->connector_creds == NULL)
- {
- gpr_log (GPR_ERROR, "Cannot create security connector, missing connector credentials.");
- return GRPC_SECURITY_ERROR;
+ ctx->md_elems = grpc_credentials_md_store_create(c->inner.num_creds);
+ while (ctx->creds_index < c->inner.num_creds) {
+ grpc_credentials *inner_creds = c->inner.creds_array[ctx->creds_index++];
+ if (grpc_credentials_has_request_metadata(inner_creds)) {
+ grpc_credentials_get_request_metadata(exec_ctx, inner_creds, pollset,
+ service_url, composite_metadata_cb,
+ ctx);
+ return;
}
- return grpc_credentials_create_security_connector (c->connector_creds, target, args, creds, sc, new_args);
+ }
+ GPR_ASSERT(0); /* Should have exited before. */
+}
+
+static grpc_security_status composite_create_security_connector(
+ grpc_credentials *creds, const char *target, const grpc_channel_args *args,
+ grpc_credentials *request_metadata_creds,
+ grpc_channel_security_connector **sc, grpc_channel_args **new_args) {
+ grpc_composite_credentials *c = (grpc_composite_credentials *)creds;
+ if (c->connector_creds == NULL) {
+ gpr_log(GPR_ERROR,
+ "Cannot create security connector, missing connector credentials.");
+ return GRPC_SECURITY_ERROR;
+ }
+ return grpc_credentials_create_security_connector(c->connector_creds, target,
+ args, creds, sc, new_args);
}
static grpc_credentials_vtable composite_credentials_vtable = {
- composite_destruct, composite_has_request_metadata,
- composite_has_request_metadata_only, composite_get_request_metadata,
- composite_create_security_connector
-};
+ composite_destruct, composite_has_request_metadata,
+ composite_has_request_metadata_only, composite_get_request_metadata,
+ composite_create_security_connector};
-static grpc_credentials_array
-get_creds_array (grpc_credentials ** creds_addr)
-{
+static grpc_credentials_array get_creds_array(grpc_credentials **creds_addr) {
grpc_credentials_array result;
grpc_credentials *creds = *creds_addr;
result.creds_array = creds_addr;
result.num_creds = 1;
- if (strcmp (creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0)
- {
- result = *grpc_composite_credentials_get_credentials (creds);
- }
+ if (strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0) {
+ result = *grpc_composite_credentials_get_credentials(creds);
+ }
return result;
}
-grpc_credentials *
-grpc_composite_credentials_create (grpc_credentials * creds1, grpc_credentials * creds2, void *reserved)
-{
+grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1,
+ grpc_credentials *creds2,
+ void *reserved) {
size_t i;
size_t creds_array_byte_size;
grpc_credentials_array creds1_array;
grpc_credentials_array creds2_array;
grpc_composite_credentials *c;
- GPR_ASSERT (reserved == NULL);
- GPR_ASSERT (creds1 != NULL);
- GPR_ASSERT (creds2 != NULL);
- c = gpr_malloc (sizeof (grpc_composite_credentials));
- memset (c, 0, sizeof (grpc_composite_credentials));
+ GPR_ASSERT(reserved == NULL);
+ GPR_ASSERT(creds1 != NULL);
+ GPR_ASSERT(creds2 != NULL);
+ c = gpr_malloc(sizeof(grpc_composite_credentials));
+ memset(c, 0, sizeof(grpc_composite_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_COMPOSITE;
c->base.vtable = &composite_credentials_vtable;
- gpr_ref_init (&c->base.refcount, 1);
- creds1_array = get_creds_array (&creds1);
- creds2_array = get_creds_array (&creds2);
+ gpr_ref_init(&c->base.refcount, 1);
+ creds1_array = get_creds_array(&creds1);
+ creds2_array = get_creds_array(&creds2);
c->inner.num_creds = creds1_array.num_creds + creds2_array.num_creds;
- creds_array_byte_size = c->inner.num_creds * sizeof (grpc_credentials *);
- c->inner.creds_array = gpr_malloc (creds_array_byte_size);
- memset (c->inner.creds_array, 0, creds_array_byte_size);
- for (i = 0; i < creds1_array.num_creds; i++)
- {
- grpc_credentials *cur_creds = creds1_array.creds_array[i];
- if (!grpc_credentials_has_request_metadata_only (cur_creds))
- {
- if (c->connector_creds == NULL)
- {
- c->connector_creds = cur_creds;
- }
- else
- {
- gpr_log (GPR_ERROR, "Cannot compose multiple connector credentials.");
- goto fail;
- }
- }
- c->inner.creds_array[i] = grpc_credentials_ref (cur_creds);
+ creds_array_byte_size = c->inner.num_creds * sizeof(grpc_credentials *);
+ c->inner.creds_array = gpr_malloc(creds_array_byte_size);
+ memset(c->inner.creds_array, 0, creds_array_byte_size);
+ for (i = 0; i < creds1_array.num_creds; i++) {
+ grpc_credentials *cur_creds = creds1_array.creds_array[i];
+ if (!grpc_credentials_has_request_metadata_only(cur_creds)) {
+ if (c->connector_creds == NULL) {
+ c->connector_creds = cur_creds;
+ } else {
+ gpr_log(GPR_ERROR, "Cannot compose multiple connector credentials.");
+ goto fail;
+ }
}
- for (i = 0; i < creds2_array.num_creds; i++)
- {
- grpc_credentials *cur_creds = creds2_array.creds_array[i];
- if (!grpc_credentials_has_request_metadata_only (cur_creds))
- {
- if (c->connector_creds == NULL)
- {
- c->connector_creds = cur_creds;
- }
- else
- {
- gpr_log (GPR_ERROR, "Cannot compose multiple connector credentials.");
- goto fail;
- }
- }
- c->inner.creds_array[i + creds1_array.num_creds] = grpc_credentials_ref (cur_creds);
+ c->inner.creds_array[i] = grpc_credentials_ref(cur_creds);
+ }
+ for (i = 0; i < creds2_array.num_creds; i++) {
+ grpc_credentials *cur_creds = creds2_array.creds_array[i];
+ if (!grpc_credentials_has_request_metadata_only(cur_creds)) {
+ if (c->connector_creds == NULL) {
+ c->connector_creds = cur_creds;
+ } else {
+ gpr_log(GPR_ERROR, "Cannot compose multiple connector credentials.");
+ goto fail;
+ }
}
+ c->inner.creds_array[i + creds1_array.num_creds] =
+ grpc_credentials_ref(cur_creds);
+ }
return &c->base;
fail:
- grpc_credentials_unref (&c->base);
+ grpc_credentials_unref(&c->base);
return NULL;
}
-const grpc_credentials_array *
-grpc_composite_credentials_get_credentials (grpc_credentials * creds)
-{
- const grpc_composite_credentials *c = (const grpc_composite_credentials *) creds;
- GPR_ASSERT (strcmp (creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0);
+const grpc_credentials_array *grpc_composite_credentials_get_credentials(
+ grpc_credentials *creds) {
+ const grpc_composite_credentials *c =
+ (const grpc_composite_credentials *)creds;
+ GPR_ASSERT(strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0);
return &c->inner;
}
-grpc_credentials *
-grpc_credentials_contains_type (grpc_credentials * creds, const char *type, grpc_credentials ** composite_creds)
-{
+grpc_credentials *grpc_credentials_contains_type(
+ grpc_credentials *creds, const char *type,
+ grpc_credentials **composite_creds) {
size_t i;
- if (strcmp (creds->type, type) == 0)
- {
- if (composite_creds != NULL)
- *composite_creds = NULL;
- return creds;
- }
- else if (strcmp (creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0)
- {
- const grpc_credentials_array *inner_creds_array = grpc_composite_credentials_get_credentials (creds);
- for (i = 0; i < inner_creds_array->num_creds; i++)
- {
- if (strcmp (type, inner_creds_array->creds_array[i]->type) == 0)
- {
- if (composite_creds != NULL)
- *composite_creds = creds;
- return inner_creds_array->creds_array[i];
- }
- }
+ if (strcmp(creds->type, type) == 0) {
+ if (composite_creds != NULL) *composite_creds = NULL;
+ return creds;
+ } else if (strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0) {
+ const grpc_credentials_array *inner_creds_array =
+ grpc_composite_credentials_get_credentials(creds);
+ for (i = 0; i < inner_creds_array->num_creds; i++) {
+ if (strcmp(type, inner_creds_array->creds_array[i]->type) == 0) {
+ if (composite_creds != NULL) *composite_creds = creds;
+ return inner_creds_array->creds_array[i];
+ }
}
+ }
return NULL;
}
/* -- IAM credentials. -- */
-static void
-iam_destruct (grpc_credentials * creds)
-{
- grpc_google_iam_credentials *c = (grpc_google_iam_credentials *) creds;
- grpc_credentials_md_store_unref (c->iam_md);
+static void iam_destruct(grpc_credentials *creds) {
+ grpc_google_iam_credentials *c = (grpc_google_iam_credentials *)creds;
+ grpc_credentials_md_store_unref(c->iam_md);
}
-static int
-iam_has_request_metadata (const grpc_credentials * creds)
-{
- return 1;
-}
+static int iam_has_request_metadata(const grpc_credentials *creds) { return 1; }
-static int
-iam_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int iam_has_request_metadata_only(const grpc_credentials *creds) {
return 1;
}
-static void
-iam_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_google_iam_credentials *c = (grpc_google_iam_credentials *) creds;
- cb (exec_ctx, user_data, c->iam_md->entries, c->iam_md->num_entries, GRPC_CREDENTIALS_OK);
+static void iam_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_google_iam_credentials *c = (grpc_google_iam_credentials *)creds;
+ cb(exec_ctx, user_data, c->iam_md->entries, c->iam_md->num_entries,
+ GRPC_CREDENTIALS_OK);
}
static grpc_credentials_vtable iam_vtable = {
- iam_destruct, iam_has_request_metadata, iam_has_request_metadata_only,
- iam_get_request_metadata, NULL
-};
+ iam_destruct, iam_has_request_metadata, iam_has_request_metadata_only,
+ iam_get_request_metadata, NULL};
-grpc_credentials *
-grpc_google_iam_credentials_create (const char *token, const char *authority_selector, void *reserved)
-{
+grpc_credentials *grpc_google_iam_credentials_create(
+ const char *token, const char *authority_selector, void *reserved) {
grpc_google_iam_credentials *c;
- GPR_ASSERT (reserved == NULL);
- GPR_ASSERT (token != NULL);
- GPR_ASSERT (authority_selector != NULL);
- c = gpr_malloc (sizeof (grpc_google_iam_credentials));
- memset (c, 0, sizeof (grpc_google_iam_credentials));
+ GPR_ASSERT(reserved == NULL);
+ GPR_ASSERT(token != NULL);
+ GPR_ASSERT(authority_selector != NULL);
+ c = gpr_malloc(sizeof(grpc_google_iam_credentials));
+ memset(c, 0, sizeof(grpc_google_iam_credentials));
c->base.type = GRPC_CREDENTIALS_TYPE_IAM;
c->base.vtable = &iam_vtable;
- gpr_ref_init (&c->base.refcount, 1);
- c->iam_md = grpc_credentials_md_store_create (2);
- grpc_credentials_md_store_add_cstrings (c->iam_md, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, token);
- grpc_credentials_md_store_add_cstrings (c->iam_md, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, authority_selector);
+ gpr_ref_init(&c->base.refcount, 1);
+ c->iam_md = grpc_credentials_md_store_create(2);
+ grpc_credentials_md_store_add_cstrings(
+ c->iam_md, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, token);
+ grpc_credentials_md_store_add_cstrings(
+ c->iam_md, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, authority_selector);
return &c->base;
}
/* -- Plugin credentials. -- */
-typedef struct
-{
+typedef struct {
void *user_data;
grpc_credentials_metadata_cb cb;
} grpc_metadata_plugin_request;
-static void
-plugin_destruct (grpc_credentials * creds)
-{
- grpc_plugin_credentials *c = (grpc_plugin_credentials *) creds;
- if (c->plugin.state != NULL && c->plugin.destroy != NULL)
- {
- c->plugin.destroy (c->plugin.state);
- }
+static void plugin_destruct(grpc_credentials *creds) {
+ grpc_plugin_credentials *c = (grpc_plugin_credentials *)creds;
+ if (c->plugin.state != NULL && c->plugin.destroy != NULL) {
+ c->plugin.destroy(c->plugin.state);
+ }
}
-static int
-plugin_has_request_metadata (const grpc_credentials * creds)
-{
+static int plugin_has_request_metadata(const grpc_credentials *creds) {
return 1;
}
-static int
-plugin_has_request_metadata_only (const grpc_credentials * creds)
-{
+static int plugin_has_request_metadata_only(const grpc_credentials *creds) {
return 1;
}
-static void
-plugin_md_request_metadata_ready (void *request, const grpc_metadata * md, size_t num_md, grpc_status_code status, const char *error_details)
-{
+static void plugin_md_request_metadata_ready(void *request,
+ const grpc_metadata *md,
+ size_t num_md,
+ grpc_status_code status,
+ const char *error_details) {
/* called from application code */
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_metadata_plugin_request *r = (grpc_metadata_plugin_request *) request;
- if (status != GRPC_STATUS_OK)
- {
- if (error_details != NULL)
- {
- gpr_log (GPR_ERROR, "Getting metadata from plugin failed with error: %s", error_details);
- }
- r->cb (&exec_ctx, r->user_data, NULL, 0, GRPC_CREDENTIALS_ERROR);
- }
- else
- {
- size_t i;
- grpc_credentials_md *md_array = NULL;
- if (num_md > 0)
- {
- md_array = gpr_malloc (num_md * sizeof (grpc_credentials_md));
- for (i = 0; i < num_md; i++)
- {
- md_array[i].key = gpr_slice_from_copied_string (md[i].key);
- md_array[i].value = gpr_slice_from_copied_buffer (md[i].value, md[i].value_length);
- }
- }
- r->cb (&exec_ctx, r->user_data, md_array, num_md, GRPC_CREDENTIALS_OK);
- if (md_array != NULL)
- {
- for (i = 0; i < num_md; i++)
- {
- gpr_slice_unref (md_array[i].key);
- gpr_slice_unref (md_array[i].value);
- }
- gpr_free (md_array);
- }
+ grpc_metadata_plugin_request *r = (grpc_metadata_plugin_request *)request;
+ if (status != GRPC_STATUS_OK) {
+ if (error_details != NULL) {
+ gpr_log(GPR_ERROR, "Getting metadata from plugin failed with error: %s",
+ error_details);
}
- gpr_free (r);
-}
-
-static void
-plugin_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data)
-{
- grpc_plugin_credentials *c = (grpc_plugin_credentials *) creds;
- if (c->plugin.get_metadata != NULL)
- {
- grpc_metadata_plugin_request *request = gpr_malloc (sizeof (*request));
- memset (request, 0, sizeof (*request));
- request->user_data = user_data;
- request->cb = cb;
- c->plugin.get_metadata (c->plugin.state, service_url, plugin_md_request_metadata_ready, request);
+ r->cb(&exec_ctx, r->user_data, NULL, 0, GRPC_CREDENTIALS_ERROR);
+ } else {
+ size_t i;
+ grpc_credentials_md *md_array = NULL;
+ if (num_md > 0) {
+ md_array = gpr_malloc(num_md * sizeof(grpc_credentials_md));
+ for (i = 0; i < num_md; i++) {
+ md_array[i].key = gpr_slice_from_copied_string(md[i].key);
+ md_array[i].value =
+ gpr_slice_from_copied_buffer(md[i].value, md[i].value_length);
+ }
}
- else
- {
- cb (exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_OK);
+ r->cb(&exec_ctx, r->user_data, md_array, num_md, GRPC_CREDENTIALS_OK);
+ if (md_array != NULL) {
+ for (i = 0; i < num_md; i++) {
+ gpr_slice_unref(md_array[i].key);
+ gpr_slice_unref(md_array[i].value);
+ }
+ gpr_free(md_array);
}
+ }
+ gpr_free(r);
+}
+
+static void plugin_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data) {
+ grpc_plugin_credentials *c = (grpc_plugin_credentials *)creds;
+ if (c->plugin.get_metadata != NULL) {
+ grpc_metadata_plugin_request *request = gpr_malloc(sizeof(*request));
+ memset(request, 0, sizeof(*request));
+ request->user_data = user_data;
+ request->cb = cb;
+ c->plugin.get_metadata(c->plugin.state, service_url,
+ plugin_md_request_metadata_ready, request);
+ } else {
+ cb(exec_ctx, user_data, NULL, 0, GRPC_CREDENTIALS_OK);
+ }
}
static grpc_credentials_vtable plugin_vtable = {
- plugin_destruct, plugin_has_request_metadata,
- plugin_has_request_metadata_only, plugin_get_request_metadata, NULL
-};
-
-grpc_credentials *
-grpc_metadata_credentials_create_from_plugin (grpc_metadata_credentials_plugin plugin, void *reserved)
-{
- grpc_plugin_credentials *c = gpr_malloc (sizeof (*c));
- GPR_ASSERT (reserved == NULL);
- memset (c, 0, sizeof (*c));
+ plugin_destruct, plugin_has_request_metadata,
+ plugin_has_request_metadata_only, plugin_get_request_metadata, NULL};
+
+grpc_credentials *grpc_metadata_credentials_create_from_plugin(
+ grpc_metadata_credentials_plugin plugin, void *reserved) {
+ grpc_plugin_credentials *c = gpr_malloc(sizeof(*c));
+ GPR_ASSERT(reserved == NULL);
+ memset(c, 0, sizeof(*c));
c->base.type = GRPC_CREDENTIALS_TYPE_METADATA_PLUGIN;
c->base.vtable = &plugin_vtable;
- gpr_ref_init (&c->base.refcount, 1);
+ gpr_ref_init(&c->base.refcount, 1);
c->plugin = plugin;
return &c->base;
}
diff --git a/src/core/security/credentials.h b/src/core/security/credentials.h
index c10c9db626..b213e052d3 100644
--- a/src/core/security/credentials.h
+++ b/src/core/security/credentials.h
@@ -47,8 +47,7 @@ struct grpc_httpcli_response;
/* --- Constants. --- */
-typedef enum
-{
+typedef enum {
GRPC_CREDENTIALS_OK = 0,
GRPC_CREDENTIALS_ERROR
} grpc_credentials_status;
@@ -90,146 +89,169 @@ typedef enum
/* --- grpc_credentials_md. --- */
-typedef struct
-{
+typedef struct {
gpr_slice key;
gpr_slice value;
} grpc_credentials_md;
-typedef struct
-{
+typedef struct {
grpc_credentials_md *entries;
size_t num_entries;
size_t allocated;
gpr_refcount refcount;
} grpc_credentials_md_store;
-grpc_credentials_md_store *grpc_credentials_md_store_create (size_t initial_capacity);
+grpc_credentials_md_store *grpc_credentials_md_store_create(
+ size_t initial_capacity);
/* Will ref key and value. */
-void grpc_credentials_md_store_add (grpc_credentials_md_store * store, gpr_slice key, gpr_slice value);
-void grpc_credentials_md_store_add_cstrings (grpc_credentials_md_store * store, const char *key, const char *value);
-grpc_credentials_md_store *grpc_credentials_md_store_ref (grpc_credentials_md_store * store);
-void grpc_credentials_md_store_unref (grpc_credentials_md_store * store);
+void grpc_credentials_md_store_add(grpc_credentials_md_store *store,
+ gpr_slice key, gpr_slice value);
+void grpc_credentials_md_store_add_cstrings(grpc_credentials_md_store *store,
+ const char *key, const char *value);
+grpc_credentials_md_store *grpc_credentials_md_store_ref(
+ grpc_credentials_md_store *store);
+void grpc_credentials_md_store_unref(grpc_credentials_md_store *store);
/* --- grpc_credentials. --- */
/* Creates a fake transport security credentials object for testing. */
-grpc_credentials *grpc_fake_transport_security_credentials_create (void);
+grpc_credentials *grpc_fake_transport_security_credentials_create(void);
/* Creates a fake server transport security credentials object for testing. */
-grpc_server_credentials *grpc_fake_transport_security_server_credentials_create (void);
+grpc_server_credentials *grpc_fake_transport_security_server_credentials_create(
+ void);
/* It is the caller's responsibility to gpr_free the result if not NULL. */
-char *grpc_get_well_known_google_credentials_file_path (void);
-
-typedef void (*grpc_credentials_metadata_cb) (grpc_exec_ctx * exec_ctx, void *user_data, grpc_credentials_md * md_elems, size_t num_md, grpc_credentials_status status);
-
-typedef struct
-{
- void (*destruct) (grpc_credentials * c);
- int (*has_request_metadata) (const grpc_credentials * c);
- int (*has_request_metadata_only) (const grpc_credentials * c);
- void (*get_request_metadata) (grpc_exec_ctx * exec_ctx, grpc_credentials * c, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data);
- grpc_security_status (*create_security_connector) (grpc_credentials * c, const char *target, const grpc_channel_args * args, grpc_credentials * request_metadata_creds, grpc_channel_security_connector ** sc, grpc_channel_args ** new_args);
+char *grpc_get_well_known_google_credentials_file_path(void);
+
+typedef void (*grpc_credentials_metadata_cb)(grpc_exec_ctx *exec_ctx,
+ void *user_data,
+ grpc_credentials_md *md_elems,
+ size_t num_md,
+ grpc_credentials_status status);
+
+typedef struct {
+ void (*destruct)(grpc_credentials *c);
+ int (*has_request_metadata)(const grpc_credentials *c);
+ int (*has_request_metadata_only)(const grpc_credentials *c);
+ void (*get_request_metadata)(grpc_exec_ctx *exec_ctx, grpc_credentials *c,
+ grpc_pollset *pollset, const char *service_url,
+ grpc_credentials_metadata_cb cb,
+ void *user_data);
+ grpc_security_status (*create_security_connector)(
+ grpc_credentials *c, const char *target, const grpc_channel_args *args,
+ grpc_credentials *request_metadata_creds,
+ grpc_channel_security_connector **sc, grpc_channel_args **new_args);
} grpc_credentials_vtable;
-struct grpc_credentials
-{
+struct grpc_credentials {
const grpc_credentials_vtable *vtable;
const char *type;
gpr_refcount refcount;
};
-grpc_credentials *grpc_credentials_ref (grpc_credentials * creds);
-void grpc_credentials_unref (grpc_credentials * creds);
-int grpc_credentials_has_request_metadata (grpc_credentials * creds);
-int grpc_credentials_has_request_metadata_only (grpc_credentials * creds);
-void grpc_credentials_get_request_metadata (grpc_exec_ctx * exec_ctx, grpc_credentials * creds, grpc_pollset * pollset, const char *service_url, grpc_credentials_metadata_cb cb, void *user_data);
+grpc_credentials *grpc_credentials_ref(grpc_credentials *creds);
+void grpc_credentials_unref(grpc_credentials *creds);
+int grpc_credentials_has_request_metadata(grpc_credentials *creds);
+int grpc_credentials_has_request_metadata_only(grpc_credentials *creds);
+void grpc_credentials_get_request_metadata(
+ grpc_exec_ctx *exec_ctx, grpc_credentials *creds, grpc_pollset *pollset,
+ const char *service_url, grpc_credentials_metadata_cb cb, void *user_data);
/* Creates a security connector for the channel. May also create new channel
args for the channel to be used in place of the passed in const args if
returned non NULL. In that case the caller is responsible for destroying
new_args after channel creation. */
-grpc_security_status grpc_credentials_create_security_connector (grpc_credentials * creds, const char *target, const grpc_channel_args * args, grpc_credentials * request_metadata_creds, grpc_channel_security_connector ** sc, grpc_channel_args ** new_args);
+grpc_security_status grpc_credentials_create_security_connector(
+ grpc_credentials *creds, const char *target, const grpc_channel_args *args,
+ grpc_credentials *request_metadata_creds,
+ grpc_channel_security_connector **sc, grpc_channel_args **new_args);
-typedef struct
-{
+typedef struct {
grpc_credentials **creds_array;
size_t num_creds;
} grpc_credentials_array;
-const grpc_credentials_array *grpc_composite_credentials_get_credentials (grpc_credentials * composite_creds);
+const grpc_credentials_array *grpc_composite_credentials_get_credentials(
+ grpc_credentials *composite_creds);
/* Returns creds if creds is of the specified type or the inner creds of the
specified type (if found), if the creds is of type COMPOSITE.
If composite_creds is not NULL, *composite_creds will point to creds if of
type COMPOSITE in case of success. */
-grpc_credentials *grpc_credentials_contains_type (grpc_credentials * creds, const char *type, grpc_credentials ** composite_creds);
+grpc_credentials *grpc_credentials_contains_type(
+ grpc_credentials *creds, const char *type,
+ grpc_credentials **composite_creds);
/* Exposed for testing only. */
-grpc_credentials_status grpc_oauth2_token_fetcher_credentials_parse_server_response (const struct grpc_httpcli_response *response, grpc_credentials_md_store ** token_md, gpr_timespec * token_lifetime);
-void grpc_flush_cached_google_default_credentials (void);
+grpc_credentials_status
+grpc_oauth2_token_fetcher_credentials_parse_server_response(
+ const struct grpc_httpcli_response *response,
+ grpc_credentials_md_store **token_md, gpr_timespec *token_lifetime);
+void grpc_flush_cached_google_default_credentials(void);
/* Metadata-only credentials with the specified key and value where
asynchronicity can be simulated for testing. */
-grpc_credentials *grpc_md_only_test_credentials_create (const char *md_key, const char *md_value, int is_async);
+grpc_credentials *grpc_md_only_test_credentials_create(const char *md_key,
+ const char *md_value,
+ int is_async);
/* Private constructor for jwt credentials from an already parsed json key.
Takes ownership of the key. */
-grpc_credentials *grpc_service_account_jwt_access_credentials_create_from_auth_json_key (grpc_auth_json_key key, gpr_timespec token_lifetime);
+grpc_credentials *
+grpc_service_account_jwt_access_credentials_create_from_auth_json_key(
+ grpc_auth_json_key key, gpr_timespec token_lifetime);
/* Private constructor for refresh token credentials from an already parsed
refresh token. Takes ownership of the refresh token. */
-grpc_credentials *grpc_refresh_token_credentials_create_from_auth_refresh_token (grpc_auth_refresh_token token);
+grpc_credentials *grpc_refresh_token_credentials_create_from_auth_refresh_token(
+ grpc_auth_refresh_token token);
/* --- grpc_server_credentials. --- */
-typedef struct
-{
- void (*destruct) (grpc_server_credentials * c);
- grpc_security_status (*create_security_connector) (grpc_server_credentials * c, grpc_security_connector ** sc);
+typedef struct {
+ void (*destruct)(grpc_server_credentials *c);
+ grpc_security_status (*create_security_connector)(
+ grpc_server_credentials *c, grpc_security_connector **sc);
} grpc_server_credentials_vtable;
/* TODO(jboeuf): Add a refcount. */
-struct grpc_server_credentials
-{
+struct grpc_server_credentials {
const grpc_server_credentials_vtable *vtable;
const char *type;
gpr_refcount refcount;
grpc_auth_metadata_processor processor;
};
-grpc_security_status grpc_server_credentials_create_security_connector (grpc_server_credentials * creds, grpc_security_connector ** sc);
+grpc_security_status grpc_server_credentials_create_security_connector(
+ grpc_server_credentials *creds, grpc_security_connector **sc);
-grpc_server_credentials *grpc_server_credentials_ref (grpc_server_credentials * creds);
+grpc_server_credentials *grpc_server_credentials_ref(
+ grpc_server_credentials *creds);
-void grpc_server_credentials_unref (grpc_server_credentials * creds);
+void grpc_server_credentials_unref(grpc_server_credentials *creds);
/* -- Ssl credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
grpc_ssl_config config;
} grpc_ssl_credentials;
-typedef struct
-{
+typedef struct {
grpc_server_credentials base;
grpc_ssl_server_config config;
} grpc_ssl_server_credentials;
/* -- Jwt credentials -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
/* Have a simple cache for now with just 1 entry. We could have a map based on
the service_url for a more sophisticated one. */
gpr_mu cache_mu;
- struct
- {
+ struct {
grpc_credentials_md_store *jwt_md;
char *service_url;
gpr_timespec jwt_expiration;
@@ -244,12 +266,17 @@ typedef struct
This object is a base for credentials that need to acquire an oauth2 token
from an http service. */
-typedef struct grpc_credentials_metadata_request grpc_credentials_metadata_request;
+typedef struct grpc_credentials_metadata_request
+ grpc_credentials_metadata_request;
-typedef void (*grpc_fetch_oauth2_func) (grpc_exec_ctx * exec_ctx, grpc_credentials_metadata_request * req, grpc_httpcli_context * http_context, grpc_pollset * pollset, grpc_httpcli_response_cb response_cb, gpr_timespec deadline);
+typedef void (*grpc_fetch_oauth2_func)(grpc_exec_ctx *exec_ctx,
+ grpc_credentials_metadata_request *req,
+ grpc_httpcli_context *http_context,
+ grpc_pollset *pollset,
+ grpc_httpcli_response_cb response_cb,
+ gpr_timespec deadline);
-typedef struct
-{
+typedef struct {
grpc_credentials base;
gpr_mu mu;
grpc_credentials_md_store *access_token_md;
@@ -260,24 +287,21 @@ typedef struct
/* -- GoogleRefreshToken credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_oauth2_token_fetcher_credentials base;
grpc_auth_refresh_token refresh_token;
} grpc_google_refresh_token_credentials;
/* -- Oauth2 Access Token credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
grpc_credentials_md_store *access_token_md;
} grpc_access_token_credentials;
/* -- Metadata-only Test credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
grpc_credentials_md_store *md_store;
int is_async;
@@ -285,16 +309,14 @@ typedef struct
/* -- GoogleIAM credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
grpc_credentials_md_store *iam_md;
} grpc_google_iam_credentials;
/* -- Composite credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
grpc_credentials_array inner;
grpc_credentials *connector_creds;
@@ -302,8 +324,7 @@ typedef struct
/* -- Plugin credentials. -- */
-typedef struct
-{
+typedef struct {
grpc_credentials base;
grpc_metadata_credentials_plugin plugin;
grpc_credentials_md_store *plugin_md;
diff --git a/src/core/security/credentials_metadata.c b/src/core/security/credentials_metadata.c
index f9a5f5fd4f..b8a132f1ea 100644
--- a/src/core/security/credentials_metadata.c
+++ b/src/core/security/credentials_metadata.c
@@ -37,78 +37,65 @@
#include <string.h>
-static void
-store_ensure_capacity (grpc_credentials_md_store * store)
-{
- if (store->num_entries == store->allocated)
- {
- store->allocated = (store->allocated == 0) ? 1 : store->allocated * 2;
- store->entries = gpr_realloc (store->entries, store->allocated * sizeof (grpc_credentials_md));
- }
+static void store_ensure_capacity(grpc_credentials_md_store *store) {
+ if (store->num_entries == store->allocated) {
+ store->allocated = (store->allocated == 0) ? 1 : store->allocated * 2;
+ store->entries = gpr_realloc(
+ store->entries, store->allocated * sizeof(grpc_credentials_md));
+ }
}
-grpc_credentials_md_store *
-grpc_credentials_md_store_create (size_t initial_capacity)
-{
- grpc_credentials_md_store *store = gpr_malloc (sizeof (grpc_credentials_md_store));
- memset (store, 0, sizeof (grpc_credentials_md_store));
- if (initial_capacity > 0)
- {
- store->entries = gpr_malloc (initial_capacity * sizeof (grpc_credentials_md));
- store->allocated = initial_capacity;
- }
- gpr_ref_init (&store->refcount, 1);
+grpc_credentials_md_store *grpc_credentials_md_store_create(
+ size_t initial_capacity) {
+ grpc_credentials_md_store *store =
+ gpr_malloc(sizeof(grpc_credentials_md_store));
+ memset(store, 0, sizeof(grpc_credentials_md_store));
+ if (initial_capacity > 0) {
+ store->entries = gpr_malloc(initial_capacity * sizeof(grpc_credentials_md));
+ store->allocated = initial_capacity;
+ }
+ gpr_ref_init(&store->refcount, 1);
return store;
}
-void
-grpc_credentials_md_store_add (grpc_credentials_md_store * store, gpr_slice key, gpr_slice value)
-{
- if (store == NULL)
- return;
- store_ensure_capacity (store);
- store->entries[store->num_entries].key = gpr_slice_ref (key);
- store->entries[store->num_entries].value = gpr_slice_ref (value);
+void grpc_credentials_md_store_add(grpc_credentials_md_store *store,
+ gpr_slice key, gpr_slice value) {
+ if (store == NULL) return;
+ store_ensure_capacity(store);
+ store->entries[store->num_entries].key = gpr_slice_ref(key);
+ store->entries[store->num_entries].value = gpr_slice_ref(value);
store->num_entries++;
}
-void
-grpc_credentials_md_store_add_cstrings (grpc_credentials_md_store * store, const char *key, const char *value)
-{
- if (store == NULL)
- return;
- store_ensure_capacity (store);
- store->entries[store->num_entries].key = gpr_slice_from_copied_string (key);
- store->entries[store->num_entries].value = gpr_slice_from_copied_string (value);
+void grpc_credentials_md_store_add_cstrings(grpc_credentials_md_store *store,
+ const char *key,
+ const char *value) {
+ if (store == NULL) return;
+ store_ensure_capacity(store);
+ store->entries[store->num_entries].key = gpr_slice_from_copied_string(key);
+ store->entries[store->num_entries].value =
+ gpr_slice_from_copied_string(value);
store->num_entries++;
}
-grpc_credentials_md_store *
-grpc_credentials_md_store_ref (grpc_credentials_md_store * store)
-{
- if (store == NULL)
- return NULL;
- gpr_ref (&store->refcount);
+grpc_credentials_md_store *grpc_credentials_md_store_ref(
+ grpc_credentials_md_store *store) {
+ if (store == NULL) return NULL;
+ gpr_ref(&store->refcount);
return store;
}
-void
-grpc_credentials_md_store_unref (grpc_credentials_md_store * store)
-{
- if (store == NULL)
- return;
- if (gpr_unref (&store->refcount))
- {
- if (store->entries != NULL)
- {
- size_t i;
- for (i = 0; i < store->num_entries; i++)
- {
- gpr_slice_unref (store->entries[i].key);
- gpr_slice_unref (store->entries[i].value);
- }
- gpr_free (store->entries);
- }
- gpr_free (store);
+void grpc_credentials_md_store_unref(grpc_credentials_md_store *store) {
+ if (store == NULL) return;
+ if (gpr_unref(&store->refcount)) {
+ if (store->entries != NULL) {
+ size_t i;
+ for (i = 0; i < store->num_entries; i++) {
+ gpr_slice_unref(store->entries[i].key);
+ gpr_slice_unref(store->entries[i].value);
+ }
+ gpr_free(store->entries);
}
+ gpr_free(store);
+ }
}
diff --git a/src/core/security/credentials_posix.c b/src/core/security/credentials_posix.c
index ba897bd7c5..20f67a7f14 100644
--- a/src/core/security/credentials_posix.c
+++ b/src/core/security/credentials_posix.c
@@ -44,18 +44,17 @@
#include "src/core/support/env.h"
#include "src/core/support/string.h"
-char *
-grpc_get_well_known_google_credentials_file_path (void)
-{
+char *grpc_get_well_known_google_credentials_file_path(void) {
char *result = NULL;
- char *home = gpr_getenv ("HOME");
- if (home == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get HOME environment variable.");
- return NULL;
- }
- gpr_asprintf (&result, "%s/.config/%s/%s", home, GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY, GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE);
- gpr_free (home);
+ char *home = gpr_getenv("HOME");
+ if (home == NULL) {
+ gpr_log(GPR_ERROR, "Could not get HOME environment variable.");
+ return NULL;
+ }
+ gpr_asprintf(&result, "%s/.config/%s/%s", home,
+ GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY,
+ GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE);
+ gpr_free(home);
return result;
}
diff --git a/src/core/security/credentials_win32.c b/src/core/security/credentials_win32.c
index 2630647121..92dfd9bdfe 100644
--- a/src/core/security/credentials_win32.c
+++ b/src/core/security/credentials_win32.c
@@ -44,18 +44,17 @@
#include "src/core/support/env.h"
#include "src/core/support/string.h"
-char *
-grpc_get_well_known_google_credentials_file_path (void)
-{
+char *grpc_get_well_known_google_credentials_file_path(void) {
char *result = NULL;
- char *appdata_path = gpr_getenv ("APPDATA");
- if (appdata_path == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get APPDATA environment variable.");
- return NULL;
- }
- gpr_asprintf (&result, "%s/%s/%s", appdata_path, GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY, GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE);
- gpr_free (appdata_path);
+ char *appdata_path = gpr_getenv("APPDATA");
+ if (appdata_path == NULL) {
+ gpr_log(GPR_ERROR, "Could not get APPDATA environment variable.");
+ return NULL;
+ }
+ gpr_asprintf(&result, "%s/%s/%s", appdata_path,
+ GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY,
+ GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE);
+ gpr_free(appdata_path);
return result;
}
diff --git a/src/core/security/google_default_credentials.c b/src/core/security/google_default_credentials.c
index 945cb42427..7b85842808 100644
--- a/src/core/security/google_default_credentials.c
+++ b/src/core/security/google_default_credentials.c
@@ -54,53 +54,42 @@ static int compute_engine_detection_done = 0;
static gpr_mu g_mu;
static gpr_once g_once = GPR_ONCE_INIT;
-static void
-init_default_credentials (void)
-{
- gpr_mu_init (&g_mu);
-}
+static void init_default_credentials(void) { gpr_mu_init(&g_mu); }
-typedef struct
-{
+typedef struct {
grpc_pollset pollset;
int is_done;
int success;
} compute_engine_detector;
-static void
-on_compute_engine_detection_http_response (grpc_exec_ctx * exec_ctx, void *user_data, const grpc_httpcli_response * response)
-{
- compute_engine_detector *detector = (compute_engine_detector *) user_data;
- if (response != NULL && response->status == 200 && response->hdr_count > 0)
- {
- /* Internet providers can return a generic response to all requests, so
- it is necessary to check that metadata header is present also. */
- size_t i;
- for (i = 0; i < response->hdr_count; i++)
- {
- grpc_httpcli_header *header = &response->hdrs[i];
- if (strcmp (header->key, "Metadata-Flavor") == 0 && strcmp (header->value, "Google") == 0)
- {
- detector->success = 1;
- break;
- }
- }
+static void on_compute_engine_detection_http_response(
+ grpc_exec_ctx *exec_ctx, void *user_data,
+ const grpc_httpcli_response *response) {
+ compute_engine_detector *detector = (compute_engine_detector *)user_data;
+ if (response != NULL && response->status == 200 && response->hdr_count > 0) {
+ /* Internet providers can return a generic response to all requests, so
+ it is necessary to check that metadata header is present also. */
+ size_t i;
+ for (i = 0; i < response->hdr_count; i++) {
+ grpc_httpcli_header *header = &response->hdrs[i];
+ if (strcmp(header->key, "Metadata-Flavor") == 0 &&
+ strcmp(header->value, "Google") == 0) {
+ detector->success = 1;
+ break;
+ }
}
- gpr_mu_lock (GRPC_POLLSET_MU (&detector->pollset));
+ }
+ gpr_mu_lock(GRPC_POLLSET_MU(&detector->pollset));
detector->is_done = 1;
- grpc_pollset_kick (&detector->pollset, NULL);
- gpr_mu_unlock (GRPC_POLLSET_MU (&detector->pollset));
+ grpc_pollset_kick(&detector->pollset, NULL);
+ gpr_mu_unlock(GRPC_POLLSET_MU(&detector->pollset));
}
-static void
-destroy_pollset (grpc_exec_ctx * exec_ctx, void *p, int s)
-{
- grpc_pollset_destroy (p);
+static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, int s) {
+ grpc_pollset_destroy(p);
}
-static int
-is_stack_running_on_compute_engine (void)
-{
+static int is_stack_running_on_compute_engine(void) {
compute_engine_detector detector;
grpc_httpcli_request request;
grpc_httpcli_context context;
@@ -109,147 +98,138 @@ is_stack_running_on_compute_engine (void)
/* The http call is local. If it takes more than one sec, it is for sure not
on compute engine. */
- gpr_timespec max_detection_delay = gpr_time_from_seconds (1, GPR_TIMESPAN);
+ gpr_timespec max_detection_delay = gpr_time_from_seconds(1, GPR_TIMESPAN);
- grpc_pollset_init (&detector.pollset);
+ grpc_pollset_init(&detector.pollset);
detector.is_done = 0;
detector.success = 0;
- memset (&request, 0, sizeof (grpc_httpcli_request));
+ memset(&request, 0, sizeof(grpc_httpcli_request));
request.host = GRPC_COMPUTE_ENGINE_DETECTION_HOST;
request.path = "/";
- grpc_httpcli_context_init (&context);
+ grpc_httpcli_context_init(&context);
- grpc_httpcli_get (&exec_ctx, &context, &detector.pollset, &request, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), max_detection_delay), on_compute_engine_detection_http_response, &detector);
+ grpc_httpcli_get(
+ &exec_ctx, &context, &detector.pollset, &request,
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), max_detection_delay),
+ on_compute_engine_detection_http_response, &detector);
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
/* Block until we get the response. This is not ideal but this should only be
called once for the lifetime of the process by the default credentials. */
- gpr_mu_lock (GRPC_POLLSET_MU (&detector.pollset));
- while (!detector.is_done)
- {
- grpc_pollset_worker worker;
- grpc_pollset_work (&exec_ctx, &detector.pollset, &worker, gpr_now (GPR_CLOCK_MONOTONIC), gpr_inf_future (GPR_CLOCK_MONOTONIC));
- }
- gpr_mu_unlock (GRPC_POLLSET_MU (&detector.pollset));
-
- grpc_httpcli_context_destroy (&context);
- grpc_closure_init (&destroy_closure, destroy_pollset, &detector.pollset);
- grpc_pollset_shutdown (&exec_ctx, &detector.pollset, &destroy_closure);
- grpc_exec_ctx_finish (&exec_ctx);
+ gpr_mu_lock(GRPC_POLLSET_MU(&detector.pollset));
+ while (!detector.is_done) {
+ grpc_pollset_worker worker;
+ grpc_pollset_work(&exec_ctx, &detector.pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC),
+ gpr_inf_future(GPR_CLOCK_MONOTONIC));
+ }
+ gpr_mu_unlock(GRPC_POLLSET_MU(&detector.pollset));
+
+ grpc_httpcli_context_destroy(&context);
+ grpc_closure_init(&destroy_closure, destroy_pollset, &detector.pollset);
+ grpc_pollset_shutdown(&exec_ctx, &detector.pollset, &destroy_closure);
+ grpc_exec_ctx_finish(&exec_ctx);
return detector.success;
}
/* Takes ownership of creds_path if not NULL. */
-static grpc_credentials *
-create_default_creds_from_path (char *creds_path)
-{
+static grpc_credentials *create_default_creds_from_path(char *creds_path) {
grpc_json *json = NULL;
grpc_auth_json_key key;
grpc_auth_refresh_token token;
grpc_credentials *result = NULL;
- gpr_slice creds_data = gpr_empty_slice ();
+ gpr_slice creds_data = gpr_empty_slice();
int file_ok = 0;
- if (creds_path == NULL)
- goto end;
- creds_data = gpr_load_file (creds_path, 0, &file_ok);
- if (!file_ok)
- goto end;
- json = grpc_json_parse_string_with_len ((char *) GPR_SLICE_START_PTR (creds_data), GPR_SLICE_LENGTH (creds_data));
- if (json == NULL)
- goto end;
+ if (creds_path == NULL) goto end;
+ creds_data = gpr_load_file(creds_path, 0, &file_ok);
+ if (!file_ok) goto end;
+ json = grpc_json_parse_string_with_len(
+ (char *)GPR_SLICE_START_PTR(creds_data), GPR_SLICE_LENGTH(creds_data));
+ if (json == NULL) goto end;
/* First, try an auth json key. */
- key = grpc_auth_json_key_create_from_json (json);
- if (grpc_auth_json_key_is_valid (&key))
- {
- result = grpc_service_account_jwt_access_credentials_create_from_auth_json_key (key, grpc_max_auth_token_lifetime);
- goto end;
- }
+ key = grpc_auth_json_key_create_from_json(json);
+ if (grpc_auth_json_key_is_valid(&key)) {
+ result =
+ grpc_service_account_jwt_access_credentials_create_from_auth_json_key(
+ key, grpc_max_auth_token_lifetime);
+ goto end;
+ }
/* Then try a refresh token if the auth json key was invalid. */
- token = grpc_auth_refresh_token_create_from_json (json);
- if (grpc_auth_refresh_token_is_valid (&token))
- {
- result = grpc_refresh_token_credentials_create_from_auth_refresh_token (token);
- goto end;
- }
+ token = grpc_auth_refresh_token_create_from_json(json);
+ if (grpc_auth_refresh_token_is_valid(&token)) {
+ result =
+ grpc_refresh_token_credentials_create_from_auth_refresh_token(token);
+ goto end;
+ }
end:
- if (creds_path != NULL)
- gpr_free (creds_path);
- gpr_slice_unref (creds_data);
- if (json != NULL)
- grpc_json_destroy (json);
+ if (creds_path != NULL) gpr_free(creds_path);
+ gpr_slice_unref(creds_data);
+ if (json != NULL) grpc_json_destroy(json);
return result;
}
-grpc_credentials *
-grpc_google_default_credentials_create (void)
-{
+grpc_credentials *grpc_google_default_credentials_create(void) {
grpc_credentials *result = NULL;
int serving_cached_credentials = 0;
- gpr_once_init (&g_once, init_default_credentials);
+ gpr_once_init(&g_once, init_default_credentials);
- gpr_mu_lock (&g_mu);
+ gpr_mu_lock(&g_mu);
- if (default_credentials != NULL)
- {
- result = grpc_credentials_ref (default_credentials);
- serving_cached_credentials = 1;
- goto end;
- }
+ if (default_credentials != NULL) {
+ result = grpc_credentials_ref(default_credentials);
+ serving_cached_credentials = 1;
+ goto end;
+ }
/* First, try the environment variable. */
- result = create_default_creds_from_path (gpr_getenv (GRPC_GOOGLE_CREDENTIALS_ENV_VAR));
- if (result != NULL)
- goto end;
+ result = create_default_creds_from_path(
+ gpr_getenv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR));
+ if (result != NULL) goto end;
/* Then the well-known file. */
- result = create_default_creds_from_path (grpc_get_well_known_google_credentials_file_path ());
- if (result != NULL)
- goto end;
+ result = create_default_creds_from_path(
+ grpc_get_well_known_google_credentials_file_path());
+ if (result != NULL) goto end;
/* At last try to see if we're on compute engine (do the detection only once
since it requires a network test). */
- if (!compute_engine_detection_done)
- {
- int need_compute_engine_creds = is_stack_running_on_compute_engine ();
- compute_engine_detection_done = 1;
- if (need_compute_engine_creds)
- {
- result = grpc_google_compute_engine_credentials_create (NULL);
- }
+ if (!compute_engine_detection_done) {
+ int need_compute_engine_creds = is_stack_running_on_compute_engine();
+ compute_engine_detection_done = 1;
+ if (need_compute_engine_creds) {
+ result = grpc_google_compute_engine_credentials_create(NULL);
}
+ }
end:
- if (!serving_cached_credentials && result != NULL)
- {
- /* Blend with default ssl credentials and add a global reference so that it
- can be cached and re-served. */
- grpc_credentials *ssl_creds = grpc_ssl_credentials_create (NULL, NULL, NULL);
- default_credentials = grpc_credentials_ref (grpc_composite_credentials_create (ssl_creds, result, NULL));
- GPR_ASSERT (default_credentials != NULL);
- grpc_credentials_unref (ssl_creds);
- grpc_credentials_unref (result);
- result = default_credentials;
- }
- gpr_mu_unlock (&g_mu);
+ if (!serving_cached_credentials && result != NULL) {
+ /* Blend with default ssl credentials and add a global reference so that it
+ can be cached and re-served. */
+ grpc_credentials *ssl_creds = grpc_ssl_credentials_create(NULL, NULL, NULL);
+ default_credentials = grpc_credentials_ref(
+ grpc_composite_credentials_create(ssl_creds, result, NULL));
+ GPR_ASSERT(default_credentials != NULL);
+ grpc_credentials_unref(ssl_creds);
+ grpc_credentials_unref(result);
+ result = default_credentials;
+ }
+ gpr_mu_unlock(&g_mu);
return result;
}
-void
-grpc_flush_cached_google_default_credentials (void)
-{
- gpr_once_init (&g_once, init_default_credentials);
- gpr_mu_lock (&g_mu);
- if (default_credentials != NULL)
- {
- grpc_credentials_unref (default_credentials);
- default_credentials = NULL;
- }
- gpr_mu_unlock (&g_mu);
+void grpc_flush_cached_google_default_credentials(void) {
+ gpr_once_init(&g_once, init_default_credentials);
+ gpr_mu_lock(&g_mu);
+ if (default_credentials != NULL) {
+ grpc_credentials_unref(default_credentials);
+ default_credentials = NULL;
+ }
+ gpr_mu_unlock(&g_mu);
}
diff --git a/src/core/security/handshake.c b/src/core/security/handshake.c
index 687cc575a7..adbdd0b40e 100644
--- a/src/core/security/handshake.c
+++ b/src/core/security/handshake.c
@@ -42,8 +42,7 @@
#define GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE 256
-typedef struct
-{
+typedef struct {
grpc_security_connector *connector;
tsi_handshaker *handshaker;
unsigned char *handshake_buffer;
@@ -59,131 +58,123 @@ typedef struct
grpc_closure on_handshake_data_received_from_peer;
} grpc_security_handshake;
-static void on_handshake_data_received_from_peer (grpc_exec_ctx * exec_ctx, void *setup, int success);
+static void on_handshake_data_received_from_peer(grpc_exec_ctx *exec_ctx,
+ void *setup, int success);
-static void on_handshake_data_sent_to_peer (grpc_exec_ctx * exec_ctx, void *setup, int success);
+static void on_handshake_data_sent_to_peer(grpc_exec_ctx *exec_ctx, void *setup,
+ int success);
-static void
-security_handshake_done (grpc_exec_ctx * exec_ctx, grpc_security_handshake * h, int is_success)
-{
- if (is_success)
- {
- h->cb (exec_ctx, h->user_data, GRPC_SECURITY_OK, h->wrapped_endpoint, h->secure_endpoint);
+static void security_handshake_done(grpc_exec_ctx *exec_ctx,
+ grpc_security_handshake *h,
+ int is_success) {
+ if (is_success) {
+ h->cb(exec_ctx, h->user_data, GRPC_SECURITY_OK, h->wrapped_endpoint,
+ h->secure_endpoint);
+ } else {
+ if (h->secure_endpoint != NULL) {
+ grpc_endpoint_shutdown(exec_ctx, h->secure_endpoint);
+ grpc_endpoint_destroy(exec_ctx, h->secure_endpoint);
+ } else {
+ grpc_endpoint_destroy(exec_ctx, h->wrapped_endpoint);
}
- else
- {
- if (h->secure_endpoint != NULL)
- {
- grpc_endpoint_shutdown (exec_ctx, h->secure_endpoint);
- grpc_endpoint_destroy (exec_ctx, h->secure_endpoint);
- }
- else
- {
- grpc_endpoint_destroy (exec_ctx, h->wrapped_endpoint);
- }
- h->cb (exec_ctx, h->user_data, GRPC_SECURITY_ERROR, h->wrapped_endpoint, NULL);
- }
- if (h->handshaker != NULL)
- tsi_handshaker_destroy (h->handshaker);
- if (h->handshake_buffer != NULL)
- gpr_free (h->handshake_buffer);
- gpr_slice_buffer_destroy (&h->left_overs);
- gpr_slice_buffer_destroy (&h->outgoing);
- gpr_slice_buffer_destroy (&h->incoming);
- GRPC_SECURITY_CONNECTOR_UNREF (h->connector, "handshake");
- gpr_free (h);
+ h->cb(exec_ctx, h->user_data, GRPC_SECURITY_ERROR, h->wrapped_endpoint,
+ NULL);
+ }
+ if (h->handshaker != NULL) tsi_handshaker_destroy(h->handshaker);
+ if (h->handshake_buffer != NULL) gpr_free(h->handshake_buffer);
+ gpr_slice_buffer_destroy(&h->left_overs);
+ gpr_slice_buffer_destroy(&h->outgoing);
+ gpr_slice_buffer_destroy(&h->incoming);
+ GRPC_SECURITY_CONNECTOR_UNREF(h->connector, "handshake");
+ gpr_free(h);
}
-static void
-on_peer_checked (grpc_exec_ctx * exec_ctx, void *user_data, grpc_security_status status)
-{
+static void on_peer_checked(grpc_exec_ctx *exec_ctx, void *user_data,
+ grpc_security_status status) {
grpc_security_handshake *h = user_data;
tsi_frame_protector *protector;
tsi_result result;
- if (status != GRPC_SECURITY_OK)
- {
- gpr_log (GPR_ERROR, "Error checking peer.");
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
- result = tsi_handshaker_create_frame_protector (h->handshaker, NULL, &protector);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Frame protector creation failed with error %s.", tsi_result_to_string (result));
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
- h->secure_endpoint = grpc_secure_endpoint_create (protector, h->wrapped_endpoint, h->left_overs.slices, h->left_overs.count);
+ if (status != GRPC_SECURITY_OK) {
+ gpr_log(GPR_ERROR, "Error checking peer.");
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
+ result =
+ tsi_handshaker_create_frame_protector(h->handshaker, NULL, &protector);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Frame protector creation failed with error %s.",
+ tsi_result_to_string(result));
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
+ h->secure_endpoint =
+ grpc_secure_endpoint_create(protector, h->wrapped_endpoint,
+ h->left_overs.slices, h->left_overs.count);
h->left_overs.count = 0;
h->left_overs.length = 0;
- security_handshake_done (exec_ctx, h, 1);
+ security_handshake_done(exec_ctx, h, 1);
return;
}
-static void
-check_peer (grpc_exec_ctx * exec_ctx, grpc_security_handshake * h)
-{
+static void check_peer(grpc_exec_ctx *exec_ctx, grpc_security_handshake *h) {
grpc_security_status peer_status;
tsi_peer peer;
- tsi_result result = tsi_handshaker_extract_peer (h->handshaker, &peer);
+ tsi_result result = tsi_handshaker_extract_peer(h->handshaker, &peer);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Peer extraction failed with error %s", tsi_result_to_string (result));
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
- peer_status = grpc_security_connector_check_peer (h->connector, peer, on_peer_checked, h);
- if (peer_status == GRPC_SECURITY_ERROR)
- {
- gpr_log (GPR_ERROR, "Peer check failed.");
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
- else if (peer_status == GRPC_SECURITY_OK)
- {
- on_peer_checked (exec_ctx, h, peer_status);
- }
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Peer extraction failed with error %s",
+ tsi_result_to_string(result));
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
+ peer_status = grpc_security_connector_check_peer(h->connector, peer,
+ on_peer_checked, h);
+ if (peer_status == GRPC_SECURITY_ERROR) {
+ gpr_log(GPR_ERROR, "Peer check failed.");
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ } else if (peer_status == GRPC_SECURITY_OK) {
+ on_peer_checked(exec_ctx, h, peer_status);
+ }
}
-static void
-send_handshake_bytes_to_peer (grpc_exec_ctx * exec_ctx, grpc_security_handshake * h)
-{
+static void send_handshake_bytes_to_peer(grpc_exec_ctx *exec_ctx,
+ grpc_security_handshake *h) {
size_t offset = 0;
tsi_result result = TSI_OK;
gpr_slice to_send;
- do
- {
- size_t to_send_size = h->handshake_buffer_size - offset;
- result = tsi_handshaker_get_bytes_to_send_to_peer (h->handshaker, h->handshake_buffer + offset, &to_send_size);
- offset += to_send_size;
- if (result == TSI_INCOMPLETE_DATA)
- {
- h->handshake_buffer_size *= 2;
- h->handshake_buffer = gpr_realloc (h->handshake_buffer, h->handshake_buffer_size);
- }
+ do {
+ size_t to_send_size = h->handshake_buffer_size - offset;
+ result = tsi_handshaker_get_bytes_to_send_to_peer(
+ h->handshaker, h->handshake_buffer + offset, &to_send_size);
+ offset += to_send_size;
+ if (result == TSI_INCOMPLETE_DATA) {
+ h->handshake_buffer_size *= 2;
+ h->handshake_buffer =
+ gpr_realloc(h->handshake_buffer, h->handshake_buffer_size);
}
- while (result == TSI_INCOMPLETE_DATA);
+ } while (result == TSI_INCOMPLETE_DATA);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshake failed with error %s", tsi_result_to_string (result));
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshake failed with error %s",
+ tsi_result_to_string(result));
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
- to_send = gpr_slice_from_copied_buffer ((const char *) h->handshake_buffer, offset);
- gpr_slice_buffer_reset_and_unref (&h->outgoing);
- gpr_slice_buffer_add (&h->outgoing, to_send);
+ to_send =
+ gpr_slice_from_copied_buffer((const char *)h->handshake_buffer, offset);
+ gpr_slice_buffer_reset_and_unref(&h->outgoing);
+ gpr_slice_buffer_add(&h->outgoing, to_send);
/* TODO(klempner,jboeuf): This should probably use the client setup
deadline */
- grpc_endpoint_write (exec_ctx, h->wrapped_endpoint, &h->outgoing, &h->on_handshake_data_sent_to_peer);
+ grpc_endpoint_write(exec_ctx, h->wrapped_endpoint, &h->outgoing,
+ &h->on_handshake_data_sent_to_peer);
}
-static void
-on_handshake_data_received_from_peer (grpc_exec_ctx * exec_ctx, void *handshake, int success)
-{
+static void on_handshake_data_received_from_peer(grpc_exec_ctx *exec_ctx,
+ void *handshake, int success) {
grpc_security_handshake *h = handshake;
size_t consumed_slice_size = 0;
tsi_result result = TSI_OK;
@@ -191,106 +182,107 @@ on_handshake_data_received_from_peer (grpc_exec_ctx * exec_ctx, void *handshake,
size_t num_left_overs;
int has_left_overs_in_current_slice = 0;
- if (!success)
- {
- gpr_log (GPR_ERROR, "Read failed.");
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
-
- for (i = 0; i < h->incoming.count; i++)
- {
- consumed_slice_size = GPR_SLICE_LENGTH (h->incoming.slices[i]);
- result = tsi_handshaker_process_bytes_from_peer (h->handshaker, GPR_SLICE_START_PTR (h->incoming.slices[i]), &consumed_slice_size);
- if (!tsi_handshaker_is_in_progress (h->handshaker))
- break;
- }
+ if (!success) {
+ gpr_log(GPR_ERROR, "Read failed.");
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
- if (tsi_handshaker_is_in_progress (h->handshaker))
- {
- /* We may need more data. */
- if (result == TSI_INCOMPLETE_DATA)
- {
- grpc_endpoint_read (exec_ctx, h->wrapped_endpoint, &h->incoming, &h->on_handshake_data_received_from_peer);
- return;
- }
- else
- {
- send_handshake_bytes_to_peer (exec_ctx, h);
- return;
- }
- }
+ for (i = 0; i < h->incoming.count; i++) {
+ consumed_slice_size = GPR_SLICE_LENGTH(h->incoming.slices[i]);
+ result = tsi_handshaker_process_bytes_from_peer(
+ h->handshaker, GPR_SLICE_START_PTR(h->incoming.slices[i]),
+ &consumed_slice_size);
+ if (!tsi_handshaker_is_in_progress(h->handshaker)) break;
+ }
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshake failed with error %s", tsi_result_to_string (result));
- security_handshake_done (exec_ctx, h, 0);
+ if (tsi_handshaker_is_in_progress(h->handshaker)) {
+ /* We may need more data. */
+ if (result == TSI_INCOMPLETE_DATA) {
+ grpc_endpoint_read(exec_ctx, h->wrapped_endpoint, &h->incoming,
+ &h->on_handshake_data_received_from_peer);
+ return;
+ } else {
+ send_handshake_bytes_to_peer(exec_ctx, h);
return;
}
+ }
+
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshake failed with error %s",
+ tsi_result_to_string(result));
+ security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
/* Handshake is done and successful this point. */
- has_left_overs_in_current_slice = (consumed_slice_size < GPR_SLICE_LENGTH (h->incoming.slices[i]));
- num_left_overs = (has_left_overs_in_current_slice ? 1 : 0) + h->incoming.count - i - 1;
- if (num_left_overs == 0)
- {
- check_peer (exec_ctx, h);
- return;
- }
+ has_left_overs_in_current_slice =
+ (consumed_slice_size < GPR_SLICE_LENGTH(h->incoming.slices[i]));
+ num_left_overs =
+ (has_left_overs_in_current_slice ? 1 : 0) + h->incoming.count - i - 1;
+ if (num_left_overs == 0) {
+ check_peer(exec_ctx, h);
+ return;
+ }
/* Put the leftovers in our buffer (ownership transfered). */
- if (has_left_overs_in_current_slice)
- {
- gpr_slice_buffer_add (&h->left_overs, gpr_slice_split_tail (&h->incoming.slices[i], consumed_slice_size));
- gpr_slice_unref (h->incoming.slices[i]); /* split_tail above increments refcount. */
- }
- gpr_slice_buffer_addn (&h->left_overs, &h->incoming.slices[i + 1], num_left_overs - (size_t) has_left_overs_in_current_slice);
- check_peer (exec_ctx, h);
+ if (has_left_overs_in_current_slice) {
+ gpr_slice_buffer_add(
+ &h->left_overs,
+ gpr_slice_split_tail(&h->incoming.slices[i], consumed_slice_size));
+ gpr_slice_unref(
+ h->incoming.slices[i]); /* split_tail above increments refcount. */
+ }
+ gpr_slice_buffer_addn(
+ &h->left_overs, &h->incoming.slices[i + 1],
+ num_left_overs - (size_t)has_left_overs_in_current_slice);
+ check_peer(exec_ctx, h);
}
/* If handshake is NULL, the handshake is done. */
-static void
-on_handshake_data_sent_to_peer (grpc_exec_ctx * exec_ctx, void *handshake, int success)
-{
+static void on_handshake_data_sent_to_peer(grpc_exec_ctx *exec_ctx,
+ void *handshake, int success) {
grpc_security_handshake *h = handshake;
/* Make sure that write is OK. */
- if (!success)
- {
- gpr_log (GPR_ERROR, "Write failed.");
- if (handshake != NULL)
- security_handshake_done (exec_ctx, h, 0);
- return;
- }
+ if (!success) {
+ gpr_log(GPR_ERROR, "Write failed.");
+ if (handshake != NULL) security_handshake_done(exec_ctx, h, 0);
+ return;
+ }
/* We may be done. */
- if (tsi_handshaker_is_in_progress (h->handshaker))
- {
- /* TODO(klempner,jboeuf): This should probably use the client setup
- deadline */
- grpc_endpoint_read (exec_ctx, h->wrapped_endpoint, &h->incoming, &h->on_handshake_data_received_from_peer);
- }
- else
- {
- check_peer (exec_ctx, h);
- }
+ if (tsi_handshaker_is_in_progress(h->handshaker)) {
+ /* TODO(klempner,jboeuf): This should probably use the client setup
+ deadline */
+ grpc_endpoint_read(exec_ctx, h->wrapped_endpoint, &h->incoming,
+ &h->on_handshake_data_received_from_peer);
+ } else {
+ check_peer(exec_ctx, h);
+ }
}
-void
-grpc_do_security_handshake (grpc_exec_ctx * exec_ctx, tsi_handshaker * handshaker, grpc_security_connector * connector, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- grpc_security_handshake *h = gpr_malloc (sizeof (grpc_security_handshake));
- memset (h, 0, sizeof (grpc_security_handshake));
+void grpc_do_security_handshake(grpc_exec_ctx *exec_ctx,
+ tsi_handshaker *handshaker,
+ grpc_security_connector *connector,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ grpc_security_handshake *h = gpr_malloc(sizeof(grpc_security_handshake));
+ memset(h, 0, sizeof(grpc_security_handshake));
h->handshaker = handshaker;
- h->connector = GRPC_SECURITY_CONNECTOR_REF (connector, "handshake");
+ h->connector = GRPC_SECURITY_CONNECTOR_REF(connector, "handshake");
h->handshake_buffer_size = GRPC_INITIAL_HANDSHAKE_BUFFER_SIZE;
- h->handshake_buffer = gpr_malloc (h->handshake_buffer_size);
+ h->handshake_buffer = gpr_malloc(h->handshake_buffer_size);
h->wrapped_endpoint = nonsecure_endpoint;
h->user_data = user_data;
h->cb = cb;
- grpc_closure_init (&h->on_handshake_data_sent_to_peer, on_handshake_data_sent_to_peer, h);
- grpc_closure_init (&h->on_handshake_data_received_from_peer, on_handshake_data_received_from_peer, h);
- gpr_slice_buffer_init (&h->left_overs);
- gpr_slice_buffer_init (&h->outgoing);
- gpr_slice_buffer_init (&h->incoming);
- send_handshake_bytes_to_peer (exec_ctx, h);
+ grpc_closure_init(&h->on_handshake_data_sent_to_peer,
+ on_handshake_data_sent_to_peer, h);
+ grpc_closure_init(&h->on_handshake_data_received_from_peer,
+ on_handshake_data_received_from_peer, h);
+ gpr_slice_buffer_init(&h->left_overs);
+ gpr_slice_buffer_init(&h->outgoing);
+ gpr_slice_buffer_init(&h->incoming);
+ send_handshake_bytes_to_peer(exec_ctx, h);
}
diff --git a/src/core/security/handshake.h b/src/core/security/handshake.h
index 84798e96ec..28eaa79dc3 100644
--- a/src/core/security/handshake.h
+++ b/src/core/security/handshake.h
@@ -38,6 +38,11 @@
#include "src/core/security/security_connector.h"
/* Calls the callback upon completion. Takes owership of handshaker. */
-void grpc_do_security_handshake (grpc_exec_ctx * exec_ctx, tsi_handshaker * handshaker, grpc_security_connector * connector, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data);
+void grpc_do_security_handshake(grpc_exec_ctx *exec_ctx,
+ tsi_handshaker *handshaker,
+ grpc_security_connector *connector,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data);
#endif /* GRPC_INTERNAL_CORE_SECURITY_HANDSHAKE_H */
diff --git a/src/core/security/json_token.c b/src/core/security/json_token.c
index cf9b25bdc6..021912f333 100644
--- a/src/core/security/json_token.c
+++ b/src/core/security/json_token.c
@@ -49,7 +49,7 @@
/* --- Constants. --- */
/* 1 hour max. */
-const gpr_timespec grpc_max_auth_token_lifetime = { 3600, 0, GPR_TIMESPAN };
+const gpr_timespec grpc_max_auth_token_lifetime = {3600, 0, GPR_TIMESPAN};
#define GRPC_JWT_RSA_SHA256_ALGORITHM "RS256"
#define GRPC_JWT_TYPE "JWT"
@@ -60,398 +60,346 @@ static grpc_jwt_encode_and_sign_override g_jwt_encode_and_sign_override = NULL;
/* --- grpc_auth_json_key. --- */
-static const char *
-json_get_string_property (const grpc_json * json, const char *prop_name)
-{
+static const char *json_get_string_property(const grpc_json *json,
+ const char *prop_name) {
grpc_json *child;
- for (child = json->child; child != NULL; child = child->next)
- {
- if (strcmp (child->key, prop_name) == 0)
- break;
- }
- if (child == NULL || child->type != GRPC_JSON_STRING)
- {
- gpr_log (GPR_ERROR, "Invalid or missing %s property.", prop_name);
- return NULL;
- }
+ for (child = json->child; child != NULL; child = child->next) {
+ if (strcmp(child->key, prop_name) == 0) break;
+ }
+ if (child == NULL || child->type != GRPC_JSON_STRING) {
+ gpr_log(GPR_ERROR, "Invalid or missing %s property.", prop_name);
+ return NULL;
+ }
return child->value;
}
-static int
-set_json_key_string_property (const grpc_json * json, const char *prop_name, char **json_key_field)
-{
- const char *prop_value = json_get_string_property (json, prop_name);
- if (prop_value == NULL)
- return 0;
- *json_key_field = gpr_strdup (prop_value);
+static int set_json_key_string_property(const grpc_json *json,
+ const char *prop_name,
+ char **json_key_field) {
+ const char *prop_value = json_get_string_property(json, prop_name);
+ if (prop_value == NULL) return 0;
+ *json_key_field = gpr_strdup(prop_value);
return 1;
}
-int
-grpc_auth_json_key_is_valid (const grpc_auth_json_key * json_key)
-{
- return (json_key != NULL) && strcmp (json_key->type, GRPC_AUTH_JSON_TYPE_INVALID);
+int grpc_auth_json_key_is_valid(const grpc_auth_json_key *json_key) {
+ return (json_key != NULL) &&
+ strcmp(json_key->type, GRPC_AUTH_JSON_TYPE_INVALID);
}
-grpc_auth_json_key
-grpc_auth_json_key_create_from_json (const grpc_json * json)
-{
+grpc_auth_json_key grpc_auth_json_key_create_from_json(const grpc_json *json) {
grpc_auth_json_key result;
BIO *bio = NULL;
const char *prop_value;
int success = 0;
- memset (&result, 0, sizeof (grpc_auth_json_key));
+ memset(&result, 0, sizeof(grpc_auth_json_key));
result.type = GRPC_AUTH_JSON_TYPE_INVALID;
- if (json == NULL)
- {
- gpr_log (GPR_ERROR, "Invalid json.");
- goto end;
- }
-
- prop_value = json_get_string_property (json, "type");
- if (prop_value == NULL || strcmp (prop_value, GRPC_AUTH_JSON_TYPE_SERVICE_ACCOUNT))
- {
- goto end;
- }
+ if (json == NULL) {
+ gpr_log(GPR_ERROR, "Invalid json.");
+ goto end;
+ }
+
+ prop_value = json_get_string_property(json, "type");
+ if (prop_value == NULL ||
+ strcmp(prop_value, GRPC_AUTH_JSON_TYPE_SERVICE_ACCOUNT)) {
+ goto end;
+ }
result.type = GRPC_AUTH_JSON_TYPE_SERVICE_ACCOUNT;
- if (!set_json_key_string_property (json, "private_key_id", &result.private_key_id) || !set_json_key_string_property (json, "client_id", &result.client_id) || !set_json_key_string_property (json, "client_email", &result.client_email))
- {
- goto end;
- }
-
- prop_value = json_get_string_property (json, "private_key");
- if (prop_value == NULL)
- {
- goto end;
- }
- bio = BIO_new (BIO_s_mem ());
- success = BIO_puts (bio, prop_value);
- if ((success < 0) || ((size_t) success != strlen (prop_value)))
- {
- gpr_log (GPR_ERROR, "Could not write into openssl BIO.");
- goto end;
- }
- result.private_key = PEM_read_bio_RSAPrivateKey (bio, NULL, NULL, "");
- if (result.private_key == NULL)
- {
- gpr_log (GPR_ERROR, "Could not deserialize private key.");
- goto end;
- }
+ if (!set_json_key_string_property(json, "private_key_id",
+ &result.private_key_id) ||
+ !set_json_key_string_property(json, "client_id", &result.client_id) ||
+ !set_json_key_string_property(json, "client_email",
+ &result.client_email)) {
+ goto end;
+ }
+
+ prop_value = json_get_string_property(json, "private_key");
+ if (prop_value == NULL) {
+ goto end;
+ }
+ bio = BIO_new(BIO_s_mem());
+ success = BIO_puts(bio, prop_value);
+ if ((success < 0) || ((size_t)success != strlen(prop_value))) {
+ gpr_log(GPR_ERROR, "Could not write into openssl BIO.");
+ goto end;
+ }
+ result.private_key = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, "");
+ if (result.private_key == NULL) {
+ gpr_log(GPR_ERROR, "Could not deserialize private key.");
+ goto end;
+ }
success = 1;
end:
- if (bio != NULL)
- BIO_free (bio);
- if (!success)
- grpc_auth_json_key_destruct (&result);
+ if (bio != NULL) BIO_free(bio);
+ if (!success) grpc_auth_json_key_destruct(&result);
return result;
}
-grpc_auth_json_key
-grpc_auth_json_key_create_from_string (const char *json_string)
-{
- char *scratchpad = gpr_strdup (json_string);
- grpc_json *json = grpc_json_parse_string (scratchpad);
- grpc_auth_json_key result = grpc_auth_json_key_create_from_json (json);
- if (json != NULL)
- grpc_json_destroy (json);
- gpr_free (scratchpad);
+grpc_auth_json_key grpc_auth_json_key_create_from_string(
+ const char *json_string) {
+ char *scratchpad = gpr_strdup(json_string);
+ grpc_json *json = grpc_json_parse_string(scratchpad);
+ grpc_auth_json_key result = grpc_auth_json_key_create_from_json(json);
+ if (json != NULL) grpc_json_destroy(json);
+ gpr_free(scratchpad);
return result;
}
-void
-grpc_auth_json_key_destruct (grpc_auth_json_key * json_key)
-{
- if (json_key == NULL)
- return;
+void grpc_auth_json_key_destruct(grpc_auth_json_key *json_key) {
+ if (json_key == NULL) return;
json_key->type = GRPC_AUTH_JSON_TYPE_INVALID;
- if (json_key->client_id != NULL)
- {
- gpr_free (json_key->client_id);
- json_key->client_id = NULL;
- }
- if (json_key->private_key_id != NULL)
- {
- gpr_free (json_key->private_key_id);
- json_key->private_key_id = NULL;
- }
- if (json_key->client_email != NULL)
- {
- gpr_free (json_key->client_email);
- json_key->client_email = NULL;
- }
- if (json_key->private_key != NULL)
- {
- RSA_free (json_key->private_key);
- json_key->private_key = NULL;
- }
+ if (json_key->client_id != NULL) {
+ gpr_free(json_key->client_id);
+ json_key->client_id = NULL;
+ }
+ if (json_key->private_key_id != NULL) {
+ gpr_free(json_key->private_key_id);
+ json_key->private_key_id = NULL;
+ }
+ if (json_key->client_email != NULL) {
+ gpr_free(json_key->client_email);
+ json_key->client_email = NULL;
+ }
+ if (json_key->private_key != NULL) {
+ RSA_free(json_key->private_key);
+ json_key->private_key = NULL;
+ }
}
/* --- jwt encoding and signature. --- */
-static grpc_json *
-create_child (grpc_json * brother, grpc_json * parent, const char *key, const char *value, grpc_json_type type)
-{
- grpc_json *child = grpc_json_create (type);
- if (brother)
- brother->next = child;
- if (!parent->child)
- parent->child = child;
+static grpc_json *create_child(grpc_json *brother, grpc_json *parent,
+ const char *key, const char *value,
+ grpc_json_type type) {
+ grpc_json *child = grpc_json_create(type);
+ if (brother) brother->next = child;
+ if (!parent->child) parent->child = child;
child->parent = parent;
child->value = value;
child->key = key;
return child;
}
-static char *
-encoded_jwt_header (const char *key_id, const char *algorithm)
-{
- grpc_json *json = grpc_json_create (GRPC_JSON_OBJECT);
+static char *encoded_jwt_header(const char *key_id, const char *algorithm) {
+ grpc_json *json = grpc_json_create(GRPC_JSON_OBJECT);
grpc_json *child = NULL;
char *json_str = NULL;
char *result = NULL;
- child = create_child (NULL, json, "alg", algorithm, GRPC_JSON_STRING);
- child = create_child (child, json, "typ", GRPC_JWT_TYPE, GRPC_JSON_STRING);
- create_child (child, json, "kid", key_id, GRPC_JSON_STRING);
+ child = create_child(NULL, json, "alg", algorithm, GRPC_JSON_STRING);
+ child = create_child(child, json, "typ", GRPC_JWT_TYPE, GRPC_JSON_STRING);
+ create_child(child, json, "kid", key_id, GRPC_JSON_STRING);
- json_str = grpc_json_dump_to_string (json, 0);
- result = grpc_base64_encode (json_str, strlen (json_str), 1, 0);
- gpr_free (json_str);
- grpc_json_destroy (json);
+ json_str = grpc_json_dump_to_string(json, 0);
+ result = grpc_base64_encode(json_str, strlen(json_str), 1, 0);
+ gpr_free(json_str);
+ grpc_json_destroy(json);
return result;
}
-static char *
-encoded_jwt_claim (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope)
-{
- grpc_json *json = grpc_json_create (GRPC_JSON_OBJECT);
+static char *encoded_jwt_claim(const grpc_auth_json_key *json_key,
+ const char *audience,
+ gpr_timespec token_lifetime, const char *scope) {
+ grpc_json *json = grpc_json_create(GRPC_JSON_OBJECT);
grpc_json *child = NULL;
char *json_str = NULL;
char *result = NULL;
- gpr_timespec now = gpr_now (GPR_CLOCK_REALTIME);
- gpr_timespec expiration = gpr_time_add (now, token_lifetime);
+ gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
+ gpr_timespec expiration = gpr_time_add(now, token_lifetime);
char now_str[GPR_LTOA_MIN_BUFSIZE];
char expiration_str[GPR_LTOA_MIN_BUFSIZE];
- if (gpr_time_cmp (token_lifetime, grpc_max_auth_token_lifetime) > 0)
- {
- gpr_log (GPR_INFO, "Cropping token lifetime to maximum allowed value.");
- expiration = gpr_time_add (now, grpc_max_auth_token_lifetime);
- }
- gpr_ltoa (now.tv_sec, now_str);
- gpr_ltoa (expiration.tv_sec, expiration_str);
-
- child = create_child (NULL, json, "iss", json_key->client_email, GRPC_JSON_STRING);
- if (scope != NULL)
- {
- child = create_child (child, json, "scope", scope, GRPC_JSON_STRING);
- }
- else
- {
- /* Unscoped JWTs need a sub field. */
- child = create_child (child, json, "sub", json_key->client_email, GRPC_JSON_STRING);
- }
-
- child = create_child (child, json, "aud", audience, GRPC_JSON_STRING);
- child = create_child (child, json, "iat", now_str, GRPC_JSON_NUMBER);
- create_child (child, json, "exp", expiration_str, GRPC_JSON_NUMBER);
-
- json_str = grpc_json_dump_to_string (json, 0);
- result = grpc_base64_encode (json_str, strlen (json_str), 1, 0);
- gpr_free (json_str);
- grpc_json_destroy (json);
+ if (gpr_time_cmp(token_lifetime, grpc_max_auth_token_lifetime) > 0) {
+ gpr_log(GPR_INFO, "Cropping token lifetime to maximum allowed value.");
+ expiration = gpr_time_add(now, grpc_max_auth_token_lifetime);
+ }
+ gpr_ltoa(now.tv_sec, now_str);
+ gpr_ltoa(expiration.tv_sec, expiration_str);
+
+ child =
+ create_child(NULL, json, "iss", json_key->client_email, GRPC_JSON_STRING);
+ if (scope != NULL) {
+ child = create_child(child, json, "scope", scope, GRPC_JSON_STRING);
+ } else {
+ /* Unscoped JWTs need a sub field. */
+ child = create_child(child, json, "sub", json_key->client_email,
+ GRPC_JSON_STRING);
+ }
+
+ child = create_child(child, json, "aud", audience, GRPC_JSON_STRING);
+ child = create_child(child, json, "iat", now_str, GRPC_JSON_NUMBER);
+ create_child(child, json, "exp", expiration_str, GRPC_JSON_NUMBER);
+
+ json_str = grpc_json_dump_to_string(json, 0);
+ result = grpc_base64_encode(json_str, strlen(json_str), 1, 0);
+ gpr_free(json_str);
+ grpc_json_destroy(json);
return result;
}
-static char *
-dot_concat_and_free_strings (char *str1, char *str2)
-{
- size_t str1_len = strlen (str1);
- size_t str2_len = strlen (str2);
- size_t result_len = str1_len + 1 /* dot */ + str2_len;
- char *result = gpr_malloc (result_len + 1 /* NULL terminated */ );
+static char *dot_concat_and_free_strings(char *str1, char *str2) {
+ size_t str1_len = strlen(str1);
+ size_t str2_len = strlen(str2);
+ size_t result_len = str1_len + 1 /* dot */ + str2_len;
+ char *result = gpr_malloc(result_len + 1 /* NULL terminated */);
char *current = result;
- memcpy (current, str1, str1_len);
+ memcpy(current, str1, str1_len);
current += str1_len;
*(current++) = '.';
- memcpy (current, str2, str2_len);
+ memcpy(current, str2, str2_len);
current += str2_len;
- GPR_ASSERT (current >= result);
- GPR_ASSERT ((gpr_uintptr) (current - result) == result_len);
+ GPR_ASSERT(current >= result);
+ GPR_ASSERT((gpr_uintptr)(current - result) == result_len);
*current = '\0';
- gpr_free (str1);
- gpr_free (str2);
+ gpr_free(str1);
+ gpr_free(str2);
return result;
}
-const EVP_MD *
-openssl_digest_from_algorithm (const char *algorithm)
-{
- if (strcmp (algorithm, GRPC_JWT_RSA_SHA256_ALGORITHM) == 0)
- {
- return EVP_sha256 ();
- }
- else
- {
- gpr_log (GPR_ERROR, "Unknown algorithm %s.", algorithm);
- return NULL;
- }
+const EVP_MD *openssl_digest_from_algorithm(const char *algorithm) {
+ if (strcmp(algorithm, GRPC_JWT_RSA_SHA256_ALGORITHM) == 0) {
+ return EVP_sha256();
+ } else {
+ gpr_log(GPR_ERROR, "Unknown algorithm %s.", algorithm);
+ return NULL;
+ }
}
-char *
-compute_and_encode_signature (const grpc_auth_json_key * json_key, const char *signature_algorithm, const char *to_sign)
-{
- const EVP_MD *md = openssl_digest_from_algorithm (signature_algorithm);
+char *compute_and_encode_signature(const grpc_auth_json_key *json_key,
+ const char *signature_algorithm,
+ const char *to_sign) {
+ const EVP_MD *md = openssl_digest_from_algorithm(signature_algorithm);
EVP_MD_CTX *md_ctx = NULL;
- EVP_PKEY *key = EVP_PKEY_new ();
+ EVP_PKEY *key = EVP_PKEY_new();
size_t sig_len = 0;
unsigned char *sig = NULL;
char *result = NULL;
- if (md == NULL)
- return NULL;
- md_ctx = EVP_MD_CTX_create ();
- if (md_ctx == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create MD_CTX");
- goto end;
- }
- EVP_PKEY_set1_RSA (key, json_key->private_key);
- if (EVP_DigestSignInit (md_ctx, NULL, md, NULL, key) != 1)
- {
- gpr_log (GPR_ERROR, "DigestInit failed.");
- goto end;
- }
- if (EVP_DigestSignUpdate (md_ctx, to_sign, strlen (to_sign)) != 1)
- {
- gpr_log (GPR_ERROR, "DigestUpdate failed.");
- goto end;
- }
- if (EVP_DigestSignFinal (md_ctx, NULL, &sig_len) != 1)
- {
- gpr_log (GPR_ERROR, "DigestFinal (get signature length) failed.");
- goto end;
- }
- sig = gpr_malloc (sig_len);
- if (EVP_DigestSignFinal (md_ctx, sig, &sig_len) != 1)
- {
- gpr_log (GPR_ERROR, "DigestFinal (signature compute) failed.");
- goto end;
- }
- result = grpc_base64_encode (sig, sig_len, 1, 0);
+ if (md == NULL) return NULL;
+ md_ctx = EVP_MD_CTX_create();
+ if (md_ctx == NULL) {
+ gpr_log(GPR_ERROR, "Could not create MD_CTX");
+ goto end;
+ }
+ EVP_PKEY_set1_RSA(key, json_key->private_key);
+ if (EVP_DigestSignInit(md_ctx, NULL, md, NULL, key) != 1) {
+ gpr_log(GPR_ERROR, "DigestInit failed.");
+ goto end;
+ }
+ if (EVP_DigestSignUpdate(md_ctx, to_sign, strlen(to_sign)) != 1) {
+ gpr_log(GPR_ERROR, "DigestUpdate failed.");
+ goto end;
+ }
+ if (EVP_DigestSignFinal(md_ctx, NULL, &sig_len) != 1) {
+ gpr_log(GPR_ERROR, "DigestFinal (get signature length) failed.");
+ goto end;
+ }
+ sig = gpr_malloc(sig_len);
+ if (EVP_DigestSignFinal(md_ctx, sig, &sig_len) != 1) {
+ gpr_log(GPR_ERROR, "DigestFinal (signature compute) failed.");
+ goto end;
+ }
+ result = grpc_base64_encode(sig, sig_len, 1, 0);
end:
- if (key != NULL)
- EVP_PKEY_free (key);
- if (md_ctx != NULL)
- EVP_MD_CTX_destroy (md_ctx);
- if (sig != NULL)
- gpr_free (sig);
+ if (key != NULL) EVP_PKEY_free(key);
+ if (md_ctx != NULL) EVP_MD_CTX_destroy(md_ctx);
+ if (sig != NULL) gpr_free(sig);
return result;
}
-char *
-grpc_jwt_encode_and_sign (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope)
-{
- if (g_jwt_encode_and_sign_override != NULL)
- {
- return g_jwt_encode_and_sign_override (json_key, audience, token_lifetime, scope);
- }
- else
- {
- const char *sig_algo = GRPC_JWT_RSA_SHA256_ALGORITHM;
- char *to_sign = dot_concat_and_free_strings (encoded_jwt_header (json_key->private_key_id, sig_algo),
- encoded_jwt_claim (json_key, audience, token_lifetime, scope));
- char *sig = compute_and_encode_signature (json_key, sig_algo, to_sign);
- if (sig == NULL)
- {
- gpr_free (to_sign);
- return NULL;
- }
- return dot_concat_and_free_strings (to_sign, sig);
+char *grpc_jwt_encode_and_sign(const grpc_auth_json_key *json_key,
+ const char *audience,
+ gpr_timespec token_lifetime, const char *scope) {
+ if (g_jwt_encode_and_sign_override != NULL) {
+ return g_jwt_encode_and_sign_override(json_key, audience, token_lifetime,
+ scope);
+ } else {
+ const char *sig_algo = GRPC_JWT_RSA_SHA256_ALGORITHM;
+ char *to_sign = dot_concat_and_free_strings(
+ encoded_jwt_header(json_key->private_key_id, sig_algo),
+ encoded_jwt_claim(json_key, audience, token_lifetime, scope));
+ char *sig = compute_and_encode_signature(json_key, sig_algo, to_sign);
+ if (sig == NULL) {
+ gpr_free(to_sign);
+ return NULL;
}
+ return dot_concat_and_free_strings(to_sign, sig);
+ }
}
-void
-grpc_jwt_encode_and_sign_set_override (grpc_jwt_encode_and_sign_override func)
-{
+void grpc_jwt_encode_and_sign_set_override(
+ grpc_jwt_encode_and_sign_override func) {
g_jwt_encode_and_sign_override = func;
}
/* --- grpc_auth_refresh_token --- */
-int
-grpc_auth_refresh_token_is_valid (const grpc_auth_refresh_token * refresh_token)
-{
- return (refresh_token != NULL) && strcmp (refresh_token->type, GRPC_AUTH_JSON_TYPE_INVALID);
+int grpc_auth_refresh_token_is_valid(
+ const grpc_auth_refresh_token *refresh_token) {
+ return (refresh_token != NULL) &&
+ strcmp(refresh_token->type, GRPC_AUTH_JSON_TYPE_INVALID);
}
-grpc_auth_refresh_token
-grpc_auth_refresh_token_create_from_json (const grpc_json * json)
-{
+grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json(
+ const grpc_json *json) {
grpc_auth_refresh_token result;
const char *prop_value;
int success = 0;
- memset (&result, 0, sizeof (grpc_auth_refresh_token));
+ memset(&result, 0, sizeof(grpc_auth_refresh_token));
result.type = GRPC_AUTH_JSON_TYPE_INVALID;
- if (json == NULL)
- {
- gpr_log (GPR_ERROR, "Invalid json.");
- goto end;
- }
-
- prop_value = json_get_string_property (json, "type");
- if (prop_value == NULL || strcmp (prop_value, GRPC_AUTH_JSON_TYPE_AUTHORIZED_USER))
- {
- goto end;
- }
+ if (json == NULL) {
+ gpr_log(GPR_ERROR, "Invalid json.");
+ goto end;
+ }
+
+ prop_value = json_get_string_property(json, "type");
+ if (prop_value == NULL ||
+ strcmp(prop_value, GRPC_AUTH_JSON_TYPE_AUTHORIZED_USER)) {
+ goto end;
+ }
result.type = GRPC_AUTH_JSON_TYPE_AUTHORIZED_USER;
- if (!set_json_key_string_property (json, "client_secret", &result.client_secret) || !set_json_key_string_property (json, "client_id", &result.client_id) || !set_json_key_string_property (json, "refresh_token", &result.refresh_token))
- {
- goto end;
- }
+ if (!set_json_key_string_property(json, "client_secret",
+ &result.client_secret) ||
+ !set_json_key_string_property(json, "client_id", &result.client_id) ||
+ !set_json_key_string_property(json, "refresh_token",
+ &result.refresh_token)) {
+ goto end;
+ }
success = 1;
end:
- if (!success)
- grpc_auth_refresh_token_destruct (&result);
+ if (!success) grpc_auth_refresh_token_destruct(&result);
return result;
}
-grpc_auth_refresh_token
-grpc_auth_refresh_token_create_from_string (const char *json_string)
-{
- char *scratchpad = gpr_strdup (json_string);
- grpc_json *json = grpc_json_parse_string (scratchpad);
- grpc_auth_refresh_token result = grpc_auth_refresh_token_create_from_json (json);
- if (json != NULL)
- grpc_json_destroy (json);
- gpr_free (scratchpad);
+grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string(
+ const char *json_string) {
+ char *scratchpad = gpr_strdup(json_string);
+ grpc_json *json = grpc_json_parse_string(scratchpad);
+ grpc_auth_refresh_token result =
+ grpc_auth_refresh_token_create_from_json(json);
+ if (json != NULL) grpc_json_destroy(json);
+ gpr_free(scratchpad);
return result;
}
-void
-grpc_auth_refresh_token_destruct (grpc_auth_refresh_token * refresh_token)
-{
- if (refresh_token == NULL)
- return;
+void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token *refresh_token) {
+ if (refresh_token == NULL) return;
refresh_token->type = GRPC_AUTH_JSON_TYPE_INVALID;
- if (refresh_token->client_id != NULL)
- {
- gpr_free (refresh_token->client_id);
- refresh_token->client_id = NULL;
- }
- if (refresh_token->client_secret != NULL)
- {
- gpr_free (refresh_token->client_secret);
- refresh_token->client_secret = NULL;
- }
- if (refresh_token->refresh_token != NULL)
- {
- gpr_free (refresh_token->refresh_token);
- refresh_token->refresh_token = NULL;
- }
+ if (refresh_token->client_id != NULL) {
+ gpr_free(refresh_token->client_id);
+ refresh_token->client_id = NULL;
+ }
+ if (refresh_token->client_secret != NULL) {
+ gpr_free(refresh_token->client_secret);
+ refresh_token->client_secret = NULL;
+ }
+ if (refresh_token->refresh_token != NULL) {
+ gpr_free(refresh_token->refresh_token);
+ refresh_token->refresh_token = NULL;
+ }
}
diff --git a/src/core/security/json_token.h b/src/core/security/json_token.h
index 1955bd5b89..7e06864ff3 100644
--- a/src/core/security/json_token.h
+++ b/src/core/security/json_token.h
@@ -49,8 +49,7 @@
/* --- auth_json_key parsing. --- */
-typedef struct
-{
+typedef struct {
const char *type;
char *private_key_id;
char *client_id;
@@ -59,35 +58,40 @@ typedef struct
} grpc_auth_json_key;
/* Returns 1 if the object is valid, 0 otherwise. */
-int grpc_auth_json_key_is_valid (const grpc_auth_json_key * json_key);
+int grpc_auth_json_key_is_valid(const grpc_auth_json_key *json_key);
/* Creates a json_key object from string. Returns an invalid object if a parsing
error has been encountered. */
-grpc_auth_json_key grpc_auth_json_key_create_from_string (const char *json_string);
+grpc_auth_json_key grpc_auth_json_key_create_from_string(
+ const char *json_string);
/* Creates a json_key object from parsed json. Returns an invalid object if a
parsing error has been encountered. */
-grpc_auth_json_key grpc_auth_json_key_create_from_json (const grpc_json * json);
+grpc_auth_json_key grpc_auth_json_key_create_from_json(const grpc_json *json);
/* Destructs the object. */
-void grpc_auth_json_key_destruct (grpc_auth_json_key * json_key);
+void grpc_auth_json_key_destruct(grpc_auth_json_key *json_key);
/* --- json token encoding and signing. --- */
/* Caller is responsible for calling gpr_free on the returned value. May return
NULL on invalid input. The scope parameter may be NULL. */
-char *grpc_jwt_encode_and_sign (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope);
+char *grpc_jwt_encode_and_sign(const grpc_auth_json_key *json_key,
+ const char *audience,
+ gpr_timespec token_lifetime, const char *scope);
/* Override encode_and_sign function for testing. */
-typedef char *(*grpc_jwt_encode_and_sign_override) (const grpc_auth_json_key * json_key, const char *audience, gpr_timespec token_lifetime, const char *scope);
+typedef char *(*grpc_jwt_encode_and_sign_override)(
+ const grpc_auth_json_key *json_key, const char *audience,
+ gpr_timespec token_lifetime, const char *scope);
/* Set a custom encode_and_sign override for testing. */
-void grpc_jwt_encode_and_sign_set_override (grpc_jwt_encode_and_sign_override func);
+void grpc_jwt_encode_and_sign_set_override(
+ grpc_jwt_encode_and_sign_override func);
/* --- auth_refresh_token parsing. --- */
-typedef struct
-{
+typedef struct {
const char *type;
char *client_id;
char *client_secret;
@@ -95,17 +99,20 @@ typedef struct
} grpc_auth_refresh_token;
/* Returns 1 if the object is valid, 0 otherwise. */
-int grpc_auth_refresh_token_is_valid (const grpc_auth_refresh_token * refresh_token);
+int grpc_auth_refresh_token_is_valid(
+ const grpc_auth_refresh_token *refresh_token);
/* Creates a refresh token object from string. Returns an invalid object if a
parsing error has been encountered. */
-grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string (const char *json_string);
+grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string(
+ const char *json_string);
/* Creates a refresh token object from parsed json. Returns an invalid object if
a parsing error has been encountered. */
-grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json (const grpc_json * json);
+grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json(
+ const grpc_json *json);
/* Destructs the object. */
-void grpc_auth_refresh_token_destruct (grpc_auth_refresh_token * refresh_token);
+void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token *refresh_token);
#endif /* GRPC_INTERNAL_CORE_SECURITY_JSON_TOKEN_H */
diff --git a/src/core/security/jwt_verifier.c b/src/core/security/jwt_verifier.c
index bc1e3f3b54..9de8482025 100644
--- a/src/core/security/jwt_verifier.c
+++ b/src/core/security/jwt_verifier.c
@@ -47,11 +47,9 @@
/* --- Utils. --- */
-const char *
-grpc_jwt_verifier_status_to_string (grpc_jwt_verifier_status status)
-{
- switch (status)
- {
+const char *grpc_jwt_verifier_status_to_string(
+ grpc_jwt_verifier_status status) {
+ switch (status) {
case GRPC_JWT_VERIFIER_OK:
return "OK";
case GRPC_JWT_VERIFIER_BAD_SIGNATURE:
@@ -68,78 +66,62 @@ grpc_jwt_verifier_status_to_string (grpc_jwt_verifier_status status)
return "GENERIC_ERROR";
default:
return "UNKNOWN";
- }
+ }
}
-static const EVP_MD *
-evp_md_from_alg (const char *alg)
-{
- if (strcmp (alg, "RS256") == 0)
- {
- return EVP_sha256 ();
- }
- else if (strcmp (alg, "RS384") == 0)
- {
- return EVP_sha384 ();
- }
- else if (strcmp (alg, "RS512") == 0)
- {
- return EVP_sha512 ();
- }
- else
- {
- return NULL;
- }
+static const EVP_MD *evp_md_from_alg(const char *alg) {
+ if (strcmp(alg, "RS256") == 0) {
+ return EVP_sha256();
+ } else if (strcmp(alg, "RS384") == 0) {
+ return EVP_sha384();
+ } else if (strcmp(alg, "RS512") == 0) {
+ return EVP_sha512();
+ } else {
+ return NULL;
+ }
}
-static grpc_json *
-parse_json_part_from_jwt (const char *str, size_t len, gpr_slice * buffer)
-{
+static grpc_json *parse_json_part_from_jwt(const char *str, size_t len,
+ gpr_slice *buffer) {
grpc_json *json;
- *buffer = grpc_base64_decode_with_len (str, len, 1);
- if (GPR_SLICE_IS_EMPTY (*buffer))
- {
- gpr_log (GPR_ERROR, "Invalid base64.");
- return NULL;
- }
- json = grpc_json_parse_string_with_len ((char *) GPR_SLICE_START_PTR (*buffer), GPR_SLICE_LENGTH (*buffer));
- if (json == NULL)
- {
- gpr_slice_unref (*buffer);
- gpr_log (GPR_ERROR, "JSON parsing error.");
- }
+ *buffer = grpc_base64_decode_with_len(str, len, 1);
+ if (GPR_SLICE_IS_EMPTY(*buffer)) {
+ gpr_log(GPR_ERROR, "Invalid base64.");
+ return NULL;
+ }
+ json = grpc_json_parse_string_with_len((char *)GPR_SLICE_START_PTR(*buffer),
+ GPR_SLICE_LENGTH(*buffer));
+ if (json == NULL) {
+ gpr_slice_unref(*buffer);
+ gpr_log(GPR_ERROR, "JSON parsing error.");
+ }
return json;
}
-static const char *
-validate_string_field (const grpc_json * json, const char *key)
-{
- if (json->type != GRPC_JSON_STRING)
- {
- gpr_log (GPR_ERROR, "Invalid %s field [%s]", key, json->value);
- return NULL;
- }
+static const char *validate_string_field(const grpc_json *json,
+ const char *key) {
+ if (json->type != GRPC_JSON_STRING) {
+ gpr_log(GPR_ERROR, "Invalid %s field [%s]", key, json->value);
+ return NULL;
+ }
return json->value;
}
-static gpr_timespec
-validate_time_field (const grpc_json * json, const char *key)
-{
- gpr_timespec result = gpr_time_0 (GPR_CLOCK_REALTIME);
- if (json->type != GRPC_JSON_NUMBER)
- {
- gpr_log (GPR_ERROR, "Invalid %s field [%s]", key, json->value);
- return result;
- }
- result.tv_sec = strtol (json->value, NULL, 10);
+static gpr_timespec validate_time_field(const grpc_json *json,
+ const char *key) {
+ gpr_timespec result = gpr_time_0(GPR_CLOCK_REALTIME);
+ if (json->type != GRPC_JSON_NUMBER) {
+ gpr_log(GPR_ERROR, "Invalid %s field [%s]", key, json->value);
+ return result;
+ }
+ result.tv_sec = strtol(json->value, NULL, 10);
return result;
}
/* --- JOSE header. see http://tools.ietf.org/html/rfc7515#section-4 --- */
-typedef struct
-{
+typedef struct {
const char *alg;
const char *kid;
const char *typ;
@@ -147,68 +129,54 @@ typedef struct
gpr_slice buffer;
} jose_header;
-static void
-jose_header_destroy (jose_header * h)
-{
- gpr_slice_unref (h->buffer);
- gpr_free (h);
+static void jose_header_destroy(jose_header *h) {
+ gpr_slice_unref(h->buffer);
+ gpr_free(h);
}
/* Takes ownership of json and buffer. */
-static jose_header *
-jose_header_from_json (grpc_json * json, gpr_slice buffer)
-{
+static jose_header *jose_header_from_json(grpc_json *json, gpr_slice buffer) {
grpc_json *cur;
- jose_header *h = gpr_malloc (sizeof (jose_header));
- memset (h, 0, sizeof (jose_header));
+ jose_header *h = gpr_malloc(sizeof(jose_header));
+ memset(h, 0, sizeof(jose_header));
h->buffer = buffer;
- for (cur = json->child; cur != NULL; cur = cur->next)
- {
- if (strcmp (cur->key, "alg") == 0)
- {
- /* We only support RSA-1.5 signatures for now.
- Beware of this if we add HMAC support:
- https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/
- */
- if (cur->type != GRPC_JSON_STRING || strncmp (cur->value, "RS", 2) || evp_md_from_alg (cur->value) == NULL)
- {
- gpr_log (GPR_ERROR, "Invalid alg field [%s]", cur->value);
- goto error;
- }
- h->alg = cur->value;
- }
- else if (strcmp (cur->key, "typ") == 0)
- {
- h->typ = validate_string_field (cur, "typ");
- if (h->typ == NULL)
- goto error;
- }
- else if (strcmp (cur->key, "kid") == 0)
- {
- h->kid = validate_string_field (cur, "kid");
- if (h->kid == NULL)
- goto error;
- }
- }
- if (h->alg == NULL)
- {
- gpr_log (GPR_ERROR, "Missing alg field.");
- goto error;
- }
- grpc_json_destroy (json);
+ for (cur = json->child; cur != NULL; cur = cur->next) {
+ if (strcmp(cur->key, "alg") == 0) {
+ /* We only support RSA-1.5 signatures for now.
+ Beware of this if we add HMAC support:
+ https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/
+ */
+ if (cur->type != GRPC_JSON_STRING || strncmp(cur->value, "RS", 2) ||
+ evp_md_from_alg(cur->value) == NULL) {
+ gpr_log(GPR_ERROR, "Invalid alg field [%s]", cur->value);
+ goto error;
+ }
+ h->alg = cur->value;
+ } else if (strcmp(cur->key, "typ") == 0) {
+ h->typ = validate_string_field(cur, "typ");
+ if (h->typ == NULL) goto error;
+ } else if (strcmp(cur->key, "kid") == 0) {
+ h->kid = validate_string_field(cur, "kid");
+ if (h->kid == NULL) goto error;
+ }
+ }
+ if (h->alg == NULL) {
+ gpr_log(GPR_ERROR, "Missing alg field.");
+ goto error;
+ }
+ grpc_json_destroy(json);
h->buffer = buffer;
return h;
error:
- grpc_json_destroy (json);
- jose_header_destroy (h);
+ grpc_json_destroy(json);
+ jose_header_destroy(h);
return NULL;
}
/* --- JWT claims. see http://tools.ietf.org/html/rfc7519#section-4.1 */
-struct grpc_jwt_claims
-{
+struct grpc_jwt_claims {
/* Well known properties already parsed. */
const char *sub;
const char *iss;
@@ -222,185 +190,135 @@ struct grpc_jwt_claims
gpr_slice buffer;
};
-void
-grpc_jwt_claims_destroy (grpc_jwt_claims * claims)
-{
- grpc_json_destroy (claims->json);
- gpr_slice_unref (claims->buffer);
- gpr_free (claims);
+void grpc_jwt_claims_destroy(grpc_jwt_claims *claims) {
+ grpc_json_destroy(claims->json);
+ gpr_slice_unref(claims->buffer);
+ gpr_free(claims);
}
-const grpc_json *
-grpc_jwt_claims_json (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return NULL;
+const grpc_json *grpc_jwt_claims_json(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return NULL;
return claims->json;
}
-const char *
-grpc_jwt_claims_subject (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return NULL;
+const char *grpc_jwt_claims_subject(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return NULL;
return claims->sub;
}
-const char *
-grpc_jwt_claims_issuer (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return NULL;
+const char *grpc_jwt_claims_issuer(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return NULL;
return claims->iss;
}
-const char *
-grpc_jwt_claims_id (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return NULL;
+const char *grpc_jwt_claims_id(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return NULL;
return claims->jti;
}
-const char *
-grpc_jwt_claims_audience (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return NULL;
+const char *grpc_jwt_claims_audience(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return NULL;
return claims->aud;
}
-gpr_timespec
-grpc_jwt_claims_issued_at (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return gpr_inf_past (GPR_CLOCK_REALTIME);
+gpr_timespec grpc_jwt_claims_issued_at(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return gpr_inf_past(GPR_CLOCK_REALTIME);
return claims->iat;
}
-gpr_timespec
-grpc_jwt_claims_expires_at (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return gpr_inf_future (GPR_CLOCK_REALTIME);
+gpr_timespec grpc_jwt_claims_expires_at(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return gpr_inf_future(GPR_CLOCK_REALTIME);
return claims->exp;
}
-gpr_timespec
-grpc_jwt_claims_not_before (const grpc_jwt_claims * claims)
-{
- if (claims == NULL)
- return gpr_inf_past (GPR_CLOCK_REALTIME);
+gpr_timespec grpc_jwt_claims_not_before(const grpc_jwt_claims *claims) {
+ if (claims == NULL) return gpr_inf_past(GPR_CLOCK_REALTIME);
return claims->nbf;
}
/* Takes ownership of json and buffer even in case of failure. */
-grpc_jwt_claims *
-grpc_jwt_claims_from_json (grpc_json * json, gpr_slice buffer)
-{
+grpc_jwt_claims *grpc_jwt_claims_from_json(grpc_json *json, gpr_slice buffer) {
grpc_json *cur;
- grpc_jwt_claims *claims = gpr_malloc (sizeof (grpc_jwt_claims));
- memset (claims, 0, sizeof (grpc_jwt_claims));
+ grpc_jwt_claims *claims = gpr_malloc(sizeof(grpc_jwt_claims));
+ memset(claims, 0, sizeof(grpc_jwt_claims));
claims->json = json;
claims->buffer = buffer;
- claims->iat = gpr_inf_past (GPR_CLOCK_REALTIME);
- claims->nbf = gpr_inf_past (GPR_CLOCK_REALTIME);
- claims->exp = gpr_inf_future (GPR_CLOCK_REALTIME);
+ claims->iat = gpr_inf_past(GPR_CLOCK_REALTIME);
+ claims->nbf = gpr_inf_past(GPR_CLOCK_REALTIME);
+ claims->exp = gpr_inf_future(GPR_CLOCK_REALTIME);
/* Per the spec, all fields are optional. */
- for (cur = json->child; cur != NULL; cur = cur->next)
- {
- if (strcmp (cur->key, "sub") == 0)
- {
- claims->sub = validate_string_field (cur, "sub");
- if (claims->sub == NULL)
- goto error;
- }
- else if (strcmp (cur->key, "iss") == 0)
- {
- claims->iss = validate_string_field (cur, "iss");
- if (claims->iss == NULL)
- goto error;
- }
- else if (strcmp (cur->key, "aud") == 0)
- {
- claims->aud = validate_string_field (cur, "aud");
- if (claims->aud == NULL)
- goto error;
- }
- else if (strcmp (cur->key, "jti") == 0)
- {
- claims->jti = validate_string_field (cur, "jti");
- if (claims->jti == NULL)
- goto error;
- }
- else if (strcmp (cur->key, "iat") == 0)
- {
- claims->iat = validate_time_field (cur, "iat");
- if (gpr_time_cmp (claims->iat, gpr_time_0 (GPR_CLOCK_REALTIME)) == 0)
- goto error;
- }
- else if (strcmp (cur->key, "exp") == 0)
- {
- claims->exp = validate_time_field (cur, "exp");
- if (gpr_time_cmp (claims->exp, gpr_time_0 (GPR_CLOCK_REALTIME)) == 0)
- goto error;
- }
- else if (strcmp (cur->key, "nbf") == 0)
- {
- claims->nbf = validate_time_field (cur, "nbf");
- if (gpr_time_cmp (claims->nbf, gpr_time_0 (GPR_CLOCK_REALTIME)) == 0)
- goto error;
- }
- }
+ for (cur = json->child; cur != NULL; cur = cur->next) {
+ if (strcmp(cur->key, "sub") == 0) {
+ claims->sub = validate_string_field(cur, "sub");
+ if (claims->sub == NULL) goto error;
+ } else if (strcmp(cur->key, "iss") == 0) {
+ claims->iss = validate_string_field(cur, "iss");
+ if (claims->iss == NULL) goto error;
+ } else if (strcmp(cur->key, "aud") == 0) {
+ claims->aud = validate_string_field(cur, "aud");
+ if (claims->aud == NULL) goto error;
+ } else if (strcmp(cur->key, "jti") == 0) {
+ claims->jti = validate_string_field(cur, "jti");
+ if (claims->jti == NULL) goto error;
+ } else if (strcmp(cur->key, "iat") == 0) {
+ claims->iat = validate_time_field(cur, "iat");
+ if (gpr_time_cmp(claims->iat, gpr_time_0(GPR_CLOCK_REALTIME)) == 0)
+ goto error;
+ } else if (strcmp(cur->key, "exp") == 0) {
+ claims->exp = validate_time_field(cur, "exp");
+ if (gpr_time_cmp(claims->exp, gpr_time_0(GPR_CLOCK_REALTIME)) == 0)
+ goto error;
+ } else if (strcmp(cur->key, "nbf") == 0) {
+ claims->nbf = validate_time_field(cur, "nbf");
+ if (gpr_time_cmp(claims->nbf, gpr_time_0(GPR_CLOCK_REALTIME)) == 0)
+ goto error;
+ }
+ }
return claims;
error:
- grpc_jwt_claims_destroy (claims);
+ grpc_jwt_claims_destroy(claims);
return NULL;
}
-grpc_jwt_verifier_status
-grpc_jwt_claims_check (const grpc_jwt_claims * claims, const char *audience)
-{
+grpc_jwt_verifier_status grpc_jwt_claims_check(const grpc_jwt_claims *claims,
+ const char *audience) {
gpr_timespec skewed_now;
int audience_ok;
- GPR_ASSERT (claims != NULL);
-
- skewed_now = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), grpc_jwt_verifier_clock_skew);
- if (gpr_time_cmp (skewed_now, claims->nbf) < 0)
- {
- gpr_log (GPR_ERROR, "JWT is not valid yet.");
- return GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE;
- }
- skewed_now = gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), grpc_jwt_verifier_clock_skew);
- if (gpr_time_cmp (skewed_now, claims->exp) > 0)
- {
- gpr_log (GPR_ERROR, "JWT is expired.");
- return GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE;
- }
-
- if (audience == NULL)
- {
- audience_ok = claims->aud == NULL;
- }
- else
- {
- audience_ok = claims->aud != NULL && strcmp (audience, claims->aud) == 0;
- }
- if (!audience_ok)
- {
- gpr_log (GPR_ERROR, "Audience mismatch: expected %s and found %s.", audience == NULL ? "NULL" : audience, claims->aud == NULL ? "NULL" : claims->aud);
- return GRPC_JWT_VERIFIER_BAD_AUDIENCE;
- }
+ GPR_ASSERT(claims != NULL);
+
+ skewed_now =
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), grpc_jwt_verifier_clock_skew);
+ if (gpr_time_cmp(skewed_now, claims->nbf) < 0) {
+ gpr_log(GPR_ERROR, "JWT is not valid yet.");
+ return GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE;
+ }
+ skewed_now =
+ gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), grpc_jwt_verifier_clock_skew);
+ if (gpr_time_cmp(skewed_now, claims->exp) > 0) {
+ gpr_log(GPR_ERROR, "JWT is expired.");
+ return GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE;
+ }
+
+ if (audience == NULL) {
+ audience_ok = claims->aud == NULL;
+ } else {
+ audience_ok = claims->aud != NULL && strcmp(audience, claims->aud) == 0;
+ }
+ if (!audience_ok) {
+ gpr_log(GPR_ERROR, "Audience mismatch: expected %s and found %s.",
+ audience == NULL ? "NULL" : audience,
+ claims->aud == NULL ? "NULL" : claims->aud);
+ return GRPC_JWT_VERIFIER_BAD_AUDIENCE;
+ }
return GRPC_JWT_VERIFIER_OK;
}
/* --- verifier_cb_ctx object. --- */
-typedef struct
-{
+typedef struct {
grpc_jwt_verifier *verifier;
grpc_pollset *pollset;
jose_header *header;
@@ -413,436 +331,377 @@ typedef struct
} verifier_cb_ctx;
/* Takes ownership of the header, claims and signature. */
-static verifier_cb_ctx *
-verifier_cb_ctx_create (grpc_jwt_verifier * verifier, grpc_pollset * pollset, jose_header * header, grpc_jwt_claims * claims, const char *audience, gpr_slice signature, const char *signed_jwt, size_t signed_jwt_len, void *user_data, grpc_jwt_verification_done_cb cb)
-{
- verifier_cb_ctx *ctx = gpr_malloc (sizeof (verifier_cb_ctx));
- memset (ctx, 0, sizeof (verifier_cb_ctx));
+static verifier_cb_ctx *verifier_cb_ctx_create(
+ grpc_jwt_verifier *verifier, grpc_pollset *pollset, jose_header *header,
+ grpc_jwt_claims *claims, const char *audience, gpr_slice signature,
+ const char *signed_jwt, size_t signed_jwt_len, void *user_data,
+ grpc_jwt_verification_done_cb cb) {
+ verifier_cb_ctx *ctx = gpr_malloc(sizeof(verifier_cb_ctx));
+ memset(ctx, 0, sizeof(verifier_cb_ctx));
ctx->verifier = verifier;
ctx->pollset = pollset;
ctx->header = header;
- ctx->audience = gpr_strdup (audience);
+ ctx->audience = gpr_strdup(audience);
ctx->claims = claims;
ctx->signature = signature;
- ctx->signed_data = gpr_slice_from_copied_buffer (signed_jwt, signed_jwt_len);
+ ctx->signed_data = gpr_slice_from_copied_buffer(signed_jwt, signed_jwt_len);
ctx->user_data = user_data;
ctx->user_cb = cb;
return ctx;
}
-void
-verifier_cb_ctx_destroy (verifier_cb_ctx * ctx)
-{
- if (ctx->audience != NULL)
- gpr_free (ctx->audience);
- if (ctx->claims != NULL)
- grpc_jwt_claims_destroy (ctx->claims);
- gpr_slice_unref (ctx->signature);
- gpr_slice_unref (ctx->signed_data);
- jose_header_destroy (ctx->header);
+void verifier_cb_ctx_destroy(verifier_cb_ctx *ctx) {
+ if (ctx->audience != NULL) gpr_free(ctx->audience);
+ if (ctx->claims != NULL) grpc_jwt_claims_destroy(ctx->claims);
+ gpr_slice_unref(ctx->signature);
+ gpr_slice_unref(ctx->signed_data);
+ jose_header_destroy(ctx->header);
/* TODO: see what to do with claims... */
- gpr_free (ctx);
+ gpr_free(ctx);
}
/* --- grpc_jwt_verifier object. --- */
/* Clock skew defaults to one minute. */
-gpr_timespec grpc_jwt_verifier_clock_skew = { 60, 0, GPR_TIMESPAN };
+gpr_timespec grpc_jwt_verifier_clock_skew = {60, 0, GPR_TIMESPAN};
/* Max delay defaults to one minute. */
-gpr_timespec grpc_jwt_verifier_max_delay = { 60, 0, GPR_TIMESPAN };
+gpr_timespec grpc_jwt_verifier_max_delay = {60, 0, GPR_TIMESPAN};
-typedef struct
-{
+typedef struct {
char *email_domain;
char *key_url_prefix;
} email_key_mapping;
-struct grpc_jwt_verifier
-{
+struct grpc_jwt_verifier {
email_key_mapping *mappings;
- size_t num_mappings; /* Should be very few, linear search ok. */
+ size_t num_mappings; /* Should be very few, linear search ok. */
size_t allocated_mappings;
grpc_httpcli_context http_ctx;
};
-static grpc_json *
-json_from_http (const grpc_httpcli_response * response)
-{
+static grpc_json *json_from_http(const grpc_httpcli_response *response) {
grpc_json *json = NULL;
- if (response == NULL)
- {
- gpr_log (GPR_ERROR, "HTTP response is NULL.");
- return NULL;
- }
- if (response->status != 200)
- {
- gpr_log (GPR_ERROR, "Call to http server failed with error %d.", response->status);
- return NULL;
- }
+ if (response == NULL) {
+ gpr_log(GPR_ERROR, "HTTP response is NULL.");
+ return NULL;
+ }
+ if (response->status != 200) {
+ gpr_log(GPR_ERROR, "Call to http server failed with error %d.",
+ response->status);
+ return NULL;
+ }
- json = grpc_json_parse_string_with_len (response->body, response->body_length);
- if (json == NULL)
- {
- gpr_log (GPR_ERROR, "Invalid JSON found in response.");
- }
+ json = grpc_json_parse_string_with_len(response->body, response->body_length);
+ if (json == NULL) {
+ gpr_log(GPR_ERROR, "Invalid JSON found in response.");
+ }
return json;
}
-static const grpc_json *
-find_property_by_name (const grpc_json * json, const char *name)
-{
+static const grpc_json *find_property_by_name(const grpc_json *json,
+ const char *name) {
const grpc_json *cur;
- for (cur = json->child; cur != NULL; cur = cur->next)
- {
- if (strcmp (cur->key, name) == 0)
- return cur;
- }
+ for (cur = json->child; cur != NULL; cur = cur->next) {
+ if (strcmp(cur->key, name) == 0) return cur;
+ }
return NULL;
}
-static EVP_PKEY *
-extract_pkey_from_x509 (const char *x509_str)
-{
+static EVP_PKEY *extract_pkey_from_x509(const char *x509_str) {
X509 *x509 = NULL;
EVP_PKEY *result = NULL;
- BIO *bio = BIO_new (BIO_s_mem ());
- size_t len = strlen (x509_str);
- GPR_ASSERT (len < INT_MAX);
- BIO_write (bio, x509_str, (int) len);
- x509 = PEM_read_bio_X509 (bio, NULL, NULL, NULL);
- if (x509 == NULL)
- {
- gpr_log (GPR_ERROR, "Unable to parse x509 cert.");
- goto end;
- }
- result = X509_get_pubkey (x509);
- if (result == NULL)
- {
- gpr_log (GPR_ERROR, "Cannot find public key in X509 cert.");
- }
+ BIO *bio = BIO_new(BIO_s_mem());
+ size_t len = strlen(x509_str);
+ GPR_ASSERT(len < INT_MAX);
+ BIO_write(bio, x509_str, (int)len);
+ x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
+ if (x509 == NULL) {
+ gpr_log(GPR_ERROR, "Unable to parse x509 cert.");
+ goto end;
+ }
+ result = X509_get_pubkey(x509);
+ if (result == NULL) {
+ gpr_log(GPR_ERROR, "Cannot find public key in X509 cert.");
+ }
end:
- BIO_free (bio);
- if (x509 != NULL)
- X509_free (x509);
+ BIO_free(bio);
+ if (x509 != NULL) X509_free(x509);
return result;
}
-static BIGNUM *
-bignum_from_base64 (const char *b64)
-{
+static BIGNUM *bignum_from_base64(const char *b64) {
BIGNUM *result = NULL;
gpr_slice bin;
- if (b64 == NULL)
+ if (b64 == NULL) return NULL;
+ bin = grpc_base64_decode(b64, 1);
+ if (GPR_SLICE_IS_EMPTY(bin)) {
+ gpr_log(GPR_ERROR, "Invalid base64 for big num.");
return NULL;
- bin = grpc_base64_decode (b64, 1);
- if (GPR_SLICE_IS_EMPTY (bin))
- {
- gpr_log (GPR_ERROR, "Invalid base64 for big num.");
- return NULL;
- }
- result = BN_bin2bn (GPR_SLICE_START_PTR (bin), (int) GPR_SLICE_LENGTH (bin), NULL);
- gpr_slice_unref (bin);
+ }
+ result =
+ BN_bin2bn(GPR_SLICE_START_PTR(bin), (int)GPR_SLICE_LENGTH(bin), NULL);
+ gpr_slice_unref(bin);
return result;
}
-static EVP_PKEY *
-pkey_from_jwk (const grpc_json * json, const char *kty)
-{
+static EVP_PKEY *pkey_from_jwk(const grpc_json *json, const char *kty) {
const grpc_json *key_prop;
RSA *rsa = NULL;
EVP_PKEY *result = NULL;
- GPR_ASSERT (kty != NULL && json != NULL);
- if (strcmp (kty, "RSA") != 0)
- {
- gpr_log (GPR_ERROR, "Unsupported key type %s.", kty);
- goto end;
- }
- rsa = RSA_new ();
- if (rsa == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create rsa key.");
- goto end;
- }
- for (key_prop = json->child; key_prop != NULL; key_prop = key_prop->next)
- {
- if (strcmp (key_prop->key, "n") == 0)
- {
- rsa->n = bignum_from_base64 (validate_string_field (key_prop, "n"));
- if (rsa->n == NULL)
- goto end;
- }
- else if (strcmp (key_prop->key, "e") == 0)
- {
- rsa->e = bignum_from_base64 (validate_string_field (key_prop, "e"));
- if (rsa->e == NULL)
- goto end;
- }
- }
- if (rsa->e == NULL || rsa->n == NULL)
- {
- gpr_log (GPR_ERROR, "Missing RSA public key field.");
- goto end;
- }
- result = EVP_PKEY_new ();
- EVP_PKEY_set1_RSA (result, rsa); /* uprefs rsa. */
+ GPR_ASSERT(kty != NULL && json != NULL);
+ if (strcmp(kty, "RSA") != 0) {
+ gpr_log(GPR_ERROR, "Unsupported key type %s.", kty);
+ goto end;
+ }
+ rsa = RSA_new();
+ if (rsa == NULL) {
+ gpr_log(GPR_ERROR, "Could not create rsa key.");
+ goto end;
+ }
+ for (key_prop = json->child; key_prop != NULL; key_prop = key_prop->next) {
+ if (strcmp(key_prop->key, "n") == 0) {
+ rsa->n = bignum_from_base64(validate_string_field(key_prop, "n"));
+ if (rsa->n == NULL) goto end;
+ } else if (strcmp(key_prop->key, "e") == 0) {
+ rsa->e = bignum_from_base64(validate_string_field(key_prop, "e"));
+ if (rsa->e == NULL) goto end;
+ }
+ }
+ if (rsa->e == NULL || rsa->n == NULL) {
+ gpr_log(GPR_ERROR, "Missing RSA public key field.");
+ goto end;
+ }
+ result = EVP_PKEY_new();
+ EVP_PKEY_set1_RSA(result, rsa); /* uprefs rsa. */
end:
- if (rsa != NULL)
- RSA_free (rsa);
+ if (rsa != NULL) RSA_free(rsa);
return result;
}
-static EVP_PKEY *
-find_verification_key (const grpc_json * json, const char *header_alg, const char *header_kid)
-{
+static EVP_PKEY *find_verification_key(const grpc_json *json,
+ const char *header_alg,
+ const char *header_kid) {
const grpc_json *jkey;
const grpc_json *jwk_keys;
/* Try to parse the json as a JWK set:
https://tools.ietf.org/html/rfc7517#section-5. */
- jwk_keys = find_property_by_name (json, "keys");
- if (jwk_keys == NULL)
- {
- /* Use the google proprietary format which is:
- { <kid1>: <x5091>, <kid2>: <x5092>, ... } */
- const grpc_json *cur = find_property_by_name (json, header_kid);
- if (cur == NULL)
- return NULL;
- return extract_pkey_from_x509 (cur->value);
- }
-
- if (jwk_keys->type != GRPC_JSON_ARRAY)
- {
- gpr_log (GPR_ERROR, "Unexpected value type of keys property in jwks key set.");
- return NULL;
- }
+ jwk_keys = find_property_by_name(json, "keys");
+ if (jwk_keys == NULL) {
+ /* Use the google proprietary format which is:
+ { <kid1>: <x5091>, <kid2>: <x5092>, ... } */
+ const grpc_json *cur = find_property_by_name(json, header_kid);
+ if (cur == NULL) return NULL;
+ return extract_pkey_from_x509(cur->value);
+ }
+
+ if (jwk_keys->type != GRPC_JSON_ARRAY) {
+ gpr_log(GPR_ERROR,
+ "Unexpected value type of keys property in jwks key set.");
+ return NULL;
+ }
/* Key format is specified in:
https://tools.ietf.org/html/rfc7518#section-6. */
- for (jkey = jwk_keys->child; jkey != NULL; jkey = jkey->next)
- {
- grpc_json *key_prop;
- const char *alg = NULL;
- const char *kid = NULL;
- const char *kty = NULL;
-
- if (jkey->type != GRPC_JSON_OBJECT)
- continue;
- for (key_prop = jkey->child; key_prop != NULL; key_prop = key_prop->next)
- {
- if (strcmp (key_prop->key, "alg") == 0 && key_prop->type == GRPC_JSON_STRING)
- {
- alg = key_prop->value;
- }
- else if (strcmp (key_prop->key, "kid") == 0 && key_prop->type == GRPC_JSON_STRING)
- {
- kid = key_prop->value;
- }
- else if (strcmp (key_prop->key, "kty") == 0 && key_prop->type == GRPC_JSON_STRING)
- {
- kty = key_prop->value;
- }
- }
- if (alg != NULL && kid != NULL && kty != NULL && strcmp (kid, header_kid) == 0 && strcmp (alg, header_alg) == 0)
- {
- return pkey_from_jwk (jkey, kty);
- }
- }
- gpr_log (GPR_ERROR, "Could not find matching key in key set for kid=%s and alg=%s", header_kid, header_alg);
+ for (jkey = jwk_keys->child; jkey != NULL; jkey = jkey->next) {
+ grpc_json *key_prop;
+ const char *alg = NULL;
+ const char *kid = NULL;
+ const char *kty = NULL;
+
+ if (jkey->type != GRPC_JSON_OBJECT) continue;
+ for (key_prop = jkey->child; key_prop != NULL; key_prop = key_prop->next) {
+ if (strcmp(key_prop->key, "alg") == 0 &&
+ key_prop->type == GRPC_JSON_STRING) {
+ alg = key_prop->value;
+ } else if (strcmp(key_prop->key, "kid") == 0 &&
+ key_prop->type == GRPC_JSON_STRING) {
+ kid = key_prop->value;
+ } else if (strcmp(key_prop->key, "kty") == 0 &&
+ key_prop->type == GRPC_JSON_STRING) {
+ kty = key_prop->value;
+ }
+ }
+ if (alg != NULL && kid != NULL && kty != NULL &&
+ strcmp(kid, header_kid) == 0 && strcmp(alg, header_alg) == 0) {
+ return pkey_from_jwk(jkey, kty);
+ }
+ }
+ gpr_log(GPR_ERROR,
+ "Could not find matching key in key set for kid=%s and alg=%s",
+ header_kid, header_alg);
return NULL;
}
-static int
-verify_jwt_signature (EVP_PKEY * key, const char *alg, gpr_slice signature, gpr_slice signed_data)
-{
- EVP_MD_CTX *md_ctx = EVP_MD_CTX_create ();
- const EVP_MD *md = evp_md_from_alg (alg);
+static int verify_jwt_signature(EVP_PKEY *key, const char *alg,
+ gpr_slice signature, gpr_slice signed_data) {
+ EVP_MD_CTX *md_ctx = EVP_MD_CTX_create();
+ const EVP_MD *md = evp_md_from_alg(alg);
int result = 0;
- GPR_ASSERT (md != NULL); /* Checked before. */
- if (md_ctx == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create EVP_MD_CTX.");
- goto end;
- }
- if (EVP_DigestVerifyInit (md_ctx, NULL, md, NULL, key) != 1)
- {
- gpr_log (GPR_ERROR, "EVP_DigestVerifyInit failed.");
- goto end;
- }
- if (EVP_DigestVerifyUpdate (md_ctx, GPR_SLICE_START_PTR (signed_data), GPR_SLICE_LENGTH (signed_data)) != 1)
- {
- gpr_log (GPR_ERROR, "EVP_DigestVerifyUpdate failed.");
- goto end;
- }
- if (EVP_DigestVerifyFinal (md_ctx, GPR_SLICE_START_PTR (signature), GPR_SLICE_LENGTH (signature)) != 1)
- {
- gpr_log (GPR_ERROR, "JWT signature verification failed.");
- goto end;
- }
+ GPR_ASSERT(md != NULL); /* Checked before. */
+ if (md_ctx == NULL) {
+ gpr_log(GPR_ERROR, "Could not create EVP_MD_CTX.");
+ goto end;
+ }
+ if (EVP_DigestVerifyInit(md_ctx, NULL, md, NULL, key) != 1) {
+ gpr_log(GPR_ERROR, "EVP_DigestVerifyInit failed.");
+ goto end;
+ }
+ if (EVP_DigestVerifyUpdate(md_ctx, GPR_SLICE_START_PTR(signed_data),
+ GPR_SLICE_LENGTH(signed_data)) != 1) {
+ gpr_log(GPR_ERROR, "EVP_DigestVerifyUpdate failed.");
+ goto end;
+ }
+ if (EVP_DigestVerifyFinal(md_ctx, GPR_SLICE_START_PTR(signature),
+ GPR_SLICE_LENGTH(signature)) != 1) {
+ gpr_log(GPR_ERROR, "JWT signature verification failed.");
+ goto end;
+ }
result = 1;
end:
- if (md_ctx != NULL)
- EVP_MD_CTX_destroy (md_ctx);
+ if (md_ctx != NULL) EVP_MD_CTX_destroy(md_ctx);
return result;
}
-static void
-on_keys_retrieved (grpc_exec_ctx * exec_ctx, void *user_data, const grpc_httpcli_response * response)
-{
- grpc_json *json = json_from_http (response);
- verifier_cb_ctx *ctx = (verifier_cb_ctx *) user_data;
+static void on_keys_retrieved(grpc_exec_ctx *exec_ctx, void *user_data,
+ const grpc_httpcli_response *response) {
+ grpc_json *json = json_from_http(response);
+ verifier_cb_ctx *ctx = (verifier_cb_ctx *)user_data;
EVP_PKEY *verification_key = NULL;
grpc_jwt_verifier_status status = GRPC_JWT_VERIFIER_GENERIC_ERROR;
grpc_jwt_claims *claims = NULL;
- if (json == NULL)
- {
- status = GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR;
- goto end;
- }
- verification_key = find_verification_key (json, ctx->header->alg, ctx->header->kid);
- if (verification_key == NULL)
- {
- gpr_log (GPR_ERROR, "Could not find verification key with kid %s.", ctx->header->kid);
- status = GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR;
- goto end;
- }
-
- if (!verify_jwt_signature (verification_key, ctx->header->alg, ctx->signature, ctx->signed_data))
- {
- status = GRPC_JWT_VERIFIER_BAD_SIGNATURE;
- goto end;
- }
-
- status = grpc_jwt_claims_check (ctx->claims, ctx->audience);
- if (status == GRPC_JWT_VERIFIER_OK)
- {
- /* Pass ownership. */
- claims = ctx->claims;
- ctx->claims = NULL;
- }
+ if (json == NULL) {
+ status = GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR;
+ goto end;
+ }
+ verification_key =
+ find_verification_key(json, ctx->header->alg, ctx->header->kid);
+ if (verification_key == NULL) {
+ gpr_log(GPR_ERROR, "Could not find verification key with kid %s.",
+ ctx->header->kid);
+ status = GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR;
+ goto end;
+ }
+
+ if (!verify_jwt_signature(verification_key, ctx->header->alg, ctx->signature,
+ ctx->signed_data)) {
+ status = GRPC_JWT_VERIFIER_BAD_SIGNATURE;
+ goto end;
+ }
+
+ status = grpc_jwt_claims_check(ctx->claims, ctx->audience);
+ if (status == GRPC_JWT_VERIFIER_OK) {
+ /* Pass ownership. */
+ claims = ctx->claims;
+ ctx->claims = NULL;
+ }
end:
- if (json != NULL)
- grpc_json_destroy (json);
- if (verification_key != NULL)
- EVP_PKEY_free (verification_key);
- ctx->user_cb (ctx->user_data, status, claims);
- verifier_cb_ctx_destroy (ctx);
+ if (json != NULL) grpc_json_destroy(json);
+ if (verification_key != NULL) EVP_PKEY_free(verification_key);
+ ctx->user_cb(ctx->user_data, status, claims);
+ verifier_cb_ctx_destroy(ctx);
}
-static void
-on_openid_config_retrieved (grpc_exec_ctx * exec_ctx, void *user_data, const grpc_httpcli_response * response)
-{
+static void on_openid_config_retrieved(grpc_exec_ctx *exec_ctx, void *user_data,
+ const grpc_httpcli_response *response) {
const grpc_json *cur;
- grpc_json *json = json_from_http (response);
- verifier_cb_ctx *ctx = (verifier_cb_ctx *) user_data;
+ grpc_json *json = json_from_http(response);
+ verifier_cb_ctx *ctx = (verifier_cb_ctx *)user_data;
grpc_httpcli_request req;
const char *jwks_uri;
/* TODO(jboeuf): Cache the jwks_uri in order to avoid this hop next time. */
- if (json == NULL)
+ if (json == NULL) goto error;
+ cur = find_property_by_name(json, "jwks_uri");
+ if (cur == NULL) {
+ gpr_log(GPR_ERROR, "Could not find jwks_uri in openid config.");
goto error;
- cur = find_property_by_name (json, "jwks_uri");
- if (cur == NULL)
- {
- gpr_log (GPR_ERROR, "Could not find jwks_uri in openid config.");
- goto error;
- }
- jwks_uri = validate_string_field (cur, "jwks_uri");
- if (jwks_uri == NULL)
+ }
+ jwks_uri = validate_string_field(cur, "jwks_uri");
+ if (jwks_uri == NULL) goto error;
+ if (strstr(jwks_uri, "https://") != jwks_uri) {
+ gpr_log(GPR_ERROR, "Invalid non https jwks_uri: %s.", jwks_uri);
goto error;
- if (strstr (jwks_uri, "https://") != jwks_uri)
- {
- gpr_log (GPR_ERROR, "Invalid non https jwks_uri: %s.", jwks_uri);
- goto error;
- }
+ }
jwks_uri += 8;
req.handshaker = &grpc_httpcli_ssl;
- req.host = gpr_strdup (jwks_uri);
- req.path = strchr (jwks_uri, '/');
- if (req.path == NULL)
- {
- req.path = "";
- }
- else
- {
- *(req.host + (req.path - jwks_uri)) = '\0';
- }
- grpc_httpcli_get (exec_ctx, &ctx->verifier->http_ctx, ctx->pollset, &req, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), grpc_jwt_verifier_max_delay), on_keys_retrieved, ctx);
- grpc_json_destroy (json);
- gpr_free (req.host);
+ req.host = gpr_strdup(jwks_uri);
+ req.path = strchr(jwks_uri, '/');
+ if (req.path == NULL) {
+ req.path = "";
+ } else {
+ *(req.host + (req.path - jwks_uri)) = '\0';
+ }
+ grpc_httpcli_get(
+ exec_ctx, &ctx->verifier->http_ctx, ctx->pollset, &req,
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), grpc_jwt_verifier_max_delay),
+ on_keys_retrieved, ctx);
+ grpc_json_destroy(json);
+ gpr_free(req.host);
return;
error:
- if (json != NULL)
- grpc_json_destroy (json);
- ctx->user_cb (ctx->user_data, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR, NULL);
- verifier_cb_ctx_destroy (ctx);
+ if (json != NULL) grpc_json_destroy(json);
+ ctx->user_cb(ctx->user_data, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR, NULL);
+ verifier_cb_ctx_destroy(ctx);
}
-static email_key_mapping *
-verifier_get_mapping (grpc_jwt_verifier * v, const char *email_domain)
-{
+static email_key_mapping *verifier_get_mapping(grpc_jwt_verifier *v,
+ const char *email_domain) {
size_t i;
- if (v->mappings == NULL)
- return NULL;
- for (i = 0; i < v->num_mappings; i++)
- {
- if (strcmp (email_domain, v->mappings[i].email_domain) == 0)
- {
- return &v->mappings[i];
- }
+ if (v->mappings == NULL) return NULL;
+ for (i = 0; i < v->num_mappings; i++) {
+ if (strcmp(email_domain, v->mappings[i].email_domain) == 0) {
+ return &v->mappings[i];
}
+ }
return NULL;
}
-static void
-verifier_put_mapping (grpc_jwt_verifier * v, const char *email_domain, const char *key_url_prefix)
-{
- email_key_mapping *mapping = verifier_get_mapping (v, email_domain);
- GPR_ASSERT (v->num_mappings < v->allocated_mappings);
- if (mapping != NULL)
- {
- gpr_free (mapping->key_url_prefix);
- mapping->key_url_prefix = gpr_strdup (key_url_prefix);
- return;
- }
- v->mappings[v->num_mappings].email_domain = gpr_strdup (email_domain);
- v->mappings[v->num_mappings].key_url_prefix = gpr_strdup (key_url_prefix);
+static void verifier_put_mapping(grpc_jwt_verifier *v, const char *email_domain,
+ const char *key_url_prefix) {
+ email_key_mapping *mapping = verifier_get_mapping(v, email_domain);
+ GPR_ASSERT(v->num_mappings < v->allocated_mappings);
+ if (mapping != NULL) {
+ gpr_free(mapping->key_url_prefix);
+ mapping->key_url_prefix = gpr_strdup(key_url_prefix);
+ return;
+ }
+ v->mappings[v->num_mappings].email_domain = gpr_strdup(email_domain);
+ v->mappings[v->num_mappings].key_url_prefix = gpr_strdup(key_url_prefix);
v->num_mappings++;
- GPR_ASSERT (v->num_mappings <= v->allocated_mappings);
+ GPR_ASSERT(v->num_mappings <= v->allocated_mappings);
}
/* Takes ownership of ctx. */
-static void
-retrieve_key_and_verify (grpc_exec_ctx * exec_ctx, verifier_cb_ctx * ctx)
-{
+static void retrieve_key_and_verify(grpc_exec_ctx *exec_ctx,
+ verifier_cb_ctx *ctx) {
const char *at_sign;
grpc_httpcli_response_cb http_cb;
char *path_prefix = NULL;
const char *iss;
grpc_httpcli_request req;
- memset (&req, 0, sizeof (grpc_httpcli_request));
+ memset(&req, 0, sizeof(grpc_httpcli_request));
req.handshaker = &grpc_httpcli_ssl;
- GPR_ASSERT (ctx != NULL && ctx->header != NULL && ctx->claims != NULL);
+ GPR_ASSERT(ctx != NULL && ctx->header != NULL && ctx->claims != NULL);
iss = ctx->claims->iss;
- if (ctx->header->kid == NULL)
- {
- gpr_log (GPR_ERROR, "Missing kid in jose header.");
- goto error;
- }
- if (iss == NULL)
- {
- gpr_log (GPR_ERROR, "Missing iss in claims.");
- goto error;
- }
+ if (ctx->header->kid == NULL) {
+ gpr_log(GPR_ERROR, "Missing kid in jose header.");
+ goto error;
+ }
+ if (iss == NULL) {
+ gpr_log(GPR_ERROR, "Missing iss in claims.");
+ goto error;
+ }
/* This code relies on:
https://openid.net/specs/openid-connect-discovery-1_0.html
@@ -852,60 +711,57 @@ retrieve_key_and_verify (grpc_exec_ctx * exec_ctx, verifier_cb_ctx * ctx)
/* Very non-sophisticated way to detect an email address. Should be good
enough for now... */
- at_sign = strchr (iss, '@');
- if (at_sign != NULL)
- {
- email_key_mapping *mapping;
- const char *email_domain = at_sign + 1;
- GPR_ASSERT (ctx->verifier != NULL);
- mapping = verifier_get_mapping (ctx->verifier, email_domain);
- if (mapping == NULL)
- {
- gpr_log (GPR_ERROR, "Missing mapping for issuer email.");
- goto error;
- }
- req.host = gpr_strdup (mapping->key_url_prefix);
- path_prefix = strchr (req.host, '/');
- if (path_prefix == NULL)
- {
- gpr_asprintf (&req.path, "/%s", iss);
- }
- else
- {
- *(path_prefix++) = '\0';
- gpr_asprintf (&req.path, "/%s/%s", path_prefix, iss);
- }
- http_cb = on_keys_retrieved;
- }
- else
- {
- req.host = gpr_strdup (strstr (iss, "https://") == iss ? iss + 8 : iss);
- path_prefix = strchr (req.host, '/');
- if (path_prefix == NULL)
- {
- req.path = gpr_strdup (GRPC_OPENID_CONFIG_URL_SUFFIX);
- }
- else
- {
- *(path_prefix++) = 0;
- gpr_asprintf (&req.path, "/%s%s", path_prefix, GRPC_OPENID_CONFIG_URL_SUFFIX);
- }
- http_cb = on_openid_config_retrieved;
+ at_sign = strchr(iss, '@');
+ if (at_sign != NULL) {
+ email_key_mapping *mapping;
+ const char *email_domain = at_sign + 1;
+ GPR_ASSERT(ctx->verifier != NULL);
+ mapping = verifier_get_mapping(ctx->verifier, email_domain);
+ if (mapping == NULL) {
+ gpr_log(GPR_ERROR, "Missing mapping for issuer email.");
+ goto error;
}
-
- grpc_httpcli_get (exec_ctx, &ctx->verifier->http_ctx, ctx->pollset, &req, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), grpc_jwt_verifier_max_delay), http_cb, ctx);
- gpr_free (req.host);
- gpr_free (req.path);
+ req.host = gpr_strdup(mapping->key_url_prefix);
+ path_prefix = strchr(req.host, '/');
+ if (path_prefix == NULL) {
+ gpr_asprintf(&req.path, "/%s", iss);
+ } else {
+ *(path_prefix++) = '\0';
+ gpr_asprintf(&req.path, "/%s/%s", path_prefix, iss);
+ }
+ http_cb = on_keys_retrieved;
+ } else {
+ req.host = gpr_strdup(strstr(iss, "https://") == iss ? iss + 8 : iss);
+ path_prefix = strchr(req.host, '/');
+ if (path_prefix == NULL) {
+ req.path = gpr_strdup(GRPC_OPENID_CONFIG_URL_SUFFIX);
+ } else {
+ *(path_prefix++) = 0;
+ gpr_asprintf(&req.path, "/%s%s", path_prefix,
+ GRPC_OPENID_CONFIG_URL_SUFFIX);
+ }
+ http_cb = on_openid_config_retrieved;
+ }
+
+ grpc_httpcli_get(
+ exec_ctx, &ctx->verifier->http_ctx, ctx->pollset, &req,
+ gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), grpc_jwt_verifier_max_delay),
+ http_cb, ctx);
+ gpr_free(req.host);
+ gpr_free(req.path);
return;
error:
- ctx->user_cb (ctx->user_data, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR, NULL);
- verifier_cb_ctx_destroy (ctx);
+ ctx->user_cb(ctx->user_data, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR, NULL);
+ verifier_cb_ctx_destroy(ctx);
}
-void
-grpc_jwt_verifier_verify (grpc_exec_ctx * exec_ctx, grpc_jwt_verifier * verifier, grpc_pollset * pollset, const char *jwt, const char *audience, grpc_jwt_verification_done_cb cb, void *user_data)
-{
+void grpc_jwt_verifier_verify(grpc_exec_ctx *exec_ctx,
+ grpc_jwt_verifier *verifier,
+ grpc_pollset *pollset, const char *jwt,
+ const char *audience,
+ grpc_jwt_verification_done_cb cb,
+ void *user_data) {
const char *dot = NULL;
grpc_json *json;
jose_header *header = NULL;
@@ -916,82 +772,71 @@ grpc_jwt_verifier_verify (grpc_exec_ctx * exec_ctx, grpc_jwt_verifier * verifier
size_t signed_jwt_len;
const char *cur = jwt;
- GPR_ASSERT (verifier != NULL && jwt != NULL && audience != NULL && cb != NULL);
- dot = strchr (cur, '.');
- if (dot == NULL)
- goto error;
- json = parse_json_part_from_jwt (cur, (size_t) (dot - cur), &header_buffer);
- if (json == NULL)
- goto error;
- header = jose_header_from_json (json, header_buffer);
- if (header == NULL)
- goto error;
+ GPR_ASSERT(verifier != NULL && jwt != NULL && audience != NULL && cb != NULL);
+ dot = strchr(cur, '.');
+ if (dot == NULL) goto error;
+ json = parse_json_part_from_jwt(cur, (size_t)(dot - cur), &header_buffer);
+ if (json == NULL) goto error;
+ header = jose_header_from_json(json, header_buffer);
+ if (header == NULL) goto error;
cur = dot + 1;
- dot = strchr (cur, '.');
- if (dot == NULL)
- goto error;
- json = parse_json_part_from_jwt (cur, (size_t) (dot - cur), &claims_buffer);
- if (json == NULL)
- goto error;
- claims = grpc_jwt_claims_from_json (json, claims_buffer);
- if (claims == NULL)
- goto error;
-
- signed_jwt_len = (size_t) (dot - jwt);
+ dot = strchr(cur, '.');
+ if (dot == NULL) goto error;
+ json = parse_json_part_from_jwt(cur, (size_t)(dot - cur), &claims_buffer);
+ if (json == NULL) goto error;
+ claims = grpc_jwt_claims_from_json(json, claims_buffer);
+ if (claims == NULL) goto error;
+
+ signed_jwt_len = (size_t)(dot - jwt);
cur = dot + 1;
- signature = grpc_base64_decode (cur, 1);
- if (GPR_SLICE_IS_EMPTY (signature))
- goto error;
- retrieve_key_and_verify (exec_ctx, verifier_cb_ctx_create (verifier, pollset, header, claims, audience, signature, jwt, signed_jwt_len, user_data, cb));
+ signature = grpc_base64_decode(cur, 1);
+ if (GPR_SLICE_IS_EMPTY(signature)) goto error;
+ retrieve_key_and_verify(
+ exec_ctx,
+ verifier_cb_ctx_create(verifier, pollset, header, claims, audience,
+ signature, jwt, signed_jwt_len, user_data, cb));
return;
error:
- if (header != NULL)
- jose_header_destroy (header);
- if (claims != NULL)
- grpc_jwt_claims_destroy (claims);
- cb (user_data, GRPC_JWT_VERIFIER_BAD_FORMAT, NULL);
+ if (header != NULL) jose_header_destroy(header);
+ if (claims != NULL) grpc_jwt_claims_destroy(claims);
+ cb(user_data, GRPC_JWT_VERIFIER_BAD_FORMAT, NULL);
}
-grpc_jwt_verifier *
-grpc_jwt_verifier_create (const grpc_jwt_verifier_email_domain_key_url_mapping * mappings, size_t num_mappings)
-{
- grpc_jwt_verifier *v = gpr_malloc (sizeof (grpc_jwt_verifier));
- memset (v, 0, sizeof (grpc_jwt_verifier));
- grpc_httpcli_context_init (&v->http_ctx);
+grpc_jwt_verifier *grpc_jwt_verifier_create(
+ const grpc_jwt_verifier_email_domain_key_url_mapping *mappings,
+ size_t num_mappings) {
+ grpc_jwt_verifier *v = gpr_malloc(sizeof(grpc_jwt_verifier));
+ memset(v, 0, sizeof(grpc_jwt_verifier));
+ grpc_httpcli_context_init(&v->http_ctx);
/* We know at least of one mapping. */
v->allocated_mappings = 1 + num_mappings;
- v->mappings = gpr_malloc (v->allocated_mappings * sizeof (email_key_mapping));
- verifier_put_mapping (v, GRPC_GOOGLE_SERVICE_ACCOUNTS_EMAIL_DOMAIN, GRPC_GOOGLE_SERVICE_ACCOUNTS_KEY_URL_PREFIX);
+ v->mappings = gpr_malloc(v->allocated_mappings * sizeof(email_key_mapping));
+ verifier_put_mapping(v, GRPC_GOOGLE_SERVICE_ACCOUNTS_EMAIL_DOMAIN,
+ GRPC_GOOGLE_SERVICE_ACCOUNTS_KEY_URL_PREFIX);
/* User-Provided mappings. */
- if (mappings != NULL)
- {
- size_t i;
- for (i = 0; i < num_mappings; i++)
- {
- verifier_put_mapping (v, mappings[i].email_domain, mappings[i].key_url_prefix);
- }
+ if (mappings != NULL) {
+ size_t i;
+ for (i = 0; i < num_mappings; i++) {
+ verifier_put_mapping(v, mappings[i].email_domain,
+ mappings[i].key_url_prefix);
}
+ }
return v;
}
-void
-grpc_jwt_verifier_destroy (grpc_jwt_verifier * v)
-{
+void grpc_jwt_verifier_destroy(grpc_jwt_verifier *v) {
size_t i;
- if (v == NULL)
- return;
- grpc_httpcli_context_destroy (&v->http_ctx);
- if (v->mappings != NULL)
- {
- for (i = 0; i < v->num_mappings; i++)
- {
- gpr_free (v->mappings[i].email_domain);
- gpr_free (v->mappings[i].key_url_prefix);
- }
- gpr_free (v->mappings);
- }
- gpr_free (v);
+ if (v == NULL) return;
+ grpc_httpcli_context_destroy(&v->http_ctx);
+ if (v->mappings != NULL) {
+ for (i = 0; i < v->num_mappings; i++) {
+ gpr_free(v->mappings[i].email_domain);
+ gpr_free(v->mappings[i].key_url_prefix);
+ }
+ gpr_free(v->mappings);
+ }
+ gpr_free(v);
}
diff --git a/src/core/security/jwt_verifier.h b/src/core/security/jwt_verifier.h
index 5fc99aafe0..51ea036e4a 100644
--- a/src/core/security/jwt_verifier.h
+++ b/src/core/security/jwt_verifier.h
@@ -50,8 +50,7 @@
/* --- grpc_jwt_verifier_status. --- */
-typedef enum
-{
+typedef enum {
GRPC_JWT_VERIFIER_OK = 0,
GRPC_JWT_VERIFIER_BAD_SIGNATURE,
GRPC_JWT_VERIFIER_BAD_FORMAT,
@@ -61,32 +60,31 @@ typedef enum
GRPC_JWT_VERIFIER_GENERIC_ERROR
} grpc_jwt_verifier_status;
-const char *grpc_jwt_verifier_status_to_string (grpc_jwt_verifier_status status);
+const char *grpc_jwt_verifier_status_to_string(grpc_jwt_verifier_status status);
/* --- grpc_jwt_claims. --- */
typedef struct grpc_jwt_claims grpc_jwt_claims;
-void grpc_jwt_claims_destroy (grpc_jwt_claims * claims);
+void grpc_jwt_claims_destroy(grpc_jwt_claims *claims);
/* Returns the whole JSON tree of the claims. */
-const grpc_json *grpc_jwt_claims_json (const grpc_jwt_claims * claims);
+const grpc_json *grpc_jwt_claims_json(const grpc_jwt_claims *claims);
/* Access to registered claims in https://tools.ietf.org/html/rfc7519#page-9 */
-const char *grpc_jwt_claims_subject (const grpc_jwt_claims * claims);
-const char *grpc_jwt_claims_issuer (const grpc_jwt_claims * claims);
-const char *grpc_jwt_claims_id (const grpc_jwt_claims * claims);
-const char *grpc_jwt_claims_audience (const grpc_jwt_claims * claims);
-gpr_timespec grpc_jwt_claims_issued_at (const grpc_jwt_claims * claims);
-gpr_timespec grpc_jwt_claims_expires_at (const grpc_jwt_claims * claims);
-gpr_timespec grpc_jwt_claims_not_before (const grpc_jwt_claims * claims);
+const char *grpc_jwt_claims_subject(const grpc_jwt_claims *claims);
+const char *grpc_jwt_claims_issuer(const grpc_jwt_claims *claims);
+const char *grpc_jwt_claims_id(const grpc_jwt_claims *claims);
+const char *grpc_jwt_claims_audience(const grpc_jwt_claims *claims);
+gpr_timespec grpc_jwt_claims_issued_at(const grpc_jwt_claims *claims);
+gpr_timespec grpc_jwt_claims_expires_at(const grpc_jwt_claims *claims);
+gpr_timespec grpc_jwt_claims_not_before(const grpc_jwt_claims *claims);
/* --- grpc_jwt_verifier. --- */
typedef struct grpc_jwt_verifier grpc_jwt_verifier;
-typedef struct
-{
+typedef struct {
/* The email domain is the part after the @ sign. */
const char *email_domain;
@@ -106,23 +104,33 @@ extern gpr_timespec grpc_jwt_verifier_max_delay;
A verifier object has one built-in mapping (unless overridden):
GRPC_GOOGLE_SERVICE_ACCOUNTS_EMAIL_DOMAIN ->
GRPC_GOOGLE_SERVICE_ACCOUNTS_KEY_URL_PREFIX.*/
-grpc_jwt_verifier *grpc_jwt_verifier_create (const grpc_jwt_verifier_email_domain_key_url_mapping * mappings, size_t num_mappings);
+grpc_jwt_verifier *grpc_jwt_verifier_create(
+ const grpc_jwt_verifier_email_domain_key_url_mapping *mappings,
+ size_t num_mappings);
/*The verifier must not be destroyed if there are still outstanding callbacks.*/
-void grpc_jwt_verifier_destroy (grpc_jwt_verifier * verifier);
+void grpc_jwt_verifier_destroy(grpc_jwt_verifier *verifier);
/* User provided callback that will be called when the verification of the JWT
is done (maybe in another thread).
It is the responsibility of the callee to call grpc_jwt_claims_destroy on
the claims. */
-typedef void (*grpc_jwt_verification_done_cb) (void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims * claims);
+typedef void (*grpc_jwt_verification_done_cb)(void *user_data,
+ grpc_jwt_verifier_status status,
+ grpc_jwt_claims *claims);
/* Verifies for the JWT for the given expected audience. */
-void grpc_jwt_verifier_verify (grpc_exec_ctx * exec_ctx, grpc_jwt_verifier * verifier, grpc_pollset * pollset, const char *jwt, const char *audience, grpc_jwt_verification_done_cb cb, void *user_data);
+void grpc_jwt_verifier_verify(grpc_exec_ctx *exec_ctx,
+ grpc_jwt_verifier *verifier,
+ grpc_pollset *pollset, const char *jwt,
+ const char *audience,
+ grpc_jwt_verification_done_cb cb,
+ void *user_data);
/* --- TESTING ONLY exposed functions. --- */
-grpc_jwt_claims *grpc_jwt_claims_from_json (grpc_json * json, gpr_slice buffer);
-grpc_jwt_verifier_status grpc_jwt_claims_check (const grpc_jwt_claims * claims, const char *audience);
+grpc_jwt_claims *grpc_jwt_claims_from_json(grpc_json *json, gpr_slice buffer);
+grpc_jwt_verifier_status grpc_jwt_claims_check(const grpc_jwt_claims *claims,
+ const char *audience);
#endif /* GRPC_INTERNAL_CORE_SECURITY_JWT_VERIFIER_H */
diff --git a/src/core/security/secure_endpoint.c b/src/core/security/secure_endpoint.c
index 642068c8c9..0288f7128a 100644
--- a/src/core/security/secure_endpoint.c
+++ b/src/core/security/secure_endpoint.c
@@ -43,8 +43,7 @@
#define STAGING_BUFFER_SIZE 8192
-typedef struct
-{
+typedef struct {
grpc_endpoint base;
grpc_endpoint *wrapped_ep;
struct tsi_frame_protector *protector;
@@ -68,19 +67,17 @@ typedef struct
int grpc_trace_secure_endpoint = 0;
-static void
-destroy (grpc_exec_ctx * exec_ctx, secure_endpoint * secure_ep)
-{
+static void destroy(grpc_exec_ctx *exec_ctx, secure_endpoint *secure_ep) {
secure_endpoint *ep = secure_ep;
- grpc_endpoint_destroy (exec_ctx, ep->wrapped_ep);
- tsi_frame_protector_destroy (ep->protector);
- gpr_slice_buffer_destroy (&ep->leftover_bytes);
- gpr_slice_unref (ep->read_staging_buffer);
- gpr_slice_unref (ep->write_staging_buffer);
- gpr_slice_buffer_destroy (&ep->output_buffer);
- gpr_slice_buffer_destroy (&ep->source_buffer);
- gpr_mu_destroy (&ep->protector_mu);
- gpr_free (ep);
+ grpc_endpoint_destroy(exec_ctx, ep->wrapped_ep);
+ tsi_frame_protector_destroy(ep->protector);
+ gpr_slice_buffer_destroy(&ep->leftover_bytes);
+ gpr_slice_unref(ep->read_staging_buffer);
+ gpr_slice_unref(ep->write_staging_buffer);
+ gpr_slice_buffer_destroy(&ep->output_buffer);
+ gpr_slice_buffer_destroy(&ep->source_buffer);
+ gpr_mu_destroy(&ep->protector_mu);
+ gpr_free(ep);
}
/*#define GRPC_SECURE_ENDPOINT_REFCOUNT_DEBUG*/
@@ -89,326 +86,299 @@ destroy (grpc_exec_ctx * exec_ctx, secure_endpoint * secure_ep)
secure_endpoint_unref((exec_ctx), (ep), (reason), __FILE__, __LINE__)
#define SECURE_ENDPOINT_REF(ep, reason) \
secure_endpoint_ref((ep), (reason), __FILE__, __LINE__)
-static void
-secure_endpoint_unref (secure_endpoint * ep, grpc_closure_list * closure_list, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "SECENDP unref %p : %s %d -> %d", ep, reason, ep->ref.count, ep->ref.count - 1);
- if (gpr_unref (&ep->ref))
- {
- destroy (exec_ctx, ep);
- }
+static void secure_endpoint_unref(secure_endpoint *ep,
+ grpc_closure_list *closure_list,
+ const char *reason, const char *file,
+ int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "SECENDP unref %p : %s %d -> %d",
+ ep, reason, ep->ref.count, ep->ref.count - 1);
+ if (gpr_unref(&ep->ref)) {
+ destroy(exec_ctx, ep);
+ }
}
-static void
-secure_endpoint_ref (secure_endpoint * ep, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "SECENDP ref %p : %s %d -> %d", ep, reason, ep->ref.count, ep->ref.count + 1);
- gpr_ref (&ep->ref);
+static void secure_endpoint_ref(secure_endpoint *ep, const char *reason,
+ const char *file, int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "SECENDP ref %p : %s %d -> %d",
+ ep, reason, ep->ref.count, ep->ref.count + 1);
+ gpr_ref(&ep->ref);
}
#else
-#define SECURE_ENDPOINT_UNREF(exec_ctx, ep, reason) secure_endpoint_unref((exec_ctx), (ep))
+#define SECURE_ENDPOINT_UNREF(exec_ctx, ep, reason) \
+ secure_endpoint_unref((exec_ctx), (ep))
#define SECURE_ENDPOINT_REF(ep, reason) secure_endpoint_ref((ep))
-static void
-secure_endpoint_unref (grpc_exec_ctx * exec_ctx, secure_endpoint * ep)
-{
- if (gpr_unref (&ep->ref))
- {
- destroy (exec_ctx, ep);
- }
+static void secure_endpoint_unref(grpc_exec_ctx *exec_ctx,
+ secure_endpoint *ep) {
+ if (gpr_unref(&ep->ref)) {
+ destroy(exec_ctx, ep);
+ }
}
-static void
-secure_endpoint_ref (secure_endpoint * ep)
-{
- gpr_ref (&ep->ref);
-}
+static void secure_endpoint_ref(secure_endpoint *ep) { gpr_ref(&ep->ref); }
#endif
-static void
-flush_read_staging_buffer (secure_endpoint * ep, gpr_uint8 ** cur, gpr_uint8 ** end)
-{
- gpr_slice_buffer_add (ep->read_buffer, ep->read_staging_buffer);
- ep->read_staging_buffer = gpr_slice_malloc (STAGING_BUFFER_SIZE);
- *cur = GPR_SLICE_START_PTR (ep->read_staging_buffer);
- *end = GPR_SLICE_END_PTR (ep->read_staging_buffer);
+static void flush_read_staging_buffer(secure_endpoint *ep, gpr_uint8 **cur,
+ gpr_uint8 **end) {
+ gpr_slice_buffer_add(ep->read_buffer, ep->read_staging_buffer);
+ ep->read_staging_buffer = gpr_slice_malloc(STAGING_BUFFER_SIZE);
+ *cur = GPR_SLICE_START_PTR(ep->read_staging_buffer);
+ *end = GPR_SLICE_END_PTR(ep->read_staging_buffer);
}
-static void
-call_read_cb (grpc_exec_ctx * exec_ctx, secure_endpoint * ep, int success)
-{
- if (grpc_trace_secure_endpoint)
- {
- size_t i;
- for (i = 0; i < ep->read_buffer->count; i++)
- {
- char *data = gpr_dump_slice (ep->read_buffer->slices[i],
- GPR_DUMP_HEX | GPR_DUMP_ASCII);
- gpr_log (GPR_DEBUG, "READ %p: %s", ep, data);
- gpr_free (data);
- }
+static void call_read_cb(grpc_exec_ctx *exec_ctx, secure_endpoint *ep,
+ int success) {
+ if (grpc_trace_secure_endpoint) {
+ size_t i;
+ for (i = 0; i < ep->read_buffer->count; i++) {
+ char *data = gpr_dump_slice(ep->read_buffer->slices[i],
+ GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ gpr_log(GPR_DEBUG, "READ %p: %s", ep, data);
+ gpr_free(data);
}
+ }
ep->read_buffer = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, ep->read_cb, success);
- SECURE_ENDPOINT_UNREF (exec_ctx, ep, "read");
+ grpc_exec_ctx_enqueue(exec_ctx, ep->read_cb, success);
+ SECURE_ENDPOINT_UNREF(exec_ctx, ep, "read");
}
-static void
-on_read (grpc_exec_ctx * exec_ctx, void *user_data, int success)
-{
+static void on_read(grpc_exec_ctx *exec_ctx, void *user_data, int success) {
unsigned i;
gpr_uint8 keep_looping = 0;
tsi_result result = TSI_OK;
- secure_endpoint *ep = (secure_endpoint *) user_data;
- gpr_uint8 *cur = GPR_SLICE_START_PTR (ep->read_staging_buffer);
- gpr_uint8 *end = GPR_SLICE_END_PTR (ep->read_staging_buffer);
-
- if (!success)
- {
- gpr_slice_buffer_reset_and_unref (ep->read_buffer);
- call_read_cb (exec_ctx, ep, 0);
- return;
- }
+ secure_endpoint *ep = (secure_endpoint *)user_data;
+ gpr_uint8 *cur = GPR_SLICE_START_PTR(ep->read_staging_buffer);
+ gpr_uint8 *end = GPR_SLICE_END_PTR(ep->read_staging_buffer);
+
+ if (!success) {
+ gpr_slice_buffer_reset_and_unref(ep->read_buffer);
+ call_read_cb(exec_ctx, ep, 0);
+ return;
+ }
/* TODO(yangg) check error, maybe bail out early */
- for (i = 0; i < ep->source_buffer.count; i++)
- {
- gpr_slice encrypted = ep->source_buffer.slices[i];
- gpr_uint8 *message_bytes = GPR_SLICE_START_PTR (encrypted);
- size_t message_size = GPR_SLICE_LENGTH (encrypted);
-
- while (message_size > 0 || keep_looping)
- {
- size_t unprotected_buffer_size_written = (size_t) (end - cur);
- size_t processed_message_size = message_size;
- gpr_mu_lock (&ep->protector_mu);
- result = tsi_frame_protector_unprotect (ep->protector, message_bytes, &processed_message_size, cur, &unprotected_buffer_size_written);
- gpr_mu_unlock (&ep->protector_mu);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Decryption error: %s", tsi_result_to_string (result));
- break;
- }
- message_bytes += processed_message_size;
- message_size -= processed_message_size;
- cur += unprotected_buffer_size_written;
-
- if (cur == end)
- {
- flush_read_staging_buffer (ep, &cur, &end);
- /* Force to enter the loop again to extract buffered bytes in protector.
- The bytes could be buffered because of running out of staging_buffer.
- If this happens at the end of all slices, doing another unprotect
- avoids leaving data in the protector. */
- keep_looping = 1;
- }
- else if (unprotected_buffer_size_written > 0)
- {
- keep_looping = 1;
- }
- else
- {
- keep_looping = 0;
- }
- }
- if (result != TSI_OK)
- break;
+ for (i = 0; i < ep->source_buffer.count; i++) {
+ gpr_slice encrypted = ep->source_buffer.slices[i];
+ gpr_uint8 *message_bytes = GPR_SLICE_START_PTR(encrypted);
+ size_t message_size = GPR_SLICE_LENGTH(encrypted);
+
+ while (message_size > 0 || keep_looping) {
+ size_t unprotected_buffer_size_written = (size_t)(end - cur);
+ size_t processed_message_size = message_size;
+ gpr_mu_lock(&ep->protector_mu);
+ result = tsi_frame_protector_unprotect(ep->protector, message_bytes,
+ &processed_message_size, cur,
+ &unprotected_buffer_size_written);
+ gpr_mu_unlock(&ep->protector_mu);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Decryption error: %s",
+ tsi_result_to_string(result));
+ break;
+ }
+ message_bytes += processed_message_size;
+ message_size -= processed_message_size;
+ cur += unprotected_buffer_size_written;
+
+ if (cur == end) {
+ flush_read_staging_buffer(ep, &cur, &end);
+ /* Force to enter the loop again to extract buffered bytes in protector.
+ The bytes could be buffered because of running out of staging_buffer.
+ If this happens at the end of all slices, doing another unprotect
+ avoids leaving data in the protector. */
+ keep_looping = 1;
+ } else if (unprotected_buffer_size_written > 0) {
+ keep_looping = 1;
+ } else {
+ keep_looping = 0;
+ }
}
+ if (result != TSI_OK) break;
+ }
- if (cur != GPR_SLICE_START_PTR (ep->read_staging_buffer))
- {
- gpr_slice_buffer_add (ep->read_buffer, gpr_slice_split_head (&ep->read_staging_buffer, (size_t) (cur - GPR_SLICE_START_PTR (ep->read_staging_buffer))));
- }
+ if (cur != GPR_SLICE_START_PTR(ep->read_staging_buffer)) {
+ gpr_slice_buffer_add(
+ ep->read_buffer,
+ gpr_slice_split_head(
+ &ep->read_staging_buffer,
+ (size_t)(cur - GPR_SLICE_START_PTR(ep->read_staging_buffer))));
+ }
/* TODO(yangg) experiment with moving this block after read_cb to see if it
helps latency */
- gpr_slice_buffer_reset_and_unref (&ep->source_buffer);
+ gpr_slice_buffer_reset_and_unref(&ep->source_buffer);
- if (result != TSI_OK)
- {
- gpr_slice_buffer_reset_and_unref (ep->read_buffer);
- call_read_cb (exec_ctx, ep, 0);
- return;
- }
+ if (result != TSI_OK) {
+ gpr_slice_buffer_reset_and_unref(ep->read_buffer);
+ call_read_cb(exec_ctx, ep, 0);
+ return;
+ }
- call_read_cb (exec_ctx, ep, 1);
+ call_read_cb(exec_ctx, ep, 1);
}
-static void
-endpoint_read (grpc_exec_ctx * exec_ctx, grpc_endpoint * secure_ep, gpr_slice_buffer * slices, grpc_closure * cb)
-{
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
+static void endpoint_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *secure_ep,
+ gpr_slice_buffer *slices, grpc_closure *cb) {
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
ep->read_cb = cb;
ep->read_buffer = slices;
- gpr_slice_buffer_reset_and_unref (ep->read_buffer);
-
- SECURE_ENDPOINT_REF (ep, "read");
- if (ep->leftover_bytes.count)
- {
- gpr_slice_buffer_swap (&ep->leftover_bytes, &ep->source_buffer);
- GPR_ASSERT (ep->leftover_bytes.count == 0);
- on_read (exec_ctx, ep, 1);
- return;
- }
-
- grpc_endpoint_read (exec_ctx, ep->wrapped_ep, &ep->source_buffer, &ep->on_read);
+ gpr_slice_buffer_reset_and_unref(ep->read_buffer);
+
+ SECURE_ENDPOINT_REF(ep, "read");
+ if (ep->leftover_bytes.count) {
+ gpr_slice_buffer_swap(&ep->leftover_bytes, &ep->source_buffer);
+ GPR_ASSERT(ep->leftover_bytes.count == 0);
+ on_read(exec_ctx, ep, 1);
+ return;
+ }
+
+ grpc_endpoint_read(exec_ctx, ep->wrapped_ep, &ep->source_buffer,
+ &ep->on_read);
}
-static void
-flush_write_staging_buffer (secure_endpoint * ep, gpr_uint8 ** cur, gpr_uint8 ** end)
-{
- gpr_slice_buffer_add (&ep->output_buffer, ep->write_staging_buffer);
- ep->write_staging_buffer = gpr_slice_malloc (STAGING_BUFFER_SIZE);
- *cur = GPR_SLICE_START_PTR (ep->write_staging_buffer);
- *end = GPR_SLICE_END_PTR (ep->write_staging_buffer);
+static void flush_write_staging_buffer(secure_endpoint *ep, gpr_uint8 **cur,
+ gpr_uint8 **end) {
+ gpr_slice_buffer_add(&ep->output_buffer, ep->write_staging_buffer);
+ ep->write_staging_buffer = gpr_slice_malloc(STAGING_BUFFER_SIZE);
+ *cur = GPR_SLICE_START_PTR(ep->write_staging_buffer);
+ *end = GPR_SLICE_END_PTR(ep->write_staging_buffer);
}
-static void
-endpoint_write (grpc_exec_ctx * exec_ctx, grpc_endpoint * secure_ep, gpr_slice_buffer * slices, grpc_closure * cb)
-{
+static void endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *secure_ep,
+ gpr_slice_buffer *slices, grpc_closure *cb) {
unsigned i;
tsi_result result = TSI_OK;
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
- gpr_uint8 *cur = GPR_SLICE_START_PTR (ep->write_staging_buffer);
- gpr_uint8 *end = GPR_SLICE_END_PTR (ep->write_staging_buffer);
-
- gpr_slice_buffer_reset_and_unref (&ep->output_buffer);
-
- if (grpc_trace_secure_endpoint)
- {
- for (i = 0; i < slices->count; i++)
- {
- char *data = gpr_dump_slice (slices->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII);
- gpr_log (GPR_DEBUG, "WRITE %p: %s", ep, data);
- gpr_free (data);
- }
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
+ gpr_uint8 *cur = GPR_SLICE_START_PTR(ep->write_staging_buffer);
+ gpr_uint8 *end = GPR_SLICE_END_PTR(ep->write_staging_buffer);
+
+ gpr_slice_buffer_reset_and_unref(&ep->output_buffer);
+
+ if (grpc_trace_secure_endpoint) {
+ for (i = 0; i < slices->count; i++) {
+ char *data =
+ gpr_dump_slice(slices->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ gpr_log(GPR_DEBUG, "WRITE %p: %s", ep, data);
+ gpr_free(data);
}
-
- for (i = 0; i < slices->count; i++)
- {
- gpr_slice plain = slices->slices[i];
- gpr_uint8 *message_bytes = GPR_SLICE_START_PTR (plain);
- size_t message_size = GPR_SLICE_LENGTH (plain);
- while (message_size > 0)
- {
- size_t protected_buffer_size_to_send = (size_t) (end - cur);
- size_t processed_message_size = message_size;
- gpr_mu_lock (&ep->protector_mu);
- result = tsi_frame_protector_protect (ep->protector, message_bytes, &processed_message_size, cur, &protected_buffer_size_to_send);
- gpr_mu_unlock (&ep->protector_mu);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Encryption error: %s", tsi_result_to_string (result));
- break;
- }
- message_bytes += processed_message_size;
- message_size -= processed_message_size;
- cur += protected_buffer_size_to_send;
-
- if (cur == end)
- {
- flush_write_staging_buffer (ep, &cur, &end);
- }
- }
- if (result != TSI_OK)
- break;
+ }
+
+ for (i = 0; i < slices->count; i++) {
+ gpr_slice plain = slices->slices[i];
+ gpr_uint8 *message_bytes = GPR_SLICE_START_PTR(plain);
+ size_t message_size = GPR_SLICE_LENGTH(plain);
+ while (message_size > 0) {
+ size_t protected_buffer_size_to_send = (size_t)(end - cur);
+ size_t processed_message_size = message_size;
+ gpr_mu_lock(&ep->protector_mu);
+ result = tsi_frame_protector_protect(ep->protector, message_bytes,
+ &processed_message_size, cur,
+ &protected_buffer_size_to_send);
+ gpr_mu_unlock(&ep->protector_mu);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Encryption error: %s",
+ tsi_result_to_string(result));
+ break;
+ }
+ message_bytes += processed_message_size;
+ message_size -= processed_message_size;
+ cur += protected_buffer_size_to_send;
+
+ if (cur == end) {
+ flush_write_staging_buffer(ep, &cur, &end);
+ }
}
- if (result == TSI_OK)
- {
- size_t still_pending_size;
- do
- {
- size_t protected_buffer_size_to_send = (size_t) (end - cur);
- gpr_mu_lock (&ep->protector_mu);
- result = tsi_frame_protector_protect_flush (ep->protector, cur, &protected_buffer_size_to_send, &still_pending_size);
- gpr_mu_unlock (&ep->protector_mu);
- if (result != TSI_OK)
- break;
- cur += protected_buffer_size_to_send;
- if (cur == end)
- {
- flush_write_staging_buffer (ep, &cur, &end);
- }
- }
- while (still_pending_size > 0);
- if (cur != GPR_SLICE_START_PTR (ep->write_staging_buffer))
- {
- gpr_slice_buffer_add (&ep->output_buffer, gpr_slice_split_head (&ep->write_staging_buffer, (size_t) (cur - GPR_SLICE_START_PTR (ep->write_staging_buffer))));
- }
+ if (result != TSI_OK) break;
+ }
+ if (result == TSI_OK) {
+ size_t still_pending_size;
+ do {
+ size_t protected_buffer_size_to_send = (size_t)(end - cur);
+ gpr_mu_lock(&ep->protector_mu);
+ result = tsi_frame_protector_protect_flush(ep->protector, cur,
+ &protected_buffer_size_to_send,
+ &still_pending_size);
+ gpr_mu_unlock(&ep->protector_mu);
+ if (result != TSI_OK) break;
+ cur += protected_buffer_size_to_send;
+ if (cur == end) {
+ flush_write_staging_buffer(ep, &cur, &end);
+ }
+ } while (still_pending_size > 0);
+ if (cur != GPR_SLICE_START_PTR(ep->write_staging_buffer)) {
+ gpr_slice_buffer_add(
+ &ep->output_buffer,
+ gpr_slice_split_head(
+ &ep->write_staging_buffer,
+ (size_t)(cur - GPR_SLICE_START_PTR(ep->write_staging_buffer))));
}
+ }
- if (result != TSI_OK)
- {
- /* TODO(yangg) do different things according to the error type? */
- gpr_slice_buffer_reset_and_unref (&ep->output_buffer);
- grpc_exec_ctx_enqueue (exec_ctx, cb, 0);
- return;
- }
+ if (result != TSI_OK) {
+ /* TODO(yangg) do different things according to the error type? */
+ gpr_slice_buffer_reset_and_unref(&ep->output_buffer);
+ grpc_exec_ctx_enqueue(exec_ctx, cb, 0);
+ return;
+ }
- grpc_endpoint_write (exec_ctx, ep->wrapped_ep, &ep->output_buffer, cb);
+ grpc_endpoint_write(exec_ctx, ep->wrapped_ep, &ep->output_buffer, cb);
}
-static void
-endpoint_shutdown (grpc_exec_ctx * exec_ctx, grpc_endpoint * secure_ep)
-{
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
- grpc_endpoint_shutdown (exec_ctx, ep->wrapped_ep);
+static void endpoint_shutdown(grpc_exec_ctx *exec_ctx,
+ grpc_endpoint *secure_ep) {
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
+ grpc_endpoint_shutdown(exec_ctx, ep->wrapped_ep);
}
-static void
-endpoint_destroy (grpc_exec_ctx * exec_ctx, grpc_endpoint * secure_ep)
-{
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
- SECURE_ENDPOINT_UNREF (exec_ctx, ep, "destroy");
+static void endpoint_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_endpoint *secure_ep) {
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
+ SECURE_ENDPOINT_UNREF(exec_ctx, ep, "destroy");
}
-static void
-endpoint_add_to_pollset (grpc_exec_ctx * exec_ctx, grpc_endpoint * secure_ep, grpc_pollset * pollset)
-{
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
- grpc_endpoint_add_to_pollset (exec_ctx, ep->wrapped_ep, pollset);
+static void endpoint_add_to_pollset(grpc_exec_ctx *exec_ctx,
+ grpc_endpoint *secure_ep,
+ grpc_pollset *pollset) {
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
+ grpc_endpoint_add_to_pollset(exec_ctx, ep->wrapped_ep, pollset);
}
-static void
-endpoint_add_to_pollset_set (grpc_exec_ctx * exec_ctx, grpc_endpoint * secure_ep, grpc_pollset_set * pollset_set)
-{
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
- grpc_endpoint_add_to_pollset_set (exec_ctx, ep->wrapped_ep, pollset_set);
+static void endpoint_add_to_pollset_set(grpc_exec_ctx *exec_ctx,
+ grpc_endpoint *secure_ep,
+ grpc_pollset_set *pollset_set) {
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
+ grpc_endpoint_add_to_pollset_set(exec_ctx, ep->wrapped_ep, pollset_set);
}
-static char *
-endpoint_get_peer (grpc_endpoint * secure_ep)
-{
- secure_endpoint *ep = (secure_endpoint *) secure_ep;
- return grpc_endpoint_get_peer (ep->wrapped_ep);
+static char *endpoint_get_peer(grpc_endpoint *secure_ep) {
+ secure_endpoint *ep = (secure_endpoint *)secure_ep;
+ return grpc_endpoint_get_peer(ep->wrapped_ep);
}
static const grpc_endpoint_vtable vtable = {
- endpoint_read, endpoint_write,
- endpoint_add_to_pollset, endpoint_add_to_pollset_set,
- endpoint_shutdown, endpoint_destroy,
- endpoint_get_peer
-};
-
-grpc_endpoint *
-grpc_secure_endpoint_create (struct tsi_frame_protector *protector, grpc_endpoint * transport, gpr_slice * leftover_slices, size_t leftover_nslices)
-{
+ endpoint_read, endpoint_write,
+ endpoint_add_to_pollset, endpoint_add_to_pollset_set,
+ endpoint_shutdown, endpoint_destroy,
+ endpoint_get_peer};
+
+grpc_endpoint *grpc_secure_endpoint_create(
+ struct tsi_frame_protector *protector, grpc_endpoint *transport,
+ gpr_slice *leftover_slices, size_t leftover_nslices) {
size_t i;
- secure_endpoint *ep = (secure_endpoint *) gpr_malloc (sizeof (secure_endpoint));
+ secure_endpoint *ep = (secure_endpoint *)gpr_malloc(sizeof(secure_endpoint));
ep->base.vtable = &vtable;
ep->wrapped_ep = transport;
ep->protector = protector;
- gpr_slice_buffer_init (&ep->leftover_bytes);
- for (i = 0; i < leftover_nslices; i++)
- {
- gpr_slice_buffer_add (&ep->leftover_bytes, gpr_slice_ref (leftover_slices[i]));
- }
- ep->write_staging_buffer = gpr_slice_malloc (STAGING_BUFFER_SIZE);
- ep->read_staging_buffer = gpr_slice_malloc (STAGING_BUFFER_SIZE);
- gpr_slice_buffer_init (&ep->output_buffer);
- gpr_slice_buffer_init (&ep->source_buffer);
+ gpr_slice_buffer_init(&ep->leftover_bytes);
+ for (i = 0; i < leftover_nslices; i++) {
+ gpr_slice_buffer_add(&ep->leftover_bytes,
+ gpr_slice_ref(leftover_slices[i]));
+ }
+ ep->write_staging_buffer = gpr_slice_malloc(STAGING_BUFFER_SIZE);
+ ep->read_staging_buffer = gpr_slice_malloc(STAGING_BUFFER_SIZE);
+ gpr_slice_buffer_init(&ep->output_buffer);
+ gpr_slice_buffer_init(&ep->source_buffer);
ep->read_buffer = NULL;
- grpc_closure_init (&ep->on_read, on_read, ep);
- gpr_mu_init (&ep->protector_mu);
- gpr_ref_init (&ep->ref, 1);
+ grpc_closure_init(&ep->on_read, on_read, ep);
+ gpr_mu_init(&ep->protector_mu);
+ gpr_ref_init(&ep->ref, 1);
return &ep->base;
}
diff --git a/src/core/security/secure_endpoint.h b/src/core/security/secure_endpoint.h
index e80a177dd5..c563bdd9c5 100644
--- a/src/core/security/secure_endpoint.h
+++ b/src/core/security/secure_endpoint.h
@@ -42,6 +42,8 @@ struct tsi_frame_protector;
extern int grpc_trace_secure_endpoint;
/* Takes ownership of protector and to_wrap, and refs leftover_slices. */
-grpc_endpoint *grpc_secure_endpoint_create (struct tsi_frame_protector *protector, grpc_endpoint * to_wrap, gpr_slice * leftover_slices, size_t leftover_nslices);
+grpc_endpoint *grpc_secure_endpoint_create(
+ struct tsi_frame_protector *protector, grpc_endpoint *to_wrap,
+ gpr_slice *leftover_slices, size_t leftover_nslices);
#endif /* GRPC_INTERNAL_CORE_SECURITY_SECURE_ENDPOINT_H */
diff --git a/src/core/security/security_connector.c b/src/core/security/security_connector.c
index f720f5d728..ec5b1c86e5 100644
--- a/src/core/security/security_connector.c
+++ b/src/core/security/security_connector.c
@@ -57,7 +57,8 @@
#ifndef INSTALL_PREFIX
static const char *installed_roots_path = "/usr/share/grpc/roots.pem";
#else
-static const char *installed_roots_path = INSTALL_PREFIX "/share/grpc/roots.pem";
+static const char *installed_roots_path =
+ INSTALL_PREFIX "/share/grpc/roots.pem";
#endif
/* -- Cipher suites. -- */
@@ -71,127 +72,106 @@ static const char *installed_roots_path = INSTALL_PREFIX "/share/grpc/roots.pem"
static gpr_once cipher_suites_once = GPR_ONCE_INIT;
static const char *cipher_suites = NULL;
-static void
-init_cipher_suites (void)
-{
- char *overridden = gpr_getenv ("GRPC_SSL_CIPHER_SUITES");
+static void init_cipher_suites(void) {
+ char *overridden = gpr_getenv("GRPC_SSL_CIPHER_SUITES");
cipher_suites = overridden != NULL ? overridden : GRPC_SSL_CIPHER_SUITES;
}
-static const char *
-ssl_cipher_suites (void)
-{
- gpr_once_init (&cipher_suites_once, init_cipher_suites);
+static const char *ssl_cipher_suites(void) {
+ gpr_once_init(&cipher_suites_once, init_cipher_suites);
return cipher_suites;
}
/* -- Common methods. -- */
/* Returns the first property with that name. */
-const tsi_peer_property *
-tsi_peer_get_property_by_name (const tsi_peer * peer, const char *name)
-{
+const tsi_peer_property *tsi_peer_get_property_by_name(const tsi_peer *peer,
+ const char *name) {
size_t i;
- if (peer == NULL)
- return NULL;
- for (i = 0; i < peer->property_count; i++)
- {
- const tsi_peer_property *property = &peer->properties[i];
- if (name == NULL && property->name == NULL)
- {
- return property;
- }
- if (name != NULL && property->name != NULL && strcmp (property->name, name) == 0)
- {
- return property;
- }
+ if (peer == NULL) return NULL;
+ for (i = 0; i < peer->property_count; i++) {
+ const tsi_peer_property *property = &peer->properties[i];
+ if (name == NULL && property->name == NULL) {
+ return property;
+ }
+ if (name != NULL && property->name != NULL &&
+ strcmp(property->name, name) == 0) {
+ return property;
}
+ }
return NULL;
}
-void
-grpc_security_connector_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- if (sc == NULL || nonsecure_endpoint == NULL)
- {
- cb (exec_ctx, user_data, GRPC_SECURITY_ERROR, nonsecure_endpoint, NULL);
- }
- else
- {
- sc->vtable->do_handshake (exec_ctx, sc, nonsecure_endpoint, cb, user_data);
- }
+void grpc_security_connector_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ if (sc == NULL || nonsecure_endpoint == NULL) {
+ cb(exec_ctx, user_data, GRPC_SECURITY_ERROR, nonsecure_endpoint, NULL);
+ } else {
+ sc->vtable->do_handshake(exec_ctx, sc, nonsecure_endpoint, cb, user_data);
+ }
}
-grpc_security_status
-grpc_security_connector_check_peer (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data)
-{
- if (sc == NULL)
- {
- tsi_peer_destruct (&peer);
- return GRPC_SECURITY_ERROR;
- }
- return sc->vtable->check_peer (sc, peer, cb, user_data);
+grpc_security_status grpc_security_connector_check_peer(
+ grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb,
+ void *user_data) {
+ if (sc == NULL) {
+ tsi_peer_destruct(&peer);
+ return GRPC_SECURITY_ERROR;
+ }
+ return sc->vtable->check_peer(sc, peer, cb, user_data);
}
-grpc_security_status
-grpc_channel_security_connector_check_call_host (grpc_exec_ctx * exec_ctx, grpc_channel_security_connector * sc, const char *host, grpc_security_check_cb cb, void *user_data)
-{
- if (sc == NULL || sc->check_call_host == NULL)
- return GRPC_SECURITY_ERROR;
- return sc->check_call_host (exec_ctx, sc, host, cb, user_data);
+grpc_security_status grpc_channel_security_connector_check_call_host(
+ grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc,
+ const char *host, grpc_security_check_cb cb, void *user_data) {
+ if (sc == NULL || sc->check_call_host == NULL) return GRPC_SECURITY_ERROR;
+ return sc->check_call_host(exec_ctx, sc, host, cb, user_data);
}
#ifdef GRPC_SECURITY_CONNECTOR_REFCOUNT_DEBUG
-grpc_security_connector *
-grpc_security_connector_ref (grpc_security_connector * sc, const char *file, int line, const char *reason)
-{
- if (sc == NULL)
- return NULL;
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "SECURITY_CONNECTOR:%p ref %d -> %d %s", sc, (int) sc->refcount.count, (int) sc->refcount.count + 1, reason);
+grpc_security_connector *grpc_security_connector_ref(
+ grpc_security_connector *sc, const char *file, int line,
+ const char *reason) {
+ if (sc == NULL) return NULL;
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "SECURITY_CONNECTOR:%p ref %d -> %d %s", sc,
+ (int)sc->refcount.count, (int)sc->refcount.count + 1, reason);
#else
-grpc_security_connector *
-grpc_security_connector_ref (grpc_security_connector * sc)
-{
- if (sc == NULL)
- return NULL;
+grpc_security_connector *grpc_security_connector_ref(
+ grpc_security_connector *sc) {
+ if (sc == NULL) return NULL;
#endif
- gpr_ref (&sc->refcount);
+ gpr_ref(&sc->refcount);
return sc;
}
#ifdef GRPC_SECURITY_CONNECTOR_REFCOUNT_DEBUG
-void
-grpc_security_connector_unref (grpc_security_connector * sc, const char *file, int line, const char *reason)
-{
- if (sc == NULL)
- return;
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "SECURITY_CONNECTOR:%p unref %d -> %d %s", sc, (int) sc->refcount.count, (int) sc->refcount.count - 1, reason);
+void grpc_security_connector_unref(grpc_security_connector *sc,
+ const char *file, int line,
+ const char *reason) {
+ if (sc == NULL) return;
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "SECURITY_CONNECTOR:%p unref %d -> %d %s", sc,
+ (int)sc->refcount.count, (int)sc->refcount.count - 1, reason);
#else
-void
-grpc_security_connector_unref (grpc_security_connector * sc)
-{
- if (sc == NULL)
- return;
+void grpc_security_connector_unref(grpc_security_connector *sc) {
+ if (sc == NULL) return;
#endif
- if (gpr_unref (&sc->refcount))
- sc->vtable->destroy (sc);
+ if (gpr_unref(&sc->refcount)) sc->vtable->destroy(sc);
}
-static void
-connector_pointer_arg_destroy (void *p)
-{
- GRPC_SECURITY_CONNECTOR_UNREF (p, "connector_pointer_arg");
+static void connector_pointer_arg_destroy(void *p) {
+ GRPC_SECURITY_CONNECTOR_UNREF(p, "connector_pointer_arg");
}
-static void *
-connector_pointer_arg_copy (void *p)
-{
- return GRPC_SECURITY_CONNECTOR_REF (p, "connector_pointer_arg");
+static void *connector_pointer_arg_copy(void *p) {
+ return GRPC_SECURITY_CONNECTOR_REF(p, "connector_pointer_arg");
}
-grpc_arg
-grpc_security_connector_to_arg (grpc_security_connector * sc)
-{
+grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc) {
grpc_arg result;
result.type = GRPC_ARG_POINTER;
result.key = GRPC_SECURITY_CONNECTOR_ARG;
@@ -201,159 +181,150 @@ grpc_security_connector_to_arg (grpc_security_connector * sc)
return result;
}
-grpc_security_connector *
-grpc_security_connector_from_arg (const grpc_arg * arg)
-{
- if (strcmp (arg->key, GRPC_SECURITY_CONNECTOR_ARG))
+grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg) {
+ if (strcmp(arg->key, GRPC_SECURITY_CONNECTOR_ARG)) return NULL;
+ if (arg->type != GRPC_ARG_POINTER) {
+ gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
+ GRPC_SECURITY_CONNECTOR_ARG);
return NULL;
- if (arg->type != GRPC_ARG_POINTER)
- {
- gpr_log (GPR_ERROR, "Invalid type %d for arg %s", arg->type, GRPC_SECURITY_CONNECTOR_ARG);
- return NULL;
- }
+ }
return arg->value.pointer.p;
}
-grpc_security_connector *
-grpc_find_security_connector_in_args (const grpc_channel_args * args)
-{
+grpc_security_connector *grpc_find_security_connector_in_args(
+ const grpc_channel_args *args) {
size_t i;
- if (args == NULL)
- return NULL;
- for (i = 0; i < args->num_args; i++)
- {
- grpc_security_connector *sc = grpc_security_connector_from_arg (&args->args[i]);
- if (sc != NULL)
- return sc;
- }
+ if (args == NULL) return NULL;
+ for (i = 0; i < args->num_args; i++) {
+ grpc_security_connector *sc =
+ grpc_security_connector_from_arg(&args->args[i]);
+ if (sc != NULL) return sc;
+ }
return NULL;
}
-static int
-check_request_metadata_creds (grpc_credentials * creds)
-{
- if (creds != NULL && !grpc_credentials_has_request_metadata (creds))
- {
- gpr_log (GPR_ERROR, "Incompatible credentials for channel security connector: needs to " "set request metadata.");
- return 0;
- }
+static int check_request_metadata_creds(grpc_credentials *creds) {
+ if (creds != NULL && !grpc_credentials_has_request_metadata(creds)) {
+ gpr_log(GPR_ERROR,
+ "Incompatible credentials for channel security connector: needs to "
+ "set request metadata.");
+ return 0;
+ }
return 1;
}
/* -- Fake implementation. -- */
-typedef struct
-{
+typedef struct {
grpc_channel_security_connector base;
int call_host_check_is_async;
} grpc_fake_channel_security_connector;
-static void
-fake_channel_destroy (grpc_security_connector * sc)
-{
- grpc_channel_security_connector *c = (grpc_channel_security_connector *) sc;
- grpc_credentials_unref (c->request_metadata_creds);
- GRPC_AUTH_CONTEXT_UNREF (sc->auth_context, "connector");
- gpr_free (sc);
+static void fake_channel_destroy(grpc_security_connector *sc) {
+ grpc_channel_security_connector *c = (grpc_channel_security_connector *)sc;
+ grpc_credentials_unref(c->request_metadata_creds);
+ GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector");
+ gpr_free(sc);
}
-static void
-fake_server_destroy (grpc_security_connector * sc)
-{
- GRPC_AUTH_CONTEXT_UNREF (sc->auth_context, "connector");
- gpr_free (sc);
+static void fake_server_destroy(grpc_security_connector *sc) {
+ GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector");
+ gpr_free(sc);
}
-static grpc_security_status
-fake_check_peer (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data)
-{
+static grpc_security_status fake_check_peer(grpc_security_connector *sc,
+ tsi_peer peer,
+ grpc_security_check_cb cb,
+ void *user_data) {
const char *prop_name;
grpc_security_status status = GRPC_SECURITY_OK;
- if (peer.property_count != 1)
- {
- gpr_log (GPR_ERROR, "Fake peers should only have 1 property.");
- status = GRPC_SECURITY_ERROR;
- goto end;
- }
+ if (peer.property_count != 1) {
+ gpr_log(GPR_ERROR, "Fake peers should only have 1 property.");
+ status = GRPC_SECURITY_ERROR;
+ goto end;
+ }
prop_name = peer.properties[0].name;
- if (prop_name == NULL || strcmp (prop_name, TSI_CERTIFICATE_TYPE_PEER_PROPERTY))
- {
- gpr_log (GPR_ERROR, "Unexpected property in fake peer: %s.", prop_name == NULL ? "<EMPTY>" : prop_name);
- status = GRPC_SECURITY_ERROR;
- goto end;
- }
- if (strncmp (peer.properties[0].value.data, TSI_FAKE_CERTIFICATE_TYPE, peer.properties[0].value.length))
- {
- gpr_log (GPR_ERROR, "Invalid value for cert type property.");
- status = GRPC_SECURITY_ERROR;
- goto end;
- }
- GRPC_AUTH_CONTEXT_UNREF (sc->auth_context, "connector");
- sc->auth_context = grpc_auth_context_create (NULL);
- grpc_auth_context_add_cstring_property (sc->auth_context, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME, GRPC_FAKE_TRANSPORT_SECURITY_TYPE);
+ if (prop_name == NULL ||
+ strcmp(prop_name, TSI_CERTIFICATE_TYPE_PEER_PROPERTY)) {
+ gpr_log(GPR_ERROR, "Unexpected property in fake peer: %s.",
+ prop_name == NULL ? "<EMPTY>" : prop_name);
+ status = GRPC_SECURITY_ERROR;
+ goto end;
+ }
+ if (strncmp(peer.properties[0].value.data, TSI_FAKE_CERTIFICATE_TYPE,
+ peer.properties[0].value.length)) {
+ gpr_log(GPR_ERROR, "Invalid value for cert type property.");
+ status = GRPC_SECURITY_ERROR;
+ goto end;
+ }
+ GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector");
+ sc->auth_context = grpc_auth_context_create(NULL);
+ grpc_auth_context_add_cstring_property(
+ sc->auth_context, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME,
+ GRPC_FAKE_TRANSPORT_SECURITY_TYPE);
end:
- tsi_peer_destruct (&peer);
+ tsi_peer_destruct(&peer);
return status;
}
-static grpc_security_status
-fake_channel_check_call_host (grpc_exec_ctx * exec_ctx, grpc_channel_security_connector * sc, const char *host, grpc_security_check_cb cb, void *user_data)
-{
- grpc_fake_channel_security_connector *c = (grpc_fake_channel_security_connector *) sc;
- if (c->call_host_check_is_async)
- {
- cb (exec_ctx, user_data, GRPC_SECURITY_OK);
- return GRPC_SECURITY_PENDING;
- }
- else
- {
- return GRPC_SECURITY_OK;
- }
+static grpc_security_status fake_channel_check_call_host(
+ grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc,
+ const char *host, grpc_security_check_cb cb, void *user_data) {
+ grpc_fake_channel_security_connector *c =
+ (grpc_fake_channel_security_connector *)sc;
+ if (c->call_host_check_is_async) {
+ cb(exec_ctx, user_data, GRPC_SECURITY_OK);
+ return GRPC_SECURITY_PENDING;
+ } else {
+ return GRPC_SECURITY_OK;
+ }
}
-static void
-fake_channel_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- grpc_do_security_handshake (exec_ctx, tsi_create_fake_handshaker (1), sc, nonsecure_endpoint, cb, user_data);
+static void fake_channel_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ grpc_do_security_handshake(exec_ctx, tsi_create_fake_handshaker(1), sc,
+ nonsecure_endpoint, cb, user_data);
}
-static void
-fake_server_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- grpc_do_security_handshake (exec_ctx, tsi_create_fake_handshaker (0), sc, nonsecure_endpoint, cb, user_data);
+static void fake_server_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ grpc_do_security_handshake(exec_ctx, tsi_create_fake_handshaker(0), sc,
+ nonsecure_endpoint, cb, user_data);
}
static grpc_security_connector_vtable fake_channel_vtable = {
- fake_channel_destroy, fake_channel_do_handshake, fake_check_peer
-};
+ fake_channel_destroy, fake_channel_do_handshake, fake_check_peer};
static grpc_security_connector_vtable fake_server_vtable = {
- fake_server_destroy, fake_server_do_handshake, fake_check_peer
-};
-
-grpc_channel_security_connector *
-grpc_fake_channel_security_connector_create (grpc_credentials * request_metadata_creds, int call_host_check_is_async)
-{
- grpc_fake_channel_security_connector *c = gpr_malloc (sizeof (grpc_fake_channel_security_connector));
- memset (c, 0, sizeof (grpc_fake_channel_security_connector));
- gpr_ref_init (&c->base.base.refcount, 1);
+ fake_server_destroy, fake_server_do_handshake, fake_check_peer};
+
+grpc_channel_security_connector *grpc_fake_channel_security_connector_create(
+ grpc_credentials *request_metadata_creds, int call_host_check_is_async) {
+ grpc_fake_channel_security_connector *c =
+ gpr_malloc(sizeof(grpc_fake_channel_security_connector));
+ memset(c, 0, sizeof(grpc_fake_channel_security_connector));
+ gpr_ref_init(&c->base.base.refcount, 1);
c->base.base.is_client_side = 1;
c->base.base.url_scheme = GRPC_FAKE_SECURITY_URL_SCHEME;
c->base.base.vtable = &fake_channel_vtable;
- GPR_ASSERT (check_request_metadata_creds (request_metadata_creds));
- c->base.request_metadata_creds = grpc_credentials_ref (request_metadata_creds);
+ GPR_ASSERT(check_request_metadata_creds(request_metadata_creds));
+ c->base.request_metadata_creds = grpc_credentials_ref(request_metadata_creds);
c->base.check_call_host = fake_channel_check_call_host;
c->call_host_check_is_async = call_host_check_is_async;
return &c->base;
}
-grpc_security_connector *
-grpc_fake_server_security_connector_create (void)
-{
- grpc_security_connector *c = gpr_malloc (sizeof (grpc_security_connector));
- memset (c, 0, sizeof (grpc_security_connector));
- gpr_ref_init (&c->refcount, 1);
+grpc_security_connector *grpc_fake_server_security_connector_create(void) {
+ grpc_security_connector *c = gpr_malloc(sizeof(grpc_security_connector));
+ memset(c, 0, sizeof(grpc_security_connector));
+ gpr_ref_init(&c->refcount, 1);
c->is_client_side = 0;
c->vtable = &fake_server_vtable;
c->url_scheme = GRPC_FAKE_SECURITY_URL_SCHEME;
@@ -362,8 +333,7 @@ grpc_fake_server_security_connector_create (void)
/* --- Ssl implementation. --- */
-typedef struct
-{
+typedef struct {
grpc_channel_security_connector base;
tsi_ssl_handshaker_factory *handshaker_factory;
char *target_name;
@@ -371,268 +341,253 @@ typedef struct
tsi_peer peer;
} grpc_ssl_channel_security_connector;
-typedef struct
-{
+typedef struct {
grpc_security_connector base;
tsi_ssl_handshaker_factory *handshaker_factory;
} grpc_ssl_server_security_connector;
-static void
-ssl_channel_destroy (grpc_security_connector * sc)
-{
- grpc_ssl_channel_security_connector *c = (grpc_ssl_channel_security_connector *) sc;
- grpc_credentials_unref (c->base.request_metadata_creds);
- if (c->handshaker_factory != NULL)
- {
- tsi_ssl_handshaker_factory_destroy (c->handshaker_factory);
- }
- if (c->target_name != NULL)
- gpr_free (c->target_name);
- if (c->overridden_target_name != NULL)
- gpr_free (c->overridden_target_name);
- tsi_peer_destruct (&c->peer);
- GRPC_AUTH_CONTEXT_UNREF (sc->auth_context, "connector");
- gpr_free (sc);
-}
-
-static void
-ssl_server_destroy (grpc_security_connector * sc)
-{
- grpc_ssl_server_security_connector *c = (grpc_ssl_server_security_connector *) sc;
- if (c->handshaker_factory != NULL)
- {
- tsi_ssl_handshaker_factory_destroy (c->handshaker_factory);
- }
- GRPC_AUTH_CONTEXT_UNREF (sc->auth_context, "connector");
- gpr_free (sc);
-}
-
-static grpc_security_status
-ssl_create_handshaker (tsi_ssl_handshaker_factory * handshaker_factory, int is_client, const char *peer_name, tsi_handshaker ** handshaker)
-{
+static void ssl_channel_destroy(grpc_security_connector *sc) {
+ grpc_ssl_channel_security_connector *c =
+ (grpc_ssl_channel_security_connector *)sc;
+ grpc_credentials_unref(c->base.request_metadata_creds);
+ if (c->handshaker_factory != NULL) {
+ tsi_ssl_handshaker_factory_destroy(c->handshaker_factory);
+ }
+ if (c->target_name != NULL) gpr_free(c->target_name);
+ if (c->overridden_target_name != NULL) gpr_free(c->overridden_target_name);
+ tsi_peer_destruct(&c->peer);
+ GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector");
+ gpr_free(sc);
+}
+
+static void ssl_server_destroy(grpc_security_connector *sc) {
+ grpc_ssl_server_security_connector *c =
+ (grpc_ssl_server_security_connector *)sc;
+ if (c->handshaker_factory != NULL) {
+ tsi_ssl_handshaker_factory_destroy(c->handshaker_factory);
+ }
+ GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector");
+ gpr_free(sc);
+}
+
+static grpc_security_status ssl_create_handshaker(
+ tsi_ssl_handshaker_factory *handshaker_factory, int is_client,
+ const char *peer_name, tsi_handshaker **handshaker) {
tsi_result result = TSI_OK;
- if (handshaker_factory == NULL)
+ if (handshaker_factory == NULL) return GRPC_SECURITY_ERROR;
+ result = tsi_ssl_handshaker_factory_create_handshaker(
+ handshaker_factory, is_client ? peer_name : NULL, handshaker);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshaker creation failed with error %s.",
+ tsi_result_to_string(result));
return GRPC_SECURITY_ERROR;
- result = tsi_ssl_handshaker_factory_create_handshaker (handshaker_factory, is_client ? peer_name : NULL, handshaker);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshaker creation failed with error %s.", tsi_result_to_string (result));
- return GRPC_SECURITY_ERROR;
- }
+ }
return GRPC_SECURITY_OK;
}
-static void
-ssl_channel_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- grpc_ssl_channel_security_connector *c = (grpc_ssl_channel_security_connector *) sc;
+static void ssl_channel_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ grpc_ssl_channel_security_connector *c =
+ (grpc_ssl_channel_security_connector *)sc;
tsi_handshaker *handshaker;
- grpc_security_status status = ssl_create_handshaker (c->handshaker_factory, 1,
- c->overridden_target_name != NULL ? c->overridden_target_name : c->target_name,
- &handshaker);
- if (status != GRPC_SECURITY_OK)
- {
- cb (exec_ctx, user_data, status, nonsecure_endpoint, NULL);
- }
- else
- {
- grpc_do_security_handshake (exec_ctx, handshaker, sc, nonsecure_endpoint, cb, user_data);
- }
-}
-
-static void
-ssl_server_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data)
-{
- grpc_ssl_server_security_connector *c = (grpc_ssl_server_security_connector *) sc;
+ grpc_security_status status = ssl_create_handshaker(
+ c->handshaker_factory, 1,
+ c->overridden_target_name != NULL ? c->overridden_target_name
+ : c->target_name,
+ &handshaker);
+ if (status != GRPC_SECURITY_OK) {
+ cb(exec_ctx, user_data, status, nonsecure_endpoint, NULL);
+ } else {
+ grpc_do_security_handshake(exec_ctx, handshaker, sc, nonsecure_endpoint, cb,
+ user_data);
+ }
+}
+
+static void ssl_server_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data) {
+ grpc_ssl_server_security_connector *c =
+ (grpc_ssl_server_security_connector *)sc;
tsi_handshaker *handshaker;
- grpc_security_status status = ssl_create_handshaker (c->handshaker_factory, 0, NULL, &handshaker);
- if (status != GRPC_SECURITY_OK)
- {
- cb (exec_ctx, user_data, status, nonsecure_endpoint, NULL);
- }
- else
- {
- grpc_do_security_handshake (exec_ctx, handshaker, sc, nonsecure_endpoint, cb, user_data);
- }
+ grpc_security_status status =
+ ssl_create_handshaker(c->handshaker_factory, 0, NULL, &handshaker);
+ if (status != GRPC_SECURITY_OK) {
+ cb(exec_ctx, user_data, status, nonsecure_endpoint, NULL);
+ } else {
+ grpc_do_security_handshake(exec_ctx, handshaker, sc, nonsecure_endpoint, cb,
+ user_data);
+ }
}
-static int
-ssl_host_matches_name (const tsi_peer * peer, const char *peer_name)
-{
+static int ssl_host_matches_name(const tsi_peer *peer, const char *peer_name) {
char *allocated_name = NULL;
int r;
- if (strchr (peer_name, ':') != NULL)
- {
- char *ignored_port;
- gpr_split_host_port (peer_name, &allocated_name, &ignored_port);
- gpr_free (ignored_port);
- peer_name = allocated_name;
- if (!peer_name)
- return 0;
- }
- r = tsi_ssl_peer_matches_name (peer, peer_name);
- gpr_free (allocated_name);
+ if (strchr(peer_name, ':') != NULL) {
+ char *ignored_port;
+ gpr_split_host_port(peer_name, &allocated_name, &ignored_port);
+ gpr_free(ignored_port);
+ peer_name = allocated_name;
+ if (!peer_name) return 0;
+ }
+ r = tsi_ssl_peer_matches_name(peer, peer_name);
+ gpr_free(allocated_name);
return r;
}
-grpc_auth_context *
-tsi_ssl_peer_to_auth_context (const tsi_peer * peer)
-{
+grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer) {
size_t i;
grpc_auth_context *ctx = NULL;
const char *peer_identity_property_name = NULL;
/* The caller has checked the certificate type property. */
- GPR_ASSERT (peer->property_count >= 1);
- ctx = grpc_auth_context_create (NULL);
- grpc_auth_context_add_cstring_property (ctx, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME, GRPC_SSL_TRANSPORT_SECURITY_TYPE);
- for (i = 0; i < peer->property_count; i++)
- {
- const tsi_peer_property *prop = &peer->properties[i];
- if (prop->name == NULL)
- continue;
- if (strcmp (prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0)
- {
- /* If there is no subject alt name, have the CN as the identity. */
- if (peer_identity_property_name == NULL)
- {
- peer_identity_property_name = GRPC_X509_CN_PROPERTY_NAME;
- }
- grpc_auth_context_add_property (ctx, GRPC_X509_CN_PROPERTY_NAME, prop->value.data, prop->value.length);
- }
- else if (strcmp (prop->name, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0)
- {
- peer_identity_property_name = GRPC_X509_SAN_PROPERTY_NAME;
- grpc_auth_context_add_property (ctx, GRPC_X509_SAN_PROPERTY_NAME, prop->value.data, prop->value.length);
- }
- }
- if (peer_identity_property_name != NULL)
- {
- GPR_ASSERT (grpc_auth_context_set_peer_identity_property_name (ctx, peer_identity_property_name) == 1);
- }
+ GPR_ASSERT(peer->property_count >= 1);
+ ctx = grpc_auth_context_create(NULL);
+ grpc_auth_context_add_cstring_property(
+ ctx, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME,
+ GRPC_SSL_TRANSPORT_SECURITY_TYPE);
+ for (i = 0; i < peer->property_count; i++) {
+ const tsi_peer_property *prop = &peer->properties[i];
+ if (prop->name == NULL) continue;
+ if (strcmp(prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) {
+ /* If there is no subject alt name, have the CN as the identity. */
+ if (peer_identity_property_name == NULL) {
+ peer_identity_property_name = GRPC_X509_CN_PROPERTY_NAME;
+ }
+ grpc_auth_context_add_property(ctx, GRPC_X509_CN_PROPERTY_NAME,
+ prop->value.data, prop->value.length);
+ } else if (strcmp(prop->name,
+ TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) {
+ peer_identity_property_name = GRPC_X509_SAN_PROPERTY_NAME;
+ grpc_auth_context_add_property(ctx, GRPC_X509_SAN_PROPERTY_NAME,
+ prop->value.data, prop->value.length);
+ }
+ }
+ if (peer_identity_property_name != NULL) {
+ GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(
+ ctx, peer_identity_property_name) == 1);
+ }
return ctx;
}
-static grpc_security_status
-ssl_check_peer (grpc_security_connector * sc, const char *peer_name, const tsi_peer * peer)
-{
+static grpc_security_status ssl_check_peer(grpc_security_connector *sc,
+ const char *peer_name,
+ const tsi_peer *peer) {
/* Check the ALPN. */
- const tsi_peer_property *p = tsi_peer_get_property_by_name (peer, TSI_SSL_ALPN_SELECTED_PROTOCOL);
- if (p == NULL)
- {
- gpr_log (GPR_ERROR, "Missing selected ALPN property.");
- return GRPC_SECURITY_ERROR;
- }
- if (!grpc_chttp2_is_alpn_version_supported (p->value.data, p->value.length))
- {
- gpr_log (GPR_ERROR, "Invalid ALPN value.");
- return GRPC_SECURITY_ERROR;
- }
+ const tsi_peer_property *p =
+ tsi_peer_get_property_by_name(peer, TSI_SSL_ALPN_SELECTED_PROTOCOL);
+ if (p == NULL) {
+ gpr_log(GPR_ERROR, "Missing selected ALPN property.");
+ return GRPC_SECURITY_ERROR;
+ }
+ if (!grpc_chttp2_is_alpn_version_supported(p->value.data, p->value.length)) {
+ gpr_log(GPR_ERROR, "Invalid ALPN value.");
+ return GRPC_SECURITY_ERROR;
+ }
/* Check the peer name if specified. */
- if (peer_name != NULL && !ssl_host_matches_name (peer, peer_name))
- {
- gpr_log (GPR_ERROR, "Peer name %s is not in peer certificate", peer_name);
- return GRPC_SECURITY_ERROR;
- }
- if (sc->auth_context != NULL)
- {
- GRPC_AUTH_CONTEXT_UNREF (sc->auth_context, "connector");
- }
- sc->auth_context = tsi_ssl_peer_to_auth_context (peer);
+ if (peer_name != NULL && !ssl_host_matches_name(peer, peer_name)) {
+ gpr_log(GPR_ERROR, "Peer name %s is not in peer certificate", peer_name);
+ return GRPC_SECURITY_ERROR;
+ }
+ if (sc->auth_context != NULL) {
+ GRPC_AUTH_CONTEXT_UNREF(sc->auth_context, "connector");
+ }
+ sc->auth_context = tsi_ssl_peer_to_auth_context(peer);
return GRPC_SECURITY_OK;
}
-static grpc_security_status
-ssl_channel_check_peer (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data)
-{
- grpc_ssl_channel_security_connector *c = (grpc_ssl_channel_security_connector *) sc;
+static grpc_security_status ssl_channel_check_peer(grpc_security_connector *sc,
+ tsi_peer peer,
+ grpc_security_check_cb cb,
+ void *user_data) {
+ grpc_ssl_channel_security_connector *c =
+ (grpc_ssl_channel_security_connector *)sc;
grpc_security_status status;
- tsi_peer_destruct (&c->peer);
+ tsi_peer_destruct(&c->peer);
c->peer = peer;
- status = ssl_check_peer (sc, c->overridden_target_name != NULL ? c->overridden_target_name : c->target_name, &peer);
+ status = ssl_check_peer(sc, c->overridden_target_name != NULL
+ ? c->overridden_target_name
+ : c->target_name,
+ &peer);
return status;
}
-static grpc_security_status
-ssl_server_check_peer (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data)
-{
- grpc_security_status status = ssl_check_peer (sc, NULL, &peer);
- tsi_peer_destruct (&peer);
+static grpc_security_status ssl_server_check_peer(grpc_security_connector *sc,
+ tsi_peer peer,
+ grpc_security_check_cb cb,
+ void *user_data) {
+ grpc_security_status status = ssl_check_peer(sc, NULL, &peer);
+ tsi_peer_destruct(&peer);
return status;
}
-static grpc_security_status
-ssl_channel_check_call_host (grpc_exec_ctx * exec_ctx, grpc_channel_security_connector * sc, const char *host, grpc_security_check_cb cb, void *user_data)
-{
- grpc_ssl_channel_security_connector *c = (grpc_ssl_channel_security_connector *) sc;
+static grpc_security_status ssl_channel_check_call_host(
+ grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc,
+ const char *host, grpc_security_check_cb cb, void *user_data) {
+ grpc_ssl_channel_security_connector *c =
+ (grpc_ssl_channel_security_connector *)sc;
- if (ssl_host_matches_name (&c->peer, host))
- return GRPC_SECURITY_OK;
+ if (ssl_host_matches_name(&c->peer, host)) return GRPC_SECURITY_OK;
/* If the target name was overridden, then the original target_name was
'checked' transitively during the previous peer check at the end of the
handshake. */
- if (c->overridden_target_name != NULL && strcmp (host, c->target_name) == 0)
- {
- return GRPC_SECURITY_OK;
- }
- else
- {
- return GRPC_SECURITY_ERROR;
- }
+ if (c->overridden_target_name != NULL && strcmp(host, c->target_name) == 0) {
+ return GRPC_SECURITY_OK;
+ } else {
+ return GRPC_SECURITY_ERROR;
+ }
}
static grpc_security_connector_vtable ssl_channel_vtable = {
- ssl_channel_destroy, ssl_channel_do_handshake, ssl_channel_check_peer
-};
+ ssl_channel_destroy, ssl_channel_do_handshake, ssl_channel_check_peer};
static grpc_security_connector_vtable ssl_server_vtable = {
- ssl_server_destroy, ssl_server_do_handshake, ssl_server_check_peer
-};
+ ssl_server_destroy, ssl_server_do_handshake, ssl_server_check_peer};
static gpr_slice default_pem_root_certs;
-static void
-init_default_pem_root_certs (void)
-{
+static void init_default_pem_root_certs(void) {
/* First try to load the roots from the environment. */
- char *default_root_certs_path = gpr_getenv (GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR);
- if (default_root_certs_path == NULL)
- {
- default_pem_root_certs = gpr_empty_slice ();
- }
- else
- {
- default_pem_root_certs = gpr_load_file (default_root_certs_path, 0, NULL);
- gpr_free (default_root_certs_path);
- }
+ char *default_root_certs_path =
+ gpr_getenv(GRPC_DEFAULT_SSL_ROOTS_FILE_PATH_ENV_VAR);
+ if (default_root_certs_path == NULL) {
+ default_pem_root_certs = gpr_empty_slice();
+ } else {
+ default_pem_root_certs = gpr_load_file(default_root_certs_path, 0, NULL);
+ gpr_free(default_root_certs_path);
+ }
/* Fall back to installed certs if needed. */
- if (GPR_SLICE_IS_EMPTY (default_pem_root_certs))
- {
- default_pem_root_certs = gpr_load_file (installed_roots_path, 0, NULL);
- }
+ if (GPR_SLICE_IS_EMPTY(default_pem_root_certs)) {
+ default_pem_root_certs = gpr_load_file(installed_roots_path, 0, NULL);
+ }
}
-size_t
-grpc_get_default_ssl_roots (const unsigned char **pem_root_certs)
-{
+size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs) {
/* TODO(jboeuf@google.com): Maybe revisit the approach which consists in
loading all the roots once for the lifetime of the process. */
static gpr_once once = GPR_ONCE_INIT;
- gpr_once_init (&once, init_default_pem_root_certs);
- *pem_root_certs = GPR_SLICE_START_PTR (default_pem_root_certs);
- return GPR_SLICE_LENGTH (default_pem_root_certs);
-}
-
-grpc_security_status
-grpc_ssl_channel_security_connector_create (grpc_credentials * request_metadata_creds, const grpc_ssl_config * config, const char *target_name, const char *overridden_target_name, grpc_channel_security_connector ** sc)
-{
- size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions ();
- const unsigned char **alpn_protocol_strings = gpr_malloc (sizeof (const char *) * num_alpn_protocols);
- unsigned char *alpn_protocol_string_lengths = gpr_malloc (sizeof (unsigned char) * num_alpn_protocols);
+ gpr_once_init(&once, init_default_pem_root_certs);
+ *pem_root_certs = GPR_SLICE_START_PTR(default_pem_root_certs);
+ return GPR_SLICE_LENGTH(default_pem_root_certs);
+}
+
+grpc_security_status grpc_ssl_channel_security_connector_create(
+ grpc_credentials *request_metadata_creds, const grpc_ssl_config *config,
+ const char *target_name, const char *overridden_target_name,
+ grpc_channel_security_connector **sc) {
+ size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions();
+ const unsigned char **alpn_protocol_strings =
+ gpr_malloc(sizeof(const char *) * num_alpn_protocols);
+ unsigned char *alpn_protocol_string_lengths =
+ gpr_malloc(sizeof(unsigned char) * num_alpn_protocols);
tsi_result result = TSI_OK;
grpc_ssl_channel_security_connector *c;
size_t i;
@@ -640,112 +595,120 @@ grpc_ssl_channel_security_connector_create (grpc_credentials * request_metadata_
size_t pem_root_certs_size;
char *port;
- for (i = 0; i < num_alpn_protocols; i++)
- {
- alpn_protocol_strings[i] = (const unsigned char *) grpc_chttp2_get_alpn_version_index (i);
- alpn_protocol_string_lengths[i] = (unsigned char) strlen (grpc_chttp2_get_alpn_version_index (i));
- }
-
- if (config == NULL || target_name == NULL)
- {
- gpr_log (GPR_ERROR, "An ssl channel needs a config and a target name.");
- goto error;
- }
- if (!check_request_metadata_creds (request_metadata_creds))
- {
+ for (i = 0; i < num_alpn_protocols; i++) {
+ alpn_protocol_strings[i] =
+ (const unsigned char *)grpc_chttp2_get_alpn_version_index(i);
+ alpn_protocol_string_lengths[i] =
+ (unsigned char)strlen(grpc_chttp2_get_alpn_version_index(i));
+ }
+
+ if (config == NULL || target_name == NULL) {
+ gpr_log(GPR_ERROR, "An ssl channel needs a config and a target name.");
+ goto error;
+ }
+ if (!check_request_metadata_creds(request_metadata_creds)) {
+ goto error;
+ }
+ if (config->pem_root_certs == NULL) {
+ pem_root_certs_size = grpc_get_default_ssl_roots(&pem_root_certs);
+ if (pem_root_certs == NULL || pem_root_certs_size == 0) {
+ gpr_log(GPR_ERROR, "Could not get default pem root certs.");
goto error;
}
- if (config->pem_root_certs == NULL)
- {
- pem_root_certs_size = grpc_get_default_ssl_roots (&pem_root_certs);
- if (pem_root_certs == NULL || pem_root_certs_size == 0)
- {
- gpr_log (GPR_ERROR, "Could not get default pem root certs.");
- goto error;
- }
- }
- else
- {
- pem_root_certs = config->pem_root_certs;
- pem_root_certs_size = config->pem_root_certs_size;
- }
+ } else {
+ pem_root_certs = config->pem_root_certs;
+ pem_root_certs_size = config->pem_root_certs_size;
+ }
- c = gpr_malloc (sizeof (grpc_ssl_channel_security_connector));
- memset (c, 0, sizeof (grpc_ssl_channel_security_connector));
+ c = gpr_malloc(sizeof(grpc_ssl_channel_security_connector));
+ memset(c, 0, sizeof(grpc_ssl_channel_security_connector));
- gpr_ref_init (&c->base.base.refcount, 1);
+ gpr_ref_init(&c->base.base.refcount, 1);
c->base.base.vtable = &ssl_channel_vtable;
c->base.base.is_client_side = 1;
c->base.base.url_scheme = GRPC_SSL_URL_SCHEME;
- c->base.request_metadata_creds = grpc_credentials_ref (request_metadata_creds);
+ c->base.request_metadata_creds = grpc_credentials_ref(request_metadata_creds);
c->base.check_call_host = ssl_channel_check_call_host;
- gpr_split_host_port (target_name, &c->target_name, &port);
- gpr_free (port);
- if (overridden_target_name != NULL)
- {
- c->overridden_target_name = gpr_strdup (overridden_target_name);
- }
- result = tsi_create_ssl_client_handshaker_factory (config->pem_private_key, config->pem_private_key_size, config->pem_cert_chain, config->pem_cert_chain_size, pem_root_certs, pem_root_certs_size, ssl_cipher_suites (), alpn_protocol_strings, alpn_protocol_string_lengths, (uint16_t) num_alpn_protocols, &c->handshaker_factory);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string (result));
- ssl_channel_destroy (&c->base.base);
- *sc = NULL;
- goto error;
- }
+ gpr_split_host_port(target_name, &c->target_name, &port);
+ gpr_free(port);
+ if (overridden_target_name != NULL) {
+ c->overridden_target_name = gpr_strdup(overridden_target_name);
+ }
+ result = tsi_create_ssl_client_handshaker_factory(
+ config->pem_private_key, config->pem_private_key_size,
+ config->pem_cert_chain, config->pem_cert_chain_size, pem_root_certs,
+ pem_root_certs_size, ssl_cipher_suites(), alpn_protocol_strings,
+ alpn_protocol_string_lengths, (uint16_t)num_alpn_protocols,
+ &c->handshaker_factory);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
+ tsi_result_to_string(result));
+ ssl_channel_destroy(&c->base.base);
+ *sc = NULL;
+ goto error;
+ }
*sc = &c->base;
- gpr_free (alpn_protocol_strings);
- gpr_free (alpn_protocol_string_lengths);
+ gpr_free(alpn_protocol_strings);
+ gpr_free(alpn_protocol_string_lengths);
return GRPC_SECURITY_OK;
error:
- gpr_free (alpn_protocol_strings);
- gpr_free (alpn_protocol_string_lengths);
+ gpr_free(alpn_protocol_strings);
+ gpr_free(alpn_protocol_string_lengths);
return GRPC_SECURITY_ERROR;
}
-grpc_security_status
-grpc_ssl_server_security_connector_create (const grpc_ssl_server_config * config, grpc_security_connector ** sc)
-{
- size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions ();
- const unsigned char **alpn_protocol_strings = gpr_malloc (sizeof (const char *) * num_alpn_protocols);
- unsigned char *alpn_protocol_string_lengths = gpr_malloc (sizeof (unsigned char) * num_alpn_protocols);
+grpc_security_status grpc_ssl_server_security_connector_create(
+ const grpc_ssl_server_config *config, grpc_security_connector **sc) {
+ size_t num_alpn_protocols = grpc_chttp2_num_alpn_versions();
+ const unsigned char **alpn_protocol_strings =
+ gpr_malloc(sizeof(const char *) * num_alpn_protocols);
+ unsigned char *alpn_protocol_string_lengths =
+ gpr_malloc(sizeof(unsigned char) * num_alpn_protocols);
tsi_result result = TSI_OK;
grpc_ssl_server_security_connector *c;
size_t i;
- for (i = 0; i < num_alpn_protocols; i++)
- {
- alpn_protocol_strings[i] = (const unsigned char *) grpc_chttp2_get_alpn_version_index (i);
- alpn_protocol_string_lengths[i] = (unsigned char) strlen (grpc_chttp2_get_alpn_version_index (i));
- }
-
- if (config == NULL || config->num_key_cert_pairs == 0)
- {
- gpr_log (GPR_ERROR, "An SSL server needs a key and a cert.");
- goto error;
- }
- c = gpr_malloc (sizeof (grpc_ssl_server_security_connector));
- memset (c, 0, sizeof (grpc_ssl_server_security_connector));
-
- gpr_ref_init (&c->base.refcount, 1);
+ for (i = 0; i < num_alpn_protocols; i++) {
+ alpn_protocol_strings[i] =
+ (const unsigned char *)grpc_chttp2_get_alpn_version_index(i);
+ alpn_protocol_string_lengths[i] =
+ (unsigned char)strlen(grpc_chttp2_get_alpn_version_index(i));
+ }
+
+ if (config == NULL || config->num_key_cert_pairs == 0) {
+ gpr_log(GPR_ERROR, "An SSL server needs a key and a cert.");
+ goto error;
+ }
+ c = gpr_malloc(sizeof(grpc_ssl_server_security_connector));
+ memset(c, 0, sizeof(grpc_ssl_server_security_connector));
+
+ gpr_ref_init(&c->base.refcount, 1);
c->base.url_scheme = GRPC_SSL_URL_SCHEME;
c->base.vtable = &ssl_server_vtable;
- result = tsi_create_ssl_server_handshaker_factory ((const unsigned char **) config->pem_private_keys, config->pem_private_keys_sizes, (const unsigned char **) config->pem_cert_chains, config->pem_cert_chains_sizes, config->num_key_cert_pairs, config->pem_root_certs, config->pem_root_certs_size, config->force_client_auth, ssl_cipher_suites (), alpn_protocol_strings, alpn_protocol_string_lengths, (uint16_t) num_alpn_protocols, &c->handshaker_factory);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Handshaker factory creation failed with %s.", tsi_result_to_string (result));
- ssl_server_destroy (&c->base);
- *sc = NULL;
- goto error;
- }
+ result = tsi_create_ssl_server_handshaker_factory(
+ (const unsigned char **)config->pem_private_keys,
+ config->pem_private_keys_sizes,
+ (const unsigned char **)config->pem_cert_chains,
+ config->pem_cert_chains_sizes, config->num_key_cert_pairs,
+ config->pem_root_certs, config->pem_root_certs_size,
+ config->force_client_auth, ssl_cipher_suites(), alpn_protocol_strings,
+ alpn_protocol_string_lengths, (uint16_t)num_alpn_protocols,
+ &c->handshaker_factory);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Handshaker factory creation failed with %s.",
+ tsi_result_to_string(result));
+ ssl_server_destroy(&c->base);
+ *sc = NULL;
+ goto error;
+ }
*sc = &c->base;
- gpr_free (alpn_protocol_strings);
- gpr_free (alpn_protocol_string_lengths);
+ gpr_free(alpn_protocol_strings);
+ gpr_free(alpn_protocol_string_lengths);
return GRPC_SECURITY_OK;
error:
- gpr_free (alpn_protocol_strings);
- gpr_free (alpn_protocol_string_lengths);
+ gpr_free(alpn_protocol_strings);
+ gpr_free(alpn_protocol_string_lengths);
return GRPC_SECURITY_ERROR;
}
diff --git a/src/core/security/security_connector.h b/src/core/security/security_connector.h
index 3af7f6f83a..9218a3caab 100644
--- a/src/core/security/security_connector.h
+++ b/src/core/security/security_connector.h
@@ -40,8 +40,7 @@
/* --- status enum. --- */
-typedef enum
-{
+typedef enum {
GRPC_SECURITY_OK = 0,
GRPC_SECURITY_PENDING,
GRPC_SECURITY_ERROR
@@ -61,25 +60,32 @@ typedef struct grpc_security_connector grpc_security_connector;
#define GRPC_SECURITY_CONNECTOR_ARG "grpc.security_connector"
-typedef void (*grpc_security_check_cb) (grpc_exec_ctx * exec_ctx, void *user_data, grpc_security_status status);
+typedef void (*grpc_security_check_cb)(grpc_exec_ctx *exec_ctx, void *user_data,
+ grpc_security_status status);
/* Ownership of the secure_endpoint is transfered. */
-typedef void (*grpc_security_handshake_done_cb) (grpc_exec_ctx * exec_ctx, void *user_data, grpc_security_status status, grpc_endpoint * wrapped_endpoint, grpc_endpoint * secure_endpoint);
-
-typedef struct
-{
- void (*destroy) (grpc_security_connector * sc);
- void (*do_handshake) (grpc_exec_ctx * exec_ctx, grpc_security_connector * sc, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data);
- grpc_security_status (*check_peer) (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data);
+typedef void (*grpc_security_handshake_done_cb)(grpc_exec_ctx *exec_ctx,
+ void *user_data,
+ grpc_security_status status,
+ grpc_endpoint *wrapped_endpoint,
+ grpc_endpoint *secure_endpoint);
+
+typedef struct {
+ void (*destroy)(grpc_security_connector *sc);
+ void (*do_handshake)(grpc_exec_ctx *exec_ctx, grpc_security_connector *sc,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb, void *user_data);
+ grpc_security_status (*check_peer)(grpc_security_connector *sc, tsi_peer peer,
+ grpc_security_check_cb cb,
+ void *user_data);
} grpc_security_connector_vtable;
-struct grpc_security_connector
-{
+struct grpc_security_connector {
const grpc_security_connector_vtable *vtable;
gpr_refcount refcount;
int is_client_side;
const char *url_scheme;
- grpc_auth_context *auth_context; /* Populated after the peer is checked. */
+ grpc_auth_context *auth_context; /* Populated after the peer is checked. */
};
/* Refcounting. */
@@ -88,17 +94,26 @@ struct grpc_security_connector
grpc_security_connector_ref((p), __FILE__, __LINE__, (r))
#define GRPC_SECURITY_CONNECTOR_UNREF(p, r) \
grpc_security_connector_unref((p), __FILE__, __LINE__, (r))
-grpc_security_connector *grpc_security_connector_ref (grpc_security_connector * policy, const char *file, int line, const char *reason);
-void grpc_security_connector_unref (grpc_security_connector * policy, const char *file, int line, const char *reason);
+grpc_security_connector *grpc_security_connector_ref(
+ grpc_security_connector *policy, const char *file, int line,
+ const char *reason);
+void grpc_security_connector_unref(grpc_security_connector *policy,
+ const char *file, int line,
+ const char *reason);
#else
#define GRPC_SECURITY_CONNECTOR_REF(p, r) grpc_security_connector_ref((p))
#define GRPC_SECURITY_CONNECTOR_UNREF(p, r) grpc_security_connector_unref((p))
-grpc_security_connector *grpc_security_connector_ref (grpc_security_connector * policy);
-void grpc_security_connector_unref (grpc_security_connector * policy);
+grpc_security_connector *grpc_security_connector_ref(
+ grpc_security_connector *policy);
+void grpc_security_connector_unref(grpc_security_connector *policy);
#endif
/* Handshake. */
-void grpc_security_connector_do_handshake (grpc_exec_ctx * exec_ctx, grpc_security_connector * connector, grpc_endpoint * nonsecure_endpoint, grpc_security_handshake_done_cb cb, void *user_data);
+void grpc_security_connector_do_handshake(grpc_exec_ctx *exec_ctx,
+ grpc_security_connector *connector,
+ grpc_endpoint *nonsecure_endpoint,
+ grpc_security_handshake_done_cb cb,
+ void *user_data);
/* Check the peer.
Implementations can choose to check the peer either synchronously or
@@ -107,16 +122,19 @@ void grpc_security_connector_do_handshake (grpc_exec_ctx * exec_ctx, grpc_securi
GRPC_SECURITY_PENDING unless an error is detected early on.
Ownership of the peer is transfered.
*/
-grpc_security_status grpc_security_connector_check_peer (grpc_security_connector * sc, tsi_peer peer, grpc_security_check_cb cb, void *user_data);
+grpc_security_status grpc_security_connector_check_peer(
+ grpc_security_connector *sc, tsi_peer peer, grpc_security_check_cb cb,
+ void *user_data);
/* Util to encapsulate the connector in a channel arg. */
-grpc_arg grpc_security_connector_to_arg (grpc_security_connector * sc);
+grpc_arg grpc_security_connector_to_arg(grpc_security_connector *sc);
/* Util to get the connector from a channel arg. */
-grpc_security_connector *grpc_security_connector_from_arg (const grpc_arg * arg);
+grpc_security_connector *grpc_security_connector_from_arg(const grpc_arg *arg);
/* Util to find the connector from channel args. */
-grpc_security_connector *grpc_find_security_connector_in_args (const grpc_channel_args * args);
+grpc_security_connector *grpc_find_security_connector_in_args(
+ const grpc_channel_args *args);
/* --- channel_security_connector object. ---
@@ -125,11 +143,14 @@ grpc_security_connector *grpc_find_security_connector_in_args (const grpc_channe
typedef struct grpc_channel_security_connector grpc_channel_security_connector;
-struct grpc_channel_security_connector
-{
- grpc_security_connector base; /* requires is_client_side to be non 0. */
+struct grpc_channel_security_connector {
+ grpc_security_connector base; /* requires is_client_side to be non 0. */
grpc_credentials *request_metadata_creds;
- grpc_security_status (*check_call_host) (grpc_exec_ctx * exec_ctx, grpc_channel_security_connector * sc, const char *host, grpc_security_check_cb cb, void *user_data);
+ grpc_security_status (*check_call_host)(grpc_exec_ctx *exec_ctx,
+ grpc_channel_security_connector *sc,
+ const char *host,
+ grpc_security_check_cb cb,
+ void *user_data);
};
/* Checks that the host that will be set for a call is acceptable.
@@ -137,21 +158,23 @@ struct grpc_channel_security_connector
asynchronously. In the first case, a successful call will return
GRPC_SECURITY_OK. In the asynchronous case, the call will return
GRPC_SECURITY_PENDING unless an error is detected early on. */
-grpc_security_status grpc_channel_security_connector_check_call_host (grpc_exec_ctx * exec_ctx, grpc_channel_security_connector * sc, const char *host, grpc_security_check_cb cb, void *user_data);
+grpc_security_status grpc_channel_security_connector_check_call_host(
+ grpc_exec_ctx *exec_ctx, grpc_channel_security_connector *sc,
+ const char *host, grpc_security_check_cb cb, void *user_data);
/* --- Creation security connectors. --- */
/* For TESTING ONLY!
Creates a fake connector that emulates real channel security. */
-grpc_channel_security_connector *grpc_fake_channel_security_connector_create (grpc_credentials * request_metadata_creds, int call_host_check_is_async);
+grpc_channel_security_connector *grpc_fake_channel_security_connector_create(
+ grpc_credentials *request_metadata_creds, int call_host_check_is_async);
/* For TESTING ONLY!
Creates a fake connector that emulates real server security. */
-grpc_security_connector *grpc_fake_server_security_connector_create (void);
+grpc_security_connector *grpc_fake_server_security_connector_create(void);
/* Config for ssl clients. */
-typedef struct
-{
+typedef struct {
unsigned char *pem_private_key;
size_t pem_private_key_size;
unsigned char *pem_cert_chain;
@@ -173,14 +196,16 @@ typedef struct
This function returns GRPC_SECURITY_OK in case of success or a
specific error code otherwise.
*/
-grpc_security_status grpc_ssl_channel_security_connector_create (grpc_credentials * request_metadata_creds, const grpc_ssl_config * config, const char *target_name, const char *overridden_target_name, grpc_channel_security_connector ** sc);
+grpc_security_status grpc_ssl_channel_security_connector_create(
+ grpc_credentials *request_metadata_creds, const grpc_ssl_config *config,
+ const char *target_name, const char *overridden_target_name,
+ grpc_channel_security_connector **sc);
/* Gets the default ssl roots. */
-size_t grpc_get_default_ssl_roots (const unsigned char **pem_root_certs);
+size_t grpc_get_default_ssl_roots(const unsigned char **pem_root_certs);
/* Config for ssl servers. */
-typedef struct
-{
+typedef struct {
unsigned char **pem_private_keys;
size_t *pem_private_keys_sizes;
unsigned char **pem_cert_chains;
@@ -197,12 +222,14 @@ typedef struct
This function returns GRPC_SECURITY_OK in case of success or a
specific error code otherwise.
*/
-grpc_security_status grpc_ssl_server_security_connector_create (const grpc_ssl_server_config * config, grpc_security_connector ** sc);
+grpc_security_status grpc_ssl_server_security_connector_create(
+ const grpc_ssl_server_config *config, grpc_security_connector **sc);
/* Util. */
-const tsi_peer_property *tsi_peer_get_property_by_name (const tsi_peer * peer, const char *name);
+const tsi_peer_property *tsi_peer_get_property_by_name(const tsi_peer *peer,
+ const char *name);
/* Exposed for testing only. */
-grpc_auth_context *tsi_ssl_peer_to_auth_context (const tsi_peer * peer);
+grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer);
#endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONNECTOR_H */
diff --git a/src/core/security/security_context.c b/src/core/security/security_context.c
index 5b3326d341..95d80ba122 100644
--- a/src/core/security/security_context.c
+++ b/src/core/security/security_context.c
@@ -44,317 +44,271 @@
/* --- grpc_call --- */
-grpc_call_error
-grpc_call_set_credentials (grpc_call * call, grpc_credentials * creds)
-{
+grpc_call_error grpc_call_set_credentials(grpc_call *call,
+ grpc_credentials *creds) {
grpc_client_security_context *ctx = NULL;
- if (!grpc_call_is_client (call))
- {
- gpr_log (GPR_ERROR, "Method is client-side only.");
- return GRPC_CALL_ERROR_NOT_ON_SERVER;
- }
- if (creds != NULL && !grpc_credentials_has_request_metadata_only (creds))
- {
- gpr_log (GPR_ERROR, "Incompatible credentials to set on a call.");
- return GRPC_CALL_ERROR;
- }
- ctx = (grpc_client_security_context *) grpc_call_context_get (call, GRPC_CONTEXT_SECURITY);
- if (ctx == NULL)
- {
- ctx = grpc_client_security_context_create ();
- ctx->creds = grpc_credentials_ref (creds);
- grpc_call_context_set (call, GRPC_CONTEXT_SECURITY, ctx, grpc_client_security_context_destroy);
- }
- else
- {
- grpc_credentials_unref (ctx->creds);
- ctx->creds = grpc_credentials_ref (creds);
- }
+ if (!grpc_call_is_client(call)) {
+ gpr_log(GPR_ERROR, "Method is client-side only.");
+ return GRPC_CALL_ERROR_NOT_ON_SERVER;
+ }
+ if (creds != NULL && !grpc_credentials_has_request_metadata_only(creds)) {
+ gpr_log(GPR_ERROR, "Incompatible credentials to set on a call.");
+ return GRPC_CALL_ERROR;
+ }
+ ctx = (grpc_client_security_context *)grpc_call_context_get(
+ call, GRPC_CONTEXT_SECURITY);
+ if (ctx == NULL) {
+ ctx = grpc_client_security_context_create();
+ ctx->creds = grpc_credentials_ref(creds);
+ grpc_call_context_set(call, GRPC_CONTEXT_SECURITY, ctx,
+ grpc_client_security_context_destroy);
+ } else {
+ grpc_credentials_unref(ctx->creds);
+ ctx->creds = grpc_credentials_ref(creds);
+ }
return GRPC_CALL_OK;
}
-grpc_auth_context *
-grpc_call_auth_context (grpc_call * call)
-{
- void *sec_ctx = grpc_call_context_get (call, GRPC_CONTEXT_SECURITY);
- if (sec_ctx == NULL)
- return NULL;
- return grpc_call_is_client (call) ? GRPC_AUTH_CONTEXT_REF (((grpc_client_security_context *) sec_ctx)->auth_context, "grpc_call_auth_context client") : GRPC_AUTH_CONTEXT_REF (((grpc_server_security_context *) sec_ctx)->auth_context, "grpc_call_auth_context server");
+grpc_auth_context *grpc_call_auth_context(grpc_call *call) {
+ void *sec_ctx = grpc_call_context_get(call, GRPC_CONTEXT_SECURITY);
+ if (sec_ctx == NULL) return NULL;
+ return grpc_call_is_client(call)
+ ? GRPC_AUTH_CONTEXT_REF(
+ ((grpc_client_security_context *)sec_ctx)->auth_context,
+ "grpc_call_auth_context client")
+ : GRPC_AUTH_CONTEXT_REF(
+ ((grpc_server_security_context *)sec_ctx)->auth_context,
+ "grpc_call_auth_context server");
}
-void
-grpc_auth_context_release (grpc_auth_context * context)
-{
- GRPC_AUTH_CONTEXT_UNREF (context, "grpc_auth_context_unref");
+void grpc_auth_context_release(grpc_auth_context *context) {
+ GRPC_AUTH_CONTEXT_UNREF(context, "grpc_auth_context_unref");
}
/* --- grpc_client_security_context --- */
-grpc_client_security_context *
-grpc_client_security_context_create (void)
-{
- grpc_client_security_context *ctx = gpr_malloc (sizeof (grpc_client_security_context));
- memset (ctx, 0, sizeof (grpc_client_security_context));
+grpc_client_security_context *grpc_client_security_context_create(void) {
+ grpc_client_security_context *ctx =
+ gpr_malloc(sizeof(grpc_client_security_context));
+ memset(ctx, 0, sizeof(grpc_client_security_context));
return ctx;
}
-void
-grpc_client_security_context_destroy (void *ctx)
-{
- grpc_client_security_context *c = (grpc_client_security_context *) ctx;
- grpc_credentials_unref (c->creds);
- GRPC_AUTH_CONTEXT_UNREF (c->auth_context, "client_security_context");
- gpr_free (ctx);
+void grpc_client_security_context_destroy(void *ctx) {
+ grpc_client_security_context *c = (grpc_client_security_context *)ctx;
+ grpc_credentials_unref(c->creds);
+ GRPC_AUTH_CONTEXT_UNREF(c->auth_context, "client_security_context");
+ gpr_free(ctx);
}
/* --- grpc_server_security_context --- */
-grpc_server_security_context *
-grpc_server_security_context_create (void)
-{
- grpc_server_security_context *ctx = gpr_malloc (sizeof (grpc_server_security_context));
- memset (ctx, 0, sizeof (grpc_server_security_context));
+grpc_server_security_context *grpc_server_security_context_create(void) {
+ grpc_server_security_context *ctx =
+ gpr_malloc(sizeof(grpc_server_security_context));
+ memset(ctx, 0, sizeof(grpc_server_security_context));
return ctx;
}
-void
-grpc_server_security_context_destroy (void *ctx)
-{
- grpc_server_security_context *c = (grpc_server_security_context *) ctx;
- GRPC_AUTH_CONTEXT_UNREF (c->auth_context, "server_security_context");
- gpr_free (ctx);
+void grpc_server_security_context_destroy(void *ctx) {
+ grpc_server_security_context *c = (grpc_server_security_context *)ctx;
+ GRPC_AUTH_CONTEXT_UNREF(c->auth_context, "server_security_context");
+ gpr_free(ctx);
}
/* --- grpc_auth_context --- */
-static grpc_auth_property_iterator empty_iterator = { NULL, 0, NULL };
-
-grpc_auth_context *
-grpc_auth_context_create (grpc_auth_context * chained)
-{
- grpc_auth_context *ctx = gpr_malloc (sizeof (grpc_auth_context));
- memset (ctx, 0, sizeof (grpc_auth_context));
- gpr_ref_init (&ctx->refcount, 1);
- if (chained != NULL)
- {
- ctx->chained = GRPC_AUTH_CONTEXT_REF (chained, "chained");
- ctx->peer_identity_property_name = ctx->chained->peer_identity_property_name;
- }
+static grpc_auth_property_iterator empty_iterator = {NULL, 0, NULL};
+
+grpc_auth_context *grpc_auth_context_create(grpc_auth_context *chained) {
+ grpc_auth_context *ctx = gpr_malloc(sizeof(grpc_auth_context));
+ memset(ctx, 0, sizeof(grpc_auth_context));
+ gpr_ref_init(&ctx->refcount, 1);
+ if (chained != NULL) {
+ ctx->chained = GRPC_AUTH_CONTEXT_REF(chained, "chained");
+ ctx->peer_identity_property_name =
+ ctx->chained->peer_identity_property_name;
+ }
return ctx;
}
#ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG
-grpc_auth_context *
-grpc_auth_context_ref (grpc_auth_context * ctx, const char *file, int line, const char *reason)
-{
- if (ctx == NULL)
- return NULL;
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "AUTH_CONTEXT:%p ref %d -> %d %s", ctx, (int) ctx->refcount.count, (int) ctx->refcount.count + 1, reason);
+grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *ctx,
+ const char *file, int line,
+ const char *reason) {
+ if (ctx == NULL) return NULL;
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "AUTH_CONTEXT:%p ref %d -> %d %s", ctx, (int)ctx->refcount.count,
+ (int)ctx->refcount.count + 1, reason);
#else
-grpc_auth_context *
-grpc_auth_context_ref (grpc_auth_context * ctx)
-{
- if (ctx == NULL)
- return NULL;
+grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *ctx) {
+ if (ctx == NULL) return NULL;
#endif
- gpr_ref (&ctx->refcount);
+ gpr_ref(&ctx->refcount);
return ctx;
}
#ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG
-void
-grpc_auth_context_unref (grpc_auth_context * ctx, const char *file, int line, const char *reason)
-{
- if (ctx == NULL)
- return;
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "AUTH_CONTEXT:%p unref %d -> %d %s", ctx, (int) ctx->refcount.count, (int) ctx->refcount.count - 1, reason);
+void grpc_auth_context_unref(grpc_auth_context *ctx, const char *file, int line,
+ const char *reason) {
+ if (ctx == NULL) return;
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "AUTH_CONTEXT:%p unref %d -> %d %s", ctx, (int)ctx->refcount.count,
+ (int)ctx->refcount.count - 1, reason);
#else
-void
-grpc_auth_context_unref (grpc_auth_context * ctx)
-{
- if (ctx == NULL)
- return;
+void grpc_auth_context_unref(grpc_auth_context *ctx) {
+ if (ctx == NULL) return;
#endif
- if (gpr_unref (&ctx->refcount))
- {
- size_t i;
- GRPC_AUTH_CONTEXT_UNREF (ctx->chained, "chained");
- if (ctx->properties.array != NULL)
- {
- for (i = 0; i < ctx->properties.count; i++)
- {
- grpc_auth_property_reset (&ctx->properties.array[i]);
- }
- gpr_free (ctx->properties.array);
- }
- gpr_free (ctx);
+ if (gpr_unref(&ctx->refcount)) {
+ size_t i;
+ GRPC_AUTH_CONTEXT_UNREF(ctx->chained, "chained");
+ if (ctx->properties.array != NULL) {
+ for (i = 0; i < ctx->properties.count; i++) {
+ grpc_auth_property_reset(&ctx->properties.array[i]);
+ }
+ gpr_free(ctx->properties.array);
}
+ gpr_free(ctx);
+ }
}
-const char *
-grpc_auth_context_peer_identity_property_name (const grpc_auth_context * ctx)
-{
+const char *grpc_auth_context_peer_identity_property_name(
+ const grpc_auth_context *ctx) {
return ctx->peer_identity_property_name;
}
-int
-grpc_auth_context_set_peer_identity_property_name (grpc_auth_context * ctx, const char *name)
-{
- grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name (ctx, name);
- const grpc_auth_property *prop = grpc_auth_property_iterator_next (&it);
- if (prop == NULL)
- {
- gpr_log (GPR_ERROR, "Property name %s not found in auth context.", name != NULL ? name : "NULL");
- return 0;
- }
+int grpc_auth_context_set_peer_identity_property_name(grpc_auth_context *ctx,
+ const char *name) {
+ grpc_auth_property_iterator it =
+ grpc_auth_context_find_properties_by_name(ctx, name);
+ const grpc_auth_property *prop = grpc_auth_property_iterator_next(&it);
+ if (prop == NULL) {
+ gpr_log(GPR_ERROR, "Property name %s not found in auth context.",
+ name != NULL ? name : "NULL");
+ return 0;
+ }
ctx->peer_identity_property_name = prop->name;
return 1;
}
-int
-grpc_auth_context_peer_is_authenticated (const grpc_auth_context * ctx)
-{
+int grpc_auth_context_peer_is_authenticated(const grpc_auth_context *ctx) {
return ctx->peer_identity_property_name == NULL ? 0 : 1;
}
-grpc_auth_property_iterator
-grpc_auth_context_property_iterator (const grpc_auth_context * ctx)
-{
+grpc_auth_property_iterator grpc_auth_context_property_iterator(
+ const grpc_auth_context *ctx) {
grpc_auth_property_iterator it = empty_iterator;
- if (ctx == NULL)
- return it;
+ if (ctx == NULL) return it;
it.ctx = ctx;
return it;
}
-const grpc_auth_property *
-grpc_auth_property_iterator_next (grpc_auth_property_iterator * it)
-{
- if (it == NULL || it->ctx == NULL)
- return NULL;
- while (it->index == it->ctx->properties.count)
- {
- if (it->ctx->chained == NULL)
- return NULL;
- it->ctx = it->ctx->chained;
- it->index = 0;
- }
- if (it->name == NULL)
- {
- return &it->ctx->properties.array[it->index++];
- }
- else
- {
- while (it->index < it->ctx->properties.count)
- {
- const grpc_auth_property *prop = &it->ctx->properties.array[it->index++];
- GPR_ASSERT (prop->name != NULL);
- if (strcmp (it->name, prop->name) == 0)
- {
- return prop;
- }
- }
- /* We could not find the name, try another round. */
- return grpc_auth_property_iterator_next (it);
+const grpc_auth_property *grpc_auth_property_iterator_next(
+ grpc_auth_property_iterator *it) {
+ if (it == NULL || it->ctx == NULL) return NULL;
+ while (it->index == it->ctx->properties.count) {
+ if (it->ctx->chained == NULL) return NULL;
+ it->ctx = it->ctx->chained;
+ it->index = 0;
+ }
+ if (it->name == NULL) {
+ return &it->ctx->properties.array[it->index++];
+ } else {
+ while (it->index < it->ctx->properties.count) {
+ const grpc_auth_property *prop = &it->ctx->properties.array[it->index++];
+ GPR_ASSERT(prop->name != NULL);
+ if (strcmp(it->name, prop->name) == 0) {
+ return prop;
+ }
}
+ /* We could not find the name, try another round. */
+ return grpc_auth_property_iterator_next(it);
+ }
}
-grpc_auth_property_iterator
-grpc_auth_context_find_properties_by_name (const grpc_auth_context * ctx, const char *name)
-{
+grpc_auth_property_iterator grpc_auth_context_find_properties_by_name(
+ const grpc_auth_context *ctx, const char *name) {
grpc_auth_property_iterator it = empty_iterator;
- if (ctx == NULL || name == NULL)
- return empty_iterator;
+ if (ctx == NULL || name == NULL) return empty_iterator;
it.ctx = ctx;
it.name = name;
return it;
}
-grpc_auth_property_iterator
-grpc_auth_context_peer_identity (const grpc_auth_context * ctx)
-{
- if (ctx == NULL)
- return empty_iterator;
- return grpc_auth_context_find_properties_by_name (ctx, ctx->peer_identity_property_name);
+grpc_auth_property_iterator grpc_auth_context_peer_identity(
+ const grpc_auth_context *ctx) {
+ if (ctx == NULL) return empty_iterator;
+ return grpc_auth_context_find_properties_by_name(
+ ctx, ctx->peer_identity_property_name);
}
-static void
-ensure_auth_context_capacity (grpc_auth_context * ctx)
-{
- if (ctx->properties.count == ctx->properties.capacity)
- {
- ctx->properties.capacity = GPR_MAX (ctx->properties.capacity + 8, ctx->properties.capacity * 2);
- ctx->properties.array = gpr_realloc (ctx->properties.array, ctx->properties.capacity * sizeof (grpc_auth_property));
- }
+static void ensure_auth_context_capacity(grpc_auth_context *ctx) {
+ if (ctx->properties.count == ctx->properties.capacity) {
+ ctx->properties.capacity =
+ GPR_MAX(ctx->properties.capacity + 8, ctx->properties.capacity * 2);
+ ctx->properties.array =
+ gpr_realloc(ctx->properties.array,
+ ctx->properties.capacity * sizeof(grpc_auth_property));
+ }
}
-void
-grpc_auth_context_add_property (grpc_auth_context * ctx, const char *name, const char *value, size_t value_length)
-{
+void grpc_auth_context_add_property(grpc_auth_context *ctx, const char *name,
+ const char *value, size_t value_length) {
grpc_auth_property *prop;
- ensure_auth_context_capacity (ctx);
+ ensure_auth_context_capacity(ctx);
prop = &ctx->properties.array[ctx->properties.count++];
- prop->name = gpr_strdup (name);
- prop->value = gpr_malloc (value_length + 1);
- memcpy (prop->value, value, value_length);
+ prop->name = gpr_strdup(name);
+ prop->value = gpr_malloc(value_length + 1);
+ memcpy(prop->value, value, value_length);
prop->value[value_length] = '\0';
prop->value_length = value_length;
}
-void
-grpc_auth_context_add_cstring_property (grpc_auth_context * ctx, const char *name, const char *value)
-{
+void grpc_auth_context_add_cstring_property(grpc_auth_context *ctx,
+ const char *name,
+ const char *value) {
grpc_auth_property *prop;
- ensure_auth_context_capacity (ctx);
+ ensure_auth_context_capacity(ctx);
prop = &ctx->properties.array[ctx->properties.count++];
- prop->name = gpr_strdup (name);
- prop->value = gpr_strdup (value);
- prop->value_length = strlen (value);
+ prop->name = gpr_strdup(name);
+ prop->value = gpr_strdup(value);
+ prop->value_length = strlen(value);
}
-void
-grpc_auth_property_reset (grpc_auth_property * property)
-{
- gpr_free (property->name);
- gpr_free (property->value);
- memset (property, 0, sizeof (grpc_auth_property));
+void grpc_auth_property_reset(grpc_auth_property *property) {
+ gpr_free(property->name);
+ gpr_free(property->value);
+ memset(property, 0, sizeof(grpc_auth_property));
}
-grpc_arg
-grpc_auth_metadata_processor_to_arg (grpc_auth_metadata_processor * p)
-{
+grpc_arg grpc_auth_metadata_processor_to_arg(grpc_auth_metadata_processor *p) {
grpc_arg arg;
- memset (&arg, 0, sizeof (grpc_arg));
+ memset(&arg, 0, sizeof(grpc_arg));
arg.type = GRPC_ARG_POINTER;
arg.key = GRPC_AUTH_METADATA_PROCESSOR_ARG;
arg.value.pointer.p = p;
return arg;
}
-grpc_auth_metadata_processor *
-grpc_auth_metadata_processor_from_arg (const grpc_arg * arg)
-{
- if (strcmp (arg->key, GRPC_AUTH_METADATA_PROCESSOR_ARG) != 0)
+grpc_auth_metadata_processor *grpc_auth_metadata_processor_from_arg(
+ const grpc_arg *arg) {
+ if (strcmp(arg->key, GRPC_AUTH_METADATA_PROCESSOR_ARG) != 0) return NULL;
+ if (arg->type != GRPC_ARG_POINTER) {
+ gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
+ GRPC_AUTH_METADATA_PROCESSOR_ARG);
return NULL;
- if (arg->type != GRPC_ARG_POINTER)
- {
- gpr_log (GPR_ERROR, "Invalid type %d for arg %s", arg->type, GRPC_AUTH_METADATA_PROCESSOR_ARG);
- return NULL;
- }
+ }
return arg->value.pointer.p;
}
-grpc_auth_metadata_processor *
-grpc_find_auth_metadata_processor_in_args (const grpc_channel_args * args)
-{
+grpc_auth_metadata_processor *grpc_find_auth_metadata_processor_in_args(
+ const grpc_channel_args *args) {
size_t i;
- if (args == NULL)
- return NULL;
- for (i = 0; i < args->num_args; i++)
- {
- grpc_auth_metadata_processor *p = grpc_auth_metadata_processor_from_arg (&args->args[i]);
- if (p != NULL)
- return p;
- }
+ if (args == NULL) return NULL;
+ for (i = 0; i < args->num_args; i++) {
+ grpc_auth_metadata_processor *p =
+ grpc_auth_metadata_processor_from_arg(&args->args[i]);
+ if (p != NULL) return p;
+ }
return NULL;
}
diff --git a/src/core/security/security_context.h b/src/core/security/security_context.h
index 93b31f60d6..a9a0306410 100644
--- a/src/core/security/security_context.h
+++ b/src/core/security/security_context.h
@@ -43,15 +43,13 @@
/* Property names are always NULL terminated. */
-typedef struct
-{
+typedef struct {
grpc_auth_property *array;
size_t count;
size_t capacity;
} grpc_auth_property_array;
-struct grpc_auth_context
-{
+struct grpc_auth_context {
struct grpc_auth_context *chained;
grpc_auth_property_array properties;
gpr_refcount refcount;
@@ -60,7 +58,7 @@ struct grpc_auth_context
};
/* Creation. */
-grpc_auth_context *grpc_auth_context_create (grpc_auth_context * chained);
+grpc_auth_context *grpc_auth_context_create(grpc_auth_context *chained);
/* Refcounting. */
#ifdef GRPC_AUTH_CONTEXT_REFCOUNT_DEBUG
@@ -68,47 +66,50 @@ grpc_auth_context *grpc_auth_context_create (grpc_auth_context * chained);
grpc_auth_context_ref((p), __FILE__, __LINE__, (r))
#define GRPC_AUTH_CONTEXT_UNREF(p, r) \
grpc_auth_context_unref((p), __FILE__, __LINE__, (r))
-grpc_auth_context *grpc_auth_context_ref (grpc_auth_context * policy, const char *file, int line, const char *reason);
-void grpc_auth_context_unref (grpc_auth_context * policy, const char *file, int line, const char *reason);
+grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *policy,
+ const char *file, int line,
+ const char *reason);
+void grpc_auth_context_unref(grpc_auth_context *policy, const char *file,
+ int line, const char *reason);
#else
#define GRPC_AUTH_CONTEXT_REF(p, r) grpc_auth_context_ref((p))
#define GRPC_AUTH_CONTEXT_UNREF(p, r) grpc_auth_context_unref((p))
-grpc_auth_context *grpc_auth_context_ref (grpc_auth_context * policy);
-void grpc_auth_context_unref (grpc_auth_context * policy);
+grpc_auth_context *grpc_auth_context_ref(grpc_auth_context *policy);
+void grpc_auth_context_unref(grpc_auth_context *policy);
#endif
-void grpc_auth_property_reset (grpc_auth_property * property);
+void grpc_auth_property_reset(grpc_auth_property *property);
/* --- grpc_client_security_context ---
Internal client-side security context. */
-typedef struct
-{
+typedef struct {
grpc_credentials *creds;
grpc_auth_context *auth_context;
} grpc_client_security_context;
-grpc_client_security_context *grpc_client_security_context_create (void);
-void grpc_client_security_context_destroy (void *ctx);
+grpc_client_security_context *grpc_client_security_context_create(void);
+void grpc_client_security_context_destroy(void *ctx);
/* --- grpc_server_security_context ---
Internal server-side security context. */
-typedef struct
-{
+typedef struct {
grpc_auth_context *auth_context;
} grpc_server_security_context;
-grpc_server_security_context *grpc_server_security_context_create (void);
-void grpc_server_security_context_destroy (void *ctx);
+grpc_server_security_context *grpc_server_security_context_create(void);
+void grpc_server_security_context_destroy(void *ctx);
/* --- Auth metadata processing. --- */
#define GRPC_AUTH_METADATA_PROCESSOR_ARG "grpc.auth_metadata_processor"
-grpc_arg grpc_auth_metadata_processor_to_arg (grpc_auth_metadata_processor * p);
-grpc_auth_metadata_processor *grpc_auth_metadata_processor_from_arg (const grpc_arg * arg);
-grpc_auth_metadata_processor *grpc_find_auth_metadata_processor_in_args (const grpc_channel_args * args);
+grpc_arg grpc_auth_metadata_processor_to_arg(grpc_auth_metadata_processor *p);
+grpc_auth_metadata_processor *grpc_auth_metadata_processor_from_arg(
+ const grpc_arg *arg);
+grpc_auth_metadata_processor *grpc_find_auth_metadata_processor_in_args(
+ const grpc_channel_args *args);
#endif /* GRPC_INTERNAL_CORE_SECURITY_SECURITY_CONTEXT_H */
diff --git a/src/core/security/server_auth_filter.c b/src/core/security/server_auth_filter.c
index 68f64c4d7e..9638b18e88 100644
--- a/src/core/security/server_auth_filter.c
+++ b/src/core/security/server_auth_filter.c
@@ -40,8 +40,7 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-typedef struct call_data
-{
+typedef struct call_data {
gpr_uint8 got_client_metadata;
grpc_stream_op_buffer *recv_ops;
/* Closure to call when finished with the auth_on_recv hook. */
@@ -58,139 +57,132 @@ typedef struct call_data
grpc_auth_context *auth_context;
} call_data;
-typedef struct channel_data
-{
+typedef struct channel_data {
grpc_security_connector *security_connector;
grpc_auth_metadata_processor processor;
grpc_mdctx *mdctx;
} channel_data;
-static grpc_metadata_array
-metadata_batch_to_md_array (const grpc_metadata_batch * batch)
-{
+static grpc_metadata_array metadata_batch_to_md_array(
+ const grpc_metadata_batch *batch) {
grpc_linked_mdelem *l;
grpc_metadata_array result;
- grpc_metadata_array_init (&result);
- for (l = batch->list.head; l != NULL; l = l->next)
- {
- grpc_metadata *usr_md = NULL;
- grpc_mdelem *md = l->md;
- grpc_mdstr *key = md->key;
- grpc_mdstr *value = md->value;
- if (result.count == result.capacity)
- {
- result.capacity = GPR_MAX (result.capacity + 8, result.capacity * 2);
- result.metadata = gpr_realloc (result.metadata, result.capacity * sizeof (grpc_metadata));
- }
- usr_md = &result.metadata[result.count++];
- usr_md->key = grpc_mdstr_as_c_string (key);
- usr_md->value = grpc_mdstr_as_c_string (value);
- usr_md->value_length = GPR_SLICE_LENGTH (value->slice);
+ grpc_metadata_array_init(&result);
+ for (l = batch->list.head; l != NULL; l = l->next) {
+ grpc_metadata *usr_md = NULL;
+ grpc_mdelem *md = l->md;
+ grpc_mdstr *key = md->key;
+ grpc_mdstr *value = md->value;
+ if (result.count == result.capacity) {
+ result.capacity = GPR_MAX(result.capacity + 8, result.capacity * 2);
+ result.metadata =
+ gpr_realloc(result.metadata, result.capacity * sizeof(grpc_metadata));
}
+ usr_md = &result.metadata[result.count++];
+ usr_md->key = grpc_mdstr_as_c_string(key);
+ usr_md->value = grpc_mdstr_as_c_string(value);
+ usr_md->value_length = GPR_SLICE_LENGTH(value->slice);
+ }
return result;
}
-static grpc_mdelem *
-remove_consumed_md (void *user_data, grpc_mdelem * md)
-{
+static grpc_mdelem *remove_consumed_md(void *user_data, grpc_mdelem *md) {
grpc_call_element *elem = user_data;
call_data *calld = elem->call_data;
size_t i;
- for (i = 0; i < calld->num_consumed_md; i++)
- {
- const grpc_metadata *consumed_md = &calld->consumed_md[i];
- /* Maybe we could do a pointer comparison but we do not have any guarantee
- that the metadata processor used the same pointers for consumed_md in the
- callback. */
- if (GPR_SLICE_LENGTH (md->key->slice) != strlen (consumed_md->key) || GPR_SLICE_LENGTH (md->value->slice) != consumed_md->value_length)
- {
- continue;
- }
- if (memcmp (GPR_SLICE_START_PTR (md->key->slice), consumed_md->key, GPR_SLICE_LENGTH (md->key->slice)) == 0 && memcmp (GPR_SLICE_START_PTR (md->value->slice), consumed_md->value, GPR_SLICE_LENGTH (md->value->slice)) == 0)
- {
- return NULL; /* Delete. */
- }
+ for (i = 0; i < calld->num_consumed_md; i++) {
+ const grpc_metadata *consumed_md = &calld->consumed_md[i];
+ /* Maybe we could do a pointer comparison but we do not have any guarantee
+ that the metadata processor used the same pointers for consumed_md in the
+ callback. */
+ if (GPR_SLICE_LENGTH(md->key->slice) != strlen(consumed_md->key) ||
+ GPR_SLICE_LENGTH(md->value->slice) != consumed_md->value_length) {
+ continue;
}
+ if (memcmp(GPR_SLICE_START_PTR(md->key->slice), consumed_md->key,
+ GPR_SLICE_LENGTH(md->key->slice)) == 0 &&
+ memcmp(GPR_SLICE_START_PTR(md->value->slice), consumed_md->value,
+ GPR_SLICE_LENGTH(md->value->slice)) == 0) {
+ return NULL; /* Delete. */
+ }
+ }
return md;
}
/* called from application code */
-static void
-on_md_processing_done (void *user_data, const grpc_metadata * consumed_md, size_t num_consumed_md, const grpc_metadata * response_md, size_t num_response_md, grpc_status_code status, const char *error_details)
-{
+static void on_md_processing_done(
+ void *user_data, const grpc_metadata *consumed_md, size_t num_consumed_md,
+ const grpc_metadata *response_md, size_t num_response_md,
+ grpc_status_code status, const char *error_details) {
grpc_call_element *elem = user_data;
call_data *calld = elem->call_data;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
/* TODO(jboeuf): Implement support for response_md. */
- if (response_md != NULL && num_response_md > 0)
- {
- gpr_log (GPR_INFO, "response_md in auth metadata processing not supported for now. " "Ignoring...");
- }
+ if (response_md != NULL && num_response_md > 0) {
+ gpr_log(GPR_INFO,
+ "response_md in auth metadata processing not supported for now. "
+ "Ignoring...");
+ }
- if (status == GRPC_STATUS_OK)
- {
- calld->consumed_md = consumed_md;
- calld->num_consumed_md = num_consumed_md;
- grpc_metadata_batch_filter (&calld->md_op->data.metadata, remove_consumed_md, elem);
- grpc_metadata_array_destroy (&calld->md);
- calld->on_done_recv->cb (&exec_ctx, calld->on_done_recv->cb_arg, 1);
- }
- else
- {
- gpr_slice message;
- grpc_metadata_array_destroy (&calld->md);
- error_details = error_details != NULL ? error_details : "Authentication metadata processing failed.";
- message = gpr_slice_from_copied_string (error_details);
- grpc_sopb_reset (calld->recv_ops);
- grpc_transport_stream_op_add_close (&calld->transport_op, status, &message);
- grpc_call_next_op (&exec_ctx, elem, &calld->transport_op);
- }
+ if (status == GRPC_STATUS_OK) {
+ calld->consumed_md = consumed_md;
+ calld->num_consumed_md = num_consumed_md;
+ grpc_metadata_batch_filter(&calld->md_op->data.metadata, remove_consumed_md,
+ elem);
+ grpc_metadata_array_destroy(&calld->md);
+ calld->on_done_recv->cb(&exec_ctx, calld->on_done_recv->cb_arg, 1);
+ } else {
+ gpr_slice message;
+ grpc_metadata_array_destroy(&calld->md);
+ error_details = error_details != NULL
+ ? error_details
+ : "Authentication metadata processing failed.";
+ message = gpr_slice_from_copied_string(error_details);
+ grpc_sopb_reset(calld->recv_ops);
+ grpc_transport_stream_op_add_close(&calld->transport_op, status, &message);
+ grpc_call_next_op(&exec_ctx, elem, &calld->transport_op);
+ }
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-static void
-auth_on_recv (grpc_exec_ctx * exec_ctx, void *user_data, int success)
-{
+static void auth_on_recv(grpc_exec_ctx *exec_ctx, void *user_data,
+ int success) {
grpc_call_element *elem = user_data;
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- if (success)
- {
- size_t i;
- size_t nops = calld->recv_ops->nops;
- grpc_stream_op *ops = calld->recv_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- if (op->type != GRPC_OP_METADATA || calld->got_client_metadata)
- continue;
- calld->got_client_metadata = 1;
- if (chand->processor.process == NULL)
- continue;
- calld->md_op = op;
- calld->md = metadata_batch_to_md_array (&op->data.metadata);
- chand->processor.process (chand->processor.state, calld->auth_context, calld->md.metadata, calld->md.count, on_md_processing_done, elem);
- return;
- }
+ if (success) {
+ size_t i;
+ size_t nops = calld->recv_ops->nops;
+ grpc_stream_op *ops = calld->recv_ops->ops;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ if (op->type != GRPC_OP_METADATA || calld->got_client_metadata) continue;
+ calld->got_client_metadata = 1;
+ if (chand->processor.process == NULL) continue;
+ calld->md_op = op;
+ calld->md = metadata_batch_to_md_array(&op->data.metadata);
+ chand->processor.process(chand->processor.state, calld->auth_context,
+ calld->md.metadata, calld->md.count,
+ on_md_processing_done, elem);
+ return;
}
- calld->on_done_recv->cb (exec_ctx, calld->on_done_recv->cb_arg, success);
+ }
+ calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
}
-static void
-set_recv_ops_md_callbacks (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void set_recv_ops_md_callbacks(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
- if (op->recv_ops && !calld->got_client_metadata)
- {
- /* substitute our callback for the higher callback */
- calld->recv_ops = op->recv_ops;
- calld->on_done_recv = op->on_done_recv;
- op->on_done_recv = &calld->auth_on_recv;
- calld->transport_op = *op;
- }
+ if (op->recv_ops && !calld->got_client_metadata) {
+ /* substitute our callback for the higher callback */
+ calld->recv_ops = op->recv_ops;
+ calld->on_done_recv = op->on_done_recv;
+ op->on_done_recv = &calld->auth_on_recv;
+ calld->transport_op = *op;
+ }
}
/* Called either:
@@ -198,88 +190,91 @@ set_recv_ops_md_callbacks (grpc_call_element * elem, grpc_transport_stream_op *
- a network event (or similar) from below, to receive something
op contains type and call direction information, in addition to the data
that is being sent or received. */
-static void
-auth_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- set_recv_ops_md_callbacks (elem, op);
- grpc_call_next_op (exec_ctx, elem, op);
+static void auth_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ set_recv_ops_md_callbacks(elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
/* Constructor for call_data */
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
/* grab pointers to our data from the call element */
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
grpc_server_security_context *server_ctx = NULL;
/* initialize members */
- memset (calld, 0, sizeof (*calld));
- grpc_closure_init (&calld->auth_on_recv, auth_on_recv, elem);
+ memset(calld, 0, sizeof(*calld));
+ grpc_closure_init(&calld->auth_on_recv, auth_on_recv, elem);
- GPR_ASSERT (initial_op && initial_op->context != NULL && initial_op->context[GRPC_CONTEXT_SECURITY].value == NULL);
+ GPR_ASSERT(initial_op && initial_op->context != NULL &&
+ initial_op->context[GRPC_CONTEXT_SECURITY].value == NULL);
/* Create a security context for the call and reference the auth context from
the channel. */
- if (initial_op->context[GRPC_CONTEXT_SECURITY].value != NULL)
- {
- initial_op->context[GRPC_CONTEXT_SECURITY].destroy (initial_op->context[GRPC_CONTEXT_SECURITY].value);
- }
- server_ctx = grpc_server_security_context_create ();
- server_ctx->auth_context = grpc_auth_context_create (chand->security_connector->auth_context);
+ if (initial_op->context[GRPC_CONTEXT_SECURITY].value != NULL) {
+ initial_op->context[GRPC_CONTEXT_SECURITY].destroy(
+ initial_op->context[GRPC_CONTEXT_SECURITY].value);
+ }
+ server_ctx = grpc_server_security_context_create();
+ server_ctx->auth_context =
+ grpc_auth_context_create(chand->security_connector->auth_context);
server_ctx->auth_context->pollset = initial_op->bind_pollset;
initial_op->context[GRPC_CONTEXT_SECURITY].value = server_ctx;
- initial_op->context[GRPC_CONTEXT_SECURITY].destroy = grpc_server_security_context_destroy;
+ initial_op->context[GRPC_CONTEXT_SECURITY].destroy =
+ grpc_server_security_context_destroy;
calld->auth_context = server_ctx->auth_context;
/* Set the metadata callbacks. */
- set_recv_ops_md_callbacks (elem, initial_op);
+ set_recv_ops_md_callbacks(elem, initial_op);
}
/* Destructor for call_data */
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
-}
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {}
/* Constructor for channel_data */
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
- grpc_security_connector *sc = grpc_find_security_connector_in_args (args);
- grpc_auth_metadata_processor *processor = grpc_find_auth_metadata_processor_in_args (args);
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
+ grpc_security_connector *sc = grpc_find_security_connector_in_args(args);
+ grpc_auth_metadata_processor *processor =
+ grpc_find_auth_metadata_processor_in_args(args);
/* grab pointers to our data from the channel element */
channel_data *chand = elem->channel_data;
/* The first and the last filters tend to be implemented differently to
handle the case that there's no 'next' filter to call on the up or down
path */
- GPR_ASSERT (!is_first);
- GPR_ASSERT (!is_last);
- GPR_ASSERT (sc != NULL);
- GPR_ASSERT (processor != NULL);
+ GPR_ASSERT(!is_first);
+ GPR_ASSERT(!is_last);
+ GPR_ASSERT(sc != NULL);
+ GPR_ASSERT(processor != NULL);
/* initialize members */
- GPR_ASSERT (!sc->is_client_side);
- chand->security_connector = GRPC_SECURITY_CONNECTOR_REF (sc, "server_auth_filter");
+ GPR_ASSERT(!sc->is_client_side);
+ chand->security_connector =
+ GRPC_SECURITY_CONNECTOR_REF(sc, "server_auth_filter");
chand->mdctx = mdctx;
chand->processor = *processor;
}
/* Destructor for channel data */
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
/* grab pointers to our data from the channel element */
channel_data *chand = elem->channel_data;
- GRPC_SECURITY_CONNECTOR_UNREF (chand->security_connector, "server_auth_filter");
+ GRPC_SECURITY_CONNECTOR_UNREF(chand->security_connector,
+ "server_auth_filter");
}
const grpc_channel_filter grpc_server_auth_filter = {
- auth_start_transport_op, grpc_channel_next_op,
- sizeof (call_data), init_call_elem,
- destroy_call_elem, sizeof (channel_data),
- init_channel_elem, destroy_channel_elem,
- grpc_call_next_get_peer, "server-auth"
-};
+ auth_start_transport_op, grpc_channel_next_op,
+ sizeof(call_data), init_call_elem,
+ destroy_call_elem, sizeof(channel_data),
+ init_channel_elem, destroy_channel_elem,
+ grpc_call_next_get_peer, "server-auth"};
diff --git a/src/core/security/server_secure_chttp2.c b/src/core/security/server_secure_chttp2.c
index 336d59f624..a6c515dc34 100644
--- a/src/core/security/server_secure_chttp2.c
+++ b/src/core/security/server_secure_chttp2.c
@@ -51,14 +51,12 @@
#include <grpc/support/sync.h>
#include <grpc/support/useful.h>
-typedef struct tcp_endpoint_list
-{
+typedef struct tcp_endpoint_list {
grpc_endpoint *tcp_endpoint;
struct tcp_endpoint_list *next;
} tcp_endpoint_list;
-typedef struct grpc_server_secure_state
-{
+typedef struct grpc_server_secure_state {
grpc_server *server;
grpc_tcp_server *tcp;
grpc_security_connector *sc;
@@ -71,160 +69,148 @@ typedef struct grpc_server_secure_state
grpc_closure *destroy_callback;
} grpc_server_secure_state;
-static void
-state_ref (grpc_server_secure_state * state)
-{
- gpr_ref (&state->refcount);
+static void state_ref(grpc_server_secure_state *state) {
+ gpr_ref(&state->refcount);
}
-static void
-state_unref (grpc_server_secure_state * state)
-{
- if (gpr_unref (&state->refcount))
- {
- /* ensure all threads have unlocked */
- gpr_mu_lock (&state->mu);
- gpr_mu_unlock (&state->mu);
- /* clean up */
- GRPC_SECURITY_CONNECTOR_UNREF (state->sc, "server");
- grpc_server_credentials_unref (state->creds);
- gpr_free (state);
- }
+static void state_unref(grpc_server_secure_state *state) {
+ if (gpr_unref(&state->refcount)) {
+ /* ensure all threads have unlocked */
+ gpr_mu_lock(&state->mu);
+ gpr_mu_unlock(&state->mu);
+ /* clean up */
+ GRPC_SECURITY_CONNECTOR_UNREF(state->sc, "server");
+ grpc_server_credentials_unref(state->creds);
+ gpr_free(state);
+ }
}
-static void
-setup_transport (grpc_exec_ctx * exec_ctx, void *statep, grpc_transport * transport, grpc_mdctx * mdctx)
-{
+static void setup_transport(grpc_exec_ctx *exec_ctx, void *statep,
+ grpc_transport *transport, grpc_mdctx *mdctx) {
static grpc_channel_filter const *extra_filters[] = {
- &grpc_server_auth_filter, &grpc_http_server_filter
- };
+ &grpc_server_auth_filter, &grpc_http_server_filter};
grpc_server_secure_state *state = statep;
grpc_channel_args *args_copy;
grpc_arg args_to_add[2];
- args_to_add[0] = grpc_security_connector_to_arg (state->sc);
- args_to_add[1] = grpc_auth_metadata_processor_to_arg (&state->creds->processor);
- args_copy = grpc_channel_args_copy_and_add (grpc_server_get_channel_args (state->server), args_to_add, GPR_ARRAY_SIZE (args_to_add));
- grpc_server_setup_transport (exec_ctx, state->server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, args_copy);
- grpc_channel_args_destroy (args_copy);
+ args_to_add[0] = grpc_security_connector_to_arg(state->sc);
+ args_to_add[1] =
+ grpc_auth_metadata_processor_to_arg(&state->creds->processor);
+ args_copy = grpc_channel_args_copy_and_add(
+ grpc_server_get_channel_args(state->server), args_to_add,
+ GPR_ARRAY_SIZE(args_to_add));
+ grpc_server_setup_transport(exec_ctx, state->server, transport, extra_filters,
+ GPR_ARRAY_SIZE(extra_filters), mdctx, args_copy);
+ grpc_channel_args_destroy(args_copy);
}
-static int
-remove_tcp_from_list_locked (grpc_server_secure_state * state, grpc_endpoint * tcp)
-{
+static int remove_tcp_from_list_locked(grpc_server_secure_state *state,
+ grpc_endpoint *tcp) {
tcp_endpoint_list *node = state->handshaking_tcp_endpoints;
tcp_endpoint_list *tmp = NULL;
- if (node && node->tcp_endpoint == tcp)
- {
- state->handshaking_tcp_endpoints = state->handshaking_tcp_endpoints->next;
- gpr_free (node);
+ if (node && node->tcp_endpoint == tcp) {
+ state->handshaking_tcp_endpoints = state->handshaking_tcp_endpoints->next;
+ gpr_free(node);
+ return 0;
+ }
+ while (node) {
+ if (node->next->tcp_endpoint == tcp) {
+ tmp = node->next;
+ node->next = node->next->next;
+ gpr_free(tmp);
return 0;
}
- while (node)
- {
- if (node->next->tcp_endpoint == tcp)
- {
- tmp = node->next;
- node->next = node->next->next;
- gpr_free (tmp);
- return 0;
- }
- node = node->next;
- }
+ node = node->next;
+ }
return -1;
}
-static void
-on_secure_handshake_done (grpc_exec_ctx * exec_ctx, void *statep, grpc_security_status status, grpc_endpoint * wrapped_endpoint, grpc_endpoint * secure_endpoint)
-{
+static void on_secure_handshake_done(grpc_exec_ctx *exec_ctx, void *statep,
+ grpc_security_status status,
+ grpc_endpoint *wrapped_endpoint,
+ grpc_endpoint *secure_endpoint) {
grpc_server_secure_state *state = statep;
grpc_transport *transport;
grpc_mdctx *mdctx;
- if (status == GRPC_SECURITY_OK)
- {
- gpr_mu_lock (&state->mu);
- remove_tcp_from_list_locked (state, wrapped_endpoint);
- if (!state->is_shutdown)
- {
- mdctx = grpc_mdctx_create ();
- transport = grpc_create_chttp2_transport (exec_ctx, grpc_server_get_channel_args (state->server), secure_endpoint, mdctx, 0);
- setup_transport (exec_ctx, state, transport, mdctx);
- grpc_chttp2_transport_start_reading (exec_ctx, transport, NULL, 0);
- }
- else
- {
- /* We need to consume this here, because the server may already have gone
- * away. */
- grpc_endpoint_destroy (exec_ctx, secure_endpoint);
- }
- gpr_mu_unlock (&state->mu);
+ if (status == GRPC_SECURITY_OK) {
+ gpr_mu_lock(&state->mu);
+ remove_tcp_from_list_locked(state, wrapped_endpoint);
+ if (!state->is_shutdown) {
+ mdctx = grpc_mdctx_create();
+ transport = grpc_create_chttp2_transport(
+ exec_ctx, grpc_server_get_channel_args(state->server),
+ secure_endpoint, mdctx, 0);
+ setup_transport(exec_ctx, state, transport, mdctx);
+ grpc_chttp2_transport_start_reading(exec_ctx, transport, NULL, 0);
+ } else {
+ /* We need to consume this here, because the server may already have gone
+ * away. */
+ grpc_endpoint_destroy(exec_ctx, secure_endpoint);
}
- else
- {
- gpr_mu_lock (&state->mu);
- remove_tcp_from_list_locked (state, wrapped_endpoint);
- gpr_mu_unlock (&state->mu);
- gpr_log (GPR_ERROR, "Secure transport failed with error %d", status);
- }
- state_unref (state);
+ gpr_mu_unlock(&state->mu);
+ } else {
+ gpr_mu_lock(&state->mu);
+ remove_tcp_from_list_locked(state, wrapped_endpoint);
+ gpr_mu_unlock(&state->mu);
+ gpr_log(GPR_ERROR, "Secure transport failed with error %d", status);
+ }
+ state_unref(state);
}
-static void
-on_accept (grpc_exec_ctx * exec_ctx, void *statep, grpc_endpoint * tcp)
-{
+static void on_accept(grpc_exec_ctx *exec_ctx, void *statep,
+ grpc_endpoint *tcp) {
grpc_server_secure_state *state = statep;
tcp_endpoint_list *node;
- state_ref (state);
- node = gpr_malloc (sizeof (tcp_endpoint_list));
+ state_ref(state);
+ node = gpr_malloc(sizeof(tcp_endpoint_list));
node->tcp_endpoint = tcp;
- gpr_mu_lock (&state->mu);
+ gpr_mu_lock(&state->mu);
node->next = state->handshaking_tcp_endpoints;
state->handshaking_tcp_endpoints = node;
- gpr_mu_unlock (&state->mu);
- grpc_security_connector_do_handshake (exec_ctx, state->sc, tcp, on_secure_handshake_done, state);
+ gpr_mu_unlock(&state->mu);
+ grpc_security_connector_do_handshake(exec_ctx, state->sc, tcp,
+ on_secure_handshake_done, state);
}
/* Server callback: start listening on our ports */
-static void
-start (grpc_exec_ctx * exec_ctx, grpc_server * server, void *statep, grpc_pollset ** pollsets, size_t pollset_count)
-{
+static void start(grpc_exec_ctx *exec_ctx, grpc_server *server, void *statep,
+ grpc_pollset **pollsets, size_t pollset_count) {
grpc_server_secure_state *state = statep;
- grpc_tcp_server_start (exec_ctx, state->tcp, pollsets, pollset_count, on_accept, state);
+ grpc_tcp_server_start(exec_ctx, state->tcp, pollsets, pollset_count,
+ on_accept, state);
}
-static void
-destroy_done (grpc_exec_ctx * exec_ctx, void *statep, int success)
-{
+static void destroy_done(grpc_exec_ctx *exec_ctx, void *statep, int success) {
grpc_server_secure_state *state = statep;
- state->destroy_callback->cb (exec_ctx, state->destroy_callback->cb_arg, success);
- gpr_mu_lock (&state->mu);
- while (state->handshaking_tcp_endpoints != NULL)
- {
- grpc_endpoint_shutdown (exec_ctx, state->handshaking_tcp_endpoints->tcp_endpoint);
- remove_tcp_from_list_locked (state, state->handshaking_tcp_endpoints->tcp_endpoint);
- }
- gpr_mu_unlock (&state->mu);
- state_unref (state);
+ state->destroy_callback->cb(exec_ctx, state->destroy_callback->cb_arg,
+ success);
+ gpr_mu_lock(&state->mu);
+ while (state->handshaking_tcp_endpoints != NULL) {
+ grpc_endpoint_shutdown(exec_ctx,
+ state->handshaking_tcp_endpoints->tcp_endpoint);
+ remove_tcp_from_list_locked(state,
+ state->handshaking_tcp_endpoints->tcp_endpoint);
+ }
+ gpr_mu_unlock(&state->mu);
+ state_unref(state);
}
/* Server callback: destroy the tcp listener (so we don't generate further
callbacks) */
-static void
-destroy (grpc_exec_ctx * exec_ctx, grpc_server * server, void *statep, grpc_closure * callback)
-{
+static void destroy(grpc_exec_ctx *exec_ctx, grpc_server *server, void *statep,
+ grpc_closure *callback) {
grpc_server_secure_state *state = statep;
grpc_tcp_server *tcp;
- gpr_mu_lock (&state->mu);
+ gpr_mu_lock(&state->mu);
state->is_shutdown = 1;
state->destroy_callback = callback;
tcp = state->tcp;
- gpr_mu_unlock (&state->mu);
- grpc_closure_init (&state->destroy_closure, destroy_done, state);
- grpc_tcp_server_destroy (exec_ctx, tcp, &state->destroy_closure);
+ gpr_mu_unlock(&state->mu);
+ grpc_closure_init(&state->destroy_closure, destroy_done, state);
+ grpc_tcp_server_destroy(exec_ctx, tcp, &state->destroy_closure);
}
-int
-grpc_server_add_secure_http2_port (grpc_server * server, const char *addr, grpc_server_credentials * creds)
-{
+int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
+ grpc_server_credentials *creds) {
grpc_resolved_addresses *resolved = NULL;
grpc_tcp_server *tcp = NULL;
grpc_server_secure_state *state = NULL;
@@ -237,92 +223,83 @@ grpc_server_add_secure_http2_port (grpc_server * server, const char *addr, grpc_
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
/* create security context */
- if (creds == NULL)
+ if (creds == NULL) goto error;
+ status = grpc_server_credentials_create_security_connector(creds, &sc);
+ if (status != GRPC_SECURITY_OK) {
+ gpr_log(GPR_ERROR,
+ "Unable to create secure server with credentials of type %s.",
+ creds->type);
goto error;
- status = grpc_server_credentials_create_security_connector (creds, &sc);
- if (status != GRPC_SECURITY_OK)
- {
- gpr_log (GPR_ERROR, "Unable to create secure server with credentials of type %s.", creds->type);
- goto error;
- }
+ }
/* resolve address */
- resolved = grpc_blocking_resolve_address (addr, "https");
- if (!resolved)
- {
- goto error;
- }
+ resolved = grpc_blocking_resolve_address(addr, "https");
+ if (!resolved) {
+ goto error;
+ }
- tcp = grpc_tcp_server_create ();
- if (!tcp)
- {
- goto error;
- }
+ tcp = grpc_tcp_server_create();
+ if (!tcp) {
+ goto error;
+ }
- for (i = 0; i < resolved->naddrs; i++)
- {
- port_temp = grpc_tcp_server_add_port (tcp, (struct sockaddr *) &resolved->addrs[i].addr, resolved->addrs[i].len);
- if (port_temp >= 0)
- {
- if (port_num == -1)
- {
- port_num = port_temp;
- }
- else
- {
- GPR_ASSERT (port_num == port_temp);
- }
- count++;
- }
- }
- if (count == 0)
- {
- gpr_log (GPR_ERROR, "No address added out of total %d resolved", resolved->naddrs);
- goto error;
+ for (i = 0; i < resolved->naddrs; i++) {
+ port_temp = grpc_tcp_server_add_port(
+ tcp, (struct sockaddr *)&resolved->addrs[i].addr,
+ resolved->addrs[i].len);
+ if (port_temp >= 0) {
+ if (port_num == -1) {
+ port_num = port_temp;
+ } else {
+ GPR_ASSERT(port_num == port_temp);
+ }
+ count++;
}
- if (count != resolved->naddrs)
- {
- gpr_log (GPR_ERROR, "Only %d addresses added out of total %d resolved", count, resolved->naddrs);
- /* if it's an error, don't we want to goto error; here ? */
- }
- grpc_resolved_addresses_destroy (resolved);
+ }
+ if (count == 0) {
+ gpr_log(GPR_ERROR, "No address added out of total %d resolved",
+ resolved->naddrs);
+ goto error;
+ }
+ if (count != resolved->naddrs) {
+ gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
+ count, resolved->naddrs);
+ /* if it's an error, don't we want to goto error; here ? */
+ }
+ grpc_resolved_addresses_destroy(resolved);
- state = gpr_malloc (sizeof (*state));
- memset (state, 0, sizeof (*state));
+ state = gpr_malloc(sizeof(*state));
+ memset(state, 0, sizeof(*state));
state->server = server;
state->tcp = tcp;
state->sc = sc;
- state->creds = grpc_server_credentials_ref (creds);
+ state->creds = grpc_server_credentials_ref(creds);
state->handshaking_tcp_endpoints = NULL;
state->is_shutdown = 0;
- gpr_mu_init (&state->mu);
- gpr_ref_init (&state->refcount, 1);
+ gpr_mu_init(&state->mu);
+ gpr_ref_init(&state->refcount, 1);
/* Register with the server only upon success */
- grpc_server_add_listener (&exec_ctx, server, state, start, destroy);
+ grpc_server_add_listener(&exec_ctx, server, state, start, destroy);
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
return port_num;
/* Error path: cleanup and return */
error:
- if (sc)
- {
- GRPC_SECURITY_CONNECTOR_UNREF (sc, "server");
- }
- if (resolved)
- {
- grpc_resolved_addresses_destroy (resolved);
- }
- if (tcp)
- {
- grpc_tcp_server_destroy (&exec_ctx, tcp, NULL);
- }
- if (state)
- {
- gpr_free (state);
- }
- grpc_exec_ctx_finish (&exec_ctx);
+ if (sc) {
+ GRPC_SECURITY_CONNECTOR_UNREF(sc, "server");
+ }
+ if (resolved) {
+ grpc_resolved_addresses_destroy(resolved);
+ }
+ if (tcp) {
+ grpc_tcp_server_destroy(&exec_ctx, tcp, NULL);
+ }
+ if (state) {
+ gpr_free(state);
+ }
+ grpc_exec_ctx_finish(&exec_ctx);
return 0;
}
diff --git a/src/core/statistics/census_init.c b/src/core/statistics/census_init.c
index abe1acf170..e6306f5e6f 100644
--- a/src/core/statistics/census_init.c
+++ b/src/core/statistics/census_init.c
@@ -37,16 +37,12 @@
#include "src/core/statistics/census_rpc_stats.h"
#include "src/core/statistics/census_tracing.h"
-void
-census_init (void)
-{
- census_tracing_init ();
- census_stats_store_init ();
+void census_init(void) {
+ census_tracing_init();
+ census_stats_store_init();
}
-void
-census_shutdown (void)
-{
- census_stats_store_shutdown ();
- census_tracing_shutdown ();
+void census_shutdown(void) {
+ census_stats_store_shutdown();
+ census_tracing_shutdown();
}
diff --git a/src/core/statistics/census_interface.h b/src/core/statistics/census_interface.h
index 22b3cc8c88..e870357276 100644
--- a/src/core/statistics/census_interface.h
+++ b/src/core/statistics/census_interface.h
@@ -41,8 +41,7 @@
/* Structure of a census op id. Define as structure because 64bit integer is not
available on every platform for C89. */
-typedef struct census_op_id
-{
+typedef struct census_op_id {
gpr_uint32 upper;
gpr_uint32 lower;
} census_op_id;
@@ -50,10 +49,10 @@ typedef struct census_op_id
typedef struct census_rpc_stats census_rpc_stats;
/* Initializes Census library. No-op if Census is already initialized. */
-void census_init (void);
+void census_init(void);
/* Shutdown Census Library. */
-void census_shutdown (void);
+void census_shutdown(void);
/* Annotates grpc method name on a census_op_id. The method name has the format
of <full quantified rpc service name>/<rpc function name>. Returns 0 iff
@@ -62,16 +61,16 @@ void census_shutdown (void);
TODO(hongyu): Figure out valid characters set for service name and command
name and document requirements here.*/
-int census_add_method_tag (census_op_id op_id, const char *method_name);
+int census_add_method_tag(census_op_id op_id, const char *method_name);
/* Annotates tracing information to a specific op_id.
Up to CENSUS_MAX_ANNOTATION_LENGTH bytes are recorded. */
-void census_tracing_print (census_op_id op_id, const char *annotation);
+void census_tracing_print(census_op_id op_id, const char *annotation);
/* Starts tracing for an RPC. Returns a locally unique census_op_id */
-census_op_id census_tracing_start_op (void);
+census_op_id census_tracing_start_op(void);
/* Ends tracing. Calling this function will invalidate the input op_id. */
-void census_tracing_end_op (census_op_id op_id);
+void census_tracing_end_op(census_op_id op_id);
#endif /* GRPC_INTERNAL_CORE_STATISTICS_CENSUS_INTERFACE_H */
diff --git a/src/core/statistics/census_log.c b/src/core/statistics/census_log.c
index 16a9e28b1f..88e338038d 100644
--- a/src/core/statistics/census_log.c
+++ b/src/core/statistics/census_log.c
@@ -101,15 +101,13 @@
/* End of platform specific code */
-typedef struct census_log_block_list_struct
-{
+typedef struct census_log_block_list_struct {
struct census_log_block_list_struct *next;
struct census_log_block_list_struct *prev;
struct census_log_block *block;
} cl_block_list_struct;
-typedef struct census_log_block
-{
+typedef struct census_log_block {
/* Pointer to underlying buffer */
char *buffer;
gpr_atm writer_lock;
@@ -148,16 +146,14 @@ typedef struct census_log_block
} cl_block;
/* A list of cl_blocks, doubly-linked through cl_block::link. */
-typedef struct census_log_block_list
-{
- gpr_int32 count; /* Number of items in list. */
- cl_block_list_struct ht; /* head/tail of linked list. */
+typedef struct census_log_block_list {
+ gpr_int32 count; /* Number of items in list. */
+ cl_block_list_struct ht; /* head/tail of linked list. */
} cl_block_list;
/* Cacheline aligned block pointers to avoid false sharing. Block pointer must
be initialized via set_block(), before calling other functions */
-typedef struct census_log_core_local_block
-{
+typedef struct census_log_core_local_block {
gpr_atm block;
/* Ensure cachline alignment: we assume sizeof(gpr_atm) == 4 or 8 */
#if defined(GPR_ARCH_64)
@@ -174,17 +170,16 @@ typedef struct census_log_core_local_block
#endif
} cl_core_local_block;
-struct census_log
-{
+struct census_log {
int discard_old_records;
/* Number of cores (aka hardware-contexts) */
unsigned num_cores;
/* number of CENSUS_LOG_2_MAX_RECORD_SIZE blocks in log */
gpr_int32 num_blocks;
- cl_block *blocks; /* Block metadata. */
- cl_core_local_block *core_local_blocks; /* Keeps core to block mappings. */
+ cl_block *blocks; /* Block metadata. */
+ cl_core_local_block *core_local_blocks; /* Keeps core to block mappings. */
gpr_mu lock;
- int initialized; /* has log been initialized? */
+ int initialized; /* has log been initialized? */
/* Keeps the state of the reader iterator. A value of 0 indicates that
iterator has reached the end. census_log_init_reader() resets the
value to num_core to restart iteration. */
@@ -206,61 +201,44 @@ static struct census_log g_log;
/* Functions that operate on an atomic memory location used as a lock */
/* Returns non-zero if lock is acquired */
-static int
-cl_try_lock (gpr_atm * lock)
-{
- return gpr_atm_acq_cas (lock, 0, 1);
-}
+static int cl_try_lock(gpr_atm *lock) { return gpr_atm_acq_cas(lock, 0, 1); }
-static void
-cl_unlock (gpr_atm * lock)
-{
- gpr_atm_rel_store (lock, 0);
-}
+static void cl_unlock(gpr_atm *lock) { gpr_atm_rel_store(lock, 0); }
/* Functions that operate on cl_core_local_block's */
-static void
-cl_core_local_block_set_block (cl_core_local_block * clb, cl_block * block)
-{
- gpr_atm_rel_store (&clb->block, (gpr_atm) block);
+static void cl_core_local_block_set_block(cl_core_local_block *clb,
+ cl_block *block) {
+ gpr_atm_rel_store(&clb->block, (gpr_atm)block);
}
-static cl_block *
-cl_core_local_block_get_block (cl_core_local_block * clb)
-{
- return (cl_block *) gpr_atm_acq_load (&clb->block);
+static cl_block *cl_core_local_block_get_block(cl_core_local_block *clb) {
+ return (cl_block *)gpr_atm_acq_load(&clb->block);
}
/* Functions that operate on cl_block_list_struct's */
-static void
-cl_block_list_struct_initialize (cl_block_list_struct * bls, cl_block * block)
-{
+static void cl_block_list_struct_initialize(cl_block_list_struct *bls,
+ cl_block *block) {
bls->next = bls->prev = bls;
bls->block = block;
}
/* Functions that operate on cl_block_list's */
-static void
-cl_block_list_initialize (cl_block_list * list)
-{
+static void cl_block_list_initialize(cl_block_list *list) {
list->count = 0;
- cl_block_list_struct_initialize (&list->ht, NULL);
+ cl_block_list_struct_initialize(&list->ht, NULL);
}
/* Returns head of *this, or NULL if empty. */
-static cl_block *
-cl_block_list_head (cl_block_list * list)
-{
+static cl_block *cl_block_list_head(cl_block_list *list) {
return list->ht.next->block;
}
/* Insert element *e after *pos. */
-static void
-cl_block_list_insert (cl_block_list * list, cl_block_list_struct * pos, cl_block_list_struct * e)
-{
+static void cl_block_list_insert(cl_block_list *list, cl_block_list_struct *pos,
+ cl_block_list_struct *e) {
list->count++;
e->next = pos->next;
e->prev = pos;
@@ -269,23 +247,17 @@ cl_block_list_insert (cl_block_list * list, cl_block_list_struct * pos, cl_block
}
/* Insert block at the head of the list */
-static void
-cl_block_list_insert_at_head (cl_block_list * list, cl_block * block)
-{
- cl_block_list_insert (list, &list->ht, &block->link);
+static void cl_block_list_insert_at_head(cl_block_list *list, cl_block *block) {
+ cl_block_list_insert(list, &list->ht, &block->link);
}
/* Insert block at the tail of the list */
-static void
-cl_block_list_insert_at_tail (cl_block_list * list, cl_block * block)
-{
- cl_block_list_insert (list, list->ht.prev, &block->link);
+static void cl_block_list_insert_at_tail(cl_block_list *list, cl_block *block) {
+ cl_block_list_insert(list, list->ht.prev, &block->link);
}
/* Removes block *b. Requires *b be in the list. */
-static void
-cl_block_list_remove (cl_block_list * list, cl_block * b)
-{
+static void cl_block_list_remove(cl_block_list *list, cl_block *b) {
list->count--;
b->link.next->prev = b->link.prev;
b->link.prev->next = b->link.next;
@@ -293,28 +265,23 @@ cl_block_list_remove (cl_block_list * list, cl_block * b)
/* Functions that operate on cl_block's */
-static void
-cl_block_initialize (cl_block * block, char *buffer)
-{
+static void cl_block_initialize(cl_block *block, char *buffer) {
block->buffer = buffer;
- gpr_atm_rel_store (&block->writer_lock, 0);
- gpr_atm_rel_store (&block->reader_lock, 0);
- gpr_atm_rel_store (&block->bytes_committed, 0);
+ gpr_atm_rel_store(&block->writer_lock, 0);
+ gpr_atm_rel_store(&block->reader_lock, 0);
+ gpr_atm_rel_store(&block->bytes_committed, 0);
block->bytes_read = 0;
- cl_block_list_struct_initialize (&block->link, block);
+ cl_block_list_struct_initialize(&block->link, block);
}
/* Guards against exposing partially written buffer to the reader. */
-static void
-cl_block_set_bytes_committed (cl_block * block, gpr_int32 bytes_committed)
-{
- gpr_atm_rel_store (&block->bytes_committed, bytes_committed);
+static void cl_block_set_bytes_committed(cl_block *block,
+ gpr_int32 bytes_committed) {
+ gpr_atm_rel_store(&block->bytes_committed, bytes_committed);
}
-static gpr_int32
-cl_block_get_bytes_committed (cl_block * block)
-{
- return gpr_atm_acq_load (&block->bytes_committed);
+static gpr_int32 cl_block_get_bytes_committed(cl_block *block) {
+ return gpr_atm_acq_load(&block->bytes_committed);
}
/* Tries to disable future read/write access to this block. Succeeds if:
@@ -324,62 +291,51 @@ cl_block_get_bytes_committed (cl_block * block)
On success, clears the block state and returns with writer_lock_ and
reader_lock_ held. These locks are released by a subsequent
cl_block_access_enable() call. */
-static int
-cl_block_try_disable_access (cl_block * block, int discard_data)
-{
- if (!cl_try_lock (&block->writer_lock))
- {
- return 0;
- }
- if (!cl_try_lock (&block->reader_lock))
- {
- cl_unlock (&block->writer_lock);
- return 0;
- }
- if (!discard_data && (block->bytes_read != cl_block_get_bytes_committed (block)))
- {
- cl_unlock (&block->reader_lock);
- cl_unlock (&block->writer_lock);
- return 0;
- }
- cl_block_set_bytes_committed (block, 0);
+static int cl_block_try_disable_access(cl_block *block, int discard_data) {
+ if (!cl_try_lock(&block->writer_lock)) {
+ return 0;
+ }
+ if (!cl_try_lock(&block->reader_lock)) {
+ cl_unlock(&block->writer_lock);
+ return 0;
+ }
+ if (!discard_data &&
+ (block->bytes_read != cl_block_get_bytes_committed(block))) {
+ cl_unlock(&block->reader_lock);
+ cl_unlock(&block->writer_lock);
+ return 0;
+ }
+ cl_block_set_bytes_committed(block, 0);
block->bytes_read = 0;
return 1;
}
-static void
-cl_block_enable_access (cl_block * block)
-{
- cl_unlock (&block->reader_lock);
- cl_unlock (&block->writer_lock);
+static void cl_block_enable_access(cl_block *block) {
+ cl_unlock(&block->reader_lock);
+ cl_unlock(&block->writer_lock);
}
/* Returns with writer_lock held. */
-static void *
-cl_block_start_write (cl_block * block, size_t size)
-{
+static void *cl_block_start_write(cl_block *block, size_t size) {
gpr_int32 bytes_committed;
- if (!cl_try_lock (&block->writer_lock))
- {
- return NULL;
- }
- bytes_committed = cl_block_get_bytes_committed (block);
- if (bytes_committed + size > CENSUS_LOG_MAX_RECORD_SIZE)
- {
- cl_unlock (&block->writer_lock);
- return NULL;
- }
+ if (!cl_try_lock(&block->writer_lock)) {
+ return NULL;
+ }
+ bytes_committed = cl_block_get_bytes_committed(block);
+ if (bytes_committed + size > CENSUS_LOG_MAX_RECORD_SIZE) {
+ cl_unlock(&block->writer_lock);
+ return NULL;
+ }
return block->buffer + bytes_committed;
}
/* Releases writer_lock and increments committed bytes by 'bytes_written'.
'bytes_written' must be <= 'size' specified in the corresponding
StartWrite() call. This function is thread-safe. */
-static void
-cl_block_end_write (cl_block * block, size_t bytes_written)
-{
- cl_block_set_bytes_committed (block, cl_block_get_bytes_committed (block) + bytes_written);
- cl_unlock (&block->writer_lock);
+static void cl_block_end_write(cl_block *block, size_t bytes_written) {
+ cl_block_set_bytes_committed(
+ block, cl_block_get_bytes_committed(block) + bytes_written);
+ cl_unlock(&block->writer_lock);
}
/* Returns a pointer to the first unread byte in buffer. The number of bytes
@@ -387,60 +343,49 @@ cl_block_end_write (cl_block * block, size_t bytes_written)
released by a subsequent cl_block_end_read() call. Returns NULL if:
- read in progress
- no data available */
-static void *
-cl_block_start_read (cl_block * block, size_t * bytes_available)
-{
+static void *cl_block_start_read(cl_block *block, size_t *bytes_available) {
void *record;
- if (!cl_try_lock (&block->reader_lock))
- {
- return NULL;
- }
+ if (!cl_try_lock(&block->reader_lock)) {
+ return NULL;
+ }
/* bytes_committed may change from under us. Use bytes_available to update
bytes_read below. */
- *bytes_available = cl_block_get_bytes_committed (block) - block->bytes_read;
- if (*bytes_available == 0)
- {
- cl_unlock (&block->reader_lock);
- return NULL;
- }
+ *bytes_available = cl_block_get_bytes_committed(block) - block->bytes_read;
+ if (*bytes_available == 0) {
+ cl_unlock(&block->reader_lock);
+ return NULL;
+ }
record = block->buffer + block->bytes_read;
block->bytes_read += *bytes_available;
return record;
}
-static void
-cl_block_end_read (cl_block * block)
-{
- cl_unlock (&block->reader_lock);
+static void cl_block_end_read(cl_block *block) {
+ cl_unlock(&block->reader_lock);
}
/* Internal functions operating on g_log */
/* Allocates a new free block (or recycles an available dirty block if log is
configured to discard old records). Returns NULL if out-of-space. */
-static cl_block *
-cl_allocate_block (void)
-{
- cl_block *block = cl_block_list_head (&g_log.free_block_list);
- if (block != NULL)
- {
- cl_block_list_remove (&g_log.free_block_list, block);
- return block;
- }
- if (!g_log.discard_old_records)
- {
- /* No free block and log is configured to keep old records. */
- return NULL;
- }
+static cl_block *cl_allocate_block(void) {
+ cl_block *block = cl_block_list_head(&g_log.free_block_list);
+ if (block != NULL) {
+ cl_block_list_remove(&g_log.free_block_list, block);
+ return block;
+ }
+ if (!g_log.discard_old_records) {
+ /* No free block and log is configured to keep old records. */
+ return NULL;
+ }
/* Recycle dirty block. Start from the oldest. */
- for (block = cl_block_list_head (&g_log.dirty_block_list); block != NULL; block = block->link.next->block)
- {
- if (cl_block_try_disable_access (block, 1 /* discard data */ ))
- {
- cl_block_list_remove (&g_log.dirty_block_list, block);
- return block;
- }
+ for (block = cl_block_list_head(&g_log.dirty_block_list); block != NULL;
+ block = block->link.next->block) {
+ if (cl_block_try_disable_access(block, 1 /* discard data */)) {
+ cl_block_list_remove(&g_log.dirty_block_list, block);
+ return block;
}
+ }
return NULL;
}
@@ -450,250 +395,210 @@ cl_allocate_block (void)
- allocated a new block OR
- 'core_id' => 'old_block' mapping changed (another thread allocated a
block before lock was acquired). */
-static int
-cl_allocate_core_local_block (gpr_int32 core_id, cl_block * old_block)
-{
+static int cl_allocate_core_local_block(gpr_int32 core_id,
+ cl_block *old_block) {
/* Now that we have the lock, check if core-local mapping has changed. */
cl_core_local_block *core_local_block = &g_log.core_local_blocks[core_id];
- cl_block *block = cl_core_local_block_get_block (core_local_block);
- if ((block != NULL) && (block != old_block))
- {
- return 1;
- }
- if (block != NULL)
- {
- cl_core_local_block_set_block (core_local_block, NULL);
- cl_block_list_insert_at_tail (&g_log.dirty_block_list, block);
- }
- block = cl_allocate_block ();
- if (block == NULL)
- {
- gpr_atm_rel_store (&g_log.is_full, 1);
- return 0;
- }
- cl_core_local_block_set_block (core_local_block, block);
- cl_block_enable_access (block);
+ cl_block *block = cl_core_local_block_get_block(core_local_block);
+ if ((block != NULL) && (block != old_block)) {
+ return 1;
+ }
+ if (block != NULL) {
+ cl_core_local_block_set_block(core_local_block, NULL);
+ cl_block_list_insert_at_tail(&g_log.dirty_block_list, block);
+ }
+ block = cl_allocate_block();
+ if (block == NULL) {
+ gpr_atm_rel_store(&g_log.is_full, 1);
+ return 0;
+ }
+ cl_core_local_block_set_block(core_local_block, block);
+ cl_block_enable_access(block);
return 1;
}
-static cl_block *
-cl_get_block (void *record)
-{
- gpr_uintptr p = (gpr_uintptr) ((char *) record - g_log.buffer);
+static cl_block *cl_get_block(void *record) {
+ gpr_uintptr p = (gpr_uintptr)((char *)record - g_log.buffer);
gpr_uintptr index = p >> CENSUS_LOG_2_MAX_RECORD_SIZE;
return &g_log.blocks[index];
}
/* Gets the next block to read and tries to free 'prev' block (if not NULL).
Returns NULL if reached the end. */
-static cl_block *
-cl_next_block_to_read (cl_block * prev)
-{
+static cl_block *cl_next_block_to_read(cl_block *prev) {
cl_block *block = NULL;
- if (g_log.read_iterator_state == g_log.num_cores)
- {
- /* We are traversing dirty list; find the next dirty block. */
- if (prev != NULL)
- {
- /* Try to free the previous block if there is no unread data. This block
- may have unread data if previously incomplete record completed between
- read_next() calls. */
- block = prev->link.next->block;
- if (cl_block_try_disable_access (prev, 0 /* do not discard data */ ))
- {
- cl_block_list_remove (&g_log.dirty_block_list, prev);
- cl_block_list_insert_at_head (&g_log.free_block_list, prev);
- gpr_atm_rel_store (&g_log.is_full, 0);
- }
- }
- else
- {
- block = cl_block_list_head (&g_log.dirty_block_list);
- }
- if (block != NULL)
- {
- return block;
- }
- /* We are done with the dirty list; moving on to core-local blocks. */
+ if (g_log.read_iterator_state == g_log.num_cores) {
+ /* We are traversing dirty list; find the next dirty block. */
+ if (prev != NULL) {
+ /* Try to free the previous block if there is no unread data. This block
+ may have unread data if previously incomplete record completed between
+ read_next() calls. */
+ block = prev->link.next->block;
+ if (cl_block_try_disable_access(prev, 0 /* do not discard data */)) {
+ cl_block_list_remove(&g_log.dirty_block_list, prev);
+ cl_block_list_insert_at_head(&g_log.free_block_list, prev);
+ gpr_atm_rel_store(&g_log.is_full, 0);
+ }
+ } else {
+ block = cl_block_list_head(&g_log.dirty_block_list);
}
- while (g_log.read_iterator_state > 0)
- {
- g_log.read_iterator_state--;
- block = cl_core_local_block_get_block (&g_log.core_local_blocks[g_log.read_iterator_state]);
- if (block != NULL)
- {
- return block;
- }
+ if (block != NULL) {
+ return block;
+ }
+ /* We are done with the dirty list; moving on to core-local blocks. */
+ }
+ while (g_log.read_iterator_state > 0) {
+ g_log.read_iterator_state--;
+ block = cl_core_local_block_get_block(
+ &g_log.core_local_blocks[g_log.read_iterator_state]);
+ if (block != NULL) {
+ return block;
}
+ }
return NULL;
}
/* External functions: primary stats_log interface */
-void
-census_log_initialize (size_t size_in_mb, int discard_old_records)
-{
+void census_log_initialize(size_t size_in_mb, int discard_old_records) {
gpr_int32 ix;
/* Check cacheline alignment. */
- GPR_ASSERT (sizeof (cl_block) % GPR_CACHELINE_SIZE == 0);
- GPR_ASSERT (sizeof (cl_core_local_block) % GPR_CACHELINE_SIZE == 0);
- GPR_ASSERT (!g_log.initialized);
+ GPR_ASSERT(sizeof(cl_block) % GPR_CACHELINE_SIZE == 0);
+ GPR_ASSERT(sizeof(cl_core_local_block) % GPR_CACHELINE_SIZE == 0);
+ GPR_ASSERT(!g_log.initialized);
g_log.discard_old_records = discard_old_records;
- g_log.num_cores = gpr_cpu_num_cores ();
+ g_log.num_cores = gpr_cpu_num_cores();
/* Ensure at least as many blocks as there are cores. */
- g_log.num_blocks = GPR_MAX (g_log.num_cores, (size_in_mb << 20) >> CENSUS_LOG_2_MAX_RECORD_SIZE);
- gpr_mu_init (&g_log.lock);
+ g_log.num_blocks = GPR_MAX(
+ g_log.num_cores, (size_in_mb << 20) >> CENSUS_LOG_2_MAX_RECORD_SIZE);
+ gpr_mu_init(&g_log.lock);
g_log.read_iterator_state = 0;
g_log.block_being_read = NULL;
- gpr_atm_rel_store (&g_log.is_full, 0);
- g_log.core_local_blocks = (cl_core_local_block *) gpr_malloc_aligned (g_log.num_cores * sizeof (cl_core_local_block), GPR_CACHELINE_SIZE_LOG);
- memset (g_log.core_local_blocks, 0, g_log.num_cores * sizeof (cl_core_local_block));
- g_log.blocks = (cl_block *) gpr_malloc_aligned (g_log.num_blocks * sizeof (cl_block), GPR_CACHELINE_SIZE_LOG);
- memset (g_log.blocks, 0, g_log.num_blocks * sizeof (cl_block));
- g_log.buffer = gpr_malloc (g_log.num_blocks * CENSUS_LOG_MAX_RECORD_SIZE);
- memset (g_log.buffer, 0, g_log.num_blocks * CENSUS_LOG_MAX_RECORD_SIZE);
- cl_block_list_initialize (&g_log.free_block_list);
- cl_block_list_initialize (&g_log.dirty_block_list);
- for (ix = 0; ix < g_log.num_blocks; ++ix)
- {
- cl_block *block = g_log.blocks + ix;
- cl_block_initialize (block, g_log.buffer + (CENSUS_LOG_MAX_RECORD_SIZE * ix));
- cl_block_try_disable_access (block, 1 /* discard data */ );
- cl_block_list_insert_at_tail (&g_log.free_block_list, block);
- }
- gpr_atm_rel_store (&g_log.out_of_space_count, 0);
+ gpr_atm_rel_store(&g_log.is_full, 0);
+ g_log.core_local_blocks = (cl_core_local_block *)gpr_malloc_aligned(
+ g_log.num_cores * sizeof(cl_core_local_block), GPR_CACHELINE_SIZE_LOG);
+ memset(g_log.core_local_blocks, 0,
+ g_log.num_cores * sizeof(cl_core_local_block));
+ g_log.blocks = (cl_block *)gpr_malloc_aligned(
+ g_log.num_blocks * sizeof(cl_block), GPR_CACHELINE_SIZE_LOG);
+ memset(g_log.blocks, 0, g_log.num_blocks * sizeof(cl_block));
+ g_log.buffer = gpr_malloc(g_log.num_blocks * CENSUS_LOG_MAX_RECORD_SIZE);
+ memset(g_log.buffer, 0, g_log.num_blocks * CENSUS_LOG_MAX_RECORD_SIZE);
+ cl_block_list_initialize(&g_log.free_block_list);
+ cl_block_list_initialize(&g_log.dirty_block_list);
+ for (ix = 0; ix < g_log.num_blocks; ++ix) {
+ cl_block *block = g_log.blocks + ix;
+ cl_block_initialize(block,
+ g_log.buffer + (CENSUS_LOG_MAX_RECORD_SIZE * ix));
+ cl_block_try_disable_access(block, 1 /* discard data */);
+ cl_block_list_insert_at_tail(&g_log.free_block_list, block);
+ }
+ gpr_atm_rel_store(&g_log.out_of_space_count, 0);
g_log.initialized = 1;
}
-void
-census_log_shutdown (void)
-{
- GPR_ASSERT (g_log.initialized);
- gpr_mu_destroy (&g_log.lock);
- gpr_free_aligned (g_log.core_local_blocks);
+void census_log_shutdown(void) {
+ GPR_ASSERT(g_log.initialized);
+ gpr_mu_destroy(&g_log.lock);
+ gpr_free_aligned(g_log.core_local_blocks);
g_log.core_local_blocks = NULL;
- gpr_free_aligned (g_log.blocks);
+ gpr_free_aligned(g_log.blocks);
g_log.blocks = NULL;
- gpr_free (g_log.buffer);
+ gpr_free(g_log.buffer);
g_log.buffer = NULL;
g_log.initialized = 0;
}
-void *
-census_log_start_write (size_t size)
-{
+void *census_log_start_write(size_t size) {
/* Used to bound number of times block allocation is attempted. */
gpr_int32 attempts_remaining = g_log.num_blocks;
/* TODO(aveitch): move this inside the do loop when current_cpu is fixed */
- gpr_int32 core_id = gpr_cpu_current_cpu ();
- GPR_ASSERT (g_log.initialized);
- if (size > CENSUS_LOG_MAX_RECORD_SIZE)
- {
+ gpr_int32 core_id = gpr_cpu_current_cpu();
+ GPR_ASSERT(g_log.initialized);
+ if (size > CENSUS_LOG_MAX_RECORD_SIZE) {
+ return NULL;
+ }
+ do {
+ int allocated;
+ void *record = NULL;
+ cl_block *block =
+ cl_core_local_block_get_block(&g_log.core_local_blocks[core_id]);
+ if (block && (record = cl_block_start_write(block, size))) {
+ return record;
+ }
+ /* Need to allocate a new block. We are here if:
+ - No block associated with the core OR
+ - Write in-progress on the block OR
+ - block is out of space */
+ if (gpr_atm_acq_load(&g_log.is_full)) {
+ gpr_atm_no_barrier_fetch_add(&g_log.out_of_space_count, 1);
return NULL;
}
- do
- {
- int allocated;
- void *record = NULL;
- cl_block *block = cl_core_local_block_get_block (&g_log.core_local_blocks[core_id]);
- if (block && (record = cl_block_start_write (block, size)))
- {
- return record;
- }
- /* Need to allocate a new block. We are here if:
- - No block associated with the core OR
- - Write in-progress on the block OR
- - block is out of space */
- if (gpr_atm_acq_load (&g_log.is_full))
- {
- gpr_atm_no_barrier_fetch_add (&g_log.out_of_space_count, 1);
- return NULL;
- }
- gpr_mu_lock (&g_log.lock);
- allocated = cl_allocate_core_local_block (core_id, block);
- gpr_mu_unlock (&g_log.lock);
- if (!allocated)
- {
- gpr_atm_no_barrier_fetch_add (&g_log.out_of_space_count, 1);
- return NULL;
- }
+ gpr_mu_lock(&g_log.lock);
+ allocated = cl_allocate_core_local_block(core_id, block);
+ gpr_mu_unlock(&g_log.lock);
+ if (!allocated) {
+ gpr_atm_no_barrier_fetch_add(&g_log.out_of_space_count, 1);
+ return NULL;
}
- while (attempts_remaining--);
+ } while (attempts_remaining--);
/* Give up. */
- gpr_atm_no_barrier_fetch_add (&g_log.out_of_space_count, 1);
+ gpr_atm_no_barrier_fetch_add(&g_log.out_of_space_count, 1);
return NULL;
}
-void
-census_log_end_write (void *record, size_t bytes_written)
-{
- GPR_ASSERT (g_log.initialized);
- cl_block_end_write (cl_get_block (record), bytes_written);
+void census_log_end_write(void *record, size_t bytes_written) {
+ GPR_ASSERT(g_log.initialized);
+ cl_block_end_write(cl_get_block(record), bytes_written);
}
-void
-census_log_init_reader (void)
-{
- GPR_ASSERT (g_log.initialized);
- gpr_mu_lock (&g_log.lock);
+void census_log_init_reader(void) {
+ GPR_ASSERT(g_log.initialized);
+ gpr_mu_lock(&g_log.lock);
/* If a block is locked for reading unlock it. */
- if (g_log.block_being_read != NULL)
- {
- cl_block_end_read (g_log.block_being_read);
- g_log.block_being_read = NULL;
- }
+ if (g_log.block_being_read != NULL) {
+ cl_block_end_read(g_log.block_being_read);
+ g_log.block_being_read = NULL;
+ }
g_log.read_iterator_state = g_log.num_cores;
- gpr_mu_unlock (&g_log.lock);
-}
-
-const void *
-census_log_read_next (size_t * bytes_available)
-{
- GPR_ASSERT (g_log.initialized);
- gpr_mu_lock (&g_log.lock);
- if (g_log.block_being_read != NULL)
- {
- cl_block_end_read (g_log.block_being_read);
- }
- do
- {
- g_log.block_being_read = cl_next_block_to_read (g_log.block_being_read);
- if (g_log.block_being_read != NULL)
- {
- void *record = cl_block_start_read (g_log.block_being_read, bytes_available);
- if (record != NULL)
- {
- gpr_mu_unlock (&g_log.lock);
- return record;
- }
- }
+ gpr_mu_unlock(&g_log.lock);
+}
+
+const void *census_log_read_next(size_t *bytes_available) {
+ GPR_ASSERT(g_log.initialized);
+ gpr_mu_lock(&g_log.lock);
+ if (g_log.block_being_read != NULL) {
+ cl_block_end_read(g_log.block_being_read);
+ }
+ do {
+ g_log.block_being_read = cl_next_block_to_read(g_log.block_being_read);
+ if (g_log.block_being_read != NULL) {
+ void *record =
+ cl_block_start_read(g_log.block_being_read, bytes_available);
+ if (record != NULL) {
+ gpr_mu_unlock(&g_log.lock);
+ return record;
+ }
}
- while (g_log.block_being_read != NULL);
- gpr_mu_unlock (&g_log.lock);
+ } while (g_log.block_being_read != NULL);
+ gpr_mu_unlock(&g_log.lock);
return NULL;
}
-size_t
-census_log_remaining_space (void)
-{
+size_t census_log_remaining_space(void) {
size_t space;
- GPR_ASSERT (g_log.initialized);
- gpr_mu_lock (&g_log.lock);
- if (g_log.discard_old_records)
- {
- /* Remaining space is not meaningful; just return the entire log space. */
- space = g_log.num_blocks << CENSUS_LOG_2_MAX_RECORD_SIZE;
- }
- else
- {
- space = g_log.free_block_list.count * CENSUS_LOG_MAX_RECORD_SIZE;
- }
- gpr_mu_unlock (&g_log.lock);
+ GPR_ASSERT(g_log.initialized);
+ gpr_mu_lock(&g_log.lock);
+ if (g_log.discard_old_records) {
+ /* Remaining space is not meaningful; just return the entire log space. */
+ space = g_log.num_blocks << CENSUS_LOG_2_MAX_RECORD_SIZE;
+ } else {
+ space = g_log.free_block_list.count * CENSUS_LOG_MAX_RECORD_SIZE;
+ }
+ gpr_mu_unlock(&g_log.lock);
return space;
}
-int
-census_log_out_of_space_count (void)
-{
- GPR_ASSERT (g_log.initialized);
- return gpr_atm_acq_load (&g_log.out_of_space_count);
+int census_log_out_of_space_count(void) {
+ GPR_ASSERT(g_log.initialized);
+ return gpr_atm_acq_load(&g_log.out_of_space_count);
}
diff --git a/src/core/statistics/census_log.h b/src/core/statistics/census_log.h
index 3c0c2dc52d..356437c346 100644
--- a/src/core/statistics/census_log.h
+++ b/src/core/statistics/census_log.h
@@ -37,7 +37,7 @@
#include <stddef.h>
/* Maximum record size, in bytes. */
-#define CENSUS_LOG_2_MAX_RECORD_SIZE 14 /* 2^14 = 16KB */
+#define CENSUS_LOG_2_MAX_RECORD_SIZE 14 /* 2^14 = 16KB */
#define CENSUS_LOG_MAX_RECORD_SIZE (1 << CENSUS_LOG_2_MAX_RECORD_SIZE)
/* Initialize the statistics logging subsystem with the given log size. A log
@@ -47,13 +47,13 @@
when the log is full. This function must be called before any other
census_log functions.
*/
-void census_log_initialize (size_t size_in_mb, int discard_old_records);
+void census_log_initialize(size_t size_in_mb, int discard_old_records);
/* Shutdown the logging subsystem. Caller must ensure that:
- no in progress or future call to any census_log functions
- no incomplete records
*/
-void census_log_shutdown (void);
+void census_log_shutdown(void);
/* Allocates and returns a 'size' bytes record and marks it in use. A
subsequent census_log_end_write() marks the record complete. The
@@ -62,9 +62,9 @@ void census_log_shutdown (void);
- log is configured to keep old records OR
- all blocks are pinned by incomplete records.
*/
-void *census_log_start_write (size_t size);
+void *census_log_start_write(size_t size);
-void census_log_end_write (void *record, size_t bytes_written);
+void census_log_end_write(void *record, size_t bytes_written);
/* census_log_read_next() iterates over blocks with data and for each block
returns a pointer to the first unread byte. The number of bytes that can be
@@ -74,18 +74,18 @@ void census_log_end_write (void *record, size_t bytes_written);
is read. census_log_init_reader() starts the iteration or aborts the
current iteration.
*/
-void census_log_init_reader (void);
-const void *census_log_read_next (size_t * bytes_available);
+void census_log_init_reader(void);
+const void *census_log_read_next(size_t *bytes_available);
/* Returns estimated remaining space across all blocks, in bytes. If log is
configured to discard old records, returns total log space. Otherwise,
returns space available in empty blocks (partially filled blocks are
treated as full).
*/
-size_t census_log_remaining_space (void);
+size_t census_log_remaining_space(void);
/* Returns the number of times gprc_stats_log_start_write() failed due to
out-of-space. */
-int census_log_out_of_space_count (void);
+int census_log_out_of_space_count(void);
#endif /* GRPC_INTERNAL_CORE_STATISTICS_CENSUS_LOG_H */
diff --git a/src/core/statistics/census_rpc_stats.c b/src/core/statistics/census_rpc_stats.c
index 87e05e19f2..ba2c81d6a3 100644
--- a/src/core/statistics/census_rpc_stats.c
+++ b/src/core/statistics/census_rpc_stats.c
@@ -59,62 +59,43 @@ static gpr_mu g_mu;
static census_ht *g_client_stats_store = NULL;
static census_ht *g_server_stats_store = NULL;
-static void
-init_mutex (void)
-{
- gpr_mu_init (&g_mu);
-}
+static void init_mutex(void) { gpr_mu_init(&g_mu); }
-static void
-init_mutex_once (void)
-{
- gpr_once_init (&g_stats_store_mu_init, init_mutex);
+static void init_mutex_once(void) {
+ gpr_once_init(&g_stats_store_mu_init, init_mutex);
}
-static int
-cmp_str_keys (const void *k1, const void *k2)
-{
- return strcmp ((const char *) k1, (const char *) k2);
+static int cmp_str_keys(const void *k1, const void *k2) {
+ return strcmp((const char *)k1, (const char *)k2);
}
/* TODO(hongyu): replace it with cityhash64 */
-static gpr_uint64
-simple_hash (const void *k)
-{
- size_t len = strlen (k);
- gpr_uint64 higher = gpr_murmur_hash3 ((const char *) k, len / 2, 0);
- return higher << 32 | gpr_murmur_hash3 ((const char *) k + len / 2, len - len / 2, 0);
+static gpr_uint64 simple_hash(const void *k) {
+ size_t len = strlen(k);
+ gpr_uint64 higher = gpr_murmur_hash3((const char *)k, len / 2, 0);
+ return higher << 32 |
+ gpr_murmur_hash3((const char *)k + len / 2, len - len / 2, 0);
}
-static void
-delete_stats (void *stats)
-{
- census_window_stats_destroy ((struct census_window_stats *) stats);
+static void delete_stats(void *stats) {
+ census_window_stats_destroy((struct census_window_stats *)stats);
}
-static void
-delete_key (void *key)
-{
- gpr_free (key);
-}
+static void delete_key(void *key) { gpr_free(key); }
static const census_ht_option ht_opt = {
- CENSUS_HT_POINTER /* key type */ , 1999 /* n_of_buckets */ ,
- simple_hash /* hash function */ , cmp_str_keys /* key comparator */ ,
- delete_stats /* data deleter */ , delete_key /* key deleter */
+ CENSUS_HT_POINTER /* key type */, 1999 /* n_of_buckets */,
+ simple_hash /* hash function */, cmp_str_keys /* key comparator */,
+ delete_stats /* data deleter */, delete_key /* key deleter */
};
-static void
-init_rpc_stats (void *stats)
-{
- memset (stats, 0, sizeof (census_rpc_stats));
+static void init_rpc_stats(void *stats) {
+ memset(stats, 0, sizeof(census_rpc_stats));
}
-static void
-stat_add_proportion (double p, void *base, const void *addme)
-{
- census_rpc_stats *b = (census_rpc_stats *) base;
- census_rpc_stats *a = (census_rpc_stats *) addme;
+static void stat_add_proportion(double p, void *base, const void *addme) {
+ census_rpc_stats *b = (census_rpc_stats *)base;
+ census_rpc_stats *a = (census_rpc_stats *)addme;
b->cnt += p * a->cnt;
b->rpc_error_cnt += p * a->rpc_error_cnt;
b->app_error_cnt += p * a->app_error_cnt;
@@ -125,186 +106,148 @@ stat_add_proportion (double p, void *base, const void *addme)
b->wire_response_bytes += p * a->wire_response_bytes;
}
-static void
-stat_add (void *base, const void *addme)
-{
- stat_add_proportion (1.0, base, addme);
+static void stat_add(void *base, const void *addme) {
+ stat_add_proportion(1.0, base, addme);
}
static gpr_timespec min_hour_total_intervals[3] = {
- {60, 0}, {3600, 0}, {36000000, 0}
-};
+ {60, 0}, {3600, 0}, {36000000, 0}};
static const census_window_stats_stat_info window_stats_settings = {
- sizeof (census_rpc_stats), init_rpc_stats, stat_add, stat_add_proportion
-};
+ sizeof(census_rpc_stats), init_rpc_stats, stat_add, stat_add_proportion};
-census_rpc_stats *
-census_rpc_stats_create_empty (void)
-{
- census_rpc_stats *ret = (census_rpc_stats *) gpr_malloc (sizeof (census_rpc_stats));
- memset (ret, 0, sizeof (census_rpc_stats));
+census_rpc_stats *census_rpc_stats_create_empty(void) {
+ census_rpc_stats *ret =
+ (census_rpc_stats *)gpr_malloc(sizeof(census_rpc_stats));
+ memset(ret, 0, sizeof(census_rpc_stats));
return ret;
}
-void
-census_aggregated_rpc_stats_set_empty (census_aggregated_rpc_stats * data)
-{
+void census_aggregated_rpc_stats_set_empty(census_aggregated_rpc_stats *data) {
int i = 0;
- for (i = 0; i < data->num_entries; i++)
- {
- if (data->stats[i].method != NULL)
- {
- gpr_free ((void *) data->stats[i].method);
- }
- }
- if (data->stats != NULL)
- {
- gpr_free (data->stats);
+ for (i = 0; i < data->num_entries; i++) {
+ if (data->stats[i].method != NULL) {
+ gpr_free((void *)data->stats[i].method);
}
+ }
+ if (data->stats != NULL) {
+ gpr_free(data->stats);
+ }
data->num_entries = 0;
data->stats = NULL;
}
-static void
-record_stats (census_ht * store, census_op_id op_id, const census_rpc_stats * stats)
-{
- gpr_mu_lock (&g_mu);
- if (store != NULL)
- {
- census_trace_obj *trace = NULL;
- census_internal_lock_trace_store ();
- trace = census_get_trace_obj_locked (op_id);
- if (trace != NULL)
- {
- const char *method_name = census_get_trace_method_name (trace);
- struct census_window_stats *window_stats = NULL;
- census_ht_key key;
- key.ptr = (void *) method_name;
- window_stats = census_ht_find (store, key);
- census_internal_unlock_trace_store ();
- if (window_stats == NULL)
- {
- window_stats = census_window_stats_create (3, min_hour_total_intervals, 30, &window_stats_settings);
- key.ptr = gpr_strdup (key.ptr);
- census_ht_insert (store, key, (void *) window_stats);
- }
- census_window_stats_add (window_stats, gpr_now (GPR_CLOCK_REALTIME), stats);
- }
- else
- {
- census_internal_unlock_trace_store ();
- }
+static void record_stats(census_ht *store, census_op_id op_id,
+ const census_rpc_stats *stats) {
+ gpr_mu_lock(&g_mu);
+ if (store != NULL) {
+ census_trace_obj *trace = NULL;
+ census_internal_lock_trace_store();
+ trace = census_get_trace_obj_locked(op_id);
+ if (trace != NULL) {
+ const char *method_name = census_get_trace_method_name(trace);
+ struct census_window_stats *window_stats = NULL;
+ census_ht_key key;
+ key.ptr = (void *)method_name;
+ window_stats = census_ht_find(store, key);
+ census_internal_unlock_trace_store();
+ if (window_stats == NULL) {
+ window_stats = census_window_stats_create(3, min_hour_total_intervals,
+ 30, &window_stats_settings);
+ key.ptr = gpr_strdup(key.ptr);
+ census_ht_insert(store, key, (void *)window_stats);
+ }
+ census_window_stats_add(window_stats, gpr_now(GPR_CLOCK_REALTIME), stats);
+ } else {
+ census_internal_unlock_trace_store();
}
- gpr_mu_unlock (&g_mu);
+ }
+ gpr_mu_unlock(&g_mu);
}
-void
-census_record_rpc_client_stats (census_op_id op_id, const census_rpc_stats * stats)
-{
- record_stats (g_client_stats_store, op_id, stats);
+void census_record_rpc_client_stats(census_op_id op_id,
+ const census_rpc_stats *stats) {
+ record_stats(g_client_stats_store, op_id, stats);
}
-void
-census_record_rpc_server_stats (census_op_id op_id, const census_rpc_stats * stats)
-{
- record_stats (g_server_stats_store, op_id, stats);
+void census_record_rpc_server_stats(census_op_id op_id,
+ const census_rpc_stats *stats) {
+ record_stats(g_server_stats_store, op_id, stats);
}
/* Get stats from input stats store */
-static void
-get_stats (census_ht * store, census_aggregated_rpc_stats * data)
-{
- GPR_ASSERT (data != NULL);
- if (data->num_entries != 0)
- {
- census_aggregated_rpc_stats_set_empty (data);
- }
- gpr_mu_lock (&g_mu);
- if (store != NULL)
- {
- size_t n;
- unsigned i, j;
- gpr_timespec now = gpr_now (GPR_CLOCK_REALTIME);
- census_ht_kv *kv = census_ht_get_all_elements (store, &n);
- if (kv != NULL)
- {
- data->num_entries = n;
- data->stats = (per_method_stats *) gpr_malloc (sizeof (per_method_stats) * n);
- for (i = 0; i < n; i++)
- {
- census_window_stats_sums sums[NUM_INTERVALS];
- for (j = 0; j < NUM_INTERVALS; j++)
- {
- sums[j].statistic = (void *) census_rpc_stats_create_empty ();
- }
- data->stats[i].method = gpr_strdup (kv[i].k.ptr);
- census_window_stats_get_sums (kv[i].v, now, sums);
- data->stats[i].minute_stats = *(census_rpc_stats *) sums[MINUTE_INTERVAL].statistic;
- data->stats[i].hour_stats = *(census_rpc_stats *) sums[HOUR_INTERVAL].statistic;
- data->stats[i].total_stats = *(census_rpc_stats *) sums[TOTAL_INTERVAL].statistic;
- for (j = 0; j < NUM_INTERVALS; j++)
- {
- gpr_free (sums[j].statistic);
- }
- }
- gpr_free (kv);
- }
- }
- gpr_mu_unlock (&g_mu);
-}
-
-void
-census_get_client_stats (census_aggregated_rpc_stats * data)
-{
- get_stats (g_client_stats_store, data);
-}
-
-void
-census_get_server_stats (census_aggregated_rpc_stats * data)
-{
- get_stats (g_server_stats_store, data);
-}
-
-void
-census_stats_store_init (void)
-{
- init_mutex_once ();
- gpr_mu_lock (&g_mu);
- if (g_client_stats_store == NULL && g_server_stats_store == NULL)
- {
- g_client_stats_store = census_ht_create (&ht_opt);
- g_server_stats_store = census_ht_create (&ht_opt);
- }
- else
- {
- gpr_log (GPR_ERROR, "Census stats store already initialized.");
- }
- gpr_mu_unlock (&g_mu);
-}
-
-void
-census_stats_store_shutdown (void)
-{
- init_mutex_once ();
- gpr_mu_lock (&g_mu);
- if (g_client_stats_store != NULL)
- {
- census_ht_destroy (g_client_stats_store);
- g_client_stats_store = NULL;
- }
- else
- {
- gpr_log (GPR_ERROR, "Census server stats store not initialized.");
- }
- if (g_server_stats_store != NULL)
- {
- census_ht_destroy (g_server_stats_store);
- g_server_stats_store = NULL;
- }
- else
- {
- gpr_log (GPR_ERROR, "Census client stats store not initialized.");
+static void get_stats(census_ht *store, census_aggregated_rpc_stats *data) {
+ GPR_ASSERT(data != NULL);
+ if (data->num_entries != 0) {
+ census_aggregated_rpc_stats_set_empty(data);
+ }
+ gpr_mu_lock(&g_mu);
+ if (store != NULL) {
+ size_t n;
+ unsigned i, j;
+ gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
+ census_ht_kv *kv = census_ht_get_all_elements(store, &n);
+ if (kv != NULL) {
+ data->num_entries = n;
+ data->stats =
+ (per_method_stats *)gpr_malloc(sizeof(per_method_stats) * n);
+ for (i = 0; i < n; i++) {
+ census_window_stats_sums sums[NUM_INTERVALS];
+ for (j = 0; j < NUM_INTERVALS; j++) {
+ sums[j].statistic = (void *)census_rpc_stats_create_empty();
+ }
+ data->stats[i].method = gpr_strdup(kv[i].k.ptr);
+ census_window_stats_get_sums(kv[i].v, now, sums);
+ data->stats[i].minute_stats =
+ *(census_rpc_stats *)sums[MINUTE_INTERVAL].statistic;
+ data->stats[i].hour_stats =
+ *(census_rpc_stats *)sums[HOUR_INTERVAL].statistic;
+ data->stats[i].total_stats =
+ *(census_rpc_stats *)sums[TOTAL_INTERVAL].statistic;
+ for (j = 0; j < NUM_INTERVALS; j++) {
+ gpr_free(sums[j].statistic);
+ }
+ }
+ gpr_free(kv);
}
- gpr_mu_unlock (&g_mu);
+ }
+ gpr_mu_unlock(&g_mu);
+}
+
+void census_get_client_stats(census_aggregated_rpc_stats *data) {
+ get_stats(g_client_stats_store, data);
+}
+
+void census_get_server_stats(census_aggregated_rpc_stats *data) {
+ get_stats(g_server_stats_store, data);
+}
+
+void census_stats_store_init(void) {
+ init_mutex_once();
+ gpr_mu_lock(&g_mu);
+ if (g_client_stats_store == NULL && g_server_stats_store == NULL) {
+ g_client_stats_store = census_ht_create(&ht_opt);
+ g_server_stats_store = census_ht_create(&ht_opt);
+ } else {
+ gpr_log(GPR_ERROR, "Census stats store already initialized.");
+ }
+ gpr_mu_unlock(&g_mu);
+}
+
+void census_stats_store_shutdown(void) {
+ init_mutex_once();
+ gpr_mu_lock(&g_mu);
+ if (g_client_stats_store != NULL) {
+ census_ht_destroy(g_client_stats_store);
+ g_client_stats_store = NULL;
+ } else {
+ gpr_log(GPR_ERROR, "Census server stats store not initialized.");
+ }
+ if (g_server_stats_store != NULL) {
+ census_ht_destroy(g_server_stats_store);
+ g_server_stats_store = NULL;
+ } else {
+ gpr_log(GPR_ERROR, "Census client stats store not initialized.");
+ }
+ gpr_mu_unlock(&g_mu);
}
diff --git a/src/core/statistics/census_rpc_stats.h b/src/core/statistics/census_rpc_stats.h
index 7119699f6e..5edbe9f478 100644
--- a/src/core/statistics/census_rpc_stats.h
+++ b/src/core/statistics/census_rpc_stats.h
@@ -38,47 +38,45 @@
#include <grpc/support/port_platform.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
- struct census_rpc_stats
- {
- gpr_uint64 cnt;
- gpr_uint64 rpc_error_cnt;
- gpr_uint64 app_error_cnt;
- double elapsed_time_ms;
- double api_request_bytes;
- double wire_request_bytes;
- double api_response_bytes;
- double wire_response_bytes;
- };
+struct census_rpc_stats {
+ gpr_uint64 cnt;
+ gpr_uint64 rpc_error_cnt;
+ gpr_uint64 app_error_cnt;
+ double elapsed_time_ms;
+ double api_request_bytes;
+ double wire_request_bytes;
+ double api_response_bytes;
+ double wire_response_bytes;
+};
/* Creates an empty rpc stats object on heap. */
- census_rpc_stats *census_rpc_stats_create_empty (void);
-
- typedef struct census_per_method_rpc_stats
- {
- const char *method;
- census_rpc_stats minute_stats; /* cumulative stats in the past minute */
- census_rpc_stats hour_stats; /* cumulative stats in the past hour */
- census_rpc_stats total_stats; /* cumulative stats from last gc */
- } census_per_method_rpc_stats;
-
- typedef struct census_aggregated_rpc_stats
- {
- int num_entries;
- census_per_method_rpc_stats *stats;
- } census_aggregated_rpc_stats;
+census_rpc_stats *census_rpc_stats_create_empty(void);
+
+typedef struct census_per_method_rpc_stats {
+ const char *method;
+ census_rpc_stats minute_stats; /* cumulative stats in the past minute */
+ census_rpc_stats hour_stats; /* cumulative stats in the past hour */
+ census_rpc_stats total_stats; /* cumulative stats from last gc */
+} census_per_method_rpc_stats;
+
+typedef struct census_aggregated_rpc_stats {
+ int num_entries;
+ census_per_method_rpc_stats *stats;
+} census_aggregated_rpc_stats;
/* Initializes an aggregated rpc stats object to an empty state. */
- void census_aggregated_rpc_stats_set_empty (census_aggregated_rpc_stats * data);
+void census_aggregated_rpc_stats_set_empty(census_aggregated_rpc_stats *data);
/* Records client side stats of a rpc. */
- void census_record_rpc_client_stats (census_op_id op_id, const census_rpc_stats * stats);
+void census_record_rpc_client_stats(census_op_id op_id,
+ const census_rpc_stats *stats);
/* Records server side stats of a rpc. */
- void census_record_rpc_server_stats (census_op_id op_id, const census_rpc_stats * stats);
+void census_record_rpc_server_stats(census_op_id op_id,
+ const census_rpc_stats *stats);
/* The following two functions are intended for inprocess query of
per-service per-method stats from grpc implementations. */
@@ -86,18 +84,18 @@ extern "C"
/* Populates *data_map with server side aggregated per-service per-method
stats.
DO NOT CALL from outside of grpc code. */
- void census_get_server_stats (census_aggregated_rpc_stats * data_map);
+void census_get_server_stats(census_aggregated_rpc_stats *data_map);
/* Populates *data_map with client side aggregated per-service per-method
stats.
DO NOT CALL from outside of grpc code. */
- void census_get_client_stats (census_aggregated_rpc_stats * data_map);
+void census_get_client_stats(census_aggregated_rpc_stats *data_map);
- void census_stats_store_init (void);
- void census_stats_store_shutdown (void);
+void census_stats_store_init(void);
+void census_stats_store_shutdown(void);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_STATISTICS_CENSUS_RPC_STATS_H */
+#endif /* GRPC_INTERNAL_CORE_STATISTICS_CENSUS_RPC_STATS_H */
diff --git a/src/core/statistics/census_tracing.c b/src/core/statistics/census_tracing.c
index f914cae1b0..0eeecfe6c8 100644
--- a/src/core/statistics/census_tracing.c
+++ b/src/core/statistics/census_tracing.c
@@ -44,255 +44,198 @@
#include <grpc/support/port_platform.h>
#include <grpc/support/sync.h>
-void
-census_trace_obj_destroy (census_trace_obj * obj)
-{
+void census_trace_obj_destroy(census_trace_obj *obj) {
census_trace_annotation *p = obj->annotations;
- while (p != NULL)
- {
- census_trace_annotation *next = p->next;
- gpr_free (p);
- p = next;
- }
- gpr_free (obj->method);
- gpr_free (obj);
+ while (p != NULL) {
+ census_trace_annotation *next = p->next;
+ gpr_free(p);
+ p = next;
+ }
+ gpr_free(obj->method);
+ gpr_free(obj);
}
-static void
-delete_trace_obj (void *obj)
-{
- census_trace_obj_destroy ((census_trace_obj *) obj);
+static void delete_trace_obj(void *obj) {
+ census_trace_obj_destroy((census_trace_obj *)obj);
}
static const census_ht_option ht_opt = {
- CENSUS_HT_UINT64 /* key type */ ,
- 571 /* n_of_buckets */ ,
- NULL /* hash */ ,
- NULL /* compare_keys */ ,
- delete_trace_obj /* delete data */ ,
- NULL /* delete key */
+ CENSUS_HT_UINT64 /* key type */,
+ 571 /* n_of_buckets */,
+ NULL /* hash */,
+ NULL /* compare_keys */,
+ delete_trace_obj /* delete data */,
+ NULL /* delete key */
};
static gpr_once g_init_mutex_once = GPR_ONCE_INIT;
-static gpr_mu g_mu; /* Guards following two static variables. */
+static gpr_mu g_mu; /* Guards following two static variables. */
static census_ht *g_trace_store = NULL;
static gpr_uint64 g_id = 0;
-static census_ht_key
-op_id_as_key (census_op_id * id)
-{
- return *(census_ht_key *) id;
+static census_ht_key op_id_as_key(census_op_id *id) {
+ return *(census_ht_key *)id;
}
-static gpr_uint64
-op_id_2_uint64 (census_op_id * id)
-{
+static gpr_uint64 op_id_2_uint64(census_op_id *id) {
gpr_uint64 ret;
- memcpy (&ret, id, sizeof (census_op_id));
+ memcpy(&ret, id, sizeof(census_op_id));
return ret;
}
-static void
-init_mutex (void)
-{
- gpr_mu_init (&g_mu);
-}
+static void init_mutex(void) { gpr_mu_init(&g_mu); }
-static void
-init_mutex_once (void)
-{
- gpr_once_init (&g_init_mutex_once, init_mutex);
+static void init_mutex_once(void) {
+ gpr_once_init(&g_init_mutex_once, init_mutex);
}
-census_op_id
-census_tracing_start_op (void)
-{
- gpr_mu_lock (&g_mu);
+census_op_id census_tracing_start_op(void) {
+ gpr_mu_lock(&g_mu);
{
- census_trace_obj *ret = gpr_malloc (sizeof (census_trace_obj));
- memset (ret, 0, sizeof (census_trace_obj));
+ census_trace_obj *ret = gpr_malloc(sizeof(census_trace_obj));
+ memset(ret, 0, sizeof(census_trace_obj));
g_id++;
- memcpy (&ret->id, &g_id, sizeof (census_op_id));
+ memcpy(&ret->id, &g_id, sizeof(census_op_id));
ret->rpc_stats.cnt = 1;
- ret->ts = gpr_now (GPR_CLOCK_REALTIME);
- census_ht_insert (g_trace_store, op_id_as_key (&ret->id), (void *) ret);
- gpr_log (GPR_DEBUG, "Start tracing for id %lu", g_id);
- gpr_mu_unlock (&g_mu);
+ ret->ts = gpr_now(GPR_CLOCK_REALTIME);
+ census_ht_insert(g_trace_store, op_id_as_key(&ret->id), (void *)ret);
+ gpr_log(GPR_DEBUG, "Start tracing for id %lu", g_id);
+ gpr_mu_unlock(&g_mu);
return ret->id;
}
}
-int
-census_add_method_tag (census_op_id op_id, const char *method)
-{
+int census_add_method_tag(census_op_id op_id, const char *method) {
int ret = 0;
census_trace_obj *trace = NULL;
- gpr_mu_lock (&g_mu);
- trace = census_ht_find (g_trace_store, op_id_as_key (&op_id));
- if (trace == NULL)
- {
- ret = 1;
- }
- else
- {
- trace->method = gpr_strdup (method);
- }
- gpr_mu_unlock (&g_mu);
+ gpr_mu_lock(&g_mu);
+ trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
+ if (trace == NULL) {
+ ret = 1;
+ } else {
+ trace->method = gpr_strdup(method);
+ }
+ gpr_mu_unlock(&g_mu);
return ret;
}
-void
-census_tracing_print (census_op_id op_id, const char *anno_txt)
-{
+void census_tracing_print(census_op_id op_id, const char *anno_txt) {
census_trace_obj *trace = NULL;
- gpr_mu_lock (&g_mu);
- trace = census_ht_find (g_trace_store, op_id_as_key (&op_id));
- if (trace != NULL)
+ gpr_mu_lock(&g_mu);
+ trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
+ if (trace != NULL) {
+ census_trace_annotation *anno = gpr_malloc(sizeof(census_trace_annotation));
+ anno->ts = gpr_now(GPR_CLOCK_REALTIME);
{
- census_trace_annotation *anno = gpr_malloc (sizeof (census_trace_annotation));
- anno->ts = gpr_now (GPR_CLOCK_REALTIME);
- {
- char *d = anno->txt;
- const char *s = anno_txt;
- int n = 0;
- for (; n < CENSUS_MAX_ANNOTATION_LENGTH && *s != '\0'; ++n)
- {
- *d++ = *s++;
- }
- *d = '\0';
+ char *d = anno->txt;
+ const char *s = anno_txt;
+ int n = 0;
+ for (; n < CENSUS_MAX_ANNOTATION_LENGTH && *s != '\0'; ++n) {
+ *d++ = *s++;
}
- anno->next = trace->annotations;
- trace->annotations = anno;
+ *d = '\0';
}
- gpr_mu_unlock (&g_mu);
+ anno->next = trace->annotations;
+ trace->annotations = anno;
+ }
+ gpr_mu_unlock(&g_mu);
}
-void
-census_tracing_end_op (census_op_id op_id)
-{
+void census_tracing_end_op(census_op_id op_id) {
census_trace_obj *trace = NULL;
- gpr_mu_lock (&g_mu);
- trace = census_ht_find (g_trace_store, op_id_as_key (&op_id));
- if (trace != NULL)
- {
- trace->rpc_stats.elapsed_time_ms = gpr_timespec_to_micros (gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), trace->ts));
- gpr_log (GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us", op_id_2_uint64 (&op_id), trace->method, trace->rpc_stats.elapsed_time_ms);
- census_ht_erase (g_trace_store, op_id_as_key (&op_id));
- }
- gpr_mu_unlock (&g_mu);
+ gpr_mu_lock(&g_mu);
+ trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
+ if (trace != NULL) {
+ trace->rpc_stats.elapsed_time_ms = gpr_timespec_to_micros(
+ gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), trace->ts));
+ gpr_log(GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us",
+ op_id_2_uint64(&op_id), trace->method,
+ trace->rpc_stats.elapsed_time_ms);
+ census_ht_erase(g_trace_store, op_id_as_key(&op_id));
+ }
+ gpr_mu_unlock(&g_mu);
}
-void
-census_tracing_init (void)
-{
- init_mutex_once ();
- gpr_mu_lock (&g_mu);
- if (g_trace_store == NULL)
- {
- g_id = 1;
- g_trace_store = census_ht_create (&ht_opt);
- }
- else
- {
- gpr_log (GPR_ERROR, "Census trace store already initialized.");
- }
- gpr_mu_unlock (&g_mu);
+void census_tracing_init(void) {
+ init_mutex_once();
+ gpr_mu_lock(&g_mu);
+ if (g_trace_store == NULL) {
+ g_id = 1;
+ g_trace_store = census_ht_create(&ht_opt);
+ } else {
+ gpr_log(GPR_ERROR, "Census trace store already initialized.");
+ }
+ gpr_mu_unlock(&g_mu);
}
-void
-census_tracing_shutdown (void)
-{
- gpr_mu_lock (&g_mu);
- if (g_trace_store != NULL)
- {
- census_ht_destroy (g_trace_store);
- g_trace_store = NULL;
- }
- else
- {
- gpr_log (GPR_ERROR, "Census trace store is not initialized.");
- }
- gpr_mu_unlock (&g_mu);
+void census_tracing_shutdown(void) {
+ gpr_mu_lock(&g_mu);
+ if (g_trace_store != NULL) {
+ census_ht_destroy(g_trace_store);
+ g_trace_store = NULL;
+ } else {
+ gpr_log(GPR_ERROR, "Census trace store is not initialized.");
+ }
+ gpr_mu_unlock(&g_mu);
}
-void
-census_internal_lock_trace_store (void)
-{
- gpr_mu_lock (&g_mu);
-}
+void census_internal_lock_trace_store(void) { gpr_mu_lock(&g_mu); }
-void
-census_internal_unlock_trace_store (void)
-{
- gpr_mu_unlock (&g_mu);
-}
+void census_internal_unlock_trace_store(void) { gpr_mu_unlock(&g_mu); }
-census_trace_obj *
-census_get_trace_obj_locked (census_op_id op_id)
-{
- if (g_trace_store == NULL)
- {
- gpr_log (GPR_ERROR, "Census trace store is not initialized.");
- return NULL;
- }
- return (census_trace_obj *) census_ht_find (g_trace_store, op_id_as_key (&op_id));
+census_trace_obj *census_get_trace_obj_locked(census_op_id op_id) {
+ if (g_trace_store == NULL) {
+ gpr_log(GPR_ERROR, "Census trace store is not initialized.");
+ return NULL;
+ }
+ return (census_trace_obj *)census_ht_find(g_trace_store,
+ op_id_as_key(&op_id));
}
-const char *
-census_get_trace_method_name (const census_trace_obj * trace)
-{
+const char *census_get_trace_method_name(const census_trace_obj *trace) {
return trace->method;
}
-static census_trace_annotation *
-dup_annotation_chain (census_trace_annotation * from)
-{
+static census_trace_annotation *dup_annotation_chain(
+ census_trace_annotation *from) {
census_trace_annotation *ret = NULL;
census_trace_annotation **to = &ret;
- for (; from != NULL; from = from->next)
- {
- *to = gpr_malloc (sizeof (census_trace_annotation));
- memcpy (*to, from, sizeof (census_trace_annotation));
- to = &(*to)->next;
- }
+ for (; from != NULL; from = from->next) {
+ *to = gpr_malloc(sizeof(census_trace_annotation));
+ memcpy(*to, from, sizeof(census_trace_annotation));
+ to = &(*to)->next;
+ }
return ret;
}
-static census_trace_obj *
-trace_obj_dup (census_trace_obj * from)
-{
+static census_trace_obj *trace_obj_dup(census_trace_obj *from) {
census_trace_obj *to = NULL;
- GPR_ASSERT (from != NULL);
- to = gpr_malloc (sizeof (census_trace_obj));
+ GPR_ASSERT(from != NULL);
+ to = gpr_malloc(sizeof(census_trace_obj));
to->id = from->id;
to->ts = from->ts;
to->rpc_stats = from->rpc_stats;
- to->method = gpr_strdup (from->method);
- to->annotations = dup_annotation_chain (from->annotations);
+ to->method = gpr_strdup(from->method);
+ to->annotations = dup_annotation_chain(from->annotations);
return to;
}
-census_trace_obj **
-census_get_active_ops (int *num_active_ops)
-{
+census_trace_obj **census_get_active_ops(int *num_active_ops) {
census_trace_obj **ret = NULL;
- gpr_mu_lock (&g_mu);
- if (g_trace_store != NULL)
- {
- size_t n = 0;
- census_ht_kv *all_kvs = census_ht_get_all_elements (g_trace_store, &n);
- *num_active_ops = (int) n;
- if (n != 0)
- {
- size_t i = 0;
- ret = gpr_malloc (sizeof (census_trace_obj *) * n);
- for (i = 0; i < n; i++)
- {
- ret[i] = trace_obj_dup ((census_trace_obj *) all_kvs[i].v);
- }
- }
- gpr_free (all_kvs);
+ gpr_mu_lock(&g_mu);
+ if (g_trace_store != NULL) {
+ size_t n = 0;
+ census_ht_kv *all_kvs = census_ht_get_all_elements(g_trace_store, &n);
+ *num_active_ops = (int)n;
+ if (n != 0) {
+ size_t i = 0;
+ ret = gpr_malloc(sizeof(census_trace_obj *) * n);
+ for (i = 0; i < n; i++) {
+ ret[i] = trace_obj_dup((census_trace_obj *)all_kvs[i].v);
+ }
}
- gpr_mu_unlock (&g_mu);
+ gpr_free(all_kvs);
+ }
+ gpr_mu_unlock(&g_mu);
return ret;
}
diff --git a/src/core/statistics/census_tracing.h b/src/core/statistics/census_tracing.h
index 02888296a9..bb3f2556d2 100644
--- a/src/core/statistics/census_tracing.h
+++ b/src/core/statistics/census_tracing.h
@@ -43,57 +43,54 @@
They are not recommended for external use.
*/
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Struct for a trace annotation. */
- typedef struct census_trace_annotation
- {
- gpr_timespec ts; /* timestamp of the annotation */
- char txt[CENSUS_MAX_ANNOTATION_LENGTH + 1]; /* actual txt annotation */
- struct census_trace_annotation *next;
- } census_trace_annotation;
+typedef struct census_trace_annotation {
+ gpr_timespec ts; /* timestamp of the annotation */
+ char txt[CENSUS_MAX_ANNOTATION_LENGTH + 1]; /* actual txt annotation */
+ struct census_trace_annotation *next;
+} census_trace_annotation;
- typedef struct census_trace_obj
- {
- census_op_id id;
- gpr_timespec ts;
- census_rpc_stats rpc_stats;
- char *method;
- census_trace_annotation *annotations;
- } census_trace_obj;
+typedef struct census_trace_obj {
+ census_op_id id;
+ gpr_timespec ts;
+ census_rpc_stats rpc_stats;
+ char *method;
+ census_trace_annotation *annotations;
+} census_trace_obj;
/* Deletes trace object. */
- void census_trace_obj_destroy (census_trace_obj * obj);
+void census_trace_obj_destroy(census_trace_obj *obj);
/* Initializes trace store. This function is thread safe. */
- void census_tracing_init (void);
+void census_tracing_init(void);
/* Shutsdown trace store. This function is thread safe. */
- void census_tracing_shutdown (void);
+void census_tracing_shutdown(void);
/* Gets trace obj corresponding to the input op_id. Returns NULL if trace store
is not initialized or trace obj is not found. Requires trace store being
locked before calling this function. */
- census_trace_obj *census_get_trace_obj_locked (census_op_id op_id);
+census_trace_obj *census_get_trace_obj_locked(census_op_id op_id);
/* The following two functions acquire and release the trace store global lock.
They are for census internal use only. */
- void census_internal_lock_trace_store (void);
- void census_internal_unlock_trace_store (void);
+void census_internal_lock_trace_store(void);
+void census_internal_unlock_trace_store(void);
/* Gets method name associated with the input trace object. */
- const char *census_get_trace_method_name (const census_trace_obj * trace);
+const char *census_get_trace_method_name(const census_trace_obj *trace);
/* Returns an array of pointers to trace objects of currently active operations
and fills in number of active operations. Returns NULL if there are no active
operations.
Caller owns the returned objects. */
- census_trace_obj **census_get_active_ops (int *num_active_ops);
+census_trace_obj **census_get_active_ops(int *num_active_ops);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_STATISTICS_CENSUS_TRACING_H */
+#endif /* GRPC_INTERNAL_CORE_STATISTICS_CENSUS_TRACING_H */
diff --git a/src/core/statistics/hash_table.c b/src/core/statistics/hash_table.c
index e0f89233d2..39b760f0e0 100644
--- a/src/core/statistics/hash_table.c
+++ b/src/core/statistics/hash_table.c
@@ -43,16 +43,14 @@
#define CENSUS_HT_NUM_BUCKETS 1999
/* A single hash table data entry */
-typedef struct ht_entry
-{
+typedef struct ht_entry {
census_ht_key key;
void *data;
struct ht_entry *next;
} ht_entry;
/* hash table bucket */
-typedef struct bucket
-{
+typedef struct bucket {
/* NULL if bucket is empty */
ht_entry *next;
/* -1 if all buckets are empty. */
@@ -61,8 +59,7 @@ typedef struct bucket
gpr_int32 next_non_empty_bucket;
} bucket;
-struct unresizable_hash_table
-{
+struct unresizable_hash_table {
/* Number of entries in the table */
size_t size;
/* Number of buckets */
@@ -78,8 +75,7 @@ struct unresizable_hash_table
census_ht_option options;
};
-typedef struct entry_locator
-{
+typedef struct entry_locator {
gpr_int32 bucket_idx;
int is_first_in_chain;
int found;
@@ -87,171 +83,139 @@ typedef struct entry_locator
} entry_locator;
/* Asserts if option is not valid. */
-void
-check_options (const census_ht_option * option)
-{
- GPR_ASSERT (option != NULL);
- GPR_ASSERT (option->num_buckets > 0);
- GPR_ASSERT (option->key_type == CENSUS_HT_UINT64 || option->key_type == CENSUS_HT_POINTER);
- if (option->key_type == CENSUS_HT_UINT64)
- {
- GPR_ASSERT (option->hash == NULL);
- }
- else if (option->key_type == CENSUS_HT_POINTER)
- {
- GPR_ASSERT (option->hash != NULL);
- GPR_ASSERT (option->compare_keys != NULL);
- }
+void check_options(const census_ht_option *option) {
+ GPR_ASSERT(option != NULL);
+ GPR_ASSERT(option->num_buckets > 0);
+ GPR_ASSERT(option->key_type == CENSUS_HT_UINT64 ||
+ option->key_type == CENSUS_HT_POINTER);
+ if (option->key_type == CENSUS_HT_UINT64) {
+ GPR_ASSERT(option->hash == NULL);
+ } else if (option->key_type == CENSUS_HT_POINTER) {
+ GPR_ASSERT(option->hash != NULL);
+ GPR_ASSERT(option->compare_keys != NULL);
+ }
}
#define REMOVE_NEXT(options, ptr) \
do { \
- ht_entry* tmp = (ptr)->next; \
+ ht_entry *tmp = (ptr)->next; \
(ptr)->next = tmp->next; \
delete_entry(options, tmp); \
} while (0)
-static void
-delete_entry (const census_ht_option * opt, ht_entry * p)
-{
- if (opt->delete_data != NULL)
- {
- opt->delete_data (p->data);
- }
- if (opt->delete_key != NULL)
- {
- opt->delete_key (p->key.ptr);
- }
- gpr_free (p);
+static void delete_entry(const census_ht_option *opt, ht_entry *p) {
+ if (opt->delete_data != NULL) {
+ opt->delete_data(p->data);
+ }
+ if (opt->delete_key != NULL) {
+ opt->delete_key(p->key.ptr);
+ }
+ gpr_free(p);
}
-static gpr_uint64
-hash (const census_ht_option * opt, census_ht_key key)
-{
- return opt->key_type == CENSUS_HT_UINT64 ? key.val : opt->hash (key.ptr);
+static gpr_uint64 hash(const census_ht_option *opt, census_ht_key key) {
+ return opt->key_type == CENSUS_HT_UINT64 ? key.val : opt->hash(key.ptr);
}
-census_ht *
-census_ht_create (const census_ht_option * option)
-{
+census_ht *census_ht_create(const census_ht_option *option) {
int i;
census_ht *ret = NULL;
- check_options (option);
- ret = (census_ht *) gpr_malloc (sizeof (census_ht));
+ check_options(option);
+ ret = (census_ht *)gpr_malloc(sizeof(census_ht));
ret->size = 0;
ret->num_buckets = option->num_buckets;
- ret->buckets = (bucket *) gpr_malloc (sizeof (bucket) * ret->num_buckets);
+ ret->buckets = (bucket *)gpr_malloc(sizeof(bucket) * ret->num_buckets);
ret->options = *option;
/* initialize each bucket */
- for (i = 0; i < ret->options.num_buckets; i++)
- {
- ret->buckets[i].prev_non_empty_bucket = -1;
- ret->buckets[i].next_non_empty_bucket = -1;
- ret->buckets[i].next = NULL;
- }
+ for (i = 0; i < ret->options.num_buckets; i++) {
+ ret->buckets[i].prev_non_empty_bucket = -1;
+ ret->buckets[i].next_non_empty_bucket = -1;
+ ret->buckets[i].next = NULL;
+ }
return ret;
}
-static gpr_int32
-find_bucket_idx (const census_ht * ht, census_ht_key key)
-{
- return hash (&ht->options, key) % ht->num_buckets;
+static gpr_int32 find_bucket_idx(const census_ht *ht, census_ht_key key) {
+ return hash(&ht->options, key) % ht->num_buckets;
}
-static int
-keys_match (const census_ht_option * opt, const ht_entry * p, const census_ht_key key)
-{
- GPR_ASSERT (opt->key_type == CENSUS_HT_UINT64 || opt->key_type == CENSUS_HT_POINTER);
- if (opt->key_type == CENSUS_HT_UINT64)
- return p->key.val == key.val;
- return !opt->compare_keys ((p->key).ptr, key.ptr);
+static int keys_match(const census_ht_option *opt, const ht_entry *p,
+ const census_ht_key key) {
+ GPR_ASSERT(opt->key_type == CENSUS_HT_UINT64 ||
+ opt->key_type == CENSUS_HT_POINTER);
+ if (opt->key_type == CENSUS_HT_UINT64) return p->key.val == key.val;
+ return !opt->compare_keys((p->key).ptr, key.ptr);
}
-static entry_locator
-ht_find (const census_ht * ht, census_ht_key key)
-{
- entry_locator loc = { 0, 0, 0, NULL };
+static entry_locator ht_find(const census_ht *ht, census_ht_key key) {
+ entry_locator loc = {0, 0, 0, NULL};
gpr_int32 idx = 0;
ht_entry *ptr = NULL;
- GPR_ASSERT (ht != NULL);
- idx = find_bucket_idx (ht, key);
+ GPR_ASSERT(ht != NULL);
+ idx = find_bucket_idx(ht, key);
ptr = ht->buckets[idx].next;
- if (ptr == NULL)
- {
- /* bucket is empty */
- return loc;
- }
- if (keys_match (&ht->options, ptr, key))
- {
- loc.bucket_idx = idx;
- loc.is_first_in_chain = 1;
- loc.found = 1;
- return loc;
- }
- else
- {
- for (; ptr->next != NULL; ptr = ptr->next)
- {
- if (keys_match (&ht->options, ptr->next, key))
- {
- loc.bucket_idx = idx;
- loc.is_first_in_chain = 0;
- loc.found = 1;
- loc.prev_entry = ptr;
- return loc;
- }
- }
+ if (ptr == NULL) {
+ /* bucket is empty */
+ return loc;
+ }
+ if (keys_match(&ht->options, ptr, key)) {
+ loc.bucket_idx = idx;
+ loc.is_first_in_chain = 1;
+ loc.found = 1;
+ return loc;
+ } else {
+ for (; ptr->next != NULL; ptr = ptr->next) {
+ if (keys_match(&ht->options, ptr->next, key)) {
+ loc.bucket_idx = idx;
+ loc.is_first_in_chain = 0;
+ loc.found = 1;
+ loc.prev_entry = ptr;
+ return loc;
+ }
}
+ }
/* Could not find the key */
return loc;
}
-void *
-census_ht_find (const census_ht * ht, census_ht_key key)
-{
- entry_locator loc = ht_find (ht, key);
- if (loc.found == 0)
- {
- return NULL;
- }
- return loc.is_first_in_chain ? ht->buckets[loc.bucket_idx].next->data : loc.prev_entry->next->data;
+void *census_ht_find(const census_ht *ht, census_ht_key key) {
+ entry_locator loc = ht_find(ht, key);
+ if (loc.found == 0) {
+ return NULL;
+ }
+ return loc.is_first_in_chain ? ht->buckets[loc.bucket_idx].next->data
+ : loc.prev_entry->next->data;
}
-void
-census_ht_insert (census_ht * ht, census_ht_key key, void *data)
-{
- gpr_int32 idx = find_bucket_idx (ht, key);
+void census_ht_insert(census_ht *ht, census_ht_key key, void *data) {
+ gpr_int32 idx = find_bucket_idx(ht, key);
ht_entry *ptr = NULL;
- entry_locator loc = ht_find (ht, key);
- if (loc.found)
- {
- /* Replace old value with new value. */
- ptr = loc.is_first_in_chain ? ht->buckets[loc.bucket_idx].next : loc.prev_entry->next;
- if (ht->options.delete_data != NULL)
- {
- ht->options.delete_data (ptr->data);
- }
- ptr->data = data;
- return;
+ entry_locator loc = ht_find(ht, key);
+ if (loc.found) {
+ /* Replace old value with new value. */
+ ptr = loc.is_first_in_chain ? ht->buckets[loc.bucket_idx].next
+ : loc.prev_entry->next;
+ if (ht->options.delete_data != NULL) {
+ ht->options.delete_data(ptr->data);
}
+ ptr->data = data;
+ return;
+ }
/* first entry in the table. */
- if (ht->size == 0)
- {
- ht->buckets[idx].next_non_empty_bucket = -1;
- ht->buckets[idx].prev_non_empty_bucket = -1;
- ht->first_non_empty_bucket = idx;
- ht->last_non_empty_bucket = idx;
- }
- else if (ht->buckets[idx].next == NULL)
- {
- /* first entry in the bucket. */
- ht->buckets[ht->last_non_empty_bucket].next_non_empty_bucket = idx;
- ht->buckets[idx].prev_non_empty_bucket = ht->last_non_empty_bucket;
- ht->buckets[idx].next_non_empty_bucket = -1;
- ht->last_non_empty_bucket = idx;
- }
- ptr = (ht_entry *) gpr_malloc (sizeof (ht_entry));
+ if (ht->size == 0) {
+ ht->buckets[idx].next_non_empty_bucket = -1;
+ ht->buckets[idx].prev_non_empty_bucket = -1;
+ ht->first_non_empty_bucket = idx;
+ ht->last_non_empty_bucket = idx;
+ } else if (ht->buckets[idx].next == NULL) {
+ /* first entry in the bucket. */
+ ht->buckets[ht->last_non_empty_bucket].next_non_empty_bucket = idx;
+ ht->buckets[idx].prev_non_empty_bucket = ht->last_non_empty_bucket;
+ ht->buckets[idx].next_non_empty_bucket = -1;
+ ht->last_non_empty_bucket = idx;
+ }
+ ptr = (ht_entry *)gpr_malloc(sizeof(ht_entry));
ptr->key = key;
ptr->data = data;
ptr->next = ht->buckets[idx].next;
@@ -259,109 +223,81 @@ census_ht_insert (census_ht * ht, census_ht_key key, void *data)
ht->size++;
}
-void
-census_ht_erase (census_ht * ht, census_ht_key key)
-{
- entry_locator loc = ht_find (ht, key);
- if (loc.found == 0)
- {
- /* noop if not found */
- return;
- }
+void census_ht_erase(census_ht *ht, census_ht_key key) {
+ entry_locator loc = ht_find(ht, key);
+ if (loc.found == 0) {
+ /* noop if not found */
+ return;
+ }
ht->size--;
- if (loc.is_first_in_chain)
- {
- bucket *b = &ht->buckets[loc.bucket_idx];
- GPR_ASSERT (b->next != NULL);
- /* The only entry in the bucket */
- if (b->next->next == NULL)
- {
- int prev = b->prev_non_empty_bucket;
- int next = b->next_non_empty_bucket;
- if (prev != -1)
- {
- ht->buckets[prev].next_non_empty_bucket = next;
- }
- else
- {
- ht->first_non_empty_bucket = next;
- }
- if (next != -1)
- {
- ht->buckets[next].prev_non_empty_bucket = prev;
- }
- else
- {
- ht->last_non_empty_bucket = prev;
- }
- }
- REMOVE_NEXT (&ht->options, b);
- }
- else
- {
- GPR_ASSERT (loc.prev_entry->next != NULL);
- REMOVE_NEXT (&ht->options, loc.prev_entry);
+ if (loc.is_first_in_chain) {
+ bucket *b = &ht->buckets[loc.bucket_idx];
+ GPR_ASSERT(b->next != NULL);
+ /* The only entry in the bucket */
+ if (b->next->next == NULL) {
+ int prev = b->prev_non_empty_bucket;
+ int next = b->next_non_empty_bucket;
+ if (prev != -1) {
+ ht->buckets[prev].next_non_empty_bucket = next;
+ } else {
+ ht->first_non_empty_bucket = next;
+ }
+ if (next != -1) {
+ ht->buckets[next].prev_non_empty_bucket = prev;
+ } else {
+ ht->last_non_empty_bucket = prev;
+ }
}
+ REMOVE_NEXT(&ht->options, b);
+ } else {
+ GPR_ASSERT(loc.prev_entry->next != NULL);
+ REMOVE_NEXT(&ht->options, loc.prev_entry);
+ }
}
/* Returns NULL if input table is empty. */
-census_ht_kv *
-census_ht_get_all_elements (const census_ht * ht, size_t * num)
-{
+census_ht_kv *census_ht_get_all_elements(const census_ht *ht, size_t *num) {
census_ht_kv *ret = NULL;
int i = 0;
gpr_int32 idx = -1;
- GPR_ASSERT (ht != NULL && num != NULL);
+ GPR_ASSERT(ht != NULL && num != NULL);
*num = ht->size;
- if (*num == 0)
- {
- return NULL;
- }
+ if (*num == 0) {
+ return NULL;
+ }
- ret = (census_ht_kv *) gpr_malloc (sizeof (census_ht_kv) * ht->size);
+ ret = (census_ht_kv *)gpr_malloc(sizeof(census_ht_kv) * ht->size);
idx = ht->first_non_empty_bucket;
- while (idx >= 0)
- {
- ht_entry *ptr = ht->buckets[idx].next;
- for (; ptr != NULL; ptr = ptr->next)
- {
- ret[i].k = ptr->key;
- ret[i].v = ptr->data;
- i++;
- }
- idx = ht->buckets[idx].next_non_empty_bucket;
+ while (idx >= 0) {
+ ht_entry *ptr = ht->buckets[idx].next;
+ for (; ptr != NULL; ptr = ptr->next) {
+ ret[i].k = ptr->key;
+ ret[i].v = ptr->data;
+ i++;
}
+ idx = ht->buckets[idx].next_non_empty_bucket;
+ }
return ret;
}
-static void
-ht_delete_entry_chain (const census_ht_option * options, ht_entry * first)
-{
- if (first == NULL)
- {
- return;
- }
- if (first->next != NULL)
- {
- ht_delete_entry_chain (options, first->next);
- }
- delete_entry (options, first);
+static void ht_delete_entry_chain(const census_ht_option *options,
+ ht_entry *first) {
+ if (first == NULL) {
+ return;
+ }
+ if (first->next != NULL) {
+ ht_delete_entry_chain(options, first->next);
+ }
+ delete_entry(options, first);
}
-void
-census_ht_destroy (census_ht * ht)
-{
+void census_ht_destroy(census_ht *ht) {
unsigned i;
- for (i = 0; i < ht->num_buckets; ++i)
- {
- ht_delete_entry_chain (&ht->options, ht->buckets[i].next);
- }
- gpr_free (ht->buckets);
- gpr_free (ht);
+ for (i = 0; i < ht->num_buckets; ++i) {
+ ht_delete_entry_chain(&ht->options, ht->buckets[i].next);
+ }
+ gpr_free(ht->buckets);
+ gpr_free(ht);
}
-size_t
-census_ht_get_size (const census_ht * ht)
-{
- return ht->size;
-}
+size_t census_ht_get_size(const census_ht *ht) { return ht->size; }
diff --git a/src/core/statistics/hash_table.h b/src/core/statistics/hash_table.h
index 21f223cceb..8b39f536fd 100644
--- a/src/core/statistics/hash_table.h
+++ b/src/core/statistics/hash_table.h
@@ -59,58 +59,54 @@ typedef struct unresizable_hash_table census_ht;
/* Currently, the hash_table can take two types of keys. (uint64 for trace
store and const char* for stats store). */
-typedef union
-{
+typedef union {
gpr_uint64 val;
void *ptr;
} census_ht_key;
-typedef enum census_ht_key_type
-{
+typedef enum census_ht_key_type {
CENSUS_HT_UINT64 = 0,
CENSUS_HT_POINTER = 1
} census_ht_key_type;
-typedef struct census_ht_option
-{
+typedef struct census_ht_option {
/* Type of hash key */
census_ht_key_type key_type;
/* Desired number of buckets, preferably a prime number */
gpr_int32 num_buckets;
/* Fucntion to calculate uint64 hash value of the key. Only takes effect if
key_type is POINTER. */
- gpr_uint64 (*hash) (const void *);
+ gpr_uint64 (*hash)(const void *);
/* Function to compare two keys, returns 0 iff equal. Only takes effect if
key_type is POINTER */
- int (*compare_keys) (const void *k1, const void *k2);
+ int (*compare_keys)(const void *k1, const void *k2);
/* Value deleter. NULL if no specialized delete function is needed. */
- void (*delete_data) (void *);
+ void (*delete_data)(void *);
/* Key deleter. NULL if table does not own the key. (e.g. key is part of the
value or key is not owned by the table.) */
- void (*delete_key) (void *);
+ void (*delete_key)(void *);
} census_ht_option;
/* Creates a hashtable with fixed number of buckets according to the settings
specified in 'options' arg. Function pointers "hash" and "compare_keys" must
be provided if key_type is POINTER. Asserts if fail to create. */
-census_ht *census_ht_create (const census_ht_option * options);
+census_ht *census_ht_create(const census_ht_option *options);
/* Deletes hash table instance. Frees all dynamic memory owned by ht.*/
-void census_ht_destroy (census_ht * ht);
+void census_ht_destroy(census_ht *ht);
/* Inserts the input key-val pair into hash_table. If an entry with the same key
exists in the table, the corresponding value will be overwritten by the input
val. */
-void census_ht_insert (census_ht * ht, census_ht_key key, void *val);
+void census_ht_insert(census_ht *ht, census_ht_key key, void *val);
/* Returns pointer to data, returns NULL if not found. */
-void *census_ht_find (const census_ht * ht, census_ht_key key);
+void *census_ht_find(const census_ht *ht, census_ht_key key);
/* Erase hash table entry with input key. Noop if key is not found. */
-void census_ht_erase (census_ht * ht, census_ht_key key);
+void census_ht_erase(census_ht *ht, census_ht_key key);
-typedef struct census_ht_kv
-{
+typedef struct census_ht_kv {
census_ht_key k;
void *v;
} census_ht_kv;
@@ -118,17 +114,18 @@ typedef struct census_ht_kv
/* Returns an array of pointers to all values in the hash table. Order of the
elements can be arbitrary. Sets 'num' to the size of returned array. Caller
owns returned array. */
-census_ht_kv *census_ht_get_all_elements (const census_ht * ht, size_t * num);
+census_ht_kv *census_ht_get_all_elements(const census_ht *ht, size_t *num);
/* Returns number of elements kept. */
-size_t census_ht_get_size (const census_ht * ht);
+size_t census_ht_get_size(const census_ht *ht);
/* Functor applied on each key-value pair while iterating through entries in the
table. The functor should not mutate data. */
-typedef void (*census_ht_itr_cb) (census_ht_key key, const void *val_ptr, void *state);
+typedef void (*census_ht_itr_cb)(census_ht_key key, const void *val_ptr,
+ void *state);
/* Iterates through all key-value pairs in the hash_table. The callback function
should not invalidate data entries. */
-gpr_uint64 census_ht_for_all (const census_ht * ht, census_ht_itr_cb);
+gpr_uint64 census_ht_for_all(const census_ht *ht, census_ht_itr_cb);
#endif /* GRPC_INTERNAL_CORE_STATISTICS_HASH_TABLE_H */
diff --git a/src/core/statistics/window_stats.c b/src/core/statistics/window_stats.c
index 385d75d4f4..4d0d3cca4a 100644
--- a/src/core/statistics/window_stats.c
+++ b/src/core/statistics/window_stats.c
@@ -46,16 +46,14 @@ typedef struct census_window_stats_sum cws_sum;
/* Each interval is composed of a number of buckets, which hold a count of
entries and a single statistic */
-typedef struct census_window_stats_bucket
-{
+typedef struct census_window_stats_bucket {
gpr_int64 count;
void *statistic;
} cws_bucket;
/* Each interval has a set of buckets, and the variables needed to keep
track of their current state */
-typedef struct census_window_stats_interval_stats
-{
+typedef struct census_window_stats_interval_stats {
/* The buckets. There will be 'granularity' + 1 of these. */
cws_bucket *buckets;
/* Index of the bucket containing the smallest time interval. */
@@ -68,8 +66,7 @@ typedef struct census_window_stats_interval_stats
gpr_int64 width;
} cws_interval_stats;
-typedef struct census_window_stats
-{
+typedef struct census_window_stats {
/* Number of intervals. */
int nintervals;
/* Number of buckets in each interval. 'granularity' + 1. */
@@ -90,252 +87,231 @@ typedef struct census_window_stats
/* The maximum seconds value we can have in a valid timespec. More than this
will result in overflow in timespec_to_ns(). This works out to ~292 years.
TODO: consider using doubles instead of int64. */
-static gpr_int64 max_seconds = (GPR_INT64_MAX - GPR_NS_PER_SEC) / GPR_NS_PER_SEC;
+static gpr_int64 max_seconds =
+ (GPR_INT64_MAX - GPR_NS_PER_SEC) / GPR_NS_PER_SEC;
-static gpr_int64
-timespec_to_ns (const gpr_timespec ts)
-{
- if (ts.tv_sec > max_seconds)
- {
- return GPR_INT64_MAX - 1;
- }
- return (gpr_int64) ts.tv_sec * GPR_NS_PER_SEC + ts.tv_nsec;
+static gpr_int64 timespec_to_ns(const gpr_timespec ts) {
+ if (ts.tv_sec > max_seconds) {
+ return GPR_INT64_MAX - 1;
+ }
+ return (gpr_int64)ts.tv_sec * GPR_NS_PER_SEC + ts.tv_nsec;
}
-static void
-cws_initialize_statistic (void *statistic, const cws_stat_info * stat_info)
-{
- if (stat_info->stat_initialize == NULL)
- {
- memset (statistic, 0, stat_info->stat_size);
- }
- else
- {
- stat_info->stat_initialize (statistic);
- }
+static void cws_initialize_statistic(void *statistic,
+ const cws_stat_info *stat_info) {
+ if (stat_info->stat_initialize == NULL) {
+ memset(statistic, 0, stat_info->stat_size);
+ } else {
+ stat_info->stat_initialize(statistic);
+ }
}
/* Create and initialize a statistic */
-static void *
-cws_create_statistic (const cws_stat_info * stat_info)
-{
- void *stat = gpr_malloc (stat_info->stat_size);
- cws_initialize_statistic (stat, stat_info);
+static void *cws_create_statistic(const cws_stat_info *stat_info) {
+ void *stat = gpr_malloc(stat_info->stat_size);
+ cws_initialize_statistic(stat, stat_info);
return stat;
}
-window_stats *
-census_window_stats_create (int nintervals, const gpr_timespec intervals[], int granularity, const cws_stat_info * stat_info)
-{
+window_stats *census_window_stats_create(int nintervals,
+ const gpr_timespec intervals[],
+ int granularity,
+ const cws_stat_info *stat_info) {
window_stats *ret;
int i;
/* validate inputs */
- GPR_ASSERT (nintervals > 0 && granularity > 2 && intervals != NULL && stat_info != NULL);
- for (i = 0; i < nintervals; i++)
- {
- gpr_int64 ns = timespec_to_ns (intervals[i]);
- GPR_ASSERT (intervals[i].tv_sec >= 0 && intervals[i].tv_nsec >= 0 && intervals[i].tv_nsec < GPR_NS_PER_SEC && ns >= 100 && granularity * 10 <= ns);
- }
+ GPR_ASSERT(nintervals > 0 && granularity > 2 && intervals != NULL &&
+ stat_info != NULL);
+ for (i = 0; i < nintervals; i++) {
+ gpr_int64 ns = timespec_to_ns(intervals[i]);
+ GPR_ASSERT(intervals[i].tv_sec >= 0 && intervals[i].tv_nsec >= 0 &&
+ intervals[i].tv_nsec < GPR_NS_PER_SEC && ns >= 100 &&
+ granularity * 10 <= ns);
+ }
/* Allocate and initialize relevant data structures */
- ret = (window_stats *) gpr_malloc (sizeof (window_stats));
+ ret = (window_stats *)gpr_malloc(sizeof(window_stats));
ret->nintervals = nintervals;
ret->nbuckets = granularity + 1;
ret->stat_info = *stat_info;
- ret->interval_stats = (cws_interval_stats *) gpr_malloc (nintervals * sizeof (cws_interval_stats));
- for (i = 0; i < nintervals; i++)
- {
- gpr_int64 size_ns = timespec_to_ns (intervals[i]);
- cws_interval_stats *is = ret->interval_stats + i;
- cws_bucket *buckets = is->buckets = (cws_bucket *) gpr_malloc (ret->nbuckets * sizeof (cws_bucket));
- int b;
- for (b = 0; b < ret->nbuckets; b++)
- {
- buckets[b].statistic = cws_create_statistic (stat_info);
- buckets[b].count = 0;
- }
- is->bottom_bucket = 0;
- is->bottom = 0;
- is->width = size_ns / granularity;
- /* Check for possible overflow issues, and maximize interval size if the
- user requested something large enough. */
- if ((GPR_INT64_MAX - is->width) > size_ns)
- {
- is->top = size_ns + is->width;
- }
- else
- {
- is->top = GPR_INT64_MAX;
- is->width = GPR_INT64_MAX / (granularity + 1);
- }
- /* If size doesn't divide evenly, we can have a width slightly too small;
- better to have it slightly large. */
- if ((size_ns - (granularity + 1) * is->width) > 0)
- {
- is->width += 1;
- }
+ ret->interval_stats =
+ (cws_interval_stats *)gpr_malloc(nintervals * sizeof(cws_interval_stats));
+ for (i = 0; i < nintervals; i++) {
+ gpr_int64 size_ns = timespec_to_ns(intervals[i]);
+ cws_interval_stats *is = ret->interval_stats + i;
+ cws_bucket *buckets = is->buckets =
+ (cws_bucket *)gpr_malloc(ret->nbuckets * sizeof(cws_bucket));
+ int b;
+ for (b = 0; b < ret->nbuckets; b++) {
+ buckets[b].statistic = cws_create_statistic(stat_info);
+ buckets[b].count = 0;
+ }
+ is->bottom_bucket = 0;
+ is->bottom = 0;
+ is->width = size_ns / granularity;
+ /* Check for possible overflow issues, and maximize interval size if the
+ user requested something large enough. */
+ if ((GPR_INT64_MAX - is->width) > size_ns) {
+ is->top = size_ns + is->width;
+ } else {
+ is->top = GPR_INT64_MAX;
+ is->width = GPR_INT64_MAX / (granularity + 1);
+ }
+ /* If size doesn't divide evenly, we can have a width slightly too small;
+ better to have it slightly large. */
+ if ((size_ns - (granularity + 1) * is->width) > 0) {
+ is->width += 1;
}
+ }
ret->newest_time = 0;
return ret;
}
/* When we try adding a measurement above the current interval range, we
need to "shift" the buckets sufficiently to cover the new range. */
-static void
-cws_shift_buckets (const window_stats * wstats, cws_interval_stats * is, gpr_int64 when_ns)
-{
+static void cws_shift_buckets(const window_stats *wstats,
+ cws_interval_stats *is, gpr_int64 when_ns) {
int i;
/* number of bucket time widths to "shift" */
int shift;
/* number of buckets to clear */
int nclear;
- GPR_ASSERT (when_ns >= is->top);
+ GPR_ASSERT(when_ns >= is->top);
/* number of bucket time widths to "shift" */
shift = ((when_ns - is->top) / is->width) + 1;
/* number of buckets to clear - limited by actual number of buckets */
- nclear = GPR_MIN (shift, wstats->nbuckets);
- for (i = 0; i < nclear; i++)
- {
- int b = BUCKET_IDX (is, i, wstats);
- is->buckets[b].count = 0;
- cws_initialize_statistic (is->buckets[b].statistic, &wstats->stat_info);
- }
+ nclear = GPR_MIN(shift, wstats->nbuckets);
+ for (i = 0; i < nclear; i++) {
+ int b = BUCKET_IDX(is, i, wstats);
+ is->buckets[b].count = 0;
+ cws_initialize_statistic(is->buckets[b].statistic, &wstats->stat_info);
+ }
/* adjust top/bottom times and current bottom bucket */
- is->bottom_bucket = BUCKET_IDX (is, shift, wstats);
+ is->bottom_bucket = BUCKET_IDX(is, shift, wstats);
is->top += shift * is->width;
is->bottom += shift * is->width;
}
-void
-census_window_stats_add (window_stats * wstats, const gpr_timespec when, const void *stat_value)
-{
+void census_window_stats_add(window_stats *wstats, const gpr_timespec when,
+ const void *stat_value) {
int i;
- gpr_int64 when_ns = timespec_to_ns (when);
- GPR_ASSERT (wstats->interval_stats != NULL);
- for (i = 0; i < wstats->nintervals; i++)
- {
- cws_interval_stats *is = wstats->interval_stats + i;
- cws_bucket *bucket;
- if (when_ns < is->bottom)
- { /* Below smallest time in interval: drop */
- continue;
- }
- if (when_ns >= is->top)
- { /* above limit: shift buckets */
- cws_shift_buckets (wstats, is, when_ns);
- }
- /* Add the stat. */
- GPR_ASSERT (is->bottom <= when_ns && when_ns < is->top);
- bucket = is->buckets + BUCKET_IDX (is, (when_ns - is->bottom) / is->width, wstats);
- bucket->count++;
- wstats->stat_info.stat_add (bucket->statistic, stat_value);
+ gpr_int64 when_ns = timespec_to_ns(when);
+ GPR_ASSERT(wstats->interval_stats != NULL);
+ for (i = 0; i < wstats->nintervals; i++) {
+ cws_interval_stats *is = wstats->interval_stats + i;
+ cws_bucket *bucket;
+ if (when_ns < is->bottom) { /* Below smallest time in interval: drop */
+ continue;
}
- if (when_ns > wstats->newest_time)
- {
- wstats->newest_time = when_ns;
+ if (when_ns >= is->top) { /* above limit: shift buckets */
+ cws_shift_buckets(wstats, is, when_ns);
}
+ /* Add the stat. */
+ GPR_ASSERT(is->bottom <= when_ns && when_ns < is->top);
+ bucket = is->buckets +
+ BUCKET_IDX(is, (when_ns - is->bottom) / is->width, wstats);
+ bucket->count++;
+ wstats->stat_info.stat_add(bucket->statistic, stat_value);
+ }
+ if (when_ns > wstats->newest_time) {
+ wstats->newest_time = when_ns;
+ }
}
/* Add a specific bucket contents to an accumulating total. */
-static void
-cws_add_bucket_to_sum (cws_sum * sum, const cws_bucket * bucket, const cws_stat_info * stat_info)
-{
+static void cws_add_bucket_to_sum(cws_sum *sum, const cws_bucket *bucket,
+ const cws_stat_info *stat_info) {
sum->count += bucket->count;
- stat_info->stat_add (sum->statistic, bucket->statistic);
+ stat_info->stat_add(sum->statistic, bucket->statistic);
}
/* Add a proportion to an accumulating sum. */
-static void
-cws_add_proportion_to_sum (double p, cws_sum * sum, const cws_bucket * bucket, const cws_stat_info * stat_info)
-{
+static void cws_add_proportion_to_sum(double p, cws_sum *sum,
+ const cws_bucket *bucket,
+ const cws_stat_info *stat_info) {
sum->count += p * bucket->count;
- stat_info->stat_add_proportion (p, sum->statistic, bucket->statistic);
+ stat_info->stat_add_proportion(p, sum->statistic, bucket->statistic);
}
-void
-census_window_stats_get_sums (const window_stats * wstats, const gpr_timespec when, cws_sum sums[])
-{
+void census_window_stats_get_sums(const window_stats *wstats,
+ const gpr_timespec when, cws_sum sums[]) {
int i;
- gpr_int64 when_ns = timespec_to_ns (when);
- GPR_ASSERT (wstats->interval_stats != NULL);
- for (i = 0; i < wstats->nintervals; i++)
- {
- int when_bucket;
- int new_bucket;
- double last_proportion = 1.0;
- double bottom_proportion;
- cws_interval_stats *is = wstats->interval_stats + i;
- cws_sum *sum = sums + i;
- sum->count = 0;
- cws_initialize_statistic (sum->statistic, &wstats->stat_info);
- if (when_ns < is->bottom)
- {
- continue;
- }
- if (when_ns >= is->top)
- {
- cws_shift_buckets (wstats, is, when_ns);
- }
- /* Calculating the appropriate amount of which buckets to use can get
- complicated. Essentially there are two cases:
- 1) if the "top" bucket (new_bucket, where the newest additions to the
- stats recorded are entered) corresponds to 'when', then we need
- to take a proportion of it - (if when < newest_time) or the full
- thing. We also (possibly) need to take a corresponding
- proportion of the bottom bucket.
- 2) Other cases, we just take a straight proportion.
- */
- when_bucket = (when_ns - is->bottom) / is->width;
- new_bucket = (wstats->newest_time - is->bottom) / is->width;
- if (new_bucket == when_bucket)
- {
- gpr_int64 bottom_bucket_time = is->bottom + when_bucket * is->width;
- if (when_ns < wstats->newest_time)
- {
- last_proportion = (double) (when_ns - bottom_bucket_time) / (double) (wstats->newest_time - bottom_bucket_time);
- bottom_proportion = (double) (is->width - (when_ns - bottom_bucket_time)) / is->width;
- }
- else
- {
- bottom_proportion = (double) (is->width - (wstats->newest_time - bottom_bucket_time)) / is->width;
- }
- }
- else
- {
- last_proportion = (double) (when_ns + 1 - is->bottom - when_bucket * is->width) / is->width;
- bottom_proportion = 1.0 - last_proportion;
- }
- cws_add_proportion_to_sum (last_proportion, sum, is->buckets + BUCKET_IDX (is, when_bucket, wstats), &wstats->stat_info);
- if (when_bucket != 0)
- { /* last bucket isn't also bottom bucket */
- int b;
- /* Add all of "bottom" bucket if we are looking at a subset of the
- full interval, or a proportion if we are adding full interval. */
- cws_add_proportion_to_sum ((when_bucket == wstats->nbuckets - 1 ? bottom_proportion : 1.0), sum, is->buckets + is->bottom_bucket, &wstats->stat_info);
- /* Add all the remaining buckets (everything but top and bottom). */
- for (b = 1; b < when_bucket; b++)
- {
- cws_add_bucket_to_sum (sum, is->buckets + BUCKET_IDX (is, b, wstats), &wstats->stat_info);
- }
- }
+ gpr_int64 when_ns = timespec_to_ns(when);
+ GPR_ASSERT(wstats->interval_stats != NULL);
+ for (i = 0; i < wstats->nintervals; i++) {
+ int when_bucket;
+ int new_bucket;
+ double last_proportion = 1.0;
+ double bottom_proportion;
+ cws_interval_stats *is = wstats->interval_stats + i;
+ cws_sum *sum = sums + i;
+ sum->count = 0;
+ cws_initialize_statistic(sum->statistic, &wstats->stat_info);
+ if (when_ns < is->bottom) {
+ continue;
+ }
+ if (when_ns >= is->top) {
+ cws_shift_buckets(wstats, is, when_ns);
+ }
+ /* Calculating the appropriate amount of which buckets to use can get
+ complicated. Essentially there are two cases:
+ 1) if the "top" bucket (new_bucket, where the newest additions to the
+ stats recorded are entered) corresponds to 'when', then we need
+ to take a proportion of it - (if when < newest_time) or the full
+ thing. We also (possibly) need to take a corresponding
+ proportion of the bottom bucket.
+ 2) Other cases, we just take a straight proportion.
+ */
+ when_bucket = (when_ns - is->bottom) / is->width;
+ new_bucket = (wstats->newest_time - is->bottom) / is->width;
+ if (new_bucket == when_bucket) {
+ gpr_int64 bottom_bucket_time = is->bottom + when_bucket * is->width;
+ if (when_ns < wstats->newest_time) {
+ last_proportion = (double)(when_ns - bottom_bucket_time) /
+ (double)(wstats->newest_time - bottom_bucket_time);
+ bottom_proportion =
+ (double)(is->width - (when_ns - bottom_bucket_time)) / is->width;
+ } else {
+ bottom_proportion =
+ (double)(is->width - (wstats->newest_time - bottom_bucket_time)) /
+ is->width;
+ }
+ } else {
+ last_proportion =
+ (double)(when_ns + 1 - is->bottom - when_bucket * is->width) /
+ is->width;
+ bottom_proportion = 1.0 - last_proportion;
}
+ cws_add_proportion_to_sum(last_proportion, sum,
+ is->buckets + BUCKET_IDX(is, when_bucket, wstats),
+ &wstats->stat_info);
+ if (when_bucket != 0) { /* last bucket isn't also bottom bucket */
+ int b;
+ /* Add all of "bottom" bucket if we are looking at a subset of the
+ full interval, or a proportion if we are adding full interval. */
+ cws_add_proportion_to_sum(
+ (when_bucket == wstats->nbuckets - 1 ? bottom_proportion : 1.0), sum,
+ is->buckets + is->bottom_bucket, &wstats->stat_info);
+ /* Add all the remaining buckets (everything but top and bottom). */
+ for (b = 1; b < when_bucket; b++) {
+ cws_add_bucket_to_sum(sum, is->buckets + BUCKET_IDX(is, b, wstats),
+ &wstats->stat_info);
+ }
+ }
+ }
}
-void
-census_window_stats_destroy (window_stats * wstats)
-{
+void census_window_stats_destroy(window_stats *wstats) {
int i;
- GPR_ASSERT (wstats->interval_stats != NULL);
- for (i = 0; i < wstats->nintervals; i++)
- {
- int b;
- for (b = 0; b < wstats->nbuckets; b++)
- {
- gpr_free (wstats->interval_stats[i].buckets[b].statistic);
- }
- gpr_free (wstats->interval_stats[i].buckets);
+ GPR_ASSERT(wstats->interval_stats != NULL);
+ for (i = 0; i < wstats->nintervals; i++) {
+ int b;
+ for (b = 0; b < wstats->nbuckets; b++) {
+ gpr_free(wstats->interval_stats[i].buckets[b].statistic);
}
- gpr_free (wstats->interval_stats);
+ gpr_free(wstats->interval_stats[i].buckets);
+ }
+ gpr_free(wstats->interval_stats);
/* Ensure any use-after free triggers assert. */
wstats->interval_stats = NULL;
- gpr_free (wstats);
+ gpr_free(wstats);
}
diff --git a/src/core/statistics/window_stats.h b/src/core/statistics/window_stats.h
index ce67633b56..f4732e96a0 100644
--- a/src/core/statistics/window_stats.h
+++ b/src/core/statistics/window_stats.h
@@ -115,19 +115,18 @@
struct census_window_stats;
/* Information provided by API user on the information they want to record */
-typedef struct census_window_stats_stat_info
-{
+typedef struct census_window_stats_stat_info {
/* Number of bytes in user-defined object. */
size_t stat_size;
/* Function to initialize a user-defined statistics object. If this is set
* to NULL, then the object will be zero-initialized. */
- void (*stat_initialize) (void *stat);
+ void (*stat_initialize)(void *stat);
/* Function to add one user-defined statistics object ('addme') to 'base' */
- void (*stat_add) (void *base, const void *addme);
+ void (*stat_add)(void *base, const void *addme);
/* As for previous function, but only add a proportion 'p'. This API will
currently only use 'p' values in the range [0,1], but other values are
possible in the future, and should be supported. */
- void (*stat_add_proportion) (double p, void *base, const void *addme);
+ void (*stat_add_proportion)(double p, void *base, const void *addme);
} census_window_stats_stat_info;
/* Create a new window_stats object. 'nintervals' is the number of
@@ -139,15 +138,17 @@ typedef struct census_window_stats_stat_info
years will be treated as essentially infinite in size. This function will
GPR_ASSERT() if the object cannot be created or any of the parameters have
invalid values. This function is thread-safe. */
-struct census_window_stats *census_window_stats_create (int nintervals, const gpr_timespec intervals[], int granularity, const census_window_stats_stat_info * stat_info);
+struct census_window_stats *census_window_stats_create(
+ int nintervals, const gpr_timespec intervals[], int granularity,
+ const census_window_stats_stat_info *stat_info);
/* Add a new measurement (in 'stat_value'), as of a given time ('when').
This function is thread-compatible. */
-void census_window_stats_add (struct census_window_stats *wstats, const gpr_timespec when, const void *stat_value);
+void census_window_stats_add(struct census_window_stats *wstats,
+ const gpr_timespec when, const void *stat_value);
/* Structure used to record a single intervals sum for a given statistic */
-typedef struct census_window_stats_sum
-{
+typedef struct census_window_stats_sum {
/* Total count of samples. Note that because some internal interpolation
is performed, the count of samples returned for each interval may not be an
integral value. */
@@ -159,12 +160,14 @@ typedef struct census_window_stats_sum
/* Retrieve a set of all values stored in a window_stats object 'wstats'. The
number of 'sums' MUST be the same as the number 'nintervals' used in
census_window_stats_create(). This function is thread-compatible. */
-void census_window_stats_get_sums (const struct census_window_stats *wstats, const gpr_timespec when, struct census_window_stats_sum sums[]);
+void census_window_stats_get_sums(const struct census_window_stats *wstats,
+ const gpr_timespec when,
+ struct census_window_stats_sum sums[]);
/* Destroy a window_stats object. Once this function has been called, the
object will no longer be usable from any of the above functions (and
calling them will most likely result in a NULL-pointer dereference or
assertion failure). This function is thread-compatible. */
-void census_window_stats_destroy (struct census_window_stats *wstats);
+void census_window_stats_destroy(struct census_window_stats *wstats);
#endif /* GRPC_INTERNAL_CORE_STATISTICS_WINDOW_STATS_H */
diff --git a/src/core/support/alloc.c b/src/core/support/alloc.c
index d83a1684e6..d2ed82e771 100644
--- a/src/core/support/alloc.c
+++ b/src/core/support/alloc.c
@@ -36,47 +36,31 @@
#include <stdlib.h>
#include <grpc/support/port_platform.h>
-void *
-gpr_malloc (size_t size)
-{
- void *p = malloc (size);
- if (!p)
- {
- abort ();
- }
+void *gpr_malloc(size_t size) {
+ void *p = malloc(size);
+ if (!p) {
+ abort();
+ }
return p;
}
-void
-gpr_free (void *p)
-{
- free (p);
-}
+void gpr_free(void *p) { free(p); }
-void *
-gpr_realloc (void *p, size_t size)
-{
- p = realloc (p, size);
- if (!p)
- {
- abort ();
- }
+void *gpr_realloc(void *p, size_t size) {
+ p = realloc(p, size);
+ if (!p) {
+ abort();
+ }
return p;
}
-void *
-gpr_malloc_aligned (size_t size, size_t alignment_log)
-{
- size_t alignment = ((size_t) 1) << alignment_log;
- size_t extra = alignment - 1 + sizeof (void *);
- void *p = gpr_malloc (size + extra);
- void **ret = (void **) (((gpr_uintptr) p + extra) & ~(alignment - 1));
+void *gpr_malloc_aligned(size_t size, size_t alignment_log) {
+ size_t alignment = ((size_t)1) << alignment_log;
+ size_t extra = alignment - 1 + sizeof(void *);
+ void *p = gpr_malloc(size + extra);
+ void **ret = (void **)(((gpr_uintptr)p + extra) & ~(alignment - 1));
ret[-1] = p;
- return (void *) ret;
+ return (void *)ret;
}
-void
-gpr_free_aligned (void *ptr)
-{
- free (((void **) ptr)[-1]);
-}
+void gpr_free_aligned(void *ptr) { free(((void **)ptr)[-1]); }
diff --git a/src/core/support/cmdline.c b/src/core/support/cmdline.c
index 1975212385..45a3182f73 100644
--- a/src/core/support/cmdline.c
+++ b/src/core/support/cmdline.c
@@ -42,11 +42,9 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-typedef enum
-{ ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype;
+typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype;
-typedef struct arg
-{
+typedef struct arg {
const char *name;
const char *help;
argtype type;
@@ -54,28 +52,25 @@ typedef struct arg
struct arg *next;
} arg;
-struct gpr_cmdline
-{
+struct gpr_cmdline {
const char *description;
arg *args;
const char *argv0;
const char *extra_arg_name;
const char *extra_arg_help;
- void (*extra_arg) (void *user_data, const char *arg);
+ void (*extra_arg)(void *user_data, const char *arg);
void *extra_arg_user_data;
- void (*state) (gpr_cmdline * cl, char *arg);
+ void (*state)(gpr_cmdline *cl, char *arg);
arg *cur_arg;
};
-static void normal_state (gpr_cmdline * cl, char *arg);
+static void normal_state(gpr_cmdline *cl, char *arg);
-gpr_cmdline *
-gpr_cmdline_create (const char *description)
-{
- gpr_cmdline *cl = gpr_malloc (sizeof (gpr_cmdline));
- memset (cl, 0, sizeof (gpr_cmdline));
+gpr_cmdline *gpr_cmdline_create(const char *description) {
+ gpr_cmdline *cl = gpr_malloc(sizeof(gpr_cmdline));
+ memset(cl, 0, sizeof(gpr_cmdline));
cl->description = description;
cl->state = normal_state;
@@ -83,30 +78,25 @@ gpr_cmdline_create (const char *description)
return cl;
}
-void
-gpr_cmdline_destroy (gpr_cmdline * cl)
-{
- while (cl->args)
- {
- arg *a = cl->args;
- cl->args = a->next;
- gpr_free (a);
- }
- gpr_free (cl);
+void gpr_cmdline_destroy(gpr_cmdline *cl) {
+ while (cl->args) {
+ arg *a = cl->args;
+ cl->args = a->next;
+ gpr_free(a);
+ }
+ gpr_free(cl);
}
-static void
-add_arg (gpr_cmdline * cl, const char *name, const char *help, argtype type, void *value)
-{
+static void add_arg(gpr_cmdline *cl, const char *name, const char *help,
+ argtype type, void *value) {
arg *a;
- for (a = cl->args; a; a = a->next)
- {
- GPR_ASSERT (0 != strcmp (a->name, name));
- }
+ for (a = cl->args; a; a = a->next) {
+ GPR_ASSERT(0 != strcmp(a->name, name));
+ }
- a = gpr_malloc (sizeof (arg));
- memset (a, 0, sizeof (arg));
+ a = gpr_malloc(sizeof(arg));
+ memset(a, 0, sizeof(arg));
a->name = name;
a->help = help;
a->type = type;
@@ -115,29 +105,26 @@ add_arg (gpr_cmdline * cl, const char *name, const char *help, argtype type, voi
cl->args = a;
}
-void
-gpr_cmdline_add_int (gpr_cmdline * cl, const char *name, const char *help, int *value)
-{
- add_arg (cl, name, help, ARGTYPE_INT, value);
+void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name, const char *help,
+ int *value) {
+ add_arg(cl, name, help, ARGTYPE_INT, value);
}
-void
-gpr_cmdline_add_flag (gpr_cmdline * cl, const char *name, const char *help, int *value)
-{
- add_arg (cl, name, help, ARGTYPE_BOOL, value);
+void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help,
+ int *value) {
+ add_arg(cl, name, help, ARGTYPE_BOOL, value);
}
-void
-gpr_cmdline_add_string (gpr_cmdline * cl, const char *name, const char *help, char **value)
-{
- add_arg (cl, name, help, ARGTYPE_STRING, value);
+void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help,
+ char **value) {
+ add_arg(cl, name, help, ARGTYPE_STRING, value);
}
-void
-gpr_cmdline_on_extra_arg (gpr_cmdline * cl, const char *name, const char *help, void (*on_extra_arg) (void *user_data, const char *arg), void *user_data)
-{
- GPR_ASSERT (!cl->extra_arg);
- GPR_ASSERT (on_extra_arg);
+void gpr_cmdline_on_extra_arg(
+ gpr_cmdline *cl, const char *name, const char *help,
+ void (*on_extra_arg)(void *user_data, const char *arg), void *user_data) {
+ GPR_ASSERT(!cl->extra_arg);
+ GPR_ASSERT(on_extra_arg);
cl->extra_arg = on_extra_arg;
cl->extra_arg_user_data = user_data;
@@ -148,238 +135,189 @@ gpr_cmdline_on_extra_arg (gpr_cmdline * cl, const char *name, const char *help,
/* recursively descend argument list, adding the last element
to s first - so that arguments are added in the order they were
added to the list by api calls */
-static void
-add_args_to_usage (gpr_strvec * s, arg * a)
-{
+static void add_args_to_usage(gpr_strvec *s, arg *a) {
char *tmp;
- if (!a)
- return;
- add_args_to_usage (s, a->next);
+ if (!a) return;
+ add_args_to_usage(s, a->next);
- switch (a->type)
- {
+ switch (a->type) {
case ARGTYPE_BOOL:
- gpr_asprintf (&tmp, " [--%s|--no-%s]", a->name, a->name);
- gpr_strvec_add (s, tmp);
+ gpr_asprintf(&tmp, " [--%s|--no-%s]", a->name, a->name);
+ gpr_strvec_add(s, tmp);
break;
case ARGTYPE_STRING:
- gpr_asprintf (&tmp, " [--%s=string]", a->name);
- gpr_strvec_add (s, tmp);
+ gpr_asprintf(&tmp, " [--%s=string]", a->name);
+ gpr_strvec_add(s, tmp);
break;
case ARGTYPE_INT:
- gpr_asprintf (&tmp, " [--%s=int]", a->name);
- gpr_strvec_add (s, tmp);
+ gpr_asprintf(&tmp, " [--%s=int]", a->name);
+ gpr_strvec_add(s, tmp);
break;
- }
+ }
}
-char *
-gpr_cmdline_usage_string (gpr_cmdline * cl, const char *argv0)
-{
+char *gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0) {
/* TODO(ctiller): make this prettier */
gpr_strvec s;
char *tmp;
- const char *name = strrchr (argv0, '/');
-
- if (name)
- {
- name++;
- }
- else
- {
- name = argv0;
- }
-
- gpr_strvec_init (&s);
-
- gpr_asprintf (&tmp, "Usage: %s", name);
- gpr_strvec_add (&s, tmp);
- add_args_to_usage (&s, cl->args);
- if (cl->extra_arg)
- {
- gpr_asprintf (&tmp, " [%s...]", cl->extra_arg_name);
- gpr_strvec_add (&s, tmp);
- }
- gpr_strvec_add (&s, gpr_strdup ("\n"));
-
- tmp = gpr_strvec_flatten (&s, NULL);
- gpr_strvec_destroy (&s);
+ const char *name = strrchr(argv0, '/');
+
+ if (name) {
+ name++;
+ } else {
+ name = argv0;
+ }
+
+ gpr_strvec_init(&s);
+
+ gpr_asprintf(&tmp, "Usage: %s", name);
+ gpr_strvec_add(&s, tmp);
+ add_args_to_usage(&s, cl->args);
+ if (cl->extra_arg) {
+ gpr_asprintf(&tmp, " [%s...]", cl->extra_arg_name);
+ gpr_strvec_add(&s, tmp);
+ }
+ gpr_strvec_add(&s, gpr_strdup("\n"));
+
+ tmp = gpr_strvec_flatten(&s, NULL);
+ gpr_strvec_destroy(&s);
return tmp;
}
-static void
-print_usage_and_die (gpr_cmdline * cl)
-{
- char *usage = gpr_cmdline_usage_string (cl, cl->argv0);
- fprintf (stderr, "%s", usage);
- gpr_free (usage);
- exit (1);
+static void print_usage_and_die(gpr_cmdline *cl) {
+ char *usage = gpr_cmdline_usage_string(cl, cl->argv0);
+ fprintf(stderr, "%s", usage);
+ gpr_free(usage);
+ exit(1);
}
-static void
-extra_state (gpr_cmdline * cl, char *arg)
-{
- if (!cl->extra_arg)
- print_usage_and_die (cl);
- cl->extra_arg (cl->extra_arg_user_data, arg);
+static void extra_state(gpr_cmdline *cl, char *arg) {
+ if (!cl->extra_arg) print_usage_and_die(cl);
+ cl->extra_arg(cl->extra_arg_user_data, arg);
}
-static arg *
-find_arg (gpr_cmdline * cl, char *name)
-{
+static arg *find_arg(gpr_cmdline *cl, char *name) {
arg *a;
- for (a = cl->args; a; a = a->next)
- {
- if (0 == strcmp (a->name, name))
- {
- break;
- }
+ for (a = cl->args; a; a = a->next) {
+ if (0 == strcmp(a->name, name)) {
+ break;
}
+ }
- if (!a)
- {
- fprintf (stderr, "Unknown argument: %s\n", name);
- print_usage_and_die (cl);
- }
+ if (!a) {
+ fprintf(stderr, "Unknown argument: %s\n", name);
+ print_usage_and_die(cl);
+ }
return a;
}
-static void
-value_state (gpr_cmdline * cl, char *arg)
-{
+static void value_state(gpr_cmdline *cl, char *arg) {
long intval;
char *end;
- GPR_ASSERT (cl->cur_arg);
+ GPR_ASSERT(cl->cur_arg);
- switch (cl->cur_arg->type)
- {
+ switch (cl->cur_arg->type) {
case ARGTYPE_INT:
- intval = strtol (arg, &end, 0);
- if (*end || intval < INT_MIN || intval > INT_MAX)
- {
- fprintf (stderr, "expected integer, got '%s' for %s\n", arg, cl->cur_arg->name);
- print_usage_and_die (cl);
- }
- *(int *) cl->cur_arg->value = (int) intval;
+ intval = strtol(arg, &end, 0);
+ if (*end || intval < INT_MIN || intval > INT_MAX) {
+ fprintf(stderr, "expected integer, got '%s' for %s\n", arg,
+ cl->cur_arg->name);
+ print_usage_and_die(cl);
+ }
+ *(int *)cl->cur_arg->value = (int)intval;
break;
case ARGTYPE_BOOL:
- if (0 == strcmp (arg, "1") || 0 == strcmp (arg, "true"))
- {
- *(int *) cl->cur_arg->value = 1;
- }
- else if (0 == strcmp (arg, "0") || 0 == strcmp (arg, "false"))
- {
- *(int *) cl->cur_arg->value = 0;
- }
- else
- {
- fprintf (stderr, "expected boolean, got '%s' for %s\n", arg, cl->cur_arg->name);
- print_usage_and_die (cl);
- }
+ if (0 == strcmp(arg, "1") || 0 == strcmp(arg, "true")) {
+ *(int *)cl->cur_arg->value = 1;
+ } else if (0 == strcmp(arg, "0") || 0 == strcmp(arg, "false")) {
+ *(int *)cl->cur_arg->value = 0;
+ } else {
+ fprintf(stderr, "expected boolean, got '%s' for %s\n", arg,
+ cl->cur_arg->name);
+ print_usage_and_die(cl);
+ }
break;
case ARGTYPE_STRING:
- *(char **) cl->cur_arg->value = arg;
+ *(char **)cl->cur_arg->value = arg;
break;
- }
+ }
cl->state = normal_state;
}
-static void
-normal_state (gpr_cmdline * cl, char *arg)
-{
+static void normal_state(gpr_cmdline *cl, char *arg) {
char *eq = NULL;
char *tmp = NULL;
char *arg_name = NULL;
- if (0 == strcmp (arg, "-help") || 0 == strcmp (arg, "--help") || 0 == strcmp (arg, "-h"))
- {
- print_usage_and_die (cl);
- }
+ if (0 == strcmp(arg, "-help") || 0 == strcmp(arg, "--help") ||
+ 0 == strcmp(arg, "-h")) {
+ print_usage_and_die(cl);
+ }
cl->cur_arg = NULL;
- if (arg[0] == '-')
- {
- if (arg[1] == '-')
- {
- if (arg[2] == 0)
- {
- /* handle '--' to move to just extra args */
- cl->state = extra_state;
- return;
- }
- arg += 2;
- }
- else
- {
- arg += 1;
- }
- /* first byte of arg is now past the leading '-' or '--' */
- if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-')
- {
- /* arg is of the form '--no-foo' - it's a flag disable */
- arg += 3;
- cl->cur_arg = find_arg (cl, arg);
- if (cl->cur_arg->type != ARGTYPE_BOOL)
- {
- fprintf (stderr, "%s is not a flag argument\n", arg);
- print_usage_and_die (cl);
- }
- *(int *) cl->cur_arg->value = 0;
- return; /* early out */
- }
- eq = strchr (arg, '=');
- if (eq != NULL)
- {
- /* copy the string into a temp buffer and extract the name */
- tmp = arg_name = gpr_malloc ((size_t) (eq - arg + 1));
- memcpy (arg_name, arg, (size_t) (eq - arg));
- arg_name[eq - arg] = 0;
- }
- else
- {
- arg_name = arg;
- }
- cl->cur_arg = find_arg (cl, arg_name);
- if (eq != NULL)
- {
- /* arg was of the type --foo=value, parse the value */
- value_state (cl, eq + 1);
- }
- else if (cl->cur_arg->type != ARGTYPE_BOOL)
- {
- /* flag types don't have a '--foo value' variant, other types do */
- cl->state = value_state;
- }
- else
- {
- /* flag parameter: just set the value */
- *(int *) cl->cur_arg->value = 1;
- }
+ if (arg[0] == '-') {
+ if (arg[1] == '-') {
+ if (arg[2] == 0) {
+ /* handle '--' to move to just extra args */
+ cl->state = extra_state;
+ return;
+ }
+ arg += 2;
+ } else {
+ arg += 1;
+ }
+ /* first byte of arg is now past the leading '-' or '--' */
+ if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-') {
+ /* arg is of the form '--no-foo' - it's a flag disable */
+ arg += 3;
+ cl->cur_arg = find_arg(cl, arg);
+ if (cl->cur_arg->type != ARGTYPE_BOOL) {
+ fprintf(stderr, "%s is not a flag argument\n", arg);
+ print_usage_and_die(cl);
+ }
+ *(int *)cl->cur_arg->value = 0;
+ return; /* early out */
}
- else
- {
- extra_state (cl, arg);
+ eq = strchr(arg, '=');
+ if (eq != NULL) {
+ /* copy the string into a temp buffer and extract the name */
+ tmp = arg_name = gpr_malloc((size_t)(eq - arg + 1));
+ memcpy(arg_name, arg, (size_t)(eq - arg));
+ arg_name[eq - arg] = 0;
+ } else {
+ arg_name = arg;
}
+ cl->cur_arg = find_arg(cl, arg_name);
+ if (eq != NULL) {
+ /* arg was of the type --foo=value, parse the value */
+ value_state(cl, eq + 1);
+ } else if (cl->cur_arg->type != ARGTYPE_BOOL) {
+ /* flag types don't have a '--foo value' variant, other types do */
+ cl->state = value_state;
+ } else {
+ /* flag parameter: just set the value */
+ *(int *)cl->cur_arg->value = 1;
+ }
+ } else {
+ extra_state(cl, arg);
+ }
- gpr_free (tmp);
+ gpr_free(tmp);
}
-void
-gpr_cmdline_parse (gpr_cmdline * cl, int argc, char **argv)
-{
+void gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv) {
int i;
- GPR_ASSERT (argc >= 1);
+ GPR_ASSERT(argc >= 1);
cl->argv0 = argv[0];
- for (i = 1; i < argc; i++)
- {
- cl->state (cl, argv[i]);
- }
+ for (i = 1; i < argc; i++) {
+ cl->state(cl, argv[i]);
+ }
}
diff --git a/src/core/support/cpu_iphone.c b/src/core/support/cpu_iphone.c
index 0d679414ac..82b49b47bc 100644
--- a/src/core/support/cpu_iphone.c
+++ b/src/core/support/cpu_iphone.c
@@ -36,11 +36,7 @@
#ifdef GPR_CPU_IPHONE
/* Probably 2 instead of 1, but see comment on gpr_cpu_current_cpu. */
-unsigned
-gpr_cpu_num_cores (void)
-{
- return 1;
-}
+unsigned gpr_cpu_num_cores(void) { return 1; }
/* Most code that's using this is using it to shard across work queues. So
unless profiling shows it's a problem or there appears a way to detect the
@@ -48,10 +44,6 @@ gpr_cpu_num_cores (void)
Note that the interface in cpu.h lets gpr_cpu_num_cores return 0, but doing
it makes it impossible for gpr_cpu_current_cpu to satisfy its stated range,
and some code might be relying on it. */
-unsigned
-gpr_cpu_current_cpu (void)
-{
- return 0;
-}
+unsigned gpr_cpu_current_cpu(void) { return 0; }
#endif /* GPR_CPU_IPHONE */
diff --git a/src/core/support/cpu_linux.c b/src/core/support/cpu_linux.c
index b969218ba9..7af6a8f009 100644
--- a/src/core/support/cpu_linux.c
+++ b/src/core/support/cpu_linux.c
@@ -50,37 +50,29 @@
static int ncpus = 0;
-static void
-init_num_cpus ()
-{
+static void init_num_cpus() {
/* This must be signed. sysconf returns -1 when the number cannot be
determined */
- ncpus = (int) sysconf (_SC_NPROCESSORS_ONLN);
- if (ncpus < 1)
- {
- gpr_log (GPR_ERROR, "Cannot determine number of CPUs: assuming 1");
- ncpus = 1;
- }
+ ncpus = (int)sysconf(_SC_NPROCESSORS_ONLN);
+ if (ncpus < 1) {
+ gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1");
+ ncpus = 1;
+ }
}
-unsigned
-gpr_cpu_num_cores (void)
-{
+unsigned gpr_cpu_num_cores(void) {
static gpr_once once = GPR_ONCE_INIT;
- gpr_once_init (&once, init_num_cpus);
- return (unsigned) ncpus;
+ gpr_once_init(&once, init_num_cpus);
+ return (unsigned)ncpus;
}
-unsigned
-gpr_cpu_current_cpu (void)
-{
- int cpu = sched_getcpu ();
- if (cpu < 0)
- {
- gpr_log (GPR_ERROR, "Error determining current CPU: %s\n", strerror (errno));
- return 0;
- }
- return (unsigned) cpu;
+unsigned gpr_cpu_current_cpu(void) {
+ int cpu = sched_getcpu();
+ if (cpu < 0) {
+ gpr_log(GPR_ERROR, "Error determining current CPU: %s\n", strerror(errno));
+ return 0;
+ }
+ return (unsigned)cpu;
}
#endif /* GPR_CPU_LINUX */
diff --git a/src/core/support/cpu_posix.c b/src/core/support/cpu_posix.c
index 22282cae9f..55d92c0555 100644
--- a/src/core/support/cpu_posix.c
+++ b/src/core/support/cpu_posix.c
@@ -46,41 +46,32 @@ static __thread char magic_thread_local;
static long ncpus = 0;
-static void
-init_ncpus ()
-{
- ncpus = sysconf (_SC_NPROCESSORS_ONLN);
- if (ncpus < 1 || ncpus > GPR_UINT32_MAX)
- {
- gpr_log (GPR_ERROR, "Cannot determine number of CPUs: assuming 1");
- ncpus = 1;
- }
+static void init_ncpus() {
+ ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+ if (ncpus < 1 || ncpus > GPR_UINT32_MAX) {
+ gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1");
+ ncpus = 1;
+ }
}
-unsigned
-gpr_cpu_num_cores (void)
-{
+unsigned gpr_cpu_num_cores(void) {
static gpr_once once = GPR_ONCE_INIT;
- gpr_once_init (&once, init_ncpus);
- return (unsigned) ncpus;
+ gpr_once_init(&once, init_ncpus);
+ return (unsigned)ncpus;
}
/* This is a cheap, but good enough, pointer hash for sharding things: */
-static size_t
-shard_ptr (const void *info)
-{
- size_t x = (size_t) info;
- return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) % gpr_cpu_num_cores ();
+static size_t shard_ptr(const void *info) {
+ size_t x = (size_t)info;
+ return ((x >> 4) ^ (x >> 9) ^ (x >> 14)) % gpr_cpu_num_cores();
}
-unsigned
-gpr_cpu_current_cpu (void)
-{
+unsigned gpr_cpu_current_cpu(void) {
/* NOTE: there's no way I know to return the actual cpu index portably...
most code that's using this is using it to shard across work queues though,
so here we use thread identity instead to achieve a similar though not
identical effect */
- return (unsigned) shard_ptr (&magic_thread_local);
+ return (unsigned)shard_ptr(&magic_thread_local);
}
#endif /* GPR_CPU_POSIX */
diff --git a/src/core/support/cpu_windows.c b/src/core/support/cpu_windows.c
index e6696db913..ce32eb0a9d 100644
--- a/src/core/support/cpu_windows.c
+++ b/src/core/support/cpu_windows.c
@@ -36,18 +36,12 @@
#ifdef GPR_WIN32
#include <grpc/support/log.h>
-unsigned
-gpr_cpu_num_cores (void)
-{
+unsigned gpr_cpu_num_cores(void) {
SYSTEM_INFO si;
- GetSystemInfo (&si);
+ GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}
-unsigned
-gpr_cpu_current_cpu (void)
-{
- return GetCurrentProcessorNumber ();
-}
+unsigned gpr_cpu_current_cpu(void) { return GetCurrentProcessorNumber(); }
#endif /* GPR_WIN32 */
diff --git a/src/core/support/env.h b/src/core/support/env.h
index 69887b2c49..24172d8673 100644
--- a/src/core/support/env.h
+++ b/src/core/support/env.h
@@ -39,8 +39,7 @@
#include <grpc/support/slice.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Env utility functions */
@@ -49,13 +48,13 @@ extern "C"
Returns a newly allocated string. It is the responsability of the caller to
gpr_free the return value if not NULL (which means that the environment
variable exists). */
- char *gpr_getenv (const char *name);
+char *gpr_getenv(const char *name);
/* Sets the the environment with the specified name to the specified value. */
- void gpr_setenv (const char *name, const char *value);
+void gpr_setenv(const char *name, const char *value);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_SUPPORT_ENV_H */
+#endif /* GRPC_INTERNAL_CORE_SUPPORT_ENV_H */
diff --git a/src/core/support/env_linux.c b/src/core/support/env_linux.c
index cdb6c75950..2e03365e33 100644
--- a/src/core/support/env_linux.c
+++ b/src/core/support/env_linux.c
@@ -49,18 +49,14 @@
#include "src/core/support/string.h"
-char *
-gpr_getenv (const char *name)
-{
- char *result = secure_getenv (name);
- return result == NULL ? result : gpr_strdup (result);
+char *gpr_getenv(const char *name) {
+ char *result = secure_getenv(name);
+ return result == NULL ? result : gpr_strdup(result);
}
-void
-gpr_setenv (const char *name, const char *value)
-{
- int res = setenv (name, value, 1);
- GPR_ASSERT (res == 0);
+void gpr_setenv(const char *name, const char *value) {
+ int res = setenv(name, value, 1);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_LINUX_ENV */
diff --git a/src/core/support/env_posix.c b/src/core/support/env_posix.c
index e30b67ed20..1dd2af56bc 100644
--- a/src/core/support/env_posix.c
+++ b/src/core/support/env_posix.c
@@ -44,18 +44,14 @@
#include "src/core/support/string.h"
#include <grpc/support/string_util.h>
-char *
-gpr_getenv (const char *name)
-{
- char *result = getenv (name);
- return result == NULL ? result : gpr_strdup (result);
+char *gpr_getenv(const char *name) {
+ char *result = getenv(name);
+ return result == NULL ? result : gpr_strdup(result);
}
-void
-gpr_setenv (const char *name, const char *value)
-{
- int res = setenv (name, value, 1);
- GPR_ASSERT (res == 0);
+void gpr_setenv(const char *name, const char *value) {
+ int res = setenv(name, value, 1);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_POSIX_ENV */
diff --git a/src/core/support/env_win32.c b/src/core/support/env_win32.c
index 976f031eb1..6b1ff102b0 100644
--- a/src/core/support/env_win32.c
+++ b/src/core/support/env_win32.c
@@ -44,27 +44,22 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-char *
-gpr_getenv (const char *name)
-{
+char *gpr_getenv(const char *name) {
size_t size;
char *result = NULL;
char *duplicated;
errno_t err;
- err = _dupenv_s (&result, &size, name);
- if (err)
- return NULL;
- duplicated = gpr_strdup (result);
- free (result);
+ err = _dupenv_s(&result, &size, name);
+ if (err) return NULL;
+ duplicated = gpr_strdup(result);
+ free(result);
return duplicated;
}
-void
-gpr_setenv (const char *name, const char *value)
-{
- errno_t res = _putenv_s (name, value);
- GPR_ASSERT (res == 0);
+void gpr_setenv(const char *name, const char *value) {
+ errno_t res = _putenv_s(name, value);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_WIN32 */
diff --git a/src/core/support/file.c b/src/core/support/file.c
index 96ca7e19da..c1361d8a9e 100644
--- a/src/core/support/file.c
+++ b/src/core/support/file.c
@@ -42,52 +42,46 @@
#include "src/core/support/string.h"
-gpr_slice
-gpr_load_file (const char *filename, int add_null_terminator, int *success)
-{
+gpr_slice gpr_load_file(const char *filename, int add_null_terminator,
+ int *success) {
unsigned char *contents = NULL;
size_t contents_size = 0;
char *error_msg = NULL;
- gpr_slice result = gpr_empty_slice ();
- FILE *file = fopen (filename, "rb");
+ gpr_slice result = gpr_empty_slice();
+ FILE *file = fopen(filename, "rb");
size_t bytes_read = 0;
- if (file == NULL)
- {
- gpr_asprintf (&error_msg, "Could not open file %s (error = %s).", filename, strerror (errno));
- GPR_ASSERT (error_msg != NULL);
- goto end;
- }
- fseek (file, 0, SEEK_END);
+ if (file == NULL) {
+ gpr_asprintf(&error_msg, "Could not open file %s (error = %s).", filename,
+ strerror(errno));
+ GPR_ASSERT(error_msg != NULL);
+ goto end;
+ }
+ fseek(file, 0, SEEK_END);
/* Converting to size_t on the assumption that it will not fail */
- contents_size = (size_t) ftell (file);
- fseek (file, 0, SEEK_SET);
- contents = gpr_malloc (contents_size + (add_null_terminator ? 1 : 0));
- bytes_read = fread (contents, 1, contents_size, file);
- if (bytes_read < contents_size)
- {
- GPR_ASSERT (ferror (file));
- gpr_asprintf (&error_msg, "Error %s occured while reading file %s.", strerror (errno), filename);
- GPR_ASSERT (error_msg != NULL);
- goto end;
- }
- if (success != NULL)
- *success = 1;
- if (add_null_terminator)
- {
- contents[contents_size++] = 0;
- }
- result = gpr_slice_new (contents, contents_size, gpr_free);
+ contents_size = (size_t)ftell(file);
+ fseek(file, 0, SEEK_SET);
+ contents = gpr_malloc(contents_size + (add_null_terminator ? 1 : 0));
+ bytes_read = fread(contents, 1, contents_size, file);
+ if (bytes_read < contents_size) {
+ GPR_ASSERT(ferror(file));
+ gpr_asprintf(&error_msg, "Error %s occured while reading file %s.",
+ strerror(errno), filename);
+ GPR_ASSERT(error_msg != NULL);
+ goto end;
+ }
+ if (success != NULL) *success = 1;
+ if (add_null_terminator) {
+ contents[contents_size++] = 0;
+ }
+ result = gpr_slice_new(contents, contents_size, gpr_free);
end:
- if (error_msg != NULL)
- {
- gpr_log (GPR_ERROR, "%s", error_msg);
- gpr_free (error_msg);
- if (success != NULL)
- *success = 0;
- }
- if (file != NULL)
- fclose (file);
+ if (error_msg != NULL) {
+ gpr_log(GPR_ERROR, "%s", error_msg);
+ gpr_free(error_msg);
+ if (success != NULL) *success = 0;
+ }
+ if (file != NULL) fclose(file);
return result;
}
diff --git a/src/core/support/file.h b/src/core/support/file.h
index 9b42f09f76..d8b7cea44f 100644
--- a/src/core/support/file.h
+++ b/src/core/support/file.h
@@ -39,8 +39,7 @@
#include <grpc/support/slice.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* File utility functions */
@@ -48,16 +47,17 @@ extern "C"
/* Loads the content of a file into a slice. add_null_terminator will add
a NULL terminator if non-zero. The success parameter, if not NULL,
will be set to 1 in case of success and 0 in case of failure. */
- gpr_slice gpr_load_file (const char *filename, int add_null_terminator, int *success);
+gpr_slice gpr_load_file(const char *filename, int add_null_terminator,
+ int *success);
/* Creates a temporary file from a prefix.
If tmp_filename is not NULL, *tmp_filename is assigned the name of the
created file and it is the responsibility of the caller to gpr_free it
unless an error occurs in which case it will be set to NULL. */
- FILE *gpr_tmpfile (const char *prefix, char **tmp_filename);
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_SUPPORT_FILE_H */
+#endif /* GRPC_INTERNAL_CORE_SUPPORT_FILE_H */
diff --git a/src/core/support/file_posix.c b/src/core/support/file_posix.c
index d68a334eea..c11c07148a 100644
--- a/src/core/support/file_posix.c
+++ b/src/core/support/file_posix.c
@@ -48,43 +48,37 @@
#include "src/core/support/string.h"
-FILE *
-gpr_tmpfile (const char *prefix, char **tmp_filename)
-{
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) {
FILE *result = NULL;
char *template;
int fd;
- if (tmp_filename != NULL)
- *tmp_filename = NULL;
+ if (tmp_filename != NULL) *tmp_filename = NULL;
- gpr_asprintf (&template, "/tmp/%s_XXXXXX", prefix);
- GPR_ASSERT (template != NULL);
+ gpr_asprintf(&template, "/tmp/%s_XXXXXX", prefix);
+ GPR_ASSERT(template != NULL);
- fd = mkstemp (template);
- if (fd == -1)
- {
- gpr_log (GPR_ERROR, "mkstemp failed for template %s with error %s.", template, strerror (errno));
- goto end;
- }
- result = fdopen (fd, "w+");
- if (result == NULL)
- {
- gpr_log (GPR_ERROR, "Could not open file %s from fd %d (error = %s).", template, fd, strerror (errno));
- unlink (template);
- close (fd);
- goto end;
- }
+ fd = mkstemp(template);
+ if (fd == -1) {
+ gpr_log(GPR_ERROR, "mkstemp failed for template %s with error %s.",
+ template, strerror(errno));
+ goto end;
+ }
+ result = fdopen(fd, "w+");
+ if (result == NULL) {
+ gpr_log(GPR_ERROR, "Could not open file %s from fd %d (error = %s).",
+ template, fd, strerror(errno));
+ unlink(template);
+ close(fd);
+ goto end;
+ }
end:
- if (result != NULL && tmp_filename != NULL)
- {
- *tmp_filename = template;
- }
- else
- {
- gpr_free (template);
- }
+ if (result != NULL && tmp_filename != NULL) {
+ *tmp_filename = template;
+ } else {
+ gpr_free(template);
+ }
return result;
}
diff --git a/src/core/support/file_win32.c b/src/core/support/file_win32.c
index 26c1c2664f..355744f79a 100644
--- a/src/core/support/file_win32.c
+++ b/src/core/support/file_win32.c
@@ -47,9 +47,7 @@
#include "src/core/support/file.h"
#include "src/core/support/string_win32.h"
-FILE *
-gpr_tmpfile (const char *prefix, char **tmp_filename_out)
-{
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) {
FILE *result = NULL;
LPTSTR template_string = NULL;
TCHAR tmp_path[MAX_PATH];
@@ -57,34 +55,29 @@ gpr_tmpfile (const char *prefix, char **tmp_filename_out)
DWORD status;
UINT success;
- if (tmp_filename_out != NULL)
- *tmp_filename_out = NULL;
+ if (tmp_filename_out != NULL) *tmp_filename_out = NULL;
/* Convert our prefix to TCHAR. */
- template_string = gpr_char_to_tchar (prefix);
- GPR_ASSERT (template_string);
+ template_string = gpr_char_to_tchar(prefix);
+ GPR_ASSERT(template_string);
/* Get the path to the best temporary folder available. */
- status = GetTempPath (MAX_PATH, tmp_path);
- if (status == 0 || status > MAX_PATH)
- goto end;
+ status = GetTempPath(MAX_PATH, tmp_path);
+ if (status == 0 || status > MAX_PATH) goto end;
/* Generate a unique filename with our template + temporary path. */
- success = GetTempFileName (tmp_path, template_string, 0, tmp_filename);
- if (!success)
- goto end;
+ success = GetTempFileName(tmp_path, template_string, 0, tmp_filename);
+ if (!success) goto end;
/* Open a file there. */
- if (_tfopen_s (&result, tmp_filename, TEXT ("wb+")) != 0)
- goto end;
+ if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end;
end:
- if (result && tmp_filename_out)
- {
- *tmp_filename_out = gpr_tchar_to_char (tmp_filename);
- }
+ if (result && tmp_filename_out) {
+ *tmp_filename_out = gpr_tchar_to_char(tmp_filename);
+ }
- gpr_free (template_string);
+ gpr_free(template_string);
return result;
}
diff --git a/src/core/support/histogram.c b/src/core/support/histogram.c
index 0383c5433b..78dbf98684 100644
--- a/src/core/support/histogram.c
+++ b/src/core/support/histogram.c
@@ -47,8 +47,7 @@
Bucket n (n>=1) contains [m**n, m**(n+1))
There are sufficient buckets to reach max_bucket_start */
-struct gpr_histogram
-{
+struct gpr_histogram {
/* Sum of all values seen so far */
double sum;
/* Sum of squares of all values seen so far */
@@ -71,224 +70,175 @@ struct gpr_histogram
};
/* determine a bucket index given a value - does no bounds checking */
-static size_t
-bucket_for_unchecked (gpr_histogram * h, double x)
-{
- return (size_t) (log (x) * h->one_on_log_multiplier);
+static size_t bucket_for_unchecked(gpr_histogram *h, double x) {
+ return (size_t)(log(x) * h->one_on_log_multiplier);
}
/* bounds checked version of the above */
-static size_t
-bucket_for (gpr_histogram * h, double x)
-{
- size_t bucket = bucket_for_unchecked (h, GPR_CLAMP (x, 1.0, h->max_possible));
- GPR_ASSERT (bucket < h->num_buckets);
+static size_t bucket_for(gpr_histogram *h, double x) {
+ size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 1.0, h->max_possible));
+ GPR_ASSERT(bucket < h->num_buckets);
return bucket;
}
/* at what value does a bucket start? */
-static double
-bucket_start (gpr_histogram * h, double x)
-{
- return pow (h->multiplier, x);
+static double bucket_start(gpr_histogram *h, double x) {
+ return pow(h->multiplier, x);
}
-gpr_histogram *
-gpr_histogram_create (double resolution, double max_bucket_start)
-{
- gpr_histogram *h = gpr_malloc (sizeof (gpr_histogram));
- GPR_ASSERT (resolution > 0.0);
- GPR_ASSERT (max_bucket_start > resolution);
+gpr_histogram *gpr_histogram_create(double resolution,
+ double max_bucket_start) {
+ gpr_histogram *h = gpr_malloc(sizeof(gpr_histogram));
+ GPR_ASSERT(resolution > 0.0);
+ GPR_ASSERT(max_bucket_start > resolution);
h->sum = 0.0;
h->sum_of_squares = 0.0;
h->multiplier = 1.0 + resolution;
- h->one_on_log_multiplier = 1.0 / log (1.0 + resolution);
+ h->one_on_log_multiplier = 1.0 / log(1.0 + resolution);
h->max_possible = max_bucket_start;
h->count = 0.0;
h->min_seen = max_bucket_start;
h->max_seen = 0.0;
- h->num_buckets = bucket_for_unchecked (h, max_bucket_start) + 1;
- GPR_ASSERT (h->num_buckets > 1);
- GPR_ASSERT (h->num_buckets < 100000000);
- h->buckets = gpr_malloc (sizeof (gpr_uint32) * h->num_buckets);
- memset (h->buckets, 0, sizeof (gpr_uint32) * h->num_buckets);
+ h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1;
+ GPR_ASSERT(h->num_buckets > 1);
+ GPR_ASSERT(h->num_buckets < 100000000);
+ h->buckets = gpr_malloc(sizeof(gpr_uint32) * h->num_buckets);
+ memset(h->buckets, 0, sizeof(gpr_uint32) * h->num_buckets);
return h;
}
-void
-gpr_histogram_destroy (gpr_histogram * h)
-{
- gpr_free (h->buckets);
- gpr_free (h);
+void gpr_histogram_destroy(gpr_histogram *h) {
+ gpr_free(h->buckets);
+ gpr_free(h);
}
-void
-gpr_histogram_add (gpr_histogram * h, double x)
-{
+void gpr_histogram_add(gpr_histogram *h, double x) {
h->sum += x;
h->sum_of_squares += x * x;
h->count++;
- if (x < h->min_seen)
- {
- h->min_seen = x;
- }
- if (x > h->max_seen)
- {
- h->max_seen = x;
- }
- h->buckets[bucket_for (h, x)]++;
-}
-
-int
-gpr_histogram_merge (gpr_histogram * dst, gpr_histogram * src)
-{
- if ((dst->num_buckets != src->num_buckets) || (dst->multiplier != src->multiplier))
- {
- /* Fail because these histograms don't match */
- return 0;
- }
- gpr_histogram_merge_contents (dst, src->buckets, src->num_buckets, src->min_seen, src->max_seen, src->sum, src->sum_of_squares, src->count);
+ if (x < h->min_seen) {
+ h->min_seen = x;
+ }
+ if (x > h->max_seen) {
+ h->max_seen = x;
+ }
+ h->buckets[bucket_for(h, x)]++;
+}
+
+int gpr_histogram_merge(gpr_histogram *dst, gpr_histogram *src) {
+ if ((dst->num_buckets != src->num_buckets) ||
+ (dst->multiplier != src->multiplier)) {
+ /* Fail because these histograms don't match */
+ return 0;
+ }
+ gpr_histogram_merge_contents(dst, src->buckets, src->num_buckets,
+ src->min_seen, src->max_seen, src->sum,
+ src->sum_of_squares, src->count);
return 1;
}
-void
-gpr_histogram_merge_contents (gpr_histogram * dst, const gpr_uint32 * data, size_t data_count, double min_seen, double max_seen, double sum, double sum_of_squares, double count)
-{
+void gpr_histogram_merge_contents(gpr_histogram *dst, const gpr_uint32 *data,
+ size_t data_count, double min_seen,
+ double max_seen, double sum,
+ double sum_of_squares, double count) {
size_t i;
- GPR_ASSERT (dst->num_buckets == data_count);
+ GPR_ASSERT(dst->num_buckets == data_count);
dst->sum += sum;
dst->sum_of_squares += sum_of_squares;
dst->count += count;
- if (min_seen < dst->min_seen)
- {
- dst->min_seen = min_seen;
- }
- if (max_seen > dst->max_seen)
- {
- dst->max_seen = max_seen;
- }
- for (i = 0; i < dst->num_buckets; i++)
- {
- dst->buckets[i] += data[i];
- }
-}
-
-static double
-threshold_for_count_below (gpr_histogram * h, double count_below)
-{
+ if (min_seen < dst->min_seen) {
+ dst->min_seen = min_seen;
+ }
+ if (max_seen > dst->max_seen) {
+ dst->max_seen = max_seen;
+ }
+ for (i = 0; i < dst->num_buckets; i++) {
+ dst->buckets[i] += data[i];
+ }
+}
+
+static double threshold_for_count_below(gpr_histogram *h, double count_below) {
double count_so_far;
double lower_bound;
double upper_bound;
size_t lower_idx;
size_t upper_idx;
- if (h->count == 0)
- {
- return 0.0;
- }
+ if (h->count == 0) {
+ return 0.0;
+ }
- if (count_below <= 0)
- {
- return h->min_seen;
- }
- if (count_below >= h->count)
- {
- return h->max_seen;
- }
+ if (count_below <= 0) {
+ return h->min_seen;
+ }
+ if (count_below >= h->count) {
+ return h->max_seen;
+ }
/* find the lowest bucket that gets us above count_below */
count_so_far = 0.0;
- for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++)
- {
- count_so_far += h->buckets[lower_idx];
- if (count_so_far >= count_below)
- {
- break;
- }
- }
- if (count_so_far == count_below)
- {
- /* this bucket hits the threshold exactly... we should be midway through
- any run of zero values following the bucket */
- for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++)
- {
- if (h->buckets[upper_idx])
- {
- break;
- }
- }
- return (bucket_start (h, (double) lower_idx) + bucket_start (h, (double) upper_idx)) / 2.0;
+ for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) {
+ count_so_far += h->buckets[lower_idx];
+ if (count_so_far >= count_below) {
+ break;
}
- else
- {
- /* treat values as uniform throughout the bucket, and find where this value
- should lie */
- lower_bound = bucket_start (h, (double) lower_idx);
- upper_bound = bucket_start (h, (double) (lower_idx + 1));
- return GPR_CLAMP (upper_bound - (upper_bound - lower_bound) * (count_so_far - count_below) / h->buckets[lower_idx], h->min_seen, h->max_seen);
+ }
+ if (count_so_far == count_below) {
+ /* this bucket hits the threshold exactly... we should be midway through
+ any run of zero values following the bucket */
+ for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) {
+ if (h->buckets[upper_idx]) {
+ break;
+ }
}
-}
-
-double
-gpr_histogram_percentile (gpr_histogram * h, double percentile)
-{
- return threshold_for_count_below (h, h->count * percentile / 100.0);
-}
-
-double
-gpr_histogram_mean (gpr_histogram * h)
-{
- GPR_ASSERT (h->count);
+ return (bucket_start(h, (double)lower_idx) +
+ bucket_start(h, (double)upper_idx)) /
+ 2.0;
+ } else {
+ /* treat values as uniform throughout the bucket, and find where this value
+ should lie */
+ lower_bound = bucket_start(h, (double)lower_idx);
+ upper_bound = bucket_start(h, (double)(lower_idx + 1));
+ return GPR_CLAMP(upper_bound -
+ (upper_bound - lower_bound) *
+ (count_so_far - count_below) /
+ h->buckets[lower_idx],
+ h->min_seen, h->max_seen);
+ }
+}
+
+double gpr_histogram_percentile(gpr_histogram *h, double percentile) {
+ return threshold_for_count_below(h, h->count * percentile / 100.0);
+}
+
+double gpr_histogram_mean(gpr_histogram *h) {
+ GPR_ASSERT(h->count);
return h->sum / h->count;
}
-double
-gpr_histogram_stddev (gpr_histogram * h)
-{
- return sqrt (gpr_histogram_variance (h));
+double gpr_histogram_stddev(gpr_histogram *h) {
+ return sqrt(gpr_histogram_variance(h));
}
-double
-gpr_histogram_variance (gpr_histogram * h)
-{
- if (h->count == 0)
- return 0.0;
- return (h->sum_of_squares * h->count - h->sum * h->sum) / (h->count * h->count);
+double gpr_histogram_variance(gpr_histogram *h) {
+ if (h->count == 0) return 0.0;
+ return (h->sum_of_squares * h->count - h->sum * h->sum) /
+ (h->count * h->count);
}
-double
-gpr_histogram_maximum (gpr_histogram * h)
-{
- return h->max_seen;
-}
+double gpr_histogram_maximum(gpr_histogram *h) { return h->max_seen; }
-double
-gpr_histogram_minimum (gpr_histogram * h)
-{
- return h->min_seen;
-}
+double gpr_histogram_minimum(gpr_histogram *h) { return h->min_seen; }
-double
-gpr_histogram_count (gpr_histogram * h)
-{
- return h->count;
-}
+double gpr_histogram_count(gpr_histogram *h) { return h->count; }
-double
-gpr_histogram_sum (gpr_histogram * h)
-{
- return h->sum;
-}
+double gpr_histogram_sum(gpr_histogram *h) { return h->sum; }
-double
-gpr_histogram_sum_of_squares (gpr_histogram * h)
-{
+double gpr_histogram_sum_of_squares(gpr_histogram *h) {
return h->sum_of_squares;
}
-const gpr_uint32 *
-gpr_histogram_get_contents (gpr_histogram * h, size_t * size)
-{
+const gpr_uint32 *gpr_histogram_get_contents(gpr_histogram *h, size_t *size) {
*size = h->num_buckets;
return h->buckets;
}
diff --git a/src/core/support/host_port.c b/src/core/support/host_port.c
index a95b9d0d81..23f65b1581 100644
--- a/src/core/support/host_port.c
+++ b/src/core/support/host_port.c
@@ -40,24 +40,17 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-int
-gpr_join_host_port (char **out, const char *host, int port)
-{
- if (host[0] != '[' && strchr (host, ':') != NULL)
- {
- /* IPv6 literals must be enclosed in brackets. */
- return gpr_asprintf (out, "[%s]:%d", host, port);
- }
- else
- {
- /* Ordinary non-bracketed host:port. */
- return gpr_asprintf (out, "%s:%d", host, port);
- }
+int gpr_join_host_port(char **out, const char *host, int port) {
+ if (host[0] != '[' && strchr(host, ':') != NULL) {
+ /* IPv6 literals must be enclosed in brackets. */
+ return gpr_asprintf(out, "[%s]:%d", host, port);
+ } else {
+ /* Ordinary non-bracketed host:port. */
+ return gpr_asprintf(out, "%s:%d", host, port);
+ }
}
-int
-gpr_split_host_port (const char *name, char **host, char **port)
-{
+int gpr_split_host_port(const char *name, char **host, char **port) {
const char *host_start;
size_t host_len;
const char *port_start;
@@ -65,67 +58,53 @@ gpr_split_host_port (const char *name, char **host, char **port)
*host = NULL;
*port = NULL;
- if (name[0] == '[')
- {
- /* Parse a bracketed host, typically an IPv6 literal. */
- const char *rbracket = strchr (name, ']');
- if (rbracket == NULL)
- {
- /* Unmatched [ */
- return 0;
- }
- if (rbracket[1] == '\0')
- {
- /* ]<end> */
- port_start = NULL;
- }
- else if (rbracket[1] == ':')
- {
- /* ]:<port?> */
- port_start = rbracket + 2;
- }
- else
- {
- /* ]<invalid> */
- return 0;
- }
- host_start = name + 1;
- host_len = (size_t) (rbracket - host_start);
- if (memchr (host_start, ':', host_len) == NULL)
- {
- /* Require all bracketed hosts to contain a colon, because a hostname or
- IPv4 address should never use brackets. */
- return 0;
- }
+ if (name[0] == '[') {
+ /* Parse a bracketed host, typically an IPv6 literal. */
+ const char *rbracket = strchr(name, ']');
+ if (rbracket == NULL) {
+ /* Unmatched [ */
+ return 0;
+ }
+ if (rbracket[1] == '\0') {
+ /* ]<end> */
+ port_start = NULL;
+ } else if (rbracket[1] == ':') {
+ /* ]:<port?> */
+ port_start = rbracket + 2;
+ } else {
+ /* ]<invalid> */
+ return 0;
}
- else
- {
- const char *colon = strchr (name, ':');
- if (colon != NULL && strchr (colon + 1, ':') == NULL)
- {
- /* Exactly 1 colon. Split into host:port. */
- host_start = name;
- host_len = (size_t) (colon - name);
- port_start = colon + 1;
- }
- else
- {
- /* 0 or 2+ colons. Bare hostname or IPv6 litearal. */
- host_start = name;
- host_len = strlen (name);
- port_start = NULL;
- }
+ host_start = name + 1;
+ host_len = (size_t)(rbracket - host_start);
+ if (memchr(host_start, ':', host_len) == NULL) {
+ /* Require all bracketed hosts to contain a colon, because a hostname or
+ IPv4 address should never use brackets. */
+ return 0;
}
+ } else {
+ const char *colon = strchr(name, ':');
+ if (colon != NULL && strchr(colon + 1, ':') == NULL) {
+ /* Exactly 1 colon. Split into host:port. */
+ host_start = name;
+ host_len = (size_t)(colon - name);
+ port_start = colon + 1;
+ } else {
+ /* 0 or 2+ colons. Bare hostname or IPv6 litearal. */
+ host_start = name;
+ host_len = strlen(name);
+ port_start = NULL;
+ }
+ }
/* Allocate return values. */
- *host = gpr_malloc (host_len + 1);
- memcpy (*host, host_start, host_len);
+ *host = gpr_malloc(host_len + 1);
+ memcpy(*host, host_start, host_len);
(*host)[host_len] = '\0';
- if (port_start != NULL)
- {
- *port = gpr_strdup (port_start);
- }
+ if (port_start != NULL) {
+ *port = gpr_strdup(port_start);
+ }
return 1;
}
diff --git a/src/core/support/log.c b/src/core/support/log.c
index 60dcdf1369..f52c2035b9 100644
--- a/src/core/support/log.c
+++ b/src/core/support/log.c
@@ -36,38 +36,30 @@
#include <stdio.h>
#include <string.h>
-extern void gpr_default_log (gpr_log_func_args * args);
+extern void gpr_default_log(gpr_log_func_args *args);
static gpr_log_func g_log_func = gpr_default_log;
-const char *
-gpr_log_severity_string (gpr_log_severity severity)
-{
- switch (severity)
- {
+const char *gpr_log_severity_string(gpr_log_severity severity) {
+ switch (severity) {
case GPR_LOG_SEVERITY_DEBUG:
return "D";
case GPR_LOG_SEVERITY_INFO:
return "I";
case GPR_LOG_SEVERITY_ERROR:
return "E";
- }
+ }
return "UNKNOWN";
}
-void
-gpr_log_message (const char *file, int line, gpr_log_severity severity, const char *message)
-{
+void gpr_log_message(const char *file, int line, gpr_log_severity severity,
+ const char *message) {
gpr_log_func_args lfargs;
- memset (&lfargs, 0, sizeof (lfargs));
+ memset(&lfargs, 0, sizeof(lfargs));
lfargs.file = file;
lfargs.line = line;
lfargs.severity = severity;
lfargs.message = message;
- g_log_func (&lfargs);
+ g_log_func(&lfargs);
}
-void
-gpr_set_log_function (gpr_log_func f)
-{
- g_log_func = f;
-}
+void gpr_set_log_function(gpr_log_func f) { g_log_func = f; }
diff --git a/src/core/support/log_android.c b/src/core/support/log_android.c
index 891281444d..5d0c7d820d 100644
--- a/src/core/support/log_android.c
+++ b/src/core/support/log_android.c
@@ -42,52 +42,46 @@
#include <string.h>
#include <android/log.h>
-static android_LogPriority
-severity_to_log_priority (gpr_log_severity severity)
-{
- switch (severity)
- {
+static android_LogPriority severity_to_log_priority(gpr_log_severity severity) {
+ switch (severity) {
case GPR_LOG_SEVERITY_DEBUG:
return ANDROID_LOG_DEBUG;
case GPR_LOG_SEVERITY_INFO:
return ANDROID_LOG_INFO;
case GPR_LOG_SEVERITY_ERROR:
return ANDROID_LOG_ERROR;
- }
+ }
return ANDROID_LOG_DEFAULT;
}
-void
-gpr_log (const char *file, int line, gpr_log_severity severity, const char *format, ...)
-{
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
char *message = NULL;
va_list args;
- va_start (args, format);
- vasprintf (&message, format, args);
- va_end (args);
- gpr_log_message (file, line, severity, message);
- free (message);
+ va_start(args, format);
+ vasprintf(&message, format, args);
+ va_end(args);
+ gpr_log_message(file, line, severity, message);
+ free(message);
}
-void
-gpr_default_log (gpr_log_func_args * args)
-{
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
char *output = NULL;
- final_slash = strrchr (args->file, '/');
+ final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = args->file;
else
display_file = final_slash + 1;
- asprintf (&output, "%s:%d] %s", display_file, args->line, args->message);
+ asprintf(&output, "%s:%d] %s", display_file, args->line, args->message);
- __android_log_write (severity_to_log_priority (args->severity), "GRPC", output);
+ __android_log_write(severity_to_log_priority(args->severity), "GRPC", output);
/* allocated by asprintf => use free, not gpr_free */
- free (output);
+ free(output);
}
#endif /* GPR_ANDROID */
diff --git a/src/core/support/log_linux.c b/src/core/support/log_linux.c
index 7f52b74b0e..02f64d8b7e 100644
--- a/src/core/support/log_linux.c
+++ b/src/core/support/log_linux.c
@@ -55,57 +55,49 @@
#include <sys/syscall.h>
#include <unistd.h>
-static long
-gettid (void)
-{
- return syscall (__NR_gettid);
-}
+static long gettid(void) { return syscall(__NR_gettid); }
-void
-gpr_log (const char *file, int line, gpr_log_severity severity, const char *format, ...)
-{
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
char *message = NULL;
va_list args;
- va_start (args, format);
- if (vasprintf (&message, format, args) == -1)
- {
- va_end (args);
- return;
- }
- va_end (args);
- gpr_log_message (file, line, severity, message);
- free (message);
+ va_start(args, format);
+ if (vasprintf(&message, format, args) == -1) {
+ va_end(args);
+ return;
+ }
+ va_end(args);
+ gpr_log_message(file, line, severity, message);
+ free(message);
}
-void
-gpr_default_log (gpr_log_func_args * args)
-{
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
char *prefix;
const char *display_file;
char time_buffer[64];
- gpr_timespec now = gpr_now (GPR_CLOCK_REALTIME);
+ gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
struct tm tm;
- final_slash = strrchr (args->file, '/');
+ final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = args->file;
else
display_file = final_slash + 1;
- if (!localtime_r (&now.tv_sec, &tm))
- {
- strcpy (time_buffer, "error:localtime");
- }
- else if (0 == strftime (time_buffer, sizeof (time_buffer), "%m%d %H:%M:%S", &tm))
- {
- strcpy (time_buffer, "error:strftime");
- }
+ if (!localtime_r(&now.tv_sec, &tm)) {
+ strcpy(time_buffer, "error:localtime");
+ } else if (0 ==
+ strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
+ strcpy(time_buffer, "error:strftime");
+ }
- gpr_asprintf (&prefix, "%s%s.%09d %7tu %s:%d]", gpr_log_severity_string (args->severity), time_buffer, (int) (now.tv_nsec), gettid (), display_file, args->line);
+ gpr_asprintf(&prefix, "%s%s.%09d %7tu %s:%d]",
+ gpr_log_severity_string(args->severity), time_buffer,
+ (int)(now.tv_nsec), gettid(), display_file, args->line);
- fprintf (stderr, "%-60s %s\n", prefix, args->message);
- gpr_free (prefix);
+ fprintf(stderr, "%-60s %s\n", prefix, args->message);
+ gpr_free(prefix);
}
#endif
diff --git a/src/core/support/log_posix.c b/src/core/support/log_posix.c
index 46ab98f140..8b050dbee7 100644
--- a/src/core/support/log_posix.c
+++ b/src/core/support/log_posix.c
@@ -45,67 +45,56 @@
#include <time.h>
#include <pthread.h>
-static gpr_intptr
-gettid (void)
-{
- return (gpr_intptr) pthread_self ();
-}
+static gpr_intptr gettid(void) { return (gpr_intptr)pthread_self(); }
-void
-gpr_log (const char *file, int line, gpr_log_severity severity, const char *format, ...)
-{
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
char buf[64];
char *allocated = NULL;
char *message = NULL;
int ret;
va_list args;
- va_start (args, format);
- ret = vsnprintf (buf, sizeof (buf), format, args);
- va_end (args);
- if (ret < 0)
- {
- message = NULL;
- }
- else if ((size_t) ret <= sizeof (buf) - 1)
- {
- message = buf;
- }
- else
- {
- message = allocated = gpr_malloc ((size_t) ret + 1);
- va_start (args, format);
- vsnprintf (message, (size_t) (ret + 1), format, args);
- va_end (args);
- }
- gpr_log_message (file, line, severity, message);
- gpr_free (allocated);
+ va_start(args, format);
+ ret = vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+ if (ret < 0) {
+ message = NULL;
+ } else if ((size_t)ret <= sizeof(buf) - 1) {
+ message = buf;
+ } else {
+ message = allocated = gpr_malloc((size_t)ret + 1);
+ va_start(args, format);
+ vsnprintf(message, (size_t)(ret + 1), format, args);
+ va_end(args);
+ }
+ gpr_log_message(file, line, severity, message);
+ gpr_free(allocated);
}
-void
-gpr_default_log (gpr_log_func_args * args)
-{
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
char time_buffer[64];
- gpr_timespec now = gpr_now (GPR_CLOCK_REALTIME);
+ gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
struct tm tm;
- final_slash = strrchr (args->file, '/');
+ final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = args->file;
else
display_file = final_slash + 1;
- if (!localtime_r (&now.tv_sec, &tm))
- {
- strcpy (time_buffer, "error:localtime");
- }
- else if (0 == strftime (time_buffer, sizeof (time_buffer), "%m%d %H:%M:%S", &tm))
- {
- strcpy (time_buffer, "error:strftime");
- }
+ if (!localtime_r(&now.tv_sec, &tm)) {
+ strcpy(time_buffer, "error:localtime");
+ } else if (0 ==
+ strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
+ strcpy(time_buffer, "error:strftime");
+ }
- fprintf (stderr, "%s%s.%09d %7tu %s:%d] %s\n", gpr_log_severity_string (args->severity), time_buffer, (int) (now.tv_nsec), gettid (), display_file, args->line, args->message);
+ fprintf(stderr, "%s%s.%09d %7tu %s:%d] %s\n",
+ gpr_log_severity_string(args->severity), time_buffer,
+ (int)(now.tv_nsec), gettid(), display_file, args->line,
+ args->message);
}
#endif /* defined(GPR_POSIX_LOG) */
diff --git a/src/core/support/log_win32.c b/src/core/support/log_win32.c
index aa31fa6de0..b68239f8f5 100644
--- a/src/core/support/log_win32.c
+++ b/src/core/support/log_win32.c
@@ -47,83 +47,76 @@
#include "src/core/support/string.h"
#include "src/core/support/string_win32.h"
-void
-gpr_log (const char *file, int line, gpr_log_severity severity, const char *format, ...)
-{
+void gpr_log(const char *file, int line, gpr_log_severity severity,
+ const char *format, ...) {
char *message = NULL;
va_list args;
int ret;
/* Determine the length. */
- va_start (args, format);
- ret = _vscprintf (format, args);
- va_end (args);
- if (ret < 0)
- {
- message = NULL;
- }
- else
- {
- /* Allocate a new buffer, with space for the NUL terminator. */
- size_t strp_buflen = (size_t) ret + 1;
- message = gpr_malloc (strp_buflen);
+ va_start(args, format);
+ ret = _vscprintf(format, args);
+ va_end(args);
+ if (ret < 0) {
+ message = NULL;
+ } else {
+ /* Allocate a new buffer, with space for the NUL terminator. */
+ size_t strp_buflen = (size_t)ret + 1;
+ message = gpr_malloc(strp_buflen);
- /* Print to the buffer. */
- va_start (args, format);
- ret = vsnprintf_s (message, strp_buflen, _TRUNCATE, format, args);
- va_end (args);
- if ((size_t) ret != strp_buflen - 1)
- {
- /* This should never happen. */
- gpr_free (message);
- message = NULL;
- }
+ /* Print to the buffer. */
+ va_start(args, format);
+ ret = vsnprintf_s(message, strp_buflen, _TRUNCATE, format, args);
+ va_end(args);
+ if ((size_t)ret != strp_buflen - 1) {
+ /* This should never happen. */
+ gpr_free(message);
+ message = NULL;
}
+ }
- gpr_log_message (file, line, severity, message);
- gpr_free (message);
+ gpr_log_message(file, line, severity, message);
+ gpr_free(message);
}
/* Simple starter implementation */
-void
-gpr_default_log (gpr_log_func_args * args)
-{
+void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
char time_buffer[64];
- gpr_timespec now = gpr_now (GPR_CLOCK_REALTIME);
+ gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
struct tm tm;
- final_slash = strrchr (args->file, '\\');
+ final_slash = strrchr(args->file, '\\');
if (final_slash == NULL)
display_file = args->file;
else
display_file = final_slash + 1;
- if (localtime_s (&tm, &now.tv_sec))
- {
- strcpy (time_buffer, "error:localtime");
- }
- else if (0 == strftime (time_buffer, sizeof (time_buffer), "%m%d %H:%M:%S", &tm))
- {
- strcpy (time_buffer, "error:strftime");
- }
+ if (localtime_s(&tm, &now.tv_sec)) {
+ strcpy(time_buffer, "error:localtime");
+ } else if (0 ==
+ strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
+ strcpy(time_buffer, "error:strftime");
+ }
- fprintf (stderr, "%s%s.%09u %5lu %s:%d] %s\n", gpr_log_severity_string (args->severity), time_buffer, (int) (now.tv_nsec), GetCurrentThreadId (), display_file, args->line, args->message);
+ fprintf(stderr, "%s%s.%09u %5lu %s:%d] %s\n",
+ gpr_log_severity_string(args->severity), time_buffer,
+ (int)(now.tv_nsec), GetCurrentThreadId(), display_file, args->line,
+ args->message);
}
-char *
-gpr_format_message (DWORD messageid)
-{
+char *gpr_format_message(DWORD messageid) {
LPTSTR tmessage;
char *message;
- DWORD status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, messageid, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) (&tmessage), 0, NULL);
- if (status == 0)
- return gpr_strdup ("Unable to retrieve error string");
- message = gpr_tchar_to_char (tmessage);
- LocalFree (tmessage);
+ DWORD status = FormatMessage(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, messageid, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR)(&tmessage), 0, NULL);
+ if (status == 0) return gpr_strdup("Unable to retrieve error string");
+ message = gpr_tchar_to_char(tmessage);
+ LocalFree(tmessage);
return message;
}
diff --git a/src/core/support/murmur_hash.c b/src/core/support/murmur_hash.c
index e4ac59467d..37fdca82ba 100644
--- a/src/core/support/murmur_hash.c
+++ b/src/core/support/murmur_hash.c
@@ -46,10 +46,8 @@
handle aligned reads, do the conversion here */
#define GETBLOCK32(p, i) (p)[(i)]
-gpr_uint32
-gpr_murmur_hash3 (const void *key, size_t len, gpr_uint32 seed)
-{
- const gpr_uint8 *data = (const gpr_uint8 *) key;
+gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed) {
+ const gpr_uint8 *data = (const gpr_uint8 *)key;
const size_t nblocks = len / 4;
int i;
@@ -59,42 +57,40 @@ gpr_murmur_hash3 (const void *key, size_t len, gpr_uint32 seed)
const gpr_uint32 c1 = 0xcc9e2d51;
const gpr_uint32 c2 = 0x1b873593;
- const gpr_uint32 *blocks = ((const gpr_uint32 *) key) + nblocks;
- const gpr_uint8 *tail = (const gpr_uint8 *) (data + nblocks * 4);
+ const gpr_uint32 *blocks = ((const gpr_uint32 *)key) + nblocks;
+ const gpr_uint8 *tail = (const gpr_uint8 *)(data + nblocks * 4);
/* body */
- for (i = -(int) nblocks; i; i++)
- {
- k1 = GETBLOCK32 (blocks, i);
+ for (i = -(int)nblocks; i; i++) {
+ k1 = GETBLOCK32(blocks, i);
- k1 *= c1;
- k1 = ROTL32 (k1, 15);
- k1 *= c2;
+ k1 *= c1;
+ k1 = ROTL32(k1, 15);
+ k1 *= c2;
- h1 ^= k1;
- h1 = ROTL32 (h1, 13);
- h1 = h1 * 5 + 0xe6546b64;
- }
+ h1 ^= k1;
+ h1 = ROTL32(h1, 13);
+ h1 = h1 * 5 + 0xe6546b64;
+ }
k1 = 0;
/* tail */
- switch (len & 3)
- {
+ switch (len & 3) {
case 3:
- k1 ^= ((gpr_uint32) tail[2]) << 16;
+ k1 ^= ((gpr_uint32)tail[2]) << 16;
case 2:
- k1 ^= ((gpr_uint32) tail[1]) << 8;
+ k1 ^= ((gpr_uint32)tail[1]) << 8;
case 1:
k1 ^= tail[0];
k1 *= c1;
- k1 = ROTL32 (k1, 15);
+ k1 = ROTL32(k1, 15);
k1 *= c2;
h1 ^= k1;
- };
+ };
/* finalization */
- h1 ^= (gpr_uint32) len;
- FMIX32 (h1);
+ h1 ^= (gpr_uint32)len;
+ FMIX32(h1);
return h1;
}
diff --git a/src/core/support/murmur_hash.h b/src/core/support/murmur_hash.h
index 0ef198e88e..343fcb99f7 100644
--- a/src/core/support/murmur_hash.h
+++ b/src/core/support/murmur_hash.h
@@ -39,6 +39,6 @@
#include <stddef.h>
/* compute the hash of key (length len) */
-gpr_uint32 gpr_murmur_hash3 (const void *key, size_t len, gpr_uint32 seed);
+gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed);
#endif /* GRPC_INTERNAL_CORE_SUPPORT_MURMUR_HASH_H */
diff --git a/src/core/support/slice.c b/src/core/support/slice.c
index d866b99eb7..53024e88f1 100644
--- a/src/core/support/slice.c
+++ b/src/core/support/slice.c
@@ -37,68 +37,52 @@
#include <string.h>
-gpr_slice
-gpr_empty_slice (void)
-{
+gpr_slice gpr_empty_slice(void) {
gpr_slice out;
out.refcount = 0;
out.data.inlined.length = 0;
return out;
}
-gpr_slice
-gpr_slice_ref (gpr_slice slice)
-{
- if (slice.refcount)
- {
- slice.refcount->ref (slice.refcount);
- }
+gpr_slice gpr_slice_ref(gpr_slice slice) {
+ if (slice.refcount) {
+ slice.refcount->ref(slice.refcount);
+ }
return slice;
}
-void
-gpr_slice_unref (gpr_slice slice)
-{
- if (slice.refcount)
- {
- slice.refcount->unref (slice.refcount);
- }
+void gpr_slice_unref(gpr_slice slice) {
+ if (slice.refcount) {
+ slice.refcount->unref(slice.refcount);
+ }
}
/* gpr_slice_new support structures - we create a refcount object extended
with the user provided data pointer & destroy function */
-typedef struct new_slice_refcount
-{
+typedef struct new_slice_refcount {
gpr_slice_refcount rc;
gpr_refcount refs;
- void (*user_destroy) (void *);
+ void (*user_destroy)(void *);
void *user_data;
} new_slice_refcount;
-static void
-new_slice_ref (void *p)
-{
+static void new_slice_ref(void *p) {
new_slice_refcount *r = p;
- gpr_ref (&r->refs);
+ gpr_ref(&r->refs);
}
-static void
-new_slice_unref (void *p)
-{
+static void new_slice_unref(void *p) {
new_slice_refcount *r = p;
- if (gpr_unref (&r->refs))
- {
- r->user_destroy (r->user_data);
- gpr_free (r);
- }
+ if (gpr_unref(&r->refs)) {
+ r->user_destroy(r->user_data);
+ gpr_free(r);
+ }
}
-gpr_slice
-gpr_slice_new (void *p, size_t len, void (*destroy) (void *))
-{
+gpr_slice gpr_slice_new(void *p, size_t len, void (*destroy)(void *)) {
gpr_slice slice;
- new_slice_refcount *rc = gpr_malloc (sizeof (new_slice_refcount));
- gpr_ref_init (&rc->refs, 1);
+ new_slice_refcount *rc = gpr_malloc(sizeof(new_slice_refcount));
+ gpr_ref_init(&rc->refs, 1);
rc->rc.ref = new_slice_ref;
rc->rc.unref = new_slice_unref;
rc->user_destroy = destroy;
@@ -112,39 +96,33 @@ gpr_slice_new (void *p, size_t len, void (*destroy) (void *))
/* gpr_slice_new_with_len support structures - we create a refcount object
extended with the user provided data pointer & destroy function */
-typedef struct new_with_len_slice_refcount
-{
+typedef struct new_with_len_slice_refcount {
gpr_slice_refcount rc;
gpr_refcount refs;
void *user_data;
size_t user_length;
- void (*user_destroy) (void *, size_t);
+ void (*user_destroy)(void *, size_t);
} new_with_len_slice_refcount;
-static void
-new_with_len_ref (void *p)
-{
+static void new_with_len_ref(void *p) {
new_with_len_slice_refcount *r = p;
- gpr_ref (&r->refs);
+ gpr_ref(&r->refs);
}
-static void
-new_with_len_unref (void *p)
-{
+static void new_with_len_unref(void *p) {
new_with_len_slice_refcount *r = p;
- if (gpr_unref (&r->refs))
- {
- r->user_destroy (r->user_data, r->user_length);
- gpr_free (r);
- }
+ if (gpr_unref(&r->refs)) {
+ r->user_destroy(r->user_data, r->user_length);
+ gpr_free(r);
+ }
}
-gpr_slice
-gpr_slice_new_with_len (void *p, size_t len, void (*destroy) (void *, size_t))
-{
+gpr_slice gpr_slice_new_with_len(void *p, size_t len,
+ void (*destroy)(void *, size_t)) {
gpr_slice slice;
- new_with_len_slice_refcount *rc = gpr_malloc (sizeof (new_with_len_slice_refcount));
- gpr_ref_init (&rc->refs, 1);
+ new_with_len_slice_refcount *rc =
+ gpr_malloc(sizeof(new_with_len_slice_refcount));
+ gpr_ref_init(&rc->refs, 1);
rc->rc.ref = new_with_len_ref;
rc->rc.unref = new_with_len_unref;
rc->user_destroy = destroy;
@@ -157,242 +135,201 @@ gpr_slice_new_with_len (void *p, size_t len, void (*destroy) (void *, size_t))
return slice;
}
-gpr_slice
-gpr_slice_from_copied_buffer (const char *source, size_t length)
-{
- gpr_slice slice = gpr_slice_malloc (length);
- memcpy (GPR_SLICE_START_PTR (slice), source, length);
+gpr_slice gpr_slice_from_copied_buffer(const char *source, size_t length) {
+ gpr_slice slice = gpr_slice_malloc(length);
+ memcpy(GPR_SLICE_START_PTR(slice), source, length);
return slice;
}
-gpr_slice
-gpr_slice_from_copied_string (const char *source)
-{
- return gpr_slice_from_copied_buffer (source, strlen (source));
+gpr_slice gpr_slice_from_copied_string(const char *source) {
+ return gpr_slice_from_copied_buffer(source, strlen(source));
}
-typedef struct
-{
+typedef struct {
gpr_slice_refcount base;
gpr_refcount refs;
} malloc_refcount;
-static void
-malloc_ref (void *p)
-{
+static void malloc_ref(void *p) {
malloc_refcount *r = p;
- gpr_ref (&r->refs);
+ gpr_ref(&r->refs);
}
-static void
-malloc_unref (void *p)
-{
+static void malloc_unref(void *p) {
malloc_refcount *r = p;
- if (gpr_unref (&r->refs))
- {
- gpr_free (r);
- }
+ if (gpr_unref(&r->refs)) {
+ gpr_free(r);
+ }
}
-gpr_slice
-gpr_slice_malloc (size_t length)
-{
+gpr_slice gpr_slice_malloc(size_t length) {
gpr_slice slice;
- if (length > sizeof (slice.data.inlined.bytes))
- {
- /* Memory layout used by the slice created here:
-
- +-----------+----------------------------------------------------------+
- | refcount | bytes |
- +-----------+----------------------------------------------------------+
-
- refcount is a malloc_refcount
- bytes is an array of bytes of the requested length
- Both parts are placed in the same allocation returned from gpr_malloc */
- malloc_refcount *rc = gpr_malloc (sizeof (malloc_refcount) + length);
-
- /* Initial refcount on rc is 1 - and it's up to the caller to release
- this reference. */
- gpr_ref_init (&rc->refs, 1);
-
- rc->base.ref = malloc_ref;
- rc->base.unref = malloc_unref;
-
- /* Build up the slice to be returned. */
- /* The slices refcount points back to the allocated block. */
- slice.refcount = &rc->base;
- /* The data bytes are placed immediately after the refcount struct */
- slice.data.refcounted.bytes = (gpr_uint8 *) (rc + 1);
- /* And the length of the block is set to the requested length */
- slice.data.refcounted.length = length;
- }
- else
- {
- /* small slice: just inline the data */
- slice.refcount = NULL;
- slice.data.inlined.length = (gpr_uint8) length;
- }
+ if (length > sizeof(slice.data.inlined.bytes)) {
+ /* Memory layout used by the slice created here:
+
+ +-----------+----------------------------------------------------------+
+ | refcount | bytes |
+ +-----------+----------------------------------------------------------+
+
+ refcount is a malloc_refcount
+ bytes is an array of bytes of the requested length
+ Both parts are placed in the same allocation returned from gpr_malloc */
+ malloc_refcount *rc = gpr_malloc(sizeof(malloc_refcount) + length);
+
+ /* Initial refcount on rc is 1 - and it's up to the caller to release
+ this reference. */
+ gpr_ref_init(&rc->refs, 1);
+
+ rc->base.ref = malloc_ref;
+ rc->base.unref = malloc_unref;
+
+ /* Build up the slice to be returned. */
+ /* The slices refcount points back to the allocated block. */
+ slice.refcount = &rc->base;
+ /* The data bytes are placed immediately after the refcount struct */
+ slice.data.refcounted.bytes = (gpr_uint8 *)(rc + 1);
+ /* And the length of the block is set to the requested length */
+ slice.data.refcounted.length = length;
+ } else {
+ /* small slice: just inline the data */
+ slice.refcount = NULL;
+ slice.data.inlined.length = (gpr_uint8)length;
+ }
return slice;
}
-gpr_slice
-gpr_slice_sub_no_ref (gpr_slice source, size_t begin, size_t end)
-{
+gpr_slice gpr_slice_sub_no_ref(gpr_slice source, size_t begin, size_t end) {
gpr_slice subset;
- GPR_ASSERT (end >= begin);
-
- if (source.refcount)
- {
- /* Enforce preconditions */
- GPR_ASSERT (source.data.refcounted.length >= end);
-
- /* Build the result */
- subset.refcount = source.refcount;
- /* Point into the source array */
- subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
- subset.data.refcounted.length = end - begin;
- }
- else
- {
- /* Enforce preconditions */
- GPR_ASSERT (source.data.inlined.length >= end);
- subset.refcount = NULL;
- subset.data.inlined.length = (gpr_uint8) (end - begin);
- memcpy (subset.data.inlined.bytes, source.data.inlined.bytes + begin, end - begin);
- }
+ GPR_ASSERT(end >= begin);
+
+ if (source.refcount) {
+ /* Enforce preconditions */
+ GPR_ASSERT(source.data.refcounted.length >= end);
+
+ /* Build the result */
+ subset.refcount = source.refcount;
+ /* Point into the source array */
+ subset.data.refcounted.bytes = source.data.refcounted.bytes + begin;
+ subset.data.refcounted.length = end - begin;
+ } else {
+ /* Enforce preconditions */
+ GPR_ASSERT(source.data.inlined.length >= end);
+ subset.refcount = NULL;
+ subset.data.inlined.length = (gpr_uint8)(end - begin);
+ memcpy(subset.data.inlined.bytes, source.data.inlined.bytes + begin,
+ end - begin);
+ }
return subset;
}
-gpr_slice
-gpr_slice_sub (gpr_slice source, size_t begin, size_t end)
-{
+gpr_slice gpr_slice_sub(gpr_slice source, size_t begin, size_t end) {
gpr_slice subset;
- if (end - begin <= sizeof (subset.data.inlined.bytes))
- {
- subset.refcount = NULL;
- subset.data.inlined.length = (gpr_uint8) (end - begin);
- memcpy (subset.data.inlined.bytes, GPR_SLICE_START_PTR (source) + begin, end - begin);
- }
- else
- {
- subset = gpr_slice_sub_no_ref (source, begin, end);
- /* Bump the refcount */
- subset.refcount->ref (subset.refcount);
- }
+ if (end - begin <= sizeof(subset.data.inlined.bytes)) {
+ subset.refcount = NULL;
+ subset.data.inlined.length = (gpr_uint8)(end - begin);
+ memcpy(subset.data.inlined.bytes, GPR_SLICE_START_PTR(source) + begin,
+ end - begin);
+ } else {
+ subset = gpr_slice_sub_no_ref(source, begin, end);
+ /* Bump the refcount */
+ subset.refcount->ref(subset.refcount);
+ }
return subset;
}
-gpr_slice
-gpr_slice_split_tail (gpr_slice * source, size_t split)
-{
+gpr_slice gpr_slice_split_tail(gpr_slice *source, size_t split) {
gpr_slice tail;
- if (source->refcount == NULL)
- {
- /* inlined data, copy it out */
- GPR_ASSERT (source->data.inlined.length >= split);
+ if (source->refcount == NULL) {
+ /* inlined data, copy it out */
+ GPR_ASSERT(source->data.inlined.length >= split);
+ tail.refcount = NULL;
+ tail.data.inlined.length = (gpr_uint8)(source->data.inlined.length - split);
+ memcpy(tail.data.inlined.bytes, source->data.inlined.bytes + split,
+ tail.data.inlined.length);
+ source->data.inlined.length = (gpr_uint8)split;
+ } else {
+ size_t tail_length = source->data.refcounted.length - split;
+ GPR_ASSERT(source->data.refcounted.length >= split);
+ if (tail_length < sizeof(tail.data.inlined.bytes)) {
+ /* Copy out the bytes - it'll be cheaper than refcounting */
tail.refcount = NULL;
- tail.data.inlined.length = (gpr_uint8) (source->data.inlined.length - split);
- memcpy (tail.data.inlined.bytes, source->data.inlined.bytes + split, tail.data.inlined.length);
- source->data.inlined.length = (gpr_uint8) split;
- }
- else
- {
- size_t tail_length = source->data.refcounted.length - split;
- GPR_ASSERT (source->data.refcounted.length >= split);
- if (tail_length < sizeof (tail.data.inlined.bytes))
- {
- /* Copy out the bytes - it'll be cheaper than refcounting */
- tail.refcount = NULL;
- tail.data.inlined.length = (gpr_uint8) tail_length;
- memcpy (tail.data.inlined.bytes, source->data.refcounted.bytes + split, tail_length);
- }
- else
- {
- /* Build the result */
- tail.refcount = source->refcount;
- /* Bump the refcount */
- tail.refcount->ref (tail.refcount);
- /* Point into the source array */
- tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
- tail.data.refcounted.length = tail_length;
- }
- source->data.refcounted.length = split;
+ tail.data.inlined.length = (gpr_uint8)tail_length;
+ memcpy(tail.data.inlined.bytes, source->data.refcounted.bytes + split,
+ tail_length);
+ } else {
+ /* Build the result */
+ tail.refcount = source->refcount;
+ /* Bump the refcount */
+ tail.refcount->ref(tail.refcount);
+ /* Point into the source array */
+ tail.data.refcounted.bytes = source->data.refcounted.bytes + split;
+ tail.data.refcounted.length = tail_length;
}
+ source->data.refcounted.length = split;
+ }
return tail;
}
-gpr_slice
-gpr_slice_split_head (gpr_slice * source, size_t split)
-{
+gpr_slice gpr_slice_split_head(gpr_slice *source, size_t split) {
gpr_slice head;
- if (source->refcount == NULL)
- {
- GPR_ASSERT (source->data.inlined.length >= split);
-
- head.refcount = NULL;
- head.data.inlined.length = (gpr_uint8) split;
- memcpy (head.data.inlined.bytes, source->data.inlined.bytes, split);
- source->data.inlined.length = (gpr_uint8) (source->data.inlined.length - split);
- memmove (source->data.inlined.bytes, source->data.inlined.bytes + split, source->data.inlined.length);
- }
- else if (split < sizeof (head.data.inlined.bytes))
- {
- GPR_ASSERT (source->data.refcounted.length >= split);
-
- head.refcount = NULL;
- head.data.inlined.length = (gpr_uint8) split;
- memcpy (head.data.inlined.bytes, source->data.refcounted.bytes, split);
- source->data.refcounted.bytes += split;
- source->data.refcounted.length -= split;
- }
- else
- {
- GPR_ASSERT (source->data.refcounted.length >= split);
-
- /* Build the result */
- head.refcount = source->refcount;
- /* Bump the refcount */
- head.refcount->ref (head.refcount);
- /* Point into the source array */
- head.data.refcounted.bytes = source->data.refcounted.bytes;
- head.data.refcounted.length = split;
- source->data.refcounted.bytes += split;
- source->data.refcounted.length -= split;
- }
+ if (source->refcount == NULL) {
+ GPR_ASSERT(source->data.inlined.length >= split);
+
+ head.refcount = NULL;
+ head.data.inlined.length = (gpr_uint8)split;
+ memcpy(head.data.inlined.bytes, source->data.inlined.bytes, split);
+ source->data.inlined.length =
+ (gpr_uint8)(source->data.inlined.length - split);
+ memmove(source->data.inlined.bytes, source->data.inlined.bytes + split,
+ source->data.inlined.length);
+ } else if (split < sizeof(head.data.inlined.bytes)) {
+ GPR_ASSERT(source->data.refcounted.length >= split);
+
+ head.refcount = NULL;
+ head.data.inlined.length = (gpr_uint8)split;
+ memcpy(head.data.inlined.bytes, source->data.refcounted.bytes, split);
+ source->data.refcounted.bytes += split;
+ source->data.refcounted.length -= split;
+ } else {
+ GPR_ASSERT(source->data.refcounted.length >= split);
+
+ /* Build the result */
+ head.refcount = source->refcount;
+ /* Bump the refcount */
+ head.refcount->ref(head.refcount);
+ /* Point into the source array */
+ head.data.refcounted.bytes = source->data.refcounted.bytes;
+ head.data.refcounted.length = split;
+ source->data.refcounted.bytes += split;
+ source->data.refcounted.length -= split;
+ }
return head;
}
-int
-gpr_slice_cmp (gpr_slice a, gpr_slice b)
-{
- int d = (int) (GPR_SLICE_LENGTH (a) - GPR_SLICE_LENGTH (b));
- if (d != 0)
- return d;
- return memcmp (GPR_SLICE_START_PTR (a), GPR_SLICE_START_PTR (b), GPR_SLICE_LENGTH (a));
+int gpr_slice_cmp(gpr_slice a, gpr_slice b) {
+ int d = (int)(GPR_SLICE_LENGTH(a) - GPR_SLICE_LENGTH(b));
+ if (d != 0) return d;
+ return memcmp(GPR_SLICE_START_PTR(a), GPR_SLICE_START_PTR(b),
+ GPR_SLICE_LENGTH(a));
}
-int
-gpr_slice_str_cmp (gpr_slice a, const char *b)
-{
- size_t b_length = strlen (b);
- int d = (int) (GPR_SLICE_LENGTH (a) - b_length);
- if (d != 0)
- return d;
- return memcmp (GPR_SLICE_START_PTR (a), b, b_length);
+int gpr_slice_str_cmp(gpr_slice a, const char *b) {
+ size_t b_length = strlen(b);
+ int d = (int)(GPR_SLICE_LENGTH(a) - b_length);
+ if (d != 0) return d;
+ return memcmp(GPR_SLICE_START_PTR(a), b, b_length);
}
-char *
-gpr_slice_to_cstring (gpr_slice slice)
-{
- char *result = gpr_malloc (GPR_SLICE_LENGTH (slice) + 1);
- memcpy (result, GPR_SLICE_START_PTR (slice), GPR_SLICE_LENGTH (slice));
- result[GPR_SLICE_LENGTH (slice)] = '\0';
+char *gpr_slice_to_cstring(gpr_slice slice) {
+ char *result = gpr_malloc(GPR_SLICE_LENGTH(slice) + 1);
+ memcpy(result, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice));
+ result[GPR_SLICE_LENGTH(slice)] = '\0';
return result;
}
diff --git a/src/core/support/slice_buffer.c b/src/core/support/slice_buffer.c
index fac90ea6a6..8873d459d5 100644
--- a/src/core/support/slice_buffer.c
+++ b/src/core/support/slice_buffer.c
@@ -42,240 +42,190 @@
/* grow a buffer; requires GRPC_SLICE_BUFFER_INLINE_ELEMENTS > 1 */
#define GROW(x) (3 * (x) / 2)
-static void
-maybe_embiggen (gpr_slice_buffer * sb)
-{
- if (sb->count == sb->capacity)
- {
- sb->capacity = GROW (sb->capacity);
- GPR_ASSERT (sb->capacity > sb->count);
- if (sb->slices == sb->inlined)
- {
- sb->slices = gpr_malloc (sb->capacity * sizeof (gpr_slice));
- memcpy (sb->slices, sb->inlined, sb->count * sizeof (gpr_slice));
- }
- else
- {
- sb->slices = gpr_realloc (sb->slices, sb->capacity * sizeof (gpr_slice));
- }
+static void maybe_embiggen(gpr_slice_buffer *sb) {
+ if (sb->count == sb->capacity) {
+ sb->capacity = GROW(sb->capacity);
+ GPR_ASSERT(sb->capacity > sb->count);
+ if (sb->slices == sb->inlined) {
+ sb->slices = gpr_malloc(sb->capacity * sizeof(gpr_slice));
+ memcpy(sb->slices, sb->inlined, sb->count * sizeof(gpr_slice));
+ } else {
+ sb->slices = gpr_realloc(sb->slices, sb->capacity * sizeof(gpr_slice));
}
+ }
}
-void
-gpr_slice_buffer_init (gpr_slice_buffer * sb)
-{
+void gpr_slice_buffer_init(gpr_slice_buffer *sb) {
sb->count = 0;
sb->length = 0;
sb->capacity = GRPC_SLICE_BUFFER_INLINE_ELEMENTS;
sb->slices = sb->inlined;
}
-void
-gpr_slice_buffer_destroy (gpr_slice_buffer * sb)
-{
- gpr_slice_buffer_reset_and_unref (sb);
- if (sb->slices != sb->inlined)
- {
- gpr_free (sb->slices);
- }
+void gpr_slice_buffer_destroy(gpr_slice_buffer *sb) {
+ gpr_slice_buffer_reset_and_unref(sb);
+ if (sb->slices != sb->inlined) {
+ gpr_free(sb->slices);
+ }
}
-gpr_uint8 *
-gpr_slice_buffer_tiny_add (gpr_slice_buffer * sb, size_t n)
-{
+gpr_uint8 *gpr_slice_buffer_tiny_add(gpr_slice_buffer *sb, size_t n) {
gpr_slice *back;
gpr_uint8 *out;
sb->length += n;
- if (sb->count == 0)
- goto add_new;
+ if (sb->count == 0) goto add_new;
back = &sb->slices[sb->count - 1];
- if (back->refcount)
- goto add_new;
- if ((back->data.inlined.length + n) > sizeof (back->data.inlined.bytes))
+ if (back->refcount) goto add_new;
+ if ((back->data.inlined.length + n) > sizeof(back->data.inlined.bytes))
goto add_new;
out = back->data.inlined.bytes + back->data.inlined.length;
- back->data.inlined.length = (gpr_uint8) (back->data.inlined.length + n);
+ back->data.inlined.length = (gpr_uint8)(back->data.inlined.length + n);
return out;
add_new:
- maybe_embiggen (sb);
+ maybe_embiggen(sb);
back = &sb->slices[sb->count];
sb->count++;
back->refcount = NULL;
- back->data.inlined.length = (gpr_uint8) n;
+ back->data.inlined.length = (gpr_uint8)n;
return back->data.inlined.bytes;
}
-size_t
-gpr_slice_buffer_add_indexed (gpr_slice_buffer * sb, gpr_slice s)
-{
+size_t gpr_slice_buffer_add_indexed(gpr_slice_buffer *sb, gpr_slice s) {
size_t out = sb->count;
- maybe_embiggen (sb);
+ maybe_embiggen(sb);
sb->slices[out] = s;
- sb->length += GPR_SLICE_LENGTH (s);
+ sb->length += GPR_SLICE_LENGTH(s);
sb->count = out + 1;
return out;
}
-void
-gpr_slice_buffer_add (gpr_slice_buffer * sb, gpr_slice s)
-{
+void gpr_slice_buffer_add(gpr_slice_buffer *sb, gpr_slice s) {
size_t n = sb->count;
/* if both the last slice in the slice buffer and the slice being added
are inlined (that is, that they carry their data inside the slice data
structure), and the back slice is not full, then concatenate directly
into the back slice, preventing many small slices being passed into
writes */
- if (!s.refcount && n)
- {
- gpr_slice *back = &sb->slices[n - 1];
- if (!back->refcount && back->data.inlined.length < GPR_SLICE_INLINED_SIZE)
- {
- if (s.data.inlined.length + back->data.inlined.length <= GPR_SLICE_INLINED_SIZE)
- {
- memcpy (back->data.inlined.bytes + back->data.inlined.length, s.data.inlined.bytes, s.data.inlined.length);
- back->data.inlined.length = (gpr_uint8) (back->data.inlined.length + s.data.inlined.length);
- }
- else
- {
- size_t cp1 = GPR_SLICE_INLINED_SIZE - back->data.inlined.length;
- memcpy (back->data.inlined.bytes + back->data.inlined.length, s.data.inlined.bytes, cp1);
- back->data.inlined.length = GPR_SLICE_INLINED_SIZE;
- maybe_embiggen (sb);
- back = &sb->slices[n];
- sb->count = n + 1;
- back->refcount = NULL;
- back->data.inlined.length = (gpr_uint8) (s.data.inlined.length - cp1);
- memcpy (back->data.inlined.bytes, s.data.inlined.bytes + cp1, s.data.inlined.length - cp1);
- }
- sb->length += s.data.inlined.length;
- return; /* early out */
- }
+ if (!s.refcount && n) {
+ gpr_slice *back = &sb->slices[n - 1];
+ if (!back->refcount && back->data.inlined.length < GPR_SLICE_INLINED_SIZE) {
+ if (s.data.inlined.length + back->data.inlined.length <=
+ GPR_SLICE_INLINED_SIZE) {
+ memcpy(back->data.inlined.bytes + back->data.inlined.length,
+ s.data.inlined.bytes, s.data.inlined.length);
+ back->data.inlined.length =
+ (gpr_uint8)(back->data.inlined.length + s.data.inlined.length);
+ } else {
+ size_t cp1 = GPR_SLICE_INLINED_SIZE - back->data.inlined.length;
+ memcpy(back->data.inlined.bytes + back->data.inlined.length,
+ s.data.inlined.bytes, cp1);
+ back->data.inlined.length = GPR_SLICE_INLINED_SIZE;
+ maybe_embiggen(sb);
+ back = &sb->slices[n];
+ sb->count = n + 1;
+ back->refcount = NULL;
+ back->data.inlined.length = (gpr_uint8)(s.data.inlined.length - cp1);
+ memcpy(back->data.inlined.bytes, s.data.inlined.bytes + cp1,
+ s.data.inlined.length - cp1);
+ }
+ sb->length += s.data.inlined.length;
+ return; /* early out */
}
- gpr_slice_buffer_add_indexed (sb, s);
+ }
+ gpr_slice_buffer_add_indexed(sb, s);
}
-void
-gpr_slice_buffer_addn (gpr_slice_buffer * sb, gpr_slice * s, size_t n)
-{
+void gpr_slice_buffer_addn(gpr_slice_buffer *sb, gpr_slice *s, size_t n) {
size_t i;
- for (i = 0; i < n; i++)
- {
- gpr_slice_buffer_add (sb, s[i]);
- }
+ for (i = 0; i < n; i++) {
+ gpr_slice_buffer_add(sb, s[i]);
+ }
}
-void
-gpr_slice_buffer_pop (gpr_slice_buffer * sb)
-{
- if (sb->count != 0)
- {
- size_t count = --sb->count;
- sb->length -= GPR_SLICE_LENGTH (sb->slices[count]);
- }
+void gpr_slice_buffer_pop(gpr_slice_buffer *sb) {
+ if (sb->count != 0) {
+ size_t count = --sb->count;
+ sb->length -= GPR_SLICE_LENGTH(sb->slices[count]);
+ }
}
-void
-gpr_slice_buffer_reset_and_unref (gpr_slice_buffer * sb)
-{
+void gpr_slice_buffer_reset_and_unref(gpr_slice_buffer *sb) {
size_t i;
- for (i = 0; i < sb->count; i++)
- {
- gpr_slice_unref (sb->slices[i]);
- }
+ for (i = 0; i < sb->count; i++) {
+ gpr_slice_unref(sb->slices[i]);
+ }
sb->count = 0;
sb->length = 0;
}
-void
-gpr_slice_buffer_swap (gpr_slice_buffer * a, gpr_slice_buffer * b)
-{
- GPR_SWAP (size_t, a->count, b->count);
- GPR_SWAP (size_t, a->capacity, b->capacity);
- GPR_SWAP (size_t, a->length, b->length);
-
- if (a->slices == a->inlined)
- {
- if (b->slices == b->inlined)
- {
- /* swap contents of inlined buffer */
- gpr_slice temp[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
- memcpy (temp, a->slices, b->count * sizeof (gpr_slice));
- memcpy (a->slices, b->slices, a->count * sizeof (gpr_slice));
- memcpy (b->slices, temp, b->count * sizeof (gpr_slice));
- }
- else
- {
- /* a is inlined, b is not - copy a inlined into b, fix pointers */
- a->slices = b->slices;
- b->slices = b->inlined;
- memcpy (b->slices, a->inlined, b->count * sizeof (gpr_slice));
- }
- }
- else if (b->slices == b->inlined)
- {
- /* b is inlined, a is not - copy b inlined int a, fix pointers */
- b->slices = a->slices;
- a->slices = a->inlined;
- memcpy (a->slices, b->inlined, a->count * sizeof (gpr_slice));
- }
- else
- {
- /* no inlining: easy swap */
- GPR_SWAP (gpr_slice *, a->slices, b->slices);
+void gpr_slice_buffer_swap(gpr_slice_buffer *a, gpr_slice_buffer *b) {
+ GPR_SWAP(size_t, a->count, b->count);
+ GPR_SWAP(size_t, a->capacity, b->capacity);
+ GPR_SWAP(size_t, a->length, b->length);
+
+ if (a->slices == a->inlined) {
+ if (b->slices == b->inlined) {
+ /* swap contents of inlined buffer */
+ gpr_slice temp[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
+ memcpy(temp, a->slices, b->count * sizeof(gpr_slice));
+ memcpy(a->slices, b->slices, a->count * sizeof(gpr_slice));
+ memcpy(b->slices, temp, b->count * sizeof(gpr_slice));
+ } else {
+ /* a is inlined, b is not - copy a inlined into b, fix pointers */
+ a->slices = b->slices;
+ b->slices = b->inlined;
+ memcpy(b->slices, a->inlined, b->count * sizeof(gpr_slice));
}
+ } else if (b->slices == b->inlined) {
+ /* b is inlined, a is not - copy b inlined int a, fix pointers */
+ b->slices = a->slices;
+ a->slices = a->inlined;
+ memcpy(a->slices, b->inlined, a->count * sizeof(gpr_slice));
+ } else {
+ /* no inlining: easy swap */
+ GPR_SWAP(gpr_slice *, a->slices, b->slices);
+ }
}
-void
-gpr_slice_buffer_move_into (gpr_slice_buffer * src, gpr_slice_buffer * dst)
-{
+void gpr_slice_buffer_move_into(gpr_slice_buffer *src, gpr_slice_buffer *dst) {
/* anything to move? */
- if (src->count == 0)
- {
- return;
- }
+ if (src->count == 0) {
+ return;
+ }
/* anything in dst? */
- if (dst->count == 0)
- {
- gpr_slice_buffer_swap (src, dst);
- return;
- }
+ if (dst->count == 0) {
+ gpr_slice_buffer_swap(src, dst);
+ return;
+ }
/* both buffers have data - copy, and reset src */
- gpr_slice_buffer_addn (dst, src->slices, src->count);
+ gpr_slice_buffer_addn(dst, src->slices, src->count);
src->count = 0;
src->length = 0;
}
-void
-gpr_slice_buffer_trim_end (gpr_slice_buffer * sb, size_t n)
-{
- GPR_ASSERT (n <= sb->length);
+void gpr_slice_buffer_trim_end(gpr_slice_buffer *sb, size_t n) {
+ GPR_ASSERT(n <= sb->length);
sb->length -= n;
- for (;;)
- {
- size_t idx = sb->count - 1;
- gpr_slice slice = sb->slices[idx];
- size_t slice_len = GPR_SLICE_LENGTH (slice);
- if (slice_len > n)
- {
- sb->slices[idx] = gpr_slice_sub_no_ref (slice, 0, slice_len - n);
- return;
- }
- else if (slice_len == n)
- {
- gpr_slice_unref (slice);
- sb->count = idx;
- return;
- }
- else
- {
- gpr_slice_unref (slice);
- n -= slice_len;
- sb->count = idx;
- }
+ for (;;) {
+ size_t idx = sb->count - 1;
+ gpr_slice slice = sb->slices[idx];
+ size_t slice_len = GPR_SLICE_LENGTH(slice);
+ if (slice_len > n) {
+ sb->slices[idx] = gpr_slice_sub_no_ref(slice, 0, slice_len - n);
+ return;
+ } else if (slice_len == n) {
+ gpr_slice_unref(slice);
+ sb->count = idx;
+ return;
+ } else {
+ gpr_slice_unref(slice);
+ n -= slice_len;
+ sb->count = idx;
}
+ }
}
diff --git a/src/core/support/stack_lockfree.c b/src/core/support/stack_lockfree.c
index 19171af8a2..7e6beec0c2 100644
--- a/src/core/support/stack_lockfree.c
+++ b/src/core/support/stack_lockfree.c
@@ -43,8 +43,7 @@
/* The lockfree node structure is a single architecture-level
word that allows for an atomic CAS to set it up. */
-struct lockfree_node_contents
-{
+struct lockfree_node_contents {
/* next thing to look at. Actual index for head, next index otherwise */
gpr_uint16 index;
#ifdef GPR_ARCH_64
@@ -60,126 +59,115 @@ struct lockfree_node_contents
};
/* Use a union to make sure that these are in the same bits as an atm word */
-typedef union lockfree_node
-{
+typedef union lockfree_node {
gpr_atm atm;
struct lockfree_node_contents contents;
} lockfree_node;
-#define ENTRY_ALIGNMENT_BITS 3 /* make sure that entries aligned to 8-bytes */
+#define ENTRY_ALIGNMENT_BITS 3 /* make sure that entries aligned to 8-bytes */
#define INVALID_ENTRY_INDEX \
- ((1 << 16) - 1) /* reserve this entry as invalid \
- */
+ ((1 << 16) - 1) /* reserve this entry as invalid \
+ */
-struct gpr_stack_lockfree
-{
+struct gpr_stack_lockfree {
lockfree_node *entries;
- lockfree_node head; /* An atomic entry describing curr head */
+ lockfree_node head; /* An atomic entry describing curr head */
#ifndef NDEBUG
/* Bitmap of pushed entries to check for double-push or pop */
- gpr_atm pushed[(INVALID_ENTRY_INDEX + 1) / (8 * sizeof (gpr_atm))];
+ gpr_atm pushed[(INVALID_ENTRY_INDEX + 1) / (8 * sizeof(gpr_atm))];
#endif
};
-gpr_stack_lockfree *
-gpr_stack_lockfree_create (size_t entries)
-{
+gpr_stack_lockfree *gpr_stack_lockfree_create(size_t entries) {
gpr_stack_lockfree *stack;
- stack = gpr_malloc (sizeof (*stack));
+ stack = gpr_malloc(sizeof(*stack));
/* Since we only allocate 16 bits to represent an entry number,
* make sure that we are within the desired range */
/* Reserve the highest entry number as a dummy */
- GPR_ASSERT (entries < INVALID_ENTRY_INDEX);
- stack->entries = gpr_malloc_aligned (entries * sizeof (stack->entries[0]), ENTRY_ALIGNMENT_BITS);
+ GPR_ASSERT(entries < INVALID_ENTRY_INDEX);
+ stack->entries = gpr_malloc_aligned(entries * sizeof(stack->entries[0]),
+ ENTRY_ALIGNMENT_BITS);
/* Clear out all entries */
- memset (stack->entries, 0, entries * sizeof (stack->entries[0]));
- memset (&stack->head, 0, sizeof (stack->head));
+ memset(stack->entries, 0, entries * sizeof(stack->entries[0]));
+ memset(&stack->head, 0, sizeof(stack->head));
#ifndef NDEBUG
- memset (&stack->pushed, 0, sizeof (stack->pushed));
+ memset(&stack->pushed, 0, sizeof(stack->pushed));
#endif
- GPR_ASSERT (sizeof (stack->entries->atm) == sizeof (stack->entries->contents));
+ GPR_ASSERT(sizeof(stack->entries->atm) == sizeof(stack->entries->contents));
/* Point the head at reserved dummy entry */
stack->head.contents.index = INVALID_ENTRY_INDEX;
return stack;
}
-void
-gpr_stack_lockfree_destroy (gpr_stack_lockfree * stack)
-{
- gpr_free_aligned (stack->entries);
- gpr_free (stack);
+void gpr_stack_lockfree_destroy(gpr_stack_lockfree *stack) {
+ gpr_free_aligned(stack->entries);
+ gpr_free(stack);
}
-int
-gpr_stack_lockfree_push (gpr_stack_lockfree * stack, int entry)
-{
+int gpr_stack_lockfree_push(gpr_stack_lockfree *stack, int entry) {
lockfree_node head;
lockfree_node newhead;
lockfree_node curent;
lockfree_node newent;
/* First fill in the entry's index and aba ctr for new head */
- newhead.contents.index = (gpr_uint16) entry;
+ newhead.contents.index = (gpr_uint16)entry;
/* Also post-increment the aba_ctr */
- curent.atm = gpr_atm_no_barrier_load (&stack->entries[entry].atm);
+ curent.atm = gpr_atm_no_barrier_load(&stack->entries[entry].atm);
newhead.contents.aba_ctr = ++curent.contents.aba_ctr;
- gpr_atm_no_barrier_store (&stack->entries[entry].atm, curent.atm);
+ gpr_atm_no_barrier_store(&stack->entries[entry].atm, curent.atm);
#ifndef NDEBUG
/* Check for double push */
{
- int pushed_index = entry / (int) (8 * sizeof (gpr_atm));
- int pushed_bit = entry % (int) (8 * sizeof (gpr_atm));
+ int pushed_index = entry / (int)(8 * sizeof(gpr_atm));
+ int pushed_bit = entry % (int)(8 * sizeof(gpr_atm));
gpr_atm old_val;
- old_val = gpr_atm_no_barrier_fetch_add (&stack->pushed[pushed_index], (gpr_atm) (1UL << pushed_bit));
- GPR_ASSERT ((old_val & (gpr_atm) (1UL << pushed_bit)) == 0);
+ old_val = gpr_atm_no_barrier_fetch_add(&stack->pushed[pushed_index],
+ (gpr_atm)(1UL << pushed_bit));
+ GPR_ASSERT((old_val & (gpr_atm)(1UL << pushed_bit)) == 0);
}
#endif
- do
- {
- /* Atomically get the existing head value for use */
- head.atm = gpr_atm_no_barrier_load (&(stack->head.atm));
- /* Point to it */
- newent.atm = gpr_atm_no_barrier_load (&stack->entries[entry].atm);
- newent.contents.index = head.contents.index;
- gpr_atm_no_barrier_store (&stack->entries[entry].atm, newent.atm);
- }
- while (!gpr_atm_rel_cas (&(stack->head.atm), head.atm, newhead.atm));
+ do {
+ /* Atomically get the existing head value for use */
+ head.atm = gpr_atm_no_barrier_load(&(stack->head.atm));
+ /* Point to it */
+ newent.atm = gpr_atm_no_barrier_load(&stack->entries[entry].atm);
+ newent.contents.index = head.contents.index;
+ gpr_atm_no_barrier_store(&stack->entries[entry].atm, newent.atm);
+ } while (!gpr_atm_rel_cas(&(stack->head.atm), head.atm, newhead.atm));
/* Use rel_cas above to make sure that entry index is set properly */
return head.contents.index == INVALID_ENTRY_INDEX;
}
-int
-gpr_stack_lockfree_pop (gpr_stack_lockfree * stack)
-{
+int gpr_stack_lockfree_pop(gpr_stack_lockfree *stack) {
lockfree_node head;
lockfree_node newhead;
- do
- {
- head.atm = gpr_atm_acq_load (&(stack->head.atm));
- if (head.contents.index == INVALID_ENTRY_INDEX)
- {
- return -1;
- }
- newhead.atm = gpr_atm_no_barrier_load (&(stack->entries[head.contents.index].atm));
-
+ do {
+ head.atm = gpr_atm_acq_load(&(stack->head.atm));
+ if (head.contents.index == INVALID_ENTRY_INDEX) {
+ return -1;
}
- while (!gpr_atm_no_barrier_cas (&(stack->head.atm), head.atm, newhead.atm));
+ newhead.atm =
+ gpr_atm_no_barrier_load(&(stack->entries[head.contents.index].atm));
+
+ } while (!gpr_atm_no_barrier_cas(&(stack->head.atm), head.atm, newhead.atm));
#ifndef NDEBUG
/* Check for valid pop */
{
- int pushed_index = head.contents.index / (8 * sizeof (gpr_atm));
- int pushed_bit = head.contents.index % (8 * sizeof (gpr_atm));
+ int pushed_index = head.contents.index / (8 * sizeof(gpr_atm));
+ int pushed_bit = head.contents.index % (8 * sizeof(gpr_atm));
gpr_atm old_val;
- old_val = gpr_atm_no_barrier_fetch_add (&stack->pushed[pushed_index], -(gpr_atm) (1UL << pushed_bit));
- GPR_ASSERT ((old_val & (gpr_atm) (1UL << pushed_bit)) != 0);
+ old_val = gpr_atm_no_barrier_fetch_add(&stack->pushed[pushed_index],
+ -(gpr_atm)(1UL << pushed_bit));
+ GPR_ASSERT((old_val & (gpr_atm)(1UL << pushed_bit)) != 0);
}
#endif
diff --git a/src/core/support/stack_lockfree.h b/src/core/support/stack_lockfree.h
index 2f966d2963..ca58dd007a 100644
--- a/src/core/support/stack_lockfree.h
+++ b/src/core/support/stack_lockfree.h
@@ -40,14 +40,14 @@ typedef struct gpr_stack_lockfree gpr_stack_lockfree;
/* This stack must specify the maximum number of entries to track.
The current implementation only allows up to 65534 entries */
-gpr_stack_lockfree *gpr_stack_lockfree_create (size_t entries);
-void gpr_stack_lockfree_destroy (gpr_stack_lockfree * stack);
+gpr_stack_lockfree *gpr_stack_lockfree_create(size_t entries);
+void gpr_stack_lockfree_destroy(gpr_stack_lockfree *stack);
/* Pass in a valid entry number for the next stack entry */
/* Returns 1 if this is the first element on the stack, 0 otherwise */
-int gpr_stack_lockfree_push (gpr_stack_lockfree *, int entry);
+int gpr_stack_lockfree_push(gpr_stack_lockfree *, int entry);
/* Returns -1 on empty or the actual entry number */
-int gpr_stack_lockfree_pop (gpr_stack_lockfree * stack);
+int gpr_stack_lockfree_pop(gpr_stack_lockfree *stack);
#endif /* GRPC_INTERNAL_CORE_SUPPORT_STACK_LOCKFREE_H */
diff --git a/src/core/support/string.c b/src/core/support/string.c
index 28207f5dc2..e0ffeb8a4a 100644
--- a/src/core/support/string.c
+++ b/src/core/support/string.c
@@ -42,215 +42,169 @@
#include <grpc/support/port_platform.h>
#include <grpc/support/useful.h>
-char *
-gpr_strdup (const char *src)
-{
+char *gpr_strdup(const char *src) {
char *dst;
size_t len;
- if (!src)
- {
- return NULL;
- }
+ if (!src) {
+ return NULL;
+ }
- len = strlen (src) + 1;
- dst = gpr_malloc (len);
+ len = strlen(src) + 1;
+ dst = gpr_malloc(len);
- memcpy (dst, src, len);
+ memcpy(dst, src, len);
return dst;
}
-typedef struct
-{
+typedef struct {
size_t capacity;
size_t length;
char *data;
} dump_out;
-static dump_out
-dump_out_create (void)
-{
- dump_out r = { 0, 0, NULL };
+static dump_out dump_out_create(void) {
+ dump_out r = {0, 0, NULL};
return r;
}
-static void
-dump_out_append (dump_out * out, char c)
-{
- if (out->length == out->capacity)
- {
- out->capacity = GPR_MAX (8, 2 * out->capacity);
- out->data = gpr_realloc (out->data, out->capacity);
- }
+static void dump_out_append(dump_out *out, char c) {
+ if (out->length == out->capacity) {
+ out->capacity = GPR_MAX(8, 2 * out->capacity);
+ out->data = gpr_realloc(out->data, out->capacity);
+ }
out->data[out->length++] = c;
}
-static void
-hexdump (dump_out * out, const char *buf, size_t len)
-{
+static void hexdump(dump_out *out, const char *buf, size_t len) {
static const char hex[16] = "0123456789abcdef";
- const gpr_uint8 *const beg = (const gpr_uint8 *) buf;
+ const gpr_uint8 *const beg = (const gpr_uint8 *)buf;
const gpr_uint8 *const end = beg + len;
const gpr_uint8 *cur;
- for (cur = beg; cur != end; ++cur)
- {
- if (cur != beg)
- dump_out_append (out, ' ');
- dump_out_append (out, hex[*cur >> 4]);
- dump_out_append (out, hex[*cur & 0xf]);
- }
+ for (cur = beg; cur != end; ++cur) {
+ if (cur != beg) dump_out_append(out, ' ');
+ dump_out_append(out, hex[*cur >> 4]);
+ dump_out_append(out, hex[*cur & 0xf]);
+ }
}
-static void
-asciidump (dump_out * out, const char *buf, size_t len)
-{
- const gpr_uint8 *const beg = (const gpr_uint8 *) buf;
+static void asciidump(dump_out *out, const char *buf, size_t len) {
+ const gpr_uint8 *const beg = (const gpr_uint8 *)buf;
const gpr_uint8 *const end = beg + len;
const gpr_uint8 *cur;
int out_was_empty = (out->length == 0);
- if (!out_was_empty)
- {
- dump_out_append (out, ' ');
- dump_out_append (out, '\'');
- }
- for (cur = beg; cur != end; ++cur)
- {
- dump_out_append (out, (char) (isprint (*cur) ? *(char *) cur : '.'));
- }
- if (!out_was_empty)
- {
- dump_out_append (out, '\'');
- }
+ if (!out_was_empty) {
+ dump_out_append(out, ' ');
+ dump_out_append(out, '\'');
+ }
+ for (cur = beg; cur != end; ++cur) {
+ dump_out_append(out, (char)(isprint(*cur) ? *(char *)cur : '.'));
+ }
+ if (!out_was_empty) {
+ dump_out_append(out, '\'');
+ }
}
-char *
-gpr_dump (const char *buf, size_t len, gpr_uint32 flags)
-{
- dump_out out = dump_out_create ();
- if (flags & GPR_DUMP_HEX)
- {
- hexdump (&out, buf, len);
- }
- if (flags & GPR_DUMP_ASCII)
- {
- asciidump (&out, buf, len);
- }
- dump_out_append (&out, 0);
+char *gpr_dump(const char *buf, size_t len, gpr_uint32 flags) {
+ dump_out out = dump_out_create();
+ if (flags & GPR_DUMP_HEX) {
+ hexdump(&out, buf, len);
+ }
+ if (flags & GPR_DUMP_ASCII) {
+ asciidump(&out, buf, len);
+ }
+ dump_out_append(&out, 0);
return out.data;
}
-char *
-gpr_dump_slice (gpr_slice s, gpr_uint32 flags)
-{
- return gpr_dump ((const char *) GPR_SLICE_START_PTR (s), GPR_SLICE_LENGTH (s), flags);
+char *gpr_dump_slice(gpr_slice s, gpr_uint32 flags) {
+ return gpr_dump((const char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s),
+ flags);
}
-int
-gpr_parse_bytes_to_uint32 (const char *buf, size_t len, gpr_uint32 * result)
-{
+int gpr_parse_bytes_to_uint32(const char *buf, size_t len, gpr_uint32 *result) {
gpr_uint32 out = 0;
gpr_uint32 new;
size_t i;
- if (len == 0)
- return 0; /* must have some bytes */
-
- for (i = 0; i < len; i++)
- {
- if (buf[i] < '0' || buf[i] > '9')
- return 0; /* bad char */
- new = 10 * out + (gpr_uint32) (buf[i] - '0');
- if (new < out)
- return 0; /* overflow */
- out = new;
- }
+ if (len == 0) return 0; /* must have some bytes */
+
+ for (i = 0; i < len; i++) {
+ if (buf[i] < '0' || buf[i] > '9') return 0; /* bad char */
+ new = 10 * out + (gpr_uint32)(buf[i] - '0');
+ if (new < out) return 0; /* overflow */
+ out = new;
+ }
*result = out;
return 1;
}
-void
-gpr_reverse_bytes (char *str, int len)
-{
+void gpr_reverse_bytes(char *str, int len) {
char *p1, *p2;
- for (p1 = str, p2 = str + len - 1; p2 > p1; ++p1, --p2)
- {
- char temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
+ for (p1 = str, p2 = str + len - 1; p2 > p1; ++p1, --p2) {
+ char temp = *p1;
+ *p1 = *p2;
+ *p2 = temp;
+ }
}
-int
-gpr_ltoa (long value, char *string)
-{
+int gpr_ltoa(long value, char *string) {
int i = 0;
int neg = value < 0;
- if (value == 0)
- {
- string[0] = '0';
- string[1] = 0;
- return 1;
- }
-
- if (neg)
- value = -value;
- while (value)
- {
- string[i++] = (char) ('0' + value % 10);
- value /= 10;
- }
- if (neg)
- string[i++] = '-';
- gpr_reverse_bytes (string, i);
+ if (value == 0) {
+ string[0] = '0';
+ string[1] = 0;
+ return 1;
+ }
+
+ if (neg) value = -value;
+ while (value) {
+ string[i++] = (char)('0' + value % 10);
+ value /= 10;
+ }
+ if (neg) string[i++] = '-';
+ gpr_reverse_bytes(string, i);
string[i] = 0;
return i;
}
-char *
-gpr_strjoin (const char **strs, size_t nstrs, size_t * final_length)
-{
- return gpr_strjoin_sep (strs, nstrs, "", final_length);
+char *gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length) {
+ return gpr_strjoin_sep(strs, nstrs, "", final_length);
}
-char *
-gpr_strjoin_sep (const char **strs, size_t nstrs, const char *sep, size_t * final_length)
-{
- const size_t sep_len = strlen (sep);
+char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
+ size_t *final_length) {
+ const size_t sep_len = strlen(sep);
size_t out_length = 0;
size_t i;
char *out;
- for (i = 0; i < nstrs; i++)
- {
- out_length += strlen (strs[i]);
- }
- out_length += 1; /* null terminator */
- if (nstrs > 0)
- {
- out_length += sep_len * (nstrs - 1); /* separators */
- }
- out = gpr_malloc (out_length);
+ for (i = 0; i < nstrs; i++) {
+ out_length += strlen(strs[i]);
+ }
+ out_length += 1; /* null terminator */
+ if (nstrs > 0) {
+ out_length += sep_len * (nstrs - 1); /* separators */
+ }
+ out = gpr_malloc(out_length);
out_length = 0;
- for (i = 0; i < nstrs; i++)
- {
- const size_t slen = strlen (strs[i]);
- if (i != 0)
- {
- memcpy (out + out_length, sep, sep_len);
- out_length += sep_len;
- }
- memcpy (out + out_length, strs[i], slen);
- out_length += slen;
+ for (i = 0; i < nstrs; i++) {
+ const size_t slen = strlen(strs[i]);
+ if (i != 0) {
+ memcpy(out + out_length, sep, sep_len);
+ out_length += sep_len;
}
+ memcpy(out + out_length, strs[i], slen);
+ out_length += slen;
+ }
out[out_length] = 0;
- if (final_length != NULL)
- {
- *final_length = out_length;
- }
+ if (final_length != NULL) {
+ *final_length = out_length;
+ }
return out;
}
@@ -259,83 +213,63 @@ gpr_strjoin_sep (const char **strs, size_t nstrs, const char *sep, size_t * fina
* str.
*
* Returns 1 and updates \a begin and \a end. Returns 0 otherwise. */
-static int
-slice_find_separator_offset (const gpr_slice str, const char *sep, const size_t read_offset, size_t * begin, size_t * end)
-{
+static int slice_find_separator_offset(const gpr_slice str, const char *sep,
+ const size_t read_offset, size_t *begin,
+ size_t *end) {
size_t i;
- const gpr_uint8 *str_ptr = GPR_SLICE_START_PTR (str) + read_offset;
- const size_t str_len = GPR_SLICE_LENGTH (str) - read_offset;
- const size_t sep_len = strlen (sep);
- if (str_len < sep_len)
- {
- return 0;
- }
-
- for (i = 0; i <= str_len - sep_len; i++)
- {
- if (memcmp (str_ptr + i, sep, sep_len) == 0)
- {
- *begin = read_offset;
- *end = read_offset + i;
- return 1;
- }
+ const gpr_uint8 *str_ptr = GPR_SLICE_START_PTR(str) + read_offset;
+ const size_t str_len = GPR_SLICE_LENGTH(str) - read_offset;
+ const size_t sep_len = strlen(sep);
+ if (str_len < sep_len) {
+ return 0;
+ }
+
+ for (i = 0; i <= str_len - sep_len; i++) {
+ if (memcmp(str_ptr + i, sep, sep_len) == 0) {
+ *begin = read_offset;
+ *end = read_offset + i;
+ return 1;
}
+ }
return 0;
}
-void
-gpr_slice_split (gpr_slice str, const char *sep, gpr_slice_buffer * dst)
-{
- const size_t sep_len = strlen (sep);
+void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst) {
+ const size_t sep_len = strlen(sep);
size_t begin, end;
- GPR_ASSERT (sep_len > 0);
-
- if (slice_find_separator_offset (str, sep, 0, &begin, &end) != 0)
- {
- do
- {
- gpr_slice_buffer_add_indexed (dst, gpr_slice_sub (str, begin, end));
- }
- while (slice_find_separator_offset (str, sep, end + sep_len, &begin, &end) != 0);
- gpr_slice_buffer_add_indexed (dst, gpr_slice_sub (str, end + sep_len, GPR_SLICE_LENGTH (str)));
- }
- else
- { /* no sep found, add whole input */
- gpr_slice_buffer_add_indexed (dst, gpr_slice_ref (str));
- }
+ GPR_ASSERT(sep_len > 0);
+
+ if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
+ do {
+ gpr_slice_buffer_add_indexed(dst, gpr_slice_sub(str, begin, end));
+ } while (slice_find_separator_offset(str, sep, end + sep_len, &begin,
+ &end) != 0);
+ gpr_slice_buffer_add_indexed(
+ dst, gpr_slice_sub(str, end + sep_len, GPR_SLICE_LENGTH(str)));
+ } else { /* no sep found, add whole input */
+ gpr_slice_buffer_add_indexed(dst, gpr_slice_ref(str));
+ }
}
-void
-gpr_strvec_init (gpr_strvec * sv)
-{
- memset (sv, 0, sizeof (*sv));
-}
+void gpr_strvec_init(gpr_strvec *sv) { memset(sv, 0, sizeof(*sv)); }
-void
-gpr_strvec_destroy (gpr_strvec * sv)
-{
+void gpr_strvec_destroy(gpr_strvec *sv) {
size_t i;
- for (i = 0; i < sv->count; i++)
- {
- gpr_free (sv->strs[i]);
- }
- gpr_free (sv->strs);
+ for (i = 0; i < sv->count; i++) {
+ gpr_free(sv->strs[i]);
+ }
+ gpr_free(sv->strs);
}
-void
-gpr_strvec_add (gpr_strvec * sv, char *str)
-{
- if (sv->count == sv->capacity)
- {
- sv->capacity = GPR_MAX (sv->capacity + 8, sv->capacity * 2);
- sv->strs = gpr_realloc (sv->strs, sizeof (char *) * sv->capacity);
- }
+void gpr_strvec_add(gpr_strvec *sv, char *str) {
+ if (sv->count == sv->capacity) {
+ sv->capacity = GPR_MAX(sv->capacity + 8, sv->capacity * 2);
+ sv->strs = gpr_realloc(sv->strs, sizeof(char *) * sv->capacity);
+ }
sv->strs[sv->count++] = str;
}
-char *
-gpr_strvec_flatten (gpr_strvec * sv, size_t * final_length)
-{
- return gpr_strjoin ((const char **) sv->strs, sv->count, final_length);
+char *gpr_strvec_flatten(gpr_strvec *sv, size_t *final_length) {
+ return gpr_strjoin((const char **)sv->strs, sv->count, final_length);
}
diff --git a/src/core/support/string.h b/src/core/support/string.h
index 4304bcb5f5..a28e00fd3e 100644
--- a/src/core/support/string.h
+++ b/src/core/support/string.h
@@ -41,8 +41,7 @@
#include <grpc/support/slice.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* String utility functions */
@@ -53,14 +52,15 @@ extern "C"
/* Converts array buf, of length len, into a C string according to the flags.
Result should be freed with gpr_free() */
- char *gpr_dump (const char *buf, size_t len, gpr_uint32 flags);
+char *gpr_dump(const char *buf, size_t len, gpr_uint32 flags);
/* Calls gpr_dump on a slice. */
- char *gpr_dump_slice (gpr_slice slice, gpr_uint32 flags);
+char *gpr_dump_slice(gpr_slice slice, gpr_uint32 flags);
/* Parses an array of bytes into an integer (base 10). Returns 1 on success,
0 on failure. */
- int gpr_parse_bytes_to_uint32 (const char *data, size_t length, gpr_uint32 * result);
+int gpr_parse_bytes_to_uint32(const char *data, size_t length,
+ gpr_uint32 *result);
/* Minimum buffer size for calling ltoa */
#define GPR_LTOA_MIN_BUFSIZE (3 * sizeof(long))
@@ -68,44 +68,44 @@ extern "C"
/* Convert a long to a string in base 10; returns the length of the
output string (or 0 on failure).
output must be at least GPR_LTOA_MIN_BUFSIZE bytes long. */
- int gpr_ltoa (long value, char *output);
+int gpr_ltoa(long value, char *output);
/* Reverse a run of bytes */
- void gpr_reverse_bytes (char *str, int len);
+void gpr_reverse_bytes(char *str, int len);
/* Join a set of strings, returning the resulting string.
Total combined length (excluding null terminator) is returned in total_length
if it is non-null. */
- char *gpr_strjoin (const char **strs, size_t nstrs, size_t * total_length);
+char *gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length);
/* Join a set of strings using a separator, returning the resulting string.
Total combined length (excluding null terminator) is returned in total_length
if it is non-null. */
- char *gpr_strjoin_sep (const char **strs, size_t nstrs, const char *sep, size_t * total_length);
+char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
+ size_t *total_length);
/** Split \a str by the separator \a sep. Results are stored in \a dst, which
* should be a properly initialized instance. */
- void gpr_slice_split (gpr_slice str, const char *sep, gpr_slice_buffer * dst);
+void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst);
/* A vector of strings... for building up a final string one piece at a time */
- typedef struct
- {
- char **strs;
- size_t count;
- size_t capacity;
- } gpr_strvec;
+typedef struct {
+ char **strs;
+ size_t count;
+ size_t capacity;
+} gpr_strvec;
/* Initialize/destroy */
- void gpr_strvec_init (gpr_strvec * strs);
- void gpr_strvec_destroy (gpr_strvec * strs);
+void gpr_strvec_init(gpr_strvec *strs);
+void gpr_strvec_destroy(gpr_strvec *strs);
/* Add a string to a strvec, takes ownership of the string */
- void gpr_strvec_add (gpr_strvec * strs, char *add);
+void gpr_strvec_add(gpr_strvec *strs, char *add);
/* Return a joined string with all added substrings, optionally setting
total_length as per gpr_strjoin */
- char *gpr_strvec_flatten (gpr_strvec * strs, size_t * total_length);
+char *gpr_strvec_flatten(gpr_strvec *strs, size_t *total_length);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_SUPPORT_STRING_H */
+#endif /* GRPC_INTERNAL_CORE_SUPPORT_STRING_H */
diff --git a/src/core/support/string_posix.c b/src/core/support/string_posix.c
index 729d5b1a9c..25c333db4e 100644
--- a/src/core/support/string_posix.c
+++ b/src/core/support/string_posix.c
@@ -41,50 +41,44 @@
#include <grpc/support/alloc.h>
-int
-gpr_asprintf (char **strp, const char *format, ...)
-{
+int gpr_asprintf(char **strp, const char *format, ...) {
va_list args;
int ret;
char buf[64];
size_t strp_buflen;
/* Use a constant-sized buffer to determine the length. */
- va_start (args, format);
- ret = vsnprintf (buf, sizeof (buf), format, args);
- va_end (args);
- if (ret < 0)
- {
- *strp = NULL;
- return -1;
- }
+ va_start(args, format);
+ ret = vsnprintf(buf, sizeof(buf), format, args);
+ va_end(args);
+ if (ret < 0) {
+ *strp = NULL;
+ return -1;
+ }
/* Allocate a new buffer, with space for the NUL terminator. */
- strp_buflen = (size_t) ret + 1;
- if ((*strp = gpr_malloc (strp_buflen)) == NULL)
- {
- /* This shouldn't happen, because gpr_malloc() calls abort(). */
- return -1;
- }
+ strp_buflen = (size_t)ret + 1;
+ if ((*strp = gpr_malloc(strp_buflen)) == NULL) {
+ /* This shouldn't happen, because gpr_malloc() calls abort(). */
+ return -1;
+ }
/* Return early if we have all the bytes. */
- if (strp_buflen <= sizeof (buf))
- {
- memcpy (*strp, buf, strp_buflen);
- return ret;
- }
+ if (strp_buflen <= sizeof(buf)) {
+ memcpy(*strp, buf, strp_buflen);
+ return ret;
+ }
/* Try again using the larger buffer. */
- va_start (args, format);
- ret = vsnprintf (*strp, strp_buflen, format, args);
- va_end (args);
- if ((size_t) ret == strp_buflen - 1)
- {
- return ret;
- }
+ va_start(args, format);
+ ret = vsnprintf(*strp, strp_buflen, format, args);
+ va_end(args);
+ if ((size_t)ret == strp_buflen - 1) {
+ return ret;
+ }
/* This should never happen. */
- gpr_free (*strp);
+ gpr_free(*strp);
*strp = NULL;
return -1;
}
diff --git a/src/core/support/string_win32.c b/src/core/support/string_win32.c
index 6d1909fa04..914ba8771c 100644
--- a/src/core/support/string_win32.c
+++ b/src/core/support/string_win32.c
@@ -45,82 +45,65 @@
#include "src/core/support/string.h"
-int
-gpr_asprintf (char **strp, const char *format, ...)
-{
+int gpr_asprintf(char **strp, const char *format, ...) {
va_list args;
int ret;
size_t strp_buflen;
/* Determine the length. */
- va_start (args, format);
- ret = _vscprintf (format, args);
- va_end (args);
- if (ret < 0)
- {
- *strp = NULL;
- return -1;
- }
+ va_start(args, format);
+ ret = _vscprintf(format, args);
+ va_end(args);
+ if (ret < 0) {
+ *strp = NULL;
+ return -1;
+ }
/* Allocate a new buffer, with space for the NUL terminator. */
- strp_buflen = (size_t) ret + 1;
- if ((*strp = gpr_malloc (strp_buflen)) == NULL)
- {
- /* This shouldn't happen, because gpr_malloc() calls abort(). */
- return -1;
- }
+ strp_buflen = (size_t)ret + 1;
+ if ((*strp = gpr_malloc(strp_buflen)) == NULL) {
+ /* This shouldn't happen, because gpr_malloc() calls abort(). */
+ return -1;
+ }
/* Print to the buffer. */
- va_start (args, format);
- ret = vsnprintf_s (*strp, strp_buflen, _TRUNCATE, format, args);
- va_end (args);
- if ((size_t) ret == strp_buflen - 1)
- {
- return ret;
- }
+ va_start(args, format);
+ ret = vsnprintf_s(*strp, strp_buflen, _TRUNCATE, format, args);
+ va_end(args);
+ if ((size_t)ret == strp_buflen - 1) {
+ return ret;
+ }
/* This should never happen. */
- gpr_free (*strp);
+ gpr_free(*strp);
*strp = NULL;
return -1;
}
#if defined UNICODE || defined _UNICODE
LPTSTR
-gpr_char_to_tchar (LPCSTR input)
-{
+gpr_char_to_tchar(LPCSTR input) {
LPTSTR ret;
- int needed = MultiByteToWideChar (CP_UTF8, 0, input, -1, NULL, 0);
- if (needed == 0)
- return NULL;
- ret = gpr_malloc (needed * sizeof (TCHAR));
- MultiByteToWideChar (CP_UTF8, 0, input, -1, ret, needed);
+ int needed = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
+ if (needed == 0) return NULL;
+ ret = gpr_malloc(needed * sizeof(TCHAR));
+ MultiByteToWideChar(CP_UTF8, 0, input, -1, ret, needed);
return ret;
}
LPSTR
-gpr_tchar_to_char (LPCTSTR input)
-{
+gpr_tchar_to_char(LPCTSTR input) {
LPSTR ret;
- int needed = WideCharToMultiByte (CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
- if (needed == 0)
- return NULL;
- ret = gpr_malloc (needed);
- WideCharToMultiByte (CP_UTF8, 0, input, -1, ret, needed, NULL, NULL);
+ int needed = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
+ if (needed == 0) return NULL;
+ ret = gpr_malloc(needed);
+ WideCharToMultiByte(CP_UTF8, 0, input, -1, ret, needed, NULL, NULL);
return ret;
}
#else
-char *
-gpr_tchar_to_char (LPTSTR input)
-{
- return gpr_strdup (input);
-}
+char *gpr_tchar_to_char(LPTSTR input) { return gpr_strdup(input); }
-char *
-gpr_char_to_tchar (LPTSTR input)
-{
- return gpr_strdup (input);
-}
+char *gpr_char_to_tchar(LPTSTR input) { return gpr_strdup(input); }
#endif
#endif /* GPR_WIN32 */
diff --git a/src/core/support/string_win32.h b/src/core/support/string_win32.h
index a07bdd9c47..e3043656fb 100644
--- a/src/core/support/string_win32.h
+++ b/src/core/support/string_win32.h
@@ -39,8 +39,8 @@
#ifdef GPR_WIN32
/* These allocate new strings using gpr_malloc to convert from and to utf-8. */
-LPTSTR gpr_char_to_tchar (LPCSTR input);
-LPSTR gpr_tchar_to_char (LPCTSTR input);
+LPTSTR gpr_char_to_tchar(LPCSTR input);
+LPSTR gpr_tchar_to_char(LPCTSTR input);
#endif /* GPR_WIN32 */
diff --git a/src/core/support/subprocess_posix.c b/src/core/support/subprocess_posix.c
index eb5cc63d62..171054e4da 100644
--- a/src/core/support/subprocess_posix.c
+++ b/src/core/support/subprocess_posix.c
@@ -50,85 +50,63 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-struct gpr_subprocess
-{
+struct gpr_subprocess {
int pid;
int joined;
};
-const char *
-gpr_subprocess_binary_extension ()
-{
- return "";
-}
+const char *gpr_subprocess_binary_extension() { return ""; }
-gpr_subprocess *
-gpr_subprocess_create (int argc, const char **argv)
-{
+gpr_subprocess *gpr_subprocess_create(int argc, const char **argv) {
gpr_subprocess *r;
int pid;
char **exec_args;
- pid = fork ();
- if (pid == -1)
- {
- return NULL;
- }
- else if (pid == 0)
- {
- exec_args = gpr_malloc (((size_t) argc + 1) * sizeof (char *));
- memcpy (exec_args, argv, (size_t) argc * sizeof (char *));
- exec_args[argc] = NULL;
- execv (exec_args[0], exec_args);
- /* if we reach here, an error has occurred */
- gpr_log (GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror (errno));
- _exit (1);
- return NULL;
- }
- else
- {
- r = gpr_malloc (sizeof (gpr_subprocess));
- memset (r, 0, sizeof (*r));
- r->pid = pid;
- return r;
- }
+ pid = fork();
+ if (pid == -1) {
+ return NULL;
+ } else if (pid == 0) {
+ exec_args = gpr_malloc(((size_t)argc + 1) * sizeof(char *));
+ memcpy(exec_args, argv, (size_t)argc * sizeof(char *));
+ exec_args[argc] = NULL;
+ execv(exec_args[0], exec_args);
+ /* if we reach here, an error has occurred */
+ gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
+ _exit(1);
+ return NULL;
+ } else {
+ r = gpr_malloc(sizeof(gpr_subprocess));
+ memset(r, 0, sizeof(*r));
+ r->pid = pid;
+ return r;
+ }
}
-void
-gpr_subprocess_destroy (gpr_subprocess * p)
-{
- if (!p->joined)
- {
- kill (p->pid, SIGKILL);
- gpr_subprocess_join (p);
- }
- gpr_free (p);
+void gpr_subprocess_destroy(gpr_subprocess *p) {
+ if (!p->joined) {
+ kill(p->pid, SIGKILL);
+ gpr_subprocess_join(p);
+ }
+ gpr_free(p);
}
-int
-gpr_subprocess_join (gpr_subprocess * p)
-{
+int gpr_subprocess_join(gpr_subprocess *p) {
int status;
retry:
- if (waitpid (p->pid, &status, 0) == -1)
- {
- if (errno == EINTR)
- {
- goto retry;
- }
- gpr_log (GPR_ERROR, "waitpid failed: %s", strerror (errno));
- return -1;
+ if (waitpid(p->pid, &status, 0) == -1) {
+ if (errno == EINTR) {
+ goto retry;
}
+ gpr_log(GPR_ERROR, "waitpid failed: %s", strerror(errno));
+ return -1;
+ }
return status;
}
-void
-gpr_subprocess_interrupt (gpr_subprocess * p)
-{
- if (!p->joined)
- {
- kill (p->pid, SIGINT);
- }
+void gpr_subprocess_interrupt(gpr_subprocess *p) {
+ if (!p->joined) {
+ kill(p->pid, SIGINT);
+ }
}
#endif /* GPR_POSIX_SUBPROCESS */
diff --git a/src/core/support/sync.c b/src/core/support/sync.c
index de3ec7c86f..d3cf77faea 100644
--- a/src/core/support/sync.c
+++ b/src/core/support/sync.c
@@ -39,120 +39,84 @@
/* Number of mutexes to allocate for events, to avoid lock contention.
Should be a prime. */
-enum
-{ event_sync_partitions = 31 };
+enum { event_sync_partitions = 31 };
/* Events are partitioned by address to avoid lock contention. */
-static struct sync_array_s
-{
+static struct sync_array_s {
gpr_mu mu;
gpr_cv cv;
} sync_array[event_sync_partitions];
/* This routine is executed once on first use, via event_once */
static gpr_once event_once = GPR_ONCE_INIT;
-static void
-event_initialize (void)
-{
+static void event_initialize(void) {
int i;
- for (i = 0; i != event_sync_partitions; i++)
- {
- gpr_mu_init (&sync_array[i].mu);
- gpr_cv_init (&sync_array[i].cv);
- }
+ for (i = 0; i != event_sync_partitions; i++) {
+ gpr_mu_init(&sync_array[i].mu);
+ gpr_cv_init(&sync_array[i].cv);
+ }
}
/* Hash ev into an element of sync_array[]. */
-static struct sync_array_s *
-hash (gpr_event * ev)
-{
- return &sync_array[((gpr_uintptr) ev) % event_sync_partitions];
+static struct sync_array_s *hash(gpr_event *ev) {
+ return &sync_array[((gpr_uintptr)ev) % event_sync_partitions];
}
-void
-gpr_event_init (gpr_event * ev)
-{
- gpr_once_init (&event_once, &event_initialize);
+void gpr_event_init(gpr_event *ev) {
+ gpr_once_init(&event_once, &event_initialize);
ev->state = 0;
}
-void
-gpr_event_set (gpr_event * ev, void *value)
-{
- struct sync_array_s *s = hash (ev);
- gpr_mu_lock (&s->mu);
- GPR_ASSERT (gpr_atm_acq_load (&ev->state) == 0);
- gpr_atm_rel_store (&ev->state, (gpr_atm) value);
- gpr_cv_broadcast (&s->cv);
- gpr_mu_unlock (&s->mu);
- GPR_ASSERT (value != NULL);
+void gpr_event_set(gpr_event *ev, void *value) {
+ struct sync_array_s *s = hash(ev);
+ gpr_mu_lock(&s->mu);
+ GPR_ASSERT(gpr_atm_acq_load(&ev->state) == 0);
+ gpr_atm_rel_store(&ev->state, (gpr_atm)value);
+ gpr_cv_broadcast(&s->cv);
+ gpr_mu_unlock(&s->mu);
+ GPR_ASSERT(value != NULL);
}
-void *
-gpr_event_get (gpr_event * ev)
-{
- return (void *) gpr_atm_acq_load (&ev->state);
+void *gpr_event_get(gpr_event *ev) {
+ return (void *)gpr_atm_acq_load(&ev->state);
}
-void *
-gpr_event_wait (gpr_event * ev, gpr_timespec abs_deadline)
-{
- void *result = (void *) gpr_atm_acq_load (&ev->state);
- if (result == NULL)
- {
- struct sync_array_s *s = hash (ev);
- gpr_mu_lock (&s->mu);
- do
- {
- result = (void *) gpr_atm_acq_load (&ev->state);
- }
- while (result == NULL && !gpr_cv_wait (&s->cv, &s->mu, abs_deadline));
- gpr_mu_unlock (&s->mu);
- }
+void *gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline) {
+ void *result = (void *)gpr_atm_acq_load(&ev->state);
+ if (result == NULL) {
+ struct sync_array_s *s = hash(ev);
+ gpr_mu_lock(&s->mu);
+ do {
+ result = (void *)gpr_atm_acq_load(&ev->state);
+ } while (result == NULL && !gpr_cv_wait(&s->cv, &s->mu, abs_deadline));
+ gpr_mu_unlock(&s->mu);
+ }
return result;
}
-void
-gpr_ref_init (gpr_refcount * r, int n)
-{
- gpr_atm_rel_store (&r->count, n);
-}
+void gpr_ref_init(gpr_refcount *r, int n) { gpr_atm_rel_store(&r->count, n); }
-void
-gpr_ref (gpr_refcount * r)
-{
- gpr_atm_no_barrier_fetch_add (&r->count, 1);
-}
+void gpr_ref(gpr_refcount *r) { gpr_atm_no_barrier_fetch_add(&r->count, 1); }
-void
-gpr_refn (gpr_refcount * r, int n)
-{
- gpr_atm_no_barrier_fetch_add (&r->count, n);
+void gpr_refn(gpr_refcount *r, int n) {
+ gpr_atm_no_barrier_fetch_add(&r->count, n);
}
-int
-gpr_unref (gpr_refcount * r)
-{
- gpr_atm prior = gpr_atm_full_fetch_add (&r->count, -1);
- GPR_ASSERT (prior > 0);
+int gpr_unref(gpr_refcount *r) {
+ gpr_atm prior = gpr_atm_full_fetch_add(&r->count, -1);
+ GPR_ASSERT(prior > 0);
return prior == 1;
}
-void
-gpr_stats_init (gpr_stats_counter * c, gpr_intptr n)
-{
- gpr_atm_rel_store (&c->value, n);
+void gpr_stats_init(gpr_stats_counter *c, gpr_intptr n) {
+ gpr_atm_rel_store(&c->value, n);
}
-void
-gpr_stats_inc (gpr_stats_counter * c, gpr_intptr inc)
-{
- gpr_atm_no_barrier_fetch_add (&c->value, inc);
+void gpr_stats_inc(gpr_stats_counter *c, gpr_intptr inc) {
+ gpr_atm_no_barrier_fetch_add(&c->value, inc);
}
-gpr_intptr
-gpr_stats_read (const gpr_stats_counter * c)
-{
+gpr_intptr gpr_stats_read(const gpr_stats_counter *c) {
/* don't need acquire-load, but we have no no-barrier load yet */
- return gpr_atm_acq_load (&c->value);
+ return gpr_atm_acq_load(&c->value);
}
diff --git a/src/core/support/sync_posix.c b/src/core/support/sync_posix.c
index e7ca7e01d4..91c30989ce 100644
--- a/src/core/support/sync_posix.c
+++ b/src/core/support/sync_posix.c
@@ -41,90 +41,52 @@
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
-void
-gpr_mu_init (gpr_mu * mu)
-{
- GPR_ASSERT (pthread_mutex_init (mu, NULL) == 0);
-}
+void gpr_mu_init(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_init(mu, NULL) == 0); }
-void
-gpr_mu_destroy (gpr_mu * mu)
-{
- GPR_ASSERT (pthread_mutex_destroy (mu) == 0);
-}
+void gpr_mu_destroy(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_destroy(mu) == 0); }
-void
-gpr_mu_lock (gpr_mu * mu)
-{
- GPR_ASSERT (pthread_mutex_lock (mu) == 0);
-}
+void gpr_mu_lock(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_lock(mu) == 0); }
-void
-gpr_mu_unlock (gpr_mu * mu)
-{
- GPR_ASSERT (pthread_mutex_unlock (mu) == 0);
-}
+void gpr_mu_unlock(gpr_mu* mu) { GPR_ASSERT(pthread_mutex_unlock(mu) == 0); }
-int
-gpr_mu_trylock (gpr_mu * mu)
-{
- int err = pthread_mutex_trylock (mu);
- GPR_ASSERT (err == 0 || err == EBUSY);
+int gpr_mu_trylock(gpr_mu* mu) {
+ int err = pthread_mutex_trylock(mu);
+ GPR_ASSERT(err == 0 || err == EBUSY);
return err == 0;
}
/*----------------------------------------*/
-void
-gpr_cv_init (gpr_cv * cv)
-{
- GPR_ASSERT (pthread_cond_init (cv, NULL) == 0);
-}
+void gpr_cv_init(gpr_cv* cv) { GPR_ASSERT(pthread_cond_init(cv, NULL) == 0); }
-void
-gpr_cv_destroy (gpr_cv * cv)
-{
- GPR_ASSERT (pthread_cond_destroy (cv) == 0);
-}
+void gpr_cv_destroy(gpr_cv* cv) { GPR_ASSERT(pthread_cond_destroy(cv) == 0); }
-int
-gpr_cv_wait (gpr_cv * cv, gpr_mu * mu, gpr_timespec abs_deadline)
-{
+int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
int err = 0;
- if (gpr_time_cmp (abs_deadline, gpr_inf_future (abs_deadline.clock_type)) == 0)
- {
- err = pthread_cond_wait (cv, mu);
- }
- else
- {
- struct timespec abs_deadline_ts;
- abs_deadline = gpr_convert_clock_type (abs_deadline, GPR_CLOCK_REALTIME);
- abs_deadline_ts.tv_sec = abs_deadline.tv_sec;
- abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
- err = pthread_cond_timedwait (cv, mu, &abs_deadline_ts);
- }
- GPR_ASSERT (err == 0 || err == ETIMEDOUT || err == EAGAIN);
+ if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
+ 0) {
+ err = pthread_cond_wait(cv, mu);
+ } else {
+ struct timespec abs_deadline_ts;
+ abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
+ abs_deadline_ts.tv_sec = abs_deadline.tv_sec;
+ abs_deadline_ts.tv_nsec = abs_deadline.tv_nsec;
+ err = pthread_cond_timedwait(cv, mu, &abs_deadline_ts);
+ }
+ GPR_ASSERT(err == 0 || err == ETIMEDOUT || err == EAGAIN);
return err == ETIMEDOUT;
}
-void
-gpr_cv_signal (gpr_cv * cv)
-{
- GPR_ASSERT (pthread_cond_signal (cv) == 0);
-}
+void gpr_cv_signal(gpr_cv* cv) { GPR_ASSERT(pthread_cond_signal(cv) == 0); }
-void
-gpr_cv_broadcast (gpr_cv * cv)
-{
- GPR_ASSERT (pthread_cond_broadcast (cv) == 0);
+void gpr_cv_broadcast(gpr_cv* cv) {
+ GPR_ASSERT(pthread_cond_broadcast(cv) == 0);
}
/*----------------------------------------*/
-void
-gpr_once_init (gpr_once * once, void (*init_function) (void))
-{
- GPR_ASSERT (pthread_once (once, init_function) == 0);
+void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
+ GPR_ASSERT(pthread_once(once, init_function) == 0);
}
#endif /* GRP_POSIX_SYNC */
diff --git a/src/core/support/sync_win32.c b/src/core/support/sync_win32.c
index 9cfcad9fe7..69dd46399a 100644
--- a/src/core/support/sync_win32.c
+++ b/src/core/support/sync_win32.c
@@ -41,126 +41,88 @@
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
-void
-gpr_mu_init (gpr_mu * mu)
-{
- InitializeCriticalSection (&mu->cs);
+void gpr_mu_init(gpr_mu *mu) {
+ InitializeCriticalSection(&mu->cs);
mu->locked = 0;
}
-void
-gpr_mu_destroy (gpr_mu * mu)
-{
- DeleteCriticalSection (&mu->cs);
-}
+void gpr_mu_destroy(gpr_mu *mu) { DeleteCriticalSection(&mu->cs); }
-void
-gpr_mu_lock (gpr_mu * mu)
-{
- EnterCriticalSection (&mu->cs);
- GPR_ASSERT (!mu->locked);
+void gpr_mu_lock(gpr_mu *mu) {
+ EnterCriticalSection(&mu->cs);
+ GPR_ASSERT(!mu->locked);
mu->locked = 1;
}
-void
-gpr_mu_unlock (gpr_mu * mu)
-{
+void gpr_mu_unlock(gpr_mu *mu) {
mu->locked = 0;
- LeaveCriticalSection (&mu->cs);
+ LeaveCriticalSection(&mu->cs);
}
-int
-gpr_mu_trylock (gpr_mu * mu)
-{
- int result = TryEnterCriticalSection (&mu->cs);
- if (result)
- {
- if (mu->locked)
- { /* This thread already holds the lock. */
- LeaveCriticalSection (&mu->cs); /* Decrement lock count. */
- result = 0; /* Indicate failure */
- }
- mu->locked = 1;
+int gpr_mu_trylock(gpr_mu *mu) {
+ int result = TryEnterCriticalSection(&mu->cs);
+ if (result) {
+ if (mu->locked) { /* This thread already holds the lock. */
+ LeaveCriticalSection(&mu->cs); /* Decrement lock count. */
+ result = 0; /* Indicate failure */
}
+ mu->locked = 1;
+ }
return result;
}
/*----------------------------------------*/
-void
-gpr_cv_init (gpr_cv * cv)
-{
- InitializeConditionVariable (cv);
-}
+void gpr_cv_init(gpr_cv *cv) { InitializeConditionVariable(cv); }
-void
-gpr_cv_destroy (gpr_cv * cv)
-{
+void gpr_cv_destroy(gpr_cv *cv) {
/* Condition variables don't need destruction in Win32. */
}
-int
-gpr_cv_wait (gpr_cv * cv, gpr_mu * mu, gpr_timespec abs_deadline)
-{
+int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline) {
int timeout = 0;
DWORD timeout_max_ms;
mu->locked = 0;
- if (gpr_time_cmp (abs_deadline, gpr_inf_future (abs_deadline.clock_type)) == 0)
- {
- SleepConditionVariableCS (cv, &mu->cs, INFINITE);
- }
- else
- {
- gpr_timespec now = gpr_now (abs_deadline.clock_type);
- gpr_int64 now_ms = (gpr_int64) now.tv_sec * 1000 + now.tv_nsec / 1000000;
- gpr_int64 deadline_ms = (gpr_int64) abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000;
- if (now_ms >= deadline_ms)
- {
- timeout = 1;
- }
- else
- {
- timeout_max_ms = (DWORD) min (deadline_ms - now_ms, INFINITE - 1);
- timeout = (SleepConditionVariableCS (cv, &mu->cs, timeout_max_ms) == 0 && GetLastError () == ERROR_TIMEOUT);
- }
+ if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
+ 0) {
+ SleepConditionVariableCS(cv, &mu->cs, INFINITE);
+ } else {
+ gpr_timespec now = gpr_now(abs_deadline.clock_type);
+ gpr_int64 now_ms = (gpr_int64)now.tv_sec * 1000 + now.tv_nsec / 1000000;
+ gpr_int64 deadline_ms =
+ (gpr_int64)abs_deadline.tv_sec * 1000 + abs_deadline.tv_nsec / 1000000;
+ if (now_ms >= deadline_ms) {
+ timeout = 1;
+ } else {
+ timeout_max_ms = (DWORD)min(deadline_ms - now_ms, INFINITE - 1);
+ timeout = (SleepConditionVariableCS(cv, &mu->cs, timeout_max_ms) == 0 &&
+ GetLastError() == ERROR_TIMEOUT);
}
+ }
mu->locked = 1;
return timeout;
}
-void
-gpr_cv_signal (gpr_cv * cv)
-{
- WakeConditionVariable (cv);
-}
+void gpr_cv_signal(gpr_cv *cv) { WakeConditionVariable(cv); }
-void
-gpr_cv_broadcast (gpr_cv * cv)
-{
- WakeAllConditionVariable (cv);
-}
+void gpr_cv_broadcast(gpr_cv *cv) { WakeAllConditionVariable(cv); }
/*----------------------------------------*/
static void *dummy;
-struct run_once_func_arg
-{
- void (*init_function) (void);
+struct run_once_func_arg {
+ void (*init_function)(void);
};
-static BOOL CALLBACK
-run_once_func (gpr_once * once, void *v, void **pv)
-{
+static BOOL CALLBACK run_once_func(gpr_once *once, void *v, void **pv) {
struct run_once_func_arg *arg = v;
- (*arg->init_function) ();
+ (*arg->init_function)();
return 1;
}
-void
-gpr_once_init (gpr_once * once, void (*init_function) (void))
-{
+void gpr_once_init(gpr_once *once, void (*init_function)(void)) {
struct run_once_func_arg arg;
arg.init_function = init_function;
- InitOnceExecuteOnce (once, run_once_func, &arg, &dummy);
+ InitOnceExecuteOnce(once, run_once_func, &arg, &dummy);
}
#endif /* GPR_WIN32 */
diff --git a/src/core/support/thd.c b/src/core/support/thd.c
index 3023cc7802..41daeb5d0e 100644
--- a/src/core/support/thd.c
+++ b/src/core/support/thd.c
@@ -37,41 +37,28 @@
#include <grpc/support/thd.h>
-enum
-{ GPR_THD_JOINABLE = 1 };
+enum { GPR_THD_JOINABLE = 1 };
-gpr_thd_options
-gpr_thd_options_default (void)
-{
+gpr_thd_options gpr_thd_options_default(void) {
gpr_thd_options options;
- memset (&options, 0, sizeof (options));
+ memset(&options, 0, sizeof(options));
return options;
}
-void
-gpr_thd_options_set_detached (gpr_thd_options * options)
-{
+void gpr_thd_options_set_detached(gpr_thd_options* options) {
options->flags &= ~GPR_THD_JOINABLE;
}
-void
-gpr_thd_options_set_joinable (gpr_thd_options * options)
-{
+void gpr_thd_options_set_joinable(gpr_thd_options* options) {
options->flags |= GPR_THD_JOINABLE;
}
-int
-gpr_thd_options_is_detached (const gpr_thd_options * options)
-{
- if (!options)
- return 1;
+int gpr_thd_options_is_detached(const gpr_thd_options* options) {
+ if (!options) return 1;
return (options->flags & GPR_THD_JOINABLE) == 0;
}
-int
-gpr_thd_options_is_joinable (const gpr_thd_options * options)
-{
- if (!options)
- return 0;
+int gpr_thd_options_is_joinable(const gpr_thd_options* options) {
+ if (!options) return 0;
return (options->flags & GPR_THD_JOINABLE) == GPR_THD_JOINABLE;
}
diff --git a/src/core/support/thd_posix.c b/src/core/support/thd_posix.c
index 0091d3a289..c36d94d044 100644
--- a/src/core/support/thd_posix.c
+++ b/src/core/support/thd_posix.c
@@ -45,61 +45,47 @@
#include <grpc/support/thd.h>
#include <grpc/support/useful.h>
-struct thd_arg
-{
- void (*body) (void *arg); /* body of a thread */
- void *arg; /* argument to a thread */
+struct thd_arg {
+ void (*body)(void *arg); /* body of a thread */
+ void *arg; /* argument to a thread */
};
/* Body of every thread started via gpr_thd_new. */
-static void *
-thread_body (void *v)
-{
- struct thd_arg a = *(struct thd_arg *) v;
- gpr_free (v);
- (*a.body) (a.arg);
+static void *thread_body(void *v) {
+ struct thd_arg a = *(struct thd_arg *)v;
+ gpr_free(v);
+ (*a.body)(a.arg);
return NULL;
}
-int
-gpr_thd_new (gpr_thd_id * t, void (*thd_body) (void *arg), void *arg, const gpr_thd_options * options)
-{
+int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg,
+ const gpr_thd_options *options) {
int thread_started;
pthread_attr_t attr;
pthread_t p;
- struct thd_arg *a = gpr_malloc (sizeof (*a));
+ struct thd_arg *a = gpr_malloc(sizeof(*a));
a->body = thd_body;
a->arg = arg;
- GPR_ASSERT (pthread_attr_init (&attr) == 0);
- if (gpr_thd_options_is_detached (options))
- {
- GPR_ASSERT (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) == 0);
- }
- else
- {
- GPR_ASSERT (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE) == 0);
- }
- thread_started = (pthread_create (&p, &attr, &thread_body, a) == 0);
- GPR_ASSERT (pthread_attr_destroy (&attr) == 0);
- if (!thread_started)
- {
- gpr_free (a);
- }
- *t = (gpr_thd_id) p;
+ GPR_ASSERT(pthread_attr_init(&attr) == 0);
+ if (gpr_thd_options_is_detached(options)) {
+ GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) ==
+ 0);
+ } else {
+ GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) ==
+ 0);
+ }
+ thread_started = (pthread_create(&p, &attr, &thread_body, a) == 0);
+ GPR_ASSERT(pthread_attr_destroy(&attr) == 0);
+ if (!thread_started) {
+ gpr_free(a);
+ }
+ *t = (gpr_thd_id)p;
return thread_started;
}
-gpr_thd_id
-gpr_thd_currentid (void)
-{
- return (gpr_thd_id) pthread_self ();
-}
+gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)pthread_self(); }
-void
-gpr_thd_join (gpr_thd_id t)
-{
- pthread_join ((pthread_t) t, NULL);
-}
+void gpr_thd_join(gpr_thd_id t) { pthread_join((pthread_t)t, NULL); }
#endif /* GPR_POSIX_SYNC */
diff --git a/src/core/support/thd_win32.c b/src/core/support/thd_win32.c
index ffb5be3028..a9db180c1b 100644
--- a/src/core/support/thd_win32.c
+++ b/src/core/support/thd_win32.c
@@ -50,91 +50,68 @@
#error "Unknown compiler - please file a bug report"
#endif
-struct thd_info
-{
- void (*body) (void *arg); /* body of a thread */
- void *arg; /* argument to a thread */
- HANDLE join_event; /* if joinable, the join event */
- int joinable; /* true if not detached */
+struct thd_info {
+ void (*body)(void *arg); /* body of a thread */
+ void *arg; /* argument to a thread */
+ HANDLE join_event; /* if joinable, the join event */
+ int joinable; /* true if not detached */
};
static thread_local struct thd_info *g_thd_info;
/* Destroys a thread info */
-static void
-destroy_thread (struct thd_info *t)
-{
- if (t->joinable)
- CloseHandle (t->join_event);
- gpr_free (t);
+static void destroy_thread(struct thd_info *t) {
+ if (t->joinable) CloseHandle(t->join_event);
+ gpr_free(t);
}
/* Body of every thread started via gpr_thd_new. */
-static DWORD WINAPI
-thread_body (void *v)
-{
- g_thd_info = (struct thd_info *) v;
- g_thd_info->body (g_thd_info->arg);
- if (g_thd_info->joinable)
- {
- BOOL ret = SetEvent (g_thd_info->join_event);
- GPR_ASSERT (ret);
- }
- else
- {
- destroy_thread (g_thd_info);
- }
+static DWORD WINAPI thread_body(void *v) {
+ g_thd_info = (struct thd_info *)v;
+ g_thd_info->body(g_thd_info->arg);
+ if (g_thd_info->joinable) {
+ BOOL ret = SetEvent(g_thd_info->join_event);
+ GPR_ASSERT(ret);
+ } else {
+ destroy_thread(g_thd_info);
+ }
return 0;
}
-int
-gpr_thd_new (gpr_thd_id * t, void (*thd_body) (void *arg), void *arg, const gpr_thd_options * options)
-{
+int gpr_thd_new(gpr_thd_id *t, void (*thd_body)(void *arg), void *arg,
+ const gpr_thd_options *options) {
HANDLE handle;
- struct thd_info *info = gpr_malloc (sizeof (*info));
+ struct thd_info *info = gpr_malloc(sizeof(*info));
info->body = thd_body;
info->arg = arg;
*t = 0;
- if (gpr_thd_options_is_joinable (options))
- {
- info->joinable = 1;
- info->join_event = CreateEvent (NULL, FALSE, FALSE, NULL);
- if (info->join_event == NULL)
- {
- gpr_free (info);
- return 0;
- }
- }
- else
- {
- info->joinable = 0;
- }
- handle = CreateThread (NULL, 64 * 1024, thread_body, info, 0, NULL);
- if (handle == NULL)
- {
- destroy_thread (info);
- }
- else
- {
- *t = (gpr_thd_id) info;
- CloseHandle (handle);
+ if (gpr_thd_options_is_joinable(options)) {
+ info->joinable = 1;
+ info->join_event = CreateEvent(NULL, FALSE, FALSE, NULL);
+ if (info->join_event == NULL) {
+ gpr_free(info);
+ return 0;
}
+ } else {
+ info->joinable = 0;
+ }
+ handle = CreateThread(NULL, 64 * 1024, thread_body, info, 0, NULL);
+ if (handle == NULL) {
+ destroy_thread(info);
+ } else {
+ *t = (gpr_thd_id)info;
+ CloseHandle(handle);
+ }
return handle != NULL;
}
-gpr_thd_id
-gpr_thd_currentid (void)
-{
- return (gpr_thd_id) g_thd_info;
-}
+gpr_thd_id gpr_thd_currentid(void) { return (gpr_thd_id)g_thd_info; }
-void
-gpr_thd_join (gpr_thd_id t)
-{
- struct thd_info *info = (struct thd_info *) t;
- DWORD ret = WaitForSingleObject (info->join_event, INFINITE);
- GPR_ASSERT (ret == WAIT_OBJECT_0);
- destroy_thread (info);
+void gpr_thd_join(gpr_thd_id t) {
+ struct thd_info *info = (struct thd_info *)t;
+ DWORD ret = WaitForSingleObject(info->join_event, INFINITE);
+ GPR_ASSERT(ret == WAIT_OBJECT_0);
+ destroy_thread(info);
}
#endif /* GPR_WIN32 */
diff --git a/src/core/support/time.c b/src/core/support/time.c
index c6fdeaee0b..929adac918 100644
--- a/src/core/support/time.c
+++ b/src/core/support/time.c
@@ -39,28 +39,21 @@
#include <string.h>
#include <grpc/support/log.h>
-int
-gpr_time_cmp (gpr_timespec a, gpr_timespec b)
-{
+int gpr_time_cmp(gpr_timespec a, gpr_timespec b) {
int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
- GPR_ASSERT (a.clock_type == b.clock_type);
- if (cmp == 0)
- {
- cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec);
- }
+ GPR_ASSERT(a.clock_type == b.clock_type);
+ if (cmp == 0) {
+ cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec);
+ }
return cmp;
}
-gpr_timespec
-gpr_time_min (gpr_timespec a, gpr_timespec b)
-{
- return gpr_time_cmp (a, b) < 0 ? a : b;
+gpr_timespec gpr_time_min(gpr_timespec a, gpr_timespec b) {
+ return gpr_time_cmp(a, b) < 0 ? a : b;
}
-gpr_timespec
-gpr_time_max (gpr_timespec a, gpr_timespec b)
-{
- return gpr_time_cmp (a, b) > 0 ? a : b;
+gpr_timespec gpr_time_max(gpr_timespec a, gpr_timespec b) {
+ return gpr_time_cmp(a, b) > 0 ? a : b;
}
/* There's no standard TIME_T_MIN and TIME_T_MAX, so we construct them. The
@@ -79,9 +72,7 @@ gpr_time_max (gpr_timespec a, gpr_timespec b)
((t)(TYPE_IS_SIGNED(t) ? (TOP_BIT_OF_TYPE(t) - 1) \
: ((TOP_BIT_OF_TYPE(t) - 1) << 1) + 1))
-gpr_timespec
-gpr_time_0 (gpr_clock_type type)
-{
+gpr_timespec gpr_time_0(gpr_clock_type type) {
gpr_timespec out;
out.tv_sec = 0;
out.tv_nsec = 0;
@@ -89,21 +80,17 @@ gpr_time_0 (gpr_clock_type type)
return out;
}
-gpr_timespec
-gpr_inf_future (gpr_clock_type type)
-{
+gpr_timespec gpr_inf_future(gpr_clock_type type) {
gpr_timespec out;
- out.tv_sec = TYPE_MAX (time_t);
+ out.tv_sec = TYPE_MAX(time_t);
out.tv_nsec = 0;
out.clock_type = type;
return out;
}
-gpr_timespec
-gpr_inf_past (gpr_clock_type type)
-{
+gpr_timespec gpr_inf_past(gpr_clock_type type) {
gpr_timespec out;
- out.tv_sec = TYPE_MIN (time_t);
+ out.tv_sec = TYPE_MIN(time_t);
out.tv_nsec = 0;
out.clock_type = type;
return out;
@@ -112,318 +99,222 @@ gpr_inf_past (gpr_clock_type type)
/* TODO(ctiller): consider merging _nanos, _micros, _millis into a single
function for maintainability. Similarly for _seconds, _minutes, and _hours */
-gpr_timespec
-gpr_time_from_nanos (long ns, gpr_clock_type type)
-{
+gpr_timespec gpr_time_from_nanos(long ns, gpr_clock_type type) {
gpr_timespec result;
result.clock_type = type;
- if (ns == LONG_MAX)
- {
- result = gpr_inf_future (type);
- }
- else if (ns == LONG_MIN)
- {
- result = gpr_inf_past (type);
- }
- else if (ns >= 0)
- {
- result.tv_sec = ns / GPR_NS_PER_SEC;
- result.tv_nsec = (int) (ns - result.tv_sec * GPR_NS_PER_SEC);
- }
- else
- {
- /* Calculation carefully formulated to avoid any possible under/overflow. */
- result.tv_sec = (-(999999999 - (ns + GPR_NS_PER_SEC)) / GPR_NS_PER_SEC) - 1;
- result.tv_nsec = (int) (ns - result.tv_sec * GPR_NS_PER_SEC);
- }
+ if (ns == LONG_MAX) {
+ result = gpr_inf_future(type);
+ } else if (ns == LONG_MIN) {
+ result = gpr_inf_past(type);
+ } else if (ns >= 0) {
+ result.tv_sec = ns / GPR_NS_PER_SEC;
+ result.tv_nsec = (int)(ns - result.tv_sec * GPR_NS_PER_SEC);
+ } else {
+ /* Calculation carefully formulated to avoid any possible under/overflow. */
+ result.tv_sec = (-(999999999 - (ns + GPR_NS_PER_SEC)) / GPR_NS_PER_SEC) - 1;
+ result.tv_nsec = (int)(ns - result.tv_sec * GPR_NS_PER_SEC);
+ }
return result;
}
-gpr_timespec
-gpr_time_from_micros (long us, gpr_clock_type type)
-{
+gpr_timespec gpr_time_from_micros(long us, gpr_clock_type type) {
gpr_timespec result;
result.clock_type = type;
- if (us == LONG_MAX)
- {
- result = gpr_inf_future (type);
- }
- else if (us == LONG_MIN)
- {
- result = gpr_inf_past (type);
- }
- else if (us >= 0)
- {
- result.tv_sec = us / 1000000;
- result.tv_nsec = (int) ((us - result.tv_sec * 1000000) * 1000);
- }
- else
- {
- /* Calculation carefully formulated to avoid any possible under/overflow. */
- result.tv_sec = (-(999999 - (us + 1000000)) / 1000000) - 1;
- result.tv_nsec = (int) ((us - result.tv_sec * 1000000) * 1000);
- }
+ if (us == LONG_MAX) {
+ result = gpr_inf_future(type);
+ } else if (us == LONG_MIN) {
+ result = gpr_inf_past(type);
+ } else if (us >= 0) {
+ result.tv_sec = us / 1000000;
+ result.tv_nsec = (int)((us - result.tv_sec * 1000000) * 1000);
+ } else {
+ /* Calculation carefully formulated to avoid any possible under/overflow. */
+ result.tv_sec = (-(999999 - (us + 1000000)) / 1000000) - 1;
+ result.tv_nsec = (int)((us - result.tv_sec * 1000000) * 1000);
+ }
return result;
}
-gpr_timespec
-gpr_time_from_millis (long ms, gpr_clock_type type)
-{
+gpr_timespec gpr_time_from_millis(long ms, gpr_clock_type type) {
gpr_timespec result;
result.clock_type = type;
- if (ms == LONG_MAX)
- {
- result = gpr_inf_future (type);
- }
- else if (ms == LONG_MIN)
- {
- result = gpr_inf_past (type);
- }
- else if (ms >= 0)
- {
- result.tv_sec = ms / 1000;
- result.tv_nsec = (int) ((ms - result.tv_sec * 1000) * 1000000);
- }
- else
- {
- /* Calculation carefully formulated to avoid any possible under/overflow. */
- result.tv_sec = (-(999 - (ms + 1000)) / 1000) - 1;
- result.tv_nsec = (int) ((ms - result.tv_sec * 1000) * 1000000);
- }
+ if (ms == LONG_MAX) {
+ result = gpr_inf_future(type);
+ } else if (ms == LONG_MIN) {
+ result = gpr_inf_past(type);
+ } else if (ms >= 0) {
+ result.tv_sec = ms / 1000;
+ result.tv_nsec = (int)((ms - result.tv_sec * 1000) * 1000000);
+ } else {
+ /* Calculation carefully formulated to avoid any possible under/overflow. */
+ result.tv_sec = (-(999 - (ms + 1000)) / 1000) - 1;
+ result.tv_nsec = (int)((ms - result.tv_sec * 1000) * 1000000);
+ }
return result;
}
-gpr_timespec
-gpr_time_from_seconds (long s, gpr_clock_type type)
-{
+gpr_timespec gpr_time_from_seconds(long s, gpr_clock_type type) {
gpr_timespec result;
result.clock_type = type;
- if (s == LONG_MAX)
- {
- result = gpr_inf_future (type);
- }
- else if (s == LONG_MIN)
- {
- result = gpr_inf_past (type);
- }
- else
- {
- result.tv_sec = s;
- result.tv_nsec = 0;
- }
+ if (s == LONG_MAX) {
+ result = gpr_inf_future(type);
+ } else if (s == LONG_MIN) {
+ result = gpr_inf_past(type);
+ } else {
+ result.tv_sec = s;
+ result.tv_nsec = 0;
+ }
return result;
}
-gpr_timespec
-gpr_time_from_minutes (long m, gpr_clock_type type)
-{
+gpr_timespec gpr_time_from_minutes(long m, gpr_clock_type type) {
gpr_timespec result;
result.clock_type = type;
- if (m >= LONG_MAX / 60)
- {
- result = gpr_inf_future (type);
- }
- else if (m <= LONG_MIN / 60)
- {
- result = gpr_inf_past (type);
- }
- else
- {
- result.tv_sec = m * 60;
- result.tv_nsec = 0;
- }
+ if (m >= LONG_MAX / 60) {
+ result = gpr_inf_future(type);
+ } else if (m <= LONG_MIN / 60) {
+ result = gpr_inf_past(type);
+ } else {
+ result.tv_sec = m * 60;
+ result.tv_nsec = 0;
+ }
return result;
}
-gpr_timespec
-gpr_time_from_hours (long h, gpr_clock_type type)
-{
+gpr_timespec gpr_time_from_hours(long h, gpr_clock_type type) {
gpr_timespec result;
result.clock_type = type;
- if (h >= LONG_MAX / 3600)
- {
- result = gpr_inf_future (type);
- }
- else if (h <= LONG_MIN / 3600)
- {
- result = gpr_inf_past (type);
- }
- else
- {
- result.tv_sec = h * 3600;
- result.tv_nsec = 0;
- }
+ if (h >= LONG_MAX / 3600) {
+ result = gpr_inf_future(type);
+ } else if (h <= LONG_MIN / 3600) {
+ result = gpr_inf_past(type);
+ } else {
+ result.tv_sec = h * 3600;
+ result.tv_nsec = 0;
+ }
return result;
}
-gpr_timespec
-gpr_time_add (gpr_timespec a, gpr_timespec b)
-{
+gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b) {
gpr_timespec sum;
int inc = 0;
- GPR_ASSERT (b.clock_type == GPR_TIMESPAN);
+ GPR_ASSERT(b.clock_type == GPR_TIMESPAN);
sum.clock_type = a.clock_type;
sum.tv_nsec = a.tv_nsec + b.tv_nsec;
- if (sum.tv_nsec >= GPR_NS_PER_SEC)
- {
- sum.tv_nsec -= GPR_NS_PER_SEC;
- inc++;
- }
- if (a.tv_sec == TYPE_MAX (time_t) || a.tv_sec == TYPE_MIN (time_t))
- {
- sum = a;
- }
- else if (b.tv_sec == TYPE_MAX (time_t) || (b.tv_sec >= 0 && a.tv_sec >= TYPE_MAX (time_t) - b.tv_sec))
- {
- sum = gpr_inf_future (sum.clock_type);
- }
- else if (b.tv_sec == TYPE_MIN (time_t) || (b.tv_sec <= 0 && a.tv_sec <= TYPE_MIN (time_t) - b.tv_sec))
- {
- sum = gpr_inf_past (sum.clock_type);
- }
- else
- {
- sum.tv_sec = a.tv_sec + b.tv_sec;
- if (inc != 0 && sum.tv_sec == TYPE_MAX (time_t) - 1)
- {
- sum = gpr_inf_future (sum.clock_type);
- }
- else
- {
- sum.tv_sec += inc;
- }
- }
+ if (sum.tv_nsec >= GPR_NS_PER_SEC) {
+ sum.tv_nsec -= GPR_NS_PER_SEC;
+ inc++;
+ }
+ if (a.tv_sec == TYPE_MAX(time_t) || a.tv_sec == TYPE_MIN(time_t)) {
+ sum = a;
+ } else if (b.tv_sec == TYPE_MAX(time_t) ||
+ (b.tv_sec >= 0 && a.tv_sec >= TYPE_MAX(time_t) - b.tv_sec)) {
+ sum = gpr_inf_future(sum.clock_type);
+ } else if (b.tv_sec == TYPE_MIN(time_t) ||
+ (b.tv_sec <= 0 && a.tv_sec <= TYPE_MIN(time_t) - b.tv_sec)) {
+ sum = gpr_inf_past(sum.clock_type);
+ } else {
+ sum.tv_sec = a.tv_sec + b.tv_sec;
+ if (inc != 0 && sum.tv_sec == TYPE_MAX(time_t) - 1) {
+ sum = gpr_inf_future(sum.clock_type);
+ } else {
+ sum.tv_sec += inc;
+ }
+ }
return sum;
}
-gpr_timespec
-gpr_time_sub (gpr_timespec a, gpr_timespec b)
-{
+gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b) {
gpr_timespec diff;
int dec = 0;
- if (b.clock_type == GPR_TIMESPAN)
- {
- diff.clock_type = a.clock_type;
- }
- else
- {
- GPR_ASSERT (a.clock_type == b.clock_type);
- diff.clock_type = GPR_TIMESPAN;
- }
+ if (b.clock_type == GPR_TIMESPAN) {
+ diff.clock_type = a.clock_type;
+ } else {
+ GPR_ASSERT(a.clock_type == b.clock_type);
+ diff.clock_type = GPR_TIMESPAN;
+ }
diff.tv_nsec = a.tv_nsec - b.tv_nsec;
- if (diff.tv_nsec < 0)
- {
- diff.tv_nsec += GPR_NS_PER_SEC;
- dec++;
- }
- if (a.tv_sec == TYPE_MAX (time_t) || a.tv_sec == TYPE_MIN (time_t))
- {
- diff = a;
- }
- else if (b.tv_sec == TYPE_MIN (time_t) || (b.tv_sec <= 0 && a.tv_sec >= TYPE_MAX (time_t) + b.tv_sec))
- {
- diff = gpr_inf_future (GPR_CLOCK_REALTIME);
- }
- else if (b.tv_sec == TYPE_MAX (time_t) || (b.tv_sec >= 0 && a.tv_sec <= TYPE_MIN (time_t) + b.tv_sec))
- {
- diff = gpr_inf_past (GPR_CLOCK_REALTIME);
- }
- else
- {
- diff.tv_sec = a.tv_sec - b.tv_sec;
- if (dec != 0 && diff.tv_sec == TYPE_MIN (time_t) + 1)
- {
- diff = gpr_inf_past (GPR_CLOCK_REALTIME);
- }
- else
- {
- diff.tv_sec -= dec;
- }
- }
+ if (diff.tv_nsec < 0) {
+ diff.tv_nsec += GPR_NS_PER_SEC;
+ dec++;
+ }
+ if (a.tv_sec == TYPE_MAX(time_t) || a.tv_sec == TYPE_MIN(time_t)) {
+ diff = a;
+ } else if (b.tv_sec == TYPE_MIN(time_t) ||
+ (b.tv_sec <= 0 && a.tv_sec >= TYPE_MAX(time_t) + b.tv_sec)) {
+ diff = gpr_inf_future(GPR_CLOCK_REALTIME);
+ } else if (b.tv_sec == TYPE_MAX(time_t) ||
+ (b.tv_sec >= 0 && a.tv_sec <= TYPE_MIN(time_t) + b.tv_sec)) {
+ diff = gpr_inf_past(GPR_CLOCK_REALTIME);
+ } else {
+ diff.tv_sec = a.tv_sec - b.tv_sec;
+ if (dec != 0 && diff.tv_sec == TYPE_MIN(time_t) + 1) {
+ diff = gpr_inf_past(GPR_CLOCK_REALTIME);
+ } else {
+ diff.tv_sec -= dec;
+ }
+ }
return diff;
}
-int
-gpr_time_similar (gpr_timespec a, gpr_timespec b, gpr_timespec threshold)
-{
+int gpr_time_similar(gpr_timespec a, gpr_timespec b, gpr_timespec threshold) {
int cmp_ab;
- GPR_ASSERT (a.clock_type == b.clock_type);
- GPR_ASSERT (threshold.clock_type == GPR_TIMESPAN);
+ GPR_ASSERT(a.clock_type == b.clock_type);
+ GPR_ASSERT(threshold.clock_type == GPR_TIMESPAN);
- cmp_ab = gpr_time_cmp (a, b);
- if (cmp_ab == 0)
- return 1;
- if (cmp_ab < 0)
- {
- return gpr_time_cmp (gpr_time_sub (b, a), threshold) <= 0;
- }
- else
- {
- return gpr_time_cmp (gpr_time_sub (a, b), threshold) <= 0;
- }
+ cmp_ab = gpr_time_cmp(a, b);
+ if (cmp_ab == 0) return 1;
+ if (cmp_ab < 0) {
+ return gpr_time_cmp(gpr_time_sub(b, a), threshold) <= 0;
+ } else {
+ return gpr_time_cmp(gpr_time_sub(a, b), threshold) <= 0;
+ }
}
-gpr_int32
-gpr_time_to_millis (gpr_timespec t)
-{
- if (t.tv_sec >= 2147483)
- {
- if (t.tv_sec == 2147483 && t.tv_nsec < 648 * GPR_NS_PER_MS)
- {
- return 2147483 * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS;
- }
- return 2147483647;
- }
- else if (t.tv_sec <= -2147483)
- {
- /* TODO(ctiller): correct handling here (it's so far in the past do we
- care?) */
- return -2147483647;
- }
- else
- {
- return (gpr_int32) (t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS);
- }
+gpr_int32 gpr_time_to_millis(gpr_timespec t) {
+ if (t.tv_sec >= 2147483) {
+ if (t.tv_sec == 2147483 && t.tv_nsec < 648 * GPR_NS_PER_MS) {
+ return 2147483 * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS;
+ }
+ return 2147483647;
+ } else if (t.tv_sec <= -2147483) {
+ /* TODO(ctiller): correct handling here (it's so far in the past do we
+ care?) */
+ return -2147483647;
+ } else {
+ return (gpr_int32)(t.tv_sec * GPR_MS_PER_SEC + t.tv_nsec / GPR_NS_PER_MS);
+ }
}
-double
-gpr_timespec_to_micros (gpr_timespec t)
-{
- return (double) t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3;
+double gpr_timespec_to_micros(gpr_timespec t) {
+ return (double)t.tv_sec * GPR_US_PER_SEC + t.tv_nsec * 1e-3;
}
-gpr_timespec
-gpr_convert_clock_type (gpr_timespec t, gpr_clock_type clock_type)
-{
- if (t.clock_type == clock_type)
- {
+gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type) {
+ if (t.clock_type == clock_type) {
+ return t;
+ }
+
+ if (t.tv_nsec == 0) {
+ if (t.tv_sec == TYPE_MAX(time_t)) {
+ t.clock_type = clock_type;
return t;
}
-
- if (t.tv_nsec == 0)
- {
- if (t.tv_sec == TYPE_MAX (time_t))
- {
- t.clock_type = clock_type;
- return t;
- }
- if (t.tv_sec == TYPE_MIN (time_t))
- {
- t.clock_type = clock_type;
- return t;
- }
+ if (t.tv_sec == TYPE_MIN(time_t)) {
+ t.clock_type = clock_type;
+ return t;
}
+ }
- if (clock_type == GPR_TIMESPAN)
- {
- return gpr_time_sub (t, gpr_now (t.clock_type));
- }
+ if (clock_type == GPR_TIMESPAN) {
+ return gpr_time_sub(t, gpr_now(t.clock_type));
+ }
- if (t.clock_type == GPR_TIMESPAN)
- {
- return gpr_time_add (gpr_now (clock_type), t);
- }
+ if (t.clock_type == GPR_TIMESPAN) {
+ return gpr_time_add(gpr_now(clock_type), t);
+ }
- return gpr_time_add (gpr_now (clock_type), gpr_time_sub (t, gpr_now (t.clock_type)));
+ return gpr_time_add(gpr_now(clock_type),
+ gpr_time_sub(t, gpr_now(t.clock_type)));
}
diff --git a/src/core/support/time_posix.c b/src/core/support/time_posix.c
index 34cbd4b3c8..dcecff0d05 100644
--- a/src/core/support/time_posix.c
+++ b/src/core/support/time_posix.c
@@ -42,9 +42,7 @@
#include <grpc/support/log.h>
#include <grpc/support/time.h>
-static struct timespec
-timespec_from_gpr (gpr_timespec gts)
-{
+static struct timespec timespec_from_gpr(gpr_timespec gts) {
struct timespec rv;
rv.tv_sec = gts.tv_sec;
rv.tv_nsec = gts.tv_nsec;
@@ -52,40 +50,31 @@ timespec_from_gpr (gpr_timespec gts)
}
#if _POSIX_TIMERS > 0
-static gpr_timespec
-gpr_from_timespec (struct timespec ts, gpr_clock_type clock)
-{
+static gpr_timespec gpr_from_timespec(struct timespec ts,
+ gpr_clock_type clock) {
gpr_timespec rv;
rv.tv_sec = ts.tv_sec;
- rv.tv_nsec = (int) ts.tv_nsec;
+ rv.tv_nsec = (int)ts.tv_nsec;
rv.clock_type = clock;
return rv;
}
/** maps gpr_clock_type --> clockid_t for clock_gettime */
-static clockid_t clockid_for_gpr_clock[] = { CLOCK_MONOTONIC, CLOCK_REALTIME };
+static clockid_t clockid_for_gpr_clock[] = {CLOCK_MONOTONIC, CLOCK_REALTIME};
-void
-gpr_time_init (void)
-{
-}
+void gpr_time_init(void) {}
-gpr_timespec
-gpr_now (gpr_clock_type clock)
-{
+gpr_timespec gpr_now(gpr_clock_type clock) {
struct timespec now;
- GPR_ASSERT (clock != GPR_TIMESPAN);
- if (clock == GPR_CLOCK_PRECISE)
- {
- gpr_timespec ret;
- gpr_precise_clock_now (&ret);
- return ret;
- }
- else
- {
- clock_gettime (clockid_for_gpr_clock[clock], &now);
- return gpr_from_timespec (now, clock);
- }
+ GPR_ASSERT(clock != GPR_TIMESPAN);
+ if (clock == GPR_CLOCK_PRECISE) {
+ gpr_timespec ret;
+ gpr_precise_clock_now(&ret);
+ return ret;
+ } else {
+ clock_gettime(clockid_for_gpr_clock[clock], &now);
+ return gpr_from_timespec(now, clock);
+ }
}
#else
/* For some reason Apple's OSes haven't implemented clock_gettime. */
@@ -97,71 +86,61 @@ gpr_now (gpr_clock_type clock)
static double g_time_scale;
static uint64_t g_time_start;
-void
-gpr_time_init (void)
-{
- mach_timebase_info_data_t tb = { 0, 1 };
- mach_timebase_info (&tb);
+void gpr_time_init(void) {
+ mach_timebase_info_data_t tb = {0, 1};
+ mach_timebase_info(&tb);
g_time_scale = tb.numer;
g_time_scale /= tb.denom;
- g_time_start = mach_absolute_time ();
+ g_time_start = mach_absolute_time();
}
-gpr_timespec
-gpr_now (gpr_clock_type clock)
-{
+gpr_timespec gpr_now(gpr_clock_type clock) {
gpr_timespec now;
struct timeval now_tv;
double now_dbl;
now.clock_type = clock;
- switch (clock)
- {
+ switch (clock) {
case GPR_CLOCK_REALTIME:
- gettimeofday (&now_tv, NULL);
+ gettimeofday(&now_tv, NULL);
now.tv_sec = now_tv.tv_sec;
now.tv_nsec = now_tv.tv_usec * 1000;
break;
case GPR_CLOCK_MONOTONIC:
- now_dbl = (mach_absolute_time () - g_time_start) * g_time_scale;
- now.tv_sec = (time_t) (now_dbl * 1e-9);
- now.tv_nsec = (int) (now_dbl - ((double) now.tv_sec) * 1e9);
+ now_dbl = (mach_absolute_time() - g_time_start) * g_time_scale;
+ now.tv_sec = (time_t)(now_dbl * 1e-9);
+ now.tv_nsec = (int)(now_dbl - ((double)now.tv_sec) * 1e9);
break;
case GPR_CLOCK_PRECISE:
- gpr_precise_clock_now (&now);
+ gpr_precise_clock_now(&now);
break;
case GPR_TIMESPAN:
- abort ();
- }
+ abort();
+ }
return now;
}
#endif
-void
-gpr_sleep_until (gpr_timespec until)
-{
+void gpr_sleep_until(gpr_timespec until) {
gpr_timespec now;
gpr_timespec delta;
struct timespec delta_ts;
- for (;;)
- {
- /* We could simplify by using clock_nanosleep instead, but it might be
- * slightly less portable. */
- now = gpr_now (until.clock_type);
- if (gpr_time_cmp (until, now) <= 0)
- {
- return;
- }
-
- delta = gpr_time_sub (until, now);
- delta_ts = timespec_from_gpr (delta);
- if (nanosleep (&delta_ts, NULL) == 0)
- {
- break;
- }
+ for (;;) {
+ /* We could simplify by using clock_nanosleep instead, but it might be
+ * slightly less portable. */
+ now = gpr_now(until.clock_type);
+ if (gpr_time_cmp(until, now) <= 0) {
+ return;
+ }
+
+ delta = gpr_time_sub(until, now);
+ delta_ts = timespec_from_gpr(delta);
+ if (nanosleep(&delta_ts, NULL) == 0) {
+ break;
}
+ }
}
#endif /* GPR_POSIX_TIME */
diff --git a/src/core/support/time_precise.h b/src/core/support/time_precise.h
index 81b3049b72..a72d37e2f1 100644
--- a/src/core/support/time_precise.h
+++ b/src/core/support/time_precise.h
@@ -40,64 +40,52 @@
#ifdef GRPC_TIMERS_RDTSC
#if defined(__i386__)
-static void
-gpr_get_cycle_counter (long long int *clk)
-{
+static void gpr_get_cycle_counter(long long int *clk) {
long long int ret;
- __asm__ volatile ("rdtsc":"=A" (ret));
+ __asm__ volatile("rdtsc" : "=A"(ret));
*clk = ret;
}
// ----------------------------------------------------------------
#elif defined(__x86_64__) || defined(__amd64__)
-static void
-gpr_get_cycle_counter (long long int *clk)
-{
+static void gpr_get_cycle_counter(long long int *clk) {
unsigned long long low, high;
- __asm__ volatile ("rdtsc":"=a" (low), "=d" (high));
+ __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
*clk = (high << 32) | low;
}
#endif
static gpr_once precise_clock_init = GPR_ONCE_INIT;
static long long cycles_per_second = 0;
-static void
-gpr_precise_clock_init ()
-{
- time_t start = time (NULL);
+static void gpr_precise_clock_init() {
+ time_t start = time(NULL);
gpr_precise_clock start_cycle;
gpr_precise_clock end_cycle;
- while (time (NULL) == start)
+ while (time(NULL) == start)
;
- gpr_get_cycle_counter (&start_cycle);
- while (time (NULL) == start + 1)
+ gpr_get_cycle_counter(&start_cycle);
+ while (time(NULL) == start + 1)
;
- gpr_get_cycle_counter (&end_cycle);
+ gpr_get_cycle_counter(&end_cycle);
cycles_per_second = end_cycle - start_cycle;
}
-static double
-grpc_precise_clock_scaling_factor ()
-{
- gpr_once_init (&precise_clock_init, grpc_precise_clock_init);
+static double grpc_precise_clock_scaling_factor() {
+ gpr_once_init(&precise_clock_init, grpc_precise_clock_init);
return 1e6 / cycles_per_second;
}
-static void
-gpr_precise_clock_now (gpr_timespec * clk)
-{
+static void gpr_precise_clock_now(gpr_timespec *clk) {
long long int counter;
- gpr_get_cycle_counter (&counter);
+ gpr_get_cycle_counter(&counter);
clk->clock = GPR_CLOCK_REALTIME;
clk->tv_sec = counter / cycles_per_second;
clk->tv_nsec = counter % cycles_per_second;
}
-#else /* GRPC_TIMERS_RDTSC */
-static void
-gpr_precise_clock_now (gpr_timespec * clk)
-{
- *clk = gpr_now (GPR_CLOCK_REALTIME);
+#else /* GRPC_TIMERS_RDTSC */
+static void gpr_precise_clock_now(gpr_timespec* clk) {
+ *clk = gpr_now(GPR_CLOCK_REALTIME);
clk->clock_type = GPR_CLOCK_PRECISE;
}
#endif /* GRPC_TIMERS_RDTSC */
diff --git a/src/core/support/time_win32.c b/src/core/support/time_win32.c
index 5af49fe5cd..f794855429 100644
--- a/src/core/support/time_win32.c
+++ b/src/core/support/time_win32.c
@@ -44,64 +44,56 @@
static LARGE_INTEGER g_start_time;
static double g_time_scale;
-void
-gpr_time_init (void)
-{
+void gpr_time_init(void) {
LARGE_INTEGER frequency;
- QueryPerformanceFrequency (&frequency);
- QueryPerformanceCounter (&g_start_time);
+ QueryPerformanceFrequency(&frequency);
+ QueryPerformanceCounter(&g_start_time);
g_time_scale = 1.0 / frequency.QuadPart;
}
-gpr_timespec
-gpr_now (gpr_clock_type clock)
-{
+gpr_timespec gpr_now(gpr_clock_type clock) {
gpr_timespec now_tv;
struct _timeb now_tb;
LARGE_INTEGER timestamp;
double now_dbl;
now_tv.clock_type = clock;
- switch (clock)
- {
+ switch (clock) {
case GPR_CLOCK_REALTIME:
- _ftime_s (&now_tb);
+ _ftime_s(&now_tb);
now_tv.tv_sec = now_tb.time;
now_tv.tv_nsec = now_tb.millitm * 1000000;
break;
case GPR_CLOCK_MONOTONIC:
- QueryPerformanceCounter (&timestamp);
+ QueryPerformanceCounter(&timestamp);
now_dbl = (timestamp.QuadPart - g_start_time.QuadPart) * g_time_scale;
- now_tv.tv_sec = (time_t) now_dbl;
- now_tv.tv_nsec = (int) ((now_dbl - (double) now_tv.tv_sec) * 1e9);
+ now_tv.tv_sec = (time_t)now_dbl;
+ now_tv.tv_nsec = (int)((now_dbl - (double)now_tv.tv_sec) * 1e9);
break;
case GPR_CLOCK_PRECISE:
- gpr_precise_clock_now (&now_tv);
+ gpr_precise_clock_now(&now_tv);
break;
- }
+ }
return now_tv;
}
-void
-gpr_sleep_until (gpr_timespec until)
-{
+void gpr_sleep_until(gpr_timespec until) {
gpr_timespec now;
gpr_timespec delta;
DWORD sleep_millis;
- for (;;)
- {
- /* We could simplify by using clock_nanosleep instead, but it might be
- * slightly less portable. */
- now = gpr_now (until.clock_type);
- if (gpr_time_cmp (until, now) <= 0)
- {
- return;
- }
-
- delta = gpr_time_sub (until, now);
- sleep_millis = (DWORD) delta.tv_sec * GPR_MS_PER_SEC + delta.tv_nsec / GPR_NS_PER_MS;
- Sleep (sleep_millis);
+ for (;;) {
+ /* We could simplify by using clock_nanosleep instead, but it might be
+ * slightly less portable. */
+ now = gpr_now(until.clock_type);
+ if (gpr_time_cmp(until, now) <= 0) {
+ return;
}
+
+ delta = gpr_time_sub(until, now);
+ sleep_millis =
+ (DWORD)delta.tv_sec * GPR_MS_PER_SEC + delta.tv_nsec / GPR_NS_PER_MS;
+ Sleep(sleep_millis);
+ }
}
#endif /* GPR_WIN32 */
diff --git a/src/core/support/tls_pthread.c b/src/core/support/tls_pthread.c
index d24b2bc114..2d28226fc4 100644
--- a/src/core/support/tls_pthread.c
+++ b/src/core/support/tls_pthread.c
@@ -37,10 +37,8 @@
#include <grpc/support/tls.h>
-gpr_intptr
-gpr_tls_set (struct gpr_pthread_thread_local * tls, gpr_intptr value)
-{
- GPR_ASSERT (0 == pthread_setspecific (tls->key, (void *) value));
+gpr_intptr gpr_tls_set(struct gpr_pthread_thread_local *tls, gpr_intptr value) {
+ GPR_ASSERT(0 == pthread_setspecific(tls->key, (void *)value));
return value;
}
diff --git a/src/core/surface/byte_buffer.c b/src/core/surface/byte_buffer.c
index c26e3e1317..a930949f2d 100644
--- a/src/core/surface/byte_buffer.c
+++ b/src/core/surface/byte_buffer.c
@@ -35,79 +35,66 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-grpc_byte_buffer *
-grpc_raw_byte_buffer_create (gpr_slice * slices, size_t nslices)
-{
- return grpc_raw_compressed_byte_buffer_create (slices, nslices, GRPC_COMPRESS_NONE);
+grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices,
+ size_t nslices) {
+ return grpc_raw_compressed_byte_buffer_create(slices, nslices,
+ GRPC_COMPRESS_NONE);
}
-grpc_byte_buffer *
-grpc_raw_compressed_byte_buffer_create (gpr_slice * slices, size_t nslices, grpc_compression_algorithm compression)
-{
+grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
+ gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression) {
size_t i;
- grpc_byte_buffer *bb = malloc (sizeof (grpc_byte_buffer));
+ grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
bb->type = GRPC_BB_RAW;
bb->data.raw.compression = compression;
- gpr_slice_buffer_init (&bb->data.raw.slice_buffer);
- for (i = 0; i < nslices; i++)
- {
- gpr_slice_ref (slices[i]);
- gpr_slice_buffer_add (&bb->data.raw.slice_buffer, slices[i]);
- }
+ gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
+ for (i = 0; i < nslices; i++) {
+ gpr_slice_ref(slices[i]);
+ gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slices[i]);
+ }
return bb;
}
-grpc_byte_buffer *
-grpc_raw_byte_buffer_from_reader (grpc_byte_buffer_reader * reader)
-{
- grpc_byte_buffer *bb = malloc (sizeof (grpc_byte_buffer));
+grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
+ grpc_byte_buffer_reader *reader) {
+ grpc_byte_buffer *bb = malloc(sizeof(grpc_byte_buffer));
gpr_slice slice;
bb->type = GRPC_BB_RAW;
bb->data.raw.compression = GRPC_COMPRESS_NONE;
- gpr_slice_buffer_init (&bb->data.raw.slice_buffer);
+ gpr_slice_buffer_init(&bb->data.raw.slice_buffer);
- while (grpc_byte_buffer_reader_next (reader, &slice))
- {
- gpr_slice_buffer_add (&bb->data.raw.slice_buffer, slice);
- }
+ while (grpc_byte_buffer_reader_next(reader, &slice)) {
+ gpr_slice_buffer_add(&bb->data.raw.slice_buffer, slice);
+ }
return bb;
}
-grpc_byte_buffer *
-grpc_byte_buffer_copy (grpc_byte_buffer * bb)
-{
- switch (bb->type)
- {
+grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
+ switch (bb->type) {
case GRPC_BB_RAW:
- return grpc_raw_byte_buffer_create (bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count);
- }
- gpr_log (GPR_INFO, "should never get here");
- abort ();
+ return grpc_raw_byte_buffer_create(bb->data.raw.slice_buffer.slices,
+ bb->data.raw.slice_buffer.count);
+ }
+ gpr_log(GPR_INFO, "should never get here");
+ abort();
return NULL;
}
-void
-grpc_byte_buffer_destroy (grpc_byte_buffer * bb)
-{
- if (!bb)
- return;
- switch (bb->type)
- {
+void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) {
+ if (!bb) return;
+ switch (bb->type) {
case GRPC_BB_RAW:
- gpr_slice_buffer_destroy (&bb->data.raw.slice_buffer);
+ gpr_slice_buffer_destroy(&bb->data.raw.slice_buffer);
break;
- }
- free (bb);
+ }
+ free(bb);
}
-size_t
-grpc_byte_buffer_length (grpc_byte_buffer * bb)
-{
- switch (bb->type)
- {
+size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) {
+ switch (bb->type) {
case GRPC_BB_RAW:
return bb->data.raw.slice_buffer.length;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
}
diff --git a/src/core/surface/byte_buffer_queue.c b/src/core/surface/byte_buffer_queue.c
index 543da6a000..e47dc4f4ce 100644
--- a/src/core/surface/byte_buffer_queue.c
+++ b/src/core/surface/byte_buffer_queue.c
@@ -35,86 +35,63 @@
#include <grpc/support/alloc.h>
#include <grpc/support/useful.h>
-static void
-bba_destroy (grpc_bbq_array * array, size_t start_pos)
-{
+static void bba_destroy(grpc_bbq_array *array, size_t start_pos) {
size_t i;
- for (i = start_pos; i < array->count; i++)
- {
- grpc_byte_buffer_destroy (array->data[i]);
- }
- gpr_free (array->data);
+ for (i = start_pos; i < array->count; i++) {
+ grpc_byte_buffer_destroy(array->data[i]);
+ }
+ gpr_free(array->data);
}
/* Append an operation to an array, expanding as needed */
-static void
-bba_push (grpc_bbq_array * a, grpc_byte_buffer * buffer)
-{
- if (a->count == a->capacity)
- {
- a->capacity = GPR_MAX (a->capacity * 2, 8);
- a->data = gpr_realloc (a->data, sizeof (grpc_byte_buffer *) * a->capacity);
- }
+static void bba_push(grpc_bbq_array *a, grpc_byte_buffer *buffer) {
+ if (a->count == a->capacity) {
+ a->capacity = GPR_MAX(a->capacity * 2, 8);
+ a->data = gpr_realloc(a->data, sizeof(grpc_byte_buffer *) * a->capacity);
+ }
a->data[a->count++] = buffer;
}
-void
-grpc_bbq_destroy (grpc_byte_buffer_queue * q)
-{
- bba_destroy (&q->filling, 0);
- bba_destroy (&q->draining, q->drain_pos);
+void grpc_bbq_destroy(grpc_byte_buffer_queue *q) {
+ bba_destroy(&q->filling, 0);
+ bba_destroy(&q->draining, q->drain_pos);
}
-int
-grpc_bbq_empty (grpc_byte_buffer_queue * q)
-{
+int grpc_bbq_empty(grpc_byte_buffer_queue *q) {
return (q->drain_pos == q->draining.count && q->filling.count == 0);
}
-void
-grpc_bbq_push (grpc_byte_buffer_queue * q, grpc_byte_buffer * buffer)
-{
- q->bytes += grpc_byte_buffer_length (buffer);
- bba_push (&q->filling, buffer);
+void grpc_bbq_push(grpc_byte_buffer_queue *q, grpc_byte_buffer *buffer) {
+ q->bytes += grpc_byte_buffer_length(buffer);
+ bba_push(&q->filling, buffer);
}
-void
-grpc_bbq_flush (grpc_byte_buffer_queue * q)
-{
+void grpc_bbq_flush(grpc_byte_buffer_queue *q) {
grpc_byte_buffer *bb;
- while ((bb = grpc_bbq_pop (q)))
- {
- grpc_byte_buffer_destroy (bb);
- }
+ while ((bb = grpc_bbq_pop(q))) {
+ grpc_byte_buffer_destroy(bb);
+ }
}
-size_t
-grpc_bbq_bytes (grpc_byte_buffer_queue * q)
-{
- return q->bytes;
-}
+size_t grpc_bbq_bytes(grpc_byte_buffer_queue *q) { return q->bytes; }
-grpc_byte_buffer *
-grpc_bbq_pop (grpc_byte_buffer_queue * q)
-{
+grpc_byte_buffer *grpc_bbq_pop(grpc_byte_buffer_queue *q) {
grpc_bbq_array temp_array;
grpc_byte_buffer *out;
- if (q->drain_pos == q->draining.count)
- {
- if (q->filling.count == 0)
- {
- return NULL;
- }
- q->draining.count = 0;
- q->drain_pos = 0;
- /* swap arrays */
- temp_array = q->filling;
- q->filling = q->draining;
- q->draining = temp_array;
+ if (q->drain_pos == q->draining.count) {
+ if (q->filling.count == 0) {
+ return NULL;
}
+ q->draining.count = 0;
+ q->drain_pos = 0;
+ /* swap arrays */
+ temp_array = q->filling;
+ q->filling = q->draining;
+ q->draining = temp_array;
+ }
out = q->draining.data[q->drain_pos++];
- q->bytes -= grpc_byte_buffer_length (out);
+ q->bytes -= grpc_byte_buffer_length(out);
return out;
}
diff --git a/src/core/surface/byte_buffer_queue.h b/src/core/surface/byte_buffer_queue.h
index 3e344c1e54..2c3b22d24e 100644
--- a/src/core/surface/byte_buffer_queue.h
+++ b/src/core/surface/byte_buffer_queue.h
@@ -38,27 +38,25 @@
/* TODO(ctiller): inline an element or two into this struct to avoid per-call
allocations */
-typedef struct
-{
+typedef struct {
grpc_byte_buffer **data;
size_t count;
size_t capacity;
} grpc_bbq_array;
/* should be initialized by zeroing memory */
-typedef struct
-{
+typedef struct {
size_t drain_pos;
grpc_bbq_array filling;
grpc_bbq_array draining;
size_t bytes;
} grpc_byte_buffer_queue;
-void grpc_bbq_destroy (grpc_byte_buffer_queue * q);
-grpc_byte_buffer *grpc_bbq_pop (grpc_byte_buffer_queue * q);
-void grpc_bbq_flush (grpc_byte_buffer_queue * q);
-int grpc_bbq_empty (grpc_byte_buffer_queue * q);
-void grpc_bbq_push (grpc_byte_buffer_queue * q, grpc_byte_buffer * bb);
-size_t grpc_bbq_bytes (grpc_byte_buffer_queue * q);
+void grpc_bbq_destroy(grpc_byte_buffer_queue *q);
+grpc_byte_buffer *grpc_bbq_pop(grpc_byte_buffer_queue *q);
+void grpc_bbq_flush(grpc_byte_buffer_queue *q);
+int grpc_bbq_empty(grpc_byte_buffer_queue *q);
+void grpc_bbq_push(grpc_byte_buffer_queue *q, grpc_byte_buffer *bb);
+size_t grpc_bbq_bytes(grpc_byte_buffer_queue *q);
#endif /* GRPC_INTERNAL_CORE_SURFACE_BYTE_BUFFER_QUEUE_H */
diff --git a/src/core/surface/byte_buffer_reader.c b/src/core/surface/byte_buffer_reader.c
index c4c9e43d7e..283db83833 100644
--- a/src/core/surface/byte_buffer_reader.c
+++ b/src/core/surface/byte_buffer_reader.c
@@ -42,77 +42,64 @@
#include "src/core/compression/message_compress.h"
-static int
-is_compressed (grpc_byte_buffer * buffer)
-{
- switch (buffer->type)
- {
+static int is_compressed(grpc_byte_buffer *buffer) {
+ switch (buffer->type) {
case GRPC_BB_RAW:
- if (buffer->data.raw.compression == GRPC_COMPRESS_NONE)
- {
- return 0 /* GPR_FALSE */ ;
- }
+ if (buffer->data.raw.compression == GRPC_COMPRESS_NONE) {
+ return 0 /* GPR_FALSE */;
+ }
break;
- }
- return 1 /* GPR_TRUE */ ;
+ }
+ return 1 /* GPR_TRUE */;
}
-void
-grpc_byte_buffer_reader_init (grpc_byte_buffer_reader * reader, grpc_byte_buffer * buffer)
-{
+void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader,
+ grpc_byte_buffer *buffer) {
gpr_slice_buffer decompressed_slices_buffer;
reader->buffer_in = buffer;
- switch (reader->buffer_in->type)
- {
+ switch (reader->buffer_in->type) {
case GRPC_BB_RAW:
- gpr_slice_buffer_init (&decompressed_slices_buffer);
- if (is_compressed (reader->buffer_in))
- {
- grpc_msg_decompress (reader->buffer_in->data.raw.compression, &reader->buffer_in->data.raw.slice_buffer, &decompressed_slices_buffer);
- reader->buffer_out = grpc_raw_byte_buffer_create (decompressed_slices_buffer.slices, decompressed_slices_buffer.count);
- gpr_slice_buffer_destroy (&decompressed_slices_buffer);
- }
- else
- { /* not compressed, use the input buffer as output */
- reader->buffer_out = reader->buffer_in;
- }
+ gpr_slice_buffer_init(&decompressed_slices_buffer);
+ if (is_compressed(reader->buffer_in)) {
+ grpc_msg_decompress(reader->buffer_in->data.raw.compression,
+ &reader->buffer_in->data.raw.slice_buffer,
+ &decompressed_slices_buffer);
+ reader->buffer_out =
+ grpc_raw_byte_buffer_create(decompressed_slices_buffer.slices,
+ decompressed_slices_buffer.count);
+ gpr_slice_buffer_destroy(&decompressed_slices_buffer);
+ } else { /* not compressed, use the input buffer as output */
+ reader->buffer_out = reader->buffer_in;
+ }
reader->current.index = 0;
break;
- }
+ }
}
-void
-grpc_byte_buffer_reader_destroy (grpc_byte_buffer_reader * reader)
-{
- switch (reader->buffer_in->type)
- {
+void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader) {
+ switch (reader->buffer_in->type) {
case GRPC_BB_RAW:
/* keeping the same if-else structure as in the init function */
- if (is_compressed (reader->buffer_in))
- {
- grpc_byte_buffer_destroy (reader->buffer_out);
- }
+ if (is_compressed(reader->buffer_in)) {
+ grpc_byte_buffer_destroy(reader->buffer_out);
+ }
break;
- }
+ }
}
-int
-grpc_byte_buffer_reader_next (grpc_byte_buffer_reader * reader, gpr_slice * slice)
-{
- switch (reader->buffer_in->type)
- {
- case GRPC_BB_RAW:
- {
- gpr_slice_buffer *slice_buffer;
- slice_buffer = &reader->buffer_out->data.raw.slice_buffer;
- if (reader->current.index < slice_buffer->count)
- {
- *slice = gpr_slice_ref (slice_buffer->slices[reader->current.index]);
- reader->current.index += 1;
- return 1;
- }
- break;
+int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
+ gpr_slice *slice) {
+ switch (reader->buffer_in->type) {
+ case GRPC_BB_RAW: {
+ gpr_slice_buffer *slice_buffer;
+ slice_buffer = &reader->buffer_out->data.raw.slice_buffer;
+ if (reader->current.index < slice_buffer->count) {
+ *slice = gpr_slice_ref(slice_buffer->slices[reader->current.index]);
+ reader->current.index += 1;
+ return 1;
}
+ break;
}
+ }
return 0;
}
diff --git a/src/core/surface/call.c b/src/core/surface/call.c
index 418c75efd2..d97bf53206 100644
--- a/src/core/surface/call.c
+++ b/src/core/surface/call.c
@@ -61,8 +61,7 @@
- status/close recv (depending on client/server) */
#define MAX_CONCURRENT_COMPLETIONS 6
-typedef struct
-{
+typedef struct {
grpc_ioreq_completion_func on_complete;
void *user_data;
int success;
@@ -74,8 +73,7 @@ typedef struct
#define MAX_SEND_INITIAL_METADATA_COUNT 3
-typedef struct
-{
+typedef struct {
/* Overall status of the operation: starts OK, may degrade to
non-OK */
gpr_uint8 success;
@@ -92,8 +90,7 @@ typedef struct
enumerates them all, and acts as a priority sorting for which
status to return to the application - earlier entries override
later ones */
-typedef enum
-{
+typedef enum {
/* Status came from the application layer overriding whatever
the wire says */
STATUS_FROM_API_OVERRIDE = 0,
@@ -107,16 +104,14 @@ typedef enum
STATUS_SOURCE_COUNT
} status_source;
-typedef struct
-{
+typedef struct {
gpr_uint8 is_set;
grpc_status_code code;
grpc_mdstr *details;
} received_status;
/* How far through the GRPC stream have we read? */
-typedef enum
-{
+typedef enum {
/* We are still waiting for initial metadata to complete */
READ_STATE_INITIAL = 0,
/* We have gotten initial metadata, and are reading either
@@ -128,15 +123,13 @@ typedef enum
READ_STATE_STREAM_CLOSED
} read_state;
-typedef enum
-{
+typedef enum {
WRITE_STATE_INITIAL = 0,
WRITE_STATE_STARTED,
WRITE_STATE_WRITE_CLOSED
} write_state;
-struct grpc_call
-{
+struct grpc_call {
grpc_completion_queue *cq;
grpc_channel *channel;
grpc_call *parent;
@@ -283,363 +276,328 @@ struct grpc_call
#define CALL_FROM_TOP_ELEM(top_elem) \
CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem))
-static void set_deadline_alarm (grpc_exec_ctx * exec_ctx, grpc_call * call, gpr_timespec deadline);
-static void call_on_done_recv (grpc_exec_ctx * exec_ctx, void *call, int success);
-static void call_on_done_send (grpc_exec_ctx * exec_ctx, void *call, int success);
-static int fill_send_ops (grpc_call * call, grpc_transport_stream_op * op);
-static void execute_op (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_transport_stream_op * op);
-static void recv_metadata (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_metadata_batch * metadata);
-static void finish_read_ops (grpc_call * call);
-static grpc_call_error cancel_with_status (grpc_call * c, grpc_status_code status, const char *description);
-static void finished_loose_op (grpc_exec_ctx * exec_ctx, void *call, int success);
-
-static void lock (grpc_call * call);
-static void unlock (grpc_exec_ctx * exec_ctx, grpc_call * call);
-
-grpc_call *
-grpc_call_create (grpc_channel * channel, grpc_call * parent_call, gpr_uint32 propagation_mask, grpc_completion_queue * cq, const void *server_transport_data, grpc_mdelem ** add_initial_metadata, size_t add_initial_metadata_count, gpr_timespec send_deadline)
-{
+static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ gpr_timespec deadline);
+static void call_on_done_recv(grpc_exec_ctx *exec_ctx, void *call, int success);
+static void call_on_done_send(grpc_exec_ctx *exec_ctx, void *call, int success);
+static int fill_send_ops(grpc_call *call, grpc_transport_stream_op *op);
+static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ grpc_transport_stream_op *op);
+static void recv_metadata(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ grpc_metadata_batch *metadata);
+static void finish_read_ops(grpc_call *call);
+static grpc_call_error cancel_with_status(grpc_call *c, grpc_status_code status,
+ const char *description);
+static void finished_loose_op(grpc_exec_ctx *exec_ctx, void *call, int success);
+
+static void lock(grpc_call *call);
+static void unlock(grpc_exec_ctx *exec_ctx, grpc_call *call);
+
+grpc_call *grpc_call_create(grpc_channel *channel, grpc_call *parent_call,
+ gpr_uint32 propagation_mask,
+ grpc_completion_queue *cq,
+ const void *server_transport_data,
+ grpc_mdelem **add_initial_metadata,
+ size_t add_initial_metadata_count,
+ gpr_timespec send_deadline) {
size_t i;
grpc_transport_stream_op initial_op;
grpc_transport_stream_op *initial_op_ptr = NULL;
- grpc_channel_stack *channel_stack = grpc_channel_get_channel_stack (channel);
+ grpc_channel_stack *channel_stack = grpc_channel_get_channel_stack(channel);
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_call *call = gpr_malloc (sizeof (grpc_call) + channel_stack->call_stack_size);
- memset (call, 0, sizeof (grpc_call));
- gpr_mu_init (&call->mu);
- gpr_mu_init (&call->completion_mu);
+ grpc_call *call =
+ gpr_malloc(sizeof(grpc_call) + channel_stack->call_stack_size);
+ memset(call, 0, sizeof(grpc_call));
+ gpr_mu_init(&call->mu);
+ gpr_mu_init(&call->completion_mu);
call->channel = channel;
call->cq = cq;
- if (cq != NULL)
- {
- GRPC_CQ_INTERNAL_REF (cq, "bind");
- }
+ if (cq != NULL) {
+ GRPC_CQ_INTERNAL_REF(cq, "bind");
+ }
call->parent = parent_call;
call->is_client = server_transport_data == NULL;
- for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++)
- {
- call->request_set[i] = REQSET_EMPTY;
- }
- if (call->is_client)
- {
- call->request_set[GRPC_IOREQ_SEND_TRAILING_METADATA] = REQSET_DONE;
- call->request_set[GRPC_IOREQ_SEND_STATUS] = REQSET_DONE;
- }
- GPR_ASSERT (add_initial_metadata_count < MAX_SEND_INITIAL_METADATA_COUNT);
- for (i = 0; i < add_initial_metadata_count; i++)
- {
- call->send_initial_metadata[i].md = add_initial_metadata[i];
- }
+ for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++) {
+ call->request_set[i] = REQSET_EMPTY;
+ }
+ if (call->is_client) {
+ call->request_set[GRPC_IOREQ_SEND_TRAILING_METADATA] = REQSET_DONE;
+ call->request_set[GRPC_IOREQ_SEND_STATUS] = REQSET_DONE;
+ }
+ GPR_ASSERT(add_initial_metadata_count < MAX_SEND_INITIAL_METADATA_COUNT);
+ for (i = 0; i < add_initial_metadata_count; i++) {
+ call->send_initial_metadata[i].md = add_initial_metadata[i];
+ }
call->send_initial_metadata_count = add_initial_metadata_count;
call->send_deadline = send_deadline;
- GRPC_CHANNEL_INTERNAL_REF (channel, "call");
- call->metadata_context = grpc_channel_get_metadata_context (channel);
- grpc_sopb_init (&call->send_ops);
- grpc_sopb_init (&call->recv_ops);
- gpr_slice_buffer_init (&call->incoming_message);
- grpc_closure_init (&call->on_done_recv, call_on_done_recv, call);
- grpc_closure_init (&call->on_done_send, call_on_done_send, call);
- grpc_closure_init (&call->on_done_bind, finished_loose_op, call);
+ GRPC_CHANNEL_INTERNAL_REF(channel, "call");
+ call->metadata_context = grpc_channel_get_metadata_context(channel);
+ grpc_sopb_init(&call->send_ops);
+ grpc_sopb_init(&call->recv_ops);
+ gpr_slice_buffer_init(&call->incoming_message);
+ grpc_closure_init(&call->on_done_recv, call_on_done_recv, call);
+ grpc_closure_init(&call->on_done_send, call_on_done_send, call);
+ grpc_closure_init(&call->on_done_bind, finished_loose_op, call);
/* dropped in destroy and when READ_STATE_STREAM_CLOSED received */
- gpr_ref_init (&call->internal_refcount, 2);
+ gpr_ref_init(&call->internal_refcount, 2);
/* server hack: start reads immediately so we can get initial metadata.
TODO(ctiller): figure out a cleaner solution */
- if (!call->is_client)
- {
- memset (&initial_op, 0, sizeof (initial_op));
- initial_op.recv_ops = &call->recv_ops;
- initial_op.recv_state = &call->recv_state;
- initial_op.on_done_recv = &call->on_done_recv;
- initial_op.context = call->context;
- call->receiving = 1;
- GRPC_CALL_INTERNAL_REF (call, "receiving");
- initial_op_ptr = &initial_op;
- }
- grpc_call_stack_init (&exec_ctx, channel_stack, server_transport_data, initial_op_ptr, CALL_STACK_FROM_CALL (call));
- if (parent_call != NULL)
- {
- GRPC_CALL_INTERNAL_REF (parent_call, "child");
- GPR_ASSERT (call->is_client);
- GPR_ASSERT (!parent_call->is_client);
-
- gpr_mu_lock (&parent_call->mu);
-
- if (propagation_mask & GRPC_PROPAGATE_DEADLINE)
- {
- send_deadline = gpr_time_min (gpr_convert_clock_type (send_deadline, parent_call->send_deadline.clock_type), parent_call->send_deadline);
- }
- /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with
- * GRPC_PROPAGATE_STATS_CONTEXT */
- /* TODO(ctiller): This should change to use the appropriate census start_op
- * call. */
- if (propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT)
- {
- GPR_ASSERT (propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
- grpc_call_context_set (call, GRPC_CONTEXT_TRACING, parent_call->context[GRPC_CONTEXT_TRACING].value, NULL);
- }
- else
- {
- GPR_ASSERT (propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
- }
- if (propagation_mask & GRPC_PROPAGATE_CANCELLATION)
- {
- call->cancellation_is_inherited = 1;
- }
-
- if (parent_call->first_child == NULL)
- {
- parent_call->first_child = call;
- call->sibling_next = call->sibling_prev = call;
- }
- else
- {
- call->sibling_next = parent_call->first_child;
- call->sibling_prev = parent_call->first_child->sibling_prev;
- call->sibling_next->sibling_prev = call->sibling_prev->sibling_next = call;
- }
-
- gpr_mu_unlock (&parent_call->mu);
- }
- if (gpr_time_cmp (send_deadline, gpr_inf_future (send_deadline.clock_type)) != 0)
- {
- set_deadline_alarm (&exec_ctx, call, send_deadline);
- }
- grpc_exec_ctx_finish (&exec_ctx);
+ if (!call->is_client) {
+ memset(&initial_op, 0, sizeof(initial_op));
+ initial_op.recv_ops = &call->recv_ops;
+ initial_op.recv_state = &call->recv_state;
+ initial_op.on_done_recv = &call->on_done_recv;
+ initial_op.context = call->context;
+ call->receiving = 1;
+ GRPC_CALL_INTERNAL_REF(call, "receiving");
+ initial_op_ptr = &initial_op;
+ }
+ grpc_call_stack_init(&exec_ctx, channel_stack, server_transport_data,
+ initial_op_ptr, CALL_STACK_FROM_CALL(call));
+ if (parent_call != NULL) {
+ GRPC_CALL_INTERNAL_REF(parent_call, "child");
+ GPR_ASSERT(call->is_client);
+ GPR_ASSERT(!parent_call->is_client);
+
+ gpr_mu_lock(&parent_call->mu);
+
+ if (propagation_mask & GRPC_PROPAGATE_DEADLINE) {
+ send_deadline = gpr_time_min(
+ gpr_convert_clock_type(send_deadline,
+ parent_call->send_deadline.clock_type),
+ parent_call->send_deadline);
+ }
+ /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with
+ * GRPC_PROPAGATE_STATS_CONTEXT */
+ /* TODO(ctiller): This should change to use the appropriate census start_op
+ * call. */
+ if (propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT) {
+ GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
+ grpc_call_context_set(call, GRPC_CONTEXT_TRACING,
+ parent_call->context[GRPC_CONTEXT_TRACING].value,
+ NULL);
+ } else {
+ GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
+ }
+ if (propagation_mask & GRPC_PROPAGATE_CANCELLATION) {
+ call->cancellation_is_inherited = 1;
+ }
+
+ if (parent_call->first_child == NULL) {
+ parent_call->first_child = call;
+ call->sibling_next = call->sibling_prev = call;
+ } else {
+ call->sibling_next = parent_call->first_child;
+ call->sibling_prev = parent_call->first_child->sibling_prev;
+ call->sibling_next->sibling_prev = call->sibling_prev->sibling_next =
+ call;
+ }
+
+ gpr_mu_unlock(&parent_call->mu);
+ }
+ if (gpr_time_cmp(send_deadline, gpr_inf_future(send_deadline.clock_type)) !=
+ 0) {
+ set_deadline_alarm(&exec_ctx, call, send_deadline);
+ }
+ grpc_exec_ctx_finish(&exec_ctx);
return call;
}
-void
-grpc_call_set_completion_queue (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_completion_queue * cq)
-{
- lock (call);
+void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ grpc_completion_queue *cq) {
+ lock(call);
call->cq = cq;
- if (cq)
- {
- GRPC_CQ_INTERNAL_REF (cq, "bind");
- }
- unlock (exec_ctx, call);
+ if (cq) {
+ GRPC_CQ_INTERNAL_REF(cq, "bind");
+ }
+ unlock(exec_ctx, call);
}
-grpc_completion_queue *
-grpc_call_get_completion_queue (grpc_call * call)
-{
+grpc_completion_queue *grpc_call_get_completion_queue(grpc_call *call) {
return call->cq;
}
-static grpc_cq_completion *
-allocate_completion (grpc_call * call)
-{
+static grpc_cq_completion *allocate_completion(grpc_call *call) {
gpr_uint8 i;
- gpr_mu_lock (&call->completion_mu);
- for (i = 0; i < GPR_ARRAY_SIZE (call->completions); i++)
- {
- if (call->allocated_completions & (1u << i))
- {
- continue;
- }
- call->allocated_completions |= (gpr_uint8) (1u << i);
- gpr_mu_unlock (&call->completion_mu);
- return &call->completions[i];
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ gpr_mu_lock(&call->completion_mu);
+ for (i = 0; i < GPR_ARRAY_SIZE(call->completions); i++) {
+ if (call->allocated_completions & (1u << i)) {
+ continue;
+ }
+ call->allocated_completions |= (gpr_uint8)(1u << i);
+ gpr_mu_unlock(&call->completion_mu);
+ return &call->completions[i];
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
}
-static void
-done_completion (grpc_exec_ctx * exec_ctx, void *call, grpc_cq_completion * completion)
-{
+static void done_completion(grpc_exec_ctx *exec_ctx, void *call,
+ grpc_cq_completion *completion) {
grpc_call *c = call;
- gpr_mu_lock (&c->completion_mu);
- c->allocated_completions &= (gpr_uint8) ~ (1u << (completion - c->completions));
- gpr_mu_unlock (&c->completion_mu);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, c, "completion");
+ gpr_mu_lock(&c->completion_mu);
+ c->allocated_completions &=
+ (gpr_uint8) ~(1u << (completion - c->completions));
+ gpr_mu_unlock(&c->completion_mu);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, c, "completion");
}
#ifdef GRPC_CALL_REF_COUNT_DEBUG
-void
-grpc_call_internal_ref (grpc_call * c, const char *reason)
-{
- gpr_log (GPR_DEBUG, "CALL: ref %p %d -> %d [%s]", c, c->internal_refcount.count, c->internal_refcount.count + 1, reason);
+void grpc_call_internal_ref(grpc_call *c, const char *reason) {
+ gpr_log(GPR_DEBUG, "CALL: ref %p %d -> %d [%s]", c,
+ c->internal_refcount.count, c->internal_refcount.count + 1, reason);
#else
-void
-grpc_call_internal_ref (grpc_call * c)
-{
+void grpc_call_internal_ref(grpc_call *c) {
#endif
- gpr_ref (&c->internal_refcount);
+ gpr_ref(&c->internal_refcount);
}
-static void
-destroy_call (grpc_exec_ctx * exec_ctx, grpc_call * call)
-{
+static void destroy_call(grpc_exec_ctx *exec_ctx, grpc_call *call) {
size_t i;
grpc_call *c = call;
- grpc_call_stack_destroy (exec_ctx, CALL_STACK_FROM_CALL (c));
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, c->channel, "call");
- gpr_mu_destroy (&c->mu);
- gpr_mu_destroy (&c->completion_mu);
- for (i = 0; i < STATUS_SOURCE_COUNT; i++)
- {
- if (c->status[i].details)
- {
- GRPC_MDSTR_UNREF (c->status[i].details);
- }
- }
- for (i = 0; i < c->owned_metadata_count; i++)
- {
- GRPC_MDELEM_UNREF (c->owned_metadata[i]);
- }
- gpr_free (c->owned_metadata);
- for (i = 0; i < GPR_ARRAY_SIZE (c->buffered_metadata); i++)
- {
- gpr_free (c->buffered_metadata[i].metadata);
- }
- for (i = 0; i < c->send_initial_metadata_count; i++)
- {
- GRPC_MDELEM_UNREF (c->send_initial_metadata[i].md);
- }
- for (i = 0; i < GRPC_CONTEXT_COUNT; i++)
- {
- if (c->context[i].destroy)
- {
- c->context[i].destroy (c->context[i].value);
- }
- }
- grpc_sopb_destroy (&c->send_ops);
- grpc_sopb_destroy (&c->recv_ops);
- grpc_bbq_destroy (&c->incoming_queue);
- gpr_slice_buffer_destroy (&c->incoming_message);
- if (c->cq)
- {
- GRPC_CQ_INTERNAL_UNREF (c->cq, "bind");
- }
- gpr_free (c);
+ grpc_call_stack_destroy(exec_ctx, CALL_STACK_FROM_CALL(c));
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, c->channel, "call");
+ gpr_mu_destroy(&c->mu);
+ gpr_mu_destroy(&c->completion_mu);
+ for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
+ if (c->status[i].details) {
+ GRPC_MDSTR_UNREF(c->status[i].details);
+ }
+ }
+ for (i = 0; i < c->owned_metadata_count; i++) {
+ GRPC_MDELEM_UNREF(c->owned_metadata[i]);
+ }
+ gpr_free(c->owned_metadata);
+ for (i = 0; i < GPR_ARRAY_SIZE(c->buffered_metadata); i++) {
+ gpr_free(c->buffered_metadata[i].metadata);
+ }
+ for (i = 0; i < c->send_initial_metadata_count; i++) {
+ GRPC_MDELEM_UNREF(c->send_initial_metadata[i].md);
+ }
+ for (i = 0; i < GRPC_CONTEXT_COUNT; i++) {
+ if (c->context[i].destroy) {
+ c->context[i].destroy(c->context[i].value);
+ }
+ }
+ grpc_sopb_destroy(&c->send_ops);
+ grpc_sopb_destroy(&c->recv_ops);
+ grpc_bbq_destroy(&c->incoming_queue);
+ gpr_slice_buffer_destroy(&c->incoming_message);
+ if (c->cq) {
+ GRPC_CQ_INTERNAL_UNREF(c->cq, "bind");
+ }
+ gpr_free(c);
}
#ifdef GRPC_CALL_REF_COUNT_DEBUG
-void
-grpc_call_internal_unref (grpc_exec_ctx * exec_ctx, grpc_call * c, const char *reason)
-{
- gpr_log (GPR_DEBUG, "CALL: unref %p %d -> %d [%s]", c, c->internal_refcount.count, c->internal_refcount.count - 1, reason);
+void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c,
+ const char *reason) {
+ gpr_log(GPR_DEBUG, "CALL: unref %p %d -> %d [%s]", c,
+ c->internal_refcount.count, c->internal_refcount.count - 1, reason);
#else
-void
-grpc_call_internal_unref (grpc_exec_ctx * exec_ctx, grpc_call * c)
-{
+void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c) {
#endif
- if (gpr_unref (&c->internal_refcount))
- {
- destroy_call (exec_ctx, c);
- }
+ if (gpr_unref(&c->internal_refcount)) {
+ destroy_call(exec_ctx, c);
+ }
}
-static void
-set_status_code (grpc_call * call, status_source source, gpr_uint32 status)
-{
- if (call->status[source].is_set)
- return;
+static void set_status_code(grpc_call *call, status_source source,
+ gpr_uint32 status) {
+ if (call->status[source].is_set) return;
call->status[source].is_set = 1;
- call->status[source].code = (grpc_status_code) status;
+ call->status[source].code = (grpc_status_code)status;
call->error_status_set = status != GRPC_STATUS_OK;
- if (status != GRPC_STATUS_OK && !grpc_bbq_empty (&call->incoming_queue))
- {
- grpc_bbq_flush (&call->incoming_queue);
- }
+ if (status != GRPC_STATUS_OK && !grpc_bbq_empty(&call->incoming_queue)) {
+ grpc_bbq_flush(&call->incoming_queue);
+ }
}
-static void
-set_compression_algorithm (grpc_call * call, grpc_compression_algorithm algo)
-{
+static void set_compression_algorithm(grpc_call *call,
+ grpc_compression_algorithm algo) {
call->compression_algorithm = algo;
}
-grpc_compression_algorithm
-grpc_call_get_compression_algorithm (const grpc_call * call)
-{
+grpc_compression_algorithm grpc_call_get_compression_algorithm(
+ const grpc_call *call) {
return call->compression_algorithm;
}
-static void
-set_encodings_accepted_by_peer (grpc_call * call, const gpr_slice accept_encoding_slice)
-{
+static void set_encodings_accepted_by_peer(
+ grpc_call *call, const gpr_slice accept_encoding_slice) {
size_t i;
grpc_compression_algorithm algorithm;
gpr_slice_buffer accept_encoding_parts;
- gpr_slice_buffer_init (&accept_encoding_parts);
- gpr_slice_split (accept_encoding_slice, ",", &accept_encoding_parts);
+ gpr_slice_buffer_init(&accept_encoding_parts);
+ gpr_slice_split(accept_encoding_slice, ",", &accept_encoding_parts);
/* No need to zero call->encodings_accepted_by_peer: grpc_call_create already
* zeroes the whole grpc_call */
/* Always support no compression */
- GPR_BITSET (&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
- for (i = 0; i < accept_encoding_parts.count; i++)
- {
- const gpr_slice *accept_encoding_entry_slice = &accept_encoding_parts.slices[i];
- if (grpc_compression_algorithm_parse ((const char *) GPR_SLICE_START_PTR (*accept_encoding_entry_slice), GPR_SLICE_LENGTH (*accept_encoding_entry_slice), &algorithm))
- {
- GPR_BITSET (&call->encodings_accepted_by_peer, algorithm);
- }
- else
- {
- char *accept_encoding_entry_str = gpr_dump_slice (*accept_encoding_entry_slice, GPR_DUMP_ASCII);
- gpr_log (GPR_ERROR, "Invalid entry in accept encoding metadata: '%s'. Ignoring.", accept_encoding_entry_str);
- gpr_free (accept_encoding_entry_str);
- }
- }
+ GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
+ for (i = 0; i < accept_encoding_parts.count; i++) {
+ const gpr_slice *accept_encoding_entry_slice =
+ &accept_encoding_parts.slices[i];
+ if (grpc_compression_algorithm_parse(
+ (const char *)GPR_SLICE_START_PTR(*accept_encoding_entry_slice),
+ GPR_SLICE_LENGTH(*accept_encoding_entry_slice), &algorithm)) {
+ GPR_BITSET(&call->encodings_accepted_by_peer, algorithm);
+ } else {
+ char *accept_encoding_entry_str =
+ gpr_dump_slice(*accept_encoding_entry_slice, GPR_DUMP_ASCII);
+ gpr_log(GPR_ERROR,
+ "Invalid entry in accept encoding metadata: '%s'. Ignoring.",
+ accept_encoding_entry_str);
+ gpr_free(accept_encoding_entry_str);
+ }
+ }
}
-gpr_uint32
-grpc_call_get_encodings_accepted_by_peer (grpc_call * call)
-{
+gpr_uint32 grpc_call_get_encodings_accepted_by_peer(grpc_call *call) {
return call->encodings_accepted_by_peer;
}
-gpr_uint32
-grpc_call_get_message_flags (const grpc_call * call)
-{
+gpr_uint32 grpc_call_get_message_flags(const grpc_call *call) {
return call->incoming_message_flags;
}
-static void
-set_status_details (grpc_call * call, status_source source, grpc_mdstr * status)
-{
- if (call->status[source].details != NULL)
- {
- GRPC_MDSTR_UNREF (call->status[source].details);
- }
+static void set_status_details(grpc_call *call, status_source source,
+ grpc_mdstr *status) {
+ if (call->status[source].details != NULL) {
+ GRPC_MDSTR_UNREF(call->status[source].details);
+ }
call->status[source].details = status;
}
-static int
-is_op_live (grpc_call * call, grpc_ioreq_op op)
-{
+static int is_op_live(grpc_call *call, grpc_ioreq_op op) {
gpr_uint8 set = call->request_set[op];
reqinfo_master *master;
- if (set >= GRPC_IOREQ_OP_COUNT)
- return 0;
+ if (set >= GRPC_IOREQ_OP_COUNT) return 0;
master = &call->masters[set];
return (master->complete_mask & (1u << op)) == 0;
}
-static void
-lock (grpc_call * call)
-{
- gpr_mu_lock (&call->mu);
-}
+static void lock(grpc_call *call) { gpr_mu_lock(&call->mu); }
-static int
-need_more_data (grpc_call * call)
-{
- if (call->read_state == READ_STATE_STREAM_CLOSED)
- return 0;
+static int need_more_data(grpc_call *call) {
+ if (call->read_state == READ_STATE_STREAM_CLOSED) return 0;
/* TODO(ctiller): this needs some serious cleanup */
- return is_op_live (call, GRPC_IOREQ_RECV_INITIAL_METADATA) || (is_op_live (call, GRPC_IOREQ_RECV_MESSAGE) && grpc_bbq_empty (&call->incoming_queue)) || is_op_live (call, GRPC_IOREQ_RECV_TRAILING_METADATA) || is_op_live (call, GRPC_IOREQ_RECV_STATUS) || is_op_live (call, GRPC_IOREQ_RECV_STATUS_DETAILS) || (is_op_live (call, GRPC_IOREQ_RECV_CLOSE) && grpc_bbq_empty (&call->incoming_queue)) || (call->write_state == WRITE_STATE_INITIAL && !call->is_client) || (call->cancel_with_status != GRPC_STATUS_OK) || call->destroy_called;
+ return is_op_live(call, GRPC_IOREQ_RECV_INITIAL_METADATA) ||
+ (is_op_live(call, GRPC_IOREQ_RECV_MESSAGE) &&
+ grpc_bbq_empty(&call->incoming_queue)) ||
+ is_op_live(call, GRPC_IOREQ_RECV_TRAILING_METADATA) ||
+ is_op_live(call, GRPC_IOREQ_RECV_STATUS) ||
+ is_op_live(call, GRPC_IOREQ_RECV_STATUS_DETAILS) ||
+ (is_op_live(call, GRPC_IOREQ_RECV_CLOSE) &&
+ grpc_bbq_empty(&call->incoming_queue)) ||
+ (call->write_state == WRITE_STATE_INITIAL && !call->is_client) ||
+ (call->cancel_with_status != GRPC_STATUS_OK) || call->destroy_called;
}
-static void
-unlock (grpc_exec_ctx * exec_ctx, grpc_call * call)
-{
+static void unlock(grpc_exec_ctx *exec_ctx, grpc_call *call) {
grpc_transport_stream_op op;
completed_request completed_requests[GRPC_IOREQ_OP_COUNT];
int completing_requests = 0;
@@ -648,677 +606,607 @@ unlock (grpc_exec_ctx * exec_ctx, grpc_call * call)
const size_t MAX_RECV_PEEK_AHEAD = 65536;
size_t buffered_bytes;
- memset (&op, 0, sizeof (op));
+ memset(&op, 0, sizeof(op));
op.cancel_with_status = call->cancel_with_status;
start_op = op.cancel_with_status != GRPC_STATUS_OK;
- call->cancel_with_status = GRPC_STATUS_OK; /* reset */
-
- if (!call->receiving && need_more_data (call))
- {
- if (grpc_bbq_empty (&call->incoming_queue) && call->reading_message)
- {
- op.max_recv_bytes = call->incoming_message_length - call->incoming_message.length + MAX_RECV_PEEK_AHEAD;
- }
- else
- {
- buffered_bytes = grpc_bbq_bytes (&call->incoming_queue);
- if (buffered_bytes > MAX_RECV_PEEK_AHEAD)
- {
- op.max_recv_bytes = 0;
- }
- else
- {
- op.max_recv_bytes = MAX_RECV_PEEK_AHEAD - buffered_bytes;
- }
- }
- /* TODO(ctiller): 1024 is basically to cover a bug
- I don't understand yet */
- if (op.max_recv_bytes > 1024)
- {
- op.recv_ops = &call->recv_ops;
- op.recv_state = &call->recv_state;
- op.on_done_recv = &call->on_done_recv;
- call->receiving = 1;
- GRPC_CALL_INTERNAL_REF (call, "receiving");
- start_op = 1;
- }
- }
-
- if (!call->sending)
- {
- if (fill_send_ops (call, &op))
- {
- call->sending = 1;
- GRPC_CALL_INTERNAL_REF (call, "sending");
- start_op = 1;
- }
- }
-
- if (!call->bound_pollset && call->cq && (!call->is_client || start_op))
- {
- call->bound_pollset = 1;
- op.bind_pollset = grpc_cq_pollset (call->cq);
+ call->cancel_with_status = GRPC_STATUS_OK; /* reset */
+
+ if (!call->receiving && need_more_data(call)) {
+ if (grpc_bbq_empty(&call->incoming_queue) && call->reading_message) {
+ op.max_recv_bytes = call->incoming_message_length -
+ call->incoming_message.length + MAX_RECV_PEEK_AHEAD;
+ } else {
+ buffered_bytes = grpc_bbq_bytes(&call->incoming_queue);
+ if (buffered_bytes > MAX_RECV_PEEK_AHEAD) {
+ op.max_recv_bytes = 0;
+ } else {
+ op.max_recv_bytes = MAX_RECV_PEEK_AHEAD - buffered_bytes;
+ }
+ }
+ /* TODO(ctiller): 1024 is basically to cover a bug
+ I don't understand yet */
+ if (op.max_recv_bytes > 1024) {
+ op.recv_ops = &call->recv_ops;
+ op.recv_state = &call->recv_state;
+ op.on_done_recv = &call->on_done_recv;
+ call->receiving = 1;
+ GRPC_CALL_INTERNAL_REF(call, "receiving");
start_op = 1;
}
+ }
- if (!call->completing && call->num_completed_requests != 0)
- {
- completing_requests = call->num_completed_requests;
- memcpy (completed_requests, call->completed_requests, sizeof (completed_requests));
- call->num_completed_requests = 0;
- call->completing = 1;
- GRPC_CALL_INTERNAL_REF (call, "completing");
- }
-
- gpr_mu_unlock (&call->mu);
-
- if (start_op)
- {
- execute_op (exec_ctx, call, &op);
- }
-
- if (completing_requests > 0)
- {
- for (i = 0; i < completing_requests; i++)
- {
- completed_requests[i].on_complete (exec_ctx, call, completed_requests[i].success, completed_requests[i].user_data);
- }
- lock (call);
- call->completing = 0;
- unlock (exec_ctx, call);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "completing");
+ if (!call->sending) {
+ if (fill_send_ops(call, &op)) {
+ call->sending = 1;
+ GRPC_CALL_INTERNAL_REF(call, "sending");
+ start_op = 1;
}
+ }
+
+ if (!call->bound_pollset && call->cq && (!call->is_client || start_op)) {
+ call->bound_pollset = 1;
+ op.bind_pollset = grpc_cq_pollset(call->cq);
+ start_op = 1;
+ }
+
+ if (!call->completing && call->num_completed_requests != 0) {
+ completing_requests = call->num_completed_requests;
+ memcpy(completed_requests, call->completed_requests,
+ sizeof(completed_requests));
+ call->num_completed_requests = 0;
+ call->completing = 1;
+ GRPC_CALL_INTERNAL_REF(call, "completing");
+ }
+
+ gpr_mu_unlock(&call->mu);
+
+ if (start_op) {
+ execute_op(exec_ctx, call, &op);
+ }
+
+ if (completing_requests > 0) {
+ for (i = 0; i < completing_requests; i++) {
+ completed_requests[i].on_complete(exec_ctx, call,
+ completed_requests[i].success,
+ completed_requests[i].user_data);
+ }
+ lock(call);
+ call->completing = 0;
+ unlock(exec_ctx, call);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completing");
+ }
}
-static void
-get_final_status (grpc_call * call, grpc_ioreq_data out)
-{
+static void get_final_status(grpc_call *call, grpc_ioreq_data out) {
int i;
- for (i = 0; i < STATUS_SOURCE_COUNT; i++)
- {
- if (call->status[i].is_set)
- {
- out.recv_status.set_value (call->status[i].code, out.recv_status.user_data);
- return;
- }
- }
- if (call->is_client)
- {
- out.recv_status.set_value (GRPC_STATUS_UNKNOWN, out.recv_status.user_data);
- }
- else
- {
- out.recv_status.set_value (GRPC_STATUS_OK, out.recv_status.user_data);
+ for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
+ if (call->status[i].is_set) {
+ out.recv_status.set_value(call->status[i].code,
+ out.recv_status.user_data);
+ return;
}
+ }
+ if (call->is_client) {
+ out.recv_status.set_value(GRPC_STATUS_UNKNOWN, out.recv_status.user_data);
+ } else {
+ out.recv_status.set_value(GRPC_STATUS_OK, out.recv_status.user_data);
+ }
}
-static void
-get_final_details (grpc_call * call, grpc_ioreq_data out)
-{
+static void get_final_details(grpc_call *call, grpc_ioreq_data out) {
int i;
- for (i = 0; i < STATUS_SOURCE_COUNT; i++)
- {
- if (call->status[i].is_set)
- {
- if (call->status[i].details)
- {
- gpr_slice details = call->status[i].details->slice;
- size_t len = GPR_SLICE_LENGTH (details);
- if (len + 1 > *out.recv_status_details.details_capacity)
- {
- *out.recv_status_details.details_capacity = GPR_MAX (len + 1, *out.recv_status_details.details_capacity * 3 / 2);
- *out.recv_status_details.details = gpr_realloc (*out.recv_status_details.details, *out.recv_status_details.details_capacity);
- }
- memcpy (*out.recv_status_details.details, GPR_SLICE_START_PTR (details), len);
- (*out.recv_status_details.details)[len] = 0;
- }
- else
- {
- goto no_details;
- }
- return;
- }
+ for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
+ if (call->status[i].is_set) {
+ if (call->status[i].details) {
+ gpr_slice details = call->status[i].details->slice;
+ size_t len = GPR_SLICE_LENGTH(details);
+ if (len + 1 > *out.recv_status_details.details_capacity) {
+ *out.recv_status_details.details_capacity = GPR_MAX(
+ len + 1, *out.recv_status_details.details_capacity * 3 / 2);
+ *out.recv_status_details.details =
+ gpr_realloc(*out.recv_status_details.details,
+ *out.recv_status_details.details_capacity);
+ }
+ memcpy(*out.recv_status_details.details, GPR_SLICE_START_PTR(details),
+ len);
+ (*out.recv_status_details.details)[len] = 0;
+ } else {
+ goto no_details;
+ }
+ return;
}
+ }
no_details:
- if (0 == *out.recv_status_details.details_capacity)
- {
- *out.recv_status_details.details_capacity = 8;
- *out.recv_status_details.details = gpr_malloc (*out.recv_status_details.details_capacity);
- }
+ if (0 == *out.recv_status_details.details_capacity) {
+ *out.recv_status_details.details_capacity = 8;
+ *out.recv_status_details.details =
+ gpr_malloc(*out.recv_status_details.details_capacity);
+ }
**out.recv_status_details.details = 0;
}
-static void
-finish_live_ioreq_op (grpc_call * call, grpc_ioreq_op op, int success)
-{
+static void finish_live_ioreq_op(grpc_call *call, grpc_ioreq_op op,
+ int success) {
completed_request *cr;
gpr_uint8 master_set = call->request_set[op];
reqinfo_master *master;
size_t i;
/* ioreq is live: we need to do something */
master = &call->masters[master_set];
- master->complete_mask |= (gpr_uint16) (1u << op);
- if (!success)
- {
- master->success = 0;
- }
- if (master->complete_mask == master->need_mask)
- {
- for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++)
- {
- if (call->request_set[i] != master_set)
- {
- continue;
- }
- call->request_set[i] = REQSET_DONE;
- switch ((grpc_ioreq_op) i)
- {
- case GRPC_IOREQ_RECV_MESSAGE:
- case GRPC_IOREQ_SEND_MESSAGE:
- call->request_set[i] = REQSET_EMPTY;
- if (!master->success)
- {
- call->write_state = WRITE_STATE_WRITE_CLOSED;
- }
- break;
- case GRPC_IOREQ_SEND_STATUS:
- if (call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details != NULL)
- {
- GRPC_MDSTR_UNREF (call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details);
- call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details = NULL;
- }
- break;
- case GRPC_IOREQ_RECV_CLOSE:
- case GRPC_IOREQ_SEND_INITIAL_METADATA:
- case GRPC_IOREQ_SEND_TRAILING_METADATA:
- case GRPC_IOREQ_SEND_CLOSE:
- break;
- case GRPC_IOREQ_RECV_STATUS:
- get_final_status (call, call->request_data[GRPC_IOREQ_RECV_STATUS]);
- break;
- case GRPC_IOREQ_RECV_STATUS_DETAILS:
- get_final_details (call, call->request_data[GRPC_IOREQ_RECV_STATUS_DETAILS]);
- break;
- case GRPC_IOREQ_RECV_INITIAL_METADATA:
- GPR_SWAP (grpc_metadata_array, call->buffered_metadata[0], *call->request_data[GRPC_IOREQ_RECV_INITIAL_METADATA].recv_metadata);
- break;
- case GRPC_IOREQ_RECV_TRAILING_METADATA:
- GPR_SWAP (grpc_metadata_array, call->buffered_metadata[1], *call->request_data[GRPC_IOREQ_RECV_TRAILING_METADATA].recv_metadata);
- break;
- case GRPC_IOREQ_OP_COUNT:
- abort ();
- break;
- }
- }
- cr = &call->completed_requests[call->num_completed_requests++];
- cr->success = master->success;
- cr->on_complete = master->on_complete;
- cr->user_data = master->user_data;
- }
+ master->complete_mask |= (gpr_uint16)(1u << op);
+ if (!success) {
+ master->success = 0;
+ }
+ if (master->complete_mask == master->need_mask) {
+ for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++) {
+ if (call->request_set[i] != master_set) {
+ continue;
+ }
+ call->request_set[i] = REQSET_DONE;
+ switch ((grpc_ioreq_op)i) {
+ case GRPC_IOREQ_RECV_MESSAGE:
+ case GRPC_IOREQ_SEND_MESSAGE:
+ call->request_set[i] = REQSET_EMPTY;
+ if (!master->success) {
+ call->write_state = WRITE_STATE_WRITE_CLOSED;
+ }
+ break;
+ case GRPC_IOREQ_SEND_STATUS:
+ if (call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details !=
+ NULL) {
+ GRPC_MDSTR_UNREF(
+ call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details);
+ call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details =
+ NULL;
+ }
+ break;
+ case GRPC_IOREQ_RECV_CLOSE:
+ case GRPC_IOREQ_SEND_INITIAL_METADATA:
+ case GRPC_IOREQ_SEND_TRAILING_METADATA:
+ case GRPC_IOREQ_SEND_CLOSE:
+ break;
+ case GRPC_IOREQ_RECV_STATUS:
+ get_final_status(call, call->request_data[GRPC_IOREQ_RECV_STATUS]);
+ break;
+ case GRPC_IOREQ_RECV_STATUS_DETAILS:
+ get_final_details(call,
+ call->request_data[GRPC_IOREQ_RECV_STATUS_DETAILS]);
+ break;
+ case GRPC_IOREQ_RECV_INITIAL_METADATA:
+ GPR_SWAP(grpc_metadata_array, call->buffered_metadata[0],
+ *call->request_data[GRPC_IOREQ_RECV_INITIAL_METADATA]
+ .recv_metadata);
+ break;
+ case GRPC_IOREQ_RECV_TRAILING_METADATA:
+ GPR_SWAP(grpc_metadata_array, call->buffered_metadata[1],
+ *call->request_data[GRPC_IOREQ_RECV_TRAILING_METADATA]
+ .recv_metadata);
+ break;
+ case GRPC_IOREQ_OP_COUNT:
+ abort();
+ break;
+ }
+ }
+ cr = &call->completed_requests[call->num_completed_requests++];
+ cr->success = master->success;
+ cr->on_complete = master->on_complete;
+ cr->user_data = master->user_data;
+ }
}
-static void
-finish_ioreq_op (grpc_call * call, grpc_ioreq_op op, int success)
-{
- if (is_op_live (call, op))
- {
- finish_live_ioreq_op (call, op, success);
- }
+static void finish_ioreq_op(grpc_call *call, grpc_ioreq_op op, int success) {
+ if (is_op_live(call, op)) {
+ finish_live_ioreq_op(call, op, success);
+ }
}
-static void
-early_out_write_ops (grpc_call * call)
-{
- switch (call->write_state)
- {
+static void early_out_write_ops(grpc_call *call) {
+ switch (call->write_state) {
case WRITE_STATE_WRITE_CLOSED:
- finish_ioreq_op (call, GRPC_IOREQ_SEND_MESSAGE, 0);
- finish_ioreq_op (call, GRPC_IOREQ_SEND_STATUS, 0);
- finish_ioreq_op (call, GRPC_IOREQ_SEND_TRAILING_METADATA, 0);
- finish_ioreq_op (call, GRPC_IOREQ_SEND_CLOSE, 1);
- /* fallthrough */
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_MESSAGE, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_STATUS, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_TRAILING_METADATA, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_CLOSE, 1);
+ /* fallthrough */
case WRITE_STATE_STARTED:
- finish_ioreq_op (call, GRPC_IOREQ_SEND_INITIAL_METADATA, 0);
- /* fallthrough */
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_INITIAL_METADATA, 0);
+ /* fallthrough */
case WRITE_STATE_INITIAL:
/* do nothing */
break;
- }
+ }
}
-static void
-call_on_done_send (grpc_exec_ctx * exec_ctx, void *pc, int success)
-{
+static void call_on_done_send(grpc_exec_ctx *exec_ctx, void *pc, int success) {
grpc_call *call = pc;
- lock (call);
- if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_INITIAL_METADATA))
- {
- finish_ioreq_op (call, GRPC_IOREQ_SEND_INITIAL_METADATA, success);
- call->write_state = WRITE_STATE_STARTED;
- }
- if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_MESSAGE))
- {
- finish_ioreq_op (call, GRPC_IOREQ_SEND_MESSAGE, success);
- }
- if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_CLOSE))
- {
- finish_ioreq_op (call, GRPC_IOREQ_SEND_TRAILING_METADATA, success);
- finish_ioreq_op (call, GRPC_IOREQ_SEND_STATUS, success);
- finish_ioreq_op (call, GRPC_IOREQ_SEND_CLOSE, 1);
- call->write_state = WRITE_STATE_WRITE_CLOSED;
- }
- if (!success)
- {
- call->write_state = WRITE_STATE_WRITE_CLOSED;
- early_out_write_ops (call);
- }
+ lock(call);
+ if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_INITIAL_METADATA)) {
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_INITIAL_METADATA, success);
+ call->write_state = WRITE_STATE_STARTED;
+ }
+ if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_MESSAGE)) {
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_MESSAGE, success);
+ }
+ if (call->last_send_contains & (1 << GRPC_IOREQ_SEND_CLOSE)) {
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_TRAILING_METADATA, success);
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_STATUS, success);
+ finish_ioreq_op(call, GRPC_IOREQ_SEND_CLOSE, 1);
+ call->write_state = WRITE_STATE_WRITE_CLOSED;
+ }
+ if (!success) {
+ call->write_state = WRITE_STATE_WRITE_CLOSED;
+ early_out_write_ops(call);
+ }
call->send_ops.nops = 0;
call->last_send_contains = 0;
call->sending = 0;
- unlock (exec_ctx, call);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "sending");
+ unlock(exec_ctx, call);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "sending");
}
-static void
-finish_message (grpc_call * call)
-{
- if (call->error_status_set == 0)
- {
- /* TODO(ctiller): this could be a lot faster if coded directly */
- grpc_byte_buffer *byte_buffer;
- /* some aliases for readability */
- gpr_slice *slices = call->incoming_message.slices;
- const size_t nslices = call->incoming_message.count;
-
- if ((call->incoming_message_flags & GRPC_WRITE_INTERNAL_COMPRESS) && (call->compression_algorithm > GRPC_COMPRESS_NONE))
- {
- byte_buffer = grpc_raw_compressed_byte_buffer_create (slices, nslices, call->compression_algorithm);
- }
- else
- {
- byte_buffer = grpc_raw_byte_buffer_create (slices, nslices);
- }
- grpc_bbq_push (&call->incoming_queue, byte_buffer);
- }
- gpr_slice_buffer_reset_and_unref (&call->incoming_message);
- GPR_ASSERT (call->incoming_message.count == 0);
+static void finish_message(grpc_call *call) {
+ if (call->error_status_set == 0) {
+ /* TODO(ctiller): this could be a lot faster if coded directly */
+ grpc_byte_buffer *byte_buffer;
+ /* some aliases for readability */
+ gpr_slice *slices = call->incoming_message.slices;
+ const size_t nslices = call->incoming_message.count;
+
+ if ((call->incoming_message_flags & GRPC_WRITE_INTERNAL_COMPRESS) &&
+ (call->compression_algorithm > GRPC_COMPRESS_NONE)) {
+ byte_buffer = grpc_raw_compressed_byte_buffer_create(
+ slices, nslices, call->compression_algorithm);
+ } else {
+ byte_buffer = grpc_raw_byte_buffer_create(slices, nslices);
+ }
+ grpc_bbq_push(&call->incoming_queue, byte_buffer);
+ }
+ gpr_slice_buffer_reset_and_unref(&call->incoming_message);
+ GPR_ASSERT(call->incoming_message.count == 0);
call->reading_message = 0;
}
-static int
-begin_message (grpc_call * call, grpc_begin_message msg)
-{
+static int begin_message(grpc_call *call, grpc_begin_message msg) {
/* can't begin a message when we're still reading a message */
- if (call->reading_message)
- {
- char *message = NULL;
- gpr_asprintf (&message, "Message terminated early; read %d bytes, expected %d", (int) call->incoming_message.length, (int) call->incoming_message_length);
- cancel_with_status (call, GRPC_STATUS_INVALID_ARGUMENT, message);
- gpr_free (message);
- return 0;
- }
+ if (call->reading_message) {
+ char *message = NULL;
+ gpr_asprintf(
+ &message, "Message terminated early; read %d bytes, expected %d",
+ (int)call->incoming_message.length, (int)call->incoming_message_length);
+ cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, message);
+ gpr_free(message);
+ return 0;
+ }
/* sanity check: if message flags indicate a compressed message, the
* compression level should already be present in the call, as parsed off its
* corresponding metadata. */
- if ((msg.flags & GRPC_WRITE_INTERNAL_COMPRESS) && (call->compression_algorithm == GRPC_COMPRESS_NONE))
- {
- char *message = NULL;
- char *alg_name;
- if (!grpc_compression_algorithm_name (call->compression_algorithm, &alg_name))
- {
- /* This shouldn't happen, other than due to data corruption */
- alg_name = "<unknown>";
- }
- gpr_asprintf (&message, "Invalid compression algorithm (%s) for compressed message.", alg_name);
- cancel_with_status (call, GRPC_STATUS_INTERNAL, message);
- gpr_free (message);
- return 0;
- }
+ if ((msg.flags & GRPC_WRITE_INTERNAL_COMPRESS) &&
+ (call->compression_algorithm == GRPC_COMPRESS_NONE)) {
+ char *message = NULL;
+ char *alg_name;
+ if (!grpc_compression_algorithm_name(call->compression_algorithm,
+ &alg_name)) {
+ /* This shouldn't happen, other than due to data corruption */
+ alg_name = "<unknown>";
+ }
+ gpr_asprintf(&message,
+ "Invalid compression algorithm (%s) for compressed message.",
+ alg_name);
+ cancel_with_status(call, GRPC_STATUS_INTERNAL, message);
+ gpr_free(message);
+ return 0;
+ }
/* stash away parameters, and prepare for incoming slices */
- if (msg.length > grpc_channel_get_max_message_length (call->channel))
- {
- char *message = NULL;
- gpr_asprintf (&message, "Maximum message length of %d exceeded by a message of length %d", grpc_channel_get_max_message_length (call->channel), msg.length);
- cancel_with_status (call, GRPC_STATUS_INVALID_ARGUMENT, message);
- gpr_free (message);
- return 0;
- }
- else if (msg.length > 0)
- {
- call->reading_message = 1;
- call->incoming_message_length = msg.length;
- call->incoming_message_flags = msg.flags;
- return 1;
- }
- else
- {
- finish_message (call);
- return 1;
- }
+ if (msg.length > grpc_channel_get_max_message_length(call->channel)) {
+ char *message = NULL;
+ gpr_asprintf(
+ &message,
+ "Maximum message length of %d exceeded by a message of length %d",
+ grpc_channel_get_max_message_length(call->channel), msg.length);
+ cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, message);
+ gpr_free(message);
+ return 0;
+ } else if (msg.length > 0) {
+ call->reading_message = 1;
+ call->incoming_message_length = msg.length;
+ call->incoming_message_flags = msg.flags;
+ return 1;
+ } else {
+ finish_message(call);
+ return 1;
+ }
}
-static int
-add_slice_to_message (grpc_call * call, gpr_slice slice)
-{
- if (GPR_SLICE_LENGTH (slice) == 0)
- {
- gpr_slice_unref (slice);
- return 1;
- }
+static int add_slice_to_message(grpc_call *call, gpr_slice slice) {
+ if (GPR_SLICE_LENGTH(slice) == 0) {
+ gpr_slice_unref(slice);
+ return 1;
+ }
/* we have to be reading a message to know what to do here */
- if (!call->reading_message)
- {
- cancel_with_status (call, GRPC_STATUS_INVALID_ARGUMENT, "Received payload data while not reading a message");
- return 0;
- }
+ if (!call->reading_message) {
+ cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT,
+ "Received payload data while not reading a message");
+ return 0;
+ }
/* append the slice to the incoming buffer */
- gpr_slice_buffer_add (&call->incoming_message, slice);
- if (call->incoming_message.length > call->incoming_message_length)
- {
- /* if we got too many bytes, complain */
- char *message = NULL;
- gpr_asprintf (&message, "Receiving message overflow; read %d bytes, expected %d", (int) call->incoming_message.length, (int) call->incoming_message_length);
- cancel_with_status (call, GRPC_STATUS_INVALID_ARGUMENT, message);
- gpr_free (message);
- return 0;
- }
- else if (call->incoming_message.length == call->incoming_message_length)
- {
- finish_message (call);
- return 1;
- }
- else
- {
- return 1;
- }
+ gpr_slice_buffer_add(&call->incoming_message, slice);
+ if (call->incoming_message.length > call->incoming_message_length) {
+ /* if we got too many bytes, complain */
+ char *message = NULL;
+ gpr_asprintf(
+ &message, "Receiving message overflow; read %d bytes, expected %d",
+ (int)call->incoming_message.length, (int)call->incoming_message_length);
+ cancel_with_status(call, GRPC_STATUS_INVALID_ARGUMENT, message);
+ gpr_free(message);
+ return 0;
+ } else if (call->incoming_message.length == call->incoming_message_length) {
+ finish_message(call);
+ return 1;
+ } else {
+ return 1;
+ }
}
-static void
-call_on_done_recv (grpc_exec_ctx * exec_ctx, void *pc, int success)
-{
+static void call_on_done_recv(grpc_exec_ctx *exec_ctx, void *pc, int success) {
grpc_call *call = pc;
grpc_call *child_call;
grpc_call *next_child_call;
size_t i;
- GRPC_TIMER_BEGIN (GRPC_PTAG_CALL_ON_DONE_RECV, 0);
- lock (call);
+ GRPC_TIMER_BEGIN(GRPC_PTAG_CALL_ON_DONE_RECV, 0);
+ lock(call);
call->receiving = 0;
- if (success)
- {
- for (i = 0; success && i < call->recv_ops.nops; i++)
- {
- grpc_stream_op *op = &call->recv_ops.ops[i];
- switch (op->type)
- {
- case GRPC_NO_OP:
- break;
- case GRPC_OP_METADATA:
- recv_metadata (exec_ctx, call, &op->data.metadata);
- break;
- case GRPC_OP_BEGIN_MESSAGE:
- success = begin_message (call, op->data.begin_message);
- break;
- case GRPC_OP_SLICE:
- success = add_slice_to_message (call, op->data.slice);
- break;
- }
- }
- if (!success)
- {
- grpc_stream_ops_unref_owned_objects (&call->recv_ops.ops[i], call->recv_ops.nops - i);
- }
- if (call->recv_state == GRPC_STREAM_RECV_CLOSED)
- {
- GPR_ASSERT (call->read_state <= READ_STATE_READ_CLOSED);
- call->read_state = READ_STATE_READ_CLOSED;
- }
- if (call->recv_state == GRPC_STREAM_CLOSED)
- {
- GPR_ASSERT (call->read_state <= READ_STATE_STREAM_CLOSED);
- call->read_state = READ_STATE_STREAM_CLOSED;
- if (call->have_alarm)
- {
- grpc_alarm_cancel (exec_ctx, &call->alarm);
- }
- /* propagate cancellation to any interested children */
- child_call = call->first_child;
- if (child_call != NULL)
- {
- do
- {
- next_child_call = child_call->sibling_next;
- if (child_call->cancellation_is_inherited)
- {
- GRPC_CALL_INTERNAL_REF (child_call, "propagate_cancel");
- grpc_call_cancel (child_call, NULL);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, child_call, "propagate_cancel");
- }
- child_call = next_child_call;
- }
- while (child_call != call->first_child);
- }
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "closed");
- }
- finish_read_ops (call);
- }
- else
- {
- finish_ioreq_op (call, GRPC_IOREQ_RECV_MESSAGE, 0);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_STATUS, 0);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_CLOSE, 0);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_TRAILING_METADATA, 0);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_INITIAL_METADATA, 0);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_STATUS_DETAILS, 0);
- }
+ if (success) {
+ for (i = 0; success && i < call->recv_ops.nops; i++) {
+ grpc_stream_op *op = &call->recv_ops.ops[i];
+ switch (op->type) {
+ case GRPC_NO_OP:
+ break;
+ case GRPC_OP_METADATA:
+ recv_metadata(exec_ctx, call, &op->data.metadata);
+ break;
+ case GRPC_OP_BEGIN_MESSAGE:
+ success = begin_message(call, op->data.begin_message);
+ break;
+ case GRPC_OP_SLICE:
+ success = add_slice_to_message(call, op->data.slice);
+ break;
+ }
+ }
+ if (!success) {
+ grpc_stream_ops_unref_owned_objects(&call->recv_ops.ops[i],
+ call->recv_ops.nops - i);
+ }
+ if (call->recv_state == GRPC_STREAM_RECV_CLOSED) {
+ GPR_ASSERT(call->read_state <= READ_STATE_READ_CLOSED);
+ call->read_state = READ_STATE_READ_CLOSED;
+ }
+ if (call->recv_state == GRPC_STREAM_CLOSED) {
+ GPR_ASSERT(call->read_state <= READ_STATE_STREAM_CLOSED);
+ call->read_state = READ_STATE_STREAM_CLOSED;
+ if (call->have_alarm) {
+ grpc_alarm_cancel(exec_ctx, &call->alarm);
+ }
+ /* propagate cancellation to any interested children */
+ child_call = call->first_child;
+ if (child_call != NULL) {
+ do {
+ next_child_call = child_call->sibling_next;
+ if (child_call->cancellation_is_inherited) {
+ GRPC_CALL_INTERNAL_REF(child_call, "propagate_cancel");
+ grpc_call_cancel(child_call, NULL);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, child_call, "propagate_cancel");
+ }
+ child_call = next_child_call;
+ } while (child_call != call->first_child);
+ }
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "closed");
+ }
+ finish_read_ops(call);
+ } else {
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_MESSAGE, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_CLOSE, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_TRAILING_METADATA, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_INITIAL_METADATA, 0);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS_DETAILS, 0);
+ }
call->recv_ops.nops = 0;
- unlock (exec_ctx, call);
+ unlock(exec_ctx, call);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "receiving");
- GRPC_TIMER_END (GRPC_PTAG_CALL_ON_DONE_RECV, 0);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "receiving");
+ GRPC_TIMER_END(GRPC_PTAG_CALL_ON_DONE_RECV, 0);
}
-static int
-prepare_application_metadata (grpc_call * call, size_t count, grpc_metadata * metadata)
-{
+static int prepare_application_metadata(grpc_call *call, size_t count,
+ grpc_metadata *metadata) {
size_t i;
- for (i = 0; i < count; i++)
- {
- grpc_metadata *md = &metadata[i];
- grpc_metadata *next_md = (i == count - 1) ? NULL : &metadata[i + 1];
- grpc_metadata *prev_md = (i == 0) ? NULL : &metadata[i - 1];
- grpc_linked_mdelem *l = (grpc_linked_mdelem *) & md->internal_data;
- GPR_ASSERT (sizeof (grpc_linked_mdelem) == sizeof (md->internal_data));
- l->md = grpc_mdelem_from_string_and_buffer (call->metadata_context, md->key, (const gpr_uint8 *) md->value, md->value_length, 1);
- if (!grpc_mdstr_is_legal_header (l->md->key))
- {
- gpr_log (GPR_ERROR, "attempt to send invalid metadata key: %s", grpc_mdstr_as_c_string (l->md->key));
- return 0;
- }
- else if (!grpc_mdstr_is_bin_suffixed (l->md->key) && !grpc_mdstr_is_legal_nonbin_header (l->md->value))
- {
- gpr_log (GPR_ERROR, "attempt to send invalid metadata value");
- return 0;
- }
- l->next = next_md ? (grpc_linked_mdelem *) & next_md->internal_data : NULL;
- l->prev = prev_md ? (grpc_linked_mdelem *) & prev_md->internal_data : NULL;
+ for (i = 0; i < count; i++) {
+ grpc_metadata *md = &metadata[i];
+ grpc_metadata *next_md = (i == count - 1) ? NULL : &metadata[i + 1];
+ grpc_metadata *prev_md = (i == 0) ? NULL : &metadata[i - 1];
+ grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
+ GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data));
+ l->md = grpc_mdelem_from_string_and_buffer(call->metadata_context, md->key,
+ (const gpr_uint8 *)md->value,
+ md->value_length, 1);
+ if (!grpc_mdstr_is_legal_header(l->md->key)) {
+ gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s",
+ grpc_mdstr_as_c_string(l->md->key));
+ return 0;
+ } else if (!grpc_mdstr_is_bin_suffixed(l->md->key) &&
+ !grpc_mdstr_is_legal_nonbin_header(l->md->value)) {
+ gpr_log(GPR_ERROR, "attempt to send invalid metadata value");
+ return 0;
}
+ l->next = next_md ? (grpc_linked_mdelem *)&next_md->internal_data : NULL;
+ l->prev = prev_md ? (grpc_linked_mdelem *)&prev_md->internal_data : NULL;
+ }
return 1;
}
-static grpc_mdelem_list
-chain_metadata_from_app (grpc_call * call, size_t count, grpc_metadata * metadata)
-{
+static grpc_mdelem_list chain_metadata_from_app(grpc_call *call, size_t count,
+ grpc_metadata *metadata) {
grpc_mdelem_list out;
- if (count == 0)
- {
- out.head = out.tail = NULL;
- return out;
- }
- out.head = (grpc_linked_mdelem *) & (metadata[0].internal_data);
- out.tail = (grpc_linked_mdelem *) & (metadata[count - 1].internal_data);
+ if (count == 0) {
+ out.head = out.tail = NULL;
+ return out;
+ }
+ out.head = (grpc_linked_mdelem *)&(metadata[0].internal_data);
+ out.tail = (grpc_linked_mdelem *)&(metadata[count - 1].internal_data);
return out;
}
/* Copy the contents of a byte buffer into stream ops */
-static void
-copy_byte_buffer_to_stream_ops (grpc_byte_buffer * byte_buffer, grpc_stream_op_buffer * sopb)
-{
+static void copy_byte_buffer_to_stream_ops(grpc_byte_buffer *byte_buffer,
+ grpc_stream_op_buffer *sopb) {
size_t i;
- switch (byte_buffer->type)
- {
+ switch (byte_buffer->type) {
case GRPC_BB_RAW:
- for (i = 0; i < byte_buffer->data.raw.slice_buffer.count; i++)
- {
- gpr_slice slice = byte_buffer->data.raw.slice_buffer.slices[i];
- gpr_slice_ref (slice);
- grpc_sopb_add_slice (sopb, slice);
- }
+ for (i = 0; i < byte_buffer->data.raw.slice_buffer.count; i++) {
+ gpr_slice slice = byte_buffer->data.raw.slice_buffer.slices[i];
+ gpr_slice_ref(slice);
+ grpc_sopb_add_slice(sopb, slice);
+ }
break;
- }
+ }
}
-static int
-fill_send_ops (grpc_call * call, grpc_transport_stream_op * op)
-{
+static int fill_send_ops(grpc_call *call, grpc_transport_stream_op *op) {
grpc_ioreq_data data;
gpr_uint32 flags;
grpc_metadata_batch mdb;
size_t i;
- GPR_ASSERT (op->send_ops == NULL);
+ GPR_ASSERT(op->send_ops == NULL);
- switch (call->write_state)
- {
+ switch (call->write_state) {
case WRITE_STATE_INITIAL:
- if (!is_op_live (call, GRPC_IOREQ_SEND_INITIAL_METADATA))
- {
- break;
- }
+ if (!is_op_live(call, GRPC_IOREQ_SEND_INITIAL_METADATA)) {
+ break;
+ }
data = call->request_data[GRPC_IOREQ_SEND_INITIAL_METADATA];
- mdb.list = chain_metadata_from_app (call, data.send_metadata.count, data.send_metadata.metadata);
+ mdb.list = chain_metadata_from_app(call, data.send_metadata.count,
+ data.send_metadata.metadata);
mdb.garbage.head = mdb.garbage.tail = NULL;
mdb.deadline = call->send_deadline;
- for (i = 0; i < call->send_initial_metadata_count; i++)
- {
- grpc_metadata_batch_link_head (&mdb, &call->send_initial_metadata[i]);
- }
- grpc_sopb_add_metadata (&call->send_ops, mdb);
+ for (i = 0; i < call->send_initial_metadata_count; i++) {
+ grpc_metadata_batch_link_head(&mdb, &call->send_initial_metadata[i]);
+ }
+ grpc_sopb_add_metadata(&call->send_ops, mdb);
op->send_ops = &call->send_ops;
call->last_send_contains |= 1 << GRPC_IOREQ_SEND_INITIAL_METADATA;
call->send_initial_metadata_count = 0;
- /* fall through intended */
+ /* fall through intended */
case WRITE_STATE_STARTED:
- if (is_op_live (call, GRPC_IOREQ_SEND_MESSAGE))
- {
- size_t length;
- data = call->request_data[GRPC_IOREQ_SEND_MESSAGE];
- flags = call->request_flags[GRPC_IOREQ_SEND_MESSAGE];
- length = grpc_byte_buffer_length (data.send_message);
- GPR_ASSERT (length <= GPR_UINT32_MAX);
- grpc_sopb_add_begin_message (&call->send_ops, (gpr_uint32) length, flags);
- copy_byte_buffer_to_stream_ops (data.send_message, &call->send_ops);
- op->send_ops = &call->send_ops;
- call->last_send_contains |= 1 << GRPC_IOREQ_SEND_MESSAGE;
- }
- if (is_op_live (call, GRPC_IOREQ_SEND_CLOSE))
- {
- op->is_last_send = 1;
- op->send_ops = &call->send_ops;
- call->last_send_contains |= 1 << GRPC_IOREQ_SEND_CLOSE;
- if (!call->is_client)
- {
- /* send trailing metadata */
- data = call->request_data[GRPC_IOREQ_SEND_TRAILING_METADATA];
- mdb.list = chain_metadata_from_app (call, data.send_metadata.count, data.send_metadata.metadata);
- mdb.garbage.head = mdb.garbage.tail = NULL;
- mdb.deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
- /* send status */
- /* TODO(ctiller): cache common status values */
- data = call->request_data[GRPC_IOREQ_SEND_STATUS];
- grpc_metadata_batch_add_tail (&mdb, &call->status_link, grpc_channel_get_reffed_status_elem (call->channel, data.send_status.code));
- if (data.send_status.details)
- {
- grpc_metadata_batch_add_tail (&mdb, &call->details_link, grpc_mdelem_from_metadata_strings (call->metadata_context, GRPC_MDSTR_REF (grpc_channel_get_message_string (call->channel)), data.send_status.details));
- call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details = NULL;
- }
- grpc_sopb_add_metadata (&call->send_ops, mdb);
- }
- }
+ if (is_op_live(call, GRPC_IOREQ_SEND_MESSAGE)) {
+ size_t length;
+ data = call->request_data[GRPC_IOREQ_SEND_MESSAGE];
+ flags = call->request_flags[GRPC_IOREQ_SEND_MESSAGE];
+ length = grpc_byte_buffer_length(data.send_message);
+ GPR_ASSERT(length <= GPR_UINT32_MAX);
+ grpc_sopb_add_begin_message(&call->send_ops, (gpr_uint32)length, flags);
+ copy_byte_buffer_to_stream_ops(data.send_message, &call->send_ops);
+ op->send_ops = &call->send_ops;
+ call->last_send_contains |= 1 << GRPC_IOREQ_SEND_MESSAGE;
+ }
+ if (is_op_live(call, GRPC_IOREQ_SEND_CLOSE)) {
+ op->is_last_send = 1;
+ op->send_ops = &call->send_ops;
+ call->last_send_contains |= 1 << GRPC_IOREQ_SEND_CLOSE;
+ if (!call->is_client) {
+ /* send trailing metadata */
+ data = call->request_data[GRPC_IOREQ_SEND_TRAILING_METADATA];
+ mdb.list = chain_metadata_from_app(call, data.send_metadata.count,
+ data.send_metadata.metadata);
+ mdb.garbage.head = mdb.garbage.tail = NULL;
+ mdb.deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
+ /* send status */
+ /* TODO(ctiller): cache common status values */
+ data = call->request_data[GRPC_IOREQ_SEND_STATUS];
+ grpc_metadata_batch_add_tail(
+ &mdb, &call->status_link,
+ grpc_channel_get_reffed_status_elem(call->channel,
+ data.send_status.code));
+ if (data.send_status.details) {
+ grpc_metadata_batch_add_tail(
+ &mdb, &call->details_link,
+ grpc_mdelem_from_metadata_strings(
+ call->metadata_context,
+ GRPC_MDSTR_REF(
+ grpc_channel_get_message_string(call->channel)),
+ data.send_status.details));
+ call->request_data[GRPC_IOREQ_SEND_STATUS].send_status.details =
+ NULL;
+ }
+ grpc_sopb_add_metadata(&call->send_ops, mdb);
+ }
+ }
break;
case WRITE_STATE_WRITE_CLOSED:
break;
- }
- if (op->send_ops)
- {
- op->on_done_send = &call->on_done_send;
- }
+ }
+ if (op->send_ops) {
+ op->on_done_send = &call->on_done_send;
+ }
return op->send_ops != NULL;
}
-static grpc_call_error
-start_ioreq_error (grpc_call * call, gpr_uint32 mutated_ops, grpc_call_error ret)
-{
+static grpc_call_error start_ioreq_error(grpc_call *call,
+ gpr_uint32 mutated_ops,
+ grpc_call_error ret) {
size_t i;
- for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++)
- {
- if (mutated_ops & (1u << i))
- {
- call->request_set[i] = REQSET_EMPTY;
- }
+ for (i = 0; i < GRPC_IOREQ_OP_COUNT; i++) {
+ if (mutated_ops & (1u << i)) {
+ call->request_set[i] = REQSET_EMPTY;
}
+ }
return ret;
}
-static void
-finish_read_ops (grpc_call * call)
-{
+static void finish_read_ops(grpc_call *call) {
int empty;
- if (is_op_live (call, GRPC_IOREQ_RECV_MESSAGE))
- {
- empty = (NULL == (*call->request_data[GRPC_IOREQ_RECV_MESSAGE].recv_message = grpc_bbq_pop (&call->incoming_queue)));
- if (!empty)
- {
- finish_live_ioreq_op (call, GRPC_IOREQ_RECV_MESSAGE, 1);
- empty = grpc_bbq_empty (&call->incoming_queue);
- }
- }
- else
- {
- empty = grpc_bbq_empty (&call->incoming_queue);
+ if (is_op_live(call, GRPC_IOREQ_RECV_MESSAGE)) {
+ empty =
+ (NULL == (*call->request_data[GRPC_IOREQ_RECV_MESSAGE].recv_message =
+ grpc_bbq_pop(&call->incoming_queue)));
+ if (!empty) {
+ finish_live_ioreq_op(call, GRPC_IOREQ_RECV_MESSAGE, 1);
+ empty = grpc_bbq_empty(&call->incoming_queue);
}
+ } else {
+ empty = grpc_bbq_empty(&call->incoming_queue);
+ }
- switch (call->read_state)
- {
+ switch (call->read_state) {
case READ_STATE_STREAM_CLOSED:
- if (empty && !call->have_alarm)
- {
- finish_ioreq_op (call, GRPC_IOREQ_RECV_CLOSE, 1);
- }
- /* fallthrough */
+ if (empty && !call->have_alarm) {
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_CLOSE, 1);
+ }
+ /* fallthrough */
case READ_STATE_READ_CLOSED:
- if (empty)
- {
- finish_ioreq_op (call, GRPC_IOREQ_RECV_MESSAGE, 1);
- }
- finish_ioreq_op (call, GRPC_IOREQ_RECV_STATUS, 1);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_STATUS_DETAILS, 1);
- finish_ioreq_op (call, GRPC_IOREQ_RECV_TRAILING_METADATA, 1);
- /* fallthrough */
+ if (empty) {
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_MESSAGE, 1);
+ }
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS, 1);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_STATUS_DETAILS, 1);
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_TRAILING_METADATA, 1);
+ /* fallthrough */
case READ_STATE_GOT_INITIAL_METADATA:
- finish_ioreq_op (call, GRPC_IOREQ_RECV_INITIAL_METADATA, 1);
- /* fallthrough */
+ finish_ioreq_op(call, GRPC_IOREQ_RECV_INITIAL_METADATA, 1);
+ /* fallthrough */
case READ_STATE_INITIAL:
/* do nothing */
break;
- }
+ }
}
-static grpc_call_error
-start_ioreq (grpc_call * call, const grpc_ioreq * reqs, size_t nreqs, grpc_ioreq_completion_func completion, void *user_data)
-{
+static grpc_call_error start_ioreq(grpc_call *call, const grpc_ioreq *reqs,
+ size_t nreqs,
+ grpc_ioreq_completion_func completion,
+ void *user_data) {
size_t i;
gpr_uint16 have_ops = 0;
grpc_ioreq_op op;
@@ -1326,46 +1214,43 @@ start_ioreq (grpc_call * call, const grpc_ioreq * reqs, size_t nreqs, grpc_ioreq
grpc_ioreq_data data;
gpr_uint8 set;
- if (nreqs == 0)
- {
- return GRPC_CALL_OK;
- }
+ if (nreqs == 0) {
+ return GRPC_CALL_OK;
+ }
set = reqs[0].op;
- for (i = 0; i < nreqs; i++)
- {
- op = reqs[i].op;
- if (call->request_set[op] < GRPC_IOREQ_OP_COUNT)
- {
- return start_ioreq_error (call, have_ops, GRPC_CALL_ERROR_TOO_MANY_OPERATIONS);
- }
- else if (call->request_set[op] == REQSET_DONE)
- {
- return start_ioreq_error (call, have_ops, GRPC_CALL_ERROR_ALREADY_INVOKED);
- }
- data = reqs[i].data;
- if (op == GRPC_IOREQ_SEND_INITIAL_METADATA || op == GRPC_IOREQ_SEND_TRAILING_METADATA)
- {
- if (!prepare_application_metadata (call, data.send_metadata.count, data.send_metadata.metadata))
- {
- return start_ioreq_error (call, have_ops, GRPC_CALL_ERROR_INVALID_METADATA);
- }
- }
- if (op == GRPC_IOREQ_SEND_STATUS)
- {
- set_status_code (call, STATUS_FROM_SERVER_STATUS, (gpr_uint32) reqs[i].data.send_status.code);
- if (reqs[i].data.send_status.details)
- {
- set_status_details (call, STATUS_FROM_SERVER_STATUS, GRPC_MDSTR_REF (reqs[i].data.send_status.details));
- }
- }
- have_ops |= (gpr_uint16) (1u << op);
-
- call->request_data[op] = data;
- call->request_flags[op] = reqs[i].flags;
- call->request_set[op] = set;
- }
+ for (i = 0; i < nreqs; i++) {
+ op = reqs[i].op;
+ if (call->request_set[op] < GRPC_IOREQ_OP_COUNT) {
+ return start_ioreq_error(call, have_ops,
+ GRPC_CALL_ERROR_TOO_MANY_OPERATIONS);
+ } else if (call->request_set[op] == REQSET_DONE) {
+ return start_ioreq_error(call, have_ops, GRPC_CALL_ERROR_ALREADY_INVOKED);
+ }
+ data = reqs[i].data;
+ if (op == GRPC_IOREQ_SEND_INITIAL_METADATA ||
+ op == GRPC_IOREQ_SEND_TRAILING_METADATA) {
+ if (!prepare_application_metadata(call, data.send_metadata.count,
+ data.send_metadata.metadata)) {
+ return start_ioreq_error(call, have_ops,
+ GRPC_CALL_ERROR_INVALID_METADATA);
+ }
+ }
+ if (op == GRPC_IOREQ_SEND_STATUS) {
+ set_status_code(call, STATUS_FROM_SERVER_STATUS,
+ (gpr_uint32)reqs[i].data.send_status.code);
+ if (reqs[i].data.send_status.details) {
+ set_status_details(call, STATUS_FROM_SERVER_STATUS,
+ GRPC_MDSTR_REF(reqs[i].data.send_status.details));
+ }
+ }
+ have_ops |= (gpr_uint16)(1u << op);
+
+ call->request_data[op] = data;
+ call->request_flags[op] = reqs[i].flags;
+ call->request_set[op] = set;
+ }
master = &call->masters[set];
master->success = 1;
@@ -1374,251 +1259,218 @@ start_ioreq (grpc_call * call, const grpc_ioreq * reqs, size_t nreqs, grpc_ioreq
master->on_complete = completion;
master->user_data = user_data;
- finish_read_ops (call);
- early_out_write_ops (call);
+ finish_read_ops(call);
+ early_out_write_ops(call);
return GRPC_CALL_OK;
}
-grpc_call_error
-grpc_call_start_ioreq_and_call_back (grpc_exec_ctx * exec_ctx, grpc_call * call, const grpc_ioreq * reqs, size_t nreqs, grpc_ioreq_completion_func on_complete, void *user_data)
-{
+grpc_call_error grpc_call_start_ioreq_and_call_back(
+ grpc_exec_ctx *exec_ctx, grpc_call *call, const grpc_ioreq *reqs,
+ size_t nreqs, grpc_ioreq_completion_func on_complete, void *user_data) {
grpc_call_error err;
- lock (call);
- err = start_ioreq (call, reqs, nreqs, on_complete, user_data);
- unlock (exec_ctx, call);
+ lock(call);
+ err = start_ioreq(call, reqs, nreqs, on_complete, user_data);
+ unlock(exec_ctx, call);
return err;
}
-void
-grpc_call_destroy (grpc_call * c)
-{
+void grpc_call_destroy(grpc_call *c) {
int cancel;
grpc_call *parent = c->parent;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- if (parent)
- {
- gpr_mu_lock (&parent->mu);
- if (c == parent->first_child)
- {
- parent->first_child = c->sibling_next;
- if (c == parent->first_child)
- {
- parent->first_child = NULL;
- }
- c->sibling_prev->sibling_next = c->sibling_next;
- c->sibling_next->sibling_prev = c->sibling_prev;
- }
- gpr_mu_unlock (&parent->mu);
- GRPC_CALL_INTERNAL_UNREF (&exec_ctx, parent, "child");
- }
-
- lock (c);
- GPR_ASSERT (!c->destroy_called);
+ if (parent) {
+ gpr_mu_lock(&parent->mu);
+ if (c == parent->first_child) {
+ parent->first_child = c->sibling_next;
+ if (c == parent->first_child) {
+ parent->first_child = NULL;
+ }
+ c->sibling_prev->sibling_next = c->sibling_next;
+ c->sibling_next->sibling_prev = c->sibling_prev;
+ }
+ gpr_mu_unlock(&parent->mu);
+ GRPC_CALL_INTERNAL_UNREF(&exec_ctx, parent, "child");
+ }
+
+ lock(c);
+ GPR_ASSERT(!c->destroy_called);
c->destroy_called = 1;
- if (c->have_alarm)
- {
- grpc_alarm_cancel (&exec_ctx, &c->alarm);
- }
+ if (c->have_alarm) {
+ grpc_alarm_cancel(&exec_ctx, &c->alarm);
+ }
cancel = c->read_state != READ_STATE_STREAM_CLOSED;
- unlock (&exec_ctx, c);
- if (cancel)
- grpc_call_cancel (c, NULL);
- GRPC_CALL_INTERNAL_UNREF (&exec_ctx, c, "destroy");
- grpc_exec_ctx_finish (&exec_ctx);
+ unlock(&exec_ctx, c);
+ if (cancel) grpc_call_cancel(c, NULL);
+ GRPC_CALL_INTERNAL_UNREF(&exec_ctx, c, "destroy");
+ grpc_exec_ctx_finish(&exec_ctx);
}
-grpc_call_error
-grpc_call_cancel (grpc_call * call, void *reserved)
-{
- GPR_ASSERT (!reserved);
- return grpc_call_cancel_with_status (call, GRPC_STATUS_CANCELLED, "Cancelled", NULL);
+grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved) {
+ GPR_ASSERT(!reserved);
+ return grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, "Cancelled",
+ NULL);
}
-grpc_call_error
-grpc_call_cancel_with_status (grpc_call * c, grpc_status_code status, const char *description, void *reserved)
-{
+grpc_call_error grpc_call_cancel_with_status(grpc_call *c,
+ grpc_status_code status,
+ const char *description,
+ void *reserved) {
grpc_call_error r;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- GPR_ASSERT (reserved == NULL);
- lock (c);
- r = cancel_with_status (c, status, description);
- unlock (&exec_ctx, c);
- grpc_exec_ctx_finish (&exec_ctx);
+ GPR_ASSERT(reserved == NULL);
+ lock(c);
+ r = cancel_with_status(c, status, description);
+ unlock(&exec_ctx, c);
+ grpc_exec_ctx_finish(&exec_ctx);
return r;
}
-static grpc_call_error
-cancel_with_status (grpc_call * c, grpc_status_code status, const char *description)
-{
- grpc_mdstr *details = description ? grpc_mdstr_from_string (c->metadata_context, description, 0) : NULL;
+static grpc_call_error cancel_with_status(grpc_call *c, grpc_status_code status,
+ const char *description) {
+ grpc_mdstr *details =
+ description ? grpc_mdstr_from_string(c->metadata_context, description, 0)
+ : NULL;
- GPR_ASSERT (status != GRPC_STATUS_OK);
+ GPR_ASSERT(status != GRPC_STATUS_OK);
- set_status_code (c, STATUS_FROM_API_OVERRIDE, (gpr_uint32) status);
- set_status_details (c, STATUS_FROM_API_OVERRIDE, details);
+ set_status_code(c, STATUS_FROM_API_OVERRIDE, (gpr_uint32)status);
+ set_status_details(c, STATUS_FROM_API_OVERRIDE, details);
c->cancel_with_status = status;
return GRPC_CALL_OK;
}
-static void
-finished_loose_op (grpc_exec_ctx * exec_ctx, void *call, int success_ignored)
-{
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "loose-op");
+static void finished_loose_op(grpc_exec_ctx *exec_ctx, void *call,
+ int success_ignored) {
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "loose-op");
}
-typedef struct
-{
+typedef struct {
grpc_call *call;
grpc_closure closure;
} finished_loose_op_allocated_args;
-static void
-finished_loose_op_allocated (grpc_exec_ctx * exec_ctx, void *alloc, int success)
-{
+static void finished_loose_op_allocated(grpc_exec_ctx *exec_ctx, void *alloc,
+ int success) {
finished_loose_op_allocated_args *args = alloc;
- finished_loose_op (exec_ctx, args->call, success);
- gpr_free (args);
+ finished_loose_op(exec_ctx, args->call, success);
+ gpr_free(args);
}
-static void
-execute_op (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_transport_stream_op * op)
-{
+static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ grpc_transport_stream_op *op) {
grpc_call_element *elem;
- GPR_ASSERT (op->on_consumed == NULL);
- if (op->cancel_with_status != GRPC_STATUS_OK || op->bind_pollset)
- {
- GRPC_CALL_INTERNAL_REF (call, "loose-op");
- if (op->bind_pollset)
- {
- op->on_consumed = &call->on_done_bind;
- }
- else
- {
- finished_loose_op_allocated_args *args = gpr_malloc (sizeof (*args));
- args->call = call;
- grpc_closure_init (&args->closure, finished_loose_op_allocated, args);
- op->on_consumed = &args->closure;
- }
+ GPR_ASSERT(op->on_consumed == NULL);
+ if (op->cancel_with_status != GRPC_STATUS_OK || op->bind_pollset) {
+ GRPC_CALL_INTERNAL_REF(call, "loose-op");
+ if (op->bind_pollset) {
+ op->on_consumed = &call->on_done_bind;
+ } else {
+ finished_loose_op_allocated_args *args = gpr_malloc(sizeof(*args));
+ args->call = call;
+ grpc_closure_init(&args->closure, finished_loose_op_allocated, args);
+ op->on_consumed = &args->closure;
}
+ }
- elem = CALL_ELEM_FROM_CALL (call, 0);
+ elem = CALL_ELEM_FROM_CALL(call, 0);
op->context = call->context;
- elem->filter->start_transport_stream_op (exec_ctx, elem, op);
+ elem->filter->start_transport_stream_op(exec_ctx, elem, op);
}
-char *
-grpc_call_get_peer (grpc_call * call)
-{
- grpc_call_element *elem = CALL_ELEM_FROM_CALL (call, 0);
+char *grpc_call_get_peer(grpc_call *call) {
+ grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0);
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- char *result = elem->filter->get_peer (&exec_ctx, elem);
- grpc_exec_ctx_finish (&exec_ctx);
+ char *result = elem->filter->get_peer(&exec_ctx, elem);
+ grpc_exec_ctx_finish(&exec_ctx);
return result;
}
-grpc_call *
-grpc_call_from_top_element (grpc_call_element * elem)
-{
- return CALL_FROM_TOP_ELEM (elem);
+grpc_call *grpc_call_from_top_element(grpc_call_element *elem) {
+ return CALL_FROM_TOP_ELEM(elem);
}
-static void
-call_alarm (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void call_alarm(grpc_exec_ctx *exec_ctx, void *arg, int success) {
grpc_call *call = arg;
- lock (call);
+ lock(call);
call->have_alarm = 0;
- if (success)
- {
- cancel_with_status (call, GRPC_STATUS_DEADLINE_EXCEEDED, "Deadline Exceeded");
- }
- finish_read_ops (call);
- unlock (exec_ctx, call);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "alarm");
+ if (success) {
+ cancel_with_status(call, GRPC_STATUS_DEADLINE_EXCEEDED,
+ "Deadline Exceeded");
+ }
+ finish_read_ops(call);
+ unlock(exec_ctx, call);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "alarm");
}
-static void
-set_deadline_alarm (grpc_exec_ctx * exec_ctx, grpc_call * call, gpr_timespec deadline)
-{
- if (call->have_alarm)
- {
- gpr_log (GPR_ERROR, "Attempt to set deadline alarm twice");
- assert (0);
- return;
- }
- GRPC_CALL_INTERNAL_REF (call, "alarm");
+static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ gpr_timespec deadline) {
+ if (call->have_alarm) {
+ gpr_log(GPR_ERROR, "Attempt to set deadline alarm twice");
+ assert(0);
+ return;
+ }
+ GRPC_CALL_INTERNAL_REF(call, "alarm");
call->have_alarm = 1;
- call->send_deadline = gpr_convert_clock_type (deadline, GPR_CLOCK_MONOTONIC);
- grpc_alarm_init (exec_ctx, &call->alarm, call->send_deadline, call_alarm, call, gpr_now (GPR_CLOCK_MONOTONIC));
+ call->send_deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
+ grpc_alarm_init(exec_ctx, &call->alarm, call->send_deadline, call_alarm, call,
+ gpr_now(GPR_CLOCK_MONOTONIC));
}
/* we offset status by a small amount when storing it into transport metadata
as metadata cannot store a 0 value (which is used as OK for grpc_status_codes
*/
#define STATUS_OFFSET 1
-static void
-destroy_status (void *ignored)
-{
-}
+static void destroy_status(void *ignored) {}
-static gpr_uint32
-decode_status (grpc_mdelem * md)
-{
+static gpr_uint32 decode_status(grpc_mdelem *md) {
gpr_uint32 status;
- void *user_data = grpc_mdelem_get_user_data (md, destroy_status);
- if (user_data)
- {
- status = ((gpr_uint32) (gpr_intptr) user_data) - STATUS_OFFSET;
- }
- else
- {
- if (!gpr_parse_bytes_to_uint32 (grpc_mdstr_as_c_string (md->value), GPR_SLICE_LENGTH (md->value->slice), &status))
- {
- status = GRPC_STATUS_UNKNOWN; /* could not parse status code */
- }
- grpc_mdelem_set_user_data (md, destroy_status, (void *) (gpr_intptr) (status + STATUS_OFFSET));
- }
+ void *user_data = grpc_mdelem_get_user_data(md, destroy_status);
+ if (user_data) {
+ status = ((gpr_uint32)(gpr_intptr)user_data) - STATUS_OFFSET;
+ } else {
+ if (!gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value),
+ GPR_SLICE_LENGTH(md->value->slice),
+ &status)) {
+ status = GRPC_STATUS_UNKNOWN; /* could not parse status code */
+ }
+ grpc_mdelem_set_user_data(md, destroy_status,
+ (void *)(gpr_intptr)(status + STATUS_OFFSET));
+ }
return status;
}
/* just as for status above, we need to offset: metadata userdata can't hold a
* zero (null), which in this case is used to signal no compression */
#define COMPRESS_OFFSET 1
-static void
-destroy_compression (void *ignored)
-{
-}
+static void destroy_compression(void *ignored) {}
-static gpr_uint32
-decode_compression (grpc_mdelem * md)
-{
+static gpr_uint32 decode_compression(grpc_mdelem *md) {
grpc_compression_algorithm algorithm;
- void *user_data = grpc_mdelem_get_user_data (md, destroy_compression);
- if (user_data)
- {
- algorithm = ((grpc_compression_algorithm) (gpr_intptr) user_data) - COMPRESS_OFFSET;
- }
- else
- {
- const char *md_c_str = grpc_mdstr_as_c_string (md->value);
- if (!grpc_compression_algorithm_parse (md_c_str, strlen (md_c_str), &algorithm))
- {
- gpr_log (GPR_ERROR, "Invalid compression algorithm: '%s'", md_c_str);
- assert (0);
- }
- grpc_mdelem_set_user_data (md, destroy_compression, (void *) (gpr_intptr) (algorithm + COMPRESS_OFFSET));
- }
+ void *user_data = grpc_mdelem_get_user_data(md, destroy_compression);
+ if (user_data) {
+ algorithm =
+ ((grpc_compression_algorithm)(gpr_intptr)user_data) - COMPRESS_OFFSET;
+ } else {
+ const char *md_c_str = grpc_mdstr_as_c_string(md->value);
+ if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str),
+ &algorithm)) {
+ gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'", md_c_str);
+ assert(0);
+ }
+ grpc_mdelem_set_user_data(
+ md, destroy_compression,
+ (void *)(gpr_intptr)(algorithm + COMPRESS_OFFSET));
+ }
return algorithm;
}
-static void
-recv_metadata (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_metadata_batch * md)
-{
+static void recv_metadata(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ grpc_metadata_batch *md) {
grpc_linked_mdelem *l;
grpc_metadata_array *dest;
grpc_metadata *mdusr;
@@ -1626,397 +1478,355 @@ recv_metadata (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_metadata_batch *
grpc_mdctx *mdctx = call->metadata_context;
is_trailing = call->read_state >= READ_STATE_GOT_INITIAL_METADATA;
- for (l = md->list.head; l != NULL; l = l->next)
- {
- grpc_mdelem *md = l->md;
- grpc_mdstr *key = md->key;
- if (key == grpc_channel_get_status_string (call->channel))
- {
- set_status_code (call, STATUS_FROM_WIRE, decode_status (md));
- }
- else if (key == grpc_channel_get_message_string (call->channel))
- {
- set_status_details (call, STATUS_FROM_WIRE, GRPC_MDSTR_REF (md->value));
- }
- else if (key == grpc_channel_get_compression_algorithm_string (call->channel))
- {
- set_compression_algorithm (call, decode_compression (md));
- }
- else if (key == grpc_channel_get_encodings_accepted_by_peer_string (call->channel))
- {
- set_encodings_accepted_by_peer (call, md->value->slice);
- }
- else
- {
- dest = &call->buffered_metadata[is_trailing];
- if (dest->count == dest->capacity)
- {
- dest->capacity = GPR_MAX (dest->capacity + 8, dest->capacity * 2);
- dest->metadata = gpr_realloc (dest->metadata, sizeof (grpc_metadata) * dest->capacity);
- }
- mdusr = &dest->metadata[dest->count++];
- mdusr->key = grpc_mdstr_as_c_string (md->key);
- mdusr->value = grpc_mdstr_as_c_string (md->value);
- mdusr->value_length = GPR_SLICE_LENGTH (md->value->slice);
- if (call->owned_metadata_count == call->owned_metadata_capacity)
- {
- call->owned_metadata_capacity = GPR_MAX (call->owned_metadata_capacity + 8, call->owned_metadata_capacity * 2);
- call->owned_metadata = gpr_realloc (call->owned_metadata, sizeof (grpc_mdelem *) * call->owned_metadata_capacity);
- }
- call->owned_metadata[call->owned_metadata_count++] = md;
- l->md = 0;
- }
- }
- if (gpr_time_cmp (md->deadline, gpr_inf_future (md->deadline.clock_type)) != 0 && !call->is_client)
- {
- set_deadline_alarm (exec_ctx, call, md->deadline);
- }
- if (!is_trailing)
- {
- call->read_state = READ_STATE_GOT_INITIAL_METADATA;
- }
-
- grpc_mdctx_lock (mdctx);
- for (l = md->list.head; l; l = l->next)
- {
- if (l->md)
- GRPC_MDCTX_LOCKED_MDELEM_UNREF (mdctx, l->md);
- }
- for (l = md->garbage.head; l; l = l->next)
- {
- GRPC_MDCTX_LOCKED_MDELEM_UNREF (mdctx, l->md);
- }
- grpc_mdctx_unlock (mdctx);
+ for (l = md->list.head; l != NULL; l = l->next) {
+ grpc_mdelem *md = l->md;
+ grpc_mdstr *key = md->key;
+ if (key == grpc_channel_get_status_string(call->channel)) {
+ set_status_code(call, STATUS_FROM_WIRE, decode_status(md));
+ } else if (key == grpc_channel_get_message_string(call->channel)) {
+ set_status_details(call, STATUS_FROM_WIRE, GRPC_MDSTR_REF(md->value));
+ } else if (key ==
+ grpc_channel_get_compression_algorithm_string(call->channel)) {
+ set_compression_algorithm(call, decode_compression(md));
+ } else if (key == grpc_channel_get_encodings_accepted_by_peer_string(
+ call->channel)) {
+ set_encodings_accepted_by_peer(call, md->value->slice);
+ } else {
+ dest = &call->buffered_metadata[is_trailing];
+ if (dest->count == dest->capacity) {
+ dest->capacity = GPR_MAX(dest->capacity + 8, dest->capacity * 2);
+ dest->metadata =
+ gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity);
+ }
+ mdusr = &dest->metadata[dest->count++];
+ mdusr->key = grpc_mdstr_as_c_string(md->key);
+ mdusr->value = grpc_mdstr_as_c_string(md->value);
+ mdusr->value_length = GPR_SLICE_LENGTH(md->value->slice);
+ if (call->owned_metadata_count == call->owned_metadata_capacity) {
+ call->owned_metadata_capacity =
+ GPR_MAX(call->owned_metadata_capacity + 8,
+ call->owned_metadata_capacity * 2);
+ call->owned_metadata =
+ gpr_realloc(call->owned_metadata,
+ sizeof(grpc_mdelem *) * call->owned_metadata_capacity);
+ }
+ call->owned_metadata[call->owned_metadata_count++] = md;
+ l->md = 0;
+ }
+ }
+ if (gpr_time_cmp(md->deadline, gpr_inf_future(md->deadline.clock_type)) !=
+ 0 &&
+ !call->is_client) {
+ set_deadline_alarm(exec_ctx, call, md->deadline);
+ }
+ if (!is_trailing) {
+ call->read_state = READ_STATE_GOT_INITIAL_METADATA;
+ }
+
+ grpc_mdctx_lock(mdctx);
+ for (l = md->list.head; l; l = l->next) {
+ if (l->md) GRPC_MDCTX_LOCKED_MDELEM_UNREF(mdctx, l->md);
+ }
+ for (l = md->garbage.head; l; l = l->next) {
+ GRPC_MDCTX_LOCKED_MDELEM_UNREF(mdctx, l->md);
+ }
+ grpc_mdctx_unlock(mdctx);
}
-grpc_call_stack *
-grpc_call_get_call_stack (grpc_call * call)
-{
- return CALL_STACK_FROM_CALL (call);
+grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) {
+ return CALL_STACK_FROM_CALL(call);
}
/*
* BATCH API IMPLEMENTATION
*/
-static void
-set_status_value_directly (grpc_status_code status, void *dest)
-{
- *(grpc_status_code *) dest = status;
+static void set_status_value_directly(grpc_status_code status, void *dest) {
+ *(grpc_status_code *)dest = status;
}
-static void
-set_cancelled_value (grpc_status_code status, void *dest)
-{
- *(grpc_status_code *) dest = (status != GRPC_STATUS_OK);
+static void set_cancelled_value(grpc_status_code status, void *dest) {
+ *(grpc_status_code *)dest = (status != GRPC_STATUS_OK);
}
-static void
-finish_batch (grpc_exec_ctx * exec_ctx, grpc_call * call, int success, void *tag)
-{
- grpc_cq_end_op (exec_ctx,call->cq, tag, success, done_completion, call, allocate_completion ( call));
+static void finish_batch(grpc_exec_ctx *exec_ctx, grpc_call *call, int success,
+ void *tag) {
+ grpc_cq_end_op(exec_ctx, call->cq, tag, success, done_completion, call,
+ allocate_completion(call));
}
-static void
-finish_batch_with_close (grpc_exec_ctx * exec_ctx, grpc_call * call, int success, void *tag)
-{
- grpc_cq_end_op (exec_ctx,call->cq, tag, 1, done_completion, call, allocate_completion ( call));
+static void finish_batch_with_close(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ int success, void *tag) {
+ grpc_cq_end_op(exec_ctx, call->cq, tag, 1, done_completion, call,
+ allocate_completion(call));
}
-static int
-are_write_flags_valid (gpr_uint32 flags)
-{
+static int are_write_flags_valid(gpr_uint32 flags) {
/* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
- const gpr_uint32 allowed_write_positions = (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK);
+ const gpr_uint32 allowed_write_positions =
+ (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK);
const gpr_uint32 invalid_positions = ~allowed_write_positions;
return !(flags & invalid_positions);
}
-grpc_call_error
-grpc_call_start_batch (grpc_call * call, const grpc_op * ops, size_t nops, void *tag, void *reserved)
-{
+grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
+ size_t nops, void *tag, void *reserved) {
grpc_ioreq reqs[GRPC_IOREQ_OP_COUNT];
size_t in;
size_t out;
const grpc_op *op;
grpc_ioreq *req;
- void (*finish_func) (grpc_exec_ctx *, grpc_call *, int, void *) = finish_batch;
+ void (*finish_func)(grpc_exec_ctx *, grpc_call *, int, void *) = finish_batch;
grpc_call_error error;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- if (reserved != NULL)
- {
- error = GRPC_CALL_ERROR;
- goto done;
- }
+ if (reserved != NULL) {
+ error = GRPC_CALL_ERROR;
+ goto done;
+ }
- GRPC_CALL_LOG_BATCH (GPR_INFO, call, ops, nops, tag);
+ GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, tag);
- if (nops == 0)
- {
- grpc_cq_begin_op (call->cq);
- GRPC_CALL_INTERNAL_REF (call, "completion");
- grpc_cq_end_op (&exec_ctx, call->cq, tag, 1, done_completion, call, allocate_completion (call));
- error = GRPC_CALL_OK;
- goto done;
- }
+ if (nops == 0) {
+ grpc_cq_begin_op(call->cq);
+ GRPC_CALL_INTERNAL_REF(call, "completion");
+ grpc_cq_end_op(&exec_ctx, call->cq, tag, 1, done_completion, call,
+ allocate_completion(call));
+ error = GRPC_CALL_OK;
+ goto done;
+ }
/* rewrite batch ops into ioreq ops */
- for (in = 0, out = 0; in < nops; in++)
- {
- op = &ops[in];
- if (op->reserved != NULL)
- {
- error = GRPC_CALL_ERROR;
- goto done;
- }
- switch (op->op)
- {
- case GRPC_OP_SEND_INITIAL_METADATA:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_SEND_INITIAL_METADATA;
- req->data.send_metadata.count = op->data.send_initial_metadata.count;
- req->data.send_metadata.metadata = op->data.send_initial_metadata.metadata;
- req->flags = op->flags;
- break;
- case GRPC_OP_SEND_MESSAGE:
- if (!are_write_flags_valid (op->flags))
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- if (op->data.send_message == NULL)
- {
- error = GRPC_CALL_ERROR_INVALID_MESSAGE;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_SEND_MESSAGE;
- req->data.send_message = op->data.send_message;
- req->flags = op->flags;
- break;
- case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- if (!call->is_client)
- {
- error = GRPC_CALL_ERROR_NOT_ON_SERVER;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_SEND_CLOSE;
- req->flags = op->flags;
- break;
- case GRPC_OP_SEND_STATUS_FROM_SERVER:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- if (call->is_client)
- {
- error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_SEND_TRAILING_METADATA;
- req->flags = op->flags;
- req->data.send_metadata.count = op->data.send_status_from_server.trailing_metadata_count;
- req->data.send_metadata.metadata = op->data.send_status_from_server.trailing_metadata;
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_SEND_STATUS;
- req->data.send_status.code = op->data.send_status_from_server.status;
- req->data.send_status.details = op->data.send_status_from_server.status_details != NULL ? grpc_mdstr_from_string (call->metadata_context, op->data.send_status_from_server.status_details, 0) : NULL;
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_SEND_CLOSE;
- break;
- case GRPC_OP_RECV_INITIAL_METADATA:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- if (!call->is_client)
- {
- error = GRPC_CALL_ERROR_NOT_ON_SERVER;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_INITIAL_METADATA;
- req->data.recv_metadata = op->data.recv_initial_metadata;
- req->data.recv_metadata->count = 0;
- req->flags = op->flags;
- break;
- case GRPC_OP_RECV_MESSAGE:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_MESSAGE;
- req->data.recv_message = op->data.recv_message;
- req->flags = op->flags;
- break;
- case GRPC_OP_RECV_STATUS_ON_CLIENT:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- if (!call->is_client)
- {
- error = GRPC_CALL_ERROR_NOT_ON_SERVER;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_STATUS;
- req->flags = op->flags;
- req->data.recv_status.set_value = set_status_value_directly;
- req->data.recv_status.user_data = op->data.recv_status_on_client.status;
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_STATUS_DETAILS;
- req->data.recv_status_details.details = op->data.recv_status_on_client.status_details;
- req->data.recv_status_details.details_capacity = op->data.recv_status_on_client.status_details_capacity;
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_TRAILING_METADATA;
- req->data.recv_metadata = op->data.recv_status_on_client.trailing_metadata;
- req->data.recv_metadata->count = 0;
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_CLOSE;
- finish_func = finish_batch_with_close;
- break;
- case GRPC_OP_RECV_CLOSE_ON_SERVER:
- /* Flag validation: currently allow no flags */
- if (op->flags != 0)
- {
- error = GRPC_CALL_ERROR_INVALID_FLAGS;
- goto done;
- }
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_STATUS;
- req->flags = op->flags;
- req->data.recv_status.set_value = set_cancelled_value;
- req->data.recv_status.user_data = op->data.recv_close_on_server.cancelled;
- req = &reqs[out++];
- if (out > GRPC_IOREQ_OP_COUNT)
- {
- error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
- goto done;
- }
- req->op = GRPC_IOREQ_RECV_CLOSE;
- finish_func = finish_batch_with_close;
- break;
- }
+ for (in = 0, out = 0; in < nops; in++) {
+ op = &ops[in];
+ if (op->reserved != NULL) {
+ error = GRPC_CALL_ERROR;
+ goto done;
}
-
- GRPC_CALL_INTERNAL_REF (call, "completion");
- grpc_cq_begin_op (call->cq);
-
- error = grpc_call_start_ioreq_and_call_back (&exec_ctx, call, reqs, out, finish_func, tag);
+ switch (op->op) {
+ case GRPC_OP_SEND_INITIAL_METADATA:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_SEND_INITIAL_METADATA;
+ req->data.send_metadata.count = op->data.send_initial_metadata.count;
+ req->data.send_metadata.metadata =
+ op->data.send_initial_metadata.metadata;
+ req->flags = op->flags;
+ break;
+ case GRPC_OP_SEND_MESSAGE:
+ if (!are_write_flags_valid(op->flags)) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ if (op->data.send_message == NULL) {
+ error = GRPC_CALL_ERROR_INVALID_MESSAGE;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_SEND_MESSAGE;
+ req->data.send_message = op->data.send_message;
+ req->flags = op->flags;
+ break;
+ case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ if (!call->is_client) {
+ error = GRPC_CALL_ERROR_NOT_ON_SERVER;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_SEND_CLOSE;
+ req->flags = op->flags;
+ break;
+ case GRPC_OP_SEND_STATUS_FROM_SERVER:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ if (call->is_client) {
+ error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_SEND_TRAILING_METADATA;
+ req->flags = op->flags;
+ req->data.send_metadata.count =
+ op->data.send_status_from_server.trailing_metadata_count;
+ req->data.send_metadata.metadata =
+ op->data.send_status_from_server.trailing_metadata;
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_SEND_STATUS;
+ req->data.send_status.code = op->data.send_status_from_server.status;
+ req->data.send_status.details =
+ op->data.send_status_from_server.status_details != NULL
+ ? grpc_mdstr_from_string(
+ call->metadata_context,
+ op->data.send_status_from_server.status_details, 0)
+ : NULL;
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_SEND_CLOSE;
+ break;
+ case GRPC_OP_RECV_INITIAL_METADATA:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ if (!call->is_client) {
+ error = GRPC_CALL_ERROR_NOT_ON_SERVER;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_INITIAL_METADATA;
+ req->data.recv_metadata = op->data.recv_initial_metadata;
+ req->data.recv_metadata->count = 0;
+ req->flags = op->flags;
+ break;
+ case GRPC_OP_RECV_MESSAGE:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_MESSAGE;
+ req->data.recv_message = op->data.recv_message;
+ req->flags = op->flags;
+ break;
+ case GRPC_OP_RECV_STATUS_ON_CLIENT:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ if (!call->is_client) {
+ error = GRPC_CALL_ERROR_NOT_ON_SERVER;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_STATUS;
+ req->flags = op->flags;
+ req->data.recv_status.set_value = set_status_value_directly;
+ req->data.recv_status.user_data = op->data.recv_status_on_client.status;
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_STATUS_DETAILS;
+ req->data.recv_status_details.details =
+ op->data.recv_status_on_client.status_details;
+ req->data.recv_status_details.details_capacity =
+ op->data.recv_status_on_client.status_details_capacity;
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_TRAILING_METADATA;
+ req->data.recv_metadata =
+ op->data.recv_status_on_client.trailing_metadata;
+ req->data.recv_metadata->count = 0;
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_CLOSE;
+ finish_func = finish_batch_with_close;
+ break;
+ case GRPC_OP_RECV_CLOSE_ON_SERVER:
+ /* Flag validation: currently allow no flags */
+ if (op->flags != 0) {
+ error = GRPC_CALL_ERROR_INVALID_FLAGS;
+ goto done;
+ }
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_STATUS;
+ req->flags = op->flags;
+ req->data.recv_status.set_value = set_cancelled_value;
+ req->data.recv_status.user_data =
+ op->data.recv_close_on_server.cancelled;
+ req = &reqs[out++];
+ if (out > GRPC_IOREQ_OP_COUNT) {
+ error = GRPC_CALL_ERROR_BATCH_TOO_BIG;
+ goto done;
+ }
+ req->op = GRPC_IOREQ_RECV_CLOSE;
+ finish_func = finish_batch_with_close;
+ break;
+ }
+ }
+
+ GRPC_CALL_INTERNAL_REF(call, "completion");
+ grpc_cq_begin_op(call->cq);
+
+ error = grpc_call_start_ioreq_and_call_back(&exec_ctx, call, reqs, out,
+ finish_func, tag);
done:
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
return error;
}
-void
-grpc_call_context_set (grpc_call * call, grpc_context_index elem, void *value, void (*destroy) (void *value))
-{
- if (call->context[elem].destroy)
- {
- call->context[elem].destroy (call->context[elem].value);
- }
+void grpc_call_context_set(grpc_call *call, grpc_context_index elem,
+ void *value, void (*destroy)(void *value)) {
+ if (call->context[elem].destroy) {
+ call->context[elem].destroy(call->context[elem].value);
+ }
call->context[elem].value = value;
call->context[elem].destroy = destroy;
}
-void *
-grpc_call_context_get (grpc_call * call, grpc_context_index elem)
-{
+void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) {
return call->context[elem].value;
}
-gpr_uint8
-grpc_call_is_client (grpc_call * call)
-{
- return call->is_client;
-}
+gpr_uint8 grpc_call_is_client(grpc_call *call) { return call->is_client; }
diff --git a/src/core/surface/call.h b/src/core/surface/call.h
index b1f3be8521..7ac0c92ab7 100644
--- a/src/core/surface/call.h
+++ b/src/core/surface/call.h
@@ -39,104 +39,120 @@
#include <grpc/grpc.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Primitive operation types - grpc_op's get rewritten into these */
- typedef enum
- {
- GRPC_IOREQ_RECV_INITIAL_METADATA,
- GRPC_IOREQ_RECV_MESSAGE,
- GRPC_IOREQ_RECV_TRAILING_METADATA,
- GRPC_IOREQ_RECV_STATUS,
- GRPC_IOREQ_RECV_STATUS_DETAILS,
- GRPC_IOREQ_RECV_CLOSE,
- GRPC_IOREQ_SEND_INITIAL_METADATA,
- GRPC_IOREQ_SEND_MESSAGE,
- GRPC_IOREQ_SEND_TRAILING_METADATA,
- GRPC_IOREQ_SEND_STATUS,
- GRPC_IOREQ_SEND_CLOSE,
- GRPC_IOREQ_OP_COUNT
- } grpc_ioreq_op;
-
- typedef union
- {
- grpc_metadata_array *recv_metadata;
- grpc_byte_buffer **recv_message;
- struct
- {
- void (*set_value) (grpc_status_code status, void *user_data);
- void *user_data;
- } recv_status;
- struct
- {
- char **details;
- size_t *details_capacity;
- } recv_status_details;
- struct
- {
- size_t count;
- grpc_metadata *metadata;
- } send_metadata;
- grpc_byte_buffer *send_message;
- struct
- {
- grpc_status_code code;
- grpc_mdstr *details;
- } send_status;
- } grpc_ioreq_data;
-
- typedef struct
- {
- grpc_ioreq_op op;
- gpr_uint32 flags;
- /**< A copy of the write flags from grpc_op */
- grpc_ioreq_data data;
- } grpc_ioreq;
-
- typedef void (*grpc_ioreq_completion_func) (grpc_exec_ctx * exec_ctx, grpc_call * call, int success, void *user_data);
-
- grpc_call *grpc_call_create (grpc_channel * channel, grpc_call * parent_call, gpr_uint32 propagation_mask, grpc_completion_queue * cq, const void *server_transport_data, grpc_mdelem ** add_initial_metadata, size_t add_initial_metadata_count, gpr_timespec send_deadline);
-
- void grpc_call_set_completion_queue (grpc_exec_ctx * exec_ctx, grpc_call * call, grpc_completion_queue * cq);
- grpc_completion_queue *grpc_call_get_completion_queue (grpc_call * call);
+typedef enum {
+ GRPC_IOREQ_RECV_INITIAL_METADATA,
+ GRPC_IOREQ_RECV_MESSAGE,
+ GRPC_IOREQ_RECV_TRAILING_METADATA,
+ GRPC_IOREQ_RECV_STATUS,
+ GRPC_IOREQ_RECV_STATUS_DETAILS,
+ GRPC_IOREQ_RECV_CLOSE,
+ GRPC_IOREQ_SEND_INITIAL_METADATA,
+ GRPC_IOREQ_SEND_MESSAGE,
+ GRPC_IOREQ_SEND_TRAILING_METADATA,
+ GRPC_IOREQ_SEND_STATUS,
+ GRPC_IOREQ_SEND_CLOSE,
+ GRPC_IOREQ_OP_COUNT
+} grpc_ioreq_op;
+
+typedef union {
+ grpc_metadata_array *recv_metadata;
+ grpc_byte_buffer **recv_message;
+ struct {
+ void (*set_value)(grpc_status_code status, void *user_data);
+ void *user_data;
+ } recv_status;
+ struct {
+ char **details;
+ size_t *details_capacity;
+ } recv_status_details;
+ struct {
+ size_t count;
+ grpc_metadata *metadata;
+ } send_metadata;
+ grpc_byte_buffer *send_message;
+ struct {
+ grpc_status_code code;
+ grpc_mdstr *details;
+ } send_status;
+} grpc_ioreq_data;
+
+typedef struct {
+ grpc_ioreq_op op;
+ gpr_uint32 flags;
+ /**< A copy of the write flags from grpc_op */
+ grpc_ioreq_data data;
+} grpc_ioreq;
+
+typedef void (*grpc_ioreq_completion_func)(grpc_exec_ctx *exec_ctx,
+ grpc_call *call, int success,
+ void *user_data);
+
+grpc_call *grpc_call_create(grpc_channel *channel, grpc_call *parent_call,
+ gpr_uint32 propagation_mask,
+ grpc_completion_queue *cq,
+ const void *server_transport_data,
+ grpc_mdelem **add_initial_metadata,
+ size_t add_initial_metadata_count,
+ gpr_timespec send_deadline);
+
+void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ grpc_completion_queue *cq);
+grpc_completion_queue *grpc_call_get_completion_queue(grpc_call *call);
#ifdef GRPC_CALL_REF_COUNT_DEBUG
- void grpc_call_internal_ref (grpc_call * call, const char *reason);
- void grpc_call_internal_unref (grpc_exec_ctx * exec_ctx, grpc_call * call, const char *reason);
+void grpc_call_internal_ref(grpc_call *call, const char *reason);
+void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ const char *reason);
#define GRPC_CALL_INTERNAL_REF(call, reason) \
grpc_call_internal_ref(call, reason)
#define GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, reason) \
grpc_call_internal_unref(exec_ctx, call, reason)
#else
- void grpc_call_internal_ref (grpc_call * call);
- void grpc_call_internal_unref (grpc_exec_ctx * exec_ctx, grpc_call * call);
+void grpc_call_internal_ref(grpc_call *call);
+void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *call);
#define GRPC_CALL_INTERNAL_REF(call, reason) grpc_call_internal_ref(call)
#define GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, reason) \
grpc_call_internal_unref(exec_ctx, call)
#endif
- grpc_call_error grpc_call_start_ioreq_and_call_back (grpc_exec_ctx * exec_ctx, grpc_call * call, const grpc_ioreq * reqs, size_t nreqs, grpc_ioreq_completion_func on_complete, void *user_data);
+grpc_call_error grpc_call_start_ioreq_and_call_back(
+ grpc_exec_ctx *exec_ctx, grpc_call *call, const grpc_ioreq *reqs,
+ size_t nreqs, grpc_ioreq_completion_func on_complete, void *user_data);
- grpc_call_stack *grpc_call_get_call_stack (grpc_call * call);
+grpc_call_stack *grpc_call_get_call_stack(grpc_call *call);
/* Given the top call_element, get the call object. */
- grpc_call *grpc_call_from_top_element (grpc_call_element * surface_element);
+grpc_call *grpc_call_from_top_element(grpc_call_element *surface_element);
- extern int grpc_trace_batch;
+extern int grpc_trace_batch;
- void grpc_call_log_batch (char *file, int line, gpr_log_severity severity, grpc_call * call, const grpc_op * ops, size_t nops, void *tag);
+void grpc_call_log_batch(char *file, int line, gpr_log_severity severity,
+ grpc_call *call, const grpc_op *ops, size_t nops,
+ void *tag);
- void grpc_server_log_request_call (char *file, int line, gpr_log_severity severity, grpc_server * server, grpc_call ** call, grpc_call_details * details, grpc_metadata_array * initial_metadata, grpc_completion_queue * cq_bound_to_call, grpc_completion_queue * cq_for_notification, void *tag);
+void grpc_server_log_request_call(char *file, int line,
+ gpr_log_severity severity,
+ grpc_server *server, grpc_call **call,
+ grpc_call_details *details,
+ grpc_metadata_array *initial_metadata,
+ grpc_completion_queue *cq_bound_to_call,
+ grpc_completion_queue *cq_for_notification,
+ void *tag);
- void grpc_server_log_shutdown (char *file, int line, gpr_log_severity severity, grpc_server * server, grpc_completion_queue * cq, void *tag);
+void grpc_server_log_shutdown(char *file, int line, gpr_log_severity severity,
+ grpc_server *server, grpc_completion_queue *cq,
+ void *tag);
/* Set a context pointer.
No thread safety guarantees are made wrt this value. */
- void grpc_call_context_set (grpc_call * call, grpc_context_index elem, void *value, void (*destroy) (void *value));
+void grpc_call_context_set(grpc_call *call, grpc_context_index elem,
+ void *value, void (*destroy)(void *value));
/* Get a context pointer. */
- void *grpc_call_context_get (grpc_call * call, grpc_context_index elem);
+void *grpc_call_context_get(grpc_call *call, grpc_context_index elem);
#define GRPC_CALL_LOG_BATCH(sev, call, ops, nops, tag) \
if (grpc_trace_batch) grpc_call_log_batch(sev, call, ops, nops, tag)
@@ -151,20 +167,21 @@ extern "C"
#define GRPC_SERVER_LOG_SHUTDOWN(sev, server, cq, tag) \
if (grpc_trace_batch) grpc_server_log_shutdown(sev, server, cq, tag)
- gpr_uint8 grpc_call_is_client (grpc_call * call);
+gpr_uint8 grpc_call_is_client(grpc_call *call);
- grpc_compression_algorithm grpc_call_get_compression_algorithm (const grpc_call * call);
+grpc_compression_algorithm grpc_call_get_compression_algorithm(
+ const grpc_call *call);
- gpr_uint32 grpc_call_get_message_flags (const grpc_call * call);
+gpr_uint32 grpc_call_get_message_flags(const grpc_call *call);
/** Returns a bitset for the encodings (compression algorithms) supported by \a
* call's peer.
*
* To be indexed by grpc_compression_algorithm enum values. */
- gpr_uint32 grpc_call_get_encodings_accepted_by_peer (grpc_call * call);
+gpr_uint32 grpc_call_get_encodings_accepted_by_peer(grpc_call *call);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_SURFACE_CALL_H */
+#endif /* GRPC_INTERNAL_CORE_SURFACE_CALL_H */
diff --git a/src/core/surface/call_details.c b/src/core/surface/call_details.c
index ac9ba7ebe2..65d2d1da5b 100644
--- a/src/core/surface/call_details.c
+++ b/src/core/surface/call_details.c
@@ -36,15 +36,11 @@
#include <string.h>
-void
-grpc_call_details_init (grpc_call_details * cd)
-{
- memset (cd, 0, sizeof (*cd));
+void grpc_call_details_init(grpc_call_details* cd) {
+ memset(cd, 0, sizeof(*cd));
}
-void
-grpc_call_details_destroy (grpc_call_details * cd)
-{
- gpr_free (cd->method);
- gpr_free (cd->host);
+void grpc_call_details_destroy(grpc_call_details* cd) {
+ gpr_free(cd->method);
+ gpr_free(cd->host);
}
diff --git a/src/core/surface/call_log_batch.c b/src/core/surface/call_log_batch.c
index 3b0f922cda..5a3ef1e5f4 100644
--- a/src/core/surface/call_log_batch.c
+++ b/src/core/surface/call_log_batch.c
@@ -39,91 +39,109 @@
int grpc_trace_batch = 0;
-static void
-add_metadata (gpr_strvec * b, const grpc_metadata * md, size_t count)
-{
+static void add_metadata(gpr_strvec *b, const grpc_metadata *md, size_t count) {
size_t i;
- for (i = 0; i < count; i++)
- {
- gpr_strvec_add (b, gpr_strdup ("\nkey="));
- gpr_strvec_add (b, gpr_strdup (md[i].key));
+ for (i = 0; i < count; i++) {
+ gpr_strvec_add(b, gpr_strdup("\nkey="));
+ gpr_strvec_add(b, gpr_strdup(md[i].key));
- gpr_strvec_add (b, gpr_strdup (" value="));
- gpr_strvec_add (b, gpr_dump (md[i].value, md[i].value_length, GPR_DUMP_HEX | GPR_DUMP_ASCII));
- }
+ gpr_strvec_add(b, gpr_strdup(" value="));
+ gpr_strvec_add(b, gpr_dump(md[i].value, md[i].value_length,
+ GPR_DUMP_HEX | GPR_DUMP_ASCII));
+ }
}
-char *
-grpc_op_string (const grpc_op * op)
-{
+char *grpc_op_string(const grpc_op *op) {
char *tmp;
char *out;
gpr_strvec b;
- gpr_strvec_init (&b);
+ gpr_strvec_init(&b);
- switch (op->op)
- {
+ switch (op->op) {
case GRPC_OP_SEND_INITIAL_METADATA:
- gpr_strvec_add (&b, gpr_strdup ("SEND_INITIAL_METADATA"));
- add_metadata (&b, op->data.send_initial_metadata.metadata, op->data.send_initial_metadata.count);
+ gpr_strvec_add(&b, gpr_strdup("SEND_INITIAL_METADATA"));
+ add_metadata(&b, op->data.send_initial_metadata.metadata,
+ op->data.send_initial_metadata.count);
break;
case GRPC_OP_SEND_MESSAGE:
- gpr_asprintf (&tmp, "SEND_MESSAGE ptr=%p", op->data.send_message);
- gpr_strvec_add (&b, tmp);
+ gpr_asprintf(&tmp, "SEND_MESSAGE ptr=%p", op->data.send_message);
+ gpr_strvec_add(&b, tmp);
break;
case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
- gpr_strvec_add (&b, gpr_strdup ("SEND_CLOSE_FROM_CLIENT"));
+ gpr_strvec_add(&b, gpr_strdup("SEND_CLOSE_FROM_CLIENT"));
break;
case GRPC_OP_SEND_STATUS_FROM_SERVER:
- gpr_asprintf (&tmp, "SEND_STATUS_FROM_SERVER status=%d details=%s", op->data.send_status_from_server.status, op->data.send_status_from_server.status_details);
- gpr_strvec_add (&b, tmp);
- add_metadata (&b, op->data.send_status_from_server.trailing_metadata, op->data.send_status_from_server.trailing_metadata_count);
+ gpr_asprintf(&tmp, "SEND_STATUS_FROM_SERVER status=%d details=%s",
+ op->data.send_status_from_server.status,
+ op->data.send_status_from_server.status_details);
+ gpr_strvec_add(&b, tmp);
+ add_metadata(&b, op->data.send_status_from_server.trailing_metadata,
+ op->data.send_status_from_server.trailing_metadata_count);
break;
case GRPC_OP_RECV_INITIAL_METADATA:
- gpr_asprintf (&tmp, "RECV_INITIAL_METADATA ptr=%p", op->data.recv_initial_metadata);
- gpr_strvec_add (&b, tmp);
+ gpr_asprintf(&tmp, "RECV_INITIAL_METADATA ptr=%p",
+ op->data.recv_initial_metadata);
+ gpr_strvec_add(&b, tmp);
break;
case GRPC_OP_RECV_MESSAGE:
- gpr_asprintf (&tmp, "RECV_MESSAGE ptr=%p", op->data.recv_message);
- gpr_strvec_add (&b, tmp);
+ gpr_asprintf(&tmp, "RECV_MESSAGE ptr=%p", op->data.recv_message);
+ gpr_strvec_add(&b, tmp);
break;
case GRPC_OP_RECV_STATUS_ON_CLIENT:
- gpr_asprintf (&tmp, "RECV_STATUS_ON_CLIENT metadata=%p status=%p details=%p", op->data.recv_status_on_client.trailing_metadata, op->data.recv_status_on_client.status, op->data.recv_status_on_client.status_details);
- gpr_strvec_add (&b, tmp);
+ gpr_asprintf(&tmp,
+ "RECV_STATUS_ON_CLIENT metadata=%p status=%p details=%p",
+ op->data.recv_status_on_client.trailing_metadata,
+ op->data.recv_status_on_client.status,
+ op->data.recv_status_on_client.status_details);
+ gpr_strvec_add(&b, tmp);
break;
case GRPC_OP_RECV_CLOSE_ON_SERVER:
- gpr_asprintf (&tmp, "RECV_CLOSE_ON_SERVER cancelled=%p", op->data.recv_close_on_server.cancelled);
- gpr_strvec_add (&b, tmp);
- }
- out = gpr_strvec_flatten (&b, NULL);
- gpr_strvec_destroy (&b);
+ gpr_asprintf(&tmp, "RECV_CLOSE_ON_SERVER cancelled=%p",
+ op->data.recv_close_on_server.cancelled);
+ gpr_strvec_add(&b, tmp);
+ }
+ out = gpr_strvec_flatten(&b, NULL);
+ gpr_strvec_destroy(&b);
return out;
}
-void
-grpc_call_log_batch (char *file, int line, gpr_log_severity severity, grpc_call * call, const grpc_op * ops, size_t nops, void *tag)
-{
+void grpc_call_log_batch(char *file, int line, gpr_log_severity severity,
+ grpc_call *call, const grpc_op *ops, size_t nops,
+ void *tag) {
char *tmp;
size_t i;
- gpr_log (file, line, severity, "grpc_call_start_batch(call=%p, ops=%p, nops=%d, tag=%p)", call, ops, nops, tag);
- for (i = 0; i < nops; i++)
- {
- tmp = grpc_op_string (&ops[i]);
- gpr_log (file, line, severity, "ops[%d]: %s", i, tmp);
- gpr_free (tmp);
- }
+ gpr_log(file, line, severity,
+ "grpc_call_start_batch(call=%p, ops=%p, nops=%d, tag=%p)", call, ops,
+ nops, tag);
+ for (i = 0; i < nops; i++) {
+ tmp = grpc_op_string(&ops[i]);
+ gpr_log(file, line, severity, "ops[%d]: %s", i, tmp);
+ gpr_free(tmp);
+ }
}
-void
-grpc_server_log_request_call (char *file, int line, gpr_log_severity severity, grpc_server * server, grpc_call ** call, grpc_call_details * details, grpc_metadata_array * initial_metadata, grpc_completion_queue * cq_bound_to_call, grpc_completion_queue * cq_for_notification, void *tag)
-{
- gpr_log (file, line, severity, "grpc_server_request_call(server=%p, call=%p, details=%p, " "initial_metadata=%p, cq_bound_to_call=%p, cq_for_notification=%p, " "tag=%p)", server, call, details, initial_metadata, cq_bound_to_call, cq_for_notification, tag);
+void grpc_server_log_request_call(char *file, int line,
+ gpr_log_severity severity,
+ grpc_server *server, grpc_call **call,
+ grpc_call_details *details,
+ grpc_metadata_array *initial_metadata,
+ grpc_completion_queue *cq_bound_to_call,
+ grpc_completion_queue *cq_for_notification,
+ void *tag) {
+ gpr_log(file, line, severity,
+ "grpc_server_request_call(server=%p, call=%p, details=%p, "
+ "initial_metadata=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
+ "tag=%p)",
+ server, call, details, initial_metadata, cq_bound_to_call,
+ cq_for_notification, tag);
}
-void
-grpc_server_log_shutdown (char *file, int line, gpr_log_severity severity, grpc_server * server, grpc_completion_queue * cq, void *tag)
-{
- gpr_log (file, line, severity, "grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", server, cq, tag);
+void grpc_server_log_shutdown(char *file, int line, gpr_log_severity severity,
+ grpc_server *server, grpc_completion_queue *cq,
+ void *tag) {
+ gpr_log(file, line, severity,
+ "grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", server,
+ cq, tag);
}
diff --git a/src/core/surface/channel.c b/src/core/surface/channel.c
index a5816c3fb3..08d003daca 100644
--- a/src/core/surface/channel.c
+++ b/src/core/surface/channel.c
@@ -53,15 +53,13 @@
* (OK, Cancelled, Unknown). */
#define NUM_CACHED_STATUS_ELEMS 3
-typedef struct registered_call
-{
+typedef struct registered_call {
grpc_mdelem *path;
grpc_mdelem *authority;
struct registered_call *next;
} registered_call;
-struct grpc_channel
-{
+struct grpc_channel {
int is_client;
gpr_refcount refs;
gpr_uint32 max_message_length;
@@ -91,297 +89,283 @@ struct grpc_channel
/* the protobuf library will (by default) start warning at 100megs */
#define DEFAULT_MAX_MESSAGE_LENGTH (100 * 1024 * 1024)
-grpc_channel *
-grpc_channel_create_from_filters (grpc_exec_ctx * exec_ctx, const char *target, const grpc_channel_filter ** filters, size_t num_filters, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_client)
-{
+grpc_channel *grpc_channel_create_from_filters(
+ grpc_exec_ctx *exec_ctx, const char *target,
+ const grpc_channel_filter **filters, size_t num_filters,
+ const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client) {
size_t i;
- size_t size = sizeof (grpc_channel) + grpc_channel_stack_size (filters, num_filters);
- grpc_channel *channel = gpr_malloc (size);
- memset (channel, 0, sizeof (*channel));
- channel->target = gpr_strdup (target);
- GPR_ASSERT (grpc_is_initialized () && "call grpc_init()");
+ size_t size =
+ sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters);
+ grpc_channel *channel = gpr_malloc(size);
+ memset(channel, 0, sizeof(*channel));
+ channel->target = gpr_strdup(target);
+ GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
channel->is_client = is_client;
/* decremented by grpc_channel_destroy */
- gpr_ref_init (&channel->refs, 1);
+ gpr_ref_init(&channel->refs, 1);
channel->metadata_context = mdctx;
- channel->grpc_status_string = grpc_mdstr_from_string (mdctx, "grpc-status", 0);
- channel->grpc_compression_algorithm_string = grpc_mdstr_from_string (mdctx, "grpc-encoding", 0);
- channel->grpc_encodings_accepted_by_peer_string = grpc_mdstr_from_string (mdctx, "grpc-accept-encoding", 0);
- channel->grpc_message_string = grpc_mdstr_from_string (mdctx, "grpc-message", 0);
- for (i = 0; i < NUM_CACHED_STATUS_ELEMS; i++)
- {
- char buf[GPR_LTOA_MIN_BUFSIZE];
- gpr_ltoa ((long) i, buf);
- channel->grpc_status_elem[i] = grpc_mdelem_from_metadata_strings (mdctx, GRPC_MDSTR_REF (channel->grpc_status_string), grpc_mdstr_from_string (mdctx, buf, 0));
- }
- channel->path_string = grpc_mdstr_from_string (mdctx, ":path", 0);
- channel->authority_string = grpc_mdstr_from_string (mdctx, ":authority", 0);
- gpr_mu_init (&channel->registered_call_mu);
+ channel->grpc_status_string = grpc_mdstr_from_string(mdctx, "grpc-status", 0);
+ channel->grpc_compression_algorithm_string =
+ grpc_mdstr_from_string(mdctx, "grpc-encoding", 0);
+ channel->grpc_encodings_accepted_by_peer_string =
+ grpc_mdstr_from_string(mdctx, "grpc-accept-encoding", 0);
+ channel->grpc_message_string =
+ grpc_mdstr_from_string(mdctx, "grpc-message", 0);
+ for (i = 0; i < NUM_CACHED_STATUS_ELEMS; i++) {
+ char buf[GPR_LTOA_MIN_BUFSIZE];
+ gpr_ltoa((long)i, buf);
+ channel->grpc_status_elem[i] = grpc_mdelem_from_metadata_strings(
+ mdctx, GRPC_MDSTR_REF(channel->grpc_status_string),
+ grpc_mdstr_from_string(mdctx, buf, 0));
+ }
+ channel->path_string = grpc_mdstr_from_string(mdctx, ":path", 0);
+ channel->authority_string = grpc_mdstr_from_string(mdctx, ":authority", 0);
+ gpr_mu_init(&channel->registered_call_mu);
channel->registered_calls = NULL;
channel->max_message_length = DEFAULT_MAX_MESSAGE_LENGTH;
- if (args)
- {
- for (i = 0; i < args->num_args; i++)
- {
- if (0 == strcmp (args->args[i].key, GRPC_ARG_MAX_MESSAGE_LENGTH))
- {
- if (args->args[i].type != GRPC_ARG_INTEGER)
- {
- gpr_log (GPR_ERROR, "%s ignored: it must be an integer", GRPC_ARG_MAX_MESSAGE_LENGTH);
- }
- else if (args->args[i].value.integer < 0)
- {
- gpr_log (GPR_ERROR, "%s ignored: it must be >= 0", GRPC_ARG_MAX_MESSAGE_LENGTH);
- }
- else
- {
- channel->max_message_length = (gpr_uint32) args->args[i].value.integer;
- }
- }
- else if (0 == strcmp (args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY))
- {
- if (args->args[i].type != GRPC_ARG_STRING)
- {
- gpr_log (GPR_ERROR, "%s: must be an string", GRPC_ARG_DEFAULT_AUTHORITY);
- }
- else
- {
- if (channel->default_authority)
- {
- /* setting this takes precedence over anything else */
- GRPC_MDELEM_UNREF (channel->default_authority);
- }
- channel->default_authority = grpc_mdelem_from_strings (mdctx, ":authority", args->args[i].value.string);
- }
- }
- else if (0 == strcmp (args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG))
- {
- if (args->args[i].type != GRPC_ARG_STRING)
- {
- gpr_log (GPR_ERROR, "%s: must be an string", GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
- }
- else
- {
- if (channel->default_authority)
- {
- /* other ways of setting this (notably ssl) take precedence */
- gpr_log (GPR_ERROR, "%s: default host already set some other way", GRPC_ARG_DEFAULT_AUTHORITY);
- }
- else
- {
- channel->default_authority = grpc_mdelem_from_strings (mdctx, ":authority", args->args[i].value.string);
- }
- }
- }
- }
+ if (args) {
+ for (i = 0; i < args->num_args; i++) {
+ if (0 == strcmp(args->args[i].key, GRPC_ARG_MAX_MESSAGE_LENGTH)) {
+ if (args->args[i].type != GRPC_ARG_INTEGER) {
+ gpr_log(GPR_ERROR, "%s ignored: it must be an integer",
+ GRPC_ARG_MAX_MESSAGE_LENGTH);
+ } else if (args->args[i].value.integer < 0) {
+ gpr_log(GPR_ERROR, "%s ignored: it must be >= 0",
+ GRPC_ARG_MAX_MESSAGE_LENGTH);
+ } else {
+ channel->max_message_length = (gpr_uint32)args->args[i].value.integer;
+ }
+ } else if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) {
+ if (args->args[i].type != GRPC_ARG_STRING) {
+ gpr_log(GPR_ERROR, "%s: must be an string",
+ GRPC_ARG_DEFAULT_AUTHORITY);
+ } else {
+ if (channel->default_authority) {
+ /* setting this takes precedence over anything else */
+ GRPC_MDELEM_UNREF(channel->default_authority);
+ }
+ channel->default_authority = grpc_mdelem_from_strings(
+ mdctx, ":authority", args->args[i].value.string);
+ }
+ } else if (0 ==
+ strcmp(args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)) {
+ if (args->args[i].type != GRPC_ARG_STRING) {
+ gpr_log(GPR_ERROR, "%s: must be an string",
+ GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
+ } else {
+ if (channel->default_authority) {
+ /* other ways of setting this (notably ssl) take precedence */
+ gpr_log(GPR_ERROR, "%s: default host already set some other way",
+ GRPC_ARG_DEFAULT_AUTHORITY);
+ } else {
+ channel->default_authority = grpc_mdelem_from_strings(
+ mdctx, ":authority", args->args[i].value.string);
+ }
+ }
+ }
}
-
- if (channel->is_client && channel->default_authority == NULL && target != NULL)
- {
- char *default_authority = grpc_get_default_authority (target);
- if (default_authority)
- {
- channel->default_authority = grpc_mdelem_from_strings (channel->metadata_context, ":authority", default_authority);
- }
- gpr_free (default_authority);
+ }
+
+ if (channel->is_client && channel->default_authority == NULL &&
+ target != NULL) {
+ char *default_authority = grpc_get_default_authority(target);
+ if (default_authority) {
+ channel->default_authority = grpc_mdelem_from_strings(
+ channel->metadata_context, ":authority", default_authority);
}
+ gpr_free(default_authority);
+ }
- grpc_channel_stack_init (exec_ctx, filters, num_filters, channel, args, channel->metadata_context, CHANNEL_STACK_FROM_CHANNEL (channel));
+ grpc_channel_stack_init(exec_ctx, filters, num_filters, channel, args,
+ channel->metadata_context,
+ CHANNEL_STACK_FROM_CHANNEL(channel));
return channel;
}
-char *
-grpc_channel_get_target (grpc_channel * channel)
-{
- return gpr_strdup (channel->target);
+char *grpc_channel_get_target(grpc_channel *channel) {
+ return gpr_strdup(channel->target);
}
-static grpc_call *
-grpc_channel_create_call_internal (grpc_channel * channel, grpc_call * parent_call, gpr_uint32 propagation_mask, grpc_completion_queue * cq, grpc_mdelem * path_mdelem, grpc_mdelem * authority_mdelem, gpr_timespec deadline)
-{
+static grpc_call *grpc_channel_create_call_internal(
+ grpc_channel *channel, grpc_call *parent_call, gpr_uint32 propagation_mask,
+ grpc_completion_queue *cq, grpc_mdelem *path_mdelem,
+ grpc_mdelem *authority_mdelem, gpr_timespec deadline) {
grpc_mdelem *send_metadata[2];
size_t num_metadata = 0;
- GPR_ASSERT (channel->is_client);
+ GPR_ASSERT(channel->is_client);
send_metadata[num_metadata++] = path_mdelem;
- if (authority_mdelem != NULL)
- {
- send_metadata[num_metadata++] = authority_mdelem;
- }
- else if (channel->default_authority != NULL)
- {
- send_metadata[num_metadata++] = GRPC_MDELEM_REF (channel->default_authority);
- }
-
- return grpc_call_create (channel, parent_call, propagation_mask, cq, NULL, send_metadata, num_metadata, deadline);
+ if (authority_mdelem != NULL) {
+ send_metadata[num_metadata++] = authority_mdelem;
+ } else if (channel->default_authority != NULL) {
+ send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority);
+ }
+
+ return grpc_call_create(channel, parent_call, propagation_mask, cq, NULL,
+ send_metadata, num_metadata, deadline);
}
-grpc_call *
-grpc_channel_create_call (grpc_channel * channel, grpc_call * parent_call, gpr_uint32 propagation_mask, grpc_completion_queue * cq, const char *method, const char *host, gpr_timespec deadline, void *reserved)
-{
- GPR_ASSERT (!reserved);
- return grpc_channel_create_call_internal (channel, parent_call, propagation_mask, cq, grpc_mdelem_from_metadata_strings (channel->metadata_context, GRPC_MDSTR_REF (channel->path_string), grpc_mdstr_from_string (channel->metadata_context, method, 0)), host ? grpc_mdelem_from_metadata_strings (channel->metadata_context, GRPC_MDSTR_REF (channel->authority_string), grpc_mdstr_from_string (channel->metadata_context, host, 0)) : NULL, deadline);
+grpc_call *grpc_channel_create_call(grpc_channel *channel,
+ grpc_call *parent_call,
+ gpr_uint32 propagation_mask,
+ grpc_completion_queue *cq,
+ const char *method, const char *host,
+ gpr_timespec deadline, void *reserved) {
+ GPR_ASSERT(!reserved);
+ return grpc_channel_create_call_internal(
+ channel, parent_call, propagation_mask, cq,
+ grpc_mdelem_from_metadata_strings(
+ channel->metadata_context, GRPC_MDSTR_REF(channel->path_string),
+ grpc_mdstr_from_string(channel->metadata_context, method, 0)),
+ host ? grpc_mdelem_from_metadata_strings(
+ channel->metadata_context,
+ GRPC_MDSTR_REF(channel->authority_string),
+ grpc_mdstr_from_string(channel->metadata_context, host, 0))
+ : NULL,
+ deadline);
}
-void *
-grpc_channel_register_call (grpc_channel * channel, const char *method, const char *host, void *reserved)
-{
- registered_call *rc = gpr_malloc (sizeof (registered_call));
- GPR_ASSERT (!reserved);
- rc->path = grpc_mdelem_from_metadata_strings (channel->metadata_context, GRPC_MDSTR_REF (channel->path_string), grpc_mdstr_from_string (channel->metadata_context, method, 0));
- rc->authority = host ? grpc_mdelem_from_metadata_strings (channel->metadata_context, GRPC_MDSTR_REF (channel->authority_string), grpc_mdstr_from_string (channel->metadata_context, host, 0)) : NULL;
- gpr_mu_lock (&channel->registered_call_mu);
+void *grpc_channel_register_call(grpc_channel *channel, const char *method,
+ const char *host, void *reserved) {
+ registered_call *rc = gpr_malloc(sizeof(registered_call));
+ GPR_ASSERT(!reserved);
+ rc->path = grpc_mdelem_from_metadata_strings(
+ channel->metadata_context, GRPC_MDSTR_REF(channel->path_string),
+ grpc_mdstr_from_string(channel->metadata_context, method, 0));
+ rc->authority =
+ host ? grpc_mdelem_from_metadata_strings(
+ channel->metadata_context,
+ GRPC_MDSTR_REF(channel->authority_string),
+ grpc_mdstr_from_string(channel->metadata_context, host, 0))
+ : NULL;
+ gpr_mu_lock(&channel->registered_call_mu);
rc->next = channel->registered_calls;
channel->registered_calls = rc;
- gpr_mu_unlock (&channel->registered_call_mu);
+ gpr_mu_unlock(&channel->registered_call_mu);
return rc;
}
-grpc_call *
-grpc_channel_create_registered_call (grpc_channel * channel, grpc_call * parent_call, gpr_uint32 propagation_mask, grpc_completion_queue * completion_queue, void *registered_call_handle, gpr_timespec deadline, void *reserved)
-{
+grpc_call *grpc_channel_create_registered_call(
+ grpc_channel *channel, grpc_call *parent_call, gpr_uint32 propagation_mask,
+ grpc_completion_queue *completion_queue, void *registered_call_handle,
+ gpr_timespec deadline, void *reserved) {
registered_call *rc = registered_call_handle;
- GPR_ASSERT (!reserved);
- return grpc_channel_create_call_internal (channel, parent_call, propagation_mask, completion_queue, GRPC_MDELEM_REF (rc->path), rc->authority ? GRPC_MDELEM_REF (rc->authority) : NULL, deadline);
+ GPR_ASSERT(!reserved);
+ return grpc_channel_create_call_internal(
+ channel, parent_call, propagation_mask, completion_queue,
+ GRPC_MDELEM_REF(rc->path),
+ rc->authority ? GRPC_MDELEM_REF(rc->authority) : NULL, deadline);
}
#ifdef GRPC_CHANNEL_REF_COUNT_DEBUG
-void
-grpc_channel_internal_ref (grpc_channel * c, const char *reason)
-{
- gpr_log (GPR_DEBUG, "CHANNEL: ref %p %d -> %d [%s]", c, c->refs.count, c->refs.count + 1, reason);
+void grpc_channel_internal_ref(grpc_channel *c, const char *reason) {
+ gpr_log(GPR_DEBUG, "CHANNEL: ref %p %d -> %d [%s]", c, c->refs.count,
+ c->refs.count + 1, reason);
#else
-void
-grpc_channel_internal_ref (grpc_channel * c)
-{
+void grpc_channel_internal_ref(grpc_channel *c) {
#endif
- gpr_ref (&c->refs);
+ gpr_ref(&c->refs);
}
-static void
-destroy_channel (grpc_exec_ctx * exec_ctx, grpc_channel * channel)
-{
+static void destroy_channel(grpc_exec_ctx *exec_ctx, grpc_channel *channel) {
size_t i;
- grpc_channel_stack_destroy (exec_ctx,CHANNEL_STACK_FROM_CHANNEL ( channel));
- for (i = 0; i < NUM_CACHED_STATUS_ELEMS; i++)
- {
- GRPC_MDELEM_UNREF (channel->grpc_status_elem[i]);
+ grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(channel));
+ for (i = 0; i < NUM_CACHED_STATUS_ELEMS; i++) {
+ GRPC_MDELEM_UNREF(channel->grpc_status_elem[i]);
+ }
+ GRPC_MDSTR_UNREF(channel->grpc_status_string);
+ GRPC_MDSTR_UNREF(channel->grpc_compression_algorithm_string);
+ GRPC_MDSTR_UNREF(channel->grpc_encodings_accepted_by_peer_string);
+ GRPC_MDSTR_UNREF(channel->grpc_message_string);
+ GRPC_MDSTR_UNREF(channel->path_string);
+ GRPC_MDSTR_UNREF(channel->authority_string);
+ while (channel->registered_calls) {
+ registered_call *rc = channel->registered_calls;
+ channel->registered_calls = rc->next;
+ GRPC_MDELEM_UNREF(rc->path);
+ if (rc->authority) {
+ GRPC_MDELEM_UNREF(rc->authority);
}
- GRPC_MDSTR_UNREF (channel->grpc_status_string);
- GRPC_MDSTR_UNREF (channel->grpc_compression_algorithm_string);
- GRPC_MDSTR_UNREF (channel->grpc_encodings_accepted_by_peer_string);
- GRPC_MDSTR_UNREF (channel->grpc_message_string);
- GRPC_MDSTR_UNREF (channel->path_string);
- GRPC_MDSTR_UNREF (channel->authority_string);
- while (channel->registered_calls)
- {
- registered_call *rc = channel->registered_calls;
- channel->registered_calls = rc->next;
- GRPC_MDELEM_UNREF (rc->path);
- if (rc->authority)
- {
- GRPC_MDELEM_UNREF (rc->authority);
- }
- gpr_free (rc);
- }
- if (channel->default_authority != NULL)
- {
- GRPC_MDELEM_UNREF (channel->default_authority);
- }
- grpc_mdctx_unref (channel->metadata_context);
- gpr_mu_destroy (&channel->registered_call_mu);
- gpr_free (channel->target);
- gpr_free (channel);
+ gpr_free(rc);
+ }
+ if (channel->default_authority != NULL) {
+ GRPC_MDELEM_UNREF(channel->default_authority);
+ }
+ grpc_mdctx_unref(channel->metadata_context);
+ gpr_mu_destroy(&channel->registered_call_mu);
+ gpr_free(channel->target);
+ gpr_free(channel);
}
#ifdef GRPC_CHANNEL_REF_COUNT_DEBUG
-void
-grpc_channel_internal_unref (grpc_exec_ctx * exec_ctx, grpc_channel * channel, const char *reason)
-{
- gpr_log (GPR_DEBUG, "CHANNEL: unref %p %d -> %d [%s]", channel, channel->refs.count, channel->refs.count - 1, reason);
+void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
+ const char *reason) {
+ gpr_log(GPR_DEBUG, "CHANNEL: unref %p %d -> %d [%s]", channel,
+ channel->refs.count, channel->refs.count - 1, reason);
#else
-void
-grpc_channel_internal_unref (grpc_exec_ctx * exec_ctx, grpc_channel * channel)
-{
+void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx,
+ grpc_channel *channel) {
#endif
- if (gpr_unref (&channel->refs))
- {
- destroy_channel (exec_ctx, channel);
- }
+ if (gpr_unref(&channel->refs)) {
+ destroy_channel(exec_ctx, channel);
+ }
}
-void
-grpc_channel_destroy (grpc_channel * channel)
-{
+void grpc_channel_destroy(grpc_channel *channel) {
grpc_transport_op op;
grpc_channel_element *elem;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- memset (&op, 0, sizeof (op));
+ memset(&op, 0, sizeof(op));
op.disconnect = 1;
- elem = grpc_channel_stack_element (CHANNEL_STACK_FROM_CHANNEL (channel), 0);
- elem->filter->start_transport_op (&exec_ctx, elem, &op);
+ elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
+ elem->filter->start_transport_op(&exec_ctx, elem, &op);
- GRPC_CHANNEL_INTERNAL_UNREF (&exec_ctx, channel, "channel");
+ GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, channel, "channel");
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-grpc_channel_stack *
-grpc_channel_get_channel_stack (grpc_channel * channel)
-{
- return CHANNEL_STACK_FROM_CHANNEL (channel);
+grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) {
+ return CHANNEL_STACK_FROM_CHANNEL(channel);
}
-grpc_mdctx *
-grpc_channel_get_metadata_context (grpc_channel * channel)
-{
+grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel) {
return channel->metadata_context;
}
-grpc_mdstr *
-grpc_channel_get_status_string (grpc_channel * channel)
-{
+grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel) {
return channel->grpc_status_string;
}
-grpc_mdstr *
-grpc_channel_get_compression_algorithm_string (grpc_channel * channel)
-{
+grpc_mdstr *grpc_channel_get_compression_algorithm_string(
+ grpc_channel *channel) {
return channel->grpc_compression_algorithm_string;
}
-grpc_mdstr *
-grpc_channel_get_encodings_accepted_by_peer_string (grpc_channel * channel)
-{
+grpc_mdstr *grpc_channel_get_encodings_accepted_by_peer_string(
+ grpc_channel *channel) {
return channel->grpc_encodings_accepted_by_peer_string;
}
-grpc_mdelem *
-grpc_channel_get_reffed_status_elem (grpc_channel * channel, int i)
-{
- if (i >= 0 && i < NUM_CACHED_STATUS_ELEMS)
- {
- return GRPC_MDELEM_REF (channel->grpc_status_elem[i]);
- }
- else
- {
- char tmp[GPR_LTOA_MIN_BUFSIZE];
- gpr_ltoa (i, tmp);
- return grpc_mdelem_from_metadata_strings (channel->metadata_context, GRPC_MDSTR_REF (channel->grpc_status_string), grpc_mdstr_from_string (channel->metadata_context, tmp, 0));
- }
+grpc_mdelem *grpc_channel_get_reffed_status_elem(grpc_channel *channel, int i) {
+ if (i >= 0 && i < NUM_CACHED_STATUS_ELEMS) {
+ return GRPC_MDELEM_REF(channel->grpc_status_elem[i]);
+ } else {
+ char tmp[GPR_LTOA_MIN_BUFSIZE];
+ gpr_ltoa(i, tmp);
+ return grpc_mdelem_from_metadata_strings(
+ channel->metadata_context, GRPC_MDSTR_REF(channel->grpc_status_string),
+ grpc_mdstr_from_string(channel->metadata_context, tmp, 0));
+ }
}
-grpc_mdstr *
-grpc_channel_get_message_string (grpc_channel * channel)
-{
+grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel) {
return channel->grpc_message_string;
}
-gpr_uint32
-grpc_channel_get_max_message_length (grpc_channel * channel)
-{
+gpr_uint32 grpc_channel_get_max_message_length(grpc_channel *channel) {
return channel->max_message_length;
}
diff --git a/src/core/surface/channel.h b/src/core/surface/channel.h
index 678aa5ba56..e5030d52d2 100644
--- a/src/core/surface/channel.h
+++ b/src/core/surface/channel.h
@@ -37,35 +37,43 @@
#include "src/core/channel/channel_stack.h"
#include "src/core/client_config/subchannel_factory.h"
-grpc_channel *grpc_channel_create_from_filters (grpc_exec_ctx * exec_ctx, const char *target, const grpc_channel_filter ** filters, size_t count, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_client);
+grpc_channel *grpc_channel_create_from_filters(
+ grpc_exec_ctx *exec_ctx, const char *target,
+ const grpc_channel_filter **filters, size_t count,
+ const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client);
/** Get a (borrowed) pointer to this channels underlying channel stack */
-grpc_channel_stack *grpc_channel_get_channel_stack (grpc_channel * channel);
+grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel);
/** Get a (borrowed) pointer to the channel wide metadata context */
-grpc_mdctx *grpc_channel_get_metadata_context (grpc_channel * channel);
+grpc_mdctx *grpc_channel_get_metadata_context(grpc_channel *channel);
/** Get a grpc_mdelem of grpc-status: X where X is the numeric value of
status_code.
The returned elem is owned by the caller. */
-grpc_mdelem *grpc_channel_get_reffed_status_elem (grpc_channel * channel, int status_code);
-grpc_mdstr *grpc_channel_get_status_string (grpc_channel * channel);
-grpc_mdstr *grpc_channel_get_compression_algorithm_string (grpc_channel * channel);
-grpc_mdstr *grpc_channel_get_encodings_accepted_by_peer_string (grpc_channel * channel);
-grpc_mdstr *grpc_channel_get_message_string (grpc_channel * channel);
-gpr_uint32 grpc_channel_get_max_message_length (grpc_channel * channel);
+grpc_mdelem *grpc_channel_get_reffed_status_elem(grpc_channel *channel,
+ int status_code);
+grpc_mdstr *grpc_channel_get_status_string(grpc_channel *channel);
+grpc_mdstr *grpc_channel_get_compression_algorithm_string(
+ grpc_channel *channel);
+grpc_mdstr *grpc_channel_get_encodings_accepted_by_peer_string(
+ grpc_channel *channel);
+grpc_mdstr *grpc_channel_get_message_string(grpc_channel *channel);
+gpr_uint32 grpc_channel_get_max_message_length(grpc_channel *channel);
#ifdef GRPC_CHANNEL_REF_COUNT_DEBUG
-void grpc_channel_internal_ref (grpc_channel * channel, const char *reason);
-void grpc_channel_internal_unref (grpc_exec_ctx * exec_ctx, grpc_channel * channel, const char *reason);
+void grpc_channel_internal_ref(grpc_channel *channel, const char *reason);
+void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
+ const char *reason);
#define GRPC_CHANNEL_INTERNAL_REF(channel, reason) \
grpc_channel_internal_ref(channel, reason)
#define GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, reason) \
grpc_channel_internal_unref(exec_ctx, channel, reason)
#else
-void grpc_channel_internal_ref (grpc_channel * channel);
-void grpc_channel_internal_unref (grpc_exec_ctx * exec_ctx, grpc_channel * channel);
+void grpc_channel_internal_ref(grpc_channel *channel);
+void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx,
+ grpc_channel *channel);
#define GRPC_CHANNEL_INTERNAL_REF(channel, reason) \
grpc_channel_internal_ref(channel)
#define GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, reason) \
diff --git a/src/core/surface/channel_connectivity.c b/src/core/surface/channel_connectivity.c
index a9fc1cf59f..a685a99eac 100644
--- a/src/core/surface/channel_connectivity.c
+++ b/src/core/surface/channel_connectivity.c
@@ -40,33 +40,34 @@
#include "src/core/iomgr/alarm.h"
#include "src/core/surface/completion_queue.h"
-grpc_connectivity_state
-grpc_channel_check_connectivity_state (grpc_channel * channel, int try_to_connect)
-{
+grpc_connectivity_state grpc_channel_check_connectivity_state(
+ grpc_channel *channel, int try_to_connect) {
/* forward through to the underlying client channel */
- grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element (grpc_channel_get_channel_stack (channel));
+ grpc_channel_element *client_channel_elem =
+ grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_connectivity_state state;
- if (client_channel_elem->filter != &grpc_client_channel_filter)
- {
- gpr_log (GPR_ERROR, "grpc_channel_check_connectivity_state called on something that is " "not a client channel, but '%s'", client_channel_elem->filter->name);
- return GRPC_CHANNEL_FATAL_FAILURE;
- }
- state = grpc_client_channel_check_connectivity_state (&exec_ctx, client_channel_elem, try_to_connect);
- grpc_exec_ctx_finish (&exec_ctx);
+ if (client_channel_elem->filter != &grpc_client_channel_filter) {
+ gpr_log(GPR_ERROR,
+ "grpc_channel_check_connectivity_state called on something that is "
+ "not a client channel, but '%s'",
+ client_channel_elem->filter->name);
+ return GRPC_CHANNEL_FATAL_FAILURE;
+ }
+ state = grpc_client_channel_check_connectivity_state(
+ &exec_ctx, client_channel_elem, try_to_connect);
+ grpc_exec_ctx_finish(&exec_ctx);
return state;
}
-typedef enum
-{
+typedef enum {
WAITING,
CALLING_BACK,
CALLING_BACK_AND_FINISHED,
CALLED_BACK
} callback_phase;
-typedef struct
-{
+typedef struct {
gpr_mu mu;
callback_phase phase;
int success;
@@ -80,26 +81,22 @@ typedef struct
void *tag;
} state_watcher;
-static void
-delete_state_watcher (grpc_exec_ctx * exec_ctx, state_watcher * w)
-{
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, w->channel, "watch_connectivity");
- gpr_mu_destroy (&w->mu);
- gpr_free (w);
+static void delete_state_watcher(grpc_exec_ctx *exec_ctx, state_watcher *w) {
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->channel, "watch_connectivity");
+ gpr_mu_destroy(&w->mu);
+ gpr_free(w);
}
-static void
-finished_completion (grpc_exec_ctx * exec_ctx, void *pw, grpc_cq_completion * ignored)
-{
+static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw,
+ grpc_cq_completion *ignored) {
int delete = 0;
state_watcher *w = pw;
- gpr_mu_lock (&w->mu);
- switch (w->phase)
- {
+ gpr_mu_lock(&w->mu);
+ switch (w->phase) {
case WAITING:
case CALLED_BACK:
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
break;
case CALLING_BACK:
w->phase = CALLED_BACK;
@@ -107,86 +104,80 @@ finished_completion (grpc_exec_ctx * exec_ctx, void *pw, grpc_cq_completion * ig
case CALLING_BACK_AND_FINISHED:
delete = 1;
break;
- }
- gpr_mu_unlock (&w->mu);
+ }
+ gpr_mu_unlock(&w->mu);
- if (delete)
- {
- delete_state_watcher (exec_ctx, w);
- }
+ if (delete) {
+ delete_state_watcher(exec_ctx, w);
+ }
}
-static void
-partly_done (grpc_exec_ctx * exec_ctx, state_watcher * w, int due_to_completion)
-{
+static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w,
+ int due_to_completion) {
int delete = 0;
grpc_channel_element *client_channel_elem = NULL;
- gpr_mu_lock (&w->mu);
- if (w->removed == 0)
- {
- w->removed = 1;
- client_channel_elem = grpc_channel_stack_last_element (grpc_channel_get_channel_stack (w->channel));
- grpc_client_channel_del_interested_party (exec_ctx, client_channel_elem, grpc_cq_pollset (w->cq));
- }
- gpr_mu_unlock (&w->mu);
- if (due_to_completion)
- {
- gpr_mu_lock (&w->mu);
- w->success = 1;
- gpr_mu_unlock (&w->mu);
- grpc_alarm_cancel (exec_ctx, &w->alarm);
- }
-
- gpr_mu_lock (&w->mu);
- switch (w->phase)
- {
+ gpr_mu_lock(&w->mu);
+ if (w->removed == 0) {
+ w->removed = 1;
+ client_channel_elem = grpc_channel_stack_last_element(
+ grpc_channel_get_channel_stack(w->channel));
+ grpc_client_channel_del_interested_party(exec_ctx, client_channel_elem,
+ grpc_cq_pollset(w->cq));
+ }
+ gpr_mu_unlock(&w->mu);
+ if (due_to_completion) {
+ gpr_mu_lock(&w->mu);
+ w->success = 1;
+ gpr_mu_unlock(&w->mu);
+ grpc_alarm_cancel(exec_ctx, &w->alarm);
+ }
+
+ gpr_mu_lock(&w->mu);
+ switch (w->phase) {
case WAITING:
w->phase = CALLING_BACK;
- grpc_cq_end_op (exec_ctx, w->cq, w->tag, w->success, finished_completion, w, &w->completion_storage);
+ grpc_cq_end_op(exec_ctx, w->cq, w->tag, w->success, finished_completion,
+ w, &w->completion_storage);
break;
case CALLING_BACK:
w->phase = CALLING_BACK_AND_FINISHED;
break;
case CALLING_BACK_AND_FINISHED:
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
break;
case CALLED_BACK:
delete = 1;
break;
- }
- gpr_mu_unlock (&w->mu);
+ }
+ gpr_mu_unlock(&w->mu);
- if (delete)
- {
- delete_state_watcher (exec_ctx, w);
- }
+ if (delete) {
+ delete_state_watcher(exec_ctx, w);
+ }
}
-static void
-watch_complete (grpc_exec_ctx * exec_ctx, void *pw, int success)
-{
- partly_done (exec_ctx, pw, 1);
+static void watch_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
+ partly_done(exec_ctx, pw, 1);
}
-static void
-timeout_complete (grpc_exec_ctx * exec_ctx, void *pw, int success)
-{
- partly_done (exec_ctx, pw, 0);
+static void timeout_complete(grpc_exec_ctx *exec_ctx, void *pw, int success) {
+ partly_done(exec_ctx, pw, 0);
}
-void
-grpc_channel_watch_connectivity_state (grpc_channel * channel, grpc_connectivity_state last_observed_state, gpr_timespec deadline, grpc_completion_queue * cq, void *tag)
-{
- grpc_channel_element *client_channel_elem = grpc_channel_stack_last_element (grpc_channel_get_channel_stack (channel));
+void grpc_channel_watch_connectivity_state(
+ grpc_channel *channel, grpc_connectivity_state last_observed_state,
+ gpr_timespec deadline, grpc_completion_queue *cq, void *tag) {
+ grpc_channel_element *client_channel_elem =
+ grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel));
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- state_watcher *w = gpr_malloc (sizeof (*w));
+ state_watcher *w = gpr_malloc(sizeof(*w));
- grpc_cq_begin_op (cq);
+ grpc_cq_begin_op(cq);
- gpr_mu_init (&w->mu);
- grpc_closure_init (&w->on_complete, watch_complete, w);
+ gpr_mu_init(&w->mu);
+ grpc_closure_init(&w->on_complete, watch_complete, w);
w->phase = WAITING;
w->state = last_observed_state;
w->success = 0;
@@ -195,19 +186,23 @@ grpc_channel_watch_connectivity_state (grpc_channel * channel, grpc_connectivity
w->tag = tag;
w->channel = channel;
- grpc_alarm_init (&exec_ctx, &w->alarm, gpr_convert_clock_type (deadline, GPR_CLOCK_MONOTONIC), timeout_complete, w, gpr_now (GPR_CLOCK_MONOTONIC));
-
- if (client_channel_elem->filter != &grpc_client_channel_filter)
- {
- gpr_log (GPR_ERROR, "grpc_channel_watch_connectivity_state called on something that is " "not a client channel, but '%s'", client_channel_elem->filter->name);
- grpc_exec_ctx_enqueue(&exec_ctx, &w->on_complete ,1);
- }
- else
- {
- GRPC_CHANNEL_INTERNAL_REF (channel, "watch_connectivity");
- grpc_client_channel_add_interested_party (&exec_ctx, client_channel_elem, grpc_cq_pollset (cq));
- grpc_client_channel_watch_connectivity_state (&exec_ctx, client_channel_elem, &w->state, &w->on_complete);
- }
-
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_alarm_init(&exec_ctx, &w->alarm,
+ gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
+ timeout_complete, w, gpr_now(GPR_CLOCK_MONOTONIC));
+
+ if (client_channel_elem->filter != &grpc_client_channel_filter) {
+ gpr_log(GPR_ERROR,
+ "grpc_channel_watch_connectivity_state called on something that is "
+ "not a client channel, but '%s'",
+ client_channel_elem->filter->name);
+ grpc_exec_ctx_enqueue(&exec_ctx, &w->on_complete, 1);
+ } else {
+ GRPC_CHANNEL_INTERNAL_REF(channel, "watch_connectivity");
+ grpc_client_channel_add_interested_party(&exec_ctx, client_channel_elem,
+ grpc_cq_pollset(cq));
+ grpc_client_channel_watch_connectivity_state(&exec_ctx, client_channel_elem,
+ &w->state, &w->on_complete);
+ }
+
+ grpc_exec_ctx_finish(&exec_ctx);
}
diff --git a/src/core/surface/channel_create.c b/src/core/surface/channel_create.c
index 1fd1479a66..05591ce27f 100644
--- a/src/core/surface/channel_create.c
+++ b/src/core/surface/channel_create.c
@@ -48,8 +48,7 @@
#include "src/core/surface/channel.h"
#include "src/core/transport/chttp2_transport.h"
-typedef struct
-{
+typedef struct {
grpc_connector base;
gpr_refcount refs;
@@ -64,73 +63,63 @@ typedef struct
grpc_closure connected;
} connector;
-static void
-connector_ref (grpc_connector * con)
-{
- connector *c = (connector *) con;
- gpr_ref (&c->refs);
+static void connector_ref(grpc_connector *con) {
+ connector *c = (connector *)con;
+ gpr_ref(&c->refs);
}
-static void
-connector_unref (grpc_exec_ctx * exec_ctx, grpc_connector * con)
-{
- connector *c = (connector *) con;
- if (gpr_unref (&c->refs))
- {
- grpc_mdctx_unref (c->mdctx);
- gpr_free (c);
- }
+static void connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *con) {
+ connector *c = (connector *)con;
+ if (gpr_unref(&c->refs)) {
+ grpc_mdctx_unref(c->mdctx);
+ gpr_free(c);
+ }
}
-static void
-connected (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void connected(grpc_exec_ctx *exec_ctx, void *arg, int success) {
connector *c = arg;
grpc_closure *notify;
grpc_endpoint *tcp = c->tcp;
- if (tcp != NULL)
- {
- c->result->transport = grpc_create_chttp2_transport (exec_ctx, c->args.channel_args, tcp, c->mdctx, 1);
- grpc_chttp2_transport_start_reading (exec_ctx, c->result->transport, NULL, 0);
- GPR_ASSERT (c->result->transport);
- c->result->filters = gpr_malloc (sizeof (grpc_channel_filter *));
- c->result->filters[0] = &grpc_http_client_filter;
- c->result->num_filters = 1;
- }
- else
- {
- memset (c->result, 0, sizeof (*c->result));
- }
+ if (tcp != NULL) {
+ c->result->transport = grpc_create_chttp2_transport(
+ exec_ctx, c->args.channel_args, tcp, c->mdctx, 1);
+ grpc_chttp2_transport_start_reading(exec_ctx, c->result->transport, NULL,
+ 0);
+ GPR_ASSERT(c->result->transport);
+ c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *));
+ c->result->filters[0] = &grpc_http_client_filter;
+ c->result->num_filters = 1;
+ } else {
+ memset(c->result, 0, sizeof(*c->result));
+ }
notify = c->notify;
c->notify = NULL;
- notify->cb (exec_ctx, notify->cb_arg, 1);
+ notify->cb(exec_ctx, notify->cb_arg, 1);
}
-static void
-connector_shutdown (grpc_exec_ctx * exec_ctx, grpc_connector * con)
-{
-}
+static void connector_shutdown(grpc_exec_ctx *exec_ctx, grpc_connector *con) {}
-static void
-connector_connect (grpc_exec_ctx * exec_ctx, grpc_connector * con, const grpc_connect_in_args * args, grpc_connect_out_args * result, grpc_closure * notify)
-{
- connector *c = (connector *) con;
- GPR_ASSERT (c->notify == NULL);
- GPR_ASSERT (notify->cb);
+static void connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *con,
+ const grpc_connect_in_args *args,
+ grpc_connect_out_args *result,
+ grpc_closure *notify) {
+ connector *c = (connector *)con;
+ GPR_ASSERT(c->notify == NULL);
+ GPR_ASSERT(notify->cb);
c->notify = notify;
c->args = *args;
c->result = result;
c->tcp = NULL;
- grpc_closure_init (&c->connected, connected, c);
- grpc_tcp_client_connect (exec_ctx, &c->connected, &c->tcp, args->interested_parties, args->addr, args->addr_len, args->deadline);
+ grpc_closure_init(&c->connected, connected, c);
+ grpc_tcp_client_connect(exec_ctx, &c->connected, &c->tcp,
+ args->interested_parties, args->addr, args->addr_len,
+ args->deadline);
}
static const grpc_connector_vtable connector_vtable = {
- connector_ref, connector_unref, connector_shutdown, connector_connect
-};
+ connector_ref, connector_unref, connector_shutdown, connector_connect};
-typedef struct
-{
+typedef struct {
grpc_subchannel_factory base;
gpr_refcount refs;
grpc_mdctx *mdctx;
@@ -138,97 +127,93 @@ typedef struct
grpc_channel *master;
} subchannel_factory;
-static void
-subchannel_factory_ref (grpc_subchannel_factory * scf)
-{
- subchannel_factory *f = (subchannel_factory *) scf;
- gpr_ref (&f->refs);
+static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
+ subchannel_factory *f = (subchannel_factory *)scf;
+ gpr_ref(&f->refs);
}
-static void
-subchannel_factory_unref (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * scf)
-{
- subchannel_factory *f = (subchannel_factory *) scf;
- if (gpr_unref (&f->refs))
- {
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, f->master, "subchannel_factory");
- grpc_channel_args_destroy (f->merge_args);
- grpc_mdctx_unref (f->mdctx);
- gpr_free (f);
- }
+static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_factory *scf) {
+ subchannel_factory *f = (subchannel_factory *)scf;
+ if (gpr_unref(&f->refs)) {
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, f->master, "subchannel_factory");
+ grpc_channel_args_destroy(f->merge_args);
+ grpc_mdctx_unref(f->mdctx);
+ gpr_free(f);
+ }
}
-static grpc_subchannel *
-subchannel_factory_create_subchannel (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * scf, grpc_subchannel_args * args)
-{
- subchannel_factory *f = (subchannel_factory *) scf;
- connector *c = gpr_malloc (sizeof (*c));
- grpc_channel_args *final_args = grpc_channel_args_merge (args->args, f->merge_args);
+static grpc_subchannel *subchannel_factory_create_subchannel(
+ grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *scf,
+ grpc_subchannel_args *args) {
+ subchannel_factory *f = (subchannel_factory *)scf;
+ connector *c = gpr_malloc(sizeof(*c));
+ grpc_channel_args *final_args =
+ grpc_channel_args_merge(args->args, f->merge_args);
grpc_subchannel *s;
- memset (c, 0, sizeof (*c));
+ memset(c, 0, sizeof(*c));
c->base.vtable = &connector_vtable;
c->mdctx = f->mdctx;
- grpc_mdctx_ref (c->mdctx);
- gpr_ref_init (&c->refs, 1);
+ grpc_mdctx_ref(c->mdctx);
+ gpr_ref_init(&c->refs, 1);
args->mdctx = f->mdctx;
args->args = final_args;
args->master = f->master;
- s = grpc_subchannel_create (&c->base, args);
- grpc_connector_unref (exec_ctx, &c->base);
- grpc_channel_args_destroy (final_args);
+ s = grpc_subchannel_create(&c->base, args);
+ grpc_connector_unref(exec_ctx, &c->base);
+ grpc_channel_args_destroy(final_args);
return s;
}
static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
- subchannel_factory_ref, subchannel_factory_unref,
- subchannel_factory_create_subchannel
-};
+ subchannel_factory_ref, subchannel_factory_unref,
+ subchannel_factory_create_subchannel};
/* Create a client channel:
Asynchronously: - resolve target
- connect to it (trying alternatives as presented)
- perform handshakes */
-grpc_channel *
-grpc_insecure_channel_create (const char *target, const grpc_channel_args * args, void *reserved)
-{
+grpc_channel *grpc_insecure_channel_create(const char *target,
+ const grpc_channel_args *args,
+ void *reserved) {
grpc_channel *channel = NULL;
#define MAX_FILTERS 3
const grpc_channel_filter *filters[MAX_FILTERS];
grpc_resolver *resolver;
subchannel_factory *f;
- grpc_mdctx *mdctx = grpc_mdctx_create ();
+ grpc_mdctx *mdctx = grpc_mdctx_create();
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
size_t n = 0;
- GPR_ASSERT (!reserved);
- if (grpc_channel_args_is_census_enabled (args))
- {
- filters[n++] = &grpc_client_census_filter;
- }
+ GPR_ASSERT(!reserved);
+ if (grpc_channel_args_is_census_enabled(args)) {
+ filters[n++] = &grpc_client_census_filter;
+ }
filters[n++] = &grpc_compress_filter;
filters[n++] = &grpc_client_channel_filter;
- GPR_ASSERT (n <= MAX_FILTERS);
+ GPR_ASSERT(n <= MAX_FILTERS);
- channel = grpc_channel_create_from_filters (&exec_ctx, target, filters, n, args, mdctx, 1);
+ channel = grpc_channel_create_from_filters(&exec_ctx, target, filters, n,
+ args, mdctx, 1);
- f = gpr_malloc (sizeof (*f));
+ f = gpr_malloc(sizeof(*f));
f->base.vtable = &subchannel_factory_vtable;
- gpr_ref_init (&f->refs, 1);
- grpc_mdctx_ref (mdctx);
+ gpr_ref_init(&f->refs, 1);
+ grpc_mdctx_ref(mdctx);
f->mdctx = mdctx;
- f->merge_args = grpc_channel_args_copy (args);
+ f->merge_args = grpc_channel_args_copy(args);
f->master = channel;
- GRPC_CHANNEL_INTERNAL_REF (f->master, "subchannel_factory");
- resolver = grpc_resolver_create (target, &f->base);
- if (!resolver)
- {
- return NULL;
- }
-
- grpc_client_channel_set_resolver (&exec_ctx, grpc_channel_get_channel_stack (channel), resolver);
- GRPC_RESOLVER_UNREF (&exec_ctx, resolver, "create");
- grpc_subchannel_factory_unref (&exec_ctx, &f->base);
-
- grpc_exec_ctx_finish (&exec_ctx);
+ GRPC_CHANNEL_INTERNAL_REF(f->master, "subchannel_factory");
+ resolver = grpc_resolver_create(target, &f->base);
+ if (!resolver) {
+ return NULL;
+ }
+
+ grpc_client_channel_set_resolver(
+ &exec_ctx, grpc_channel_get_channel_stack(channel), resolver);
+ GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "create");
+ grpc_subchannel_factory_unref(&exec_ctx, &f->base);
+
+ grpc_exec_ctx_finish(&exec_ctx);
return channel;
}
diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c
index 56dceec30c..5dac8ebcf8 100644
--- a/src/core/surface/completion_queue.c
+++ b/src/core/surface/completion_queue.c
@@ -45,15 +45,13 @@
#include <grpc/support/atm.h>
#include <grpc/support/log.h>
-typedef struct
-{
+typedef struct {
grpc_pollset_worker *worker;
void *tag;
} plucker;
/* Completion queue structure */
-struct grpc_completion_queue
-{
+struct grpc_completion_queue {
/** completed events */
grpc_cq_completion completed_head;
grpc_cq_completion *completed_tail;
@@ -72,80 +70,73 @@ struct grpc_completion_queue
grpc_closure pollset_destroy_done;
};
-static void on_pollset_destroy_done (grpc_exec_ctx * exec_ctx, void *cc, int success);
+static void on_pollset_destroy_done(grpc_exec_ctx *exec_ctx, void *cc,
+ int success);
-grpc_completion_queue *
-grpc_completion_queue_create (void *reserved)
-{
- grpc_completion_queue *cc = gpr_malloc (sizeof (grpc_completion_queue));
- GPR_ASSERT (!reserved);
- memset (cc, 0, sizeof (*cc));
+grpc_completion_queue *grpc_completion_queue_create(void *reserved) {
+ grpc_completion_queue *cc = gpr_malloc(sizeof(grpc_completion_queue));
+ GPR_ASSERT(!reserved);
+ memset(cc, 0, sizeof(*cc));
/* Initial ref is dropped by grpc_completion_queue_shutdown */
- gpr_ref_init (&cc->pending_events, 1);
+ gpr_ref_init(&cc->pending_events, 1);
/* One for destroy(), one for pollset_shutdown */
- gpr_ref_init (&cc->owning_refs, 2);
- grpc_pollset_init (&cc->pollset);
+ gpr_ref_init(&cc->owning_refs, 2);
+ grpc_pollset_init(&cc->pollset);
cc->completed_tail = &cc->completed_head;
- cc->completed_head.next = (gpr_uintptr) cc->completed_tail;
- grpc_closure_init (&cc->pollset_destroy_done, on_pollset_destroy_done, cc);
+ cc->completed_head.next = (gpr_uintptr)cc->completed_tail;
+ grpc_closure_init(&cc->pollset_destroy_done, on_pollset_destroy_done, cc);
return cc;
}
#ifdef GRPC_CQ_REF_COUNT_DEBUG
-void
-grpc_cq_internal_ref (grpc_completion_queue * cc, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p ref %d -> %d %s", cc, (int) cc->owning_refs.count, (int) cc->owning_refs.count + 1, reason);
+void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason,
+ const char *file, int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p ref %d -> %d %s", cc,
+ (int)cc->owning_refs.count, (int)cc->owning_refs.count + 1, reason);
#else
-void
-grpc_cq_internal_ref (grpc_completion_queue * cc)
-{
+void grpc_cq_internal_ref(grpc_completion_queue *cc) {
#endif
- gpr_ref (&cc->owning_refs);
+ gpr_ref(&cc->owning_refs);
}
-static void
-on_pollset_destroy_done (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void on_pollset_destroy_done(grpc_exec_ctx *exec_ctx, void *arg,
+ int success) {
grpc_completion_queue *cc = arg;
- GRPC_CQ_INTERNAL_UNREF (cc, "pollset_destroy");
+ GRPC_CQ_INTERNAL_UNREF(cc, "pollset_destroy");
}
#ifdef GRPC_CQ_REF_COUNT_DEBUG
-void
-grpc_cq_internal_unref (grpc_completion_queue * cc, const char *reason, const char *file, int line)
-{
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p unref %d -> %d %s", cc, (int) cc->owning_refs.count, (int) cc->owning_refs.count - 1, reason);
+void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason,
+ const char *file, int line) {
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "CQ:%p unref %d -> %d %s", cc,
+ (int)cc->owning_refs.count, (int)cc->owning_refs.count - 1, reason);
#else
-void
-grpc_cq_internal_unref (grpc_completion_queue * cc)
-{
+void grpc_cq_internal_unref(grpc_completion_queue *cc) {
#endif
- if (gpr_unref (&cc->owning_refs))
- {
- GPR_ASSERT (cc->completed_head.next == (gpr_uintptr) & cc->completed_head);
- grpc_pollset_destroy (&cc->pollset);
- gpr_free (cc);
- }
+ if (gpr_unref(&cc->owning_refs)) {
+ GPR_ASSERT(cc->completed_head.next == (gpr_uintptr)&cc->completed_head);
+ grpc_pollset_destroy(&cc->pollset);
+ gpr_free(cc);
+ }
}
-void
-grpc_cq_begin_op (grpc_completion_queue * cc)
-{
+void grpc_cq_begin_op(grpc_completion_queue *cc) {
#ifndef NDEBUG
- gpr_mu_lock (GRPC_POLLSET_MU (&cc->pollset));
- GPR_ASSERT (!cc->shutdown_called);
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
+ gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
+ GPR_ASSERT(!cc->shutdown_called);
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
#endif
- gpr_ref (&cc->pending_events);
+ gpr_ref(&cc->pending_events);
}
/* Signal the end of an operation - if this is the last waiting-to-be-queued
event, then enter shutdown mode */
/* Queue a GRPC_OP_COMPLETED operation */
-void
-grpc_cq_end_op (grpc_exec_ctx * exec_ctx,grpc_completion_queue * cc, void *tag, int success, void (*done) (grpc_exec_ctx * exec_ctx, void *done_arg, grpc_cq_completion * storage), void *done_arg, grpc_cq_completion * storage)
-{
+void grpc_cq_end_op(grpc_exec_ctx *exec_ctx, grpc_completion_queue *cc,
+ void *tag, int success,
+ void (*done)(grpc_exec_ctx *exec_ctx, void *done_arg,
+ grpc_cq_completion *storage),
+ void *done_arg, grpc_cq_completion *storage) {
int shutdown;
int i;
grpc_pollset_worker *pluck_worker;
@@ -153,127 +144,113 @@ grpc_cq_end_op (grpc_exec_ctx * exec_ctx,grpc_completion_queue * cc, void *tag,
storage->tag = tag;
storage->done = done;
storage->done_arg = done_arg;
- storage->next = ((gpr_uintptr) & cc->completed_head) | ((gpr_uintptr) (success != 0));
+ storage->next =
+ ((gpr_uintptr)&cc->completed_head) | ((gpr_uintptr)(success != 0));
- gpr_mu_lock (GRPC_POLLSET_MU (&cc->pollset));
- shutdown = gpr_unref (&cc->pending_events);
- if (!shutdown)
- {
- cc->completed_tail->next = ((gpr_uintptr) storage) | (1u & (gpr_uintptr) cc->completed_tail->next);
- cc->completed_tail = storage;
- pluck_worker = NULL;
- for (i = 0; i < cc->num_pluckers; i++)
- {
- if (cc->pluckers[i].tag == tag)
- {
- pluck_worker = cc->pluckers[i].worker;
- break;
- }
- }
- grpc_pollset_kick (&cc->pollset, pluck_worker);
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- }
- else
- {
- cc->completed_tail->next = ((gpr_uintptr) storage) | (1u & (gpr_uintptr) cc->completed_tail->next);
- cc->completed_tail = storage;
- GPR_ASSERT (!cc->shutdown);
- GPR_ASSERT (cc->shutdown_called);
- cc->shutdown = 1;
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- grpc_pollset_shutdown (exec_ctx, &cc->pollset, &cc->pollset_destroy_done);
+ gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
+ shutdown = gpr_unref(&cc->pending_events);
+ if (!shutdown) {
+ cc->completed_tail->next =
+ ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
+ cc->completed_tail = storage;
+ pluck_worker = NULL;
+ for (i = 0; i < cc->num_pluckers; i++) {
+ if (cc->pluckers[i].tag == tag) {
+ pluck_worker = cc->pluckers[i].worker;
+ break;
+ }
}
+ grpc_pollset_kick(&cc->pollset, pluck_worker);
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ } else {
+ cc->completed_tail->next =
+ ((gpr_uintptr)storage) | (1u & (gpr_uintptr)cc->completed_tail->next);
+ cc->completed_tail = storage;
+ GPR_ASSERT(!cc->shutdown);
+ GPR_ASSERT(cc->shutdown_called);
+ cc->shutdown = 1;
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ grpc_pollset_shutdown(exec_ctx, &cc->pollset, &cc->pollset_destroy_done);
+ }
}
-grpc_event
-grpc_completion_queue_next (grpc_completion_queue * cc, gpr_timespec deadline, void *reserved)
-{
+grpc_event grpc_completion_queue_next(grpc_completion_queue *cc,
+ gpr_timespec deadline, void *reserved) {
grpc_event ret;
grpc_pollset_worker worker;
int first_loop = 1;
gpr_timespec now;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- GPR_ASSERT (!reserved);
+ GPR_ASSERT(!reserved);
- deadline = gpr_convert_clock_type (deadline, GPR_CLOCK_MONOTONIC);
+ deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
- GRPC_CQ_INTERNAL_REF (cc, "next");
- gpr_mu_lock (GRPC_POLLSET_MU (&cc->pollset));
- for (;;)
- {
- if (cc->completed_tail != &cc->completed_head)
- {
- grpc_cq_completion *c = (grpc_cq_completion *) cc->completed_head.next;
- cc->completed_head.next = c->next & ~(gpr_uintptr) 1;
- if (c == cc->completed_tail)
- {
- cc->completed_tail = &cc->completed_head;
- }
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- ret.type = GRPC_OP_COMPLETE;
- ret.success = c->next & 1u;
- ret.tag = c->tag;
- c->done (&exec_ctx, c->done_arg, c);
- break;
- }
- if (cc->shutdown)
- {
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- memset (&ret, 0, sizeof (ret));
- ret.type = GRPC_QUEUE_SHUTDOWN;
- break;
- }
- now = gpr_now (GPR_CLOCK_MONOTONIC);
- if (!first_loop && gpr_time_cmp (now, deadline) >= 0)
- {
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- memset (&ret, 0, sizeof (ret));
- ret.type = GRPC_QUEUE_TIMEOUT;
- break;
- }
- first_loop = 0;
- grpc_pollset_work (&exec_ctx, &cc->pollset, &worker, now, deadline);
+ GRPC_CQ_INTERNAL_REF(cc, "next");
+ gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
+ for (;;) {
+ if (cc->completed_tail != &cc->completed_head) {
+ grpc_cq_completion *c = (grpc_cq_completion *)cc->completed_head.next;
+ cc->completed_head.next = c->next & ~(gpr_uintptr)1;
+ if (c == cc->completed_tail) {
+ cc->completed_tail = &cc->completed_head;
+ }
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ ret.type = GRPC_OP_COMPLETE;
+ ret.success = c->next & 1u;
+ ret.tag = c->tag;
+ c->done(&exec_ctx, c->done_arg, c);
+ break;
+ }
+ if (cc->shutdown) {
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ memset(&ret, 0, sizeof(ret));
+ ret.type = GRPC_QUEUE_SHUTDOWN;
+ break;
+ }
+ now = gpr_now(GPR_CLOCK_MONOTONIC);
+ if (!first_loop && gpr_time_cmp(now, deadline) >= 0) {
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ memset(&ret, 0, sizeof(ret));
+ ret.type = GRPC_QUEUE_TIMEOUT;
+ break;
}
- GRPC_SURFACE_TRACE_RETURNED_EVENT (cc, &ret);
- GRPC_CQ_INTERNAL_UNREF (cc, "next");
- grpc_exec_ctx_finish (&exec_ctx);
+ first_loop = 0;
+ grpc_pollset_work(&exec_ctx, &cc->pollset, &worker, now, deadline);
+ }
+ GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
+ GRPC_CQ_INTERNAL_UNREF(cc, "next");
+ grpc_exec_ctx_finish(&exec_ctx);
return ret;
}
-static int
-add_plucker (grpc_completion_queue * cc, void *tag, grpc_pollset_worker * worker)
-{
- if (cc->num_pluckers == GRPC_MAX_COMPLETION_QUEUE_PLUCKERS)
- {
- return 0;
- }
+static int add_plucker(grpc_completion_queue *cc, void *tag,
+ grpc_pollset_worker *worker) {
+ if (cc->num_pluckers == GRPC_MAX_COMPLETION_QUEUE_PLUCKERS) {
+ return 0;
+ }
cc->pluckers[cc->num_pluckers].tag = tag;
cc->pluckers[cc->num_pluckers].worker = worker;
cc->num_pluckers++;
return 1;
}
-static void
-del_plucker (grpc_completion_queue * cc, void *tag, grpc_pollset_worker * worker)
-{
+static void del_plucker(grpc_completion_queue *cc, void *tag,
+ grpc_pollset_worker *worker) {
int i;
- for (i = 0; i < cc->num_pluckers; i++)
- {
- if (cc->pluckers[i].tag == tag && cc->pluckers[i].worker == worker)
- {
- cc->num_pluckers--;
- GPR_SWAP (plucker, cc->pluckers[i], cc->pluckers[cc->num_pluckers]);
- return;
- }
+ for (i = 0; i < cc->num_pluckers; i++) {
+ if (cc->pluckers[i].tag == tag && cc->pluckers[i].worker == worker) {
+ cc->num_pluckers--;
+ GPR_SWAP(plucker, cc->pluckers[i], cc->pluckers[cc->num_pluckers]);
+ return;
}
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
}
-grpc_event
-grpc_completion_queue_pluck (grpc_completion_queue * cc, void *tag, gpr_timespec deadline, void *reserved)
-{
+grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag,
+ gpr_timespec deadline, void *reserved) {
grpc_event ret;
grpc_cq_completion *c;
grpc_cq_completion *prev;
@@ -282,116 +259,98 @@ grpc_completion_queue_pluck (grpc_completion_queue * cc, void *tag, gpr_timespec
int first_loop = 1;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- GPR_ASSERT (!reserved);
+ GPR_ASSERT(!reserved);
- deadline = gpr_convert_clock_type (deadline, GPR_CLOCK_MONOTONIC);
+ deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
- GRPC_CQ_INTERNAL_REF (cc, "pluck");
- gpr_mu_lock (GRPC_POLLSET_MU (&cc->pollset));
- for (;;)
- {
- prev = &cc->completed_head;
- while ((c = (grpc_cq_completion *) (prev->next & ~(gpr_uintptr) 1)) != &cc->completed_head)
- {
- if (c->tag == tag)
- {
- prev->next = (prev->next & (gpr_uintptr) 1) | (c->next & ~(gpr_uintptr) 1);
- if (c == cc->completed_tail)
- {
- cc->completed_tail = prev;
- }
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- ret.type = GRPC_OP_COMPLETE;
- ret.success = c->next & 1u;
- ret.tag = c->tag;
- c->done (&exec_ctx, c->done_arg, c);
- goto done;
- }
- prev = c;
- }
- if (cc->shutdown)
- {
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- memset (&ret, 0, sizeof (ret));
- ret.type = GRPC_QUEUE_SHUTDOWN;
- break;
- }
- if (!add_plucker (cc, tag, &worker))
- {
- gpr_log (GPR_DEBUG, "Too many outstanding grpc_completion_queue_pluck calls: maximum " "is %d", GRPC_MAX_COMPLETION_QUEUE_PLUCKERS);
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- memset (&ret, 0, sizeof (ret));
- /* TODO(ctiller): should we use a different result here */
- ret.type = GRPC_QUEUE_TIMEOUT;
- break;
- }
- now = gpr_now (GPR_CLOCK_MONOTONIC);
- if (!first_loop && gpr_time_cmp (now, deadline) >= 0)
- {
- del_plucker (cc, tag, &worker);
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- memset (&ret, 0, sizeof (ret));
- ret.type = GRPC_QUEUE_TIMEOUT;
- break;
- }
- first_loop = 0;
- grpc_pollset_work (&exec_ctx, &cc->pollset, &worker, now, deadline);
- del_plucker (cc, tag, &worker);
+ GRPC_CQ_INTERNAL_REF(cc, "pluck");
+ gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
+ for (;;) {
+ prev = &cc->completed_head;
+ while ((c = (grpc_cq_completion *)(prev->next & ~(gpr_uintptr)1)) !=
+ &cc->completed_head) {
+ if (c->tag == tag) {
+ prev->next =
+ (prev->next & (gpr_uintptr)1) | (c->next & ~(gpr_uintptr)1);
+ if (c == cc->completed_tail) {
+ cc->completed_tail = prev;
+ }
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ ret.type = GRPC_OP_COMPLETE;
+ ret.success = c->next & 1u;
+ ret.tag = c->tag;
+ c->done(&exec_ctx, c->done_arg, c);
+ goto done;
+ }
+ prev = c;
+ }
+ if (cc->shutdown) {
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ memset(&ret, 0, sizeof(ret));
+ ret.type = GRPC_QUEUE_SHUTDOWN;
+ break;
}
+ if (!add_plucker(cc, tag, &worker)) {
+ gpr_log(GPR_DEBUG,
+ "Too many outstanding grpc_completion_queue_pluck calls: maximum "
+ "is %d",
+ GRPC_MAX_COMPLETION_QUEUE_PLUCKERS);
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ memset(&ret, 0, sizeof(ret));
+ /* TODO(ctiller): should we use a different result here */
+ ret.type = GRPC_QUEUE_TIMEOUT;
+ break;
+ }
+ now = gpr_now(GPR_CLOCK_MONOTONIC);
+ if (!first_loop && gpr_time_cmp(now, deadline) >= 0) {
+ del_plucker(cc, tag, &worker);
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ memset(&ret, 0, sizeof(ret));
+ ret.type = GRPC_QUEUE_TIMEOUT;
+ break;
+ }
+ first_loop = 0;
+ grpc_pollset_work(&exec_ctx, &cc->pollset, &worker, now, deadline);
+ del_plucker(cc, tag, &worker);
+ }
done:
- GRPC_SURFACE_TRACE_RETURNED_EVENT (cc, &ret);
- GRPC_CQ_INTERNAL_UNREF (cc, "pluck");
- grpc_exec_ctx_finish (&exec_ctx);
+ GRPC_SURFACE_TRACE_RETURNED_EVENT(cc, &ret);
+ GRPC_CQ_INTERNAL_UNREF(cc, "pluck");
+ grpc_exec_ctx_finish(&exec_ctx);
return ret;
}
/* Shutdown simply drops a ref that we reserved at creation time; if we drop
to zero here, then enter shutdown mode and wake up any waiters */
-void
-grpc_completion_queue_shutdown (grpc_completion_queue * cc)
-{
+void grpc_completion_queue_shutdown(grpc_completion_queue *cc) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- gpr_mu_lock (GRPC_POLLSET_MU (&cc->pollset));
- if (cc->shutdown_called)
- {
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- return;
- }
+ gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
+ if (cc->shutdown_called) {
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ return;
+ }
cc->shutdown_called = 1;
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
- if (gpr_unref (&cc->pending_events))
- {
- gpr_mu_lock (GRPC_POLLSET_MU (&cc->pollset));
- GPR_ASSERT (!cc->shutdown);
- cc->shutdown = 1;
- gpr_mu_unlock (GRPC_POLLSET_MU (&cc->pollset));
- grpc_pollset_shutdown (&exec_ctx, &cc->pollset, &cc->pollset_destroy_done);
- }
- grpc_exec_ctx_finish (&exec_ctx);
+ if (gpr_unref(&cc->pending_events)) {
+ gpr_mu_lock(GRPC_POLLSET_MU(&cc->pollset));
+ GPR_ASSERT(!cc->shutdown);
+ cc->shutdown = 1;
+ gpr_mu_unlock(GRPC_POLLSET_MU(&cc->pollset));
+ grpc_pollset_shutdown(&exec_ctx, &cc->pollset, &cc->pollset_destroy_done);
+ }
+ grpc_exec_ctx_finish(&exec_ctx);
}
-void
-grpc_completion_queue_destroy (grpc_completion_queue * cc)
-{
- grpc_completion_queue_shutdown (cc);
- GRPC_CQ_INTERNAL_UNREF (cc, "destroy");
+void grpc_completion_queue_destroy(grpc_completion_queue *cc) {
+ grpc_completion_queue_shutdown(cc);
+ GRPC_CQ_INTERNAL_UNREF(cc, "destroy");
}
-grpc_pollset *
-grpc_cq_pollset (grpc_completion_queue * cc)
-{
+grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc) {
return &cc->pollset;
}
-void
-grpc_cq_mark_server_cq (grpc_completion_queue * cc)
-{
- cc->is_server_cq = 1;
-}
+void grpc_cq_mark_server_cq(grpc_completion_queue *cc) { cc->is_server_cq = 1; }
-int
-grpc_cq_is_server_cq (grpc_completion_queue * cc)
-{
- return cc->is_server_cq;
-}
+int grpc_cq_is_server_cq(grpc_completion_queue *cc) { return cc->is_server_cq; }
diff --git a/src/core/surface/completion_queue.h b/src/core/surface/completion_queue.h
index d9b06223a8..5f8282e542 100644
--- a/src/core/surface/completion_queue.h
+++ b/src/core/surface/completion_queue.h
@@ -39,42 +39,48 @@
#include "src/core/iomgr/pollset.h"
#include <grpc/grpc.h>
-typedef struct grpc_cq_completion
-{
+typedef struct grpc_cq_completion {
/** user supplied tag */
void *tag;
/** done callback - called when this queue element is no longer
needed by the completion queue */
- void (*done) (grpc_exec_ctx * exec_ctx, void *done_arg, struct grpc_cq_completion * c);
+ void (*done)(grpc_exec_ctx *exec_ctx, void *done_arg,
+ struct grpc_cq_completion *c);
void *done_arg;
/** next pointer; low bit is used to indicate success or not */
gpr_uintptr next;
} grpc_cq_completion;
#ifdef GRPC_CQ_REF_COUNT_DEBUG
-void grpc_cq_internal_ref (grpc_completion_queue * cc, const char *reason, const char *file, int line);
-void grpc_cq_internal_unref (grpc_completion_queue * cc, const char *reason, const char *file, int line);
+void grpc_cq_internal_ref(grpc_completion_queue *cc, const char *reason,
+ const char *file, int line);
+void grpc_cq_internal_unref(grpc_completion_queue *cc, const char *reason,
+ const char *file, int line);
#define GRPC_CQ_INTERNAL_REF(cc, reason) \
grpc_cq_internal_ref(cc, reason, __FILE__, __LINE__)
#define GRPC_CQ_INTERNAL_UNREF(cc, reason) \
grpc_cq_internal_unref(cc, reason, __FILE__, __LINE__)
#else
-void grpc_cq_internal_ref (grpc_completion_queue * cc);
-void grpc_cq_internal_unref (grpc_completion_queue * cc);
+void grpc_cq_internal_ref(grpc_completion_queue *cc);
+void grpc_cq_internal_unref(grpc_completion_queue *cc);
#define GRPC_CQ_INTERNAL_REF(cc, reason) grpc_cq_internal_ref(cc)
#define GRPC_CQ_INTERNAL_UNREF(cc, reason) grpc_cq_internal_unref(cc)
#endif
/* Flag that an operation is beginning: the completion channel will not finish
shutdown until a corrensponding grpc_cq_end_* call is made */
-void grpc_cq_begin_op (grpc_completion_queue * cc);
+void grpc_cq_begin_op(grpc_completion_queue *cc);
/* Queue a GRPC_OP_COMPLETED operation */
-void grpc_cq_end_op (grpc_exec_ctx * exec_ctx, grpc_completion_queue * cc, void *tag, int success, void (*done) (grpc_exec_ctx * exec_ctx,void *done_arg, grpc_cq_completion * storage), void *done_arg, grpc_cq_completion * storage);
+void grpc_cq_end_op(grpc_exec_ctx *exec_ctx, grpc_completion_queue *cc,
+ void *tag, int success,
+ void (*done)(grpc_exec_ctx *exec_ctx, void *done_arg,
+ grpc_cq_completion *storage),
+ void *done_arg, grpc_cq_completion *storage);
-grpc_pollset *grpc_cq_pollset (grpc_completion_queue * cc);
+grpc_pollset *grpc_cq_pollset(grpc_completion_queue *cc);
-void grpc_cq_mark_server_cq (grpc_completion_queue * cc);
-int grpc_cq_is_server_cq (grpc_completion_queue * cc);
+void grpc_cq_mark_server_cq(grpc_completion_queue *cc);
+int grpc_cq_is_server_cq(grpc_completion_queue *cc);
#endif /* GRPC_INTERNAL_CORE_SURFACE_COMPLETION_QUEUE_H */
diff --git a/src/core/surface/event_string.c b/src/core/surface/event_string.c
index ac5cb657cd..33cd4a43aa 100644
--- a/src/core/surface/event_string.c
+++ b/src/core/surface/event_string.c
@@ -39,55 +39,43 @@
#include <grpc/byte_buffer.h>
#include <grpc/support/string_util.h>
-static void
-addhdr (gpr_strvec * buf, grpc_event * ev)
-{
+static void addhdr(gpr_strvec *buf, grpc_event *ev) {
char *tmp;
- gpr_asprintf (&tmp, "tag:%p", ev->tag);
- gpr_strvec_add (buf, tmp);
+ gpr_asprintf(&tmp, "tag:%p", ev->tag);
+ gpr_strvec_add(buf, tmp);
}
-static const char *
-errstr (int success)
-{
- return success ? "OK" : "ERROR";
-}
+static const char *errstr(int success) { return success ? "OK" : "ERROR"; }
-static void
-adderr (gpr_strvec * buf, int success)
-{
+static void adderr(gpr_strvec *buf, int success) {
char *tmp;
- gpr_asprintf (&tmp, " %s", errstr (success));
- gpr_strvec_add (buf, tmp);
+ gpr_asprintf(&tmp, " %s", errstr(success));
+ gpr_strvec_add(buf, tmp);
}
-char *
-grpc_event_string (grpc_event * ev)
-{
+char *grpc_event_string(grpc_event *ev) {
char *out;
gpr_strvec buf;
- if (ev == NULL)
- return gpr_strdup ("null");
+ if (ev == NULL) return gpr_strdup("null");
- gpr_strvec_init (&buf);
+ gpr_strvec_init(&buf);
- switch (ev->type)
- {
+ switch (ev->type) {
case GRPC_QUEUE_TIMEOUT:
- gpr_strvec_add (&buf, gpr_strdup ("QUEUE_TIMEOUT"));
+ gpr_strvec_add(&buf, gpr_strdup("QUEUE_TIMEOUT"));
break;
case GRPC_QUEUE_SHUTDOWN:
- gpr_strvec_add (&buf, gpr_strdup ("QUEUE_SHUTDOWN"));
+ gpr_strvec_add(&buf, gpr_strdup("QUEUE_SHUTDOWN"));
break;
case GRPC_OP_COMPLETE:
- gpr_strvec_add (&buf, gpr_strdup ("OP_COMPLETE: "));
- addhdr (&buf, ev);
- adderr (&buf, ev->success);
+ gpr_strvec_add(&buf, gpr_strdup("OP_COMPLETE: "));
+ addhdr(&buf, ev);
+ adderr(&buf, ev->success);
break;
- }
+ }
- out = gpr_strvec_flatten (&buf, NULL);
- gpr_strvec_destroy (&buf);
+ out = gpr_strvec_flatten(&buf, NULL);
+ gpr_strvec_destroy(&buf);
return out;
}
diff --git a/src/core/surface/event_string.h b/src/core/surface/event_string.h
index 6f2dfe0282..07c474e3a0 100644
--- a/src/core/surface/event_string.h
+++ b/src/core/surface/event_string.h
@@ -37,6 +37,6 @@
#include <grpc/grpc.h>
/* Returns a string describing an event. Must be later freed with gpr_free() */
-char *grpc_event_string (grpc_event * ev);
+char *grpc_event_string(grpc_event *ev);
#endif /* GRPC_INTERNAL_CORE_SURFACE_EVENT_STRING_H */
diff --git a/src/core/surface/init.c b/src/core/surface/init.c
index 4d0be4f7b9..93c27c77bf 100644
--- a/src/core/surface/init.c
+++ b/src/core/surface/init.c
@@ -61,110 +61,91 @@ static gpr_once g_basic_init = GPR_ONCE_INIT;
static gpr_mu g_init_mu;
static int g_initializations;
-static void
-do_basic_init (void)
-{
- gpr_mu_init (&g_init_mu);
+static void do_basic_init(void) {
+ gpr_mu_init(&g_init_mu);
g_initializations = 0;
}
-typedef struct grpc_plugin
-{
- void (*init) ();
- void (*destroy) ();
+typedef struct grpc_plugin {
+ void (*init)();
+ void (*destroy)();
} grpc_plugin;
static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
static int g_number_of_plugins = 0;
-void
-grpc_register_plugin (void (*init) (void), void (*destroy) (void))
-{
- GPR_ASSERT (g_number_of_plugins != MAX_PLUGINS);
+void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
+ GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
g_all_of_the_plugins[g_number_of_plugins].init = init;
g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
g_number_of_plugins++;
}
-void
-grpc_init (void)
-{
+void grpc_init(void) {
int i;
- gpr_once_init (&g_basic_init, do_basic_init);
+ gpr_once_init(&g_basic_init, do_basic_init);
- gpr_mu_lock (&g_init_mu);
- if (++g_initializations == 1)
- {
- gpr_time_init ();
- grpc_lb_policy_registry_init (grpc_pick_first_lb_factory_create ());
- grpc_register_lb_policy (grpc_pick_first_lb_factory_create ());
- grpc_register_lb_policy (grpc_round_robin_lb_factory_create ());
- grpc_resolver_registry_init ("dns:///");
- grpc_register_resolver_type (grpc_dns_resolver_factory_create ());
- grpc_register_resolver_type (grpc_ipv4_resolver_factory_create ());
- grpc_register_resolver_type (grpc_ipv6_resolver_factory_create ());
+ gpr_mu_lock(&g_init_mu);
+ if (++g_initializations == 1) {
+ gpr_time_init();
+ grpc_lb_policy_registry_init(grpc_pick_first_lb_factory_create());
+ grpc_register_lb_policy(grpc_pick_first_lb_factory_create());
+ grpc_register_lb_policy(grpc_round_robin_lb_factory_create());
+ grpc_resolver_registry_init("dns:///");
+ grpc_register_resolver_type(grpc_dns_resolver_factory_create());
+ grpc_register_resolver_type(grpc_ipv4_resolver_factory_create());
+ grpc_register_resolver_type(grpc_ipv6_resolver_factory_create());
#ifdef GPR_POSIX_SOCKET
- grpc_register_resolver_type (grpc_unix_resolver_factory_create ());
+ grpc_register_resolver_type(grpc_unix_resolver_factory_create());
#endif
- grpc_register_tracer ("channel", &grpc_trace_channel);
- grpc_register_tracer ("surface", &grpc_surface_trace);
- grpc_register_tracer ("http", &grpc_http_trace);
- grpc_register_tracer ("flowctl", &grpc_flowctl_trace);
- grpc_register_tracer ("batch", &grpc_trace_batch);
- grpc_register_tracer ("connectivity_state", &grpc_connectivity_state_trace);
- grpc_security_pre_init ();
- grpc_iomgr_init ();
- grpc_tracer_init ("GRPC_TRACE");
- /* Only initialize census if noone else has. */
- if (census_enabled () == CENSUS_FEATURE_NONE)
- {
- if (census_initialize (census_supported ()))
- { /* enable all features. */
- gpr_log (GPR_ERROR, "Could not initialize census.");
- }
- }
- grpc_timers_global_init ();
- for (i = 0; i < g_number_of_plugins; i++)
- {
- if (g_all_of_the_plugins[i].init != NULL)
- {
- g_all_of_the_plugins[i].init ();
- }
- }
+ grpc_register_tracer("channel", &grpc_trace_channel);
+ grpc_register_tracer("surface", &grpc_surface_trace);
+ grpc_register_tracer("http", &grpc_http_trace);
+ grpc_register_tracer("flowctl", &grpc_flowctl_trace);
+ grpc_register_tracer("batch", &grpc_trace_batch);
+ grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace);
+ grpc_security_pre_init();
+ grpc_iomgr_init();
+ grpc_tracer_init("GRPC_TRACE");
+ /* Only initialize census if noone else has. */
+ if (census_enabled() == CENSUS_FEATURE_NONE) {
+ if (census_initialize(census_supported())) { /* enable all features. */
+ gpr_log(GPR_ERROR, "Could not initialize census.");
+ }
}
- gpr_mu_unlock (&g_init_mu);
+ grpc_timers_global_init();
+ for (i = 0; i < g_number_of_plugins; i++) {
+ if (g_all_of_the_plugins[i].init != NULL) {
+ g_all_of_the_plugins[i].init();
+ }
+ }
+ }
+ gpr_mu_unlock(&g_init_mu);
}
-void
-grpc_shutdown (void)
-{
+void grpc_shutdown(void) {
int i;
- gpr_mu_lock (&g_init_mu);
- if (--g_initializations == 0)
- {
- grpc_iomgr_shutdown ();
- census_shutdown ();
- grpc_timers_global_destroy ();
- grpc_tracer_shutdown ();
- grpc_resolver_registry_shutdown ();
- for (i = 0; i < g_number_of_plugins; i++)
- {
- if (g_all_of_the_plugins[i].destroy != NULL)
- {
- g_all_of_the_plugins[i].destroy ();
- }
- }
+ gpr_mu_lock(&g_init_mu);
+ if (--g_initializations == 0) {
+ grpc_iomgr_shutdown();
+ census_shutdown();
+ grpc_timers_global_destroy();
+ grpc_tracer_shutdown();
+ grpc_resolver_registry_shutdown();
+ for (i = 0; i < g_number_of_plugins; i++) {
+ if (g_all_of_the_plugins[i].destroy != NULL) {
+ g_all_of_the_plugins[i].destroy();
+ }
}
- gpr_mu_unlock (&g_init_mu);
+ }
+ gpr_mu_unlock(&g_init_mu);
}
-int
-grpc_is_initialized (void)
-{
+int grpc_is_initialized(void) {
int r;
- gpr_once_init (&g_basic_init, do_basic_init);
- gpr_mu_lock (&g_init_mu);
+ gpr_once_init(&g_basic_init, do_basic_init);
+ gpr_mu_lock(&g_init_mu);
r = g_initializations > 0;
- gpr_mu_unlock (&g_init_mu);
+ gpr_mu_unlock(&g_init_mu);
return r;
}
diff --git a/src/core/surface/init.h b/src/core/surface/init.h
index e880e3b2c0..771c30f412 100644
--- a/src/core/surface/init.h
+++ b/src/core/surface/init.h
@@ -34,7 +34,7 @@
#ifndef GRPC_INTERNAL_CORE_SURFACE_INIT_H
#define GRPC_INTERNAL_CORE_SURFACE_INIT_H
-void grpc_security_pre_init (void);
-int grpc_is_initialized (void);
+void grpc_security_pre_init(void);
+int grpc_is_initialized(void);
#endif /* GRPC_INTERNAL_CORE_SURFACE_INIT_H */
diff --git a/src/core/surface/init_secure.c b/src/core/surface/init_secure.c
index 2ae1dd8b6f..fa20e91583 100644
--- a/src/core/surface/init_secure.c
+++ b/src/core/surface/init_secure.c
@@ -36,9 +36,7 @@
#include "src/core/security/secure_endpoint.h"
#include "src/core/tsi/transport_security_interface.h"
-void
-grpc_security_pre_init (void)
-{
- grpc_register_tracer ("secure_endpoint", &grpc_trace_secure_endpoint);
- grpc_register_tracer ("transport_security", &tsi_tracing_enabled);
+void grpc_security_pre_init(void) {
+ grpc_register_tracer("secure_endpoint", &grpc_trace_secure_endpoint);
+ grpc_register_tracer("transport_security", &tsi_tracing_enabled);
}
diff --git a/src/core/surface/init_unsecure.c b/src/core/surface/init_unsecure.c
index 5b8a0abf6f..630d564a7d 100644
--- a/src/core/surface/init_unsecure.c
+++ b/src/core/surface/init_unsecure.c
@@ -33,7 +33,4 @@
#include "src/core/surface/init.h"
-void
-grpc_security_pre_init (void)
-{
-}
+void grpc_security_pre_init(void) {}
diff --git a/src/core/surface/lame_client.c b/src/core/surface/lame_client.c
index 2d78623590..0b22d225d0 100644
--- a/src/core/surface/lame_client.c
+++ b/src/core/surface/lame_client.c
@@ -42,135 +42,126 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-typedef struct
-{
+typedef struct {
grpc_linked_mdelem status;
grpc_linked_mdelem details;
} call_data;
-typedef struct
-{
+typedef struct {
grpc_mdctx *mdctx;
grpc_channel *master;
grpc_status_code error_code;
const char *error_message;
} channel_data;
-static void
-lame_start_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void lame_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- GRPC_CALL_LOG_OP (GPR_INFO, elem, op);
- if (op->send_ops != NULL)
- {
- grpc_stream_ops_unref_owned_objects (op->send_ops->ops, op->send_ops->nops);
- op->on_done_send->cb (exec_ctx, op->on_done_send->cb_arg, 0);
- }
- if (op->recv_ops != NULL)
- {
- char tmp[GPR_LTOA_MIN_BUFSIZE];
- grpc_metadata_batch mdb;
- gpr_ltoa (chand->error_code, tmp);
- calld->status.md = grpc_mdelem_from_strings (chand->mdctx, "grpc-status", tmp);
- calld->details.md = grpc_mdelem_from_strings (chand->mdctx, "grpc-message", chand->error_message);
- calld->status.prev = calld->details.next = NULL;
- calld->status.next = &calld->details;
- calld->details.prev = &calld->status;
- mdb.list.head = &calld->status;
- mdb.list.tail = &calld->details;
- mdb.garbage.head = mdb.garbage.tail = NULL;
- mdb.deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
- grpc_sopb_add_metadata (op->recv_ops, mdb);
- *op->recv_state = GRPC_STREAM_CLOSED;
- op->on_done_recv->cb (exec_ctx, op->on_done_recv->cb_arg, 1);
- }
- if (op->on_consumed != NULL)
- {
- op->on_consumed->cb (exec_ctx, op->on_consumed->cb_arg, 0);
- }
+ GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
+ if (op->send_ops != NULL) {
+ grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops);
+ op->on_done_send->cb(exec_ctx, op->on_done_send->cb_arg, 0);
+ }
+ if (op->recv_ops != NULL) {
+ char tmp[GPR_LTOA_MIN_BUFSIZE];
+ grpc_metadata_batch mdb;
+ gpr_ltoa(chand->error_code, tmp);
+ calld->status.md =
+ grpc_mdelem_from_strings(chand->mdctx, "grpc-status", tmp);
+ calld->details.md = grpc_mdelem_from_strings(chand->mdctx, "grpc-message",
+ chand->error_message);
+ calld->status.prev = calld->details.next = NULL;
+ calld->status.next = &calld->details;
+ calld->details.prev = &calld->status;
+ mdb.list.head = &calld->status;
+ mdb.list.tail = &calld->details;
+ mdb.garbage.head = mdb.garbage.tail = NULL;
+ mdb.deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
+ grpc_sopb_add_metadata(op->recv_ops, mdb);
+ *op->recv_state = GRPC_STREAM_CLOSED;
+ op->on_done_recv->cb(exec_ctx, op->on_done_recv->cb_arg, 1);
+ }
+ if (op->on_consumed != NULL) {
+ op->on_consumed->cb(exec_ctx, op->on_consumed->cb_arg, 0);
+ }
}
-static char *
-lame_get_peer (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static char *lame_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
channel_data *chand = elem->channel_data;
- return grpc_channel_get_target (chand->master);
+ return grpc_channel_get_target(chand->master);
}
-static void
-lame_start_transport_op (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_transport_op * op)
-{
- if (op->on_connectivity_state_change)
- {
- GPR_ASSERT (*op->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE);
- *op->connectivity_state = GRPC_CHANNEL_FATAL_FAILURE;
- op->on_connectivity_state_change->cb (exec_ctx, op->on_connectivity_state_change->cb_arg, 1);
- }
- if (op->on_consumed != NULL)
- {
- op->on_consumed->cb (exec_ctx, op->on_consumed->cb_arg, 1);
- }
+static void lame_start_transport_op(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem,
+ grpc_transport_op *op) {
+ if (op->on_connectivity_state_change) {
+ GPR_ASSERT(*op->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE);
+ *op->connectivity_state = GRPC_CHANNEL_FATAL_FAILURE;
+ op->on_connectivity_state_change->cb(
+ exec_ctx, op->on_connectivity_state_change->cb_arg, 1);
+ }
+ if (op->on_consumed != NULL) {
+ op->on_consumed->cb(exec_ctx, op->on_consumed->cb_arg, 1);
+ }
}
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *transport_server_data, grpc_transport_stream_op * initial_op)
-{
- if (initial_op)
- {
- grpc_transport_stream_op_finish_with_failure (exec_ctx, initial_op);
- }
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *transport_server_data,
+ grpc_transport_stream_op *initial_op) {
+ if (initial_op) {
+ grpc_transport_stream_op_finish_with_failure(exec_ctx, initial_op);
+ }
}
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
-}
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {}
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * mdctx, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args, grpc_mdctx *mdctx,
+ int is_first, int is_last) {
channel_data *chand = elem->channel_data;
- GPR_ASSERT (is_first);
- GPR_ASSERT (is_last);
+ GPR_ASSERT(is_first);
+ GPR_ASSERT(is_last);
chand->mdctx = mdctx;
chand->master = master;
}
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
-}
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {}
static const grpc_channel_filter lame_filter = {
- lame_start_transport_stream_op,
- lame_start_transport_op,
- sizeof (call_data),
- init_call_elem,
- destroy_call_elem,
- sizeof (channel_data),
- init_channel_elem,
- destroy_channel_elem,
- lame_get_peer,
- "lame-client",
+ lame_start_transport_stream_op,
+ lame_start_transport_op,
+ sizeof(call_data),
+ init_call_elem,
+ destroy_call_elem,
+ sizeof(channel_data),
+ init_channel_elem,
+ destroy_channel_elem,
+ lame_get_peer,
+ "lame-client",
};
#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
-grpc_channel *
-grpc_lame_client_channel_create (const char *target, grpc_status_code error_code, const char *error_message)
-{
+grpc_channel *grpc_lame_client_channel_create(const char *target,
+ grpc_status_code error_code,
+ const char *error_message) {
grpc_channel *channel;
grpc_channel_element *elem;
channel_data *chand;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- static const grpc_channel_filter *filters[] = { &lame_filter };
- channel = grpc_channel_create_from_filters (&exec_ctx, target, filters, 1, NULL, grpc_mdctx_create (), 1);
- elem = grpc_channel_stack_element (grpc_channel_get_channel_stack (channel), 0);
- GPR_ASSERT (elem->filter == &lame_filter);
- chand = (channel_data *) elem->channel_data;
+ static const grpc_channel_filter *filters[] = {&lame_filter};
+ channel = grpc_channel_create_from_filters(&exec_ctx, target, filters, 1,
+ NULL, grpc_mdctx_create(), 1);
+ elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
+ GPR_ASSERT(elem->filter == &lame_filter);
+ chand = (channel_data *)elem->channel_data;
chand->error_code = error_code;
chand->error_message = error_message;
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
return channel;
}
diff --git a/src/core/surface/metadata_array.c b/src/core/surface/metadata_array.c
index 5abc582f64..648c579266 100644
--- a/src/core/surface/metadata_array.c
+++ b/src/core/surface/metadata_array.c
@@ -36,14 +36,10 @@
#include <string.h>
-void
-grpc_metadata_array_init (grpc_metadata_array * array)
-{
- memset (array, 0, sizeof (*array));
+void grpc_metadata_array_init(grpc_metadata_array* array) {
+ memset(array, 0, sizeof(*array));
}
-void
-grpc_metadata_array_destroy (grpc_metadata_array * array)
-{
- gpr_free (array->metadata);
+void grpc_metadata_array_destroy(grpc_metadata_array* array) {
+ gpr_free(array->metadata);
}
diff --git a/src/core/surface/secure_channel_create.c b/src/core/surface/secure_channel_create.c
index dbbf6f5140..ae2eaad896 100644
--- a/src/core/surface/secure_channel_create.c
+++ b/src/core/surface/secure_channel_create.c
@@ -51,8 +51,7 @@
#include "src/core/transport/chttp2_transport.h"
#include "src/core/tsi/transport_security_interface.h"
-typedef struct
-{
+typedef struct {
grpc_connector base;
gpr_refcount refs;
@@ -71,120 +70,107 @@ typedef struct
grpc_mdctx *mdctx;
} connector;
-static void
-connector_ref (grpc_connector * con)
-{
- connector *c = (connector *) con;
- gpr_ref (&c->refs);
+static void connector_ref(grpc_connector *con) {
+ connector *c = (connector *)con;
+ gpr_ref(&c->refs);
}
-static void
-connector_unref (grpc_exec_ctx * exec_ctx, grpc_connector * con)
-{
- connector *c = (connector *) con;
- if (gpr_unref (&c->refs))
- {
- grpc_mdctx_unref (c->mdctx);
- gpr_free (c);
- }
+static void connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *con) {
+ connector *c = (connector *)con;
+ if (gpr_unref(&c->refs)) {
+ grpc_mdctx_unref(c->mdctx);
+ gpr_free(c);
+ }
}
-static void
-on_secure_handshake_done (grpc_exec_ctx * exec_ctx, void *arg, grpc_security_status status, grpc_endpoint * wrapped_endpoint, grpc_endpoint * secure_endpoint)
-{
+static void on_secure_handshake_done(grpc_exec_ctx *exec_ctx, void *arg,
+ grpc_security_status status,
+ grpc_endpoint *wrapped_endpoint,
+ grpc_endpoint *secure_endpoint) {
connector *c = arg;
grpc_closure *notify;
- gpr_mu_lock (&c->mu);
- if (c->connecting_endpoint == NULL)
- {
- memset (c->result, 0, sizeof (*c->result));
- gpr_mu_unlock (&c->mu);
- }
- else if (status != GRPC_SECURITY_OK)
- {
- GPR_ASSERT (c->connecting_endpoint == wrapped_endpoint);
- gpr_log (GPR_ERROR, "Secure handshake failed with error %d.", status);
- memset (c->result, 0, sizeof (*c->result));
- c->connecting_endpoint = NULL;
- gpr_mu_unlock (&c->mu);
- }
- else
- {
- GPR_ASSERT (c->connecting_endpoint == wrapped_endpoint);
- c->connecting_endpoint = NULL;
- gpr_mu_unlock (&c->mu);
- c->result->transport = grpc_create_chttp2_transport (exec_ctx, c->args.channel_args, secure_endpoint, c->mdctx, 1);
- grpc_chttp2_transport_start_reading (exec_ctx, c->result->transport, NULL, 0);
- c->result->filters = gpr_malloc (sizeof (grpc_channel_filter *) * 2);
- c->result->filters[0] = &grpc_http_client_filter;
- c->result->filters[1] = &grpc_client_auth_filter;
- c->result->num_filters = 2;
- }
+ gpr_mu_lock(&c->mu);
+ if (c->connecting_endpoint == NULL) {
+ memset(c->result, 0, sizeof(*c->result));
+ gpr_mu_unlock(&c->mu);
+ } else if (status != GRPC_SECURITY_OK) {
+ GPR_ASSERT(c->connecting_endpoint == wrapped_endpoint);
+ gpr_log(GPR_ERROR, "Secure handshake failed with error %d.", status);
+ memset(c->result, 0, sizeof(*c->result));
+ c->connecting_endpoint = NULL;
+ gpr_mu_unlock(&c->mu);
+ } else {
+ GPR_ASSERT(c->connecting_endpoint == wrapped_endpoint);
+ c->connecting_endpoint = NULL;
+ gpr_mu_unlock(&c->mu);
+ c->result->transport = grpc_create_chttp2_transport(
+ exec_ctx, c->args.channel_args, secure_endpoint, c->mdctx, 1);
+ grpc_chttp2_transport_start_reading(exec_ctx, c->result->transport, NULL,
+ 0);
+ c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *) * 2);
+ c->result->filters[0] = &grpc_http_client_filter;
+ c->result->filters[1] = &grpc_client_auth_filter;
+ c->result->num_filters = 2;
+ }
notify = c->notify;
c->notify = NULL;
- notify->cb (exec_ctx, notify->cb_arg, 1);
+ notify->cb(exec_ctx, notify->cb_arg, 1);
}
-static void
-connected (grpc_exec_ctx * exec_ctx, void *arg, int success)
-{
+static void connected(grpc_exec_ctx *exec_ctx, void *arg, int success) {
connector *c = arg;
grpc_closure *notify;
grpc_endpoint *tcp = c->newly_connecting_endpoint;
- if (tcp != NULL)
- {
- gpr_mu_lock (&c->mu);
- GPR_ASSERT (c->connecting_endpoint == NULL);
- c->connecting_endpoint = tcp;
- gpr_mu_unlock (&c->mu);
- grpc_security_connector_do_handshake (exec_ctx, &c->security_connector->base, tcp, on_secure_handshake_done, c);
- }
- else
- {
- memset (c->result, 0, sizeof (*c->result));
- notify = c->notify;
- c->notify = NULL;
- notify->cb (exec_ctx, notify->cb_arg, 1);
- }
+ if (tcp != NULL) {
+ gpr_mu_lock(&c->mu);
+ GPR_ASSERT(c->connecting_endpoint == NULL);
+ c->connecting_endpoint = tcp;
+ gpr_mu_unlock(&c->mu);
+ grpc_security_connector_do_handshake(exec_ctx, &c->security_connector->base,
+ tcp, on_secure_handshake_done, c);
+ } else {
+ memset(c->result, 0, sizeof(*c->result));
+ notify = c->notify;
+ c->notify = NULL;
+ notify->cb(exec_ctx, notify->cb_arg, 1);
+ }
}
-static void
-connector_shutdown (grpc_exec_ctx * exec_ctx, grpc_connector * con)
-{
- connector *c = (connector *) con;
+static void connector_shutdown(grpc_exec_ctx *exec_ctx, grpc_connector *con) {
+ connector *c = (connector *)con;
grpc_endpoint *ep;
- gpr_mu_lock (&c->mu);
+ gpr_mu_lock(&c->mu);
ep = c->connecting_endpoint;
c->connecting_endpoint = NULL;
- gpr_mu_unlock (&c->mu);
- if (ep)
- {
- grpc_endpoint_shutdown (exec_ctx, ep);
- }
+ gpr_mu_unlock(&c->mu);
+ if (ep) {
+ grpc_endpoint_shutdown(exec_ctx, ep);
+ }
}
-static void
-connector_connect (grpc_exec_ctx * exec_ctx, grpc_connector * con, const grpc_connect_in_args * args, grpc_connect_out_args * result, grpc_closure * notify)
-{
- connector *c = (connector *) con;
- GPR_ASSERT (c->notify == NULL);
- GPR_ASSERT (notify->cb);
+static void connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *con,
+ const grpc_connect_in_args *args,
+ grpc_connect_out_args *result,
+ grpc_closure *notify) {
+ connector *c = (connector *)con;
+ GPR_ASSERT(c->notify == NULL);
+ GPR_ASSERT(notify->cb);
c->notify = notify;
c->args = *args;
c->result = result;
- gpr_mu_lock (&c->mu);
- GPR_ASSERT (c->connecting_endpoint == NULL);
- gpr_mu_unlock (&c->mu);
- grpc_closure_init (&c->connected_closure, connected, c);
- grpc_tcp_client_connect (exec_ctx, &c->connected_closure, &c->newly_connecting_endpoint, args->interested_parties, args->addr, args->addr_len, args->deadline);
+ gpr_mu_lock(&c->mu);
+ GPR_ASSERT(c->connecting_endpoint == NULL);
+ gpr_mu_unlock(&c->mu);
+ grpc_closure_init(&c->connected_closure, connected, c);
+ grpc_tcp_client_connect(
+ exec_ctx, &c->connected_closure, &c->newly_connecting_endpoint,
+ args->interested_parties, args->addr, args->addr_len, args->deadline);
}
static const grpc_connector_vtable connector_vtable = {
- connector_ref, connector_unref, connector_shutdown, connector_connect
-};
+ connector_ref, connector_unref, connector_shutdown, connector_connect};
-typedef struct
-{
+typedef struct {
grpc_subchannel_factory base;
gpr_refcount refs;
grpc_mdctx *mdctx;
@@ -193,61 +179,59 @@ typedef struct
grpc_channel *master;
} subchannel_factory;
-static void
-subchannel_factory_ref (grpc_subchannel_factory * scf)
-{
- subchannel_factory *f = (subchannel_factory *) scf;
- gpr_ref (&f->refs);
+static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
+ subchannel_factory *f = (subchannel_factory *)scf;
+ gpr_ref(&f->refs);
}
-static void
-subchannel_factory_unref (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * scf)
-{
- subchannel_factory *f = (subchannel_factory *) scf;
- if (gpr_unref (&f->refs))
- {
- GRPC_SECURITY_CONNECTOR_UNREF (&f->security_connector->base, "subchannel_factory");
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, f->master, "subchannel_factory");
- grpc_channel_args_destroy (f->merge_args);
- grpc_mdctx_unref (f->mdctx);
- gpr_free (f);
- }
+static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
+ grpc_subchannel_factory *scf) {
+ subchannel_factory *f = (subchannel_factory *)scf;
+ if (gpr_unref(&f->refs)) {
+ GRPC_SECURITY_CONNECTOR_UNREF(&f->security_connector->base,
+ "subchannel_factory");
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, f->master, "subchannel_factory");
+ grpc_channel_args_destroy(f->merge_args);
+ grpc_mdctx_unref(f->mdctx);
+ gpr_free(f);
+ }
}
-static grpc_subchannel *
-subchannel_factory_create_subchannel (grpc_exec_ctx * exec_ctx, grpc_subchannel_factory * scf, grpc_subchannel_args * args)
-{
- subchannel_factory *f = (subchannel_factory *) scf;
- connector *c = gpr_malloc (sizeof (*c));
- grpc_channel_args *final_args = grpc_channel_args_merge (args->args, f->merge_args);
+static grpc_subchannel *subchannel_factory_create_subchannel(
+ grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *scf,
+ grpc_subchannel_args *args) {
+ subchannel_factory *f = (subchannel_factory *)scf;
+ connector *c = gpr_malloc(sizeof(*c));
+ grpc_channel_args *final_args =
+ grpc_channel_args_merge(args->args, f->merge_args);
grpc_subchannel *s;
- memset (c, 0, sizeof (*c));
+ memset(c, 0, sizeof(*c));
c->base.vtable = &connector_vtable;
c->security_connector = f->security_connector;
c->mdctx = f->mdctx;
- grpc_mdctx_ref (c->mdctx);
- gpr_ref_init (&c->refs, 1);
+ grpc_mdctx_ref(c->mdctx);
+ gpr_ref_init(&c->refs, 1);
args->args = final_args;
args->master = f->master;
args->mdctx = f->mdctx;
- s = grpc_subchannel_create (&c->base, args);
- grpc_connector_unref (exec_ctx, &c->base);
- grpc_channel_args_destroy (final_args);
+ s = grpc_subchannel_create(&c->base, args);
+ grpc_connector_unref(exec_ctx, &c->base);
+ grpc_channel_args_destroy(final_args);
return s;
}
static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
- subchannel_factory_ref, subchannel_factory_unref,
- subchannel_factory_create_subchannel
-};
+ subchannel_factory_ref, subchannel_factory_unref,
+ subchannel_factory_create_subchannel};
/* Create a secure client channel:
Asynchronously: - resolve target
- connect to it (trying alternatives as presented)
- perform handshakes */
-grpc_channel *
-grpc_secure_channel_create (grpc_credentials * creds, const char *target, const grpc_channel_args * args, void *reserved)
-{
+grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
+ const char *target,
+ const grpc_channel_args *args,
+ void *reserved) {
grpc_channel *channel;
grpc_arg connector_arg;
grpc_channel_args *args_copy;
@@ -261,59 +245,64 @@ grpc_secure_channel_create (grpc_credentials * creds, const char *target, const
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
size_t n = 0;
- GPR_ASSERT (reserved == NULL);
- if (grpc_find_security_connector_in_args (args) != NULL)
- {
- gpr_log (GPR_ERROR, "Cannot set security context in channel args.");
- return grpc_lame_client_channel_create (target, GRPC_STATUS_INVALID_ARGUMENT, "Security connector exists in channel args.");
- }
-
- if (grpc_credentials_create_security_connector (creds, target, args, NULL, &connector, &new_args_from_connector) != GRPC_SECURITY_OK)
- {
- return grpc_lame_client_channel_create (target, GRPC_STATUS_INVALID_ARGUMENT, "Failed to create security connector.");
- }
- mdctx = grpc_mdctx_create ();
-
- connector_arg = grpc_security_connector_to_arg (&connector->base);
- args_copy = grpc_channel_args_copy_and_add (new_args_from_connector != NULL ? new_args_from_connector : args, &connector_arg, 1);
- if (grpc_channel_args_is_census_enabled (args))
- {
- filters[n++] = &grpc_client_census_filter;
- }
+ GPR_ASSERT(reserved == NULL);
+ if (grpc_find_security_connector_in_args(args) != NULL) {
+ gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
+ return grpc_lame_client_channel_create(
+ target, GRPC_STATUS_INVALID_ARGUMENT,
+ "Security connector exists in channel args.");
+ }
+
+ if (grpc_credentials_create_security_connector(
+ creds, target, args, NULL, &connector, &new_args_from_connector) !=
+ GRPC_SECURITY_OK) {
+ return grpc_lame_client_channel_create(
+ target, GRPC_STATUS_INVALID_ARGUMENT,
+ "Failed to create security connector.");
+ }
+ mdctx = grpc_mdctx_create();
+
+ connector_arg = grpc_security_connector_to_arg(&connector->base);
+ args_copy = grpc_channel_args_copy_and_add(
+ new_args_from_connector != NULL ? new_args_from_connector : args,
+ &connector_arg, 1);
+ if (grpc_channel_args_is_census_enabled(args)) {
+ filters[n++] = &grpc_client_census_filter;
+ }
filters[n++] = &grpc_compress_filter;
filters[n++] = &grpc_client_channel_filter;
- GPR_ASSERT (n <= MAX_FILTERS);
+ GPR_ASSERT(n <= MAX_FILTERS);
- channel = grpc_channel_create_from_filters (&exec_ctx, target, filters, n, args_copy, mdctx, 1);
+ channel = grpc_channel_create_from_filters(&exec_ctx, target, filters, n,
+ args_copy, mdctx, 1);
- f = gpr_malloc (sizeof (*f));
+ f = gpr_malloc(sizeof(*f));
f->base.vtable = &subchannel_factory_vtable;
- gpr_ref_init (&f->refs, 1);
- grpc_mdctx_ref (mdctx);
+ gpr_ref_init(&f->refs, 1);
+ grpc_mdctx_ref(mdctx);
f->mdctx = mdctx;
- GRPC_SECURITY_CONNECTOR_REF (&connector->base, "subchannel_factory");
+ GRPC_SECURITY_CONNECTOR_REF(&connector->base, "subchannel_factory");
f->security_connector = connector;
- f->merge_args = grpc_channel_args_copy (args_copy);
+ f->merge_args = grpc_channel_args_copy(args_copy);
f->master = channel;
- GRPC_CHANNEL_INTERNAL_REF (channel, "subchannel_factory");
- resolver = grpc_resolver_create (target, &f->base);
- if (!resolver)
- {
- return NULL;
- }
-
- grpc_client_channel_set_resolver (&exec_ctx, grpc_channel_get_channel_stack (channel), resolver);
- GRPC_RESOLVER_UNREF (&exec_ctx, resolver, "create");
- grpc_subchannel_factory_unref (&exec_ctx, &f->base);
- GRPC_SECURITY_CONNECTOR_UNREF (&connector->base, "channel_create");
-
- grpc_channel_args_destroy (args_copy);
- if (new_args_from_connector != NULL)
- {
- grpc_channel_args_destroy (new_args_from_connector);
- }
-
- grpc_exec_ctx_finish (&exec_ctx);
+ GRPC_CHANNEL_INTERNAL_REF(channel, "subchannel_factory");
+ resolver = grpc_resolver_create(target, &f->base);
+ if (!resolver) {
+ return NULL;
+ }
+
+ grpc_client_channel_set_resolver(
+ &exec_ctx, grpc_channel_get_channel_stack(channel), resolver);
+ GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "create");
+ grpc_subchannel_factory_unref(&exec_ctx, &f->base);
+ GRPC_SECURITY_CONNECTOR_UNREF(&connector->base, "channel_create");
+
+ grpc_channel_args_destroy(args_copy);
+ if (new_args_from_connector != NULL) {
+ grpc_channel_args_destroy(new_args_from_connector);
+ }
+
+ grpc_exec_ctx_finish(&exec_ctx);
return channel;
}
diff --git a/src/core/surface/server.c b/src/core/surface/server.c
index c11ea26da7..a26b84c0d3 100644
--- a/src/core/surface/server.c
+++ b/src/core/surface/server.c
@@ -54,11 +54,12 @@
#include "src/core/surface/init.h"
#include "src/core/transport/metadata.h"
-typedef struct listener
-{
+typedef struct listener {
void *arg;
- void (*start) (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, grpc_pollset ** pollsets, size_t pollset_count);
- void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, grpc_closure * closure);
+ void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ grpc_pollset **pollsets, size_t pollset_count);
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ grpc_closure *closure);
struct listener *next;
grpc_closure destroy_done;
} listener;
@@ -67,17 +68,14 @@ typedef struct call_data call_data;
typedef struct channel_data channel_data;
typedef struct registered_method registered_method;
-typedef struct
-{
+typedef struct {
call_data *next;
call_data *prev;
} call_link;
-typedef enum
-{ BATCH_CALL, REGISTERED_CALL } requested_call_type;
+typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type;
-typedef struct requested_call
-{
+typedef struct requested_call {
requested_call_type type;
void *tag;
grpc_server *server;
@@ -85,15 +83,12 @@ typedef struct requested_call
grpc_completion_queue *cq_for_notification;
grpc_call **call;
grpc_cq_completion completion;
- union
- {
- struct
- {
+ union {
+ struct {
grpc_call_details *details;
grpc_metadata_array *initial_metadata;
} batch;
- struct
- {
+ struct {
registered_method *registered_method;
gpr_timespec *deadline;
grpc_metadata_array *initial_metadata;
@@ -102,15 +97,13 @@ typedef struct requested_call
} data;
} requested_call;
-typedef struct channel_registered_method
-{
+typedef struct channel_registered_method {
registered_method *server_registered_method;
grpc_mdstr *method;
grpc_mdstr *host;
} channel_registered_method;
-struct channel_data
-{
+struct channel_data {
grpc_server *server;
grpc_connectivity_state connectivity_state;
grpc_channel *channel;
@@ -126,15 +119,13 @@ struct channel_data
grpc_closure channel_connectivity_changed;
};
-typedef struct shutdown_tag
-{
+typedef struct shutdown_tag {
void *tag;
grpc_completion_queue *cq;
grpc_cq_completion completion;
} shutdown_tag;
-typedef enum
-{
+typedef enum {
/* waiting for metadata */
NOT_STARTED,
/* inital metadata read, not flow controlled in yet */
@@ -147,8 +138,7 @@ typedef enum
typedef struct request_matcher request_matcher;
-struct call_data
-{
+struct call_data {
grpc_call *call;
/** protects state */
@@ -173,29 +163,25 @@ struct call_data
call_data *pending_next;
};
-struct request_matcher
-{
+struct request_matcher {
call_data *pending_head;
call_data *pending_tail;
gpr_stack_lockfree *requests;
};
-struct registered_method
-{
+struct registered_method {
char *method;
char *host;
request_matcher request_matcher;
registered_method *next;
};
-typedef struct
-{
+typedef struct {
grpc_channel **channels;
size_t num_channels;
} channel_broadcaster;
-struct grpc_server
-{
+struct grpc_server {
size_t channel_filter_count;
const grpc_channel_filter **channel_filters;
grpc_channel_args *channel_args;
@@ -211,8 +197,8 @@ struct grpc_server
If they are ever required to be nested, you must lock mu_global
before mu_call. This is currently used in shutdown processing
(grpc_server_shutdown_and_notify and maybe_finish_shutdown) */
- gpr_mu mu_global; /* mutex for server and channel state */
- gpr_mu mu_call; /* mutex for call-specific state */
+ gpr_mu mu_global; /* mutex for server and channel state */
+ gpr_mu mu_call; /* mutex for call-specific state */
registered_method *registered_methods;
request_matcher unregistered_request_matcher;
@@ -240,267 +226,230 @@ struct grpc_server
#define SERVER_FROM_CALL_ELEM(elem) \
(((channel_data *)(elem)->channel_data)->server)
-static void begin_call (grpc_exec_ctx * exec_ctx, grpc_server * server, call_data * calld, requested_call * rc);
-static void fail_call (grpc_exec_ctx * exec_ctx, grpc_server * server, requested_call * rc);
+static void begin_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
+ call_data *calld, requested_call *rc);
+static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
+ requested_call *rc);
/* Before calling maybe_finish_shutdown, we must hold mu_global and not
hold mu_call */
-static void maybe_finish_shutdown (grpc_exec_ctx * exec_ctx, grpc_server * server);
+static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
/*
* channel broadcaster
*/
/* assumes server locked */
-static void
-channel_broadcaster_init (grpc_server * s, channel_broadcaster * cb)
-{
+static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
channel_data *c;
size_t count = 0;
- for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next)
- {
- count++;
- }
+ for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
+ count++;
+ }
cb->num_channels = count;
- cb->channels = gpr_malloc (sizeof (*cb->channels) * cb->num_channels);
+ cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
count = 0;
- for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next)
- {
- cb->channels[count++] = c->channel;
- GRPC_CHANNEL_INTERNAL_REF (c->channel, "broadcast");
- }
+ for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
+ cb->channels[count++] = c->channel;
+ GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
+ }
}
-struct shutdown_cleanup_args
-{
+struct shutdown_cleanup_args {
grpc_closure closure;
gpr_slice slice;
};
-static void
-shutdown_cleanup (grpc_exec_ctx * exec_ctx, void *arg, int iomgr_status_ignored)
-{
+static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
+ int iomgr_status_ignored) {
struct shutdown_cleanup_args *a = arg;
- gpr_slice_unref (a->slice);
- gpr_free (a);
+ gpr_slice_unref(a->slice);
+ gpr_free(a);
}
-static void
-send_shutdown (grpc_exec_ctx * exec_ctx, grpc_channel * channel, int send_goaway, int send_disconnect)
-{
+static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
+ int send_goaway, int send_disconnect) {
grpc_transport_op op;
struct shutdown_cleanup_args *sc;
grpc_channel_element *elem;
- memset (&op, 0, sizeof (op));
+ memset(&op, 0, sizeof(op));
op.send_goaway = send_goaway;
- sc = gpr_malloc (sizeof (*sc));
- sc->slice = gpr_slice_from_copied_string ("Server shutdown");
+ sc = gpr_malloc(sizeof(*sc));
+ sc->slice = gpr_slice_from_copied_string("Server shutdown");
op.goaway_message = &sc->slice;
op.goaway_status = GRPC_STATUS_OK;
op.disconnect = send_disconnect;
- grpc_closure_init (&sc->closure, shutdown_cleanup, sc);
+ grpc_closure_init(&sc->closure, shutdown_cleanup, sc);
op.on_consumed = &sc->closure;
- elem = grpc_channel_stack_element (grpc_channel_get_channel_stack (channel), 0);
- elem->filter->start_transport_op (exec_ctx, elem, &op);
+ elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
+ elem->filter->start_transport_op(exec_ctx, elem, &op);
}
-static void
-channel_broadcaster_shutdown (grpc_exec_ctx * exec_ctx, channel_broadcaster * cb, int send_goaway, int force_disconnect)
-{
+static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
+ channel_broadcaster *cb,
+ int send_goaway,
+ int force_disconnect) {
size_t i;
- for (i = 0; i < cb->num_channels; i++)
- {
- send_shutdown (exec_ctx, cb->channels[i], send_goaway, force_disconnect);
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, cb->channels[i], "broadcast");
- }
- gpr_free (cb->channels);
+ for (i = 0; i < cb->num_channels; i++) {
+ send_shutdown(exec_ctx, cb->channels[i], send_goaway, force_disconnect);
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
+ }
+ gpr_free(cb->channels);
}
/*
* request_matcher
*/
-static void
-request_matcher_init (request_matcher * request_matcher, size_t entries)
-{
- memset (request_matcher, 0, sizeof (*request_matcher));
- request_matcher->requests = gpr_stack_lockfree_create (entries);
+static void request_matcher_init(request_matcher *request_matcher,
+ size_t entries) {
+ memset(request_matcher, 0, sizeof(*request_matcher));
+ request_matcher->requests = gpr_stack_lockfree_create(entries);
}
-static void
-request_matcher_destroy (request_matcher * request_matcher)
-{
- GPR_ASSERT (gpr_stack_lockfree_pop (request_matcher->requests) == -1);
- gpr_stack_lockfree_destroy (request_matcher->requests);
+static void request_matcher_destroy(request_matcher *request_matcher) {
+ GPR_ASSERT(gpr_stack_lockfree_pop(request_matcher->requests) == -1);
+ gpr_stack_lockfree_destroy(request_matcher->requests);
}
-static void
-kill_zombie (grpc_exec_ctx * exec_ctx, void *elem, int success)
-{
- grpc_call_destroy (grpc_call_from_top_element (elem));
+static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem, int success) {
+ grpc_call_destroy(grpc_call_from_top_element(elem));
}
-static void
-request_matcher_zombify_all_pending_calls (grpc_exec_ctx * exec_ctx, request_matcher * request_matcher)
-{
- while (request_matcher->pending_head)
- {
- call_data *calld = request_matcher->pending_head;
- request_matcher->pending_head = calld->pending_next;
- gpr_mu_lock (&calld->mu_state);
- calld->state = ZOMBIED;
- gpr_mu_unlock (&calld->mu_state);
- grpc_closure_init (&calld->kill_zombie_closure, kill_zombie, grpc_call_stack_element (grpc_call_get_call_stack (calld->call), 0));
- grpc_exec_ctx_enqueue (exec_ctx, &calld->kill_zombie_closure, 1);
- }
+static void request_matcher_zombify_all_pending_calls(
+ grpc_exec_ctx *exec_ctx, request_matcher *request_matcher) {
+ while (request_matcher->pending_head) {
+ call_data *calld = request_matcher->pending_head;
+ request_matcher->pending_head = calld->pending_next;
+ gpr_mu_lock(&calld->mu_state);
+ calld->state = ZOMBIED;
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_closure_init(
+ &calld->kill_zombie_closure, kill_zombie,
+ grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
+ grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1);
+ }
}
-static void
-request_matcher_kill_requests (grpc_exec_ctx * exec_ctx, grpc_server * server, request_matcher * rm)
-{
+static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
+ grpc_server *server,
+ request_matcher *rm) {
int request_id;
- while ((request_id = gpr_stack_lockfree_pop (rm->requests)) != -1)
- {
- fail_call (exec_ctx, server, &server->requested_calls[request_id]);
- }
+ while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) {
+ fail_call(exec_ctx, server, &server->requested_calls[request_id]);
+ }
}
/*
* server proper
*/
-static void
-server_ref (grpc_server * server)
-{
- gpr_ref (&server->internal_refcount);
+static void server_ref(grpc_server *server) {
+ gpr_ref(&server->internal_refcount);
}
-static void
-server_delete (grpc_exec_ctx * exec_ctx, grpc_server * server)
-{
+static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
registered_method *rm;
size_t i;
- grpc_channel_args_destroy (server->channel_args);
- gpr_mu_destroy (&server->mu_global);
- gpr_mu_destroy (&server->mu_call);
- gpr_free (server->channel_filters);
- while ((rm = server->registered_methods) != NULL)
- {
- server->registered_methods = rm->next;
- request_matcher_destroy (&rm->request_matcher);
- gpr_free (rm->method);
- gpr_free (rm->host);
- gpr_free (rm);
- }
- for (i = 0; i < server->cq_count; i++)
- {
- GRPC_CQ_INTERNAL_UNREF (server->cqs[i], "server");
- }
- request_matcher_destroy (&server->unregistered_request_matcher);
- gpr_stack_lockfree_destroy (server->request_freelist);
- gpr_free (server->cqs);
- gpr_free (server->pollsets);
- gpr_free (server->shutdown_tags);
- gpr_free (server->requested_calls);
- gpr_free (server);
+ grpc_channel_args_destroy(server->channel_args);
+ gpr_mu_destroy(&server->mu_global);
+ gpr_mu_destroy(&server->mu_call);
+ gpr_free(server->channel_filters);
+ while ((rm = server->registered_methods) != NULL) {
+ server->registered_methods = rm->next;
+ request_matcher_destroy(&rm->request_matcher);
+ gpr_free(rm->method);
+ gpr_free(rm->host);
+ gpr_free(rm);
+ }
+ for (i = 0; i < server->cq_count; i++) {
+ GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
+ }
+ request_matcher_destroy(&server->unregistered_request_matcher);
+ gpr_stack_lockfree_destroy(server->request_freelist);
+ gpr_free(server->cqs);
+ gpr_free(server->pollsets);
+ gpr_free(server->shutdown_tags);
+ gpr_free(server->requested_calls);
+ gpr_free(server);
}
-static void
-server_unref (grpc_exec_ctx * exec_ctx, grpc_server * server)
-{
- if (gpr_unref (&server->internal_refcount))
- {
- server_delete (exec_ctx, server);
- }
+static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
+ if (gpr_unref(&server->internal_refcount)) {
+ server_delete(exec_ctx, server);
+ }
}
-static int
-is_channel_orphaned (channel_data * chand)
-{
+static int is_channel_orphaned(channel_data *chand) {
return chand->next == chand;
}
-static void
-orphan_channel (channel_data * chand)
-{
+static void orphan_channel(channel_data *chand) {
chand->next->prev = chand->prev;
chand->prev->next = chand->next;
chand->next = chand->prev = chand;
}
-static void
-finish_destroy_channel (grpc_exec_ctx * exec_ctx, void *cd, int success)
-{
+static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
+ int success) {
channel_data *chand = cd;
grpc_server *server = chand->server;
- gpr_log (GPR_DEBUG, "finish_destroy_channel: %p", chand->channel);
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, chand->channel, "server");
- server_unref (exec_ctx, server);
+ gpr_log(GPR_DEBUG, "finish_destroy_channel: %p", chand->channel);
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
+ server_unref(exec_ctx, server);
}
-static void
-destroy_channel (grpc_exec_ctx * exec_ctx, channel_data * chand)
-{
- if (is_channel_orphaned (chand))
- return;
- GPR_ASSERT (chand->server != NULL);
- orphan_channel (chand);
- server_ref (chand->server);
- maybe_finish_shutdown (exec_ctx, chand->server);
+static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
+ if (is_channel_orphaned(chand)) return;
+ GPR_ASSERT(chand->server != NULL);
+ orphan_channel(chand);
+ server_ref(chand->server);
+ maybe_finish_shutdown(exec_ctx, chand->server);
chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
chand->finish_destroy_channel_closure.cb_arg = chand;
- grpc_exec_ctx_enqueue (exec_ctx, &chand->finish_destroy_channel_closure, 1);
+ grpc_exec_ctx_enqueue(exec_ctx, &chand->finish_destroy_channel_closure, 1);
}
-static void
-finish_start_new_rpc (grpc_exec_ctx * exec_ctx, grpc_server * server, grpc_call_element * elem, request_matcher * request_matcher)
-{
+static void finish_start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_server *server,
+ grpc_call_element *elem,
+ request_matcher *request_matcher) {
call_data *calld = elem->call_data;
int request_id;
- if (gpr_atm_acq_load (&server->shutdown_flag))
- {
- gpr_mu_lock (&calld->mu_state);
- calld->state = ZOMBIED;
- gpr_mu_unlock (&calld->mu_state);
- grpc_closure_init (&calld->kill_zombie_closure, kill_zombie, elem);
- grpc_exec_ctx_enqueue (exec_ctx, &calld->kill_zombie_closure, 1);
- return;
- }
-
- request_id = gpr_stack_lockfree_pop (request_matcher->requests);
- if (request_id == -1)
- {
- gpr_mu_lock (&server->mu_call);
- gpr_mu_lock (&calld->mu_state);
- calld->state = PENDING;
- gpr_mu_unlock (&calld->mu_state);
- if (request_matcher->pending_head == NULL)
- {
- request_matcher->pending_tail = request_matcher->pending_head = calld;
- }
- else
- {
- request_matcher->pending_tail->pending_next = calld;
- request_matcher->pending_tail = calld;
- }
- calld->pending_next = NULL;
- gpr_mu_unlock (&server->mu_call);
- }
- else
- {
- gpr_mu_lock (&calld->mu_state);
- calld->state = ACTIVATED;
- gpr_mu_unlock (&calld->mu_state);
- begin_call (exec_ctx, server, calld, &server->requested_calls[request_id]);
+ if (gpr_atm_acq_load(&server->shutdown_flag)) {
+ gpr_mu_lock(&calld->mu_state);
+ calld->state = ZOMBIED;
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
+ grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1);
+ return;
+ }
+
+ request_id = gpr_stack_lockfree_pop(request_matcher->requests);
+ if (request_id == -1) {
+ gpr_mu_lock(&server->mu_call);
+ gpr_mu_lock(&calld->mu_state);
+ calld->state = PENDING;
+ gpr_mu_unlock(&calld->mu_state);
+ if (request_matcher->pending_head == NULL) {
+ request_matcher->pending_tail = request_matcher->pending_head = calld;
+ } else {
+ request_matcher->pending_tail->pending_next = calld;
+ request_matcher->pending_tail = calld;
}
+ calld->pending_next = NULL;
+ gpr_mu_unlock(&server->mu_call);
+ } else {
+ gpr_mu_lock(&calld->mu_state);
+ calld->state = ACTIVATED;
+ gpr_mu_unlock(&calld->mu_state);
+ begin_call(exec_ctx, server, calld, &server->requested_calls[request_id]);
+ }
}
-static void
-start_new_rpc (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
channel_data *chand = elem->channel_data;
call_data *calld = elem->call_data;
grpc_server *server = chand->server;
@@ -508,411 +457,380 @@ start_new_rpc (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
gpr_uint32 hash;
channel_registered_method *rm;
- if (chand->registered_methods && calld->path && calld->host)
- {
- /* TODO(ctiller): unify these two searches */
- /* check for an exact match with host */
- hash = GRPC_MDSTR_KV_HASH (calld->host->hash, calld->path->hash);
- for (i = 0; i <= chand->registered_method_max_probes; i++)
- {
- rm = &chand->registered_methods[(hash + i) % chand->registered_method_slots];
- if (!rm)
- break;
- if (rm->host != calld->host)
- continue;
- if (rm->method != calld->path)
- continue;
- finish_start_new_rpc (exec_ctx, server, elem, &rm->server_registered_method->request_matcher);
- return;
- }
- /* check for a wildcard method definition (no host set) */
- hash = GRPC_MDSTR_KV_HASH (0, calld->path->hash);
- for (i = 0; i <= chand->registered_method_max_probes; i++)
- {
- rm = &chand->registered_methods[(hash + i) % chand->registered_method_slots];
- if (!rm)
- break;
- if (rm->host != NULL)
- continue;
- if (rm->method != calld->path)
- continue;
- finish_start_new_rpc (exec_ctx, server, elem, &rm->server_registered_method->request_matcher);
- return;
- }
+ if (chand->registered_methods && calld->path && calld->host) {
+ /* TODO(ctiller): unify these two searches */
+ /* check for an exact match with host */
+ hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
+ for (i = 0; i <= chand->registered_method_max_probes; i++) {
+ rm = &chand->registered_methods[(hash + i) %
+ chand->registered_method_slots];
+ if (!rm) break;
+ if (rm->host != calld->host) continue;
+ if (rm->method != calld->path) continue;
+ finish_start_new_rpc(exec_ctx, server, elem,
+ &rm->server_registered_method->request_matcher);
+ return;
+ }
+ /* check for a wildcard method definition (no host set) */
+ hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
+ for (i = 0; i <= chand->registered_method_max_probes; i++) {
+ rm = &chand->registered_methods[(hash + i) %
+ chand->registered_method_slots];
+ if (!rm) break;
+ if (rm->host != NULL) continue;
+ if (rm->method != calld->path) continue;
+ finish_start_new_rpc(exec_ctx, server, elem,
+ &rm->server_registered_method->request_matcher);
+ return;
}
- finish_start_new_rpc (exec_ctx, server, elem, &server->unregistered_request_matcher);
+ }
+ finish_start_new_rpc(exec_ctx, server, elem,
+ &server->unregistered_request_matcher);
}
-static int
-num_listeners (grpc_server * server)
-{
+static int num_listeners(grpc_server *server) {
listener *l;
int n = 0;
- for (l = server->listeners; l; l = l->next)
- {
- n++;
- }
+ for (l = server->listeners; l; l = l->next) {
+ n++;
+ }
return n;
}
-static void
-done_shutdown_event (grpc_exec_ctx * exec_ctx, void *server, grpc_cq_completion * completion)
-{
- server_unref (exec_ctx, server);
+static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
+ grpc_cq_completion *completion) {
+ server_unref(exec_ctx, server);
}
-static int
-num_channels (grpc_server * server)
-{
+static int num_channels(grpc_server *server) {
channel_data *chand;
int n = 0;
- for (chand = server->root_channel_data.next; chand != &server->root_channel_data; chand = chand->next)
- {
- n++;
- }
+ for (chand = server->root_channel_data.next;
+ chand != &server->root_channel_data; chand = chand->next) {
+ n++;
+ }
return n;
}
-static void
-kill_pending_work_locked (grpc_exec_ctx * exec_ctx, grpc_server * server)
-{
+static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
+ grpc_server *server) {
registered_method *rm;
- request_matcher_kill_requests (exec_ctx, server, &server->unregistered_request_matcher);
- request_matcher_zombify_all_pending_calls (exec_ctx, &server->unregistered_request_matcher);
- for (rm = server->registered_methods; rm; rm = rm->next)
- {
- request_matcher_kill_requests (exec_ctx, server, &rm->request_matcher);
- request_matcher_zombify_all_pending_calls (exec_ctx, &rm->request_matcher);
- }
+ request_matcher_kill_requests(exec_ctx, server,
+ &server->unregistered_request_matcher);
+ request_matcher_zombify_all_pending_calls(
+ exec_ctx, &server->unregistered_request_matcher);
+ for (rm = server->registered_methods; rm; rm = rm->next) {
+ request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher);
+ request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
+ }
}
-static void
-maybe_finish_shutdown (grpc_exec_ctx * exec_ctx, grpc_server * server)
-{
+static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
+ grpc_server *server) {
size_t i;
- if (!gpr_atm_acq_load (&server->shutdown_flag) || server->shutdown_published)
- {
- return;
- }
-
- kill_pending_work_locked (exec_ctx, server);
-
- if (server->root_channel_data.next != &server->root_channel_data || server->listeners_destroyed < num_listeners (server))
- {
- if (gpr_time_cmp (gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), server->last_shutdown_message_time), gpr_time_from_seconds (1, GPR_TIMESPAN)) >= 0)
- {
- server->last_shutdown_message_time = gpr_now (GPR_CLOCK_REALTIME);
- gpr_log (GPR_DEBUG, "Waiting for %d channels and %d/%d listeners to be destroyed" " before shutting down server", num_channels (server), num_listeners (server) - server->listeners_destroyed, num_listeners (server));
- }
- return;
+ if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
+ return;
+ }
+
+ kill_pending_work_locked(exec_ctx, server);
+
+ if (server->root_channel_data.next != &server->root_channel_data ||
+ server->listeners_destroyed < num_listeners(server)) {
+ if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
+ server->last_shutdown_message_time),
+ gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
+ server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
+ gpr_log(GPR_DEBUG,
+ "Waiting for %d channels and %d/%d listeners to be destroyed"
+ " before shutting down server",
+ num_channels(server),
+ num_listeners(server) - server->listeners_destroyed,
+ num_listeners(server));
}
+ return;
+ }
server->shutdown_published = 1;
- for (i = 0; i < server->num_shutdown_tags; i++)
- {
- server_ref (server);
- grpc_cq_end_op (exec_ctx, server->shutdown_tags[i].cq, server->shutdown_tags[i].tag, 1, done_shutdown_event, server, &server->shutdown_tags[i].completion);
- }
+ for (i = 0; i < server->num_shutdown_tags; i++) {
+ server_ref(server);
+ grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
+ server->shutdown_tags[i].tag, 1, done_shutdown_event, server,
+ &server->shutdown_tags[i].completion);
+ }
}
-static grpc_mdelem *
-server_filter (void *user_data, grpc_mdelem * md)
-{
+static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
grpc_call_element *elem = user_data;
channel_data *chand = elem->channel_data;
call_data *calld = elem->call_data;
- if (md->key == chand->path_key)
- {
- calld->path = GRPC_MDSTR_REF (md->value);
- return NULL;
- }
- else if (md->key == chand->authority_key)
- {
- calld->host = GRPC_MDSTR_REF (md->value);
- return NULL;
- }
+ if (md->key == chand->path_key) {
+ calld->path = GRPC_MDSTR_REF(md->value);
+ return NULL;
+ } else if (md->key == chand->authority_key) {
+ calld->host = GRPC_MDSTR_REF(md->value);
+ return NULL;
+ }
return md;
}
-static void
-server_on_recv (grpc_exec_ctx * exec_ctx, void *ptr, int success)
-{
+static void server_on_recv(grpc_exec_ctx *exec_ctx, void *ptr, int success) {
grpc_call_element *elem = ptr;
call_data *calld = elem->call_data;
gpr_timespec op_deadline;
- if (success && !calld->got_initial_metadata)
- {
- size_t i;
- size_t nops = calld->recv_ops->nops;
- grpc_stream_op *ops = calld->recv_ops->ops;
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- if (op->type != GRPC_OP_METADATA)
- continue;
- grpc_metadata_batch_filter (&op->data.metadata, server_filter, elem);
- op_deadline = op->data.metadata.deadline;
- if (0 != gpr_time_cmp (op_deadline, gpr_inf_future (op_deadline.clock_type)))
- {
- calld->deadline = op->data.metadata.deadline;
- }
- if (calld->host && calld->path)
- {
- calld->got_initial_metadata = 1;
- start_new_rpc (exec_ctx, elem);
- }
- break;
- }
+ if (success && !calld->got_initial_metadata) {
+ size_t i;
+ size_t nops = calld->recv_ops->nops;
+ grpc_stream_op *ops = calld->recv_ops->ops;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ if (op->type != GRPC_OP_METADATA) continue;
+ grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem);
+ op_deadline = op->data.metadata.deadline;
+ if (0 !=
+ gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
+ calld->deadline = op->data.metadata.deadline;
+ }
+ if (calld->host && calld->path) {
+ calld->got_initial_metadata = 1;
+ start_new_rpc(exec_ctx, elem);
+ }
+ break;
}
+ }
- switch (*calld->recv_state)
- {
+ switch (*calld->recv_state) {
case GRPC_STREAM_OPEN:
break;
case GRPC_STREAM_SEND_CLOSED:
break;
case GRPC_STREAM_RECV_CLOSED:
- gpr_mu_lock (&calld->mu_state);
- if (calld->state == NOT_STARTED)
- {
- calld->state = ZOMBIED;
- gpr_mu_unlock (&calld->mu_state);
- grpc_closure_init (&calld->kill_zombie_closure, kill_zombie, elem);
- grpc_exec_ctx_enqueue (exec_ctx, &calld->kill_zombie_closure, 1);
- }
- else
- {
- gpr_mu_unlock (&calld->mu_state);
- }
+ gpr_mu_lock(&calld->mu_state);
+ if (calld->state == NOT_STARTED) {
+ calld->state = ZOMBIED;
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
+ grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1);
+ } else {
+ gpr_mu_unlock(&calld->mu_state);
+ }
break;
case GRPC_STREAM_CLOSED:
- gpr_mu_lock (&calld->mu_state);
- if (calld->state == NOT_STARTED)
- {
- calld->state = ZOMBIED;
- gpr_mu_unlock (&calld->mu_state);
- grpc_closure_init (&calld->kill_zombie_closure, kill_zombie, elem);
- grpc_exec_ctx_enqueue (exec_ctx, &calld->kill_zombie_closure, 1);
- }
- else if (calld->state == PENDING)
- {
- calld->state = ZOMBIED;
- gpr_mu_unlock (&calld->mu_state);
- /* zombied call will be destroyed when it's removed from the pending
- queue... later */
- }
- else
- {
- gpr_mu_unlock (&calld->mu_state);
- }
+ gpr_mu_lock(&calld->mu_state);
+ if (calld->state == NOT_STARTED) {
+ calld->state = ZOMBIED;
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
+ grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1);
+ } else if (calld->state == PENDING) {
+ calld->state = ZOMBIED;
+ gpr_mu_unlock(&calld->mu_state);
+ /* zombied call will be destroyed when it's removed from the pending
+ queue... later */
+ } else {
+ gpr_mu_unlock(&calld->mu_state);
+ }
break;
- }
+ }
- calld->on_done_recv->cb (exec_ctx, calld->on_done_recv->cb_arg, success);
+ calld->on_done_recv->cb(exec_ctx, calld->on_done_recv->cb_arg, success);
}
-static void
-server_mutate_op (grpc_call_element * elem, grpc_transport_stream_op * op)
-{
+static void server_mutate_op(grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
call_data *calld = elem->call_data;
- if (op->recv_ops)
- {
- /* substitute our callback for the higher callback */
- calld->recv_ops = op->recv_ops;
- calld->recv_state = op->recv_state;
- calld->on_done_recv = op->on_done_recv;
- op->on_done_recv = &calld->server_on_recv;
- }
+ if (op->recv_ops) {
+ /* substitute our callback for the higher callback */
+ calld->recv_ops = op->recv_ops;
+ calld->recv_state = op->recv_state;
+ calld->on_done_recv = op->on_done_recv;
+ op->on_done_recv = &calld->server_on_recv;
+ }
}
-static void
-server_start_transport_stream_op (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- GRPC_CALL_LOG_OP (GPR_INFO, elem, op);
- server_mutate_op (elem, op);
- grpc_call_next_op (exec_ctx, elem, op);
+static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem,
+ grpc_transport_stream_op *op) {
+ GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
+ server_mutate_op(elem, op);
+ grpc_call_next_op(exec_ctx, elem, op);
}
-static void
-accept_stream (void *cd, grpc_transport * transport, const void *transport_server_data)
-{
+static void accept_stream(void *cd, grpc_transport *transport,
+ const void *transport_server_data) {
channel_data *chand = cd;
/* create a call */
- grpc_call_create (chand->channel, NULL, 0, NULL, transport_server_data, NULL, 0, gpr_inf_future (GPR_CLOCK_MONOTONIC));
+ grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data, NULL,
+ 0, gpr_inf_future(GPR_CLOCK_MONOTONIC));
}
-static void
-channel_connectivity_changed (grpc_exec_ctx * exec_ctx, void *cd, int iomgr_status_ignored)
-{
+static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
+ int iomgr_status_ignored) {
channel_data *chand = cd;
grpc_server *server = chand->server;
- if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE)
- {
- grpc_transport_op op;
- memset (&op, 0, sizeof (op));
- op.on_connectivity_state_change = &chand->channel_connectivity_changed, op.connectivity_state = &chand->connectivity_state;
- grpc_channel_next_op (exec_ctx, grpc_channel_stack_element (grpc_channel_get_channel_stack (chand->channel), 0), &op);
- }
- else
- {
- gpr_mu_lock (&server->mu_global);
- destroy_channel (exec_ctx, chand);
- gpr_mu_unlock (&server->mu_global);
- GRPC_CHANNEL_INTERNAL_UNREF (exec_ctx, chand->channel, "connectivity");
- }
+ if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) {
+ grpc_transport_op op;
+ memset(&op, 0, sizeof(op));
+ op.on_connectivity_state_change = &chand->channel_connectivity_changed,
+ op.connectivity_state = &chand->connectivity_state;
+ grpc_channel_next_op(exec_ctx,
+ grpc_channel_stack_element(
+ grpc_channel_get_channel_stack(chand->channel), 0),
+ &op);
+ } else {
+ gpr_mu_lock(&server->mu_global);
+ destroy_channel(exec_ctx, chand);
+ gpr_mu_unlock(&server->mu_global);
+ GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
+ }
}
-static void
-init_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem, const void *server_transport_data, grpc_transport_stream_op * initial_op)
-{
+static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
+ const void *server_transport_data,
+ grpc_transport_stream_op *initial_op) {
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- memset (calld, 0, sizeof (call_data));
- calld->deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
- calld->call = grpc_call_from_top_element (elem);
- gpr_mu_init (&calld->mu_state);
+ memset(calld, 0, sizeof(call_data));
+ calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
+ calld->call = grpc_call_from_top_element(elem);
+ gpr_mu_init(&calld->mu_state);
- grpc_closure_init (&calld->server_on_recv, server_on_recv, elem);
+ grpc_closure_init(&calld->server_on_recv, server_on_recv, elem);
- server_ref (chand->server);
+ server_ref(chand->server);
- if (initial_op)
- server_mutate_op (elem, initial_op);
+ if (initial_op) server_mutate_op(elem, initial_op);
}
-static void
-destroy_call_elem (grpc_exec_ctx * exec_ctx, grpc_call_element * elem)
-{
+static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
+ grpc_call_element *elem) {
channel_data *chand = elem->channel_data;
call_data *calld = elem->call_data;
- GPR_ASSERT (calld->state != PENDING);
+ GPR_ASSERT(calld->state != PENDING);
- if (calld->host)
- {
- GRPC_MDSTR_UNREF (calld->host);
- }
- if (calld->path)
- {
- GRPC_MDSTR_UNREF (calld->path);
- }
+ if (calld->host) {
+ GRPC_MDSTR_UNREF(calld->host);
+ }
+ if (calld->path) {
+ GRPC_MDSTR_UNREF(calld->path);
+ }
- gpr_mu_destroy (&calld->mu_state);
+ gpr_mu_destroy(&calld->mu_state);
- server_unref (exec_ctx, chand->server);
+ server_unref(exec_ctx, chand->server);
}
-static void
-init_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem, grpc_channel * master, const grpc_channel_args * args, grpc_mdctx * metadata_context, int is_first, int is_last)
-{
+static void init_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem, grpc_channel *master,
+ const grpc_channel_args *args,
+ grpc_mdctx *metadata_context, int is_first,
+ int is_last) {
channel_data *chand = elem->channel_data;
- GPR_ASSERT (is_first);
- GPR_ASSERT (!is_last);
+ GPR_ASSERT(is_first);
+ GPR_ASSERT(!is_last);
chand->server = NULL;
chand->channel = NULL;
- chand->path_key = grpc_mdstr_from_string (metadata_context, ":path", 0);
- chand->authority_key = grpc_mdstr_from_string (metadata_context, ":authority", 0);
+ chand->path_key = grpc_mdstr_from_string(metadata_context, ":path", 0);
+ chand->authority_key =
+ grpc_mdstr_from_string(metadata_context, ":authority", 0);
chand->next = chand->prev = chand;
chand->registered_methods = NULL;
chand->connectivity_state = GRPC_CHANNEL_IDLE;
- grpc_closure_init (&chand->channel_connectivity_changed, channel_connectivity_changed, chand);
+ grpc_closure_init(&chand->channel_connectivity_changed,
+ channel_connectivity_changed, chand);
}
-static void
-destroy_channel_elem (grpc_exec_ctx * exec_ctx, grpc_channel_element * elem)
-{
+static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
+ grpc_channel_element *elem) {
size_t i;
channel_data *chand = elem->channel_data;
- if (chand->registered_methods)
- {
- for (i = 0; i < chand->registered_method_slots; i++)
- {
- if (chand->registered_methods[i].method)
- {
- GRPC_MDSTR_UNREF (chand->registered_methods[i].method);
- }
- if (chand->registered_methods[i].host)
- {
- GRPC_MDSTR_UNREF (chand->registered_methods[i].host);
- }
- }
- gpr_free (chand->registered_methods);
- }
- if (chand->server)
- {
- gpr_mu_lock (&chand->server->mu_global);
- chand->next->prev = chand->prev;
- chand->prev->next = chand->next;
- chand->next = chand->prev = chand;
- maybe_finish_shutdown (exec_ctx, chand->server);
- gpr_mu_unlock (&chand->server->mu_global);
- GRPC_MDSTR_UNREF (chand->path_key);
- GRPC_MDSTR_UNREF (chand->authority_key);
- server_unref (exec_ctx, chand->server);
+ if (chand->registered_methods) {
+ for (i = 0; i < chand->registered_method_slots; i++) {
+ if (chand->registered_methods[i].method) {
+ GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
+ }
+ if (chand->registered_methods[i].host) {
+ GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
+ }
}
+ gpr_free(chand->registered_methods);
+ }
+ if (chand->server) {
+ gpr_mu_lock(&chand->server->mu_global);
+ chand->next->prev = chand->prev;
+ chand->prev->next = chand->next;
+ chand->next = chand->prev = chand;
+ maybe_finish_shutdown(exec_ctx, chand->server);
+ gpr_mu_unlock(&chand->server->mu_global);
+ GRPC_MDSTR_UNREF(chand->path_key);
+ GRPC_MDSTR_UNREF(chand->authority_key);
+ server_unref(exec_ctx, chand->server);
+ }
}
static const grpc_channel_filter server_surface_filter = {
- server_start_transport_stream_op,
- grpc_channel_next_op,
- sizeof (call_data),
- init_call_elem,
- destroy_call_elem,
- sizeof (channel_data),
- init_channel_elem,
- destroy_channel_elem,
- grpc_call_next_get_peer,
- "server",
+ server_start_transport_stream_op,
+ grpc_channel_next_op,
+ sizeof(call_data),
+ init_call_elem,
+ destroy_call_elem,
+ sizeof(channel_data),
+ init_channel_elem,
+ destroy_channel_elem,
+ grpc_call_next_get_peer,
+ "server",
};
-void
-grpc_server_register_completion_queue (grpc_server * server, grpc_completion_queue * cq, void *reserved)
-{
+void grpc_server_register_completion_queue(grpc_server *server,
+ grpc_completion_queue *cq,
+ void *reserved) {
size_t i, n;
- GPR_ASSERT (!reserved);
- for (i = 0; i < server->cq_count; i++)
- {
- if (server->cqs[i] == cq)
- return;
- }
- GRPC_CQ_INTERNAL_REF (cq, "server");
- grpc_cq_mark_server_cq (cq);
+ GPR_ASSERT(!reserved);
+ for (i = 0; i < server->cq_count; i++) {
+ if (server->cqs[i] == cq) return;
+ }
+ GRPC_CQ_INTERNAL_REF(cq, "server");
+ grpc_cq_mark_server_cq(cq);
n = server->cq_count++;
- server->cqs = gpr_realloc (server->cqs, server->cq_count * sizeof (grpc_completion_queue *));
+ server->cqs = gpr_realloc(server->cqs,
+ server->cq_count * sizeof(grpc_completion_queue *));
server->cqs[n] = cq;
}
-grpc_server *
-grpc_server_create_from_filters (const grpc_channel_filter ** filters, size_t filter_count, const grpc_channel_args * args)
-{
+grpc_server *grpc_server_create_from_filters(
+ const grpc_channel_filter **filters, size_t filter_count,
+ const grpc_channel_args *args) {
size_t i;
/* TODO(census): restore this once we finalize census filter etc.
int census_enabled = grpc_channel_args_is_census_enabled(args); */
int census_enabled = 0;
- grpc_server *server = gpr_malloc (sizeof (grpc_server));
+ grpc_server *server = gpr_malloc(sizeof(grpc_server));
- GPR_ASSERT (grpc_is_initialized () && "call grpc_init()");
+ GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
- memset (server, 0, sizeof (grpc_server));
+ memset(server, 0, sizeof(grpc_server));
- gpr_mu_init (&server->mu_global);
- gpr_mu_init (&server->mu_call);
+ gpr_mu_init(&server->mu_global);
+ gpr_mu_init(&server->mu_call);
/* decremented by grpc_server_destroy */
- gpr_ref_init (&server->internal_refcount, 1);
- server->root_channel_data.next = server->root_channel_data.prev = &server->root_channel_data;
+ gpr_ref_init(&server->internal_refcount, 1);
+ server->root_channel_data.next = server->root_channel_data.prev =
+ &server->root_channel_data;
/* TODO(ctiller): expose a channel_arg for this */
server->max_requested_calls = 32768;
- server->request_freelist = gpr_stack_lockfree_create (server->max_requested_calls);
- for (i = 0; i < (size_t) server->max_requested_calls; i++)
- {
- gpr_stack_lockfree_push (server->request_freelist, (int) i);
- }
- request_matcher_init (&server->unregistered_request_matcher, server->max_requested_calls);
- server->requested_calls = gpr_malloc (server->max_requested_calls * sizeof (*server->requested_calls));
+ server->request_freelist =
+ gpr_stack_lockfree_create(server->max_requested_calls);
+ for (i = 0; i < (size_t)server->max_requested_calls; i++) {
+ gpr_stack_lockfree_push(server->request_freelist, (int)i);
+ }
+ request_matcher_init(&server->unregistered_request_matcher,
+ server->max_requested_calls);
+ server->requested_calls = gpr_malloc(server->max_requested_calls *
+ sizeof(*server->requested_calls));
/* Server filter stack is:
@@ -921,87 +839,78 @@ grpc_server_create_from_filters (const grpc_channel_filter ** filters, size_t fi
{passed in filter stack}
grpc_connected_channel_filter - for interfacing with transports */
server->channel_filter_count = filter_count + 1u + (census_enabled ? 1u : 0u);
- server->channel_filters = gpr_malloc (server->channel_filter_count * sizeof (grpc_channel_filter *));
+ server->channel_filters =
+ gpr_malloc(server->channel_filter_count * sizeof(grpc_channel_filter *));
server->channel_filters[0] = &server_surface_filter;
- if (census_enabled)
- {
- server->channel_filters[1] = &grpc_server_census_filter;
- }
- for (i = 0; i < filter_count; i++)
- {
- server->channel_filters[i + 1u + (census_enabled ? 1u : 0u)] = filters[i];
- }
+ if (census_enabled) {
+ server->channel_filters[1] = &grpc_server_census_filter;
+ }
+ for (i = 0; i < filter_count; i++) {
+ server->channel_filters[i + 1u + (census_enabled ? 1u : 0u)] = filters[i];
+ }
- server->channel_args = grpc_channel_args_copy (args);
+ server->channel_args = grpc_channel_args_copy(args);
return server;
}
-static int
-streq (const char *a, const char *b)
-{
- if (a == NULL && b == NULL)
- return 1;
- if (a == NULL)
- return 0;
- if (b == NULL)
- return 0;
- return 0 == strcmp (a, b);
+static int streq(const char *a, const char *b) {
+ if (a == NULL && b == NULL) return 1;
+ if (a == NULL) return 0;
+ if (b == NULL) return 0;
+ return 0 == strcmp(a, b);
}
-void *
-grpc_server_register_method (grpc_server * server, const char *method, const char *host)
-{
+void *grpc_server_register_method(grpc_server *server, const char *method,
+ const char *host) {
registered_method *m;
- if (!method)
- {
- gpr_log (GPR_ERROR, "grpc_server_register_method method string cannot be NULL");
+ if (!method) {
+ gpr_log(GPR_ERROR,
+ "grpc_server_register_method method string cannot be NULL");
+ return NULL;
+ }
+ for (m = server->registered_methods; m; m = m->next) {
+ if (streq(m->method, method) && streq(m->host, host)) {
+ gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
+ host ? host : "*");
return NULL;
}
- for (m = server->registered_methods; m; m = m->next)
- {
- if (streq (m->method, method) && streq (m->host, host))
- {
- gpr_log (GPR_ERROR, "duplicate registration for %s@%s", method, host ? host : "*");
- return NULL;
- }
- }
- m = gpr_malloc (sizeof (registered_method));
- memset (m, 0, sizeof (*m));
- request_matcher_init (&m->request_matcher, server->max_requested_calls);
- m->method = gpr_strdup (method);
- m->host = gpr_strdup (host);
+ }
+ m = gpr_malloc(sizeof(registered_method));
+ memset(m, 0, sizeof(*m));
+ request_matcher_init(&m->request_matcher, server->max_requested_calls);
+ m->method = gpr_strdup(method);
+ m->host = gpr_strdup(host);
m->next = server->registered_methods;
server->registered_methods = m;
return m;
}
-void
-grpc_server_start (grpc_server * server)
-{
+void grpc_server_start(grpc_server *server) {
listener *l;
size_t i;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- server->pollsets = gpr_malloc (sizeof (grpc_pollset *) * server->cq_count);
- for (i = 0; i < server->cq_count; i++)
- {
- server->pollsets[i] = grpc_cq_pollset (server->cqs[i]);
- }
+ server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
+ for (i = 0; i < server->cq_count; i++) {
+ server->pollsets[i] = grpc_cq_pollset(server->cqs[i]);
+ }
- for (l = server->listeners; l; l = l->next)
- {
- l->start (&exec_ctx, server, l->arg, server->pollsets, server->cq_count);
- }
+ for (l = server->listeners; l; l = l->next) {
+ l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count);
+ }
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-void
-grpc_server_setup_transport (grpc_exec_ctx * exec_ctx, grpc_server * s, grpc_transport * transport, grpc_channel_filter const **extra_filters, size_t num_extra_filters, grpc_mdctx * mdctx, const grpc_channel_args * args)
-{
+void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
+ grpc_transport *transport,
+ grpc_channel_filter const **extra_filters,
+ size_t num_extra_filters, grpc_mdctx *mdctx,
+ const grpc_channel_args *args) {
size_t num_filters = s->channel_filter_count + num_extra_filters + 1;
- grpc_channel_filter const **filters = gpr_malloc (sizeof (grpc_channel_filter *) * num_filters);
+ grpc_channel_filter const **filters =
+ gpr_malloc(sizeof(grpc_channel_filter *) * num_filters);
size_t i;
size_t num_registered_methods;
size_t alloc;
@@ -1017,194 +926,188 @@ grpc_server_setup_transport (grpc_exec_ctx * exec_ctx, grpc_server * s, grpc_tra
gpr_uint32 max_probes = 0;
grpc_transport_op op;
- for (i = 0; i < s->channel_filter_count; i++)
- {
- filters[i] = s->channel_filters[i];
- }
- for (; i < s->channel_filter_count + num_extra_filters; i++)
- {
- filters[i] = extra_filters[i - s->channel_filter_count];
- }
+ for (i = 0; i < s->channel_filter_count; i++) {
+ filters[i] = s->channel_filters[i];
+ }
+ for (; i < s->channel_filter_count + num_extra_filters; i++) {
+ filters[i] = extra_filters[i - s->channel_filter_count];
+ }
filters[i] = &grpc_connected_channel_filter;
- for (i = 0; i < s->cq_count; i++)
- {
- memset (&op, 0, sizeof (op));
- op.bind_pollset = grpc_cq_pollset (s->cqs[i]);
- grpc_transport_perform_op (exec_ctx, transport, &op);
- }
-
- channel = grpc_channel_create_from_filters (exec_ctx, NULL, filters, num_filters, args, mdctx, 0);
- chand = (channel_data *) grpc_channel_stack_element (grpc_channel_get_channel_stack (channel), 0)->channel_data;
+ for (i = 0; i < s->cq_count; i++) {
+ memset(&op, 0, sizeof(op));
+ op.bind_pollset = grpc_cq_pollset(s->cqs[i]);
+ grpc_transport_perform_op(exec_ctx, transport, &op);
+ }
+
+ channel = grpc_channel_create_from_filters(exec_ctx, NULL, filters,
+ num_filters, args, mdctx, 0);
+ chand = (channel_data *)grpc_channel_stack_element(
+ grpc_channel_get_channel_stack(channel), 0)
+ ->channel_data;
chand->server = s;
- server_ref (s);
+ server_ref(s);
chand->channel = channel;
num_registered_methods = 0;
- for (rm = s->registered_methods; rm; rm = rm->next)
- {
- num_registered_methods++;
- }
+ for (rm = s->registered_methods; rm; rm = rm->next) {
+ num_registered_methods++;
+ }
/* build a lookup table phrased in terms of mdstr's in this channels context
to quickly find registered methods */
- if (num_registered_methods > 0)
- {
- slots = 2 * num_registered_methods;
- alloc = sizeof (channel_registered_method) * slots;
- chand->registered_methods = gpr_malloc (alloc);
- memset (chand->registered_methods, 0, alloc);
- for (rm = s->registered_methods; rm; rm = rm->next)
- {
- host = rm->host ? grpc_mdstr_from_string (mdctx, rm->host, 0) : NULL;
- method = grpc_mdstr_from_string (mdctx, rm->method, 0);
- hash = GRPC_MDSTR_KV_HASH (host ? host->hash : 0, method->hash);
- for (probes = 0; chand->registered_methods[(hash + probes) % slots].server_registered_method != NULL; probes++)
- ;
- if (probes > max_probes)
- max_probes = probes;
- crm = &chand->registered_methods[(hash + probes) % slots];
- crm->server_registered_method = rm;
- crm->host = host;
- crm->method = method;
- }
- GPR_ASSERT (slots <= GPR_UINT32_MAX);
- chand->registered_method_slots = (gpr_uint32) slots;
- chand->registered_method_max_probes = max_probes;
+ if (num_registered_methods > 0) {
+ slots = 2 * num_registered_methods;
+ alloc = sizeof(channel_registered_method) * slots;
+ chand->registered_methods = gpr_malloc(alloc);
+ memset(chand->registered_methods, 0, alloc);
+ for (rm = s->registered_methods; rm; rm = rm->next) {
+ host = rm->host ? grpc_mdstr_from_string(mdctx, rm->host, 0) : NULL;
+ method = grpc_mdstr_from_string(mdctx, rm->method, 0);
+ hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
+ for (probes = 0; chand->registered_methods[(hash + probes) % slots]
+ .server_registered_method != NULL;
+ probes++)
+ ;
+ if (probes > max_probes) max_probes = probes;
+ crm = &chand->registered_methods[(hash + probes) % slots];
+ crm->server_registered_method = rm;
+ crm->host = host;
+ crm->method = method;
}
+ GPR_ASSERT(slots <= GPR_UINT32_MAX);
+ chand->registered_method_slots = (gpr_uint32)slots;
+ chand->registered_method_max_probes = max_probes;
+ }
- grpc_connected_channel_bind_transport (grpc_channel_get_channel_stack (channel), transport);
+ grpc_connected_channel_bind_transport(grpc_channel_get_channel_stack(channel),
+ transport);
- gpr_mu_lock (&s->mu_global);
+ gpr_mu_lock(&s->mu_global);
chand->next = &s->root_channel_data;
chand->prev = chand->next->prev;
chand->next->prev = chand->prev->next = chand;
- gpr_mu_unlock (&s->mu_global);
+ gpr_mu_unlock(&s->mu_global);
- gpr_free (filters);
+ gpr_free(filters);
- GRPC_CHANNEL_INTERNAL_REF (channel, "connectivity");
- memset (&op, 0, sizeof (op));
+ GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
+ memset(&op, 0, sizeof(op));
op.set_accept_stream = accept_stream;
op.set_accept_stream_user_data = chand;
op.on_connectivity_state_change = &chand->channel_connectivity_changed;
op.connectivity_state = &chand->connectivity_state;
- op.disconnect = gpr_atm_acq_load (&s->shutdown_flag) != 0;
- grpc_transport_perform_op (exec_ctx, transport, &op);
+ op.disconnect = gpr_atm_acq_load(&s->shutdown_flag) != 0;
+ grpc_transport_perform_op(exec_ctx, transport, &op);
}
-void
-done_published_shutdown (grpc_exec_ctx * exec_ctx, void *done_arg, grpc_cq_completion * storage)
-{
- (void) done_arg;
- gpr_free (storage);
+void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
+ grpc_cq_completion *storage) {
+ (void)done_arg;
+ gpr_free(storage);
}
-static void
-listener_destroy_done (grpc_exec_ctx * exec_ctx, void *s, int success)
-{
+static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
+ int success) {
grpc_server *server = s;
- gpr_mu_lock (&server->mu_global);
+ gpr_mu_lock(&server->mu_global);
server->listeners_destroyed++;
- maybe_finish_shutdown (exec_ctx, server);
- gpr_mu_unlock (&server->mu_global);
+ maybe_finish_shutdown(exec_ctx, server);
+ gpr_mu_unlock(&server->mu_global);
}
-void
-grpc_server_shutdown_and_notify (grpc_server * server, grpc_completion_queue * cq, void *tag)
-{
+void grpc_server_shutdown_and_notify(grpc_server *server,
+ grpc_completion_queue *cq, void *tag) {
listener *l;
shutdown_tag *sdt;
channel_broadcaster broadcaster;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- GRPC_SERVER_LOG_SHUTDOWN (GPR_INFO, server, cq, tag);
+ GRPC_SERVER_LOG_SHUTDOWN(GPR_INFO, server, cq, tag);
/* lock, and gather up some stuff to do */
- gpr_mu_lock (&server->mu_global);
- grpc_cq_begin_op (cq);
- if (server->shutdown_published)
- {
- grpc_cq_end_op (&exec_ctx, cq, tag, 1, done_published_shutdown, NULL, gpr_malloc (sizeof (grpc_cq_completion)));
- gpr_mu_unlock (&server->mu_global);
- goto done;
- }
- server->shutdown_tags = gpr_realloc (server->shutdown_tags, sizeof (shutdown_tag) * (server->num_shutdown_tags + 1));
+ gpr_mu_lock(&server->mu_global);
+ grpc_cq_begin_op(cq);
+ if (server->shutdown_published) {
+ grpc_cq_end_op(&exec_ctx, cq, tag, 1, done_published_shutdown, NULL,
+ gpr_malloc(sizeof(grpc_cq_completion)));
+ gpr_mu_unlock(&server->mu_global);
+ goto done;
+ }
+ server->shutdown_tags =
+ gpr_realloc(server->shutdown_tags,
+ sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
sdt = &server->shutdown_tags[server->num_shutdown_tags++];
sdt->tag = tag;
sdt->cq = cq;
- if (gpr_atm_acq_load (&server->shutdown_flag))
- {
- gpr_mu_unlock (&server->mu_global);
- goto done;
- }
+ if (gpr_atm_acq_load(&server->shutdown_flag)) {
+ gpr_mu_unlock(&server->mu_global);
+ goto done;
+ }
- server->last_shutdown_message_time = gpr_now (GPR_CLOCK_REALTIME);
+ server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
- channel_broadcaster_init (server, &broadcaster);
+ channel_broadcaster_init(server, &broadcaster);
/* collect all unregistered then registered calls */
- gpr_mu_lock (&server->mu_call);
- kill_pending_work_locked (&exec_ctx, server);
- gpr_mu_unlock (&server->mu_call);
+ gpr_mu_lock(&server->mu_call);
+ kill_pending_work_locked(&exec_ctx, server);
+ gpr_mu_unlock(&server->mu_call);
- gpr_atm_rel_store (&server->shutdown_flag, 1);
- maybe_finish_shutdown (&exec_ctx, server);
- gpr_mu_unlock (&server->mu_global);
+ gpr_atm_rel_store(&server->shutdown_flag, 1);
+ maybe_finish_shutdown(&exec_ctx, server);
+ gpr_mu_unlock(&server->mu_global);
/* Shutdown listeners */
- for (l = server->listeners; l; l = l->next)
- {
- grpc_closure_init (&l->destroy_done, listener_destroy_done, server);
- l->destroy (&exec_ctx, server, l->arg, &l->destroy_done);
- }
+ for (l = server->listeners; l; l = l->next) {
+ grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
+ l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
+ }
- channel_broadcaster_shutdown (&exec_ctx, &broadcaster, 1, 0);
+ channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
done:
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-void
-grpc_server_cancel_all_calls (grpc_server * server)
-{
+void grpc_server_cancel_all_calls(grpc_server *server) {
channel_broadcaster broadcaster;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- gpr_mu_lock (&server->mu_global);
- channel_broadcaster_init (server, &broadcaster);
- gpr_mu_unlock (&server->mu_global);
+ gpr_mu_lock(&server->mu_global);
+ channel_broadcaster_init(server, &broadcaster);
+ gpr_mu_unlock(&server->mu_global);
- channel_broadcaster_shutdown (&exec_ctx, &broadcaster, 0, 1);
- grpc_exec_ctx_finish (&exec_ctx);
+ channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0, 1);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-void
-grpc_server_destroy (grpc_server * server)
-{
+void grpc_server_destroy(grpc_server *server) {
listener *l;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- gpr_mu_lock (&server->mu_global);
- GPR_ASSERT (gpr_atm_acq_load (&server->shutdown_flag) || !server->listeners);
- GPR_ASSERT (server->listeners_destroyed == num_listeners (server));
+ gpr_mu_lock(&server->mu_global);
+ GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
+ GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
- while (server->listeners)
- {
- l = server->listeners;
- server->listeners = l->next;
- gpr_free (l);
- }
+ while (server->listeners) {
+ l = server->listeners;
+ server->listeners = l->next;
+ gpr_free(l);
+ }
- gpr_mu_unlock (&server->mu_global);
+ gpr_mu_unlock(&server->mu_global);
- server_unref (&exec_ctx, server);
- grpc_exec_ctx_finish (&exec_ctx);
+ server_unref(&exec_ctx, server);
+ grpc_exec_ctx_finish(&exec_ctx);
}
-void
-grpc_server_add_listener (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, void (*start) (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, grpc_pollset ** pollsets, size_t pollset_count), void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, grpc_closure * on_done))
-{
- listener *l = gpr_malloc (sizeof (listener));
+void grpc_server_add_listener(
+ grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ grpc_pollset **pollsets, size_t pollset_count),
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ grpc_closure *on_done)) {
+ listener *l = gpr_malloc(sizeof(listener));
l->arg = arg;
l->start = start;
l->destroy = destroy;
@@ -1212,82 +1115,79 @@ grpc_server_add_listener (grpc_exec_ctx * exec_ctx, grpc_server * server, void *
server->listeners = l;
}
-static grpc_call_error
-queue_call_request (grpc_exec_ctx * exec_ctx, grpc_server * server, requested_call * rc)
-{
+static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
+ grpc_server *server,
+ requested_call *rc) {
call_data *calld = NULL;
request_matcher *request_matcher = NULL;
int request_id;
- if (gpr_atm_acq_load (&server->shutdown_flag))
- {
- fail_call (exec_ctx, server, rc);
- return GRPC_CALL_OK;
- }
- request_id = gpr_stack_lockfree_pop (server->request_freelist);
- if (request_id == -1)
- {
- /* out of request ids: just fail this one */
- fail_call (exec_ctx, server, rc);
- return GRPC_CALL_OK;
- }
- switch (rc->type)
- {
+ if (gpr_atm_acq_load(&server->shutdown_flag)) {
+ fail_call(exec_ctx, server, rc);
+ return GRPC_CALL_OK;
+ }
+ request_id = gpr_stack_lockfree_pop(server->request_freelist);
+ if (request_id == -1) {
+ /* out of request ids: just fail this one */
+ fail_call(exec_ctx, server, rc);
+ return GRPC_CALL_OK;
+ }
+ switch (rc->type) {
case BATCH_CALL:
request_matcher = &server->unregistered_request_matcher;
break;
case REGISTERED_CALL:
request_matcher = &rc->data.registered.registered_method->request_matcher;
break;
- }
+ }
server->requested_calls[request_id] = *rc;
- gpr_free (rc);
- if (gpr_stack_lockfree_push (request_matcher->requests, request_id))
- {
- /* this was the first queued request: we need to lock and start
- matching calls */
- gpr_mu_lock (&server->mu_call);
- while ((calld = request_matcher->pending_head) != NULL)
- {
- request_id = gpr_stack_lockfree_pop (request_matcher->requests);
- if (request_id == -1)
- break;
- request_matcher->pending_head = calld->pending_next;
- gpr_mu_unlock (&server->mu_call);
- gpr_mu_lock (&calld->mu_state);
- if (calld->state == ZOMBIED)
- {
- gpr_mu_unlock (&calld->mu_state);
- grpc_closure_init (&calld->kill_zombie_closure, kill_zombie, grpc_call_stack_element (grpc_call_get_call_stack (calld->call), 0));
- grpc_exec_ctx_enqueue (exec_ctx, &calld->kill_zombie_closure, 1);
- }
- else
- {
- GPR_ASSERT (calld->state == PENDING);
- calld->state = ACTIVATED;
- gpr_mu_unlock (&calld->mu_state);
- begin_call (exec_ctx, server, calld, &server->requested_calls[request_id]);
- }
- gpr_mu_lock (&server->mu_call);
- }
- gpr_mu_unlock (&server->mu_call);
+ gpr_free(rc);
+ if (gpr_stack_lockfree_push(request_matcher->requests, request_id)) {
+ /* this was the first queued request: we need to lock and start
+ matching calls */
+ gpr_mu_lock(&server->mu_call);
+ while ((calld = request_matcher->pending_head) != NULL) {
+ request_id = gpr_stack_lockfree_pop(request_matcher->requests);
+ if (request_id == -1) break;
+ request_matcher->pending_head = calld->pending_next;
+ gpr_mu_unlock(&server->mu_call);
+ gpr_mu_lock(&calld->mu_state);
+ if (calld->state == ZOMBIED) {
+ gpr_mu_unlock(&calld->mu_state);
+ grpc_closure_init(
+ &calld->kill_zombie_closure, kill_zombie,
+ grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
+ grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, 1);
+ } else {
+ GPR_ASSERT(calld->state == PENDING);
+ calld->state = ACTIVATED;
+ gpr_mu_unlock(&calld->mu_state);
+ begin_call(exec_ctx, server, calld,
+ &server->requested_calls[request_id]);
+ }
+ gpr_mu_lock(&server->mu_call);
}
+ gpr_mu_unlock(&server->mu_call);
+ }
return GRPC_CALL_OK;
}
-grpc_call_error
-grpc_server_request_call (grpc_server * server, grpc_call ** call, grpc_call_details * details, grpc_metadata_array * initial_metadata, grpc_completion_queue * cq_bound_to_call, grpc_completion_queue * cq_for_notification, void *tag)
-{
+grpc_call_error grpc_server_request_call(
+ grpc_server *server, grpc_call **call, grpc_call_details *details,
+ grpc_metadata_array *initial_metadata,
+ grpc_completion_queue *cq_bound_to_call,
+ grpc_completion_queue *cq_for_notification, void *tag) {
grpc_call_error error;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- requested_call *rc = gpr_malloc (sizeof (*rc));
- GRPC_SERVER_LOG_REQUEST_CALL (GPR_INFO, server, call, details, initial_metadata, cq_bound_to_call, cq_for_notification, tag);
- if (!grpc_cq_is_server_cq (cq_for_notification))
- {
- gpr_free (rc);
- error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
- goto done;
- }
- grpc_cq_begin_op (cq_for_notification);
+ requested_call *rc = gpr_malloc(sizeof(*rc));
+ GRPC_SERVER_LOG_REQUEST_CALL(GPR_INFO, server, call, details,
+ initial_metadata, cq_bound_to_call,
+ cq_for_notification, tag);
+ if (!grpc_cq_is_server_cq(cq_for_notification)) {
+ gpr_free(rc);
+ error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
+ goto done;
+ }
+ grpc_cq_begin_op(cq_for_notification);
details->reserved = NULL;
rc->type = BATCH_CALL;
rc->server = server;
@@ -1297,26 +1197,27 @@ grpc_server_request_call (grpc_server * server, grpc_call ** call, grpc_call_det
rc->call = call;
rc->data.batch.details = details;
rc->data.batch.initial_metadata = initial_metadata;
- error = queue_call_request (&exec_ctx, server, rc);
+ error = queue_call_request(&exec_ctx, server, rc);
done:
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
return error;
}
-grpc_call_error
-grpc_server_request_registered_call (grpc_server * server, void *rm, grpc_call ** call, gpr_timespec * deadline, grpc_metadata_array * initial_metadata, grpc_byte_buffer ** optional_payload, grpc_completion_queue * cq_bound_to_call, grpc_completion_queue * cq_for_notification, void *tag)
-{
+grpc_call_error grpc_server_request_registered_call(
+ grpc_server *server, void *rm, grpc_call **call, gpr_timespec *deadline,
+ grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
+ grpc_completion_queue *cq_bound_to_call,
+ grpc_completion_queue *cq_for_notification, void *tag) {
grpc_call_error error;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- requested_call *rc = gpr_malloc (sizeof (*rc));
+ requested_call *rc = gpr_malloc(sizeof(*rc));
registered_method *registered_method = rm;
- if (!grpc_cq_is_server_cq (cq_for_notification))
- {
- gpr_free (rc);
- error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
- goto done;
- }
- grpc_cq_begin_op (cq_for_notification);
+ if (!grpc_cq_is_server_cq(cq_for_notification)) {
+ gpr_free(rc);
+ error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
+ goto done;
+ }
+ grpc_cq_begin_op(cq_for_notification);
rc->type = REGISTERED_CALL;
rc->server = server;
rc->tag = tag;
@@ -1327,36 +1228,33 @@ grpc_server_request_registered_call (grpc_server * server, void *rm, grpc_call *
rc->data.registered.deadline = deadline;
rc->data.registered.initial_metadata = initial_metadata;
rc->data.registered.optional_payload = optional_payload;
- error = queue_call_request (&exec_ctx, server, rc);
+ error = queue_call_request(&exec_ctx, server, rc);
done:
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
return error;
}
-static void publish_registered_or_batch (grpc_exec_ctx * exec_ctx, grpc_call * call, int success, void *tag);
-static void
-publish_was_not_set (grpc_exec_ctx * exec_ctx, grpc_call * call, int success, void *tag)
-{
- abort ();
+static void publish_registered_or_batch(grpc_exec_ctx *exec_ctx,
+ grpc_call *call, int success,
+ void *tag);
+static void publish_was_not_set(grpc_exec_ctx *exec_ctx, grpc_call *call,
+ int success, void *tag) {
+ abort();
}
-static void
-cpstr (char **dest, size_t * capacity, grpc_mdstr * value)
-{
+static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
gpr_slice slice = value->slice;
- size_t len = GPR_SLICE_LENGTH (slice);
+ size_t len = GPR_SLICE_LENGTH(slice);
- if (len + 1 > *capacity)
- {
- *capacity = GPR_MAX (len + 1, *capacity * 2);
- *dest = gpr_realloc (*dest, *capacity);
- }
- memcpy (*dest, grpc_mdstr_as_c_string (value), len + 1);
+ if (len + 1 > *capacity) {
+ *capacity = GPR_MAX(len + 1, *capacity * 2);
+ *dest = gpr_realloc(*dest, *capacity);
+ }
+ memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
}
-static void
-begin_call (grpc_exec_ctx * exec_ctx, grpc_server * server, call_data * calld, requested_call * rc)
-{
+static void begin_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
+ call_data *calld, requested_call *rc) {
grpc_ioreq_completion_func publish = publish_was_not_set;
grpc_ioreq req[2];
grpc_ioreq *r = req;
@@ -1367,16 +1265,17 @@ begin_call (grpc_exec_ctx * exec_ctx, grpc_server * server, call_data * calld, r
fill in the metadata array passed by the client, we need to perform
an ioreq op, that should complete immediately. */
- grpc_call_set_completion_queue (exec_ctx, calld->call, rc->cq_bound_to_call);
+ grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
*rc->call = calld->call;
calld->cq_new = rc->cq_for_notification;
- switch (rc->type)
- {
+ switch (rc->type) {
case BATCH_CALL:
- GPR_ASSERT (calld->host != NULL);
- GPR_ASSERT (calld->path != NULL);
- cpstr (&rc->data.batch.details->host, &rc->data.batch.details->host_capacity, calld->host);
- cpstr (&rc->data.batch.details->method, &rc->data.batch.details->method_capacity, calld->path);
+ GPR_ASSERT(calld->host != NULL);
+ GPR_ASSERT(calld->path != NULL);
+ cpstr(&rc->data.batch.details->host,
+ &rc->data.batch.details->host_capacity, calld->host);
+ cpstr(&rc->data.batch.details->method,
+ &rc->data.batch.details->method_capacity, calld->path);
rc->data.batch.details->deadline = calld->deadline;
r->op = GRPC_IOREQ_RECV_INITIAL_METADATA;
r->data.recv_metadata = rc->data.batch.initial_metadata;
@@ -1390,81 +1289,76 @@ begin_call (grpc_exec_ctx * exec_ctx, grpc_server * server, call_data * calld, r
r->data.recv_metadata = rc->data.registered.initial_metadata;
r->flags = 0;
r++;
- if (rc->data.registered.optional_payload)
- {
- r->op = GRPC_IOREQ_RECV_MESSAGE;
- r->data.recv_message = rc->data.registered.optional_payload;
- r->flags = 0;
- r++;
- }
+ if (rc->data.registered.optional_payload) {
+ r->op = GRPC_IOREQ_RECV_MESSAGE;
+ r->data.recv_message = rc->data.registered.optional_payload;
+ r->flags = 0;
+ r++;
+ }
publish = publish_registered_or_batch;
break;
- }
+ }
- GRPC_CALL_INTERNAL_REF (calld->call, "server");
- grpc_call_start_ioreq_and_call_back (exec_ctx, calld->call, req, (size_t) (r - req), publish, rc);
+ GRPC_CALL_INTERNAL_REF(calld->call, "server");
+ grpc_call_start_ioreq_and_call_back(exec_ctx, calld->call, req,
+ (size_t)(r - req), publish, rc);
}
-static void
-done_request_event (grpc_exec_ctx * exec_ctx, void *req, grpc_cq_completion * c)
-{
+static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
+ grpc_cq_completion *c) {
requested_call *rc = req;
grpc_server *server = rc->server;
- if (rc >= server->requested_calls && rc < server->requested_calls + server->max_requested_calls)
- {
- GPR_ASSERT (rc - server->requested_calls <= INT_MAX);
- gpr_stack_lockfree_push (server->request_freelist, (int) (rc - server->requested_calls));
- }
- else
- {
- gpr_free (req);
- }
+ if (rc >= server->requested_calls &&
+ rc < server->requested_calls + server->max_requested_calls) {
+ GPR_ASSERT(rc - server->requested_calls <= INT_MAX);
+ gpr_stack_lockfree_push(server->request_freelist,
+ (int)(rc - server->requested_calls));
+ } else {
+ gpr_free(req);
+ }
- server_unref (exec_ctx, server);
+ server_unref(exec_ctx, server);
}
-static void
-fail_call (grpc_exec_ctx * exec_ctx, grpc_server * server, requested_call * rc)
-{
+static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
+ requested_call *rc) {
*rc->call = NULL;
- switch (rc->type)
- {
+ switch (rc->type) {
case BATCH_CALL:
rc->data.batch.initial_metadata->count = 0;
break;
case REGISTERED_CALL:
rc->data.registered.initial_metadata->count = 0;
break;
- }
- server_ref (server);
- grpc_cq_end_op (exec_ctx, rc->cq_for_notification, rc->tag, 0, done_request_event, rc, &rc->completion);
+ }
+ server_ref(server);
+ grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, 0,
+ done_request_event, rc, &rc->completion);
}
-static void
-publish_registered_or_batch (grpc_exec_ctx * exec_ctx, grpc_call * call, int success, void *prc)
-{
- grpc_call_element *elem = grpc_call_stack_element (grpc_call_get_call_stack (call), 0);
+static void publish_registered_or_batch(grpc_exec_ctx *exec_ctx,
+ grpc_call *call, int success,
+ void *prc) {
+ grpc_call_element *elem =
+ grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
requested_call *rc = prc;
call_data *calld = elem->call_data;
channel_data *chand = elem->channel_data;
- server_ref (chand->server);
- grpc_cq_end_op (exec_ctx, calld->cq_new, rc->tag, success, done_request_event, rc, &rc->completion);
- GRPC_CALL_INTERNAL_UNREF (exec_ctx, call, "server");
+ server_ref(chand->server);
+ grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, success, done_request_event,
+ rc, &rc->completion);
+ GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "server");
}
-const grpc_channel_args *
-grpc_server_get_channel_args (grpc_server * server)
-{
+const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
return server->channel_args;
}
-int
-grpc_server_has_open_connections (grpc_server * server)
-{
+int grpc_server_has_open_connections(grpc_server *server) {
int r;
- gpr_mu_lock (&server->mu_global);
+ gpr_mu_lock(&server->mu_global);
r = server->root_channel_data.next != &server->root_channel_data;
- gpr_mu_unlock (&server->mu_global);
+ gpr_mu_unlock(&server->mu_global);
return r;
}
diff --git a/src/core/surface/server.h b/src/core/surface/server.h
index 96d47de3cb..4c46d07679 100644
--- a/src/core/surface/server.h
+++ b/src/core/surface/server.h
@@ -39,18 +39,29 @@
#include "src/core/transport/transport.h"
/* Create a server */
-grpc_server *grpc_server_create_from_filters (const grpc_channel_filter ** filters, size_t filter_count, const grpc_channel_args * args);
+grpc_server *grpc_server_create_from_filters(
+ const grpc_channel_filter **filters, size_t filter_count,
+ const grpc_channel_args *args);
/* Add a listener to the server: when the server starts, it will call start,
and when it shuts down, it will call destroy */
-void grpc_server_add_listener (grpc_exec_ctx * exec_ctx, grpc_server * server, void *listener, void (*start) (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, grpc_pollset ** pollsets, size_t npollsets), void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_server * server, void *arg, grpc_closure * on_done));
+void grpc_server_add_listener(
+ grpc_exec_ctx *exec_ctx, grpc_server *server, void *listener,
+ void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ grpc_pollset **pollsets, size_t npollsets),
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
+ grpc_closure *on_done));
/* Setup a transport - creates a channel stack, binds the transport to the
server */
-void grpc_server_setup_transport (grpc_exec_ctx * exec_ctx, grpc_server * server, grpc_transport * transport, grpc_channel_filter const **extra_filters, size_t num_extra_filters, grpc_mdctx * mdctx, const grpc_channel_args * args);
+void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *server,
+ grpc_transport *transport,
+ grpc_channel_filter const **extra_filters,
+ size_t num_extra_filters, grpc_mdctx *mdctx,
+ const grpc_channel_args *args);
-const grpc_channel_args *grpc_server_get_channel_args (grpc_server * server);
+const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server);
-int grpc_server_has_open_connections (grpc_server * server);
+int grpc_server_has_open_connections(grpc_server *server);
#endif /* GRPC_INTERNAL_CORE_SURFACE_SERVER_H */
diff --git a/src/core/surface/server_chttp2.c b/src/core/surface/server_chttp2.c
index 8ea3f734e4..3904ce969d 100644
--- a/src/core/surface/server_chttp2.c
+++ b/src/core/surface/server_chttp2.c
@@ -42,18 +42,17 @@
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
-static void
-setup_transport (grpc_exec_ctx * exec_ctx, void *server, grpc_transport * transport, grpc_mdctx * mdctx)
-{
+static void setup_transport(grpc_exec_ctx *exec_ctx, void *server,
+ grpc_transport *transport, grpc_mdctx *mdctx) {
static grpc_channel_filter const *extra_filters[] = {
- &grpc_http_server_filter
- };
- grpc_server_setup_transport (exec_ctx, server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (server));
+ &grpc_http_server_filter};
+ grpc_server_setup_transport(exec_ctx, server, transport, extra_filters,
+ GPR_ARRAY_SIZE(extra_filters), mdctx,
+ grpc_server_get_channel_args(server));
}
-static void
-new_transport (grpc_exec_ctx * exec_ctx, void *server, grpc_endpoint * tcp)
-{
+static void new_transport(grpc_exec_ctx *exec_ctx, void *server,
+ grpc_endpoint *tcp) {
/*
* Beware that the call to grpc_create_chttp2_transport() has to happen before
* grpc_tcp_server_destroy(). This is fine here, but similar code
@@ -61,32 +60,30 @@ new_transport (grpc_exec_ctx * exec_ctx, void *server, grpc_endpoint * tcp)
* (as in server_secure_chttp2.c) needs to add synchronization to avoid this
* case.
*/
- grpc_mdctx *mdctx = grpc_mdctx_create ();
- grpc_transport *transport = grpc_create_chttp2_transport (exec_ctx, grpc_server_get_channel_args (server), tcp, mdctx, 0);
- setup_transport (exec_ctx, server, transport, mdctx);
- grpc_chttp2_transport_start_reading (exec_ctx, transport, NULL, 0);
+ grpc_mdctx *mdctx = grpc_mdctx_create();
+ grpc_transport *transport = grpc_create_chttp2_transport(
+ exec_ctx, grpc_server_get_channel_args(server), tcp, mdctx, 0);
+ setup_transport(exec_ctx, server, transport, mdctx);
+ grpc_chttp2_transport_start_reading(exec_ctx, transport, NULL, 0);
}
/* Server callback: start listening on our ports */
-static void
-start (grpc_exec_ctx * exec_ctx, grpc_server * server, void *tcpp, grpc_pollset ** pollsets, size_t pollset_count)
-{
+static void start(grpc_exec_ctx *exec_ctx, grpc_server *server, void *tcpp,
+ grpc_pollset **pollsets, size_t pollset_count) {
grpc_tcp_server *tcp = tcpp;
- grpc_tcp_server_start (exec_ctx, tcp, pollsets, pollset_count, new_transport, server);
+ grpc_tcp_server_start(exec_ctx, tcp, pollsets, pollset_count, new_transport,
+ server);
}
/* Server callback: destroy the tcp listener (so we don't generate further
callbacks) */
-static void
-destroy (grpc_exec_ctx * exec_ctx, grpc_server * server, void *tcpp, grpc_closure * destroy_done)
-{
+static void destroy(grpc_exec_ctx *exec_ctx, grpc_server *server, void *tcpp,
+ grpc_closure *destroy_done) {
grpc_tcp_server *tcp = tcpp;
- grpc_tcp_server_destroy (exec_ctx, tcp, destroy_done);
+ grpc_tcp_server_destroy(exec_ctx, tcp, destroy_done);
}
-int
-grpc_server_add_insecure_http2_port (grpc_server * server, const char *addr)
-{
+int grpc_server_add_insecure_http2_port(grpc_server *server, const char *addr) {
grpc_resolved_addresses *resolved = NULL;
grpc_tcp_server *tcp = NULL;
size_t i;
@@ -95,62 +92,55 @@ grpc_server_add_insecure_http2_port (grpc_server * server, const char *addr)
int port_temp;
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- resolved = grpc_blocking_resolve_address (addr, "http");
- if (!resolved)
- {
- goto error;
- }
+ resolved = grpc_blocking_resolve_address(addr, "http");
+ if (!resolved) {
+ goto error;
+ }
- tcp = grpc_tcp_server_create ();
- if (!tcp)
- {
- goto error;
- }
+ tcp = grpc_tcp_server_create();
+ if (!tcp) {
+ goto error;
+ }
- for (i = 0; i < resolved->naddrs; i++)
- {
- port_temp = grpc_tcp_server_add_port (tcp, (struct sockaddr *) &resolved->addrs[i].addr, resolved->addrs[i].len);
- if (port_temp >= 0)
- {
- if (port_num == -1)
- {
- port_num = port_temp;
- }
- else
- {
- GPR_ASSERT (port_num == port_temp);
- }
- count++;
- }
- }
- if (count == 0)
- {
- gpr_log (GPR_ERROR, "No address added out of total %d resolved", resolved->naddrs);
- goto error;
+ for (i = 0; i < resolved->naddrs; i++) {
+ port_temp = grpc_tcp_server_add_port(
+ tcp, (struct sockaddr *)&resolved->addrs[i].addr,
+ resolved->addrs[i].len);
+ if (port_temp >= 0) {
+ if (port_num == -1) {
+ port_num = port_temp;
+ } else {
+ GPR_ASSERT(port_num == port_temp);
+ }
+ count++;
}
- if (count != resolved->naddrs)
- {
- gpr_log (GPR_ERROR, "Only %d addresses added out of total %d resolved", count, resolved->naddrs);
- }
- grpc_resolved_addresses_destroy (resolved);
+ }
+ if (count == 0) {
+ gpr_log(GPR_ERROR, "No address added out of total %d resolved",
+ resolved->naddrs);
+ goto error;
+ }
+ if (count != resolved->naddrs) {
+ gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
+ count, resolved->naddrs);
+ }
+ grpc_resolved_addresses_destroy(resolved);
/* Register with the server only upon success */
- grpc_server_add_listener (&exec_ctx, server, tcp, start, destroy);
+ grpc_server_add_listener(&exec_ctx, server, tcp, start, destroy);
goto done;
/* Error path: cleanup and return */
error:
- if (resolved)
- {
- grpc_resolved_addresses_destroy (resolved);
- }
- if (tcp)
- {
- grpc_tcp_server_destroy (&exec_ctx, tcp, NULL);
- }
+ if (resolved) {
+ grpc_resolved_addresses_destroy(resolved);
+ }
+ if (tcp) {
+ grpc_tcp_server_destroy(&exec_ctx, tcp, NULL);
+ }
port_num = 0;
done:
- grpc_exec_ctx_finish (&exec_ctx);
+ grpc_exec_ctx_finish(&exec_ctx);
return port_num;
}
diff --git a/src/core/surface/server_create.c b/src/core/surface/server_create.c
index d2f7b8e262..fc7ae820f5 100644
--- a/src/core/surface/server_create.c
+++ b/src/core/surface/server_create.c
@@ -36,10 +36,9 @@
#include "src/core/surface/server.h"
#include "src/core/channel/compress_filter.h"
-grpc_server *
-grpc_server_create (const grpc_channel_args * args, void *reserved)
-{
- const grpc_channel_filter *filters[] = { &grpc_compress_filter };
- (void) reserved;
- return grpc_server_create_from_filters (filters, GPR_ARRAY_SIZE (filters), args);
+grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
+ const grpc_channel_filter *filters[] = {&grpc_compress_filter};
+ (void)reserved;
+ return grpc_server_create_from_filters(filters, GPR_ARRAY_SIZE(filters),
+ args);
}
diff --git a/src/core/surface/version.c b/src/core/surface/version.c
index 4b90e06a04..e559d51448 100644
--- a/src/core/surface/version.c
+++ b/src/core/surface/version.c
@@ -36,6 +36,4 @@
#include <grpc/grpc.h>
-const char *grpc_version_string(void) {
- return "0.11.0.0";
-}
+const char *grpc_version_string(void) { return "0.11.0.0"; }
diff --git a/src/core/transport/chttp2/alpn.c b/src/core/transport/chttp2/alpn.c
index f008615c9a..69da4e6718 100644
--- a/src/core/transport/chttp2/alpn.c
+++ b/src/core/transport/chttp2/alpn.c
@@ -36,29 +36,21 @@
#include <grpc/support/useful.h>
/* in order of preference */
-static const char *const supported_versions[] = { "h2" };
+static const char *const supported_versions[] = {"h2"};
-int
-grpc_chttp2_is_alpn_version_supported (const char *version, size_t size)
-{
+int grpc_chttp2_is_alpn_version_supported(const char *version, size_t size) {
size_t i;
- for (i = 0; i < GPR_ARRAY_SIZE (supported_versions); i++)
- {
- if (!strncmp (version, supported_versions[i], size))
- return 1;
- }
+ for (i = 0; i < GPR_ARRAY_SIZE(supported_versions); i++) {
+ if (!strncmp(version, supported_versions[i], size)) return 1;
+ }
return 0;
}
-size_t
-grpc_chttp2_num_alpn_versions (void)
-{
- return GPR_ARRAY_SIZE (supported_versions);
+size_t grpc_chttp2_num_alpn_versions(void) {
+ return GPR_ARRAY_SIZE(supported_versions);
}
-const char *
-grpc_chttp2_get_alpn_version_index (size_t i)
-{
- GPR_ASSERT (i < GPR_ARRAY_SIZE (supported_versions));
+const char *grpc_chttp2_get_alpn_version_index(size_t i) {
+ GPR_ASSERT(i < GPR_ARRAY_SIZE(supported_versions));
return supported_versions[i];
}
diff --git a/src/core/transport/chttp2/alpn.h b/src/core/transport/chttp2/alpn.h
index b45081eccf..f38b4c3167 100644
--- a/src/core/transport/chttp2/alpn.h
+++ b/src/core/transport/chttp2/alpn.h
@@ -37,13 +37,13 @@
#include <string.h>
/* Retuns 1 if the version is supported, 0 otherwise. */
-int grpc_chttp2_is_alpn_version_supported (const char *version, size_t size);
+int grpc_chttp2_is_alpn_version_supported(const char *version, size_t size);
/* Returns the number of protocol versions to advertise */
-size_t grpc_chttp2_num_alpn_versions (void);
+size_t grpc_chttp2_num_alpn_versions(void);
/* Returns the protocol version at index i (0 <= i <
* grpc_chttp2_num_alpn_versions()) */
-const char *grpc_chttp2_get_alpn_version_index (size_t i);
+const char *grpc_chttp2_get_alpn_version_index(size_t i);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_ALPN_H */
diff --git a/src/core/transport/chttp2/bin_encoder.c b/src/core/transport/chttp2/bin_encoder.c
index 929f65ad66..e21d800083 100644
--- a/src/core/transport/chttp2/bin_encoder.c
+++ b/src/core/transport/chttp2/bin_encoder.c
@@ -38,56 +38,51 @@
#include "src/core/transport/chttp2/huffsyms.h"
#include <grpc/support/log.h>
-static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static const char alphabet[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-typedef struct
-{
+typedef struct {
gpr_uint16 bits;
gpr_uint8 length;
} b64_huff_sym;
static const b64_huff_sym huff_alphabet[64] = {
- {0x21, 6}, {0x5d, 7}, {0x5e, 7}, {0x5f, 7}, {0x60, 7}, {0x61, 7},
- {0x62, 7}, {0x63, 7}, {0x64, 7}, {0x65, 7}, {0x66, 7}, {0x67, 7},
- {0x68, 7}, {0x69, 7}, {0x6a, 7}, {0x6b, 7}, {0x6c, 7}, {0x6d, 7},
- {0x6e, 7}, {0x6f, 7}, {0x70, 7}, {0x71, 7}, {0x72, 7}, {0xfc, 8},
- {0x73, 7}, {0xfd, 8}, {0x3, 5}, {0x23, 6}, {0x4, 5}, {0x24, 6},
- {0x5, 5}, {0x25, 6}, {0x26, 6}, {0x27, 6}, {0x6, 5}, {0x74, 7},
- {0x75, 7}, {0x28, 6}, {0x29, 6}, {0x2a, 6}, {0x7, 5}, {0x2b, 6},
- {0x76, 7}, {0x2c, 6}, {0x8, 5}, {0x9, 5}, {0x2d, 6}, {0x77, 7},
- {0x78, 7}, {0x79, 7}, {0x7a, 7}, {0x7b, 7}, {0x0, 5}, {0x1, 5},
- {0x2, 5}, {0x19, 6}, {0x1a, 6}, {0x1b, 6}, {0x1c, 6}, {0x1d, 6},
- {0x1e, 6}, {0x1f, 6}, {0x7fb, 11}, {0x18, 6}
-};
-
-static const gpr_uint8 tail_xtra[3] = { 0, 2, 3 };
-
-gpr_slice
-grpc_chttp2_base64_encode (gpr_slice input)
-{
- size_t input_length = GPR_SLICE_LENGTH (input);
+ {0x21, 6}, {0x5d, 7}, {0x5e, 7}, {0x5f, 7}, {0x60, 7}, {0x61, 7},
+ {0x62, 7}, {0x63, 7}, {0x64, 7}, {0x65, 7}, {0x66, 7}, {0x67, 7},
+ {0x68, 7}, {0x69, 7}, {0x6a, 7}, {0x6b, 7}, {0x6c, 7}, {0x6d, 7},
+ {0x6e, 7}, {0x6f, 7}, {0x70, 7}, {0x71, 7}, {0x72, 7}, {0xfc, 8},
+ {0x73, 7}, {0xfd, 8}, {0x3, 5}, {0x23, 6}, {0x4, 5}, {0x24, 6},
+ {0x5, 5}, {0x25, 6}, {0x26, 6}, {0x27, 6}, {0x6, 5}, {0x74, 7},
+ {0x75, 7}, {0x28, 6}, {0x29, 6}, {0x2a, 6}, {0x7, 5}, {0x2b, 6},
+ {0x76, 7}, {0x2c, 6}, {0x8, 5}, {0x9, 5}, {0x2d, 6}, {0x77, 7},
+ {0x78, 7}, {0x79, 7}, {0x7a, 7}, {0x7b, 7}, {0x0, 5}, {0x1, 5},
+ {0x2, 5}, {0x19, 6}, {0x1a, 6}, {0x1b, 6}, {0x1c, 6}, {0x1d, 6},
+ {0x1e, 6}, {0x1f, 6}, {0x7fb, 11}, {0x18, 6}};
+
+static const gpr_uint8 tail_xtra[3] = {0, 2, 3};
+
+gpr_slice grpc_chttp2_base64_encode(gpr_slice input) {
+ size_t input_length = GPR_SLICE_LENGTH(input);
size_t input_triplets = input_length / 3;
size_t tail_case = input_length % 3;
size_t output_length = input_triplets * 4 + tail_xtra[tail_case];
- gpr_slice output = gpr_slice_malloc (output_length);
- gpr_uint8 *in = GPR_SLICE_START_PTR (input);
- char *out = (char *) GPR_SLICE_START_PTR (output);
+ gpr_slice output = gpr_slice_malloc(output_length);
+ gpr_uint8 *in = GPR_SLICE_START_PTR(input);
+ char *out = (char *)GPR_SLICE_START_PTR(output);
size_t i;
/* encode full triplets */
- for (i = 0; i < input_triplets; i++)
- {
- out[0] = alphabet[in[0] >> 2];
- out[1] = alphabet[((in[0] & 0x3) << 4) | (in[1] >> 4)];
- out[2] = alphabet[((in[1] & 0xf) << 2) | (in[2] >> 6)];
- out[3] = alphabet[in[2] & 0x3f];
- out += 4;
- in += 3;
- }
+ for (i = 0; i < input_triplets; i++) {
+ out[0] = alphabet[in[0] >> 2];
+ out[1] = alphabet[((in[0] & 0x3) << 4) | (in[1] >> 4)];
+ out[2] = alphabet[((in[1] & 0xf) << 2) | (in[2] >> 6)];
+ out[3] = alphabet[in[2] & 0x3f];
+ out += 4;
+ in += 3;
+ }
/* encode the remaining bytes */
- switch (tail_case)
- {
+ switch (tail_case) {
case 0:
break;
case 1:
@@ -103,16 +98,14 @@ grpc_chttp2_base64_encode (gpr_slice input)
out += 3;
in += 2;
break;
- }
+ }
- GPR_ASSERT (out == (char *) GPR_SLICE_END_PTR (output));
- GPR_ASSERT (in == GPR_SLICE_END_PTR (input));
+ GPR_ASSERT(out == (char *)GPR_SLICE_END_PTR(output));
+ GPR_ASSERT(in == GPR_SLICE_END_PTR(input));
return output;
}
-gpr_slice
-grpc_chttp2_huffman_compress (gpr_slice input)
-{
+gpr_slice grpc_chttp2_huffman_compress(gpr_slice input) {
size_t nbits;
gpr_uint8 *in;
gpr_uint8 *out;
@@ -121,85 +114,73 @@ grpc_chttp2_huffman_compress (gpr_slice input)
gpr_uint32 temp_length = 0;
nbits = 0;
- for (in = GPR_SLICE_START_PTR (input); in != GPR_SLICE_END_PTR (input); ++in)
- {
- nbits += grpc_chttp2_huffsyms[*in].length;
+ for (in = GPR_SLICE_START_PTR(input); in != GPR_SLICE_END_PTR(input); ++in) {
+ nbits += grpc_chttp2_huffsyms[*in].length;
+ }
+
+ output = gpr_slice_malloc(nbits / 8 + (nbits % 8 != 0));
+ out = GPR_SLICE_START_PTR(output);
+ for (in = GPR_SLICE_START_PTR(input); in != GPR_SLICE_END_PTR(input); ++in) {
+ int sym = *in;
+ temp <<= grpc_chttp2_huffsyms[sym].length;
+ temp |= grpc_chttp2_huffsyms[sym].bits;
+ temp_length += grpc_chttp2_huffsyms[sym].length;
+
+ while (temp_length > 8) {
+ temp_length -= 8;
+ *out++ = (gpr_uint8)(temp >> temp_length);
}
+ }
- output = gpr_slice_malloc (nbits / 8 + (nbits % 8 != 0));
- out = GPR_SLICE_START_PTR (output);
- for (in = GPR_SLICE_START_PTR (input); in != GPR_SLICE_END_PTR (input); ++in)
- {
- int sym = *in;
- temp <<= grpc_chttp2_huffsyms[sym].length;
- temp |= grpc_chttp2_huffsyms[sym].bits;
- temp_length += grpc_chttp2_huffsyms[sym].length;
-
- while (temp_length > 8)
- {
- temp_length -= 8;
- *out++ = (gpr_uint8) (temp >> temp_length);
- }
- }
+ if (temp_length) {
+ *out++ = (gpr_uint8)(temp << (8u - temp_length)) |
+ (gpr_uint8)(0xffu >> temp_length);
+ }
- if (temp_length)
- {
- *out++ = (gpr_uint8) (temp << (8u - temp_length)) | (gpr_uint8) (0xffu >> temp_length);
- }
-
- GPR_ASSERT (out == GPR_SLICE_END_PTR (output));
+ GPR_ASSERT(out == GPR_SLICE_END_PTR(output));
return output;
}
-typedef struct
-{
+typedef struct {
gpr_uint32 temp;
gpr_uint32 temp_length;
gpr_uint8 *out;
} huff_out;
-static void
-enc_flush_some (huff_out * out)
-{
- while (out->temp_length > 8)
- {
- out->temp_length -= 8;
- *out->out++ = (gpr_uint8) (out->temp >> out->temp_length);
- }
+static void enc_flush_some(huff_out *out) {
+ while (out->temp_length > 8) {
+ out->temp_length -= 8;
+ *out->out++ = (gpr_uint8)(out->temp >> out->temp_length);
+ }
}
-static void
-enc_add2 (huff_out * out, gpr_uint8 a, gpr_uint8 b)
-{
+static void enc_add2(huff_out *out, gpr_uint8 a, gpr_uint8 b) {
b64_huff_sym sa = huff_alphabet[a];
b64_huff_sym sb = huff_alphabet[b];
- out->temp = (out->temp << (sa.length + sb.length)) | ((gpr_uint32) sa.bits << sb.length) | sb.bits;
- out->temp_length += (gpr_uint32) sa.length + (gpr_uint32) sb.length;
- enc_flush_some (out);
+ out->temp = (out->temp << (sa.length + sb.length)) |
+ ((gpr_uint32)sa.bits << sb.length) | sb.bits;
+ out->temp_length += (gpr_uint32)sa.length + (gpr_uint32)sb.length;
+ enc_flush_some(out);
}
-static void
-enc_add1 (huff_out * out, gpr_uint8 a)
-{
+static void enc_add1(huff_out *out, gpr_uint8 a) {
b64_huff_sym sa = huff_alphabet[a];
out->temp = (out->temp << sa.length) | sa.bits;
out->temp_length += sa.length;
- enc_flush_some (out);
+ enc_flush_some(out);
}
-gpr_slice
-grpc_chttp2_base64_encode_and_huffman_compress (gpr_slice input)
-{
- size_t input_length = GPR_SLICE_LENGTH (input);
+gpr_slice grpc_chttp2_base64_encode_and_huffman_compress(gpr_slice input) {
+ size_t input_length = GPR_SLICE_LENGTH(input);
size_t input_triplets = input_length / 3;
size_t tail_case = input_length % 3;
size_t output_syms = input_triplets * 4 + tail_xtra[tail_case];
size_t max_output_bits = 11 * output_syms;
size_t max_output_length = max_output_bits / 8 + (max_output_bits % 8 != 0);
- gpr_slice output = gpr_slice_malloc (max_output_length);
- gpr_uint8 *in = GPR_SLICE_START_PTR (input);
- gpr_uint8 *start_out = GPR_SLICE_START_PTR (output);
+ gpr_slice output = gpr_slice_malloc(max_output_length);
+ gpr_uint8 *in = GPR_SLICE_START_PTR(input);
+ gpr_uint8 *start_out = GPR_SLICE_START_PTR(output);
huff_out out;
size_t i;
@@ -208,45 +189,42 @@ grpc_chttp2_base64_encode_and_huffman_compress (gpr_slice input)
out.out = start_out;
/* encode full triplets */
- for (i = 0; i < input_triplets; i++)
- {
- enc_add2 (&out, in[0] >> 2, (gpr_uint8) ((in[0] & 0x3) << 4) | (in[1] >> 4));
- enc_add2 (&out, (gpr_uint8) ((in[1] & 0xf) << 2) | (in[2] >> 6), (gpr_uint8) (in[2] & 0x3f));
- in += 3;
- }
+ for (i = 0; i < input_triplets; i++) {
+ enc_add2(&out, in[0] >> 2, (gpr_uint8)((in[0] & 0x3) << 4) | (in[1] >> 4));
+ enc_add2(&out, (gpr_uint8)((in[1] & 0xf) << 2) | (in[2] >> 6),
+ (gpr_uint8)(in[2] & 0x3f));
+ in += 3;
+ }
/* encode the remaining bytes */
- switch (tail_case)
- {
+ switch (tail_case) {
case 0:
break;
case 1:
- enc_add2 (&out, in[0] >> 2, (gpr_uint8) ((in[0] & 0x3) << 4));
+ enc_add2(&out, in[0] >> 2, (gpr_uint8)((in[0] & 0x3) << 4));
in += 1;
break;
case 2:
- enc_add2 (&out, in[0] >> 2, (gpr_uint8) ((in[0] & 0x3) << 4) | (gpr_uint8) (in[1] >> 4));
- enc_add1 (&out, (gpr_uint8) ((in[1] & 0xf) << 2));
+ enc_add2(&out, in[0] >> 2,
+ (gpr_uint8)((in[0] & 0x3) << 4) | (gpr_uint8)(in[1] >> 4));
+ enc_add1(&out, (gpr_uint8)((in[1] & 0xf) << 2));
in += 2;
break;
- }
+ }
- if (out.temp_length)
- {
- *out.out++ = (gpr_uint8) (out.temp << (8u - out.temp_length)) | (gpr_uint8) (0xffu >> out.temp_length);
- }
+ if (out.temp_length) {
+ *out.out++ = (gpr_uint8)(out.temp << (8u - out.temp_length)) |
+ (gpr_uint8)(0xffu >> out.temp_length);
+ }
- GPR_ASSERT (out.out <= GPR_SLICE_END_PTR (output));
- GPR_SLICE_SET_LENGTH (output, out.out - start_out);
+ GPR_ASSERT(out.out <= GPR_SLICE_END_PTR(output));
+ GPR_SLICE_SET_LENGTH(output, out.out - start_out);
- GPR_ASSERT (in == GPR_SLICE_END_PTR (input));
+ GPR_ASSERT(in == GPR_SLICE_END_PTR(input));
return output;
}
-int
-grpc_is_binary_header (const char *key, size_t length)
-{
- if (length < 5)
- return 0;
- return 0 == memcmp (key + length - 4, "-bin", 4);
+int grpc_is_binary_header(const char *key, size_t length) {
+ if (length < 5) return 0;
+ return 0 == memcmp(key + length - 4, "-bin", 4);
}
diff --git a/src/core/transport/chttp2/bin_encoder.h b/src/core/transport/chttp2/bin_encoder.h
index 6e57615f7f..d3e5a855dd 100644
--- a/src/core/transport/chttp2/bin_encoder.h
+++ b/src/core/transport/chttp2/bin_encoder.h
@@ -38,19 +38,19 @@
/* base64 encode a slice. Returns a new slice, does not take ownership of the
input */
-gpr_slice grpc_chttp2_base64_encode (gpr_slice input);
+gpr_slice grpc_chttp2_base64_encode(gpr_slice input);
/* Compress a slice with the static huffman encoder detailed in the hpack
standard. Returns a new slice, does not take ownership of the input */
-gpr_slice grpc_chttp2_huffman_compress (gpr_slice input);
+gpr_slice grpc_chttp2_huffman_compress(gpr_slice input);
/* equivalent to:
gpr_slice x = grpc_chttp2_base64_encode(input);
gpr_slice y = grpc_chttp2_huffman_compress(x);
gpr_slice_unref(x);
return y; */
-gpr_slice grpc_chttp2_base64_encode_and_huffman_compress (gpr_slice input);
+gpr_slice grpc_chttp2_base64_encode_and_huffman_compress(gpr_slice input);
-int grpc_is_binary_header (const char *key, size_t length);
+int grpc_is_binary_header(const char *key, size_t length);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_BIN_ENCODER_H */
diff --git a/src/core/transport/chttp2/frame.h b/src/core/transport/chttp2/frame.h
index 84aa154967..879ee036fa 100644
--- a/src/core/transport/chttp2/frame.h
+++ b/src/core/transport/chttp2/frame.h
@@ -39,8 +39,7 @@
/* Common definitions for frame handling in the chttp2 transport */
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_PARSE_OK,
GRPC_CHTTP2_STREAM_ERROR,
GRPC_CHTTP2_CONNECTION_ERROR
diff --git a/src/core/transport/chttp2/frame_data.c b/src/core/transport/chttp2/frame_data.c
index a806e357a8..99414523d5 100644
--- a/src/core/transport/chttp2/frame_data.c
+++ b/src/core/transport/chttp2/frame_data.c
@@ -42,146 +42,133 @@
#include <grpc/support/useful.h>
#include "src/core/transport/transport.h"
-grpc_chttp2_parse_error
-grpc_chttp2_data_parser_init (grpc_chttp2_data_parser * parser)
-{
+grpc_chttp2_parse_error grpc_chttp2_data_parser_init(
+ grpc_chttp2_data_parser *parser) {
parser->state = GRPC_CHTTP2_DATA_FH_0;
- grpc_sopb_init (&parser->incoming_sopb);
+ grpc_sopb_init(&parser->incoming_sopb);
return GRPC_CHTTP2_PARSE_OK;
}
-void
-grpc_chttp2_data_parser_destroy (grpc_chttp2_data_parser * parser)
-{
- grpc_sopb_destroy (&parser->incoming_sopb);
+void grpc_chttp2_data_parser_destroy(grpc_chttp2_data_parser *parser) {
+ grpc_sopb_destroy(&parser->incoming_sopb);
}
-grpc_chttp2_parse_error
-grpc_chttp2_data_parser_begin_frame (grpc_chttp2_data_parser * parser, gpr_uint8 flags)
-{
- if (flags & ~GRPC_CHTTP2_DATA_FLAG_END_STREAM)
- {
- gpr_log (GPR_ERROR, "unsupported data flags: 0x%02x", flags);
- return GRPC_CHTTP2_STREAM_ERROR;
- }
+grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame(
+ grpc_chttp2_data_parser *parser, gpr_uint8 flags) {
+ if (flags & ~GRPC_CHTTP2_DATA_FLAG_END_STREAM) {
+ gpr_log(GPR_ERROR, "unsupported data flags: 0x%02x", flags);
+ return GRPC_CHTTP2_STREAM_ERROR;
+ }
- if (flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM)
- {
- parser->is_last_frame = 1;
- }
- else
- {
- parser->is_last_frame = 0;
- }
+ if (flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM) {
+ parser->is_last_frame = 1;
+ } else {
+ parser->is_last_frame = 0;
+ }
return GRPC_CHTTP2_PARSE_OK;
}
-grpc_chttp2_parse_error
-grpc_chttp2_data_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
- gpr_uint8 *const beg = GPR_SLICE_START_PTR (slice);
- gpr_uint8 *const end = GPR_SLICE_END_PTR (slice);
+grpc_chttp2_parse_error grpc_chttp2_data_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
+ gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
+ gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
gpr_uint8 *cur = beg;
grpc_chttp2_data_parser *p = parser;
gpr_uint32 message_flags = 0;
- if (is_last && p->is_last_frame)
- {
- stream_parsing->received_close = 1;
- }
+ if (is_last && p->is_last_frame) {
+ stream_parsing->received_close = 1;
+ }
- if (cur == end)
- {
- return GRPC_CHTTP2_PARSE_OK;
- }
+ if (cur == end) {
+ return GRPC_CHTTP2_PARSE_OK;
+ }
- switch (p->state)
- {
- fh_0:
+ switch (p->state) {
+ fh_0:
case GRPC_CHTTP2_DATA_FH_0:
p->frame_type = *cur;
- switch (p->frame_type)
- {
- case 0:
- p->is_frame_compressed = 0; /* GPR_FALSE */
- break;
- case 1:
- p->is_frame_compressed = 1; /* GPR_TRUE */
- break;
- default:
- gpr_log (GPR_ERROR, "Bad GRPC frame type 0x%02x", p->frame_type);
- return GRPC_CHTTP2_STREAM_ERROR;
- }
- if (++cur == end)
- {
- p->state = GRPC_CHTTP2_DATA_FH_1;
- return GRPC_CHTTP2_PARSE_OK;
- }
- /* fallthrough */
+ switch (p->frame_type) {
+ case 0:
+ p->is_frame_compressed = 0; /* GPR_FALSE */
+ break;
+ case 1:
+ p->is_frame_compressed = 1; /* GPR_TRUE */
+ break;
+ default:
+ gpr_log(GPR_ERROR, "Bad GRPC frame type 0x%02x", p->frame_type);
+ return GRPC_CHTTP2_STREAM_ERROR;
+ }
+ if (++cur == end) {
+ p->state = GRPC_CHTTP2_DATA_FH_1;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ /* fallthrough */
case GRPC_CHTTP2_DATA_FH_1:
- p->frame_size = ((gpr_uint32) * cur) << 24;
- if (++cur == end)
- {
- p->state = GRPC_CHTTP2_DATA_FH_2;
- return GRPC_CHTTP2_PARSE_OK;
- }
- /* fallthrough */
+ p->frame_size = ((gpr_uint32)*cur) << 24;
+ if (++cur == end) {
+ p->state = GRPC_CHTTP2_DATA_FH_2;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ /* fallthrough */
case GRPC_CHTTP2_DATA_FH_2:
- p->frame_size |= ((gpr_uint32) * cur) << 16;
- if (++cur == end)
- {
- p->state = GRPC_CHTTP2_DATA_FH_3;
- return GRPC_CHTTP2_PARSE_OK;
- }
- /* fallthrough */
+ p->frame_size |= ((gpr_uint32)*cur) << 16;
+ if (++cur == end) {
+ p->state = GRPC_CHTTP2_DATA_FH_3;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ /* fallthrough */
case GRPC_CHTTP2_DATA_FH_3:
- p->frame_size |= ((gpr_uint32) * cur) << 8;
- if (++cur == end)
- {
- p->state = GRPC_CHTTP2_DATA_FH_4;
- return GRPC_CHTTP2_PARSE_OK;
- }
- /* fallthrough */
+ p->frame_size |= ((gpr_uint32)*cur) << 8;
+ if (++cur == end) {
+ p->state = GRPC_CHTTP2_DATA_FH_4;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ /* fallthrough */
case GRPC_CHTTP2_DATA_FH_4:
- p->frame_size |= ((gpr_uint32) * cur);
+ p->frame_size |= ((gpr_uint32)*cur);
p->state = GRPC_CHTTP2_DATA_FRAME;
++cur;
- if (p->is_frame_compressed)
- {
- message_flags |= GRPC_WRITE_INTERNAL_COMPRESS;
- }
- grpc_sopb_add_begin_message (&p->incoming_sopb, p->frame_size, message_flags);
- /* fallthrough */
+ if (p->is_frame_compressed) {
+ message_flags |= GRPC_WRITE_INTERNAL_COMPRESS;
+ }
+ grpc_sopb_add_begin_message(&p->incoming_sopb, p->frame_size,
+ message_flags);
+ /* fallthrough */
case GRPC_CHTTP2_DATA_FRAME:
- if (cur == end)
- {
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
- return GRPC_CHTTP2_PARSE_OK;
- }
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
- if ((gpr_uint32) (end - cur) == p->frame_size)
- {
- grpc_sopb_add_slice (&p->incoming_sopb, gpr_slice_sub (slice, (size_t) (cur - beg), (size_t) (end - beg)));
- p->state = GRPC_CHTTP2_DATA_FH_0;
- return GRPC_CHTTP2_PARSE_OK;
- }
- else if ((gpr_uint32) (end - cur) > p->frame_size)
- {
- grpc_sopb_add_slice (&p->incoming_sopb, gpr_slice_sub (slice, (size_t) (cur - beg), (size_t) (cur + p->frame_size - beg)));
- cur += p->frame_size;
- goto fh_0; /* loop */
- }
- else
- {
- grpc_sopb_add_slice (&p->incoming_sopb, gpr_slice_sub (slice, (size_t) (cur - beg), (size_t) (end - beg)));
- GPR_ASSERT (end - cur <= p->frame_size);
- p->frame_size -= (gpr_uint32) (end - cur);
- return GRPC_CHTTP2_PARSE_OK;
- }
- }
+ if (cur == end) {
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
+ stream_parsing);
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
+ stream_parsing);
+ if ((gpr_uint32)(end - cur) == p->frame_size) {
+ grpc_sopb_add_slice(
+ &p->incoming_sopb,
+ gpr_slice_sub(slice, (size_t)(cur - beg), (size_t)(end - beg)));
+ p->state = GRPC_CHTTP2_DATA_FH_0;
+ return GRPC_CHTTP2_PARSE_OK;
+ } else if ((gpr_uint32)(end - cur) > p->frame_size) {
+ grpc_sopb_add_slice(&p->incoming_sopb,
+ gpr_slice_sub(slice, (size_t)(cur - beg),
+ (size_t)(cur + p->frame_size - beg)));
+ cur += p->frame_size;
+ goto fh_0; /* loop */
+ } else {
+ grpc_sopb_add_slice(
+ &p->incoming_sopb,
+ gpr_slice_sub(slice, (size_t)(cur - beg), (size_t)(end - beg)));
+ GPR_ASSERT(end - cur <= p->frame_size);
+ p->frame_size -= (gpr_uint32)(end - cur);
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
return GRPC_CHTTP2_CONNECTION_ERROR;
}
diff --git a/src/core/transport/chttp2/frame_data.h b/src/core/transport/chttp2/frame_data.h
index 18c7e00cd1..6762484e5b 100644
--- a/src/core/transport/chttp2/frame_data.h
+++ b/src/core/transport/chttp2/frame_data.h
@@ -42,8 +42,7 @@
#include "src/core/transport/stream_op.h"
#include "src/core/transport/chttp2/frame.h"
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_DATA_FH_0,
GRPC_CHTTP2_DATA_FH_1,
GRPC_CHTTP2_DATA_FH_2,
@@ -52,8 +51,7 @@ typedef enum
GRPC_CHTTP2_DATA_FRAME
} grpc_chttp2_stream_state;
-typedef struct
-{
+typedef struct {
grpc_chttp2_stream_state state;
gpr_uint8 is_last_frame;
gpr_uint8 frame_type;
@@ -64,18 +62,23 @@ typedef struct
} grpc_chttp2_data_parser;
/* initialize per-stream state for data frame parsing */
-grpc_chttp2_parse_error grpc_chttp2_data_parser_init (grpc_chttp2_data_parser * parser);
+grpc_chttp2_parse_error grpc_chttp2_data_parser_init(
+ grpc_chttp2_data_parser *parser);
-void grpc_chttp2_data_parser_destroy (grpc_chttp2_data_parser * parser);
+void grpc_chttp2_data_parser_destroy(grpc_chttp2_data_parser *parser);
/* start processing a new data frame */
-grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame (grpc_chttp2_data_parser * parser, gpr_uint8 flags);
+grpc_chttp2_parse_error grpc_chttp2_data_parser_begin_frame(
+ grpc_chttp2_data_parser *parser, gpr_uint8 flags);
/* handle a slice of a data frame - is_last indicates the last slice of a
frame */
-grpc_chttp2_parse_error grpc_chttp2_data_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+grpc_chttp2_parse_error grpc_chttp2_data_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
/* create a slice with an empty data frame and is_last set */
-gpr_slice grpc_chttp2_data_frame_create_empty_close (gpr_uint32 id);
+gpr_slice grpc_chttp2_data_frame_create_empty_close(gpr_uint32 id);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_DATA_H */
diff --git a/src/core/transport/chttp2/frame_goaway.c b/src/core/transport/chttp2/frame_goaway.c
index 891e77e333..d2065ce79d 100644
--- a/src/core/transport/chttp2/frame_goaway.c
+++ b/src/core/transport/chttp2/frame_goaway.c
@@ -39,151 +39,137 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-void
-grpc_chttp2_goaway_parser_init (grpc_chttp2_goaway_parser * p)
-{
+void grpc_chttp2_goaway_parser_init(grpc_chttp2_goaway_parser *p) {
p->debug_data = NULL;
}
-void
-grpc_chttp2_goaway_parser_destroy (grpc_chttp2_goaway_parser * p)
-{
- gpr_free (p->debug_data);
+void grpc_chttp2_goaway_parser_destroy(grpc_chttp2_goaway_parser *p) {
+ gpr_free(p->debug_data);
}
-grpc_chttp2_parse_error
-grpc_chttp2_goaway_parser_begin_frame (grpc_chttp2_goaway_parser * p, gpr_uint32 length, gpr_uint8 flags)
-{
- if (length < 8)
- {
- gpr_log (GPR_ERROR, "goaway frame too short (%d bytes)", length);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
+grpc_chttp2_parse_error grpc_chttp2_goaway_parser_begin_frame(
+ grpc_chttp2_goaway_parser *p, gpr_uint32 length, gpr_uint8 flags) {
+ if (length < 8) {
+ gpr_log(GPR_ERROR, "goaway frame too short (%d bytes)", length);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
- gpr_free (p->debug_data);
+ gpr_free(p->debug_data);
p->debug_length = length - 8;
- p->debug_data = gpr_malloc (p->debug_length);
+ p->debug_data = gpr_malloc(p->debug_length);
p->debug_pos = 0;
p->state = GRPC_CHTTP2_GOAWAY_LSI0;
return GRPC_CHTTP2_PARSE_OK;
}
-grpc_chttp2_parse_error
-grpc_chttp2_goaway_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
- gpr_uint8 *const beg = GPR_SLICE_START_PTR (slice);
- gpr_uint8 *const end = GPR_SLICE_END_PTR (slice);
+grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
+ gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
+ gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
gpr_uint8 *cur = beg;
grpc_chttp2_goaway_parser *p = parser;
- switch (p->state)
- {
+ switch (p->state) {
case GRPC_CHTTP2_GOAWAY_LSI0:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_LSI0;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->last_stream_id = ((gpr_uint32) * cur) << 24;
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_LSI0;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->last_stream_id = ((gpr_uint32)*cur) << 24;
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_LSI1:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_LSI1;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->last_stream_id |= ((gpr_uint32) * cur) << 16;
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_LSI1;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->last_stream_id |= ((gpr_uint32)*cur) << 16;
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_LSI2:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_LSI2;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->last_stream_id |= ((gpr_uint32) * cur) << 8;
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_LSI2;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->last_stream_id |= ((gpr_uint32)*cur) << 8;
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_LSI3:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_LSI3;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->last_stream_id |= ((gpr_uint32) * cur);
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_LSI3;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->last_stream_id |= ((gpr_uint32)*cur);
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_ERR0:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_ERR0;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->error_code = ((gpr_uint32) * cur) << 24;
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_ERR0;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->error_code = ((gpr_uint32)*cur) << 24;
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_ERR1:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_ERR1;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->error_code |= ((gpr_uint32) * cur) << 16;
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_ERR1;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->error_code |= ((gpr_uint32)*cur) << 16;
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_ERR2:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_ERR2;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->error_code |= ((gpr_uint32) * cur) << 8;
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_ERR2;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->error_code |= ((gpr_uint32)*cur) << 8;
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_ERR3:
- if (cur == end)
- {
- p->state = GRPC_CHTTP2_GOAWAY_ERR3;
- return GRPC_CHTTP2_PARSE_OK;
- }
- p->error_code |= ((gpr_uint32) * cur);
+ if (cur == end) {
+ p->state = GRPC_CHTTP2_GOAWAY_ERR3;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ p->error_code |= ((gpr_uint32)*cur);
++cur;
- /* fallthrough */
+ /* fallthrough */
case GRPC_CHTTP2_GOAWAY_DEBUG:
- memcpy (p->debug_data + p->debug_pos, cur, (size_t) (end - cur));
- GPR_ASSERT (end - cur < GPR_UINT32_MAX - p->debug_pos);
- p->debug_pos += (gpr_uint32) (end - cur);
+ memcpy(p->debug_data + p->debug_pos, cur, (size_t)(end - cur));
+ GPR_ASSERT(end - cur < GPR_UINT32_MAX - p->debug_pos);
+ p->debug_pos += (gpr_uint32)(end - cur);
p->state = GRPC_CHTTP2_GOAWAY_DEBUG;
- if (is_last)
- {
- transport_parsing->goaway_received = 1;
- transport_parsing->goaway_last_stream_index = p->last_stream_id;
- gpr_slice_unref (transport_parsing->goaway_text);
- transport_parsing->goaway_error = (grpc_status_code) p->error_code;
- transport_parsing->goaway_text = gpr_slice_new (p->debug_data, p->debug_length, gpr_free);
- p->debug_data = NULL;
- }
+ if (is_last) {
+ transport_parsing->goaway_received = 1;
+ transport_parsing->goaway_last_stream_index = p->last_stream_id;
+ gpr_slice_unref(transport_parsing->goaway_text);
+ transport_parsing->goaway_error = (grpc_status_code)p->error_code;
+ transport_parsing->goaway_text =
+ gpr_slice_new(p->debug_data, p->debug_length, gpr_free);
+ p->debug_data = NULL;
+ }
return GRPC_CHTTP2_PARSE_OK;
- }
- gpr_log (GPR_ERROR, "Should never end up here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "Should never end up here");
+ abort();
return GRPC_CHTTP2_CONNECTION_ERROR;
}
-void
-grpc_chttp2_goaway_append (gpr_uint32 last_stream_id, gpr_uint32 error_code, gpr_slice debug_data, gpr_slice_buffer * slice_buffer)
-{
- gpr_slice header = gpr_slice_malloc (9 + 4 + 4);
- gpr_uint8 *p = GPR_SLICE_START_PTR (header);
+void grpc_chttp2_goaway_append(gpr_uint32 last_stream_id, gpr_uint32 error_code,
+ gpr_slice debug_data,
+ gpr_slice_buffer *slice_buffer) {
+ gpr_slice header = gpr_slice_malloc(9 + 4 + 4);
+ gpr_uint8 *p = GPR_SLICE_START_PTR(header);
gpr_uint32 frame_length;
- GPR_ASSERT (GPR_SLICE_LENGTH (debug_data) < GPR_UINT32_MAX - 4 - 4);
- frame_length = 4 + 4 + (gpr_uint32) GPR_SLICE_LENGTH (debug_data);
+ GPR_ASSERT(GPR_SLICE_LENGTH(debug_data) < GPR_UINT32_MAX - 4 - 4);
+ frame_length = 4 + 4 + (gpr_uint32)GPR_SLICE_LENGTH(debug_data);
/* frame header: length */
- *p++ = (gpr_uint8) (frame_length >> 16);
- *p++ = (gpr_uint8) (frame_length >> 8);
- *p++ = (gpr_uint8) (frame_length);
+ *p++ = (gpr_uint8)(frame_length >> 16);
+ *p++ = (gpr_uint8)(frame_length >> 8);
+ *p++ = (gpr_uint8)(frame_length);
/* frame header: type */
*p++ = GRPC_CHTTP2_FRAME_GOAWAY;
/* frame header: flags */
@@ -194,16 +180,16 @@ grpc_chttp2_goaway_append (gpr_uint32 last_stream_id, gpr_uint32 error_code, gpr
*p++ = 0;
*p++ = 0;
/* payload: last stream id */
- *p++ = (gpr_uint8) (last_stream_id >> 24);
- *p++ = (gpr_uint8) (last_stream_id >> 16);
- *p++ = (gpr_uint8) (last_stream_id >> 8);
- *p++ = (gpr_uint8) (last_stream_id);
+ *p++ = (gpr_uint8)(last_stream_id >> 24);
+ *p++ = (gpr_uint8)(last_stream_id >> 16);
+ *p++ = (gpr_uint8)(last_stream_id >> 8);
+ *p++ = (gpr_uint8)(last_stream_id);
/* payload: error code */
- *p++ = (gpr_uint8) (error_code >> 24);
- *p++ = (gpr_uint8) (error_code >> 16);
- *p++ = (gpr_uint8) (error_code >> 8);
- *p++ = (gpr_uint8) (error_code);
- GPR_ASSERT (p == GPR_SLICE_END_PTR (header));
- gpr_slice_buffer_add (slice_buffer, header);
- gpr_slice_buffer_add (slice_buffer, debug_data);
+ *p++ = (gpr_uint8)(error_code >> 24);
+ *p++ = (gpr_uint8)(error_code >> 16);
+ *p++ = (gpr_uint8)(error_code >> 8);
+ *p++ = (gpr_uint8)(error_code);
+ GPR_ASSERT(p == GPR_SLICE_END_PTR(header));
+ gpr_slice_buffer_add(slice_buffer, header);
+ gpr_slice_buffer_add(slice_buffer, debug_data);
}
diff --git a/src/core/transport/chttp2/frame_goaway.h b/src/core/transport/chttp2/frame_goaway.h
index f1eada9881..06aaa92f07 100644
--- a/src/core/transport/chttp2/frame_goaway.h
+++ b/src/core/transport/chttp2/frame_goaway.h
@@ -40,8 +40,7 @@
#include <grpc/support/slice.h>
#include <grpc/support/slice_buffer.h>
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_GOAWAY_LSI0,
GRPC_CHTTP2_GOAWAY_LSI1,
GRPC_CHTTP2_GOAWAY_LSI2,
@@ -53,8 +52,7 @@ typedef enum
GRPC_CHTTP2_GOAWAY_DEBUG
} grpc_chttp2_goaway_parse_state;
-typedef struct
-{
+typedef struct {
grpc_chttp2_goaway_parse_state state;
gpr_uint32 last_stream_id;
gpr_uint32 error_code;
@@ -63,11 +61,17 @@ typedef struct
gpr_uint32 debug_pos;
} grpc_chttp2_goaway_parser;
-void grpc_chttp2_goaway_parser_init (grpc_chttp2_goaway_parser * p);
-void grpc_chttp2_goaway_parser_destroy (grpc_chttp2_goaway_parser * p);
-grpc_chttp2_parse_error grpc_chttp2_goaway_parser_begin_frame (grpc_chttp2_goaway_parser * parser, gpr_uint32 length, gpr_uint8 flags);
-grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+void grpc_chttp2_goaway_parser_init(grpc_chttp2_goaway_parser *p);
+void grpc_chttp2_goaway_parser_destroy(grpc_chttp2_goaway_parser *p);
+grpc_chttp2_parse_error grpc_chttp2_goaway_parser_begin_frame(
+ grpc_chttp2_goaway_parser *parser, gpr_uint32 length, gpr_uint8 flags);
+grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
-void grpc_chttp2_goaway_append (gpr_uint32 last_stream_id, gpr_uint32 error_code, gpr_slice debug_data, gpr_slice_buffer * slice_buffer);
+void grpc_chttp2_goaway_append(gpr_uint32 last_stream_id, gpr_uint32 error_code,
+ gpr_slice debug_data,
+ gpr_slice_buffer *slice_buffer);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_GOAWAY_H */
diff --git a/src/core/transport/chttp2/frame_ping.c b/src/core/transport/chttp2/frame_ping.c
index eaa086bd2b..4d2c54269d 100644
--- a/src/core/transport/chttp2/frame_ping.c
+++ b/src/core/transport/chttp2/frame_ping.c
@@ -39,11 +39,9 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-gpr_slice
-grpc_chttp2_ping_create (gpr_uint8 ack, gpr_uint8 * opaque_8bytes)
-{
- gpr_slice slice = gpr_slice_malloc (9 + 8);
- gpr_uint8 *p = GPR_SLICE_START_PTR (slice);
+gpr_slice grpc_chttp2_ping_create(gpr_uint8 ack, gpr_uint8 *opaque_8bytes) {
+ gpr_slice slice = gpr_slice_malloc(9 + 8);
+ gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
*p++ = 0;
*p++ = 0;
@@ -54,61 +52,55 @@ grpc_chttp2_ping_create (gpr_uint8 ack, gpr_uint8 * opaque_8bytes)
*p++ = 0;
*p++ = 0;
*p++ = 0;
- memcpy (p, opaque_8bytes, 8);
+ memcpy(p, opaque_8bytes, 8);
return slice;
}
-grpc_chttp2_parse_error
-grpc_chttp2_ping_parser_begin_frame (grpc_chttp2_ping_parser * parser, gpr_uint32 length, gpr_uint8 flags)
-{
- if (flags & 0xfe || length != 8)
- {
- gpr_log (GPR_ERROR, "invalid ping: length=%d, flags=%02x", length, flags);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
+grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame(
+ grpc_chttp2_ping_parser *parser, gpr_uint32 length, gpr_uint8 flags) {
+ if (flags & 0xfe || length != 8) {
+ gpr_log(GPR_ERROR, "invalid ping: length=%d, flags=%02x", length, flags);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
parser->byte = 0;
parser->is_ack = flags;
return GRPC_CHTTP2_PARSE_OK;
}
-grpc_chttp2_parse_error
-grpc_chttp2_ping_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
- gpr_uint8 *const beg = GPR_SLICE_START_PTR (slice);
- gpr_uint8 *const end = GPR_SLICE_END_PTR (slice);
+grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
+ gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
+ gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
gpr_uint8 *cur = beg;
grpc_chttp2_ping_parser *p = parser;
grpc_chttp2_outstanding_ping *ping;
- while (p->byte != 8 && cur != end)
- {
- p->opaque_8bytes[p->byte] = *cur;
- cur++;
- p->byte++;
- }
+ while (p->byte != 8 && cur != end) {
+ p->opaque_8bytes[p->byte] = *cur;
+ cur++;
+ p->byte++;
+ }
- if (p->byte == 8)
- {
- GPR_ASSERT (is_last);
- if (p->is_ack)
- {
- for (ping = transport_parsing->pings.next; ping != &transport_parsing->pings; ping = ping->next)
- {
- if (0 == memcmp (p->opaque_8bytes, ping->id, 8))
- {
- grpc_exec_ctx_enqueue (exec_ctx, ping->on_recv, 1);
- }
- ping->next->prev = ping->prev;
- ping->prev->next = ping->next;
- gpr_free (ping);
- }
- }
- else
- {
- gpr_slice_buffer_add (&transport_parsing->qbuf, grpc_chttp2_ping_create (1, p->opaque_8bytes));
- }
+ if (p->byte == 8) {
+ GPR_ASSERT(is_last);
+ if (p->is_ack) {
+ for (ping = transport_parsing->pings.next;
+ ping != &transport_parsing->pings; ping = ping->next) {
+ if (0 == memcmp(p->opaque_8bytes, ping->id, 8)) {
+ grpc_exec_ctx_enqueue(exec_ctx, ping->on_recv, 1);
+ }
+ ping->next->prev = ping->prev;
+ ping->prev->next = ping->next;
+ gpr_free(ping);
+ }
+ } else {
+ gpr_slice_buffer_add(&transport_parsing->qbuf,
+ grpc_chttp2_ping_create(1, p->opaque_8bytes));
}
+ }
return GRPC_CHTTP2_PARSE_OK;
}
diff --git a/src/core/transport/chttp2/frame_ping.h b/src/core/transport/chttp2/frame_ping.h
index 066573940a..2c71d0d491 100644
--- a/src/core/transport/chttp2/frame_ping.h
+++ b/src/core/transport/chttp2/frame_ping.h
@@ -38,16 +38,19 @@
#include <grpc/support/slice.h>
#include "src/core/transport/chttp2/frame.h"
-typedef struct
-{
+typedef struct {
gpr_uint8 byte;
gpr_uint8 is_ack;
gpr_uint8 opaque_8bytes[8];
} grpc_chttp2_ping_parser;
-gpr_slice grpc_chttp2_ping_create (gpr_uint8 ack, gpr_uint8 * opaque_8bytes);
+gpr_slice grpc_chttp2_ping_create(gpr_uint8 ack, gpr_uint8 *opaque_8bytes);
-grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame (grpc_chttp2_ping_parser * parser, gpr_uint32 length, gpr_uint8 flags);
-grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+grpc_chttp2_parse_error grpc_chttp2_ping_parser_begin_frame(
+ grpc_chttp2_ping_parser *parser, gpr_uint32 length, gpr_uint8 flags);
+grpc_chttp2_parse_error grpc_chttp2_ping_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_PING_H */
diff --git a/src/core/transport/chttp2/frame_rst_stream.c b/src/core/transport/chttp2/frame_rst_stream.c
index 545d1b42b9..3cd5bcfc39 100644
--- a/src/core/transport/chttp2/frame_rst_stream.c
+++ b/src/core/transport/chttp2/frame_rst_stream.c
@@ -38,63 +38,63 @@
#include "src/core/transport/chttp2/frame.h"
-gpr_slice
-grpc_chttp2_rst_stream_create (gpr_uint32 id, gpr_uint32 code)
-{
- gpr_slice slice = gpr_slice_malloc (13);
- gpr_uint8 *p = GPR_SLICE_START_PTR (slice);
+gpr_slice grpc_chttp2_rst_stream_create(gpr_uint32 id, gpr_uint32 code) {
+ gpr_slice slice = gpr_slice_malloc(13);
+ gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
*p++ = 0;
*p++ = 0;
*p++ = 4;
*p++ = GRPC_CHTTP2_FRAME_RST_STREAM;
*p++ = 0;
- *p++ = (gpr_uint8) (id >> 24);
- *p++ = (gpr_uint8) (id >> 16);
- *p++ = (gpr_uint8) (id >> 8);
- *p++ = (gpr_uint8) (id);
- *p++ = (gpr_uint8) (code >> 24);
- *p++ = (gpr_uint8) (code >> 16);
- *p++ = (gpr_uint8) (code >> 8);
- *p++ = (gpr_uint8) (code);
+ *p++ = (gpr_uint8)(id >> 24);
+ *p++ = (gpr_uint8)(id >> 16);
+ *p++ = (gpr_uint8)(id >> 8);
+ *p++ = (gpr_uint8)(id);
+ *p++ = (gpr_uint8)(code >> 24);
+ *p++ = (gpr_uint8)(code >> 16);
+ *p++ = (gpr_uint8)(code >> 8);
+ *p++ = (gpr_uint8)(code);
return slice;
}
-grpc_chttp2_parse_error
-grpc_chttp2_rst_stream_parser_begin_frame (grpc_chttp2_rst_stream_parser * parser, gpr_uint32 length, gpr_uint8 flags)
-{
- if (length != 4)
- {
- gpr_log (GPR_ERROR, "invalid rst_stream: length=%d, flags=%02x", length, flags);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
+grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame(
+ grpc_chttp2_rst_stream_parser *parser, gpr_uint32 length, gpr_uint8 flags) {
+ if (length != 4) {
+ gpr_log(GPR_ERROR, "invalid rst_stream: length=%d, flags=%02x", length,
+ flags);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
parser->byte = 0;
return GRPC_CHTTP2_PARSE_OK;
}
-grpc_chttp2_parse_error
-grpc_chttp2_rst_stream_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
- gpr_uint8 *const beg = GPR_SLICE_START_PTR (slice);
- gpr_uint8 *const end = GPR_SLICE_END_PTR (slice);
+grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
+ gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
+ gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
gpr_uint8 *cur = beg;
grpc_chttp2_rst_stream_parser *p = parser;
- while (p->byte != 4 && cur != end)
- {
- p->reason_bytes[p->byte] = *cur;
- cur++;
- p->byte++;
- }
+ while (p->byte != 4 && cur != end) {
+ p->reason_bytes[p->byte] = *cur;
+ cur++;
+ p->byte++;
+ }
- if (p->byte == 4)
- {
- GPR_ASSERT (is_last);
- stream_parsing->received_close = 1;
- stream_parsing->saw_rst_stream = 1;
- stream_parsing->rst_stream_reason = (((gpr_uint32) p->reason_bytes[0]) << 24) | (((gpr_uint32) p->reason_bytes[1]) << 16) | (((gpr_uint32) p->reason_bytes[2]) << 8) | (((gpr_uint32) p->reason_bytes[3]));
- }
+ if (p->byte == 4) {
+ GPR_ASSERT(is_last);
+ stream_parsing->received_close = 1;
+ stream_parsing->saw_rst_stream = 1;
+ stream_parsing->rst_stream_reason =
+ (((gpr_uint32)p->reason_bytes[0]) << 24) |
+ (((gpr_uint32)p->reason_bytes[1]) << 16) |
+ (((gpr_uint32)p->reason_bytes[2]) << 8) |
+ (((gpr_uint32)p->reason_bytes[3]));
+ }
return GRPC_CHTTP2_PARSE_OK;
}
diff --git a/src/core/transport/chttp2/frame_rst_stream.h b/src/core/transport/chttp2/frame_rst_stream.h
index b222e70861..92cb77c971 100644
--- a/src/core/transport/chttp2/frame_rst_stream.h
+++ b/src/core/transport/chttp2/frame_rst_stream.h
@@ -38,15 +38,18 @@
#include "src/core/transport/chttp2/frame.h"
#include "src/core/iomgr/exec_ctx.h"
-typedef struct
-{
+typedef struct {
gpr_uint8 byte;
gpr_uint8 reason_bytes[4];
} grpc_chttp2_rst_stream_parser;
-gpr_slice grpc_chttp2_rst_stream_create (gpr_uint32 stream_id, gpr_uint32 code);
+gpr_slice grpc_chttp2_rst_stream_create(gpr_uint32 stream_id, gpr_uint32 code);
-grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame (grpc_chttp2_rst_stream_parser * parser, gpr_uint32 length, gpr_uint8 flags);
-grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_begin_frame(
+ grpc_chttp2_rst_stream_parser *parser, gpr_uint32 length, gpr_uint8 flags);
+grpc_chttp2_parse_error grpc_chttp2_rst_stream_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_RST_STREAM_H */
diff --git a/src/core/transport/chttp2/frame_settings.c b/src/core/transport/chttp2/frame_settings.c
index 4b6204e6ea..395a2da452 100644
--- a/src/core/transport/chttp2/frame_settings.c
+++ b/src/core/transport/chttp2/frame_settings.c
@@ -43,27 +43,27 @@
#include <grpc/support/useful.h>
/* HTTP/2 mandated initial connection settings */
-const grpc_chttp2_setting_parameters grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS] = {
- {NULL, 0, 0, 0, GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
- {"HEADER_TABLE_SIZE", 4096, 0, 0xffffffff,
- GRPC_CHTTP2_CLAMP_INVALID_VALUE},
- {"ENABLE_PUSH", 1, 0, 1, GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
- {"MAX_CONCURRENT_STREAMS", 0xffffffffu, 0, 0xffffffffu,
- GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
- {"INITIAL_WINDOW_SIZE", 65535, 0, 0xffffffffu,
- GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
- {"MAX_FRAME_SIZE", 16384, 16384, 16777215,
- GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
- {"MAX_HEADER_LIST_SIZE", 0xffffffffu, 0, 0xffffffffu,
- GRPC_CHTTP2_CLAMP_INVALID_VALUE},
+const grpc_chttp2_setting_parameters
+ grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS] = {
+ {NULL, 0, 0, 0, GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
+ {"HEADER_TABLE_SIZE", 4096, 0, 0xffffffff,
+ GRPC_CHTTP2_CLAMP_INVALID_VALUE},
+ {"ENABLE_PUSH", 1, 0, 1, GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
+ {"MAX_CONCURRENT_STREAMS", 0xffffffffu, 0, 0xffffffffu,
+ GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
+ {"INITIAL_WINDOW_SIZE", 65535, 0, 0xffffffffu,
+ GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
+ {"MAX_FRAME_SIZE", 16384, 16384, 16777215,
+ GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE},
+ {"MAX_HEADER_LIST_SIZE", 0xffffffffu, 0, 0xffffffffu,
+ GRPC_CHTTP2_CLAMP_INVALID_VALUE},
};
-static gpr_uint8 *
-fill_header (gpr_uint8 * out, gpr_uint32 length, gpr_uint8 flags)
-{
- *out++ = (gpr_uint8) (length >> 16);
- *out++ = (gpr_uint8) (length >> 8);
- *out++ = (gpr_uint8) (length);
+static gpr_uint8 *fill_header(gpr_uint8 *out, gpr_uint32 length,
+ gpr_uint8 flags) {
+ *out++ = (gpr_uint8)(length >> 16);
+ *out++ = (gpr_uint8)(length >> 8);
+ *out++ = (gpr_uint8)(length);
*out++ = GRPC_CHTTP2_FRAME_SETTINGS;
*out++ = flags;
*out++ = 0;
@@ -73,194 +73,175 @@ fill_header (gpr_uint8 * out, gpr_uint32 length, gpr_uint8 flags)
return out;
}
-gpr_slice
-grpc_chttp2_settings_create (gpr_uint32 * old, const gpr_uint32 * new, gpr_uint32 force_mask, size_t count)
-{
+gpr_slice grpc_chttp2_settings_create(gpr_uint32 *old, const gpr_uint32 *new,
+ gpr_uint32 force_mask, size_t count) {
size_t i;
gpr_uint32 n = 0;
gpr_slice output;
gpr_uint8 *p;
- for (i = 0; i < count; i++)
- {
- n += (new[i] != old[i] || (force_mask & (1u << i)) != 0);
+ for (i = 0; i < count; i++) {
+ n += (new[i] != old[i] || (force_mask & (1u << i)) != 0);
+ }
+
+ output = gpr_slice_malloc(9 + 6 * n);
+ p = fill_header(GPR_SLICE_START_PTR(output), 6 * n, 0);
+
+ for (i = 0; i < count; i++) {
+ if (new[i] != old[i] || (force_mask & (1u << i)) != 0) {
+ GPR_ASSERT(i);
+ *p++ = (gpr_uint8)(i >> 8);
+ *p++ = (gpr_uint8)(i);
+ *p++ = (gpr_uint8)(new[i] >> 24);
+ *p++ = (gpr_uint8)(new[i] >> 16);
+ *p++ = (gpr_uint8)(new[i] >> 8);
+ *p++ = (gpr_uint8)(new[i]);
+ old[i] = new[i];
}
+ }
- output = gpr_slice_malloc (9 + 6 * n);
- p = fill_header (GPR_SLICE_START_PTR (output), 6 * n, 0);
-
- for (i = 0; i < count; i++)
- {
- if (new[i] != old[i] || (force_mask & (1u << i)) != 0)
- {
- GPR_ASSERT (i);
- *p++ = (gpr_uint8) (i >> 8);
- *p++ = (gpr_uint8) (i);
- *p++ = (gpr_uint8) (new[i] >> 24);
- *p++ = (gpr_uint8) (new[i] >> 16);
- *p++ = (gpr_uint8) (new[i] >> 8);
- *p++ = (gpr_uint8) (new[i]);
- old[i] = new[i];
- }
- }
-
- GPR_ASSERT (p == GPR_SLICE_END_PTR (output));
+ GPR_ASSERT(p == GPR_SLICE_END_PTR(output));
return output;
}
-gpr_slice
-grpc_chttp2_settings_ack_create (void)
-{
- gpr_slice output = gpr_slice_malloc (9);
- fill_header (GPR_SLICE_START_PTR (output), 0, GRPC_CHTTP2_FLAG_ACK);
+gpr_slice grpc_chttp2_settings_ack_create(void) {
+ gpr_slice output = gpr_slice_malloc(9);
+ fill_header(GPR_SLICE_START_PTR(output), 0, GRPC_CHTTP2_FLAG_ACK);
return output;
}
-grpc_chttp2_parse_error
-grpc_chttp2_settings_parser_begin_frame (grpc_chttp2_settings_parser * parser, gpr_uint32 length, gpr_uint8 flags, gpr_uint32 * settings)
-{
+grpc_chttp2_parse_error grpc_chttp2_settings_parser_begin_frame(
+ grpc_chttp2_settings_parser *parser, gpr_uint32 length, gpr_uint8 flags,
+ gpr_uint32 *settings) {
parser->target_settings = settings;
- memcpy (parser->incoming_settings, settings, GRPC_CHTTP2_NUM_SETTINGS * sizeof (gpr_uint32));
+ memcpy(parser->incoming_settings, settings,
+ GRPC_CHTTP2_NUM_SETTINGS * sizeof(gpr_uint32));
parser->is_ack = 0;
parser->state = GRPC_CHTTP2_SPS_ID0;
- if (flags == GRPC_CHTTP2_FLAG_ACK)
- {
- parser->is_ack = 1;
- if (length != 0)
- {
- gpr_log (GPR_ERROR, "non-empty settings ack frame received");
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
- return GRPC_CHTTP2_PARSE_OK;
- }
- else if (flags != 0)
- {
- gpr_log (GPR_ERROR, "invalid flags on settings frame");
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
- else if (length % 6 != 0)
- {
- gpr_log (GPR_ERROR, "settings frames must be a multiple of six bytes");
+ if (flags == GRPC_CHTTP2_FLAG_ACK) {
+ parser->is_ack = 1;
+ if (length != 0) {
+ gpr_log(GPR_ERROR, "non-empty settings ack frame received");
return GRPC_CHTTP2_CONNECTION_ERROR;
}
- else
- {
- return GRPC_CHTTP2_PARSE_OK;
- }
+ return GRPC_CHTTP2_PARSE_OK;
+ } else if (flags != 0) {
+ gpr_log(GPR_ERROR, "invalid flags on settings frame");
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ } else if (length % 6 != 0) {
+ gpr_log(GPR_ERROR, "settings frames must be a multiple of six bytes");
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ } else {
+ return GRPC_CHTTP2_PARSE_OK;
+ }
}
-grpc_chttp2_parse_error
-grpc_chttp2_settings_parser_parse (grpc_exec_ctx * exec_ctx, void *p, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
+grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *p,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
grpc_chttp2_settings_parser *parser = p;
- const gpr_uint8 *cur = GPR_SLICE_START_PTR (slice);
- const gpr_uint8 *end = GPR_SLICE_END_PTR (slice);
-
- if (parser->is_ack)
- {
- return GRPC_CHTTP2_PARSE_OK;
- }
-
- for (;;)
- {
- switch (parser->state)
- {
- case GRPC_CHTTP2_SPS_ID0:
- if (cur == end)
- {
- parser->state = GRPC_CHTTP2_SPS_ID0;
- if (is_last)
- {
- transport_parsing->settings_updated = 1;
- memcpy (parser->target_settings, parser->incoming_settings, GRPC_CHTTP2_NUM_SETTINGS * sizeof (gpr_uint32));
- gpr_slice_buffer_add (&transport_parsing->qbuf, grpc_chttp2_settings_ack_create ());
- }
- return GRPC_CHTTP2_PARSE_OK;
- }
- parser->id = (gpr_uint16) (((gpr_uint16) * cur) << 8);
- cur++;
- /* fallthrough */
- case GRPC_CHTTP2_SPS_ID1:
- if (cur == end)
- {
- parser->state = GRPC_CHTTP2_SPS_ID1;
- return GRPC_CHTTP2_PARSE_OK;
- }
- parser->id = (gpr_uint16) (parser->id | (*cur));
- cur++;
- /* fallthrough */
- case GRPC_CHTTP2_SPS_VAL0:
- if (cur == end)
- {
- parser->state = GRPC_CHTTP2_SPS_VAL0;
- return GRPC_CHTTP2_PARSE_OK;
- }
- parser->value = ((gpr_uint32) * cur) << 24;
- cur++;
- /* fallthrough */
- case GRPC_CHTTP2_SPS_VAL1:
- if (cur == end)
- {
- parser->state = GRPC_CHTTP2_SPS_VAL1;
- return GRPC_CHTTP2_PARSE_OK;
- }
- parser->value |= ((gpr_uint32) * cur) << 16;
- cur++;
- /* fallthrough */
- case GRPC_CHTTP2_SPS_VAL2:
- if (cur == end)
- {
- parser->state = GRPC_CHTTP2_SPS_VAL2;
- return GRPC_CHTTP2_PARSE_OK;
- }
- parser->value |= ((gpr_uint32) * cur) << 8;
- cur++;
- /* fallthrough */
- case GRPC_CHTTP2_SPS_VAL3:
- if (cur == end)
- {
- parser->state = GRPC_CHTTP2_SPS_VAL3;
- return GRPC_CHTTP2_PARSE_OK;
- }
- else
- {
- parser->state = GRPC_CHTTP2_SPS_ID0;
- }
- parser->value |= *cur;
- cur++;
-
- if (parser->id > 0 && parser->id < GRPC_CHTTP2_NUM_SETTINGS)
- {
- const grpc_chttp2_setting_parameters *sp = &grpc_chttp2_settings_parameters[parser->id];
- if (parser->value < sp->min_value || parser->value > sp->max_value)
- {
- switch (sp->invalid_value_behavior)
- {
- case GRPC_CHTTP2_CLAMP_INVALID_VALUE:
- parser->value = GPR_CLAMP (parser->value, sp->min_value, sp->max_value);
- break;
- case GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE:
- gpr_log (GPR_ERROR, "invalid value %u passed for %s", parser->value, sp->name);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
- }
- if (parser->id == GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE && parser->incoming_settings[parser->id] != parser->value)
- {
- transport_parsing->initial_window_update = (gpr_int64) parser->value - parser->incoming_settings[parser->id];
- gpr_log (GPR_DEBUG, "adding %d for initial_window change", (int) transport_parsing->initial_window_update);
- }
- parser->incoming_settings[parser->id] = parser->value;
- if (grpc_http_trace)
- {
- gpr_log (GPR_DEBUG, "CHTTP2:%s: got setting %d = %d", transport_parsing->is_client ? "CLI" : "SVR", parser->id, parser->value);
- }
- }
- else
- {
- gpr_log (GPR_ERROR, "CHTTP2: Ignoring unknown setting %d (value %d)", parser->id, parser->value);
- }
- break;
- }
+ const gpr_uint8 *cur = GPR_SLICE_START_PTR(slice);
+ const gpr_uint8 *end = GPR_SLICE_END_PTR(slice);
+
+ if (parser->is_ack) {
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+
+ for (;;) {
+ switch (parser->state) {
+ case GRPC_CHTTP2_SPS_ID0:
+ if (cur == end) {
+ parser->state = GRPC_CHTTP2_SPS_ID0;
+ if (is_last) {
+ transport_parsing->settings_updated = 1;
+ memcpy(parser->target_settings, parser->incoming_settings,
+ GRPC_CHTTP2_NUM_SETTINGS * sizeof(gpr_uint32));
+ gpr_slice_buffer_add(&transport_parsing->qbuf,
+ grpc_chttp2_settings_ack_create());
+ }
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ parser->id = (gpr_uint16)(((gpr_uint16)*cur) << 8);
+ cur++;
+ /* fallthrough */
+ case GRPC_CHTTP2_SPS_ID1:
+ if (cur == end) {
+ parser->state = GRPC_CHTTP2_SPS_ID1;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ parser->id = (gpr_uint16)(parser->id | (*cur));
+ cur++;
+ /* fallthrough */
+ case GRPC_CHTTP2_SPS_VAL0:
+ if (cur == end) {
+ parser->state = GRPC_CHTTP2_SPS_VAL0;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ parser->value = ((gpr_uint32)*cur) << 24;
+ cur++;
+ /* fallthrough */
+ case GRPC_CHTTP2_SPS_VAL1:
+ if (cur == end) {
+ parser->state = GRPC_CHTTP2_SPS_VAL1;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ parser->value |= ((gpr_uint32)*cur) << 16;
+ cur++;
+ /* fallthrough */
+ case GRPC_CHTTP2_SPS_VAL2:
+ if (cur == end) {
+ parser->state = GRPC_CHTTP2_SPS_VAL2;
+ return GRPC_CHTTP2_PARSE_OK;
+ }
+ parser->value |= ((gpr_uint32)*cur) << 8;
+ cur++;
+ /* fallthrough */
+ case GRPC_CHTTP2_SPS_VAL3:
+ if (cur == end) {
+ parser->state = GRPC_CHTTP2_SPS_VAL3;
+ return GRPC_CHTTP2_PARSE_OK;
+ } else {
+ parser->state = GRPC_CHTTP2_SPS_ID0;
+ }
+ parser->value |= *cur;
+ cur++;
+
+ if (parser->id > 0 && parser->id < GRPC_CHTTP2_NUM_SETTINGS) {
+ const grpc_chttp2_setting_parameters *sp =
+ &grpc_chttp2_settings_parameters[parser->id];
+ if (parser->value < sp->min_value || parser->value > sp->max_value) {
+ switch (sp->invalid_value_behavior) {
+ case GRPC_CHTTP2_CLAMP_INVALID_VALUE:
+ parser->value =
+ GPR_CLAMP(parser->value, sp->min_value, sp->max_value);
+ break;
+ case GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE:
+ gpr_log(GPR_ERROR, "invalid value %u passed for %s",
+ parser->value, sp->name);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
+ }
+ if (parser->id == GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE &&
+ parser->incoming_settings[parser->id] != parser->value) {
+ transport_parsing->initial_window_update =
+ (gpr_int64)parser->value -
+ parser->incoming_settings[parser->id];
+ gpr_log(GPR_DEBUG, "adding %d for initial_window change",
+ (int)transport_parsing->initial_window_update);
+ }
+ parser->incoming_settings[parser->id] = parser->value;
+ if (grpc_http_trace) {
+ gpr_log(GPR_DEBUG, "CHTTP2:%s: got setting %d = %d",
+ transport_parsing->is_client ? "CLI" : "SVR", parser->id,
+ parser->value);
+ }
+ } else {
+ gpr_log(GPR_ERROR, "CHTTP2: Ignoring unknown setting %d (value %d)",
+ parser->id, parser->value);
+ }
+ break;
}
+ }
}
diff --git a/src/core/transport/chttp2/frame_settings.h b/src/core/transport/chttp2/frame_settings.h
index 3c57e3ae89..cf857dd602 100644
--- a/src/core/transport/chttp2/frame_settings.h
+++ b/src/core/transport/chttp2/frame_settings.h
@@ -39,8 +39,7 @@
#include "src/core/transport/chttp2/frame.h"
#include "src/core/iomgr/exec_ctx.h"
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_SPS_ID0,
GRPC_CHTTP2_SPS_ID1,
GRPC_CHTTP2_SPS_VAL0,
@@ -50,8 +49,7 @@ typedef enum
} grpc_chttp2_settings_parse_state;
/* The things HTTP/2 defines as connection level settings */
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_SETTINGS_HEADER_TABLE_SIZE = 1,
GRPC_CHTTP2_SETTINGS_ENABLE_PUSH = 2,
GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS = 3,
@@ -61,8 +59,7 @@ typedef enum
GRPC_CHTTP2_NUM_SETTINGS
} grpc_chttp2_setting_id;
-typedef struct
-{
+typedef struct {
grpc_chttp2_settings_parse_state state;
gpr_uint32 *target_settings;
gpr_uint8 is_ack;
@@ -71,14 +68,12 @@ typedef struct
gpr_uint32 incoming_settings[GRPC_CHTTP2_NUM_SETTINGS];
} grpc_chttp2_settings_parser;
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_CLAMP_INVALID_VALUE,
GRPC_CHTTP2_DISCONNECT_ON_INVALID_VALUE
} grpc_chttp2_invalid_value_behavior;
-typedef struct
-{
+typedef struct {
const char *name;
gpr_uint32 default_value;
gpr_uint32 min_value;
@@ -87,14 +82,21 @@ typedef struct
} grpc_chttp2_setting_parameters;
/* HTTP/2 mandated connection setting parameters */
-extern const grpc_chttp2_setting_parameters grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS];
+extern const grpc_chttp2_setting_parameters
+ grpc_chttp2_settings_parameters[GRPC_CHTTP2_NUM_SETTINGS];
/* Create a settings frame by diffing old & new, and updating old to be new */
-gpr_slice grpc_chttp2_settings_create (gpr_uint32 * old, const gpr_uint32 * new, gpr_uint32 force_mask, size_t count);
+gpr_slice grpc_chttp2_settings_create(gpr_uint32 *old, const gpr_uint32 *new,
+ gpr_uint32 force_mask, size_t count);
/* Create an ack settings frame */
-gpr_slice grpc_chttp2_settings_ack_create (void);
+gpr_slice grpc_chttp2_settings_ack_create(void);
-grpc_chttp2_parse_error grpc_chttp2_settings_parser_begin_frame (grpc_chttp2_settings_parser * parser, gpr_uint32 length, gpr_uint8 flags, gpr_uint32 * settings);
-grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+grpc_chttp2_parse_error grpc_chttp2_settings_parser_begin_frame(
+ grpc_chttp2_settings_parser *parser, gpr_uint32 length, gpr_uint8 flags,
+ gpr_uint32 *settings);
+grpc_chttp2_parse_error grpc_chttp2_settings_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_SETTINGS_H */
diff --git a/src/core/transport/chttp2/frame_window_update.c b/src/core/transport/chttp2/frame_window_update.c
index 81f2ce2b52..91bbcfe2c1 100644
--- a/src/core/transport/chttp2/frame_window_update.c
+++ b/src/core/transport/chttp2/frame_window_update.c
@@ -36,83 +36,80 @@
#include <grpc/support/log.h>
-gpr_slice
-grpc_chttp2_window_update_create (gpr_uint32 id, gpr_uint32 window_update)
-{
- gpr_slice slice = gpr_slice_malloc (13);
- gpr_uint8 *p = GPR_SLICE_START_PTR (slice);
+gpr_slice grpc_chttp2_window_update_create(gpr_uint32 id,
+ gpr_uint32 window_update) {
+ gpr_slice slice = gpr_slice_malloc(13);
+ gpr_uint8 *p = GPR_SLICE_START_PTR(slice);
- GPR_ASSERT (window_update);
+ GPR_ASSERT(window_update);
*p++ = 0;
*p++ = 0;
*p++ = 4;
*p++ = GRPC_CHTTP2_FRAME_WINDOW_UPDATE;
*p++ = 0;
- *p++ = (gpr_uint8) (id >> 24);
- *p++ = (gpr_uint8) (id >> 16);
- *p++ = (gpr_uint8) (id >> 8);
- *p++ = (gpr_uint8) (id);
- *p++ = (gpr_uint8) (window_update >> 24);
- *p++ = (gpr_uint8) (window_update >> 16);
- *p++ = (gpr_uint8) (window_update >> 8);
- *p++ = (gpr_uint8) (window_update);
+ *p++ = (gpr_uint8)(id >> 24);
+ *p++ = (gpr_uint8)(id >> 16);
+ *p++ = (gpr_uint8)(id >> 8);
+ *p++ = (gpr_uint8)(id);
+ *p++ = (gpr_uint8)(window_update >> 24);
+ *p++ = (gpr_uint8)(window_update >> 16);
+ *p++ = (gpr_uint8)(window_update >> 8);
+ *p++ = (gpr_uint8)(window_update);
return slice;
}
-grpc_chttp2_parse_error
-grpc_chttp2_window_update_parser_begin_frame (grpc_chttp2_window_update_parser * parser, gpr_uint32 length, gpr_uint8 flags)
-{
- if (flags || length != 4)
- {
- gpr_log (GPR_ERROR, "invalid window update: length=%d, flags=%02x", length, flags);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
+grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame(
+ grpc_chttp2_window_update_parser *parser, gpr_uint32 length,
+ gpr_uint8 flags) {
+ if (flags || length != 4) {
+ gpr_log(GPR_ERROR, "invalid window update: length=%d, flags=%02x", length,
+ flags);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
parser->byte = 0;
parser->amount = 0;
return GRPC_CHTTP2_PARSE_OK;
}
-grpc_chttp2_parse_error
-grpc_chttp2_window_update_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
- gpr_uint8 *const beg = GPR_SLICE_START_PTR (slice);
- gpr_uint8 *const end = GPR_SLICE_END_PTR (slice);
+grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
+ gpr_uint8 *const beg = GPR_SLICE_START_PTR(slice);
+ gpr_uint8 *const end = GPR_SLICE_END_PTR(slice);
gpr_uint8 *cur = beg;
grpc_chttp2_window_update_parser *p = parser;
- while (p->byte != 4 && cur != end)
- {
- p->amount |= ((gpr_uint32) * cur) << (8 * (3 - p->byte));
- cur++;
- p->byte++;
- }
+ while (p->byte != 4 && cur != end) {
+ p->amount |= ((gpr_uint32)*cur) << (8 * (3 - p->byte));
+ cur++;
+ p->byte++;
+ }
- if (p->byte == 4)
- {
- if (p->amount == 0 || (p->amount & 0x80000000u))
- {
- gpr_log (GPR_ERROR, "invalid window update bytes: %d", p->amount);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
- GPR_ASSERT (is_last);
+ if (p->byte == 4) {
+ if (p->amount == 0 || (p->amount & 0x80000000u)) {
+ gpr_log(GPR_ERROR, "invalid window update bytes: %d", p->amount);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
+ GPR_ASSERT(is_last);
- if (transport_parsing->incoming_stream_id != 0)
- {
- if (stream_parsing != NULL)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("update", transport_parsing, stream_parsing, outgoing_window_update, p->amount);
- stream_parsing->outgoing_window_update += p->amount;
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
- }
- }
- else
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("update", transport_parsing, outgoing_window_update, p->amount);
- transport_parsing->outgoing_window_update += p->amount;
- }
+ if (transport_parsing->incoming_stream_id != 0) {
+ if (stream_parsing != NULL) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("update", transport_parsing,
+ stream_parsing, outgoing_window_update,
+ p->amount);
+ stream_parsing->outgoing_window_update += p->amount;
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
+ stream_parsing);
+ }
+ } else {
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("update", transport_parsing,
+ outgoing_window_update, p->amount);
+ transport_parsing->outgoing_window_update += p->amount;
}
+ }
return GRPC_CHTTP2_PARSE_OK;
}
diff --git a/src/core/transport/chttp2/frame_window_update.h b/src/core/transport/chttp2/frame_window_update.h
index b6b61e8987..fc074092ff 100644
--- a/src/core/transport/chttp2/frame_window_update.h
+++ b/src/core/transport/chttp2/frame_window_update.h
@@ -38,16 +38,21 @@
#include <grpc/support/slice.h>
#include "src/core/transport/chttp2/frame.h"
-typedef struct
-{
+typedef struct {
gpr_uint8 byte;
gpr_uint8 is_connection_update;
gpr_uint32 amount;
} grpc_chttp2_window_update_parser;
-gpr_slice grpc_chttp2_window_update_create (gpr_uint32 id, gpr_uint32 window_delta);
+gpr_slice grpc_chttp2_window_update_create(gpr_uint32 id,
+ gpr_uint32 window_delta);
-grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame (grpc_chttp2_window_update_parser * parser, gpr_uint32 length, gpr_uint8 flags);
-grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+grpc_chttp2_parse_error grpc_chttp2_window_update_parser_begin_frame(
+ grpc_chttp2_window_update_parser *parser, gpr_uint32 length,
+ gpr_uint8 flags);
+grpc_chttp2_parse_error grpc_chttp2_window_update_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_FRAME_WINDOW_UPDATE_H */
diff --git a/src/core/transport/chttp2/hpack_parser.c b/src/core/transport/chttp2/hpack_parser.c
index 0d19a577cc..3b0a5e7207 100644
--- a/src/core/transport/chttp2/hpack_parser.c
+++ b/src/core/transport/chttp2/hpack_parser.c
@@ -45,8 +45,7 @@
#include <grpc/support/port_platform.h>
#include <grpc/support/useful.h>
-typedef enum
-{
+typedef enum {
NOT_BINARY,
B64_BYTE0,
B64_BYTE1,
@@ -69,34 +68,61 @@ typedef enum
a set of indirect jumps, and so not waste stack space. */
/* forward declarations for parsing states */
-static int parse_begin (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_error (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-
-static int parse_string_prefix (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_key_string (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value_string_with_indexed_key (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value_string_with_literal_key (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-
-static int parse_value0 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value1 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value2 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value3 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value4 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_value5up (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-
-static int parse_indexed_field (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_indexed_field_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_incidx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_incidx_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_incidx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_notidx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_notidx_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_notidx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_nvridx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_nvridx_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_lithdr_nvridx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_max_tbl_size (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
-static int parse_max_tbl_size_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end);
+static int parse_begin(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_error(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+
+static int parse_string_prefix(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_key_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value_string_with_indexed_key(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value_string_with_literal_key(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+
+static int parse_value0(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value1(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value2(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value3(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value4(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_value5up(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+
+static int parse_indexed_field(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_indexed_field_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_incidx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_incidx_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_incidx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_notidx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_notidx_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_notidx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_nvridx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_nvridx_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_lithdr_nvridx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
+static int parse_max_tbl_size(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end);
+static int parse_max_tbl_size_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end);
/* we translate the first byte of a hpack field into one of these decoding
cases, then use a lookup table to jump directly to the appropriate parser.
@@ -104,8 +130,7 @@ static int parse_max_tbl_size_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 *
_X => the integer index is all ones, meaning we need to do varint decoding
_V => the integer index is all zeros, meaning we need to decode an additional
string value */
-typedef enum
-{
+typedef enum {
INDEXED_FIELD,
INDEXED_FIELD_X,
LITHDR_INCIDX,
@@ -125,82 +150,81 @@ typedef enum
/* jump table of parse state functions -- order must match first_byte_type
above */
static const grpc_chttp2_hpack_parser_state first_byte_action[] = {
- parse_indexed_field, parse_indexed_field_x,
- parse_lithdr_incidx, parse_lithdr_incidx_x,
- parse_lithdr_incidx_v, parse_lithdr_notidx,
- parse_lithdr_notidx_x, parse_lithdr_notidx_v,
- parse_lithdr_nvridx, parse_lithdr_nvridx_x,
- parse_lithdr_nvridx_v, parse_max_tbl_size,
- parse_max_tbl_size_x, parse_error
-};
+ parse_indexed_field, parse_indexed_field_x,
+ parse_lithdr_incidx, parse_lithdr_incidx_x,
+ parse_lithdr_incidx_v, parse_lithdr_notidx,
+ parse_lithdr_notidx_x, parse_lithdr_notidx_v,
+ parse_lithdr_nvridx, parse_lithdr_nvridx_x,
+ parse_lithdr_nvridx_v, parse_max_tbl_size,
+ parse_max_tbl_size_x, parse_error};
/* indexes the first byte to a parse state function - generated by
gen_hpack_tables.c */
static const gpr_uint8 first_byte_lut[256] = {
- LITHDR_NOTIDX_V, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX,
- LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX,
- LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX,
- LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX_X,
- LITHDR_NVRIDX_V, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX,
- LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX,
- LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX,
- LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX_X,
- ILLEGAL, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
- MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE_X,
- LITHDR_INCIDX_V, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
- LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX_X,
- ILLEGAL, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
- INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD_X,
+ LITHDR_NOTIDX_V, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX,
+ LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX,
+ LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX,
+ LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX, LITHDR_NOTIDX_X,
+ LITHDR_NVRIDX_V, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX,
+ LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX,
+ LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX,
+ LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX, LITHDR_NVRIDX_X,
+ ILLEGAL, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE,
+ MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE, MAX_TBL_SIZE_X,
+ LITHDR_INCIDX_V, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX,
+ LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX, LITHDR_INCIDX_X,
+ ILLEGAL, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD,
+ INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD, INDEXED_FIELD_X,
};
/* state table for huffman decoding: given a state, gives an index/16 into
@@ -209,76 +233,76 @@ static const gpr_uint8 first_byte_lut[256] = {
generated by gen_hpack_tables.c */
static const gpr_uint8 next_tbl[256] = {
- 0, 1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 3, 3, 9, 10, 11, 1, 1,
- 1, 12, 1, 2, 13, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
- 14, 1, 15, 16, 1, 17, 1, 15, 2, 7, 3, 18, 19, 1, 1, 1, 1, 20, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 7, 21, 1, 22, 1, 1, 1, 1, 1,
- 1, 1, 1, 15, 2, 2, 2, 2, 2, 2, 23, 24, 25, 1, 1, 1, 1, 2, 2, 2,
- 26, 3, 3, 27, 10, 28, 1, 1, 1, 1, 1, 1, 2, 3, 29, 10, 30, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 31, 1, 1, 1, 1, 1, 1, 1, 2,
- 2, 2, 2, 2, 2, 2, 2, 32, 1, 1, 15, 33, 1, 34, 35, 9, 36, 1, 1, 1,
- 1, 1, 1, 1, 37, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 26, 9,
- 38, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 2, 2, 26, 3, 3, 39, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 7, 3, 3, 3, 40, 2,
- 41, 1, 1, 1, 42, 43, 1, 1, 44, 1, 1, 1, 1, 15, 2, 2, 2, 2, 2, 2,
- 3, 3, 3, 45, 46, 1, 1, 2, 2, 2, 35, 3, 3, 18, 47, 2,
+ 0, 1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 3, 3, 9, 10, 11, 1, 1,
+ 1, 12, 1, 2, 13, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
+ 14, 1, 15, 16, 1, 17, 1, 15, 2, 7, 3, 18, 19, 1, 1, 1, 1, 20, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 7, 21, 1, 22, 1, 1, 1, 1, 1,
+ 1, 1, 1, 15, 2, 2, 2, 2, 2, 2, 23, 24, 25, 1, 1, 1, 1, 2, 2, 2,
+ 26, 3, 3, 27, 10, 28, 1, 1, 1, 1, 1, 1, 2, 3, 29, 10, 30, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 31, 1, 1, 1, 1, 1, 1, 1, 2,
+ 2, 2, 2, 2, 2, 2, 2, 32, 1, 1, 15, 33, 1, 34, 35, 9, 36, 1, 1, 1,
+ 1, 1, 1, 1, 37, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 26, 9,
+ 38, 1, 1, 1, 1, 1, 1, 1, 15, 2, 2, 2, 2, 26, 3, 3, 39, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 7, 3, 3, 3, 40, 2,
+ 41, 1, 1, 1, 42, 43, 1, 1, 44, 1, 1, 1, 1, 15, 2, 2, 2, 2, 2, 2,
+ 3, 3, 3, 45, 46, 1, 1, 2, 2, 2, 35, 3, 3, 18, 47, 2,
};
/* next state, based upon current state and the current nibble: see above.
generated by gen_hpack_tables.c */
static const gpr_int16 next_sub_tbl[48 * 16] = {
- 1, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
- 218, 2, 6, 10, 13, 14, 15, 16, 17, 2, 6, 10, 13, 14, 15,
- 16, 17, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, 11, 24, 3,
- 7, 11, 24, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8,
- 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
- 199, 200, 201, 202, 203, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 9, 133, 134, 135, 136, 137, 138, 139, 140,
- 141, 142, 143, 144, 145, 146, 147, 3, 7, 11, 24, 3, 7, 11, 24,
- 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 12, 132, 4, 8, 4, 8, 4, 8,
- 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 18, 19, 20, 21, 4, 8, 4,
- 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 22, 23, 91, 25, 26,
- 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 3,
- 7, 11, 24, 3, 7, 11, 24, 0, 0, 0, 0, 0, 41, 42, 43,
- 2, 6, 10, 13, 14, 15, 16, 17, 3, 7, 11, 24, 3, 7, 11,
- 24, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0,
- 44, 45, 2, 6, 10, 13, 14, 15, 16, 17, 46, 47, 48, 49, 50,
- 51, 52, 57, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
- 68, 69, 70, 71, 72, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 73, 75, 76, 77, 78, 79, 80, 81, 82,
- 83, 84, 85, 86, 87, 88, 89, 90, 3, 7, 11, 24, 3, 7, 11,
- 24, 3, 7, 11, 24, 0, 0, 0, 0, 3, 7, 11, 24, 3, 7,
- 11, 24, 4, 8, 4, 8, 0, 0, 0, 92, 0, 0, 0, 93, 94,
- 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 3, 7, 11, 24,
- 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4,
- 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 4,
- 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0,
- 0, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
- 131, 2, 6, 10, 13, 14, 15, 16, 17, 4, 8, 4, 8, 4, 8,
- 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 148,
- 149, 150, 151, 3, 7, 11, 24, 4, 8, 4, 8, 0, 0, 0, 0,
- 0, 0, 152, 153, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, 11,
- 24, 154, 155, 156, 164, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7,
- 11, 24, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 157, 158, 159, 160, 161, 162, 163, 165, 166, 167, 168, 169, 170, 171, 172,
- 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
- 188, 189, 190, 191, 192, 193, 194, 195, 196, 4, 8, 4, 8, 4, 8,
- 4, 8, 4, 8, 4, 8, 4, 8, 197, 198, 4, 8, 4, 8, 4,
- 8, 4, 8, 0, 0, 0, 0, 0, 0, 219, 220, 3, 7, 11, 24,
- 4, 8, 4, 8, 4, 8, 0, 0, 221, 222, 223, 224, 3, 7, 11,
- 24, 3, 7, 11, 24, 4, 8, 4, 8, 4, 8, 225, 228, 4, 8,
- 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 226, 227, 229,
- 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244,
- 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 245, 246, 247, 248, 249, 250, 251, 252,
- 253, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 255,
+ 1, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
+ 218, 2, 6, 10, 13, 14, 15, 16, 17, 2, 6, 10, 13, 14, 15,
+ 16, 17, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, 11, 24, 3,
+ 7, 11, 24, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8,
+ 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
+ 199, 200, 201, 202, 203, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 9, 133, 134, 135, 136, 137, 138, 139, 140,
+ 141, 142, 143, 144, 145, 146, 147, 3, 7, 11, 24, 3, 7, 11, 24,
+ 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 12, 132, 4, 8, 4, 8, 4, 8,
+ 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 18, 19, 20, 21, 4, 8, 4,
+ 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 22, 23, 91, 25, 26,
+ 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 3,
+ 7, 11, 24, 3, 7, 11, 24, 0, 0, 0, 0, 0, 41, 42, 43,
+ 2, 6, 10, 13, 14, 15, 16, 17, 3, 7, 11, 24, 3, 7, 11,
+ 24, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0,
+ 44, 45, 2, 6, 10, 13, 14, 15, 16, 17, 46, 47, 48, 49, 50,
+ 51, 52, 57, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
+ 68, 69, 70, 71, 72, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 73, 75, 76, 77, 78, 79, 80, 81, 82,
+ 83, 84, 85, 86, 87, 88, 89, 90, 3, 7, 11, 24, 3, 7, 11,
+ 24, 3, 7, 11, 24, 0, 0, 0, 0, 3, 7, 11, 24, 3, 7,
+ 11, 24, 4, 8, 4, 8, 0, 0, 0, 92, 0, 0, 0, 93, 94,
+ 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 3, 7, 11, 24,
+ 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4,
+ 8, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 4,
+ 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0,
+ 0, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
+ 131, 2, 6, 10, 13, 14, 15, 16, 17, 4, 8, 4, 8, 4, 8,
+ 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 148,
+ 149, 150, 151, 3, 7, 11, 24, 4, 8, 4, 8, 0, 0, 0, 0,
+ 0, 0, 152, 153, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7, 11,
+ 24, 154, 155, 156, 164, 3, 7, 11, 24, 3, 7, 11, 24, 3, 7,
+ 11, 24, 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 157, 158, 159, 160, 161, 162, 163, 165, 166, 167, 168, 169, 170, 171, 172,
+ 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
+ 188, 189, 190, 191, 192, 193, 194, 195, 196, 4, 8, 4, 8, 4, 8,
+ 4, 8, 4, 8, 4, 8, 4, 8, 197, 198, 4, 8, 4, 8, 4,
+ 8, 4, 8, 0, 0, 0, 0, 0, 0, 219, 220, 3, 7, 11, 24,
+ 4, 8, 4, 8, 4, 8, 0, 0, 221, 222, 223, 224, 3, 7, 11,
+ 24, 3, 7, 11, 24, 4, 8, 4, 8, 4, 8, 225, 228, 4, 8,
+ 4, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 226, 227, 229,
+ 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244,
+ 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 245, 246, 247, 248, 249, 250, 251, 252,
+ 253, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 255,
};
/* emission table: indexed like next_tbl, ultimately gives the byte to be
@@ -286,1140 +310,1044 @@ static const gpr_int16 next_sub_tbl[48 * 16] = {
generated by gen_hpack_tables.c */
static const gpr_uint16 emit_tbl[256] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 0, 8, 9, 10, 11, 12, 13,
- 14, 15, 16, 17, 18, 19, 20, 21, 22, 0, 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, 0, 55, 56,
- 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 0,
- 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,
- 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
- 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
- 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
- 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 0,
- 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174,
- 0, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
- 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
- 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218,
- 219, 220, 221, 0, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
- 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
- 248,
+ 0, 1, 2, 3, 4, 5, 6, 7, 0, 8, 9, 10, 11, 12, 13,
+ 14, 15, 16, 17, 18, 19, 20, 21, 22, 0, 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, 0, 55, 56,
+ 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 0,
+ 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,
+ 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
+ 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
+ 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
+ 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 0,
+ 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174,
+ 0, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
+ 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
+ 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218,
+ 219, 220, 221, 0, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
+ 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
+ 248,
};
/* generated by gen_hpack_tables.c */
static const gpr_int16 emit_sub_tbl[249 * 16] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49,
- 49, 49, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 97,
- 97, 97, 97, 48, 48, 49, 49, 50, 50, 97, 97, 99, 99, 101, 101,
- 105, 105, 111, 111, 48, 49, 50, 97, 99, 101, 105, 111, 115, 116, -1,
- -1, -1, -1, -1, -1, 32, 32, 32, 32, 32, 32, 32, 32, 37, 37,
- 37, 37, 37, 37, 37, 37, 99, 99, 99, 99, 101, 101, 101, 101, 105,
- 105, 105, 105, 111, 111, 111, 111, 115, 115, 116, 116, 32, 37, 45, 46,
- 47, 51, 52, 53, 54, 55, 56, 57, 61, 61, 61, 61, 61, 61, 61,
- 61, 65, 65, 65, 65, 65, 65, 65, 65, 115, 115, 115, 115, 116, 116,
- 116, 116, 32, 32, 37, 37, 45, 45, 46, 46, 61, 65, 95, 98, 100,
- 102, 103, 104, 108, 109, 110, 112, 114, 117, -1, -1, 58, 58, 58, 58,
- 58, 58, 58, 58, 66, 66, 66, 66, 66, 66, 66, 66, 47, 47, 51,
- 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 61, 61,
- 65, 65, 95, 95, 98, 98, 100, 100, 102, 102, 103, 103, 104, 104, 108,
- 108, 109, 109, 110, 110, 112, 112, 114, 114, 117, 117, 58, 66, 67, 68,
- 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
- 84, 85, 86, 87, 89, 106, 107, 113, 118, 119, 120, 121, 122, -1, -1,
- -1, -1, 38, 38, 38, 38, 38, 38, 38, 38, 42, 42, 42, 42, 42,
- 42, 42, 42, 44, 44, 44, 44, 44, 44, 44, 44, 59, 59, 59, 59,
- 59, 59, 59, 59, 88, 88, 88, 88, 88, 88, 88, 88, 90, 90, 90,
- 90, 90, 90, 90, 90, 33, 33, 34, 34, 40, 40, 41, 41, 63, 63,
- 39, 43, 124, -1, -1, -1, 35, 35, 35, 35, 35, 35, 35, 35, 62,
- 62, 62, 62, 62, 62, 62, 62, 0, 0, 0, 0, 36, 36, 36, 36,
- 64, 64, 64, 64, 91, 91, 91, 91, 69, 69, 69, 69, 69, 69, 69,
- 69, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71,
- 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 73, 73, 73, 73, 73,
- 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75,
- 75, 75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77,
- 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 78, 78, 79, 79,
- 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 81,
- 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82,
- 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 84,
- 84, 85, 85, 85, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 86,
- 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 89, 89, 89, 89, 89,
- 89, 89, 89, 106, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
- 107, 107, 107, 107, 113, 113, 113, 113, 113, 113, 113, 113, 118, 118, 118,
- 118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 119, 119, 120, 120,
- 120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 122,
- 122, 122, 122, 122, 122, 122, 122, 38, 38, 38, 38, 42, 42, 42, 42,
- 44, 44, 44, 44, 59, 59, 59, 59, 88, 88, 88, 88, 90, 90, 90,
- 90, 33, 34, 40, 41, 63, -1, -1, -1, 39, 39, 39, 39, 39, 39,
- 39, 39, 43, 43, 43, 43, 43, 43, 43, 43, 124, 124, 124, 124, 124,
- 124, 124, 124, 35, 35, 35, 35, 62, 62, 62, 62, 0, 0, 36, 36,
- 64, 64, 91, 91, 93, 93, 126, 126, 94, 125, -1, -1, 60, 60, 60,
- 60, 60, 60, 60, 60, 96, 96, 96, 96, 96, 96, 96, 96, 123, 123,
- 123, 123, 123, 123, 123, 123, -1, -1, -1, -1, -1, -1, -1, -1, 92,
- 92, 92, 92, 92, 92, 92, 92, 195, 195, 195, 195, 195, 195, 195, 195,
- 208, 208, 208, 208, 208, 208, 208, 208, 128, 128, 128, 128, 130, 130, 130,
- 130, 131, 131, 131, 131, 162, 162, 162, 162, 184, 184, 184, 184, 194, 194,
- 194, 194, 224, 224, 224, 224, 226, 226, 226, 226, 153, 153, 161, 161, 167,
- 167, 172, 172, 176, 176, 177, 177, 179, 179, 209, 209, 216, 216, 217, 217,
- 227, 227, 229, 229, 230, 230, 129, 132, 133, 134, 136, 146, 154, 156, 160,
- 163, 164, 169, 170, 173, 178, 181, 185, 186, 187, 189, 190, 196, 198, 228,
- 232, 233, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 135,
- 135, 135, 135, 135, 135, 135, 135, 137, 137, 137, 137, 137, 137, 137, 137,
- 138, 138, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139, 139, 139, 139,
- 139, 140, 140, 140, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 141,
- 141, 141, 143, 143, 143, 143, 143, 143, 143, 143, 147, 147, 147, 147, 147,
- 147, 147, 147, 149, 149, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150,
- 150, 150, 150, 150, 151, 151, 151, 151, 151, 151, 151, 151, 152, 152, 152,
- 152, 152, 152, 152, 152, 155, 155, 155, 155, 155, 155, 155, 155, 157, 157,
- 157, 157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 158, 158, 165,
- 165, 165, 165, 165, 165, 165, 165, 166, 166, 166, 166, 166, 166, 166, 166,
- 168, 168, 168, 168, 168, 168, 168, 168, 174, 174, 174, 174, 174, 174, 174,
- 174, 175, 175, 175, 175, 175, 175, 175, 175, 180, 180, 180, 180, 180, 180,
- 180, 180, 182, 182, 182, 182, 182, 182, 182, 182, 183, 183, 183, 183, 183,
- 183, 183, 183, 188, 188, 188, 188, 188, 188, 188, 188, 191, 191, 191, 191,
- 191, 191, 191, 191, 197, 197, 197, 197, 197, 197, 197, 197, 231, 231, 231,
- 231, 231, 231, 231, 231, 239, 239, 239, 239, 239, 239, 239, 239, 9, 9,
- 9, 9, 142, 142, 142, 142, 144, 144, 144, 144, 145, 145, 145, 145, 148,
- 148, 148, 148, 159, 159, 159, 159, 171, 171, 171, 171, 206, 206, 206, 206,
- 215, 215, 215, 215, 225, 225, 225, 225, 236, 236, 236, 236, 237, 237, 237,
- 237, 199, 199, 207, 207, 234, 234, 235, 235, 192, 193, 200, 201, 202, 205,
- 210, 213, 218, 219, 238, 240, 242, 243, 255, -1, 203, 203, 203, 203, 203,
- 203, 203, 203, 204, 204, 204, 204, 204, 204, 204, 204, 211, 211, 211, 211,
- 211, 211, 211, 211, 212, 212, 212, 212, 212, 212, 212, 212, 214, 214, 214,
- 214, 214, 214, 214, 214, 221, 221, 221, 221, 221, 221, 221, 221, 222, 222,
- 222, 222, 222, 222, 222, 222, 223, 223, 223, 223, 223, 223, 223, 223, 241,
- 241, 241, 241, 241, 241, 241, 241, 244, 244, 244, 244, 244, 244, 244, 244,
- 245, 245, 245, 245, 245, 245, 245, 245, 246, 246, 246, 246, 246, 246, 246,
- 246, 247, 247, 247, 247, 247, 247, 247, 247, 248, 248, 248, 248, 248, 248,
- 248, 248, 250, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251,
- 251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, 253, 253,
- 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 2, 2, 2,
- 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
- 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 11, 11, 11, 11, 12,
- 12, 12, 12, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
- 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20,
- 20, 21, 21, 21, 21, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25,
- 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29,
- 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 127, 127, 127, 127,
- 220, 220, 220, 220, 249, 249, 249, 249, 10, 13, 22, 256, 93, 93, 93,
- 93, 126, 126, 126, 126, 94, 94, 125, 125, 60, 96, 123, -1, 92, 195,
- 208, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 128,
- 128, 128, 128, 128, 128, 128, 128, 130, 130, 130, 130, 130, 130, 130, 130,
- 131, 131, 131, 131, 131, 131, 131, 131, 162, 162, 162, 162, 162, 162, 162,
- 162, 184, 184, 184, 184, 184, 184, 184, 184, 194, 194, 194, 194, 194, 194,
- 194, 194, 224, 224, 224, 224, 224, 224, 224, 224, 226, 226, 226, 226, 226,
- 226, 226, 226, 153, 153, 153, 153, 161, 161, 161, 161, 167, 167, 167, 167,
- 172, 172, 172, 172, 176, 176, 176, 176, 177, 177, 177, 177, 179, 179, 179,
- 179, 209, 209, 209, 209, 216, 216, 216, 216, 217, 217, 217, 217, 227, 227,
- 227, 227, 229, 229, 229, 229, 230, 230, 230, 230, 129, 129, 132, 132, 133,
- 133, 134, 134, 136, 136, 146, 146, 154, 154, 156, 156, 160, 160, 163, 163,
- 164, 164, 169, 169, 170, 170, 173, 173, 178, 178, 181, 181, 185, 185, 186,
- 186, 187, 187, 189, 189, 190, 190, 196, 196, 198, 198, 228, 228, 232, 232,
- 233, 233, 1, 135, 137, 138, 139, 140, 141, 143, 147, 149, 150, 151, 152,
- 155, 157, 158, 165, 166, 168, 174, 175, 180, 182, 183, 188, 191, 197, 231,
- 239, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 9, 9, 9,
- 9, 9, 9, 9, 9, 142, 142, 142, 142, 142, 142, 142, 142, 144, 144,
- 144, 144, 144, 144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 145, 148,
- 148, 148, 148, 148, 148, 148, 148, 159, 159, 159, 159, 159, 159, 159, 159,
- 171, 171, 171, 171, 171, 171, 171, 171, 206, 206, 206, 206, 206, 206, 206,
- 206, 215, 215, 215, 215, 215, 215, 215, 215, 225, 225, 225, 225, 225, 225,
- 225, 225, 236, 236, 236, 236, 236, 236, 236, 236, 237, 237, 237, 237, 237,
- 237, 237, 237, 199, 199, 199, 199, 207, 207, 207, 207, 234, 234, 234, 234,
- 235, 235, 235, 235, 192, 192, 193, 193, 200, 200, 201, 201, 202, 202, 205,
- 205, 210, 210, 213, 213, 218, 218, 219, 219, 238, 238, 240, 240, 242, 242,
- 243, 243, 255, 255, 203, 204, 211, 212, 214, 221, 222, 223, 241, 244, 245,
- 246, 247, 248, 250, 251, 252, 253, 254, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2,
- 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
- 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
- 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
- 8, 8, 8, 8, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12,
- 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15,
- 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17,
- 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18,
- 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
- 20, 21, 21, 21, 21, 21, 21, 21, 21, 23, 23, 23, 23, 23, 23,
- 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
- 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27,
- 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29,
- 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31,
- 31, 31, 31, 31, 31, 31, 127, 127, 127, 127, 127, 127, 127, 127, 220,
- 220, 220, 220, 220, 220, 220, 220, 249, 249, 249, 249, 249, 249, 249, 249,
- 10, 10, 13, 13, 22, 22, 256, 256, 67, 67, 67, 67, 67, 67, 67,
- 67, 68, 68, 68, 68, 68, 68, 68, 68, 95, 95, 95, 95, 95, 95,
- 95, 95, 98, 98, 98, 98, 98, 98, 98, 98, 100, 100, 100, 100, 100,
- 100, 100, 100, 102, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103,
- 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 108, 108, 108,
- 108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109, 109, 109, 110, 110,
- 110, 110, 110, 110, 110, 110, 112, 112, 112, 112, 112, 112, 112, 112, 114,
- 114, 114, 114, 114, 114, 114, 114, 117, 117, 117, 117, 117, 117, 117, 117,
- 58, 58, 58, 58, 66, 66, 66, 66, 67, 67, 67, 67, 68, 68, 68,
- 68, 69, 69, 69, 69, 70, 70, 70, 70, 71, 71, 71, 71, 72, 72,
- 72, 72, 73, 73, 73, 73, 74, 74, 74, 74, 75, 75, 75, 75, 76,
- 76, 76, 76, 77, 77, 77, 77, 78, 78, 78, 78, 79, 79, 79, 79,
- 80, 80, 80, 80, 81, 81, 81, 81, 82, 82, 82, 82, 83, 83, 83,
- 83, 84, 84, 84, 84, 85, 85, 85, 85, 86, 86, 86, 86, 87, 87,
- 87, 87, 89, 89, 89, 89, 106, 106, 106, 106, 107, 107, 107, 107, 113,
- 113, 113, 113, 118, 118, 118, 118, 119, 119, 119, 119, 120, 120, 120, 120,
- 121, 121, 121, 121, 122, 122, 122, 122, 38, 38, 42, 42, 44, 44, 59,
- 59, 88, 88, 90, 90, -1, -1, -1, -1, 33, 33, 33, 33, 33, 33,
- 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 40, 40, 40, 40, 40,
- 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 63, 63, 63, 63,
- 63, 63, 63, 63, 39, 39, 39, 39, 43, 43, 43, 43, 124, 124, 124,
- 124, 35, 35, 62, 62, 0, 36, 64, 91, 93, 126, -1, -1, 94, 94,
- 94, 94, 94, 94, 94, 94, 125, 125, 125, 125, 125, 125, 125, 125, 60,
- 60, 60, 60, 96, 96, 96, 96, 123, 123, 123, 123, -1, -1, -1, -1,
- 92, 92, 92, 92, 195, 195, 195, 195, 208, 208, 208, 208, 128, 128, 130,
- 130, 131, 131, 162, 162, 184, 184, 194, 194, 224, 224, 226, 226, 153, 161,
- 167, 172, 176, 177, 179, 209, 216, 217, 227, 229, 230, -1, -1, -1, -1,
- -1, -1, -1, 129, 129, 129, 129, 129, 129, 129, 129, 132, 132, 132, 132,
- 132, 132, 132, 132, 133, 133, 133, 133, 133, 133, 133, 133, 134, 134, 134,
- 134, 134, 134, 134, 134, 136, 136, 136, 136, 136, 136, 136, 136, 146, 146,
- 146, 146, 146, 146, 146, 146, 154, 154, 154, 154, 154, 154, 154, 154, 156,
- 156, 156, 156, 156, 156, 156, 156, 160, 160, 160, 160, 160, 160, 160, 160,
- 163, 163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 164, 164, 164,
- 164, 169, 169, 169, 169, 169, 169, 169, 169, 170, 170, 170, 170, 170, 170,
- 170, 170, 173, 173, 173, 173, 173, 173, 173, 173, 178, 178, 178, 178, 178,
- 178, 178, 178, 181, 181, 181, 181, 181, 181, 181, 181, 185, 185, 185, 185,
- 185, 185, 185, 185, 186, 186, 186, 186, 186, 186, 186, 186, 187, 187, 187,
- 187, 187, 187, 187, 187, 189, 189, 189, 189, 189, 189, 189, 189, 190, 190,
- 190, 190, 190, 190, 190, 190, 196, 196, 196, 196, 196, 196, 196, 196, 198,
- 198, 198, 198, 198, 198, 198, 198, 228, 228, 228, 228, 228, 228, 228, 228,
- 232, 232, 232, 232, 232, 232, 232, 232, 233, 233, 233, 233, 233, 233, 233,
- 233, 1, 1, 1, 1, 135, 135, 135, 135, 137, 137, 137, 137, 138, 138,
- 138, 138, 139, 139, 139, 139, 140, 140, 140, 140, 141, 141, 141, 141, 143,
- 143, 143, 143, 147, 147, 147, 147, 149, 149, 149, 149, 150, 150, 150, 150,
- 151, 151, 151, 151, 152, 152, 152, 152, 155, 155, 155, 155, 157, 157, 157,
- 157, 158, 158, 158, 158, 165, 165, 165, 165, 166, 166, 166, 166, 168, 168,
- 168, 168, 174, 174, 174, 174, 175, 175, 175, 175, 180, 180, 180, 180, 182,
- 182, 182, 182, 183, 183, 183, 183, 188, 188, 188, 188, 191, 191, 191, 191,
- 197, 197, 197, 197, 231, 231, 231, 231, 239, 239, 239, 239, 9, 9, 142,
- 142, 144, 144, 145, 145, 148, 148, 159, 159, 171, 171, 206, 206, 215, 215,
- 225, 225, 236, 236, 237, 237, 199, 207, 234, 235, 192, 192, 192, 192, 192,
- 192, 192, 192, 193, 193, 193, 193, 193, 193, 193, 193, 200, 200, 200, 200,
- 200, 200, 200, 200, 201, 201, 201, 201, 201, 201, 201, 201, 202, 202, 202,
- 202, 202, 202, 202, 202, 205, 205, 205, 205, 205, 205, 205, 205, 210, 210,
- 210, 210, 210, 210, 210, 210, 213, 213, 213, 213, 213, 213, 213, 213, 218,
- 218, 218, 218, 218, 218, 218, 218, 219, 219, 219, 219, 219, 219, 219, 219,
- 238, 238, 238, 238, 238, 238, 238, 238, 240, 240, 240, 240, 240, 240, 240,
- 240, 242, 242, 242, 242, 242, 242, 242, 242, 243, 243, 243, 243, 243, 243,
- 243, 243, 255, 255, 255, 255, 255, 255, 255, 255, 203, 203, 203, 203, 204,
- 204, 204, 204, 211, 211, 211, 211, 212, 212, 212, 212, 214, 214, 214, 214,
- 221, 221, 221, 221, 222, 222, 222, 222, 223, 223, 223, 223, 241, 241, 241,
- 241, 244, 244, 244, 244, 245, 245, 245, 245, 246, 246, 246, 246, 247, 247,
- 247, 247, 248, 248, 248, 248, 250, 250, 250, 250, 251, 251, 251, 251, 252,
- 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 2, 2, 3, 3,
- 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 11, 11, 12, 12, 14,
- 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21,
- 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
- 30, 31, 31, 127, 127, 220, 220, 249, 249, -1, -1, 10, 10, 10, 10,
- 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13, 22, 22, 22,
- 22, 22, 22, 22, 22, 256, 256, 256, 256, 256, 256, 256, 256, 45, 45,
- 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 47,
- 47, 47, 47, 47, 47, 47, 47, 51, 51, 51, 51, 51, 51, 51, 51,
- 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53,
- 53, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55,
- 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57,
- 57, 57, 57, 50, 50, 50, 50, 50, 50, 50, 50, 97, 97, 97, 97,
- 97, 97, 97, 97, 99, 99, 99, 99, 99, 99, 99, 99, 101, 101, 101,
- 101, 101, 101, 101, 101, 105, 105, 105, 105, 105, 105, 105, 105, 111, 111,
- 111, 111, 111, 111, 111, 111, 115, 115, 115, 115, 115, 115, 115, 115, 116,
- 116, 116, 116, 116, 116, 116, 116, 32, 32, 32, 32, 37, 37, 37, 37,
- 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 51, 51, 51,
- 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55,
- 55, 55, 56, 56, 56, 56, 57, 57, 57, 57, 61, 61, 61, 61, 65,
- 65, 65, 65, 95, 95, 95, 95, 98, 98, 98, 98, 100, 100, 100, 100,
- 102, 102, 102, 102, 103, 103, 103, 103, 104, 104, 104, 104, 108, 108, 108,
- 108, 109, 109, 109, 109, 110, 110, 110, 110, 112, 112, 112, 112, 114, 114,
- 114, 114, 117, 117, 117, 117, 58, 58, 66, 66, 67, 67, 68, 68, 69,
- 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76,
- 77, 77, 78, 78, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, 84,
- 84, 85, 85, 86, 86, 87, 87, 89, 89, 106, 106, 107, 107, 113, 113,
- 118, 118, 119, 119, 120, 120, 121, 121, 122, 122, 38, 42, 44, 59, 88,
- 90, -1, -1, 33, 33, 33, 33, 34, 34, 34, 34, 40, 40, 40, 40,
- 41, 41, 41, 41, 63, 63, 63, 63, 39, 39, 43, 43, 124, 124, 35,
- 62, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 36, 36,
- 36, 36, 36, 36, 36, 36, 64, 64, 64, 64, 64, 64, 64, 64, 91,
- 91, 91, 91, 91, 91, 91, 91, 93, 93, 93, 93, 93, 93, 93, 93,
- 126, 126, 126, 126, 126, 126, 126, 126, 94, 94, 94, 94, 125, 125, 125,
- 125, 60, 60, 96, 96, 123, 123, -1, -1, 92, 92, 195, 195, 208, 208,
- 128, 130, 131, 162, 184, 194, 224, 226, -1, -1, 153, 153, 153, 153, 153,
- 153, 153, 153, 161, 161, 161, 161, 161, 161, 161, 161, 167, 167, 167, 167,
- 167, 167, 167, 167, 172, 172, 172, 172, 172, 172, 172, 172, 176, 176, 176,
- 176, 176, 176, 176, 176, 177, 177, 177, 177, 177, 177, 177, 177, 179, 179,
- 179, 179, 179, 179, 179, 179, 209, 209, 209, 209, 209, 209, 209, 209, 216,
- 216, 216, 216, 216, 216, 216, 216, 217, 217, 217, 217, 217, 217, 217, 217,
- 227, 227, 227, 227, 227, 227, 227, 227, 229, 229, 229, 229, 229, 229, 229,
- 229, 230, 230, 230, 230, 230, 230, 230, 230, 129, 129, 129, 129, 132, 132,
- 132, 132, 133, 133, 133, 133, 134, 134, 134, 134, 136, 136, 136, 136, 146,
- 146, 146, 146, 154, 154, 154, 154, 156, 156, 156, 156, 160, 160, 160, 160,
- 163, 163, 163, 163, 164, 164, 164, 164, 169, 169, 169, 169, 170, 170, 170,
- 170, 173, 173, 173, 173, 178, 178, 178, 178, 181, 181, 181, 181, 185, 185,
- 185, 185, 186, 186, 186, 186, 187, 187, 187, 187, 189, 189, 189, 189, 190,
- 190, 190, 190, 196, 196, 196, 196, 198, 198, 198, 198, 228, 228, 228, 228,
- 232, 232, 232, 232, 233, 233, 233, 233, 1, 1, 135, 135, 137, 137, 138,
- 138, 139, 139, 140, 140, 141, 141, 143, 143, 147, 147, 149, 149, 150, 150,
- 151, 151, 152, 152, 155, 155, 157, 157, 158, 158, 165, 165, 166, 166, 168,
- 168, 174, 174, 175, 175, 180, 180, 182, 182, 183, 183, 188, 188, 191, 191,
- 197, 197, 231, 231, 239, 239, 9, 142, 144, 145, 148, 159, 171, 206, 215,
- 225, 236, 237, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 199, 199,
- 199, 199, 199, 199, 199, 199, 207, 207, 207, 207, 207, 207, 207, 207, 234,
- 234, 234, 234, 234, 234, 234, 234, 235, 235, 235, 235, 235, 235, 235, 235,
- 192, 192, 192, 192, 193, 193, 193, 193, 200, 200, 200, 200, 201, 201, 201,
- 201, 202, 202, 202, 202, 205, 205, 205, 205, 210, 210, 210, 210, 213, 213,
- 213, 213, 218, 218, 218, 218, 219, 219, 219, 219, 238, 238, 238, 238, 240,
- 240, 240, 240, 242, 242, 242, 242, 243, 243, 243, 243, 255, 255, 255, 255,
- 203, 203, 204, 204, 211, 211, 212, 212, 214, 214, 221, 221, 222, 222, 223,
- 223, 241, 241, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 250, 250,
- 251, 251, 252, 252, 253, 253, 254, 254, 2, 3, 4, 5, 6, 7, 8,
- 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27,
- 28, 29, 30, 31, 127, 220, 249, -1, 10, 10, 10, 10, 13, 13, 13,
- 13, 22, 22, 22, 22, 256, 256, 256, 256,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49,
+ 49, 49, 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 97,
+ 97, 97, 97, 48, 48, 49, 49, 50, 50, 97, 97, 99, 99, 101, 101,
+ 105, 105, 111, 111, 48, 49, 50, 97, 99, 101, 105, 111, 115, 116, -1,
+ -1, -1, -1, -1, -1, 32, 32, 32, 32, 32, 32, 32, 32, 37, 37,
+ 37, 37, 37, 37, 37, 37, 99, 99, 99, 99, 101, 101, 101, 101, 105,
+ 105, 105, 105, 111, 111, 111, 111, 115, 115, 116, 116, 32, 37, 45, 46,
+ 47, 51, 52, 53, 54, 55, 56, 57, 61, 61, 61, 61, 61, 61, 61,
+ 61, 65, 65, 65, 65, 65, 65, 65, 65, 115, 115, 115, 115, 116, 116,
+ 116, 116, 32, 32, 37, 37, 45, 45, 46, 46, 61, 65, 95, 98, 100,
+ 102, 103, 104, 108, 109, 110, 112, 114, 117, -1, -1, 58, 58, 58, 58,
+ 58, 58, 58, 58, 66, 66, 66, 66, 66, 66, 66, 66, 47, 47, 51,
+ 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 61, 61,
+ 65, 65, 95, 95, 98, 98, 100, 100, 102, 102, 103, 103, 104, 104, 108,
+ 108, 109, 109, 110, 110, 112, 112, 114, 114, 117, 117, 58, 66, 67, 68,
+ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
+ 84, 85, 86, 87, 89, 106, 107, 113, 118, 119, 120, 121, 122, -1, -1,
+ -1, -1, 38, 38, 38, 38, 38, 38, 38, 38, 42, 42, 42, 42, 42,
+ 42, 42, 42, 44, 44, 44, 44, 44, 44, 44, 44, 59, 59, 59, 59,
+ 59, 59, 59, 59, 88, 88, 88, 88, 88, 88, 88, 88, 90, 90, 90,
+ 90, 90, 90, 90, 90, 33, 33, 34, 34, 40, 40, 41, 41, 63, 63,
+ 39, 43, 124, -1, -1, -1, 35, 35, 35, 35, 35, 35, 35, 35, 62,
+ 62, 62, 62, 62, 62, 62, 62, 0, 0, 0, 0, 36, 36, 36, 36,
+ 64, 64, 64, 64, 91, 91, 91, 91, 69, 69, 69, 69, 69, 69, 69,
+ 69, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71,
+ 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 73, 73, 73, 73, 73,
+ 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75,
+ 75, 75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77,
+ 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 78, 78, 79, 79,
+ 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 81,
+ 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82,
+ 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 84,
+ 84, 85, 85, 85, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 86,
+ 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 89, 89, 89, 89, 89,
+ 89, 89, 89, 106, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
+ 107, 107, 107, 107, 113, 113, 113, 113, 113, 113, 113, 113, 118, 118, 118,
+ 118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 119, 119, 120, 120,
+ 120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 122,
+ 122, 122, 122, 122, 122, 122, 122, 38, 38, 38, 38, 42, 42, 42, 42,
+ 44, 44, 44, 44, 59, 59, 59, 59, 88, 88, 88, 88, 90, 90, 90,
+ 90, 33, 34, 40, 41, 63, -1, -1, -1, 39, 39, 39, 39, 39, 39,
+ 39, 39, 43, 43, 43, 43, 43, 43, 43, 43, 124, 124, 124, 124, 124,
+ 124, 124, 124, 35, 35, 35, 35, 62, 62, 62, 62, 0, 0, 36, 36,
+ 64, 64, 91, 91, 93, 93, 126, 126, 94, 125, -1, -1, 60, 60, 60,
+ 60, 60, 60, 60, 60, 96, 96, 96, 96, 96, 96, 96, 96, 123, 123,
+ 123, 123, 123, 123, 123, 123, -1, -1, -1, -1, -1, -1, -1, -1, 92,
+ 92, 92, 92, 92, 92, 92, 92, 195, 195, 195, 195, 195, 195, 195, 195,
+ 208, 208, 208, 208, 208, 208, 208, 208, 128, 128, 128, 128, 130, 130, 130,
+ 130, 131, 131, 131, 131, 162, 162, 162, 162, 184, 184, 184, 184, 194, 194,
+ 194, 194, 224, 224, 224, 224, 226, 226, 226, 226, 153, 153, 161, 161, 167,
+ 167, 172, 172, 176, 176, 177, 177, 179, 179, 209, 209, 216, 216, 217, 217,
+ 227, 227, 229, 229, 230, 230, 129, 132, 133, 134, 136, 146, 154, 156, 160,
+ 163, 164, 169, 170, 173, 178, 181, 185, 186, 187, 189, 190, 196, 198, 228,
+ 232, 233, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 135,
+ 135, 135, 135, 135, 135, 135, 135, 137, 137, 137, 137, 137, 137, 137, 137,
+ 138, 138, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139, 139, 139, 139,
+ 139, 140, 140, 140, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 141,
+ 141, 141, 143, 143, 143, 143, 143, 143, 143, 143, 147, 147, 147, 147, 147,
+ 147, 147, 147, 149, 149, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150,
+ 150, 150, 150, 150, 151, 151, 151, 151, 151, 151, 151, 151, 152, 152, 152,
+ 152, 152, 152, 152, 152, 155, 155, 155, 155, 155, 155, 155, 155, 157, 157,
+ 157, 157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 158, 158, 165,
+ 165, 165, 165, 165, 165, 165, 165, 166, 166, 166, 166, 166, 166, 166, 166,
+ 168, 168, 168, 168, 168, 168, 168, 168, 174, 174, 174, 174, 174, 174, 174,
+ 174, 175, 175, 175, 175, 175, 175, 175, 175, 180, 180, 180, 180, 180, 180,
+ 180, 180, 182, 182, 182, 182, 182, 182, 182, 182, 183, 183, 183, 183, 183,
+ 183, 183, 183, 188, 188, 188, 188, 188, 188, 188, 188, 191, 191, 191, 191,
+ 191, 191, 191, 191, 197, 197, 197, 197, 197, 197, 197, 197, 231, 231, 231,
+ 231, 231, 231, 231, 231, 239, 239, 239, 239, 239, 239, 239, 239, 9, 9,
+ 9, 9, 142, 142, 142, 142, 144, 144, 144, 144, 145, 145, 145, 145, 148,
+ 148, 148, 148, 159, 159, 159, 159, 171, 171, 171, 171, 206, 206, 206, 206,
+ 215, 215, 215, 215, 225, 225, 225, 225, 236, 236, 236, 236, 237, 237, 237,
+ 237, 199, 199, 207, 207, 234, 234, 235, 235, 192, 193, 200, 201, 202, 205,
+ 210, 213, 218, 219, 238, 240, 242, 243, 255, -1, 203, 203, 203, 203, 203,
+ 203, 203, 203, 204, 204, 204, 204, 204, 204, 204, 204, 211, 211, 211, 211,
+ 211, 211, 211, 211, 212, 212, 212, 212, 212, 212, 212, 212, 214, 214, 214,
+ 214, 214, 214, 214, 214, 221, 221, 221, 221, 221, 221, 221, 221, 222, 222,
+ 222, 222, 222, 222, 222, 222, 223, 223, 223, 223, 223, 223, 223, 223, 241,
+ 241, 241, 241, 241, 241, 241, 241, 244, 244, 244, 244, 244, 244, 244, 244,
+ 245, 245, 245, 245, 245, 245, 245, 245, 246, 246, 246, 246, 246, 246, 246,
+ 246, 247, 247, 247, 247, 247, 247, 247, 247, 248, 248, 248, 248, 248, 248,
+ 248, 248, 250, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251,
+ 251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, 253, 253,
+ 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 2, 2, 2,
+ 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
+ 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 11, 11, 11, 11, 12,
+ 12, 12, 12, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16,
+ 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20,
+ 20, 21, 21, 21, 21, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25,
+ 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29,
+ 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 127, 127, 127, 127,
+ 220, 220, 220, 220, 249, 249, 249, 249, 10, 13, 22, 256, 93, 93, 93,
+ 93, 126, 126, 126, 126, 94, 94, 125, 125, 60, 96, 123, -1, 92, 195,
+ 208, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 128,
+ 128, 128, 128, 128, 128, 128, 128, 130, 130, 130, 130, 130, 130, 130, 130,
+ 131, 131, 131, 131, 131, 131, 131, 131, 162, 162, 162, 162, 162, 162, 162,
+ 162, 184, 184, 184, 184, 184, 184, 184, 184, 194, 194, 194, 194, 194, 194,
+ 194, 194, 224, 224, 224, 224, 224, 224, 224, 224, 226, 226, 226, 226, 226,
+ 226, 226, 226, 153, 153, 153, 153, 161, 161, 161, 161, 167, 167, 167, 167,
+ 172, 172, 172, 172, 176, 176, 176, 176, 177, 177, 177, 177, 179, 179, 179,
+ 179, 209, 209, 209, 209, 216, 216, 216, 216, 217, 217, 217, 217, 227, 227,
+ 227, 227, 229, 229, 229, 229, 230, 230, 230, 230, 129, 129, 132, 132, 133,
+ 133, 134, 134, 136, 136, 146, 146, 154, 154, 156, 156, 160, 160, 163, 163,
+ 164, 164, 169, 169, 170, 170, 173, 173, 178, 178, 181, 181, 185, 185, 186,
+ 186, 187, 187, 189, 189, 190, 190, 196, 196, 198, 198, 228, 228, 232, 232,
+ 233, 233, 1, 135, 137, 138, 139, 140, 141, 143, 147, 149, 150, 151, 152,
+ 155, 157, 158, 165, 166, 168, 174, 175, 180, 182, 183, 188, 191, 197, 231,
+ 239, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 9, 9, 9,
+ 9, 9, 9, 9, 9, 142, 142, 142, 142, 142, 142, 142, 142, 144, 144,
+ 144, 144, 144, 144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 145, 148,
+ 148, 148, 148, 148, 148, 148, 148, 159, 159, 159, 159, 159, 159, 159, 159,
+ 171, 171, 171, 171, 171, 171, 171, 171, 206, 206, 206, 206, 206, 206, 206,
+ 206, 215, 215, 215, 215, 215, 215, 215, 215, 225, 225, 225, 225, 225, 225,
+ 225, 225, 236, 236, 236, 236, 236, 236, 236, 236, 237, 237, 237, 237, 237,
+ 237, 237, 237, 199, 199, 199, 199, 207, 207, 207, 207, 234, 234, 234, 234,
+ 235, 235, 235, 235, 192, 192, 193, 193, 200, 200, 201, 201, 202, 202, 205,
+ 205, 210, 210, 213, 213, 218, 218, 219, 219, 238, 238, 240, 240, 242, 242,
+ 243, 243, 255, 255, 203, 204, 211, 212, 214, 221, 222, 223, 241, 244, 245,
+ 246, 247, 248, 250, 251, 252, 253, 254, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2,
+ 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
+ 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
+ 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
+ 8, 8, 8, 8, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12,
+ 12, 12, 12, 12, 12, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15,
+ 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17,
+ 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18,
+ 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
+ 20, 21, 21, 21, 21, 21, 21, 21, 21, 23, 23, 23, 23, 23, 23,
+ 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
+ 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27,
+ 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29,
+ 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31,
+ 31, 31, 31, 31, 31, 31, 127, 127, 127, 127, 127, 127, 127, 127, 220,
+ 220, 220, 220, 220, 220, 220, 220, 249, 249, 249, 249, 249, 249, 249, 249,
+ 10, 10, 13, 13, 22, 22, 256, 256, 67, 67, 67, 67, 67, 67, 67,
+ 67, 68, 68, 68, 68, 68, 68, 68, 68, 95, 95, 95, 95, 95, 95,
+ 95, 95, 98, 98, 98, 98, 98, 98, 98, 98, 100, 100, 100, 100, 100,
+ 100, 100, 100, 102, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103,
+ 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 108, 108, 108,
+ 108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109, 109, 109, 110, 110,
+ 110, 110, 110, 110, 110, 110, 112, 112, 112, 112, 112, 112, 112, 112, 114,
+ 114, 114, 114, 114, 114, 114, 114, 117, 117, 117, 117, 117, 117, 117, 117,
+ 58, 58, 58, 58, 66, 66, 66, 66, 67, 67, 67, 67, 68, 68, 68,
+ 68, 69, 69, 69, 69, 70, 70, 70, 70, 71, 71, 71, 71, 72, 72,
+ 72, 72, 73, 73, 73, 73, 74, 74, 74, 74, 75, 75, 75, 75, 76,
+ 76, 76, 76, 77, 77, 77, 77, 78, 78, 78, 78, 79, 79, 79, 79,
+ 80, 80, 80, 80, 81, 81, 81, 81, 82, 82, 82, 82, 83, 83, 83,
+ 83, 84, 84, 84, 84, 85, 85, 85, 85, 86, 86, 86, 86, 87, 87,
+ 87, 87, 89, 89, 89, 89, 106, 106, 106, 106, 107, 107, 107, 107, 113,
+ 113, 113, 113, 118, 118, 118, 118, 119, 119, 119, 119, 120, 120, 120, 120,
+ 121, 121, 121, 121, 122, 122, 122, 122, 38, 38, 42, 42, 44, 44, 59,
+ 59, 88, 88, 90, 90, -1, -1, -1, -1, 33, 33, 33, 33, 33, 33,
+ 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 40, 40, 40, 40, 40,
+ 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 63, 63, 63, 63,
+ 63, 63, 63, 63, 39, 39, 39, 39, 43, 43, 43, 43, 124, 124, 124,
+ 124, 35, 35, 62, 62, 0, 36, 64, 91, 93, 126, -1, -1, 94, 94,
+ 94, 94, 94, 94, 94, 94, 125, 125, 125, 125, 125, 125, 125, 125, 60,
+ 60, 60, 60, 96, 96, 96, 96, 123, 123, 123, 123, -1, -1, -1, -1,
+ 92, 92, 92, 92, 195, 195, 195, 195, 208, 208, 208, 208, 128, 128, 130,
+ 130, 131, 131, 162, 162, 184, 184, 194, 194, 224, 224, 226, 226, 153, 161,
+ 167, 172, 176, 177, 179, 209, 216, 217, 227, 229, 230, -1, -1, -1, -1,
+ -1, -1, -1, 129, 129, 129, 129, 129, 129, 129, 129, 132, 132, 132, 132,
+ 132, 132, 132, 132, 133, 133, 133, 133, 133, 133, 133, 133, 134, 134, 134,
+ 134, 134, 134, 134, 134, 136, 136, 136, 136, 136, 136, 136, 136, 146, 146,
+ 146, 146, 146, 146, 146, 146, 154, 154, 154, 154, 154, 154, 154, 154, 156,
+ 156, 156, 156, 156, 156, 156, 156, 160, 160, 160, 160, 160, 160, 160, 160,
+ 163, 163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 164, 164, 164,
+ 164, 169, 169, 169, 169, 169, 169, 169, 169, 170, 170, 170, 170, 170, 170,
+ 170, 170, 173, 173, 173, 173, 173, 173, 173, 173, 178, 178, 178, 178, 178,
+ 178, 178, 178, 181, 181, 181, 181, 181, 181, 181, 181, 185, 185, 185, 185,
+ 185, 185, 185, 185, 186, 186, 186, 186, 186, 186, 186, 186, 187, 187, 187,
+ 187, 187, 187, 187, 187, 189, 189, 189, 189, 189, 189, 189, 189, 190, 190,
+ 190, 190, 190, 190, 190, 190, 196, 196, 196, 196, 196, 196, 196, 196, 198,
+ 198, 198, 198, 198, 198, 198, 198, 228, 228, 228, 228, 228, 228, 228, 228,
+ 232, 232, 232, 232, 232, 232, 232, 232, 233, 233, 233, 233, 233, 233, 233,
+ 233, 1, 1, 1, 1, 135, 135, 135, 135, 137, 137, 137, 137, 138, 138,
+ 138, 138, 139, 139, 139, 139, 140, 140, 140, 140, 141, 141, 141, 141, 143,
+ 143, 143, 143, 147, 147, 147, 147, 149, 149, 149, 149, 150, 150, 150, 150,
+ 151, 151, 151, 151, 152, 152, 152, 152, 155, 155, 155, 155, 157, 157, 157,
+ 157, 158, 158, 158, 158, 165, 165, 165, 165, 166, 166, 166, 166, 168, 168,
+ 168, 168, 174, 174, 174, 174, 175, 175, 175, 175, 180, 180, 180, 180, 182,
+ 182, 182, 182, 183, 183, 183, 183, 188, 188, 188, 188, 191, 191, 191, 191,
+ 197, 197, 197, 197, 231, 231, 231, 231, 239, 239, 239, 239, 9, 9, 142,
+ 142, 144, 144, 145, 145, 148, 148, 159, 159, 171, 171, 206, 206, 215, 215,
+ 225, 225, 236, 236, 237, 237, 199, 207, 234, 235, 192, 192, 192, 192, 192,
+ 192, 192, 192, 193, 193, 193, 193, 193, 193, 193, 193, 200, 200, 200, 200,
+ 200, 200, 200, 200, 201, 201, 201, 201, 201, 201, 201, 201, 202, 202, 202,
+ 202, 202, 202, 202, 202, 205, 205, 205, 205, 205, 205, 205, 205, 210, 210,
+ 210, 210, 210, 210, 210, 210, 213, 213, 213, 213, 213, 213, 213, 213, 218,
+ 218, 218, 218, 218, 218, 218, 218, 219, 219, 219, 219, 219, 219, 219, 219,
+ 238, 238, 238, 238, 238, 238, 238, 238, 240, 240, 240, 240, 240, 240, 240,
+ 240, 242, 242, 242, 242, 242, 242, 242, 242, 243, 243, 243, 243, 243, 243,
+ 243, 243, 255, 255, 255, 255, 255, 255, 255, 255, 203, 203, 203, 203, 204,
+ 204, 204, 204, 211, 211, 211, 211, 212, 212, 212, 212, 214, 214, 214, 214,
+ 221, 221, 221, 221, 222, 222, 222, 222, 223, 223, 223, 223, 241, 241, 241,
+ 241, 244, 244, 244, 244, 245, 245, 245, 245, 246, 246, 246, 246, 247, 247,
+ 247, 247, 248, 248, 248, 248, 250, 250, 250, 250, 251, 251, 251, 251, 252,
+ 252, 252, 252, 253, 253, 253, 253, 254, 254, 254, 254, 2, 2, 3, 3,
+ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 11, 11, 12, 12, 14,
+ 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21,
+ 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,
+ 30, 31, 31, 127, 127, 220, 220, 249, 249, -1, -1, 10, 10, 10, 10,
+ 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13, 22, 22, 22,
+ 22, 22, 22, 22, 22, 256, 256, 256, 256, 256, 256, 256, 256, 45, 45,
+ 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 47,
+ 47, 47, 47, 47, 47, 47, 47, 51, 51, 51, 51, 51, 51, 51, 51,
+ 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53,
+ 53, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55,
+ 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57,
+ 57, 57, 57, 50, 50, 50, 50, 50, 50, 50, 50, 97, 97, 97, 97,
+ 97, 97, 97, 97, 99, 99, 99, 99, 99, 99, 99, 99, 101, 101, 101,
+ 101, 101, 101, 101, 101, 105, 105, 105, 105, 105, 105, 105, 105, 111, 111,
+ 111, 111, 111, 111, 111, 111, 115, 115, 115, 115, 115, 115, 115, 115, 116,
+ 116, 116, 116, 116, 116, 116, 116, 32, 32, 32, 32, 37, 37, 37, 37,
+ 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 51, 51, 51,
+ 51, 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55,
+ 55, 55, 56, 56, 56, 56, 57, 57, 57, 57, 61, 61, 61, 61, 65,
+ 65, 65, 65, 95, 95, 95, 95, 98, 98, 98, 98, 100, 100, 100, 100,
+ 102, 102, 102, 102, 103, 103, 103, 103, 104, 104, 104, 104, 108, 108, 108,
+ 108, 109, 109, 109, 109, 110, 110, 110, 110, 112, 112, 112, 112, 114, 114,
+ 114, 114, 117, 117, 117, 117, 58, 58, 66, 66, 67, 67, 68, 68, 69,
+ 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76,
+ 77, 77, 78, 78, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, 84,
+ 84, 85, 85, 86, 86, 87, 87, 89, 89, 106, 106, 107, 107, 113, 113,
+ 118, 118, 119, 119, 120, 120, 121, 121, 122, 122, 38, 42, 44, 59, 88,
+ 90, -1, -1, 33, 33, 33, 33, 34, 34, 34, 34, 40, 40, 40, 40,
+ 41, 41, 41, 41, 63, 63, 63, 63, 39, 39, 43, 43, 124, 124, 35,
+ 62, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 36, 36,
+ 36, 36, 36, 36, 36, 36, 64, 64, 64, 64, 64, 64, 64, 64, 91,
+ 91, 91, 91, 91, 91, 91, 91, 93, 93, 93, 93, 93, 93, 93, 93,
+ 126, 126, 126, 126, 126, 126, 126, 126, 94, 94, 94, 94, 125, 125, 125,
+ 125, 60, 60, 96, 96, 123, 123, -1, -1, 92, 92, 195, 195, 208, 208,
+ 128, 130, 131, 162, 184, 194, 224, 226, -1, -1, 153, 153, 153, 153, 153,
+ 153, 153, 153, 161, 161, 161, 161, 161, 161, 161, 161, 167, 167, 167, 167,
+ 167, 167, 167, 167, 172, 172, 172, 172, 172, 172, 172, 172, 176, 176, 176,
+ 176, 176, 176, 176, 176, 177, 177, 177, 177, 177, 177, 177, 177, 179, 179,
+ 179, 179, 179, 179, 179, 179, 209, 209, 209, 209, 209, 209, 209, 209, 216,
+ 216, 216, 216, 216, 216, 216, 216, 217, 217, 217, 217, 217, 217, 217, 217,
+ 227, 227, 227, 227, 227, 227, 227, 227, 229, 229, 229, 229, 229, 229, 229,
+ 229, 230, 230, 230, 230, 230, 230, 230, 230, 129, 129, 129, 129, 132, 132,
+ 132, 132, 133, 133, 133, 133, 134, 134, 134, 134, 136, 136, 136, 136, 146,
+ 146, 146, 146, 154, 154, 154, 154, 156, 156, 156, 156, 160, 160, 160, 160,
+ 163, 163, 163, 163, 164, 164, 164, 164, 169, 169, 169, 169, 170, 170, 170,
+ 170, 173, 173, 173, 173, 178, 178, 178, 178, 181, 181, 181, 181, 185, 185,
+ 185, 185, 186, 186, 186, 186, 187, 187, 187, 187, 189, 189, 189, 189, 190,
+ 190, 190, 190, 196, 196, 196, 196, 198, 198, 198, 198, 228, 228, 228, 228,
+ 232, 232, 232, 232, 233, 233, 233, 233, 1, 1, 135, 135, 137, 137, 138,
+ 138, 139, 139, 140, 140, 141, 141, 143, 143, 147, 147, 149, 149, 150, 150,
+ 151, 151, 152, 152, 155, 155, 157, 157, 158, 158, 165, 165, 166, 166, 168,
+ 168, 174, 174, 175, 175, 180, 180, 182, 182, 183, 183, 188, 188, 191, 191,
+ 197, 197, 231, 231, 239, 239, 9, 142, 144, 145, 148, 159, 171, 206, 215,
+ 225, 236, 237, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 199, 199,
+ 199, 199, 199, 199, 199, 199, 207, 207, 207, 207, 207, 207, 207, 207, 234,
+ 234, 234, 234, 234, 234, 234, 234, 235, 235, 235, 235, 235, 235, 235, 235,
+ 192, 192, 192, 192, 193, 193, 193, 193, 200, 200, 200, 200, 201, 201, 201,
+ 201, 202, 202, 202, 202, 205, 205, 205, 205, 210, 210, 210, 210, 213, 213,
+ 213, 213, 218, 218, 218, 218, 219, 219, 219, 219, 238, 238, 238, 238, 240,
+ 240, 240, 240, 242, 242, 242, 242, 243, 243, 243, 243, 255, 255, 255, 255,
+ 203, 203, 204, 204, 211, 211, 212, 212, 214, 214, 221, 221, 222, 222, 223,
+ 223, 241, 241, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 250, 250,
+ 251, 251, 252, 252, 253, 253, 254, 254, 2, 3, 4, 5, 6, 7, 8,
+ 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27,
+ 28, 29, 30, 31, 127, 220, 249, -1, 10, 10, 10, 10, 13, 13, 13,
+ 13, 22, 22, 22, 22, 256, 256, 256, 256,
};
static const gpr_uint8 inverse_base64[256] = {
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255,
- 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
- 255, 64, 255, 255, 255, 0, 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, 255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255,
+ 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
+ 255, 64, 255, 255, 255, 0, 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, 255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
+ 255,
};
/* emission helpers */
-static void
-on_hdr (grpc_chttp2_hpack_parser * p, grpc_mdelem * md, int add_to_table)
-{
- if (add_to_table)
- {
- GRPC_MDELEM_REF (md);
- grpc_chttp2_hptbl_add (&p->table, md);
- }
- p->on_header (p->on_header_user_data, md);
+static void on_hdr(grpc_chttp2_hpack_parser *p, grpc_mdelem *md,
+ int add_to_table) {
+ if (add_to_table) {
+ GRPC_MDELEM_REF(md);
+ grpc_chttp2_hptbl_add(&p->table, md);
+ }
+ p->on_header(p->on_header_user_data, md);
}
-static grpc_mdstr *
-take_string (grpc_chttp2_hpack_parser * p, grpc_chttp2_hpack_parser_string * str)
-{
- grpc_mdstr *s = grpc_mdstr_from_buffer (p->table.mdctx, (gpr_uint8 *) str->str,
- str->length);
+static grpc_mdstr *take_string(grpc_chttp2_hpack_parser *p,
+ grpc_chttp2_hpack_parser_string *str) {
+ grpc_mdstr *s = grpc_mdstr_from_buffer(p->table.mdctx, (gpr_uint8 *)str->str,
+ str->length);
str->length = 0;
return s;
}
/* jump to the next state */
-static int
-parse_next (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_next(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
p->state = *p->next_state++;
- return p->state (p, cur, end);
+ return p->state(p, cur, end);
}
/* begin parsing a header: all functionality is encoded into lookup tables
above */
-static int
-parse_begin (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_begin;
- return 1;
- }
-
- return first_byte_action[first_byte_lut[*cur]] (p, cur, end);
+static int parse_begin(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_begin;
+ return 1;
+ }
+
+ return first_byte_action[first_byte_lut[*cur]](p, cur, end);
}
/* stream dependency and prioritization data: we just skip it */
-static int
-parse_stream_weight (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_stream_weight;
- return 1;
- }
-
- return p->after_prioritization (p, cur + 1, end);
+static int parse_stream_weight(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_stream_weight;
+ return 1;
+ }
+
+ return p->after_prioritization(p, cur + 1, end);
}
-static int
-parse_stream_dep3 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_stream_dep3;
- return 1;
- }
+static int parse_stream_dep3(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_stream_dep3;
+ return 1;
+ }
- return parse_stream_weight (p, cur + 1, end);
+ return parse_stream_weight(p, cur + 1, end);
}
-static int
-parse_stream_dep2 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_stream_dep2;
- return 1;
- }
+static int parse_stream_dep2(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_stream_dep2;
+ return 1;
+ }
- return parse_stream_dep3 (p, cur + 1, end);
+ return parse_stream_dep3(p, cur + 1, end);
}
-static int
-parse_stream_dep1 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_stream_dep1;
- return 1;
- }
+static int parse_stream_dep1(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_stream_dep1;
+ return 1;
+ }
- return parse_stream_dep2 (p, cur + 1, end);
+ return parse_stream_dep2(p, cur + 1, end);
}
-static int
-parse_stream_dep0 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_stream_dep0;
- return 1;
- }
+static int parse_stream_dep0(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_stream_dep0;
+ return 1;
+ }
- return parse_stream_dep1 (p, cur + 1, end);
+ return parse_stream_dep1(p, cur + 1, end);
}
/* emit an indexed field; for now just logs it to console; jumps to
begin the next field on completion */
-static int
-finish_indexed_field (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- grpc_mdelem *md = grpc_chttp2_hptbl_lookup (&p->table, p->index);
- GRPC_MDELEM_REF (md);
- on_hdr (p, md, 0);
- return parse_begin (p, cur, end);
+static int finish_indexed_field(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index);
+ GRPC_MDELEM_REF(md);
+ on_hdr(p, md, 0);
+ return parse_begin(p, cur, end);
}
/* parse an indexed field with index < 127 */
-static int
-parse_indexed_field (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_indexed_field(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
p->index = (*cur) & 0x7f;
- return finish_indexed_field (p, cur + 1, end);
+ return finish_indexed_field(p, cur + 1, end);
}
/* parse an indexed field with index >= 127 */
-static int
-parse_indexed_field_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_indexed_field_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- finish_indexed_field
- };
+ finish_indexed_field};
p->next_state = and_then;
p->index = 0x7f;
p->parsing.value = &p->index;
- return parse_value0 (p, cur + 1, end);
+ return parse_value0(p, cur + 1, end);
}
/* finish a literal header with incremental indexing: just log, and jump to '
begin */
-static int
-finish_lithdr_incidx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- grpc_mdelem *md = grpc_chttp2_hptbl_lookup (&p->table, p->index);
- on_hdr (p, grpc_mdelem_from_metadata_strings (p->table.mdctx, GRPC_MDSTR_REF (md->key), take_string (p, &p->value)), 1);
- return parse_begin (p, cur, end);
+static int finish_lithdr_incidx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index);
+ on_hdr(p, grpc_mdelem_from_metadata_strings(p->table.mdctx,
+ GRPC_MDSTR_REF(md->key),
+ take_string(p, &p->value)),
+ 1);
+ return parse_begin(p, cur, end);
}
/* finish a literal header with incremental indexing with no index */
-static int
-finish_lithdr_incidx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- on_hdr (p, grpc_mdelem_from_metadata_strings (p->table.mdctx, take_string (p, &p->key), take_string (p, &p->value)), 1);
- return parse_begin (p, cur, end);
+static int finish_lithdr_incidx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ on_hdr(p, grpc_mdelem_from_metadata_strings(p->table.mdctx,
+ take_string(p, &p->key),
+ take_string(p, &p->value)),
+ 1);
+ return parse_begin(p, cur, end);
}
/* parse a literal header with incremental indexing; index < 63 */
-static int
-parse_lithdr_incidx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_incidx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_value_string_with_indexed_key, finish_lithdr_incidx
- };
+ parse_value_string_with_indexed_key, finish_lithdr_incidx};
p->next_state = and_then;
p->index = (*cur) & 0x3f;
- return parse_string_prefix (p, cur + 1, end);
+ return parse_string_prefix(p, cur + 1, end);
}
/* parse a literal header with incremental indexing; index >= 63 */
-static int
-parse_lithdr_incidx_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_incidx_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_string_prefix, parse_value_string_with_indexed_key,
- finish_lithdr_incidx
- };
+ parse_string_prefix, parse_value_string_with_indexed_key,
+ finish_lithdr_incidx};
p->next_state = and_then;
p->index = 0x3f;
p->parsing.value = &p->index;
- return parse_value0 (p, cur + 1, end);
+ return parse_value0(p, cur + 1, end);
}
/* parse a literal header with incremental indexing; index = 0 */
-static int
-parse_lithdr_incidx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_incidx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_key_string, parse_string_prefix,
- parse_value_string_with_literal_key, finish_lithdr_incidx_v
- };
+ parse_key_string, parse_string_prefix,
+ parse_value_string_with_literal_key, finish_lithdr_incidx_v};
p->next_state = and_then;
- return parse_string_prefix (p, cur + 1, end);
+ return parse_string_prefix(p, cur + 1, end);
}
/* finish a literal header without incremental indexing */
-static int
-finish_lithdr_notidx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- grpc_mdelem *md = grpc_chttp2_hptbl_lookup (&p->table, p->index);
- on_hdr (p, grpc_mdelem_from_metadata_strings (p->table.mdctx, GRPC_MDSTR_REF (md->key), take_string (p, &p->value)), 0);
- return parse_begin (p, cur, end);
+static int finish_lithdr_notidx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index);
+ on_hdr(p, grpc_mdelem_from_metadata_strings(p->table.mdctx,
+ GRPC_MDSTR_REF(md->key),
+ take_string(p, &p->value)),
+ 0);
+ return parse_begin(p, cur, end);
}
/* finish a literal header without incremental indexing with index = 0 */
-static int
-finish_lithdr_notidx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- on_hdr (p, grpc_mdelem_from_metadata_strings (p->table.mdctx, take_string (p, &p->key), take_string (p, &p->value)), 0);
- return parse_begin (p, cur, end);
+static int finish_lithdr_notidx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ on_hdr(p, grpc_mdelem_from_metadata_strings(p->table.mdctx,
+ take_string(p, &p->key),
+ take_string(p, &p->value)),
+ 0);
+ return parse_begin(p, cur, end);
}
/* parse a literal header without incremental indexing; index < 15 */
-static int
-parse_lithdr_notidx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_notidx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_value_string_with_indexed_key, finish_lithdr_notidx
- };
+ parse_value_string_with_indexed_key, finish_lithdr_notidx};
p->next_state = and_then;
p->index = (*cur) & 0xf;
- return parse_string_prefix (p, cur + 1, end);
+ return parse_string_prefix(p, cur + 1, end);
}
/* parse a literal header without incremental indexing; index >= 15 */
-static int
-parse_lithdr_notidx_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_notidx_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_string_prefix, parse_value_string_with_indexed_key,
- finish_lithdr_notidx
- };
+ parse_string_prefix, parse_value_string_with_indexed_key,
+ finish_lithdr_notidx};
p->next_state = and_then;
p->index = 0xf;
p->parsing.value = &p->index;
- return parse_value0 (p, cur + 1, end);
+ return parse_value0(p, cur + 1, end);
}
/* parse a literal header without incremental indexing; index == 0 */
-static int
-parse_lithdr_notidx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_notidx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_key_string, parse_string_prefix,
- parse_value_string_with_literal_key, finish_lithdr_notidx_v
- };
+ parse_key_string, parse_string_prefix,
+ parse_value_string_with_literal_key, finish_lithdr_notidx_v};
p->next_state = and_then;
- return parse_string_prefix (p, cur + 1, end);
+ return parse_string_prefix(p, cur + 1, end);
}
/* finish a literal header that is never indexed */
-static int
-finish_lithdr_nvridx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- grpc_mdelem *md = grpc_chttp2_hptbl_lookup (&p->table, p->index);
- on_hdr (p, grpc_mdelem_from_metadata_strings (p->table.mdctx, GRPC_MDSTR_REF (md->key), take_string (p, &p->value)), 0);
- return parse_begin (p, cur, end);
+static int finish_lithdr_nvridx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ grpc_mdelem *md = grpc_chttp2_hptbl_lookup(&p->table, p->index);
+ on_hdr(p, grpc_mdelem_from_metadata_strings(p->table.mdctx,
+ GRPC_MDSTR_REF(md->key),
+ take_string(p, &p->value)),
+ 0);
+ return parse_begin(p, cur, end);
}
/* finish a literal header that is never indexed with an extra value */
-static int
-finish_lithdr_nvridx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- on_hdr (p, grpc_mdelem_from_metadata_strings (p->table.mdctx, take_string (p, &p->key), take_string (p, &p->value)), 0);
- return parse_begin (p, cur, end);
+static int finish_lithdr_nvridx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ on_hdr(p, grpc_mdelem_from_metadata_strings(p->table.mdctx,
+ take_string(p, &p->key),
+ take_string(p, &p->value)),
+ 0);
+ return parse_begin(p, cur, end);
}
/* parse a literal header that is never indexed; index < 15 */
-static int
-parse_lithdr_nvridx (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_nvridx(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_value_string_with_indexed_key, finish_lithdr_nvridx
- };
+ parse_value_string_with_indexed_key, finish_lithdr_nvridx};
p->next_state = and_then;
p->index = (*cur) & 0xf;
- return parse_string_prefix (p, cur + 1, end);
+ return parse_string_prefix(p, cur + 1, end);
}
/* parse a literal header that is never indexed; index >= 15 */
-static int
-parse_lithdr_nvridx_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_nvridx_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_string_prefix, parse_value_string_with_indexed_key,
- finish_lithdr_nvridx
- };
+ parse_string_prefix, parse_value_string_with_indexed_key,
+ finish_lithdr_nvridx};
p->next_state = and_then;
p->index = 0xf;
p->parsing.value = &p->index;
- return parse_value0 (p, cur + 1, end);
+ return parse_value0(p, cur + 1, end);
}
/* parse a literal header that is never indexed; index == 0 */
-static int
-parse_lithdr_nvridx_v (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_lithdr_nvridx_v(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- parse_key_string, parse_string_prefix,
- parse_value_string_with_literal_key, finish_lithdr_nvridx_v
- };
+ parse_key_string, parse_string_prefix,
+ parse_value_string_with_literal_key, finish_lithdr_nvridx_v};
p->next_state = and_then;
- return parse_string_prefix (p, cur + 1, end);
+ return parse_string_prefix(p, cur + 1, end);
}
/* finish parsing a max table size change */
-static int
-finish_max_tbl_size (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- gpr_log (GPR_INFO, "MAX TABLE SIZE: %d", p->index);
- abort (); /* not implemented */
- return parse_begin (p, cur, end);
+static int finish_max_tbl_size(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ gpr_log(GPR_INFO, "MAX TABLE SIZE: %d", p->index);
+ abort(); /* not implemented */
+ return parse_begin(p, cur, end);
}
/* parse a max table size change, max size < 15 */
-static int
-parse_max_tbl_size (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_max_tbl_size(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
p->index = (*cur) & 0xf;
- return finish_max_tbl_size (p, cur + 1, end);
+ return finish_max_tbl_size(p, cur + 1, end);
}
/* parse a max table size change, max size >= 15 */
-static int
-parse_max_tbl_size_x (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_max_tbl_size_x(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
static const grpc_chttp2_hpack_parser_state and_then[] = {
- finish_max_tbl_size
- };
+ finish_max_tbl_size};
p->next_state = and_then;
p->index = 0xf;
p->parsing.value = &p->index;
- return parse_value0 (p, cur + 1, end);
+ return parse_value0(p, cur + 1, end);
}
/* a parse error: jam the parse state into parse_error, and return error */
-static int
-parse_error (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_error(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
p->state = parse_error;
return 0;
}
/* parse the 1st byte of a varint into p->parsing.value
no overflow is possible */
-static int
-parse_value0 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_value0;
- return 1;
- }
+static int parse_value0(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_value0;
+ return 1;
+ }
*p->parsing.value += (*cur) & 0x7f;
- if ((*cur) & 0x80)
- {
- return parse_value1 (p, cur + 1, end);
- }
- else
- {
- return parse_next (p, cur + 1, end);
- }
+ if ((*cur) & 0x80) {
+ return parse_value1(p, cur + 1, end);
+ } else {
+ return parse_next(p, cur + 1, end);
+ }
}
/* parse the 2nd byte of a varint into p->parsing.value
no overflow is possible */
-static int
-parse_value1 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_value1;
- return 1;
- }
-
- *p->parsing.value += (((gpr_uint32) * cur) & 0x7f) << 7;
-
- if ((*cur) & 0x80)
- {
- return parse_value2 (p, cur + 1, end);
- }
- else
- {
- return parse_next (p, cur + 1, end);
- }
+static int parse_value1(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_value1;
+ return 1;
+ }
+
+ *p->parsing.value += (((gpr_uint32)*cur) & 0x7f) << 7;
+
+ if ((*cur) & 0x80) {
+ return parse_value2(p, cur + 1, end);
+ } else {
+ return parse_next(p, cur + 1, end);
+ }
}
/* parse the 3rd byte of a varint into p->parsing.value
no overflow is possible */
-static int
-parse_value2 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_value2;
- return 1;
- }
-
- *p->parsing.value += (((gpr_uint32) * cur) & 0x7f) << 14;
-
- if ((*cur) & 0x80)
- {
- return parse_value3 (p, cur + 1, end);
- }
- else
- {
- return parse_next (p, cur + 1, end);
- }
+static int parse_value2(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_value2;
+ return 1;
+ }
+
+ *p->parsing.value += (((gpr_uint32)*cur) & 0x7f) << 14;
+
+ if ((*cur) & 0x80) {
+ return parse_value3(p, cur + 1, end);
+ } else {
+ return parse_next(p, cur + 1, end);
+ }
}
/* parse the 4th byte of a varint into p->parsing.value
no overflow is possible */
-static int
-parse_value3 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_value3;
- return 1;
- }
-
- *p->parsing.value += (((gpr_uint32) * cur) & 0x7f) << 21;
-
- if ((*cur) & 0x80)
- {
- return parse_value4 (p, cur + 1, end);
- }
- else
- {
- return parse_next (p, cur + 1, end);
- }
+static int parse_value3(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_value3;
+ return 1;
+ }
+
+ *p->parsing.value += (((gpr_uint32)*cur) & 0x7f) << 21;
+
+ if ((*cur) & 0x80) {
+ return parse_value4(p, cur + 1, end);
+ } else {
+ return parse_next(p, cur + 1, end);
+ }
}
/* parse the 5th byte of a varint into p->parsing.value
depending on the byte, we may overflow, and care must be taken */
-static int
-parse_value4 (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_value4(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
gpr_uint8 c;
gpr_uint32 cur_value;
gpr_uint32 add_value;
- if (cur == end)
- {
- p->state = parse_value4;
- return 1;
- }
+ if (cur == end) {
+ p->state = parse_value4;
+ return 1;
+ }
c = (*cur) & 0x7f;
- if (c > 0xf)
- {
- goto error;
- }
+ if (c > 0xf) {
+ goto error;
+ }
cur_value = *p->parsing.value;
- add_value = ((gpr_uint32) c) << 28;
- if (add_value > 0xffffffffu - cur_value)
- {
- goto error;
- }
+ add_value = ((gpr_uint32)c) << 28;
+ if (add_value > 0xffffffffu - cur_value) {
+ goto error;
+ }
*p->parsing.value = cur_value + add_value;
- if ((*cur) & 0x80)
- {
- return parse_value5up (p, cur + 1, end);
- }
- else
- {
- return parse_next (p, cur + 1, end);
- }
+ if ((*cur) & 0x80) {
+ return parse_value5up(p, cur + 1, end);
+ } else {
+ return parse_next(p, cur + 1, end);
+ }
error:
- gpr_log (GPR_ERROR, "integer overflow in hpack integer decoding: have 0x%08x, " "got byte 0x%02x", *p->parsing.value, *cur);
- return parse_error (p, cur, end);
+ gpr_log(GPR_ERROR,
+ "integer overflow in hpack integer decoding: have 0x%08x, "
+ "got byte 0x%02x",
+ *p->parsing.value, *cur);
+ return parse_error(p, cur, end);
}
/* parse any trailing bytes in a varint: it's possible to append an arbitrary
number of 0x80's and not affect the value - a zero will terminate - and
anything else will overflow */
-static int
-parse_value5up (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- while (cur != end && *cur == 0x80)
- {
- ++cur;
- }
-
- if (cur == end)
- {
- p->state = parse_value5up;
- return 1;
- }
-
- if (*cur == 0)
- {
- return parse_next (p, cur + 1, end);
- }
-
- gpr_log (GPR_ERROR, "integer overflow in hpack integer decoding: have 0x%08x, " "got byte 0x%02x sometime after byte 4");
- return parse_error (p, cur, end);
+static int parse_value5up(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ while (cur != end && *cur == 0x80) {
+ ++cur;
+ }
+
+ if (cur == end) {
+ p->state = parse_value5up;
+ return 1;
+ }
+
+ if (*cur == 0) {
+ return parse_next(p, cur + 1, end);
+ }
+
+ gpr_log(GPR_ERROR,
+ "integer overflow in hpack integer decoding: have 0x%08x, "
+ "got byte 0x%02x sometime after byte 4");
+ return parse_error(p, cur, end);
}
/* parse a string prefix */
-static int
-parse_string_prefix (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (cur == end)
- {
- p->state = parse_string_prefix;
- return 1;
- }
+static int parse_string_prefix(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur, const gpr_uint8 *end) {
+ if (cur == end) {
+ p->state = parse_string_prefix;
+ return 1;
+ }
p->strlen = (*cur) & 0x7f;
p->huff = (*cur) >> 7;
- if (p->strlen == 0x7f)
- {
- p->parsing.value = &p->strlen;
- return parse_value0 (p, cur + 1, end);
- }
- else
- {
- return parse_next (p, cur + 1, end);
- }
+ if (p->strlen == 0x7f) {
+ p->parsing.value = &p->strlen;
+ return parse_value0(p, cur + 1, end);
+ } else {
+ return parse_next(p, cur + 1, end);
+ }
}
/* append some bytes to a string */
-static void
-append_bytes (grpc_chttp2_hpack_parser_string * str, const gpr_uint8 * data, size_t length)
-{
- if (length + str->length > str->capacity)
- {
- GPR_ASSERT (str->length + length <= GPR_UINT32_MAX);
- str->capacity = (gpr_uint32) (str->length + length);
- str->str = gpr_realloc (str->str, str->capacity);
- }
- memcpy (str->str + str->length, data, length);
- GPR_ASSERT (length <= GPR_UINT32_MAX - str->length);
- str->length += (gpr_uint32) length;
+static void append_bytes(grpc_chttp2_hpack_parser_string *str,
+ const gpr_uint8 *data, size_t length) {
+ if (length + str->length > str->capacity) {
+ GPR_ASSERT(str->length + length <= GPR_UINT32_MAX);
+ str->capacity = (gpr_uint32)(str->length + length);
+ str->str = gpr_realloc(str->str, str->capacity);
+ }
+ memcpy(str->str + str->length, data, length);
+ GPR_ASSERT(length <= GPR_UINT32_MAX - str->length);
+ str->length += (gpr_uint32)length;
}
-static int
-append_string (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int append_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
grpc_chttp2_hpack_parser_string *str = p->parsing.str;
gpr_uint32 bits;
gpr_uint8 decoded[3];
- switch ((binary_state) p->binary)
- {
+ switch ((binary_state)p->binary) {
case NOT_BINARY:
- append_bytes (str, cur, (size_t) (end - cur));
+ append_bytes(str, cur, (size_t)(end - cur));
return 1;
b64_byte0:
case B64_BYTE0:
- if (cur == end)
- {
- p->binary = B64_BYTE0;
- return 1;
- }
+ if (cur == end) {
+ p->binary = B64_BYTE0;
+ return 1;
+ }
bits = inverse_base64[*cur];
++cur;
if (bits == 255)
- return 0;
+ return 0;
else if (bits == 64)
- goto b64_byte0;
+ goto b64_byte0;
p->base64_buffer = bits << 18;
- /* fallthrough */
+ /* fallthrough */
b64_byte1:
case B64_BYTE1:
- if (cur == end)
- {
- p->binary = B64_BYTE1;
- return 1;
- }
+ if (cur == end) {
+ p->binary = B64_BYTE1;
+ return 1;
+ }
bits = inverse_base64[*cur];
++cur;
if (bits == 255)
- return 0;
+ return 0;
else if (bits == 64)
- goto b64_byte1;
+ goto b64_byte1;
p->base64_buffer |= bits << 12;
- /* fallthrough */
+ /* fallthrough */
b64_byte2:
case B64_BYTE2:
- if (cur == end)
- {
- p->binary = B64_BYTE2;
- return 1;
- }
+ if (cur == end) {
+ p->binary = B64_BYTE2;
+ return 1;
+ }
bits = inverse_base64[*cur];
++cur;
if (bits == 255)
- return 0;
+ return 0;
else if (bits == 64)
- goto b64_byte2;
+ goto b64_byte2;
p->base64_buffer |= bits << 6;
- /* fallthrough */
+ /* fallthrough */
b64_byte3:
case B64_BYTE3:
- if (cur == end)
- {
- p->binary = B64_BYTE3;
- return 1;
- }
+ if (cur == end) {
+ p->binary = B64_BYTE3;
+ return 1;
+ }
bits = inverse_base64[*cur];
++cur;
if (bits == 255)
- return 0;
+ return 0;
else if (bits == 64)
- goto b64_byte3;
+ goto b64_byte3;
p->base64_buffer |= bits;
bits = p->base64_buffer;
- decoded[0] = (gpr_uint8) (bits >> 16);
- decoded[1] = (gpr_uint8) (bits >> 8);
- decoded[2] = (gpr_uint8) (bits);
- append_bytes (str, decoded, 3);
+ decoded[0] = (gpr_uint8)(bits >> 16);
+ decoded[1] = (gpr_uint8)(bits >> 8);
+ decoded[2] = (gpr_uint8)(bits);
+ append_bytes(str, decoded, 3);
goto b64_byte0;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
return 1;
}
/* append a null terminator to a string */
-static int
-finish_str (grpc_chttp2_hpack_parser * p)
-{
+static int finish_str(grpc_chttp2_hpack_parser *p) {
gpr_uint8 terminator = 0;
gpr_uint8 decoded[2];
gpr_uint32 bits;
grpc_chttp2_hpack_parser_string *str = p->parsing.str;
- switch ((binary_state) p->binary)
- {
+ switch ((binary_state)p->binary) {
case NOT_BINARY:
break;
case B64_BYTE0:
break;
case B64_BYTE1:
- gpr_log (GPR_ERROR, "illegal base64 encoding");
- return 0; /* illegal encoding */
+ gpr_log(GPR_ERROR, "illegal base64 encoding");
+ return 0; /* illegal encoding */
case B64_BYTE2:
bits = p->base64_buffer;
- if (bits & 0xffff)
- {
- gpr_log (GPR_ERROR, "trailing bits in base64 encoding: 0x%04x", bits & 0xffff);
- return 0;
- }
- decoded[0] = (gpr_uint8) (bits >> 16);
- append_bytes (str, decoded, 1);
+ if (bits & 0xffff) {
+ gpr_log(GPR_ERROR, "trailing bits in base64 encoding: 0x%04x",
+ bits & 0xffff);
+ return 0;
+ }
+ decoded[0] = (gpr_uint8)(bits >> 16);
+ append_bytes(str, decoded, 1);
break;
case B64_BYTE3:
bits = p->base64_buffer;
- if (bits & 0xff)
- {
- gpr_log (GPR_ERROR, "trailing bits in base64 encoding: 0x%02x", bits & 0xff);
- return 0;
- }
- decoded[0] = (gpr_uint8) (bits >> 16);
- decoded[1] = (gpr_uint8) (bits >> 8);
- append_bytes (str, decoded, 2);
+ if (bits & 0xff) {
+ gpr_log(GPR_ERROR, "trailing bits in base64 encoding: 0x%02x",
+ bits & 0xff);
+ return 0;
+ }
+ decoded[0] = (gpr_uint8)(bits >> 16);
+ decoded[1] = (gpr_uint8)(bits >> 8);
+ append_bytes(str, decoded, 2);
break;
- }
- append_bytes (str, &terminator, 1);
- p->parsing.str->length--; /* don't actually count the null terminator */
+ }
+ append_bytes(str, &terminator, 1);
+ p->parsing.str->length--; /* don't actually count the null terminator */
return 1;
}
/* decode a nibble from a huffman encoded stream */
-static int
-huff_nibble (grpc_chttp2_hpack_parser * p, gpr_uint8 nibble)
-{
+static int huff_nibble(grpc_chttp2_hpack_parser *p, gpr_uint8 nibble) {
gpr_int16 emit = emit_sub_tbl[16 * emit_tbl[p->huff_state] + nibble];
gpr_int16 next = next_sub_tbl[16 * next_tbl[p->huff_state] + nibble];
- if (emit != -1)
- {
- if (emit >= 0 && emit < 256)
- {
- gpr_uint8 c = (gpr_uint8) emit;
- if (!append_string (p, &c, (&c) + 1))
- return 0;
- }
- else
- {
- assert (emit == 256);
- }
+ if (emit != -1) {
+ if (emit >= 0 && emit < 256) {
+ gpr_uint8 c = (gpr_uint8)emit;
+ if (!append_string(p, &c, (&c) + 1)) return 0;
+ } else {
+ assert(emit == 256);
}
+ }
p->huff_state = next;
return 1;
}
/* decode full bytes from a huffman encoded stream */
-static int
-add_huff_bytes (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- for (; cur != end; ++cur)
- {
- if (!huff_nibble (p, *cur >> 4) || !huff_nibble (p, *cur & 0xf))
- return 0;
- }
+static int add_huff_bytes(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ for (; cur != end; ++cur) {
+ if (!huff_nibble(p, *cur >> 4) || !huff_nibble(p, *cur & 0xf)) return 0;
+ }
return 1;
}
/* decode some string bytes based on the current decoding mode
(huffman or not) */
-static int
-add_str_bytes (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- if (p->huff)
- {
- return add_huff_bytes (p, cur, end);
- }
- else
- {
- return append_string (p, cur, end);
- }
+static int add_str_bytes(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ if (p->huff) {
+ return add_huff_bytes(p, cur, end);
+ } else {
+ return append_string(p, cur, end);
+ }
}
/* parse a string - tries to do large chunks at a time */
-static int
-parse_string (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
+static int parse_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
size_t remaining = p->strlen - p->strgot;
- size_t given = (size_t) (end - cur);
- if (remaining <= given)
- {
- return add_str_bytes (p, cur, cur + remaining) && finish_str (p) && parse_next (p, cur + remaining, end);
- }
- else
- {
- if (!add_str_bytes (p, cur, cur + given))
- return 0;
- GPR_ASSERT (given <= GPR_UINT32_MAX - p->strgot);
- p->strgot += (gpr_uint32) given;
- p->state = parse_string;
- return 1;
- }
+ size_t given = (size_t)(end - cur);
+ if (remaining <= given) {
+ return add_str_bytes(p, cur, cur + remaining) && finish_str(p) &&
+ parse_next(p, cur + remaining, end);
+ } else {
+ if (!add_str_bytes(p, cur, cur + given)) return 0;
+ GPR_ASSERT(given <= GPR_UINT32_MAX - p->strgot);
+ p->strgot += (gpr_uint32)given;
+ p->state = parse_string;
+ return 1;
+ }
}
/* begin parsing a string - performs setup, calls parse_string */
-static int
-begin_parse_string (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end, gpr_uint8 binary, grpc_chttp2_hpack_parser_string * str)
-{
+static int begin_parse_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end, gpr_uint8 binary,
+ grpc_chttp2_hpack_parser_string *str) {
p->strgot = 0;
str->length = 0;
p->parsing.str = str;
p->huff_state = 0;
p->binary = binary;
- return parse_string (p, cur, end);
+ return parse_string(p, cur, end);
}
/* parse the key string */
-static int
-parse_key_string (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- return begin_parse_string (p, cur, end, NOT_BINARY, &p->key);
+static int parse_key_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ return begin_parse_string(p, cur, end, NOT_BINARY, &p->key);
}
/* check if a key represents a binary header or not */
-typedef enum
-{ BINARY_HEADER, PLAINTEXT_HEADER, ERROR_HEADER } is_binary_header;
+typedef enum { BINARY_HEADER, PLAINTEXT_HEADER, ERROR_HEADER } is_binary_header;
-static is_binary_header
-is_binary_literal_header (grpc_chttp2_hpack_parser * p)
-{
- return grpc_is_binary_header (p->key.str, p->key.length) ? BINARY_HEADER : PLAINTEXT_HEADER;
+static is_binary_header is_binary_literal_header(grpc_chttp2_hpack_parser *p) {
+ return grpc_is_binary_header(p->key.str, p->key.length) ? BINARY_HEADER
+ : PLAINTEXT_HEADER;
}
-static is_binary_header
-is_binary_indexed_header (grpc_chttp2_hpack_parser * p)
-{
- grpc_mdelem *elem = grpc_chttp2_hptbl_lookup (&p->table, p->index);
- if (!elem)
- return ERROR_HEADER;
- return grpc_is_binary_header ((const char *) GPR_SLICE_START_PTR (elem->key->slice), GPR_SLICE_LENGTH (elem->key->slice)) ? BINARY_HEADER : PLAINTEXT_HEADER;
+static is_binary_header is_binary_indexed_header(grpc_chttp2_hpack_parser *p) {
+ grpc_mdelem *elem = grpc_chttp2_hptbl_lookup(&p->table, p->index);
+ if (!elem) return ERROR_HEADER;
+ return grpc_is_binary_header(
+ (const char *)GPR_SLICE_START_PTR(elem->key->slice),
+ GPR_SLICE_LENGTH(elem->key->slice))
+ ? BINARY_HEADER
+ : PLAINTEXT_HEADER;
}
/* parse the value string */
-static int
-parse_value_string (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end, is_binary_header type)
-{
- switch (type)
- {
+static int parse_value_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur,
+ const gpr_uint8 *end, is_binary_header type) {
+ switch (type) {
case BINARY_HEADER:
- return begin_parse_string (p, cur, end, B64_BYTE0, &p->value);
+ return begin_parse_string(p, cur, end, B64_BYTE0, &p->value);
case PLAINTEXT_HEADER:
- return begin_parse_string (p, cur, end, NOT_BINARY, &p->value);
+ return begin_parse_string(p, cur, end, NOT_BINARY, &p->value);
case ERROR_HEADER:
return 0;
- }
+ }
/* Add code to prevent return without value error */
- gpr_log (GPR_ERROR, "Should never reach beyond switch in parse_value_string");
- abort ();
+ gpr_log(GPR_ERROR, "Should never reach beyond switch in parse_value_string");
+ abort();
return 0;
}
-static int
-parse_value_string_with_indexed_key (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- return parse_value_string (p, cur, end, is_binary_indexed_header (p));
+static int parse_value_string_with_indexed_key(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ return parse_value_string(p, cur, end, is_binary_indexed_header(p));
}
-static int
-parse_value_string_with_literal_key (grpc_chttp2_hpack_parser * p, const gpr_uint8 * cur, const gpr_uint8 * end)
-{
- return parse_value_string (p, cur, end, is_binary_literal_header (p));
+static int parse_value_string_with_literal_key(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *cur,
+ const gpr_uint8 *end) {
+ return parse_value_string(p, cur, end, is_binary_literal_header(p));
}
/* PUBLIC INTERFACE */
-static void
-on_header_not_set (void *user_data, grpc_mdelem * md)
-{
- char *keyhex = gpr_dump_slice (md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
- char *valuehex = gpr_dump_slice (md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
- gpr_log (GPR_ERROR, "on_header callback not set; key=%s value=%s", keyhex, valuehex);
- gpr_free (keyhex);
- gpr_free (valuehex);
- GRPC_MDELEM_UNREF (md);
- abort ();
+static void on_header_not_set(void *user_data, grpc_mdelem *md) {
+ char *keyhex = gpr_dump_slice(md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ char *valuehex =
+ gpr_dump_slice(md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ gpr_log(GPR_ERROR, "on_header callback not set; key=%s value=%s", keyhex,
+ valuehex);
+ gpr_free(keyhex);
+ gpr_free(valuehex);
+ GRPC_MDELEM_UNREF(md);
+ abort();
}
-void
-grpc_chttp2_hpack_parser_init (grpc_chttp2_hpack_parser * p, grpc_mdctx * mdctx)
-{
+void grpc_chttp2_hpack_parser_init(grpc_chttp2_hpack_parser *p,
+ grpc_mdctx *mdctx) {
p->on_header = on_header_not_set;
p->on_header_user_data = NULL;
p->state = parse_begin;
@@ -1429,62 +1357,58 @@ grpc_chttp2_hpack_parser_init (grpc_chttp2_hpack_parser * p, grpc_mdctx * mdctx)
p->value.str = NULL;
p->value.capacity = 0;
p->value.length = 0;
- grpc_chttp2_hptbl_init (&p->table, mdctx);
+ grpc_chttp2_hptbl_init(&p->table, mdctx);
}
-void
-grpc_chttp2_hpack_parser_set_has_priority (grpc_chttp2_hpack_parser * p)
-{
+void grpc_chttp2_hpack_parser_set_has_priority(grpc_chttp2_hpack_parser *p) {
p->after_prioritization = p->state;
p->state = parse_stream_dep0;
}
-void
-grpc_chttp2_hpack_parser_destroy (grpc_chttp2_hpack_parser * p)
-{
- grpc_chttp2_hptbl_destroy (&p->table);
- gpr_free (p->key.str);
- gpr_free (p->value.str);
+void grpc_chttp2_hpack_parser_destroy(grpc_chttp2_hpack_parser *p) {
+ grpc_chttp2_hptbl_destroy(&p->table);
+ gpr_free(p->key.str);
+ gpr_free(p->value.str);
}
-int
-grpc_chttp2_hpack_parser_parse (grpc_chttp2_hpack_parser * p, const gpr_uint8 * beg, const gpr_uint8 * end)
-{
+int grpc_chttp2_hpack_parser_parse(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *beg, const gpr_uint8 *end) {
/* TODO(ctiller): limit the distance of end from beg, and perform multiple
steps in the event of a large chunk of data to limit
stack space usage when no tail call optimization is
available */
- return p->state (p, beg, end);
+ return p->state(p, beg, end);
}
-grpc_chttp2_parse_error
-grpc_chttp2_header_parser_parse (grpc_exec_ctx * exec_ctx, void *hpack_parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
+grpc_chttp2_parse_error grpc_chttp2_header_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *hpack_parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
grpc_chttp2_hpack_parser *parser = hpack_parser;
- if (!grpc_chttp2_hpack_parser_parse (parser, GPR_SLICE_START_PTR (slice), GPR_SLICE_END_PTR (slice)))
- {
+ if (!grpc_chttp2_hpack_parser_parse(parser, GPR_SLICE_START_PTR(slice),
+ GPR_SLICE_END_PTR(slice))) {
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
+ if (is_last) {
+ if (parser->is_boundary && parser->state != parse_begin) {
+ gpr_log(GPR_ERROR,
+ "end of header frame not aligned with a hpack record boundary");
return GRPC_CHTTP2_CONNECTION_ERROR;
}
- if (is_last)
- {
- if (parser->is_boundary && parser->state != parse_begin)
- {
- gpr_log (GPR_ERROR, "end of header frame not aligned with a hpack record boundary");
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
- if (parser->is_boundary)
- {
- grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into (&stream_parsing->incoming_metadata, &stream_parsing->data_parser.incoming_sopb);
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
- }
- if (parser->is_eof)
- {
- stream_parsing->received_close = 1;
- }
- parser->on_header = on_header_not_set;
- parser->on_header_user_data = NULL;
- parser->is_boundary = 0xde;
- parser->is_eof = 0xde;
+ if (parser->is_boundary) {
+ grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into(
+ &stream_parsing->incoming_metadata,
+ &stream_parsing->data_parser.incoming_sopb);
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
+ stream_parsing);
+ }
+ if (parser->is_eof) {
+ stream_parsing->received_close = 1;
}
+ parser->on_header = on_header_not_set;
+ parser->on_header_user_data = NULL;
+ parser->is_boundary = 0xde;
+ parser->is_eof = 0xde;
+ }
return GRPC_CHTTP2_PARSE_OK;
}
diff --git a/src/core/transport/chttp2/hpack_parser.h b/src/core/transport/chttp2/hpack_parser.h
index 209bb6b9c1..f56867016c 100644
--- a/src/core/transport/chttp2/hpack_parser.h
+++ b/src/core/transport/chttp2/hpack_parser.h
@@ -44,19 +44,19 @@
typedef struct grpc_chttp2_hpack_parser grpc_chttp2_hpack_parser;
-typedef int (*grpc_chttp2_hpack_parser_state) (grpc_chttp2_hpack_parser * p, const gpr_uint8 * beg, const gpr_uint8 * end);
+typedef int (*grpc_chttp2_hpack_parser_state)(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *beg,
+ const gpr_uint8 *end);
-typedef struct
-{
+typedef struct {
char *str;
gpr_uint32 length;
gpr_uint32 capacity;
} grpc_chttp2_hpack_parser_string;
-struct grpc_chttp2_hpack_parser
-{
+struct grpc_chttp2_hpack_parser {
/* user specified callback for each header output */
- void (*on_header) (void *user_data, grpc_mdelem * md);
+ void (*on_header)(void *user_data, grpc_mdelem *md);
void *on_header_user_data;
/* current parse state - or a function that implements it */
@@ -66,8 +66,7 @@ struct grpc_chttp2_hpack_parser
/* what to do after skipping prioritization data */
grpc_chttp2_hpack_parser_state after_prioritization;
/* the value we're currently parsing */
- union
- {
+ union {
gpr_uint32 *value;
grpc_chttp2_hpack_parser_string *str;
} parsing;
@@ -96,16 +95,21 @@ struct grpc_chttp2_hpack_parser
grpc_chttp2_hptbl table;
};
-void grpc_chttp2_hpack_parser_init (grpc_chttp2_hpack_parser * p, grpc_mdctx * mdctx);
-void grpc_chttp2_hpack_parser_destroy (grpc_chttp2_hpack_parser * p);
+void grpc_chttp2_hpack_parser_init(grpc_chttp2_hpack_parser *p,
+ grpc_mdctx *mdctx);
+void grpc_chttp2_hpack_parser_destroy(grpc_chttp2_hpack_parser *p);
-void grpc_chttp2_hpack_parser_set_has_priority (grpc_chttp2_hpack_parser * p);
+void grpc_chttp2_hpack_parser_set_has_priority(grpc_chttp2_hpack_parser *p);
/* returns 1 on success, 0 on error */
-int grpc_chttp2_hpack_parser_parse (grpc_chttp2_hpack_parser * p, const gpr_uint8 * beg, const gpr_uint8 * end);
+int grpc_chttp2_hpack_parser_parse(grpc_chttp2_hpack_parser *p,
+ const gpr_uint8 *beg, const gpr_uint8 *end);
/* wraps grpc_chttp2_hpack_parser_parse to provide a frame level parser for
the transport */
-grpc_chttp2_parse_error grpc_chttp2_header_parser_parse (grpc_exec_ctx * exec_ctx, void *hpack_parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+grpc_chttp2_parse_error grpc_chttp2_header_parser_parse(
+ grpc_exec_ctx *exec_ctx, void *hpack_parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HPACK_PARSER_H */
diff --git a/src/core/transport/chttp2/hpack_table.c b/src/core/transport/chttp2/hpack_table.c
index 099766c85e..d5cb752789 100644
--- a/src/core/transport/chttp2/hpack_table.c
+++ b/src/core/transport/chttp2/hpack_table.c
@@ -39,329 +39,253 @@
#include <grpc/support/log.h>
#include "src/core/support/murmur_hash.h"
-static struct
-{
+static struct {
const char *key;
const char *value;
-} static_table[] =
-{
- /* 0: */
- {
- NULL, NULL},
+} static_table[] = {
+ /* 0: */
+ {NULL, NULL},
/* 1: */
- {
- ":authority", ""},
+ {":authority", ""},
/* 2: */
- {
- ":method", "GET"},
+ {":method", "GET"},
/* 3: */
- {
- ":method", "POST"},
+ {":method", "POST"},
/* 4: */
- {
- ":path", "/"},
+ {":path", "/"},
/* 5: */
- {
- ":path", "/index.html"},
+ {":path", "/index.html"},
/* 6: */
- {
- ":scheme", "http"},
+ {":scheme", "http"},
/* 7: */
- {
- ":scheme", "https"},
+ {":scheme", "https"},
/* 8: */
- {
- ":status", "200"},
+ {":status", "200"},
/* 9: */
- {
- ":status", "204"},
+ {":status", "204"},
/* 10: */
- {
- ":status", "206"},
+ {":status", "206"},
/* 11: */
- {
- ":status", "304"},
+ {":status", "304"},
/* 12: */
- {
- ":status", "400"},
+ {":status", "400"},
/* 13: */
- {
- ":status", "404"},
+ {":status", "404"},
/* 14: */
- {
- ":status", "500"},
+ {":status", "500"},
/* 15: */
- {
- "accept-charset", ""},
+ {"accept-charset", ""},
/* 16: */
- {
- "accept-encoding", "gzip, deflate"},
+ {"accept-encoding", "gzip, deflate"},
/* 17: */
- {
- "accept-language", ""},
+ {"accept-language", ""},
/* 18: */
- {
- "accept-ranges", ""},
+ {"accept-ranges", ""},
/* 19: */
- {
- "accept", ""},
+ {"accept", ""},
/* 20: */
- {
- "access-control-allow-origin", ""},
+ {"access-control-allow-origin", ""},
/* 21: */
- {
- "age", ""},
+ {"age", ""},
/* 22: */
- {
- "allow", ""},
+ {"allow", ""},
/* 23: */
- {
- "authorization", ""},
+ {"authorization", ""},
/* 24: */
- {
- "cache-control", ""},
+ {"cache-control", ""},
/* 25: */
- {
- "content-disposition", ""},
+ {"content-disposition", ""},
/* 26: */
- {
- "content-encoding", ""},
+ {"content-encoding", ""},
/* 27: */
- {
- "content-language", ""},
+ {"content-language", ""},
/* 28: */
- {
- "content-length", ""},
+ {"content-length", ""},
/* 29: */
- {
- "content-location", ""},
+ {"content-location", ""},
/* 30: */
- {
- "content-range", ""},
+ {"content-range", ""},
/* 31: */
- {
- "content-type", ""},
+ {"content-type", ""},
/* 32: */
- {
- "cookie", ""},
+ {"cookie", ""},
/* 33: */
- {
- "date", ""},
+ {"date", ""},
/* 34: */
- {
- "etag", ""},
+ {"etag", ""},
/* 35: */
- {
- "expect", ""},
+ {"expect", ""},
/* 36: */
- {
- "expires", ""},
+ {"expires", ""},
/* 37: */
- {
- "from", ""},
+ {"from", ""},
/* 38: */
- {
- "host", ""},
+ {"host", ""},
/* 39: */
- {
- "if-match", ""},
+ {"if-match", ""},
/* 40: */
- {
- "if-modified-since", ""},
+ {"if-modified-since", ""},
/* 41: */
- {
- "if-none-match", ""},
+ {"if-none-match", ""},
/* 42: */
- {
- "if-range", ""},
+ {"if-range", ""},
/* 43: */
- {
- "if-unmodified-since", ""},
+ {"if-unmodified-since", ""},
/* 44: */
- {
- "last-modified", ""},
+ {"last-modified", ""},
/* 45: */
- {
- "link", ""},
+ {"link", ""},
/* 46: */
- {
- "location", ""},
+ {"location", ""},
/* 47: */
- {
- "max-forwards", ""},
+ {"max-forwards", ""},
/* 48: */
- {
- "proxy-authenticate", ""},
+ {"proxy-authenticate", ""},
/* 49: */
- {
- "proxy-authorization", ""},
+ {"proxy-authorization", ""},
/* 50: */
- {
- "range", ""},
+ {"range", ""},
/* 51: */
- {
- "referer", ""},
+ {"referer", ""},
/* 52: */
- {
- "refresh", ""},
+ {"refresh", ""},
/* 53: */
- {
- "retry-after", ""},
+ {"retry-after", ""},
/* 54: */
- {
- "server", ""},
+ {"server", ""},
/* 55: */
- {
- "set-cookie", ""},
+ {"set-cookie", ""},
/* 56: */
- {
- "strict-transport-security", ""},
+ {"strict-transport-security", ""},
/* 57: */
- {
- "transfer-encoding", ""},
+ {"transfer-encoding", ""},
/* 58: */
- {
- "user-agent", ""},
+ {"user-agent", ""},
/* 59: */
- {
- "vary", ""},
+ {"vary", ""},
/* 60: */
- {
- "via", ""},
+ {"via", ""},
/* 61: */
- {
-"www-authenticate", ""},};
+ {"www-authenticate", ""},
+};
-void
-grpc_chttp2_hptbl_init (grpc_chttp2_hptbl * tbl, grpc_mdctx * mdctx)
-{
+void grpc_chttp2_hptbl_init(grpc_chttp2_hptbl *tbl, grpc_mdctx *mdctx) {
size_t i;
- memset (tbl, 0, sizeof (*tbl));
+ memset(tbl, 0, sizeof(*tbl));
tbl->mdctx = mdctx;
tbl->max_bytes = GRPC_CHTTP2_INITIAL_HPACK_TABLE_SIZE;
- for (i = 1; i <= GRPC_CHTTP2_LAST_STATIC_ENTRY; i++)
- {
- tbl->static_ents[i - 1] = grpc_mdelem_from_strings (mdctx, static_table[i].key, static_table[i].value);
- }
+ for (i = 1; i <= GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
+ tbl->static_ents[i - 1] = grpc_mdelem_from_strings(
+ mdctx, static_table[i].key, static_table[i].value);
+ }
}
-void
-grpc_chttp2_hptbl_destroy (grpc_chttp2_hptbl * tbl)
-{
+void grpc_chttp2_hptbl_destroy(grpc_chttp2_hptbl *tbl) {
size_t i;
- for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++)
- {
- GRPC_MDELEM_UNREF (tbl->static_ents[i]);
- }
- for (i = 0; i < tbl->num_ents; i++)
- {
- GRPC_MDELEM_UNREF (tbl->ents[(tbl->first_ent + i) % GRPC_CHTTP2_MAX_TABLE_COUNT]);
- }
+ for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
+ GRPC_MDELEM_UNREF(tbl->static_ents[i]);
+ }
+ for (i = 0; i < tbl->num_ents; i++) {
+ GRPC_MDELEM_UNREF(
+ tbl->ents[(tbl->first_ent + i) % GRPC_CHTTP2_MAX_TABLE_COUNT]);
+ }
}
-grpc_mdelem *
-grpc_chttp2_hptbl_lookup (const grpc_chttp2_hptbl * tbl, gpr_uint32 index)
-{
+grpc_mdelem *grpc_chttp2_hptbl_lookup(const grpc_chttp2_hptbl *tbl,
+ gpr_uint32 index) {
/* Static table comes first, just return an entry from it */
- if (index <= GRPC_CHTTP2_LAST_STATIC_ENTRY)
- {
- return tbl->static_ents[index - 1];
- }
+ if (index <= GRPC_CHTTP2_LAST_STATIC_ENTRY) {
+ return tbl->static_ents[index - 1];
+ }
/* Otherwise, find the value in the list of valid entries */
index -= (GRPC_CHTTP2_LAST_STATIC_ENTRY + 1);
- if (index < tbl->num_ents)
- {
- gpr_uint32 offset = (tbl->num_ents - 1u - index + tbl->first_ent) % GRPC_CHTTP2_MAX_TABLE_COUNT;
- return tbl->ents[offset];
- }
+ if (index < tbl->num_ents) {
+ gpr_uint32 offset = (tbl->num_ents - 1u - index + tbl->first_ent) %
+ GRPC_CHTTP2_MAX_TABLE_COUNT;
+ return tbl->ents[offset];
+ }
/* Invalid entry: return error */
return NULL;
}
/* Evict one element from the table */
-static void
-evict1 (grpc_chttp2_hptbl * tbl)
-{
+static void evict1(grpc_chttp2_hptbl *tbl) {
grpc_mdelem *first_ent = tbl->ents[tbl->first_ent];
- size_t elem_bytes = GPR_SLICE_LENGTH (first_ent->key->slice) + GPR_SLICE_LENGTH (first_ent->value->slice) + GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
- GPR_ASSERT (elem_bytes <= tbl->mem_used);
- tbl->mem_used = (gpr_uint16) (tbl->mem_used - elem_bytes);
- tbl->first_ent = (gpr_uint16) ((tbl->first_ent + 1) % GRPC_CHTTP2_MAX_TABLE_COUNT);
+ size_t elem_bytes = GPR_SLICE_LENGTH(first_ent->key->slice) +
+ GPR_SLICE_LENGTH(first_ent->value->slice) +
+ GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
+ GPR_ASSERT(elem_bytes <= tbl->mem_used);
+ tbl->mem_used = (gpr_uint16)(tbl->mem_used - elem_bytes);
+ tbl->first_ent =
+ (gpr_uint16)((tbl->first_ent + 1) % GRPC_CHTTP2_MAX_TABLE_COUNT);
tbl->num_ents--;
- GRPC_MDELEM_UNREF (first_ent);
+ GRPC_MDELEM_UNREF(first_ent);
}
-void
-grpc_chttp2_hptbl_add (grpc_chttp2_hptbl * tbl, grpc_mdelem * md)
-{
+void grpc_chttp2_hptbl_add(grpc_chttp2_hptbl *tbl, grpc_mdelem *md) {
/* determine how many bytes of buffer this entry represents */
- size_t elem_bytes = GPR_SLICE_LENGTH (md->key->slice) + GPR_SLICE_LENGTH (md->value->slice) + GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
+ size_t elem_bytes = GPR_SLICE_LENGTH(md->key->slice) +
+ GPR_SLICE_LENGTH(md->value->slice) +
+ GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
/* we can't add elements bigger than the max table size */
- if (elem_bytes > tbl->max_bytes)
- {
- /* HPACK draft 10 section 4.4 states:
- * If the size of the new entry is less than or equal to the maximum
- * size, that entry is added to the table. It is not an error to
- * attempt to add an entry that is larger than the maximum size; an
- * attempt to add an entry larger than the entire table causes
- * the table
- * to be emptied of all existing entries, and results in an
- * empty table.
- */
- while (tbl->num_ents)
- {
- evict1 (tbl);
- }
- return;
+ if (elem_bytes > tbl->max_bytes) {
+ /* HPACK draft 10 section 4.4 states:
+ * If the size of the new entry is less than or equal to the maximum
+ * size, that entry is added to the table. It is not an error to
+ * attempt to add an entry that is larger than the maximum size; an
+ * attempt to add an entry larger than the entire table causes
+ * the table
+ * to be emptied of all existing entries, and results in an
+ * empty table.
+ */
+ while (tbl->num_ents) {
+ evict1(tbl);
}
+ return;
+ }
/* evict entries to ensure no overflow */
- while (elem_bytes > (size_t) tbl->max_bytes - tbl->mem_used)
- {
- evict1 (tbl);
- }
+ while (elem_bytes > (size_t)tbl->max_bytes - tbl->mem_used) {
+ evict1(tbl);
+ }
/* copy the finalized entry in */
tbl->ents[tbl->last_ent] = md;
/* update accounting values */
- tbl->last_ent = (gpr_uint16) ((tbl->last_ent + 1) % GRPC_CHTTP2_MAX_TABLE_COUNT);
+ tbl->last_ent =
+ (gpr_uint16)((tbl->last_ent + 1) % GRPC_CHTTP2_MAX_TABLE_COUNT);
tbl->num_ents++;
- tbl->mem_used = (gpr_uint16) (tbl->mem_used + elem_bytes);
+ tbl->mem_used = (gpr_uint16)(tbl->mem_used + elem_bytes);
}
-grpc_chttp2_hptbl_find_result
-grpc_chttp2_hptbl_find (const grpc_chttp2_hptbl * tbl, grpc_mdelem * md)
-{
- grpc_chttp2_hptbl_find_result r = { 0, 0 };
+grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find(
+ const grpc_chttp2_hptbl *tbl, grpc_mdelem *md) {
+ grpc_chttp2_hptbl_find_result r = {0, 0};
gpr_uint16 i;
/* See if the string is in the static table */
- for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++)
- {
- grpc_mdelem *ent = tbl->static_ents[i];
- if (md->key != ent->key)
- continue;
- r.index = (gpr_uint16) (i + 1);
- r.has_value = md->value == ent->value;
- if (r.has_value)
- return r;
- }
+ for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
+ grpc_mdelem *ent = tbl->static_ents[i];
+ if (md->key != ent->key) continue;
+ r.index = (gpr_uint16)(i + 1);
+ r.has_value = md->value == ent->value;
+ if (r.has_value) return r;
+ }
/* Scan the dynamic table */
- for (i = 0; i < tbl->num_ents; i++)
- {
- gpr_uint16 idx = (gpr_uint16) (tbl->num_ents - i + GRPC_CHTTP2_LAST_STATIC_ENTRY);
- grpc_mdelem *ent = tbl->ents[(tbl->first_ent + i) % GRPC_CHTTP2_MAX_TABLE_COUNT];
- if (md->key != ent->key)
- continue;
- r.index = idx;
- r.has_value = md->value == ent->value;
- if (r.has_value)
- return r;
- }
+ for (i = 0; i < tbl->num_ents; i++) {
+ gpr_uint16 idx =
+ (gpr_uint16)(tbl->num_ents - i + GRPC_CHTTP2_LAST_STATIC_ENTRY);
+ grpc_mdelem *ent =
+ tbl->ents[(tbl->first_ent + i) % GRPC_CHTTP2_MAX_TABLE_COUNT];
+ if (md->key != ent->key) continue;
+ r.index = idx;
+ r.has_value = md->value == ent->value;
+ if (r.has_value) return r;
+ }
return r;
}
diff --git a/src/core/transport/chttp2/hpack_table.h b/src/core/transport/chttp2/hpack_table.h
index 5c58a35186..4f882e2e03 100644
--- a/src/core/transport/chttp2/hpack_table.h
+++ b/src/core/transport/chttp2/hpack_table.h
@@ -56,8 +56,7 @@
GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD)
/* hpack decoder table */
-typedef struct
-{
+typedef struct {
grpc_mdctx *mdctx;
/* the first used entry in ents */
gpr_uint16 first_ent;
@@ -78,20 +77,21 @@ typedef struct
} grpc_chttp2_hptbl;
/* initialize a hpack table */
-void grpc_chttp2_hptbl_init (grpc_chttp2_hptbl * tbl, grpc_mdctx * mdctx);
-void grpc_chttp2_hptbl_destroy (grpc_chttp2_hptbl * tbl);
+void grpc_chttp2_hptbl_init(grpc_chttp2_hptbl *tbl, grpc_mdctx *mdctx);
+void grpc_chttp2_hptbl_destroy(grpc_chttp2_hptbl *tbl);
/* lookup a table entry based on its hpack index */
-grpc_mdelem *grpc_chttp2_hptbl_lookup (const grpc_chttp2_hptbl * tbl, gpr_uint32 index);
+grpc_mdelem *grpc_chttp2_hptbl_lookup(const grpc_chttp2_hptbl *tbl,
+ gpr_uint32 index);
/* add a table entry to the index */
-void grpc_chttp2_hptbl_add (grpc_chttp2_hptbl * tbl, grpc_mdelem * md);
+void grpc_chttp2_hptbl_add(grpc_chttp2_hptbl *tbl, grpc_mdelem *md);
/* Find a key/value pair in the table... returns the index in the table of the
most similar entry, or 0 if the value was not found */
-typedef struct
-{
+typedef struct {
gpr_uint16 index;
gpr_uint8 has_value;
} grpc_chttp2_hptbl_find_result;
-grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find (const grpc_chttp2_hptbl * tbl, grpc_mdelem * md);
+grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find(
+ const grpc_chttp2_hptbl *tbl, grpc_mdelem *md);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HPACK_TABLE_H */
diff --git a/src/core/transport/chttp2/http2_errors.h b/src/core/transport/chttp2/http2_errors.h
index 8385d6ba44..a4f309e056 100644
--- a/src/core/transport/chttp2/http2_errors.h
+++ b/src/core/transport/chttp2/http2_errors.h
@@ -35,8 +35,7 @@
#define GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_HTTP2_ERRORS_H
/* error codes for RST_STREAM from http2 draft 14 section 7 */
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_NO_ERROR = 0x0,
GRPC_CHTTP2_PROTOCOL_ERROR = 0x1,
GRPC_CHTTP2_INTERNAL_ERROR = 0x2,
diff --git a/src/core/transport/chttp2/huffsyms.c b/src/core/transport/chttp2/huffsyms.c
index 058c6ee61e..6f5cf6a2a9 100644
--- a/src/core/transport/chttp2/huffsyms.c
+++ b/src/core/transport/chttp2/huffsyms.c
@@ -37,69 +37,69 @@
command:
:%s/.* \([0-9a-f]\+\) \[ *\([0-9]\+\)\]/{0x\1, \2},/g */
const grpc_chttp2_huffsym grpc_chttp2_huffsyms[GRPC_CHTTP2_NUM_HUFFSYMS] = {
- {0x1ff8, 13}, {0x7fffd8, 23}, {0xfffffe2, 28}, {0xfffffe3, 28},
- {0xfffffe4, 28}, {0xfffffe5, 28}, {0xfffffe6, 28}, {0xfffffe7, 28},
- {0xfffffe8, 28}, {0xffffea, 24}, {0x3ffffffc, 30}, {0xfffffe9, 28},
- {0xfffffea, 28}, {0x3ffffffd, 30}, {0xfffffeb, 28}, {0xfffffec, 28},
- {0xfffffed, 28}, {0xfffffee, 28}, {0xfffffef, 28}, {0xffffff0, 28},
- {0xffffff1, 28}, {0xffffff2, 28}, {0x3ffffffe, 30}, {0xffffff3, 28},
- {0xffffff4, 28}, {0xffffff5, 28}, {0xffffff6, 28}, {0xffffff7, 28},
- {0xffffff8, 28}, {0xffffff9, 28}, {0xffffffa, 28}, {0xffffffb, 28},
- {0x14, 6}, {0x3f8, 10}, {0x3f9, 10}, {0xffa, 12},
- {0x1ff9, 13}, {0x15, 6}, {0xf8, 8}, {0x7fa, 11},
- {0x3fa, 10}, {0x3fb, 10}, {0xf9, 8}, {0x7fb, 11},
- {0xfa, 8}, {0x16, 6}, {0x17, 6}, {0x18, 6},
- {0x0, 5}, {0x1, 5}, {0x2, 5}, {0x19, 6},
- {0x1a, 6}, {0x1b, 6}, {0x1c, 6}, {0x1d, 6},
- {0x1e, 6}, {0x1f, 6}, {0x5c, 7}, {0xfb, 8},
- {0x7ffc, 15}, {0x20, 6}, {0xffb, 12}, {0x3fc, 10},
- {0x1ffa, 13}, {0x21, 6}, {0x5d, 7}, {0x5e, 7},
- {0x5f, 7}, {0x60, 7}, {0x61, 7}, {0x62, 7},
- {0x63, 7}, {0x64, 7}, {0x65, 7}, {0x66, 7},
- {0x67, 7}, {0x68, 7}, {0x69, 7}, {0x6a, 7},
- {0x6b, 7}, {0x6c, 7}, {0x6d, 7}, {0x6e, 7},
- {0x6f, 7}, {0x70, 7}, {0x71, 7}, {0x72, 7},
- {0xfc, 8}, {0x73, 7}, {0xfd, 8}, {0x1ffb, 13},
- {0x7fff0, 19}, {0x1ffc, 13}, {0x3ffc, 14}, {0x22, 6},
- {0x7ffd, 15}, {0x3, 5}, {0x23, 6}, {0x4, 5},
- {0x24, 6}, {0x5, 5}, {0x25, 6}, {0x26, 6},
- {0x27, 6}, {0x6, 5}, {0x74, 7}, {0x75, 7},
- {0x28, 6}, {0x29, 6}, {0x2a, 6}, {0x7, 5},
- {0x2b, 6}, {0x76, 7}, {0x2c, 6}, {0x8, 5},
- {0x9, 5}, {0x2d, 6}, {0x77, 7}, {0x78, 7},
- {0x79, 7}, {0x7a, 7}, {0x7b, 7}, {0x7ffe, 15},
- {0x7fc, 11}, {0x3ffd, 14}, {0x1ffd, 13}, {0xffffffc, 28},
- {0xfffe6, 20}, {0x3fffd2, 22}, {0xfffe7, 20}, {0xfffe8, 20},
- {0x3fffd3, 22}, {0x3fffd4, 22}, {0x3fffd5, 22}, {0x7fffd9, 23},
- {0x3fffd6, 22}, {0x7fffda, 23}, {0x7fffdb, 23}, {0x7fffdc, 23},
- {0x7fffdd, 23}, {0x7fffde, 23}, {0xffffeb, 24}, {0x7fffdf, 23},
- {0xffffec, 24}, {0xffffed, 24}, {0x3fffd7, 22}, {0x7fffe0, 23},
- {0xffffee, 24}, {0x7fffe1, 23}, {0x7fffe2, 23}, {0x7fffe3, 23},
- {0x7fffe4, 23}, {0x1fffdc, 21}, {0x3fffd8, 22}, {0x7fffe5, 23},
- {0x3fffd9, 22}, {0x7fffe6, 23}, {0x7fffe7, 23}, {0xffffef, 24},
- {0x3fffda, 22}, {0x1fffdd, 21}, {0xfffe9, 20}, {0x3fffdb, 22},
- {0x3fffdc, 22}, {0x7fffe8, 23}, {0x7fffe9, 23}, {0x1fffde, 21},
- {0x7fffea, 23}, {0x3fffdd, 22}, {0x3fffde, 22}, {0xfffff0, 24},
- {0x1fffdf, 21}, {0x3fffdf, 22}, {0x7fffeb, 23}, {0x7fffec, 23},
- {0x1fffe0, 21}, {0x1fffe1, 21}, {0x3fffe0, 22}, {0x1fffe2, 21},
- {0x7fffed, 23}, {0x3fffe1, 22}, {0x7fffee, 23}, {0x7fffef, 23},
- {0xfffea, 20}, {0x3fffe2, 22}, {0x3fffe3, 22}, {0x3fffe4, 22},
- {0x7ffff0, 23}, {0x3fffe5, 22}, {0x3fffe6, 22}, {0x7ffff1, 23},
- {0x3ffffe0, 26}, {0x3ffffe1, 26}, {0xfffeb, 20}, {0x7fff1, 19},
- {0x3fffe7, 22}, {0x7ffff2, 23}, {0x3fffe8, 22}, {0x1ffffec, 25},
- {0x3ffffe2, 26}, {0x3ffffe3, 26}, {0x3ffffe4, 26}, {0x7ffffde, 27},
- {0x7ffffdf, 27}, {0x3ffffe5, 26}, {0xfffff1, 24}, {0x1ffffed, 25},
- {0x7fff2, 19}, {0x1fffe3, 21}, {0x3ffffe6, 26}, {0x7ffffe0, 27},
- {0x7ffffe1, 27}, {0x3ffffe7, 26}, {0x7ffffe2, 27}, {0xfffff2, 24},
- {0x1fffe4, 21}, {0x1fffe5, 21}, {0x3ffffe8, 26}, {0x3ffffe9, 26},
- {0xffffffd, 28}, {0x7ffffe3, 27}, {0x7ffffe4, 27}, {0x7ffffe5, 27},
- {0xfffec, 20}, {0xfffff3, 24}, {0xfffed, 20}, {0x1fffe6, 21},
- {0x3fffe9, 22}, {0x1fffe7, 21}, {0x1fffe8, 21}, {0x7ffff3, 23},
- {0x3fffea, 22}, {0x3fffeb, 22}, {0x1ffffee, 25}, {0x1ffffef, 25},
- {0xfffff4, 24}, {0xfffff5, 24}, {0x3ffffea, 26}, {0x7ffff4, 23},
- {0x3ffffeb, 26}, {0x7ffffe6, 27}, {0x3ffffec, 26}, {0x3ffffed, 26},
- {0x7ffffe7, 27}, {0x7ffffe8, 27}, {0x7ffffe9, 27}, {0x7ffffea, 27},
- {0x7ffffeb, 27}, {0xffffffe, 28}, {0x7ffffec, 27}, {0x7ffffed, 27},
- {0x7ffffee, 27}, {0x7ffffef, 27}, {0x7fffff0, 27}, {0x3ffffee, 26},
- {0x3fffffff, 30},
+ {0x1ff8, 13}, {0x7fffd8, 23}, {0xfffffe2, 28}, {0xfffffe3, 28},
+ {0xfffffe4, 28}, {0xfffffe5, 28}, {0xfffffe6, 28}, {0xfffffe7, 28},
+ {0xfffffe8, 28}, {0xffffea, 24}, {0x3ffffffc, 30}, {0xfffffe9, 28},
+ {0xfffffea, 28}, {0x3ffffffd, 30}, {0xfffffeb, 28}, {0xfffffec, 28},
+ {0xfffffed, 28}, {0xfffffee, 28}, {0xfffffef, 28}, {0xffffff0, 28},
+ {0xffffff1, 28}, {0xffffff2, 28}, {0x3ffffffe, 30}, {0xffffff3, 28},
+ {0xffffff4, 28}, {0xffffff5, 28}, {0xffffff6, 28}, {0xffffff7, 28},
+ {0xffffff8, 28}, {0xffffff9, 28}, {0xffffffa, 28}, {0xffffffb, 28},
+ {0x14, 6}, {0x3f8, 10}, {0x3f9, 10}, {0xffa, 12},
+ {0x1ff9, 13}, {0x15, 6}, {0xf8, 8}, {0x7fa, 11},
+ {0x3fa, 10}, {0x3fb, 10}, {0xf9, 8}, {0x7fb, 11},
+ {0xfa, 8}, {0x16, 6}, {0x17, 6}, {0x18, 6},
+ {0x0, 5}, {0x1, 5}, {0x2, 5}, {0x19, 6},
+ {0x1a, 6}, {0x1b, 6}, {0x1c, 6}, {0x1d, 6},
+ {0x1e, 6}, {0x1f, 6}, {0x5c, 7}, {0xfb, 8},
+ {0x7ffc, 15}, {0x20, 6}, {0xffb, 12}, {0x3fc, 10},
+ {0x1ffa, 13}, {0x21, 6}, {0x5d, 7}, {0x5e, 7},
+ {0x5f, 7}, {0x60, 7}, {0x61, 7}, {0x62, 7},
+ {0x63, 7}, {0x64, 7}, {0x65, 7}, {0x66, 7},
+ {0x67, 7}, {0x68, 7}, {0x69, 7}, {0x6a, 7},
+ {0x6b, 7}, {0x6c, 7}, {0x6d, 7}, {0x6e, 7},
+ {0x6f, 7}, {0x70, 7}, {0x71, 7}, {0x72, 7},
+ {0xfc, 8}, {0x73, 7}, {0xfd, 8}, {0x1ffb, 13},
+ {0x7fff0, 19}, {0x1ffc, 13}, {0x3ffc, 14}, {0x22, 6},
+ {0x7ffd, 15}, {0x3, 5}, {0x23, 6}, {0x4, 5},
+ {0x24, 6}, {0x5, 5}, {0x25, 6}, {0x26, 6},
+ {0x27, 6}, {0x6, 5}, {0x74, 7}, {0x75, 7},
+ {0x28, 6}, {0x29, 6}, {0x2a, 6}, {0x7, 5},
+ {0x2b, 6}, {0x76, 7}, {0x2c, 6}, {0x8, 5},
+ {0x9, 5}, {0x2d, 6}, {0x77, 7}, {0x78, 7},
+ {0x79, 7}, {0x7a, 7}, {0x7b, 7}, {0x7ffe, 15},
+ {0x7fc, 11}, {0x3ffd, 14}, {0x1ffd, 13}, {0xffffffc, 28},
+ {0xfffe6, 20}, {0x3fffd2, 22}, {0xfffe7, 20}, {0xfffe8, 20},
+ {0x3fffd3, 22}, {0x3fffd4, 22}, {0x3fffd5, 22}, {0x7fffd9, 23},
+ {0x3fffd6, 22}, {0x7fffda, 23}, {0x7fffdb, 23}, {0x7fffdc, 23},
+ {0x7fffdd, 23}, {0x7fffde, 23}, {0xffffeb, 24}, {0x7fffdf, 23},
+ {0xffffec, 24}, {0xffffed, 24}, {0x3fffd7, 22}, {0x7fffe0, 23},
+ {0xffffee, 24}, {0x7fffe1, 23}, {0x7fffe2, 23}, {0x7fffe3, 23},
+ {0x7fffe4, 23}, {0x1fffdc, 21}, {0x3fffd8, 22}, {0x7fffe5, 23},
+ {0x3fffd9, 22}, {0x7fffe6, 23}, {0x7fffe7, 23}, {0xffffef, 24},
+ {0x3fffda, 22}, {0x1fffdd, 21}, {0xfffe9, 20}, {0x3fffdb, 22},
+ {0x3fffdc, 22}, {0x7fffe8, 23}, {0x7fffe9, 23}, {0x1fffde, 21},
+ {0x7fffea, 23}, {0x3fffdd, 22}, {0x3fffde, 22}, {0xfffff0, 24},
+ {0x1fffdf, 21}, {0x3fffdf, 22}, {0x7fffeb, 23}, {0x7fffec, 23},
+ {0x1fffe0, 21}, {0x1fffe1, 21}, {0x3fffe0, 22}, {0x1fffe2, 21},
+ {0x7fffed, 23}, {0x3fffe1, 22}, {0x7fffee, 23}, {0x7fffef, 23},
+ {0xfffea, 20}, {0x3fffe2, 22}, {0x3fffe3, 22}, {0x3fffe4, 22},
+ {0x7ffff0, 23}, {0x3fffe5, 22}, {0x3fffe6, 22}, {0x7ffff1, 23},
+ {0x3ffffe0, 26}, {0x3ffffe1, 26}, {0xfffeb, 20}, {0x7fff1, 19},
+ {0x3fffe7, 22}, {0x7ffff2, 23}, {0x3fffe8, 22}, {0x1ffffec, 25},
+ {0x3ffffe2, 26}, {0x3ffffe3, 26}, {0x3ffffe4, 26}, {0x7ffffde, 27},
+ {0x7ffffdf, 27}, {0x3ffffe5, 26}, {0xfffff1, 24}, {0x1ffffed, 25},
+ {0x7fff2, 19}, {0x1fffe3, 21}, {0x3ffffe6, 26}, {0x7ffffe0, 27},
+ {0x7ffffe1, 27}, {0x3ffffe7, 26}, {0x7ffffe2, 27}, {0xfffff2, 24},
+ {0x1fffe4, 21}, {0x1fffe5, 21}, {0x3ffffe8, 26}, {0x3ffffe9, 26},
+ {0xffffffd, 28}, {0x7ffffe3, 27}, {0x7ffffe4, 27}, {0x7ffffe5, 27},
+ {0xfffec, 20}, {0xfffff3, 24}, {0xfffed, 20}, {0x1fffe6, 21},
+ {0x3fffe9, 22}, {0x1fffe7, 21}, {0x1fffe8, 21}, {0x7ffff3, 23},
+ {0x3fffea, 22}, {0x3fffeb, 22}, {0x1ffffee, 25}, {0x1ffffef, 25},
+ {0xfffff4, 24}, {0xfffff5, 24}, {0x3ffffea, 26}, {0x7ffff4, 23},
+ {0x3ffffeb, 26}, {0x7ffffe6, 27}, {0x3ffffec, 26}, {0x3ffffed, 26},
+ {0x7ffffe7, 27}, {0x7ffffe8, 27}, {0x7ffffe9, 27}, {0x7ffffea, 27},
+ {0x7ffffeb, 27}, {0xffffffe, 28}, {0x7ffffec, 27}, {0x7ffffed, 27},
+ {0x7ffffee, 27}, {0x7ffffef, 27}, {0x7fffff0, 27}, {0x3ffffee, 26},
+ {0x3fffffff, 30},
};
diff --git a/src/core/transport/chttp2/huffsyms.h b/src/core/transport/chttp2/huffsyms.h
index 04f062dd5e..a3cdba8235 100644
--- a/src/core/transport/chttp2/huffsyms.h
+++ b/src/core/transport/chttp2/huffsyms.h
@@ -38,8 +38,7 @@
#define GRPC_CHTTP2_NUM_HUFFSYMS 257
-typedef struct
-{
+typedef struct {
unsigned bits;
unsigned length;
} grpc_chttp2_huffsym;
diff --git a/src/core/transport/chttp2/incoming_metadata.c b/src/core/transport/chttp2/incoming_metadata.c
index 93eee0be3f..d216c42113 100644
--- a/src/core/transport/chttp2/incoming_metadata.c
+++ b/src/core/transport/chttp2/incoming_metadata.c
@@ -40,50 +40,43 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-void
-grpc_chttp2_incoming_metadata_buffer_init (grpc_chttp2_incoming_metadata_buffer * buffer)
-{
- buffer->deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
+void grpc_chttp2_incoming_metadata_buffer_init(
+ grpc_chttp2_incoming_metadata_buffer *buffer) {
+ buffer->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
}
-void
-grpc_chttp2_incoming_metadata_buffer_destroy (grpc_chttp2_incoming_metadata_buffer * buffer)
-{
+void grpc_chttp2_incoming_metadata_buffer_destroy(
+ grpc_chttp2_incoming_metadata_buffer *buffer) {
size_t i;
- for (i = 0; i < buffer->count; i++)
- {
- GRPC_MDELEM_UNREF (buffer->elems[i].md);
- }
- gpr_free (buffer->elems);
+ for (i = 0; i < buffer->count; i++) {
+ GRPC_MDELEM_UNREF(buffer->elems[i].md);
+ }
+ gpr_free(buffer->elems);
}
-void
-grpc_chttp2_incoming_metadata_buffer_add (grpc_chttp2_incoming_metadata_buffer * buffer, grpc_mdelem * elem)
-{
- if (buffer->capacity == buffer->count)
- {
- buffer->capacity = GPR_MAX (8, 2 * buffer->capacity);
- buffer->elems = gpr_realloc (buffer->elems, sizeof (*buffer->elems) * buffer->capacity);
- }
+void grpc_chttp2_incoming_metadata_buffer_add(
+ grpc_chttp2_incoming_metadata_buffer *buffer, grpc_mdelem *elem) {
+ if (buffer->capacity == buffer->count) {
+ buffer->capacity = GPR_MAX(8, 2 * buffer->capacity);
+ buffer->elems =
+ gpr_realloc(buffer->elems, sizeof(*buffer->elems) * buffer->capacity);
+ }
buffer->elems[buffer->count++].md = elem;
}
-void
-grpc_chttp2_incoming_metadata_buffer_set_deadline (grpc_chttp2_incoming_metadata_buffer * buffer, gpr_timespec deadline)
-{
+void grpc_chttp2_incoming_metadata_buffer_set_deadline(
+ grpc_chttp2_incoming_metadata_buffer *buffer, gpr_timespec deadline) {
buffer->deadline = deadline;
}
-void
-grpc_chttp2_incoming_metadata_live_op_buffer_end (grpc_chttp2_incoming_metadata_live_op_buffer * buffer)
-{
- gpr_free (buffer->elems);
+void grpc_chttp2_incoming_metadata_live_op_buffer_end(
+ grpc_chttp2_incoming_metadata_live_op_buffer *buffer) {
+ gpr_free(buffer->elems);
buffer->elems = NULL;
}
-void
-grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into (grpc_chttp2_incoming_metadata_buffer * buffer, grpc_stream_op_buffer * sopb)
-{
+void grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into(
+ grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb) {
grpc_metadata_batch b;
b.list.head = NULL;
@@ -91,56 +84,52 @@ grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into (grpc_chttp2_inco
we can reconstitute the list.
We can't do list building here as later incoming metadata may reallocate
the underlying array. */
- b.list.tail = (void *) (gpr_intptr) buffer->count;
+ b.list.tail = (void *)(gpr_intptr)buffer->count;
b.garbage.head = b.garbage.tail = NULL;
b.deadline = buffer->deadline;
- buffer->deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
+ buffer->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
- grpc_sopb_add_metadata (sopb, b);
+ grpc_sopb_add_metadata(sopb, b);
}
-void
-grpc_chttp2_incoming_metadata_buffer_swap (grpc_chttp2_incoming_metadata_buffer * a, grpc_chttp2_incoming_metadata_buffer * b)
-{
- GPR_SWAP (grpc_chttp2_incoming_metadata_buffer, *a, *b);
+void grpc_chttp2_incoming_metadata_buffer_swap(
+ grpc_chttp2_incoming_metadata_buffer *a,
+ grpc_chttp2_incoming_metadata_buffer *b) {
+ GPR_SWAP(grpc_chttp2_incoming_metadata_buffer, *a, *b);
}
-void
-grpc_incoming_metadata_buffer_move_to_referencing_sopb (grpc_chttp2_incoming_metadata_buffer * src, grpc_chttp2_incoming_metadata_buffer * dst, grpc_stream_op_buffer * sopb)
-{
+void grpc_incoming_metadata_buffer_move_to_referencing_sopb(
+ grpc_chttp2_incoming_metadata_buffer *src,
+ grpc_chttp2_incoming_metadata_buffer *dst, grpc_stream_op_buffer *sopb) {
size_t delta;
size_t i;
- dst->deadline = gpr_time_min (src->deadline, dst->deadline);
-
- if (src->count == 0)
- {
- return;
- }
- if (dst->count == 0)
- {
- grpc_chttp2_incoming_metadata_buffer_swap (src, dst);
- return;
- }
+ dst->deadline = gpr_time_min(src->deadline, dst->deadline);
+
+ if (src->count == 0) {
+ return;
+ }
+ if (dst->count == 0) {
+ grpc_chttp2_incoming_metadata_buffer_swap(src, dst);
+ return;
+ }
delta = dst->count;
- if (dst->capacity < src->count + dst->count)
- {
- dst->capacity = GPR_MAX (dst->capacity * 2, src->count + dst->count);
- dst->elems = gpr_realloc (dst->elems, dst->capacity * sizeof (*dst->elems));
- }
- memcpy (dst->elems + dst->count, src->elems, src->count * sizeof (*src->elems));
+ if (dst->capacity < src->count + dst->count) {
+ dst->capacity = GPR_MAX(dst->capacity * 2, src->count + dst->count);
+ dst->elems = gpr_realloc(dst->elems, dst->capacity * sizeof(*dst->elems));
+ }
+ memcpy(dst->elems + dst->count, src->elems, src->count * sizeof(*src->elems));
dst->count += src->count;
- for (i = 0; i < sopb->nops; i++)
- {
- if (sopb->ops[i].type != GRPC_OP_METADATA)
- continue;
- sopb->ops[i].data.metadata.list.tail = (void *) (delta + (gpr_uintptr) sopb->ops[i].data.metadata.list.tail);
- }
+ for (i = 0; i < sopb->nops; i++) {
+ if (sopb->ops[i].type != GRPC_OP_METADATA) continue;
+ sopb->ops[i].data.metadata.list.tail =
+ (void *)(delta + (gpr_uintptr)sopb->ops[i].data.metadata.list.tail);
+ }
src->count = 0;
}
-void
-grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op (grpc_chttp2_incoming_metadata_buffer * buffer, grpc_stream_op_buffer * sopb, grpc_chttp2_incoming_metadata_live_op_buffer * live_op_buffer)
-{
+void grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op(
+ grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb,
+ grpc_chttp2_incoming_metadata_live_op_buffer *live_op_buffer) {
grpc_stream_op *ops = sopb->ops;
size_t nops = sopb->nops;
size_t i;
@@ -152,49 +141,42 @@ grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op (grpc_ch
/* rework the array of metadata into a linked list, making use
of the breadcrumbs we left in metadata batches during
add_metadata_batch */
- for (i = 0; i < nops; i++)
- {
- grpc_stream_op *op = &ops[i];
- if (op->type != GRPC_OP_METADATA)
- continue;
- found_metadata = 1;
- /* we left a breadcrumb indicating where the end of this list is,
- and since we add sequentially, we know from the end of the last
- segment where this segment begins */
- last_mdidx = (size_t) (gpr_intptr) (op->data.metadata.list.tail);
- GPR_ASSERT (last_mdidx > mdidx);
- GPR_ASSERT (last_mdidx <= buffer->count);
- /* turn the array into a doubly linked list */
- op->data.metadata.list.head = &buffer->elems[mdidx];
- op->data.metadata.list.tail = &buffer->elems[last_mdidx - 1];
- for (j = mdidx + 1; j < last_mdidx; j++)
- {
- buffer->elems[j].prev = &buffer->elems[j - 1];
- buffer->elems[j - 1].next = &buffer->elems[j];
- }
- buffer->elems[mdidx].prev = NULL;
- buffer->elems[last_mdidx - 1].next = NULL;
- /* track where we're up to */
- mdidx = last_mdidx;
+ for (i = 0; i < nops; i++) {
+ grpc_stream_op *op = &ops[i];
+ if (op->type != GRPC_OP_METADATA) continue;
+ found_metadata = 1;
+ /* we left a breadcrumb indicating where the end of this list is,
+ and since we add sequentially, we know from the end of the last
+ segment where this segment begins */
+ last_mdidx = (size_t)(gpr_intptr)(op->data.metadata.list.tail);
+ GPR_ASSERT(last_mdidx > mdidx);
+ GPR_ASSERT(last_mdidx <= buffer->count);
+ /* turn the array into a doubly linked list */
+ op->data.metadata.list.head = &buffer->elems[mdidx];
+ op->data.metadata.list.tail = &buffer->elems[last_mdidx - 1];
+ for (j = mdidx + 1; j < last_mdidx; j++) {
+ buffer->elems[j].prev = &buffer->elems[j - 1];
+ buffer->elems[j - 1].next = &buffer->elems[j];
}
- if (found_metadata)
- {
- live_op_buffer->elems = buffer->elems;
- if (mdidx != buffer->count)
- {
- /* we have a partially read metadata batch still in incoming_metadata */
- size_t new_count = buffer->count - mdidx;
- size_t copy_bytes = sizeof (*buffer->elems) * new_count;
- GPR_ASSERT (mdidx < buffer->count);
- buffer->elems = gpr_malloc (copy_bytes);
- memcpy (live_op_buffer->elems + mdidx, buffer->elems, copy_bytes);
- buffer->count = buffer->capacity = new_count;
- }
- else
- {
- buffer->elems = NULL;
- buffer->count = 0;
- buffer->capacity = 0;
- }
+ buffer->elems[mdidx].prev = NULL;
+ buffer->elems[last_mdidx - 1].next = NULL;
+ /* track where we're up to */
+ mdidx = last_mdidx;
+ }
+ if (found_metadata) {
+ live_op_buffer->elems = buffer->elems;
+ if (mdidx != buffer->count) {
+ /* we have a partially read metadata batch still in incoming_metadata */
+ size_t new_count = buffer->count - mdidx;
+ size_t copy_bytes = sizeof(*buffer->elems) * new_count;
+ GPR_ASSERT(mdidx < buffer->count);
+ buffer->elems = gpr_malloc(copy_bytes);
+ memcpy(live_op_buffer->elems + mdidx, buffer->elems, copy_bytes);
+ buffer->count = buffer->capacity = new_count;
+ } else {
+ buffer->elems = NULL;
+ buffer->count = 0;
+ buffer->capacity = 0;
}
+ }
}
diff --git a/src/core/transport/chttp2/incoming_metadata.h b/src/core/transport/chttp2/incoming_metadata.h
index 7ca0f722c3..2f1de411ba 100644
--- a/src/core/transport/chttp2/incoming_metadata.h
+++ b/src/core/transport/chttp2/incoming_metadata.h
@@ -36,36 +36,45 @@
#include "src/core/transport/transport.h"
-typedef struct
-{
+typedef struct {
grpc_linked_mdelem *elems;
size_t count;
size_t capacity;
gpr_timespec deadline;
} grpc_chttp2_incoming_metadata_buffer;
-typedef struct
-{
+typedef struct {
grpc_linked_mdelem *elems;
} grpc_chttp2_incoming_metadata_live_op_buffer;
/** assumes everything initially zeroed */
-void grpc_chttp2_incoming_metadata_buffer_init (grpc_chttp2_incoming_metadata_buffer * buffer);
-void grpc_chttp2_incoming_metadata_buffer_destroy (grpc_chttp2_incoming_metadata_buffer * buffer);
-void grpc_chttp2_incoming_metadata_buffer_reset (grpc_chttp2_incoming_metadata_buffer * buffer);
+void grpc_chttp2_incoming_metadata_buffer_init(
+ grpc_chttp2_incoming_metadata_buffer *buffer);
+void grpc_chttp2_incoming_metadata_buffer_destroy(
+ grpc_chttp2_incoming_metadata_buffer *buffer);
+void grpc_chttp2_incoming_metadata_buffer_reset(
+ grpc_chttp2_incoming_metadata_buffer *buffer);
-void grpc_chttp2_incoming_metadata_buffer_add (grpc_chttp2_incoming_metadata_buffer * buffer, grpc_mdelem * elem);
-void grpc_chttp2_incoming_metadata_buffer_set_deadline (grpc_chttp2_incoming_metadata_buffer * buffer, gpr_timespec deadline);
+void grpc_chttp2_incoming_metadata_buffer_add(
+ grpc_chttp2_incoming_metadata_buffer *buffer, grpc_mdelem *elem);
+void grpc_chttp2_incoming_metadata_buffer_set_deadline(
+ grpc_chttp2_incoming_metadata_buffer *buffer, gpr_timespec deadline);
/** extend sopb with a metadata batch; this must be post-processed by
grpc_chttp2_incoming_metadata_buffer_postprocess_sopb before being handed
out of the transport */
-void grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into (grpc_chttp2_incoming_metadata_buffer * buffer, grpc_stream_op_buffer * sopb);
+void grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into(
+ grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb);
-void grpc_incoming_metadata_buffer_move_to_referencing_sopb (grpc_chttp2_incoming_metadata_buffer * src, grpc_chttp2_incoming_metadata_buffer * dst, grpc_stream_op_buffer * sopb);
+void grpc_incoming_metadata_buffer_move_to_referencing_sopb(
+ grpc_chttp2_incoming_metadata_buffer *src,
+ grpc_chttp2_incoming_metadata_buffer *dst, grpc_stream_op_buffer *sopb);
-void grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op (grpc_chttp2_incoming_metadata_buffer * buffer, grpc_stream_op_buffer * sopb, grpc_chttp2_incoming_metadata_live_op_buffer * live_op_buffer);
+void grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op(
+ grpc_chttp2_incoming_metadata_buffer *buffer, grpc_stream_op_buffer *sopb,
+ grpc_chttp2_incoming_metadata_live_op_buffer *live_op_buffer);
-void grpc_chttp2_incoming_metadata_live_op_buffer_end (grpc_chttp2_incoming_metadata_live_op_buffer * live_op_buffer);
+void grpc_chttp2_incoming_metadata_live_op_buffer_end(
+ grpc_chttp2_incoming_metadata_live_op_buffer *live_op_buffer);
#endif /* GRPC_INTERNAL_CORE_CHTTP2_INCOMING_METADATA_H */
diff --git a/src/core/transport/chttp2/internal.h b/src/core/transport/chttp2/internal.h
index aaf090cd44..b35f8b5d88 100644
--- a/src/core/transport/chttp2/internal.h
+++ b/src/core/transport/chttp2/internal.h
@@ -54,8 +54,7 @@ typedef struct grpc_chttp2_stream grpc_chttp2_stream;
/* streams are kept in various linked lists depending on what things need to
happen to them... this enum labels each list */
-typedef enum
-{
+typedef enum {
GRPC_CHTTP2_LIST_ALL_STREAMS,
GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED,
GRPC_CHTTP2_LIST_WRITABLE,
@@ -68,12 +67,11 @@ typedef enum
/** streams that are waiting to start because there are too many concurrent
streams on the connection */
GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY,
- STREAM_LIST_COUNT /* must be last */
+ STREAM_LIST_COUNT /* must be last */
} grpc_chttp2_stream_list_id;
/* deframer state for the overall http2 stream of bytes */
-typedef enum
-{
+typedef enum {
/* prefix: one entry per http2 connection prefix byte */
GRPC_DTS_CLIENT_PREFIX_0 = 0,
GRPC_DTS_CLIENT_PREFIX_1,
@@ -115,8 +113,7 @@ typedef enum
GRPC_DTS_FRAME
} grpc_chttp2_deframe_transport_state;
-typedef enum
-{
+typedef enum {
GRPC_WRITE_STATE_OPEN,
GRPC_WRITE_STATE_QUEUED_CLOSE,
GRPC_WRITE_STATE_SENT_CLOSE
@@ -126,28 +123,24 @@ typedef enum
#define GRPC_CHTTP2_WRITING_DATA 1
#define GRPC_CHTTP2_WRITING_WINDOW 2
-typedef enum
-{
+typedef enum {
GRPC_DONT_SEND_CLOSED = 0,
GRPC_SEND_CLOSED,
GRPC_SEND_CLOSED_WITH_RST_STREAM
} grpc_chttp2_send_closed;
-typedef struct
-{
+typedef struct {
grpc_chttp2_stream *head;
grpc_chttp2_stream *tail;
} grpc_chttp2_stream_list;
-typedef struct
-{
+typedef struct {
grpc_chttp2_stream *next;
grpc_chttp2_stream *prev;
} grpc_chttp2_stream_link;
/* We keep several sets of connection wide parameters */
-typedef enum
-{
+typedef enum {
/* The settings our peer has asked for (and we have acked) */
GRPC_PEER_SETTINGS = 0,
/* The settings we'd like to have */
@@ -160,16 +153,14 @@ typedef enum
} grpc_chttp2_setting_set;
/* Outstanding ping request data */
-typedef struct grpc_chttp2_outstanding_ping
-{
+typedef struct grpc_chttp2_outstanding_ping {
gpr_uint8 id[8];
grpc_closure *on_recv;
struct grpc_chttp2_outstanding_ping *next;
struct grpc_chttp2_outstanding_ping *prev;
} grpc_chttp2_outstanding_ping;
-typedef struct
-{
+typedef struct {
/** data to write next write */
gpr_slice_buffer qbuf;
@@ -213,8 +204,7 @@ typedef struct
gpr_uint32 concurrent_stream_count;
} grpc_chttp2_transport_global;
-typedef struct
-{
+typedef struct {
/** data to write now */
gpr_slice_buffer outbuf;
/** hpack encoding */
@@ -225,8 +215,7 @@ typedef struct
grpc_closure done_cb;
} grpc_chttp2_transport_writing;
-struct grpc_chttp2_transport_parsing
-{
+struct grpc_chttp2_transport_parsing {
/** is this transport a client? (boolean) */
gpr_uint8 is_client;
@@ -247,8 +236,7 @@ struct grpc_chttp2_transport_parsing
/** parser for headers */
grpc_chttp2_hpack_parser hpack_parser;
/** simple one shot parsers */
- union
- {
+ union {
grpc_chttp2_window_update_parser window_update;
grpc_chttp2_settings_parser settings;
grpc_chttp2_ping_parser ping;
@@ -277,7 +265,10 @@ struct grpc_chttp2_transport_parsing
/* active parser */
void *parser_data;
grpc_chttp2_stream_parsing *incoming_stream;
- grpc_chttp2_parse_error (*parser) (grpc_exec_ctx * exec_ctx, void *parser_user_data, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last);
+ grpc_chttp2_parse_error (*parser)(
+ grpc_exec_ctx *exec_ctx, void *parser_user_data,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
/* received settings */
gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS];
@@ -293,9 +284,8 @@ struct grpc_chttp2_transport_parsing
grpc_chttp2_outstanding_ping pings;
};
-struct grpc_chttp2_transport
-{
- grpc_transport base; /* must be first */
+struct grpc_chttp2_transport {
+ grpc_transport base; /* must be first */
grpc_endpoint *ep;
grpc_mdctx *metadata_context;
gpr_refcount refs;
@@ -353,10 +343,10 @@ struct grpc_chttp2_transport
publish the accepted server stream */
grpc_chttp2_stream **accepting_stream;
- struct
- {
+ struct {
/* accept stream callback */
- void (*accept_stream) (void *user_data, grpc_transport * transport, const void *server_data);
+ void (*accept_stream)(void *user_data, grpc_transport *transport,
+ const void *server_data);
void *accept_stream_user_data;
/** connectivity tracking */
@@ -364,8 +354,7 @@ struct grpc_chttp2_transport
} channel_callback;
};
-typedef struct
-{
+typedef struct {
/** HTTP2 stream id for this stream, or zero if one has not been assigned */
gpr_uint32 id;
@@ -421,8 +410,7 @@ typedef struct
grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata;
} grpc_chttp2_stream_global;
-typedef struct
-{
+typedef struct {
/** HTTP2 stream id for this stream, or zero if one has not been assigned */
gpr_uint32 id;
/** sops that have passed flow control to be written */
@@ -433,8 +421,7 @@ typedef struct
gpr_uint32 announce_window;
} grpc_chttp2_stream_writing;
-struct grpc_chttp2_stream_parsing
-{
+struct grpc_chttp2_stream_parsing {
/** HTTP2 stream id for this stream, or zero if one has not been assigned */
gpr_uint32 id;
/** has this stream received a close */
@@ -456,8 +443,7 @@ struct grpc_chttp2_stream_parsing
grpc_chttp2_incoming_metadata_buffer incoming_metadata;
};
-struct grpc_chttp2_stream
-{
+struct grpc_chttp2_stream {
grpc_chttp2_stream_global global;
grpc_chttp2_stream_writing writing;
grpc_chttp2_stream_parsing parsing;
@@ -479,71 +465,141 @@ struct grpc_chttp2_stream
/** Someone is unlocking the transport mutex: check to see if writes
are required, and schedule them if so */
-int grpc_chttp2_unlocking_check_writes (grpc_chttp2_transport_global * global, grpc_chttp2_transport_writing * writing);
-void grpc_chttp2_perform_writes (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_writing * transport_writing, grpc_endpoint * endpoint);
-void grpc_chttp2_terminate_writing (grpc_exec_ctx * exec_ctx, void *transport_writing, int success);
-void grpc_chttp2_cleanup_writing (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * global, grpc_chttp2_transport_writing * writing);
-
-void grpc_chttp2_prepare_to_read (grpc_chttp2_transport_global * global, grpc_chttp2_transport_parsing * parsing);
+int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global,
+ grpc_chttp2_transport_writing *writing);
+void grpc_chttp2_perform_writes(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_writing *transport_writing,
+ grpc_endpoint *endpoint);
+void grpc_chttp2_terminate_writing(grpc_exec_ctx *exec_ctx,
+ void *transport_writing, int success);
+void grpc_chttp2_cleanup_writing(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport_global *global,
+ grpc_chttp2_transport_writing *writing);
+
+void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global,
+ grpc_chttp2_transport_parsing *parsing);
/** Process one slice of incoming data; return 1 if the connection is still
viable after reading, or 0 if the connection should be torn down */
-int grpc_chttp2_perform_read (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_parsing * transport_parsing, gpr_slice slice);
-void grpc_chttp2_publish_reads (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * global, grpc_chttp2_transport_parsing * parsing);
+int grpc_chttp2_perform_read(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ gpr_slice slice);
+void grpc_chttp2_publish_reads(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport_global *global,
+ grpc_chttp2_transport_parsing *parsing);
/** Get a writable stream
returns non-zero if there was a stream available */
-void grpc_chttp2_list_add_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-void grpc_chttp2_list_add_first_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-int grpc_chttp2_list_pop_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_writing ** stream_writing);
-void grpc_chttp2_list_remove_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-
-void grpc_chttp2_list_add_incoming_window_updated (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-int grpc_chttp2_list_pop_incoming_window_updated (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_parsing ** stream_parsing);
-void grpc_chttp2_list_remove_incoming_window_updated (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-
-void grpc_chttp2_list_add_writing_stream (grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_writing * stream_writing);
-int grpc_chttp2_list_have_writing_streams (grpc_chttp2_transport_writing * transport_writing);
-int grpc_chttp2_list_pop_writing_stream (grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_writing ** stream_writing);
-
-void grpc_chttp2_list_add_written_stream (grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_writing * stream_writing);
-int grpc_chttp2_list_pop_written_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_writing ** stream_writing);
-
-void grpc_chttp2_list_add_parsing_seen_stream (grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing);
-int grpc_chttp2_list_pop_parsing_seen_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_parsing ** stream_parsing);
-
-void grpc_chttp2_list_add_waiting_for_concurrency (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-int grpc_chttp2_list_pop_waiting_for_concurrency (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global);
-
-void grpc_chttp2_list_add_closed_waiting_for_parsing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-int grpc_chttp2_list_pop_closed_waiting_for_parsing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global);
-
-void grpc_chttp2_list_add_cancelled_waiting_for_writing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-int grpc_chttp2_list_pop_cancelled_waiting_for_writing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global);
-
-void grpc_chttp2_list_add_read_write_state_changed (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global);
-int grpc_chttp2_list_pop_read_write_state_changed (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global);
-
-grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream (grpc_chttp2_transport_parsing * transport_parsing, gpr_uint32 id);
-grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream (grpc_chttp2_transport_parsing * transport_parsing, gpr_uint32 id);
-
-void grpc_chttp2_add_incoming_goaway (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, gpr_uint32 goaway_error, gpr_slice goaway_text);
-
-void grpc_chttp2_register_stream (grpc_chttp2_transport * t, grpc_chttp2_stream * s);
+void grpc_chttp2_list_add_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+void grpc_chttp2_list_add_first_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+int grpc_chttp2_list_pop_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_writing **stream_writing);
+void grpc_chttp2_list_remove_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+
+void grpc_chttp2_list_add_incoming_window_updated(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+int grpc_chttp2_list_pop_incoming_window_updated(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_parsing **stream_parsing);
+void grpc_chttp2_list_remove_incoming_window_updated(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+
+void grpc_chttp2_list_add_writing_stream(
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_writing *stream_writing);
+int grpc_chttp2_list_have_writing_streams(
+ grpc_chttp2_transport_writing *transport_writing);
+int grpc_chttp2_list_pop_writing_stream(
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_writing **stream_writing);
+
+void grpc_chttp2_list_add_written_stream(
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_writing *stream_writing);
+int grpc_chttp2_list_pop_written_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_writing **stream_writing);
+
+void grpc_chttp2_list_add_parsing_seen_stream(
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing);
+int grpc_chttp2_list_pop_parsing_seen_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_parsing **stream_parsing);
+
+void grpc_chttp2_list_add_waiting_for_concurrency(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+int grpc_chttp2_list_pop_waiting_for_concurrency(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global);
+
+void grpc_chttp2_list_add_closed_waiting_for_parsing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+int grpc_chttp2_list_pop_closed_waiting_for_parsing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global);
+
+void grpc_chttp2_list_add_cancelled_waiting_for_writing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+int grpc_chttp2_list_pop_cancelled_waiting_for_writing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global);
+
+void grpc_chttp2_list_add_read_write_state_changed(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global);
+int grpc_chttp2_list_pop_read_write_state_changed(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global);
+
+grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
+ grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
+grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
+ grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
+
+void grpc_chttp2_add_incoming_goaway(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ gpr_uint32 goaway_error, gpr_slice goaway_text);
+
+void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s);
/* returns 1 if this is the last stream, 0 otherwise */
-int
-grpc_chttp2_unregister_stream (grpc_chttp2_transport * t, grpc_chttp2_stream * s)
- GRPC_MUST_USE_RESULT;
- int grpc_chttp2_has_streams (grpc_chttp2_transport * t);
- void grpc_chttp2_for_all_streams (grpc_chttp2_transport_global * transport_global, void *user_data, void (*cb) (grpc_chttp2_transport_global * transport_global, void *user_data, grpc_chttp2_stream_global * stream_global));
+int grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s) GRPC_MUST_USE_RESULT;
+int grpc_chttp2_has_streams(grpc_chttp2_transport *t);
+void grpc_chttp2_for_all_streams(
+ grpc_chttp2_transport_global *transport_global, void *user_data,
+ void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
+ grpc_chttp2_stream_global *stream_global));
- void grpc_chttp2_parsing_become_skip_parser (grpc_chttp2_transport_parsing * transport_parsing);
+void grpc_chttp2_parsing_become_skip_parser(
+ grpc_chttp2_transport_parsing *transport_parsing);
#define GRPC_CHTTP2_CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \
(sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1)
- extern int grpc_http_trace;
- extern int grpc_flowctl_trace;
+extern int grpc_http_trace;
+extern int grpc_flowctl_trace;
#define GRPC_CHTTP2_IF_TRACING(stmt) \
if (!(grpc_http_trace)) \
@@ -568,6 +624,9 @@ grpc_chttp2_unregister_stream (grpc_chttp2_transport * t, grpc_chttp2_stream * s
(gpr_int64)(context->var), (gpr_int64)(delta)); \
}
- void grpc_chttp2_flowctl_trace (const char *file, int line, const char *reason, const char *context, const char *var, int is_client, gpr_uint32 stream_id, gpr_int64 current_value, gpr_int64 delta);
+void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason,
+ const char *context, const char *var,
+ int is_client, gpr_uint32 stream_id,
+ gpr_int64 current_value, gpr_int64 delta);
#endif
diff --git a/src/core/transport/chttp2/parsing.c b/src/core/transport/chttp2/parsing.c
index 9e19590e72..f7a0a10581 100644
--- a/src/core/transport/chttp2/parsing.c
+++ b/src/core/transport/chttp2/parsing.c
@@ -42,46 +42,58 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-static int init_frame_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_header_frame_parser (grpc_chttp2_transport_parsing * transport_parsing, int is_continuation);
-static int init_data_frame_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_rst_stream_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_settings_frame_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_window_update_frame_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_ping_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_goaway_parser (grpc_chttp2_transport_parsing * transport_parsing);
-static int init_skip_frame_parser (grpc_chttp2_transport_parsing * transport_parsing, int is_header);
-
-static int parse_frame_slice (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_parsing * transport_parsing, gpr_slice slice, int is_last);
-
-void
-grpc_chttp2_prepare_to_read (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_parsing * transport_parsing)
-{
+static int init_frame_parser(grpc_chttp2_transport_parsing *transport_parsing);
+static int init_header_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing, int is_continuation);
+static int init_data_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing);
+static int init_rst_stream_parser(
+ grpc_chttp2_transport_parsing *transport_parsing);
+static int init_settings_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing);
+static int init_window_update_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing);
+static int init_ping_parser(grpc_chttp2_transport_parsing *transport_parsing);
+static int init_goaway_parser(grpc_chttp2_transport_parsing *transport_parsing);
+static int init_skip_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing, int is_header);
+
+static int parse_frame_slice(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ gpr_slice slice, int is_last);
+
+void grpc_chttp2_prepare_to_read(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_parsing *transport_parsing) {
grpc_chttp2_stream_global *stream_global;
grpc_chttp2_stream_parsing *stream_parsing;
transport_parsing->next_stream_id = transport_global->next_stream_id;
/* update the parsing view of incoming window */
- if (transport_parsing->incoming_window != transport_global->incoming_window)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("parse", transport_parsing, incoming_window, (gpr_int64) transport_global->incoming_window - (gpr_int64) transport_parsing->incoming_window);
- transport_parsing->incoming_window = transport_global->incoming_window;
- }
- while (grpc_chttp2_list_pop_incoming_window_updated (transport_global, transport_parsing, &stream_global, &stream_parsing))
- {
- stream_parsing->id = stream_global->id;
- if (stream_parsing->incoming_window != stream_global->incoming_window)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("parse", transport_parsing, stream_parsing, incoming_window, (gpr_int64) stream_global->incoming_window - (gpr_int64) stream_parsing->incoming_window);
- stream_parsing->incoming_window = stream_global->incoming_window;
- }
- }
+ if (transport_parsing->incoming_window != transport_global->incoming_window) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "parse", transport_parsing, incoming_window,
+ (gpr_int64)transport_global->incoming_window -
+ (gpr_int64)transport_parsing->incoming_window);
+ transport_parsing->incoming_window = transport_global->incoming_window;
+ }
+ while (grpc_chttp2_list_pop_incoming_window_updated(
+ transport_global, transport_parsing, &stream_global, &stream_parsing)) {
+ stream_parsing->id = stream_global->id;
+ if (stream_parsing->incoming_window != stream_global->incoming_window) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "parse", transport_parsing, stream_parsing, incoming_window,
+ (gpr_int64)stream_global->incoming_window -
+ (gpr_int64)stream_parsing->incoming_window);
+ stream_parsing->incoming_window = stream_global->incoming_window;
+ }
+ }
}
-void
-grpc_chttp2_publish_reads (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_parsing * transport_parsing)
-{
+void grpc_chttp2_publish_reads(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_parsing *transport_parsing) {
grpc_chttp2_stream_global *stream_global;
grpc_chttp2_stream_parsing *stream_parsing;
@@ -93,126 +105,146 @@ grpc_chttp2_publish_reads (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_globa
ID. So,
since we don't have server pushed streams, client should send
GOAWAY last-grpc_chttp2_stream-id=0 in this case. */
- if (!transport_parsing->is_client)
- {
- transport_global->last_incoming_stream_id = transport_parsing->incoming_stream_id;
- }
+ if (!transport_parsing->is_client) {
+ transport_global->last_incoming_stream_id =
+ transport_parsing->incoming_stream_id;
+ }
/* copy parsing qbuf to global qbuf */
- gpr_slice_buffer_move_into (&transport_parsing->qbuf, &transport_global->qbuf);
+ gpr_slice_buffer_move_into(&transport_parsing->qbuf, &transport_global->qbuf);
/* update global settings */
- if (transport_parsing->settings_updated)
- {
- memcpy (transport_global->settings[GRPC_PEER_SETTINGS], transport_parsing->settings, sizeof (transport_parsing->settings));
- transport_parsing->settings_updated = 0;
- }
+ if (transport_parsing->settings_updated) {
+ memcpy(transport_global->settings[GRPC_PEER_SETTINGS],
+ transport_parsing->settings, sizeof(transport_parsing->settings));
+ transport_parsing->settings_updated = 0;
+ }
/* update settings based on ack if received */
- if (transport_parsing->settings_ack_received)
- {
- memcpy (transport_global->settings[GRPC_ACKED_SETTINGS], transport_global->settings[GRPC_SENT_SETTINGS], GRPC_CHTTP2_NUM_SETTINGS * sizeof (gpr_uint32));
- transport_parsing->settings_ack_received = 0;
- }
+ if (transport_parsing->settings_ack_received) {
+ memcpy(transport_global->settings[GRPC_ACKED_SETTINGS],
+ transport_global->settings[GRPC_SENT_SETTINGS],
+ GRPC_CHTTP2_NUM_SETTINGS * sizeof(gpr_uint32));
+ transport_parsing->settings_ack_received = 0;
+ }
/* move goaway to the global state if we received one (it will be
published later */
- if (transport_parsing->goaway_received)
- {
- grpc_chttp2_add_incoming_goaway (exec_ctx,transport_global, ( gpr_uint32) transport_parsing->goaway_error, transport_parsing->goaway_text);
- transport_parsing->goaway_text = gpr_empty_slice ();
- transport_parsing->goaway_received = 0;
- }
+ if (transport_parsing->goaway_received) {
+ grpc_chttp2_add_incoming_goaway(exec_ctx, transport_global,
+ (gpr_uint32)transport_parsing->goaway_error,
+ transport_parsing->goaway_text);
+ transport_parsing->goaway_text = gpr_empty_slice();
+ transport_parsing->goaway_received = 0;
+ }
/* propagate flow control tokens to global state */
- if (transport_parsing->outgoing_window_update)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("parsed", transport_global, outgoing_window, transport_parsing->outgoing_window_update);
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("parsed", transport_parsing, outgoing_window_update, -(gpr_int64) transport_parsing->outgoing_window_update);
- transport_global->outgoing_window += transport_parsing->outgoing_window_update;
- transport_parsing->outgoing_window_update = 0;
- }
-
- if (transport_parsing->incoming_window_delta)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("parsed", transport_global, incoming_window, -(gpr_int64) transport_parsing->incoming_window_delta);
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("parsed", transport_parsing, incoming_window_delta, -(gpr_int64) transport_parsing->incoming_window_delta);
- transport_global->incoming_window -= transport_parsing->incoming_window_delta;
- transport_parsing->incoming_window_delta = 0;
- }
+ if (transport_parsing->outgoing_window_update) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "parsed", transport_global, outgoing_window,
+ transport_parsing->outgoing_window_update);
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "parsed", transport_parsing, outgoing_window_update,
+ -(gpr_int64)transport_parsing->outgoing_window_update);
+ transport_global->outgoing_window +=
+ transport_parsing->outgoing_window_update;
+ transport_parsing->outgoing_window_update = 0;
+ }
+
+ if (transport_parsing->incoming_window_delta) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "parsed", transport_global, incoming_window,
+ -(gpr_int64)transport_parsing->incoming_window_delta);
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "parsed", transport_parsing, incoming_window_delta,
+ -(gpr_int64)transport_parsing->incoming_window_delta);
+ transport_global->incoming_window -=
+ transport_parsing->incoming_window_delta;
+ transport_parsing->incoming_window_delta = 0;
+ }
/* for each stream that saw an update, fixup global state */
- while (grpc_chttp2_list_pop_parsing_seen_stream (transport_global, transport_parsing, &stream_global, &stream_parsing))
- {
- /* update incoming flow control window */
- if (stream_parsing->incoming_window_delta)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("parsed", transport_parsing, stream_global, incoming_window, -(gpr_int64) stream_parsing->incoming_window_delta);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("parsed", transport_parsing, stream_parsing, incoming_window_delta, -(gpr_int64) stream_parsing->incoming_window_delta);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("parsed", transport_parsing, stream_global, max_recv_bytes, -(gpr_int64) stream_parsing->incoming_window_delta);
- stream_global->incoming_window -= stream_parsing->incoming_window_delta;
- GPR_ASSERT (stream_global->max_recv_bytes >= stream_parsing->incoming_window_delta);
- stream_global->max_recv_bytes -= stream_parsing->incoming_window_delta;
- stream_parsing->incoming_window_delta = 0;
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
- }
-
- /* update outgoing flow control window */
- if (stream_parsing->outgoing_window_update)
- {
- int was_zero = stream_global->outgoing_window <= 0;
- int is_zero;
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("parsed", transport_parsing, stream_global, outgoing_window, stream_parsing->outgoing_window_update);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("parsed", transport_parsing, stream_parsing, outgoing_window_update, -(gpr_int64) stream_parsing->outgoing_window_update);
- GPR_ASSERT (stream_parsing->outgoing_window_update <= GPR_UINT32_MAX);
- stream_global->outgoing_window += (gpr_uint32) stream_parsing->outgoing_window_update;
- stream_parsing->outgoing_window_update = 0;
- is_zero = stream_global->outgoing_window <= 0;
- if (was_zero && !is_zero)
- {
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
- }
- }
-
- /* updating closed status */
- if (stream_parsing->received_close)
- {
- stream_global->read_closed = 1;
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
- }
- if (stream_parsing->saw_rst_stream)
- {
- stream_global->cancelled = 1;
- stream_global->cancelled_status = grpc_chttp2_http2_error_to_grpc_status ((grpc_chttp2_error_code) stream_parsing->rst_stream_reason);
- if (stream_parsing->rst_stream_reason == GRPC_CHTTP2_NO_ERROR)
- {
- stream_global->published_cancelled = 1;
- }
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
- }
-
- /* publish incoming stream ops */
- if (stream_parsing->data_parser.incoming_sopb.nops > 0)
- {
- grpc_incoming_metadata_buffer_move_to_referencing_sopb (&stream_parsing->incoming_metadata, &stream_global->incoming_metadata, &stream_parsing->data_parser.incoming_sopb);
- grpc_sopb_move_to (&stream_parsing->data_parser.incoming_sopb, &stream_global->incoming_sopb);
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
- }
- }
+ while (grpc_chttp2_list_pop_parsing_seen_stream(
+ transport_global, transport_parsing, &stream_global, &stream_parsing)) {
+ /* update incoming flow control window */
+ if (stream_parsing->incoming_window_delta) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "parsed", transport_parsing, stream_global, incoming_window,
+ -(gpr_int64)stream_parsing->incoming_window_delta);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "parsed", transport_parsing, stream_parsing, incoming_window_delta,
+ -(gpr_int64)stream_parsing->incoming_window_delta);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "parsed", transport_parsing, stream_global, max_recv_bytes,
+ -(gpr_int64)stream_parsing->incoming_window_delta);
+ stream_global->incoming_window -= stream_parsing->incoming_window_delta;
+ GPR_ASSERT(stream_global->max_recv_bytes >=
+ stream_parsing->incoming_window_delta);
+ stream_global->max_recv_bytes -= stream_parsing->incoming_window_delta;
+ stream_parsing->incoming_window_delta = 0;
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
+ }
+
+ /* update outgoing flow control window */
+ if (stream_parsing->outgoing_window_update) {
+ int was_zero = stream_global->outgoing_window <= 0;
+ int is_zero;
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("parsed", transport_parsing,
+ stream_global, outgoing_window,
+ stream_parsing->outgoing_window_update);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "parsed", transport_parsing, stream_parsing, outgoing_window_update,
+ -(gpr_int64)stream_parsing->outgoing_window_update);
+ GPR_ASSERT(stream_parsing->outgoing_window_update <= GPR_UINT32_MAX);
+ stream_global->outgoing_window +=
+ (gpr_uint32)stream_parsing->outgoing_window_update;
+ stream_parsing->outgoing_window_update = 0;
+ is_zero = stream_global->outgoing_window <= 0;
+ if (was_zero && !is_zero) {
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
+ }
+ }
+
+ /* updating closed status */
+ if (stream_parsing->received_close) {
+ stream_global->read_closed = 1;
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
+ }
+ if (stream_parsing->saw_rst_stream) {
+ stream_global->cancelled = 1;
+ stream_global->cancelled_status = grpc_chttp2_http2_error_to_grpc_status(
+ (grpc_chttp2_error_code)stream_parsing->rst_stream_reason);
+ if (stream_parsing->rst_stream_reason == GRPC_CHTTP2_NO_ERROR) {
+ stream_global->published_cancelled = 1;
+ }
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
+ }
+
+ /* publish incoming stream ops */
+ if (stream_parsing->data_parser.incoming_sopb.nops > 0) {
+ grpc_incoming_metadata_buffer_move_to_referencing_sopb(
+ &stream_parsing->incoming_metadata, &stream_global->incoming_metadata,
+ &stream_parsing->data_parser.incoming_sopb);
+ grpc_sopb_move_to(&stream_parsing->data_parser.incoming_sopb,
+ &stream_global->incoming_sopb);
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
+ }
+ }
}
-int
-grpc_chttp2_perform_read (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_parsing * transport_parsing, gpr_slice slice)
-{
- gpr_uint8 *beg = GPR_SLICE_START_PTR (slice);
- gpr_uint8 *end = GPR_SLICE_END_PTR (slice);
+int grpc_chttp2_perform_read(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ gpr_slice slice) {
+ gpr_uint8 *beg = GPR_SLICE_START_PTR(slice);
+ gpr_uint8 *end = GPR_SLICE_END_PTR(slice);
gpr_uint8 *cur = beg;
- if (cur == end)
- return 1;
+ if (cur == end) return 1;
- switch (transport_parsing->deframe_state)
- {
+ switch (transport_parsing->deframe_state) {
case GRPC_DTS_CLIENT_PREFIX_0:
case GRPC_DTS_CLIENT_PREFIX_1:
case GRPC_DTS_CLIENT_PREFIX_2:
@@ -237,294 +269,300 @@ grpc_chttp2_perform_read (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_parsin
case GRPC_DTS_CLIENT_PREFIX_21:
case GRPC_DTS_CLIENT_PREFIX_22:
case GRPC_DTS_CLIENT_PREFIX_23:
- while (cur != end && transport_parsing->deframe_state != GRPC_DTS_FH_0)
- {
- if (*cur != GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing->deframe_state])
- {
- gpr_log (GPR_INFO, "Connect string mismatch: expected '%c' (%d) got '%c' (%d) " "at byte %d", GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing->deframe_state], (int) (gpr_uint8) GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing->deframe_state], *cur, (int) *cur, transport_parsing->deframe_state);
- return 0;
- }
- ++cur;
- ++transport_parsing->deframe_state;
- }
- if (cur == end)
- {
- return 1;
- }
- /* fallthrough */
+ while (cur != end && transport_parsing->deframe_state != GRPC_DTS_FH_0) {
+ if (*cur != GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing
+ ->deframe_state]) {
+ gpr_log(GPR_INFO,
+ "Connect string mismatch: expected '%c' (%d) got '%c' (%d) "
+ "at byte %d",
+ GRPC_CHTTP2_CLIENT_CONNECT_STRING[transport_parsing
+ ->deframe_state],
+ (int)(gpr_uint8)GRPC_CHTTP2_CLIENT_CONNECT_STRING
+ [transport_parsing->deframe_state],
+ *cur, (int)*cur, transport_parsing->deframe_state);
+ return 0;
+ }
+ ++cur;
+ ++transport_parsing->deframe_state;
+ }
+ if (cur == end) {
+ return 1;
+ }
+ /* fallthrough */
dts_fh_0:
case GRPC_DTS_FH_0:
- GPR_ASSERT (cur < end);
- transport_parsing->incoming_frame_size = ((gpr_uint32) * cur) << 16;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_1;
- return 1;
- }
- /* fallthrough */
+ GPR_ASSERT(cur < end);
+ transport_parsing->incoming_frame_size = ((gpr_uint32)*cur) << 16;
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_1;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_1:
- GPR_ASSERT (cur < end);
- transport_parsing->incoming_frame_size |= ((gpr_uint32) * cur) << 8;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_2;
- return 1;
- }
- /* fallthrough */
+ GPR_ASSERT(cur < end);
+ transport_parsing->incoming_frame_size |= ((gpr_uint32)*cur) << 8;
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_2;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_2:
- GPR_ASSERT (cur < end);
+ GPR_ASSERT(cur < end);
transport_parsing->incoming_frame_size |= *cur;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_3;
- return 1;
- }
- /* fallthrough */
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_3;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_3:
- GPR_ASSERT (cur < end);
+ GPR_ASSERT(cur < end);
transport_parsing->incoming_frame_type = *cur;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_4;
- return 1;
- }
- /* fallthrough */
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_4;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_4:
- GPR_ASSERT (cur < end);
+ GPR_ASSERT(cur < end);
transport_parsing->incoming_frame_flags = *cur;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_5;
- return 1;
- }
- /* fallthrough */
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_5;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_5:
- GPR_ASSERT (cur < end);
- transport_parsing->incoming_stream_id = (((gpr_uint32) * cur) & 0x7f) << 24;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_6;
- return 1;
- }
- /* fallthrough */
+ GPR_ASSERT(cur < end);
+ transport_parsing->incoming_stream_id = (((gpr_uint32)*cur) & 0x7f) << 24;
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_6;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_6:
- GPR_ASSERT (cur < end);
- transport_parsing->incoming_stream_id |= ((gpr_uint32) * cur) << 16;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_7;
- return 1;
- }
- /* fallthrough */
+ GPR_ASSERT(cur < end);
+ transport_parsing->incoming_stream_id |= ((gpr_uint32)*cur) << 16;
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_7;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_7:
- GPR_ASSERT (cur < end);
- transport_parsing->incoming_stream_id |= ((gpr_uint32) * cur) << 8;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_8;
- return 1;
- }
- /* fallthrough */
+ GPR_ASSERT(cur < end);
+ transport_parsing->incoming_stream_id |= ((gpr_uint32)*cur) << 8;
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_8;
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FH_8:
- GPR_ASSERT (cur < end);
- transport_parsing->incoming_stream_id |= ((gpr_uint32) * cur);
+ GPR_ASSERT(cur < end);
+ transport_parsing->incoming_stream_id |= ((gpr_uint32)*cur);
transport_parsing->deframe_state = GRPC_DTS_FRAME;
- if (!init_frame_parser (transport_parsing))
- {
- return 0;
- }
- if (transport_parsing->incoming_stream_id)
- {
- transport_parsing->last_incoming_stream_id = transport_parsing->incoming_stream_id;
- }
- if (transport_parsing->incoming_frame_size == 0)
- {
- if (!parse_frame_slice (exec_ctx, transport_parsing, gpr_empty_slice (), 1))
- {
- return 0;
- }
- transport_parsing->incoming_stream = NULL;
- if (++cur == end)
- {
- transport_parsing->deframe_state = GRPC_DTS_FH_0;
- return 1;
- }
- goto dts_fh_0; /* loop */
- }
- if (++cur == end)
- {
- return 1;
- }
- /* fallthrough */
+ if (!init_frame_parser(transport_parsing)) {
+ return 0;
+ }
+ if (transport_parsing->incoming_stream_id) {
+ transport_parsing->last_incoming_stream_id =
+ transport_parsing->incoming_stream_id;
+ }
+ if (transport_parsing->incoming_frame_size == 0) {
+ if (!parse_frame_slice(exec_ctx, transport_parsing, gpr_empty_slice(),
+ 1)) {
+ return 0;
+ }
+ transport_parsing->incoming_stream = NULL;
+ if (++cur == end) {
+ transport_parsing->deframe_state = GRPC_DTS_FH_0;
+ return 1;
+ }
+ goto dts_fh_0; /* loop */
+ }
+ if (++cur == end) {
+ return 1;
+ }
+ /* fallthrough */
case GRPC_DTS_FRAME:
- GPR_ASSERT (cur < end);
- if ((gpr_uint32) (end - cur) == transport_parsing->incoming_frame_size)
- {
- if (!parse_frame_slice (exec_ctx, transport_parsing, gpr_slice_sub_no_ref (slice, (size_t) (cur - beg), (size_t) (end - beg)), 1))
- {
- return 0;
- }
- transport_parsing->deframe_state = GRPC_DTS_FH_0;
- transport_parsing->incoming_stream = NULL;
- return 1;
- }
- else if ((gpr_uint32) (end - cur) > transport_parsing->incoming_frame_size)
- {
- size_t cur_offset = (size_t) (cur - beg);
- if (!parse_frame_slice (exec_ctx, transport_parsing, gpr_slice_sub_no_ref (slice, cur_offset, cur_offset + transport_parsing->incoming_frame_size), 1))
- {
- return 0;
- }
- cur += transport_parsing->incoming_frame_size;
- transport_parsing->incoming_stream = NULL;
- goto dts_fh_0; /* loop */
- }
- else
- {
- if (!parse_frame_slice (exec_ctx, transport_parsing, gpr_slice_sub_no_ref (slice, (size_t) (cur - beg), (size_t) (end - beg)), 0))
- {
- return 0;
- }
- transport_parsing->incoming_frame_size -= (gpr_uint32) (end - cur);
- return 1;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
- }
-
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ GPR_ASSERT(cur < end);
+ if ((gpr_uint32)(end - cur) == transport_parsing->incoming_frame_size) {
+ if (!parse_frame_slice(exec_ctx, transport_parsing,
+ gpr_slice_sub_no_ref(slice, (size_t)(cur - beg),
+ (size_t)(end - beg)),
+ 1)) {
+ return 0;
+ }
+ transport_parsing->deframe_state = GRPC_DTS_FH_0;
+ transport_parsing->incoming_stream = NULL;
+ return 1;
+ } else if ((gpr_uint32)(end - cur) >
+ transport_parsing->incoming_frame_size) {
+ size_t cur_offset = (size_t)(cur - beg);
+ if (!parse_frame_slice(
+ exec_ctx, transport_parsing,
+ gpr_slice_sub_no_ref(
+ slice, cur_offset,
+ cur_offset + transport_parsing->incoming_frame_size),
+ 1)) {
+ return 0;
+ }
+ cur += transport_parsing->incoming_frame_size;
+ transport_parsing->incoming_stream = NULL;
+ goto dts_fh_0; /* loop */
+ } else {
+ if (!parse_frame_slice(exec_ctx, transport_parsing,
+ gpr_slice_sub_no_ref(slice, (size_t)(cur - beg),
+ (size_t)(end - beg)),
+ 0)) {
+ return 0;
+ }
+ transport_parsing->incoming_frame_size -= (gpr_uint32)(end - cur);
+ return 1;
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
+ }
+
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
return 0;
}
-static int
-init_frame_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- if (transport_parsing->expect_continuation_stream_id != 0)
- {
- if (transport_parsing->incoming_frame_type != GRPC_CHTTP2_FRAME_CONTINUATION)
- {
- gpr_log (GPR_ERROR, "Expected CONTINUATION frame, got frame type %02x", transport_parsing->incoming_frame_type);
- return 0;
- }
- if (transport_parsing->expect_continuation_stream_id != transport_parsing->incoming_stream_id)
- {
- gpr_log (GPR_ERROR, "Expected CONTINUATION frame for grpc_chttp2_stream %08x, got " "grpc_chttp2_stream %08x", transport_parsing->expect_continuation_stream_id, transport_parsing->incoming_stream_id);
- return 0;
- }
- return init_header_frame_parser (transport_parsing, 1);
+static int init_frame_parser(grpc_chttp2_transport_parsing *transport_parsing) {
+ if (transport_parsing->expect_continuation_stream_id != 0) {
+ if (transport_parsing->incoming_frame_type !=
+ GRPC_CHTTP2_FRAME_CONTINUATION) {
+ gpr_log(GPR_ERROR, "Expected CONTINUATION frame, got frame type %02x",
+ transport_parsing->incoming_frame_type);
+ return 0;
+ }
+ if (transport_parsing->expect_continuation_stream_id !=
+ transport_parsing->incoming_stream_id) {
+ gpr_log(GPR_ERROR,
+ "Expected CONTINUATION frame for grpc_chttp2_stream %08x, got "
+ "grpc_chttp2_stream %08x",
+ transport_parsing->expect_continuation_stream_id,
+ transport_parsing->incoming_stream_id);
+ return 0;
}
- switch (transport_parsing->incoming_frame_type)
- {
+ return init_header_frame_parser(transport_parsing, 1);
+ }
+ switch (transport_parsing->incoming_frame_type) {
case GRPC_CHTTP2_FRAME_DATA:
- return init_data_frame_parser (transport_parsing);
+ return init_data_frame_parser(transport_parsing);
case GRPC_CHTTP2_FRAME_HEADER:
- return init_header_frame_parser (transport_parsing, 0);
+ return init_header_frame_parser(transport_parsing, 0);
case GRPC_CHTTP2_FRAME_CONTINUATION:
- gpr_log (GPR_ERROR, "Unexpected CONTINUATION frame");
+ gpr_log(GPR_ERROR, "Unexpected CONTINUATION frame");
return 0;
case GRPC_CHTTP2_FRAME_RST_STREAM:
- return init_rst_stream_parser (transport_parsing);
+ return init_rst_stream_parser(transport_parsing);
case GRPC_CHTTP2_FRAME_SETTINGS:
- return init_settings_frame_parser (transport_parsing);
+ return init_settings_frame_parser(transport_parsing);
case GRPC_CHTTP2_FRAME_WINDOW_UPDATE:
- return init_window_update_frame_parser (transport_parsing);
+ return init_window_update_frame_parser(transport_parsing);
case GRPC_CHTTP2_FRAME_PING:
- return init_ping_parser (transport_parsing);
+ return init_ping_parser(transport_parsing);
case GRPC_CHTTP2_FRAME_GOAWAY:
- return init_goaway_parser (transport_parsing);
+ return init_goaway_parser(transport_parsing);
default:
- gpr_log (GPR_ERROR, "Unknown frame type %02x", transport_parsing->incoming_frame_type);
- return init_skip_frame_parser (transport_parsing, 0);
- }
+ gpr_log(GPR_ERROR, "Unknown frame type %02x",
+ transport_parsing->incoming_frame_type);
+ return init_skip_frame_parser(transport_parsing, 0);
+ }
}
-static grpc_chttp2_parse_error
-skip_parser (grpc_exec_ctx * exec_ctx, void *parser, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing, gpr_slice slice, int is_last)
-{
+static grpc_chttp2_parse_error skip_parser(
+ grpc_exec_ctx *exec_ctx, void *parser,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last) {
return GRPC_CHTTP2_PARSE_OK;
}
-static void
-skip_header (void *tp, grpc_mdelem * md)
-{
- GRPC_MDELEM_UNREF (md);
-}
-
-static int
-init_skip_frame_parser (grpc_chttp2_transport_parsing * transport_parsing, int is_header)
-{
- if (is_header)
- {
- gpr_uint8 is_eoh = transport_parsing->expect_continuation_stream_id != 0;
- transport_parsing->parser = grpc_chttp2_header_parser_parse;
- transport_parsing->parser_data = &transport_parsing->hpack_parser;
- transport_parsing->hpack_parser.on_header = skip_header;
- transport_parsing->hpack_parser.on_header_user_data = NULL;
- transport_parsing->hpack_parser.is_boundary = is_eoh;
- transport_parsing->hpack_parser.is_eof = (gpr_uint8) (is_eoh ? transport_parsing->header_eof : 0);
- }
- else
- {
- transport_parsing->parser = skip_parser;
- }
+static void skip_header(void *tp, grpc_mdelem *md) { GRPC_MDELEM_UNREF(md); }
+
+static int init_skip_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing, int is_header) {
+ if (is_header) {
+ gpr_uint8 is_eoh = transport_parsing->expect_continuation_stream_id != 0;
+ transport_parsing->parser = grpc_chttp2_header_parser_parse;
+ transport_parsing->parser_data = &transport_parsing->hpack_parser;
+ transport_parsing->hpack_parser.on_header = skip_header;
+ transport_parsing->hpack_parser.on_header_user_data = NULL;
+ transport_parsing->hpack_parser.is_boundary = is_eoh;
+ transport_parsing->hpack_parser.is_eof =
+ (gpr_uint8)(is_eoh ? transport_parsing->header_eof : 0);
+ } else {
+ transport_parsing->parser = skip_parser;
+ }
return 1;
}
-void
-grpc_chttp2_parsing_become_skip_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- init_skip_frame_parser (transport_parsing, transport_parsing->parser == grpc_chttp2_header_parser_parse);
+void grpc_chttp2_parsing_become_skip_parser(
+ grpc_chttp2_transport_parsing *transport_parsing) {
+ init_skip_frame_parser(
+ transport_parsing,
+ transport_parsing->parser == grpc_chttp2_header_parser_parse);
}
-static grpc_chttp2_parse_error
-update_incoming_window (grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing)
-{
- if (transport_parsing->incoming_frame_size > transport_parsing->incoming_window)
- {
- gpr_log (GPR_ERROR, "frame of size %d overflows incoming window of %d", transport_parsing->incoming_frame_size, transport_parsing->incoming_window);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
-
- if (transport_parsing->incoming_frame_size > stream_parsing->incoming_window)
- {
- gpr_log (GPR_ERROR, "frame of size %d overflows incoming window of %d", transport_parsing->incoming_frame_size, stream_parsing->incoming_window);
- return GRPC_CHTTP2_CONNECTION_ERROR;
- }
-
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("data", transport_parsing, incoming_window, -(gpr_int64) transport_parsing->incoming_frame_size);
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("data", transport_parsing, incoming_window_delta, transport_parsing->incoming_frame_size);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("data", transport_parsing, stream_parsing, incoming_window, -(gpr_int64) transport_parsing->incoming_frame_size);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("data", transport_parsing, stream_parsing, incoming_window_delta, transport_parsing->incoming_frame_size);
+static grpc_chttp2_parse_error update_incoming_window(
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing) {
+ if (transport_parsing->incoming_frame_size >
+ transport_parsing->incoming_window) {
+ gpr_log(GPR_ERROR, "frame of size %d overflows incoming window of %d",
+ transport_parsing->incoming_frame_size,
+ transport_parsing->incoming_window);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
+
+ if (transport_parsing->incoming_frame_size >
+ stream_parsing->incoming_window) {
+ gpr_log(GPR_ERROR, "frame of size %d overflows incoming window of %d",
+ transport_parsing->incoming_frame_size,
+ stream_parsing->incoming_window);
+ return GRPC_CHTTP2_CONNECTION_ERROR;
+ }
+
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "data", transport_parsing, incoming_window,
+ -(gpr_int64)transport_parsing->incoming_frame_size);
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("data", transport_parsing,
+ incoming_window_delta,
+ transport_parsing->incoming_frame_size);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "data", transport_parsing, stream_parsing, incoming_window,
+ -(gpr_int64)transport_parsing->incoming_frame_size);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("data", transport_parsing, stream_parsing,
+ incoming_window_delta,
+ transport_parsing->incoming_frame_size);
transport_parsing->incoming_window -= transport_parsing->incoming_frame_size;
- transport_parsing->incoming_window_delta += transport_parsing->incoming_frame_size;
+ transport_parsing->incoming_window_delta +=
+ transport_parsing->incoming_frame_size;
stream_parsing->incoming_window -= transport_parsing->incoming_frame_size;
- stream_parsing->incoming_window_delta += transport_parsing->incoming_frame_size;
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
+ stream_parsing->incoming_window_delta +=
+ transport_parsing->incoming_frame_size;
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, stream_parsing);
return GRPC_CHTTP2_PARSE_OK;
}
-static int
-init_data_frame_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- grpc_chttp2_stream_parsing *stream_parsing = grpc_chttp2_parsing_lookup_stream (transport_parsing,
- transport_parsing->incoming_stream_id);
+static int init_data_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing) {
+ grpc_chttp2_stream_parsing *stream_parsing =
+ grpc_chttp2_parsing_lookup_stream(transport_parsing,
+ transport_parsing->incoming_stream_id);
grpc_chttp2_parse_error err = GRPC_CHTTP2_PARSE_OK;
if (!stream_parsing || stream_parsing->received_close)
- return init_skip_frame_parser (transport_parsing, 0);
- if (err == GRPC_CHTTP2_PARSE_OK)
- {
- err = update_incoming_window (transport_parsing, stream_parsing);
- }
- if (err == GRPC_CHTTP2_PARSE_OK)
- {
- err = grpc_chttp2_data_parser_begin_frame (&stream_parsing->data_parser, transport_parsing->incoming_frame_flags);
- }
- switch (err)
- {
+ return init_skip_frame_parser(transport_parsing, 0);
+ if (err == GRPC_CHTTP2_PARSE_OK) {
+ err = update_incoming_window(transport_parsing, stream_parsing);
+ }
+ if (err == GRPC_CHTTP2_PARSE_OK) {
+ err = grpc_chttp2_data_parser_begin_frame(
+ &stream_parsing->data_parser, transport_parsing->incoming_frame_flags);
+ }
+ switch (err) {
case GRPC_CHTTP2_PARSE_OK:
transport_parsing->incoming_stream = stream_parsing;
transport_parsing->parser = grpc_chttp2_data_parser_parse;
@@ -534,214 +572,215 @@ init_data_frame_parser (grpc_chttp2_transport_parsing * transport_parsing)
stream_parsing->received_close = 1;
stream_parsing->saw_rst_stream = 1;
stream_parsing->rst_stream_reason = GRPC_CHTTP2_PROTOCOL_ERROR;
- gpr_slice_buffer_add (&transport_parsing->qbuf, grpc_chttp2_rst_stream_create (transport_parsing->incoming_stream_id, GRPC_CHTTP2_PROTOCOL_ERROR));
- return init_skip_frame_parser (transport_parsing, 0);
+ gpr_slice_buffer_add(
+ &transport_parsing->qbuf,
+ grpc_chttp2_rst_stream_create(transport_parsing->incoming_stream_id,
+ GRPC_CHTTP2_PROTOCOL_ERROR));
+ return init_skip_frame_parser(transport_parsing, 0);
case GRPC_CHTTP2_CONNECTION_ERROR:
return 0;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
return 0;
}
-static void
-free_timeout (void *p)
-{
- gpr_free (p);
-}
+static void free_timeout(void *p) { gpr_free(p); }
-static void
-on_header (void *tp, grpc_mdelem * md)
-{
+static void on_header(void *tp, grpc_mdelem *md) {
grpc_chttp2_transport_parsing *transport_parsing = tp;
- grpc_chttp2_stream_parsing *stream_parsing = transport_parsing->incoming_stream;
-
- GPR_ASSERT (stream_parsing);
-
- GRPC_CHTTP2_IF_TRACING (gpr_log (GPR_INFO, "HTTP:%d:HDR:%s: %s: %s", stream_parsing->id, transport_parsing->is_client ? "CLI" : "SVR", grpc_mdstr_as_c_string (md->key), grpc_mdstr_as_c_string (md->value)));
-
- if (md->key == transport_parsing->str_grpc_timeout)
- {
- gpr_timespec *cached_timeout = grpc_mdelem_get_user_data (md, free_timeout);
- if (!cached_timeout)
- {
- /* not already parsed: parse it now, and store the result away */
- cached_timeout = gpr_malloc (sizeof (gpr_timespec));
- if (!grpc_chttp2_decode_timeout (grpc_mdstr_as_c_string (md->value), cached_timeout))
- {
- gpr_log (GPR_ERROR, "Ignoring bad timeout value '%s'", grpc_mdstr_as_c_string (md->value));
- *cached_timeout = gpr_inf_future (GPR_CLOCK_REALTIME);
- }
- grpc_mdelem_set_user_data (md, free_timeout, cached_timeout);
- }
- grpc_chttp2_incoming_metadata_buffer_set_deadline (&stream_parsing->incoming_metadata, gpr_time_add (gpr_now (GPR_CLOCK_MONOTONIC), *cached_timeout));
- GRPC_MDELEM_UNREF (md);
- }
- else
- {
- grpc_chttp2_incoming_metadata_buffer_add (&stream_parsing->incoming_metadata, md);
- }
-
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
+ grpc_chttp2_stream_parsing *stream_parsing =
+ transport_parsing->incoming_stream;
+
+ GPR_ASSERT(stream_parsing);
+
+ GRPC_CHTTP2_IF_TRACING(gpr_log(
+ GPR_INFO, "HTTP:%d:HDR:%s: %s: %s", stream_parsing->id,
+ transport_parsing->is_client ? "CLI" : "SVR",
+ grpc_mdstr_as_c_string(md->key), grpc_mdstr_as_c_string(md->value)));
+
+ if (md->key == transport_parsing->str_grpc_timeout) {
+ gpr_timespec *cached_timeout = grpc_mdelem_get_user_data(md, free_timeout);
+ if (!cached_timeout) {
+ /* not already parsed: parse it now, and store the result away */
+ cached_timeout = gpr_malloc(sizeof(gpr_timespec));
+ if (!grpc_chttp2_decode_timeout(grpc_mdstr_as_c_string(md->value),
+ cached_timeout)) {
+ gpr_log(GPR_ERROR, "Ignoring bad timeout value '%s'",
+ grpc_mdstr_as_c_string(md->value));
+ *cached_timeout = gpr_inf_future(GPR_CLOCK_REALTIME);
+ }
+ grpc_mdelem_set_user_data(md, free_timeout, cached_timeout);
+ }
+ grpc_chttp2_incoming_metadata_buffer_set_deadline(
+ &stream_parsing->incoming_metadata,
+ gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), *cached_timeout));
+ GRPC_MDELEM_UNREF(md);
+ } else {
+ grpc_chttp2_incoming_metadata_buffer_add(&stream_parsing->incoming_metadata,
+ md);
+ }
+
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing, stream_parsing);
}
-static int
-init_header_frame_parser (grpc_chttp2_transport_parsing * transport_parsing, int is_continuation)
-{
- gpr_uint8 is_eoh = (transport_parsing->incoming_frame_flags & GRPC_CHTTP2_DATA_FLAG_END_HEADERS) != 0;
+static int init_header_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing, int is_continuation) {
+ gpr_uint8 is_eoh = (transport_parsing->incoming_frame_flags &
+ GRPC_CHTTP2_DATA_FLAG_END_HEADERS) != 0;
int via_accept = 0;
grpc_chttp2_stream_parsing *stream_parsing;
- if (is_eoh)
- {
- transport_parsing->expect_continuation_stream_id = 0;
- }
- else
- {
- transport_parsing->expect_continuation_stream_id = transport_parsing->incoming_stream_id;
- }
+ if (is_eoh) {
+ transport_parsing->expect_continuation_stream_id = 0;
+ } else {
+ transport_parsing->expect_continuation_stream_id =
+ transport_parsing->incoming_stream_id;
+ }
- if (!is_continuation)
- {
- transport_parsing->header_eof = (transport_parsing->incoming_frame_flags & GRPC_CHTTP2_DATA_FLAG_END_STREAM) != 0;
- }
+ if (!is_continuation) {
+ transport_parsing->header_eof = (transport_parsing->incoming_frame_flags &
+ GRPC_CHTTP2_DATA_FLAG_END_STREAM) != 0;
+ }
/* could be a new grpc_chttp2_stream or an existing grpc_chttp2_stream */
- stream_parsing = grpc_chttp2_parsing_lookup_stream (transport_parsing, transport_parsing->incoming_stream_id);
- if (stream_parsing == NULL)
- {
- if (is_continuation)
- {
- gpr_log (GPR_ERROR, "grpc_chttp2_stream disbanded before CONTINUATION received");
- return init_skip_frame_parser (transport_parsing, 1);
- }
- if (transport_parsing->is_client)
- {
- if ((transport_parsing->incoming_stream_id & 1) && transport_parsing->incoming_stream_id < transport_parsing->next_stream_id)
- {
- /* this is an old (probably cancelled) grpc_chttp2_stream */
- }
- else
- {
- gpr_log (GPR_ERROR, "ignoring new grpc_chttp2_stream creation on client");
- }
- return init_skip_frame_parser (transport_parsing, 1);
- }
- else if (transport_parsing->last_incoming_stream_id > transport_parsing->incoming_stream_id)
- {
- gpr_log (GPR_ERROR, "ignoring out of order new grpc_chttp2_stream request on server; " "last grpc_chttp2_stream " "id=%d, new grpc_chttp2_stream id=%d", transport_parsing->last_incoming_stream_id, transport_parsing->incoming_stream_id);
- return init_skip_frame_parser (transport_parsing, 1);
- }
- else if ((transport_parsing->incoming_stream_id & 1) == 0)
- {
- gpr_log (GPR_ERROR, "ignoring grpc_chttp2_stream with non-client generated index %d", transport_parsing->incoming_stream_id);
- return init_skip_frame_parser (transport_parsing, 1);
- }
- stream_parsing = transport_parsing->incoming_stream = grpc_chttp2_parsing_accept_stream (transport_parsing, transport_parsing->incoming_stream_id);
- if (stream_parsing == NULL)
- {
- gpr_log (GPR_ERROR, "grpc_chttp2_stream not accepted");
- return init_skip_frame_parser (transport_parsing, 1);
- }
- via_accept = 1;
- }
- else
- {
- transport_parsing->incoming_stream = stream_parsing;
- }
- GPR_ASSERT (stream_parsing != NULL && (via_accept == 0 || via_accept == 1));
- if (stream_parsing->received_close)
- {
- gpr_log (GPR_ERROR, "skipping already closed grpc_chttp2_stream header");
- transport_parsing->incoming_stream = NULL;
- return init_skip_frame_parser (transport_parsing, 1);
- }
+ stream_parsing = grpc_chttp2_parsing_lookup_stream(
+ transport_parsing, transport_parsing->incoming_stream_id);
+ if (stream_parsing == NULL) {
+ if (is_continuation) {
+ gpr_log(GPR_ERROR,
+ "grpc_chttp2_stream disbanded before CONTINUATION received");
+ return init_skip_frame_parser(transport_parsing, 1);
+ }
+ if (transport_parsing->is_client) {
+ if ((transport_parsing->incoming_stream_id & 1) &&
+ transport_parsing->incoming_stream_id <
+ transport_parsing->next_stream_id) {
+ /* this is an old (probably cancelled) grpc_chttp2_stream */
+ } else {
+ gpr_log(GPR_ERROR,
+ "ignoring new grpc_chttp2_stream creation on client");
+ }
+ return init_skip_frame_parser(transport_parsing, 1);
+ } else if (transport_parsing->last_incoming_stream_id >
+ transport_parsing->incoming_stream_id) {
+ gpr_log(GPR_ERROR,
+ "ignoring out of order new grpc_chttp2_stream request on server; "
+ "last grpc_chttp2_stream "
+ "id=%d, new grpc_chttp2_stream id=%d",
+ transport_parsing->last_incoming_stream_id,
+ transport_parsing->incoming_stream_id);
+ return init_skip_frame_parser(transport_parsing, 1);
+ } else if ((transport_parsing->incoming_stream_id & 1) == 0) {
+ gpr_log(GPR_ERROR,
+ "ignoring grpc_chttp2_stream with non-client generated index %d",
+ transport_parsing->incoming_stream_id);
+ return init_skip_frame_parser(transport_parsing, 1);
+ }
+ stream_parsing = transport_parsing->incoming_stream =
+ grpc_chttp2_parsing_accept_stream(
+ transport_parsing, transport_parsing->incoming_stream_id);
+ if (stream_parsing == NULL) {
+ gpr_log(GPR_ERROR, "grpc_chttp2_stream not accepted");
+ return init_skip_frame_parser(transport_parsing, 1);
+ }
+ via_accept = 1;
+ } else {
+ transport_parsing->incoming_stream = stream_parsing;
+ }
+ GPR_ASSERT(stream_parsing != NULL && (via_accept == 0 || via_accept == 1));
+ if (stream_parsing->received_close) {
+ gpr_log(GPR_ERROR, "skipping already closed grpc_chttp2_stream header");
+ transport_parsing->incoming_stream = NULL;
+ return init_skip_frame_parser(transport_parsing, 1);
+ }
transport_parsing->parser = grpc_chttp2_header_parser_parse;
transport_parsing->parser_data = &transport_parsing->hpack_parser;
transport_parsing->hpack_parser.on_header = on_header;
transport_parsing->hpack_parser.on_header_user_data = transport_parsing;
transport_parsing->hpack_parser.is_boundary = is_eoh;
- transport_parsing->hpack_parser.is_eof = (gpr_uint8) (is_eoh ? transport_parsing->header_eof : 0);
- if (!is_continuation && (transport_parsing->incoming_frame_flags & GRPC_CHTTP2_FLAG_HAS_PRIORITY))
- {
- grpc_chttp2_hpack_parser_set_has_priority (&transport_parsing->hpack_parser);
- }
+ transport_parsing->hpack_parser.is_eof =
+ (gpr_uint8)(is_eoh ? transport_parsing->header_eof : 0);
+ if (!is_continuation && (transport_parsing->incoming_frame_flags &
+ GRPC_CHTTP2_FLAG_HAS_PRIORITY)) {
+ grpc_chttp2_hpack_parser_set_has_priority(&transport_parsing->hpack_parser);
+ }
return 1;
}
-static int
-init_window_update_frame_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_window_update_parser_begin_frame (&transport_parsing->simple.window_update,
- transport_parsing->incoming_frame_size,
- transport_parsing->incoming_frame_flags);
- if (transport_parsing->incoming_stream_id)
- {
- transport_parsing->incoming_stream = grpc_chttp2_parsing_lookup_stream (transport_parsing, transport_parsing->incoming_stream_id);
- }
+static int init_window_update_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing) {
+ int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_window_update_parser_begin_frame(
+ &transport_parsing->simple.window_update,
+ transport_parsing->incoming_frame_size,
+ transport_parsing->incoming_frame_flags);
+ if (transport_parsing->incoming_stream_id) {
+ transport_parsing->incoming_stream = grpc_chttp2_parsing_lookup_stream(
+ transport_parsing, transport_parsing->incoming_stream_id);
+ }
transport_parsing->parser = grpc_chttp2_window_update_parser_parse;
transport_parsing->parser_data = &transport_parsing->simple.window_update;
return ok;
}
-static int
-init_ping_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_ping_parser_begin_frame (&transport_parsing->simple.ping,
- transport_parsing->incoming_frame_size,
- transport_parsing->incoming_frame_flags);
+static int init_ping_parser(grpc_chttp2_transport_parsing *transport_parsing) {
+ int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_ping_parser_begin_frame(
+ &transport_parsing->simple.ping,
+ transport_parsing->incoming_frame_size,
+ transport_parsing->incoming_frame_flags);
transport_parsing->parser = grpc_chttp2_ping_parser_parse;
transport_parsing->parser_data = &transport_parsing->simple.ping;
return ok;
}
-static int
-init_rst_stream_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_rst_stream_parser_begin_frame (&transport_parsing->simple.rst_stream,
- transport_parsing->incoming_frame_size,
- transport_parsing->incoming_frame_flags);
- transport_parsing->incoming_stream = grpc_chttp2_parsing_lookup_stream (transport_parsing, transport_parsing->incoming_stream_id);
- if (!transport_parsing->incoming_stream)
- {
- return init_skip_frame_parser (transport_parsing, 0);
- }
+static int init_rst_stream_parser(
+ grpc_chttp2_transport_parsing *transport_parsing) {
+ int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_rst_stream_parser_begin_frame(
+ &transport_parsing->simple.rst_stream,
+ transport_parsing->incoming_frame_size,
+ transport_parsing->incoming_frame_flags);
+ transport_parsing->incoming_stream = grpc_chttp2_parsing_lookup_stream(
+ transport_parsing, transport_parsing->incoming_stream_id);
+ if (!transport_parsing->incoming_stream) {
+ return init_skip_frame_parser(transport_parsing, 0);
+ }
transport_parsing->parser = grpc_chttp2_rst_stream_parser_parse;
transport_parsing->parser_data = &transport_parsing->simple.rst_stream;
return ok;
}
-static int
-init_goaway_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
- int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_goaway_parser_begin_frame (&transport_parsing->goaway_parser,
- transport_parsing->incoming_frame_size,
- transport_parsing->incoming_frame_flags);
+static int init_goaway_parser(
+ grpc_chttp2_transport_parsing *transport_parsing) {
+ int ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_goaway_parser_begin_frame(
+ &transport_parsing->goaway_parser,
+ transport_parsing->incoming_frame_size,
+ transport_parsing->incoming_frame_flags);
transport_parsing->parser = grpc_chttp2_goaway_parser_parse;
transport_parsing->parser_data = &transport_parsing->goaway_parser;
return ok;
}
-static int
-init_settings_frame_parser (grpc_chttp2_transport_parsing * transport_parsing)
-{
+static int init_settings_frame_parser(
+ grpc_chttp2_transport_parsing *transport_parsing) {
int ok;
- if (transport_parsing->incoming_stream_id != 0)
- {
- gpr_log (GPR_ERROR, "settings frame received for grpc_chttp2_stream %d", transport_parsing->incoming_stream_id);
- return 0;
- }
-
- ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_settings_parser_begin_frame (&transport_parsing->simple.settings, transport_parsing->incoming_frame_size, transport_parsing->incoming_frame_flags, transport_parsing->settings);
- if (!ok)
- {
- return 0;
- }
- if (transport_parsing->incoming_frame_flags & GRPC_CHTTP2_FLAG_ACK)
- {
- transport_parsing->settings_ack_received = 1;
- }
+ if (transport_parsing->incoming_stream_id != 0) {
+ gpr_log(GPR_ERROR, "settings frame received for grpc_chttp2_stream %d",
+ transport_parsing->incoming_stream_id);
+ return 0;
+ }
+
+ ok = GRPC_CHTTP2_PARSE_OK == grpc_chttp2_settings_parser_begin_frame(
+ &transport_parsing->simple.settings,
+ transport_parsing->incoming_frame_size,
+ transport_parsing->incoming_frame_flags,
+ transport_parsing->settings);
+ if (!ok) {
+ return 0;
+ }
+ if (transport_parsing->incoming_frame_flags & GRPC_CHTTP2_FLAG_ACK) {
+ transport_parsing->settings_ack_received = 1;
+ }
transport_parsing->parser = grpc_chttp2_settings_parser_parse;
transport_parsing->parser_data = &transport_parsing->simple.settings;
return ok;
@@ -753,31 +792,35 @@ static int is_window_update_legal(gpr_int64 window_update, gpr_int64 window) {
}
*/
-static int
-parse_frame_slice (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_parsing * transport_parsing, gpr_slice slice, int is_last)
-{
- grpc_chttp2_stream_parsing *stream_parsing = transport_parsing->incoming_stream;
- switch (transport_parsing->parser (exec_ctx, transport_parsing->parser_data, transport_parsing, stream_parsing, slice, is_last))
- {
+static int parse_frame_slice(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ gpr_slice slice, int is_last) {
+ grpc_chttp2_stream_parsing *stream_parsing =
+ transport_parsing->incoming_stream;
+ switch (transport_parsing->parser(exec_ctx, transport_parsing->parser_data,
+ transport_parsing, stream_parsing, slice,
+ is_last)) {
case GRPC_CHTTP2_PARSE_OK:
- if (stream_parsing)
- {
- grpc_chttp2_list_add_parsing_seen_stream (transport_parsing, stream_parsing);
- }
+ if (stream_parsing) {
+ grpc_chttp2_list_add_parsing_seen_stream(transport_parsing,
+ stream_parsing);
+ }
return 1;
case GRPC_CHTTP2_STREAM_ERROR:
- grpc_chttp2_parsing_become_skip_parser (transport_parsing);
- if (stream_parsing)
- {
- stream_parsing->saw_rst_stream = 1;
- stream_parsing->rst_stream_reason = GRPC_CHTTP2_PROTOCOL_ERROR;
- gpr_slice_buffer_add (&transport_parsing->qbuf, grpc_chttp2_rst_stream_create (transport_parsing->incoming_stream_id, GRPC_CHTTP2_PROTOCOL_ERROR));
- }
+ grpc_chttp2_parsing_become_skip_parser(transport_parsing);
+ if (stream_parsing) {
+ stream_parsing->saw_rst_stream = 1;
+ stream_parsing->rst_stream_reason = GRPC_CHTTP2_PROTOCOL_ERROR;
+ gpr_slice_buffer_add(
+ &transport_parsing->qbuf,
+ grpc_chttp2_rst_stream_create(transport_parsing->incoming_stream_id,
+ GRPC_CHTTP2_PROTOCOL_ERROR));
+ }
return 1;
case GRPC_CHTTP2_CONNECTION_ERROR:
return 0;
- }
- gpr_log (GPR_ERROR, "should never reach here");
- abort ();
+ }
+ gpr_log(GPR_ERROR, "should never reach here");
+ abort();
return 0;
}
diff --git a/src/core/transport/chttp2/status_conversion.c b/src/core/transport/chttp2/status_conversion.c
index 8be4dc6150..bf214b017a 100644
--- a/src/core/transport/chttp2/status_conversion.c
+++ b/src/core/transport/chttp2/status_conversion.c
@@ -33,11 +33,8 @@
#include "src/core/transport/chttp2/status_conversion.h"
-int
-grpc_chttp2_grpc_status_to_http2_error (grpc_status_code status)
-{
- switch (status)
- {
+int grpc_chttp2_grpc_status_to_http2_error(grpc_status_code status) {
+ switch (status) {
case GRPC_STATUS_OK:
return GRPC_CHTTP2_NO_ERROR;
case GRPC_STATUS_CANCELLED:
@@ -50,14 +47,12 @@ grpc_chttp2_grpc_status_to_http2_error (grpc_status_code status)
return GRPC_CHTTP2_REFUSED_STREAM;
default:
return GRPC_CHTTP2_INTERNAL_ERROR;
- }
+ }
}
-grpc_status_code
-grpc_chttp2_http2_error_to_grpc_status (grpc_chttp2_error_code error)
-{
- switch (error)
- {
+grpc_status_code grpc_chttp2_http2_error_to_grpc_status(
+ grpc_chttp2_error_code error) {
+ switch (error) {
case GRPC_CHTTP2_NO_ERROR:
/* should never be received */
return GRPC_STATUS_INTERNAL;
@@ -71,15 +66,12 @@ grpc_chttp2_http2_error_to_grpc_status (grpc_chttp2_error_code error)
return GRPC_STATUS_UNAVAILABLE;
default:
return GRPC_STATUS_INTERNAL;
- }
+ }
}
-grpc_status_code
-grpc_chttp2_http2_status_to_grpc_status (int status)
-{
- switch (status)
- {
- /* these HTTP2 status codes are called out explicitly in status.proto */
+grpc_status_code grpc_chttp2_http2_status_to_grpc_status(int status) {
+ switch (status) {
+ /* these HTTP2 status codes are called out explicitly in status.proto */
case 200:
return GRPC_STATUS_OK;
case 400:
@@ -106,14 +98,12 @@ grpc_chttp2_http2_status_to_grpc_status (int status)
return GRPC_STATUS_UNAVAILABLE;
case 504:
return GRPC_STATUS_DEADLINE_EXCEEDED;
- /* everything else is unknown */
+ /* everything else is unknown */
default:
return GRPC_STATUS_UNKNOWN;
- }
+ }
}
-int
-grpc_chttp2_grpc_status_to_http2_status (grpc_status_code status)
-{
+int grpc_chttp2_grpc_status_to_http2_status(grpc_status_code status) {
return 200;
}
diff --git a/src/core/transport/chttp2/status_conversion.h b/src/core/transport/chttp2/status_conversion.h
index ae5b4960df..0ec5b560b8 100644
--- a/src/core/transport/chttp2/status_conversion.h
+++ b/src/core/transport/chttp2/status_conversion.h
@@ -38,11 +38,13 @@
#include "src/core/transport/chttp2/http2_errors.h"
/* Conversion of grpc status codes to http2 error codes (for RST_STREAM) */
-grpc_chttp2_error_code grpc_chttp2_grpc_status_to_http2_error (grpc_status_code status);
-grpc_status_code grpc_chttp2_http2_error_to_grpc_status (grpc_chttp2_error_code error);
+grpc_chttp2_error_code grpc_chttp2_grpc_status_to_http2_error(
+ grpc_status_code status);
+grpc_status_code grpc_chttp2_http2_error_to_grpc_status(
+ grpc_chttp2_error_code error);
/* Conversion of HTTP status codes (:status) to grpc status codes */
-grpc_status_code grpc_chttp2_http2_status_to_grpc_status (int status);
-int grpc_chttp2_grpc_status_to_http2_status (grpc_status_code status);
+grpc_status_code grpc_chttp2_http2_status_to_grpc_status(int status);
+int grpc_chttp2_grpc_status_to_http2_status(grpc_status_code status);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STATUS_CONVERSION_H */
diff --git a/src/core/transport/chttp2/stream_encoder.c b/src/core/transport/chttp2/stream_encoder.c
index cafd735ba2..eb02ccdec3 100644
--- a/src/core/transport/chttp2/stream_encoder.c
+++ b/src/core/transport/chttp2/stream_encoder.c
@@ -55,11 +55,9 @@
#define MAX_DECODER_SPACE_USAGE 512
/* what kind of frame our we encoding? */
-typedef enum
-{ HEADER, DATA, NONE } frame_type;
+typedef enum { HEADER, DATA, NONE } frame_type;
-typedef struct
-{
+typedef struct {
frame_type cur_frame_type;
/* number of bytes in 'output' when we started the frame - used to calculate
frame length */
@@ -76,30 +74,28 @@ typedef struct
} framer_state;
/* fills p (which is expected to be 9 bytes long) with a data frame header */
-static void
-fill_header (gpr_uint8 * p, gpr_uint8 type, gpr_uint32 id, size_t len, gpr_uint8 flags)
-{
- GPR_ASSERT (len < 16777316);
- *p++ = (gpr_uint8) (len >> 16);
- *p++ = (gpr_uint8) (len >> 8);
- *p++ = (gpr_uint8) (len);
+static void fill_header(gpr_uint8 *p, gpr_uint8 type, gpr_uint32 id, size_t len,
+ gpr_uint8 flags) {
+ GPR_ASSERT(len < 16777316);
+ *p++ = (gpr_uint8)(len >> 16);
+ *p++ = (gpr_uint8)(len >> 8);
+ *p++ = (gpr_uint8)(len);
*p++ = type;
*p++ = flags;
- *p++ = (gpr_uint8) (id >> 24);
- *p++ = (gpr_uint8) (id >> 16);
- *p++ = (gpr_uint8) (id >> 8);
- *p++ = (gpr_uint8) (id);
+ *p++ = (gpr_uint8)(id >> 24);
+ *p++ = (gpr_uint8)(id >> 16);
+ *p++ = (gpr_uint8)(id >> 8);
+ *p++ = (gpr_uint8)(id);
}
/* finish a frame - fill in the previously reserved header */
-static void
-finish_frame (framer_state * st, int is_header_boundary, int is_last_in_stream)
-{
+static void finish_frame(framer_state *st, int is_header_boundary,
+ int is_last_in_stream) {
gpr_uint8 type = 0xff;
- switch (st->cur_frame_type)
- {
+ switch (st->cur_frame_type) {
case HEADER:
- type = st->last_was_header ? GRPC_CHTTP2_FRAME_CONTINUATION : GRPC_CHTTP2_FRAME_HEADER;
+ type = st->last_was_header ? GRPC_CHTTP2_FRAME_CONTINUATION
+ : GRPC_CHTTP2_FRAME_HEADER;
st->last_was_header = 1;
break;
case DATA:
@@ -109,435 +105,397 @@ finish_frame (framer_state * st, int is_header_boundary, int is_last_in_stream)
break;
case NONE:
return;
- }
- fill_header (GPR_SLICE_START_PTR (st->output->slices[st->header_idx]), type, st->stream_id, st->output->length - st->output_length_at_start_of_frame, (gpr_uint8) ((is_last_in_stream ? GRPC_CHTTP2_DATA_FLAG_END_STREAM : 0) | (is_header_boundary ? GRPC_CHTTP2_DATA_FLAG_END_HEADERS : 0)));
+ }
+ fill_header(
+ GPR_SLICE_START_PTR(st->output->slices[st->header_idx]), type,
+ st->stream_id, st->output->length - st->output_length_at_start_of_frame,
+ (gpr_uint8)(
+ (is_last_in_stream ? GRPC_CHTTP2_DATA_FLAG_END_STREAM : 0) |
+ (is_header_boundary ? GRPC_CHTTP2_DATA_FLAG_END_HEADERS : 0)));
st->cur_frame_type = NONE;
}
/* begin a new frame: reserve off header space, remember how many bytes we'd
output before beginning */
-static void
-begin_frame (framer_state * st, frame_type type)
-{
- GPR_ASSERT (type != NONE);
- GPR_ASSERT (st->cur_frame_type == NONE);
+static void begin_frame(framer_state *st, frame_type type) {
+ GPR_ASSERT(type != NONE);
+ GPR_ASSERT(st->cur_frame_type == NONE);
st->cur_frame_type = type;
- st->header_idx = gpr_slice_buffer_add_indexed (st->output, gpr_slice_malloc (9));
+ st->header_idx =
+ gpr_slice_buffer_add_indexed(st->output, gpr_slice_malloc(9));
st->output_length_at_start_of_frame = st->output->length;
}
-static void
-begin_new_frame (framer_state * st, frame_type type)
-{
- finish_frame (st, 1, 0);
+static void begin_new_frame(framer_state *st, frame_type type) {
+ finish_frame(st, 1, 0);
st->last_was_header = 0;
- begin_frame (st, type);
+ begin_frame(st, type);
}
/* make sure that the current frame is of the type desired, and has sufficient
space to add at least about_to_add bytes -- finishes the current frame if
needed */
-static void
-ensure_frame_type (framer_state * st, frame_type type, size_t need_bytes)
-{
- if (st->cur_frame_type == type && st->output->length - st->output_length_at_start_of_frame + need_bytes <= GRPC_CHTTP2_MAX_PAYLOAD_LENGTH)
- {
- return;
- }
- finish_frame (st, type != HEADER, 0);
- begin_frame (st, type);
+static void ensure_frame_type(framer_state *st, frame_type type,
+ size_t need_bytes) {
+ if (st->cur_frame_type == type &&
+ st->output->length - st->output_length_at_start_of_frame + need_bytes <=
+ GRPC_CHTTP2_MAX_PAYLOAD_LENGTH) {
+ return;
+ }
+ finish_frame(st, type != HEADER, 0);
+ begin_frame(st, type);
}
/* increment a filter count, halve all counts if one element reaches max */
-static void
-inc_filter (gpr_uint8 idx, gpr_uint32 * sum, gpr_uint8 * elems)
-{
+static void inc_filter(gpr_uint8 idx, gpr_uint32 *sum, gpr_uint8 *elems) {
elems[idx]++;
- if (elems[idx] < 255)
- {
- (*sum)++;
- }
- else
- {
- int i;
- *sum = 0;
- for (i = 0; i < GRPC_CHTTP2_HPACKC_NUM_FILTERS; i++)
- {
- elems[i] /= 2;
- (*sum) += elems[i];
- }
- }
+ if (elems[idx] < 255) {
+ (*sum)++;
+ } else {
+ int i;
+ *sum = 0;
+ for (i = 0; i < GRPC_CHTTP2_HPACKC_NUM_FILTERS; i++) {
+ elems[i] /= 2;
+ (*sum) += elems[i];
+ }
+ }
}
-static void
-add_header_data (framer_state * st, gpr_slice slice)
-{
- size_t len = GPR_SLICE_LENGTH (slice);
+static void add_header_data(framer_state *st, gpr_slice slice) {
+ size_t len = GPR_SLICE_LENGTH(slice);
size_t remaining;
- if (len == 0)
- return;
- ensure_frame_type (st, HEADER, 1);
- remaining = GRPC_CHTTP2_MAX_PAYLOAD_LENGTH + st->output_length_at_start_of_frame - st->output->length;
- if (len <= remaining)
- {
- gpr_slice_buffer_add (st->output, slice);
- }
- else
- {
- gpr_slice_buffer_add (st->output, gpr_slice_split_head (&slice, remaining));
- add_header_data (st, slice);
- }
+ if (len == 0) return;
+ ensure_frame_type(st, HEADER, 1);
+ remaining = GRPC_CHTTP2_MAX_PAYLOAD_LENGTH +
+ st->output_length_at_start_of_frame - st->output->length;
+ if (len <= remaining) {
+ gpr_slice_buffer_add(st->output, slice);
+ } else {
+ gpr_slice_buffer_add(st->output, gpr_slice_split_head(&slice, remaining));
+ add_header_data(st, slice);
+ }
}
-static gpr_uint8 *
-add_tiny_header_data (framer_state * st, size_t len)
-{
- ensure_frame_type (st, HEADER, len);
- return gpr_slice_buffer_tiny_add (st->output, len);
+static gpr_uint8 *add_tiny_header_data(framer_state *st, size_t len) {
+ ensure_frame_type(st, HEADER, len);
+ return gpr_slice_buffer_tiny_add(st->output, len);
}
/* add an element to the decoder table: returns metadata element to unref */
-static grpc_mdelem *
-add_elem (grpc_chttp2_hpack_compressor * c, grpc_mdelem * elem)
-{
+static grpc_mdelem *add_elem(grpc_chttp2_hpack_compressor *c,
+ grpc_mdelem *elem) {
gpr_uint32 key_hash = elem->key->hash;
- gpr_uint32 elem_hash = GRPC_MDSTR_KV_HASH (key_hash, elem->value->hash);
+ gpr_uint32 elem_hash = GRPC_MDSTR_KV_HASH(key_hash, elem->value->hash);
gpr_uint32 new_index = c->tail_remote_index + c->table_elems + 1;
- size_t elem_size = 32 + GPR_SLICE_LENGTH (elem->key->slice) + GPR_SLICE_LENGTH (elem->value->slice);
+ size_t elem_size = 32 + GPR_SLICE_LENGTH(elem->key->slice) +
+ GPR_SLICE_LENGTH(elem->value->slice);
grpc_mdelem *elem_to_unref;
- GPR_ASSERT (elem_size < 65536);
+ GPR_ASSERT(elem_size < 65536);
/* Reserve space for this element in the remote table: if this overflows
the current table, drop elements until it fits, matching the decompressor
algorithm */
/* TODO(ctiller): constant */
- while (c->table_size + elem_size > 4096)
- {
- c->tail_remote_index++;
- GPR_ASSERT (c->tail_remote_index > 0);
- GPR_ASSERT (c->table_size >= c->table_elem_size[c->tail_remote_index % GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS]);
- GPR_ASSERT (c->table_elems > 0);
- c->table_size = (gpr_uint16) (c->table_size - c->table_elem_size[c->tail_remote_index % GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS]);
- c->table_elems--;
- }
- GPR_ASSERT (c->table_elems < GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS);
- c->table_elem_size[new_index % GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS] = (gpr_uint16) elem_size;
- c->table_size = (gpr_uint16) (c->table_size + elem_size);
+ while (c->table_size + elem_size > 4096) {
+ c->tail_remote_index++;
+ GPR_ASSERT(c->tail_remote_index > 0);
+ GPR_ASSERT(c->table_size >=
+ c->table_elem_size[c->tail_remote_index %
+ GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS]);
+ GPR_ASSERT(c->table_elems > 0);
+ c->table_size =
+ (gpr_uint16)(c->table_size -
+ c->table_elem_size[c->tail_remote_index %
+ GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS]);
+ c->table_elems--;
+ }
+ GPR_ASSERT(c->table_elems < GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS);
+ c->table_elem_size[new_index % GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS] =
+ (gpr_uint16)elem_size;
+ c->table_size = (gpr_uint16)(c->table_size + elem_size);
c->table_elems++;
/* Store this element into {entries,indices}_elem */
- if (c->entries_elems[HASH_FRAGMENT_2 (elem_hash)] == elem)
- {
- /* already there: update with new index */
- c->indices_elems[HASH_FRAGMENT_2 (elem_hash)] = new_index;
- elem_to_unref = elem;
- }
- else if (c->entries_elems[HASH_FRAGMENT_3 (elem_hash)] == elem)
- {
- /* already there (cuckoo): update with new index */
- c->indices_elems[HASH_FRAGMENT_3 (elem_hash)] = new_index;
- elem_to_unref = elem;
- }
- else if (c->entries_elems[HASH_FRAGMENT_2 (elem_hash)] == NULL)
- {
- /* not there, but a free element: add */
- c->entries_elems[HASH_FRAGMENT_2 (elem_hash)] = elem;
- c->indices_elems[HASH_FRAGMENT_2 (elem_hash)] = new_index;
- elem_to_unref = NULL;
- }
- else if (c->entries_elems[HASH_FRAGMENT_3 (elem_hash)] == NULL)
- {
- /* not there (cuckoo), but a free element: add */
- c->entries_elems[HASH_FRAGMENT_3 (elem_hash)] = elem;
- c->indices_elems[HASH_FRAGMENT_3 (elem_hash)] = new_index;
- elem_to_unref = NULL;
- }
- else if (c->indices_elems[HASH_FRAGMENT_2 (elem_hash)] < c->indices_elems[HASH_FRAGMENT_3 (elem_hash)])
- {
- /* not there: replace oldest */
- elem_to_unref = c->entries_elems[HASH_FRAGMENT_2 (elem_hash)];
- c->entries_elems[HASH_FRAGMENT_2 (elem_hash)] = elem;
- c->indices_elems[HASH_FRAGMENT_2 (elem_hash)] = new_index;
- }
- else
- {
- /* not there: replace oldest */
- elem_to_unref = c->entries_elems[HASH_FRAGMENT_3 (elem_hash)];
- c->entries_elems[HASH_FRAGMENT_3 (elem_hash)] = elem;
- c->indices_elems[HASH_FRAGMENT_3 (elem_hash)] = new_index;
- }
+ if (c->entries_elems[HASH_FRAGMENT_2(elem_hash)] == elem) {
+ /* already there: update with new index */
+ c->indices_elems[HASH_FRAGMENT_2(elem_hash)] = new_index;
+ elem_to_unref = elem;
+ } else if (c->entries_elems[HASH_FRAGMENT_3(elem_hash)] == elem) {
+ /* already there (cuckoo): update with new index */
+ c->indices_elems[HASH_FRAGMENT_3(elem_hash)] = new_index;
+ elem_to_unref = elem;
+ } else if (c->entries_elems[HASH_FRAGMENT_2(elem_hash)] == NULL) {
+ /* not there, but a free element: add */
+ c->entries_elems[HASH_FRAGMENT_2(elem_hash)] = elem;
+ c->indices_elems[HASH_FRAGMENT_2(elem_hash)] = new_index;
+ elem_to_unref = NULL;
+ } else if (c->entries_elems[HASH_FRAGMENT_3(elem_hash)] == NULL) {
+ /* not there (cuckoo), but a free element: add */
+ c->entries_elems[HASH_FRAGMENT_3(elem_hash)] = elem;
+ c->indices_elems[HASH_FRAGMENT_3(elem_hash)] = new_index;
+ elem_to_unref = NULL;
+ } else if (c->indices_elems[HASH_FRAGMENT_2(elem_hash)] <
+ c->indices_elems[HASH_FRAGMENT_3(elem_hash)]) {
+ /* not there: replace oldest */
+ elem_to_unref = c->entries_elems[HASH_FRAGMENT_2(elem_hash)];
+ c->entries_elems[HASH_FRAGMENT_2(elem_hash)] = elem;
+ c->indices_elems[HASH_FRAGMENT_2(elem_hash)] = new_index;
+ } else {
+ /* not there: replace oldest */
+ elem_to_unref = c->entries_elems[HASH_FRAGMENT_3(elem_hash)];
+ c->entries_elems[HASH_FRAGMENT_3(elem_hash)] = elem;
+ c->indices_elems[HASH_FRAGMENT_3(elem_hash)] = new_index;
+ }
/* do exactly the same for the key (so we can find by that again too) */
- if (c->entries_keys[HASH_FRAGMENT_2 (key_hash)] == elem->key)
- {
- c->indices_keys[HASH_FRAGMENT_2 (key_hash)] = new_index;
- }
- else if (c->entries_keys[HASH_FRAGMENT_3 (key_hash)] == elem->key)
- {
- c->indices_keys[HASH_FRAGMENT_3 (key_hash)] = new_index;
- }
- else if (c->entries_keys[HASH_FRAGMENT_2 (key_hash)] == NULL)
- {
- c->entries_keys[HASH_FRAGMENT_2 (key_hash)] = GRPC_MDSTR_REF (elem->key);
- c->indices_keys[HASH_FRAGMENT_2 (key_hash)] = new_index;
- }
- else if (c->entries_keys[HASH_FRAGMENT_3 (key_hash)] == NULL)
- {
- c->entries_keys[HASH_FRAGMENT_3 (key_hash)] = GRPC_MDSTR_REF (elem->key);
- c->indices_keys[HASH_FRAGMENT_3 (key_hash)] = new_index;
- }
- else if (c->indices_keys[HASH_FRAGMENT_2 (key_hash)] < c->indices_keys[HASH_FRAGMENT_3 (key_hash)])
- {
- GRPC_MDSTR_UNREF (c->entries_keys[HASH_FRAGMENT_2 (key_hash)]);
- c->entries_keys[HASH_FRAGMENT_2 (key_hash)] = GRPC_MDSTR_REF (elem->key);
- c->indices_keys[HASH_FRAGMENT_2 (key_hash)] = new_index;
- }
- else
- {
- GRPC_MDSTR_UNREF (c->entries_keys[HASH_FRAGMENT_3 (key_hash)]);
- c->entries_keys[HASH_FRAGMENT_3 (key_hash)] = GRPC_MDSTR_REF (elem->key);
- c->indices_keys[HASH_FRAGMENT_3 (key_hash)] = new_index;
- }
+ if (c->entries_keys[HASH_FRAGMENT_2(key_hash)] == elem->key) {
+ c->indices_keys[HASH_FRAGMENT_2(key_hash)] = new_index;
+ } else if (c->entries_keys[HASH_FRAGMENT_3(key_hash)] == elem->key) {
+ c->indices_keys[HASH_FRAGMENT_3(key_hash)] = new_index;
+ } else if (c->entries_keys[HASH_FRAGMENT_2(key_hash)] == NULL) {
+ c->entries_keys[HASH_FRAGMENT_2(key_hash)] = GRPC_MDSTR_REF(elem->key);
+ c->indices_keys[HASH_FRAGMENT_2(key_hash)] = new_index;
+ } else if (c->entries_keys[HASH_FRAGMENT_3(key_hash)] == NULL) {
+ c->entries_keys[HASH_FRAGMENT_3(key_hash)] = GRPC_MDSTR_REF(elem->key);
+ c->indices_keys[HASH_FRAGMENT_3(key_hash)] = new_index;
+ } else if (c->indices_keys[HASH_FRAGMENT_2(key_hash)] <
+ c->indices_keys[HASH_FRAGMENT_3(key_hash)]) {
+ GRPC_MDSTR_UNREF(c->entries_keys[HASH_FRAGMENT_2(key_hash)]);
+ c->entries_keys[HASH_FRAGMENT_2(key_hash)] = GRPC_MDSTR_REF(elem->key);
+ c->indices_keys[HASH_FRAGMENT_2(key_hash)] = new_index;
+ } else {
+ GRPC_MDSTR_UNREF(c->entries_keys[HASH_FRAGMENT_3(key_hash)]);
+ c->entries_keys[HASH_FRAGMENT_3(key_hash)] = GRPC_MDSTR_REF(elem->key);
+ c->indices_keys[HASH_FRAGMENT_3(key_hash)] = new_index;
+ }
return elem_to_unref;
}
-static void
-emit_indexed (grpc_chttp2_hpack_compressor * c, gpr_uint32 index, framer_state * st)
-{
- gpr_uint32 len = GRPC_CHTTP2_VARINT_LENGTH (index, 1);
- GRPC_CHTTP2_WRITE_VARINT (index, 1, 0x80, add_tiny_header_data (st, len), len);
+static void emit_indexed(grpc_chttp2_hpack_compressor *c, gpr_uint32 index,
+ framer_state *st) {
+ gpr_uint32 len = GRPC_CHTTP2_VARINT_LENGTH(index, 1);
+ GRPC_CHTTP2_WRITE_VARINT(index, 1, 0x80, add_tiny_header_data(st, len), len);
}
-static gpr_slice
-get_wire_value (grpc_mdelem * elem, gpr_uint8 * huffman_prefix)
-{
- if (grpc_is_binary_header ((const char *) GPR_SLICE_START_PTR (elem->key->slice), GPR_SLICE_LENGTH (elem->key->slice)))
- {
- *huffman_prefix = 0x80;
- return grpc_mdstr_as_base64_encoded_and_huffman_compressed (elem->value);
- }
+static gpr_slice get_wire_value(grpc_mdelem *elem, gpr_uint8 *huffman_prefix) {
+ if (grpc_is_binary_header((const char *)GPR_SLICE_START_PTR(elem->key->slice),
+ GPR_SLICE_LENGTH(elem->key->slice))) {
+ *huffman_prefix = 0x80;
+ return grpc_mdstr_as_base64_encoded_and_huffman_compressed(elem->value);
+ }
/* TODO(ctiller): opportunistically compress non-binary headers */
*huffman_prefix = 0x00;
return elem->value->slice;
}
-static void
-emit_lithdr_incidx (grpc_chttp2_hpack_compressor * c, gpr_uint32 key_index, grpc_mdelem * elem, framer_state * st)
-{
- gpr_uint32 len_pfx = GRPC_CHTTP2_VARINT_LENGTH (key_index, 2);
+static void emit_lithdr_incidx(grpc_chttp2_hpack_compressor *c,
+ gpr_uint32 key_index, grpc_mdelem *elem,
+ framer_state *st) {
+ gpr_uint32 len_pfx = GRPC_CHTTP2_VARINT_LENGTH(key_index, 2);
gpr_uint8 huffman_prefix;
- gpr_slice value_slice = get_wire_value (elem, &huffman_prefix);
- size_t len_val = GPR_SLICE_LENGTH (value_slice);
+ gpr_slice value_slice = get_wire_value(elem, &huffman_prefix);
+ size_t len_val = GPR_SLICE_LENGTH(value_slice);
gpr_uint32 len_val_len;
- GPR_ASSERT (len_val <= GPR_UINT32_MAX);
- len_val_len = GRPC_CHTTP2_VARINT_LENGTH ((gpr_uint32) len_val, 1);
- GRPC_CHTTP2_WRITE_VARINT (key_index, 2, 0x40, add_tiny_header_data (st, len_pfx), len_pfx);
- GRPC_CHTTP2_WRITE_VARINT ((gpr_uint32) len_val, 1, 0x00, add_tiny_header_data (st, len_val_len), len_val_len);
- add_header_data (st, gpr_slice_ref (value_slice));
+ GPR_ASSERT(len_val <= GPR_UINT32_MAX);
+ len_val_len = GRPC_CHTTP2_VARINT_LENGTH((gpr_uint32)len_val, 1);
+ GRPC_CHTTP2_WRITE_VARINT(key_index, 2, 0x40,
+ add_tiny_header_data(st, len_pfx), len_pfx);
+ GRPC_CHTTP2_WRITE_VARINT((gpr_uint32)len_val, 1, 0x00,
+ add_tiny_header_data(st, len_val_len), len_val_len);
+ add_header_data(st, gpr_slice_ref(value_slice));
}
-static void
-emit_lithdr_noidx (grpc_chttp2_hpack_compressor * c, gpr_uint32 key_index, grpc_mdelem * elem, framer_state * st)
-{
- gpr_uint32 len_pfx = GRPC_CHTTP2_VARINT_LENGTH (key_index, 4);
+static void emit_lithdr_noidx(grpc_chttp2_hpack_compressor *c,
+ gpr_uint32 key_index, grpc_mdelem *elem,
+ framer_state *st) {
+ gpr_uint32 len_pfx = GRPC_CHTTP2_VARINT_LENGTH(key_index, 4);
gpr_uint8 huffman_prefix;
- gpr_slice value_slice = get_wire_value (elem, &huffman_prefix);
- size_t len_val = GPR_SLICE_LENGTH (value_slice);
+ gpr_slice value_slice = get_wire_value(elem, &huffman_prefix);
+ size_t len_val = GPR_SLICE_LENGTH(value_slice);
gpr_uint32 len_val_len;
- GPR_ASSERT (len_val <= GPR_UINT32_MAX);
- len_val_len = GRPC_CHTTP2_VARINT_LENGTH ((gpr_uint32) len_val, 1);
- GRPC_CHTTP2_WRITE_VARINT (key_index, 4, 0x00, add_tiny_header_data (st, len_pfx), len_pfx);
- GRPC_CHTTP2_WRITE_VARINT ((gpr_uint32) len_val, 1, 0x00, add_tiny_header_data (st, len_val_len), len_val_len);
- add_header_data (st, gpr_slice_ref (value_slice));
+ GPR_ASSERT(len_val <= GPR_UINT32_MAX);
+ len_val_len = GRPC_CHTTP2_VARINT_LENGTH((gpr_uint32)len_val, 1);
+ GRPC_CHTTP2_WRITE_VARINT(key_index, 4, 0x00,
+ add_tiny_header_data(st, len_pfx), len_pfx);
+ GRPC_CHTTP2_WRITE_VARINT((gpr_uint32)len_val, 1, 0x00,
+ add_tiny_header_data(st, len_val_len), len_val_len);
+ add_header_data(st, gpr_slice_ref(value_slice));
}
-static void
-emit_lithdr_incidx_v (grpc_chttp2_hpack_compressor * c, grpc_mdelem * elem, framer_state * st)
-{
- gpr_uint32 len_key = (gpr_uint32) GPR_SLICE_LENGTH (elem->key->slice);
+static void emit_lithdr_incidx_v(grpc_chttp2_hpack_compressor *c,
+ grpc_mdelem *elem, framer_state *st) {
+ gpr_uint32 len_key = (gpr_uint32)GPR_SLICE_LENGTH(elem->key->slice);
gpr_uint8 huffman_prefix;
- gpr_slice value_slice = get_wire_value (elem, &huffman_prefix);
- gpr_uint32 len_val = (gpr_uint32) GPR_SLICE_LENGTH (value_slice);
- gpr_uint32 len_key_len = GRPC_CHTTP2_VARINT_LENGTH (len_key, 1);
- gpr_uint32 len_val_len = GRPC_CHTTP2_VARINT_LENGTH (len_val, 1);
- GPR_ASSERT (len_key <= GPR_UINT32_MAX);
- GPR_ASSERT (GPR_SLICE_LENGTH (value_slice) <= GPR_UINT32_MAX);
- *add_tiny_header_data (st, 1) = 0x40;
- GRPC_CHTTP2_WRITE_VARINT (len_key, 1, 0x00, add_tiny_header_data (st, len_key_len), len_key_len);
- add_header_data (st, gpr_slice_ref (elem->key->slice));
- GRPC_CHTTP2_WRITE_VARINT (len_val, 1, huffman_prefix, add_tiny_header_data (st, len_val_len), len_val_len);
- add_header_data (st, gpr_slice_ref (value_slice));
+ gpr_slice value_slice = get_wire_value(elem, &huffman_prefix);
+ gpr_uint32 len_val = (gpr_uint32)GPR_SLICE_LENGTH(value_slice);
+ gpr_uint32 len_key_len = GRPC_CHTTP2_VARINT_LENGTH(len_key, 1);
+ gpr_uint32 len_val_len = GRPC_CHTTP2_VARINT_LENGTH(len_val, 1);
+ GPR_ASSERT(len_key <= GPR_UINT32_MAX);
+ GPR_ASSERT(GPR_SLICE_LENGTH(value_slice) <= GPR_UINT32_MAX);
+ *add_tiny_header_data(st, 1) = 0x40;
+ GRPC_CHTTP2_WRITE_VARINT(len_key, 1, 0x00,
+ add_tiny_header_data(st, len_key_len), len_key_len);
+ add_header_data(st, gpr_slice_ref(elem->key->slice));
+ GRPC_CHTTP2_WRITE_VARINT(len_val, 1, huffman_prefix,
+ add_tiny_header_data(st, len_val_len), len_val_len);
+ add_header_data(st, gpr_slice_ref(value_slice));
}
-static void
-emit_lithdr_noidx_v (grpc_chttp2_hpack_compressor * c, grpc_mdelem * elem, framer_state * st)
-{
- gpr_uint32 len_key = (gpr_uint32) GPR_SLICE_LENGTH (elem->key->slice);
+static void emit_lithdr_noidx_v(grpc_chttp2_hpack_compressor *c,
+ grpc_mdelem *elem, framer_state *st) {
+ gpr_uint32 len_key = (gpr_uint32)GPR_SLICE_LENGTH(elem->key->slice);
gpr_uint8 huffman_prefix;
- gpr_slice value_slice = get_wire_value (elem, &huffman_prefix);
- gpr_uint32 len_val = (gpr_uint32) GPR_SLICE_LENGTH (value_slice);
- gpr_uint32 len_key_len = GRPC_CHTTP2_VARINT_LENGTH (len_key, 1);
- gpr_uint32 len_val_len = GRPC_CHTTP2_VARINT_LENGTH (len_val, 1);
- GPR_ASSERT (len_key <= GPR_UINT32_MAX);
- GPR_ASSERT (GPR_SLICE_LENGTH (value_slice) <= GPR_UINT32_MAX);
- *add_tiny_header_data (st, 1) = 0x00;
- GRPC_CHTTP2_WRITE_VARINT (len_key, 1, 0x00, add_tiny_header_data (st, len_key_len), len_key_len);
- add_header_data (st, gpr_slice_ref (elem->key->slice));
- GRPC_CHTTP2_WRITE_VARINT (len_val, 1, huffman_prefix, add_tiny_header_data (st, len_val_len), len_val_len);
- add_header_data (st, gpr_slice_ref (value_slice));
+ gpr_slice value_slice = get_wire_value(elem, &huffman_prefix);
+ gpr_uint32 len_val = (gpr_uint32)GPR_SLICE_LENGTH(value_slice);
+ gpr_uint32 len_key_len = GRPC_CHTTP2_VARINT_LENGTH(len_key, 1);
+ gpr_uint32 len_val_len = GRPC_CHTTP2_VARINT_LENGTH(len_val, 1);
+ GPR_ASSERT(len_key <= GPR_UINT32_MAX);
+ GPR_ASSERT(GPR_SLICE_LENGTH(value_slice) <= GPR_UINT32_MAX);
+ *add_tiny_header_data(st, 1) = 0x00;
+ GRPC_CHTTP2_WRITE_VARINT(len_key, 1, 0x00,
+ add_tiny_header_data(st, len_key_len), len_key_len);
+ add_header_data(st, gpr_slice_ref(elem->key->slice));
+ GRPC_CHTTP2_WRITE_VARINT(len_val, 1, huffman_prefix,
+ add_tiny_header_data(st, len_val_len), len_val_len);
+ add_header_data(st, gpr_slice_ref(value_slice));
}
-static gpr_uint32
-dynidx (grpc_chttp2_hpack_compressor * c, gpr_uint32 index)
-{
- return 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY + c->tail_remote_index + c->table_elems - index;
+static gpr_uint32 dynidx(grpc_chttp2_hpack_compressor *c, gpr_uint32 index) {
+ return 1 + GRPC_CHTTP2_LAST_STATIC_ENTRY + c->tail_remote_index +
+ c->table_elems - index;
}
/* encode an mdelem; returns metadata element to unref */
-static grpc_mdelem *
-hpack_enc (grpc_chttp2_hpack_compressor * c, grpc_mdelem * elem, framer_state * st)
-{
+static grpc_mdelem *hpack_enc(grpc_chttp2_hpack_compressor *c,
+ grpc_mdelem *elem, framer_state *st) {
gpr_uint32 key_hash = elem->key->hash;
- gpr_uint32 elem_hash = GRPC_MDSTR_KV_HASH (key_hash, elem->value->hash);
+ gpr_uint32 elem_hash = GRPC_MDSTR_KV_HASH(key_hash, elem->value->hash);
size_t decoder_space_usage;
gpr_uint32 indices_key;
int should_add_elem;
- GPR_ASSERT (GPR_SLICE_LENGTH (elem->key->slice) > 0);
- if (GPR_SLICE_START_PTR (elem->key->slice)[0] != ':')
- { /* regular header */
- st->seen_regular_header = 1;
- }
- else if (st->seen_regular_header != 0)
- { /* reserved header */
- gpr_log (GPR_ERROR, "Reserved header (colon-prefixed) happening after regular ones.");
- abort ();
- }
+ GPR_ASSERT(GPR_SLICE_LENGTH(elem->key->slice) > 0);
+ if (GPR_SLICE_START_PTR(elem->key->slice)[0] != ':') { /* regular header */
+ st->seen_regular_header = 1;
+ } else if (st->seen_regular_header != 0) { /* reserved header */
+ gpr_log(GPR_ERROR,
+ "Reserved header (colon-prefixed) happening after regular ones.");
+ abort();
+ }
- inc_filter (HASH_FRAGMENT_1 (elem_hash), &c->filter_elems_sum, c->filter_elems);
+ inc_filter(HASH_FRAGMENT_1(elem_hash), &c->filter_elems_sum, c->filter_elems);
/* is this elem currently in the decoders table? */
- if (c->entries_elems[HASH_FRAGMENT_2 (elem_hash)] == elem && c->indices_elems[HASH_FRAGMENT_2 (elem_hash)] > c->tail_remote_index)
- {
- /* HIT: complete element (first cuckoo hash) */
- emit_indexed (c, dynidx (c, c->indices_elems[HASH_FRAGMENT_2 (elem_hash)]), st);
- return elem;
- }
-
- if (c->entries_elems[HASH_FRAGMENT_3 (elem_hash)] == elem && c->indices_elems[HASH_FRAGMENT_3 (elem_hash)] > c->tail_remote_index)
- {
- /* HIT: complete element (second cuckoo hash) */
- emit_indexed (c, dynidx (c, c->indices_elems[HASH_FRAGMENT_3 (elem_hash)]), st);
- return elem;
- }
+ if (c->entries_elems[HASH_FRAGMENT_2(elem_hash)] == elem &&
+ c->indices_elems[HASH_FRAGMENT_2(elem_hash)] > c->tail_remote_index) {
+ /* HIT: complete element (first cuckoo hash) */
+ emit_indexed(c, dynidx(c, c->indices_elems[HASH_FRAGMENT_2(elem_hash)]),
+ st);
+ return elem;
+ }
+
+ if (c->entries_elems[HASH_FRAGMENT_3(elem_hash)] == elem &&
+ c->indices_elems[HASH_FRAGMENT_3(elem_hash)] > c->tail_remote_index) {
+ /* HIT: complete element (second cuckoo hash) */
+ emit_indexed(c, dynidx(c, c->indices_elems[HASH_FRAGMENT_3(elem_hash)]),
+ st);
+ return elem;
+ }
/* should this elem be in the table? */
- decoder_space_usage = 32 + GPR_SLICE_LENGTH (elem->key->slice) + GPR_SLICE_LENGTH (elem->value->slice);
- should_add_elem = decoder_space_usage < MAX_DECODER_SPACE_USAGE && c->filter_elems[HASH_FRAGMENT_1 (elem_hash)] >= c->filter_elems_sum / ONE_ON_ADD_PROBABILITY;
+ decoder_space_usage = 32 + GPR_SLICE_LENGTH(elem->key->slice) +
+ GPR_SLICE_LENGTH(elem->value->slice);
+ should_add_elem = decoder_space_usage < MAX_DECODER_SPACE_USAGE &&
+ c->filter_elems[HASH_FRAGMENT_1(elem_hash)] >=
+ c->filter_elems_sum / ONE_ON_ADD_PROBABILITY;
/* no hits for the elem... maybe there's a key? */
- indices_key = c->indices_keys[HASH_FRAGMENT_2 (key_hash)];
- if (c->entries_keys[HASH_FRAGMENT_2 (key_hash)] == elem->key && indices_key > c->tail_remote_index)
- {
- /* HIT: key (first cuckoo hash) */
- if (should_add_elem)
- {
- emit_lithdr_incidx (c, dynidx (c, indices_key), elem, st);
- return add_elem (c, elem);
- }
- else
- {
- emit_lithdr_noidx (c, dynidx (c, indices_key), elem, st);
- return elem;
- }
- abort ();
+ indices_key = c->indices_keys[HASH_FRAGMENT_2(key_hash)];
+ if (c->entries_keys[HASH_FRAGMENT_2(key_hash)] == elem->key &&
+ indices_key > c->tail_remote_index) {
+ /* HIT: key (first cuckoo hash) */
+ if (should_add_elem) {
+ emit_lithdr_incidx(c, dynidx(c, indices_key), elem, st);
+ return add_elem(c, elem);
+ } else {
+ emit_lithdr_noidx(c, dynidx(c, indices_key), elem, st);
+ return elem;
}
-
- indices_key = c->indices_keys[HASH_FRAGMENT_3 (key_hash)];
- if (c->entries_keys[HASH_FRAGMENT_3 (key_hash)] == elem->key && indices_key > c->tail_remote_index)
- {
- /* HIT: key (first cuckoo hash) */
- if (should_add_elem)
- {
- emit_lithdr_incidx (c, dynidx (c, indices_key), elem, st);
- return add_elem (c, elem);
- }
- else
- {
- emit_lithdr_noidx (c, dynidx (c, indices_key), elem, st);
- return elem;
- }
- abort ();
+ abort();
+ }
+
+ indices_key = c->indices_keys[HASH_FRAGMENT_3(key_hash)];
+ if (c->entries_keys[HASH_FRAGMENT_3(key_hash)] == elem->key &&
+ indices_key > c->tail_remote_index) {
+ /* HIT: key (first cuckoo hash) */
+ if (should_add_elem) {
+ emit_lithdr_incidx(c, dynidx(c, indices_key), elem, st);
+ return add_elem(c, elem);
+ } else {
+ emit_lithdr_noidx(c, dynidx(c, indices_key), elem, st);
+ return elem;
}
+ abort();
+ }
/* no elem, key in the table... fall back to literal emission */
- if (should_add_elem)
- {
- emit_lithdr_incidx_v (c, elem, st);
- return add_elem (c, elem);
- }
- else
- {
- emit_lithdr_noidx_v (c, elem, st);
- return elem;
- }
- abort ();
+ if (should_add_elem) {
+ emit_lithdr_incidx_v(c, elem, st);
+ return add_elem(c, elem);
+ } else {
+ emit_lithdr_noidx_v(c, elem, st);
+ return elem;
+ }
+ abort();
}
#define STRLEN_LIT(x) (sizeof(x) - 1)
#define TIMEOUT_KEY "grpc-timeout"
-static void
-deadline_enc (grpc_chttp2_hpack_compressor * c, gpr_timespec deadline, framer_state * st)
-{
+static void deadline_enc(grpc_chttp2_hpack_compressor *c, gpr_timespec deadline,
+ framer_state *st) {
char timeout_str[GRPC_CHTTP2_TIMEOUT_ENCODE_MIN_BUFSIZE];
grpc_mdelem *mdelem;
- grpc_chttp2_encode_timeout (gpr_time_sub (deadline, gpr_now (deadline.clock_type)), timeout_str);
- mdelem = grpc_mdelem_from_metadata_strings (c->mdctx, GRPC_MDSTR_REF (c->timeout_key_str), grpc_mdstr_from_string (c->mdctx, timeout_str, 0));
- mdelem = hpack_enc (c, mdelem, st);
- if (mdelem)
- GRPC_MDELEM_UNREF (mdelem);
+ grpc_chttp2_encode_timeout(
+ gpr_time_sub(deadline, gpr_now(deadline.clock_type)), timeout_str);
+ mdelem = grpc_mdelem_from_metadata_strings(
+ c->mdctx, GRPC_MDSTR_REF(c->timeout_key_str),
+ grpc_mdstr_from_string(c->mdctx, timeout_str, 0));
+ mdelem = hpack_enc(c, mdelem, st);
+ if (mdelem) GRPC_MDELEM_UNREF(mdelem);
}
-gpr_slice
-grpc_chttp2_data_frame_create_empty_close (gpr_uint32 id)
-{
- gpr_slice slice = gpr_slice_malloc (9);
- fill_header (GPR_SLICE_START_PTR (slice), GRPC_CHTTP2_FRAME_DATA, id, 0, 1);
+gpr_slice grpc_chttp2_data_frame_create_empty_close(gpr_uint32 id) {
+ gpr_slice slice = gpr_slice_malloc(9);
+ fill_header(GPR_SLICE_START_PTR(slice), GRPC_CHTTP2_FRAME_DATA, id, 0, 1);
return slice;
}
-void
-grpc_chttp2_hpack_compressor_init (grpc_chttp2_hpack_compressor * c, grpc_mdctx * ctx)
-{
- memset (c, 0, sizeof (*c));
+void grpc_chttp2_hpack_compressor_init(grpc_chttp2_hpack_compressor *c,
+ grpc_mdctx *ctx) {
+ memset(c, 0, sizeof(*c));
c->mdctx = ctx;
- c->timeout_key_str = grpc_mdstr_from_string (ctx, "grpc-timeout", 0);
+ c->timeout_key_str = grpc_mdstr_from_string(ctx, "grpc-timeout", 0);
}
-void
-grpc_chttp2_hpack_compressor_destroy (grpc_chttp2_hpack_compressor * c)
-{
+void grpc_chttp2_hpack_compressor_destroy(grpc_chttp2_hpack_compressor *c) {
int i;
- for (i = 0; i < GRPC_CHTTP2_HPACKC_NUM_VALUES; i++)
- {
- if (c->entries_keys[i])
- GRPC_MDSTR_UNREF (c->entries_keys[i]);
- if (c->entries_elems[i])
- GRPC_MDELEM_UNREF (c->entries_elems[i]);
- }
- GRPC_MDSTR_UNREF (c->timeout_key_str);
+ for (i = 0; i < GRPC_CHTTP2_HPACKC_NUM_VALUES; i++) {
+ if (c->entries_keys[i]) GRPC_MDSTR_UNREF(c->entries_keys[i]);
+ if (c->entries_elems[i]) GRPC_MDELEM_UNREF(c->entries_elems[i]);
+ }
+ GRPC_MDSTR_UNREF(c->timeout_key_str);
}
-gpr_uint32
-grpc_chttp2_preencode (grpc_stream_op * inops, size_t * inops_count, gpr_uint32 max_flow_controlled_bytes, grpc_stream_op_buffer * outops)
-{
+gpr_uint32 grpc_chttp2_preencode(grpc_stream_op *inops, size_t *inops_count,
+ gpr_uint32 max_flow_controlled_bytes,
+ grpc_stream_op_buffer *outops) {
gpr_slice slice;
grpc_stream_op *op;
gpr_uint32 max_take_size;
@@ -546,85 +504,78 @@ grpc_chttp2_preencode (grpc_stream_op * inops, size_t * inops_count, gpr_uint32
gpr_uint8 *p;
gpr_uint8 compressed_flag_set = 0;
- while (curop < *inops_count)
- {
- GPR_ASSERT (flow_controlled_bytes_taken <= max_flow_controlled_bytes);
- op = &inops[curop];
- switch (op->type)
- {
- case GRPC_NO_OP:
- /* skip */
- curop++;
- break;
- case GRPC_OP_METADATA:
- grpc_metadata_batch_assert_ok (&op->data.metadata);
- /* these just get copied as they don't impact the number of flow
- controlled bytes */
- grpc_sopb_append (outops, op, 1);
- curop++;
- break;
- case GRPC_OP_BEGIN_MESSAGE:
- /* begin op: for now we just convert the op to a slice and fall
- through - this lets us reuse the slice framing code below */
- compressed_flag_set = (op->data.begin_message.flags & GRPC_WRITE_INTERNAL_COMPRESS) != 0;
- slice = gpr_slice_malloc (5);
-
- p = GPR_SLICE_START_PTR (slice);
- p[0] = compressed_flag_set;
- p[1] = (gpr_uint8) (op->data.begin_message.length >> 24);
- p[2] = (gpr_uint8) (op->data.begin_message.length >> 16);
- p[3] = (gpr_uint8) (op->data.begin_message.length >> 8);
- p[4] = (gpr_uint8) (op->data.begin_message.length);
- op->type = GRPC_OP_SLICE;
- op->data.slice = slice;
- /* fallthrough */
- case GRPC_OP_SLICE:
- slice = op->data.slice;
- if (!GPR_SLICE_LENGTH (slice))
- {
- /* skip zero length slices */
- gpr_slice_unref (slice);
- curop++;
- break;
- }
- max_take_size = max_flow_controlled_bytes - flow_controlled_bytes_taken;
- if (max_take_size == 0)
- {
- goto exit_loop;
- }
- if (GPR_SLICE_LENGTH (slice) > max_take_size)
- {
- slice = gpr_slice_split_head (&op->data.slice, max_take_size);
- grpc_sopb_add_slice (outops, slice);
- }
- else
- {
- /* consume this op immediately */
- grpc_sopb_append (outops, op, 1);
- curop++;
- }
- flow_controlled_bytes_taken += (gpr_uint32) GPR_SLICE_LENGTH (slice);
- break;
- }
- }
+ while (curop < *inops_count) {
+ GPR_ASSERT(flow_controlled_bytes_taken <= max_flow_controlled_bytes);
+ op = &inops[curop];
+ switch (op->type) {
+ case GRPC_NO_OP:
+ /* skip */
+ curop++;
+ break;
+ case GRPC_OP_METADATA:
+ grpc_metadata_batch_assert_ok(&op->data.metadata);
+ /* these just get copied as they don't impact the number of flow
+ controlled bytes */
+ grpc_sopb_append(outops, op, 1);
+ curop++;
+ break;
+ case GRPC_OP_BEGIN_MESSAGE:
+ /* begin op: for now we just convert the op to a slice and fall
+ through - this lets us reuse the slice framing code below */
+ compressed_flag_set =
+ (op->data.begin_message.flags & GRPC_WRITE_INTERNAL_COMPRESS) != 0;
+ slice = gpr_slice_malloc(5);
+
+ p = GPR_SLICE_START_PTR(slice);
+ p[0] = compressed_flag_set;
+ p[1] = (gpr_uint8)(op->data.begin_message.length >> 24);
+ p[2] = (gpr_uint8)(op->data.begin_message.length >> 16);
+ p[3] = (gpr_uint8)(op->data.begin_message.length >> 8);
+ p[4] = (gpr_uint8)(op->data.begin_message.length);
+ op->type = GRPC_OP_SLICE;
+ op->data.slice = slice;
+ /* fallthrough */
+ case GRPC_OP_SLICE:
+ slice = op->data.slice;
+ if (!GPR_SLICE_LENGTH(slice)) {
+ /* skip zero length slices */
+ gpr_slice_unref(slice);
+ curop++;
+ break;
+ }
+ max_take_size = max_flow_controlled_bytes - flow_controlled_bytes_taken;
+ if (max_take_size == 0) {
+ goto exit_loop;
+ }
+ if (GPR_SLICE_LENGTH(slice) > max_take_size) {
+ slice = gpr_slice_split_head(&op->data.slice, max_take_size);
+ grpc_sopb_add_slice(outops, slice);
+ } else {
+ /* consume this op immediately */
+ grpc_sopb_append(outops, op, 1);
+ curop++;
+ }
+ flow_controlled_bytes_taken += (gpr_uint32)GPR_SLICE_LENGTH(slice);
+ break;
+ }
+ }
exit_loop:
*inops_count -= curop;
- memmove (inops, inops + curop, *inops_count * sizeof (grpc_stream_op));
-
- for (curop = 0; curop < *inops_count; curop++)
- {
- if (inops[curop].type == GRPC_OP_METADATA)
- {
- grpc_metadata_batch_assert_ok (&inops[curop].data.metadata);
- }
+ memmove(inops, inops + curop, *inops_count * sizeof(grpc_stream_op));
+
+ for (curop = 0; curop < *inops_count; curop++) {
+ if (inops[curop].type == GRPC_OP_METADATA) {
+ grpc_metadata_batch_assert_ok(&inops[curop].data.metadata);
}
+ }
return flow_controlled_bytes_taken;
}
-void
-grpc_chttp2_encode (grpc_stream_op * ops, size_t ops_count, int eof, gpr_uint32 stream_id, grpc_chttp2_hpack_compressor * compressor, gpr_slice_buffer * output)
-{
+void grpc_chttp2_encode(grpc_stream_op *ops, size_t ops_count, int eof,
+ gpr_uint32 stream_id,
+ grpc_chttp2_hpack_compressor *compressor,
+ gpr_slice_buffer *output) {
framer_state st;
gpr_slice slice;
grpc_stream_op *op;
@@ -636,7 +587,7 @@ grpc_chttp2_encode (grpc_stream_op * ops, size_t ops_count, int eof, gpr_uint32
int need_unref = 0;
gpr_timespec deadline;
- GPR_ASSERT (stream_id != 0);
+ GPR_ASSERT(stream_id != 0);
st.cur_frame_type = NONE;
st.last_was_header = 0;
@@ -644,80 +595,70 @@ grpc_chttp2_encode (grpc_stream_op * ops, size_t ops_count, int eof, gpr_uint32
st.stream_id = stream_id;
st.output = output;
- while (curop < ops_count)
- {
- op = &ops[curop];
- switch (op->type)
- {
- case GRPC_NO_OP:
- case GRPC_OP_BEGIN_MESSAGE:
- gpr_log (GPR_ERROR, "These stream ops should be filtered out by grpc_chttp2_preencode");
- abort ();
- case GRPC_OP_METADATA:
- /* Encode a metadata batch; store the returned values, representing
- a metadata element that needs to be unreffed back into the metadata
- slot. THIS MAY NOT BE THE SAME ELEMENT (if a decoder table slot got
- updated). After this loop, we'll do a batch unref of elements. */
- begin_new_frame (&st, HEADER);
- need_unref |= op->data.metadata.garbage.head != NULL;
- grpc_metadata_batch_assert_ok (&op->data.metadata);
- for (l = op->data.metadata.list.head; l; l = l->next)
- {
- l->md = hpack_enc (compressor, l->md, &st);
- need_unref |= l->md != NULL;
- }
- deadline = op->data.metadata.deadline;
- if (gpr_time_cmp (deadline, gpr_inf_future (deadline.clock_type)) != 0)
- {
- deadline_enc (compressor, deadline, &st);
- }
- curop++;
- break;
- case GRPC_OP_SLICE:
- slice = op->data.slice;
- if (st.cur_frame_type == DATA && st.output->length - st.output_length_at_start_of_frame == GRPC_CHTTP2_MAX_PAYLOAD_LENGTH)
- {
- finish_frame (&st, 0, 0);
- }
- ensure_frame_type (&st, DATA, 1);
- max_take_size = GRPC_CHTTP2_MAX_PAYLOAD_LENGTH + st.output_length_at_start_of_frame - st.output->length;
- if (GPR_SLICE_LENGTH (slice) > max_take_size)
- {
- slice = gpr_slice_split_head (&op->data.slice, max_take_size);
- }
- else
- {
- /* consume this op immediately */
- curop++;
- }
- gpr_slice_buffer_add (output, slice);
- break;
- }
- }
- if (eof && st.cur_frame_type == NONE)
- {
- begin_frame (&st, DATA);
- }
- finish_frame (&st, 1, eof);
-
- if (need_unref)
- {
- grpc_mdctx_lock (mdctx);
- for (unref_op = 0; unref_op < curop; unref_op++)
- {
- op = &ops[unref_op];
- if (op->type != GRPC_OP_METADATA)
- continue;
- for (l = op->data.metadata.list.head; l; l = l->next)
- {
- if (l->md)
- GRPC_MDCTX_LOCKED_MDELEM_UNREF (mdctx, l->md);
- }
- for (l = op->data.metadata.garbage.head; l; l = l->next)
- {
- GRPC_MDCTX_LOCKED_MDELEM_UNREF (mdctx, l->md);
- }
- }
- grpc_mdctx_unlock (mdctx);
- }
+ while (curop < ops_count) {
+ op = &ops[curop];
+ switch (op->type) {
+ case GRPC_NO_OP:
+ case GRPC_OP_BEGIN_MESSAGE:
+ gpr_log(
+ GPR_ERROR,
+ "These stream ops should be filtered out by grpc_chttp2_preencode");
+ abort();
+ case GRPC_OP_METADATA:
+ /* Encode a metadata batch; store the returned values, representing
+ a metadata element that needs to be unreffed back into the metadata
+ slot. THIS MAY NOT BE THE SAME ELEMENT (if a decoder table slot got
+ updated). After this loop, we'll do a batch unref of elements. */
+ begin_new_frame(&st, HEADER);
+ need_unref |= op->data.metadata.garbage.head != NULL;
+ grpc_metadata_batch_assert_ok(&op->data.metadata);
+ for (l = op->data.metadata.list.head; l; l = l->next) {
+ l->md = hpack_enc(compressor, l->md, &st);
+ need_unref |= l->md != NULL;
+ }
+ deadline = op->data.metadata.deadline;
+ if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) != 0) {
+ deadline_enc(compressor, deadline, &st);
+ }
+ curop++;
+ break;
+ case GRPC_OP_SLICE:
+ slice = op->data.slice;
+ if (st.cur_frame_type == DATA &&
+ st.output->length - st.output_length_at_start_of_frame ==
+ GRPC_CHTTP2_MAX_PAYLOAD_LENGTH) {
+ finish_frame(&st, 0, 0);
+ }
+ ensure_frame_type(&st, DATA, 1);
+ max_take_size = GRPC_CHTTP2_MAX_PAYLOAD_LENGTH +
+ st.output_length_at_start_of_frame - st.output->length;
+ if (GPR_SLICE_LENGTH(slice) > max_take_size) {
+ slice = gpr_slice_split_head(&op->data.slice, max_take_size);
+ } else {
+ /* consume this op immediately */
+ curop++;
+ }
+ gpr_slice_buffer_add(output, slice);
+ break;
+ }
+ }
+ if (eof && st.cur_frame_type == NONE) {
+ begin_frame(&st, DATA);
+ }
+ finish_frame(&st, 1, eof);
+
+ if (need_unref) {
+ grpc_mdctx_lock(mdctx);
+ for (unref_op = 0; unref_op < curop; unref_op++) {
+ op = &ops[unref_op];
+ if (op->type != GRPC_OP_METADATA) continue;
+ for (l = op->data.metadata.list.head; l; l = l->next) {
+ if (l->md) GRPC_MDCTX_LOCKED_MDELEM_UNREF(mdctx, l->md);
+ }
+ for (l = op->data.metadata.garbage.head; l; l = l->next) {
+ GRPC_MDCTX_LOCKED_MDELEM_UNREF(mdctx, l->md);
+ }
+ }
+ grpc_mdctx_unlock(mdctx);
+ }
}
diff --git a/src/core/transport/chttp2/stream_encoder.h b/src/core/transport/chttp2/stream_encoder.h
index 32f003d000..db52f2a0f6 100644
--- a/src/core/transport/chttp2/stream_encoder.h
+++ b/src/core/transport/chttp2/stream_encoder.h
@@ -45,8 +45,7 @@
#define GRPC_CHTTP2_HPACKC_NUM_VALUES 256
#define GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS (4096 / 32)
-typedef struct
-{
+typedef struct {
gpr_uint32 filter_elems_sum;
/* one before the lowest usable table index */
gpr_uint32 tail_remote_index;
@@ -75,14 +74,20 @@ typedef struct
gpr_uint16 table_elem_size[GRPC_CHTTP2_HPACKC_MAX_TABLE_ELEMS];
} grpc_chttp2_hpack_compressor;
-void grpc_chttp2_hpack_compressor_init (grpc_chttp2_hpack_compressor * c, grpc_mdctx * mdctx);
-void grpc_chttp2_hpack_compressor_destroy (grpc_chttp2_hpack_compressor * c);
+void grpc_chttp2_hpack_compressor_init(grpc_chttp2_hpack_compressor *c,
+ grpc_mdctx *mdctx);
+void grpc_chttp2_hpack_compressor_destroy(grpc_chttp2_hpack_compressor *c);
/* select stream ops to be encoded, moving them from inops to outops, and
moving subsequent ops in inops forward in the queue */
-gpr_uint32 grpc_chttp2_preencode (grpc_stream_op * inops, size_t * inops_count, gpr_uint32 max_flow_controlled_bytes, grpc_stream_op_buffer * outops);
+gpr_uint32 grpc_chttp2_preencode(grpc_stream_op *inops, size_t *inops_count,
+ gpr_uint32 max_flow_controlled_bytes,
+ grpc_stream_op_buffer *outops);
/* encode stream ops to output */
-void grpc_chttp2_encode (grpc_stream_op * ops, size_t ops_count, int eof, gpr_uint32 stream_id, grpc_chttp2_hpack_compressor * compressor, gpr_slice_buffer * output);
+void grpc_chttp2_encode(grpc_stream_op *ops, size_t ops_count, int eof,
+ gpr_uint32 stream_id,
+ grpc_chttp2_hpack_compressor *compressor,
+ gpr_slice_buffer *output);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STREAM_ENCODER_H */
diff --git a/src/core/transport/chttp2/stream_lists.c b/src/core/transport/chttp2/stream_lists.c
index 43d7b0b39f..781db7b0d6 100644
--- a/src/core/transport/chttp2/stream_lists.c
+++ b/src/core/transport/chttp2/stream_lists.c
@@ -58,348 +58,346 @@
/* core list management */
-static int
-stream_list_empty (grpc_chttp2_transport * t, grpc_chttp2_stream_list_id id)
-{
+static int stream_list_empty(grpc_chttp2_transport *t,
+ grpc_chttp2_stream_list_id id) {
return t->lists[id].head == NULL;
}
-static int
-stream_list_pop (grpc_chttp2_transport * t, grpc_chttp2_stream ** stream, grpc_chttp2_stream_list_id id)
-{
+static int stream_list_pop(grpc_chttp2_transport *t,
+ grpc_chttp2_stream **stream,
+ grpc_chttp2_stream_list_id id) {
grpc_chttp2_stream *s = t->lists[id].head;
- if (s)
- {
- grpc_chttp2_stream *new_head = s->links[id].next;
- GPR_ASSERT (s->included[id]);
- if (new_head)
- {
- t->lists[id].head = new_head;
- new_head->links[id].prev = NULL;
- }
- else
- {
- t->lists[id].head = NULL;
- t->lists[id].tail = NULL;
- }
- s->included[id] = 0;
+ if (s) {
+ grpc_chttp2_stream *new_head = s->links[id].next;
+ GPR_ASSERT(s->included[id]);
+ if (new_head) {
+ t->lists[id].head = new_head;
+ new_head->links[id].prev = NULL;
+ } else {
+ t->lists[id].head = NULL;
+ t->lists[id].tail = NULL;
}
+ s->included[id] = 0;
+ }
*stream = s;
return s != 0;
}
-static void
-stream_list_remove (grpc_chttp2_transport * t, grpc_chttp2_stream * s, grpc_chttp2_stream_list_id id)
-{
- GPR_ASSERT (s->included[id]);
+static void stream_list_remove(grpc_chttp2_transport *t, grpc_chttp2_stream *s,
+ grpc_chttp2_stream_list_id id) {
+ GPR_ASSERT(s->included[id]);
s->included[id] = 0;
- if (s->links[id].prev)
- {
- s->links[id].prev->links[id].next = s->links[id].next;
- }
- else
- {
- GPR_ASSERT (t->lists[id].head == s);
- t->lists[id].head = s->links[id].next;
- }
- if (s->links[id].next)
- {
- s->links[id].next->links[id].prev = s->links[id].prev;
- }
- else
- {
- t->lists[id].tail = s->links[id].prev;
- }
-}
-
-static void
-stream_list_maybe_remove (grpc_chttp2_transport * t, grpc_chttp2_stream * s, grpc_chttp2_stream_list_id id)
-{
- if (s->included[id])
- {
- stream_list_remove (t, s, id);
- }
-}
-
-static void
-stream_list_add_head (grpc_chttp2_transport * t, grpc_chttp2_stream * s, grpc_chttp2_stream_list_id id)
-{
+ if (s->links[id].prev) {
+ s->links[id].prev->links[id].next = s->links[id].next;
+ } else {
+ GPR_ASSERT(t->lists[id].head == s);
+ t->lists[id].head = s->links[id].next;
+ }
+ if (s->links[id].next) {
+ s->links[id].next->links[id].prev = s->links[id].prev;
+ } else {
+ t->lists[id].tail = s->links[id].prev;
+ }
+}
+
+static void stream_list_maybe_remove(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s,
+ grpc_chttp2_stream_list_id id) {
+ if (s->included[id]) {
+ stream_list_remove(t, s, id);
+ }
+}
+
+static void stream_list_add_head(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s,
+ grpc_chttp2_stream_list_id id) {
grpc_chttp2_stream *old_head;
- GPR_ASSERT (!s->included[id]);
+ GPR_ASSERT(!s->included[id]);
old_head = t->lists[id].head;
s->links[id].next = old_head;
s->links[id].prev = NULL;
- if (old_head)
- {
- old_head->links[id].prev = s;
- }
- else
- {
- t->lists[id].tail = s;
- }
+ if (old_head) {
+ old_head->links[id].prev = s;
+ } else {
+ t->lists[id].tail = s;
+ }
t->lists[id].head = s;
s->included[id] = 1;
}
-static void
-stream_list_add_tail (grpc_chttp2_transport * t, grpc_chttp2_stream * s, grpc_chttp2_stream_list_id id)
-{
+static void stream_list_add_tail(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s,
+ grpc_chttp2_stream_list_id id) {
grpc_chttp2_stream *old_tail;
- GPR_ASSERT (!s->included[id]);
+ GPR_ASSERT(!s->included[id]);
old_tail = t->lists[id].tail;
s->links[id].next = NULL;
s->links[id].prev = old_tail;
- if (old_tail)
- {
- old_tail->links[id].next = s;
- }
- else
- {
- t->lists[id].head = s;
- }
+ if (old_tail) {
+ old_tail->links[id].next = s;
+ } else {
+ t->lists[id].head = s;
+ }
t->lists[id].tail = s;
s->included[id] = 1;
}
-static void
-stream_list_add (grpc_chttp2_transport * t, grpc_chttp2_stream * s, grpc_chttp2_stream_list_id id)
-{
- if (s->included[id])
- {
- return;
- }
- stream_list_add_tail (t, s, id);
+static void stream_list_add(grpc_chttp2_transport *t, grpc_chttp2_stream *s,
+ grpc_chttp2_stream_list_id id) {
+ if (s->included[id]) {
+ return;
+ }
+ stream_list_add_tail(t, s, id);
}
/* wrappers for specializations */
-void
-grpc_chttp2_list_add_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- GPR_ASSERT (stream_global->id != 0);
- stream_list_add (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_WRITABLE);
+void grpc_chttp2_list_add_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ GPR_ASSERT(stream_global->id != 0);
+ stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global), GRPC_CHTTP2_LIST_WRITABLE);
}
-void
-grpc_chttp2_list_add_first_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- GPR_ASSERT (stream_global->id != 0);
- stream_list_add_head (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_WRITABLE);
+void grpc_chttp2_list_add_first_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ GPR_ASSERT(stream_global->id != 0);
+ stream_list_add_head(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_WRITABLE);
}
-int
-grpc_chttp2_list_pop_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_writing ** stream_writing)
-{
+int grpc_chttp2_list_pop_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_writing **stream_writing) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_GLOBAL (transport_global), &stream,
- GRPC_CHTTP2_LIST_WRITABLE);
- if (r != 0)
- {
- *stream_global = &stream->global;
- *stream_writing = &stream->writing;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
+ GRPC_CHTTP2_LIST_WRITABLE);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ *stream_writing = &stream->writing;
+ }
return r;
}
-void
-grpc_chttp2_list_remove_writable_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_maybe_remove (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_WRITABLE);
+void grpc_chttp2_list_remove_writable_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_maybe_remove(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_WRITABLE);
}
-void
-grpc_chttp2_list_add_writing_stream (grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_writing * stream_writing)
-{
- stream_list_add (TRANSPORT_FROM_WRITING (transport_writing), STREAM_FROM_WRITING (stream_writing), GRPC_CHTTP2_LIST_WRITING);
+void grpc_chttp2_list_add_writing_stream(
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_writing *stream_writing) {
+ stream_list_add(TRANSPORT_FROM_WRITING(transport_writing),
+ STREAM_FROM_WRITING(stream_writing),
+ GRPC_CHTTP2_LIST_WRITING);
}
-int
-grpc_chttp2_list_have_writing_streams (grpc_chttp2_transport_writing * transport_writing)
-{
- return !stream_list_empty (TRANSPORT_FROM_WRITING (transport_writing), GRPC_CHTTP2_LIST_WRITING);
+int grpc_chttp2_list_have_writing_streams(
+ grpc_chttp2_transport_writing *transport_writing) {
+ return !stream_list_empty(TRANSPORT_FROM_WRITING(transport_writing),
+ GRPC_CHTTP2_LIST_WRITING);
}
-int
-grpc_chttp2_list_pop_writing_stream (grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_writing ** stream_writing)
-{
+int grpc_chttp2_list_pop_writing_stream(
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_writing **stream_writing) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_WRITING (transport_writing), &stream,
- GRPC_CHTTP2_LIST_WRITING);
- if (r != 0)
- {
- *stream_writing = &stream->writing;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_WRITING(transport_writing), &stream,
+ GRPC_CHTTP2_LIST_WRITING);
+ if (r != 0) {
+ *stream_writing = &stream->writing;
+ }
return r;
}
-void
-grpc_chttp2_list_add_written_stream (grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_writing * stream_writing)
-{
- stream_list_add (TRANSPORT_FROM_WRITING (transport_writing), STREAM_FROM_WRITING (stream_writing), GRPC_CHTTP2_LIST_WRITTEN);
+void grpc_chttp2_list_add_written_stream(
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_writing *stream_writing) {
+ stream_list_add(TRANSPORT_FROM_WRITING(transport_writing),
+ STREAM_FROM_WRITING(stream_writing),
+ GRPC_CHTTP2_LIST_WRITTEN);
}
-int
-grpc_chttp2_list_pop_written_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_writing * transport_writing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_writing ** stream_writing)
-{
+int grpc_chttp2_list_pop_written_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_writing *transport_writing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_writing **stream_writing) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_WRITING (transport_writing), &stream,
- GRPC_CHTTP2_LIST_WRITTEN);
- if (r != 0)
- {
- *stream_global = &stream->global;
- *stream_writing = &stream->writing;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_WRITING(transport_writing), &stream,
+ GRPC_CHTTP2_LIST_WRITTEN);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ *stream_writing = &stream->writing;
+ }
return r;
}
-void
-grpc_chttp2_list_add_parsing_seen_stream (grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_parsing * stream_parsing)
-{
- stream_list_add (TRANSPORT_FROM_PARSING (transport_parsing), STREAM_FROM_PARSING (stream_parsing), GRPC_CHTTP2_LIST_PARSING_SEEN);
+void grpc_chttp2_list_add_parsing_seen_stream(
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_parsing *stream_parsing) {
+ stream_list_add(TRANSPORT_FROM_PARSING(transport_parsing),
+ STREAM_FROM_PARSING(stream_parsing),
+ GRPC_CHTTP2_LIST_PARSING_SEEN);
}
-int
-grpc_chttp2_list_pop_parsing_seen_stream (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_parsing ** stream_parsing)
-{
+int grpc_chttp2_list_pop_parsing_seen_stream(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_parsing **stream_parsing) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_PARSING (transport_parsing), &stream,
- GRPC_CHTTP2_LIST_PARSING_SEEN);
- if (r != 0)
- {
- *stream_global = &stream->global;
- *stream_parsing = &stream->parsing;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_PARSING(transport_parsing), &stream,
+ GRPC_CHTTP2_LIST_PARSING_SEEN);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ *stream_parsing = &stream->parsing;
+ }
return r;
}
-void
-grpc_chttp2_list_add_waiting_for_concurrency (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_add (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY);
+void grpc_chttp2_list_add_waiting_for_concurrency(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY);
}
-int
-grpc_chttp2_list_pop_waiting_for_concurrency (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global)
-{
+int grpc_chttp2_list_pop_waiting_for_concurrency(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_GLOBAL (transport_global), &stream,
- GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY);
- if (r != 0)
- {
- *stream_global = &stream->global;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
+ GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ }
return r;
}
-void
-grpc_chttp2_list_add_closed_waiting_for_parsing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_add (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING);
+void grpc_chttp2_list_add_closed_waiting_for_parsing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING);
}
-int
-grpc_chttp2_list_pop_closed_waiting_for_parsing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global)
-{
+int grpc_chttp2_list_pop_closed_waiting_for_parsing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_GLOBAL (transport_global), &stream,
- GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING);
- if (r != 0)
- {
- *stream_global = &stream->global;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
+ GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ }
return r;
}
-void
-grpc_chttp2_list_add_cancelled_waiting_for_writing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_add (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_CANCELLED_WAITING_FOR_WRITING);
+void grpc_chttp2_list_add_cancelled_waiting_for_writing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_CANCELLED_WAITING_FOR_WRITING);
}
-int
-grpc_chttp2_list_pop_cancelled_waiting_for_writing (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global)
-{
+int grpc_chttp2_list_pop_cancelled_waiting_for_writing(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_GLOBAL (transport_global), &stream,
- GRPC_CHTTP2_LIST_CANCELLED_WAITING_FOR_WRITING);
- if (r != 0)
- {
- *stream_global = &stream->global;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
+ GRPC_CHTTP2_LIST_CANCELLED_WAITING_FOR_WRITING);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ }
return r;
}
-void
-grpc_chttp2_list_add_incoming_window_updated (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_add (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
+void grpc_chttp2_list_add_incoming_window_updated(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
}
-int
-grpc_chttp2_list_pop_incoming_window_updated (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_parsing * transport_parsing, grpc_chttp2_stream_global ** stream_global, grpc_chttp2_stream_parsing ** stream_parsing)
-{
+int grpc_chttp2_list_pop_incoming_window_updated(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_parsing *transport_parsing,
+ grpc_chttp2_stream_global **stream_global,
+ grpc_chttp2_stream_parsing **stream_parsing) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_GLOBAL (transport_global), &stream,
- GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
- if (r != 0)
- {
- *stream_global = &stream->global;
- *stream_parsing = &stream->parsing;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
+ GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ *stream_parsing = &stream->parsing;
+ }
return r;
}
-void
-grpc_chttp2_list_remove_incoming_window_updated (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_maybe_remove (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
+void grpc_chttp2_list_remove_incoming_window_updated(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_maybe_remove(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED);
}
-void
-grpc_chttp2_list_add_read_write_state_changed (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global)
-{
- stream_list_add (TRANSPORT_FROM_GLOBAL (transport_global), STREAM_FROM_GLOBAL (stream_global), GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED);
+void grpc_chttp2_list_add_read_write_state_changed(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global) {
+ stream_list_add(TRANSPORT_FROM_GLOBAL(transport_global),
+ STREAM_FROM_GLOBAL(stream_global),
+ GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED);
}
-int
-grpc_chttp2_list_pop_read_write_state_changed (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global ** stream_global)
-{
+int grpc_chttp2_list_pop_read_write_state_changed(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global **stream_global) {
grpc_chttp2_stream *stream;
- int r = stream_list_pop (TRANSPORT_FROM_GLOBAL (transport_global), &stream,
- GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED);
- if (r != 0)
- {
- *stream_global = &stream->global;
- }
+ int r = stream_list_pop(TRANSPORT_FROM_GLOBAL(transport_global), &stream,
+ GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED);
+ if (r != 0) {
+ *stream_global = &stream->global;
+ }
return r;
}
-void
-grpc_chttp2_register_stream (grpc_chttp2_transport * t, grpc_chttp2_stream * s)
-{
- stream_list_add_tail (t, s, GRPC_CHTTP2_LIST_ALL_STREAMS);
+void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s) {
+ stream_list_add_tail(t, s, GRPC_CHTTP2_LIST_ALL_STREAMS);
}
-int
-grpc_chttp2_unregister_stream (grpc_chttp2_transport * t, grpc_chttp2_stream * s)
-{
- stream_list_maybe_remove (t, s, GRPC_CHTTP2_LIST_ALL_STREAMS);
- return stream_list_empty (t, GRPC_CHTTP2_LIST_ALL_STREAMS);
+int grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
+ grpc_chttp2_stream *s) {
+ stream_list_maybe_remove(t, s, GRPC_CHTTP2_LIST_ALL_STREAMS);
+ return stream_list_empty(t, GRPC_CHTTP2_LIST_ALL_STREAMS);
}
-int
-grpc_chttp2_has_streams (grpc_chttp2_transport * t)
-{
- return !stream_list_empty (t, GRPC_CHTTP2_LIST_ALL_STREAMS);
+int grpc_chttp2_has_streams(grpc_chttp2_transport *t) {
+ return !stream_list_empty(t, GRPC_CHTTP2_LIST_ALL_STREAMS);
}
-void
-grpc_chttp2_for_all_streams (grpc_chttp2_transport_global * transport_global, void *user_data, void (*cb) (grpc_chttp2_transport_global * transport_global, void *user_data, grpc_chttp2_stream_global * stream_global))
-{
+void grpc_chttp2_for_all_streams(
+ grpc_chttp2_transport_global *transport_global, void *user_data,
+ void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
+ grpc_chttp2_stream_global *stream_global)) {
grpc_chttp2_stream *s;
- grpc_chttp2_transport *t = TRANSPORT_FROM_GLOBAL (transport_global);
- for (s = t->lists[GRPC_CHTTP2_LIST_ALL_STREAMS].head; s != NULL; s = s->links[GRPC_CHTTP2_LIST_ALL_STREAMS].next)
- {
- cb (transport_global, user_data, &s->global);
- }
+ grpc_chttp2_transport *t = TRANSPORT_FROM_GLOBAL(transport_global);
+ for (s = t->lists[GRPC_CHTTP2_LIST_ALL_STREAMS].head; s != NULL;
+ s = s->links[GRPC_CHTTP2_LIST_ALL_STREAMS].next) {
+ cb(transport_global, user_data, &s->global);
+ }
}
diff --git a/src/core/transport/chttp2/stream_map.c b/src/core/transport/chttp2/stream_map.c
index 00878c1d2a..c983105abb 100644
--- a/src/core/transport/chttp2/stream_map.c
+++ b/src/core/transport/chttp2/stream_map.c
@@ -39,117 +39,98 @@
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
-void
-grpc_chttp2_stream_map_init (grpc_chttp2_stream_map * map, size_t initial_capacity)
-{
- GPR_ASSERT (initial_capacity > 1);
- map->keys = gpr_malloc (sizeof (gpr_uint32) * initial_capacity);
- map->values = gpr_malloc (sizeof (void *) * initial_capacity);
+void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map,
+ size_t initial_capacity) {
+ GPR_ASSERT(initial_capacity > 1);
+ map->keys = gpr_malloc(sizeof(gpr_uint32) * initial_capacity);
+ map->values = gpr_malloc(sizeof(void *) * initial_capacity);
map->count = 0;
map->free = 0;
map->capacity = initial_capacity;
}
-void
-grpc_chttp2_stream_map_destroy (grpc_chttp2_stream_map * map)
-{
- gpr_free (map->keys);
- gpr_free (map->values);
+void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map *map) {
+ gpr_free(map->keys);
+ gpr_free(map->values);
}
-static size_t
-compact (gpr_uint32 * keys, void **values, size_t count)
-{
+static size_t compact(gpr_uint32 *keys, void **values, size_t count) {
size_t i, out;
- for (i = 0, out = 0; i < count; i++)
- {
- if (values[i])
- {
- keys[out] = keys[i];
- values[out] = values[i];
- out++;
- }
+ for (i = 0, out = 0; i < count; i++) {
+ if (values[i]) {
+ keys[out] = keys[i];
+ values[out] = values[i];
+ out++;
}
+ }
return out;
}
-void
-grpc_chttp2_stream_map_add (grpc_chttp2_stream_map * map, gpr_uint32 key, void *value)
-{
+void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, gpr_uint32 key,
+ void *value) {
size_t count = map->count;
size_t capacity = map->capacity;
gpr_uint32 *keys = map->keys;
void **values = map->values;
- GPR_ASSERT (count == 0 || keys[count - 1] < key);
- GPR_ASSERT (value);
-
- if (count == capacity)
- {
- if (map->free > capacity / 4)
- {
- count = compact (keys, values, count);
- map->free = 0;
- }
- else
- {
- /* resize when less than 25% of the table is free, because compaction
- won't help much */
- map->capacity = capacity = 3 * capacity / 2;
- map->keys = keys = gpr_realloc (keys, capacity * sizeof (gpr_uint32));
- map->values = values = gpr_realloc (values, capacity * sizeof (void *));
- }
+ GPR_ASSERT(count == 0 || keys[count - 1] < key);
+ GPR_ASSERT(value);
+
+ if (count == capacity) {
+ if (map->free > capacity / 4) {
+ count = compact(keys, values, count);
+ map->free = 0;
+ } else {
+ /* resize when less than 25% of the table is free, because compaction
+ won't help much */
+ map->capacity = capacity = 3 * capacity / 2;
+ map->keys = keys = gpr_realloc(keys, capacity * sizeof(gpr_uint32));
+ map->values = values = gpr_realloc(values, capacity * sizeof(void *));
}
+ }
keys[count] = key;
values[count] = value;
map->count = count + 1;
}
-void
-grpc_chttp2_stream_map_move_into (grpc_chttp2_stream_map * src, grpc_chttp2_stream_map * dst)
-{
+void grpc_chttp2_stream_map_move_into(grpc_chttp2_stream_map *src,
+ grpc_chttp2_stream_map *dst) {
/* if src is empty we dont need to do anything */
- if (src->count == src->free)
- {
- return;
- }
+ if (src->count == src->free) {
+ return;
+ }
/* if dst is empty we simply need to swap */
- if (dst->count == dst->free)
- {
- GPR_SWAP (grpc_chttp2_stream_map, *src, *dst);
- return;
- }
+ if (dst->count == dst->free) {
+ GPR_SWAP(grpc_chttp2_stream_map, *src, *dst);
+ return;
+ }
/* the first element of src must be greater than the last of dst...
* however the maps may need compacting for this property to hold */
- if (src->keys[0] <= dst->keys[dst->count - 1])
- {
- src->count = compact (src->keys, src->values, src->count);
- src->free = 0;
- dst->count = compact (dst->keys, dst->values, dst->count);
- dst->free = 0;
- }
- GPR_ASSERT (src->keys[0] > dst->keys[dst->count - 1]);
+ if (src->keys[0] <= dst->keys[dst->count - 1]) {
+ src->count = compact(src->keys, src->values, src->count);
+ src->free = 0;
+ dst->count = compact(dst->keys, dst->values, dst->count);
+ dst->free = 0;
+ }
+ GPR_ASSERT(src->keys[0] > dst->keys[dst->count - 1]);
/* if dst doesn't have capacity, resize */
- if (dst->count + src->count > dst->capacity)
- {
- dst->capacity = GPR_MAX (dst->capacity * 3 / 2, dst->count + src->count);
- dst->keys = gpr_realloc (dst->keys, dst->capacity * sizeof (gpr_uint32));
- dst->values = gpr_realloc (dst->values, dst->capacity * sizeof (void *));
- }
- memcpy (dst->keys + dst->count, src->keys, src->count * sizeof (gpr_uint32));
- memcpy (dst->values + dst->count, src->values, src->count * sizeof (void *));
+ if (dst->count + src->count > dst->capacity) {
+ dst->capacity = GPR_MAX(dst->capacity * 3 / 2, dst->count + src->count);
+ dst->keys = gpr_realloc(dst->keys, dst->capacity * sizeof(gpr_uint32));
+ dst->values = gpr_realloc(dst->values, dst->capacity * sizeof(void *));
+ }
+ memcpy(dst->keys + dst->count, src->keys, src->count * sizeof(gpr_uint32));
+ memcpy(dst->values + dst->count, src->values, src->count * sizeof(void *));
dst->count += src->count;
dst->free += src->free;
src->count = 0;
src->free = 0;
}
-static void **
-find (grpc_chttp2_stream_map * map, gpr_uint32 key)
-{
+static void **find(grpc_chttp2_stream_map *map, gpr_uint32 key) {
size_t min_idx = 0;
size_t max_idx = map->count;
size_t mid_idx;
@@ -157,75 +138,61 @@ find (grpc_chttp2_stream_map * map, gpr_uint32 key)
void **values = map->values;
gpr_uint32 mid_key;
- if (max_idx == 0)
- return NULL;
+ if (max_idx == 0) return NULL;
- while (min_idx < max_idx)
+ while (min_idx < max_idx) {
+ /* find the midpoint, avoiding overflow */
+ mid_idx = min_idx + ((max_idx - min_idx) / 2);
+ mid_key = keys[mid_idx];
+
+ if (mid_key < key) {
+ min_idx = mid_idx + 1;
+ } else if (mid_key > key) {
+ max_idx = mid_idx;
+ } else /* mid_key == key */
{
- /* find the midpoint, avoiding overflow */
- mid_idx = min_idx + ((max_idx - min_idx) / 2);
- mid_key = keys[mid_idx];
-
- if (mid_key < key)
- {
- min_idx = mid_idx + 1;
- }
- else if (mid_key > key)
- {
- max_idx = mid_idx;
- }
- else /* mid_key == key */
- {
- return &values[mid_idx];
- }
+ return &values[mid_idx];
}
+ }
return NULL;
}
-void *
-grpc_chttp2_stream_map_delete (grpc_chttp2_stream_map * map, gpr_uint32 key)
-{
- void **pvalue = find (map, key);
+void *grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map,
+ gpr_uint32 key) {
+ void **pvalue = find(map, key);
void *out = NULL;
- if (pvalue != NULL)
- {
- out = *pvalue;
- *pvalue = NULL;
- map->free += (out != NULL);
- /* recognize complete emptyness and ensure we can skip
- * defragmentation later */
- if (map->free == map->count)
- {
- map->free = map->count = 0;
- }
+ if (pvalue != NULL) {
+ out = *pvalue;
+ *pvalue = NULL;
+ map->free += (out != NULL);
+ /* recognize complete emptyness and ensure we can skip
+ * defragmentation later */
+ if (map->free == map->count) {
+ map->free = map->count = 0;
}
+ }
return out;
}
-void *
-grpc_chttp2_stream_map_find (grpc_chttp2_stream_map * map, gpr_uint32 key)
-{
- void **pvalue = find (map, key);
+void *grpc_chttp2_stream_map_find(grpc_chttp2_stream_map *map, gpr_uint32 key) {
+ void **pvalue = find(map, key);
return pvalue != NULL ? *pvalue : NULL;
}
-size_t
-grpc_chttp2_stream_map_size (grpc_chttp2_stream_map * map)
-{
+size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map *map) {
return map->count - map->free;
}
-void
-grpc_chttp2_stream_map_for_each (grpc_chttp2_stream_map * map, void (*f) (void *user_data, gpr_uint32 key, void *value), void *user_data)
-{
+void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map *map,
+ void (*f)(void *user_data, gpr_uint32 key,
+ void *value),
+ void *user_data) {
size_t i;
- for (i = 0; i < map->count; i++)
- {
- if (map->values[i])
- {
- f (user_data, map->keys[i], map->values[i]);
- }
+ for (i = 0; i < map->count; i++) {
+ if (map->values[i]) {
+ f(user_data, map->keys[i], map->values[i]);
}
+ }
}
diff --git a/src/core/transport/chttp2/stream_map.h b/src/core/transport/chttp2/stream_map.h
index 2af61212da..71b0582054 100644
--- a/src/core/transport/chttp2/stream_map.h
+++ b/src/core/transport/chttp2/stream_map.h
@@ -44,8 +44,7 @@
Lookups are performed with binary search.
Adds are restricted to strictly higher keys than previously seen (this is
guaranteed by http2). */
-typedef struct
-{
+typedef struct {
gpr_uint32 *keys;
void **values;
size_t count;
@@ -53,27 +52,34 @@ typedef struct
size_t capacity;
} grpc_chttp2_stream_map;
-void grpc_chttp2_stream_map_init (grpc_chttp2_stream_map * map, size_t initial_capacity);
-void grpc_chttp2_stream_map_destroy (grpc_chttp2_stream_map * map);
+void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map,
+ size_t initial_capacity);
+void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map *map);
/* Add a new key: given http2 semantics, new keys must always be greater than
existing keys - this is asserted */
-void grpc_chttp2_stream_map_add (grpc_chttp2_stream_map * map, gpr_uint32 key, void *value);
+void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, gpr_uint32 key,
+ void *value);
/* Delete an existing key - returns the previous value of the key if it existed,
or NULL otherwise */
-void *grpc_chttp2_stream_map_delete (grpc_chttp2_stream_map * map, gpr_uint32 key);
+void *grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map,
+ gpr_uint32 key);
/* Move all elements of src into dst */
-void grpc_chttp2_stream_map_move_into (grpc_chttp2_stream_map * src, grpc_chttp2_stream_map * dst);
+void grpc_chttp2_stream_map_move_into(grpc_chttp2_stream_map *src,
+ grpc_chttp2_stream_map *dst);
/* Return an existing key, or NULL if it does not exist */
-void *grpc_chttp2_stream_map_find (grpc_chttp2_stream_map * map, gpr_uint32 key);
+void *grpc_chttp2_stream_map_find(grpc_chttp2_stream_map *map, gpr_uint32 key);
/* How many (populated) entries are in the stream map? */
-size_t grpc_chttp2_stream_map_size (grpc_chttp2_stream_map * map);
+size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map *map);
/* Callback on each stream */
-void grpc_chttp2_stream_map_for_each (grpc_chttp2_stream_map * map, void (*f) (void *user_data, gpr_uint32 key, void *value), void *user_data);
+void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map *map,
+ void (*f)(void *user_data, gpr_uint32 key,
+ void *value),
+ void *user_data);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_STREAM_MAP_H */
diff --git a/src/core/transport/chttp2/timeout_encoding.c b/src/core/transport/chttp2/timeout_encoding.c
index e241242403..8a9b290ecb 100644
--- a/src/core/transport/chttp2/timeout_encoding.c
+++ b/src/core/transport/chttp2/timeout_encoding.c
@@ -38,215 +38,147 @@
#include "src/core/support/string.h"
-static int
-round_up (int x, int divisor)
-{
+static int round_up(int x, int divisor) {
return (x / divisor + (x % divisor != 0)) * divisor;
}
/* round an integer up to the next value with three significant figures */
-static int
-round_up_to_three_sig_figs (int x)
-{
- if (x < 1000)
- return x;
- if (x < 10000)
- return round_up (x, 10);
- if (x < 100000)
- return round_up (x, 100);
- if (x < 1000000)
- return round_up (x, 1000);
- if (x < 10000000)
- return round_up (x, 10000);
- if (x < 100000000)
- return round_up (x, 100000);
- if (x < 1000000000)
- return round_up (x, 1000000);
- return round_up (x, 10000000);
+static int round_up_to_three_sig_figs(int x) {
+ if (x < 1000) return x;
+ if (x < 10000) return round_up(x, 10);
+ if (x < 100000) return round_up(x, 100);
+ if (x < 1000000) return round_up(x, 1000);
+ if (x < 10000000) return round_up(x, 10000);
+ if (x < 100000000) return round_up(x, 100000);
+ if (x < 1000000000) return round_up(x, 1000000);
+ return round_up(x, 10000000);
}
/* encode our minimum viable timeout value */
-static void
-enc_tiny (char *buffer)
-{
- memcpy (buffer, "1n", 3);
-}
+static void enc_tiny(char *buffer) { memcpy(buffer, "1n", 3); }
-static void
-enc_ext (char *buffer, long value, char ext)
-{
- int n = gpr_ltoa (value, buffer);
+static void enc_ext(char *buffer, long value, char ext) {
+ int n = gpr_ltoa(value, buffer);
buffer[n] = ext;
buffer[n + 1] = 0;
}
-static void
-enc_seconds (char *buffer, long sec)
-{
- if (sec % 3600 == 0)
- {
- enc_ext (buffer, sec / 3600, 'H');
- }
- else if (sec % 60 == 0)
- {
- enc_ext (buffer, sec / 60, 'M');
- }
- else
- {
- enc_ext (buffer, sec, 'S');
- }
+static void enc_seconds(char *buffer, long sec) {
+ if (sec % 3600 == 0) {
+ enc_ext(buffer, sec / 3600, 'H');
+ } else if (sec % 60 == 0) {
+ enc_ext(buffer, sec / 60, 'M');
+ } else {
+ enc_ext(buffer, sec, 'S');
+ }
}
-static void
-enc_nanos (char *buffer, int x)
-{
- x = round_up_to_three_sig_figs (x);
- if (x < 100000)
- {
- if (x % 1000 == 0)
- {
- enc_ext (buffer, x / 1000, 'u');
- }
- else
- {
- enc_ext (buffer, x, 'n');
- }
- }
- else if (x < 100000000)
- {
- if (x % 1000000 == 0)
- {
- enc_ext (buffer, x / 1000000, 'm');
- }
- else
- {
- enc_ext (buffer, x / 1000, 'u');
- }
- }
- else if (x < 1000000000)
- {
- enc_ext (buffer, x / 1000000, 'm');
- }
- else
- {
- /* note that this is only ever called with times of less than one second,
- so if we reach here the time must have been rounded up to a whole second
- (and no more) */
- memcpy (buffer, "1S", 3);
- }
+static void enc_nanos(char *buffer, int x) {
+ x = round_up_to_three_sig_figs(x);
+ if (x < 100000) {
+ if (x % 1000 == 0) {
+ enc_ext(buffer, x / 1000, 'u');
+ } else {
+ enc_ext(buffer, x, 'n');
+ }
+ } else if (x < 100000000) {
+ if (x % 1000000 == 0) {
+ enc_ext(buffer, x / 1000000, 'm');
+ } else {
+ enc_ext(buffer, x / 1000, 'u');
+ }
+ } else if (x < 1000000000) {
+ enc_ext(buffer, x / 1000000, 'm');
+ } else {
+ /* note that this is only ever called with times of less than one second,
+ so if we reach here the time must have been rounded up to a whole second
+ (and no more) */
+ memcpy(buffer, "1S", 3);
+ }
}
-static void
-enc_micros (char *buffer, int x)
-{
- x = round_up_to_three_sig_figs (x);
- if (x < 100000)
- {
- if (x % 1000 == 0)
- {
- enc_ext (buffer, x / 1000, 'm');
- }
- else
- {
- enc_ext (buffer, x, 'u');
- }
- }
- else if (x < 100000000)
- {
- if (x % 1000000 == 0)
- {
- enc_ext (buffer, x / 1000000, 'S');
- }
- else
- {
- enc_ext (buffer, x / 1000, 'm');
- }
- }
- else
- {
- enc_ext (buffer, x / 1000000, 'S');
- }
+static void enc_micros(char *buffer, int x) {
+ x = round_up_to_three_sig_figs(x);
+ if (x < 100000) {
+ if (x % 1000 == 0) {
+ enc_ext(buffer, x / 1000, 'm');
+ } else {
+ enc_ext(buffer, x, 'u');
+ }
+ } else if (x < 100000000) {
+ if (x % 1000000 == 0) {
+ enc_ext(buffer, x / 1000000, 'S');
+ } else {
+ enc_ext(buffer, x / 1000, 'm');
+ }
+ } else {
+ enc_ext(buffer, x / 1000000, 'S');
+ }
}
-void
-grpc_chttp2_encode_timeout (gpr_timespec timeout, char *buffer)
-{
- if (timeout.tv_sec < 0)
- {
- enc_tiny (buffer);
- }
- else if (timeout.tv_sec == 0)
- {
- enc_nanos (buffer, timeout.tv_nsec);
- }
- else if (timeout.tv_sec < 1000 && timeout.tv_nsec != 0)
- {
- enc_micros (buffer, (int) (timeout.tv_sec * 1000000) + (timeout.tv_nsec / 1000 + (timeout.tv_nsec % 1000 != 0)));
- }
- else
- {
- enc_seconds (buffer, timeout.tv_sec + (timeout.tv_nsec != 0));
- }
+void grpc_chttp2_encode_timeout(gpr_timespec timeout, char *buffer) {
+ if (timeout.tv_sec < 0) {
+ enc_tiny(buffer);
+ } else if (timeout.tv_sec == 0) {
+ enc_nanos(buffer, timeout.tv_nsec);
+ } else if (timeout.tv_sec < 1000 && timeout.tv_nsec != 0) {
+ enc_micros(buffer,
+ (int)(timeout.tv_sec * 1000000) +
+ (timeout.tv_nsec / 1000 + (timeout.tv_nsec % 1000 != 0)));
+ } else {
+ enc_seconds(buffer, timeout.tv_sec + (timeout.tv_nsec != 0));
+ }
}
-static int
-is_all_whitespace (const char *p)
-{
- while (*p == ' ')
- p++;
+static int is_all_whitespace(const char *p) {
+ while (*p == ' ') p++;
return *p == 0;
}
-int
-grpc_chttp2_decode_timeout (const char *buffer, gpr_timespec * timeout)
-{
+int grpc_chttp2_decode_timeout(const char *buffer, gpr_timespec *timeout) {
gpr_uint32 x = 0;
- const gpr_uint8 *p = (const gpr_uint8 *) buffer;
+ const gpr_uint8 *p = (const gpr_uint8 *)buffer;
int have_digit = 0;
/* skip whitespace */
for (; *p == ' '; p++)
;
/* decode numeric part */
- for (; *p >= '0' && *p <= '9'; p++)
- {
- gpr_uint32 xp = x * 10u + (gpr_uint32) * p - (gpr_uint32) '0';
- have_digit = 1;
- if (xp < x)
- {
- *timeout = gpr_inf_future (GPR_CLOCK_REALTIME);
- return 1;
- }
- x = xp;
- }
- if (!have_digit)
- return 0;
+ for (; *p >= '0' && *p <= '9'; p++) {
+ gpr_uint32 xp = x * 10u + (gpr_uint32)*p - (gpr_uint32)'0';
+ have_digit = 1;
+ if (xp < x) {
+ *timeout = gpr_inf_future(GPR_CLOCK_REALTIME);
+ return 1;
+ }
+ x = xp;
+ }
+ if (!have_digit) return 0;
/* skip whitespace */
for (; *p == ' '; p++)
;
/* decode unit specifier */
- switch (*p)
- {
+ switch (*p) {
case 'n':
- *timeout = gpr_time_from_nanos (x, GPR_TIMESPAN);
+ *timeout = gpr_time_from_nanos(x, GPR_TIMESPAN);
break;
case 'u':
- *timeout = gpr_time_from_micros (x, GPR_TIMESPAN);
+ *timeout = gpr_time_from_micros(x, GPR_TIMESPAN);
break;
case 'm':
- *timeout = gpr_time_from_millis (x, GPR_TIMESPAN);
+ *timeout = gpr_time_from_millis(x, GPR_TIMESPAN);
break;
case 'S':
- *timeout = gpr_time_from_seconds (x, GPR_TIMESPAN);
+ *timeout = gpr_time_from_seconds(x, GPR_TIMESPAN);
break;
case 'M':
- *timeout = gpr_time_from_minutes (x, GPR_TIMESPAN);
+ *timeout = gpr_time_from_minutes(x, GPR_TIMESPAN);
break;
case 'H':
- *timeout = gpr_time_from_hours (x, GPR_TIMESPAN);
+ *timeout = gpr_time_from_hours(x, GPR_TIMESPAN);
break;
default:
return 0;
- }
+ }
p++;
- return is_all_whitespace ((const char *) p);
+ return is_all_whitespace((const char *)p);
}
diff --git a/src/core/transport/chttp2/timeout_encoding.h b/src/core/transport/chttp2/timeout_encoding.h
index b68ce2e5d6..9d8756e799 100644
--- a/src/core/transport/chttp2/timeout_encoding.h
+++ b/src/core/transport/chttp2/timeout_encoding.h
@@ -41,7 +41,7 @@
/* Encode/decode timeouts to the GRPC over HTTP2 format;
encoding may round up arbitrarily */
-void grpc_chttp2_encode_timeout (gpr_timespec timeout, char *buffer);
-int grpc_chttp2_decode_timeout (const char *buffer, gpr_timespec * timeout);
+void grpc_chttp2_encode_timeout(gpr_timespec timeout, char *buffer);
+int grpc_chttp2_decode_timeout(const char *buffer, gpr_timespec *timeout);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_TIMEOUT_ENCODING_H */
diff --git a/src/core/transport/chttp2/varint.c b/src/core/transport/chttp2/varint.c
index f478a9d847..056f68047b 100644
--- a/src/core/transport/chttp2/varint.c
+++ b/src/core/transport/chttp2/varint.c
@@ -33,46 +33,34 @@
#include "src/core/transport/chttp2/varint.h"
-gpr_uint32
-grpc_chttp2_hpack_varint_length (gpr_uint32 tail_value)
-{
- if (tail_value < (1 << 7))
- {
- return 2;
- }
- else if (tail_value < (1 << 14))
- {
- return 3;
- }
- else if (tail_value < (1 << 21))
- {
- return 4;
- }
- else if (tail_value < (1 << 28))
- {
- return 5;
- }
- else
- {
- return 6;
- }
+gpr_uint32 grpc_chttp2_hpack_varint_length(gpr_uint32 tail_value) {
+ if (tail_value < (1 << 7)) {
+ return 2;
+ } else if (tail_value < (1 << 14)) {
+ return 3;
+ } else if (tail_value < (1 << 21)) {
+ return 4;
+ } else if (tail_value < (1 << 28)) {
+ return 5;
+ } else {
+ return 6;
+ }
}
-void
-grpc_chttp2_hpack_write_varint_tail (gpr_uint32 tail_value, gpr_uint8 * target, gpr_uint32 tail_length)
-{
- switch (tail_length)
- {
+void grpc_chttp2_hpack_write_varint_tail(gpr_uint32 tail_value,
+ gpr_uint8* target,
+ gpr_uint32 tail_length) {
+ switch (tail_length) {
case 5:
- target[4] = (gpr_uint8) ((tail_value >> 28) | 0x80);
+ target[4] = (gpr_uint8)((tail_value >> 28) | 0x80);
case 4:
- target[3] = (gpr_uint8) ((tail_value >> 21) | 0x80);
+ target[3] = (gpr_uint8)((tail_value >> 21) | 0x80);
case 3:
- target[2] = (gpr_uint8) ((tail_value >> 14) | 0x80);
+ target[2] = (gpr_uint8)((tail_value >> 14) | 0x80);
case 2:
- target[1] = (gpr_uint8) ((tail_value >> 7) | 0x80);
+ target[1] = (gpr_uint8)((tail_value >> 7) | 0x80);
case 1:
- target[0] = (gpr_uint8) ((tail_value) | 0x80);
- }
+ target[0] = (gpr_uint8)((tail_value) | 0x80);
+ }
target[tail_length - 1] &= 0x7f;
}
diff --git a/src/core/transport/chttp2/varint.h b/src/core/transport/chttp2/varint.h
index 4193305d0f..4dfcc76773 100644
--- a/src/core/transport/chttp2/varint.h
+++ b/src/core/transport/chttp2/varint.h
@@ -41,9 +41,11 @@
/* length of a value that needs varint tail encoding (it's bigger than can be
bitpacked into the opcode byte) - returned value includes the length of the
opcode byte */
-gpr_uint32 grpc_chttp2_hpack_varint_length (gpr_uint32 tail_value);
+gpr_uint32 grpc_chttp2_hpack_varint_length(gpr_uint32 tail_value);
-void grpc_chttp2_hpack_write_varint_tail (gpr_uint32 tail_value, gpr_uint8 * target, gpr_uint32 tail_length);
+void grpc_chttp2_hpack_write_varint_tail(gpr_uint32 tail_value,
+ gpr_uint8* target,
+ gpr_uint32 tail_length);
/* maximum value that can be bitpacked with the opcode if the opcode has a
prefix
diff --git a/src/core/transport/chttp2/writing.c b/src/core/transport/chttp2/writing.c
index ebc170fb43..d1c9da6df0 100644
--- a/src/core/transport/chttp2/writing.c
+++ b/src/core/transport/chttp2/writing.c
@@ -39,174 +39,203 @@
#include "src/core/transport/chttp2/http2_errors.h"
-static void finalize_outbuf (grpc_chttp2_transport_writing * transport_writing);
+static void finalize_outbuf(grpc_chttp2_transport_writing *transport_writing);
-int
-grpc_chttp2_unlocking_check_writes (grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_writing * transport_writing)
-{
+int grpc_chttp2_unlocking_check_writes(
+ grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_writing *transport_writing) {
grpc_chttp2_stream_global *stream_global;
grpc_chttp2_stream_writing *stream_writing;
grpc_chttp2_stream_global *first_reinserted_stream = NULL;
gpr_uint32 window_delta;
/* simple writes are queued to qbuf, and flushed here */
- gpr_slice_buffer_swap (&transport_global->qbuf, &transport_writing->outbuf);
- GPR_ASSERT (transport_global->qbuf.count == 0);
-
- if (transport_global->dirtied_local_settings && !transport_global->sent_local_settings)
- {
- gpr_slice_buffer_add (&transport_writing->outbuf, grpc_chttp2_settings_create (transport_global->settings[GRPC_SENT_SETTINGS], transport_global->settings[GRPC_LOCAL_SETTINGS], transport_global->force_send_settings, GRPC_CHTTP2_NUM_SETTINGS));
- transport_global->force_send_settings = 0;
- transport_global->dirtied_local_settings = 0;
- transport_global->sent_local_settings = 1;
- }
+ gpr_slice_buffer_swap(&transport_global->qbuf, &transport_writing->outbuf);
+ GPR_ASSERT(transport_global->qbuf.count == 0);
+
+ if (transport_global->dirtied_local_settings &&
+ !transport_global->sent_local_settings) {
+ gpr_slice_buffer_add(
+ &transport_writing->outbuf,
+ grpc_chttp2_settings_create(
+ transport_global->settings[GRPC_SENT_SETTINGS],
+ transport_global->settings[GRPC_LOCAL_SETTINGS],
+ transport_global->force_send_settings, GRPC_CHTTP2_NUM_SETTINGS));
+ transport_global->force_send_settings = 0;
+ transport_global->dirtied_local_settings = 0;
+ transport_global->sent_local_settings = 1;
+ }
/* for each grpc_chttp2_stream that's become writable, frame it's data
(according to available window sizes) and add to the output buffer */
- while (grpc_chttp2_list_pop_writable_stream (transport_global, transport_writing, &stream_global, &stream_writing))
- {
- if (stream_global == first_reinserted_stream)
- {
- /* prevent infinite loop */
- grpc_chttp2_list_add_first_writable_stream (transport_global, stream_global);
- break;
- }
-
- stream_writing->id = stream_global->id;
- stream_writing->send_closed = GRPC_DONT_SEND_CLOSED;
-
- if (stream_global->outgoing_sopb)
- {
- window_delta = grpc_chttp2_preencode (stream_global->outgoing_sopb->ops, &stream_global->outgoing_sopb->nops, (gpr_uint32) GPR_MIN (GPR_MIN (transport_global->outgoing_window, stream_global->outgoing_window), GPR_UINT32_MAX), &stream_writing->sopb);
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("write", transport_global, outgoing_window, -(gpr_int64) window_delta);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("write", transport_global, stream_global, outgoing_window, -(gpr_int64) window_delta);
- transport_global->outgoing_window -= window_delta;
- stream_global->outgoing_window -= window_delta;
-
- if (stream_global->write_state == GRPC_WRITE_STATE_QUEUED_CLOSE && stream_global->outgoing_sopb->nops == 0)
- {
- if (!transport_global->is_client && !stream_global->read_closed)
- {
- stream_writing->send_closed = GRPC_SEND_CLOSED_WITH_RST_STREAM;
- }
- else
- {
- stream_writing->send_closed = GRPC_SEND_CLOSED;
- }
- }
-
- if (stream_global->outgoing_window > 0 && stream_global->outgoing_sopb->nops != 0)
- {
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
- if (first_reinserted_stream == NULL && transport_global->outgoing_window == 0)
- {
- first_reinserted_stream = stream_global;
- }
- }
- }
-
- if (!stream_global->read_closed && stream_global->unannounced_incoming_window > 0)
- {
- GPR_ASSERT (stream_writing->announce_window == 0);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("write", transport_writing, stream_writing, announce_window, stream_global->unannounced_incoming_window);
- stream_writing->announce_window = stream_global->unannounced_incoming_window;
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("write", transport_global, stream_global, incoming_window, stream_global->unannounced_incoming_window);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("write", transport_global, stream_global, unannounced_incoming_window, -(gpr_int64) stream_global->unannounced_incoming_window);
- stream_global->incoming_window += stream_global->unannounced_incoming_window;
- stream_global->unannounced_incoming_window = 0;
- grpc_chttp2_list_add_incoming_window_updated (transport_global, stream_global);
- stream_global->writing_now |= GRPC_CHTTP2_WRITING_WINDOW;
- }
- if (stream_writing->sopb.nops > 0 || stream_writing->send_closed != GRPC_DONT_SEND_CLOSED)
- {
- stream_global->writing_now |= GRPC_CHTTP2_WRITING_DATA;
- }
- if (stream_global->writing_now != 0)
- {
- grpc_chttp2_list_add_writing_stream (transport_writing, stream_writing);
- }
+ while (grpc_chttp2_list_pop_writable_stream(
+ transport_global, transport_writing, &stream_global, &stream_writing)) {
+ if (stream_global == first_reinserted_stream) {
+ /* prevent infinite loop */
+ grpc_chttp2_list_add_first_writable_stream(transport_global,
+ stream_global);
+ break;
}
- /* if the grpc_chttp2_transport is ready to send a window update, do so here
- also; 3/4 is a magic number that will likely get tuned soon */
- if (transport_global->incoming_window < transport_global->connection_window_target * 3 / 4)
- {
- window_delta = transport_global->connection_window_target - transport_global->incoming_window;
- gpr_slice_buffer_add (&transport_writing->outbuf, grpc_chttp2_window_update_create (0, window_delta));
- GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT ("write", transport_global, incoming_window, window_delta);
- transport_global->incoming_window += window_delta;
+ stream_writing->id = stream_global->id;
+ stream_writing->send_closed = GRPC_DONT_SEND_CLOSED;
+
+ if (stream_global->outgoing_sopb) {
+ window_delta = grpc_chttp2_preencode(
+ stream_global->outgoing_sopb->ops,
+ &stream_global->outgoing_sopb->nops,
+ (gpr_uint32)GPR_MIN(GPR_MIN(transport_global->outgoing_window,
+ stream_global->outgoing_window),
+ GPR_UINT32_MAX),
+ &stream_writing->sopb);
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(
+ "write", transport_global, outgoing_window, -(gpr_int64)window_delta);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("write", transport_global, stream_global,
+ outgoing_window,
+ -(gpr_int64)window_delta);
+ transport_global->outgoing_window -= window_delta;
+ stream_global->outgoing_window -= window_delta;
+
+ if (stream_global->write_state == GRPC_WRITE_STATE_QUEUED_CLOSE &&
+ stream_global->outgoing_sopb->nops == 0) {
+ if (!transport_global->is_client && !stream_global->read_closed) {
+ stream_writing->send_closed = GRPC_SEND_CLOSED_WITH_RST_STREAM;
+ } else {
+ stream_writing->send_closed = GRPC_SEND_CLOSED;
+ }
+ }
+
+ if (stream_global->outgoing_window > 0 &&
+ stream_global->outgoing_sopb->nops != 0) {
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
+ if (first_reinserted_stream == NULL &&
+ transport_global->outgoing_window == 0) {
+ first_reinserted_stream = stream_global;
+ }
+ }
+ }
+
+ if (!stream_global->read_closed &&
+ stream_global->unannounced_incoming_window > 0) {
+ GPR_ASSERT(stream_writing->announce_window == 0);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "write", transport_writing, stream_writing, announce_window,
+ stream_global->unannounced_incoming_window);
+ stream_writing->announce_window =
+ stream_global->unannounced_incoming_window;
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "write", transport_global, stream_global, incoming_window,
+ stream_global->unannounced_incoming_window);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "write", transport_global, stream_global, unannounced_incoming_window,
+ -(gpr_int64)stream_global->unannounced_incoming_window);
+ stream_global->incoming_window +=
+ stream_global->unannounced_incoming_window;
+ stream_global->unannounced_incoming_window = 0;
+ grpc_chttp2_list_add_incoming_window_updated(transport_global,
+ stream_global);
+ stream_global->writing_now |= GRPC_CHTTP2_WRITING_WINDOW;
+ }
+ if (stream_writing->sopb.nops > 0 ||
+ stream_writing->send_closed != GRPC_DONT_SEND_CLOSED) {
+ stream_global->writing_now |= GRPC_CHTTP2_WRITING_DATA;
}
+ if (stream_global->writing_now != 0) {
+ grpc_chttp2_list_add_writing_stream(transport_writing, stream_writing);
+ }
+ }
- return transport_writing->outbuf.count > 0 || grpc_chttp2_list_have_writing_streams (transport_writing);
+ /* if the grpc_chttp2_transport is ready to send a window update, do so here
+ also; 3/4 is a magic number that will likely get tuned soon */
+ if (transport_global->incoming_window <
+ transport_global->connection_window_target * 3 / 4) {
+ window_delta = transport_global->connection_window_target -
+ transport_global->incoming_window;
+ gpr_slice_buffer_add(&transport_writing->outbuf,
+ grpc_chttp2_window_update_create(0, window_delta));
+ GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT("write", transport_global,
+ incoming_window, window_delta);
+ transport_global->incoming_window += window_delta;
+ }
+
+ return transport_writing->outbuf.count > 0 ||
+ grpc_chttp2_list_have_writing_streams(transport_writing);
}
-void
-grpc_chttp2_perform_writes (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_writing * transport_writing, grpc_endpoint * endpoint)
-{
- GPR_ASSERT (transport_writing->outbuf.count > 0 || grpc_chttp2_list_have_writing_streams (transport_writing));
+void grpc_chttp2_perform_writes(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_writing *transport_writing,
+ grpc_endpoint *endpoint) {
+ GPR_ASSERT(transport_writing->outbuf.count > 0 ||
+ grpc_chttp2_list_have_writing_streams(transport_writing));
- finalize_outbuf (transport_writing);
+ finalize_outbuf(transport_writing);
- GPR_ASSERT (transport_writing->outbuf.count > 0);
- GPR_ASSERT (endpoint);
+ GPR_ASSERT(transport_writing->outbuf.count > 0);
+ GPR_ASSERT(endpoint);
- grpc_endpoint_write (exec_ctx, endpoint, &transport_writing->outbuf, &transport_writing->done_cb);
+ grpc_endpoint_write(exec_ctx, endpoint, &transport_writing->outbuf,
+ &transport_writing->done_cb);
}
-static void
-finalize_outbuf (grpc_chttp2_transport_writing * transport_writing)
-{
+static void finalize_outbuf(grpc_chttp2_transport_writing *transport_writing) {
grpc_chttp2_stream_writing *stream_writing;
- while (grpc_chttp2_list_pop_writing_stream (transport_writing, &stream_writing))
- {
- if (stream_writing->sopb.nops > 0 || stream_writing->send_closed != GRPC_DONT_SEND_CLOSED)
- {
- grpc_chttp2_encode (stream_writing->sopb.ops, stream_writing->sopb.nops, stream_writing->send_closed != GRPC_DONT_SEND_CLOSED, stream_writing->id, &transport_writing->hpack_compressor, &transport_writing->outbuf);
- stream_writing->sopb.nops = 0;
- }
- if (stream_writing->announce_window > 0)
- {
- gpr_slice_buffer_add (&transport_writing->outbuf, grpc_chttp2_window_update_create (stream_writing->id, stream_writing->announce_window));
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("write", transport_writing, stream_writing, announce_window, -(gpr_int64) stream_writing->announce_window);
- stream_writing->announce_window = 0;
- }
- if (stream_writing->send_closed == GRPC_SEND_CLOSED_WITH_RST_STREAM)
- {
- gpr_slice_buffer_add (&transport_writing->outbuf, grpc_chttp2_rst_stream_create (stream_writing->id, GRPC_CHTTP2_NO_ERROR));
- }
- grpc_chttp2_list_add_written_stream (transport_writing, stream_writing);
+ while (
+ grpc_chttp2_list_pop_writing_stream(transport_writing, &stream_writing)) {
+ if (stream_writing->sopb.nops > 0 ||
+ stream_writing->send_closed != GRPC_DONT_SEND_CLOSED) {
+ grpc_chttp2_encode(stream_writing->sopb.ops, stream_writing->sopb.nops,
+ stream_writing->send_closed != GRPC_DONT_SEND_CLOSED,
+ stream_writing->id,
+ &transport_writing->hpack_compressor,
+ &transport_writing->outbuf);
+ stream_writing->sopb.nops = 0;
}
+ if (stream_writing->announce_window > 0) {
+ gpr_slice_buffer_add(
+ &transport_writing->outbuf,
+ grpc_chttp2_window_update_create(stream_writing->id,
+ stream_writing->announce_window));
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "write", transport_writing, stream_writing, announce_window,
+ -(gpr_int64)stream_writing->announce_window);
+ stream_writing->announce_window = 0;
+ }
+ if (stream_writing->send_closed == GRPC_SEND_CLOSED_WITH_RST_STREAM) {
+ gpr_slice_buffer_add(&transport_writing->outbuf,
+ grpc_chttp2_rst_stream_create(stream_writing->id,
+ GRPC_CHTTP2_NO_ERROR));
+ }
+ grpc_chttp2_list_add_written_stream(transport_writing, stream_writing);
+ }
}
-void
-grpc_chttp2_cleanup_writing (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, grpc_chttp2_transport_writing * transport_writing)
-{
+void grpc_chttp2_cleanup_writing(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_transport_writing *transport_writing) {
grpc_chttp2_stream_writing *stream_writing;
grpc_chttp2_stream_global *stream_global;
- while (grpc_chttp2_list_pop_written_stream (transport_global, transport_writing, &stream_global, &stream_writing))
- {
- GPR_ASSERT (stream_global->writing_now != 0);
- if (stream_writing->send_closed != GRPC_DONT_SEND_CLOSED)
- {
- stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE;
- if (!transport_global->is_client)
- {
- stream_global->read_closed = 1;
- }
- }
- if (stream_global->writing_now & GRPC_CHTTP2_WRITING_DATA)
- {
- if (stream_global->outgoing_sopb != NULL && stream_global->outgoing_sopb->nops == 0)
- {
- GPR_ASSERT (stream_global->write_state != GRPC_WRITE_STATE_QUEUED_CLOSE);
- stream_global->outgoing_sopb = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, stream_global->send_done_closure, 1);
- }
- }
- stream_global->writing_now = 0;
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
+ while (grpc_chttp2_list_pop_written_stream(
+ transport_global, transport_writing, &stream_global, &stream_writing)) {
+ GPR_ASSERT(stream_global->writing_now != 0);
+ if (stream_writing->send_closed != GRPC_DONT_SEND_CLOSED) {
+ stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE;
+ if (!transport_global->is_client) {
+ stream_global->read_closed = 1;
+ }
+ }
+ if (stream_global->writing_now & GRPC_CHTTP2_WRITING_DATA) {
+ if (stream_global->outgoing_sopb != NULL &&
+ stream_global->outgoing_sopb->nops == 0) {
+ GPR_ASSERT(stream_global->write_state != GRPC_WRITE_STATE_QUEUED_CLOSE);
+ stream_global->outgoing_sopb = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, stream_global->send_done_closure, 1);
+ }
}
- gpr_slice_buffer_reset_and_unref (&transport_writing->outbuf);
+ stream_global->writing_now = 0;
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
+ }
+ gpr_slice_buffer_reset_and_unref(&transport_writing->outbuf);
}
diff --git a/src/core/transport/chttp2_transport.c b/src/core/transport/chttp2_transport.c
index bfcb62426c..1679e4345d 100644
--- a/src/core/transport/chttp2_transport.c
+++ b/src/core/transport/chttp2_transport.c
@@ -77,153 +77,161 @@ int grpc_flowctl_trace = 0;
static const grpc_transport_vtable vtable;
-static void lock (grpc_chttp2_transport * t);
-static void unlock (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t);
+static void lock(grpc_chttp2_transport *t);
+static void unlock(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t);
-static void unlock_check_read_write_state (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t);
+static void unlock_check_read_write_state(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t);
/* forward declarations of various callbacks that we'll build closures around */
-static void writing_action (grpc_exec_ctx * exec_ctx, void *t, int iomgr_success_ignored);
+static void writing_action(grpc_exec_ctx *exec_ctx, void *t,
+ int iomgr_success_ignored);
/** Set a transport level setting, and push it to our peer */
-static void push_setting (grpc_chttp2_transport * t, grpc_chttp2_setting_id id, gpr_uint32 value);
+static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id,
+ gpr_uint32 value);
/** Endpoint callback to process incoming data */
-static void recv_data (grpc_exec_ctx * exec_ctx, void *tp, int success);
+static void recv_data(grpc_exec_ctx *exec_ctx, void *tp, int success);
/** Start disconnection chain */
-static void drop_connection (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t);
+static void drop_connection(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t);
/** Perform a transport_op */
-static void perform_stream_op_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global, grpc_transport_stream_op * op);
+static void perform_stream_op_locked(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global, grpc_transport_stream_op *op);
/** Cancel a stream: coming from the transport API */
-static void cancel_from_api (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global, grpc_status_code status);
+static void cancel_from_api(grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global,
+ grpc_status_code status);
-static void close_from_api (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global, grpc_status_code status, gpr_slice * optional_message);
+static void close_from_api(grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global,
+ grpc_status_code status,
+ gpr_slice *optional_message);
/** Add endpoint from this transport to pollset */
-static void add_to_pollset_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, grpc_pollset * pollset);
-static void add_to_pollset_set_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, grpc_pollset_set * pollset_set);
+static void add_to_pollset_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t,
+ grpc_pollset *pollset);
+static void add_to_pollset_set_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t,
+ grpc_pollset_set *pollset_set);
/** Start new streams that have been created if we can */
-static void maybe_start_some_streams (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global);
+static void maybe_start_some_streams(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global);
-static void connectivity_state_set (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, grpc_connectivity_state state, const char *reason);
+static void connectivity_state_set(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ grpc_connectivity_state state, const char *reason);
/*
* CONSTRUCTION/DESTRUCTION/REFCOUNTING
*/
-static void
-destruct_transport (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
+static void destruct_transport(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
size_t i;
- gpr_mu_lock (&t->mu);
+ gpr_mu_lock(&t->mu);
- GPR_ASSERT (t->ep == NULL);
+ GPR_ASSERT(t->ep == NULL);
- gpr_slice_buffer_destroy (&t->global.qbuf);
+ gpr_slice_buffer_destroy(&t->global.qbuf);
- gpr_slice_buffer_destroy (&t->writing.outbuf);
- grpc_chttp2_hpack_compressor_destroy (&t->writing.hpack_compressor);
+ gpr_slice_buffer_destroy(&t->writing.outbuf);
+ grpc_chttp2_hpack_compressor_destroy(&t->writing.hpack_compressor);
- gpr_slice_buffer_destroy (&t->parsing.qbuf);
- gpr_slice_buffer_destroy (&t->read_buffer);
- grpc_chttp2_hpack_parser_destroy (&t->parsing.hpack_parser);
- grpc_chttp2_goaway_parser_destroy (&t->parsing.goaway_parser);
+ gpr_slice_buffer_destroy(&t->parsing.qbuf);
+ gpr_slice_buffer_destroy(&t->read_buffer);
+ grpc_chttp2_hpack_parser_destroy(&t->parsing.hpack_parser);
+ grpc_chttp2_goaway_parser_destroy(&t->parsing.goaway_parser);
- GRPC_MDSTR_UNREF (t->parsing.str_grpc_timeout);
+ GRPC_MDSTR_UNREF(t->parsing.str_grpc_timeout);
- for (i = 0; i < STREAM_LIST_COUNT; i++)
- {
- GPR_ASSERT (t->lists[i].head == NULL);
- GPR_ASSERT (t->lists[i].tail == NULL);
- }
+ for (i = 0; i < STREAM_LIST_COUNT; i++) {
+ GPR_ASSERT(t->lists[i].head == NULL);
+ GPR_ASSERT(t->lists[i].tail == NULL);
+ }
- GPR_ASSERT (grpc_chttp2_stream_map_size (&t->parsing_stream_map) == 0);
- GPR_ASSERT (grpc_chttp2_stream_map_size (&t->new_stream_map) == 0);
+ GPR_ASSERT(grpc_chttp2_stream_map_size(&t->parsing_stream_map) == 0);
+ GPR_ASSERT(grpc_chttp2_stream_map_size(&t->new_stream_map) == 0);
- grpc_chttp2_stream_map_destroy (&t->parsing_stream_map);
- grpc_chttp2_stream_map_destroy (&t->new_stream_map);
- grpc_connectivity_state_destroy (exec_ctx, &t->channel_callback.state_tracker);
+ grpc_chttp2_stream_map_destroy(&t->parsing_stream_map);
+ grpc_chttp2_stream_map_destroy(&t->new_stream_map);
+ grpc_connectivity_state_destroy(exec_ctx, &t->channel_callback.state_tracker);
- gpr_mu_unlock (&t->mu);
- gpr_mu_destroy (&t->mu);
+ gpr_mu_unlock(&t->mu);
+ gpr_mu_destroy(&t->mu);
/* callback remaining pings: they're not allowed to call into the transpot,
and maybe they hold resources that need to be freed */
- while (t->global.pings.next != &t->global.pings)
- {
- grpc_chttp2_outstanding_ping *ping = t->global.pings.next;
- grpc_exec_ctx_enqueue (exec_ctx, ping->on_recv, 0);
- ping->next->prev = ping->prev;
- ping->prev->next = ping->next;
- gpr_free (ping);
- }
-
- grpc_mdctx_unref (t->metadata_context);
-
- gpr_free (t->peer_string);
- gpr_free (t);
+ while (t->global.pings.next != &t->global.pings) {
+ grpc_chttp2_outstanding_ping *ping = t->global.pings.next;
+ grpc_exec_ctx_enqueue(exec_ctx, ping->on_recv, 0);
+ ping->next->prev = ping->prev;
+ ping->prev->next = ping->next;
+ gpr_free(ping);
+ }
+
+ grpc_mdctx_unref(t->metadata_context);
+
+ gpr_free(t->peer_string);
+ gpr_free(t);
}
#ifdef REFCOUNTING_DEBUG
#define REF_TRANSPORT(t, r) ref_transport(t, r, __FILE__, __LINE__)
#define UNREF_TRANSPORT(cl, t, r) unref_transport(cl, t, r, __FILE__, __LINE__)
-static void
-unref_transport (grpc_exec_ctx *exec_ctx, grpc_chttp2_transport * t, const char *reason, const char *file, int line)
-{
- gpr_log (GPR_DEBUG, "chttp2:unref:%p %d->%d %s [%s:%d]", t, t->refs.count, t->refs.count - 1, reason, file, line);
- if (!gpr_unref (&t->refs))
- return;
- destruct_transport (exec_ctx, t);
+static void unref_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
+ const char *reason, const char *file, int line) {
+ gpr_log(GPR_DEBUG, "chttp2:unref:%p %d->%d %s [%s:%d]", t, t->refs.count,
+ t->refs.count - 1, reason, file, line);
+ if (!gpr_unref(&t->refs)) return;
+ destruct_transport(exec_ctx, t);
}
-static void
-ref_transport (grpc_chttp2_transport * t, const char *reason, const char *file, int line)
-{
- gpr_log (GPR_DEBUG, "chttp2: ref:%p %d->%d %s [%s:%d]", t, t->refs.count, t->refs.count + 1, reason, file, line);
- gpr_ref (&t->refs);
+static void ref_transport(grpc_chttp2_transport *t, const char *reason,
+ const char *file, int line) {
+ gpr_log(GPR_DEBUG, "chttp2: ref:%p %d->%d %s [%s:%d]", t, t->refs.count,
+ t->refs.count + 1, reason, file, line);
+ gpr_ref(&t->refs);
}
#else
#define REF_TRANSPORT(t, r) ref_transport(t)
#define UNREF_TRANSPORT(cl, t, r) unref_transport(cl, t)
-static void
-unref_transport (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- if (!gpr_unref (&t->refs))
- return;
- destruct_transport (exec_ctx, t);
+static void unref_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) {
+ if (!gpr_unref(&t->refs)) return;
+ destruct_transport(exec_ctx, t);
}
-static void
-ref_transport (grpc_chttp2_transport * t)
-{
- gpr_ref (&t->refs);
-}
+static void ref_transport(grpc_chttp2_transport *t) { gpr_ref(&t->refs); }
#endif
-static void
-init_transport (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, const grpc_channel_args * channel_args, grpc_endpoint * ep, grpc_mdctx * mdctx, gpr_uint8 is_client)
-{
+static void init_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
+ const grpc_channel_args *channel_args,
+ grpc_endpoint *ep, grpc_mdctx *mdctx,
+ gpr_uint8 is_client) {
size_t i;
int j;
- GPR_ASSERT (strlen (GRPC_CHTTP2_CLIENT_CONNECT_STRING) == GRPC_CHTTP2_CLIENT_CONNECT_STRLEN);
+ GPR_ASSERT(strlen(GRPC_CHTTP2_CLIENT_CONNECT_STRING) ==
+ GRPC_CHTTP2_CLIENT_CONNECT_STRLEN);
- memset (t, 0, sizeof (*t));
+ memset(t, 0, sizeof(*t));
t->base.vtable = &vtable;
t->ep = ep;
/* one ref is for destroy, the other for when ep becomes NULL */
- gpr_ref_init (&t->refs, 2);
+ gpr_ref_init(&t->refs, 2);
/* ref is dropped at transport close() */
- gpr_ref_init (&t->shutdown_ep_refs, 1);
- gpr_mu_init (&t->mu);
- grpc_mdctx_ref (mdctx);
- t->peer_string = grpc_endpoint_get_peer (ep);
+ gpr_ref_init(&t->shutdown_ep_refs, 1);
+ gpr_mu_init(&t->mu);
+ grpc_mdctx_ref(mdctx);
+ t->peer_string = grpc_endpoint_get_peer(ep);
t->metadata_context = mdctx;
t->endpoint_reading = 1;
t->global.next_stream_id = is_client ? 1 : 2;
@@ -234,46 +242,51 @@ init_transport (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, const grpc_
t->global.ping_counter = 1;
t->global.pings.next = t->global.pings.prev = &t->global.pings;
t->parsing.is_client = is_client;
- t->parsing.str_grpc_timeout = grpc_mdstr_from_string (t->metadata_context, "grpc-timeout", 0);
- t->parsing.deframe_state = is_client ? GRPC_DTS_FH_0 : GRPC_DTS_CLIENT_PREFIX_0;
+ t->parsing.str_grpc_timeout =
+ grpc_mdstr_from_string(t->metadata_context, "grpc-timeout", 0);
+ t->parsing.deframe_state =
+ is_client ? GRPC_DTS_FH_0 : GRPC_DTS_CLIENT_PREFIX_0;
t->writing.is_client = is_client;
- grpc_connectivity_state_init (&t->channel_callback.state_tracker, GRPC_CHANNEL_READY, is_client ? "client_transport" : "server_transport");
-
- gpr_slice_buffer_init (&t->global.qbuf);
-
- gpr_slice_buffer_init (&t->writing.outbuf);
- grpc_chttp2_hpack_compressor_init (&t->writing.hpack_compressor, mdctx);
- grpc_closure_init (&t->writing_action, writing_action, t);
-
- gpr_slice_buffer_init (&t->parsing.qbuf);
- grpc_chttp2_goaway_parser_init (&t->parsing.goaway_parser);
- grpc_chttp2_hpack_parser_init (&t->parsing.hpack_parser, t->metadata_context);
-
- grpc_closure_init (&t->writing.done_cb, grpc_chttp2_terminate_writing, &t->writing);
- grpc_closure_init (&t->recv_data, recv_data, t);
- gpr_slice_buffer_init (&t->read_buffer);
-
- if (is_client)
- {
- gpr_slice_buffer_add (&t->global.qbuf, gpr_slice_from_copied_string (GRPC_CHTTP2_CLIENT_CONNECT_STRING));
- }
+ grpc_connectivity_state_init(
+ &t->channel_callback.state_tracker, GRPC_CHANNEL_READY,
+ is_client ? "client_transport" : "server_transport");
+
+ gpr_slice_buffer_init(&t->global.qbuf);
+
+ gpr_slice_buffer_init(&t->writing.outbuf);
+ grpc_chttp2_hpack_compressor_init(&t->writing.hpack_compressor, mdctx);
+ grpc_closure_init(&t->writing_action, writing_action, t);
+
+ gpr_slice_buffer_init(&t->parsing.qbuf);
+ grpc_chttp2_goaway_parser_init(&t->parsing.goaway_parser);
+ grpc_chttp2_hpack_parser_init(&t->parsing.hpack_parser, t->metadata_context);
+
+ grpc_closure_init(&t->writing.done_cb, grpc_chttp2_terminate_writing,
+ &t->writing);
+ grpc_closure_init(&t->recv_data, recv_data, t);
+ gpr_slice_buffer_init(&t->read_buffer);
+
+ if (is_client) {
+ gpr_slice_buffer_add(
+ &t->global.qbuf,
+ gpr_slice_from_copied_string(GRPC_CHTTP2_CLIENT_CONNECT_STRING));
+ }
/* 8 is a random stab in the dark as to a good initial size: it's small enough
that it shouldn't waste memory for infrequently used connections, yet
large enough that the exponential growth should happen nicely when it's
needed.
TODO(ctiller): tune this */
- grpc_chttp2_stream_map_init (&t->parsing_stream_map, 8);
- grpc_chttp2_stream_map_init (&t->new_stream_map, 8);
+ grpc_chttp2_stream_map_init(&t->parsing_stream_map, 8);
+ grpc_chttp2_stream_map_init(&t->new_stream_map, 8);
/* copy in initial settings to all setting sets */
- for (i = 0; i < GRPC_CHTTP2_NUM_SETTINGS; i++)
- {
- t->parsing.settings[i] = grpc_chttp2_settings_parameters[i].default_value;
- for (j = 0; j < GRPC_NUM_SETTING_SETS; j++)
- {
- t->global.settings[j][i] = grpc_chttp2_settings_parameters[i].default_value;
- }
+ for (i = 0; i < GRPC_CHTTP2_NUM_SETTINGS; i++) {
+ t->parsing.settings[i] = grpc_chttp2_settings_parameters[i].default_value;
+ for (j = 0; j < GRPC_NUM_SETTING_SETS; j++) {
+ t->global.settings[j][i] =
+ grpc_chttp2_settings_parameters[i].default_value;
}
+ }
t->global.dirtied_local_settings = 1;
/* Hack: it's common for implementations to assume 65536 bytes initial send
window -- this should by rights be 0 */
@@ -281,220 +294,205 @@ init_transport (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, const grpc_
t->global.sent_local_settings = 0;
/* configure http2 the way we like it */
- if (is_client)
- {
- push_setting (t, GRPC_CHTTP2_SETTINGS_ENABLE_PUSH, 0);
- push_setting (t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 0);
- }
- push_setting (t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, DEFAULT_WINDOW);
-
- if (channel_args)
- {
- for (i = 0; i < channel_args->num_args; i++)
- {
- if (0 == strcmp (channel_args->args[i].key, GRPC_ARG_MAX_CONCURRENT_STREAMS))
- {
- if (is_client)
- {
- gpr_log (GPR_ERROR, "%s: is ignored on the client", GRPC_ARG_MAX_CONCURRENT_STREAMS);
- }
- else if (channel_args->args[i].type != GRPC_ARG_INTEGER)
- {
- gpr_log (GPR_ERROR, "%s: must be an integer", GRPC_ARG_MAX_CONCURRENT_STREAMS);
- }
- else
- {
- push_setting (t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, (gpr_uint32) channel_args->args[i].value.integer);
- }
- }
- else if (0 == strcmp (channel_args->args[i].key, GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER))
- {
- if (channel_args->args[i].type != GRPC_ARG_INTEGER)
- {
- gpr_log (GPR_ERROR, "%s: must be an integer", GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER);
- }
- else if ((t->global.next_stream_id & 1) !=(channel_args->args[i].value.integer & 1))
- {
- gpr_log (GPR_ERROR, "%s: low bit must be %d on %s", GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, t->global.next_stream_id & 1, is_client ? "client" : "server");
- }
- else
- {
- t->global.next_stream_id = (gpr_uint32) channel_args->args[i].value.integer;
- }
- }
- }
+ if (is_client) {
+ push_setting(t, GRPC_CHTTP2_SETTINGS_ENABLE_PUSH, 0);
+ push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 0);
+ }
+ push_setting(t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, DEFAULT_WINDOW);
+
+ if (channel_args) {
+ for (i = 0; i < channel_args->num_args; i++) {
+ if (0 ==
+ strcmp(channel_args->args[i].key, GRPC_ARG_MAX_CONCURRENT_STREAMS)) {
+ if (is_client) {
+ gpr_log(GPR_ERROR, "%s: is ignored on the client",
+ GRPC_ARG_MAX_CONCURRENT_STREAMS);
+ } else if (channel_args->args[i].type != GRPC_ARG_INTEGER) {
+ gpr_log(GPR_ERROR, "%s: must be an integer",
+ GRPC_ARG_MAX_CONCURRENT_STREAMS);
+ } else {
+ push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS,
+ (gpr_uint32)channel_args->args[i].value.integer);
+ }
+ } else if (0 == strcmp(channel_args->args[i].key,
+ GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER)) {
+ if (channel_args->args[i].type != GRPC_ARG_INTEGER) {
+ gpr_log(GPR_ERROR, "%s: must be an integer",
+ GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER);
+ } else if ((t->global.next_stream_id & 1) !=
+ (channel_args->args[i].value.integer & 1)) {
+ gpr_log(GPR_ERROR, "%s: low bit must be %d on %s",
+ GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER,
+ t->global.next_stream_id & 1,
+ is_client ? "client" : "server");
+ } else {
+ t->global.next_stream_id =
+ (gpr_uint32)channel_args->args[i].value.integer;
+ }
+ }
}
+ }
}
-static void
-destroy_transport (grpc_exec_ctx * exec_ctx, grpc_transport * gt)
-{
- grpc_chttp2_transport *t = (grpc_chttp2_transport *) gt;
+static void destroy_transport(grpc_exec_ctx *exec_ctx, grpc_transport *gt) {
+ grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt;
- lock (t);
+ lock(t);
t->destroying = 1;
- drop_connection (exec_ctx, t);
- unlock (exec_ctx, t);
+ drop_connection(exec_ctx, t);
+ unlock(exec_ctx, t);
- UNREF_TRANSPORT (exec_ctx, t, "destroy");
+ UNREF_TRANSPORT(exec_ctx, t, "destroy");
}
/** block grpc_endpoint_shutdown being called until a paired
allow_endpoint_shutdown is made */
-static void
-prevent_endpoint_shutdown (grpc_chttp2_transport * t)
-{
- GPR_ASSERT (t->ep);
- gpr_ref (&t->shutdown_ep_refs);
+static void prevent_endpoint_shutdown(grpc_chttp2_transport *t) {
+ GPR_ASSERT(t->ep);
+ gpr_ref(&t->shutdown_ep_refs);
}
-static void
-allow_endpoint_shutdown_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- if (gpr_unref (&t->shutdown_ep_refs))
- {
- if (t->ep)
- {
- grpc_endpoint_shutdown (exec_ctx, t->ep);
- }
+static void allow_endpoint_shutdown_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
+ if (gpr_unref(&t->shutdown_ep_refs)) {
+ if (t->ep) {
+ grpc_endpoint_shutdown(exec_ctx, t->ep);
}
+ }
}
-static void
-allow_endpoint_shutdown_unlocked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- if (gpr_unref (&t->shutdown_ep_refs))
- {
- gpr_mu_lock (&t->mu);
- if (t->ep)
- {
- grpc_endpoint_shutdown (exec_ctx, t->ep);
- }
- gpr_mu_unlock (&t->mu);
+static void allow_endpoint_shutdown_unlocked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
+ if (gpr_unref(&t->shutdown_ep_refs)) {
+ gpr_mu_lock(&t->mu);
+ if (t->ep) {
+ grpc_endpoint_shutdown(exec_ctx, t->ep);
}
+ gpr_mu_unlock(&t->mu);
+ }
}
-static void
-destroy_endpoint (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- grpc_endpoint_destroy (exec_ctx, t->ep);
+static void destroy_endpoint(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
+ grpc_endpoint_destroy(exec_ctx, t->ep);
t->ep = NULL;
/* safe because we'll still have the ref for write */
- UNREF_TRANSPORT (exec_ctx, t, "disconnect");
+ UNREF_TRANSPORT(exec_ctx, t, "disconnect");
}
-static void
-close_transport_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- if (!t->closed)
- {
- t->closed = 1;
- connectivity_state_set (exec_ctx, &t->global, GRPC_CHANNEL_FATAL_FAILURE, "close_transport");
- if (t->ep)
- {
- allow_endpoint_shutdown_locked (exec_ctx, t);
- }
+static void close_transport_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
+ if (!t->closed) {
+ t->closed = 1;
+ connectivity_state_set(exec_ctx, &t->global, GRPC_CHANNEL_FATAL_FAILURE,
+ "close_transport");
+ if (t->ep) {
+ allow_endpoint_shutdown_locked(exec_ctx, t);
}
+ }
}
-static int
-init_stream (grpc_exec_ctx * exec_ctx, grpc_transport * gt, grpc_stream * gs, const void *server_data, grpc_transport_stream_op * initial_op)
-{
- grpc_chttp2_transport *t = (grpc_chttp2_transport *) gt;
- grpc_chttp2_stream *s = (grpc_chttp2_stream *) gs;
-
- memset (s, 0, sizeof (*s));
-
- grpc_chttp2_incoming_metadata_buffer_init (&s->parsing.incoming_metadata);
- grpc_chttp2_incoming_metadata_buffer_init (&s->global.incoming_metadata);
- grpc_sopb_init (&s->writing.sopb);
- grpc_sopb_init (&s->global.incoming_sopb);
- grpc_chttp2_data_parser_init (&s->parsing.data_parser);
-
- REF_TRANSPORT (t, "stream");
-
- lock (t);
- grpc_chttp2_register_stream (t, s);
- if (server_data)
- {
- GPR_ASSERT (t->parsing_active);
- s->global.id = (gpr_uint32) (gpr_uintptr) server_data;
- s->global.outgoing_window = t->global.settings[GRPC_PEER_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
- s->global.max_recv_bytes = s->parsing.incoming_window = s->global.incoming_window = t->global.settings[GRPC_SENT_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
- *t->accepting_stream = s;
- grpc_chttp2_stream_map_add (&t->parsing_stream_map, s->global.id, s);
- s->global.in_stream_map = 1;
- }
+static int init_stream(grpc_exec_ctx *exec_ctx, grpc_transport *gt,
+ grpc_stream *gs, const void *server_data,
+ grpc_transport_stream_op *initial_op) {
+ grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt;
+ grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs;
+
+ memset(s, 0, sizeof(*s));
+
+ grpc_chttp2_incoming_metadata_buffer_init(&s->parsing.incoming_metadata);
+ grpc_chttp2_incoming_metadata_buffer_init(&s->global.incoming_metadata);
+ grpc_sopb_init(&s->writing.sopb);
+ grpc_sopb_init(&s->global.incoming_sopb);
+ grpc_chttp2_data_parser_init(&s->parsing.data_parser);
+
+ REF_TRANSPORT(t, "stream");
+
+ lock(t);
+ grpc_chttp2_register_stream(t, s);
+ if (server_data) {
+ GPR_ASSERT(t->parsing_active);
+ s->global.id = (gpr_uint32)(gpr_uintptr)server_data;
+ s->global.outgoing_window =
+ t->global.settings[GRPC_PEER_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
+ s->global.max_recv_bytes = s->parsing.incoming_window =
+ s->global.incoming_window =
+ t->global.settings[GRPC_SENT_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
+ *t->accepting_stream = s;
+ grpc_chttp2_stream_map_add(&t->parsing_stream_map, s->global.id, s);
+ s->global.in_stream_map = 1;
+ }
if (initial_op)
- perform_stream_op_locked (exec_ctx, &t->global, &s->global, initial_op);
- unlock (exec_ctx, t);
+ perform_stream_op_locked(exec_ctx, &t->global, &s->global, initial_op);
+ unlock(exec_ctx, t);
return 0;
}
-static void
-destroy_stream (grpc_exec_ctx * exec_ctx, grpc_transport * gt, grpc_stream * gs)
-{
- grpc_chttp2_transport *t = (grpc_chttp2_transport *) gt;
- grpc_chttp2_stream *s = (grpc_chttp2_stream *) gs;
+static void destroy_stream(grpc_exec_ctx *exec_ctx, grpc_transport *gt,
+ grpc_stream *gs) {
+ grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt;
+ grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs;
int i;
- gpr_mu_lock (&t->mu);
-
- GPR_ASSERT (s->global.published_state == GRPC_STREAM_CLOSED || s->global.id == 0);
- GPR_ASSERT (!s->global.in_stream_map);
- if (grpc_chttp2_unregister_stream (t, s) && t->global.sent_goaway)
- {
- close_transport_locked (exec_ctx, t);
- }
- if (!t->parsing_active && s->global.id)
- {
- GPR_ASSERT (grpc_chttp2_stream_map_find (&t->parsing_stream_map, s->global.id) == NULL);
- }
-
- grpc_chttp2_list_remove_incoming_window_updated (&t->global, &s->global);
- grpc_chttp2_list_remove_writable_stream (&t->global, &s->global);
-
- gpr_mu_unlock (&t->mu);
-
- for (i = 0; i < STREAM_LIST_COUNT; i++)
- {
- if (s->included[i])
- {
- gpr_log (GPR_ERROR, "%s stream %d still included in list %d", t->global.is_client ? "client" : "server", s->global.id, i);
- abort ();
- }
+ gpr_mu_lock(&t->mu);
+
+ GPR_ASSERT(s->global.published_state == GRPC_STREAM_CLOSED ||
+ s->global.id == 0);
+ GPR_ASSERT(!s->global.in_stream_map);
+ if (grpc_chttp2_unregister_stream(t, s) && t->global.sent_goaway) {
+ close_transport_locked(exec_ctx, t);
+ }
+ if (!t->parsing_active && s->global.id) {
+ GPR_ASSERT(grpc_chttp2_stream_map_find(&t->parsing_stream_map,
+ s->global.id) == NULL);
+ }
+
+ grpc_chttp2_list_remove_incoming_window_updated(&t->global, &s->global);
+ grpc_chttp2_list_remove_writable_stream(&t->global, &s->global);
+
+ gpr_mu_unlock(&t->mu);
+
+ for (i = 0; i < STREAM_LIST_COUNT; i++) {
+ if (s->included[i]) {
+ gpr_log(GPR_ERROR, "%s stream %d still included in list %d",
+ t->global.is_client ? "client" : "server", s->global.id, i);
+ abort();
}
-
- GPR_ASSERT (s->global.outgoing_sopb == NULL);
- GPR_ASSERT (s->global.publish_sopb == NULL);
- grpc_sopb_destroy (&s->writing.sopb);
- grpc_sopb_destroy (&s->global.incoming_sopb);
- grpc_chttp2_data_parser_destroy (&s->parsing.data_parser);
- grpc_chttp2_incoming_metadata_buffer_destroy (&s->parsing.incoming_metadata);
- grpc_chttp2_incoming_metadata_buffer_destroy (&s->global.incoming_metadata);
- grpc_chttp2_incoming_metadata_live_op_buffer_end (&s->global.outstanding_metadata);
-
- UNREF_TRANSPORT (exec_ctx, t, "stream");
+ }
+
+ GPR_ASSERT(s->global.outgoing_sopb == NULL);
+ GPR_ASSERT(s->global.publish_sopb == NULL);
+ grpc_sopb_destroy(&s->writing.sopb);
+ grpc_sopb_destroy(&s->global.incoming_sopb);
+ grpc_chttp2_data_parser_destroy(&s->parsing.data_parser);
+ grpc_chttp2_incoming_metadata_buffer_destroy(&s->parsing.incoming_metadata);
+ grpc_chttp2_incoming_metadata_buffer_destroy(&s->global.incoming_metadata);
+ grpc_chttp2_incoming_metadata_live_op_buffer_end(
+ &s->global.outstanding_metadata);
+
+ UNREF_TRANSPORT(exec_ctx, t, "stream");
}
-grpc_chttp2_stream_parsing *
-grpc_chttp2_parsing_lookup_stream (grpc_chttp2_transport_parsing * transport_parsing, gpr_uint32 id)
-{
- grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING (transport_parsing);
- grpc_chttp2_stream *s = grpc_chttp2_stream_map_find (&t->parsing_stream_map, id);
+grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
+ grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id) {
+ grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing);
+ grpc_chttp2_stream *s =
+ grpc_chttp2_stream_map_find(&t->parsing_stream_map, id);
return s ? &s->parsing : NULL;
}
-grpc_chttp2_stream_parsing *
-grpc_chttp2_parsing_accept_stream (grpc_chttp2_transport_parsing * transport_parsing, gpr_uint32 id)
-{
+grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
+ grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id) {
grpc_chttp2_stream *accepting;
- grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING (transport_parsing);
- GPR_ASSERT (t->accepting_stream == NULL);
+ grpc_chttp2_transport *t = TRANSPORT_FROM_PARSING(transport_parsing);
+ GPR_ASSERT(t->accepting_stream == NULL);
t->accepting_stream = &accepting;
- t->channel_callback.accept_stream (t->channel_callback.accept_stream_user_data, &t->base, (void *) (gpr_uintptr) id);
+ t->channel_callback.accept_stream(t->channel_callback.accept_stream_user_data,
+ &t->base, (void *)(gpr_uintptr)id);
t->accepting_stream = NULL;
return &accepting->parsing;
}
@@ -509,467 +507,466 @@ grpc_chttp2_parsing_accept_stream (grpc_chttp2_transport_parsing * transport_par
is always queued, as are callbacks to process data. During unlock() we
check our todo lists and initiate callbacks and flush writes. */
-static void
-lock (grpc_chttp2_transport * t)
-{
- gpr_mu_lock (&t->mu);
-}
+static void lock(grpc_chttp2_transport *t) { gpr_mu_lock(&t->mu); }
-static void
-unlock (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- unlock_check_read_write_state (exec_ctx, t);
- if (!t->writing_active && !t->closed && grpc_chttp2_unlocking_check_writes (&t->global, &t->writing))
- {
- t->writing_active = 1;
- REF_TRANSPORT (t, "writing");
- grpc_exec_ctx_enqueue (exec_ctx, &t->writing_action, 1);
- prevent_endpoint_shutdown (t);
- }
+static void unlock(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) {
+ unlock_check_read_write_state(exec_ctx, t);
+ if (!t->writing_active && !t->closed &&
+ grpc_chttp2_unlocking_check_writes(&t->global, &t->writing)) {
+ t->writing_active = 1;
+ REF_TRANSPORT(t, "writing");
+ grpc_exec_ctx_enqueue(exec_ctx, &t->writing_action, 1);
+ prevent_endpoint_shutdown(t);
+ }
- gpr_mu_unlock (&t->mu);
+ gpr_mu_unlock(&t->mu);
}
/*
* OUTPUT PROCESSING
*/
-static void
-push_setting (grpc_chttp2_transport * t, grpc_chttp2_setting_id id, gpr_uint32 value)
-{
- const grpc_chttp2_setting_parameters *sp = &grpc_chttp2_settings_parameters[id];
- gpr_uint32 use_value = GPR_CLAMP (value, sp->min_value, sp->max_value);
- if (use_value != value)
- {
- gpr_log (GPR_INFO, "Requested parameter %s clamped from %d to %d", sp->name, value, use_value);
- }
- if (use_value != t->global.settings[GRPC_LOCAL_SETTINGS][id])
- {
- t->global.settings[GRPC_LOCAL_SETTINGS][id] = use_value;
- t->global.dirtied_local_settings = 1;
- }
+static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id,
+ gpr_uint32 value) {
+ const grpc_chttp2_setting_parameters *sp =
+ &grpc_chttp2_settings_parameters[id];
+ gpr_uint32 use_value = GPR_CLAMP(value, sp->min_value, sp->max_value);
+ if (use_value != value) {
+ gpr_log(GPR_INFO, "Requested parameter %s clamped from %d to %d", sp->name,
+ value, use_value);
+ }
+ if (use_value != t->global.settings[GRPC_LOCAL_SETTINGS][id]) {
+ t->global.settings[GRPC_LOCAL_SETTINGS][id] = use_value;
+ t->global.dirtied_local_settings = 1;
+ }
}
-void
-grpc_chttp2_terminate_writing (grpc_exec_ctx * exec_ctx, void *transport_writing_ptr, int success)
-{
+void grpc_chttp2_terminate_writing(grpc_exec_ctx *exec_ctx,
+ void *transport_writing_ptr, int success) {
grpc_chttp2_transport_writing *transport_writing = transport_writing_ptr;
- grpc_chttp2_transport *t = TRANSPORT_FROM_WRITING (transport_writing);
+ grpc_chttp2_transport *t = TRANSPORT_FROM_WRITING(transport_writing);
- lock (t);
+ lock(t);
- allow_endpoint_shutdown_locked (exec_ctx, t);
+ allow_endpoint_shutdown_locked(exec_ctx, t);
- if (!success)
- {
- drop_connection (exec_ctx, t);
- }
+ if (!success) {
+ drop_connection(exec_ctx, t);
+ }
/* cleanup writing related jazz */
- grpc_chttp2_cleanup_writing (exec_ctx, &t->global, &t->writing);
+ grpc_chttp2_cleanup_writing(exec_ctx, &t->global, &t->writing);
/* leave the writing flag up on shutdown to prevent further writes in unlock()
from starting */
t->writing_active = 0;
- if (t->ep && !t->endpoint_reading)
- {
- destroy_endpoint (exec_ctx, t);
- }
+ if (t->ep && !t->endpoint_reading) {
+ destroy_endpoint(exec_ctx, t);
+ }
- unlock (exec_ctx, t);
+ unlock(exec_ctx, t);
- UNREF_TRANSPORT (exec_ctx, t, "writing");
+ UNREF_TRANSPORT(exec_ctx, t, "writing");
}
-static void
-writing_action (grpc_exec_ctx * exec_ctx, void *gt, int iomgr_success_ignored)
-{
+static void writing_action(grpc_exec_ctx *exec_ctx, void *gt,
+ int iomgr_success_ignored) {
grpc_chttp2_transport *t = gt;
- grpc_chttp2_perform_writes (exec_ctx, &t->writing, t->ep);
+ grpc_chttp2_perform_writes(exec_ctx, &t->writing, t->ep);
}
-void
-grpc_chttp2_add_incoming_goaway (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, gpr_uint32 goaway_error, gpr_slice goaway_text)
-{
- char *msg = gpr_dump_slice (goaway_text, GPR_DUMP_HEX | GPR_DUMP_ASCII);
- gpr_log (GPR_DEBUG, "got goaway [%d]: %s", goaway_error, msg);
- gpr_free (msg);
- gpr_slice_unref (goaway_text);
+void grpc_chttp2_add_incoming_goaway(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ gpr_uint32 goaway_error, gpr_slice goaway_text) {
+ char *msg = gpr_dump_slice(goaway_text, GPR_DUMP_HEX | GPR_DUMP_ASCII);
+ gpr_log(GPR_DEBUG, "got goaway [%d]: %s", goaway_error, msg);
+ gpr_free(msg);
+ gpr_slice_unref(goaway_text);
transport_global->seen_goaway = 1;
- connectivity_state_set (exec_ctx, transport_global, GRPC_CHANNEL_FATAL_FAILURE, "got_goaway");
+ connectivity_state_set(exec_ctx, transport_global, GRPC_CHANNEL_FATAL_FAILURE,
+ "got_goaway");
}
-static void
-maybe_start_some_streams (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global)
-{
+static void maybe_start_some_streams(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global) {
grpc_chttp2_stream_global *stream_global;
/* start streams where we have free grpc_chttp2_stream ids and free
* concurrency */
- while (transport_global->next_stream_id <= MAX_CLIENT_STREAM_ID && transport_global->concurrent_stream_count < transport_global->settings[GRPC_PEER_SETTINGS][GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] && grpc_chttp2_list_pop_waiting_for_concurrency (transport_global, &stream_global))
- {
- GRPC_CHTTP2_IF_TRACING (gpr_log (GPR_DEBUG, "HTTP:%s: Allocating new grpc_chttp2_stream %p to id %d", transport_global->is_client ? "CLI" : "SVR", stream_global, transport_global->next_stream_id));
-
- GPR_ASSERT (stream_global->id == 0);
- stream_global->id = transport_global->next_stream_id;
- transport_global->next_stream_id += 2;
-
- if (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID)
- {
- connectivity_state_set (exec_ctx, transport_global, GRPC_CHANNEL_TRANSIENT_FAILURE, "no_more_stream_ids");
- }
-
- stream_global->outgoing_window = transport_global->settings[GRPC_PEER_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
- stream_global->incoming_window = transport_global->settings[GRPC_SENT_SETTINGS][GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
- stream_global->max_recv_bytes = GPR_MAX (stream_global->incoming_window, stream_global->max_recv_bytes);
- grpc_chttp2_stream_map_add (&TRANSPORT_FROM_GLOBAL (transport_global)->new_stream_map, stream_global->id, STREAM_FROM_GLOBAL (stream_global));
- stream_global->in_stream_map = 1;
- transport_global->concurrent_stream_count++;
- grpc_chttp2_list_add_incoming_window_updated (transport_global, stream_global);
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
+ while (transport_global->next_stream_id <= MAX_CLIENT_STREAM_ID &&
+ transport_global->concurrent_stream_count <
+ transport_global
+ ->settings[GRPC_PEER_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS] &&
+ grpc_chttp2_list_pop_waiting_for_concurrency(transport_global,
+ &stream_global)) {
+ GRPC_CHTTP2_IF_TRACING(gpr_log(
+ GPR_DEBUG, "HTTP:%s: Allocating new grpc_chttp2_stream %p to id %d",
+ transport_global->is_client ? "CLI" : "SVR", stream_global,
+ transport_global->next_stream_id));
+
+ GPR_ASSERT(stream_global->id == 0);
+ stream_global->id = transport_global->next_stream_id;
+ transport_global->next_stream_id += 2;
+
+ if (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID) {
+ connectivity_state_set(exec_ctx, transport_global,
+ GRPC_CHANNEL_TRANSIENT_FAILURE,
+ "no_more_stream_ids");
}
+
+ stream_global->outgoing_window =
+ transport_global->settings[GRPC_PEER_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
+ stream_global->incoming_window =
+ transport_global->settings[GRPC_SENT_SETTINGS]
+ [GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE];
+ stream_global->max_recv_bytes =
+ GPR_MAX(stream_global->incoming_window, stream_global->max_recv_bytes);
+ grpc_chttp2_stream_map_add(
+ &TRANSPORT_FROM_GLOBAL(transport_global)->new_stream_map,
+ stream_global->id, STREAM_FROM_GLOBAL(stream_global));
+ stream_global->in_stream_map = 1;
+ transport_global->concurrent_stream_count++;
+ grpc_chttp2_list_add_incoming_window_updated(transport_global,
+ stream_global);
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
+ }
/* cancel out streams that will never be started */
- while (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID && grpc_chttp2_list_pop_waiting_for_concurrency (transport_global, &stream_global))
- {
- cancel_from_api (transport_global, stream_global, GRPC_STATUS_UNAVAILABLE);
- }
+ while (transport_global->next_stream_id >= MAX_CLIENT_STREAM_ID &&
+ grpc_chttp2_list_pop_waiting_for_concurrency(transport_global,
+ &stream_global)) {
+ cancel_from_api(transport_global, stream_global, GRPC_STATUS_UNAVAILABLE);
+ }
}
-static void
-perform_stream_op_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global, grpc_transport_stream_op * op)
-{
- if (op->cancel_with_status != GRPC_STATUS_OK)
- {
- cancel_from_api (transport_global, stream_global, op->cancel_with_status);
- }
-
- if (op->close_with_status != GRPC_STATUS_OK)
- {
- close_from_api (transport_global, stream_global, op->close_with_status, op->optional_close_message);
+static void perform_stream_op_locked(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global, grpc_transport_stream_op *op) {
+ if (op->cancel_with_status != GRPC_STATUS_OK) {
+ cancel_from_api(transport_global, stream_global, op->cancel_with_status);
+ }
+
+ if (op->close_with_status != GRPC_STATUS_OK) {
+ close_from_api(transport_global, stream_global, op->close_with_status,
+ op->optional_close_message);
+ }
+
+ if (op->send_ops) {
+ GPR_ASSERT(stream_global->outgoing_sopb == NULL);
+ stream_global->send_done_closure = op->on_done_send;
+ if (!stream_global->cancelled) {
+ stream_global->written_anything = 1;
+ stream_global->outgoing_sopb = op->send_ops;
+ if (op->is_last_send &&
+ stream_global->write_state == GRPC_WRITE_STATE_OPEN) {
+ stream_global->write_state = GRPC_WRITE_STATE_QUEUED_CLOSE;
+ }
+ if (stream_global->id == 0) {
+ GRPC_CHTTP2_IF_TRACING(gpr_log(
+ GPR_DEBUG,
+ "HTTP:%s: New grpc_chttp2_stream %p waiting for concurrency",
+ transport_global->is_client ? "CLI" : "SVR", stream_global));
+ grpc_chttp2_list_add_waiting_for_concurrency(transport_global,
+ stream_global);
+ maybe_start_some_streams(exec_ctx, transport_global);
+ } else if (stream_global->outgoing_window > 0) {
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
+ }
+ } else {
+ grpc_sopb_reset(op->send_ops);
+ grpc_exec_ctx_enqueue(exec_ctx, stream_global->send_done_closure, 0);
}
-
- if (op->send_ops)
- {
- GPR_ASSERT (stream_global->outgoing_sopb == NULL);
- stream_global->send_done_closure = op->on_done_send;
- if (!stream_global->cancelled)
- {
- stream_global->written_anything = 1;
- stream_global->outgoing_sopb = op->send_ops;
- if (op->is_last_send && stream_global->write_state == GRPC_WRITE_STATE_OPEN)
- {
- stream_global->write_state = GRPC_WRITE_STATE_QUEUED_CLOSE;
- }
- if (stream_global->id == 0)
- {
- GRPC_CHTTP2_IF_TRACING (gpr_log (GPR_DEBUG, "HTTP:%s: New grpc_chttp2_stream %p waiting for concurrency", transport_global->is_client ? "CLI" : "SVR", stream_global));
- grpc_chttp2_list_add_waiting_for_concurrency (transport_global, stream_global);
- maybe_start_some_streams (exec_ctx, transport_global);
- }
- else if (stream_global->outgoing_window > 0)
- {
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
- }
- }
- else
- {
- grpc_sopb_reset (op->send_ops);
- grpc_exec_ctx_enqueue (exec_ctx, stream_global->send_done_closure, 0);
- }
+ }
+
+ if (op->recv_ops) {
+ GPR_ASSERT(stream_global->publish_sopb == NULL);
+ GPR_ASSERT(stream_global->published_state != GRPC_STREAM_CLOSED);
+ stream_global->recv_done_closure = op->on_done_recv;
+ stream_global->publish_sopb = op->recv_ops;
+ stream_global->publish_sopb->nops = 0;
+ stream_global->publish_state = op->recv_state;
+ /* clamp max recv bytes */
+ op->max_recv_bytes = GPR_MIN(op->max_recv_bytes, GPR_UINT32_MAX);
+ if (stream_global->max_recv_bytes < op->max_recv_bytes) {
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "op", transport_global, stream_global, max_recv_bytes,
+ op->max_recv_bytes - stream_global->max_recv_bytes);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(
+ "op", transport_global, stream_global, unannounced_incoming_window,
+ op->max_recv_bytes - stream_global->max_recv_bytes);
+ stream_global->unannounced_incoming_window +=
+ (gpr_uint32)op->max_recv_bytes - stream_global->max_recv_bytes;
+ stream_global->max_recv_bytes = (gpr_uint32)op->max_recv_bytes;
}
-
- if (op->recv_ops)
- {
- GPR_ASSERT (stream_global->publish_sopb == NULL);
- GPR_ASSERT (stream_global->published_state != GRPC_STREAM_CLOSED);
- stream_global->recv_done_closure = op->on_done_recv;
- stream_global->publish_sopb = op->recv_ops;
- stream_global->publish_sopb->nops = 0;
- stream_global->publish_state = op->recv_state;
- /* clamp max recv bytes */
- op->max_recv_bytes = GPR_MIN (op->max_recv_bytes, GPR_UINT32_MAX);
- if (stream_global->max_recv_bytes < op->max_recv_bytes)
- {
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("op", transport_global, stream_global, max_recv_bytes, op->max_recv_bytes - stream_global->max_recv_bytes);
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("op", transport_global, stream_global, unannounced_incoming_window, op->max_recv_bytes - stream_global->max_recv_bytes);
- stream_global->unannounced_incoming_window += (gpr_uint32) op->max_recv_bytes - stream_global->max_recv_bytes;
- stream_global->max_recv_bytes = (gpr_uint32) op->max_recv_bytes;
- }
- grpc_chttp2_incoming_metadata_live_op_buffer_end (&stream_global->outstanding_metadata);
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
- if (stream_global->id != 0)
- {
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
- }
+ grpc_chttp2_incoming_metadata_live_op_buffer_end(
+ &stream_global->outstanding_metadata);
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
+ if (stream_global->id != 0) {
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
}
+ }
- if (op->bind_pollset)
- {
- add_to_pollset_locked (exec_ctx,TRANSPORT_FROM_GLOBAL ( transport_global), op->bind_pollset);
- }
+ if (op->bind_pollset) {
+ add_to_pollset_locked(exec_ctx, TRANSPORT_FROM_GLOBAL(transport_global),
+ op->bind_pollset);
+ }
- grpc_exec_ctx_enqueue (exec_ctx, op->on_consumed, 1);
+ grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, 1);
}
-static void
-perform_stream_op (grpc_exec_ctx * exec_ctx, grpc_transport * gt, grpc_stream * gs, grpc_transport_stream_op * op)
-{
- grpc_chttp2_transport *t = (grpc_chttp2_transport *) gt;
- grpc_chttp2_stream *s = (grpc_chttp2_stream *) gs;
+static void perform_stream_op(grpc_exec_ctx *exec_ctx, grpc_transport *gt,
+ grpc_stream *gs, grpc_transport_stream_op *op) {
+ grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt;
+ grpc_chttp2_stream *s = (grpc_chttp2_stream *)gs;
- lock (t);
- perform_stream_op_locked (exec_ctx, &t->global, &s->global, op);
- unlock (exec_ctx, t);
+ lock(t);
+ perform_stream_op_locked(exec_ctx, &t->global, &s->global, op);
+ unlock(exec_ctx, t);
}
-static void
-send_ping_locked (grpc_chttp2_transport * t, grpc_closure * on_recv)
-{
- grpc_chttp2_outstanding_ping *p = gpr_malloc (sizeof (*p));
+static void send_ping_locked(grpc_chttp2_transport *t, grpc_closure *on_recv) {
+ grpc_chttp2_outstanding_ping *p = gpr_malloc(sizeof(*p));
p->next = &t->global.pings;
p->prev = p->next->prev;
p->prev->next = p->next->prev = p;
- p->id[0] = (gpr_uint8) ((t->global.ping_counter >> 56) &0xff);
- p->id[1] = (gpr_uint8) ((t->global.ping_counter >> 48) &0xff);
- p->id[2] = (gpr_uint8) ((t->global.ping_counter >> 40) &0xff);
- p->id[3] = (gpr_uint8) ((t->global.ping_counter >> 32) &0xff);
- p->id[4] = (gpr_uint8) ((t->global.ping_counter >> 24) &0xff);
- p->id[5] = (gpr_uint8) ((t->global.ping_counter >> 16) &0xff);
- p->id[6] = (gpr_uint8) ((t->global.ping_counter >> 8) &0xff);
- p->id[7] = (gpr_uint8) (t->global.ping_counter & 0xff);
+ p->id[0] = (gpr_uint8)((t->global.ping_counter >> 56) & 0xff);
+ p->id[1] = (gpr_uint8)((t->global.ping_counter >> 48) & 0xff);
+ p->id[2] = (gpr_uint8)((t->global.ping_counter >> 40) & 0xff);
+ p->id[3] = (gpr_uint8)((t->global.ping_counter >> 32) & 0xff);
+ p->id[4] = (gpr_uint8)((t->global.ping_counter >> 24) & 0xff);
+ p->id[5] = (gpr_uint8)((t->global.ping_counter >> 16) & 0xff);
+ p->id[6] = (gpr_uint8)((t->global.ping_counter >> 8) & 0xff);
+ p->id[7] = (gpr_uint8)(t->global.ping_counter & 0xff);
p->on_recv = on_recv;
- gpr_slice_buffer_add (&t->global.qbuf, grpc_chttp2_ping_create (0, p->id));
+ gpr_slice_buffer_add(&t->global.qbuf, grpc_chttp2_ping_create(0, p->id));
}
-static void
-perform_transport_op (grpc_exec_ctx * exec_ctx, grpc_transport * gt, grpc_transport_op * op)
-{
- grpc_chttp2_transport *t = (grpc_chttp2_transport *) gt;
+static void perform_transport_op(grpc_exec_ctx *exec_ctx, grpc_transport *gt,
+ grpc_transport_op *op) {
+ grpc_chttp2_transport *t = (grpc_chttp2_transport *)gt;
int close_transport = 0;
- lock (t);
-
- grpc_exec_ctx_enqueue (exec_ctx, op->on_consumed, 1);
-
- if (op->on_connectivity_state_change)
- {
- grpc_connectivity_state_notify_on_state_change (exec_ctx, &t->channel_callback.state_tracker, op->connectivity_state, op->on_connectivity_state_change);
- }
-
- if (op->send_goaway)
- {
- t->global.sent_goaway = 1;
- grpc_chttp2_goaway_append (t->global.last_incoming_stream_id, (gpr_uint32) grpc_chttp2_grpc_status_to_http2_error (op->goaway_status), gpr_slice_ref (*op->goaway_message), &t->global.qbuf);
- close_transport = !grpc_chttp2_has_streams (t);
- }
-
- if (op->set_accept_stream != NULL)
- {
- t->channel_callback.accept_stream = op->set_accept_stream;
- t->channel_callback.accept_stream_user_data = op->set_accept_stream_user_data;
- }
-
- if (op->bind_pollset)
- {
- add_to_pollset_locked (exec_ctx, t, op->bind_pollset);
- }
-
- if (op->bind_pollset_set)
- {
- add_to_pollset_set_locked (exec_ctx, t, op->bind_pollset_set);
- }
-
- if (op->send_ping)
- {
- send_ping_locked (t, op->send_ping);
- }
-
- if (op->disconnect)
- {
- close_transport_locked (exec_ctx, t);
- }
-
- unlock (exec_ctx, t);
-
- if (close_transport)
- {
- lock (t);
- close_transport_locked (exec_ctx, t);
- unlock (exec_ctx, t);
- }
+ lock(t);
+
+ grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, 1);
+
+ if (op->on_connectivity_state_change) {
+ grpc_connectivity_state_notify_on_state_change(
+ exec_ctx, &t->channel_callback.state_tracker, op->connectivity_state,
+ op->on_connectivity_state_change);
+ }
+
+ if (op->send_goaway) {
+ t->global.sent_goaway = 1;
+ grpc_chttp2_goaway_append(
+ t->global.last_incoming_stream_id,
+ (gpr_uint32)grpc_chttp2_grpc_status_to_http2_error(op->goaway_status),
+ gpr_slice_ref(*op->goaway_message), &t->global.qbuf);
+ close_transport = !grpc_chttp2_has_streams(t);
+ }
+
+ if (op->set_accept_stream != NULL) {
+ t->channel_callback.accept_stream = op->set_accept_stream;
+ t->channel_callback.accept_stream_user_data =
+ op->set_accept_stream_user_data;
+ }
+
+ if (op->bind_pollset) {
+ add_to_pollset_locked(exec_ctx, t, op->bind_pollset);
+ }
+
+ if (op->bind_pollset_set) {
+ add_to_pollset_set_locked(exec_ctx, t, op->bind_pollset_set);
+ }
+
+ if (op->send_ping) {
+ send_ping_locked(t, op->send_ping);
+ }
+
+ if (op->disconnect) {
+ close_transport_locked(exec_ctx, t);
+ }
+
+ unlock(exec_ctx, t);
+
+ if (close_transport) {
+ lock(t);
+ close_transport_locked(exec_ctx, t);
+ unlock(exec_ctx, t);
+ }
}
/*
* INPUT PROCESSING
*/
-static grpc_stream_state
-compute_state (gpr_uint8 write_closed, gpr_uint8 read_closed)
-{
- if (write_closed && read_closed)
- return GRPC_STREAM_CLOSED;
- if (write_closed)
- return GRPC_STREAM_SEND_CLOSED;
- if (read_closed)
- return GRPC_STREAM_RECV_CLOSED;
+static grpc_stream_state compute_state(gpr_uint8 write_closed,
+ gpr_uint8 read_closed) {
+ if (write_closed && read_closed) return GRPC_STREAM_CLOSED;
+ if (write_closed) return GRPC_STREAM_SEND_CLOSED;
+ if (read_closed) return GRPC_STREAM_RECV_CLOSED;
return GRPC_STREAM_OPEN;
}
-static void
-remove_stream (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, gpr_uint32 id)
-{
+static void remove_stream(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
+ gpr_uint32 id) {
size_t new_stream_count;
- grpc_chttp2_stream *s = grpc_chttp2_stream_map_delete (&t->parsing_stream_map, id);
- if (!s)
- {
- s = grpc_chttp2_stream_map_delete (&t->new_stream_map, id);
- }
- grpc_chttp2_list_remove_writable_stream (&t->global, &s->global);
- GPR_ASSERT (s);
+ grpc_chttp2_stream *s =
+ grpc_chttp2_stream_map_delete(&t->parsing_stream_map, id);
+ if (!s) {
+ s = grpc_chttp2_stream_map_delete(&t->new_stream_map, id);
+ }
+ grpc_chttp2_list_remove_writable_stream(&t->global, &s->global);
+ GPR_ASSERT(s);
s->global.in_stream_map = 0;
- if (t->parsing.incoming_stream == &s->parsing)
- {
- t->parsing.incoming_stream = NULL;
- grpc_chttp2_parsing_become_skip_parser (&t->parsing);
- }
- if (grpc_chttp2_unregister_stream (t, s) && t->global.sent_goaway)
- {
- close_transport_locked (exec_ctx, t);
- }
-
- new_stream_count = grpc_chttp2_stream_map_size (&t->parsing_stream_map) + grpc_chttp2_stream_map_size (&t->new_stream_map);
- GPR_ASSERT (new_stream_count <= GPR_UINT32_MAX);
- if (new_stream_count != t->global.concurrent_stream_count)
- {
- t->global.concurrent_stream_count = (gpr_uint32) new_stream_count;
- maybe_start_some_streams (exec_ctx, &t->global);
- }
+ if (t->parsing.incoming_stream == &s->parsing) {
+ t->parsing.incoming_stream = NULL;
+ grpc_chttp2_parsing_become_skip_parser(&t->parsing);
+ }
+ if (grpc_chttp2_unregister_stream(t, s) && t->global.sent_goaway) {
+ close_transport_locked(exec_ctx, t);
+ }
+
+ new_stream_count = grpc_chttp2_stream_map_size(&t->parsing_stream_map) +
+ grpc_chttp2_stream_map_size(&t->new_stream_map);
+ GPR_ASSERT(new_stream_count <= GPR_UINT32_MAX);
+ if (new_stream_count != t->global.concurrent_stream_count) {
+ t->global.concurrent_stream_count = (gpr_uint32)new_stream_count;
+ maybe_start_some_streams(exec_ctx, &t->global);
+ }
}
-static void
-unlock_check_read_write_state (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
+static void unlock_check_read_write_state(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
grpc_chttp2_transport_global *transport_global = &t->global;
grpc_chttp2_stream_global *stream_global;
grpc_stream_state state;
- if (!t->parsing_active)
- {
- /* if a stream is in the stream map, and gets cancelled, we need to ensure
- we are not parsing before continuing the cancellation to keep things in
- a sane state */
- while (grpc_chttp2_list_pop_closed_waiting_for_parsing (transport_global, &stream_global))
- {
- GPR_ASSERT (stream_global->in_stream_map);
- GPR_ASSERT (stream_global->write_state != GRPC_WRITE_STATE_OPEN);
- GPR_ASSERT (stream_global->read_closed);
- remove_stream (exec_ctx, t, stream_global->id);
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
- }
+ if (!t->parsing_active) {
+ /* if a stream is in the stream map, and gets cancelled, we need to ensure
+ we are not parsing before continuing the cancellation to keep things in
+ a sane state */
+ while (grpc_chttp2_list_pop_closed_waiting_for_parsing(transport_global,
+ &stream_global)) {
+ GPR_ASSERT(stream_global->in_stream_map);
+ GPR_ASSERT(stream_global->write_state != GRPC_WRITE_STATE_OPEN);
+ GPR_ASSERT(stream_global->read_closed);
+ remove_stream(exec_ctx, t, stream_global->id);
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
}
+ }
- if (!t->writing_active)
- {
- while (grpc_chttp2_list_pop_cancelled_waiting_for_writing (transport_global, &stream_global))
- {
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
- }
+ if (!t->writing_active) {
+ while (grpc_chttp2_list_pop_cancelled_waiting_for_writing(transport_global,
+ &stream_global)) {
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
}
-
- while (grpc_chttp2_list_pop_read_write_state_changed (transport_global, &stream_global))
- {
- if (stream_global->cancelled)
- {
- if (t->writing_active && stream_global->write_state != GRPC_WRITE_STATE_SENT_CLOSE)
- {
- grpc_chttp2_list_add_cancelled_waiting_for_writing (transport_global, stream_global);
- }
- else
- {
- stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE;
- if (stream_global->outgoing_sopb != NULL)
- {
- grpc_sopb_reset (stream_global->outgoing_sopb);
- stream_global->outgoing_sopb = NULL;
- grpc_exec_ctx_enqueue (exec_ctx, stream_global->send_done_closure, 1);
- }
- stream_global->read_closed = 1;
- if (!stream_global->published_cancelled)
- {
- char buffer[GPR_LTOA_MIN_BUFSIZE];
- gpr_ltoa (stream_global->cancelled_status, buffer);
- grpc_chttp2_incoming_metadata_buffer_add (&stream_global->incoming_metadata, grpc_mdelem_from_strings (t->metadata_context, "grpc-status", buffer));
- grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into (&stream_global->incoming_metadata, &stream_global->incoming_sopb);
- stream_global->published_cancelled = 1;
- }
- }
- }
- if (stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE && stream_global->read_closed && stream_global->in_stream_map)
- {
- if (t->parsing_active)
- {
- grpc_chttp2_list_add_closed_waiting_for_parsing (transport_global, stream_global);
- }
- else
- {
- remove_stream (exec_ctx, t, stream_global->id);
- }
- }
- if (!stream_global->publish_sopb)
- {
- continue;
- }
- if (stream_global->writing_now != 0)
- {
- continue;
- }
- /* FIXME(ctiller): we include in_stream_map in our computation of
- whether the stream is write-closed. This is completely bogus,
- but has the effect of delaying stream-closed until the stream
- is indeed evicted from the stream map, making it safe to delete.
- To fix this will require having an edge after stream-closed
- indicating that the stream is closed AND safe to delete. */
- state = compute_state (stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE && !stream_global->in_stream_map, stream_global->read_closed);
- if (stream_global->incoming_sopb.nops == 0 && state == stream_global->published_state)
- {
- continue;
- }
- grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op (&stream_global->incoming_metadata, &stream_global->incoming_sopb, &stream_global->outstanding_metadata);
- grpc_sopb_swap (stream_global->publish_sopb, &stream_global->incoming_sopb);
- stream_global->published_state = *stream_global->publish_state = state;
- grpc_exec_ctx_enqueue (exec_ctx, stream_global->recv_done_closure, 1);
- stream_global->recv_done_closure = NULL;
- stream_global->publish_sopb = NULL;
- stream_global->publish_state = NULL;
+ }
+
+ while (grpc_chttp2_list_pop_read_write_state_changed(transport_global,
+ &stream_global)) {
+ if (stream_global->cancelled) {
+ if (t->writing_active &&
+ stream_global->write_state != GRPC_WRITE_STATE_SENT_CLOSE) {
+ grpc_chttp2_list_add_cancelled_waiting_for_writing(transport_global,
+ stream_global);
+ } else {
+ stream_global->write_state = GRPC_WRITE_STATE_SENT_CLOSE;
+ if (stream_global->outgoing_sopb != NULL) {
+ grpc_sopb_reset(stream_global->outgoing_sopb);
+ stream_global->outgoing_sopb = NULL;
+ grpc_exec_ctx_enqueue(exec_ctx, stream_global->send_done_closure, 1);
+ }
+ stream_global->read_closed = 1;
+ if (!stream_global->published_cancelled) {
+ char buffer[GPR_LTOA_MIN_BUFSIZE];
+ gpr_ltoa(stream_global->cancelled_status, buffer);
+ grpc_chttp2_incoming_metadata_buffer_add(
+ &stream_global->incoming_metadata,
+ grpc_mdelem_from_strings(t->metadata_context, "grpc-status",
+ buffer));
+ grpc_chttp2_incoming_metadata_buffer_place_metadata_batch_into(
+ &stream_global->incoming_metadata, &stream_global->incoming_sopb);
+ stream_global->published_cancelled = 1;
+ }
+ }
+ }
+ if (stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE &&
+ stream_global->read_closed && stream_global->in_stream_map) {
+ if (t->parsing_active) {
+ grpc_chttp2_list_add_closed_waiting_for_parsing(transport_global,
+ stream_global);
+ } else {
+ remove_stream(exec_ctx, t, stream_global->id);
+ }
+ }
+ if (!stream_global->publish_sopb) {
+ continue;
}
+ if (stream_global->writing_now != 0) {
+ continue;
+ }
+ /* FIXME(ctiller): we include in_stream_map in our computation of
+ whether the stream is write-closed. This is completely bogus,
+ but has the effect of delaying stream-closed until the stream
+ is indeed evicted from the stream map, making it safe to delete.
+ To fix this will require having an edge after stream-closed
+ indicating that the stream is closed AND safe to delete. */
+ state = compute_state(
+ stream_global->write_state == GRPC_WRITE_STATE_SENT_CLOSE &&
+ !stream_global->in_stream_map,
+ stream_global->read_closed);
+ if (stream_global->incoming_sopb.nops == 0 &&
+ state == stream_global->published_state) {
+ continue;
+ }
+ grpc_chttp2_incoming_metadata_buffer_postprocess_sopb_and_begin_live_op(
+ &stream_global->incoming_metadata, &stream_global->incoming_sopb,
+ &stream_global->outstanding_metadata);
+ grpc_sopb_swap(stream_global->publish_sopb, &stream_global->incoming_sopb);
+ stream_global->published_state = *stream_global->publish_state = state;
+ grpc_exec_ctx_enqueue(exec_ctx, stream_global->recv_done_closure, 1);
+ stream_global->recv_done_closure = NULL;
+ stream_global->publish_sopb = NULL;
+ stream_global->publish_state = NULL;
+ }
}
-static void
-cancel_from_api (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global, grpc_status_code status)
-{
+static void cancel_from_api(grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global,
+ grpc_status_code status) {
stream_global->cancelled = 1;
stream_global->cancelled_status = status;
- if (stream_global->id != 0)
- {
- gpr_slice_buffer_add (&transport_global->qbuf, grpc_chttp2_rst_stream_create (stream_global->id, (gpr_uint32) grpc_chttp2_grpc_status_to_http2_error (status)));
- }
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
+ if (stream_global->id != 0) {
+ gpr_slice_buffer_add(
+ &transport_global->qbuf,
+ grpc_chttp2_rst_stream_create(
+ stream_global->id,
+ (gpr_uint32)grpc_chttp2_grpc_status_to_http2_error(status)));
+ }
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
}
-static void
-close_from_api (grpc_chttp2_transport_global * transport_global, grpc_chttp2_stream_global * stream_global, grpc_status_code status, gpr_slice * optional_message)
-{
+static void close_from_api(grpc_chttp2_transport_global *transport_global,
+ grpc_chttp2_stream_global *stream_global,
+ grpc_status_code status,
+ gpr_slice *optional_message) {
gpr_slice hdr;
gpr_slice status_hdr;
gpr_slice message_pfx;
gpr_uint8 *p;
gpr_uint32 len = 0;
- GPR_ASSERT (status >= 0 && (int) status < 100);
+ GPR_ASSERT(status >= 0 && (int)status < 100);
stream_global->cancelled = 1;
stream_global->cancelled_status = status;
- GPR_ASSERT (stream_global->id != 0);
- GPR_ASSERT (!stream_global->written_anything);
+ GPR_ASSERT(stream_global->id != 0);
+ GPR_ASSERT(!stream_global->written_anything);
/* Hand roll a header block.
This is unnecessarily ugly - at some point we should find a more elegant
@@ -977,10 +974,10 @@ close_from_api (grpc_chttp2_transport_global * transport_global, grpc_chttp2_str
It's complicated by the fact that our send machinery would be dead by the
time we got around to sending this, so instead we ignore HPACK compression
and just write the uncompressed bytes onto the wire. */
- status_hdr = gpr_slice_malloc (15 + (status >= 10));
- p = GPR_SLICE_START_PTR (status_hdr);
- *p++ = 0x40; /* literal header */
- *p++ = 11; /* len(grpc-status) */
+ status_hdr = gpr_slice_malloc(15 + (status >= 10));
+ p = GPR_SLICE_START_PTR(status_hdr);
+ *p++ = 0x40; /* literal header */
+ *p++ = 11; /* len(grpc-status) */
*p++ = 'g';
*p++ = 'r';
*p++ = 'p';
@@ -992,94 +989,87 @@ close_from_api (grpc_chttp2_transport_global * transport_global, grpc_chttp2_str
*p++ = 't';
*p++ = 'u';
*p++ = 's';
- if (status < 10)
- {
- *p++ = 1;
- *p++ = (gpr_uint8) ('0' + status);
- }
- else
- {
- *p++ = 2;
- *p++ = (gpr_uint8) ('0' + (status / 10));
- *p++ = (gpr_uint8) ('0' + (status % 10));
- }
- GPR_ASSERT (p == GPR_SLICE_END_PTR (status_hdr));
- len += (gpr_uint32) GPR_SLICE_LENGTH (status_hdr);
-
- if (optional_message)
- {
- GPR_ASSERT (GPR_SLICE_LENGTH (*optional_message) < 127);
- message_pfx = gpr_slice_malloc (15);
- p = GPR_SLICE_START_PTR (message_pfx);
- *p++ = 0x40;
- *p++ = 12; /* len(grpc-message) */
- *p++ = 'g';
- *p++ = 'r';
- *p++ = 'p';
- *p++ = 'c';
- *p++ = '-';
- *p++ = 'm';
- *p++ = 'e';
- *p++ = 's';
- *p++ = 's';
- *p++ = 'a';
- *p++ = 'g';
- *p++ = 'e';
- *p++ = (gpr_uint8) GPR_SLICE_LENGTH (*optional_message);
- GPR_ASSERT (p == GPR_SLICE_END_PTR (message_pfx));
- len += (gpr_uint32) GPR_SLICE_LENGTH (message_pfx);
- len += (gpr_uint32) GPR_SLICE_LENGTH (*optional_message);
- }
-
- hdr = gpr_slice_malloc (9);
- p = GPR_SLICE_START_PTR (hdr);
- *p++ = (gpr_uint8) (len >> 16);
- *p++ = (gpr_uint8) (len >> 8);
- *p++ = (gpr_uint8) (len);
+ if (status < 10) {
+ *p++ = 1;
+ *p++ = (gpr_uint8)('0' + status);
+ } else {
+ *p++ = 2;
+ *p++ = (gpr_uint8)('0' + (status / 10));
+ *p++ = (gpr_uint8)('0' + (status % 10));
+ }
+ GPR_ASSERT(p == GPR_SLICE_END_PTR(status_hdr));
+ len += (gpr_uint32)GPR_SLICE_LENGTH(status_hdr);
+
+ if (optional_message) {
+ GPR_ASSERT(GPR_SLICE_LENGTH(*optional_message) < 127);
+ message_pfx = gpr_slice_malloc(15);
+ p = GPR_SLICE_START_PTR(message_pfx);
+ *p++ = 0x40;
+ *p++ = 12; /* len(grpc-message) */
+ *p++ = 'g';
+ *p++ = 'r';
+ *p++ = 'p';
+ *p++ = 'c';
+ *p++ = '-';
+ *p++ = 'm';
+ *p++ = 'e';
+ *p++ = 's';
+ *p++ = 's';
+ *p++ = 'a';
+ *p++ = 'g';
+ *p++ = 'e';
+ *p++ = (gpr_uint8)GPR_SLICE_LENGTH(*optional_message);
+ GPR_ASSERT(p == GPR_SLICE_END_PTR(message_pfx));
+ len += (gpr_uint32)GPR_SLICE_LENGTH(message_pfx);
+ len += (gpr_uint32)GPR_SLICE_LENGTH(*optional_message);
+ }
+
+ hdr = gpr_slice_malloc(9);
+ p = GPR_SLICE_START_PTR(hdr);
+ *p++ = (gpr_uint8)(len >> 16);
+ *p++ = (gpr_uint8)(len >> 8);
+ *p++ = (gpr_uint8)(len);
*p++ = GRPC_CHTTP2_FRAME_HEADER;
*p++ = GRPC_CHTTP2_DATA_FLAG_END_STREAM | GRPC_CHTTP2_DATA_FLAG_END_HEADERS;
- *p++ = (gpr_uint8) (stream_global->id >> 24);
- *p++ = (gpr_uint8) (stream_global->id >> 16);
- *p++ = (gpr_uint8) (stream_global->id >> 8);
- *p++ = (gpr_uint8) (stream_global->id);
- GPR_ASSERT (p == GPR_SLICE_END_PTR (hdr));
-
- gpr_slice_buffer_add (&transport_global->qbuf, hdr);
- gpr_slice_buffer_add (&transport_global->qbuf, status_hdr);
- if (optional_message)
- {
- gpr_slice_buffer_add (&transport_global->qbuf, message_pfx);
- gpr_slice_buffer_add (&transport_global->qbuf, gpr_slice_ref (*optional_message));
- }
-
- gpr_slice_buffer_add (&transport_global->qbuf, grpc_chttp2_rst_stream_create (stream_global->id, GRPC_CHTTP2_NO_ERROR));
-
- grpc_chttp2_list_add_read_write_state_changed (transport_global, stream_global);
+ *p++ = (gpr_uint8)(stream_global->id >> 24);
+ *p++ = (gpr_uint8)(stream_global->id >> 16);
+ *p++ = (gpr_uint8)(stream_global->id >> 8);
+ *p++ = (gpr_uint8)(stream_global->id);
+ GPR_ASSERT(p == GPR_SLICE_END_PTR(hdr));
+
+ gpr_slice_buffer_add(&transport_global->qbuf, hdr);
+ gpr_slice_buffer_add(&transport_global->qbuf, status_hdr);
+ if (optional_message) {
+ gpr_slice_buffer_add(&transport_global->qbuf, message_pfx);
+ gpr_slice_buffer_add(&transport_global->qbuf,
+ gpr_slice_ref(*optional_message));
+ }
+
+ gpr_slice_buffer_add(
+ &transport_global->qbuf,
+ grpc_chttp2_rst_stream_create(stream_global->id, GRPC_CHTTP2_NO_ERROR));
+
+ grpc_chttp2_list_add_read_write_state_changed(transport_global,
+ stream_global);
}
-static void
-cancel_stream_cb (grpc_chttp2_transport_global * transport_global, void *user_data, grpc_chttp2_stream_global * stream_global)
-{
- cancel_from_api (transport_global, stream_global, GRPC_STATUS_UNAVAILABLE);
+static void cancel_stream_cb(grpc_chttp2_transport_global *transport_global,
+ void *user_data,
+ grpc_chttp2_stream_global *stream_global) {
+ cancel_from_api(transport_global, stream_global, GRPC_STATUS_UNAVAILABLE);
}
-static void
-end_all_the_calls (grpc_chttp2_transport * t)
-{
- grpc_chttp2_for_all_streams (&t->global, NULL, cancel_stream_cb);
+static void end_all_the_calls(grpc_chttp2_transport *t) {
+ grpc_chttp2_for_all_streams(&t->global, NULL, cancel_stream_cb);
}
-static void
-drop_connection (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
- close_transport_locked (exec_ctx, t);
- end_all_the_calls (t);
+static void drop_connection(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t) {
+ close_transport_locked(exec_ctx, t);
+ end_all_the_calls(t);
}
/** update window from a settings change */
-static void
-update_global_window (void *args, gpr_uint32 id, void *stream)
-{
+static void update_global_window(void *args, gpr_uint32 id, void *stream) {
grpc_chttp2_transport *t = args;
grpc_chttp2_stream *s = stream;
grpc_chttp2_transport_global *transport_global = &t->global;
@@ -1087,184 +1077,179 @@ update_global_window (void *args, gpr_uint32 id, void *stream)
int was_zero;
int is_zero;
- GRPC_CHTTP2_FLOWCTL_TRACE_STREAM ("settings", transport_global, stream_global, outgoing_window, t->parsing.initial_window_update);
+ GRPC_CHTTP2_FLOWCTL_TRACE_STREAM("settings", transport_global, stream_global,
+ outgoing_window,
+ t->parsing.initial_window_update);
was_zero = stream_global->outgoing_window <= 0;
stream_global->outgoing_window += t->parsing.initial_window_update;
is_zero = stream_global->outgoing_window <= 0;
- if (was_zero && !is_zero)
- {
- grpc_chttp2_list_add_writable_stream (transport_global, stream_global);
- }
+ if (was_zero && !is_zero) {
+ grpc_chttp2_list_add_writable_stream(transport_global, stream_global);
+ }
}
-static void
-read_error_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t)
-{
+static void read_error_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t) {
t->endpoint_reading = 0;
- if (!t->writing_active && t->ep)
- {
- destroy_endpoint (exec_ctx, t);
- }
+ if (!t->writing_active && t->ep) {
+ destroy_endpoint(exec_ctx, t);
+ }
}
/* tcp read callback */
-static void
-recv_data (grpc_exec_ctx * exec_ctx, void *tp, int success)
-{
+static void recv_data(grpc_exec_ctx *exec_ctx, void *tp, int success) {
size_t i;
int keep_reading = 0;
grpc_chttp2_transport *t = tp;
- lock (t);
+ lock(t);
i = 0;
- GPR_ASSERT (!t->parsing_active);
- if (!t->closed)
- {
- t->parsing_active = 1;
- /* merge stream lists */
- grpc_chttp2_stream_map_move_into (&t->new_stream_map, &t->parsing_stream_map);
- grpc_chttp2_prepare_to_read (&t->global, &t->parsing);
- gpr_mu_unlock (&t->mu);
- for (; i < t->read_buffer.count && grpc_chttp2_perform_read (exec_ctx, &t->parsing, t->read_buffer.slices[i]); i++)
- ;
- gpr_mu_lock (&t->mu);
- if (i != t->read_buffer.count)
- {
- drop_connection (exec_ctx, t);
- }
- /* merge stream lists */
- grpc_chttp2_stream_map_move_into (&t->new_stream_map, &t->parsing_stream_map);
- t->global.concurrent_stream_count = (gpr_uint32) grpc_chttp2_stream_map_size (&t->parsing_stream_map);
- if (t->parsing.initial_window_update != 0)
- {
- grpc_chttp2_stream_map_for_each (&t->parsing_stream_map, update_global_window, t);
- t->parsing.initial_window_update = 0;
- }
- /* handle higher level things */
- grpc_chttp2_publish_reads (exec_ctx, &t->global, &t->parsing);
- t->parsing_active = 0;
- }
- if (!success || i != t->read_buffer.count)
- {
- drop_connection (exec_ctx, t);
- read_error_locked (exec_ctx, t);
- }
- else if (!t->closed)
- {
- keep_reading = 1;
- REF_TRANSPORT (t, "keep_reading");
- prevent_endpoint_shutdown (t);
- }
- gpr_slice_buffer_reset_and_unref (&t->read_buffer);
- unlock (exec_ctx, t);
-
- if (keep_reading)
- {
- grpc_endpoint_read (exec_ctx, t->ep, &t->read_buffer, &t->recv_data);
- allow_endpoint_shutdown_unlocked (exec_ctx, t);
- UNREF_TRANSPORT (exec_ctx, t, "keep_reading");
+ GPR_ASSERT(!t->parsing_active);
+ if (!t->closed) {
+ t->parsing_active = 1;
+ /* merge stream lists */
+ grpc_chttp2_stream_map_move_into(&t->new_stream_map,
+ &t->parsing_stream_map);
+ grpc_chttp2_prepare_to_read(&t->global, &t->parsing);
+ gpr_mu_unlock(&t->mu);
+ for (; i < t->read_buffer.count &&
+ grpc_chttp2_perform_read(exec_ctx, &t->parsing,
+ t->read_buffer.slices[i]);
+ i++)
+ ;
+ gpr_mu_lock(&t->mu);
+ if (i != t->read_buffer.count) {
+ drop_connection(exec_ctx, t);
}
- else
- {
- UNREF_TRANSPORT (exec_ctx, t, "recv_data");
+ /* merge stream lists */
+ grpc_chttp2_stream_map_move_into(&t->new_stream_map,
+ &t->parsing_stream_map);
+ t->global.concurrent_stream_count =
+ (gpr_uint32)grpc_chttp2_stream_map_size(&t->parsing_stream_map);
+ if (t->parsing.initial_window_update != 0) {
+ grpc_chttp2_stream_map_for_each(&t->parsing_stream_map,
+ update_global_window, t);
+ t->parsing.initial_window_update = 0;
}
+ /* handle higher level things */
+ grpc_chttp2_publish_reads(exec_ctx, &t->global, &t->parsing);
+ t->parsing_active = 0;
+ }
+ if (!success || i != t->read_buffer.count) {
+ drop_connection(exec_ctx, t);
+ read_error_locked(exec_ctx, t);
+ } else if (!t->closed) {
+ keep_reading = 1;
+ REF_TRANSPORT(t, "keep_reading");
+ prevent_endpoint_shutdown(t);
+ }
+ gpr_slice_buffer_reset_and_unref(&t->read_buffer);
+ unlock(exec_ctx, t);
+
+ if (keep_reading) {
+ grpc_endpoint_read(exec_ctx, t->ep, &t->read_buffer, &t->recv_data);
+ allow_endpoint_shutdown_unlocked(exec_ctx, t);
+ UNREF_TRANSPORT(exec_ctx, t, "keep_reading");
+ } else {
+ UNREF_TRANSPORT(exec_ctx, t, "recv_data");
+ }
}
/*
* CALLBACK LOOP
*/
-static void
-connectivity_state_set (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport_global * transport_global, grpc_connectivity_state state, const char *reason)
-{
- GRPC_CHTTP2_IF_TRACING (gpr_log (GPR_DEBUG, "set connectivity_state=%d", state));
- grpc_connectivity_state_set (exec_ctx,&TRANSPORT_FROM_GLOBAL ( transport_global)->channel_callback.state_tracker, state, reason);
+static void connectivity_state_set(
+ grpc_exec_ctx *exec_ctx, grpc_chttp2_transport_global *transport_global,
+ grpc_connectivity_state state, const char *reason) {
+ GRPC_CHTTP2_IF_TRACING(
+ gpr_log(GPR_DEBUG, "set connectivity_state=%d", state));
+ grpc_connectivity_state_set(exec_ctx, &TRANSPORT_FROM_GLOBAL(transport_global)
+ ->channel_callback.state_tracker,
+ state, reason);
}
/*
* POLLSET STUFF
*/
-static void
-add_to_pollset_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, grpc_pollset * pollset)
-{
- if (t->ep)
- {
- grpc_endpoint_add_to_pollset (exec_ctx, t->ep, pollset);
- }
+static void add_to_pollset_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t,
+ grpc_pollset *pollset) {
+ if (t->ep) {
+ grpc_endpoint_add_to_pollset(exec_ctx, t->ep, pollset);
+ }
}
-static void
-add_to_pollset_set_locked (grpc_exec_ctx * exec_ctx, grpc_chttp2_transport * t, grpc_pollset_set * pollset_set)
-{
- if (t->ep)
- {
- grpc_endpoint_add_to_pollset_set (exec_ctx, t->ep, pollset_set);
- }
+static void add_to_pollset_set_locked(grpc_exec_ctx *exec_ctx,
+ grpc_chttp2_transport *t,
+ grpc_pollset_set *pollset_set) {
+ if (t->ep) {
+ grpc_endpoint_add_to_pollset_set(exec_ctx, t->ep, pollset_set);
+ }
}
/*
* TRACING
*/
-void
-grpc_chttp2_flowctl_trace (const char *file, int line, const char *reason, const char *context, const char *var, int is_client, gpr_uint32 stream_id, gpr_int64 current_value, gpr_int64 delta)
-{
+void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason,
+ const char *context, const char *var,
+ int is_client, gpr_uint32 stream_id,
+ gpr_int64 current_value, gpr_int64 delta) {
char *identifier;
char *context_scope;
char *context_thread;
- char *underscore_pos = strchr (context, '_');
- GPR_ASSERT (underscore_pos);
- context_thread = gpr_strdup (underscore_pos + 1);
- context_scope = gpr_strdup (context);
+ char *underscore_pos = strchr(context, '_');
+ GPR_ASSERT(underscore_pos);
+ context_thread = gpr_strdup(underscore_pos + 1);
+ context_scope = gpr_strdup(context);
context_scope[underscore_pos - context] = 0;
- if (stream_id)
- {
- gpr_asprintf (&identifier, "%s[%d]", context_scope, stream_id);
- }
- else
- {
- identifier = gpr_strdup (context_scope);
- }
- gpr_log (GPR_INFO, "FLOWCTL: %s %-10s %8s %-27s %8lld %c %8lld = %8lld %-10s [%s:%d]", is_client ? "client" : "server", identifier, context_thread, var, current_value, delta < 0 ? '-' : '+', delta < 0 ? -delta : delta, current_value + delta, reason, file, line);
- gpr_free (identifier);
- gpr_free (context_thread);
- gpr_free (context_scope);
+ if (stream_id) {
+ gpr_asprintf(&identifier, "%s[%d]", context_scope, stream_id);
+ } else {
+ identifier = gpr_strdup(context_scope);
+ }
+ gpr_log(GPR_INFO,
+ "FLOWCTL: %s %-10s %8s %-27s %8lld %c %8lld = %8lld %-10s [%s:%d]",
+ is_client ? "client" : "server", identifier, context_thread, var,
+ current_value, delta < 0 ? '-' : '+', delta < 0 ? -delta : delta,
+ current_value + delta, reason, file, line);
+ gpr_free(identifier);
+ gpr_free(context_thread);
+ gpr_free(context_scope);
}
/*
* INTEGRATION GLUE
*/
-static char *
-chttp2_get_peer (grpc_exec_ctx * exec_ctx, grpc_transport * t)
-{
- return gpr_strdup (((grpc_chttp2_transport *) t)->peer_string);
+static char *chttp2_get_peer(grpc_exec_ctx *exec_ctx, grpc_transport *t) {
+ return gpr_strdup(((grpc_chttp2_transport *)t)->peer_string);
}
-static const grpc_transport_vtable vtable = { sizeof (grpc_chttp2_stream),
- init_stream,
- perform_stream_op,
- perform_transport_op,
- destroy_stream,
- destroy_transport,
- chttp2_get_peer
-};
-
-grpc_transport *
-grpc_create_chttp2_transport (grpc_exec_ctx * exec_ctx, const grpc_channel_args * channel_args, grpc_endpoint * ep, grpc_mdctx * mdctx, int is_client)
-{
- grpc_chttp2_transport *t = gpr_malloc (sizeof (grpc_chttp2_transport));
- init_transport (exec_ctx, t, channel_args, ep, mdctx, is_client != 0);
+static const grpc_transport_vtable vtable = {sizeof(grpc_chttp2_stream),
+ init_stream,
+ perform_stream_op,
+ perform_transport_op,
+ destroy_stream,
+ destroy_transport,
+ chttp2_get_peer};
+
+grpc_transport *grpc_create_chttp2_transport(
+ grpc_exec_ctx *exec_ctx, const grpc_channel_args *channel_args,
+ grpc_endpoint *ep, grpc_mdctx *mdctx, int is_client) {
+ grpc_chttp2_transport *t = gpr_malloc(sizeof(grpc_chttp2_transport));
+ init_transport(exec_ctx, t, channel_args, ep, mdctx, is_client != 0);
return &t->base;
}
-void
-grpc_chttp2_transport_start_reading (grpc_exec_ctx * exec_ctx, grpc_transport * transport, gpr_slice * slices, size_t nslices)
-{
- grpc_chttp2_transport *t = (grpc_chttp2_transport *) transport;
- REF_TRANSPORT (t, "recv_data"); /* matches unref inside recv_data */
- gpr_slice_buffer_addn (&t->read_buffer, slices, nslices);
- recv_data (exec_ctx, t, 1);
+void grpc_chttp2_transport_start_reading(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ gpr_slice *slices, size_t nslices) {
+ grpc_chttp2_transport *t = (grpc_chttp2_transport *)transport;
+ REF_TRANSPORT(t, "recv_data"); /* matches unref inside recv_data */
+ gpr_slice_buffer_addn(&t->read_buffer, slices, nslices);
+ recv_data(exec_ctx, t, 1);
}
diff --git a/src/core/transport/chttp2_transport.h b/src/core/transport/chttp2_transport.h
index fe2f78a5ea..fce2b680fd 100644
--- a/src/core/transport/chttp2_transport.h
+++ b/src/core/transport/chttp2_transport.h
@@ -40,8 +40,12 @@
extern int grpc_http_trace;
extern int grpc_flowctl_trace;
-grpc_transport *grpc_create_chttp2_transport (grpc_exec_ctx * exec_ctx, const grpc_channel_args * channel_args, grpc_endpoint * ep, grpc_mdctx * metadata_context, int is_client);
+grpc_transport *grpc_create_chttp2_transport(
+ grpc_exec_ctx *exec_ctx, const grpc_channel_args *channel_args,
+ grpc_endpoint *ep, grpc_mdctx *metadata_context, int is_client);
-void grpc_chttp2_transport_start_reading (grpc_exec_ctx * exec_ctx, grpc_transport * transport, gpr_slice * slices, size_t nslices);
+void grpc_chttp2_transport_start_reading(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ gpr_slice *slices, size_t nslices);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CHTTP2_TRANSPORT_H */
diff --git a/src/core/transport/connectivity_state.c b/src/core/transport/connectivity_state.c
index fc39515f91..a53fecc198 100644
--- a/src/core/transport/connectivity_state.c
+++ b/src/core/transport/connectivity_state.c
@@ -41,11 +41,8 @@
int grpc_connectivity_state_trace = 0;
-const char *
-grpc_connectivity_state_name (grpc_connectivity_state state)
-{
- switch (state)
- {
+const char *grpc_connectivity_state_name(grpc_connectivity_state state) {
+ switch (state) {
case GRPC_CHANNEL_IDLE:
return "IDLE";
case GRPC_CHANNEL_CONNECTING:
@@ -56,95 +53,87 @@ grpc_connectivity_state_name (grpc_connectivity_state state)
return "TRANSIENT_FAILURE";
case GRPC_CHANNEL_FATAL_FAILURE:
return "FATAL_FAILURE";
- }
- abort ();
+ }
+ abort();
return "UNKNOWN";
}
-void
-grpc_connectivity_state_init (grpc_connectivity_state_tracker * tracker, grpc_connectivity_state init_state, const char *name)
-{
+void grpc_connectivity_state_init(grpc_connectivity_state_tracker *tracker,
+ grpc_connectivity_state init_state,
+ const char *name) {
tracker->current_state = init_state;
tracker->watchers = NULL;
- tracker->name = gpr_strdup (name);
+ tracker->name = gpr_strdup(name);
}
-void
-grpc_connectivity_state_destroy (grpc_exec_ctx * exec_ctx, grpc_connectivity_state_tracker * tracker)
-{
+void grpc_connectivity_state_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_connectivity_state_tracker *tracker) {
int success;
grpc_connectivity_state_watcher *w;
- while ((w = tracker->watchers))
- {
- tracker->watchers = w->next;
+ while ((w = tracker->watchers)) {
+ tracker->watchers = w->next;
- if (GRPC_CHANNEL_FATAL_FAILURE != *w->current)
- {
- *w->current = GRPC_CHANNEL_FATAL_FAILURE;
- success = 1;
- }
- else
- {
- success = 0;
- }
- grpc_exec_ctx_enqueue (exec_ctx, w->notify, success);
- gpr_free (w);
+ if (GRPC_CHANNEL_FATAL_FAILURE != *w->current) {
+ *w->current = GRPC_CHANNEL_FATAL_FAILURE;
+ success = 1;
+ } else {
+ success = 0;
}
- gpr_free (tracker->name);
+ grpc_exec_ctx_enqueue(exec_ctx, w->notify, success);
+ gpr_free(w);
+ }
+ gpr_free(tracker->name);
}
-grpc_connectivity_state
-grpc_connectivity_state_check (grpc_connectivity_state_tracker * tracker)
-{
- if (grpc_connectivity_state_trace)
- {
- gpr_log (GPR_DEBUG, "CONWATCH: %s: get %s", tracker->name, grpc_connectivity_state_name (tracker->current_state));
- }
+grpc_connectivity_state grpc_connectivity_state_check(
+ grpc_connectivity_state_tracker *tracker) {
+ if (grpc_connectivity_state_trace) {
+ gpr_log(GPR_DEBUG, "CONWATCH: %s: get %s", tracker->name,
+ grpc_connectivity_state_name(tracker->current_state));
+ }
return tracker->current_state;
}
-int
-grpc_connectivity_state_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_connectivity_state_tracker * tracker, grpc_connectivity_state * current, grpc_closure * notify)
-{
- if (grpc_connectivity_state_trace)
- {
- gpr_log (GPR_DEBUG, "CONWATCH: %s: from %s [cur=%s] notify=%p", tracker->name, grpc_connectivity_state_name (*current), grpc_connectivity_state_name (tracker->current_state), notify);
- }
- if (tracker->current_state != *current)
- {
- *current = tracker->current_state;
- grpc_exec_ctx_enqueue (exec_ctx, notify, 1);
- }
- else
- {
- grpc_connectivity_state_watcher *w = gpr_malloc (sizeof (*w));
- w->current = current;
- w->notify = notify;
- w->next = tracker->watchers;
- tracker->watchers = w;
- }
+int grpc_connectivity_state_notify_on_state_change(
+ grpc_exec_ctx *exec_ctx, grpc_connectivity_state_tracker *tracker,
+ grpc_connectivity_state *current, grpc_closure *notify) {
+ if (grpc_connectivity_state_trace) {
+ gpr_log(GPR_DEBUG, "CONWATCH: %s: from %s [cur=%s] notify=%p",
+ tracker->name, grpc_connectivity_state_name(*current),
+ grpc_connectivity_state_name(tracker->current_state), notify);
+ }
+ if (tracker->current_state != *current) {
+ *current = tracker->current_state;
+ grpc_exec_ctx_enqueue(exec_ctx, notify, 1);
+ } else {
+ grpc_connectivity_state_watcher *w = gpr_malloc(sizeof(*w));
+ w->current = current;
+ w->notify = notify;
+ w->next = tracker->watchers;
+ tracker->watchers = w;
+ }
return tracker->current_state == GRPC_CHANNEL_IDLE;
}
-void
-grpc_connectivity_state_set (grpc_exec_ctx * exec_ctx, grpc_connectivity_state_tracker * tracker, grpc_connectivity_state state, const char *reason)
-{
+void grpc_connectivity_state_set(grpc_exec_ctx *exec_ctx,
+ grpc_connectivity_state_tracker *tracker,
+ grpc_connectivity_state state,
+ const char *reason) {
grpc_connectivity_state_watcher *w;
- if (grpc_connectivity_state_trace)
- {
- gpr_log (GPR_DEBUG, "SET: %s: %s --> %s [%s]", tracker->name, grpc_connectivity_state_name (tracker->current_state), grpc_connectivity_state_name (state), reason);
- }
- if (tracker->current_state == state)
- {
- return;
- }
- GPR_ASSERT (tracker->current_state != GRPC_CHANNEL_FATAL_FAILURE);
+ if (grpc_connectivity_state_trace) {
+ gpr_log(GPR_DEBUG, "SET: %s: %s --> %s [%s]", tracker->name,
+ grpc_connectivity_state_name(tracker->current_state),
+ grpc_connectivity_state_name(state), reason);
+ }
+ if (tracker->current_state == state) {
+ return;
+ }
+ GPR_ASSERT(tracker->current_state != GRPC_CHANNEL_FATAL_FAILURE);
tracker->current_state = state;
- while ((w = tracker->watchers) != NULL)
- {
- *w->current = tracker->current_state;
- tracker->watchers = w->next;
- grpc_exec_ctx_enqueue (exec_ctx, w->notify, 1);
- gpr_free (w);
- }
+ while ((w = tracker->watchers) != NULL) {
+ *w->current = tracker->current_state;
+ tracker->watchers = w->next;
+ grpc_exec_ctx_enqueue(exec_ctx, w->notify, 1);
+ gpr_free(w);
+ }
}
diff --git a/src/core/transport/connectivity_state.h b/src/core/transport/connectivity_state.h
index 38742834fb..8b6b0554cd 100644
--- a/src/core/transport/connectivity_state.h
+++ b/src/core/transport/connectivity_state.h
@@ -37,8 +37,7 @@
#include <grpc/grpc.h>
#include "src/core/iomgr/exec_ctx.h"
-typedef struct grpc_connectivity_state_watcher
-{
+typedef struct grpc_connectivity_state_watcher {
/** we keep watchers in a linked list */
struct grpc_connectivity_state_watcher *next;
/** closure to notify on change */
@@ -47,8 +46,7 @@ typedef struct grpc_connectivity_state_watcher
grpc_connectivity_state *current;
} grpc_connectivity_state_watcher;
-typedef struct
-{
+typedef struct {
/** current connectivity state */
grpc_connectivity_state current_state;
/** all our watchers */
@@ -59,16 +57,25 @@ typedef struct
extern int grpc_connectivity_state_trace;
-void grpc_connectivity_state_init (grpc_connectivity_state_tracker * tracker, grpc_connectivity_state init_state, const char *name);
-void grpc_connectivity_state_destroy (grpc_exec_ctx * exec_ctx, grpc_connectivity_state_tracker * tracker);
+void grpc_connectivity_state_init(grpc_connectivity_state_tracker *tracker,
+ grpc_connectivity_state init_state,
+ const char *name);
+void grpc_connectivity_state_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_connectivity_state_tracker *tracker);
/** Set connectivity state; not thread safe; access must be serialized with an
* external lock */
-void grpc_connectivity_state_set (grpc_exec_ctx * exec_ctx, grpc_connectivity_state_tracker * tracker, grpc_connectivity_state state, const char *reason);
+void grpc_connectivity_state_set(grpc_exec_ctx *exec_ctx,
+ grpc_connectivity_state_tracker *tracker,
+ grpc_connectivity_state state,
+ const char *reason);
-grpc_connectivity_state grpc_connectivity_state_check (grpc_connectivity_state_tracker * tracker);
+grpc_connectivity_state grpc_connectivity_state_check(
+ grpc_connectivity_state_tracker *tracker);
/** Return 1 if the channel should start connecting, 0 otherwise */
-int grpc_connectivity_state_notify_on_state_change (grpc_exec_ctx * exec_ctx, grpc_connectivity_state_tracker * tracker, grpc_connectivity_state * current, grpc_closure * notify);
+int grpc_connectivity_state_notify_on_state_change(
+ grpc_exec_ctx *exec_ctx, grpc_connectivity_state_tracker *tracker,
+ grpc_connectivity_state *current, grpc_closure *notify);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_CONNECTIVITY_STATE_H */
diff --git a/src/core/transport/metadata.c b/src/core/transport/metadata.c
index 0ce18712c3..9d135f4356 100644
--- a/src/core/transport/metadata.c
+++ b/src/core/transport/metadata.c
@@ -62,8 +62,7 @@
#define REF_MD_LOCKED(s) ref_md_locked((s))
#endif
-typedef struct internal_string
-{
+typedef struct internal_string {
/* must be byte compatible with grpc_mdstr */
gpr_slice slice;
gpr_uint32 hash;
@@ -80,8 +79,7 @@ typedef struct internal_string
struct internal_string *bucket_next;
} internal_string;
-typedef struct internal_metadata
-{
+typedef struct internal_metadata {
/* must be byte compatible with grpc_mdelem */
internal_string *key;
internal_string *value;
@@ -91,14 +89,13 @@ typedef struct internal_metadata
/* private only data */
gpr_mu mu_user_data;
void *user_data;
- void (*destroy_user_data) (void *user_data);
+ void (*destroy_user_data)(void *user_data);
grpc_mdctx *context;
struct internal_metadata *bucket_next;
} internal_metadata;
-struct grpc_mdctx
-{
+struct grpc_mdctx {
gpr_uint32 hash_seed;
int refs;
@@ -114,21 +111,15 @@ struct grpc_mdctx
size_t mdtab_capacity;
};
-static void internal_string_ref (internal_string * s DEBUG_ARGS);
-static void internal_string_unref (internal_string * s DEBUG_ARGS);
-static void discard_metadata (grpc_mdctx * ctx);
-static void gc_mdtab (grpc_mdctx * ctx);
-static void metadata_context_destroy_locked (grpc_mdctx * ctx);
+static void internal_string_ref(internal_string *s DEBUG_ARGS);
+static void internal_string_unref(internal_string *s DEBUG_ARGS);
+static void discard_metadata(grpc_mdctx *ctx);
+static void gc_mdtab(grpc_mdctx *ctx);
+static void metadata_context_destroy_locked(grpc_mdctx *ctx);
-static void
-lock (grpc_mdctx * ctx)
-{
- gpr_mu_lock (&ctx->mu);
-}
+static void lock(grpc_mdctx *ctx) { gpr_mu_lock(&ctx->mu); }
-static void
-unlock (grpc_mdctx * ctx)
-{
+static void unlock(grpc_mdctx *ctx) {
/* If the context has been orphaned we'd like to delete it soon. We check
conditions in unlock as it signals the end of mutations on a context.
@@ -141,52 +132,49 @@ unlock (grpc_mdctx * ctx)
mdelems on every unlock (instead of the usual 'I'm too loaded' trigger
case), since otherwise we can be stuck waiting for a garbage collection
that will never happen. */
- if (ctx->refs == 0)
- {
+ if (ctx->refs == 0) {
/* uncomment if you're having trouble diagnosing an mdelem leak to make
things clearer (slows down destruction a lot, however) */
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gc_mdtab (ctx);
+ gc_mdtab(ctx);
#endif
- if (ctx->mdtab_count && ctx->mdtab_count == ctx->mdtab_free)
- {
- discard_metadata (ctx);
- }
- if (ctx->strtab_count == 0)
- {
- metadata_context_destroy_locked (ctx);
- return;
- }
+ if (ctx->mdtab_count && ctx->mdtab_count == ctx->mdtab_free) {
+ discard_metadata(ctx);
+ }
+ if (ctx->strtab_count == 0) {
+ metadata_context_destroy_locked(ctx);
+ return;
}
- gpr_mu_unlock (&ctx->mu);
+ }
+ gpr_mu_unlock(&ctx->mu);
}
-static void
-ref_md_locked (internal_metadata * md DEBUG_ARGS)
-{
+static void ref_md_locked(internal_metadata *md DEBUG_ARGS) {
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "ELM REF:%p:%d->%d: '%s' = '%s'", md, gpr_atm_no_barrier_load (&md->refcnt), gpr_atm_no_barrier_load (&md->refcnt) + 1, grpc_mdstr_as_c_string ((grpc_mdstr *) md->key), grpc_mdstr_as_c_string ((grpc_mdstr *) md->value));
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "ELM REF:%p:%d->%d: '%s' = '%s'", md,
+ gpr_atm_no_barrier_load(&md->refcnt),
+ gpr_atm_no_barrier_load(&md->refcnt) + 1,
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
#endif
- if (0 == gpr_atm_no_barrier_fetch_add (&md->refcnt, 1))
- {
- md->context->mdtab_free--;
- }
+ if (0 == gpr_atm_no_barrier_fetch_add(&md->refcnt, 1)) {
+ md->context->mdtab_free--;
+ }
}
-grpc_mdctx *
-grpc_mdctx_create_with_seed (gpr_uint32 seed)
-{
- grpc_mdctx *ctx = gpr_malloc (sizeof (grpc_mdctx));
+grpc_mdctx *grpc_mdctx_create_with_seed(gpr_uint32 seed) {
+ grpc_mdctx *ctx = gpr_malloc(sizeof(grpc_mdctx));
ctx->refs = 1;
ctx->hash_seed = seed;
- gpr_mu_init (&ctx->mu);
- ctx->strtab = gpr_malloc (sizeof (internal_string *) * INITIAL_STRTAB_CAPACITY);
- memset (ctx->strtab, 0, sizeof (grpc_mdstr *) * INITIAL_STRTAB_CAPACITY);
+ gpr_mu_init(&ctx->mu);
+ ctx->strtab = gpr_malloc(sizeof(internal_string *) * INITIAL_STRTAB_CAPACITY);
+ memset(ctx->strtab, 0, sizeof(grpc_mdstr *) * INITIAL_STRTAB_CAPACITY);
ctx->strtab_count = 0;
ctx->strtab_capacity = INITIAL_STRTAB_CAPACITY;
- ctx->mdtab = gpr_malloc (sizeof (internal_metadata *) * INITIAL_MDTAB_CAPACITY);
- memset (ctx->mdtab, 0, sizeof (grpc_mdelem *) * INITIAL_MDTAB_CAPACITY);
+ ctx->mdtab = gpr_malloc(sizeof(internal_metadata *) * INITIAL_MDTAB_CAPACITY);
+ memset(ctx->mdtab, 0, sizeof(grpc_mdelem *) * INITIAL_MDTAB_CAPACITY);
ctx->mdtab_count = 0;
ctx->mdtab_capacity = INITIAL_MDTAB_CAPACITY;
ctx->mdtab_free = 0;
@@ -194,258 +182,218 @@ grpc_mdctx_create_with_seed (gpr_uint32 seed)
return ctx;
}
-grpc_mdctx *
-grpc_mdctx_create (void)
-{
+grpc_mdctx *grpc_mdctx_create(void) {
/* This seed is used to prevent remote connections from controlling hash table
* collisions. It needs to be somewhat unpredictable to a remote connection.
*/
- return grpc_mdctx_create_with_seed ((gpr_uint32) gpr_now (GPR_CLOCK_REALTIME).tv_nsec);
+ return grpc_mdctx_create_with_seed(
+ (gpr_uint32)gpr_now(GPR_CLOCK_REALTIME).tv_nsec);
}
-static void
-discard_metadata (grpc_mdctx * ctx)
-{
+static void discard_metadata(grpc_mdctx *ctx) {
size_t i;
internal_metadata *next, *cur;
- for (i = 0; i < ctx->mdtab_capacity; i++)
- {
- cur = ctx->mdtab[i];
- while (cur)
- {
- GPR_ASSERT (gpr_atm_acq_load (&cur->refcnt) == 0);
- next = cur->bucket_next;
- INTERNAL_STRING_UNREF (cur->key);
- INTERNAL_STRING_UNREF (cur->value);
- if (cur->user_data)
- {
- cur->destroy_user_data (cur->user_data);
- }
- gpr_mu_destroy (&cur->mu_user_data);
- gpr_free (cur);
- cur = next;
- ctx->mdtab_free--;
- ctx->mdtab_count--;
- }
- ctx->mdtab[i] = NULL;
+ for (i = 0; i < ctx->mdtab_capacity; i++) {
+ cur = ctx->mdtab[i];
+ while (cur) {
+ GPR_ASSERT(gpr_atm_acq_load(&cur->refcnt) == 0);
+ next = cur->bucket_next;
+ INTERNAL_STRING_UNREF(cur->key);
+ INTERNAL_STRING_UNREF(cur->value);
+ if (cur->user_data) {
+ cur->destroy_user_data(cur->user_data);
+ }
+ gpr_mu_destroy(&cur->mu_user_data);
+ gpr_free(cur);
+ cur = next;
+ ctx->mdtab_free--;
+ ctx->mdtab_count--;
}
+ ctx->mdtab[i] = NULL;
+ }
+}
+
+static void metadata_context_destroy_locked(grpc_mdctx *ctx) {
+ GPR_ASSERT(ctx->strtab_count == 0);
+ GPR_ASSERT(ctx->mdtab_count == 0);
+ GPR_ASSERT(ctx->mdtab_free == 0);
+ gpr_free(ctx->strtab);
+ gpr_free(ctx->mdtab);
+ gpr_mu_unlock(&ctx->mu);
+ gpr_mu_destroy(&ctx->mu);
+ gpr_free(ctx);
}
-static void
-metadata_context_destroy_locked (grpc_mdctx * ctx)
-{
- GPR_ASSERT (ctx->strtab_count == 0);
- GPR_ASSERT (ctx->mdtab_count == 0);
- GPR_ASSERT (ctx->mdtab_free == 0);
- gpr_free (ctx->strtab);
- gpr_free (ctx->mdtab);
- gpr_mu_unlock (&ctx->mu);
- gpr_mu_destroy (&ctx->mu);
- gpr_free (ctx);
-}
-
-void
-grpc_mdctx_ref (grpc_mdctx * ctx)
-{
- lock (ctx);
- GPR_ASSERT (ctx->refs > 0);
+void grpc_mdctx_ref(grpc_mdctx *ctx) {
+ lock(ctx);
+ GPR_ASSERT(ctx->refs > 0);
ctx->refs++;
- unlock (ctx);
+ unlock(ctx);
}
-void
-grpc_mdctx_unref (grpc_mdctx * ctx)
-{
- lock (ctx);
- GPR_ASSERT (ctx->refs > 0);
+void grpc_mdctx_unref(grpc_mdctx *ctx) {
+ lock(ctx);
+ GPR_ASSERT(ctx->refs > 0);
ctx->refs--;
- unlock (ctx);
+ unlock(ctx);
}
-static void
-grow_strtab (grpc_mdctx * ctx)
-{
+static void grow_strtab(grpc_mdctx *ctx) {
size_t capacity = ctx->strtab_capacity * 2;
size_t i;
- internal_string **strtab = gpr_malloc (sizeof (internal_string *) * capacity);
+ internal_string **strtab = gpr_malloc(sizeof(internal_string *) * capacity);
internal_string *s, *next;
- memset (strtab, 0, sizeof (internal_string *) * capacity);
-
- for (i = 0; i < ctx->strtab_capacity; i++)
- {
- for (s = ctx->strtab[i]; s; s = next)
- {
- next = s->bucket_next;
- s->bucket_next = strtab[s->hash % capacity];
- strtab[s->hash % capacity] = s;
- }
+ memset(strtab, 0, sizeof(internal_string *) * capacity);
+
+ for (i = 0; i < ctx->strtab_capacity; i++) {
+ for (s = ctx->strtab[i]; s; s = next) {
+ next = s->bucket_next;
+ s->bucket_next = strtab[s->hash % capacity];
+ strtab[s->hash % capacity] = s;
}
+ }
- gpr_free (ctx->strtab);
+ gpr_free(ctx->strtab);
ctx->strtab = strtab;
ctx->strtab_capacity = capacity;
}
-static void
-internal_destroy_string (internal_string * is)
-{
+static void internal_destroy_string(internal_string *is) {
internal_string **prev_next;
internal_string *cur;
grpc_mdctx *ctx = is->context;
- if (is->has_base64_and_huffman_encoded)
- {
- gpr_slice_unref (is->base64_and_huffman);
- }
- for (prev_next = &ctx->strtab[is->hash % ctx->strtab_capacity], cur = *prev_next; cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
+ if (is->has_base64_and_huffman_encoded) {
+ gpr_slice_unref(is->base64_and_huffman);
+ }
+ for (prev_next = &ctx->strtab[is->hash % ctx->strtab_capacity],
+ cur = *prev_next;
+ cur != is; prev_next = &cur->bucket_next, cur = cur->bucket_next)
;
*prev_next = cur->bucket_next;
ctx->strtab_count--;
- gpr_free (is);
+ gpr_free(is);
}
-static void
-internal_string_ref (internal_string * s DEBUG_ARGS)
-{
+static void internal_string_ref(internal_string *s DEBUG_ARGS) {
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "STR REF:%p:%d->%d: '%s'", s, s->refs, s->refs + 1, grpc_mdstr_as_c_string ((grpc_mdstr *) s));
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "STR REF:%p:%d->%d: '%s'", s,
+ s->refs, s->refs + 1, grpc_mdstr_as_c_string((grpc_mdstr *)s));
#endif
++s->refs;
}
-static void
-internal_string_unref (internal_string * s DEBUG_ARGS)
-{
+static void internal_string_unref(internal_string *s DEBUG_ARGS) {
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "STR UNREF:%p:%d->%d: '%s'", s, s->refs, s->refs - 1, grpc_mdstr_as_c_string ((grpc_mdstr *) s));
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "STR UNREF:%p:%d->%d: '%s'", s,
+ s->refs, s->refs - 1, grpc_mdstr_as_c_string((grpc_mdstr *)s));
#endif
- GPR_ASSERT (s->refs > 0);
- if (0 == --s->refs)
- {
- internal_destroy_string (s);
- }
+ GPR_ASSERT(s->refs > 0);
+ if (0 == --s->refs) {
+ internal_destroy_string(s);
+ }
}
-static void
-slice_ref (void *p)
-{
- internal_string *is = (internal_string *) ((char *) p - offsetof (internal_string, refcount));
+static void slice_ref(void *p) {
+ internal_string *is =
+ (internal_string *)((char *)p - offsetof(internal_string, refcount));
grpc_mdctx *ctx = is->context;
- lock (ctx);
- INTERNAL_STRING_REF (is);
- unlock (ctx);
+ lock(ctx);
+ INTERNAL_STRING_REF(is);
+ unlock(ctx);
}
-static void
-slice_unref (void *p)
-{
- internal_string *is = (internal_string *) ((char *) p - offsetof (internal_string, refcount));
+static void slice_unref(void *p) {
+ internal_string *is =
+ (internal_string *)((char *)p - offsetof(internal_string, refcount));
grpc_mdctx *ctx = is->context;
- lock (ctx);
- INTERNAL_STRING_UNREF (is);
- unlock (ctx);
-}
-
-grpc_mdstr *
-grpc_mdstr_from_string (grpc_mdctx * ctx, const char *str, int canonicalize_key)
-{
- if (canonicalize_key)
- {
- size_t len;
- size_t i;
- int canonical = 1;
-
- for (i = 0; str[i]; i++)
- {
- if (str[i] >= 'A' && str[i] <= 'Z')
- {
- canonical = 0;
- /* Keep going in loop just to get string length */
- }
- }
- len = i;
-
- if (canonical)
- {
- return grpc_mdstr_from_buffer (ctx, (const gpr_uint8 *) str, len);
- }
- else
- {
- char *copy = gpr_malloc (len);
- grpc_mdstr *ret;
- for (i = 0; i < len; i++)
- {
- if (str[i] >= 'A' && str[i] <= 'Z')
- {
- copy[i] = (char) (str[i] - 'A' + 'a');
- }
- else
- {
- copy[i] = str[i];
- }
- }
- ret = grpc_mdstr_from_buffer (ctx, (const gpr_uint8 *) copy, len);
- gpr_free (copy);
- return ret;
- }
+ lock(ctx);
+ INTERNAL_STRING_UNREF(is);
+ unlock(ctx);
+}
+
+grpc_mdstr *grpc_mdstr_from_string(grpc_mdctx *ctx, const char *str,
+ int canonicalize_key) {
+ if (canonicalize_key) {
+ size_t len;
+ size_t i;
+ int canonical = 1;
+
+ for (i = 0; str[i]; i++) {
+ if (str[i] >= 'A' && str[i] <= 'Z') {
+ canonical = 0;
+ /* Keep going in loop just to get string length */
+ }
}
- return grpc_mdstr_from_buffer (ctx, (const gpr_uint8 *) str, strlen (str));
+ len = i;
+
+ if (canonical) {
+ return grpc_mdstr_from_buffer(ctx, (const gpr_uint8 *)str, len);
+ } else {
+ char *copy = gpr_malloc(len);
+ grpc_mdstr *ret;
+ for (i = 0; i < len; i++) {
+ if (str[i] >= 'A' && str[i] <= 'Z') {
+ copy[i] = (char)(str[i] - 'A' + 'a');
+ } else {
+ copy[i] = str[i];
+ }
+ }
+ ret = grpc_mdstr_from_buffer(ctx, (const gpr_uint8 *)copy, len);
+ gpr_free(copy);
+ return ret;
+ }
+ }
+ return grpc_mdstr_from_buffer(ctx, (const gpr_uint8 *)str, strlen(str));
}
-grpc_mdstr *
-grpc_mdstr_from_slice (grpc_mdctx * ctx, gpr_slice slice)
-{
- grpc_mdstr *result = grpc_mdstr_from_buffer (ctx, GPR_SLICE_START_PTR (slice),
- GPR_SLICE_LENGTH (slice));
- gpr_slice_unref (slice);
+grpc_mdstr *grpc_mdstr_from_slice(grpc_mdctx *ctx, gpr_slice slice) {
+ grpc_mdstr *result = grpc_mdstr_from_buffer(ctx, GPR_SLICE_START_PTR(slice),
+ GPR_SLICE_LENGTH(slice));
+ gpr_slice_unref(slice);
return result;
}
-grpc_mdstr *
-grpc_mdstr_from_buffer (grpc_mdctx * ctx, const gpr_uint8 * buf, size_t length)
-{
- gpr_uint32 hash = gpr_murmur_hash3 (buf, length, ctx->hash_seed);
+grpc_mdstr *grpc_mdstr_from_buffer(grpc_mdctx *ctx, const gpr_uint8 *buf,
+ size_t length) {
+ gpr_uint32 hash = gpr_murmur_hash3(buf, length, ctx->hash_seed);
internal_string *s;
- lock (ctx);
+ lock(ctx);
/* search for an existing string */
- for (s = ctx->strtab[hash % ctx->strtab_capacity]; s; s = s->bucket_next)
- {
- if (s->hash == hash && GPR_SLICE_LENGTH (s->slice) == length && 0 == memcmp (buf, GPR_SLICE_START_PTR (s->slice), length))
- {
- INTERNAL_STRING_REF (s);
- unlock (ctx);
- return (grpc_mdstr *) s;
- }
+ for (s = ctx->strtab[hash % ctx->strtab_capacity]; s; s = s->bucket_next) {
+ if (s->hash == hash && GPR_SLICE_LENGTH(s->slice) == length &&
+ 0 == memcmp(buf, GPR_SLICE_START_PTR(s->slice), length)) {
+ INTERNAL_STRING_REF(s);
+ unlock(ctx);
+ return (grpc_mdstr *)s;
}
+ }
/* not found: create a new string */
- if (length + 1 < GPR_SLICE_INLINED_SIZE)
- {
- /* string data goes directly into the slice */
- s = gpr_malloc (sizeof (internal_string));
- s->refs = 1;
- s->slice.refcount = NULL;
- memcpy (s->slice.data.inlined.bytes, buf, length);
- s->slice.data.inlined.bytes[length] = 0;
- s->slice.data.inlined.length = (gpr_uint8) length;
- }
- else
- {
- /* string data goes after the internal_string header, and we +1 for null
- terminator */
- s = gpr_malloc (sizeof (internal_string) + length + 1);
- s->refs = 1;
- s->refcount.ref = slice_ref;
- s->refcount.unref = slice_unref;
- s->slice.refcount = &s->refcount;
- s->slice.data.refcounted.bytes = (gpr_uint8 *) (s + 1);
- s->slice.data.refcounted.length = length;
- memcpy (s->slice.data.refcounted.bytes, buf, length);
- /* add a null terminator for cheap c string conversion when desired */
- s->slice.data.refcounted.bytes[length] = 0;
- }
+ if (length + 1 < GPR_SLICE_INLINED_SIZE) {
+ /* string data goes directly into the slice */
+ s = gpr_malloc(sizeof(internal_string));
+ s->refs = 1;
+ s->slice.refcount = NULL;
+ memcpy(s->slice.data.inlined.bytes, buf, length);
+ s->slice.data.inlined.bytes[length] = 0;
+ s->slice.data.inlined.length = (gpr_uint8)length;
+ } else {
+ /* string data goes after the internal_string header, and we +1 for null
+ terminator */
+ s = gpr_malloc(sizeof(internal_string) + length + 1);
+ s->refs = 1;
+ s->refcount.ref = slice_ref;
+ s->refcount.unref = slice_unref;
+ s->slice.refcount = &s->refcount;
+ s->slice.data.refcounted.bytes = (gpr_uint8 *)(s + 1);
+ s->slice.data.refcounted.length = length;
+ memcpy(s->slice.data.refcounted.bytes, buf, length);
+ /* add a null terminator for cheap c string conversion when desired */
+ s->slice.data.refcounted.bytes[length] = 0;
+ }
s->has_base64_and_huffman_encoded = 0;
s->hash = hash;
s->context = ctx;
@@ -454,357 +402,316 @@ grpc_mdstr_from_buffer (grpc_mdctx * ctx, const gpr_uint8 * buf, size_t length)
ctx->strtab_count++;
- if (ctx->strtab_count > ctx->strtab_capacity * 2)
- {
- grow_strtab (ctx);
- }
+ if (ctx->strtab_count > ctx->strtab_capacity * 2) {
+ grow_strtab(ctx);
+ }
- unlock (ctx);
+ unlock(ctx);
- return (grpc_mdstr *) s;
+ return (grpc_mdstr *)s;
}
-static void
-gc_mdtab (grpc_mdctx * ctx)
-{
+static void gc_mdtab(grpc_mdctx *ctx) {
size_t i;
internal_metadata **prev_next;
internal_metadata *md, *next;
- for (i = 0; i < ctx->mdtab_capacity; i++)
- {
- prev_next = &ctx->mdtab[i];
- for (md = ctx->mdtab[i]; md; md = next)
- {
- next = md->bucket_next;
- if (gpr_atm_acq_load (&md->refcnt) == 0)
- {
- INTERNAL_STRING_UNREF (md->key);
- INTERNAL_STRING_UNREF (md->value);
- if (md->user_data)
- {
- md->destroy_user_data (md->user_data);
- }
- gpr_free (md);
- *prev_next = next;
- ctx->mdtab_free--;
- ctx->mdtab_count--;
- }
- else
- {
- prev_next = &md->bucket_next;
- }
- }
+ for (i = 0; i < ctx->mdtab_capacity; i++) {
+ prev_next = &ctx->mdtab[i];
+ for (md = ctx->mdtab[i]; md; md = next) {
+ next = md->bucket_next;
+ if (gpr_atm_acq_load(&md->refcnt) == 0) {
+ INTERNAL_STRING_UNREF(md->key);
+ INTERNAL_STRING_UNREF(md->value);
+ if (md->user_data) {
+ md->destroy_user_data(md->user_data);
+ }
+ gpr_free(md);
+ *prev_next = next;
+ ctx->mdtab_free--;
+ ctx->mdtab_count--;
+ } else {
+ prev_next = &md->bucket_next;
+ }
}
+ }
- GPR_ASSERT (ctx->mdtab_free == 0);
+ GPR_ASSERT(ctx->mdtab_free == 0);
}
-static void
-grow_mdtab (grpc_mdctx * ctx)
-{
+static void grow_mdtab(grpc_mdctx *ctx) {
size_t capacity = ctx->mdtab_capacity * 2;
size_t i;
- internal_metadata **mdtab = gpr_malloc (sizeof (internal_metadata *) * capacity);
+ internal_metadata **mdtab =
+ gpr_malloc(sizeof(internal_metadata *) * capacity);
internal_metadata *md, *next;
gpr_uint32 hash;
- memset (mdtab, 0, sizeof (internal_metadata *) * capacity);
-
- for (i = 0; i < ctx->mdtab_capacity; i++)
- {
- for (md = ctx->mdtab[i]; md; md = next)
- {
- hash = GRPC_MDSTR_KV_HASH (md->key->hash, md->value->hash);
- next = md->bucket_next;
- md->bucket_next = mdtab[hash % capacity];
- mdtab[hash % capacity] = md;
- }
+ memset(mdtab, 0, sizeof(internal_metadata *) * capacity);
+
+ for (i = 0; i < ctx->mdtab_capacity; i++) {
+ for (md = ctx->mdtab[i]; md; md = next) {
+ hash = GRPC_MDSTR_KV_HASH(md->key->hash, md->value->hash);
+ next = md->bucket_next;
+ md->bucket_next = mdtab[hash % capacity];
+ mdtab[hash % capacity] = md;
}
+ }
- gpr_free (ctx->mdtab);
+ gpr_free(ctx->mdtab);
ctx->mdtab = mdtab;
ctx->mdtab_capacity = capacity;
}
-static void
-rehash_mdtab (grpc_mdctx * ctx)
-{
- if (ctx->mdtab_free > ctx->mdtab_capacity / 4)
- {
- gc_mdtab (ctx);
- }
- else
- {
- grow_mdtab (ctx);
- }
+static void rehash_mdtab(grpc_mdctx *ctx) {
+ if (ctx->mdtab_free > ctx->mdtab_capacity / 4) {
+ gc_mdtab(ctx);
+ } else {
+ grow_mdtab(ctx);
+ }
}
-grpc_mdelem *
-grpc_mdelem_from_metadata_strings (grpc_mdctx * ctx, grpc_mdstr * mkey, grpc_mdstr * mvalue)
-{
- internal_string *key = (internal_string *) mkey;
- internal_string *value = (internal_string *) mvalue;
- gpr_uint32 hash = GRPC_MDSTR_KV_HASH (mkey->hash, mvalue->hash);
+grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdctx *ctx,
+ grpc_mdstr *mkey,
+ grpc_mdstr *mvalue) {
+ internal_string *key = (internal_string *)mkey;
+ internal_string *value = (internal_string *)mvalue;
+ gpr_uint32 hash = GRPC_MDSTR_KV_HASH(mkey->hash, mvalue->hash);
internal_metadata *md;
- GPR_ASSERT (key->context == ctx);
- GPR_ASSERT (value->context == ctx);
+ GPR_ASSERT(key->context == ctx);
+ GPR_ASSERT(value->context == ctx);
- lock (ctx);
+ lock(ctx);
/* search for an existing pair */
- for (md = ctx->mdtab[hash % ctx->mdtab_capacity]; md; md = md->bucket_next)
- {
- if (md->key == key && md->value == value)
- {
- REF_MD_LOCKED (md);
- INTERNAL_STRING_UNREF (key);
- INTERNAL_STRING_UNREF (value);
- unlock (ctx);
- return (grpc_mdelem *) md;
- }
+ for (md = ctx->mdtab[hash % ctx->mdtab_capacity]; md; md = md->bucket_next) {
+ if (md->key == key && md->value == value) {
+ REF_MD_LOCKED(md);
+ INTERNAL_STRING_UNREF(key);
+ INTERNAL_STRING_UNREF(value);
+ unlock(ctx);
+ return (grpc_mdelem *)md;
}
+ }
/* not found: create a new pair */
- md = gpr_malloc (sizeof (internal_metadata));
- gpr_atm_rel_store (&md->refcnt, 1);
+ md = gpr_malloc(sizeof(internal_metadata));
+ gpr_atm_rel_store(&md->refcnt, 1);
md->context = ctx;
md->key = key;
md->value = value;
md->user_data = NULL;
md->destroy_user_data = NULL;
md->bucket_next = ctx->mdtab[hash % ctx->mdtab_capacity];
- gpr_mu_init (&md->mu_user_data);
+ gpr_mu_init(&md->mu_user_data);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md, gpr_atm_no_barrier_load (&md->refcnt), grpc_mdstr_as_c_string ((grpc_mdstr *) md->key), grpc_mdstr_as_c_string ((grpc_mdstr *) md->value));
+ gpr_log(GPR_DEBUG, "ELM NEW:%p:%d: '%s' = '%s'", md,
+ gpr_atm_no_barrier_load(&md->refcnt),
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
#endif
ctx->mdtab[hash % ctx->mdtab_capacity] = md;
ctx->mdtab_count++;
- if (ctx->mdtab_count > ctx->mdtab_capacity * 2)
- {
- rehash_mdtab (ctx);
- }
+ if (ctx->mdtab_count > ctx->mdtab_capacity * 2) {
+ rehash_mdtab(ctx);
+ }
- unlock (ctx);
+ unlock(ctx);
- return (grpc_mdelem *) md;
+ return (grpc_mdelem *)md;
}
-grpc_mdelem *
-grpc_mdelem_from_strings (grpc_mdctx * ctx, const char *key, const char *value)
-{
- return grpc_mdelem_from_metadata_strings (ctx, grpc_mdstr_from_string (ctx, key, 0), grpc_mdstr_from_string (ctx, value, 0));
+grpc_mdelem *grpc_mdelem_from_strings(grpc_mdctx *ctx, const char *key,
+ const char *value) {
+ return grpc_mdelem_from_metadata_strings(
+ ctx, grpc_mdstr_from_string(ctx, key, 0),
+ grpc_mdstr_from_string(ctx, value, 0));
}
-grpc_mdelem *
-grpc_mdelem_from_slices (grpc_mdctx * ctx, gpr_slice key, gpr_slice value)
-{
- return grpc_mdelem_from_metadata_strings (ctx, grpc_mdstr_from_slice (ctx, key), grpc_mdstr_from_slice (ctx, value));
+grpc_mdelem *grpc_mdelem_from_slices(grpc_mdctx *ctx, gpr_slice key,
+ gpr_slice value) {
+ return grpc_mdelem_from_metadata_strings(ctx, grpc_mdstr_from_slice(ctx, key),
+ grpc_mdstr_from_slice(ctx, value));
}
-grpc_mdelem *
-grpc_mdelem_from_string_and_buffer (grpc_mdctx * ctx, const char *key, const gpr_uint8 * value, size_t value_length, int canonicalize_key)
-{
- return grpc_mdelem_from_metadata_strings (ctx, grpc_mdstr_from_string (ctx, key, canonicalize_key), grpc_mdstr_from_buffer (ctx, value, value_length));
+grpc_mdelem *grpc_mdelem_from_string_and_buffer(grpc_mdctx *ctx,
+ const char *key,
+ const gpr_uint8 *value,
+ size_t value_length,
+ int canonicalize_key) {
+ return grpc_mdelem_from_metadata_strings(
+ ctx, grpc_mdstr_from_string(ctx, key, canonicalize_key),
+ grpc_mdstr_from_buffer(ctx, value, value_length));
}
-grpc_mdelem *
-grpc_mdelem_ref (grpc_mdelem * gmd DEBUG_ARGS)
-{
- internal_metadata *md = (internal_metadata *) gmd;
+grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *gmd DEBUG_ARGS) {
+ internal_metadata *md = (internal_metadata *)gmd;
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "ELM REF:%p:%d->%d: '%s' = '%s'", md, gpr_atm_no_barrier_load (&md->refcnt), gpr_atm_no_barrier_load (&md->refcnt) + 1, grpc_mdstr_as_c_string ((grpc_mdstr *) md->key), grpc_mdstr_as_c_string ((grpc_mdstr *) md->value));
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "ELM REF:%p:%d->%d: '%s' = '%s'", md,
+ gpr_atm_no_barrier_load(&md->refcnt),
+ gpr_atm_no_barrier_load(&md->refcnt) + 1,
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
#endif
/* we can assume the ref count is >= 1 as the application is calling
this function - meaning that no adjustment to mdtab_free is necessary,
simplifying the logic here to be just an atomic increment */
/* use C assert to have this removed in opt builds */
- assert (gpr_atm_no_barrier_load (&md->refcnt) >= 1);
- gpr_atm_no_barrier_fetch_add (&md->refcnt, 1);
+ assert(gpr_atm_no_barrier_load(&md->refcnt) >= 1);
+ gpr_atm_no_barrier_fetch_add(&md->refcnt, 1);
return gmd;
}
-void
-grpc_mdelem_unref (grpc_mdelem * gmd DEBUG_ARGS)
-{
- internal_metadata *md = (internal_metadata *) gmd;
+void grpc_mdelem_unref(grpc_mdelem *gmd DEBUG_ARGS) {
+ internal_metadata *md = (internal_metadata *)gmd;
grpc_mdctx *ctx = md->context;
- lock (ctx);
+ lock(ctx);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "ELM UNREF:%p:%d->%d: '%s' = '%s'", md, gpr_atm_no_barrier_load (&md->refcnt), gpr_atm_no_barrier_load (&md->refcnt) - 1, grpc_mdstr_as_c_string ((grpc_mdstr *) md->key), grpc_mdstr_as_c_string ((grpc_mdstr *) md->value));
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
+ gpr_atm_no_barrier_load(&md->refcnt),
+ gpr_atm_no_barrier_load(&md->refcnt) - 1,
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
#endif
- assert (gpr_atm_no_barrier_load (&md->refcnt) >= 1);
- if (1 == gpr_atm_full_fetch_add (&md->refcnt, -1))
- {
- ctx->mdtab_free++;
- }
- unlock (ctx);
+ assert(gpr_atm_no_barrier_load(&md->refcnt) >= 1);
+ if (1 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
+ ctx->mdtab_free++;
+ }
+ unlock(ctx);
}
-const char *
-grpc_mdstr_as_c_string (grpc_mdstr * s)
-{
- return (const char *) GPR_SLICE_START_PTR (s->slice);
+const char *grpc_mdstr_as_c_string(grpc_mdstr *s) {
+ return (const char *)GPR_SLICE_START_PTR(s->slice);
}
-grpc_mdstr *
-grpc_mdstr_ref (grpc_mdstr * gs DEBUG_ARGS)
-{
- internal_string *s = (internal_string *) gs;
+grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *gs DEBUG_ARGS) {
+ internal_string *s = (internal_string *)gs;
grpc_mdctx *ctx = s->context;
- lock (ctx);
- internal_string_ref (s FWD_DEBUG_ARGS);
- unlock (ctx);
+ lock(ctx);
+ internal_string_ref(s FWD_DEBUG_ARGS);
+ unlock(ctx);
return gs;
}
-void
-grpc_mdstr_unref (grpc_mdstr * gs DEBUG_ARGS)
-{
- internal_string *s = (internal_string *) gs;
+void grpc_mdstr_unref(grpc_mdstr *gs DEBUG_ARGS) {
+ internal_string *s = (internal_string *)gs;
grpc_mdctx *ctx = s->context;
- lock (ctx);
- internal_string_unref (s FWD_DEBUG_ARGS);
- unlock (ctx);
+ lock(ctx);
+ internal_string_unref(s FWD_DEBUG_ARGS);
+ unlock(ctx);
}
-size_t
-grpc_mdctx_get_mdtab_capacity_test_only (grpc_mdctx * ctx)
-{
+size_t grpc_mdctx_get_mdtab_capacity_test_only(grpc_mdctx *ctx) {
return ctx->mdtab_capacity;
}
-size_t
-grpc_mdctx_get_mdtab_count_test_only (grpc_mdctx * ctx)
-{
+size_t grpc_mdctx_get_mdtab_count_test_only(grpc_mdctx *ctx) {
return ctx->mdtab_count;
}
-size_t
-grpc_mdctx_get_mdtab_free_test_only (grpc_mdctx * ctx)
-{
+size_t grpc_mdctx_get_mdtab_free_test_only(grpc_mdctx *ctx) {
return ctx->mdtab_free;
}
-void *
-grpc_mdelem_get_user_data (grpc_mdelem * md, void (*if_destroy_func) (void *))
-{
- internal_metadata *im = (internal_metadata *) md;
+void *grpc_mdelem_get_user_data(grpc_mdelem *md,
+ void (*if_destroy_func)(void *)) {
+ internal_metadata *im = (internal_metadata *)md;
void *result;
- gpr_mu_lock (&im->mu_user_data);
+ gpr_mu_lock(&im->mu_user_data);
result = im->destroy_user_data == if_destroy_func ? im->user_data : NULL;
- gpr_mu_unlock (&im->mu_user_data);
+ gpr_mu_unlock(&im->mu_user_data);
return result;
}
-void
-grpc_mdelem_set_user_data (grpc_mdelem * md, void (*destroy_func) (void *), void *user_data)
-{
- internal_metadata *im = (internal_metadata *) md;
- GPR_ASSERT ((user_data == NULL) == (destroy_func == NULL));
- gpr_mu_lock (&im->mu_user_data);
- if (im->destroy_user_data)
- {
- /* user data can only be set once */
- gpr_mu_unlock (&im->mu_user_data);
- if (destroy_func != NULL)
- {
- destroy_func (user_data);
- }
- return;
+void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
+ void *user_data) {
+ internal_metadata *im = (internal_metadata *)md;
+ GPR_ASSERT((user_data == NULL) == (destroy_func == NULL));
+ gpr_mu_lock(&im->mu_user_data);
+ if (im->destroy_user_data) {
+ /* user data can only be set once */
+ gpr_mu_unlock(&im->mu_user_data);
+ if (destroy_func != NULL) {
+ destroy_func(user_data);
}
+ return;
+ }
im->destroy_user_data = destroy_func;
im->user_data = user_data;
- gpr_mu_unlock (&im->mu_user_data);
+ gpr_mu_unlock(&im->mu_user_data);
}
-gpr_slice
-grpc_mdstr_as_base64_encoded_and_huffman_compressed (grpc_mdstr * gs)
-{
- internal_string *s = (internal_string *) gs;
+gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *gs) {
+ internal_string *s = (internal_string *)gs;
gpr_slice slice;
grpc_mdctx *ctx = s->context;
- lock (ctx);
- if (!s->has_base64_and_huffman_encoded)
- {
- s->base64_and_huffman = grpc_chttp2_base64_encode_and_huffman_compress (s->slice);
- s->has_base64_and_huffman_encoded = 1;
- }
+ lock(ctx);
+ if (!s->has_base64_and_huffman_encoded) {
+ s->base64_and_huffman =
+ grpc_chttp2_base64_encode_and_huffman_compress(s->slice);
+ s->has_base64_and_huffman_encoded = 1;
+ }
slice = s->base64_and_huffman;
- unlock (ctx);
+ unlock(ctx);
return slice;
}
-void
-grpc_mdctx_lock (grpc_mdctx * ctx)
-{
- lock (ctx);
-}
+void grpc_mdctx_lock(grpc_mdctx *ctx) { lock(ctx); }
-void
-grpc_mdctx_locked_mdelem_unref (grpc_mdctx * ctx, grpc_mdelem * gmd DEBUG_ARGS)
-{
- internal_metadata *md = (internal_metadata *) gmd;
+void grpc_mdctx_locked_mdelem_unref(grpc_mdctx *ctx,
+ grpc_mdelem *gmd DEBUG_ARGS) {
+ internal_metadata *md = (internal_metadata *)gmd;
grpc_mdctx *elem_ctx = md->context;
- GPR_ASSERT (ctx == elem_ctx);
+ GPR_ASSERT(ctx == elem_ctx);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- gpr_log (file, line, GPR_LOG_SEVERITY_DEBUG, "ELM UNREF:%p:%d->%d: '%s' = '%s'", md, gpr_atm_no_barrier_load (&md->refcnt), gpr_atm_no_barrier_load (&md->refcnt) - 1, grpc_mdstr_as_c_string ((grpc_mdstr *) md->key), grpc_mdstr_as_c_string ((grpc_mdstr *) md->value));
+ gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
+ "ELM UNREF:%p:%d->%d: '%s' = '%s'", md,
+ gpr_atm_no_barrier_load(&md->refcnt),
+ gpr_atm_no_barrier_load(&md->refcnt) - 1,
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->key),
+ grpc_mdstr_as_c_string((grpc_mdstr *)md->value));
#endif
- assert (gpr_atm_no_barrier_load (&md->refcnt) >= 1);
- if (1 == gpr_atm_full_fetch_add (&md->refcnt, -1))
- {
- ctx->mdtab_free++;
- }
-}
-
-void
-grpc_mdctx_unlock (grpc_mdctx * ctx)
-{
- unlock (ctx);
-}
-
-static int
-conforms_to (grpc_mdstr * s, const gpr_uint8 * legal_bits)
-{
- const gpr_uint8 *p = GPR_SLICE_START_PTR (s->slice);
- const gpr_uint8 *e = GPR_SLICE_END_PTR (s->slice);
- for (; p != e; p++)
- {
- int idx = *p;
- int byte = idx / 8;
- int bit = idx % 8;
- if ((legal_bits[byte] & (1 << bit)) == 0)
- return 0;
- }
+ assert(gpr_atm_no_barrier_load(&md->refcnt) >= 1);
+ if (1 == gpr_atm_full_fetch_add(&md->refcnt, -1)) {
+ ctx->mdtab_free++;
+ }
+}
+
+void grpc_mdctx_unlock(grpc_mdctx *ctx) { unlock(ctx); }
+
+static int conforms_to(grpc_mdstr *s, const gpr_uint8 *legal_bits) {
+ const gpr_uint8 *p = GPR_SLICE_START_PTR(s->slice);
+ const gpr_uint8 *e = GPR_SLICE_END_PTR(s->slice);
+ for (; p != e; p++) {
+ int idx = *p;
+ int byte = idx / 8;
+ int bit = idx % 8;
+ if ((legal_bits[byte] & (1 << bit)) == 0) return 0;
+ }
return 1;
}
-int
-grpc_mdstr_is_legal_header (grpc_mdstr * s)
-{
+int grpc_mdstr_is_legal_header(grpc_mdstr *s) {
static const gpr_uint8 legal_header_bits[256 / 8] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xff, 0x03, 0x00, 0x00, 0x00,
- 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- return conforms_to (s, legal_header_bits);
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xff, 0x03, 0x00, 0x00, 0x00,
+ 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ return conforms_to(s, legal_header_bits);
}
-int
-grpc_mdstr_is_legal_nonbin_header (grpc_mdstr * s)
-{
+int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s) {
static const gpr_uint8 legal_header_bits[256 / 8] = {
- 0x00, 0x00, 0x00, 0x00, 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- return conforms_to (s, legal_header_bits);
+ 0x00, 0x00, 0x00, 0x00, 0xff, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ return conforms_to(s, legal_header_bits);
}
-int
-grpc_mdstr_is_bin_suffixed (grpc_mdstr * s)
-{
+int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s) {
/* TODO(ctiller): consider caching this */
- return grpc_is_binary_header ((const char *) GPR_SLICE_START_PTR (s->slice), GPR_SLICE_LENGTH (s->slice));
+ return grpc_is_binary_header((const char *)GPR_SLICE_START_PTR(s->slice),
+ GPR_SLICE_LENGTH(s->slice));
}
diff --git a/src/core/transport/metadata.h b/src/core/transport/metadata.h
index 75d456eeb6..eb17747be7 100644
--- a/src/core/transport/metadata.h
+++ b/src/core/transport/metadata.h
@@ -67,8 +67,7 @@ typedef struct grpc_mdstr grpc_mdstr;
typedef struct grpc_mdelem grpc_mdelem;
/* if changing this, make identical changes in internal_string in metadata.c */
-struct grpc_mdstr
-{
+struct grpc_mdstr {
const gpr_slice slice;
const gpr_uint32 hash;
/* there is a private part to this in metadata.c */
@@ -76,48 +75,58 @@ struct grpc_mdstr
/* if changing this, make identical changes in internal_metadata in
metadata.c */
-struct grpc_mdelem
-{
+struct grpc_mdelem {
grpc_mdstr *const key;
grpc_mdstr *const value;
/* there is a private part to this in metadata.c */
};
/* Create/orphan a metadata context */
-grpc_mdctx *grpc_mdctx_create (void);
-grpc_mdctx *grpc_mdctx_create_with_seed (gpr_uint32 seed);
-void grpc_mdctx_ref (grpc_mdctx * mdctx);
-void grpc_mdctx_unref (grpc_mdctx * mdctx);
+grpc_mdctx *grpc_mdctx_create(void);
+grpc_mdctx *grpc_mdctx_create_with_seed(gpr_uint32 seed);
+void grpc_mdctx_ref(grpc_mdctx *mdctx);
+void grpc_mdctx_unref(grpc_mdctx *mdctx);
/* Test only accessors to internal state - only for testing this code - do not
rely on it outside of metadata_test.c */
-size_t grpc_mdctx_get_mdtab_capacity_test_only (grpc_mdctx * mdctx);
-size_t grpc_mdctx_get_mdtab_count_test_only (grpc_mdctx * mdctx);
-size_t grpc_mdctx_get_mdtab_free_test_only (grpc_mdctx * mdctx);
+size_t grpc_mdctx_get_mdtab_capacity_test_only(grpc_mdctx *mdctx);
+size_t grpc_mdctx_get_mdtab_count_test_only(grpc_mdctx *mdctx);
+size_t grpc_mdctx_get_mdtab_free_test_only(grpc_mdctx *mdctx);
/* Constructors for grpc_mdstr instances; take a variety of data types that
clients may have handy */
-grpc_mdstr *grpc_mdstr_from_string (grpc_mdctx * ctx, const char *str, int perform_key_canonicalization);
+grpc_mdstr *grpc_mdstr_from_string(grpc_mdctx *ctx, const char *str,
+ int perform_key_canonicalization);
/* Unrefs the slice. */
-grpc_mdstr *grpc_mdstr_from_slice (grpc_mdctx * ctx, gpr_slice slice);
-grpc_mdstr *grpc_mdstr_from_buffer (grpc_mdctx * ctx, const gpr_uint8 * str, size_t length);
+grpc_mdstr *grpc_mdstr_from_slice(grpc_mdctx *ctx, gpr_slice slice);
+grpc_mdstr *grpc_mdstr_from_buffer(grpc_mdctx *ctx, const gpr_uint8 *str,
+ size_t length);
/* Returns a borrowed slice from the mdstr with its contents base64 encoded
and huffman compressed */
-gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed (grpc_mdstr * str);
+gpr_slice grpc_mdstr_as_base64_encoded_and_huffman_compressed(grpc_mdstr *str);
/* Constructors for grpc_mdelem instances; take a variety of data types that
clients may have handy */
-grpc_mdelem *grpc_mdelem_from_metadata_strings (grpc_mdctx * ctx, grpc_mdstr * key, grpc_mdstr * value);
-grpc_mdelem *grpc_mdelem_from_strings (grpc_mdctx * ctx, const char *key, const char *value);
+grpc_mdelem *grpc_mdelem_from_metadata_strings(grpc_mdctx *ctx, grpc_mdstr *key,
+ grpc_mdstr *value);
+grpc_mdelem *grpc_mdelem_from_strings(grpc_mdctx *ctx, const char *key,
+ const char *value);
/* Unrefs the slices. */
-grpc_mdelem *grpc_mdelem_from_slices (grpc_mdctx * ctx, gpr_slice key, gpr_slice value);
-grpc_mdelem *grpc_mdelem_from_string_and_buffer (grpc_mdctx * ctx, const char *key, const gpr_uint8 * value, size_t value_length, int canonicalize_key);
+grpc_mdelem *grpc_mdelem_from_slices(grpc_mdctx *ctx, gpr_slice key,
+ gpr_slice value);
+grpc_mdelem *grpc_mdelem_from_string_and_buffer(grpc_mdctx *ctx,
+ const char *key,
+ const gpr_uint8 *value,
+ size_t value_length,
+ int canonicalize_key);
/* Mutator and accessor for grpc_mdelem user data. The destructor function
is used as a type tag and is checked during user_data fetch. */
-void *grpc_mdelem_get_user_data (grpc_mdelem * md, void (*if_destroy_func) (void *));
-void grpc_mdelem_set_user_data (grpc_mdelem * md, void (*destroy_func) (void *), void *user_data);
+void *grpc_mdelem_get_user_data(grpc_mdelem *md,
+ void (*if_destroy_func)(void *));
+void grpc_mdelem_set_user_data(grpc_mdelem *md, void (*destroy_func)(void *),
+ void *user_data);
/* Reference counting */
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
@@ -125,28 +134,28 @@ void grpc_mdelem_set_user_data (grpc_mdelem * md, void (*destroy_func) (void *),
#define GRPC_MDSTR_UNREF(s) grpc_mdstr_unref((s), __FILE__, __LINE__)
#define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s), __FILE__, __LINE__)
#define GRPC_MDELEM_UNREF(s) grpc_mdelem_unref((s), __FILE__, __LINE__)
-grpc_mdstr *grpc_mdstr_ref (grpc_mdstr * s, const char *file, int line);
-void grpc_mdstr_unref (grpc_mdstr * s, const char *file, int line);
-grpc_mdelem *grpc_mdelem_ref (grpc_mdelem * md, const char *file, int line);
-void grpc_mdelem_unref (grpc_mdelem * md, const char *file, int line);
+grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s, const char *file, int line);
+void grpc_mdstr_unref(grpc_mdstr *s, const char *file, int line);
+grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md, const char *file, int line);
+void grpc_mdelem_unref(grpc_mdelem *md, const char *file, int line);
#else
#define GRPC_MDSTR_REF(s) grpc_mdstr_ref((s))
#define GRPC_MDSTR_UNREF(s) grpc_mdstr_unref((s))
#define GRPC_MDELEM_REF(s) grpc_mdelem_ref((s))
#define GRPC_MDELEM_UNREF(s) grpc_mdelem_unref((s))
-grpc_mdstr *grpc_mdstr_ref (grpc_mdstr * s);
-void grpc_mdstr_unref (grpc_mdstr * s);
-grpc_mdelem *grpc_mdelem_ref (grpc_mdelem * md);
-void grpc_mdelem_unref (grpc_mdelem * md);
+grpc_mdstr *grpc_mdstr_ref(grpc_mdstr *s);
+void grpc_mdstr_unref(grpc_mdstr *s);
+grpc_mdelem *grpc_mdelem_ref(grpc_mdelem *md);
+void grpc_mdelem_unref(grpc_mdelem *md);
#endif
/* Recover a char* from a grpc_mdstr. The returned string is null terminated.
Does not promise that the returned string has no embedded nulls however. */
-const char *grpc_mdstr_as_c_string (grpc_mdstr * s);
+const char *grpc_mdstr_as_c_string(grpc_mdstr *s);
-int grpc_mdstr_is_legal_header (grpc_mdstr * s);
-int grpc_mdstr_is_legal_nonbin_header (grpc_mdstr * s);
-int grpc_mdstr_is_bin_suffixed (grpc_mdstr * s);
+int grpc_mdstr_is_legal_header(grpc_mdstr *s);
+int grpc_mdstr_is_legal_nonbin_header(grpc_mdstr *s);
+int grpc_mdstr_is_bin_suffixed(grpc_mdstr *s);
/* Batch mode metadata functions.
These API's have equivalents above, but allow taking the mdctx just once,
@@ -154,20 +163,21 @@ int grpc_mdstr_is_bin_suffixed (grpc_mdstr * s);
/* Lock the metadata context: it's only safe to call _locked_ functions against
this context from the calling thread until grpc_mdctx_unlock is called */
-void grpc_mdctx_lock (grpc_mdctx * ctx);
+void grpc_mdctx_lock(grpc_mdctx *ctx);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
#define GRPC_MDCTX_LOCKED_MDELEM_UNREF(ctx, elem) \
grpc_mdctx_locked_mdelem_unref((ctx), (elem), __FILE__, __LINE__)
/* Unref a metadata element */
-void grpc_mdctx_locked_mdelem_unref (grpc_mdctx * ctx, grpc_mdelem * elem, const char *file, int line);
+void grpc_mdctx_locked_mdelem_unref(grpc_mdctx *ctx, grpc_mdelem *elem,
+ const char *file, int line);
#else
#define GRPC_MDCTX_LOCKED_MDELEM_UNREF(ctx, elem) \
grpc_mdctx_locked_mdelem_unref((ctx), (elem))
/* Unref a metadata element */
-void grpc_mdctx_locked_mdelem_unref (grpc_mdctx * ctx, grpc_mdelem * elem);
+void grpc_mdctx_locked_mdelem_unref(grpc_mdctx *ctx, grpc_mdelem *elem);
#endif
/* Unlock the metadata context */
-void grpc_mdctx_unlock (grpc_mdctx * ctx);
+void grpc_mdctx_unlock(grpc_mdctx *ctx);
#define GRPC_MDSTR_KV_HASH(k_hash, v_hash) (GPR_ROTL((k_hash), 2) ^ (v_hash))
diff --git a/src/core/transport/stream_op.c b/src/core/transport/stream_op.c
index f188c7d1ae..038586d48e 100644
--- a/src/core/transport/stream_op.c
+++ b/src/core/transport/stream_op.c
@@ -42,367 +42,290 @@
Currently we grow by 1.5 times upon reallocation. */
#define GROW(x) (3 * (x) / 2)
-void
-grpc_sopb_init (grpc_stream_op_buffer * sopb)
-{
+void grpc_sopb_init(grpc_stream_op_buffer *sopb) {
sopb->ops = sopb->inlined_ops;
sopb->nops = 0;
sopb->capacity = GRPC_SOPB_INLINE_ELEMENTS;
}
-void
-grpc_sopb_destroy (grpc_stream_op_buffer * sopb)
-{
- grpc_stream_ops_unref_owned_objects (sopb->ops, sopb->nops);
- if (sopb->ops != sopb->inlined_ops)
- gpr_free (sopb->ops);
+void grpc_sopb_destroy(grpc_stream_op_buffer *sopb) {
+ grpc_stream_ops_unref_owned_objects(sopb->ops, sopb->nops);
+ if (sopb->ops != sopb->inlined_ops) gpr_free(sopb->ops);
}
-void
-grpc_sopb_reset (grpc_stream_op_buffer * sopb)
-{
- grpc_stream_ops_unref_owned_objects (sopb->ops, sopb->nops);
+void grpc_sopb_reset(grpc_stream_op_buffer *sopb) {
+ grpc_stream_ops_unref_owned_objects(sopb->ops, sopb->nops);
sopb->nops = 0;
}
-void
-grpc_sopb_swap (grpc_stream_op_buffer * a, grpc_stream_op_buffer * b)
-{
- GPR_SWAP (size_t, a->nops, b->nops);
- GPR_SWAP (size_t, a->capacity, b->capacity);
-
- if (a->ops == a->inlined_ops)
- {
- if (b->ops == b->inlined_ops)
- {
- /* swap contents of inlined buffer */
- grpc_stream_op temp[GRPC_SOPB_INLINE_ELEMENTS];
- memcpy (temp, a->ops, b->nops * sizeof (grpc_stream_op));
- memcpy (a->ops, b->ops, a->nops * sizeof (grpc_stream_op));
- memcpy (b->ops, temp, b->nops * sizeof (grpc_stream_op));
- }
- else
- {
- /* a is inlined, b is not - copy a inlined into b, fix pointers */
- a->ops = b->ops;
- b->ops = b->inlined_ops;
- memcpy (b->ops, a->inlined_ops, b->nops * sizeof (grpc_stream_op));
- }
- }
- else if (b->ops == b->inlined_ops)
- {
- /* b is inlined, a is not - copy b inlined int a, fix pointers */
- b->ops = a->ops;
- a->ops = a->inlined_ops;
- memcpy (a->ops, b->inlined_ops, a->nops * sizeof (grpc_stream_op));
- }
- else
- {
- /* no inlining: easy swap */
- GPR_SWAP (grpc_stream_op *, a->ops, b->ops);
+void grpc_sopb_swap(grpc_stream_op_buffer *a, grpc_stream_op_buffer *b) {
+ GPR_SWAP(size_t, a->nops, b->nops);
+ GPR_SWAP(size_t, a->capacity, b->capacity);
+
+ if (a->ops == a->inlined_ops) {
+ if (b->ops == b->inlined_ops) {
+ /* swap contents of inlined buffer */
+ grpc_stream_op temp[GRPC_SOPB_INLINE_ELEMENTS];
+ memcpy(temp, a->ops, b->nops * sizeof(grpc_stream_op));
+ memcpy(a->ops, b->ops, a->nops * sizeof(grpc_stream_op));
+ memcpy(b->ops, temp, b->nops * sizeof(grpc_stream_op));
+ } else {
+ /* a is inlined, b is not - copy a inlined into b, fix pointers */
+ a->ops = b->ops;
+ b->ops = b->inlined_ops;
+ memcpy(b->ops, a->inlined_ops, b->nops * sizeof(grpc_stream_op));
}
+ } else if (b->ops == b->inlined_ops) {
+ /* b is inlined, a is not - copy b inlined int a, fix pointers */
+ b->ops = a->ops;
+ a->ops = a->inlined_ops;
+ memcpy(a->ops, b->inlined_ops, a->nops * sizeof(grpc_stream_op));
+ } else {
+ /* no inlining: easy swap */
+ GPR_SWAP(grpc_stream_op *, a->ops, b->ops);
+ }
}
-void
-grpc_stream_ops_unref_owned_objects (grpc_stream_op * ops, size_t nops)
-{
+void grpc_stream_ops_unref_owned_objects(grpc_stream_op *ops, size_t nops) {
size_t i;
- for (i = 0; i < nops; i++)
- {
- switch (ops[i].type)
- {
- case GRPC_OP_SLICE:
- gpr_slice_unref (ops[i].data.slice);
- break;
- case GRPC_OP_METADATA:
- grpc_metadata_batch_destroy (&ops[i].data.metadata);
- break;
- case GRPC_NO_OP:
- case GRPC_OP_BEGIN_MESSAGE:
- break;
- }
+ for (i = 0; i < nops; i++) {
+ switch (ops[i].type) {
+ case GRPC_OP_SLICE:
+ gpr_slice_unref(ops[i].data.slice);
+ break;
+ case GRPC_OP_METADATA:
+ grpc_metadata_batch_destroy(&ops[i].data.metadata);
+ break;
+ case GRPC_NO_OP:
+ case GRPC_OP_BEGIN_MESSAGE:
+ break;
}
+ }
}
-static void
-expandto (grpc_stream_op_buffer * sopb, size_t new_capacity)
-{
+static void expandto(grpc_stream_op_buffer *sopb, size_t new_capacity) {
sopb->capacity = new_capacity;
- if (sopb->ops == sopb->inlined_ops)
- {
- sopb->ops = gpr_malloc (sizeof (grpc_stream_op) * new_capacity);
- memcpy (sopb->ops, sopb->inlined_ops, sopb->nops * sizeof (grpc_stream_op));
- }
- else
- {
- sopb->ops = gpr_realloc (sopb->ops, sizeof (grpc_stream_op) * new_capacity);
- }
+ if (sopb->ops == sopb->inlined_ops) {
+ sopb->ops = gpr_malloc(sizeof(grpc_stream_op) * new_capacity);
+ memcpy(sopb->ops, sopb->inlined_ops, sopb->nops * sizeof(grpc_stream_op));
+ } else {
+ sopb->ops = gpr_realloc(sopb->ops, sizeof(grpc_stream_op) * new_capacity);
+ }
}
-static grpc_stream_op *
-add (grpc_stream_op_buffer * sopb)
-{
+static grpc_stream_op *add(grpc_stream_op_buffer *sopb) {
grpc_stream_op *out;
- GPR_ASSERT (sopb->nops <= sopb->capacity);
- if (sopb->nops == sopb->capacity)
- {
- expandto (sopb, GROW (sopb->capacity));
- }
+ GPR_ASSERT(sopb->nops <= sopb->capacity);
+ if (sopb->nops == sopb->capacity) {
+ expandto(sopb, GROW(sopb->capacity));
+ }
out = sopb->ops + sopb->nops;
sopb->nops++;
return out;
}
-void
-grpc_sopb_add_no_op (grpc_stream_op_buffer * sopb)
-{
- add (sopb)->type = GRPC_NO_OP;
+void grpc_sopb_add_no_op(grpc_stream_op_buffer *sopb) {
+ add(sopb)->type = GRPC_NO_OP;
}
-void
-grpc_sopb_add_begin_message (grpc_stream_op_buffer * sopb, gpr_uint32 length, gpr_uint32 flags)
-{
- grpc_stream_op *op = add (sopb);
+void grpc_sopb_add_begin_message(grpc_stream_op_buffer *sopb, gpr_uint32 length,
+ gpr_uint32 flags) {
+ grpc_stream_op *op = add(sopb);
op->type = GRPC_OP_BEGIN_MESSAGE;
op->data.begin_message.length = length;
op->data.begin_message.flags = flags;
}
-void
-grpc_sopb_add_metadata (grpc_stream_op_buffer * sopb, grpc_metadata_batch b)
-{
- grpc_stream_op *op = add (sopb);
+void grpc_sopb_add_metadata(grpc_stream_op_buffer *sopb,
+ grpc_metadata_batch b) {
+ grpc_stream_op *op = add(sopb);
op->type = GRPC_OP_METADATA;
op->data.metadata = b;
}
-void
-grpc_sopb_add_slice (grpc_stream_op_buffer * sopb, gpr_slice slice)
-{
- grpc_stream_op *op = add (sopb);
+void grpc_sopb_add_slice(grpc_stream_op_buffer *sopb, gpr_slice slice) {
+ grpc_stream_op *op = add(sopb);
op->type = GRPC_OP_SLICE;
op->data.slice = slice;
}
-void
-grpc_sopb_append (grpc_stream_op_buffer * sopb, grpc_stream_op * ops, size_t nops)
-{
+void grpc_sopb_append(grpc_stream_op_buffer *sopb, grpc_stream_op *ops,
+ size_t nops) {
size_t orig_nops = sopb->nops;
size_t new_nops = orig_nops + nops;
- if (new_nops > sopb->capacity)
- {
- expandto (sopb, GPR_MAX (GROW (sopb->capacity), new_nops));
- }
+ if (new_nops > sopb->capacity) {
+ expandto(sopb, GPR_MAX(GROW(sopb->capacity), new_nops));
+ }
- memcpy (sopb->ops + orig_nops, ops, sizeof (grpc_stream_op) * nops);
+ memcpy(sopb->ops + orig_nops, ops, sizeof(grpc_stream_op) * nops);
sopb->nops = new_nops;
}
-void
-grpc_sopb_move_to (grpc_stream_op_buffer * src, grpc_stream_op_buffer * dst)
-{
- if (src->nops == 0)
- {
- return;
- }
- if (dst->nops == 0)
- {
- grpc_sopb_swap (src, dst);
- return;
- }
- grpc_sopb_append (dst, src->ops, src->nops);
+void grpc_sopb_move_to(grpc_stream_op_buffer *src, grpc_stream_op_buffer *dst) {
+ if (src->nops == 0) {
+ return;
+ }
+ if (dst->nops == 0) {
+ grpc_sopb_swap(src, dst);
+ return;
+ }
+ grpc_sopb_append(dst, src->ops, src->nops);
src->nops = 0;
}
-static void
-assert_valid_list (grpc_mdelem_list * list)
-{
+static void assert_valid_list(grpc_mdelem_list *list) {
#ifndef NDEBUG
grpc_linked_mdelem *l;
- GPR_ASSERT ((list->head == NULL) == (list->tail == NULL));
- if (!list->head)
- return;
- GPR_ASSERT (list->head->prev == NULL);
- GPR_ASSERT (list->tail->next == NULL);
- GPR_ASSERT ((list->head == list->tail) == (list->head->next == NULL));
-
- for (l = list->head; l; l = l->next)
- {
- GPR_ASSERT (l->md);
- GPR_ASSERT ((l->prev == NULL) == (l == list->head));
- GPR_ASSERT ((l->next == NULL) == (l == list->tail));
- if (l->next)
- GPR_ASSERT (l->next->prev == l);
- if (l->prev)
- GPR_ASSERT (l->prev->next == l);
- }
+ GPR_ASSERT((list->head == NULL) == (list->tail == NULL));
+ if (!list->head) return;
+ GPR_ASSERT(list->head->prev == NULL);
+ GPR_ASSERT(list->tail->next == NULL);
+ GPR_ASSERT((list->head == list->tail) == (list->head->next == NULL));
+
+ for (l = list->head; l; l = l->next) {
+ GPR_ASSERT(l->md);
+ GPR_ASSERT((l->prev == NULL) == (l == list->head));
+ GPR_ASSERT((l->next == NULL) == (l == list->tail));
+ if (l->next) GPR_ASSERT(l->next->prev == l);
+ if (l->prev) GPR_ASSERT(l->prev->next == l);
+ }
#endif /* NDEBUG */
}
#ifndef NDEBUG
-void
-grpc_metadata_batch_assert_ok (grpc_metadata_batch * batch)
-{
- assert_valid_list (&batch->list);
- assert_valid_list (&batch->garbage);
+void grpc_metadata_batch_assert_ok(grpc_metadata_batch *batch) {
+ assert_valid_list(&batch->list);
+ assert_valid_list(&batch->garbage);
}
#endif /* NDEBUG */
-void
-grpc_metadata_batch_init (grpc_metadata_batch * batch)
-{
- batch->list.head = batch->list.tail = batch->garbage.head = batch->garbage.tail = NULL;
- batch->deadline = gpr_inf_future (GPR_CLOCK_REALTIME);
+void grpc_metadata_batch_init(grpc_metadata_batch *batch) {
+ batch->list.head = batch->list.tail = batch->garbage.head =
+ batch->garbage.tail = NULL;
+ batch->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
}
-void
-grpc_metadata_batch_destroy (grpc_metadata_batch * batch)
-{
+void grpc_metadata_batch_destroy(grpc_metadata_batch *batch) {
grpc_linked_mdelem *l;
- for (l = batch->list.head; l; l = l->next)
- {
- GRPC_MDELEM_UNREF (l->md);
- }
- for (l = batch->garbage.head; l; l = l->next)
- {
- GRPC_MDELEM_UNREF (l->md);
- }
+ for (l = batch->list.head; l; l = l->next) {
+ GRPC_MDELEM_UNREF(l->md);
+ }
+ for (l = batch->garbage.head; l; l = l->next) {
+ GRPC_MDELEM_UNREF(l->md);
+ }
}
-void
-grpc_metadata_batch_add_head (grpc_metadata_batch * batch, grpc_linked_mdelem * storage, grpc_mdelem * elem_to_add)
-{
- GPR_ASSERT (elem_to_add);
+void grpc_metadata_batch_add_head(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage,
+ grpc_mdelem *elem_to_add) {
+ GPR_ASSERT(elem_to_add);
storage->md = elem_to_add;
- grpc_metadata_batch_link_head (batch, storage);
+ grpc_metadata_batch_link_head(batch, storage);
}
-static void
-link_head (grpc_mdelem_list * list, grpc_linked_mdelem * storage)
-{
- assert_valid_list (list);
- GPR_ASSERT (storage->md);
+static void link_head(grpc_mdelem_list *list, grpc_linked_mdelem *storage) {
+ assert_valid_list(list);
+ GPR_ASSERT(storage->md);
storage->prev = NULL;
storage->next = list->head;
- if (list->head != NULL)
- {
- list->head->prev = storage;
- }
- else
- {
- list->tail = storage;
- }
+ if (list->head != NULL) {
+ list->head->prev = storage;
+ } else {
+ list->tail = storage;
+ }
list->head = storage;
- assert_valid_list (list);
+ assert_valid_list(list);
}
-void
-grpc_metadata_batch_link_head (grpc_metadata_batch * batch, grpc_linked_mdelem * storage)
-{
- link_head (&batch->list, storage);
+void grpc_metadata_batch_link_head(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage) {
+ link_head(&batch->list, storage);
}
-void
-grpc_metadata_batch_add_tail (grpc_metadata_batch * batch, grpc_linked_mdelem * storage, grpc_mdelem * elem_to_add)
-{
- GPR_ASSERT (elem_to_add);
+void grpc_metadata_batch_add_tail(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage,
+ grpc_mdelem *elem_to_add) {
+ GPR_ASSERT(elem_to_add);
storage->md = elem_to_add;
- grpc_metadata_batch_link_tail (batch, storage);
+ grpc_metadata_batch_link_tail(batch, storage);
}
-static void
-link_tail (grpc_mdelem_list * list, grpc_linked_mdelem * storage)
-{
- assert_valid_list (list);
- GPR_ASSERT (storage->md);
+static void link_tail(grpc_mdelem_list *list, grpc_linked_mdelem *storage) {
+ assert_valid_list(list);
+ GPR_ASSERT(storage->md);
storage->prev = list->tail;
storage->next = NULL;
storage->reserved = NULL;
- if (list->tail != NULL)
- {
- list->tail->next = storage;
- }
- else
- {
- list->head = storage;
- }
+ if (list->tail != NULL) {
+ list->tail->next = storage;
+ } else {
+ list->head = storage;
+ }
list->tail = storage;
- assert_valid_list (list);
+ assert_valid_list(list);
}
-void
-grpc_metadata_batch_link_tail (grpc_metadata_batch * batch, grpc_linked_mdelem * storage)
-{
- link_tail (&batch->list, storage);
+void grpc_metadata_batch_link_tail(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage) {
+ link_tail(&batch->list, storage);
}
-void
-grpc_metadata_batch_merge (grpc_metadata_batch * target, grpc_metadata_batch * add)
-{
+void grpc_metadata_batch_merge(grpc_metadata_batch *target,
+ grpc_metadata_batch *add) {
grpc_linked_mdelem *l;
grpc_linked_mdelem *next;
- for (l = add->list.head; l; l = next)
- {
- next = l->next;
- link_tail (&target->list, l);
- }
- for (l = add->garbage.head; l; l = next)
- {
- next = l->next;
- link_tail (&target->garbage, l);
- }
+ for (l = add->list.head; l; l = next) {
+ next = l->next;
+ link_tail(&target->list, l);
+ }
+ for (l = add->garbage.head; l; l = next) {
+ next = l->next;
+ link_tail(&target->garbage, l);
+ }
}
-void
-grpc_metadata_batch_move (grpc_metadata_batch * dst, grpc_metadata_batch * src)
-{
+void grpc_metadata_batch_move(grpc_metadata_batch *dst,
+ grpc_metadata_batch *src) {
*dst = *src;
- memset (src, 0, sizeof (grpc_metadata_batch));
+ memset(src, 0, sizeof(grpc_metadata_batch));
}
-void
-grpc_metadata_batch_filter (grpc_metadata_batch * batch, grpc_mdelem * (*filter) (void *user_data, grpc_mdelem * elem), void *user_data)
-{
+void grpc_metadata_batch_filter(grpc_metadata_batch *batch,
+ grpc_mdelem *(*filter)(void *user_data,
+ grpc_mdelem *elem),
+ void *user_data) {
grpc_linked_mdelem *l;
grpc_linked_mdelem *next;
- assert_valid_list (&batch->list);
- assert_valid_list (&batch->garbage);
- for (l = batch->list.head; l; l = next)
- {
- grpc_mdelem *orig = l->md;
- grpc_mdelem *filt = filter (user_data, orig);
- next = l->next;
- if (filt == NULL)
- {
- if (l->prev)
- {
- l->prev->next = l->next;
- }
- if (l->next)
- {
- l->next->prev = l->prev;
- }
- if (batch->list.head == l)
- {
- batch->list.head = l->next;
- }
- if (batch->list.tail == l)
- {
- batch->list.tail = l->prev;
- }
- assert_valid_list (&batch->list);
- link_head (&batch->garbage, l);
- }
- else if (filt != orig)
- {
- GRPC_MDELEM_UNREF (orig);
- l->md = filt;
- }
+ assert_valid_list(&batch->list);
+ assert_valid_list(&batch->garbage);
+ for (l = batch->list.head; l; l = next) {
+ grpc_mdelem *orig = l->md;
+ grpc_mdelem *filt = filter(user_data, orig);
+ next = l->next;
+ if (filt == NULL) {
+ if (l->prev) {
+ l->prev->next = l->next;
+ }
+ if (l->next) {
+ l->next->prev = l->prev;
+ }
+ if (batch->list.head == l) {
+ batch->list.head = l->next;
+ }
+ if (batch->list.tail == l) {
+ batch->list.tail = l->prev;
+ }
+ assert_valid_list(&batch->list);
+ link_head(&batch->garbage, l);
+ } else if (filt != orig) {
+ GRPC_MDELEM_UNREF(orig);
+ l->md = filt;
}
- assert_valid_list (&batch->list);
- assert_valid_list (&batch->garbage);
+ }
+ assert_valid_list(&batch->list);
+ assert_valid_list(&batch->garbage);
}
diff --git a/src/core/transport/stream_op.h b/src/core/transport/stream_op.h
index e37ab09f71..37f18b02d9 100644
--- a/src/core/transport/stream_op.h
+++ b/src/core/transport/stream_op.h
@@ -45,8 +45,7 @@
/* Operations that can be performed on a stream.
Used by grpc_stream_op. */
-typedef enum grpc_stream_op_code
-{
+typedef enum grpc_stream_op_code {
/* Do nothing code. Useful if rewriting a batch to exclude some operations.
Must be ignored by receivers */
GRPC_NO_OP,
@@ -66,8 +65,7 @@ typedef enum grpc_stream_op_code
#define GRPC_WRITE_INTERNAL_USED_MASK (GRPC_WRITE_INTERNAL_COMPRESS)
/* Arguments for GRPC_OP_BEGIN_MESSAGE */
-typedef struct grpc_begin_message
-{
+typedef struct grpc_begin_message {
/* How many bytes of data will this message contain */
gpr_uint32 length;
/* Write flags for the message: see grpc.h GRPC_WRITE_* for the public bits,
@@ -75,22 +73,19 @@ typedef struct grpc_begin_message
gpr_uint32 flags;
} grpc_begin_message;
-typedef struct grpc_linked_mdelem
-{
+typedef struct grpc_linked_mdelem {
grpc_mdelem *md;
struct grpc_linked_mdelem *next;
struct grpc_linked_mdelem *prev;
void *reserved;
} grpc_linked_mdelem;
-typedef struct grpc_mdelem_list
-{
+typedef struct grpc_mdelem_list {
grpc_linked_mdelem *head;
grpc_linked_mdelem *tail;
} grpc_mdelem_list;
-typedef struct grpc_metadata_batch
-{
+typedef struct grpc_metadata_batch {
/** Metadata elements in this batch */
grpc_mdelem_list list;
/** Elements that have been removed from the batch, but have
@@ -103,26 +98,30 @@ typedef struct grpc_metadata_batch
gpr_timespec deadline;
} grpc_metadata_batch;
-void grpc_metadata_batch_init (grpc_metadata_batch * batch);
-void grpc_metadata_batch_destroy (grpc_metadata_batch * batch);
-void grpc_metadata_batch_merge (grpc_metadata_batch * target, grpc_metadata_batch * add);
+void grpc_metadata_batch_init(grpc_metadata_batch *batch);
+void grpc_metadata_batch_destroy(grpc_metadata_batch *batch);
+void grpc_metadata_batch_merge(grpc_metadata_batch *target,
+ grpc_metadata_batch *add);
/** Moves the metadata information from \a src to \a dst. Upon return, \a src is
* zeroed. */
-void grpc_metadata_batch_move (grpc_metadata_batch * dst, grpc_metadata_batch * src);
+void grpc_metadata_batch_move(grpc_metadata_batch *dst,
+ grpc_metadata_batch *src);
/** Add \a storage to the beginning of \a batch. storage->md is
assumed to be valid.
\a storage is owned by the caller and must survive for the
lifetime of batch. This usually means it should be around
for the lifetime of the call. */
-void grpc_metadata_batch_link_head (grpc_metadata_batch * batch, grpc_linked_mdelem * storage);
+void grpc_metadata_batch_link_head(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage);
/** Add \a storage to the end of \a batch. storage->md is
assumed to be valid.
\a storage is owned by the caller and must survive for the
lifetime of batch. This usually means it should be around
for the lifetime of the call. */
-void grpc_metadata_batch_link_tail (grpc_metadata_batch * batch, grpc_linked_mdelem * storage);
+void grpc_metadata_batch_link_tail(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage);
/** Add \a elem_to_add as the first element in \a batch, using
\a storage as backing storage for the linked list element.
@@ -130,23 +129,30 @@ void grpc_metadata_batch_link_tail (grpc_metadata_batch * batch, grpc_linked_mde
lifetime of batch. This usually means it should be around
for the lifetime of the call.
Takes ownership of \a elem_to_add */
-void grpc_metadata_batch_add_head (grpc_metadata_batch * batch, grpc_linked_mdelem * storage, grpc_mdelem * elem_to_add);
+void grpc_metadata_batch_add_head(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage,
+ grpc_mdelem *elem_to_add);
/** Add \a elem_to_add as the last element in \a batch, using
\a storage as backing storage for the linked list element.
\a storage is owned by the caller and must survive for the
lifetime of batch. This usually means it should be around
for the lifetime of the call.
Takes ownership of \a elem_to_add */
-void grpc_metadata_batch_add_tail (grpc_metadata_batch * batch, grpc_linked_mdelem * storage, grpc_mdelem * elem_to_add);
+void grpc_metadata_batch_add_tail(grpc_metadata_batch *batch,
+ grpc_linked_mdelem *storage,
+ grpc_mdelem *elem_to_add);
/** For each element in \a batch, execute \a filter.
The return value from \a filter will be substituted for the
grpc_mdelem passed to \a filter. If \a filter returns NULL,
the element will be moved to the garbage list. */
-void grpc_metadata_batch_filter (grpc_metadata_batch * batch, grpc_mdelem * (*filter) (void *user_data, grpc_mdelem * elem), void *user_data);
+void grpc_metadata_batch_filter(grpc_metadata_batch *batch,
+ grpc_mdelem *(*filter)(void *user_data,
+ grpc_mdelem *elem),
+ void *user_data);
#ifndef NDEBUG
-void grpc_metadata_batch_assert_ok (grpc_metadata_batch * comd);
+void grpc_metadata_batch_assert_ok(grpc_metadata_batch *comd);
#else
#define grpc_metadata_batch_assert_ok(comd) \
do { \
@@ -154,14 +160,12 @@ void grpc_metadata_batch_assert_ok (grpc_metadata_batch * comd);
#endif
/* Represents a single operation performed on a stream/transport */
-typedef struct grpc_stream_op
-{
+typedef struct grpc_stream_op {
/* the operation to be applied */
enum grpc_stream_op_code type;
/* the arguments to this operation. union fields are named according to the
associated op-code */
- union
- {
+ union {
grpc_begin_message begin_message;
grpc_metadata_batch metadata;
gpr_slice slice;
@@ -170,8 +174,7 @@ typedef struct grpc_stream_op
/** A stream op buffer is a wrapper around stream operations that is
* dynamically extendable. */
-typedef struct grpc_stream_op_buffer
-{
+typedef struct grpc_stream_op_buffer {
grpc_stream_op *ops;
size_t nops;
size_t capacity;
@@ -179,28 +182,31 @@ typedef struct grpc_stream_op_buffer
} grpc_stream_op_buffer;
/* Initialize a stream op buffer */
-void grpc_sopb_init (grpc_stream_op_buffer * sopb);
+void grpc_sopb_init(grpc_stream_op_buffer *sopb);
/* Destroy a stream op buffer */
-void grpc_sopb_destroy (grpc_stream_op_buffer * sopb);
+void grpc_sopb_destroy(grpc_stream_op_buffer *sopb);
/* Reset a sopb to no elements */
-void grpc_sopb_reset (grpc_stream_op_buffer * sopb);
+void grpc_sopb_reset(grpc_stream_op_buffer *sopb);
/* Swap two sopbs */
-void grpc_sopb_swap (grpc_stream_op_buffer * a, grpc_stream_op_buffer * b);
+void grpc_sopb_swap(grpc_stream_op_buffer *a, grpc_stream_op_buffer *b);
-void grpc_stream_ops_unref_owned_objects (grpc_stream_op * ops, size_t nops);
+void grpc_stream_ops_unref_owned_objects(grpc_stream_op *ops, size_t nops);
/* Append a GRPC_NO_OP to a buffer */
-void grpc_sopb_add_no_op (grpc_stream_op_buffer * sopb);
+void grpc_sopb_add_no_op(grpc_stream_op_buffer *sopb);
/* Append a GRPC_OP_BEGIN to a buffer */
-void grpc_sopb_add_begin_message (grpc_stream_op_buffer * sopb, gpr_uint32 length, gpr_uint32 flags);
-void grpc_sopb_add_metadata (grpc_stream_op_buffer * sopb, grpc_metadata_batch metadata);
+void grpc_sopb_add_begin_message(grpc_stream_op_buffer *sopb, gpr_uint32 length,
+ gpr_uint32 flags);
+void grpc_sopb_add_metadata(grpc_stream_op_buffer *sopb,
+ grpc_metadata_batch metadata);
/* Append a GRPC_SLICE to a buffer - does not ref/unref the slice */
-void grpc_sopb_add_slice (grpc_stream_op_buffer * sopb, gpr_slice slice);
+void grpc_sopb_add_slice(grpc_stream_op_buffer *sopb, gpr_slice slice);
/* Append a buffer to a buffer - does not ref/unref any internal objects */
-void grpc_sopb_append (grpc_stream_op_buffer * sopb, grpc_stream_op * ops, size_t nops);
+void grpc_sopb_append(grpc_stream_op_buffer *sopb, grpc_stream_op *ops,
+ size_t nops);
-void grpc_sopb_move_to (grpc_stream_op_buffer * src, grpc_stream_op_buffer * dst);
+void grpc_sopb_move_to(grpc_stream_op_buffer *src, grpc_stream_op_buffer *dst);
-char *grpc_sopb_string (grpc_stream_op_buffer * sopb);
+char *grpc_sopb_string(grpc_stream_op_buffer *sopb);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_STREAM_OP_H */
diff --git a/src/core/transport/transport.c b/src/core/transport/transport.c
index 35ba0ba067..828d212cfe 100644
--- a/src/core/transport/transport.c
+++ b/src/core/transport/transport.c
@@ -36,115 +36,103 @@
#include <grpc/support/log.h>
#include "src/core/transport/transport_impl.h"
-size_t
-grpc_transport_stream_size (grpc_transport * transport)
-{
+size_t grpc_transport_stream_size(grpc_transport *transport) {
return transport->vtable->sizeof_stream;
}
-void
-grpc_transport_destroy (grpc_exec_ctx * exec_ctx, grpc_transport * transport)
-{
- transport->vtable->destroy (exec_ctx, transport);
+void grpc_transport_destroy(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport) {
+ transport->vtable->destroy(exec_ctx, transport);
}
-int
-grpc_transport_init_stream (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream, const void *server_data, grpc_transport_stream_op * initial_op)
-{
- return transport->vtable->init_stream (exec_ctx, transport, stream, server_data, initial_op);
+int grpc_transport_init_stream(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport, grpc_stream *stream,
+ const void *server_data,
+ grpc_transport_stream_op *initial_op) {
+ return transport->vtable->init_stream(exec_ctx, transport, stream,
+ server_data, initial_op);
}
-void
-grpc_transport_perform_stream_op (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream, grpc_transport_stream_op * op)
-{
- transport->vtable->perform_stream_op (exec_ctx, transport, stream, op);
+void grpc_transport_perform_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ grpc_stream *stream,
+ grpc_transport_stream_op *op) {
+ transport->vtable->perform_stream_op(exec_ctx, transport, stream, op);
}
-void
-grpc_transport_perform_op (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_transport_op * op)
-{
- transport->vtable->perform_op (exec_ctx, transport, op);
+void grpc_transport_perform_op(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ grpc_transport_op *op) {
+ transport->vtable->perform_op(exec_ctx, transport, op);
}
-void
-grpc_transport_destroy_stream (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream)
-{
- transport->vtable->destroy_stream (exec_ctx, transport, stream);
+void grpc_transport_destroy_stream(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ grpc_stream *stream) {
+ transport->vtable->destroy_stream(exec_ctx, transport, stream);
}
-char *
-grpc_transport_get_peer (grpc_exec_ctx * exec_ctx, grpc_transport * transport)
-{
- return transport->vtable->get_peer (exec_ctx, transport);
+char *grpc_transport_get_peer(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport) {
+ return transport->vtable->get_peer(exec_ctx, transport);
}
-void
-grpc_transport_stream_op_finish_with_failure (grpc_exec_ctx * exec_ctx, grpc_transport_stream_op * op)
-{
- grpc_exec_ctx_enqueue (exec_ctx, op->on_done_recv, 0);
- grpc_exec_ctx_enqueue (exec_ctx, op->on_done_send, 0);
- grpc_exec_ctx_enqueue (exec_ctx, op->on_consumed, 0);
+void grpc_transport_stream_op_finish_with_failure(
+ grpc_exec_ctx *exec_ctx, grpc_transport_stream_op *op) {
+ grpc_exec_ctx_enqueue(exec_ctx, op->on_done_recv, 0);
+ grpc_exec_ctx_enqueue(exec_ctx, op->on_done_send, 0);
+ grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, 0);
}
-void
-grpc_transport_stream_op_add_cancellation (grpc_transport_stream_op * op, grpc_status_code status)
-{
- GPR_ASSERT (status != GRPC_STATUS_OK);
- if (op->cancel_with_status == GRPC_STATUS_OK)
- {
- op->cancel_with_status = status;
- }
- if (op->close_with_status != GRPC_STATUS_OK)
- {
- op->close_with_status = GRPC_STATUS_OK;
- if (op->optional_close_message != NULL)
- {
- gpr_slice_unref (*op->optional_close_message);
- op->optional_close_message = NULL;
- }
+void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op,
+ grpc_status_code status) {
+ GPR_ASSERT(status != GRPC_STATUS_OK);
+ if (op->cancel_with_status == GRPC_STATUS_OK) {
+ op->cancel_with_status = status;
+ }
+ if (op->close_with_status != GRPC_STATUS_OK) {
+ op->close_with_status = GRPC_STATUS_OK;
+ if (op->optional_close_message != NULL) {
+ gpr_slice_unref(*op->optional_close_message);
+ op->optional_close_message = NULL;
}
+ }
}
-typedef struct
-{
+typedef struct {
gpr_slice message;
grpc_closure *then_call;
grpc_closure closure;
} close_message_data;
-static void
-free_message (grpc_exec_ctx * exec_ctx, void *p, int iomgr_success)
-{
+static void free_message(grpc_exec_ctx *exec_ctx, void *p, int iomgr_success) {
close_message_data *cmd = p;
- gpr_slice_unref (cmd->message);
- if (cmd->then_call != NULL)
- {
- cmd->then_call->cb (exec_ctx, cmd->then_call->cb_arg, iomgr_success);
- }
- gpr_free (cmd);
+ gpr_slice_unref(cmd->message);
+ if (cmd->then_call != NULL) {
+ cmd->then_call->cb(exec_ctx, cmd->then_call->cb_arg, iomgr_success);
+ }
+ gpr_free(cmd);
}
-void
-grpc_transport_stream_op_add_close (grpc_transport_stream_op * op, grpc_status_code status, gpr_slice * optional_message)
-{
+void grpc_transport_stream_op_add_close(grpc_transport_stream_op *op,
+ grpc_status_code status,
+ gpr_slice *optional_message) {
close_message_data *cmd;
- GPR_ASSERT (status != GRPC_STATUS_OK);
- if (op->cancel_with_status != GRPC_STATUS_OK || op->close_with_status != GRPC_STATUS_OK)
- {
- if (optional_message)
- {
- gpr_slice_unref (*optional_message);
- }
- return;
- }
- if (optional_message)
- {
- cmd = gpr_malloc (sizeof (*cmd));
- cmd->message = *optional_message;
- cmd->then_call = op->on_consumed;
- grpc_closure_init (&cmd->closure, free_message, cmd);
- op->on_consumed = &cmd->closure;
- op->optional_close_message = &cmd->message;
+ GPR_ASSERT(status != GRPC_STATUS_OK);
+ if (op->cancel_with_status != GRPC_STATUS_OK ||
+ op->close_with_status != GRPC_STATUS_OK) {
+ if (optional_message) {
+ gpr_slice_unref(*optional_message);
}
+ return;
+ }
+ if (optional_message) {
+ cmd = gpr_malloc(sizeof(*cmd));
+ cmd->message = *optional_message;
+ cmd->then_call = op->on_consumed;
+ grpc_closure_init(&cmd->closure, free_message, cmd);
+ op->on_consumed = &cmd->closure;
+ op->optional_close_message = &cmd->message;
+ }
op->close_with_status = status;
}
diff --git a/src/core/transport/transport.h b/src/core/transport/transport.h
index 80d842b4be..d4cee03862 100644
--- a/src/core/transport/transport.h
+++ b/src/core/transport/transport.h
@@ -50,8 +50,7 @@ typedef struct grpc_transport grpc_transport;
typedef struct grpc_stream grpc_stream;
/* Represents the send/recv closed state of a stream. */
-typedef enum grpc_stream_state
-{
+typedef enum grpc_stream_state {
/* the stream is open for sends and receives */
GRPC_STREAM_OPEN,
/* the stream is closed for sends, but may still receive data */
@@ -64,8 +63,7 @@ typedef enum grpc_stream_state
/* Transport stream op: a set of operations to perform on a transport
against a single stream */
-typedef struct grpc_transport_stream_op
-{
+typedef struct grpc_transport_stream_op {
grpc_closure *on_consumed;
grpc_stream_op_buffer *send_ops;
@@ -95,8 +93,7 @@ typedef struct grpc_transport_stream_op
} grpc_transport_stream_op;
/** Transport op: a set of operations to perform on a transport as a whole */
-typedef struct grpc_transport_op
-{
+typedef struct grpc_transport_op {
/** called when processing of this op is done */
grpc_closure *on_consumed;
/** connectivity monitoring */
@@ -113,7 +110,8 @@ typedef struct grpc_transport_op
gpr_slice *goaway_message;
/** set the callback for accepting new streams;
this is a permanent callback, unlike the other one-shot closures */
- void (*set_accept_stream) (void *user_data, grpc_transport * transport, const void *server_data);
+ void (*set_accept_stream)(void *user_data, grpc_transport *transport,
+ const void *server_data);
void *set_accept_stream_user_data;
/** add this transport to a pollset */
grpc_pollset *bind_pollset;
@@ -125,7 +123,7 @@ typedef struct grpc_transport_op
/* Returns the amount of memory required to store a grpc_stream for this
transport */
-size_t grpc_transport_stream_size (grpc_transport * transport);
+size_t grpc_transport_stream_size(grpc_transport *transport);
/* Initialize transport data for a stream.
@@ -136,7 +134,10 @@ size_t grpc_transport_stream_size (grpc_transport * transport);
stream - a pointer to uninitialized memory to initialize
server_data - either NULL for a client initiated stream, or a pointer
supplied from the accept_stream callback function */
-int grpc_transport_init_stream (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream, const void *server_data, grpc_transport_stream_op * initial_op);
+int grpc_transport_init_stream(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport, grpc_stream *stream,
+ const void *server_data,
+ grpc_transport_stream_op *initial_op);
/* Destroy transport data for a stream.
@@ -148,15 +149,21 @@ int grpc_transport_init_stream (grpc_exec_ctx * exec_ctx, grpc_transport * trans
transport - the transport on which to create this stream
stream - the grpc_stream to destroy (memory is still owned by the
caller, but any child memory must be cleaned up) */
-void grpc_transport_destroy_stream (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream);
+void grpc_transport_destroy_stream(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ grpc_stream *stream);
-void grpc_transport_stream_op_finish_with_failure (grpc_exec_ctx * exec_ctx, grpc_transport_stream_op * op);
+void grpc_transport_stream_op_finish_with_failure(grpc_exec_ctx *exec_ctx,
+ grpc_transport_stream_op *op);
-void grpc_transport_stream_op_add_cancellation (grpc_transport_stream_op * op, grpc_status_code status);
+void grpc_transport_stream_op_add_cancellation(grpc_transport_stream_op *op,
+ grpc_status_code status);
-void grpc_transport_stream_op_add_close (grpc_transport_stream_op * op, grpc_status_code status, gpr_slice * optional_message);
+void grpc_transport_stream_op_add_close(grpc_transport_stream_op *op,
+ grpc_status_code status,
+ gpr_slice *optional_message);
-char *grpc_transport_stream_op_string (grpc_transport_stream_op * op);
+char *grpc_transport_stream_op_string(grpc_transport_stream_op *op);
/* Send a batch of operations on a transport
@@ -167,25 +174,32 @@ char *grpc_transport_stream_op_string (grpc_transport_stream_op * op);
stream - the stream on which to send the operations. This must be
non-NULL and previously initialized by the same transport.
op - a grpc_transport_stream_op specifying the op to perform */
-void grpc_transport_perform_stream_op (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_stream * stream, grpc_transport_stream_op * op);
+void grpc_transport_perform_stream_op(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ grpc_stream *stream,
+ grpc_transport_stream_op *op);
-void grpc_transport_perform_op (grpc_exec_ctx * exec_ctx, grpc_transport * transport, grpc_transport_op * op);
+void grpc_transport_perform_op(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport,
+ grpc_transport_op *op);
/* Send a ping on a transport
Calls cb with user data when a response is received. */
-void grpc_transport_ping (grpc_transport * transport, grpc_closure * cb);
+void grpc_transport_ping(grpc_transport *transport, grpc_closure *cb);
/* Advise peer of pending connection termination. */
-void grpc_transport_goaway (grpc_transport * transport, grpc_status_code status, gpr_slice debug_data);
+void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
+ gpr_slice debug_data);
/* Close a transport. Aborts all open streams. */
-void grpc_transport_close (grpc_transport * transport);
+void grpc_transport_close(grpc_transport *transport);
/* Destroy the transport */
-void grpc_transport_destroy (grpc_exec_ctx * exec_ctx, grpc_transport * transport);
+void grpc_transport_destroy(grpc_exec_ctx *exec_ctx, grpc_transport *transport);
/* Get the transports peer */
-char *grpc_transport_get_peer (grpc_exec_ctx * exec_ctx, grpc_transport * transport);
+char *grpc_transport_get_peer(grpc_exec_ctx *exec_ctx,
+ grpc_transport *transport);
#endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */
diff --git a/src/core/transport/transport_impl.h b/src/core/transport/transport_impl.h
index 54ebc24ff3..900c6340ff 100644
--- a/src/core/transport/transport_impl.h
+++ b/src/core/transport/transport_impl.h
@@ -36,34 +36,37 @@
#include "src/core/transport/transport.h"
-typedef struct grpc_transport_vtable
-{
+typedef struct grpc_transport_vtable {
/* Memory required for a single stream element - this is allocated by upper
layers and initialized by the transport */
- size_t sizeof_stream; /* = sizeof(transport stream) */
+ size_t sizeof_stream; /* = sizeof(transport stream) */
/* implementation of grpc_transport_init_stream */
- int (*init_stream) (grpc_exec_ctx * exec_ctx, grpc_transport * self, grpc_stream * stream, const void *server_data, grpc_transport_stream_op * initial_op);
+ int (*init_stream)(grpc_exec_ctx *exec_ctx, grpc_transport *self,
+ grpc_stream *stream, const void *server_data,
+ grpc_transport_stream_op *initial_op);
/* implementation of grpc_transport_perform_stream_op */
- void (*perform_stream_op) (grpc_exec_ctx * exec_ctx, grpc_transport * self, grpc_stream * stream, grpc_transport_stream_op * op);
+ void (*perform_stream_op)(grpc_exec_ctx *exec_ctx, grpc_transport *self,
+ grpc_stream *stream, grpc_transport_stream_op *op);
/* implementation of grpc_transport_perform_op */
- void (*perform_op) (grpc_exec_ctx * exec_ctx, grpc_transport * self, grpc_transport_op * op);
+ void (*perform_op)(grpc_exec_ctx *exec_ctx, grpc_transport *self,
+ grpc_transport_op *op);
/* implementation of grpc_transport_destroy_stream */
- void (*destroy_stream) (grpc_exec_ctx * exec_ctx, grpc_transport * self, grpc_stream * stream);
+ void (*destroy_stream)(grpc_exec_ctx *exec_ctx, grpc_transport *self,
+ grpc_stream *stream);
/* implementation of grpc_transport_destroy */
- void (*destroy) (grpc_exec_ctx * exec_ctx, grpc_transport * self);
+ void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_transport *self);
/* implementation of grpc_transport_get_peer */
- char *(*get_peer) (grpc_exec_ctx * exec_ctx, grpc_transport * self);
+ char *(*get_peer)(grpc_exec_ctx *exec_ctx, grpc_transport *self);
} grpc_transport_vtable;
/* an instance of a grpc transport */
-struct grpc_transport
-{
+struct grpc_transport {
/* pointer to a vtable defining operations on this transport */
const grpc_transport_vtable *vtable;
};
diff --git a/src/core/transport/transport_op_string.c b/src/core/transport/transport_op_string.c
index f963413310..f62c340e97 100644
--- a/src/core/transport/transport_op_string.c
+++ b/src/core/transport/transport_op_string.c
@@ -45,142 +45,122 @@
/* These routines are here to facilitate debugging - they produce string
representations of various transport data structures */
-static void
-put_metadata (gpr_strvec * b, grpc_mdelem * md)
-{
- gpr_strvec_add (b, gpr_strdup ("key="));
- gpr_strvec_add (b, gpr_dump_slice (md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
-
- gpr_strvec_add (b, gpr_strdup (" value="));
- gpr_strvec_add (b, gpr_dump_slice (md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
+static void put_metadata(gpr_strvec *b, grpc_mdelem *md) {
+ gpr_strvec_add(b, gpr_strdup("key="));
+ gpr_strvec_add(b,
+ gpr_dump_slice(md->key->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
+
+ gpr_strvec_add(b, gpr_strdup(" value="));
+ gpr_strvec_add(
+ b, gpr_dump_slice(md->value->slice, GPR_DUMP_HEX | GPR_DUMP_ASCII));
}
-static void
-put_metadata_list (gpr_strvec * b, grpc_metadata_batch md)
-{
+static void put_metadata_list(gpr_strvec *b, grpc_metadata_batch md) {
grpc_linked_mdelem *m;
- for (m = md.list.head; m != NULL; m = m->next)
- {
- if (m != md.list.head)
- gpr_strvec_add (b, gpr_strdup (", "));
- put_metadata (b, m->md);
- }
- if (gpr_time_cmp (md.deadline, gpr_inf_future (md.deadline.clock_type)) != 0)
- {
- char *tmp;
- gpr_asprintf (&tmp, " deadline=%d.%09d", md.deadline.tv_sec, md.deadline.tv_nsec);
- gpr_strvec_add (b, tmp);
- }
+ for (m = md.list.head; m != NULL; m = m->next) {
+ if (m != md.list.head) gpr_strvec_add(b, gpr_strdup(", "));
+ put_metadata(b, m->md);
+ }
+ if (gpr_time_cmp(md.deadline, gpr_inf_future(md.deadline.clock_type)) != 0) {
+ char *tmp;
+ gpr_asprintf(&tmp, " deadline=%d.%09d", md.deadline.tv_sec,
+ md.deadline.tv_nsec);
+ gpr_strvec_add(b, tmp);
+ }
}
-char *
-grpc_sopb_string (grpc_stream_op_buffer * sopb)
-{
+char *grpc_sopb_string(grpc_stream_op_buffer *sopb) {
char *out;
char *tmp;
size_t i;
gpr_strvec b;
- gpr_strvec_init (&b);
-
- for (i = 0; i < sopb->nops; i++)
- {
- grpc_stream_op *op = &sopb->ops[i];
- if (i > 0)
- gpr_strvec_add (&b, gpr_strdup (", "));
- switch (op->type)
- {
- case GRPC_NO_OP:
- gpr_strvec_add (&b, gpr_strdup ("NO_OP"));
- break;
- case GRPC_OP_BEGIN_MESSAGE:
- gpr_asprintf (&tmp, "BEGIN_MESSAGE:%d", op->data.begin_message.length);
- gpr_strvec_add (&b, tmp);
- break;
- case GRPC_OP_SLICE:
- gpr_asprintf (&tmp, "SLICE:%d", GPR_SLICE_LENGTH (op->data.slice));
- gpr_strvec_add (&b, tmp);
- break;
- case GRPC_OP_METADATA:
- gpr_strvec_add (&b, gpr_strdup ("METADATA{"));
- put_metadata_list (&b, op->data.metadata);
- gpr_strvec_add (&b, gpr_strdup ("}"));
- break;
- }
+ gpr_strvec_init(&b);
+
+ for (i = 0; i < sopb->nops; i++) {
+ grpc_stream_op *op = &sopb->ops[i];
+ if (i > 0) gpr_strvec_add(&b, gpr_strdup(", "));
+ switch (op->type) {
+ case GRPC_NO_OP:
+ gpr_strvec_add(&b, gpr_strdup("NO_OP"));
+ break;
+ case GRPC_OP_BEGIN_MESSAGE:
+ gpr_asprintf(&tmp, "BEGIN_MESSAGE:%d", op->data.begin_message.length);
+ gpr_strvec_add(&b, tmp);
+ break;
+ case GRPC_OP_SLICE:
+ gpr_asprintf(&tmp, "SLICE:%d", GPR_SLICE_LENGTH(op->data.slice));
+ gpr_strvec_add(&b, tmp);
+ break;
+ case GRPC_OP_METADATA:
+ gpr_strvec_add(&b, gpr_strdup("METADATA{"));
+ put_metadata_list(&b, op->data.metadata);
+ gpr_strvec_add(&b, gpr_strdup("}"));
+ break;
}
+ }
- out = gpr_strvec_flatten (&b, NULL);
- gpr_strvec_destroy (&b);
+ out = gpr_strvec_flatten(&b, NULL);
+ gpr_strvec_destroy(&b);
return out;
}
-char *
-grpc_transport_stream_op_string (grpc_transport_stream_op * op)
-{
+char *grpc_transport_stream_op_string(grpc_transport_stream_op *op) {
char *tmp;
char *out;
int first = 1;
gpr_strvec b;
- gpr_strvec_init (&b);
-
- if (op->send_ops)
- {
- if (!first)
- gpr_strvec_add (&b, gpr_strdup (" "));
- first = 0;
- gpr_asprintf (&tmp, "SEND%s:%p", op->is_last_send ? "_LAST" : "", op->on_done_send);
- gpr_strvec_add (&b, tmp);
- gpr_strvec_add (&b, gpr_strdup ("["));
- gpr_strvec_add (&b, grpc_sopb_string (op->send_ops));
- gpr_strvec_add (&b, gpr_strdup ("]"));
- }
-
- if (op->recv_ops)
- {
- if (!first)
- gpr_strvec_add (&b, gpr_strdup (" "));
- first = 0;
- gpr_asprintf (&tmp, "RECV:%p:max_recv_bytes=%d", op->on_done_recv, op->max_recv_bytes);
- gpr_strvec_add (&b, tmp);
- }
-
- if (op->bind_pollset)
- {
- if (!first)
- gpr_strvec_add (&b, gpr_strdup (" "));
- first = 0;
- gpr_strvec_add (&b, gpr_strdup ("BIND"));
- }
-
- if (op->cancel_with_status != GRPC_STATUS_OK)
- {
- if (!first)
- gpr_strvec_add (&b, gpr_strdup (" "));
- first = 0;
- gpr_asprintf (&tmp, "CANCEL:%d", op->cancel_with_status);
- gpr_strvec_add (&b, tmp);
- }
-
- if (op->on_consumed != NULL)
- {
- if (!first)
- gpr_strvec_add (&b, gpr_strdup (" "));
- first = 0;
- gpr_asprintf (&tmp, "ON_CONSUMED:%p", op->on_consumed);
- gpr_strvec_add (&b, tmp);
- }
-
- out = gpr_strvec_flatten (&b, NULL);
- gpr_strvec_destroy (&b);
+ gpr_strvec_init(&b);
+
+ if (op->send_ops) {
+ if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
+ first = 0;
+ gpr_asprintf(&tmp, "SEND%s:%p", op->is_last_send ? "_LAST" : "",
+ op->on_done_send);
+ gpr_strvec_add(&b, tmp);
+ gpr_strvec_add(&b, gpr_strdup("["));
+ gpr_strvec_add(&b, grpc_sopb_string(op->send_ops));
+ gpr_strvec_add(&b, gpr_strdup("]"));
+ }
+
+ if (op->recv_ops) {
+ if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
+ first = 0;
+ gpr_asprintf(&tmp, "RECV:%p:max_recv_bytes=%d", op->on_done_recv,
+ op->max_recv_bytes);
+ gpr_strvec_add(&b, tmp);
+ }
+
+ if (op->bind_pollset) {
+ if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
+ first = 0;
+ gpr_strvec_add(&b, gpr_strdup("BIND"));
+ }
+
+ if (op->cancel_with_status != GRPC_STATUS_OK) {
+ if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
+ first = 0;
+ gpr_asprintf(&tmp, "CANCEL:%d", op->cancel_with_status);
+ gpr_strvec_add(&b, tmp);
+ }
+
+ if (op->on_consumed != NULL) {
+ if (!first) gpr_strvec_add(&b, gpr_strdup(" "));
+ first = 0;
+ gpr_asprintf(&tmp, "ON_CONSUMED:%p", op->on_consumed);
+ gpr_strvec_add(&b, tmp);
+ }
+
+ out = gpr_strvec_flatten(&b, NULL);
+ gpr_strvec_destroy(&b);
return out;
}
-void
-grpc_call_log_op (char *file, int line, gpr_log_severity severity, grpc_call_element * elem, grpc_transport_stream_op * op)
-{
- char *str = grpc_transport_stream_op_string (op);
- gpr_log (file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str);
- gpr_free (str);
+void grpc_call_log_op(char *file, int line, gpr_log_severity severity,
+ grpc_call_element *elem, grpc_transport_stream_op *op) {
+ char *str = grpc_transport_stream_op_string(op);
+ gpr_log(file, line, severity, "OP[%s:%p]: %s", elem->filter->name, elem, str);
+ gpr_free(str);
}
diff --git a/src/core/tsi/fake_transport_security.c b/src/core/tsi/fake_transport_security.c
index 3a8383d4b0..9ef4b163be 100644
--- a/src/core/tsi/fake_transport_security.c
+++ b/src/core/tsi/fake_transport_security.c
@@ -52,8 +52,7 @@
| size | data |
where the size field value is the size of the size field plus the size of
the data encoded in little endian on 4 bytes. */
-typedef struct
-{
+typedef struct {
unsigned char *data;
size_t size;
size_t allocated_size;
@@ -61,8 +60,7 @@ typedef struct
int needs_draining;
} tsi_fake_frame;
-typedef enum
-{
+typedef enum {
TSI_FAKE_CLIENT_INIT = 0,
TSI_FAKE_SERVER_INIT = 1,
TSI_FAKE_CLIENT_FINISHED = 2,
@@ -70,8 +68,7 @@ typedef enum
TSI_FAKE_HANDSHAKE_MESSAGE_MAX = 4
} tsi_fake_handshake_message;
-typedef struct
-{
+typedef struct {
tsi_handshaker base;
int is_client;
tsi_fake_handshake_message next_message_to_send;
@@ -81,8 +78,7 @@ typedef struct
tsi_result result;
} tsi_fake_handshaker;
-typedef struct
-{
+typedef struct {
tsi_frame_protector base;
tsi_fake_frame protect_frame;
tsi_fake_frame unprotect_frame;
@@ -92,187 +88,156 @@ typedef struct
/* --- Utils. ---*/
static const char *tsi_fake_handshake_message_strings[] = {
- "CLIENT_INIT", "SERVER_INIT", "CLIENT_FINISHED", "SERVER_FINISHED"
-};
+ "CLIENT_INIT", "SERVER_INIT", "CLIENT_FINISHED", "SERVER_FINISHED"};
-static const char *
-tsi_fake_handshake_message_to_string (int msg)
-{
- if (msg < 0 || msg >= TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- gpr_log (GPR_ERROR, "Invalid message %d", msg);
- return "UNKNOWN";
- }
+static const char *tsi_fake_handshake_message_to_string(int msg) {
+ if (msg < 0 || msg >= TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ gpr_log(GPR_ERROR, "Invalid message %d", msg);
+ return "UNKNOWN";
+ }
return tsi_fake_handshake_message_strings[msg];
}
-static tsi_result
-tsi_fake_handshake_message_from_string (const char *msg_string, tsi_fake_handshake_message * msg)
-{
+static tsi_result tsi_fake_handshake_message_from_string(
+ const char *msg_string, tsi_fake_handshake_message *msg) {
tsi_fake_handshake_message i;
- for (i = 0; i < TSI_FAKE_HANDSHAKE_MESSAGE_MAX; i++)
- {
- if (strncmp (msg_string, tsi_fake_handshake_message_strings[i], strlen (tsi_fake_handshake_message_strings[i])) == 0)
- {
- *msg = i;
- return TSI_OK;
- }
+ for (i = 0; i < TSI_FAKE_HANDSHAKE_MESSAGE_MAX; i++) {
+ if (strncmp(msg_string, tsi_fake_handshake_message_strings[i],
+ strlen(tsi_fake_handshake_message_strings[i])) == 0) {
+ *msg = i;
+ return TSI_OK;
}
- gpr_log (GPR_ERROR, "Invalid handshake message.");
+ }
+ gpr_log(GPR_ERROR, "Invalid handshake message.");
return TSI_DATA_CORRUPTED;
}
-static gpr_uint32
-load32_little_endian (const unsigned char *buf)
-{
- return ((gpr_uint32) (buf[0]) | (gpr_uint32) (buf[1] << 8) | (gpr_uint32) (buf[2] << 16) | (gpr_uint32) (buf[3] << 24));
+static gpr_uint32 load32_little_endian(const unsigned char *buf) {
+ return ((gpr_uint32)(buf[0]) | (gpr_uint32)(buf[1] << 8) |
+ (gpr_uint32)(buf[2] << 16) | (gpr_uint32)(buf[3] << 24));
}
-static void
-store32_little_endian (gpr_uint32 value, unsigned char *buf)
-{
- buf[3] = (unsigned char) (value >> 24) & 0xFF;
- buf[2] = (unsigned char) (value >> 16) & 0xFF;
- buf[1] = (unsigned char) (value >> 8) & 0xFF;
- buf[0] = (unsigned char) (value) & 0xFF;
+static void store32_little_endian(gpr_uint32 value, unsigned char *buf) {
+ buf[3] = (unsigned char)(value >> 24) & 0xFF;
+ buf[2] = (unsigned char)(value >> 16) & 0xFF;
+ buf[1] = (unsigned char)(value >> 8) & 0xFF;
+ buf[0] = (unsigned char)(value)&0xFF;
}
-static void
-tsi_fake_frame_reset (tsi_fake_frame * frame, int needs_draining)
-{
+static void tsi_fake_frame_reset(tsi_fake_frame *frame, int needs_draining) {
frame->offset = 0;
frame->needs_draining = needs_draining;
- if (!needs_draining)
- frame->size = 0;
+ if (!needs_draining) frame->size = 0;
}
/* Returns 1 if successful, 0 otherwise. */
-static int
-tsi_fake_frame_ensure_size (tsi_fake_frame * frame)
-{
- if (frame->data == NULL)
- {
- frame->allocated_size = frame->size;
- frame->data = malloc (frame->allocated_size);
- if (frame->data == NULL)
- return 0;
- }
- else if (frame->size > frame->allocated_size)
- {
- unsigned char *new_data = realloc (frame->data, frame->size);
- if (new_data == NULL)
- {
- free (frame->data);
- frame->data = NULL;
- return 0;
- }
- frame->data = new_data;
- frame->allocated_size = frame->size;
+static int tsi_fake_frame_ensure_size(tsi_fake_frame *frame) {
+ if (frame->data == NULL) {
+ frame->allocated_size = frame->size;
+ frame->data = malloc(frame->allocated_size);
+ if (frame->data == NULL) return 0;
+ } else if (frame->size > frame->allocated_size) {
+ unsigned char *new_data = realloc(frame->data, frame->size);
+ if (new_data == NULL) {
+ free(frame->data);
+ frame->data = NULL;
+ return 0;
}
+ frame->data = new_data;
+ frame->allocated_size = frame->size;
+ }
return 1;
}
/* This method should not be called if frame->needs_framing is not 0. */
-static tsi_result
-fill_frame_from_bytes (const unsigned char *incoming_bytes, size_t * incoming_bytes_size, tsi_fake_frame * frame)
-{
+static tsi_result fill_frame_from_bytes(const unsigned char *incoming_bytes,
+ size_t *incoming_bytes_size,
+ tsi_fake_frame *frame) {
size_t available_size = *incoming_bytes_size;
size_t to_read_size = 0;
const unsigned char *bytes_cursor = incoming_bytes;
- if (frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->data == NULL)
- {
- frame->allocated_size = TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE;
- frame->data = malloc (frame->allocated_size);
- if (frame->data == NULL)
- return TSI_OUT_OF_RESOURCES;
- }
-
- if (frame->offset < TSI_FAKE_FRAME_HEADER_SIZE)
- {
- to_read_size = TSI_FAKE_FRAME_HEADER_SIZE - frame->offset;
- if (to_read_size > available_size)
- {
- /* Just fill what we can and exit. */
- memcpy (frame->data + frame->offset, bytes_cursor, available_size);
- bytes_cursor += available_size;
- frame->offset += available_size;
- *incoming_bytes_size = (size_t) (bytes_cursor - incoming_bytes);
- return TSI_INCOMPLETE_DATA;
- }
- memcpy (frame->data + frame->offset, bytes_cursor, to_read_size);
- bytes_cursor += to_read_size;
- frame->offset += to_read_size;
- available_size -= to_read_size;
- frame->size = load32_little_endian (frame->data);
- if (!tsi_fake_frame_ensure_size (frame))
- return TSI_OUT_OF_RESOURCES;
- }
-
- to_read_size = frame->size - frame->offset;
- if (to_read_size > available_size)
- {
- memcpy (frame->data + frame->offset, bytes_cursor, available_size);
- frame->offset += available_size;
+ if (frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->data == NULL) {
+ frame->allocated_size = TSI_FAKE_FRAME_INITIAL_ALLOCATED_SIZE;
+ frame->data = malloc(frame->allocated_size);
+ if (frame->data == NULL) return TSI_OUT_OF_RESOURCES;
+ }
+
+ if (frame->offset < TSI_FAKE_FRAME_HEADER_SIZE) {
+ to_read_size = TSI_FAKE_FRAME_HEADER_SIZE - frame->offset;
+ if (to_read_size > available_size) {
+ /* Just fill what we can and exit. */
+ memcpy(frame->data + frame->offset, bytes_cursor, available_size);
bytes_cursor += available_size;
- *incoming_bytes_size = (size_t) (bytes_cursor - incoming_bytes);
+ frame->offset += available_size;
+ *incoming_bytes_size = (size_t)(bytes_cursor - incoming_bytes);
return TSI_INCOMPLETE_DATA;
}
- memcpy (frame->data + frame->offset, bytes_cursor, to_read_size);
+ memcpy(frame->data + frame->offset, bytes_cursor, to_read_size);
+ bytes_cursor += to_read_size;
+ frame->offset += to_read_size;
+ available_size -= to_read_size;
+ frame->size = load32_little_endian(frame->data);
+ if (!tsi_fake_frame_ensure_size(frame)) return TSI_OUT_OF_RESOURCES;
+ }
+
+ to_read_size = frame->size - frame->offset;
+ if (to_read_size > available_size) {
+ memcpy(frame->data + frame->offset, bytes_cursor, available_size);
+ frame->offset += available_size;
+ bytes_cursor += available_size;
+ *incoming_bytes_size = (size_t)(bytes_cursor - incoming_bytes);
+ return TSI_INCOMPLETE_DATA;
+ }
+ memcpy(frame->data + frame->offset, bytes_cursor, to_read_size);
bytes_cursor += to_read_size;
- *incoming_bytes_size = (size_t) (bytes_cursor - incoming_bytes);
- tsi_fake_frame_reset (frame, 1 /* needs_draining */ );
+ *incoming_bytes_size = (size_t)(bytes_cursor - incoming_bytes);
+ tsi_fake_frame_reset(frame, 1 /* needs_draining */);
return TSI_OK;
}
/* This method should not be called if frame->needs_framing is 0. */
-static tsi_result
-drain_frame_to_bytes (unsigned char *outgoing_bytes, size_t * outgoing_bytes_size, tsi_fake_frame * frame)
-{
+static tsi_result drain_frame_to_bytes(unsigned char *outgoing_bytes,
+ size_t *outgoing_bytes_size,
+ tsi_fake_frame *frame) {
size_t to_write_size = frame->size - frame->offset;
- if (!frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (*outgoing_bytes_size < to_write_size)
- {
- memcpy (outgoing_bytes, frame->data + frame->offset, *outgoing_bytes_size);
- frame->offset += *outgoing_bytes_size;
- return TSI_INCOMPLETE_DATA;
- }
- memcpy (outgoing_bytes, frame->data + frame->offset, to_write_size);
+ if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (*outgoing_bytes_size < to_write_size) {
+ memcpy(outgoing_bytes, frame->data + frame->offset, *outgoing_bytes_size);
+ frame->offset += *outgoing_bytes_size;
+ return TSI_INCOMPLETE_DATA;
+ }
+ memcpy(outgoing_bytes, frame->data + frame->offset, to_write_size);
*outgoing_bytes_size = to_write_size;
- tsi_fake_frame_reset (frame, 0 /* needs_draining */ );
+ tsi_fake_frame_reset(frame, 0 /* needs_draining */);
return TSI_OK;
}
-static tsi_result
-bytes_to_frame (unsigned char *bytes, size_t bytes_size, tsi_fake_frame * frame)
-{
+static tsi_result bytes_to_frame(unsigned char *bytes, size_t bytes_size,
+ tsi_fake_frame *frame) {
frame->offset = 0;
frame->size = bytes_size + TSI_FAKE_FRAME_HEADER_SIZE;
- if (!tsi_fake_frame_ensure_size (frame))
- return TSI_OUT_OF_RESOURCES;
- store32_little_endian ((gpr_uint32) frame->size, frame->data);
- memcpy (frame->data + TSI_FAKE_FRAME_HEADER_SIZE, bytes, bytes_size);
- tsi_fake_frame_reset (frame, 1 /* needs draining */ );
+ if (!tsi_fake_frame_ensure_size(frame)) return TSI_OUT_OF_RESOURCES;
+ store32_little_endian((gpr_uint32)frame->size, frame->data);
+ memcpy(frame->data + TSI_FAKE_FRAME_HEADER_SIZE, bytes, bytes_size);
+ tsi_fake_frame_reset(frame, 1 /* needs draining */);
return TSI_OK;
}
-static void
-tsi_fake_frame_destruct (tsi_fake_frame * frame)
-{
- if (frame->data != NULL)
- free (frame->data);
+static void tsi_fake_frame_destruct(tsi_fake_frame *frame) {
+ if (frame->data != NULL) free(frame->data);
}
/* --- tsi_frame_protector methods implementation. ---*/
-static tsi_result
-fake_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size)
-{
+static tsi_result fake_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size) {
tsi_result result = TSI_OK;
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
unsigned char frame_header[TSI_FAKE_FRAME_HEADER_SIZE];
tsi_fake_frame *frame = &impl->protect_frame;
size_t saved_output_size = *protected_output_frames_size;
@@ -281,86 +246,79 @@ fake_protector_protect (tsi_frame_protector * self, const unsigned char *unprote
*num_bytes_written = 0;
/* Try to drain first. */
- if (frame->needs_draining)
- {
- drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (protected_output_frames, &drained_size, frame);
- *num_bytes_written += drained_size;
- protected_output_frames += drained_size;
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- {
- *unprotected_bytes_size = 0;
- result = TSI_OK;
- }
- return result;
- }
+ if (frame->needs_draining) {
+ drained_size = saved_output_size - *num_bytes_written;
+ result =
+ drain_frame_to_bytes(protected_output_frames, &drained_size, frame);
+ *num_bytes_written += drained_size;
+ protected_output_frames += drained_size;
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) {
+ *unprotected_bytes_size = 0;
+ result = TSI_OK;
+ }
+ return result;
}
+ }
/* Now process the unprotected_bytes. */
- if (frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->size == 0)
- {
- /* New frame, create a header. */
- size_t written_in_frame_size = 0;
- store32_little_endian ((gpr_uint32) impl->max_frame_size, frame_header);
- written_in_frame_size = TSI_FAKE_FRAME_HEADER_SIZE;
- result = fill_frame_from_bytes (frame_header, &written_in_frame_size, frame);
- if (result != TSI_INCOMPLETE_DATA)
- {
- gpr_log (GPR_ERROR, "fill_frame_from_bytes returned %s", tsi_result_to_string (result));
- return result;
- }
- }
- result = fill_frame_from_bytes (unprotected_bytes, unprotected_bytes_size, frame);
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->size == 0) {
+ /* New frame, create a header. */
+ size_t written_in_frame_size = 0;
+ store32_little_endian((gpr_uint32)impl->max_frame_size, frame_header);
+ written_in_frame_size = TSI_FAKE_FRAME_HEADER_SIZE;
+ result = fill_frame_from_bytes(frame_header, &written_in_frame_size, frame);
+ if (result != TSI_INCOMPLETE_DATA) {
+ gpr_log(GPR_ERROR, "fill_frame_from_bytes returned %s",
+ tsi_result_to_string(result));
return result;
}
+ }
+ result =
+ fill_frame_from_bytes(unprotected_bytes, unprotected_bytes_size, frame);
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
+ return result;
+ }
/* Try to drain again. */
- if (!frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->offset != 0)
- return TSI_INTERNAL_ERROR;
+ if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->offset != 0) return TSI_INTERNAL_ERROR;
drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (protected_output_frames, &drained_size, frame);
+ result = drain_frame_to_bytes(protected_output_frames, &drained_size, frame);
*num_bytes_written += drained_size;
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
return result;
}
-static tsi_result
-fake_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size)
-{
+static tsi_result fake_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size) {
tsi_result result = TSI_OK;
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
tsi_fake_frame *frame = &impl->protect_frame;
- if (!frame->needs_draining)
- {
- /* Create a short frame. */
- frame->size = frame->offset;
- frame->offset = 0;
- frame->needs_draining = 1;
- store32_little_endian ((gpr_uint32) frame->size, frame->data); /* Overwrite header. */
- }
- result = drain_frame_to_bytes (protected_output_frames, protected_output_frames_size, frame);
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (!frame->needs_draining) {
+ /* Create a short frame. */
+ frame->size = frame->offset;
+ frame->offset = 0;
+ frame->needs_draining = 1;
+ store32_little_endian((gpr_uint32)frame->size,
+ frame->data); /* Overwrite header. */
+ }
+ result = drain_frame_to_bytes(protected_output_frames,
+ protected_output_frames_size, frame);
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
*still_pending_size = frame->size - frame->offset;
return result;
}
-static tsi_result
-fake_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
+static tsi_result fake_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
tsi_result result = TSI_OK;
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
tsi_fake_frame *frame = &impl->unprotect_frame;
size_t saved_output_size = *unprotected_bytes_size;
size_t drained_size = 0;
@@ -368,232 +326,202 @@ fake_protector_unprotect (tsi_frame_protector * self, const unsigned char *prote
*num_bytes_written = 0;
/* Try to drain first. */
- if (frame->needs_draining)
- {
- /* Go past the header if needed. */
- if (frame->offset == 0)
- frame->offset = TSI_FAKE_FRAME_HEADER_SIZE;
- drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (unprotected_bytes, &drained_size, frame);
- unprotected_bytes += drained_size;
- *num_bytes_written += drained_size;
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- {
- *protected_frames_bytes_size = 0;
- result = TSI_OK;
- }
- return result;
- }
+ if (frame->needs_draining) {
+ /* Go past the header if needed. */
+ if (frame->offset == 0) frame->offset = TSI_FAKE_FRAME_HEADER_SIZE;
+ drained_size = saved_output_size - *num_bytes_written;
+ result = drain_frame_to_bytes(unprotected_bytes, &drained_size, frame);
+ unprotected_bytes += drained_size;
+ *num_bytes_written += drained_size;
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) {
+ *protected_frames_bytes_size = 0;
+ result = TSI_OK;
+ }
+ return result;
}
+ }
/* Now process the protected_bytes. */
- if (frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- result = fill_frame_from_bytes (protected_frames_bytes, protected_frames_bytes_size, frame);
- if (result != TSI_OK)
- {
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
- return result;
- }
+ if (frame->needs_draining) return TSI_INTERNAL_ERROR;
+ result = fill_frame_from_bytes(protected_frames_bytes,
+ protected_frames_bytes_size, frame);
+ if (result != TSI_OK) {
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
+ return result;
+ }
/* Try to drain again. */
- if (!frame->needs_draining)
- return TSI_INTERNAL_ERROR;
- if (frame->offset != 0)
- return TSI_INTERNAL_ERROR;
- frame->offset = TSI_FAKE_FRAME_HEADER_SIZE; /* Go past the header. */
+ if (!frame->needs_draining) return TSI_INTERNAL_ERROR;
+ if (frame->offset != 0) return TSI_INTERNAL_ERROR;
+ frame->offset = TSI_FAKE_FRAME_HEADER_SIZE; /* Go past the header. */
drained_size = saved_output_size - *num_bytes_written;
- result = drain_frame_to_bytes (unprotected_bytes, &drained_size, frame);
+ result = drain_frame_to_bytes(unprotected_bytes, &drained_size, frame);
*num_bytes_written += drained_size;
- if (result == TSI_INCOMPLETE_DATA)
- result = TSI_OK;
+ if (result == TSI_INCOMPLETE_DATA) result = TSI_OK;
return result;
}
-static void
-fake_protector_destroy (tsi_frame_protector * self)
-{
- tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *) self;
- tsi_fake_frame_destruct (&impl->protect_frame);
- tsi_fake_frame_destruct (&impl->unprotect_frame);
- free (self);
+static void fake_protector_destroy(tsi_frame_protector *self) {
+ tsi_fake_frame_protector *impl = (tsi_fake_frame_protector *)self;
+ tsi_fake_frame_destruct(&impl->protect_frame);
+ tsi_fake_frame_destruct(&impl->unprotect_frame);
+ free(self);
}
static const tsi_frame_protector_vtable frame_protector_vtable = {
- fake_protector_protect, fake_protector_protect_flush,
- fake_protector_unprotect, fake_protector_destroy,
+ fake_protector_protect, fake_protector_protect_flush,
+ fake_protector_unprotect, fake_protector_destroy,
};
/* --- tsi_handshaker methods implementation. ---*/
-static tsi_result
-fake_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size)
-{
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
+static tsi_result fake_handshaker_get_bytes_to_send_to_peer(
+ tsi_handshaker *self, unsigned char *bytes, size_t *bytes_size) {
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
tsi_result result = TSI_OK;
- if (impl->needs_incoming_message || impl->result == TSI_OK)
- {
- *bytes_size = 0;
- return TSI_OK;
+ if (impl->needs_incoming_message || impl->result == TSI_OK) {
+ *bytes_size = 0;
+ return TSI_OK;
+ }
+ if (!impl->outgoing.needs_draining) {
+ tsi_fake_handshake_message next_message_to_send =
+ impl->next_message_to_send + 2;
+ const char *msg_string =
+ tsi_fake_handshake_message_to_string(impl->next_message_to_send);
+ result = bytes_to_frame((unsigned char *)msg_string, strlen(msg_string),
+ &impl->outgoing);
+ if (result != TSI_OK) return result;
+ if (next_message_to_send > TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ next_message_to_send = TSI_FAKE_HANDSHAKE_MESSAGE_MAX;
}
- if (!impl->outgoing.needs_draining)
- {
- tsi_fake_handshake_message next_message_to_send = impl->next_message_to_send + 2;
- const char *msg_string = tsi_fake_handshake_message_to_string (impl->next_message_to_send);
- result = bytes_to_frame ((unsigned char *) msg_string, strlen (msg_string), &impl->outgoing);
- if (result != TSI_OK)
- return result;
- if (next_message_to_send > TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- next_message_to_send = TSI_FAKE_HANDSHAKE_MESSAGE_MAX;
- }
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%s prepared %s.", impl->is_client ? "Client" : "Server", tsi_fake_handshake_message_to_string (impl->next_message_to_send));
- }
- impl->next_message_to_send = next_message_to_send;
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%s prepared %s.",
+ impl->is_client ? "Client" : "Server",
+ tsi_fake_handshake_message_to_string(impl->next_message_to_send));
}
- result = drain_frame_to_bytes (bytes, bytes_size, &impl->outgoing);
- if (result != TSI_OK)
- return result;
- if (!impl->is_client && impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- /* We're done. */
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "Server is done.");
- }
- impl->result = TSI_OK;
- }
- else
- {
- impl->needs_incoming_message = 1;
+ impl->next_message_to_send = next_message_to_send;
+ }
+ result = drain_frame_to_bytes(bytes, bytes_size, &impl->outgoing);
+ if (result != TSI_OK) return result;
+ if (!impl->is_client &&
+ impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ /* We're done. */
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "Server is done.");
}
+ impl->result = TSI_OK;
+ } else {
+ impl->needs_incoming_message = 1;
+ }
return TSI_OK;
}
-static tsi_result
-fake_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size)
-{
+static tsi_result fake_handshaker_process_bytes_from_peer(
+ tsi_handshaker *self, const unsigned char *bytes, size_t *bytes_size) {
tsi_result result = TSI_OK;
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
tsi_fake_handshake_message expected_msg = impl->next_message_to_send - 1;
tsi_fake_handshake_message received_msg;
- if (!impl->needs_incoming_message || impl->result == TSI_OK)
- {
- *bytes_size = 0;
- return TSI_OK;
- }
- result = fill_frame_from_bytes (bytes, bytes_size, &impl->incoming);
- if (result != TSI_OK)
- return result;
+ if (!impl->needs_incoming_message || impl->result == TSI_OK) {
+ *bytes_size = 0;
+ return TSI_OK;
+ }
+ result = fill_frame_from_bytes(bytes, bytes_size, &impl->incoming);
+ if (result != TSI_OK) return result;
/* We now have a complete frame. */
- result = tsi_fake_handshake_message_from_string ((const char *) impl->incoming.data + TSI_FAKE_FRAME_HEADER_SIZE, &received_msg);
- if (result != TSI_OK)
- {
- impl->result = result;
- return result;
- }
- if (received_msg != expected_msg)
- {
- gpr_log (GPR_ERROR, "Invalid received message (%s instead of %s)", tsi_fake_handshake_message_to_string (received_msg), tsi_fake_handshake_message_to_string (expected_msg));
- }
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%s received %s.", impl->is_client ? "Client" : "Server", tsi_fake_handshake_message_to_string (received_msg));
- }
- tsi_fake_frame_reset (&impl->incoming, 0 /* needs_draining */ );
+ result = tsi_fake_handshake_message_from_string(
+ (const char *)impl->incoming.data + TSI_FAKE_FRAME_HEADER_SIZE,
+ &received_msg);
+ if (result != TSI_OK) {
+ impl->result = result;
+ return result;
+ }
+ if (received_msg != expected_msg) {
+ gpr_log(GPR_ERROR, "Invalid received message (%s instead of %s)",
+ tsi_fake_handshake_message_to_string(received_msg),
+ tsi_fake_handshake_message_to_string(expected_msg));
+ }
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%s received %s.", impl->is_client ? "Client" : "Server",
+ tsi_fake_handshake_message_to_string(received_msg));
+ }
+ tsi_fake_frame_reset(&impl->incoming, 0 /* needs_draining */);
impl->needs_incoming_message = 0;
- if (impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX)
- {
- /* We're done. */
- if (tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%s is done.", impl->is_client ? "Client" : "Server");
- }
- impl->result = TSI_OK;
+ if (impl->next_message_to_send == TSI_FAKE_HANDSHAKE_MESSAGE_MAX) {
+ /* We're done. */
+ if (tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%s is done.", impl->is_client ? "Client" : "Server");
}
+ impl->result = TSI_OK;
+ }
return TSI_OK;
}
-static tsi_result
-fake_handshaker_get_result (tsi_handshaker * self)
-{
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
+static tsi_result fake_handshaker_get_result(tsi_handshaker *self) {
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
return impl->result;
}
-static tsi_result
-fake_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer)
-{
- tsi_result result = tsi_construct_peer (1, peer);
- if (result != TSI_OK)
- return result;
- result = tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_FAKE_CERTIFICATE_TYPE, &peer->properties[0]);
- if (result != TSI_OK)
- tsi_peer_destruct (peer);
+static tsi_result fake_handshaker_extract_peer(tsi_handshaker *self,
+ tsi_peer *peer) {
+ tsi_result result = tsi_construct_peer(1, peer);
+ if (result != TSI_OK) return result;
+ result = tsi_construct_string_peer_property_from_cstring(
+ TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_FAKE_CERTIFICATE_TYPE,
+ &peer->properties[0]);
+ if (result != TSI_OK) tsi_peer_destruct(peer);
return result;
}
-static tsi_result
-fake_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_protected_frame_size, tsi_frame_protector ** protector)
-{
- *protector = tsi_create_fake_protector (max_protected_frame_size);
- if (*protector == NULL)
- return TSI_OUT_OF_RESOURCES;
+static tsi_result fake_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_protected_frame_size,
+ tsi_frame_protector **protector) {
+ *protector = tsi_create_fake_protector(max_protected_frame_size);
+ if (*protector == NULL) return TSI_OUT_OF_RESOURCES;
return TSI_OK;
}
-static void
-fake_handshaker_destroy (tsi_handshaker * self)
-{
- tsi_fake_handshaker *impl = (tsi_fake_handshaker *) self;
- tsi_fake_frame_destruct (&impl->incoming);
- tsi_fake_frame_destruct (&impl->outgoing);
- free (self);
+static void fake_handshaker_destroy(tsi_handshaker *self) {
+ tsi_fake_handshaker *impl = (tsi_fake_handshaker *)self;
+ tsi_fake_frame_destruct(&impl->incoming);
+ tsi_fake_frame_destruct(&impl->outgoing);
+ free(self);
}
static const tsi_handshaker_vtable handshaker_vtable = {
- fake_handshaker_get_bytes_to_send_to_peer,
- fake_handshaker_process_bytes_from_peer,
- fake_handshaker_get_result,
- fake_handshaker_extract_peer,
- fake_handshaker_create_frame_protector,
- fake_handshaker_destroy,
+ fake_handshaker_get_bytes_to_send_to_peer,
+ fake_handshaker_process_bytes_from_peer,
+ fake_handshaker_get_result,
+ fake_handshaker_extract_peer,
+ fake_handshaker_create_frame_protector,
+ fake_handshaker_destroy,
};
-tsi_handshaker *
-tsi_create_fake_handshaker (int is_client)
-{
- tsi_fake_handshaker *impl = calloc (1, sizeof (tsi_fake_handshaker));
+tsi_handshaker *tsi_create_fake_handshaker(int is_client) {
+ tsi_fake_handshaker *impl = calloc(1, sizeof(tsi_fake_handshaker));
impl->base.vtable = &handshaker_vtable;
impl->is_client = is_client;
impl->result = TSI_HANDSHAKE_IN_PROGRESS;
- if (is_client)
- {
- impl->needs_incoming_message = 0;
- impl->next_message_to_send = TSI_FAKE_CLIENT_INIT;
- }
- else
- {
- impl->needs_incoming_message = 1;
- impl->next_message_to_send = TSI_FAKE_SERVER_INIT;
- }
+ if (is_client) {
+ impl->needs_incoming_message = 0;
+ impl->next_message_to_send = TSI_FAKE_CLIENT_INIT;
+ } else {
+ impl->needs_incoming_message = 1;
+ impl->next_message_to_send = TSI_FAKE_SERVER_INIT;
+ }
return &impl->base;
}
-tsi_frame_protector *
-tsi_create_fake_protector (size_t * max_protected_frame_size)
-{
- tsi_fake_frame_protector *impl = calloc (1, sizeof (tsi_fake_frame_protector));
- if (impl == NULL)
- return NULL;
- impl->max_frame_size = (max_protected_frame_size == NULL) ? TSI_FAKE_DEFAULT_FRAME_SIZE : *max_protected_frame_size;
+tsi_frame_protector *tsi_create_fake_protector(
+ size_t *max_protected_frame_size) {
+ tsi_fake_frame_protector *impl = calloc(1, sizeof(tsi_fake_frame_protector));
+ if (impl == NULL) return NULL;
+ impl->max_frame_size = (max_protected_frame_size == NULL)
+ ? TSI_FAKE_DEFAULT_FRAME_SIZE
+ : *max_protected_frame_size;
impl->base.vtable = &frame_protector_vtable;
return &impl->base;
}
diff --git a/src/core/tsi/fake_transport_security.h b/src/core/tsi/fake_transport_security.h
index 27b4008c94..fe295aa536 100644
--- a/src/core/tsi/fake_transport_security.h
+++ b/src/core/tsi/fake_transport_security.h
@@ -37,8 +37,7 @@
#include "src/core/tsi/transport_security_interface.h"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Value for the TSI_CERTIFICATE_TYPE_PEER_PROPERTY property for FAKE certs. */
@@ -49,13 +48,14 @@ extern "C"
No cryptography is performed in these objects. They just simulate handshake
messages going back and forth for the handshaker and do some framing on
cleartext data for the protector. */
- tsi_handshaker *tsi_create_fake_handshaker (int is_client);
+tsi_handshaker *tsi_create_fake_handshaker(int is_client);
/* Creates a protector directly without going through the handshake phase. */
- tsi_frame_protector *tsi_create_fake_protector (size_t * max_protected_frame_size);
+tsi_frame_protector *tsi_create_fake_protector(
+ size_t *max_protected_frame_size);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_FAKE_TRANSPORT_SECURITY_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_FAKE_TRANSPORT_SECURITY_H */
diff --git a/src/core/tsi/ssl_transport_security.c b/src/core/tsi/ssl_transport_security.c
index a4e3503a96..ad6b2d7684 100644
--- a/src/core/tsi/ssl_transport_security.c
+++ b/src/core/tsi/ssl_transport_security.c
@@ -43,7 +43,7 @@
#include "src/core/tsi/transport_security.h"
#include <openssl/bio.h>
-#include <openssl/crypto.h> /* For OPENSSL_free */
+#include <openssl/crypto.h> /* For OPENSSL_free */
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
@@ -67,22 +67,21 @@
/* --- Structure definitions. ---*/
-struct tsi_ssl_handshaker_factory
-{
- tsi_result (*create_handshaker) (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker);
- void (*destroy) (tsi_ssl_handshaker_factory * self);
+struct tsi_ssl_handshaker_factory {
+ tsi_result (*create_handshaker)(tsi_ssl_handshaker_factory *self,
+ const char *server_name_indication,
+ tsi_handshaker **handshaker);
+ void (*destroy)(tsi_ssl_handshaker_factory *self);
};
-typedef struct
-{
+typedef struct {
tsi_ssl_handshaker_factory base;
SSL_CTX *ssl_context;
unsigned char *alpn_protocol_list;
size_t alpn_protocol_list_length;
} tsi_ssl_client_handshaker_factory;
-typedef struct
-{
+typedef struct {
tsi_ssl_handshaker_factory base;
/* Several contexts to support SNI.
@@ -95,8 +94,7 @@ typedef struct
size_t alpn_protocol_list_length;
} tsi_ssl_server_handshaker_factory;
-typedef struct
-{
+typedef struct {
tsi_handshaker base;
SSL *ssl;
BIO *into_ssl;
@@ -104,8 +102,7 @@ typedef struct
tsi_result result;
} tsi_ssl_handshaker;
-typedef struct
-{
+typedef struct {
tsi_frame_protector base;
SSL *ssl;
BIO *into_ssl;
@@ -120,52 +117,39 @@ typedef struct
static gpr_once init_openssl_once = GPR_ONCE_INIT;
static gpr_mu *openssl_mutexes = NULL;
-static void
-openssl_locking_cb (int mode, int type, const char *file, int line)
-{
- if (mode & CRYPTO_LOCK)
- {
- gpr_mu_lock (&openssl_mutexes[type]);
- }
- else
- {
- gpr_mu_unlock (&openssl_mutexes[type]);
- }
+static void openssl_locking_cb(int mode, int type, const char *file, int line) {
+ if (mode & CRYPTO_LOCK) {
+ gpr_mu_lock(&openssl_mutexes[type]);
+ } else {
+ gpr_mu_unlock(&openssl_mutexes[type]);
+ }
}
-static unsigned long
-openssl_thread_id_cb (void)
-{
- return (unsigned long) gpr_thd_currentid ();
+static unsigned long openssl_thread_id_cb(void) {
+ return (unsigned long)gpr_thd_currentid();
}
-static void
-init_openssl (void)
-{
+static void init_openssl(void) {
int i;
int num_locks;
- SSL_library_init ();
- SSL_load_error_strings ();
- OpenSSL_add_all_algorithms ();
- num_locks = CRYPTO_num_locks ();
- GPR_ASSERT (num_locks > 0);
- openssl_mutexes = malloc ((size_t) num_locks * sizeof (gpr_mu));
- GPR_ASSERT (openssl_mutexes != NULL);
- for (i = 0; i < CRYPTO_num_locks (); i++)
- {
- gpr_mu_init (&openssl_mutexes[i]);
- }
- CRYPTO_set_locking_callback (openssl_locking_cb);
- CRYPTO_set_id_callback (openssl_thread_id_cb);
+ SSL_library_init();
+ SSL_load_error_strings();
+ OpenSSL_add_all_algorithms();
+ num_locks = CRYPTO_num_locks();
+ GPR_ASSERT(num_locks > 0);
+ openssl_mutexes = malloc((size_t)num_locks * sizeof(gpr_mu));
+ GPR_ASSERT(openssl_mutexes != NULL);
+ for (i = 0; i < CRYPTO_num_locks(); i++) {
+ gpr_mu_init(&openssl_mutexes[i]);
+ }
+ CRYPTO_set_locking_callback(openssl_locking_cb);
+ CRYPTO_set_id_callback(openssl_thread_id_cb);
}
/* --- Ssl utils. ---*/
-static const char *
-ssl_error_string (int error)
-{
- switch (error)
- {
+static const char *ssl_error_string(int error) {
+ switch (error) {
case SSL_ERROR_NONE:
return "SSL_ERROR_NONE";
case SSL_ERROR_ZERO_RETURN:
@@ -186,885 +170,791 @@ ssl_error_string (int error)
return "SSL_ERROR_SSL";
default:
return "Unknown error";
- }
+ }
}
/* TODO(jboeuf): Remove when we are past the debugging phase with this code. */
-static void
-ssl_log_where_info (const SSL * ssl, int where, int flag, const char *msg)
-{
- if ((where & flag) && tsi_tracing_enabled)
- {
- gpr_log (GPR_INFO, "%20.20s - %30.30s - %5.10s", msg, SSL_state_string_long (ssl), SSL_state_string (ssl));
- }
+static void ssl_log_where_info(const SSL *ssl, int where, int flag,
+ const char *msg) {
+ if ((where & flag) && tsi_tracing_enabled) {
+ gpr_log(GPR_INFO, "%20.20s - %30.30s - %5.10s", msg,
+ SSL_state_string_long(ssl), SSL_state_string(ssl));
+ }
}
/* Used for debugging. TODO(jboeuf): Remove when code is mature enough. */
-static void
-ssl_info_callback (const SSL * ssl, int where, int ret)
-{
- if (ret == 0)
- {
- gpr_log (GPR_ERROR, "ssl_info_callback: error occured.\n");
- return;
- }
+static void ssl_info_callback(const SSL *ssl, int where, int ret) {
+ if (ret == 0) {
+ gpr_log(GPR_ERROR, "ssl_info_callback: error occured.\n");
+ return;
+ }
- ssl_log_where_info (ssl, where, SSL_CB_LOOP, "LOOP");
- ssl_log_where_info (ssl, where, SSL_CB_HANDSHAKE_START, "HANDSHAKE START");
- ssl_log_where_info (ssl, where, SSL_CB_HANDSHAKE_DONE, "HANDSHAKE DONE");
+ ssl_log_where_info(ssl, where, SSL_CB_LOOP, "LOOP");
+ ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_START, "HANDSHAKE START");
+ ssl_log_where_info(ssl, where, SSL_CB_HANDSHAKE_DONE, "HANDSHAKE DONE");
}
/* Returns 1 if name looks like an IP address, 0 otherwise.
This is a very rough heuristic as it does not handle IPV6 or things like:
0300.0250.00.01, 0xC0.0Xa8.0x0.0x1, 000030052000001, 0xc0.052000001 */
-static int
-looks_like_ip_address (const char *name)
-{
+static int looks_like_ip_address(const char *name) {
size_t i;
size_t dot_count = 0;
size_t num_size = 0;
- for (i = 0; i < strlen (name); i++)
- {
- if (name[i] >= '0' && name[i] <= '9')
- {
- if (num_size > 3)
- return 0;
- num_size++;
- }
- else if (name[i] == '.')
- {
- if (dot_count > 3 || num_size == 0)
- return 0;
- dot_count++;
- num_size = 0;
- }
- else
- {
- return 0;
- }
+ for (i = 0; i < strlen(name); i++) {
+ if (name[i] >= '0' && name[i] <= '9') {
+ if (num_size > 3) return 0;
+ num_size++;
+ } else if (name[i] == '.') {
+ if (dot_count > 3 || num_size == 0) return 0;
+ dot_count++;
+ num_size = 0;
+ } else {
+ return 0;
}
- if (dot_count < 3 || num_size == 0)
- return 0;
+ }
+ if (dot_count < 3 || num_size == 0) return 0;
return 1;
}
/* Gets the subject CN from an X509 cert. */
-static tsi_result
-ssl_get_x509_common_name (X509 * cert, unsigned char **utf8, size_t * utf8_size)
-{
+static tsi_result ssl_get_x509_common_name(X509 *cert, unsigned char **utf8,
+ size_t *utf8_size) {
int common_name_index = -1;
X509_NAME_ENTRY *common_name_entry = NULL;
ASN1_STRING *common_name_asn1 = NULL;
- X509_NAME *subject_name = X509_get_subject_name (cert);
+ X509_NAME *subject_name = X509_get_subject_name(cert);
int utf8_returned_size = 0;
- if (subject_name == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get subject name from certificate.");
- return TSI_NOT_FOUND;
- }
- common_name_index = X509_NAME_get_index_by_NID (subject_name, NID_commonName, -1);
- if (common_name_index == -1)
- {
- gpr_log (GPR_ERROR, "Could not get common name of subject from certificate.");
- return TSI_NOT_FOUND;
- }
- common_name_entry = X509_NAME_get_entry (subject_name, common_name_index);
- if (common_name_entry == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get common name entry from certificate.");
- return TSI_INTERNAL_ERROR;
- }
- common_name_asn1 = X509_NAME_ENTRY_get_data (common_name_entry);
- if (common_name_asn1 == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get common name entry asn1 from certificate.");
- return TSI_INTERNAL_ERROR;
- }
- utf8_returned_size = ASN1_STRING_to_UTF8 (utf8, common_name_asn1);
- if (utf8_returned_size < 0)
- {
- gpr_log (GPR_ERROR, "Could not extract utf8 from asn1 string.");
- return TSI_OUT_OF_RESOURCES;
- }
- *utf8_size = (size_t) utf8_returned_size;
+ if (subject_name == NULL) {
+ gpr_log(GPR_ERROR, "Could not get subject name from certificate.");
+ return TSI_NOT_FOUND;
+ }
+ common_name_index =
+ X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1);
+ if (common_name_index == -1) {
+ gpr_log(GPR_ERROR,
+ "Could not get common name of subject from certificate.");
+ return TSI_NOT_FOUND;
+ }
+ common_name_entry = X509_NAME_get_entry(subject_name, common_name_index);
+ if (common_name_entry == NULL) {
+ gpr_log(GPR_ERROR, "Could not get common name entry from certificate.");
+ return TSI_INTERNAL_ERROR;
+ }
+ common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
+ if (common_name_asn1 == NULL) {
+ gpr_log(GPR_ERROR,
+ "Could not get common name entry asn1 from certificate.");
+ return TSI_INTERNAL_ERROR;
+ }
+ utf8_returned_size = ASN1_STRING_to_UTF8(utf8, common_name_asn1);
+ if (utf8_returned_size < 0) {
+ gpr_log(GPR_ERROR, "Could not extract utf8 from asn1 string.");
+ return TSI_OUT_OF_RESOURCES;
+ }
+ *utf8_size = (size_t)utf8_returned_size;
return TSI_OK;
}
/* Gets the subject CN of an X509 cert as a tsi_peer_property. */
-static tsi_result
-peer_property_from_x509_common_name (X509 * cert, tsi_peer_property * property)
-{
+static tsi_result peer_property_from_x509_common_name(
+ X509 *cert, tsi_peer_property *property) {
unsigned char *common_name;
size_t common_name_size;
- tsi_result result = ssl_get_x509_common_name (cert, &common_name, &common_name_size);
- if (result != TSI_OK)
- {
- if (result == TSI_NOT_FOUND)
- {
- common_name = NULL;
- common_name_size = 0;
- }
- else
- {
- return result;
- }
+ tsi_result result =
+ ssl_get_x509_common_name(cert, &common_name, &common_name_size);
+ if (result != TSI_OK) {
+ if (result == TSI_NOT_FOUND) {
+ common_name = NULL;
+ common_name_size = 0;
+ } else {
+ return result;
}
- result = tsi_construct_string_peer_property (TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY, common_name == NULL ? "" : (const char *) common_name, common_name_size, property);
- OPENSSL_free (common_name);
+ }
+ result = tsi_construct_string_peer_property(
+ TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY,
+ common_name == NULL ? "" : (const char *)common_name, common_name_size,
+ property);
+ OPENSSL_free(common_name);
return result;
}
/* Gets the subject SANs from an X509 cert as a tsi_peer_property. */
-static tsi_result
-add_subject_alt_names_properties_to_peer (tsi_peer * peer, GENERAL_NAMES * subject_alt_names, size_t subject_alt_name_count)
-{
+static tsi_result add_subject_alt_names_properties_to_peer(
+ tsi_peer *peer, GENERAL_NAMES *subject_alt_names,
+ size_t subject_alt_name_count) {
size_t i;
tsi_result result = TSI_OK;
/* Reset for DNS entries filtering. */
peer->property_count -= subject_alt_name_count;
- for (i = 0; i < subject_alt_name_count; i++)
- {
- GENERAL_NAME *subject_alt_name = sk_GENERAL_NAME_value (subject_alt_names, (int) i);
- /* Filter out the non-dns entries names. */
- if (subject_alt_name->type == GEN_DNS)
- {
- unsigned char *dns_name = NULL;
- int dns_name_size = ASN1_STRING_to_UTF8 (&dns_name, subject_alt_name->d.dNSName);
- if (dns_name_size < 0)
- {
- gpr_log (GPR_ERROR, "Could not get utf8 from asn1 string.");
- result = TSI_INTERNAL_ERROR;
- break;
- }
- result = tsi_construct_string_peer_property (TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY, (const char *) dns_name, (size_t) dns_name_size, &peer->properties[peer->property_count++]);
- OPENSSL_free (dns_name);
- if (result != TSI_OK)
- break;
- }
+ for (i = 0; i < subject_alt_name_count; i++) {
+ GENERAL_NAME *subject_alt_name =
+ sk_GENERAL_NAME_value(subject_alt_names, (int)i);
+ /* Filter out the non-dns entries names. */
+ if (subject_alt_name->type == GEN_DNS) {
+ unsigned char *dns_name = NULL;
+ int dns_name_size =
+ ASN1_STRING_to_UTF8(&dns_name, subject_alt_name->d.dNSName);
+ if (dns_name_size < 0) {
+ gpr_log(GPR_ERROR, "Could not get utf8 from asn1 string.");
+ result = TSI_INTERNAL_ERROR;
+ break;
+ }
+ result = tsi_construct_string_peer_property(
+ TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY,
+ (const char *)dns_name, (size_t)dns_name_size,
+ &peer->properties[peer->property_count++]);
+ OPENSSL_free(dns_name);
+ if (result != TSI_OK) break;
}
+ }
return result;
}
/* Gets information about the peer's X509 cert as a tsi_peer object. */
-static tsi_result
-peer_from_x509 (X509 * cert, int include_certificate_type, tsi_peer * peer)
-{
+static tsi_result peer_from_x509(X509 *cert, int include_certificate_type,
+ tsi_peer *peer) {
/* TODO(jboeuf): Maybe add more properties. */
- GENERAL_NAMES *subject_alt_names = X509_get_ext_d2i (cert, NID_subject_alt_name, 0, 0);
- int subject_alt_name_count = (subject_alt_names != NULL) ? sk_GENERAL_NAME_num (subject_alt_names) : 0;
+ GENERAL_NAMES *subject_alt_names =
+ X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0);
+ int subject_alt_name_count =
+ (subject_alt_names != NULL) ? sk_GENERAL_NAME_num(subject_alt_names) : 0;
size_t property_count;
tsi_result result;
- GPR_ASSERT (subject_alt_name_count >= 0);
- property_count = (include_certificate_type ? (size_t) 1 : 0) + 1 /* common name */ + (size_t) subject_alt_name_count;
- result = tsi_construct_peer (property_count, peer);
- if (result != TSI_OK)
- return result;
- do
- {
- if (include_certificate_type)
- {
- result = tsi_construct_string_peer_property_from_cstring (TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE, &peer->properties[0]);
- if (result != TSI_OK)
- break;
- }
- result = peer_property_from_x509_common_name (cert, &peer->properties[include_certificate_type ? 1 : 0]);
- if (result != TSI_OK)
- break;
-
- if (subject_alt_name_count != 0)
- {
- result = add_subject_alt_names_properties_to_peer (peer, subject_alt_names, (size_t) subject_alt_name_count);
- if (result != TSI_OK)
- break;
- }
- }
- while (0);
-
- if (subject_alt_names != NULL)
- {
- sk_GENERAL_NAME_pop_free (subject_alt_names, GENERAL_NAME_free);
- }
- if (result != TSI_OK)
- tsi_peer_destruct (peer);
+ GPR_ASSERT(subject_alt_name_count >= 0);
+ property_count = (include_certificate_type ? (size_t)1 : 0) +
+ 1 /* common name */ + (size_t)subject_alt_name_count;
+ result = tsi_construct_peer(property_count, peer);
+ if (result != TSI_OK) return result;
+ do {
+ if (include_certificate_type) {
+ result = tsi_construct_string_peer_property_from_cstring(
+ TSI_CERTIFICATE_TYPE_PEER_PROPERTY, TSI_X509_CERTIFICATE_TYPE,
+ &peer->properties[0]);
+ if (result != TSI_OK) break;
+ }
+ result = peer_property_from_x509_common_name(
+ cert, &peer->properties[include_certificate_type ? 1 : 0]);
+ if (result != TSI_OK) break;
+
+ if (subject_alt_name_count != 0) {
+ result = add_subject_alt_names_properties_to_peer(
+ peer, subject_alt_names, (size_t)subject_alt_name_count);
+ if (result != TSI_OK) break;
+ }
+ } while (0);
+
+ if (subject_alt_names != NULL) {
+ sk_GENERAL_NAME_pop_free(subject_alt_names, GENERAL_NAME_free);
+ }
+ if (result != TSI_OK) tsi_peer_destruct(peer);
return result;
}
/* Logs the SSL error stack. */
-static void
-log_ssl_error_stack (void)
-{
+static void log_ssl_error_stack(void) {
unsigned long err;
- while ((err = ERR_get_error ()) != 0)
- {
- char details[256];
- ERR_error_string_n (err, details, sizeof (details));
- gpr_log (GPR_ERROR, "%s", details);
- }
+ while ((err = ERR_get_error()) != 0) {
+ char details[256];
+ ERR_error_string_n(err, details, sizeof(details));
+ gpr_log(GPR_ERROR, "%s", details);
+ }
}
/* Performs an SSL_read and handle errors. */
-static tsi_result
-do_ssl_read (SSL * ssl, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
+static tsi_result do_ssl_read(SSL *ssl, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
int read_from_ssl;
- GPR_ASSERT (*unprotected_bytes_size <= INT_MAX);
- read_from_ssl = SSL_read (ssl, unprotected_bytes, (int) *unprotected_bytes_size);
- if (read_from_ssl == 0)
- {
- gpr_log (GPR_ERROR, "SSL_read returned 0 unexpectedly.");
- return TSI_INTERNAL_ERROR;
- }
- if (read_from_ssl < 0)
- {
- read_from_ssl = SSL_get_error (ssl, read_from_ssl);
- switch (read_from_ssl)
- {
- case SSL_ERROR_WANT_READ:
- /* We need more data to finish the frame. */
- *unprotected_bytes_size = 0;
- return TSI_OK;
- case SSL_ERROR_WANT_WRITE:
- gpr_log (GPR_ERROR, "Peer tried to renegotiate SSL connection. This is unsupported.");
- return TSI_UNIMPLEMENTED;
- case SSL_ERROR_SSL:
- gpr_log (GPR_ERROR, "Corruption detected.");
- log_ssl_error_stack ();
- return TSI_DATA_CORRUPTED;
- default:
- gpr_log (GPR_ERROR, "SSL_read failed with error %s.", ssl_error_string (read_from_ssl));
- return TSI_PROTOCOL_FAILURE;
- }
+ GPR_ASSERT(*unprotected_bytes_size <= INT_MAX);
+ read_from_ssl =
+ SSL_read(ssl, unprotected_bytes, (int)*unprotected_bytes_size);
+ if (read_from_ssl == 0) {
+ gpr_log(GPR_ERROR, "SSL_read returned 0 unexpectedly.");
+ return TSI_INTERNAL_ERROR;
+ }
+ if (read_from_ssl < 0) {
+ read_from_ssl = SSL_get_error(ssl, read_from_ssl);
+ switch (read_from_ssl) {
+ case SSL_ERROR_WANT_READ:
+ /* We need more data to finish the frame. */
+ *unprotected_bytes_size = 0;
+ return TSI_OK;
+ case SSL_ERROR_WANT_WRITE:
+ gpr_log(
+ GPR_ERROR,
+ "Peer tried to renegotiate SSL connection. This is unsupported.");
+ return TSI_UNIMPLEMENTED;
+ case SSL_ERROR_SSL:
+ gpr_log(GPR_ERROR, "Corruption detected.");
+ log_ssl_error_stack();
+ return TSI_DATA_CORRUPTED;
+ default:
+ gpr_log(GPR_ERROR, "SSL_read failed with error %s.",
+ ssl_error_string(read_from_ssl));
+ return TSI_PROTOCOL_FAILURE;
}
- *unprotected_bytes_size = (size_t) read_from_ssl;
+ }
+ *unprotected_bytes_size = (size_t)read_from_ssl;
return TSI_OK;
}
/* Performs an SSL_write and handle errors. */
-static tsi_result
-do_ssl_write (SSL * ssl, unsigned char *unprotected_bytes, size_t unprotected_bytes_size)
-{
+static tsi_result do_ssl_write(SSL *ssl, unsigned char *unprotected_bytes,
+ size_t unprotected_bytes_size) {
int ssl_write_result;
- GPR_ASSERT (unprotected_bytes_size <= INT_MAX);
- ssl_write_result = SSL_write (ssl, unprotected_bytes, (int) unprotected_bytes_size);
- if (ssl_write_result < 0)
- {
- ssl_write_result = SSL_get_error (ssl, ssl_write_result);
- if (ssl_write_result == SSL_ERROR_WANT_READ)
- {
- gpr_log (GPR_ERROR, "Peer tried to renegotiate SSL connection. This is unsupported.");
- return TSI_UNIMPLEMENTED;
- }
- else
- {
- gpr_log (GPR_ERROR, "SSL_write failed with error %s.", ssl_error_string (ssl_write_result));
- return TSI_INTERNAL_ERROR;
- }
+ GPR_ASSERT(unprotected_bytes_size <= INT_MAX);
+ ssl_write_result =
+ SSL_write(ssl, unprotected_bytes, (int)unprotected_bytes_size);
+ if (ssl_write_result < 0) {
+ ssl_write_result = SSL_get_error(ssl, ssl_write_result);
+ if (ssl_write_result == SSL_ERROR_WANT_READ) {
+ gpr_log(GPR_ERROR,
+ "Peer tried to renegotiate SSL connection. This is unsupported.");
+ return TSI_UNIMPLEMENTED;
+ } else {
+ gpr_log(GPR_ERROR, "SSL_write failed with error %s.",
+ ssl_error_string(ssl_write_result));
+ return TSI_INTERNAL_ERROR;
}
+ }
return TSI_OK;
}
/* Loads an in-memory PEM certificate chain into the SSL context. */
-static tsi_result
-ssl_ctx_use_certificate_chain (SSL_CTX * context, const unsigned char *pem_cert_chain, size_t pem_cert_chain_size)
-{
+static tsi_result ssl_ctx_use_certificate_chain(
+ SSL_CTX *context, const unsigned char *pem_cert_chain,
+ size_t pem_cert_chain_size) {
tsi_result result = TSI_OK;
X509 *certificate = NULL;
BIO *pem;
- GPR_ASSERT (pem_cert_chain_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_cert_chain, (int) pem_cert_chain_size);
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
+ GPR_ASSERT(pem_cert_chain_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_cert_chain, (int)pem_cert_chain_size);
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
- do
- {
- certificate = PEM_read_bio_X509_AUX (pem, NULL, NULL, "");
- if (certificate == NULL)
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- if (!SSL_CTX_use_certificate (context, certificate))
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- while (1)
- {
- X509 *certificate_authority = PEM_read_bio_X509 (pem, NULL, NULL, "");
- if (certificate_authority == NULL)
- {
- ERR_clear_error ();
- break; /* Done reading. */
- }
- if (!SSL_CTX_add_extra_chain_cert (context, certificate_authority))
- {
- X509_free (certificate_authority);
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- /* We don't need to free certificate_authority as its ownership has been
- transfered to the context. That is not the case for certificate though.
- */
- }
+ do {
+ certificate = PEM_read_bio_X509_AUX(pem, NULL, NULL, "");
+ if (certificate == NULL) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ if (!SSL_CTX_use_certificate(context, certificate)) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ while (1) {
+ X509 *certificate_authority = PEM_read_bio_X509(pem, NULL, NULL, "");
+ if (certificate_authority == NULL) {
+ ERR_clear_error();
+ break; /* Done reading. */
+ }
+ if (!SSL_CTX_add_extra_chain_cert(context, certificate_authority)) {
+ X509_free(certificate_authority);
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ /* We don't need to free certificate_authority as its ownership has been
+ transfered to the context. That is not the case for certificate though.
+ */
}
- while (0);
+ } while (0);
- if (certificate != NULL)
- X509_free (certificate);
- BIO_free (pem);
+ if (certificate != NULL) X509_free(certificate);
+ BIO_free(pem);
return result;
}
/* Loads an in-memory PEM private key into the SSL context. */
-static tsi_result
-ssl_ctx_use_private_key (SSL_CTX * context, const unsigned char *pem_key, size_t pem_key_size)
-{
+static tsi_result ssl_ctx_use_private_key(SSL_CTX *context,
+ const unsigned char *pem_key,
+ size_t pem_key_size) {
tsi_result result = TSI_OK;
EVP_PKEY *private_key = NULL;
BIO *pem;
- GPR_ASSERT (pem_key_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_key, (int) pem_key_size);
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
- do
- {
- private_key = PEM_read_bio_PrivateKey (pem, NULL, NULL, "");
- if (private_key == NULL)
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- if (!SSL_CTX_use_PrivateKey (context, private_key))
- {
- result = TSI_INVALID_ARGUMENT;
- break;
- }
+ GPR_ASSERT(pem_key_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_key, (int)pem_key_size);
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
+ do {
+ private_key = PEM_read_bio_PrivateKey(pem, NULL, NULL, "");
+ if (private_key == NULL) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
}
- while (0);
- if (private_key != NULL)
- EVP_PKEY_free (private_key);
- BIO_free (pem);
+ if (!SSL_CTX_use_PrivateKey(context, private_key)) {
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ } while (0);
+ if (private_key != NULL) EVP_PKEY_free(private_key);
+ BIO_free(pem);
return result;
}
/* Loads in-memory PEM verification certs into the SSL context and optionally
returns the verification cert names (root_names can be NULL). */
-static tsi_result
-ssl_ctx_load_verification_certs (SSL_CTX * context, const unsigned char *pem_roots, size_t pem_roots_size, STACK_OF (X509_NAME) * *root_names)
-{
+static tsi_result ssl_ctx_load_verification_certs(
+ SSL_CTX *context, const unsigned char *pem_roots, size_t pem_roots_size,
+ STACK_OF(X509_NAME) * *root_names) {
tsi_result result = TSI_OK;
size_t num_roots = 0;
X509 *root = NULL;
X509_NAME *root_name = NULL;
BIO *pem;
X509_STORE *root_store;
- GPR_ASSERT (pem_roots_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_roots, (int) pem_roots_size);
- root_store = SSL_CTX_get_cert_store (context);
- if (root_store == NULL)
- return TSI_INVALID_ARGUMENT;
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
- if (root_names != NULL)
- {
- *root_names = sk_X509_NAME_new_null ();
- if (*root_names == NULL)
- return TSI_OUT_OF_RESOURCES;
- }
+ GPR_ASSERT(pem_roots_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_roots, (int)pem_roots_size);
+ root_store = SSL_CTX_get_cert_store(context);
+ if (root_store == NULL) return TSI_INVALID_ARGUMENT;
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
+ if (root_names != NULL) {
+ *root_names = sk_X509_NAME_new_null();
+ if (*root_names == NULL) return TSI_OUT_OF_RESOURCES;
+ }
- while (1)
- {
- root = PEM_read_bio_X509_AUX (pem, NULL, NULL, "");
- if (root == NULL)
- {
- ERR_clear_error ();
- break; /* We're at the end of stream. */
- }
- if (root_names != NULL)
- {
- root_name = X509_get_subject_name (root);
- if (root_name == NULL)
- {
- gpr_log (GPR_ERROR, "Could not get name from root certificate.");
- result = TSI_INVALID_ARGUMENT;
- break;
- }
- root_name = X509_NAME_dup (root_name);
- if (root_name == NULL)
- {
- result = TSI_OUT_OF_RESOURCES;
- break;
- }
- sk_X509_NAME_push (*root_names, root_name);
- root_name = NULL;
- }
- if (!X509_STORE_add_cert (root_store, root))
- {
- gpr_log (GPR_ERROR, "Could not add root certificate to ssl context.");
- result = TSI_INTERNAL_ERROR;
- break;
- }
- X509_free (root);
- num_roots++;
+ while (1) {
+ root = PEM_read_bio_X509_AUX(pem, NULL, NULL, "");
+ if (root == NULL) {
+ ERR_clear_error();
+ break; /* We're at the end of stream. */
+ }
+ if (root_names != NULL) {
+ root_name = X509_get_subject_name(root);
+ if (root_name == NULL) {
+ gpr_log(GPR_ERROR, "Could not get name from root certificate.");
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
+ root_name = X509_NAME_dup(root_name);
+ if (root_name == NULL) {
+ result = TSI_OUT_OF_RESOURCES;
+ break;
+ }
+ sk_X509_NAME_push(*root_names, root_name);
+ root_name = NULL;
}
-
- if (num_roots == 0)
- {
- gpr_log (GPR_ERROR, "Could not load any root certificate.");
- result = TSI_INVALID_ARGUMENT;
+ if (!X509_STORE_add_cert(root_store, root)) {
+ gpr_log(GPR_ERROR, "Could not add root certificate to ssl context.");
+ result = TSI_INTERNAL_ERROR;
+ break;
}
+ X509_free(root);
+ num_roots++;
+ }
- if (result != TSI_OK)
- {
- if (root != NULL)
- X509_free (root);
- if (root_names != NULL)
- {
- sk_X509_NAME_pop_free (*root_names, X509_NAME_free);
- *root_names = NULL;
- if (root_name != NULL)
- X509_NAME_free (root_name);
- }
+ if (num_roots == 0) {
+ gpr_log(GPR_ERROR, "Could not load any root certificate.");
+ result = TSI_INVALID_ARGUMENT;
+ }
+
+ if (result != TSI_OK) {
+ if (root != NULL) X509_free(root);
+ if (root_names != NULL) {
+ sk_X509_NAME_pop_free(*root_names, X509_NAME_free);
+ *root_names = NULL;
+ if (root_name != NULL) X509_NAME_free(root_name);
}
- BIO_free (pem);
+ }
+ BIO_free(pem);
return result;
}
/* Populates the SSL context with a private key and a cert chain, and sets the
cipher list and the ephemeral ECDH key. */
-static tsi_result
-populate_ssl_context (SSL_CTX * context, const unsigned char *pem_private_key, size_t pem_private_key_size, const unsigned char *pem_certificate_chain, size_t pem_certificate_chain_size, const char *cipher_list)
-{
+static tsi_result populate_ssl_context(
+ SSL_CTX *context, const unsigned char *pem_private_key,
+ size_t pem_private_key_size, const unsigned char *pem_certificate_chain,
+ size_t pem_certificate_chain_size, const char *cipher_list) {
tsi_result result = TSI_OK;
- if (pem_certificate_chain != NULL)
- {
- result = ssl_ctx_use_certificate_chain (context, pem_certificate_chain, pem_certificate_chain_size);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Invalid cert chain file.");
- return result;
- }
- }
- if (pem_private_key != NULL)
- {
- result = ssl_ctx_use_private_key (context, pem_private_key, pem_private_key_size);
- if (result != TSI_OK || !SSL_CTX_check_private_key (context))
- {
- gpr_log (GPR_ERROR, "Invalid private key.");
- return result != TSI_OK ? result : TSI_INVALID_ARGUMENT;
- }
+ if (pem_certificate_chain != NULL) {
+ result = ssl_ctx_use_certificate_chain(context, pem_certificate_chain,
+ pem_certificate_chain_size);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Invalid cert chain file.");
+ return result;
}
- if ((cipher_list != NULL) && !SSL_CTX_set_cipher_list (context, cipher_list))
- {
- gpr_log (GPR_ERROR, "Invalid cipher list: %s.", cipher_list);
- return TSI_INVALID_ARGUMENT;
+ }
+ if (pem_private_key != NULL) {
+ result =
+ ssl_ctx_use_private_key(context, pem_private_key, pem_private_key_size);
+ if (result != TSI_OK || !SSL_CTX_check_private_key(context)) {
+ gpr_log(GPR_ERROR, "Invalid private key.");
+ return result != TSI_OK ? result : TSI_INVALID_ARGUMENT;
}
+ }
+ if ((cipher_list != NULL) && !SSL_CTX_set_cipher_list(context, cipher_list)) {
+ gpr_log(GPR_ERROR, "Invalid cipher list: %s.", cipher_list);
+ return TSI_INVALID_ARGUMENT;
+ }
{
- EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
- if (!SSL_CTX_set_tmp_ecdh (context, ecdh))
- {
- gpr_log (GPR_ERROR, "Could not set ephemeral ECDH key.");
- EC_KEY_free (ecdh);
- return TSI_INTERNAL_ERROR;
- }
- SSL_CTX_set_options (context, SSL_OP_SINGLE_ECDH_USE);
- EC_KEY_free (ecdh);
+ EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
+ if (!SSL_CTX_set_tmp_ecdh(context, ecdh)) {
+ gpr_log(GPR_ERROR, "Could not set ephemeral ECDH key.");
+ EC_KEY_free(ecdh);
+ return TSI_INTERNAL_ERROR;
+ }
+ SSL_CTX_set_options(context, SSL_OP_SINGLE_ECDH_USE);
+ EC_KEY_free(ecdh);
}
return TSI_OK;
}
/* Extracts the CN and the SANs from an X509 cert as a peer object. */
-static tsi_result
-extract_x509_subject_names_from_pem_cert (const unsigned char *pem_cert, size_t pem_cert_size, tsi_peer * peer)
-{
+static tsi_result extract_x509_subject_names_from_pem_cert(
+ const unsigned char *pem_cert, size_t pem_cert_size, tsi_peer *peer) {
tsi_result result = TSI_OK;
X509 *cert = NULL;
BIO *pem;
- GPR_ASSERT (pem_cert_size <= INT_MAX);
- pem = BIO_new_mem_buf ((void *) pem_cert, (int) pem_cert_size);
- if (pem == NULL)
- return TSI_OUT_OF_RESOURCES;
-
- cert = PEM_read_bio_X509 (pem, NULL, NULL, "");
- if (cert == NULL)
- {
- gpr_log (GPR_ERROR, "Invalid certificate");
- result = TSI_INVALID_ARGUMENT;
- }
- else
- {
- result = peer_from_x509 (cert, 0, peer);
- }
- if (cert != NULL)
- X509_free (cert);
- BIO_free (pem);
+ GPR_ASSERT(pem_cert_size <= INT_MAX);
+ pem = BIO_new_mem_buf((void *)pem_cert, (int)pem_cert_size);
+ if (pem == NULL) return TSI_OUT_OF_RESOURCES;
+
+ cert = PEM_read_bio_X509(pem, NULL, NULL, "");
+ if (cert == NULL) {
+ gpr_log(GPR_ERROR, "Invalid certificate");
+ result = TSI_INVALID_ARGUMENT;
+ } else {
+ result = peer_from_x509(cert, 0, peer);
+ }
+ if (cert != NULL) X509_free(cert);
+ BIO_free(pem);
return result;
}
/* Builds the alpn protocol name list according to rfc 7301. */
-static tsi_result
-build_alpn_protocol_name_list (const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, unsigned char **protocol_name_list, size_t * protocol_name_list_length)
-{
+static tsi_result build_alpn_protocol_name_list(
+ const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ unsigned char **protocol_name_list, size_t *protocol_name_list_length) {
uint16_t i;
unsigned char *current;
*protocol_name_list = NULL;
*protocol_name_list_length = 0;
- if (num_alpn_protocols == 0)
- return TSI_INVALID_ARGUMENT;
- for (i = 0; i < num_alpn_protocols; i++)
- {
- if (alpn_protocols_lengths[i] == 0)
- {
- gpr_log (GPR_ERROR, "Invalid 0-length protocol name.");
- return TSI_INVALID_ARGUMENT;
- }
- *protocol_name_list_length += (size_t) alpn_protocols_lengths[i] + 1;
+ if (num_alpn_protocols == 0) return TSI_INVALID_ARGUMENT;
+ for (i = 0; i < num_alpn_protocols; i++) {
+ if (alpn_protocols_lengths[i] == 0) {
+ gpr_log(GPR_ERROR, "Invalid 0-length protocol name.");
+ return TSI_INVALID_ARGUMENT;
}
- *protocol_name_list = malloc (*protocol_name_list_length);
- if (*protocol_name_list == NULL)
- return TSI_OUT_OF_RESOURCES;
+ *protocol_name_list_length += (size_t)alpn_protocols_lengths[i] + 1;
+ }
+ *protocol_name_list = malloc(*protocol_name_list_length);
+ if (*protocol_name_list == NULL) return TSI_OUT_OF_RESOURCES;
current = *protocol_name_list;
- for (i = 0; i < num_alpn_protocols; i++)
- {
- *(current++) = alpn_protocols_lengths[i];
- memcpy (current, alpn_protocols[i], alpn_protocols_lengths[i]);
- current += alpn_protocols_lengths[i];
- }
+ for (i = 0; i < num_alpn_protocols; i++) {
+ *(current++) = alpn_protocols_lengths[i];
+ memcpy(current, alpn_protocols[i], alpn_protocols_lengths[i]);
+ current += alpn_protocols_lengths[i];
+ }
/* Safety check. */
- if ((current < *protocol_name_list) || ((gpr_uintptr) (current - *protocol_name_list) != *protocol_name_list_length))
- {
- return TSI_INTERNAL_ERROR;
- }
+ if ((current < *protocol_name_list) ||
+ ((gpr_uintptr)(current - *protocol_name_list) !=
+ *protocol_name_list_length)) {
+ return TSI_INTERNAL_ERROR;
+ }
return TSI_OK;
}
/* --- tsi_frame_protector methods implementation. ---*/
-static tsi_result
-ssl_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size)
-{
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
+static tsi_result ssl_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size) {
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
int read_from_ssl;
size_t available;
tsi_result result = TSI_OK;
/* First see if we have some pending data in the SSL BIO. */
- int pending_in_ssl = BIO_pending (impl->from_ssl);
- if (pending_in_ssl > 0)
- {
- *unprotected_bytes_size = 0;
- GPR_ASSERT (*protected_output_frames_size <= INT_MAX);
- read_from_ssl = BIO_read (impl->from_ssl, protected_output_frames, (int) *protected_output_frames_size);
- if (read_from_ssl < 0)
- {
- gpr_log (GPR_ERROR, "Could not read from BIO even though some data is pending");
- return TSI_INTERNAL_ERROR;
- }
- *protected_output_frames_size = (size_t) read_from_ssl;
- return TSI_OK;
+ int pending_in_ssl = BIO_pending(impl->from_ssl);
+ if (pending_in_ssl > 0) {
+ *unprotected_bytes_size = 0;
+ GPR_ASSERT(*protected_output_frames_size <= INT_MAX);
+ read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames,
+ (int)*protected_output_frames_size);
+ if (read_from_ssl < 0) {
+ gpr_log(GPR_ERROR,
+ "Could not read from BIO even though some data is pending");
+ return TSI_INTERNAL_ERROR;
}
+ *protected_output_frames_size = (size_t)read_from_ssl;
+ return TSI_OK;
+ }
/* Now see if we can send a complete frame. */
available = impl->buffer_size - impl->buffer_offset;
- if (available > *unprotected_bytes_size)
- {
- /* If we cannot, just copy the data in our internal buffer. */
- memcpy (impl->buffer + impl->buffer_offset, unprotected_bytes, *unprotected_bytes_size);
- impl->buffer_offset += *unprotected_bytes_size;
- *protected_output_frames_size = 0;
- return TSI_OK;
- }
+ if (available > *unprotected_bytes_size) {
+ /* If we cannot, just copy the data in our internal buffer. */
+ memcpy(impl->buffer + impl->buffer_offset, unprotected_bytes,
+ *unprotected_bytes_size);
+ impl->buffer_offset += *unprotected_bytes_size;
+ *protected_output_frames_size = 0;
+ return TSI_OK;
+ }
/* If we can, prepare the buffer, send it to SSL_write and read. */
- memcpy (impl->buffer + impl->buffer_offset, unprotected_bytes, available);
- result = do_ssl_write (impl->ssl, impl->buffer, impl->buffer_size);
- if (result != TSI_OK)
- return result;
-
- GPR_ASSERT (*protected_output_frames_size <= INT_MAX);
- read_from_ssl = BIO_read (impl->from_ssl, protected_output_frames, (int) *protected_output_frames_size);
- if (read_from_ssl < 0)
- {
- gpr_log (GPR_ERROR, "Could not read from BIO after SSL_write.");
- return TSI_INTERNAL_ERROR;
- }
- *protected_output_frames_size = (size_t) read_from_ssl;
+ memcpy(impl->buffer + impl->buffer_offset, unprotected_bytes, available);
+ result = do_ssl_write(impl->ssl, impl->buffer, impl->buffer_size);
+ if (result != TSI_OK) return result;
+
+ GPR_ASSERT(*protected_output_frames_size <= INT_MAX);
+ read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames,
+ (int)*protected_output_frames_size);
+ if (read_from_ssl < 0) {
+ gpr_log(GPR_ERROR, "Could not read from BIO after SSL_write.");
+ return TSI_INTERNAL_ERROR;
+ }
+ *protected_output_frames_size = (size_t)read_from_ssl;
*unprotected_bytes_size = available;
impl->buffer_offset = 0;
return TSI_OK;
}
-static tsi_result
-ssl_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size)
-{
+static tsi_result ssl_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size) {
tsi_result result = TSI_OK;
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
int read_from_ssl = 0;
int pending;
- if (impl->buffer_offset != 0)
- {
- result = do_ssl_write (impl->ssl, impl->buffer, impl->buffer_offset);
- if (result != TSI_OK)
- return result;
- impl->buffer_offset = 0;
- }
-
- pending = BIO_pending (impl->from_ssl);
- GPR_ASSERT (pending >= 0);
- *still_pending_size = (size_t) pending;
- if (*still_pending_size == 0)
- return TSI_OK;
+ if (impl->buffer_offset != 0) {
+ result = do_ssl_write(impl->ssl, impl->buffer, impl->buffer_offset);
+ if (result != TSI_OK) return result;
+ impl->buffer_offset = 0;
+ }
- GPR_ASSERT (*protected_output_frames_size <= INT_MAX);
- read_from_ssl = BIO_read (impl->from_ssl, protected_output_frames, (int) *protected_output_frames_size);
- if (read_from_ssl <= 0)
- {
- gpr_log (GPR_ERROR, "Could not read from BIO after SSL_write.");
- return TSI_INTERNAL_ERROR;
- }
- *protected_output_frames_size = (size_t) read_from_ssl;
- pending = BIO_pending (impl->from_ssl);
- GPR_ASSERT (pending >= 0);
- *still_pending_size = (size_t) pending;
+ pending = BIO_pending(impl->from_ssl);
+ GPR_ASSERT(pending >= 0);
+ *still_pending_size = (size_t)pending;
+ if (*still_pending_size == 0) return TSI_OK;
+
+ GPR_ASSERT(*protected_output_frames_size <= INT_MAX);
+ read_from_ssl = BIO_read(impl->from_ssl, protected_output_frames,
+ (int)*protected_output_frames_size);
+ if (read_from_ssl <= 0) {
+ gpr_log(GPR_ERROR, "Could not read from BIO after SSL_write.");
+ return TSI_INTERNAL_ERROR;
+ }
+ *protected_output_frames_size = (size_t)read_from_ssl;
+ pending = BIO_pending(impl->from_ssl);
+ GPR_ASSERT(pending >= 0);
+ *still_pending_size = (size_t)pending;
return TSI_OK;
}
-static tsi_result
-ssl_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
+static tsi_result ssl_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
tsi_result result = TSI_OK;
int written_into_ssl = 0;
size_t output_bytes_size = *unprotected_bytes_size;
size_t output_bytes_offset = 0;
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
/* First, try to read remaining data from ssl. */
- result = do_ssl_read (impl->ssl, unprotected_bytes, unprotected_bytes_size);
- if (result != TSI_OK)
- return result;
- if (*unprotected_bytes_size == output_bytes_size)
- {
- /* We have read everything we could and cannot process any more input. */
- *protected_frames_bytes_size = 0;
- return TSI_OK;
- }
+ result = do_ssl_read(impl->ssl, unprotected_bytes, unprotected_bytes_size);
+ if (result != TSI_OK) return result;
+ if (*unprotected_bytes_size == output_bytes_size) {
+ /* We have read everything we could and cannot process any more input. */
+ *protected_frames_bytes_size = 0;
+ return TSI_OK;
+ }
output_bytes_offset = *unprotected_bytes_size;
unprotected_bytes += output_bytes_offset;
*unprotected_bytes_size = output_bytes_size - output_bytes_offset;
/* Then, try to write some data to ssl. */
- GPR_ASSERT (*protected_frames_bytes_size <= INT_MAX);
- written_into_ssl = BIO_write (impl->into_ssl, protected_frames_bytes, (int) *protected_frames_bytes_size);
- if (written_into_ssl < 0)
- {
- gpr_log (GPR_ERROR, "Sending protected frame to ssl failed with %d", written_into_ssl);
- return TSI_INTERNAL_ERROR;
- }
- *protected_frames_bytes_size = (size_t) written_into_ssl;
+ GPR_ASSERT(*protected_frames_bytes_size <= INT_MAX);
+ written_into_ssl = BIO_write(impl->into_ssl, protected_frames_bytes,
+ (int)*protected_frames_bytes_size);
+ if (written_into_ssl < 0) {
+ gpr_log(GPR_ERROR, "Sending protected frame to ssl failed with %d",
+ written_into_ssl);
+ return TSI_INTERNAL_ERROR;
+ }
+ *protected_frames_bytes_size = (size_t)written_into_ssl;
/* Now try to read some data again. */
- result = do_ssl_read (impl->ssl, unprotected_bytes, unprotected_bytes_size);
- if (result == TSI_OK)
- {
- /* Don't forget to output the total number of bytes read. */
- *unprotected_bytes_size += output_bytes_offset;
- }
+ result = do_ssl_read(impl->ssl, unprotected_bytes, unprotected_bytes_size);
+ if (result == TSI_OK) {
+ /* Don't forget to output the total number of bytes read. */
+ *unprotected_bytes_size += output_bytes_offset;
+ }
return result;
}
-static void
-ssl_protector_destroy (tsi_frame_protector * self)
-{
- tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *) self;
- if (impl->buffer != NULL)
- free (impl->buffer);
- if (impl->ssl != NULL)
- SSL_free (impl->ssl);
- free (self);
+static void ssl_protector_destroy(tsi_frame_protector *self) {
+ tsi_ssl_frame_protector *impl = (tsi_ssl_frame_protector *)self;
+ if (impl->buffer != NULL) free(impl->buffer);
+ if (impl->ssl != NULL) SSL_free(impl->ssl);
+ free(self);
}
static const tsi_frame_protector_vtable frame_protector_vtable = {
- ssl_protector_protect, ssl_protector_protect_flush, ssl_protector_unprotect,
- ssl_protector_destroy,
+ ssl_protector_protect, ssl_protector_protect_flush, ssl_protector_unprotect,
+ ssl_protector_destroy,
};
/* --- tsi_handshaker methods implementation. ---*/
-static tsi_result
-ssl_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
+static tsi_result ssl_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
int bytes_read_from_ssl = 0;
- if (bytes == NULL || bytes_size == NULL || *bytes_size == 0 || *bytes_size > INT_MAX)
- {
- return TSI_INVALID_ARGUMENT;
- }
- GPR_ASSERT (*bytes_size <= INT_MAX);
- bytes_read_from_ssl = BIO_read (impl->from_ssl, bytes, (int) *bytes_size);
- if (bytes_read_from_ssl < 0)
- {
- *bytes_size = 0;
- if (!BIO_should_retry (impl->from_ssl))
- {
- impl->result = TSI_INTERNAL_ERROR;
- return impl->result;
- }
- else
- {
- return TSI_OK;
- }
+ if (bytes == NULL || bytes_size == NULL || *bytes_size == 0 ||
+ *bytes_size > INT_MAX) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ GPR_ASSERT(*bytes_size <= INT_MAX);
+ bytes_read_from_ssl = BIO_read(impl->from_ssl, bytes, (int)*bytes_size);
+ if (bytes_read_from_ssl < 0) {
+ *bytes_size = 0;
+ if (!BIO_should_retry(impl->from_ssl)) {
+ impl->result = TSI_INTERNAL_ERROR;
+ return impl->result;
+ } else {
+ return TSI_OK;
}
- *bytes_size = (size_t) bytes_read_from_ssl;
- return BIO_pending (impl->from_ssl) == 0 ? TSI_OK : TSI_INCOMPLETE_DATA;
+ }
+ *bytes_size = (size_t)bytes_read_from_ssl;
+ return BIO_pending(impl->from_ssl) == 0 ? TSI_OK : TSI_INCOMPLETE_DATA;
}
-static tsi_result
-ssl_handshaker_get_result (tsi_handshaker * self)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- if ((impl->result == TSI_HANDSHAKE_IN_PROGRESS) && SSL_is_init_finished (impl->ssl))
- {
- impl->result = TSI_OK;
- }
+static tsi_result ssl_handshaker_get_result(tsi_handshaker *self) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ if ((impl->result == TSI_HANDSHAKE_IN_PROGRESS) &&
+ SSL_is_init_finished(impl->ssl)) {
+ impl->result = TSI_OK;
+ }
return impl->result;
}
-static tsi_result
-ssl_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
+static tsi_result ssl_handshaker_process_bytes_from_peer(
+ tsi_handshaker *self, const unsigned char *bytes, size_t *bytes_size) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
int bytes_written_into_ssl_size = 0;
- if (bytes == NULL || bytes_size == 0 || *bytes_size > INT_MAX)
- {
- return TSI_INVALID_ARGUMENT;
- }
- GPR_ASSERT (*bytes_size <= INT_MAX);
- bytes_written_into_ssl_size = BIO_write (impl->into_ssl, bytes, (int) *bytes_size);
- if (bytes_written_into_ssl_size < 0)
- {
- gpr_log (GPR_ERROR, "Could not write to memory BIO.");
- impl->result = TSI_INTERNAL_ERROR;
- return impl->result;
- }
- *bytes_size = (size_t) bytes_written_into_ssl_size;
-
- if (!tsi_handshaker_is_in_progress (self))
- {
- impl->result = TSI_OK;
- return impl->result;
- }
- else
- {
- /* Get ready to get some bytes from SSL. */
- int ssl_result = SSL_do_handshake (impl->ssl);
- ssl_result = SSL_get_error (impl->ssl, ssl_result);
- switch (ssl_result)
- {
- case SSL_ERROR_WANT_READ:
- if (BIO_pending (impl->from_ssl) == 0)
- {
- /* We need more data. */
- return TSI_INCOMPLETE_DATA;
- }
- else
- {
- return TSI_OK;
- }
- case SSL_ERROR_NONE:
- return TSI_OK;
- default:
- {
- char err_str[256];
- ERR_error_string_n (ERR_get_error (), err_str, sizeof (err_str));
- gpr_log (GPR_ERROR, "Handshake failed with fatal error %s: %s.", ssl_error_string (ssl_result), err_str);
- impl->result = TSI_PROTOCOL_FAILURE;
- return impl->result;
- }
- }
+ if (bytes == NULL || bytes_size == 0 || *bytes_size > INT_MAX) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ GPR_ASSERT(*bytes_size <= INT_MAX);
+ bytes_written_into_ssl_size =
+ BIO_write(impl->into_ssl, bytes, (int)*bytes_size);
+ if (bytes_written_into_ssl_size < 0) {
+ gpr_log(GPR_ERROR, "Could not write to memory BIO.");
+ impl->result = TSI_INTERNAL_ERROR;
+ return impl->result;
+ }
+ *bytes_size = (size_t)bytes_written_into_ssl_size;
+
+ if (!tsi_handshaker_is_in_progress(self)) {
+ impl->result = TSI_OK;
+ return impl->result;
+ } else {
+ /* Get ready to get some bytes from SSL. */
+ int ssl_result = SSL_do_handshake(impl->ssl);
+ ssl_result = SSL_get_error(impl->ssl, ssl_result);
+ switch (ssl_result) {
+ case SSL_ERROR_WANT_READ:
+ if (BIO_pending(impl->from_ssl) == 0) {
+ /* We need more data. */
+ return TSI_INCOMPLETE_DATA;
+ } else {
+ return TSI_OK;
+ }
+ case SSL_ERROR_NONE:
+ return TSI_OK;
+ default: {
+ char err_str[256];
+ ERR_error_string_n(ERR_get_error(), err_str, sizeof(err_str));
+ gpr_log(GPR_ERROR, "Handshake failed with fatal error %s: %s.",
+ ssl_error_string(ssl_result), err_str);
+ impl->result = TSI_PROTOCOL_FAILURE;
+ return impl->result;
+ }
}
+ }
}
-static tsi_result
-ssl_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer)
-{
+static tsi_result ssl_handshaker_extract_peer(tsi_handshaker *self,
+ tsi_peer *peer) {
tsi_result result = TSI_OK;
const unsigned char *alpn_selected = NULL;
unsigned int alpn_selected_len;
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- X509 *peer_cert = SSL_get_peer_certificate (impl->ssl);
- if (peer_cert != NULL)
- {
- result = peer_from_x509 (peer_cert, 1, peer);
- X509_free (peer_cert);
- if (result != TSI_OK)
- return result;
- }
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ X509 *peer_cert = SSL_get_peer_certificate(impl->ssl);
+ if (peer_cert != NULL) {
+ result = peer_from_x509(peer_cert, 1, peer);
+ X509_free(peer_cert);
+ if (result != TSI_OK) return result;
+ }
#if TSI_OPENSSL_ALPN_SUPPORT
- SSL_get0_alpn_selected (impl->ssl, &alpn_selected, &alpn_selected_len);
+ SSL_get0_alpn_selected(impl->ssl, &alpn_selected, &alpn_selected_len);
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
- if (alpn_selected == NULL)
- {
- /* Try npn. */
- SSL_get0_next_proto_negotiated (impl->ssl, &alpn_selected, &alpn_selected_len);
- }
- if (alpn_selected != NULL)
- {
- size_t i;
- tsi_peer_property *new_properties = calloc (1, sizeof (tsi_peer_property) * (peer->property_count + 1));
- if (new_properties == NULL)
- return TSI_OUT_OF_RESOURCES;
- for (i = 0; i < peer->property_count; i++)
- {
- new_properties[i] = peer->properties[i];
- }
- result = tsi_construct_string_peer_property (TSI_SSL_ALPN_SELECTED_PROTOCOL, (const char *) alpn_selected, alpn_selected_len, &new_properties[peer->property_count]);
- if (result != TSI_OK)
- {
- free (new_properties);
- return result;
- }
- if (peer->properties != NULL)
- free (peer->properties);
- peer->property_count++;
- peer->properties = new_properties;
+ if (alpn_selected == NULL) {
+ /* Try npn. */
+ SSL_get0_next_proto_negotiated(impl->ssl, &alpn_selected,
+ &alpn_selected_len);
+ }
+ if (alpn_selected != NULL) {
+ size_t i;
+ tsi_peer_property *new_properties =
+ calloc(1, sizeof(tsi_peer_property) * (peer->property_count + 1));
+ if (new_properties == NULL) return TSI_OUT_OF_RESOURCES;
+ for (i = 0; i < peer->property_count; i++) {
+ new_properties[i] = peer->properties[i];
+ }
+ result = tsi_construct_string_peer_property(
+ TSI_SSL_ALPN_SELECTED_PROTOCOL, (const char *)alpn_selected,
+ alpn_selected_len, &new_properties[peer->property_count]);
+ if (result != TSI_OK) {
+ free(new_properties);
+ return result;
}
+ if (peer->properties != NULL) free(peer->properties);
+ peer->property_count++;
+ peer->properties = new_properties;
+ }
return result;
}
-static tsi_result
-ssl_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_output_protected_frame_size, tsi_frame_protector ** protector)
-{
- size_t actual_max_output_protected_frame_size = TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- tsi_ssl_frame_protector *protector_impl = calloc (1, sizeof (tsi_ssl_frame_protector));
- if (protector_impl == NULL)
- {
- return TSI_OUT_OF_RESOURCES;
- }
+static tsi_result ssl_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_output_protected_frame_size,
+ tsi_frame_protector **protector) {
+ size_t actual_max_output_protected_frame_size =
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ tsi_ssl_frame_protector *protector_impl =
+ calloc(1, sizeof(tsi_ssl_frame_protector));
+ if (protector_impl == NULL) {
+ return TSI_OUT_OF_RESOURCES;
+ }
- if (max_output_protected_frame_size != NULL)
- {
- if (*max_output_protected_frame_size > TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND)
- {
- *max_output_protected_frame_size = TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
- }
- else if (*max_output_protected_frame_size < TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND)
- {
- *max_output_protected_frame_size = TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND;
- }
- actual_max_output_protected_frame_size = *max_output_protected_frame_size;
- }
- protector_impl->buffer_size = actual_max_output_protected_frame_size - TSI_SSL_MAX_PROTECTION_OVERHEAD;
- protector_impl->buffer = malloc (protector_impl->buffer_size);
- if (protector_impl->buffer == NULL)
- {
- gpr_log (GPR_ERROR, "Could not allocated buffer for tsi_ssl_frame_protector.");
- free (protector_impl);
- return TSI_INTERNAL_ERROR;
- }
+ if (max_output_protected_frame_size != NULL) {
+ if (*max_output_protected_frame_size >
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND) {
+ *max_output_protected_frame_size =
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_UPPER_BOUND;
+ } else if (*max_output_protected_frame_size <
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND) {
+ *max_output_protected_frame_size =
+ TSI_SSL_MAX_PROTECTED_FRAME_SIZE_LOWER_BOUND;
+ }
+ actual_max_output_protected_frame_size = *max_output_protected_frame_size;
+ }
+ protector_impl->buffer_size =
+ actual_max_output_protected_frame_size - TSI_SSL_MAX_PROTECTION_OVERHEAD;
+ protector_impl->buffer = malloc(protector_impl->buffer_size);
+ if (protector_impl->buffer == NULL) {
+ gpr_log(GPR_ERROR,
+ "Could not allocated buffer for tsi_ssl_frame_protector.");
+ free(protector_impl);
+ return TSI_INTERNAL_ERROR;
+ }
/* Transfer ownership of ssl to the frame protector. It is OK as the caller
* cannot call anything else but destroy on the handshaker after this call. */
@@ -1078,106 +968,91 @@ ssl_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_outpu
return TSI_OK;
}
-static void
-ssl_handshaker_destroy (tsi_handshaker * self)
-{
- tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *) self;
- SSL_free (impl->ssl); /* The BIO objects are owned by ssl */
- free (impl);
+static void ssl_handshaker_destroy(tsi_handshaker *self) {
+ tsi_ssl_handshaker *impl = (tsi_ssl_handshaker *)self;
+ SSL_free(impl->ssl); /* The BIO objects are owned by ssl */
+ free(impl);
}
static const tsi_handshaker_vtable handshaker_vtable = {
- ssl_handshaker_get_bytes_to_send_to_peer,
- ssl_handshaker_process_bytes_from_peer,
- ssl_handshaker_get_result,
- ssl_handshaker_extract_peer,
- ssl_handshaker_create_frame_protector,
- ssl_handshaker_destroy,
+ ssl_handshaker_get_bytes_to_send_to_peer,
+ ssl_handshaker_process_bytes_from_peer,
+ ssl_handshaker_get_result,
+ ssl_handshaker_extract_peer,
+ ssl_handshaker_create_frame_protector,
+ ssl_handshaker_destroy,
};
/* --- tsi_ssl_handshaker_factory common methods. --- */
-tsi_result
-tsi_ssl_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- if (self == NULL || handshaker == NULL)
- return TSI_INVALID_ARGUMENT;
- return self->create_handshaker (self, server_name_indication, handshaker);
+tsi_result tsi_ssl_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ if (self == NULL || handshaker == NULL) return TSI_INVALID_ARGUMENT;
+ return self->create_handshaker(self, server_name_indication, handshaker);
}
-void
-tsi_ssl_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self)
-{
- if (self == NULL)
- return;
- self->destroy (self);
+void tsi_ssl_handshaker_factory_destroy(tsi_ssl_handshaker_factory *self) {
+ if (self == NULL) return;
+ self->destroy(self);
}
-static tsi_result
-create_tsi_ssl_handshaker (SSL_CTX * ctx, int is_client, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- SSL *ssl = SSL_new (ctx);
+static tsi_result create_tsi_ssl_handshaker(SSL_CTX *ctx, int is_client,
+ const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ SSL *ssl = SSL_new(ctx);
BIO *into_ssl = NULL;
BIO *from_ssl = NULL;
tsi_ssl_handshaker *impl = NULL;
*handshaker = NULL;
- if (ctx == NULL)
- {
- gpr_log (GPR_ERROR, "SSL Context is null. Should never happen.");
- return TSI_INTERNAL_ERROR;
- }
- if (ssl == NULL)
- {
- return TSI_OUT_OF_RESOURCES;
- }
- SSL_set_info_callback (ssl, ssl_info_callback);
-
- into_ssl = BIO_new (BIO_s_mem ());
- from_ssl = BIO_new (BIO_s_mem ());
- if (into_ssl == NULL || from_ssl == NULL)
- {
- gpr_log (GPR_ERROR, "BIO_new failed.");
- SSL_free (ssl);
- if (into_ssl != NULL)
- BIO_free (into_ssl);
- if (from_ssl != NULL)
- BIO_free (into_ssl);
- return TSI_OUT_OF_RESOURCES;
- }
- SSL_set_bio (ssl, into_ssl, from_ssl);
- if (is_client)
- {
- int ssl_result;
- SSL_set_connect_state (ssl);
- if (server_name_indication != NULL)
- {
- if (!SSL_set_tlsext_host_name (ssl, server_name_indication))
- {
- gpr_log (GPR_ERROR, "Invalid server name indication %s.", server_name_indication);
- SSL_free (ssl);
- return TSI_INTERNAL_ERROR;
- }
- }
- ssl_result = SSL_do_handshake (ssl);
- ssl_result = SSL_get_error (ssl, ssl_result);
- if (ssl_result != SSL_ERROR_WANT_READ)
- {
- gpr_log (GPR_ERROR, "Unexpected error received from first SSL_do_handshake call: %s", ssl_error_string (ssl_result));
- SSL_free (ssl);
- return TSI_INTERNAL_ERROR;
- }
+ if (ctx == NULL) {
+ gpr_log(GPR_ERROR, "SSL Context is null. Should never happen.");
+ return TSI_INTERNAL_ERROR;
+ }
+ if (ssl == NULL) {
+ return TSI_OUT_OF_RESOURCES;
+ }
+ SSL_set_info_callback(ssl, ssl_info_callback);
+
+ into_ssl = BIO_new(BIO_s_mem());
+ from_ssl = BIO_new(BIO_s_mem());
+ if (into_ssl == NULL || from_ssl == NULL) {
+ gpr_log(GPR_ERROR, "BIO_new failed.");
+ SSL_free(ssl);
+ if (into_ssl != NULL) BIO_free(into_ssl);
+ if (from_ssl != NULL) BIO_free(into_ssl);
+ return TSI_OUT_OF_RESOURCES;
+ }
+ SSL_set_bio(ssl, into_ssl, from_ssl);
+ if (is_client) {
+ int ssl_result;
+ SSL_set_connect_state(ssl);
+ if (server_name_indication != NULL) {
+ if (!SSL_set_tlsext_host_name(ssl, server_name_indication)) {
+ gpr_log(GPR_ERROR, "Invalid server name indication %s.",
+ server_name_indication);
+ SSL_free(ssl);
+ return TSI_INTERNAL_ERROR;
+ }
}
- else
- {
- SSL_set_accept_state (ssl);
+ ssl_result = SSL_do_handshake(ssl);
+ ssl_result = SSL_get_error(ssl, ssl_result);
+ if (ssl_result != SSL_ERROR_WANT_READ) {
+ gpr_log(GPR_ERROR,
+ "Unexpected error received from first SSL_do_handshake call: %s",
+ ssl_error_string(ssl_result));
+ SSL_free(ssl);
+ return TSI_INTERNAL_ERROR;
}
+ } else {
+ SSL_set_accept_state(ssl);
+ }
- impl = calloc (1, sizeof (tsi_ssl_handshaker));
- if (impl == NULL)
- {
- SSL_free (ssl);
- return TSI_OUT_OF_RESOURCES;
- }
+ impl = calloc(1, sizeof(tsi_ssl_handshaker));
+ if (impl == NULL) {
+ SSL_free(ssl);
+ return TSI_OUT_OF_RESOURCES;
+ }
impl->ssl = ssl;
impl->into_ssl = into_ssl;
impl->from_ssl = from_ssl;
@@ -1187,412 +1062,407 @@ create_tsi_ssl_handshaker (SSL_CTX * ctx, int is_client, const char *server_name
return TSI_OK;
}
-static int
-select_protocol_list (const unsigned char **out, unsigned char *outlen, const unsigned char *client_list, size_t client_list_len, const unsigned char *server_list, size_t server_list_len)
-{
+static int select_protocol_list(const unsigned char **out,
+ unsigned char *outlen,
+ const unsigned char *client_list,
+ size_t client_list_len,
+ const unsigned char *server_list,
+ size_t server_list_len) {
const unsigned char *client_current = client_list;
- while ((unsigned int) (client_current - client_list) < client_list_len)
- {
- unsigned char client_current_len = *(client_current++);
- const unsigned char *server_current = server_list;
- while ((server_current >= server_list) && (gpr_uintptr) (server_current - server_list) < server_list_len)
- {
- unsigned char server_current_len = *(server_current++);
- if ((client_current_len == server_current_len) && !memcmp (client_current, server_current, server_current_len))
- {
- *out = server_current;
- *outlen = server_current_len;
- return SSL_TLSEXT_ERR_OK;
- }
- server_current += server_current_len;
- }
- client_current += client_current_len;
+ while ((unsigned int)(client_current - client_list) < client_list_len) {
+ unsigned char client_current_len = *(client_current++);
+ const unsigned char *server_current = server_list;
+ while ((server_current >= server_list) &&
+ (gpr_uintptr)(server_current - server_list) < server_list_len) {
+ unsigned char server_current_len = *(server_current++);
+ if ((client_current_len == server_current_len) &&
+ !memcmp(client_current, server_current, server_current_len)) {
+ *out = server_current;
+ *outlen = server_current_len;
+ return SSL_TLSEXT_ERR_OK;
+ }
+ server_current += server_current_len;
}
+ client_current += client_current_len;
+ }
return SSL_TLSEXT_ERR_NOACK;
}
/* --- tsi_ssl__client_handshaker_factory methods implementation. --- */
-static tsi_result
-ssl_client_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- tsi_ssl_client_handshaker_factory *impl = (tsi_ssl_client_handshaker_factory *) self;
- return create_tsi_ssl_handshaker (impl->ssl_context, 1, server_name_indication, handshaker);
+static tsi_result ssl_client_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ tsi_ssl_client_handshaker_factory *impl =
+ (tsi_ssl_client_handshaker_factory *)self;
+ return create_tsi_ssl_handshaker(impl->ssl_context, 1, server_name_indication,
+ handshaker);
}
-static void
-ssl_client_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self)
-{
- tsi_ssl_client_handshaker_factory *impl = (tsi_ssl_client_handshaker_factory *) self;
- if (impl->ssl_context != NULL)
- SSL_CTX_free (impl->ssl_context);
- if (impl->alpn_protocol_list != NULL)
- free (impl->alpn_protocol_list);
- free (impl);
+static void ssl_client_handshaker_factory_destroy(
+ tsi_ssl_handshaker_factory *self) {
+ tsi_ssl_client_handshaker_factory *impl =
+ (tsi_ssl_client_handshaker_factory *)self;
+ if (impl->ssl_context != NULL) SSL_CTX_free(impl->ssl_context);
+ if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list);
+ free(impl);
}
-static int
-client_handshaker_factory_npn_callback (SSL * ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
-{
- tsi_ssl_client_handshaker_factory *factory = (tsi_ssl_client_handshaker_factory *) arg;
- return select_protocol_list ((const unsigned char **) out, outlen, factory->alpn_protocol_list, factory->alpn_protocol_list_length, in, inlen);
+static int client_handshaker_factory_npn_callback(SSL *ssl, unsigned char **out,
+ unsigned char *outlen,
+ const unsigned char *in,
+ unsigned int inlen,
+ void *arg) {
+ tsi_ssl_client_handshaker_factory *factory =
+ (tsi_ssl_client_handshaker_factory *)arg;
+ return select_protocol_list((const unsigned char **)out, outlen,
+ factory->alpn_protocol_list,
+ factory->alpn_protocol_list_length, in, inlen);
}
/* --- tsi_ssl_server_handshaker_factory methods implementation. --- */
-static tsi_result
-ssl_server_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker)
-{
- tsi_ssl_server_handshaker_factory *impl = (tsi_ssl_server_handshaker_factory *) self;
- if (impl->ssl_context_count == 0 || server_name_indication != NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
+static tsi_result ssl_server_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker) {
+ tsi_ssl_server_handshaker_factory *impl =
+ (tsi_ssl_server_handshaker_factory *)self;
+ if (impl->ssl_context_count == 0 || server_name_indication != NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
/* Create the handshaker with the first context. We will switch if needed
because of SNI in ssl_server_handshaker_factory_servername_callback. */
- return create_tsi_ssl_handshaker (impl->ssl_contexts[0], 0, NULL, handshaker);
+ return create_tsi_ssl_handshaker(impl->ssl_contexts[0], 0, NULL, handshaker);
}
-static void
-ssl_server_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self)
-{
- tsi_ssl_server_handshaker_factory *impl = (tsi_ssl_server_handshaker_factory *) self;
+static void ssl_server_handshaker_factory_destroy(
+ tsi_ssl_handshaker_factory *self) {
+ tsi_ssl_server_handshaker_factory *impl =
+ (tsi_ssl_server_handshaker_factory *)self;
size_t i;
- for (i = 0; i < impl->ssl_context_count; i++)
- {
- if (impl->ssl_contexts[i] != NULL)
- {
- SSL_CTX_free (impl->ssl_contexts[i]);
- tsi_peer_destruct (&impl->ssl_context_x509_subject_names[i]);
- }
+ for (i = 0; i < impl->ssl_context_count; i++) {
+ if (impl->ssl_contexts[i] != NULL) {
+ SSL_CTX_free(impl->ssl_contexts[i]);
+ tsi_peer_destruct(&impl->ssl_context_x509_subject_names[i]);
}
- if (impl->ssl_contexts != NULL)
- free (impl->ssl_contexts);
- if (impl->ssl_context_x509_subject_names != NULL)
- {
- free (impl->ssl_context_x509_subject_names);
- }
- if (impl->alpn_protocol_list != NULL)
- free (impl->alpn_protocol_list);
- free (impl);
+ }
+ if (impl->ssl_contexts != NULL) free(impl->ssl_contexts);
+ if (impl->ssl_context_x509_subject_names != NULL) {
+ free(impl->ssl_context_x509_subject_names);
+ }
+ if (impl->alpn_protocol_list != NULL) free(impl->alpn_protocol_list);
+ free(impl);
}
-static int
-does_entry_match_name (const char *entry, size_t entry_length, const char *name)
-{
+static int does_entry_match_name(const char *entry, size_t entry_length,
+ const char *name) {
const char *dot;
const char *name_subdomain = NULL;
- size_t name_length = strlen (name);
+ size_t name_length = strlen(name);
size_t name_subdomain_length;
- if (entry_length == 0)
- return 0;
+ if (entry_length == 0) return 0;
/* Take care of '.' terminations. */
- if (name[name_length - 1] == '.')
- {
- name_length--;
- }
- if (entry[entry_length - 1] == '.')
- {
- entry_length--;
- if (entry_length == 0)
- return 0;
- }
+ if (name[name_length - 1] == '.') {
+ name_length--;
+ }
+ if (entry[entry_length - 1] == '.') {
+ entry_length--;
+ if (entry_length == 0) return 0;
+ }
- if ((name_length == entry_length) && strncmp (name, entry, entry_length) == 0)
- {
- return 1; /* Perfect match. */
- }
- if (entry[0] != '*')
- return 0;
+ if ((name_length == entry_length) &&
+ strncmp(name, entry, entry_length) == 0) {
+ return 1; /* Perfect match. */
+ }
+ if (entry[0] != '*') return 0;
/* Wildchar subdomain matching. */
- if (entry_length < 3 || entry[1] != '.')
- { /* At least *.x */
- gpr_log (GPR_ERROR, "Invalid wildchar entry.");
- return 0;
- }
- name_subdomain = strchr (name, '.');
- if (name_subdomain == NULL)
- return 0;
- name_subdomain_length = strlen (name_subdomain);
- if (name_subdomain_length < 2)
+ if (entry_length < 3 || entry[1] != '.') { /* At least *.x */
+ gpr_log(GPR_ERROR, "Invalid wildchar entry.");
return 0;
- name_subdomain++; /* Starts after the dot. */
+ }
+ name_subdomain = strchr(name, '.');
+ if (name_subdomain == NULL) return 0;
+ name_subdomain_length = strlen(name_subdomain);
+ if (name_subdomain_length < 2) return 0;
+ name_subdomain++; /* Starts after the dot. */
name_subdomain_length--;
- entry += 2; /* Remove *. */
+ entry += 2; /* Remove *. */
entry_length -= 2;
- dot = strchr (name_subdomain, '.');
- if ((dot == NULL) || (dot == &name_subdomain[name_subdomain_length - 1]))
- {
- gpr_log (GPR_ERROR, "Invalid toplevel subdomain: %s", name_subdomain);
- return 0;
- }
- if (name_subdomain[name_subdomain_length - 1] == '.')
- {
- name_subdomain_length--;
- }
- return ((entry_length > 0) && (name_subdomain_length == entry_length) && strncmp (entry, name_subdomain, entry_length) == 0);
+ dot = strchr(name_subdomain, '.');
+ if ((dot == NULL) || (dot == &name_subdomain[name_subdomain_length - 1])) {
+ gpr_log(GPR_ERROR, "Invalid toplevel subdomain: %s", name_subdomain);
+ return 0;
+ }
+ if (name_subdomain[name_subdomain_length - 1] == '.') {
+ name_subdomain_length--;
+ }
+ return ((entry_length > 0) && (name_subdomain_length == entry_length) &&
+ strncmp(entry, name_subdomain, entry_length) == 0);
}
-static int
-ssl_server_handshaker_factory_servername_callback (SSL * ssl, int *ap, void *arg)
-{
- tsi_ssl_server_handshaker_factory *impl = (tsi_ssl_server_handshaker_factory *) arg;
+static int ssl_server_handshaker_factory_servername_callback(SSL *ssl, int *ap,
+ void *arg) {
+ tsi_ssl_server_handshaker_factory *impl =
+ (tsi_ssl_server_handshaker_factory *)arg;
size_t i = 0;
- const char *servername = SSL_get_servername (ssl, TLSEXT_NAMETYPE_host_name);
- if (servername == NULL || strlen (servername) == 0)
- {
- return SSL_TLSEXT_ERR_NOACK;
- }
+ const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
+ if (servername == NULL || strlen(servername) == 0) {
+ return SSL_TLSEXT_ERR_NOACK;
+ }
- for (i = 0; i < impl->ssl_context_count; i++)
- {
- if (tsi_ssl_peer_matches_name (&impl->ssl_context_x509_subject_names[i], servername))
- {
- SSL_set_SSL_CTX (ssl, impl->ssl_contexts[i]);
- return SSL_TLSEXT_ERR_OK;
- }
+ for (i = 0; i < impl->ssl_context_count; i++) {
+ if (tsi_ssl_peer_matches_name(&impl->ssl_context_x509_subject_names[i],
+ servername)) {
+ SSL_set_SSL_CTX(ssl, impl->ssl_contexts[i]);
+ return SSL_TLSEXT_ERR_OK;
}
- gpr_log (GPR_ERROR, "No match found for server name: %s.", servername);
+ }
+ gpr_log(GPR_ERROR, "No match found for server name: %s.", servername);
return SSL_TLSEXT_ERR_ALERT_WARNING;
}
#if TSI_OPENSSL_ALPN_SUPPORT
-static int
-server_handshaker_factory_alpn_callback (SSL * ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
-{
- tsi_ssl_server_handshaker_factory *factory = (tsi_ssl_server_handshaker_factory *) arg;
- return select_protocol_list (out, outlen, in, inlen, factory->alpn_protocol_list, factory->alpn_protocol_list_length);
+static int server_handshaker_factory_alpn_callback(
+ SSL *ssl, const unsigned char **out, unsigned char *outlen,
+ const unsigned char *in, unsigned int inlen, void *arg) {
+ tsi_ssl_server_handshaker_factory *factory =
+ (tsi_ssl_server_handshaker_factory *)arg;
+ return select_protocol_list(out, outlen, in, inlen,
+ factory->alpn_protocol_list,
+ factory->alpn_protocol_list_length);
}
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
-static int
-server_handshaker_factory_npn_advertised_callback (SSL * ssl, const unsigned char **out, unsigned int *outlen, void *arg)
-{
- tsi_ssl_server_handshaker_factory *factory = (tsi_ssl_server_handshaker_factory *) arg;
+static int server_handshaker_factory_npn_advertised_callback(
+ SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg) {
+ tsi_ssl_server_handshaker_factory *factory =
+ (tsi_ssl_server_handshaker_factory *)arg;
*out = factory->alpn_protocol_list;
- GPR_ASSERT (factory->alpn_protocol_list_length <= UINT_MAX);
- *outlen = (unsigned int) factory->alpn_protocol_list_length;
+ GPR_ASSERT(factory->alpn_protocol_list_length <= UINT_MAX);
+ *outlen = (unsigned int)factory->alpn_protocol_list_length;
return SSL_TLSEXT_ERR_OK;
}
/* --- tsi_ssl_handshaker_factory constructors. --- */
-tsi_result
-tsi_create_ssl_client_handshaker_factory (const unsigned char *pem_private_key, size_t pem_private_key_size, const unsigned char *pem_cert_chain, size_t pem_cert_chain_size, const unsigned char *pem_root_certs, size_t pem_root_certs_size, const char *cipher_list, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory)
-{
+tsi_result tsi_create_ssl_client_handshaker_factory(
+ const unsigned char *pem_private_key, size_t pem_private_key_size,
+ const unsigned char *pem_cert_chain, size_t pem_cert_chain_size,
+ const unsigned char *pem_root_certs, size_t pem_root_certs_size,
+ const char *cipher_list, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory) {
SSL_CTX *ssl_context = NULL;
tsi_ssl_client_handshaker_factory *impl = NULL;
tsi_result result = TSI_OK;
- gpr_once_init (&init_openssl_once, init_openssl);
+ gpr_once_init(&init_openssl_once, init_openssl);
- if (factory == NULL)
- return TSI_INVALID_ARGUMENT;
+ if (factory == NULL) return TSI_INVALID_ARGUMENT;
*factory = NULL;
- if (pem_root_certs == NULL)
- return TSI_INVALID_ARGUMENT;
+ if (pem_root_certs == NULL) return TSI_INVALID_ARGUMENT;
- ssl_context = SSL_CTX_new (TLSv1_2_method ());
- if (ssl_context == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create ssl context.");
- return TSI_INVALID_ARGUMENT;
- }
+ ssl_context = SSL_CTX_new(TLSv1_2_method());
+ if (ssl_context == NULL) {
+ gpr_log(GPR_ERROR, "Could not create ssl context.");
+ return TSI_INVALID_ARGUMENT;
+ }
- impl = calloc (1, sizeof (tsi_ssl_client_handshaker_factory));
- if (impl == NULL)
- {
- SSL_CTX_free (ssl_context);
- return TSI_OUT_OF_RESOURCES;
- }
+ impl = calloc(1, sizeof(tsi_ssl_client_handshaker_factory));
+ if (impl == NULL) {
+ SSL_CTX_free(ssl_context);
+ return TSI_OUT_OF_RESOURCES;
+ }
impl->ssl_context = ssl_context;
- do
- {
- result = populate_ssl_context (ssl_context, pem_private_key, pem_private_key_size, pem_cert_chain, pem_cert_chain_size, cipher_list);
- if (result != TSI_OK)
- break;
- result = ssl_ctx_load_verification_certs (ssl_context, pem_root_certs, pem_root_certs_size, NULL);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Cannot load server root certificates.");
- break;
- }
-
- if (num_alpn_protocols != 0)
- {
- result = build_alpn_protocol_name_list (alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Building alpn list failed with error %s.", tsi_result_to_string (result));
- break;
- }
+ do {
+ result =
+ populate_ssl_context(ssl_context, pem_private_key, pem_private_key_size,
+ pem_cert_chain, pem_cert_chain_size, cipher_list);
+ if (result != TSI_OK) break;
+ result = ssl_ctx_load_verification_certs(ssl_context, pem_root_certs,
+ pem_root_certs_size, NULL);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Cannot load server root certificates.");
+ break;
+ }
+
+ if (num_alpn_protocols != 0) {
+ result = build_alpn_protocol_name_list(
+ alpn_protocols, alpn_protocols_lengths, num_alpn_protocols,
+ &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Building alpn list failed with error %s.",
+ tsi_result_to_string(result));
+ break;
+ }
#if TSI_OPENSSL_ALPN_SUPPORT
- GPR_ASSERT (impl->alpn_protocol_list_length < UINT_MAX);
- if (SSL_CTX_set_alpn_protos (ssl_context, impl->alpn_protocol_list, (unsigned int) impl->alpn_protocol_list_length))
- {
- gpr_log (GPR_ERROR, "Could not set alpn protocol list to context.");
- result = TSI_INVALID_ARGUMENT;
- break;
- }
+ GPR_ASSERT(impl->alpn_protocol_list_length < UINT_MAX);
+ if (SSL_CTX_set_alpn_protos(
+ ssl_context, impl->alpn_protocol_list,
+ (unsigned int)impl->alpn_protocol_list_length)) {
+ gpr_log(GPR_ERROR, "Could not set alpn protocol list to context.");
+ result = TSI_INVALID_ARGUMENT;
+ break;
+ }
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
- SSL_CTX_set_next_proto_select_cb (ssl_context, client_handshaker_factory_npn_callback, impl);
- }
+ SSL_CTX_set_next_proto_select_cb(
+ ssl_context, client_handshaker_factory_npn_callback, impl);
}
- while (0);
- if (result != TSI_OK)
- {
- ssl_client_handshaker_factory_destroy (&impl->base);
- return result;
- }
- SSL_CTX_set_verify (ssl_context, SSL_VERIFY_PEER, NULL);
+ } while (0);
+ if (result != TSI_OK) {
+ ssl_client_handshaker_factory_destroy(&impl->base);
+ return result;
+ }
+ SSL_CTX_set_verify(ssl_context, SSL_VERIFY_PEER, NULL);
/* TODO(jboeuf): Add revocation verification. */
- impl->base.create_handshaker = ssl_client_handshaker_factory_create_handshaker;
+ impl->base.create_handshaker =
+ ssl_client_handshaker_factory_create_handshaker;
impl->base.destroy = ssl_client_handshaker_factory_destroy;
*factory = &impl->base;
return TSI_OK;
}
-tsi_result
-tsi_create_ssl_server_handshaker_factory (const unsigned char **pem_private_keys, const size_t * pem_private_keys_sizes, const unsigned char **pem_cert_chains, const size_t * pem_cert_chains_sizes, size_t key_cert_pair_count, const unsigned char *pem_client_root_certs, size_t pem_client_root_certs_size, int force_client_auth, const char *cipher_list, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory)
-{
+tsi_result tsi_create_ssl_server_handshaker_factory(
+ const unsigned char **pem_private_keys,
+ const size_t *pem_private_keys_sizes, const unsigned char **pem_cert_chains,
+ const size_t *pem_cert_chains_sizes, size_t key_cert_pair_count,
+ const unsigned char *pem_client_root_certs,
+ size_t pem_client_root_certs_size, int force_client_auth,
+ const char *cipher_list, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory) {
tsi_ssl_server_handshaker_factory *impl = NULL;
tsi_result result = TSI_OK;
size_t i = 0;
- gpr_once_init (&init_openssl_once, init_openssl);
+ gpr_once_init(&init_openssl_once, init_openssl);
- if (factory == NULL)
- return TSI_INVALID_ARGUMENT;
+ if (factory == NULL) return TSI_INVALID_ARGUMENT;
*factory = NULL;
- if (key_cert_pair_count == 0 || pem_private_keys == NULL || pem_cert_chains == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
+ if (key_cert_pair_count == 0 || pem_private_keys == NULL ||
+ pem_cert_chains == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
- impl = calloc (1, sizeof (tsi_ssl_server_handshaker_factory));
- if (impl == NULL)
- return TSI_OUT_OF_RESOURCES;
- impl->base.create_handshaker = ssl_server_handshaker_factory_create_handshaker;
+ impl = calloc(1, sizeof(tsi_ssl_server_handshaker_factory));
+ if (impl == NULL) return TSI_OUT_OF_RESOURCES;
+ impl->base.create_handshaker =
+ ssl_server_handshaker_factory_create_handshaker;
impl->base.destroy = ssl_server_handshaker_factory_destroy;
- impl->ssl_contexts = calloc (key_cert_pair_count, sizeof (SSL_CTX *));
- impl->ssl_context_x509_subject_names = calloc (key_cert_pair_count, sizeof (tsi_peer));
- if (impl->ssl_contexts == NULL || impl->ssl_context_x509_subject_names == NULL)
- {
- tsi_ssl_handshaker_factory_destroy (&impl->base);
- return TSI_OUT_OF_RESOURCES;
- }
+ impl->ssl_contexts = calloc(key_cert_pair_count, sizeof(SSL_CTX *));
+ impl->ssl_context_x509_subject_names =
+ calloc(key_cert_pair_count, sizeof(tsi_peer));
+ if (impl->ssl_contexts == NULL ||
+ impl->ssl_context_x509_subject_names == NULL) {
+ tsi_ssl_handshaker_factory_destroy(&impl->base);
+ return TSI_OUT_OF_RESOURCES;
+ }
impl->ssl_context_count = key_cert_pair_count;
- if (num_alpn_protocols > 0)
- {
- result = build_alpn_protocol_name_list (alpn_protocols, alpn_protocols_lengths, num_alpn_protocols, &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
- if (result != TSI_OK)
- {
- tsi_ssl_handshaker_factory_destroy (&impl->base);
- return result;
- }
+ if (num_alpn_protocols > 0) {
+ result = build_alpn_protocol_name_list(
+ alpn_protocols, alpn_protocols_lengths, num_alpn_protocols,
+ &impl->alpn_protocol_list, &impl->alpn_protocol_list_length);
+ if (result != TSI_OK) {
+ tsi_ssl_handshaker_factory_destroy(&impl->base);
+ return result;
}
+ }
+
+ for (i = 0; i < key_cert_pair_count; i++) {
+ do {
+ impl->ssl_contexts[i] = SSL_CTX_new(TLSv1_2_method());
+ if (impl->ssl_contexts[i] == NULL) {
+ gpr_log(GPR_ERROR, "Could not create ssl context.");
+ result = TSI_OUT_OF_RESOURCES;
+ break;
+ }
+ result = populate_ssl_context(
+ impl->ssl_contexts[i], pem_private_keys[i], pem_private_keys_sizes[i],
+ pem_cert_chains[i], pem_cert_chains_sizes[i], cipher_list);
+ if (result != TSI_OK) break;
+
+ if (pem_client_root_certs != NULL) {
+ int flags = SSL_VERIFY_PEER;
+ STACK_OF(X509_NAME) *root_names = NULL;
+ result = ssl_ctx_load_verification_certs(
+ impl->ssl_contexts[i], pem_client_root_certs,
+ pem_client_root_certs_size, &root_names);
+ if (result != TSI_OK) {
+ gpr_log(GPR_ERROR, "Invalid verification certs.");
+ break;
+ }
+ SSL_CTX_set_client_CA_list(impl->ssl_contexts[i], root_names);
+ if (force_client_auth) flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
+ SSL_CTX_set_verify(impl->ssl_contexts[i], flags, NULL);
+ /* TODO(jboeuf): Add revocation verification. */
+ }
+
+ result = extract_x509_subject_names_from_pem_cert(
+ pem_cert_chains[i], pem_cert_chains_sizes[i],
+ &impl->ssl_context_x509_subject_names[i]);
+ if (result != TSI_OK) break;
- for (i = 0; i < key_cert_pair_count; i++)
- {
- do
- {
- impl->ssl_contexts[i] = SSL_CTX_new (TLSv1_2_method ());
- if (impl->ssl_contexts[i] == NULL)
- {
- gpr_log (GPR_ERROR, "Could not create ssl context.");
- result = TSI_OUT_OF_RESOURCES;
- break;
- }
- result = populate_ssl_context (impl->ssl_contexts[i], pem_private_keys[i], pem_private_keys_sizes[i], pem_cert_chains[i], pem_cert_chains_sizes[i], cipher_list);
- if (result != TSI_OK)
- break;
-
- if (pem_client_root_certs != NULL)
- {
- int flags = SSL_VERIFY_PEER;
- STACK_OF (X509_NAME) * root_names = NULL;
- result = ssl_ctx_load_verification_certs (impl->ssl_contexts[i], pem_client_root_certs, pem_client_root_certs_size, &root_names);
- if (result != TSI_OK)
- {
- gpr_log (GPR_ERROR, "Invalid verification certs.");
- break;
- }
- SSL_CTX_set_client_CA_list (impl->ssl_contexts[i], root_names);
- if (force_client_auth)
- flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
- SSL_CTX_set_verify (impl->ssl_contexts[i], flags, NULL);
- /* TODO(jboeuf): Add revocation verification. */
- }
-
- result = extract_x509_subject_names_from_pem_cert (pem_cert_chains[i], pem_cert_chains_sizes[i], &impl->ssl_context_x509_subject_names[i]);
- if (result != TSI_OK)
- break;
-
- SSL_CTX_set_tlsext_servername_callback (impl->ssl_contexts[i], ssl_server_handshaker_factory_servername_callback);
- SSL_CTX_set_tlsext_servername_arg (impl->ssl_contexts[i], impl);
+ SSL_CTX_set_tlsext_servername_callback(
+ impl->ssl_contexts[i],
+ ssl_server_handshaker_factory_servername_callback);
+ SSL_CTX_set_tlsext_servername_arg(impl->ssl_contexts[i], impl);
#if TSI_OPENSSL_ALPN_SUPPORT
- SSL_CTX_set_alpn_select_cb (impl->ssl_contexts[i], server_handshaker_factory_alpn_callback, impl);
+ SSL_CTX_set_alpn_select_cb(impl->ssl_contexts[i],
+ server_handshaker_factory_alpn_callback, impl);
#endif /* TSI_OPENSSL_ALPN_SUPPORT */
- SSL_CTX_set_next_protos_advertised_cb (impl->ssl_contexts[i], server_handshaker_factory_npn_advertised_callback, impl);
- }
- while (0);
-
- if (result != TSI_OK)
- {
- tsi_ssl_handshaker_factory_destroy (&impl->base);
- return result;
- }
+ SSL_CTX_set_next_protos_advertised_cb(
+ impl->ssl_contexts[i],
+ server_handshaker_factory_npn_advertised_callback, impl);
+ } while (0);
+
+ if (result != TSI_OK) {
+ tsi_ssl_handshaker_factory_destroy(&impl->base);
+ return result;
}
+ }
*factory = &impl->base;
return TSI_OK;
}
/* --- tsi_ssl utils. --- */
-int
-tsi_ssl_peer_matches_name (const tsi_peer * peer, const char *name)
-{
+int tsi_ssl_peer_matches_name(const tsi_peer *peer, const char *name) {
size_t i = 0;
size_t san_count = 0;
const tsi_peer_property *cn_property = NULL;
/* For now reject what looks like an IP address. */
- if (looks_like_ip_address (name))
- return 0;
+ if (looks_like_ip_address(name)) return 0;
/* Check the SAN first. */
- for (i = 0; i < peer->property_count; i++)
- {
- const tsi_peer_property *property = &peer->properties[i];
- if (property->name == NULL)
- continue;
- if (strcmp (property->name, TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0)
- {
- san_count++;
- if (does_entry_match_name (property->value.data, property->value.length, name))
- {
- return 1;
- }
- }
- else if (strcmp (property->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0)
- {
- cn_property = property;
- }
+ for (i = 0; i < peer->property_count; i++) {
+ const tsi_peer_property *property = &peer->properties[i];
+ if (property->name == NULL) continue;
+ if (strcmp(property->name,
+ TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) {
+ san_count++;
+ if (does_entry_match_name(property->value.data, property->value.length,
+ name)) {
+ return 1;
+ }
+ } else if (strcmp(property->name,
+ TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) {
+ cn_property = property;
}
+ }
/* If there's no SAN, try the CN. */
- if (san_count == 0 && cn_property != NULL)
- {
- if (does_entry_match_name (cn_property->value.data, cn_property->value.length, name))
- {
- return 1;
- }
+ if (san_count == 0 && cn_property != NULL) {
+ if (does_entry_match_name(cn_property->value.data,
+ cn_property->value.length, name)) {
+ return 1;
}
+ }
- return 0; /* Not found. */
+ return 0; /* Not found. */
}
diff --git a/src/core/tsi/ssl_transport_security.h b/src/core/tsi/ssl_transport_security.h
index c3c4c2f973..51c0003a85 100644
--- a/src/core/tsi/ssl_transport_security.h
+++ b/src/core/tsi/ssl_transport_security.h
@@ -37,8 +37,7 @@
#include "src/core/tsi/transport_security_interface.h"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* Value for the TSI_CERTIFICATE_TYPE_PEER_PROPERTY property for X509 certs. */
@@ -56,7 +55,7 @@ extern "C"
This object creates tsi_handshaker objects implemented in terms of the
TLS 1.2 specificiation. */
- typedef struct tsi_ssl_handshaker_factory tsi_ssl_handshaker_factory;
+typedef struct tsi_ssl_handshaker_factory tsi_ssl_handshaker_factory;
/* Creates a client handshaker factory.
- pem_private_key is the buffer containing the PEM encoding of the client's
@@ -85,7 +84,13 @@ extern "C"
- This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
where a parameter is invalid. */
- tsi_result tsi_create_ssl_client_handshaker_factory (const unsigned char *pem_private_key, size_t pem_private_key_size, const unsigned char *pem_cert_chain, size_t pem_cert_chain_size, const unsigned char *pem_root_certs, size_t pem_root_certs_size, const char *cipher_suites, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory);
+tsi_result tsi_create_ssl_client_handshaker_factory(
+ const unsigned char *pem_private_key, size_t pem_private_key_size,
+ const unsigned char *pem_cert_chain, size_t pem_cert_chain_size,
+ const unsigned char *pem_root_certs, size_t pem_root_certs_size,
+ const char *cipher_suites, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory);
/* Creates a server handshaker factory.
- version indicates which version of the specification to use.
@@ -125,7 +130,15 @@ extern "C"
- This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
where a parameter is invalid. */
- tsi_result tsi_create_ssl_server_handshaker_factory (const unsigned char **pem_private_keys, const size_t * pem_private_keys_sizes, const unsigned char **pem_cert_chains, const size_t * pem_cert_chains_sizes, size_t key_cert_pair_count, const unsigned char *pem_client_root_certs, size_t pem_client_root_certs_size, int force_client_auth, const char *cipher_suites, const unsigned char **alpn_protocols, const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols, tsi_ssl_handshaker_factory ** factory);
+tsi_result tsi_create_ssl_server_handshaker_factory(
+ const unsigned char **pem_private_keys,
+ const size_t *pem_private_keys_sizes, const unsigned char **pem_cert_chains,
+ const size_t *pem_cert_chains_sizes, size_t key_cert_pair_count,
+ const unsigned char *pem_client_root_certs,
+ size_t pem_client_root_certs_size, int force_client_auth,
+ const char *cipher_suites, const unsigned char **alpn_protocols,
+ const unsigned char *alpn_protocols_lengths, uint16_t num_alpn_protocols,
+ tsi_ssl_handshaker_factory **factory);
/* Creates a handshaker.
- self is the factory from which the handshaker will be created.
@@ -137,11 +150,13 @@ extern "C"
- This method returns TSI_OK on success or TSI_INVALID_PARAMETER in the case
where a parameter is invalid. */
- tsi_result tsi_ssl_handshaker_factory_create_handshaker (tsi_ssl_handshaker_factory * self, const char *server_name_indication, tsi_handshaker ** handshaker);
+tsi_result tsi_ssl_handshaker_factory_create_handshaker(
+ tsi_ssl_handshaker_factory *self, const char *server_name_indication,
+ tsi_handshaker **handshaker);
/* Destroys the handshaker factory. WARNING: it is unsafe to destroy a factory
while handshakers created with this factory are still in use. */
- void tsi_ssl_handshaker_factory_destroy (tsi_ssl_handshaker_factory * self);
+void tsi_ssl_handshaker_factory_destroy(tsi_ssl_handshaker_factory *self);
/* Util that checks that an ssl peer matches a specific name.
Still TODO(jboeuf):
@@ -149,10 +164,10 @@ extern "C"
- handle %encoded chars.
- handle public suffix wildchar more strictly (e.g. *.co.uk)
- handle IP addresses in SAN. */
- int tsi_ssl_peer_matches_name (const tsi_peer * peer, const char *name);
+int tsi_ssl_peer_matches_name(const tsi_peer *peer, const char *name);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_SSL_TRANSPORT_SECURITY_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_SSL_TRANSPORT_SECURITY_H */
diff --git a/src/core/tsi/transport_security.c b/src/core/tsi/transport_security.c
index c26ee40c4c..c39e584496 100644
--- a/src/core/tsi/transport_security.c
+++ b/src/core/tsi/transport_security.c
@@ -42,28 +42,21 @@ int tsi_tracing_enabled = 0;
/* --- Utils. --- */
-char *
-tsi_strdup (const char *src)
-{
+char *tsi_strdup(const char *src) {
char *dst;
size_t len;
- if (!src)
- return NULL;
- len = strlen (src) + 1;
- dst = malloc (len);
- if (!dst)
- return NULL;
- memcpy (dst, src, len);
+ if (!src) return NULL;
+ len = strlen(src) + 1;
+ dst = malloc(len);
+ if (!dst) return NULL;
+ memcpy(dst, src, len);
return dst;
}
/* --- tsi_result common implementation. --- */
-const char *
-tsi_result_to_string (tsi_result result)
-{
- switch (result)
- {
+const char *tsi_result_to_string(tsi_result result) {
+ switch (result) {
case TSI_OK:
return "TSI_OK";
case TSI_UNKNOWN_ERROR:
@@ -92,228 +85,196 @@ tsi_result_to_string (tsi_result result)
return "TSI_OUT_OF_RESOURCES";
default:
return "UNKNOWN";
- }
+ }
}
/* --- tsi_frame_protector common implementation. ---
Calls specific implementation after state/input validation. */
-tsi_result
-tsi_frame_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size)
-{
- if (self == NULL || unprotected_bytes == NULL || unprotected_bytes_size == NULL || protected_output_frames == NULL || protected_output_frames_size == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
- return self->vtable->protect (self, unprotected_bytes, unprotected_bytes_size, protected_output_frames, protected_output_frames_size);
+tsi_result tsi_frame_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size) {
+ if (self == NULL || unprotected_bytes == NULL ||
+ unprotected_bytes_size == NULL || protected_output_frames == NULL ||
+ protected_output_frames_size == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ return self->vtable->protect(self, unprotected_bytes, unprotected_bytes_size,
+ protected_output_frames,
+ protected_output_frames_size);
}
-tsi_result
-tsi_frame_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size)
-{
- if (self == NULL || protected_output_frames == NULL || protected_output_frames == NULL || still_pending_size == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
- return self->vtable->protect_flush (self, protected_output_frames, protected_output_frames_size, still_pending_size);
+tsi_result tsi_frame_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size) {
+ if (self == NULL || protected_output_frames == NULL ||
+ protected_output_frames == NULL || still_pending_size == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ return self->vtable->protect_flush(self, protected_output_frames,
+ protected_output_frames_size,
+ still_pending_size);
}
-tsi_result
-tsi_frame_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size)
-{
- if (self == NULL || protected_frames_bytes == NULL || protected_frames_bytes_size == NULL || unprotected_bytes == NULL || unprotected_bytes_size == NULL)
- {
- return TSI_INVALID_ARGUMENT;
- }
- return self->vtable->unprotect (self, protected_frames_bytes, protected_frames_bytes_size, unprotected_bytes, unprotected_bytes_size);
+tsi_result tsi_frame_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size) {
+ if (self == NULL || protected_frames_bytes == NULL ||
+ protected_frames_bytes_size == NULL || unprotected_bytes == NULL ||
+ unprotected_bytes_size == NULL) {
+ return TSI_INVALID_ARGUMENT;
+ }
+ return self->vtable->unprotect(self, protected_frames_bytes,
+ protected_frames_bytes_size, unprotected_bytes,
+ unprotected_bytes_size);
}
-void
-tsi_frame_protector_destroy (tsi_frame_protector * self)
-{
- if (self == NULL)
- return;
- self->vtable->destroy (self);
+void tsi_frame_protector_destroy(tsi_frame_protector *self) {
+ if (self == NULL) return;
+ self->vtable->destroy(self);
}
/* --- tsi_handshaker common implementation. ---
Calls specific implementation after state/input validation. */
-tsi_result
-tsi_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size)
-{
- if (self == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
- return TSI_FAILED_PRECONDITION;
- return self->vtable->get_bytes_to_send_to_peer (self, bytes, bytes_size);
+tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size) {
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ return self->vtable->get_bytes_to_send_to_peer(self, bytes, bytes_size);
}
-tsi_result
-tsi_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size)
-{
- if (self == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
- return TSI_FAILED_PRECONDITION;
- return self->vtable->process_bytes_from_peer (self, bytes, bytes_size);
+tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self,
+ const unsigned char *bytes,
+ size_t *bytes_size) {
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ return self->vtable->process_bytes_from_peer(self, bytes, bytes_size);
}
-tsi_result
-tsi_handshaker_get_result (tsi_handshaker * self)
-{
- if (self == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
- return TSI_FAILED_PRECONDITION;
- return self->vtable->get_result (self);
+tsi_result tsi_handshaker_get_result(tsi_handshaker *self) {
+ if (self == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ return self->vtable->get_result(self);
}
-tsi_result
-tsi_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer)
-{
- if (self == NULL || peer == NULL)
- return TSI_INVALID_ARGUMENT;
- memset (peer, 0, sizeof (tsi_peer));
- if (self->frame_protector_created)
+tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer) {
+ if (self == NULL || peer == NULL) return TSI_INVALID_ARGUMENT;
+ memset(peer, 0, sizeof(tsi_peer));
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ if (tsi_handshaker_get_result(self) != TSI_OK) {
return TSI_FAILED_PRECONDITION;
- if (tsi_handshaker_get_result (self) != TSI_OK)
- {
- return TSI_FAILED_PRECONDITION;
- }
- return self->vtable->extract_peer (self, peer);
+ }
+ return self->vtable->extract_peer(self, peer);
}
-tsi_result
-tsi_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_protected_frame_size, tsi_frame_protector ** protector)
-{
+tsi_result tsi_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_protected_frame_size,
+ tsi_frame_protector **protector) {
tsi_result result;
- if (self == NULL || protector == NULL)
- return TSI_INVALID_ARGUMENT;
- if (self->frame_protector_created)
+ if (self == NULL || protector == NULL) return TSI_INVALID_ARGUMENT;
+ if (self->frame_protector_created) return TSI_FAILED_PRECONDITION;
+ if (tsi_handshaker_get_result(self) != TSI_OK) {
return TSI_FAILED_PRECONDITION;
- if (tsi_handshaker_get_result (self) != TSI_OK)
- {
- return TSI_FAILED_PRECONDITION;
- }
- result = self->vtable->create_frame_protector (self, max_protected_frame_size, protector);
- if (result == TSI_OK)
- {
- self->frame_protector_created = 1;
- }
+ }
+ result = self->vtable->create_frame_protector(self, max_protected_frame_size,
+ protector);
+ if (result == TSI_OK) {
+ self->frame_protector_created = 1;
+ }
return result;
}
-void
-tsi_handshaker_destroy (tsi_handshaker * self)
-{
- if (self == NULL)
- return;
- self->vtable->destroy (self);
+void tsi_handshaker_destroy(tsi_handshaker *self) {
+ if (self == NULL) return;
+ self->vtable->destroy(self);
}
/* --- tsi_peer implementation. --- */
-tsi_peer_property
-tsi_init_peer_property (void)
-{
+tsi_peer_property tsi_init_peer_property(void) {
tsi_peer_property property;
- memset (&property, 0, sizeof (tsi_peer_property));
+ memset(&property, 0, sizeof(tsi_peer_property));
return property;
}
-static void
-tsi_peer_destroy_list_property (tsi_peer_property * children, size_t child_count)
-{
+static void tsi_peer_destroy_list_property(tsi_peer_property *children,
+ size_t child_count) {
size_t i;
- for (i = 0; i < child_count; i++)
- {
- tsi_peer_property_destruct (&children[i]);
- }
- free (children);
+ for (i = 0; i < child_count; i++) {
+ tsi_peer_property_destruct(&children[i]);
+ }
+ free(children);
}
-void
-tsi_peer_property_destruct (tsi_peer_property * property)
-{
- if (property->name != NULL)
- {
- free (property->name);
- }
- if (property->value.data != NULL)
- {
- free (property->value.data);
- }
- *property = tsi_init_peer_property (); /* Reset everything to 0. */
+void tsi_peer_property_destruct(tsi_peer_property *property) {
+ if (property->name != NULL) {
+ free(property->name);
+ }
+ if (property->value.data != NULL) {
+ free(property->value.data);
+ }
+ *property = tsi_init_peer_property(); /* Reset everything to 0. */
}
-void
-tsi_peer_destruct (tsi_peer * self)
-{
- if (self == NULL)
- return;
- if (self->properties != NULL)
- {
- tsi_peer_destroy_list_property (self->properties, self->property_count);
- self->properties = NULL;
- }
+void tsi_peer_destruct(tsi_peer *self) {
+ if (self == NULL) return;
+ if (self->properties != NULL) {
+ tsi_peer_destroy_list_property(self->properties, self->property_count);
+ self->properties = NULL;
+ }
self->property_count = 0;
}
-tsi_result
-tsi_construct_allocated_string_peer_property (const char *name, size_t value_length, tsi_peer_property * property)
-{
- *property = tsi_init_peer_property ();
- if (name != NULL)
- {
- property->name = tsi_strdup (name);
- if (property->name == NULL)
- return TSI_OUT_OF_RESOURCES;
- }
- if (value_length > 0)
- {
- property->value.data = calloc (1, value_length);
- if (property->value.data == NULL)
- {
- tsi_peer_property_destruct (property);
- return TSI_OUT_OF_RESOURCES;
- }
- property->value.length = value_length;
+tsi_result tsi_construct_allocated_string_peer_property(
+ const char *name, size_t value_length, tsi_peer_property *property) {
+ *property = tsi_init_peer_property();
+ if (name != NULL) {
+ property->name = tsi_strdup(name);
+ if (property->name == NULL) return TSI_OUT_OF_RESOURCES;
+ }
+ if (value_length > 0) {
+ property->value.data = calloc(1, value_length);
+ if (property->value.data == NULL) {
+ tsi_peer_property_destruct(property);
+ return TSI_OUT_OF_RESOURCES;
}
+ property->value.length = value_length;
+ }
return TSI_OK;
}
-tsi_result
-tsi_construct_string_peer_property_from_cstring (const char *name, const char *value, tsi_peer_property * property)
-{
- return tsi_construct_string_peer_property (name, value, strlen (value), property);
+tsi_result tsi_construct_string_peer_property_from_cstring(
+ const char *name, const char *value, tsi_peer_property *property) {
+ return tsi_construct_string_peer_property(name, value, strlen(value),
+ property);
}
-tsi_result
-tsi_construct_string_peer_property (const char *name, const char *value, size_t value_length, tsi_peer_property * property)
-{
- tsi_result result = tsi_construct_allocated_string_peer_property (name, value_length, property);
- if (result != TSI_OK)
- return result;
- if (value_length > 0)
- {
- memcpy (property->value.data, value, value_length);
- }
+tsi_result tsi_construct_string_peer_property(const char *name,
+ const char *value,
+ size_t value_length,
+ tsi_peer_property *property) {
+ tsi_result result = tsi_construct_allocated_string_peer_property(
+ name, value_length, property);
+ if (result != TSI_OK) return result;
+ if (value_length > 0) {
+ memcpy(property->value.data, value, value_length);
+ }
return TSI_OK;
}
-tsi_result
-tsi_construct_peer (size_t property_count, tsi_peer * peer)
-{
- memset (peer, 0, sizeof (tsi_peer));
- if (property_count > 0)
- {
- peer->properties = calloc (property_count, sizeof (tsi_peer_property));
- if (peer->properties == NULL)
- return TSI_OUT_OF_RESOURCES;
- peer->property_count = property_count;
- }
+tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer) {
+ memset(peer, 0, sizeof(tsi_peer));
+ if (property_count > 0) {
+ peer->properties = calloc(property_count, sizeof(tsi_peer_property));
+ if (peer->properties == NULL) return TSI_OUT_OF_RESOURCES;
+ peer->property_count = property_count;
+ }
return TSI_OK;
}
diff --git a/src/core/tsi/transport_security.h b/src/core/tsi/transport_security.h
index 3ae82a31cc..4077737473 100644
--- a/src/core/tsi/transport_security.h
+++ b/src/core/tsi/transport_security.h
@@ -37,58 +37,75 @@
#include "src/core/tsi/transport_security_interface.h"
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
- extern int tsi_tracing_enabled;
+extern int tsi_tracing_enabled;
/* Base for tsi_frame_protector implementations.
See transport_security_interface.h for documentation. */
- typedef struct
- {
- tsi_result (*protect) (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size);
- tsi_result (*protect_flush) (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size);
- tsi_result (*unprotect) (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size);
- void (*destroy) (tsi_frame_protector * self);
- } tsi_frame_protector_vtable;
+typedef struct {
+ tsi_result (*protect)(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size);
+ tsi_result (*protect_flush)(tsi_frame_protector *self,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size,
+ size_t *still_pending_size);
+ tsi_result (*unprotect)(tsi_frame_protector *self,
+ const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size,
+ unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size);
+ void (*destroy)(tsi_frame_protector *self);
+} tsi_frame_protector_vtable;
- struct tsi_frame_protector
- {
- const tsi_frame_protector_vtable *vtable;
- };
+struct tsi_frame_protector {
+ const tsi_frame_protector_vtable *vtable;
+};
/* Base for tsi_handshaker implementations.
See transport_security_interface.h for documentation. */
- typedef struct
- {
- tsi_result (*get_bytes_to_send_to_peer) (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size);
- tsi_result (*process_bytes_from_peer) (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size);
- tsi_result (*get_result) (tsi_handshaker * self);
- tsi_result (*extract_peer) (tsi_handshaker * self, tsi_peer * peer);
- tsi_result (*create_frame_protector) (tsi_handshaker * self, size_t * max_protected_frame_size, tsi_frame_protector ** protector);
- void (*destroy) (tsi_handshaker * self);
- } tsi_handshaker_vtable;
+typedef struct {
+ tsi_result (*get_bytes_to_send_to_peer)(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size);
+ tsi_result (*process_bytes_from_peer)(tsi_handshaker *self,
+ const unsigned char *bytes,
+ size_t *bytes_size);
+ tsi_result (*get_result)(tsi_handshaker *self);
+ tsi_result (*extract_peer)(tsi_handshaker *self, tsi_peer *peer);
+ tsi_result (*create_frame_protector)(tsi_handshaker *self,
+ size_t *max_protected_frame_size,
+ tsi_frame_protector **protector);
+ void (*destroy)(tsi_handshaker *self);
+} tsi_handshaker_vtable;
- struct tsi_handshaker
- {
- const tsi_handshaker_vtable *vtable;
- int frame_protector_created;
- };
+struct tsi_handshaker {
+ const tsi_handshaker_vtable *vtable;
+ int frame_protector_created;
+};
/* Peer and property construction/destruction functions. */
- tsi_result tsi_construct_peer (size_t property_count, tsi_peer * peer);
- tsi_peer_property tsi_init_peer_property (void);
- void tsi_peer_property_destruct (tsi_peer_property * property);
- tsi_result tsi_construct_string_peer_property (const char *name, const char *value, size_t value_length, tsi_peer_property * property);
- tsi_result tsi_construct_allocated_string_peer_property (const char *name, size_t value_length, tsi_peer_property * property);
- tsi_result tsi_construct_string_peer_property_from_cstring (const char *name, const char *value, tsi_peer_property * property);
+tsi_result tsi_construct_peer(size_t property_count, tsi_peer *peer);
+tsi_peer_property tsi_init_peer_property(void);
+void tsi_peer_property_destruct(tsi_peer_property *property);
+tsi_result tsi_construct_string_peer_property(const char *name,
+ const char *value,
+ size_t value_length,
+ tsi_peer_property *property);
+tsi_result tsi_construct_allocated_string_peer_property(
+ const char *name, size_t value_length, tsi_peer_property *property);
+tsi_result tsi_construct_string_peer_property_from_cstring(
+ const char *name, const char *value, tsi_peer_property *property);
/* Utils. */
- char *tsi_strdup (const char *src); /* Sadly, no strdup in C89. */
+char *tsi_strdup(const char *src); /* Sadly, no strdup in C89. */
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_H */
diff --git a/src/core/tsi/transport_security_interface.h b/src/core/tsi/transport_security_interface.h
index e43b31fd06..69ee17ae91 100644
--- a/src/core/tsi/transport_security_interface.h
+++ b/src/core/tsi/transport_security_interface.h
@@ -38,42 +38,40 @@
#include <stdlib.h>
#ifdef __cplusplus
-extern "C"
-{
+extern "C" {
#endif
/* --- tsi result --- */
- typedef enum
- {
- TSI_OK = 0,
- TSI_UNKNOWN_ERROR = 1,
- TSI_INVALID_ARGUMENT = 2,
- TSI_PERMISSION_DENIED = 3,
- TSI_INCOMPLETE_DATA = 4,
- TSI_FAILED_PRECONDITION = 5,
- TSI_UNIMPLEMENTED = 6,
- TSI_INTERNAL_ERROR = 7,
- TSI_DATA_CORRUPTED = 8,
- TSI_NOT_FOUND = 9,
- TSI_PROTOCOL_FAILURE = 10,
- TSI_HANDSHAKE_IN_PROGRESS = 11,
- TSI_OUT_OF_RESOURCES = 12
- } tsi_result;
-
- const char *tsi_result_to_string (tsi_result result);
+typedef enum {
+ TSI_OK = 0,
+ TSI_UNKNOWN_ERROR = 1,
+ TSI_INVALID_ARGUMENT = 2,
+ TSI_PERMISSION_DENIED = 3,
+ TSI_INCOMPLETE_DATA = 4,
+ TSI_FAILED_PRECONDITION = 5,
+ TSI_UNIMPLEMENTED = 6,
+ TSI_INTERNAL_ERROR = 7,
+ TSI_DATA_CORRUPTED = 8,
+ TSI_NOT_FOUND = 9,
+ TSI_PROTOCOL_FAILURE = 10,
+ TSI_HANDSHAKE_IN_PROGRESS = 11,
+ TSI_OUT_OF_RESOURCES = 12
+} tsi_result;
+
+const char *tsi_result_to_string(tsi_result result);
/* --- tsi tracing --- */
/* Set this early to avoid races */
- extern int tsi_tracing_enabled;
+extern int tsi_tracing_enabled;
/* --- tsi_frame_protector object ---
This object protects and unprotects buffers once the handshake is done.
Implementations of this object must be thread compatible. */
- typedef struct tsi_frame_protector tsi_frame_protector;
+typedef struct tsi_frame_protector tsi_frame_protector;
/* Outputs protected frames.
- unprotected_bytes is an input only parameter and points to the data
@@ -128,7 +126,11 @@ extern "C"
if (result != TSI_OK) HandleError(result);
------------------------------------------------------------------------ */
- tsi_result tsi_frame_protector_protect (tsi_frame_protector * self, const unsigned char *unprotected_bytes, size_t * unprotected_bytes_size, unsigned char *protected_output_frames, size_t * protected_output_frames_size);
+tsi_result tsi_frame_protector_protect(tsi_frame_protector *self,
+ const unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size,
+ unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size);
/* Indicates that we need to flush the bytes buffered in the protector and get
the resulting frame.
@@ -138,7 +140,9 @@ extern "C"
caller to specify how many bytes are available in protected_output_frames.
- still_pending_bytes is an output parameter indicating the number of bytes
that still need to be flushed from the protector.*/
- tsi_result tsi_frame_protector_protect_flush (tsi_frame_protector * self, unsigned char *protected_output_frames, size_t * protected_output_frames_size, size_t * still_pending_size);
+tsi_result tsi_frame_protector_protect_flush(
+ tsi_frame_protector *self, unsigned char *protected_output_frames,
+ size_t *protected_output_frames_size, size_t *still_pending_size);
/* Outputs unprotected bytes.
- protected_frames_bytes is an input only parameter and points to the
@@ -162,10 +166,13 @@ extern "C"
unprotected_bytes_size will be set to 0 and cases where the internal buffer
needs to be read before new protected data can be processed in which case
protected_frames_size will be set to 0. */
- tsi_result tsi_frame_protector_unprotect (tsi_frame_protector * self, const unsigned char *protected_frames_bytes, size_t * protected_frames_bytes_size, unsigned char *unprotected_bytes, size_t * unprotected_bytes_size);
+tsi_result tsi_frame_protector_unprotect(
+ tsi_frame_protector *self, const unsigned char *protected_frames_bytes,
+ size_t *protected_frames_bytes_size, unsigned char *unprotected_bytes,
+ size_t *unprotected_bytes_size);
/* Destroys the tsi_frame_protector object. */
- void tsi_frame_protector_destroy (tsi_frame_protector * self);
+void tsi_frame_protector_destroy(tsi_frame_protector *self);
/* --- tsi_peer objects ---
@@ -176,24 +183,21 @@ extern "C"
/* Property values may contain NULL characters just like C++ strings.
The length field gives the length of the string. */
- typedef struct tsi_peer_property
- {
- char *name;
- struct
- {
- char *data;
- size_t length;
- } value;
- } tsi_peer_property;
-
- typedef struct
- {
- tsi_peer_property *properties;
- size_t property_count;
- } tsi_peer;
+typedef struct tsi_peer_property {
+ char *name;
+ struct {
+ char *data;
+ size_t length;
+ } value;
+} tsi_peer_property;
+
+typedef struct {
+ tsi_peer_property *properties;
+ size_t property_count;
+} tsi_peer;
/* Destructs the tsi_peer object. */
- void tsi_peer_destruct (tsi_peer * self);
+void tsi_peer_destruct(tsi_peer *self);
/* --- tsi_handshaker objects ----
@@ -262,7 +266,7 @@ extern "C"
}
...
------------------------------------------------------------------------ */
- typedef struct tsi_handshaker tsi_handshaker;
+typedef struct tsi_handshaker tsi_handshaker;
/* Gets bytes that need to be sent to the peer.
- bytes is the buffer that will be written with the data to be sent to the
@@ -275,7 +279,9 @@ extern "C"
needs to be called again to get all the bytes to send to the peer (there
was more data to write than the specified bytes_size). In case of a fatal
error in the handshake, another specific error code is returned. */
- tsi_result tsi_handshaker_get_bytes_to_send_to_peer (tsi_handshaker * self, unsigned char *bytes, size_t * bytes_size);
+tsi_result tsi_handshaker_get_bytes_to_send_to_peer(tsi_handshaker *self,
+ unsigned char *bytes,
+ size_t *bytes_size);
/* Processes bytes received from the peer.
- bytes is the buffer containing the data.
@@ -286,14 +292,16 @@ extern "C"
needs to be called again to complete the data needed for processing. In
case of a fatal error in the handshake, another specific error code is
returned. */
- tsi_result tsi_handshaker_process_bytes_from_peer (tsi_handshaker * self, const unsigned char *bytes, size_t * bytes_size);
+tsi_result tsi_handshaker_process_bytes_from_peer(tsi_handshaker *self,
+ const unsigned char *bytes,
+ size_t *bytes_size);
/* Gets the result of the handshaker.
Returns TSI_OK if the hanshake completed successfully and there has been no
errors. Returns TSI_HANDSHAKE_IN_PROGRESS if the handshaker is not done yet
but no error has been encountered so far. Otherwise the handshaker failed
with the returned error. */
- tsi_result tsi_handshaker_get_result (tsi_handshaker * self);
+tsi_result tsi_handshaker_get_result(tsi_handshaker *self);
/* Returns 1 if the handshake is in progress, 0 otherwise. */
#define tsi_handshaker_is_in_progress(h) \
@@ -303,7 +311,7 @@ extern "C"
tsi_handshaker_is_in_progress returns 1, it returns TSI_OK otherwise
assuming the handshaker is not in a fatal error state.
The caller is responsible for destructing the peer. */
- tsi_result tsi_handshaker_extract_peer (tsi_handshaker * self, tsi_peer * peer);
+tsi_result tsi_handshaker_extract_peer(tsi_handshaker *self, tsi_peer *peer);
/* This method creates a tsi_frame_protector object after the handshake phase
is done. After this method has been called successfully, the only method
@@ -321,14 +329,16 @@ extern "C"
tsi_handshaker_is_in_progress returns 1, it returns TSI_OK otherwise assuming
the handshaker is not in a fatal error state.
The caller is responsible for destroying the protector. */
- tsi_result tsi_handshaker_create_frame_protector (tsi_handshaker * self, size_t * max_output_protected_frame_size, tsi_frame_protector ** protector);
+tsi_result tsi_handshaker_create_frame_protector(
+ tsi_handshaker *self, size_t *max_output_protected_frame_size,
+ tsi_frame_protector **protector);
/* This method releases the tsi_handshaker object. After this method is called,
no other method can be called on the object. */
- void tsi_handshaker_destroy (tsi_handshaker * self);
+void tsi_handshaker_destroy(tsi_handshaker *self);
#ifdef __cplusplus
}
#endif
-#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H */
+#endif /* GRPC_INTERNAL_CORE_TSI_TRANSPORT_SECURITY_INTERFACE_H */