grpc 0.14.1 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +1398 -817
  3. data/include/grpc/compression.h +2 -1
  4. data/include/grpc/grpc.h +10 -1
  5. data/include/grpc/grpc_cronet.h +51 -0
  6. data/include/grpc/grpc_posix.h +70 -0
  7. data/include/grpc/impl/codegen/atm.h +2 -2
  8. data/include/grpc/impl/codegen/{atm_win32.h → atm_windows.h} +3 -3
  9. data/include/grpc/impl/codegen/compression_types.h +39 -5
  10. data/include/grpc/impl/codegen/connectivity_state.h +1 -1
  11. data/include/grpc/impl/codegen/grpc_types.h +10 -0
  12. data/include/grpc/impl/codegen/log.h +2 -1
  13. data/include/grpc/impl/codegen/port_platform.h +30 -12
  14. data/include/grpc/impl/codegen/slice_buffer.h +2 -3
  15. data/include/grpc/impl/codegen/sync.h +2 -2
  16. data/include/grpc/impl/codegen/{sync_win32.h → sync_windows.h} +3 -3
  17. data/include/grpc/support/{sync_win32.h → atm_windows.h} +4 -4
  18. data/include/grpc/support/avl.h +5 -0
  19. data/include/grpc/support/{log_win32.h → log_windows.h} +3 -3
  20. data/include/grpc/support/string_util.h +2 -1
  21. data/include/grpc/support/{atm_win32.h → sync_windows.h} +4 -4
  22. data/src/core/ext/census/gen/census.pb.c +179 -0
  23. data/src/core/ext/census/gen/census.pb.h +294 -0
  24. data/src/core/ext/census/grpc_filter.c +11 -7
  25. data/src/core/ext/client_config/channel_connectivity.c +28 -14
  26. data/src/core/ext/client_config/client_channel.c +77 -53
  27. data/src/core/ext/client_config/connector.h +1 -1
  28. data/src/core/ext/client_config/lb_policy.c +9 -6
  29. data/src/core/ext/client_config/lb_policy.h +9 -5
  30. data/src/core/ext/client_config/subchannel.c +58 -39
  31. data/src/core/ext/client_config/subchannel.h +3 -2
  32. data/src/core/ext/client_config/subchannel_call_holder.c +34 -19
  33. data/src/core/ext/client_config/subchannel_call_holder.h +2 -1
  34. data/src/core/ext/client_config/subchannel_index.c +20 -9
  35. data/src/core/ext/lb_policy/grpclb/load_balancer_api.c +7 -7
  36. data/src/core/ext/lb_policy/grpclb/load_balancer_api.h +5 -5
  37. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/{v0 → v1}/load_balancer.pb.c +29 -30
  38. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v1/load_balancer.pb.h +178 -0
  39. data/src/core/ext/lb_policy/pick_first/pick_first.c +65 -45
  40. data/src/core/ext/lb_policy/round_robin/round_robin.c +84 -43
  41. data/src/core/ext/load_reporting/load_reporting.c +133 -0
  42. data/src/core/ext/load_reporting/load_reporting.h +75 -0
  43. data/src/core/ext/load_reporting/load_reporting_filter.c +151 -0
  44. data/src/core/ext/load_reporting/load_reporting_filter.h +41 -0
  45. data/src/core/ext/resolver/dns/native/dns_resolver.c +22 -8
  46. data/src/core/ext/resolver/sockaddr/sockaddr_resolver.c +2 -2
  47. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +4 -4
  48. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +95 -0
  49. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.c +14 -18
  50. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2.c +49 -24
  51. data/src/core/ext/transport/chttp2/server/insecure/server_chttp2_posix.c +82 -0
  52. data/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.c +104 -60
  53. data/src/core/ext/transport/chttp2/transport/bin_decoder.c +232 -0
  54. data/src/{ruby/ext/grpc/rb_signal.c → core/ext/transport/chttp2/transport/bin_decoder.h} +27 -31
  55. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +481 -260
  56. data/src/core/ext/transport/chttp2/transport/frame.h +1 -7
  57. data/src/core/ext/transport/chttp2/transport/frame_data.c +44 -27
  58. data/src/core/ext/transport/chttp2/transport/frame_data.h +6 -5
  59. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +23 -17
  60. data/src/core/ext/transport/chttp2/transport/frame_goaway.h +2 -2
  61. data/src/core/ext/transport/chttp2/transport/frame_ping.c +12 -7
  62. data/src/core/ext/transport/chttp2/transport/frame_ping.h +3 -3
  63. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +25 -12
  64. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.h +2 -2
  65. data/src/core/ext/transport/chttp2/transport/frame_settings.c +23 -21
  66. data/src/core/ext/transport/chttp2/transport/frame_settings.h +2 -2
  67. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +17 -9
  68. data/src/core/ext/transport/chttp2/transport/frame_window_update.h +2 -2
  69. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +365 -287
  70. data/src/core/ext/transport/chttp2/transport/hpack_parser.h +8 -6
  71. data/src/core/ext/transport/chttp2/transport/hpack_table.c +24 -20
  72. data/src/core/ext/transport/chttp2/transport/hpack_table.h +5 -4
  73. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +1 -0
  74. data/src/core/ext/transport/chttp2/transport/incoming_metadata.h +1 -0
  75. data/src/core/ext/transport/chttp2/transport/internal.h +34 -32
  76. data/src/core/ext/transport/chttp2/transport/parsing.c +296 -212
  77. data/src/core/ext/transport/chttp2/transport/writing.c +12 -9
  78. data/src/core/lib/channel/channel_args.c +26 -12
  79. data/src/core/lib/channel/channel_args.h +1 -1
  80. data/src/core/lib/channel/channel_stack.c +12 -8
  81. data/src/core/lib/channel/channel_stack.h +27 -11
  82. data/src/core/lib/channel/channel_stack_builder.c +2 -2
  83. data/src/core/lib/channel/compress_filter.c +26 -31
  84. data/src/core/lib/channel/compress_filter.h +4 -4
  85. data/src/core/lib/channel/connected_channel.c +7 -5
  86. data/src/core/lib/channel/http_client_filter.c +34 -8
  87. data/src/core/lib/channel/http_client_filter.h +1 -1
  88. data/src/core/lib/channel/http_server_filter.c +21 -12
  89. data/src/core/lib/compression/{compression_algorithm.c → compression.c} +22 -21
  90. data/src/core/lib/http/httpcli.c +81 -59
  91. data/src/core/lib/http/httpcli.h +11 -15
  92. data/src/core/lib/http/httpcli_security_connector.c +5 -3
  93. data/src/core/lib/http/parser.c +127 -118
  94. data/src/core/lib/http/parser.h +11 -6
  95. data/src/core/lib/iomgr/closure.c +20 -16
  96. data/src/core/lib/iomgr/closure.h +19 -15
  97. data/src/core/lib/iomgr/endpoint.h +1 -1
  98. data/src/core/lib/iomgr/endpoint_pair_posix.c +2 -2
  99. data/src/core/lib/iomgr/error.c +535 -0
  100. data/src/core/lib/iomgr/error.h +192 -0
  101. data/src/core/lib/iomgr/ev_poll_and_epoll_posix.c +190 -83
  102. data/src/core/lib/iomgr/ev_poll_posix.c +1267 -0
  103. data/src/{ruby/ext/grpc/rb_signal.h → core/lib/iomgr/ev_poll_posix.h} +7 -5
  104. data/src/core/lib/iomgr/ev_posix.c +104 -14
  105. data/src/core/lib/iomgr/ev_posix.h +17 -7
  106. data/src/core/lib/iomgr/exec_ctx.c +25 -7
  107. data/src/core/lib/iomgr/exec_ctx.h +27 -8
  108. data/src/core/lib/iomgr/executor.c +2 -2
  109. data/src/core/lib/iomgr/executor.h +1 -1
  110. data/src/core/lib/iomgr/iocp_windows.c +2 -41
  111. data/src/core/lib/iomgr/iocp_windows.h +0 -8
  112. data/src/core/lib/iomgr/iomgr.c +5 -4
  113. data/src/core/lib/iomgr/iomgr_posix.c +5 -1
  114. data/src/core/lib/iomgr/iomgr_windows.c +1 -1
  115. data/src/core/lib/{support → iomgr}/load_file.c +15 -17
  116. data/src/core/lib/{support → iomgr}/load_file.h +8 -7
  117. data/src/core/lib/iomgr/polling_entity.c +104 -0
  118. data/src/core/lib/iomgr/polling_entity.h +81 -0
  119. data/src/core/lib/iomgr/pollset.h +6 -5
  120. data/src/core/lib/iomgr/pollset_set_windows.c +4 -1
  121. data/src/core/lib/iomgr/pollset_windows.c +10 -6
  122. data/src/core/lib/iomgr/resolve_address.h +5 -9
  123. data/src/core/lib/iomgr/resolve_address_posix.c +55 -38
  124. data/src/core/lib/iomgr/resolve_address_windows.c +51 -37
  125. data/src/core/lib/iomgr/sockaddr.h +2 -2
  126. data/src/core/lib/iomgr/{sockaddr_win32.h → sockaddr_windows.h} +3 -3
  127. data/src/core/lib/iomgr/socket_utils_common_posix.c +92 -45
  128. data/src/core/lib/iomgr/socket_utils_posix.h +19 -12
  129. data/src/core/lib/iomgr/socket_windows.c +61 -2
  130. data/src/core/lib/iomgr/socket_windows.h +13 -0
  131. data/src/core/lib/iomgr/tcp_client_posix.c +54 -39
  132. data/src/core/lib/iomgr/tcp_client_windows.c +34 -34
  133. data/src/core/lib/iomgr/tcp_posix.c +43 -39
  134. data/src/core/lib/iomgr/tcp_server.h +5 -3
  135. data/src/core/lib/iomgr/tcp_server_posix.c +103 -64
  136. data/src/core/lib/iomgr/tcp_server_windows.c +114 -101
  137. data/src/core/lib/iomgr/tcp_windows.c +45 -50
  138. data/src/core/lib/iomgr/tcp_windows.h +1 -1
  139. data/src/core/lib/iomgr/timer.c +26 -13
  140. data/src/core/lib/iomgr/udp_server.c +28 -4
  141. data/src/core/lib/iomgr/udp_server.h +5 -1
  142. data/src/core/lib/iomgr/unix_sockets_posix.c +8 -7
  143. data/src/core/lib/iomgr/unix_sockets_posix.h +2 -1
  144. data/src/core/lib/iomgr/unix_sockets_posix_noop.c +4 -2
  145. data/src/core/lib/iomgr/wakeup_fd_eventfd.c +15 -5
  146. data/src/core/lib/iomgr/wakeup_fd_pipe.c +13 -9
  147. data/src/core/lib/iomgr/wakeup_fd_posix.c +6 -6
  148. data/src/core/lib/iomgr/wakeup_fd_posix.h +9 -6
  149. data/src/core/lib/iomgr/workqueue.h +5 -4
  150. data/src/core/lib/iomgr/workqueue_posix.c +40 -26
  151. data/src/core/lib/iomgr/workqueue_windows.c +2 -2
  152. data/src/core/lib/profiling/basic_timers.c +2 -2
  153. data/src/core/lib/security/{security_context.c → context/security_context.c} +1 -1
  154. data/src/core/lib/security/{security_context.h → context/security_context.h} +4 -4
  155. data/src/core/lib/security/credentials/composite/composite_credentials.c +263 -0
  156. data/src/core/lib/security/credentials/composite/composite_credentials.h +72 -0
  157. data/src/core/lib/security/credentials/credentials.c +233 -0
  158. data/src/core/lib/security/{credentials.h → credentials/credentials.h} +19 -157
  159. data/src/core/lib/security/{credentials_metadata.c → credentials/credentials_metadata.c} +1 -1
  160. data/src/core/lib/security/credentials/fake/fake_credentials.c +139 -0
  161. data/src/core/lib/security/credentials/fake/fake_credentials.h +56 -0
  162. data/src/core/lib/security/{credentials_posix.c → credentials/google_default/credentials_posix.c} +1 -1
  163. data/src/core/lib/security/{credentials_win32.c → credentials/google_default/credentials_windows.c} +3 -3
  164. data/src/core/lib/security/{google_default_credentials.c → credentials/google_default/google_default_credentials.c} +93 -35
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +46 -0
  166. data/src/core/lib/security/credentials/iam/iam_credentials.c +85 -0
  167. data/src/core/lib/security/credentials/iam/iam_credentials.h +44 -0
  168. data/src/core/lib/security/{json_token.c → credentials/jwt/json_token.c} +10 -101
  169. data/src/core/lib/security/{json_token.h → credentials/jwt/json_token.h} +3 -33
  170. data/src/core/lib/security/credentials/jwt/jwt_credentials.c +160 -0
  171. data/src/core/lib/security/credentials/jwt/jwt_credentials.h +62 -0
  172. data/src/core/lib/security/{jwt_verifier.c → credentials/jwt/jwt_verifier.c} +35 -15
  173. data/src/core/lib/security/{jwt_verifier.h → credentials/jwt/jwt_verifier.h} +3 -3
  174. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +433 -0
  175. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +109 -0
  176. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +129 -0
  177. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +45 -0
  178. data/src/core/lib/security/credentials/ssl/ssl_credentials.c +240 -0
  179. data/src/core/lib/security/credentials/ssl/ssl_credentials.h +48 -0
  180. data/src/core/lib/security/{auth_filters.h → transport/auth_filters.h} +3 -3
  181. data/src/core/lib/security/{client_auth_filter.c → transport/client_auth_filter.c} +27 -20
  182. data/src/core/lib/security/{handshake.c → transport/handshake.c} +77 -45
  183. data/src/core/lib/security/{handshake.h → transport/handshake.h} +9 -11
  184. data/src/core/lib/security/{secure_endpoint.c → transport/secure_endpoint.c} +19 -12
  185. data/src/core/lib/security/{secure_endpoint.h → transport/secure_endpoint.h} +3 -3
  186. data/src/core/lib/security/{security_connector.c → transport/security_connector.c} +26 -17
  187. data/src/core/lib/security/{security_connector.h → transport/security_connector.h} +8 -8
  188. data/src/core/lib/security/{server_auth_filter.c → transport/server_auth_filter.c} +24 -16
  189. data/src/core/lib/security/transport/tsi_error.c +40 -0
  190. data/src/core/lib/security/transport/tsi_error.h +42 -0
  191. data/src/core/lib/security/{b64.c → util/b64.c} +1 -1
  192. data/src/core/lib/security/{b64.h → util/b64.h} +3 -3
  193. data/src/core/lib/security/util/json_util.c +61 -0
  194. data/src/core/lib/security/util/json_util.h +55 -0
  195. data/src/core/lib/support/avl.c +11 -0
  196. data/src/core/lib/support/cpu_windows.c +2 -2
  197. data/src/core/lib/support/{env_win32.c → env_windows.c} +3 -3
  198. data/src/core/lib/support/log.c +3 -1
  199. data/src/core/lib/support/log_linux.c +2 -2
  200. data/src/core/lib/support/{log_win32.c → log_windows.c} +4 -4
  201. data/src/core/lib/support/murmur_hash.c +3 -5
  202. data/src/core/lib/support/string.c +10 -0
  203. data/src/core/lib/support/string.h +4 -0
  204. data/src/core/lib/support/{string_util_win32.c → string_util_windows.c} +3 -3
  205. data/src/core/lib/support/{string_win32.c → string_windows.c} +2 -2
  206. data/src/core/lib/support/{string_win32.h → string_windows.h} +5 -5
  207. data/src/core/lib/support/subprocess_windows.c +1 -1
  208. data/src/core/lib/support/{sync_win32.c → sync_windows.c} +2 -2
  209. data/src/core/lib/support/{thd_win32.c → thd_windows.c} +2 -2
  210. data/src/core/lib/support/{time_win32.c → time_windows.c} +2 -2
  211. data/src/core/lib/support/tmpfile_msys.c +1 -1
  212. data/src/core/lib/support/{tmpfile_win32.c → tmpfile_windows.c} +3 -3
  213. data/src/core/lib/surface/alarm.c +2 -2
  214. data/src/core/lib/surface/byte_buffer_reader.c +13 -6
  215. data/src/core/lib/surface/call.c +323 -123
  216. data/src/core/lib/surface/call.h +2 -0
  217. data/src/core/lib/surface/call_log_batch.c +1 -1
  218. data/src/core/lib/surface/channel.c +64 -15
  219. data/src/core/lib/surface/channel.h +9 -0
  220. data/src/core/lib/surface/channel_ping.c +3 -3
  221. data/src/core/lib/surface/completion_queue.c +75 -19
  222. data/src/core/lib/surface/completion_queue.h +7 -2
  223. data/src/core/lib/surface/init.c +2 -1
  224. data/src/core/lib/surface/init_secure.c +4 -4
  225. data/src/core/lib/surface/lame_client.c +12 -8
  226. data/src/core/lib/surface/server.c +213 -120
  227. data/src/core/lib/surface/server.h +1 -0
  228. data/src/core/lib/surface/version.c +1 -1
  229. data/src/core/lib/transport/connectivity_state.c +40 -18
  230. data/src/core/lib/transport/connectivity_state.h +4 -1
  231. data/src/core/lib/transport/metadata.c +23 -23
  232. data/src/core/lib/transport/metadata.h +4 -0
  233. data/src/core/lib/transport/metadata_batch.c +9 -0
  234. data/src/core/lib/transport/metadata_batch.h +3 -0
  235. data/src/core/lib/transport/static_metadata.c +6 -5
  236. data/src/core/lib/transport/static_metadata.h +64 -60
  237. data/src/core/lib/transport/transport.c +24 -12
  238. data/src/core/lib/transport/transport.h +6 -5
  239. data/src/core/lib/transport/transport_impl.h +4 -0
  240. data/src/core/lib/transport/transport_op_string.c +2 -2
  241. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -0
  242. data/src/ruby/bin/math_services.rb +41 -2
  243. data/src/ruby/ext/grpc/rb_call.c +42 -40
  244. data/src/ruby/ext/grpc/rb_channel.c +1 -1
  245. data/src/ruby/ext/grpc/rb_completion_queue.c +59 -6
  246. data/src/ruby/ext/grpc/rb_completion_queue.h +1 -1
  247. data/src/ruby/ext/grpc/rb_grpc.c +1 -3
  248. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +12 -2
  249. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +21 -5
  250. data/src/ruby/ext/grpc/rb_loader.c +1 -1
  251. data/src/ruby/ext/grpc/rb_server.c +5 -3
  252. data/src/ruby/lib/grpc.rb +0 -3
  253. data/src/ruby/lib/grpc/errors.rb +3 -2
  254. data/src/ruby/lib/grpc/generic/active_call.rb +32 -42
  255. data/src/ruby/lib/grpc/generic/bidi_call.rb +20 -0
  256. data/src/ruby/lib/grpc/generic/client_stub.rb +31 -54
  257. data/src/ruby/lib/grpc/generic/rpc_desc.rb +4 -4
  258. data/src/ruby/lib/grpc/generic/rpc_server.rb +12 -23
  259. data/src/ruby/lib/grpc/generic/service.rb +8 -8
  260. data/src/ruby/lib/grpc/version.rb +1 -1
  261. data/src/ruby/pb/grpc/health/v1/health_services.rb +30 -2
  262. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services.rb +34 -4
  263. data/src/ruby/pb/grpc/testing/metrics_services.rb +39 -2
  264. data/src/ruby/pb/src/proto/grpc/testing/empty.rb +15 -0
  265. data/src/ruby/pb/src/proto/grpc/testing/messages.rb +84 -0
  266. data/src/ruby/pb/src/proto/grpc/testing/test.rb +14 -0
  267. data/src/ruby/pb/src/proto/grpc/testing/test_services.rb +110 -0
  268. data/src/ruby/pb/test/client.rb +5 -2
  269. data/src/ruby/spec/generic/active_call_spec.rb +3 -2
  270. data/src/ruby/spec/generic/client_stub_spec.rb +27 -24
  271. data/src/ruby/spec/generic/rpc_desc_spec.rb +11 -11
  272. data/src/ruby/spec/generic/rpc_server_spec.rb +42 -61
  273. data/src/ruby/spec/pb/health/checker_spec.rb +3 -5
  274. metadata +86 -48
  275. data/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0/load_balancer.pb.h +0 -182
  276. data/src/core/lib/security/credentials.c +0 -1296
  277. data/src/ruby/lib/grpc/signals.rb +0 -69
@@ -36,6 +36,7 @@
36
36
 
37
37
  #include <grpc/support/port_platform.h>
38
38
  #include <grpc/support/slice.h>
39
+ #include "src/core/lib/iomgr/error.h"
39
40
 
40
41
  /* Maximum length of a header string of the form 'Key: Value\r\n' */
41
42
  #define GRPC_HTTP_PARSER_MAX_HEADER_LENGTH 4096
@@ -61,7 +62,6 @@ typedef enum {
61
62
  typedef enum {
62
63
  GRPC_HTTP_RESPONSE,
63
64
  GRPC_HTTP_REQUEST,
64
- GRPC_HTTP_UNKNOWN
65
65
  } grpc_http_type;
66
66
 
67
67
  /* A request */
@@ -97,8 +97,9 @@ typedef struct {
97
97
  grpc_http_type type;
98
98
 
99
99
  union {
100
- grpc_http_response response;
101
- grpc_http_request request;
100
+ grpc_http_response *response;
101
+ grpc_http_request *request;
102
+ void *request_or_response;
102
103
  } http;
103
104
  size_t body_capacity;
104
105
  size_t hdr_capacity;
@@ -108,11 +109,15 @@ typedef struct {
108
109
  size_t cur_line_end_length;
109
110
  } grpc_http_parser;
110
111
 
111
- void grpc_http_parser_init(grpc_http_parser *parser);
112
+ void grpc_http_parser_init(grpc_http_parser *parser, grpc_http_type type,
113
+ void *request_or_response);
112
114
  void grpc_http_parser_destroy(grpc_http_parser *parser);
113
115
 
114
- int grpc_http_parser_parse(grpc_http_parser *parser, gpr_slice slice);
115
- int grpc_http_parser_eof(grpc_http_parser *parser);
116
+ grpc_error *grpc_http_parser_parse(grpc_http_parser *parser, gpr_slice slice);
117
+ grpc_error *grpc_http_parser_eof(grpc_http_parser *parser);
118
+
119
+ void grpc_http_request_destroy(grpc_http_request *request);
120
+ void grpc_http_response_destroy(grpc_http_response *response);
116
121
 
117
122
  extern int grpc_http1_trace;
118
123
 
@@ -39,25 +39,32 @@ void grpc_closure_init(grpc_closure *closure, grpc_iomgr_cb_func cb,
39
39
  void *cb_arg) {
40
40
  closure->cb = cb;
41
41
  closure->cb_arg = cb_arg;
42
- closure->final_data = 0;
43
42
  }
44
43
 
45
- void grpc_closure_list_add(grpc_closure_list *closure_list,
46
- grpc_closure *closure, bool success) {
47
- if (closure == NULL) return;
48
- closure->final_data = (success != 0);
44
+ void grpc_closure_list_append(grpc_closure_list *closure_list,
45
+ grpc_closure *closure, grpc_error *error) {
46
+ if (closure == NULL) {
47
+ GRPC_ERROR_UNREF(error);
48
+ return;
49
+ }
50
+ closure->error = error;
51
+ closure->next_data.next = NULL;
49
52
  if (closure_list->head == NULL) {
50
53
  closure_list->head = closure;
51
54
  } else {
52
- closure_list->tail->final_data |= (uintptr_t)closure;
55
+ closure_list->tail->next_data.next = closure;
53
56
  }
54
57
  closure_list->tail = closure;
55
58
  }
56
59
 
57
- void grpc_closure_list_fail_all(grpc_closure_list *list) {
58
- for (grpc_closure *c = list->head; c != NULL; c = grpc_closure_next(c)) {
59
- c->final_data &= ~(uintptr_t)1;
60
+ void grpc_closure_list_fail_all(grpc_closure_list *list,
61
+ grpc_error *forced_failure) {
62
+ for (grpc_closure *c = list->head; c != NULL; c = c->next_data.next) {
63
+ if (c->error == GRPC_ERROR_NONE) {
64
+ c->error = GRPC_ERROR_REF(forced_failure);
65
+ }
60
66
  }
67
+ GRPC_ERROR_UNREF(forced_failure);
61
68
  }
62
69
 
63
70
  bool grpc_closure_list_empty(grpc_closure_list closure_list) {
@@ -71,7 +78,7 @@ void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst) {
71
78
  if (dst->head == NULL) {
72
79
  *dst = *src;
73
80
  } else {
74
- dst->tail->final_data |= (uintptr_t)src->head;
81
+ dst->tail->next_data.next = src->head;
75
82
  dst->tail = src->tail;
76
83
  }
77
84
  src->head = src->tail = NULL;
@@ -83,12 +90,13 @@ typedef struct {
83
90
  grpc_closure wrapper;
84
91
  } wrapped_closure;
85
92
 
86
- static void closure_wrapper(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
93
+ static void closure_wrapper(grpc_exec_ctx *exec_ctx, void *arg,
94
+ grpc_error *error) {
87
95
  wrapped_closure *wc = arg;
88
96
  grpc_iomgr_cb_func cb = wc->cb;
89
97
  void *cb_arg = wc->cb_arg;
90
98
  gpr_free(wc);
91
- cb(exec_ctx, cb_arg, success);
99
+ cb(exec_ctx, cb_arg, error);
92
100
  }
93
101
 
94
102
  grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg) {
@@ -98,7 +106,3 @@ grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg) {
98
106
  grpc_closure_init(&wc->wrapper, closure_wrapper, wc);
99
107
  return &wc->wrapper;
100
108
  }
101
-
102
- grpc_closure *grpc_closure_next(grpc_closure *closure) {
103
- return (grpc_closure *)(closure->final_data & ~(uintptr_t)1);
104
- }
@@ -36,6 +36,7 @@
36
36
 
37
37
  #include <grpc/support/port_platform.h>
38
38
  #include <stdbool.h>
39
+ #include "src/core/lib/iomgr/error.h"
39
40
 
40
41
  struct grpc_closure;
41
42
  typedef struct grpc_closure grpc_closure;
@@ -52,10 +53,10 @@ typedef struct grpc_closure_list {
52
53
  /** gRPC Callback definition.
53
54
  *
54
55
  * \param arg Arbitrary input.
55
- * \param success An indication on the state of the iomgr. On false, cleanup
56
- * actions should be taken (eg, shutdown). */
56
+ * \param error GRPC_ERROR_NONE if no error occurred, otherwise some grpc_error
57
+ * describing what went wrong */
57
58
  typedef void (*grpc_iomgr_cb_func)(grpc_exec_ctx *exec_ctx, void *arg,
58
- bool success);
59
+ grpc_error *error);
59
60
 
60
61
  /** A closure over a grpc_iomgr_cb_func. */
61
62
  struct grpc_closure {
@@ -65,10 +66,15 @@ struct grpc_closure {
65
66
  /** Arguments to be passed to "cb". */
66
67
  void *cb_arg;
67
68
 
68
- /** Once enqueued, contains in the lower bit the success of the closure,
69
- and in the upper bits the pointer to the next closure in the list.
70
- Before enqueing for execution, this is usable for scratch data. */
71
- uintptr_t final_data;
69
+ /** Once queued, the result of the closure. Before then: scratch space */
70
+ grpc_error *error;
71
+
72
+ /** Once queued, next indicates the next queued closure; before then, scratch
73
+ * space */
74
+ union {
75
+ grpc_closure *next;
76
+ uintptr_t scratch;
77
+ } next_data;
72
78
  };
73
79
 
74
80
  /** Initializes \a closure with \a cb and \a cb_arg. */
@@ -81,13 +87,14 @@ grpc_closure *grpc_closure_create(grpc_iomgr_cb_func cb, void *cb_arg);
81
87
  #define GRPC_CLOSURE_LIST_INIT \
82
88
  { NULL, NULL }
83
89
 
84
- /** add \a closure to the end of \a list and set \a closure's success to \a
85
- * success */
86
- void grpc_closure_list_add(grpc_closure_list *list, grpc_closure *closure,
87
- bool success);
90
+ /** add \a closure to the end of \a list
91
+ and set \a closure's result to \a error */
92
+ void grpc_closure_list_append(grpc_closure_list *list, grpc_closure *closure,
93
+ grpc_error *error);
88
94
 
89
95
  /** force all success bits in \a list to false */
90
- void grpc_closure_list_fail_all(grpc_closure_list *list);
96
+ void grpc_closure_list_fail_all(grpc_closure_list *list,
97
+ grpc_error *forced_failure);
91
98
 
92
99
  /** append all closures from \a src to \a dst and empty \a src. */
93
100
  void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst);
@@ -95,7 +102,4 @@ void grpc_closure_list_move(grpc_closure_list *src, grpc_closure_list *dst);
95
102
  /** return whether \a list is empty. */
96
103
  bool grpc_closure_list_empty(grpc_closure_list list);
97
104
 
98
- /** return the next pointer for a queued closure list */
99
- grpc_closure *grpc_closure_next(grpc_closure *closure);
100
-
101
105
  #endif /* GRPC_CORE_LIB_IOMGR_CLOSURE_H */
@@ -82,7 +82,7 @@ char *grpc_endpoint_get_peer(grpc_endpoint *ep);
82
82
  void grpc_endpoint_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
83
83
  gpr_slice_buffer *slices, grpc_closure *cb);
84
84
 
85
- /* Causes any pending read/write callbacks to run immediately with
85
+ /* Causes any pending and future read/write callbacks to run immediately with
86
86
  success==0 */
87
87
  void grpc_endpoint_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
88
88
  void grpc_endpoint_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
@@ -58,8 +58,8 @@ static void create_sockets(int sv[2]) {
58
58
  GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
59
59
  flags = fcntl(sv[1], F_GETFL, 0);
60
60
  GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
61
- GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]));
62
- GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]));
61
+ GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]) == GRPC_ERROR_NONE);
62
+ GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]) == GRPC_ERROR_NONE);
63
63
  }
64
64
 
65
65
  grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name,
@@ -0,0 +1,535 @@
1
+ /*
2
+ *
3
+ * Copyright 2016, Google Inc.
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are
8
+ * met:
9
+ *
10
+ * * Redistributions of source code must retain the above copyright
11
+ * notice, this list of conditions and the following disclaimer.
12
+ * * Redistributions in binary form must reproduce the above
13
+ * copyright notice, this list of conditions and the following disclaimer
14
+ * in the documentation and/or other materials provided with the
15
+ * distribution.
16
+ * * Neither the name of Google Inc. nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *
32
+ */
33
+
34
+ #include "src/core/lib/iomgr/error.h"
35
+
36
+ #include <inttypes.h>
37
+ #include <stdbool.h>
38
+ #include <string.h>
39
+
40
+ #include <grpc/support/alloc.h>
41
+ #include <grpc/support/avl.h>
42
+ #include <grpc/support/log.h>
43
+ #include <grpc/support/string_util.h>
44
+ #include <grpc/support/useful.h>
45
+
46
+ #ifdef GPR_WINDOWS
47
+ #include <grpc/support/log_windows.h>
48
+ #endif
49
+
50
+ static void destroy_integer(void *key) {}
51
+
52
+ static void *copy_integer(void *key) { return key; }
53
+
54
+ static long compare_integers(void *key1, void *key2) {
55
+ return GPR_ICMP((uintptr_t)key1, (uintptr_t)key2);
56
+ }
57
+
58
+ static void destroy_string(void *str) { gpr_free(str); }
59
+
60
+ static void *copy_string(void *str) { return gpr_strdup(str); }
61
+
62
+ static void destroy_err(void *err) { GRPC_ERROR_UNREF(err); }
63
+
64
+ static void *copy_err(void *err) { return GRPC_ERROR_REF(err); }
65
+
66
+ static void destroy_time(void *tm) { gpr_free(tm); }
67
+
68
+ static gpr_timespec *box_time(gpr_timespec tm) {
69
+ gpr_timespec *out = gpr_malloc(sizeof(*out));
70
+ *out = tm;
71
+ return out;
72
+ }
73
+
74
+ static void *copy_time(void *tm) { return box_time(*(gpr_timespec *)tm); }
75
+
76
+ static const gpr_avl_vtable avl_vtable_ints = {destroy_integer, copy_integer,
77
+ compare_integers,
78
+ destroy_integer, copy_integer};
79
+
80
+ static const gpr_avl_vtable avl_vtable_strs = {destroy_integer, copy_integer,
81
+ compare_integers, destroy_string,
82
+ copy_string};
83
+
84
+ static const gpr_avl_vtable avl_vtable_times = {
85
+ destroy_integer, copy_integer, compare_integers, destroy_time, copy_time};
86
+
87
+ static const gpr_avl_vtable avl_vtable_errs = {
88
+ destroy_integer, copy_integer, compare_integers, destroy_err, copy_err};
89
+
90
+ static const char *error_int_name(grpc_error_ints key) {
91
+ switch (key) {
92
+ case GRPC_ERROR_INT_ERRNO:
93
+ return "errno";
94
+ case GRPC_ERROR_INT_FILE_LINE:
95
+ return "file_line";
96
+ case GRPC_ERROR_INT_STREAM_ID:
97
+ return "stream_id";
98
+ case GRPC_ERROR_INT_GRPC_STATUS:
99
+ return "grpc_status";
100
+ case GRPC_ERROR_INT_OFFSET:
101
+ return "offset";
102
+ case GRPC_ERROR_INT_INDEX:
103
+ return "index";
104
+ case GRPC_ERROR_INT_SIZE:
105
+ return "size";
106
+ case GRPC_ERROR_INT_HTTP2_ERROR:
107
+ return "http2_error";
108
+ case GRPC_ERROR_INT_TSI_CODE:
109
+ return "tsi_code";
110
+ case GRPC_ERROR_INT_SECURITY_STATUS:
111
+ return "security_status";
112
+ case GRPC_ERROR_INT_FD:
113
+ return "fd";
114
+ case GRPC_ERROR_INT_WSA_ERROR:
115
+ return "wsa_error";
116
+ case GRPC_ERROR_INT_HTTP_STATUS:
117
+ return "http_status";
118
+ }
119
+ GPR_UNREACHABLE_CODE(return "unknown");
120
+ }
121
+
122
+ static const char *error_str_name(grpc_error_strs key) {
123
+ switch (key) {
124
+ case GRPC_ERROR_STR_DESCRIPTION:
125
+ return "description";
126
+ case GRPC_ERROR_STR_OS_ERROR:
127
+ return "os_error";
128
+ case GRPC_ERROR_STR_TARGET_ADDRESS:
129
+ return "target_address";
130
+ case GRPC_ERROR_STR_SYSCALL:
131
+ return "syscall";
132
+ case GRPC_ERROR_STR_FILE:
133
+ return "file";
134
+ case GRPC_ERROR_STR_GRPC_MESSAGE:
135
+ return "grpc_message";
136
+ case GRPC_ERROR_STR_RAW_BYTES:
137
+ return "raw_bytes";
138
+ case GRPC_ERROR_STR_TSI_ERROR:
139
+ return "tsi_error";
140
+ case GRPC_ERROR_STR_FILENAME:
141
+ return "filename";
142
+ }
143
+ GPR_UNREACHABLE_CODE(return "unknown");
144
+ }
145
+
146
+ static const char *error_time_name(grpc_error_times key) {
147
+ switch (key) {
148
+ case GRPC_ERROR_TIME_CREATED:
149
+ return "created";
150
+ }
151
+ GPR_UNREACHABLE_CODE(return "unknown");
152
+ }
153
+
154
+ struct grpc_error {
155
+ gpr_refcount refs;
156
+ gpr_avl ints;
157
+ gpr_avl strs;
158
+ gpr_avl times;
159
+ gpr_avl errs;
160
+ uintptr_t next_err;
161
+ };
162
+
163
+ static bool is_special(grpc_error *err) {
164
+ return err == GRPC_ERROR_NONE || err == GRPC_ERROR_OOM ||
165
+ err == GRPC_ERROR_CANCELLED;
166
+ }
167
+
168
+ #ifdef GRPC_ERROR_REFCOUNT_DEBUG
169
+ grpc_error *grpc_error_ref(grpc_error *err, const char *file, int line,
170
+ const char *func) {
171
+ if (is_special(err)) return err;
172
+ gpr_log(GPR_DEBUG, "%p: %d -> %d [%s:%d %s]", err, err->refs.count,
173
+ err->refs.count + 1, file, line, func);
174
+ gpr_ref(&err->refs);
175
+ return err;
176
+ }
177
+ #else
178
+ grpc_error *grpc_error_ref(grpc_error *err) {
179
+ if (is_special(err)) return err;
180
+ gpr_ref(&err->refs);
181
+ return err;
182
+ }
183
+ #endif
184
+
185
+ static void error_destroy(grpc_error *err) {
186
+ GPR_ASSERT(!is_special(err));
187
+ gpr_avl_unref(err->ints);
188
+ gpr_avl_unref(err->strs);
189
+ gpr_avl_unref(err->errs);
190
+ gpr_avl_unref(err->times);
191
+ gpr_free(err);
192
+ }
193
+
194
+ #ifdef GRPC_ERROR_REFCOUNT_DEBUG
195
+ void grpc_error_unref(grpc_error *err, const char *file, int line,
196
+ const char *func) {
197
+ if (is_special(err)) return;
198
+ gpr_log(GPR_DEBUG, "%p: %d -> %d [%s:%d %s]", err, err->refs.count,
199
+ err->refs.count - 1, file, line, func);
200
+ if (gpr_unref(&err->refs)) {
201
+ error_destroy(err);
202
+ }
203
+ }
204
+ #else
205
+ void grpc_error_unref(grpc_error *err) {
206
+ if (is_special(err)) return;
207
+ if (gpr_unref(&err->refs)) {
208
+ error_destroy(err);
209
+ }
210
+ }
211
+ #endif
212
+
213
+ grpc_error *grpc_error_create(const char *file, int line, const char *desc,
214
+ grpc_error **referencing,
215
+ size_t num_referencing) {
216
+ grpc_error *err = gpr_malloc(sizeof(*err));
217
+ if (err == NULL) { // TODO(ctiller): make gpr_malloc return NULL
218
+ return GRPC_ERROR_OOM;
219
+ }
220
+ #ifdef GRPC_ERROR_REFCOUNT_DEBUG
221
+ gpr_log(GPR_DEBUG, "%p create [%s:%d]", err, file, line);
222
+ #endif
223
+ err->ints = gpr_avl_add(gpr_avl_create(&avl_vtable_ints),
224
+ (void *)(uintptr_t)GRPC_ERROR_INT_FILE_LINE,
225
+ (void *)(uintptr_t)line);
226
+ err->strs = gpr_avl_add(
227
+ gpr_avl_add(gpr_avl_create(&avl_vtable_strs),
228
+ (void *)(uintptr_t)GRPC_ERROR_STR_FILE, gpr_strdup(file)),
229
+ (void *)(uintptr_t)GRPC_ERROR_STR_DESCRIPTION, gpr_strdup(desc));
230
+ err->errs = gpr_avl_create(&avl_vtable_errs);
231
+ err->next_err = 0;
232
+ for (size_t i = 0; i < num_referencing; i++) {
233
+ if (referencing[i] == GRPC_ERROR_NONE) continue;
234
+ err->errs = gpr_avl_add(err->errs, (void *)(err->next_err++),
235
+ GRPC_ERROR_REF(referencing[i]));
236
+ }
237
+ err->times = gpr_avl_add(gpr_avl_create(&avl_vtable_times),
238
+ (void *)(uintptr_t)GRPC_ERROR_TIME_CREATED,
239
+ box_time(gpr_now(GPR_CLOCK_REALTIME)));
240
+ gpr_ref_init(&err->refs, 1);
241
+ return err;
242
+ }
243
+
244
+ static grpc_error *copy_error_and_unref(grpc_error *in) {
245
+ if (is_special(in)) {
246
+ if (in == GRPC_ERROR_NONE) return GRPC_ERROR_CREATE("no error");
247
+ if (in == GRPC_ERROR_OOM) return GRPC_ERROR_CREATE("oom");
248
+ if (in == GRPC_ERROR_CANCELLED) return GRPC_ERROR_CREATE("cancelled");
249
+ return GRPC_ERROR_CREATE("unknown");
250
+ }
251
+ grpc_error *out = gpr_malloc(sizeof(*out));
252
+ #ifdef GRPC_ERROR_REFCOUNT_DEBUG
253
+ gpr_log(GPR_DEBUG, "%p create copying", out);
254
+ #endif
255
+ out->ints = gpr_avl_ref(in->ints);
256
+ out->strs = gpr_avl_ref(in->strs);
257
+ out->errs = gpr_avl_ref(in->errs);
258
+ out->times = gpr_avl_ref(in->times);
259
+ out->next_err = in->next_err;
260
+ gpr_ref_init(&out->refs, 1);
261
+ GRPC_ERROR_UNREF(in);
262
+ return out;
263
+ }
264
+
265
+ grpc_error *grpc_error_set_int(grpc_error *src, grpc_error_ints which,
266
+ intptr_t value) {
267
+ grpc_error *new = copy_error_and_unref(src);
268
+ new->ints = gpr_avl_add(new->ints, (void *)(uintptr_t)which, (void *)value);
269
+ return new;
270
+ }
271
+
272
+ bool grpc_error_get_int(grpc_error *err, grpc_error_ints which, intptr_t *p) {
273
+ void *pp;
274
+ if (gpr_avl_maybe_get(err->ints, (void *)(uintptr_t)which, &pp)) {
275
+ if (p != NULL) *p = (intptr_t)pp;
276
+ return true;
277
+ }
278
+ return false;
279
+ }
280
+
281
+ grpc_error *grpc_error_set_str(grpc_error *src, grpc_error_strs which,
282
+ const char *value) {
283
+ grpc_error *new = copy_error_and_unref(src);
284
+ new->strs =
285
+ gpr_avl_add(new->strs, (void *)(uintptr_t)which, gpr_strdup(value));
286
+ return new;
287
+ }
288
+
289
+ grpc_error *grpc_error_add_child(grpc_error *src, grpc_error *child) {
290
+ grpc_error *new = copy_error_and_unref(src);
291
+ new->errs = gpr_avl_add(new->errs, (void *)(new->next_err++), child);
292
+ return new;
293
+ }
294
+
295
+ static const char *no_error_string = "null";
296
+ static const char *oom_error_string = "\"Out of memory\"";
297
+ static const char *cancelled_error_string = "\"Cancelled\"";
298
+
299
+ typedef struct {
300
+ char *key;
301
+ char *value;
302
+ } kv_pair;
303
+
304
+ typedef struct {
305
+ kv_pair *kvs;
306
+ size_t num_kvs;
307
+ size_t cap_kvs;
308
+ } kv_pairs;
309
+
310
+ static void append_kv(kv_pairs *kvs, char *key, char *value) {
311
+ if (kvs->num_kvs == kvs->cap_kvs) {
312
+ kvs->cap_kvs = GPR_MAX(3 * kvs->cap_kvs / 2, 4);
313
+ kvs->kvs = gpr_realloc(kvs->kvs, sizeof(*kvs->kvs) * kvs->cap_kvs);
314
+ }
315
+ kvs->kvs[kvs->num_kvs].key = key;
316
+ kvs->kvs[kvs->num_kvs].value = value;
317
+ kvs->num_kvs++;
318
+ }
319
+
320
+ static void collect_kvs(gpr_avl_node *node, char *key(void *k),
321
+ char *fmt(void *v), kv_pairs *kvs) {
322
+ if (node == NULL) return;
323
+ append_kv(kvs, key(node->key), fmt(node->value));
324
+ collect_kvs(node->left, key, fmt, kvs);
325
+ collect_kvs(node->right, key, fmt, kvs);
326
+ }
327
+
328
+ static char *key_int(void *p) {
329
+ return gpr_strdup(error_int_name((grpc_error_ints)(uintptr_t)p));
330
+ }
331
+
332
+ static char *key_str(void *p) {
333
+ return gpr_strdup(error_str_name((grpc_error_strs)(uintptr_t)p));
334
+ }
335
+
336
+ static char *key_time(void *p) {
337
+ return gpr_strdup(error_time_name((grpc_error_times)(uintptr_t)p));
338
+ }
339
+
340
+ static char *fmt_int(void *p) {
341
+ char *s;
342
+ gpr_asprintf(&s, "%" PRIdPTR, (intptr_t)p);
343
+ return s;
344
+ }
345
+
346
+ static void append_chr(char c, char **s, size_t *sz, size_t *cap) {
347
+ if (*sz == *cap) {
348
+ *cap = GPR_MAX(8, 3 * *cap / 2);
349
+ *s = gpr_realloc(*s, *cap);
350
+ }
351
+ (*s)[(*sz)++] = c;
352
+ }
353
+
354
+ static void append_str(const char *str, char **s, size_t *sz, size_t *cap) {
355
+ for (const char *c = str; *c; c++) {
356
+ append_chr(*c, s, sz, cap);
357
+ }
358
+ }
359
+
360
+ static void append_esc_str(const char *str, char **s, size_t *sz, size_t *cap) {
361
+ static const char *hex = "0123456789abcdef";
362
+ append_chr('"', s, sz, cap);
363
+ for (const uint8_t *c = (const uint8_t *)str; *c; c++) {
364
+ if (*c < 32 || *c >= 127) {
365
+ append_chr('\\', s, sz, cap);
366
+ switch (*c) {
367
+ case '\b':
368
+ append_chr('b', s, sz, cap);
369
+ break;
370
+ case '\f':
371
+ append_chr('f', s, sz, cap);
372
+ break;
373
+ case '\n':
374
+ append_chr('n', s, sz, cap);
375
+ break;
376
+ case '\r':
377
+ append_chr('r', s, sz, cap);
378
+ break;
379
+ case '\t':
380
+ append_chr('t', s, sz, cap);
381
+ break;
382
+ default:
383
+ append_chr('u', s, sz, cap);
384
+ append_chr('0', s, sz, cap);
385
+ append_chr('0', s, sz, cap);
386
+ append_chr(hex[*c >> 4], s, sz, cap);
387
+ append_chr(hex[*c & 0x0f], s, sz, cap);
388
+ break;
389
+ }
390
+ } else {
391
+ append_chr((char)*c, s, sz, cap);
392
+ }
393
+ }
394
+ append_chr('"', s, sz, cap);
395
+ }
396
+
397
+ static char *fmt_str(void *p) {
398
+ char *s = NULL;
399
+ size_t sz = 0;
400
+ size_t cap = 0;
401
+ append_esc_str(p, &s, &sz, &cap);
402
+ append_chr(0, &s, &sz, &cap);
403
+ return s;
404
+ }
405
+
406
+ static char *fmt_time(void *p) {
407
+ gpr_timespec tm = *(gpr_timespec *)p;
408
+ char *out;
409
+ char *pfx = "!!";
410
+ switch (tm.clock_type) {
411
+ case GPR_CLOCK_MONOTONIC:
412
+ pfx = "@monotonic:";
413
+ break;
414
+ case GPR_CLOCK_REALTIME:
415
+ pfx = "@";
416
+ break;
417
+ case GPR_CLOCK_PRECISE:
418
+ pfx = "@precise:";
419
+ break;
420
+ case GPR_TIMESPAN:
421
+ pfx = "";
422
+ break;
423
+ }
424
+ gpr_asprintf(&out, "\"%s%" PRId64 ".%09d\"", pfx, tm.tv_sec, tm.tv_nsec);
425
+ return out;
426
+ }
427
+
428
+ static void add_errs(gpr_avl_node *n, char **s, size_t *sz, size_t *cap) {
429
+ if (n == NULL) return;
430
+ add_errs(n->left, s, sz, cap);
431
+ const char *e = grpc_error_string(n->value);
432
+ append_str(e, s, sz, cap);
433
+ grpc_error_free_string(e);
434
+ add_errs(n->right, s, sz, cap);
435
+ }
436
+
437
+ static char *errs_string(grpc_error *err) {
438
+ char *s = NULL;
439
+ size_t sz = 0;
440
+ size_t cap = 0;
441
+ append_chr('[', &s, &sz, &cap);
442
+ add_errs(err->errs.root, &s, &sz, &cap);
443
+ append_chr(']', &s, &sz, &cap);
444
+ append_chr(0, &s, &sz, &cap);
445
+ return s;
446
+ }
447
+
448
+ static int cmp_kvs(const void *a, const void *b) {
449
+ const kv_pair *ka = a;
450
+ const kv_pair *kb = b;
451
+ return strcmp(ka->key, kb->key);
452
+ }
453
+
454
+ static const char *finish_kvs(kv_pairs *kvs) {
455
+ char *s = NULL;
456
+ size_t sz = 0;
457
+ size_t cap = 0;
458
+
459
+ append_chr('{', &s, &sz, &cap);
460
+ for (size_t i = 0; i < kvs->num_kvs; i++) {
461
+ if (i != 0) append_chr(',', &s, &sz, &cap);
462
+ append_esc_str(kvs->kvs[i].key, &s, &sz, &cap);
463
+ gpr_free(kvs->kvs[i].key);
464
+ append_chr(':', &s, &sz, &cap);
465
+ append_str(kvs->kvs[i].value, &s, &sz, &cap);
466
+ gpr_free(kvs->kvs[i].value);
467
+ }
468
+ append_chr('}', &s, &sz, &cap);
469
+ append_chr(0, &s, &sz, &cap);
470
+
471
+ gpr_free(kvs->kvs);
472
+ return s;
473
+ }
474
+
475
+ void grpc_error_free_string(const char *str) {
476
+ if (str == no_error_string) return;
477
+ if (str == oom_error_string) return;
478
+ if (str == cancelled_error_string) return;
479
+ gpr_free((char *)str);
480
+ }
481
+
482
+ const char *grpc_error_string(grpc_error *err) {
483
+ if (err == GRPC_ERROR_NONE) return no_error_string;
484
+ if (err == GRPC_ERROR_OOM) return oom_error_string;
485
+ if (err == GRPC_ERROR_CANCELLED) return cancelled_error_string;
486
+
487
+ kv_pairs kvs;
488
+ memset(&kvs, 0, sizeof(kvs));
489
+
490
+ collect_kvs(err->ints.root, key_int, fmt_int, &kvs);
491
+ collect_kvs(err->strs.root, key_str, fmt_str, &kvs);
492
+ collect_kvs(err->times.root, key_time, fmt_time, &kvs);
493
+ if (!gpr_avl_is_empty(err->errs)) {
494
+ append_kv(&kvs, gpr_strdup("referenced_errors"), errs_string(err));
495
+ }
496
+
497
+ qsort(kvs.kvs, kvs.num_kvs, sizeof(kv_pair), cmp_kvs);
498
+
499
+ return finish_kvs(&kvs);
500
+ }
501
+
502
+ grpc_error *grpc_os_error(const char *file, int line, int err,
503
+ const char *call_name) {
504
+ return grpc_error_set_str(
505
+ grpc_error_set_str(
506
+ grpc_error_set_int(grpc_error_create(file, line, "OS Error", NULL, 0),
507
+ GRPC_ERROR_INT_ERRNO, err),
508
+ GRPC_ERROR_STR_OS_ERROR, strerror(err)),
509
+ GRPC_ERROR_STR_SYSCALL, call_name);
510
+ }
511
+
512
+ #ifdef GPR_WINDOWS
513
+ grpc_error *grpc_wsa_error(const char *file, int line, int err,
514
+ const char *call_name) {
515
+ char *utf8_message = gpr_format_message(err);
516
+ grpc_error *error = grpc_error_set_str(
517
+ grpc_error_set_str(
518
+ grpc_error_set_int(grpc_error_create(file, line, "OS Error", NULL, 0),
519
+ GRPC_ERROR_INT_WSA_ERROR, err),
520
+ GRPC_ERROR_STR_OS_ERROR, utf8_message),
521
+ GRPC_ERROR_STR_SYSCALL, call_name);
522
+ gpr_free(utf8_message);
523
+ return error;
524
+ }
525
+ #endif
526
+
527
+ bool grpc_log_if_error(const char *what, grpc_error *error, const char *file,
528
+ int line) {
529
+ if (error == GRPC_ERROR_NONE) return true;
530
+ const char *msg = grpc_error_string(error);
531
+ gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "%s: %s", what, msg);
532
+ grpc_error_free_string(msg);
533
+ GRPC_ERROR_UNREF(error);
534
+ return false;
535
+ }