Merge github.com:google/grpc into api

pull/34/head
Craig Tiller 10 years ago
commit 375605b65f
  1. 65
      .clang-format
  2. 6
      .gitignore
  3. 1573
      Makefile
  4. 162
      build.json
  5. 2
      include/grpc++/async_server_context.h
  6. 8
      include/grpc++/channel_interface.h
  7. 1
      include/grpc++/config.h
  8. 1
      include/grpc++/create_channel.h
  9. 29
      include/grpc++/credentials.h
  10. 6
      include/grpc++/impl/internal_stub.h
  11. 10
      include/grpc++/impl/rpc_method.h
  12. 30
      include/grpc++/impl/rpc_service_method.h
  13. 1
      include/grpc++/status_code_enum.h
  14. 2
      include/grpc/byte_buffer.h
  15. 2
      include/grpc/byte_buffer_reader.h
  16. 5
      include/grpc/grpc.h
  17. 2
      include/grpc/grpc_security.h
  18. 3
      include/grpc/status.h
  19. 2
      include/grpc/support/alloc.h
  20. 2
      include/grpc/support/atm.h
  21. 2
      include/grpc/support/atm_gcc_atomic.h
  22. 2
      include/grpc/support/atm_gcc_sync.h
  23. 4
      include/grpc/support/atm_win32.h
  24. 2
      include/grpc/support/cancellable_platform.h
  25. 2
      include/grpc/support/cmdline.h
  26. 2
      include/grpc/support/histogram.h
  27. 20
      include/grpc/support/log.h
  28. 4
      include/grpc/support/port_platform.h
  29. 2
      include/grpc/support/slice.h
  30. 2
      include/grpc/support/slice_buffer.h
  31. 2
      include/grpc/support/string.h
  32. 2
      include/grpc/support/sync.h
  33. 2
      include/grpc/support/sync_generic.h
  34. 2
      include/grpc/support/sync_posix.h
  35. 2
      include/grpc/support/sync_win32.h
  36. 2
      include/grpc/support/thd.h
  37. 2
      include/grpc/support/thd_posix.h
  38. 2
      include/grpc/support/thd_win32.h
  39. 2
      include/grpc/support/time.h
  40. 2
      include/grpc/support/time_posix.h
  41. 2
      include/grpc/support/time_win32.h
  42. 2
      include/grpc/support/useful.h
  43. 68
      src/compiler/cpp_generator.cc
  44. 3
      src/compiler/cpp_generator_helpers.h
  45. 7
      src/compiler/cpp_plugin.cc
  46. 530
      src/compiler/go_generator.cc
  47. 51
      src/compiler/go_generator.h
  48. 83
      src/compiler/go_plugin.cc
  49. 13
      src/compiler/ruby_generator.cc
  50. 8
      src/compiler/ruby_generator_helpers-inl.h
  51. 6
      src/compiler/ruby_generator_map-inl.h
  52. 6
      src/compiler/ruby_generator_string-inl.h
  53. 2
      src/core/channel/channel_args.h
  54. 11
      src/core/channel/channel_stack.c
  55. 2
      src/core/channel/channel_stack.h
  56. 2
      src/core/channel/client_channel.h
  57. 2
      src/core/channel/client_setup.h
  58. 49
      src/core/channel/connected_channel.c
  59. 2
      src/core/channel/connected_channel.h
  60. 108
      src/core/channel/http_server_filter.c
  61. 2
      src/core/channel/metadata_buffer.c
  62. 2
      src/core/channel/metadata_buffer.h
  63. 4
      src/core/channel/noop_filter.c
  64. 2
      src/core/channel/noop_filter.h
  65. 2
      src/core/compression/algorithm.h
  66. 2
      src/core/compression/message_compress.h
  67. 2
      src/core/httpcli/format_request.h
  68. 2
      src/core/httpcli/httpcli.h
  69. 2
      src/core/httpcli/httpcli_security_context.h
  70. 2
      src/core/httpcli/parser.h
  71. 1
      src/core/iomgr/pollset.h
  72. 28
      src/core/iomgr/sockaddr_utils.c
  73. 6
      src/core/iomgr/sockaddr_utils.h
  74. 5
      src/core/iomgr/tcp_server.h
  75. 87
      src/core/iomgr/tcp_server_posix.c
  76. 5
      src/core/security/auth.c
  77. 2
      src/core/security/auth.h
  78. 21
      src/core/security/credentials.c
  79. 12
      src/core/security/credentials.h
  80. 80
      src/core/security/factories.c
  81. 2
      src/core/security/google_root_certs.h
  82. 2
      src/core/security/secure_endpoint.h
  83. 2
      src/core/security/secure_transport_setup.h
  84. 131
      src/core/security/security_context.c
  85. 36
      src/core/security/security_context.h
  86. 3
      src/core/security/server_secure_chttp2.c
  87. 2
      src/core/statistics/census_interface.h
  88. 2
      src/core/statistics/census_rpc_stats.h
  89. 30
      src/core/statistics/census_tracing.c
  90. 6
      src/core/statistics/hash_table.c
  91. 4
      src/core/support/alloc.c
  92. 2
      src/core/support/cpu.h
  93. 24
      src/core/support/cpu_linux.c
  94. 23
      src/core/support/log.c
  95. 24
      src/core/support/log_android.c
  96. 31
      src/core/support/log_linux.c
  97. 41
      src/core/support/log_posix.c
  98. 40
      src/core/support/log_win32.c
  99. 2
      src/core/support/murmur_hash.h
  100. 2
      src/core/support/thd_internal.h
  101. Some files were not shown because too many files have changed in this diff Show More

@ -0,0 +1,65 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: true
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: true
BinPackArguments: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
DerivePointerAlignment: true
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpacesBeforeTrailingComments: 2
Cpp11BracedListStyle: true
Standard: Auto
IndentWidth: 2
TabWidth: 8
UseTab: Never
BreakBeforeBraces: Attach
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
DisableFormat: false
...

6
.gitignore vendored

@ -0,0 +1,6 @@
bins
deps
libs
objs
*.pyc

1573
Makefile

File diff suppressed because one or more lines are too long

@ -257,6 +257,7 @@
"src/core/security/auth.c",
"src/core/security/base64.c",
"src/core/security/credentials.c",
"src/core/security/factories.c",
"src/core/security/google_root_certs.c",
"src/core/security/json_token.c",
"src/core/security/secure_endpoint.c",
@ -281,8 +282,7 @@
"src/core/tsi/fake_transport_security.h",
"src/core/tsi/ssl_transport_security.h",
"src/core/tsi/transport_security.h",
"src/core/tsi/transport_security_interface.h",
"src/core/tsi/transport_security_test_lib.h"
"src/core/tsi/transport_security_interface.h"
],
"deps": [
"gpr"
@ -301,6 +301,14 @@
"gpr"
]
},
{
"name": "gpr_test_util",
"build": "private",
"vs_project_guid": "{EAB0A629-17A9-44DB-B5FF-E91A721FE037}",
"src": [
"test/core/util/test_config.c"
]
},
{
"name": "grpc_test_util",
"build": "private",
@ -315,10 +323,9 @@
"test/core/statistics/census_log_tests.c",
"test/core/transport/transport_end2end_tests.c",
"test/core/util/grpc_profiler.c",
"test/core/util/parse_hexstring.c",
"test/core/util/port_posix.c",
"test/core/util/slice_splitter.c",
"test/core/util/test_config.c"
"test/core/util/parse_hexstring.c",
"test/core/util/slice_splitter.c"
]
},
{
@ -335,7 +342,7 @@
"src/cpp/client/credentials.cc",
"src/cpp/client/internal_stub.cc",
"src/cpp/proto/proto_utils.cc",
"src/cpp/rpc_method.cc",
"src/cpp/common/rpc_method.cc",
"src/cpp/server/async_server.cc",
"src/cpp/server/async_server_context.cc",
"src/cpp/server/completion_queue.cc",
@ -359,6 +366,9 @@
"include/grpc++/config.h",
"include/grpc++/create_channel.h",
"include/grpc++/credentials.h",
"include/grpc++/impl/internal_stub.h",
"include/grpc++/impl/rpc_method.h",
"include/grpc++/impl/rpc_service_method.h",
"include/grpc++/server_builder.h",
"include/grpc++/server_context.h",
"include/grpc++/server_credentials.h",
@ -369,10 +379,7 @@
],
"headers": [
"src/cpp/client/channel.h",
"src/cpp/client/internal_stub.h",
"src/cpp/proto/proto_utils.h",
"src/cpp/rpc_method.h",
"src/cpp/server/rpc_service_method.h",
"src/cpp/server/server_rpc_handler.h",
"src/cpp/server/thread_pool.h",
"src/cpp/stream/stream_context.h",
@ -384,7 +391,7 @@
},
{
"name": "grpc++_test_util",
"build": "test",
"build": "private",
"src": [
"test/cpp/util/messages.proto",
"test/cpp/util/echo.proto",
@ -442,6 +449,24 @@
"deps": []
},
{
"name": "go_plugin",
"build": "protoc",
"c++": true,
"secure": false,
"src": [
"src/compiler/go_plugin.cpp",
"src/compiler/go_generator.cpp"
],
"headers": [
"src/compiler/go_generator.h",
"src/compiler/go_generator_helpers-inl.h",
"src/compiler/go_generator_map-inl.h",
"src/compiler/go_generator_string-inl.h"
],
"deps": []
},
{
"name": "grpc_byte_buffer_reader_test",
"build": "test",
@ -451,6 +476,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -461,7 +487,7 @@
"test/core/support/cancellable_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -472,7 +498,7 @@
"test/core/support/log_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -483,7 +509,7 @@
"test/core/support/useful_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -494,7 +520,7 @@
"test/core/support/cmdline_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -505,7 +531,7 @@
"test/core/support/histogram_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -516,7 +542,7 @@
"test/core/support/host_port_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -527,7 +553,7 @@
"test/core/support/slice_buffer_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -538,7 +564,7 @@
"test/core/support/slice_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -549,7 +575,7 @@
"test/core/support/string_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -560,7 +586,7 @@
"test/core/support/sync_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -571,7 +597,7 @@
"test/core/support/thd_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -582,7 +608,7 @@
"test/core/support/time_test.c"
],
"deps": [
"grpc_test_util",
"gpr_test_util",
"gpr"
]
},
@ -593,8 +619,7 @@
"test/core/support/murmur_hash_test.c"
],
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -607,6 +632,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -619,6 +645,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -631,6 +658,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -643,6 +671,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -655,6 +684,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -667,6 +697,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -679,6 +710,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -691,6 +723,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -703,6 +736,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -715,6 +749,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -727,6 +762,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -739,6 +775,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -751,6 +788,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -763,6 +801,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -775,6 +814,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -787,6 +827,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -799,6 +840,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -811,6 +853,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -823,6 +866,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -835,6 +879,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -847,6 +892,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -859,6 +905,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -871,6 +918,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -883,6 +931,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -895,6 +944,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -907,6 +957,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -919,6 +970,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -931,6 +983,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -943,6 +996,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -955,6 +1009,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -967,6 +1022,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -980,6 +1036,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -993,6 +1050,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1005,6 +1063,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1018,6 +1077,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1031,6 +1091,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1043,6 +1104,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1055,6 +1117,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1067,6 +1130,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1079,6 +1143,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1091,6 +1156,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1103,6 +1169,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1115,6 +1182,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1127,6 +1195,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1139,6 +1208,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1151,6 +1221,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1163,6 +1234,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1175,6 +1247,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1187,6 +1260,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1199,6 +1273,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1211,6 +1286,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1223,6 +1299,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1237,6 +1314,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1251,6 +1329,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1262,9 +1341,11 @@
"test/cpp/end2end/sync_client_async_server_test.cc"
],
"deps": [
"grpc++_test_util",
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1273,9 +1354,7 @@
"build": "test",
"c++": true,
"src": [
"test/cpp/interop/empty.proto",
"test/cpp/interop/messages.proto",
"test/cpp/interop/test.proto",
"test/cpp/qps/qpstest.proto",
"test/cpp/qps/client.cc"
],
"deps": [
@ -1283,6 +1362,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1291,9 +1371,7 @@
"build": "test",
"c++": true,
"src": [
"test/cpp/interop/empty.proto",
"test/cpp/interop/messages.proto",
"test/cpp/interop/test.proto",
"test/cpp/qps/qpstest.proto",
"test/cpp/qps/server.cc"
],
"deps": [
@ -1301,6 +1379,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1320,6 +1399,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1339,6 +1419,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1354,6 +1435,7 @@
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1366,7 +1448,21 @@
],
"deps": [
"grpc++",
"grpc"
"grpc",
"gpr"
]
},
{
"name": "credentials_test",
"build": "test",
"c++": true,
"src": [
"test/cpp/client/credentials_test.cc"
],
"deps": [
"grpc++",
"grpc",
"gpr"
]
},
{
@ -1378,6 +1474,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1390,6 +1487,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1402,6 +1500,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
},
@ -1414,6 +1513,7 @@
"deps": [
"grpc_test_util",
"grpc",
"gpr_test_util",
"gpr"
]
}

@ -87,7 +87,7 @@ class AsyncServerContext {
system_clock::time_point absolute_deadline_;
google::protobuf::Message* request_; // not owned
grpc_call* call_; // owned
grpc_call* call_; // owned
};
} // namespace grpc

@ -57,10 +57,10 @@ class ChannelInterface {
const google::protobuf::Message& request,
google::protobuf::Message* result) = 0;
virtual StreamContextInterface* CreateStream(const RpcMethod& method,
ClientContext* context,
const google::protobuf::Message* request,
google::protobuf::Message* result) = 0;
virtual StreamContextInterface* CreateStream(
const RpcMethod& method, ClientContext* context,
const google::protobuf::Message* request,
google::protobuf::Message* result) = 0;
};
} // namespace grpc

@ -39,7 +39,6 @@
namespace grpc {
typedef std::string string;
}
#endif // __GRPCPP_CONFIG_H__

@ -46,6 +46,7 @@ class ChannelInterface;
std::shared_ptr<ChannelInterface> CreateChannel(const grpc::string& target,
const ChannelArguments& args);
// If creds does not hold an object or is invalid, a lame channel is returned.
std::shared_ptr<ChannelInterface> CreateChannel(
const grpc::string& target, const std::unique_ptr<Credentials>& creds,
const ChannelArguments& args);

@ -34,6 +34,7 @@
#ifndef __GRPCPP_CREDENTIALS_H_
#define __GRPCPP_CREDENTIALS_H_
#include <chrono>
#include <memory>
#include <grpc++/config.h>
@ -49,6 +50,7 @@ namespace grpc {
class Credentials final {
public:
~Credentials();
// TODO(abhikumar): Specify a plugin API here to be implemented by
// credentials that do not have a corresponding implementation in C.
@ -63,6 +65,15 @@ class Credentials final {
};
// Options used to build SslCredentials
// pem_roots_cert is the buffer containing the PEM encoding of the server root
// certificates. This parameter cannot be empty.
// pem_private_key is the buffer containing the PEM encoding of the client's
// private key. This parameter can be empty if the client does not have a
// private key.
// pem_cert_chain is the buffer containing the PEM encoding of the client's
// certificate chain. This parameter can be empty if the client does not have
// a certificate chain.
// TODO(jboeuf) Change it to point to a file.
struct SslCredentialsOptions {
grpc::string pem_root_certs;
grpc::string pem_private_key;
@ -70,6 +81,10 @@ struct SslCredentialsOptions {
};
// Factory for building different types of Credentials
// The methods may return empty unique_ptr when credentials cannot be created.
// If a Credentials pointer is returned, it can still be invalid when used to
// create a channel. A lame channel will be created then and all rpcs will
// fail on it.
class CredentialsFactory {
public:
// Builds credentials with reasonable defaults.
@ -82,6 +97,20 @@ class CredentialsFactory {
// Builds credentials for use when running in GCE
static std::unique_ptr<Credentials> ComputeEngineCredentials();
// Builds service account credentials.
// json_key is the JSON key string containing the client's private key.
// scope is a space-delimited list of the requested permissions.
// token_lifetime is the lifetime of each token acquired through this service
// account credentials. It should be positive and should not exceed
// grpc_max_auth_token_lifetime or will be cropped to this value.
static std::unique_ptr<Credentials> ServiceAccountCredentials(
const grpc::string& json_key, const grpc::string& scope,
std::chrono::seconds token_lifetime);
// Builds IAM credentials.
static std::unique_ptr<Credentials> IAMCredentials(
const grpc::string& authorization_token,
const grpc::string& authority_selector);
// Combines two credentials objects into a composite credentials
static std::unique_ptr<Credentials> ComposeCredentials(

@ -31,8 +31,8 @@
*
*/
#ifndef __GRPCPP_INTERNAL_CLIENT_INTERNAL_STUB_H__
#define __GRPCPP_INTERNAL_CLIENT_INTERNAL_STUB_H__
#ifndef __GRPCPP_IMPL_INTERNAL_STUB_H__
#define __GRPCPP_IMPL_INTERNAL_STUB_H__
#include <memory>
@ -57,4 +57,4 @@ class InternalStub {
} // namespace grpc
#endif // __GRPCPP_INTERNAL_CLIENT_INTERNAL_STUB_H__
#endif // __GRPCPP_IMPL_INTERNAL_STUB_H__

@ -31,8 +31,8 @@
*
*/
#ifndef __GRPCPP_INTERNAL_RPC_METHOD_H__
#define __GRPCPP_INTERNAL_RPC_METHOD_H__
#ifndef __GRPCPP_IMPL_RPC_METHOD_H__
#define __GRPCPP_IMPL_RPC_METHOD_H__
namespace google {
namespace protobuf {
@ -55,15 +55,15 @@ class RpcMethod {
: name_(name), method_type_(NORMAL_RPC) {}
RpcMethod(const char* name, RpcType type) : name_(name), method_type_(type) {}
const char *name() const { return name_; }
const char* name() const { return name_; }
RpcType method_type() const { return method_type_; }
private:
const char *name_;
const char* name_;
const RpcType method_type_;
};
} // namespace grpc
#endif // __GRPCPP_INTERNAL_RPC_METHOD_H__
#endif // __GRPCPP_IMPL_RPC_METHOD_H__

@ -31,18 +31,18 @@
*
*/
#ifndef __GRPCPP_INTERNAL_SERVER_RPC_SERVICE_METHOD_H__
#define __GRPCPP_INTERNAL_SERVER_RPC_SERVICE_METHOD_H__
#ifndef __GRPCPP_IMPL_RPC_SERVICE_METHOD_H__
#define __GRPCPP_IMPL_RPC_SERVICE_METHOD_H__
#include <functional>
#include <map>
#include <memory>
#include <vector>
#include "src/cpp/rpc_method.h"
#include <google/protobuf/message.h>
#include <grpc++/impl/rpc_method.h>
#include <grpc++/status.h>
#include <grpc++/stream.h>
#include <google/protobuf/message.h>
namespace grpc {
class ServerContext;
@ -55,14 +55,17 @@ class MethodHandler {
public:
virtual ~MethodHandler() {}
struct HandlerParameter {
HandlerParameter(ServerContext* context, const google::protobuf::Message* req,
HandlerParameter(ServerContext* context,
const google::protobuf::Message* req,
google::protobuf::Message* resp)
: server_context(context),
request(req),
response(resp),
stream_context(nullptr) {}
HandlerParameter(ServerContext* context, const google::protobuf::Message* req,
google::protobuf::Message* resp, StreamContextInterface* stream)
HandlerParameter(ServerContext* context,
const google::protobuf::Message* req,
google::protobuf::Message* resp,
StreamContextInterface* stream)
: server_context(context),
request(req),
response(resp),
@ -171,7 +174,8 @@ class RpcServiceMethod : public RpcMethod {
public:
// Takes ownership of the handler and two prototype objects.
RpcServiceMethod(const char* name, RpcMethod::RpcType type,
MethodHandler* handler, google::protobuf::Message* request_prototype,
MethodHandler* handler,
google::protobuf::Message* request_prototype,
google::protobuf::Message* response_prototype)
: RpcMethod(name, type),
handler_(handler),
@ -180,7 +184,9 @@ class RpcServiceMethod : public RpcMethod {
MethodHandler* handler() { return handler_.get(); }
google::protobuf::Message* AllocateRequestProto() { return request_prototype_->New(); }
google::protobuf::Message* AllocateRequestProto() {
return request_prototype_->New();
}
google::protobuf::Message* AllocateResponseProto() {
return response_prototype_->New();
}
@ -200,9 +206,7 @@ class RpcService {
methods_.push_back(std::unique_ptr<RpcServiceMethod>(method));
}
RpcServiceMethod* GetMethod(int i) {
return methods_[i].get();
}
RpcServiceMethod* GetMethod(int i) { return methods_[i].get(); }
int GetMethodCount() const { return methods_.size(); }
private:
@ -211,4 +215,4 @@ class RpcService {
} // namespace grpc
#endif // __GRPCPP_INTERNAL_SERVER_RPC_SERVICE_METHOD_H__
#endif // __GRPCPP_IMPL_RPC_SERVICE_METHOD_H__

@ -34,7 +34,6 @@
#ifndef __GRPCPP_STATUS_CODE_ENUM_H__
#define __GRPCPP_STATUS_CODE_ENUM_H__
namespace grpc {
enum StatusCode {

@ -47,4 +47,4 @@ struct grpc_byte_buffer {
} data;
};
#endif /* __GRPC_BYTE_BUFFER_H__ */
#endif /* __GRPC_BYTE_BUFFER_H__ */

@ -46,4 +46,4 @@ struct grpc_byte_buffer_reader {
} current;
};
#endif /* __GRPC_BYTE_BUFFER_READER_H__ */
#endif /* __GRPC_BYTE_BUFFER_READER_H__ */

@ -433,7 +433,8 @@ grpc_call_error grpc_server_request_call(grpc_server *server, void *tag_new);
grpc_server *grpc_server_create(grpc_completion_queue *cq,
const grpc_channel_args *args);
/* Add a http2 over tcp listener; returns 1 on success, 0 on failure
/* Add a http2 over tcp listener.
Returns bound port number on success, 0 on failure.
REQUIRES: server not started */
int grpc_server_add_http2_port(grpc_server *server, const char *addr);
@ -457,4 +458,4 @@ void grpc_server_destroy(grpc_server *server);
}
#endif
#endif /* __GRPC_GRPC_H__ */
#endif /* __GRPC_GRPC_H__ */

@ -97,7 +97,6 @@ grpc_credentials *grpc_fake_transport_security_credentials_create(void);
grpc_credentials *grpc_iam_credentials_create(const char *authorization_token,
const char *authority_selector);
/* --- Secure channel creation. --- */
/* The caller of the secure_channel_create functions may override the target
@ -152,7 +151,6 @@ grpc_server_credentials *grpc_ssl_server_credentials_create(
grpc_server_credentials *grpc_fake_transport_security_server_credentials_create(
void);
/* --- Secure server creation. --- */
/* Creates a secure server using the passed-in server credentials. */

@ -34,7 +34,6 @@
#ifndef __GRPC_STATUS_H__
#define __GRPC_STATUS_H__
#ifdef __cplusplus
extern "C" {
#endif
@ -200,4 +199,4 @@ typedef enum {
}
#endif
#endif /* __GRPC_STATUS_H__ */
#endif /* __GRPC_STATUS_H__ */

@ -55,4 +55,4 @@ void gpr_free_aligned(void *ptr);
}
#endif
#endif /* __GRPC_SUPPORT_ALLOC_H__ */
#endif /* __GRPC_SUPPORT_ALLOC_H__ */

@ -89,4 +89,4 @@
#error could not determine platform for atm
#endif
#endif /* __GRPC_SUPPORT_ATM_H__ */
#endif /* __GRPC_SUPPORT_ATM_H__ */

@ -66,4 +66,4 @@ static __inline int gpr_atm_rel_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
__ATOMIC_RELAXED);
}
#endif /* __GRPC_SUPPORT_ATM_GCC_ATOMIC_H__ */
#endif /* __GRPC_SUPPORT_ATM_GCC_ATOMIC_H__ */

@ -70,4 +70,4 @@ static __inline void gpr_atm_rel_store(gpr_atm *p, gpr_atm value) {
#define gpr_atm_acq_cas(p, o, n) (__sync_bool_compare_and_swap((p), (o), (n)))
#define gpr_atm_rel_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
#endif /* __GRPC_SUPPORT_ATM_GCC_SYNC_H__ */
#endif /* __GRPC_SUPPORT_ATM_GCC_SYNC_H__ */

@ -55,8 +55,8 @@ static __inline void gpr_atm_rel_store(gpr_atm *p, gpr_atm value) {
}
static __inline int gpr_atm_no_barrier_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
/* InterlockedCompareExchangePointerNoFence() not available on vista or
windows7 */
/* InterlockedCompareExchangePointerNoFence() not available on vista or
windows7 */
#ifdef GPR_ARCH_64
return o == (gpr_atm)InterlockedCompareExchangeAcquire64(p, n, o);
#else

@ -53,4 +53,4 @@ typedef struct {
struct gpr_cancellable_list_ waiters;
} gpr_cancellable;
#endif /* __GRPC_SUPPORT_CANCELLABLE_PLATFORM_H__ */
#endif /* __GRPC_SUPPORT_CANCELLABLE_PLATFORM_H__ */

@ -92,4 +92,4 @@ void gpr_cmdline_destroy(gpr_cmdline *cl);
}
#endif
#endif /* __GRPC_SUPPORT_CMDLINE_H__ */
#endif /* __GRPC_SUPPORT_CMDLINE_H__ */

@ -63,4 +63,4 @@ double gpr_histogram_sum_of_squares(gpr_histogram *histogram);
}
#endif
#endif /* __GRPC_SUPPORT_HISTOGRAM_H__ */
#endif /* __GRPC_SUPPORT_HISTOGRAM_H__ */

@ -72,9 +72,21 @@ const char *gpr_log_severity_string(gpr_log_severity severity);
void gpr_log(const char *file, int line, gpr_log_severity severity,
const char *format, ...);
/* Same as above, but using a va_list instead. */
void gpr_vlog(const char *file, int line, gpr_log_severity severity,
const char *format, va_list args);
void gpr_log_message(const char *file, int line, gpr_log_severity severity,
const char *message);
/* Log overrides: applications can use this API to intercept logging calls
and use their own implementations */
typedef struct {
const char *file;
int line;
gpr_log_severity severity;
const char *message;
} gpr_log_func_args;
typedef void (*gpr_log_func)(gpr_log_func_args *args);
void gpr_set_log_function(gpr_log_func func);
/* abort() the process if x is zero, having written a line to the log.
@ -93,4 +105,4 @@ void gpr_vlog(const char *file, int line, gpr_log_severity severity,
}
#endif
#endif /* __GRPC_SUPPORT_LOG_H__ */
#endif /* __GRPC_SUPPORT_LOG_H__ */

@ -126,8 +126,8 @@
#error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
#endif
#if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) != 1
#error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX
#if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WIN32) != 1
#error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WIN32
#endif
typedef int16_t gpr_int16;

@ -173,4 +173,4 @@ int gpr_slice_str_cmp(gpr_slice a, const char *b);
}
#endif
#endif /* __GRPC_SUPPORT_SLICE_H__ */
#endif /* __GRPC_SUPPORT_SLICE_H__ */

@ -81,4 +81,4 @@ void gpr_slice_buffer_reset_and_unref(gpr_slice_buffer *sb);
}
#endif
#endif /* __GRPC_SUPPORT_SLICE_BUFFER_H__ */
#endif /* __GRPC_SUPPORT_SLICE_BUFFER_H__ */

@ -74,4 +74,4 @@ int gpr_asprintf(char **strp, const char *format, ...);
}
#endif
#endif /* __GRPC_SUPPORT_STRING_H__ */
#endif /* __GRPC_SUPPORT_STRING_H__ */

@ -345,4 +345,4 @@ gpr_intptr gpr_stats_read(const gpr_stats_counter *c);
}
#endif
#endif /* __GRPC_SUPPORT_SYNC_H__ */
#endif /* __GRPC_SUPPORT_SYNC_H__ */

@ -52,4 +52,4 @@ typedef struct { gpr_atm value; } gpr_stats_counter;
#define GPR_STATS_INIT \
{ 0 }
#endif /* __GRPC_SUPPORT_SYNC_GENERIC_H__ */
#endif /* __GRPC_SUPPORT_SYNC_GENERIC_H__ */

@ -45,4 +45,4 @@ typedef pthread_once_t gpr_once;
#define GPR_ONCE_INIT PTHREAD_ONCE_INIT
#endif /* __GRPC_SUPPORT_SYNC_POSIX_H__ */
#endif /* __GRPC_SUPPORT_SYNC_POSIX_H__ */

@ -49,4 +49,4 @@ typedef CONDITION_VARIABLE gpr_cv;
typedef INIT_ONCE gpr_once;
#define GPR_ONCE_INIT INIT_ONCE_STATIC_INIT
#endif /* __GRPC_SUPPORT_SYNC_WIN32_H__ */
#endif /* __GRPC_SUPPORT_SYNC_WIN32_H__ */

@ -76,4 +76,4 @@ gpr_thd_options gpr_thd_options_default(void);
}
#endif
#endif /* __GRPC_SUPPORT_THD_H__ */
#endif /* __GRPC_SUPPORT_THD_H__ */

@ -39,4 +39,4 @@
typedef pthread_t gpr_thd_id;
#endif /* __GRPC_SUPPORT_THD_POSIX_H__ */
#endif /* __GRPC_SUPPORT_THD_POSIX_H__ */

@ -41,4 +41,4 @@
typedef int gpr_thd_id;
#endif /* __GRPC_SUPPORT_THD_WIN32_H__ */
#endif /* __GRPC_SUPPORT_THD_WIN32_H__ */

@ -113,4 +113,4 @@ double gpr_timespec_to_micros(gpr_timespec t);
}
#endif
#endif /* __GRPC_SUPPORT_TIME_H__ */
#endif /* __GRPC_SUPPORT_TIME_H__ */

@ -40,4 +40,4 @@
typedef struct timespec gpr_timespec;
#endif /* __GRPC_SUPPORT_TIME_POSIX_H__ */
#endif /* __GRPC_SUPPORT_TIME_POSIX_H__ */

@ -43,4 +43,4 @@ typedef struct gpr_timespec {
long tv_nsec;
} gpr_timespec;
#endif /* __GRPC_SUPPORT_TIME_WIN32_H__ */
#endif /* __GRPC_SUPPORT_TIME_WIN32_H__ */

@ -45,4 +45,4 @@
#define GPR_ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array)))
#endif /* __GRPC_SUPPORT_USEFUL_H__ */
#endif /* __GRPC_SUPPORT_USEFUL_H__ */

@ -43,23 +43,19 @@ namespace grpc_cpp_generator {
namespace {
bool NoStreaming(const google::protobuf::MethodDescriptor* method) {
return !method->client_streaming() &&
!method->server_streaming();
return !method->client_streaming() && !method->server_streaming();
}
bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor* method) {
return method->client_streaming() &&
!method->server_streaming();
return method->client_streaming() && !method->server_streaming();
}
bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor* method) {
return !method->client_streaming() &&
method->server_streaming();
return !method->client_streaming() && method->server_streaming();
}
bool BidiStreaming(const google::protobuf::MethodDescriptor* method) {
return method->client_streaming() &&
method->server_streaming();
return method->client_streaming() && method->server_streaming();
}
bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor* file) {
@ -98,7 +94,7 @@ bool HasBidiStreaming(const google::protobuf::FileDescriptor* file) {
string GetHeaderIncludes(const google::protobuf::FileDescriptor* file) {
string temp =
"#include \"src/cpp/client/internal_stub.h\"\n"
"#include \"grpc++/impl/internal_stub.h\"\n"
"#include \"grpc++/status.h\"\n"
"\n"
"namespace grpc {\n"
@ -126,15 +122,15 @@ string GetHeaderIncludes(const google::protobuf::FileDescriptor* file) {
}
string GetSourceIncludes() {
return "#include \"src/cpp/rpc_method.h\"\n"
"#include \"src/cpp/server/rpc_service_method.h\"\n"
"#include \"grpc++/channel_interface.h\"\n"
return "#include \"grpc++/channel_interface.h\"\n"
"#include \"grpc++/impl/rpc_method.h\"\n"
"#include \"grpc++/impl/rpc_service_method.h\"\n"
"#include \"grpc++/stream.h\"\n";
}
void PrintHeaderClientMethod(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] =
grpc_cpp_generator::ClassName(method->input_type(), true);
@ -205,8 +201,9 @@ void PrintHeaderService(google::protobuf::io::Printer* printer,
printer->Indent();
// Client side
printer->Print("class Stub : public ::grpc::InternalStub {\n"
" public:\n");
printer->Print(
"class Stub : public ::grpc::InternalStub {\n"
" public:\n");
printer->Indent();
for (int i = 0; i < service->method_count(); ++i) {
PrintHeaderClientMethod(printer, service->method(i), vars);
@ -220,8 +217,9 @@ void PrintHeaderService(google::protobuf::io::Printer* printer,
printer->Print("\n");
// Server side
printer->Print("class Service {\n"
" public:\n");
printer->Print(
"class Service {\n"
" public:\n");
printer->Indent();
printer->Print("Service() : service_(nullptr) {}\n");
printer->Print("virtual ~Service();\n");
@ -230,8 +228,9 @@ void PrintHeaderService(google::protobuf::io::Printer* printer,
}
printer->Print("::grpc::RpcService* service();\n");
printer->Outdent();
printer->Print(" private:\n"
" ::grpc::RpcService* service_;\n");
printer->Print(
" private:\n"
" ::grpc::RpcService* service_;\n");
printer->Print("};\n");
printer->Outdent();
@ -252,8 +251,8 @@ string GetHeaderServices(const google::protobuf::FileDescriptor* file) {
}
void PrintSourceClientMethod(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] =
grpc_cpp_generator::ClassName(method->input_type(), true);
@ -309,8 +308,8 @@ void PrintSourceClientMethod(google::protobuf::io::Printer* printer,
}
void PrintSourceServerMethod(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] =
grpc_cpp_generator::ClassName(method->input_type(), true);
@ -363,12 +362,12 @@ void PrintSourceService(google::protobuf::io::Printer* printer,
map<string, string>* vars) {
(*vars)["Service"] = service->name();
printer->Print(*vars,
"$Service$::Stub* $Service$::NewStub("
"const std::shared_ptr<::grpc::ChannelInterface>& channel) {\n"
" $Service$::Stub* stub = new $Service$::Stub();\n"
" stub->set_channel(channel);\n"
" return stub;\n"
"};\n\n");
"$Service$::Stub* $Service$::NewStub("
"const std::shared_ptr<::grpc::ChannelInterface>& channel) {\n"
" $Service$::Stub* stub = new $Service$::Stub();\n"
" stub->set_channel(channel);\n"
" return stub;\n"
"};\n\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintSourceClientMethod(printer, service->method(i), vars);
}
@ -381,11 +380,12 @@ void PrintSourceService(google::protobuf::io::Printer* printer,
PrintSourceServerMethod(printer, service->method(i), vars);
}
printer->Print(*vars,
"::grpc::RpcService* $Service$::Service::service() {\n");
"::grpc::RpcService* $Service$::Service::service() {\n");
printer->Indent();
printer->Print("if (service_ != nullptr) {\n"
" return service_;\n"
"}\n");
printer->Print(
"if (service_ != nullptr) {\n"
" return service_;\n"
"}\n");
printer->Print("service_ = new ::grpc::RpcService();\n");
for (int i = 0; i < service->method_count(); ++i) {
const google::protobuf::MethodDescriptor* method = service->method(i);

@ -85,7 +85,8 @@ inline string DotsToUnderscores(const string& name) {
return StringReplace(name, ".", "_");
}
inline string ClassName(const google::protobuf::Descriptor* descriptor, bool qualified) {
inline string ClassName(const google::protobuf::Descriptor* descriptor,
bool qualified) {
// Find "outer", the descriptor of the top-level message in which
// "descriptor" is embedded.
const google::protobuf::Descriptor* outer = descriptor;

@ -54,9 +54,10 @@ class CppGrpcGenerator : public google::protobuf::compiler::CodeGenerator {
google::protobuf::compiler::GeneratorContext* context,
string* error) const {
if (file->options().cc_generic_services()) {
*error = "cpp grpc proto compiler plugin does not work with generic "
"services. To generate cpp grpc APIs, please set \""
"cc_generic_service = false\".";
*error =
"cpp grpc proto compiler plugin does not work with generic "
"services. To generate cpp grpc APIs, please set \""
"cc_generic_service = false\".";
return false;
}

@ -0,0 +1,530 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "src/compiler/go_generator.h"
#include <cctype>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/descriptor.h>
using namespace std;
namespace grpc_go_generator {
bool NoStreaming(const google::protobuf::MethodDescriptor* method) {
return !method->client_streaming() && !method->server_streaming();
}
bool ClientOnlyStreaming(const google::protobuf::MethodDescriptor* method) {
return method->client_streaming() && !method->server_streaming();
}
bool ServerOnlyStreaming(const google::protobuf::MethodDescriptor* method) {
return !method->client_streaming() && method->server_streaming();
}
bool BidiStreaming(const google::protobuf::MethodDescriptor* method) {
return method->client_streaming() && method->server_streaming();
}
bool HasClientOnlyStreaming(const google::protobuf::FileDescriptor* file) {
for (int i = 0; i < file->service_count(); i++) {
for (int j = 0; j < file->service(i)->method_count(); j++) {
if (ClientOnlyStreaming(file->service(i)->method(j))) {
return true;
}
}
}
return false;
}
string LowerCaseService(const string& service) {
string ret = service;
if (!ret.empty() && ret[0] >= 'A' && ret[0] <= 'Z') {
ret[0] = ret[0] - 'A' + 'a';
}
return ret;
}
void PrintClientMethodDef(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type()->name();
(*vars)["Response"] = method->output_type()->name();
if (NoStreaming(method)) {
printer->Print(*vars,
"\t$Method$(ctx context.Context, in *$Request$, opts "
"...rpc.CallOption) "
"(*$Response$, error)\n");
} else if (BidiStreaming(method)) {
printer->Print(*vars,
"\t$Method$(ctx context.Context, opts ...rpc.CallOption) "
"($Service$_$Method$Client, error)\n");
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"\t$Method$(ctx context.Context, m *$Request$, opts ...rpc.CallOption) "
"($Service$_$Method$Client, error)\n");
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars,
"\t$Method$(ctx context.Context, opts ...rpc.CallOption) "
"($Service$_$Method$Client, error)\n");
}
}
void PrintClientMethodImpl(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type()->name();
(*vars)["Response"] = method->output_type()->name();
if (NoStreaming(method)) {
printer->Print(
*vars,
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, "
"in *$Request$, opts ...rpc.CallOption) (*$Response$, error) {\n");
printer->Print(*vars, "\tout := new($Response$)\n");
printer->Print(*vars,
"\terr := rpc.Invoke(ctx, \"/$Package$$Service$/$Method$\", "
"in, out, c.cc, opts...)\n");
printer->Print("\tif err != nil {\n");
printer->Print("\t\treturn nil, err\n");
printer->Print("\t}\n");
printer->Print("\treturn out, nil\n");
printer->Print("}\n\n");
} else if (BidiStreaming(method)) {
printer->Print(
*vars,
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, opts "
"...rpc.CallOption) ($Service$_$Method$Client, error) {\n"
"\tstream, err := rpc.NewClientStream(ctx, c.cc, "
"\"/$Package$$Service$/$Method$\", opts...)\n"
"\tif err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn &$ServiceStruct$$Method$Client{stream}, nil\n"
"}\n\n");
printer->Print(*vars,
"type $Service$_$Method$Client interface {\n"
"\tSend(*$Request$) error\n"
"\tRecv() (*$Response$, error)\n"
"\trpc.ClientStream\n"
"}\n\n");
printer->Print(*vars,
"type $ServiceStruct$$Method$Client struct {\n"
"\trpc.ClientStream\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Client) Send(m *$Request$) error {\n"
"\treturn x.ClientStream.SendProto(m)\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Client) Recv() (*$Response$, error) "
"{\n"
"\tm := new($Response$)\n"
"\tif err := x.ClientStream.RecvProto(m); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn m, nil\n"
"}\n\n");
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, m "
"*$Request$, "
"opts ...rpc.CallOption) ($Service$_$Method$Client, error) {\n"
"\tstream, err := rpc.NewClientStream(ctx, c.cc, "
"\"/$Package$$Service$/$Method$\", opts...)\n"
"\tif err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\tx := &$ServiceStruct$$Method$Client{stream}\n"
"\tif err := x.ClientStream.SendProto(m); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\tif err := x.ClientStream.CloseSend(); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn x, nil\n"
"}\n\n");
printer->Print(*vars,
"type $Service$_$Method$Client interface {\n"
"\tRecv() (*$Response$, error)\n"
"\trpc.ClientStream\n"
"}\n\n");
printer->Print(*vars,
"type $ServiceStruct$$Method$Client struct {\n"
"\trpc.ClientStream\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Client) Recv() (*$Response$, error) "
"{\n"
"\tm := new($Response$)\n"
"\tif err := x.ClientStream.RecvProto(m); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn m, nil\n"
"}\n\n");
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
"func (c *$ServiceStruct$Client) $Method$(ctx context.Context, opts "
"...rpc.CallOption) ($Service$_$Method$Client, error) {\n"
"\tstream, err := rpc.NewClientStream(ctx, c.cc, "
"\"/$Package$$Service$/$Method$\", opts...)\n"
"\tif err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn &$ServiceStruct$$Method$Client{stream}, nil\n"
"}\n\n");
printer->Print(*vars,
"type $Service$_$Method$Client interface {\n"
"\tSend(*$Request$) error\n"
"\tCloseAndRecv() (*$Response$, error)\n"
"\trpc.ClientStream\n"
"}\n\n");
printer->Print(*vars,
"type $ServiceStruct$$Method$Client struct {\n"
"\trpc.ClientStream\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Client) Send(m *$Request$) error {\n"
"\treturn x.ClientStream.SendProto(m)\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Client) CloseAndRecv() (*$Response$, "
"error) {\n"
"\tif err := x.ClientStream.CloseSend(); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\tm := new($Response$)\n"
"\tif err := x.ClientStream.RecvProto(m); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\t// Read EOF.\n"
"\tif err := x.ClientStream.RecvProto(m); err == io.EOF {\n"
"\t\treturn m, io.EOF\n"
"\t}\n"
"\t// gRPC protocol violation.\n"
"\treturn m, fmt.Errorf(\"Violate gRPC client streaming protocol: no "
"EOF after the response.\")\n"
"}\n\n");
}
}
void PrintClient(google::protobuf::io::Printer* printer,
const google::protobuf::ServiceDescriptor* service,
map<string, string>* vars) {
(*vars)["Service"] = service->name();
(*vars)["ServiceStruct"] = LowerCaseService(service->name());
printer->Print(*vars, "type $Service$Client interface {\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintClientMethodDef(printer, service->method(i), vars);
}
printer->Print("}\n\n");
printer->Print(*vars,
"type $ServiceStruct$Client struct {\n"
"\tcc *rpc.ClientConn\n"
"}\n\n");
printer->Print(
*vars,
"func New$Service$Client(cc *rpc.ClientConn) $Service$Client {\n"
"\treturn &$ServiceStruct$Client{cc}\n"
"}\n\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintClientMethodImpl(printer, service->method(i), vars);
}
}
void PrintServerMethodDef(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type()->name();
(*vars)["Response"] = method->output_type()->name();
if (NoStreaming(method)) {
printer->Print(
*vars,
"\t$Method$(context.Context, *$Request$) (*$Response$, error)\n");
} else if (BidiStreaming(method)) {
printer->Print(*vars, "\t$Method$($Service$_$Method$Server) error\n");
} else if (ServerOnlyStreaming(method)) {
printer->Print(*vars,
"\t$Method$(*$Request$, $Service$_$Method$Server) error\n");
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars, "\t$Method$($Service$_$Method$Server) error\n");
}
}
void PrintServerHandler(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type()->name();
(*vars)["Response"] = method->output_type()->name();
if (NoStreaming(method)) {
printer->Print(
*vars,
"func _$Service$_$Method$_Handler(srv interface{}, ctx context.Context,"
" buf []byte) (proto.Message, error) {\n");
printer->Print(*vars, "\tin := new($Request$)\n");
printer->Print("\tif err := proto.Unmarshal(buf, in); err != nil {\n");
printer->Print("\t\treturn nil, err\n");
printer->Print("\t}\n");
printer->Print(*vars,
"\tout, err := srv.($Service$Server).$Method$(ctx, in)\n");
printer->Print("\tif err != nil {\n");
printer->Print("\t\treturn nil, err\n");
printer->Print("\t}\n");
printer->Print("\treturn out, nil\n");
printer->Print("}\n\n");
} else if (BidiStreaming(method)) {
printer->Print(
*vars,
"func _$Service$_$Method$_Handler(srv interface{}, stream rpc.Stream) "
"error {\n"
"\treturn srv.($Service$Server).$Method$(&$ServiceStruct$$Method$Server"
"{stream})\n"
"}\n\n");
printer->Print(*vars,
"type $Service$_$Method$Server interface {\n"
"\tSend(*$Response$) error\n"
"\tRecv() (*$Request$, error)\n"
"\trpc.Stream\n"
"}\n\n");
printer->Print(*vars,
"type $ServiceStruct$$Method$Server struct {\n"
"\trpc.Stream\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Server) Send(m *$Response$) error {\n"
"\treturn x.Stream.SendProto(m)\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Server) Recv() (*$Request$, error) "
"{\n"
"\tm := new($Request$)\n"
"\tif err := x.Stream.RecvProto(m); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn m, nil\n"
"}\n\n");
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"func _$Service$_$Method$_Handler(srv interface{}, stream rpc.Stream) "
"error {\n"
"\tm := new($Request$)\n"
"\tif err := stream.RecvProto(m); err != nil {\n"
"\t\treturn err\n"
"\t}\n"
"\treturn srv.($Service$Server).$Method$(m, "
"&$ServiceStruct$$Method$Server{stream})\n"
"}\n\n");
printer->Print(*vars,
"type $Service$_$Method$Server interface {\n"
"\tSend(*$Response$) error\n"
"\trpc.Stream\n"
"}\n\n");
printer->Print(*vars,
"type $ServiceStruct$$Method$Server struct {\n"
"\trpc.Stream\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Server) Send(m *$Response$) error {\n"
"\treturn x.Stream.SendProto(m)\n"
"}\n\n");
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
"func _$Service$_$Method$_Handler(srv interface{}, stream rpc.Stream) "
"error {\n"
"\treturn srv.($Service$Server).$Method$(&$ServiceStruct$$Method$Server"
"{stream})\n"
"}\n\n");
printer->Print(*vars,
"type $Service$_$Method$Server interface {\n"
"\tSendAndClose(*$Response$) error\n"
"\tRecv() (*$Request$, error)\n"
"\trpc.Stream\n"
"}\n\n");
printer->Print(*vars,
"type $ServiceStruct$$Method$Server struct {\n"
"\trpc.Stream\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Server) SendAndClose(m *$Response$) "
"error {\n"
"\tif err := x.Stream.SendProto(m); err != nil {\n"
"\t\treturn err\n"
"\t}\n"
"\treturn nil\n"
"}\n\n");
printer->Print(
*vars,
"func (x *$ServiceStruct$$Method$Server) Recv() (*$Request$, error) {\n"
"\tm := new($Request$)\n"
"\tif err := x.Stream.RecvProto(m); err != nil {\n"
"\t\treturn nil, err\n"
"\t}\n"
"\treturn m, nil\n"
"}\n\n");
}
}
void PrintServerMethodDesc(google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
printer->Print("\t\t{\n");
printer->Print(*vars, "\t\t\tMethodName:\t\"$Method$\",\n");
printer->Print(*vars, "\t\t\tHandler:\t_$Service$_$Method$_Handler,\n");
printer->Print("\t\t},\n");
}
void PrintServerStreamingMethodDesc(
google::protobuf::io::Printer* printer,
const google::protobuf::MethodDescriptor* method,
map<string, string>* vars) {
(*vars)["Method"] = method->name();
printer->Print("\t\t{\n");
printer->Print(*vars, "\t\t\tStreamName:\t\"$Method$\",\n");
printer->Print(*vars, "\t\t\tHandler:\t_$Service$_$Method$_Handler,\n");
printer->Print("\t\t},\n");
}
void PrintServer(google::protobuf::io::Printer* printer,
const google::protobuf::ServiceDescriptor* service,
map<string, string>* vars) {
(*vars)["Service"] = service->name();
printer->Print(*vars, "type $Service$Server interface {\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintServerMethodDef(printer, service->method(i), vars);
}
printer->Print("}\n\n");
printer->Print(*vars,
"func RegisterService(s *rpc.Server, srv $Service$Server) {\n"
"\ts.RegisterService(&_$Service$_serviceDesc, srv)\n"
"}\n\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintServerHandler(printer, service->method(i), vars);
}
printer->Print(*vars,
"var _$Service$_serviceDesc = rpc.ServiceDesc{\n"
"\tServiceName: \"$Package$$Service$\",\n"
"\tHandlerType: (*$Service$Server)(nil),\n"
"\tMethods: []rpc.MethodDesc{\n");
for (int i = 0; i < service->method_count(); ++i) {
if (NoStreaming(service->method(i))) {
PrintServerMethodDesc(printer, service->method(i), vars);
}
}
printer->Print("\t},\n");
printer->Print("\tStreams: []rpc.StreamDesc{\n");
for (int i = 0; i < service->method_count(); ++i) {
if (!NoStreaming(service->method(i))) {
PrintServerStreamingMethodDesc(printer, service->method(i), vars);
}
}
printer->Print(
"\t},\n"
"}\n\n");
}
std::string BadToUnderscore(std::string str) {
for (unsigned i = 0; i < str.size(); ++i) {
if (!std::isalnum(str[i])) {
str[i] = '_';
}
}
return str;
}
string GetServices(const google::protobuf::FileDescriptor* file) {
string output;
google::protobuf::io::StringOutputStream output_stream(&output);
google::protobuf::io::Printer printer(&output_stream, '$');
map<string, string> vars;
string package_name = !file->options().go_package().empty()
? file->options().go_package()
: file->package();
vars["PackageName"] = BadToUnderscore(package_name);
printer.Print(vars, "package $PackageName$\n\n");
printer.Print("import (\n");
if (HasClientOnlyStreaming(file)) {
printer.Print(
"\t\"fmt\"\n"
"\t\"io\"\n");
}
printer.Print(
"\t\"google/net/grpc/go/rpc\"\n"
"\tcontext \"google/third_party/golang/go_net/context/context\"\n"
"\tproto \"google/net/proto2/go/proto\"\n"
")\n\n");
// $Package$ is used to fully qualify method names.
vars["Package"] = file->package();
if (!file->package().empty()) {
vars["Package"].append(".");
}
for (int i = 0; i < file->service_count(); ++i) {
PrintClient(&printer, file->service(0), &vars);
printer.Print("\n");
PrintServer(&printer, file->service(0), &vars);
printer.Print("\n");
}
return output;
}
} // namespace grpc_go_generator

@ -0,0 +1,51 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef NET_GRPC_COMPILER_GO_GENERATOR_H_
#define NET_GRPC_COMPILER_GO_GENERATOR_H_
#include <string>
namespace google {
namespace protobuf {
class FileDescriptor;
} // namespace protobuf
} // namespace google
namespace grpc_go_generator {
std::string GetServices(const google::protobuf::FileDescriptor* file);
} // namespace grpc_go_generator
#endif // NET_GRPC_COMPILER_GO_GENERATOR_H_

@ -0,0 +1,83 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
// Generates go gRPC service interface out of Protobuf IDL.
//
// This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto
// and net/proto2/compiler/public/plugin.h for more information on plugins.
#include <fstream>
#include <memory>
using namespace std;
#include "src/compiler/go_generator.h"
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/plugin.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/descriptor.h>
class GoGrpcGenerator : public google::protobuf::compiler::CodeGenerator {
public:
GoGrpcGenerator() {}
virtual ~GoGrpcGenerator() {}
virtual bool Generate(const google::protobuf::FileDescriptor* file,
const string& parameter,
google::protobuf::compiler::GeneratorContext* context,
string* error) const {
// Get output file name.
string file_name;
if (file->name().size() > 6 &&
file->name().find_last_of(".proto") == file->name().size() - 1) {
file_name =
file->name().substr(0, file->name().size() - 6) + "_grpc.pb.go";
} else {
*error = "Invalid proto file name. Proto file must end with .proto";
return false;
}
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
context->Open(file_name));
google::protobuf::io::CodedOutputStream coded_out(output.get());
string code = grpc_go_generator::GetServices(file);
coded_out.WriteRaw(code.data(), code.size());
return true;
}
};
int main(int argc, char* argv[]) {
GoGrpcGenerator generator;
return google::protobuf::compiler::PluginMain(argc, argv, &generator);
}

@ -67,9 +67,8 @@ void PrintMethod(const MethodDescriptor* method, const string& package,
output_type = "stream(" + output_type + ")";
}
map<string, string> method_vars = ListToDict({
"mth.name", method->name(),
"input.type", input_type,
"output.type", output_type,
"mth.name", method->name(), "input.type", input_type, "output.type",
output_type,
});
out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n");
}
@ -105,8 +104,7 @@ void PrintService(const ServiceDescriptor* service, const string& package,
out->Print("self.marshal_class_method = :encode\n");
out->Print("self.unmarshal_class_method = :decode\n");
map<string, string> pkg_vars = ListToDict({
"service.name", service->name(),
"pkg.name", package,
"service.name", service->name(), "pkg.name", package,
});
out->Print(pkg_vars, "self.service_name = '$pkg.name$.$service.name$'\n");
out->Print("\n");
@ -139,9 +137,8 @@ string GetServices(const FileDescriptor* file) {
// Write out a file header.
map<string, string> header_comment_vars = ListToDict({
"file.name", file->name(),
"file.package", file->package(),
});
"file.name", file->name(), "file.package", file->package(),
});
out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
out.Print(header_comment_vars,
"# Source: $file.name$ for package '$file.package$'\n");

@ -47,8 +47,9 @@ inline bool ServicesFilename(const google::protobuf::FileDescriptor* file,
static const unsigned proto_suffix_length = 6; // length of ".proto"
if (file->name().size() > proto_suffix_length &&
file->name().find_last_of(".proto") == file->name().size() - 1) {
*file_name_or_error = file->name().substr(
0, file->name().size() - proto_suffix_length) + "_services.rb";
*file_name_or_error =
file->name().substr(0, file->name().size() - proto_suffix_length) +
"_services.rb";
return true;
} else {
*file_name_or_error = "Invalid proto file name: must end with .proto";
@ -56,7 +57,8 @@ inline bool ServicesFilename(const google::protobuf::FileDescriptor* file,
}
}
inline string MessagesRequireName(const google::protobuf::FileDescriptor* file) {
inline string MessagesRequireName(
const google::protobuf::FileDescriptor* file) {
return Replace(file->name(), ".proto", "");
}

@ -40,7 +40,6 @@
#include <string>
#include <vector>
using std::initializer_list;
using std::map;
using std::vector;
@ -51,11 +50,12 @@ namespace grpc_ruby_generator {
// into a map of key* to value*. Is merely a readability helper for later code.
inline map<string, string> ListToDict(const initializer_list<string>& values) {
if (values.size() % 2 != 0) {
// MOE: insert std::cerr << "Not every 'key' has a value in `values`." << std::endl;
// MOE: insert std::cerr << "Not every 'key' has a value in `values`."
// << std::endl;
}
map<string, string> value_map;
auto value_iter = values.begin();
for (unsigned i = 0; i < values.size()/2; ++i) {
for (unsigned i = 0; i < values.size() / 2; ++i) {
string key = *value_iter;
++value_iter;
string value = *value_iter;

@ -45,7 +45,7 @@ using std::transform;
namespace grpc_ruby_generator {
// Split splits a string using char into elems.
inline vector<string> &Split(const string &s, char delim,
inline vector<string>& Split(const string& s, char delim,
vector<string>* elems) {
stringstream ss(s);
string item;
@ -56,7 +56,7 @@ inline vector<string> &Split(const string &s, char delim,
}
// Split splits a string using char, returning the result in a vector.
inline vector<string> Split(const string &s, char delim) {
inline vector<string> Split(const string& s, char delim) {
vector<string> elems;
Split(s, delim, &elems);
return elems;
@ -106,7 +106,7 @@ inline string CapitalizeFirst(string s) {
inline string RubyTypeOf(const string& a_type, const string& package) {
string res(a_type);
ReplacePrefix(&res, package, ""); // remove the leading package if present
ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
ReplacePrefix(&res, ".", ""); // remove the leading . (no package)
if (res.find('.') == string::npos) {
return res;
} else {

@ -51,4 +51,4 @@ void grpc_channel_args_destroy(grpc_channel_args *a);
is specified in channel args, otherwise returns 0. */
int grpc_channel_args_is_census_enabled(const grpc_channel_args *a);
#endif /* __GRPC_INTERNAL_CHANNEL_CHANNEL_ARGS_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_CHANNEL_ARGS_H__ */

@ -54,7 +54,7 @@
/* Given a size, round up to the next multiple of sizeof(void*) */
#define ROUND_UP_TO_ALIGNMENT_SIZE(x) \
(((x)+GPR_MAX_ALIGNMENT - 1) & ~(GPR_MAX_ALIGNMENT - 1))
(((x) + GPR_MAX_ALIGNMENT - 1) & ~(GPR_MAX_ALIGNMENT - 1))
size_t grpc_channel_stack_size(const grpc_channel_filter **filters,
size_t filter_count) {
@ -190,14 +190,13 @@ void grpc_channel_next_op(grpc_channel_element *elem, grpc_channel_op *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)));
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)));
return (grpc_call_stack *)((char *)(elem)-ROUND_UP_TO_ALIGNMENT_SIZE(
sizeof(grpc_call_stack)));
}
static void do_nothing(void *user_data, grpc_op_error error) {}

@ -302,4 +302,4 @@ void grpc_call_element_send_cancel(grpc_call_element *cur_elem);
} while (0)
#endif
#endif /* __GRPC_INTERNAL_CHANNEL_CHANNEL_STACK_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_CHANNEL_STACK_H__ */

@ -59,4 +59,4 @@ grpc_transport_setup_result grpc_client_channel_transport_setup_complete(
grpc_channel_filter const **channel_filters, size_t num_channel_filters,
grpc_mdctx *mdctx);
#endif /* __GRPC_INTERNAL_CHANNEL_CLIENT_CHANNEL_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_CLIENT_CHANNEL_H__ */

@ -64,4 +64,4 @@ gpr_timespec grpc_client_setup_request_deadline(grpc_client_setup_request *r);
grpc_mdctx *grpc_client_setup_get_mdctx(grpc_client_setup_request *r);
#endif /* __GRPC_INTERNAL_CHANNEL_CLIENT_SETUP_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_CLIENT_SETUP_H__ */

@ -69,7 +69,7 @@ typedef struct {
/* 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
cache line requests */
#define TRANSPORT_STREAM_FROM_CALL_DATA(calld) ((grpc_stream *)((calld)+1))
#define TRANSPORT_STREAM_FROM_CALL_DATA(calld) ((grpc_stream *)((calld) + 1))
#define CALL_DATA_FROM_TRANSPORT_STREAM(transport_stream) \
(((call_data *)(transport_stream)) - 1)
@ -257,9 +257,9 @@ static void destroy_channel_elem(grpc_channel_element *elem) {
}
const grpc_channel_filter grpc_connected_channel_filter = {
call_op, channel_op,
call_op, channel_op,
sizeof(call_data), init_call_elem, destroy_call_elem,
sizeof(call_data), init_call_elem, destroy_call_elem,
sizeof(channel_data), init_channel_elem, destroy_channel_elem,
@ -289,12 +289,8 @@ static void accept_stream(void *user_data, grpc_transport *transport,
}
static void recv_error(channel_data *chand, call_data *calld, int line,
const char *fmt, ...) {
va_list a;
va_start(a, fmt);
gpr_vlog(__FILE__, line, GPR_LOG_SEVERITY_ERROR, fmt, a);
va_end(a);
const char *message) {
gpr_log_message(__FILE__, line, GPR_LOG_SEVERITY_ERROR, message);
if (chand->transport) {
grpc_transport_abort_stream(chand->transport,
@ -388,19 +384,23 @@ static void recv_batch(void *user_data, grpc_transport *transport,
case GRPC_OP_BEGIN_MESSAGE:
/* can't begin a message when we're still reading a message */
if (calld->reading_message) {
recv_error(chand, calld, __LINE__,
"Message terminated early; read %d bytes, expected %d",
calld->incoming_message.length,
calld->incoming_message_length);
char message[128];
sprintf(message,
"Message terminated early; read %d bytes, expected %d",
(int)calld->incoming_message.length,
(int)calld->incoming_message_length);
recv_error(chand, calld, __LINE__, message);
return;
}
/* stash away parameters, and prepare for incoming slices */
length = stream_op->data.begin_message.length;
if (length > calld->max_message_length) {
recv_error(
chand, calld, __LINE__,
char message[128];
sprintf(
message,
"Maximum message length of %d exceeded by a message of length %d",
calld->max_message_length, length);
recv_error(chand, calld, __LINE__, message);
} else if (length > 0) {
calld->reading_message = 1;
calld->incoming_message_length = length;
@ -423,10 +423,12 @@ static void recv_batch(void *user_data, grpc_transport *transport,
gpr_slice_buffer_add(&calld->incoming_message, stream_op->data.slice);
if (calld->incoming_message.length > calld->incoming_message_length) {
/* if we got too many bytes, complain */
recv_error(chand, calld, __LINE__,
"Receiving message overflow; read %d bytes, expected %d",
calld->incoming_message.length,
calld->incoming_message_length);
char message[128];
sprintf(message,
"Receiving message overflow; read %d bytes, expected %d",
(int)calld->incoming_message.length,
(int)calld->incoming_message_length);
recv_error(chand, calld, __LINE__, message);
return;
} else if (calld->incoming_message.length ==
calld->incoming_message_length) {
@ -439,10 +441,11 @@ static void recv_batch(void *user_data, grpc_transport *transport,
final_state == GRPC_STREAM_CLOSED)) {
calld->got_read_close = 1;
if (calld->reading_message) {
recv_error(chand, calld, __LINE__,
"Last message truncated; read %d bytes, expected %d",
calld->incoming_message.length,
calld->incoming_message_length);
char message[128];
sprintf(message, "Last message truncated; read %d bytes, expected %d",
(int)calld->incoming_message.length,
(int)calld->incoming_message_length);
recv_error(chand, calld, __LINE__, message);
}
call_op.type = GRPC_RECV_HALF_CLOSE;
call_op.dir = GRPC_CALL_UP;

@ -46,4 +46,4 @@ extern const grpc_channel_filter grpc_connected_channel_filter;
grpc_transport_setup_result grpc_connected_channel_bind_transport(
grpc_channel_stack *channel_stack, grpc_transport *transport);
#endif /* __GRPC_INTERNAL_CHANNEL_CONNECTED_CHANNEL_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_CONNECTED_CHANNEL_H__ */

@ -32,13 +32,26 @@
*/
#include "src/core/channel/http_server_filter.h"
#include <string.h>
#include <grpc/support/log.h>
typedef struct call_data { int sent_status; } call_data;
typedef struct call_data {
int sent_status;
int seen_scheme;
int seen_method;
int seen_te_trailers;
} call_data;
typedef struct channel_data {
grpc_mdelem *te_trailers;
grpc_mdelem *status_md;
grpc_mdelem *method;
grpc_mdelem *http_scheme;
grpc_mdelem *https_scheme;
/* TODO(klempner): Remove this once we stop using it */
grpc_mdelem *grpc_scheme;
grpc_mdelem *content_type;
grpc_mdelem *status;
} channel_data;
/* used to silence 'variable not used' warnings */
@ -56,20 +69,54 @@ static void call_op(grpc_call_element *elem, grpc_call_element *from_elem,
channel_data *channeld = elem->channel_data;
GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
ignore_unused(calld);
ignore_unused(channeld);
switch (op->type) {
case GRPC_RECV_METADATA:
/* check if it's a te: trailers header */
if (op->data.metadata == channeld->te_trailers) {
/* Check if it is one of the headers we care about. */
if (op->data.metadata == channeld->te_trailers ||
op->data.metadata == channeld->method ||
op->data.metadata == channeld->http_scheme ||
op->data.metadata == channeld->https_scheme ||
op->data.metadata == channeld->grpc_scheme ||
op->data.metadata == channeld->content_type) {
/* swallow it */
if (op->data.metadata == channeld->method) {
calld->seen_method = 1;
} else if (op->data.metadata->key == channeld->http_scheme->key) {
calld->seen_scheme = 1;
} else if (op->data.metadata == channeld->te_trailers) {
calld->seen_te_trailers = 1;
}
/* TODO(klempner): Track that we've seen all the headers we should
require */
grpc_mdelem_unref(op->data.metadata);
op->done_cb(op->user_data, GRPC_OP_OK);
} else if (op->data.metadata->key == channeld->te_trailers->key) {
gpr_log(GPR_ERROR, "Invalid te: header: '%s'",
} else if (op->data.metadata->key == channeld->content_type->key) {
if (strncmp(grpc_mdstr_as_c_string(op->data.metadata->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);
}
grpc_mdelem_unref(op->data.metadata);
op->done_cb(op->user_data, GRPC_OP_OK);
} else if (op->data.metadata->key == channeld->te_trailers->key ||
op->data.metadata->key == channeld->method->key ||
op->data.metadata->key == channeld->http_scheme->key ||
op->data.metadata->key == channeld->content_type->key) {
gpr_log(GPR_ERROR, "Invalid %s: header: '%s'",
grpc_mdstr_as_c_string(op->data.metadata->key),
grpc_mdstr_as_c_string(op->data.metadata->value));
/* swallow it */
/* swallow it and error everything out. */
/* TODO(klempner): We ought to generate more descriptive error messages
on the wire here. */
grpc_mdelem_unref(op->data.metadata);
op->done_cb(op->user_data, GRPC_OP_OK);
grpc_call_element_send_cancel(elem);
@ -78,14 +125,33 @@ static void call_op(grpc_call_element *elem, grpc_call_element *from_elem,
grpc_call_next_op(elem, op);
}
break;
case GRPC_RECV_END_OF_INITIAL_METADATA:
/* 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_method && calld->seen_scheme && calld->seen_te_trailers) {
grpc_call_next_op(elem, op);
} else {
if (!calld->seen_method) {
gpr_log(GPR_ERROR, "Missing :method header");
} else if (!calld->seen_scheme) {
gpr_log(GPR_ERROR, "Missing :scheme header");
} else if (!calld->seen_te_trailers) {
gpr_log(GPR_ERROR, "Missing te trailers header");
}
/* Error this call out */
op->done_cb(op->user_data, GRPC_OP_OK);
grpc_call_element_send_cancel(elem);
}
break;
case GRPC_SEND_START:
case GRPC_SEND_METADATA:
/* If we haven't sent status 200 yet, we need to so so because it needs to
come before any non : prefixed metadata. */
if (!calld->sent_status) {
calld->sent_status = 1;
/* status_md is reffed by grpc_call_element_send_metadata */
grpc_call_element_send_metadata(elem, channeld->status_md);
/* status is reffed by grpc_call_element_send_metadata */
grpc_call_element_send_metadata(elem, channeld->status);
}
grpc_call_next_op(elem, op);
break;
@ -124,6 +190,9 @@ static void init_call_elem(grpc_call_element *elem,
/* initialize members */
calld->sent_status = 0;
calld->seen_scheme = 0;
calld->seen_method = 0;
calld->seen_te_trailers = 0;
}
/* Destructor for call_data */
@ -151,7 +220,13 @@ static void init_channel_elem(grpc_channel_element *elem,
/* initialize members */
channeld->te_trailers = grpc_mdelem_from_strings(mdctx, "te", "trailers");
channeld->status_md = grpc_mdelem_from_strings(mdctx, ":status", "200");
channeld->status = grpc_mdelem_from_strings(mdctx, ":status", "200");
channeld->method = 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->content_type =
grpc_mdelem_from_strings(mdctx, "content-type", "application/grpc");
}
/* Destructor for channel data */
@ -160,7 +235,12 @@ static void destroy_channel_elem(grpc_channel_element *elem) {
channel_data *channeld = elem->channel_data;
grpc_mdelem_unref(channeld->te_trailers);
grpc_mdelem_unref(channeld->status_md);
grpc_mdelem_unref(channeld->status);
grpc_mdelem_unref(channeld->method);
grpc_mdelem_unref(channeld->http_scheme);
grpc_mdelem_unref(channeld->https_scheme);
grpc_mdelem_unref(channeld->grpc_scheme);
grpc_mdelem_unref(channeld->content_type);
}
const grpc_channel_filter grpc_http_server_filter = {

@ -61,7 +61,7 @@ struct grpc_metadata_buffer_impl {
size_t elem_cap;
};
#define ELEMS(buffer) ((qelem *)((buffer)+1))
#define ELEMS(buffer) ((qelem *)((buffer) + 1))
void grpc_metadata_buffer_init(grpc_metadata_buffer *buffer) {
/* start buffer as NULL, indicating no elements */

@ -67,4 +67,4 @@ grpc_metadata *grpc_metadata_buffer_extract_elements(
grpc_metadata_buffer *buffer);
void grpc_metadata_buffer_cleanup_elements(void *elements, grpc_op_error error);
#endif /* __GRPC_INTERNAL_CHANNEL_METADATA_BUFFER_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_METADATA_BUFFER_H__ */

@ -131,9 +131,9 @@ static void destroy_channel_elem(grpc_channel_element *elem) {
}
const grpc_channel_filter grpc_no_op_filter = {
call_op, channel_op,
call_op, channel_op,
sizeof(call_data), init_call_elem, destroy_call_elem,
sizeof(call_data), init_call_elem, destroy_call_elem,
sizeof(channel_data), init_channel_elem, destroy_channel_elem,

@ -41,4 +41,4 @@
customize for their own filters */
extern const grpc_channel_filter grpc_no_op_filter;
#endif /* __GRPC_INTERNAL_CHANNEL_NOOP_FILTER_H__ */
#endif /* __GRPC_INTERNAL_CHANNEL_NOOP_FILTER_H__ */

@ -46,4 +46,4 @@ typedef enum {
const char *grpc_compression_algorithm_name(
grpc_compression_algorithm algorithm);
#endif /* __GRPC_INTERNAL_COMPRESSION_ALGORITHM_H__ */
#endif /* __GRPC_INTERNAL_COMPRESSION_ALGORITHM_H__ */

@ -49,4 +49,4 @@ int grpc_msg_compress(grpc_compression_algorithm algorithm,
int grpc_msg_decompress(grpc_compression_algorithm algorithm,
gpr_slice_buffer *input, gpr_slice_buffer *output);
#endif /* __GRPC_INTERNAL_COMPRESSION_MESSAGE_COMPRESS_H__ */
#endif /* __GRPC_INTERNAL_COMPRESSION_MESSAGE_COMPRESS_H__ */

@ -42,4 +42,4 @@ gpr_slice grpc_httpcli_format_post_request(const grpc_httpcli_request *request,
const char *body_bytes,
size_t body_size);
#endif /* __GRPC_INTERNAL_HTTPCLI_FORMAT_REQUEST_H__ */
#endif /* __GRPC_INTERNAL_HTTPCLI_FORMAT_REQUEST_H__ */

@ -115,4 +115,4 @@ typedef int (*grpc_httpcli_post_override)(const grpc_httpcli_request *request,
void grpc_httpcli_set_override(grpc_httpcli_get_override get,
grpc_httpcli_post_override post);
#endif /* __GRPC_INTERNAL_HTTPCLI_HTTPCLI_H__ */
#endif /* __GRPC_INTERNAL_HTTPCLI_HTTPCLI_H__ */

@ -40,4 +40,4 @@ grpc_security_status grpc_httpcli_ssl_channel_security_context_create(
const unsigned char *pem_root_certs, size_t pem_root_certs_size,
const char *secure_peer_name, grpc_channel_security_context **ctx);
#endif /* __GRPC_INTERNAL_HTTPCLI_HTTPCLI_SECURITY_CONTEXT_H__ */
#endif /* __GRPC_INTERNAL_HTTPCLI_HTTPCLI_SECURITY_CONTEXT_H__ */

@ -61,4 +61,4 @@ 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);
#endif /* __GRPC_INTERNAL_HTTPCLI_PARSER_H__ */
#endif /* __GRPC_INTERNAL_HTTPCLI_PARSER_H__ */

@ -35,6 +35,7 @@
#define __GRPC_INTERNAL_IOMGR_POLLSET_H_
#include <grpc/support/port_platform.h>
#include <grpc/support/time.h>
/* A grpc_pollset is a set of file descriptors that a higher level item is
interested in. For example:

@ -153,3 +153,31 @@ int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
errno = save_errno;
return ret;
}
int grpc_sockaddr_get_port(const struct sockaddr *addr) {
switch (addr->sa_family) {
case AF_INET:
return ntohs(((struct sockaddr_in *)addr)->sin_port);
case AF_INET6:
return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
default:
gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
__FUNCTION__);
return 0;
}
}
int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
switch (addr->sa_family) {
case AF_INET:
((struct sockaddr_in *)addr)->sin_port = htons(port);
return 1;
case AF_INET6:
((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
return 1;
default:
gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
__FUNCTION__);
return 0;
}
}

@ -57,6 +57,12 @@ int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out);
void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
struct sockaddr_in6 *wild6_out);
/* Return the IP port number of a sockaddr */
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);
/* Converts a sockaddr into a newly-allocated human-readable string.
Currently, only the AF_INET and AF_INET6 families are recognized.

@ -52,7 +52,8 @@ grpc_tcp_server *grpc_tcp_server_create();
void grpc_tcp_server_start(grpc_tcp_server *server, grpc_pollset *pollset,
grpc_tcp_server_cb cb, void *cb_arg);
/* Add a port to the server, returning true on success, or false otherwise.
/* Add a port to the server, returning port number on success, or negative
on failure.
The :: and 0.0.0.0 wildcard addresses are treated identically, accepting
both IPv4 and IPv6 connections, but :: is the preferred style. This usually
@ -60,6 +61,8 @@ void grpc_tcp_server_start(grpc_tcp_server *server, grpc_pollset *pollset,
but not dualstack sockets.
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 struct sockaddr *addr,
int addr_len);

@ -154,6 +154,9 @@ static int get_max_accept_queue_size() {
/* Prepare a recently-created socket for listening. */
static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) {
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
if (fd < 0) {
goto error;
}
@ -179,13 +182,18 @@ static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) {
goto error;
}
return 1;
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);
}
return 0;
return -1;
}
/* event manager callback when reads are ready */
@ -234,39 +242,64 @@ error:
static int add_socket_to_server(grpc_tcp_server *s, int fd,
const struct sockaddr *addr, int addr_len) {
server_port *sp;
int port;
if (!prepare_socket(fd, addr, addr_len)) {
return 0;
}
gpr_mu_lock(&s->mu);
GPR_ASSERT(!s->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);
port = prepare_socket(fd, addr, addr_len);
if (port >= 0) {
gpr_mu_lock(&s->mu);
GPR_ASSERT(!s->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);
GPR_ASSERT(sp->emfd);
gpr_mu_unlock(&s->mu);
}
sp = &s->ports[s->nports++];
sp->server = s;
sp->fd = fd;
sp->emfd = grpc_fd_create(fd);
GPR_ASSERT(sp->emfd);
gpr_mu_unlock(&s->mu);
return 1;
return port;
}
int grpc_tcp_server_add_port(grpc_tcp_server *s, const struct sockaddr *addr,
int addr_len) {
int ok = 0;
int allocated_port1 = -1;
int allocated_port2 = -1;
int i;
int fd;
grpc_dualstack_mode dsmode;
struct sockaddr_in6 addr6_v4mapped;
struct sockaddr_in wild4;
struct sockaddr_in6 wild6;
struct sockaddr_in addr4_copy;
struct sockaddr *allocated_addr = NULL;
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
int port;
/* 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_to_v4mapped(addr, &addr6_v4mapped)) {
addr = (const struct sockaddr *)&addr6_v4mapped;
addr_len = sizeof(addr6_v4mapped);
@ -280,12 +313,15 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const struct sockaddr *addr,
addr = (struct sockaddr *)&wild6;
addr_len = sizeof(wild6);
fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
ok |= add_socket_to_server(s, fd, addr, addr_len);
allocated_port1 = add_socket_to_server(s, fd, addr, addr_len);
if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
return ok;
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);
}
@ -299,8 +335,11 @@ int grpc_tcp_server_add_port(grpc_tcp_server *s, const struct sockaddr *addr,
addr = (struct sockaddr *)&addr4_copy;
addr_len = sizeof(addr4_copy);
}
ok |= add_socket_to_server(s, fd, addr, addr_len);
return ok;
allocated_port2 = add_socket_to_server(s, fd, addr, addr_len);
done:
gpr_free(allocated_addr);
return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
}
int grpc_tcp_server_get_fd(grpc_tcp_server *s, int index) {

@ -157,6 +157,5 @@ static void destroy_channel_elem(grpc_channel_element *elem) {
}
const grpc_channel_filter grpc_client_auth_filter = {
call_op, channel_op, sizeof(call_data),
init_call_elem, destroy_call_elem, sizeof(channel_data),
init_channel_elem, destroy_channel_elem, "auth"};
call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem,
sizeof(channel_data), init_channel_elem, destroy_channel_elem, "auth"};

@ -38,4 +38,4 @@
extern const grpc_channel_filter grpc_client_auth_filter;
#endif /* __GRPC_INTERNAL_SECURITY_AUTH_H__ */
#endif /* __GRPC_INTERNAL_SECURITY_AUTH_H__ */

@ -819,6 +819,26 @@ const grpc_credentials_array *grpc_composite_credentials_get_credentials(
return &c->inner;
}
grpc_credentials *grpc_credentials_contains_type(
grpc_credentials *creds, const char *type,
grpc_credentials **composite_creds) {
size_t i;
if (!strcmp(creds->type, type)) {
if (composite_creds != NULL) *composite_creds = NULL;
return creds;
} else if (!strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE)) {
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)) {
if (composite_creds != NULL) *composite_creds = creds;
return inner_creds_array->creds_array[i];
}
}
}
return NULL;
}
/* -- IAM credentials. -- */
typedef struct {
@ -877,4 +897,3 @@ grpc_credentials *grpc_iam_credentials_create(const char *token,
/* -- Default credentials TODO(jboeuf). -- */
grpc_credentials *grpc_default_credentials_create(void) { return NULL; }

@ -108,6 +108,14 @@ typedef struct {
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);
/* Exposed for testing only. */
grpc_credentials_status
grpc_oauth2_token_fetcher_credentials_parse_server_response(
@ -118,7 +126,6 @@ grpc_oauth2_token_fetcher_credentials_parse_server_response(
grpc_credentials *grpc_fake_oauth2_credentials_create(
const char *token_md_value, int is_async);
/* --- grpc_server_credentials. --- */
typedef struct {
@ -136,5 +143,4 @@ struct grpc_server_credentials {
const grpc_ssl_config *grpc_ssl_server_credentials_get_config(
const grpc_server_credentials *ssl_creds);
#endif /* __GRPC_INTERNAL_SECURITY_CREDENTIALS_H__ */
#endif /* __GRPC_INTERNAL_SECURITY_CREDENTIALS_H__ */

@ -0,0 +1,80 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <string.h>
#include "src/core/security/credentials.h"
#include "src/core/security/security_context.h"
#include "src/core/surface/lame_client.h"
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/useful.h>
grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
const char *target,
const grpc_channel_args *args) {
grpc_secure_channel_factory factories[] = {
{GRPC_CREDENTIALS_TYPE_SSL, grpc_ssl_channel_create},
{GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY,
grpc_fake_transport_security_channel_create}};
return grpc_secure_channel_create_with_factories(
factories, GPR_ARRAY_SIZE(factories), creds, target, args);
}
grpc_server *grpc_secure_server_create(grpc_server_credentials *creds,
grpc_completion_queue *cq,
const grpc_channel_args *args) {
grpc_security_status status = GRPC_SECURITY_ERROR;
grpc_security_context *ctx = NULL;
grpc_server *server = NULL;
if (creds == NULL) return NULL; /* TODO(ctiller): Return lame server. */
if (!strcmp(creds->type, GRPC_CREDENTIALS_TYPE_SSL)) {
status = grpc_ssl_server_security_context_create(
grpc_ssl_server_credentials_get_config(creds), &ctx);
} else if (!strcmp(creds->type,
GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY)) {
ctx = grpc_fake_server_security_context_create();
status = GRPC_SECURITY_OK;
}
if (status != GRPC_SECURITY_OK) {
gpr_log(GPR_ERROR,
"Unable to create secure server with credentials of type %s.",
creds->type);
return NULL; /* TODO(ctiller): Return lame server. */
}
server = grpc_secure_server_create_internal(cq, args, ctx);
grpc_security_context_unref(ctx);
return server;
}

@ -37,4 +37,4 @@
extern unsigned char grpc_google_root_certs[];
extern unsigned int grpc_google_root_certs_size;
#endif /* __GRPC_INTERNAL_SECURITY_GOOGLE_ROOT_CERTS_H__ */
#endif /* __GRPC_INTERNAL_SECURITY_GOOGLE_ROOT_CERTS_H__ */

@ -44,4 +44,4 @@ 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_ENDPOINT_SECURE_ENDPOINT_H__ */
#endif /* __GRPC_INTERNAL_ENDPOINT_SECURE_ENDPOINT_H__ */

@ -50,4 +50,4 @@ void grpc_setup_secure_transport(grpc_security_context *ctx,
grpc_secure_transport_setup_done_cb cb,
void *user_data);
#endif /* __GRPC_INTERNAL_SECURITY_SECURE_TRANSPORT_SETUP_H__ */
#endif /* __GRPC_INTERNAL_SECURITY_SECURE_TRANSPORT_SETUP_H__ */

@ -100,8 +100,7 @@ grpc_arg grpc_security_context_to_arg(grpc_security_context *ctx) {
return result;
}
grpc_security_context *grpc_security_context_from_arg(
const grpc_arg *arg) {
grpc_security_context *grpc_security_context_from_arg(const grpc_arg *arg) {
if (strcmp(arg->key, GRPC_SECURITY_CONTEXT_ARG)) return NULL;
if (arg->type != GRPC_ARG_POINTER) {
gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
@ -140,9 +139,7 @@ static void fake_channel_destroy(grpc_security_context *ctx) {
gpr_free(ctx);
}
static void fake_server_destroy(grpc_security_context *ctx) {
gpr_free(ctx);
}
static void fake_server_destroy(grpc_security_context *ctx) { gpr_free(ctx); }
static grpc_security_status fake_channel_create_handshaker(
grpc_security_context *ctx, tsi_handshaker **handshaker) {
@ -234,8 +231,7 @@ static void ssl_channel_destroy(grpc_security_context *ctx) {
}
static void ssl_server_destroy(grpc_security_context *ctx) {
grpc_ssl_server_security_context *c =
(grpc_ssl_server_security_context *)ctx;
grpc_ssl_server_security_context *c = (grpc_ssl_server_security_context *)ctx;
if (c->handshaker_factory != NULL) {
tsi_ssl_handshaker_factory_destroy(c->handshaker_factory);
}
@ -267,8 +263,7 @@ static grpc_security_status ssl_channel_create_handshaker(
static grpc_security_status ssl_server_create_handshaker(
grpc_security_context *ctx, tsi_handshaker **handshaker) {
grpc_ssl_server_security_context *c =
(grpc_ssl_server_security_context *)ctx;
grpc_ssl_server_security_context *c = (grpc_ssl_server_security_context *)ctx;
return ssl_create_handshaker(c->handshaker_factory, 0, NULL, handshaker);
}
@ -438,20 +433,19 @@ error:
return GRPC_SECURITY_ERROR;
}
/* -- High level objects. -- */
static grpc_channel *grpc_ssl_channel_create(grpc_credentials *creds,
const grpc_ssl_config *config,
const char *target,
const grpc_channel_args *args) {
grpc_channel *grpc_ssl_channel_create(grpc_credentials *ssl_creds,
grpc_credentials *request_metadata_creds,
const char *target,
const grpc_channel_args *args) {
grpc_channel_security_context *ctx = NULL;
grpc_channel *channel = NULL;
grpc_security_status status = GRPC_SECURITY_OK;
size_t i = 0;
const char *secure_peer_name = target;
for (i = 0; i < args->num_args; i++) {
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) &&
arg->type == GRPC_ARG_STRING) {
@ -459,8 +453,9 @@ static grpc_channel *grpc_ssl_channel_create(grpc_credentials *creds,
break;
}
}
status = grpc_ssl_channel_security_context_create(creds, config,
secure_peer_name, &ctx);
status = grpc_ssl_channel_security_context_create(
request_metadata_creds, grpc_ssl_credentials_get_config(ssl_creds),
secure_peer_name, &ctx);
if (status != GRPC_SECURITY_OK) {
return grpc_lame_client_channel_create();
}
@ -469,58 +464,47 @@ static grpc_channel *grpc_ssl_channel_create(grpc_credentials *creds,
return channel;
}
static grpc_credentials *get_creds_from_composite(
grpc_credentials *composite_creds, const char *type) {
size_t i;
const grpc_credentials_array *inner_creds_array =
grpc_composite_credentials_get_credentials(composite_creds);
for (i = 0; i < inner_creds_array->num_creds; i++) {
if (!strcmp(type, inner_creds_array->creds_array[i]->type)) {
return inner_creds_array->creds_array[i];
}
}
return NULL;
grpc_channel *grpc_fake_transport_security_channel_create(
grpc_credentials *fake_creds, grpc_credentials *request_metadata_creds,
const char *target, const grpc_channel_args *args) {
grpc_channel_security_context *ctx =
grpc_fake_channel_security_context_create(request_metadata_creds);
grpc_channel *channel =
grpc_secure_channel_create_internal(target, args, ctx);
grpc_security_context_unref(&ctx->base);
return channel;
}
static grpc_channel *grpc_channel_create_from_composite_creds(
grpc_credentials *composite_creds, const char *target,
grpc_channel *grpc_secure_channel_create_with_factories(
const grpc_secure_channel_factory *factories, size_t num_factories,
grpc_credentials *creds, const char *target,
const grpc_channel_args *args) {
grpc_credentials *creds =
get_creds_from_composite(composite_creds, GRPC_CREDENTIALS_TYPE_SSL);
if (creds != NULL) {
return grpc_ssl_channel_create(
composite_creds, grpc_ssl_credentials_get_config(creds), target, args);
size_t i;
if (creds == NULL) {
gpr_log(GPR_ERROR, "No credentials to create a secure channel.");
return grpc_lame_client_channel_create();
}
return NULL; /* TODO(ctiller): return lame channel. */
}
grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
const char *target,
const grpc_channel_args *args) {
if (grpc_credentials_has_request_metadata_only(creds)) {
gpr_log(GPR_ERROR,
"Credentials is insufficient to create a secure channel.");
return grpc_lame_client_channel_create();
}
if (!strcmp(creds->type, GRPC_CREDENTIALS_TYPE_SSL)) {
return grpc_ssl_channel_create(NULL, grpc_ssl_credentials_get_config(creds),
target, args);
} else if (!strcmp(creds->type,
GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY)) {
grpc_channel_security_context *ctx =
grpc_fake_channel_security_context_create(NULL);
grpc_channel *channel =
grpc_secure_channel_create_internal(target, args, ctx);
grpc_security_context_unref(&ctx->base);
return channel;
} else if (!strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE)) {
return grpc_channel_create_from_composite_creds(creds, target, args);
} else {
gpr_log(GPR_ERROR,
"Unknown credentials type %s for creating a secure channel.");
return grpc_lame_client_channel_create();
for (i = 0; i < num_factories; i++) {
grpc_credentials *composite_creds = NULL;
grpc_credentials *transport_security_creds = NULL;
transport_security_creds = grpc_credentials_contains_type(
creds, factories[i].creds_type, &composite_creds);
if (transport_security_creds != NULL) {
return factories[i].factory(transport_security_creds, composite_creds,
target, args);
}
}
gpr_log(GPR_ERROR,
"Unknown credentials type %s for creating a secure channel.",
creds->type);
return grpc_lame_client_channel_create();
}
grpc_channel *grpc_default_secure_channel_create(
@ -528,30 +512,3 @@ grpc_channel *grpc_default_secure_channel_create(
return grpc_secure_channel_create(grpc_default_credentials_create(), target,
args);
}
grpc_server *grpc_secure_server_create(grpc_server_credentials *creds,
grpc_completion_queue *cq,
const grpc_channel_args *args) {
grpc_security_status status = GRPC_SECURITY_ERROR;
grpc_security_context *ctx = NULL;
grpc_server *server = NULL;
if (creds == NULL) return NULL; /* TODO(ctiller): Return lame server. */
if (!strcmp(creds->type, GRPC_CREDENTIALS_TYPE_SSL)) {
status = grpc_ssl_server_security_context_create(
grpc_ssl_server_credentials_get_config(creds), &ctx);
} else if (!strcmp(creds->type,
GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY)) {
ctx = grpc_fake_server_security_context_create();
status = GRPC_SECURITY_OK;
} else {
gpr_log(GPR_ERROR,
"Unable to create secure server with credentials of type %s.",
creds->type);
}
if (status != GRPC_SECURITY_OK) {
return NULL; /* TODO(ctiller): Return lame server. */
}
server = grpc_secure_server_create_internal(cq, args, ctx);
grpc_security_context_unref(ctx);
return server;
}

@ -118,7 +118,7 @@ grpc_security_context *grpc_find_security_context_in_args(
typedef struct grpc_channel_security_context grpc_channel_security_context;
struct grpc_channel_security_context {
grpc_security_context base; /* requires is_client_side to be non 0. */
grpc_security_context base; /* requires is_client_side to be non 0. */
grpc_credentials *request_metadata_creds;
};
@ -159,17 +159,41 @@ grpc_security_status grpc_ssl_channel_security_context_create(
grpc_security_status grpc_ssl_server_security_context_create(
const grpc_ssl_config *config, grpc_security_context **ctx);
/* --- Creation of high level objects. --- */
/* Secure client channel creation. */
grpc_channel *grpc_ssl_channel_create(grpc_credentials *ssl_creds,
grpc_credentials *request_metadata_creds,
const char *target,
const grpc_channel_args *args);
grpc_channel *grpc_fake_transport_security_channel_create(
grpc_credentials *fake_creds, grpc_credentials *request_metadata_creds,
const char *target, const grpc_channel_args *args);
grpc_channel *grpc_secure_channel_create_internal(
const char *target, const grpc_channel_args *args,
grpc_channel_security_context *ctx);
typedef grpc_channel *(*grpc_secure_channel_factory_func)(
grpc_credentials *transport_security_creds,
grpc_credentials *request_metadata_creds, const char *target,
const grpc_channel_args *args);
typedef struct {
const char *creds_type;
grpc_secure_channel_factory_func factory;
} grpc_secure_channel_factory;
grpc_channel *grpc_secure_channel_create_with_factories(
const grpc_secure_channel_factory *factories, size_t num_factories,
grpc_credentials *creds, const char *target, const grpc_channel_args *args);
/* Secure server creation. */
grpc_server *grpc_secure_server_create_internal(
grpc_completion_queue *cq, const grpc_channel_args *args,
grpc_security_context *ctx);
#endif /* __GRPC_INTERNAL_SECURITY_SECURITY_CONTEXT_H__ */
grpc_server *grpc_secure_server_create_internal(grpc_completion_queue *cq,
const grpc_channel_args *args,
grpc_security_context *ctx);
#endif /* __GRPC_INTERNAL_SECURITY_SECURITY_CONTEXT_H__ */

@ -70,8 +70,7 @@ static void on_accept(void *server, grpc_endpoint *tcp) {
const grpc_channel_args *args = grpc_server_get_channel_args(server);
grpc_security_context *ctx = grpc_find_security_context_in_args(args);
GPR_ASSERT(ctx);
grpc_setup_secure_transport(ctx, tcp, on_secure_transport_setup_done,
server);
grpc_setup_secure_transport(ctx, tcp, on_secure_transport_setup_done, server);
}
/* Note: the following code is the same with server_chttp2.c */

@ -73,4 +73,4 @@ census_op_id census_tracing_start_op();
/* Ends tracing. Calling this function will invalidate the input op_id. */
void census_tracing_end_op(census_op_id op_id);
#endif /* __GRPC_INTERNAL_STATISTICS_CENSUS_INTERFACE_H__ */
#endif /* __GRPC_INTERNAL_STATISTICS_CENSUS_INTERFACE_H__ */

@ -98,4 +98,4 @@ void census_stats_store_shutdown();
}
#endif
#endif /* __GRPC_INTERNAL_STATISTICS_CENSUS_RPC_STATS_H__ */
#endif /* __GRPC_INTERNAL_STATISTICS_CENSUS_RPC_STATS_H__ */

@ -47,8 +47,8 @@
/* Struct for a trace annotation. */
typedef struct annotation {
gpr_uint64 ts; /* timestamp of the annotation */
char txt[CENSUS_MAX_ANNOTATION_LENGTH]; /* actual txt annotation */
gpr_timespec ts; /* timestamp of the annotation */
char txt[CENSUS_MAX_ANNOTATION_LENGTH + 1]; /* actual txt annotation */
struct annotation* next;
} annotation;
@ -107,8 +107,8 @@ census_op_id census_tracing_start_op() {
ret->rpc_stats.cnt = 1;
ret->ts = gpr_now();
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);
gpr_log(GPR_DEBUG, "Start tracing for id %lu\n", g_id);
return ret->id;
}
}
@ -127,7 +127,27 @@ int census_add_method_tag(census_op_id op_id, const char* method) {
return ret;
}
void census_tracing_print(census_op_id op_id, const char* annotation) {}
void census_tracing_print(census_op_id op_id, const char* anno_txt) {
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) {
annotation* anno = gpr_malloc(sizeof(annotation));
anno->ts = gpr_now();
{
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';
}
anno->next = trace->annotations;
trace->annotations = anno;
}
gpr_mu_unlock(&g_mu);
}
void census_tracing_end_op(census_op_id op_id) {
trace_obj* trace = NULL;
@ -136,7 +156,7 @@ void census_tracing_end_op(census_op_id op_id) {
if (trace != NULL) {
trace->rpc_stats.elapsed_time_ms =
gpr_timespec_to_micros(gpr_time_sub(gpr_now(), trace->ts));
gpr_log(GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us\n",
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));

@ -141,10 +141,10 @@ static gpr_int32 find_bucket_idx(const census_ht* ht, census_ht_key key) {
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;
if (opt->key_type == CENSUS_HT_POINTER)
return !opt->compare_keys((p->key).ptr, key.ptr);
return 0;
return !opt->compare_keys((p->key).ptr, key.ptr);
}
static entry_locator ht_find(const census_ht* ht, census_ht_key key) {

@ -62,6 +62,4 @@ void *gpr_malloc_aligned(size_t size, size_t alignment) {
return (void *)ret;
}
void gpr_free_aligned(void *ptr) {
free(((void **)ptr)[-1]);
}
void gpr_free_aligned(void *ptr) { free(((void **)ptr)[-1]); }

@ -46,4 +46,4 @@ int gpr_cpu_num_cores();
[0, gpr_cpu_num_cores() - 1] */
int gpr_cpu_current_cpu();
#endif /* __GRPC_INTERNAL_SUPPORT_CPU_H__ */
#endif /* __GRPC_INTERNAL_SUPPORT_CPU_H__ */

@ -37,13 +37,37 @@
#include "src/core/support/cpu.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#define GRPC_GNU_SOURCE
#endif
#ifndef __USE_GNU
#define __USE_GNU
#define GRPC_USE_GNU
#endif
#ifndef __USE_MISC
#define __USE_MISC
#define GRPC_USE_MISC
#endif
#include <sched.h>
#ifdef GRPC_GNU_SOURCE
#undef _GNU_SOURCE
#undef GRPC_GNU_SOURCE
#endif
#ifdef GRPC_USE_GNU
#undef __USE_GNU
#undef GRPC_USE_GNU
#endif
#ifdef GRPC_USE_MISC
#undef __USE_MISC
#undef GRPC_USE_MISC
#endif
#include <errno.h>
#include <unistd.h>

@ -34,6 +34,10 @@
#include <grpc/support/log.h>
#include <stdio.h>
#include <string.h>
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) {
@ -47,12 +51,15 @@ const char *gpr_log_severity_string(gpr_log_severity severity) {
return "UNKNOWN";
}
void gpr_log(const char *file, int line, gpr_log_severity severity,
const char *format, ...) {
va_list args;
va_start(args, format);
gpr_vlog(file, line, severity, format, args);
va_end(args);
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));
lfargs.file = file;
lfargs.line = line;
lfargs.severity = severity;
lfargs.message = message;
g_log_func(&lfargs);
}
void gpr_set_log_function(gpr_log_func f) { g_log_func = f; }

@ -54,25 +54,31 @@ static android_LogPriority severity_to_log_priority(gpr_log_severity severity) {
return ANDROID_LOG_DEFAULT;
}
void gpr_vlog(const char *file, int line, gpr_log_severity severity,
const char *format, va_list args) {
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);
}
void gpr_default_log(gpr_log_func_args *args) {
char *final_slash;
const char *display_file;
char *prefix = NULL;
char *suffix = NULL;
char *output = NULL;
final_slash = strrchr(file, '/');
final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = file;
else
display_file = final_slash + 1;
asprintf(&prefix, "%s:%d] ", display_file, line);
vasprintf(&suffix, format, args);
asprintf(&output, "%s%s", prefix, suffix);
asprintf(&prefix, "%s:%d] %s", display_file, args->line, args->message);
__android_log_write(severity_to_log_priority(severity), "GRPC", output);
__android_log_write(severity_to_log_priority(args->severity), "GRPC", output);
/* allocated by asprintf => use free, not gpr_free */
free(prefix);

@ -49,17 +49,30 @@
static long gettid() { return syscall(__NR_gettid); }
void gpr_vlog(const char *file, int line, gpr_log_severity severity,
const char *format, va_list args) {
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);
}
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();
struct tm tm;
final_slash = strrchr(file, '/');
final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = file;
display_file = args->file;
else
display_file = final_slash + 1;
@ -70,12 +83,10 @@ void gpr_vlog(const char *file, int line, gpr_log_severity severity,
strcpy(time_buffer, "error:strftime");
}
flockfile(stderr);
fprintf(stderr, "%s%s.%09d %7ld %s:%d] ", gpr_log_severity_string(severity),
time_buffer, (int)(now.tv_nsec), gettid(), display_file, line);
vfprintf(stderr, format, args);
fputc('\n', stderr);
funlockfile(stderr);
fprintf(stderr, "%s%s.%09d %7ld %s:%d] %s\n",
gpr_log_severity_string(args->severity), time_buffer,
(int)(now.tv_nsec), gettid(), display_file, args->line,
args->message);
}
#endif

@ -47,17 +47,40 @@
static long gettid() { return pthread_self(); }
void gpr_vlog(const char *file, int line, gpr_log_severity severity,
const char *format, va_list args) {
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, format, args);
va_end(args);
if (ret < 0) {
message = NULL;
} else if (ret <= sizeof(buf) - 1) {
message = buf;
} else {
message = allocated = gpr_malloc(ret + 1);
va_start(args, format);
vsnprintf(message, format, args);
va_end(args);
}
gpr_log_message(file, line, severity, message);
gpr_free(allocated);
}
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();
struct tm tm;
final_slash = strrchr(file, '/');
final_slash = strrchr(args->file, '/');
if (final_slash == NULL)
display_file = file;
display_file = args->file;
else
display_file = final_slash + 1;
@ -68,12 +91,10 @@ void gpr_vlog(const char *file, int line, gpr_log_severity severity,
strcpy(time_buffer, "error:strftime");
}
flockfile(stderr);
fprintf(stderr, "%s%s.%09d %7ld %s:%d] ", gpr_log_severity_string(severity),
time_buffer, (int)(now.tv_nsec), gettid(), display_file, line);
vfprintf(stderr, format, args);
fputc('\n', stderr);
funlockfile(stderr);
fprintf(stderr, "%s%s.%09d %7ld %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) */

@ -39,12 +39,42 @@
#include <stdio.h>
#include <stdarg.h>
void gpr_log(const char *file, int line, gpr_log_severity severity,
const char *message) {
const char *message = NULL;
va_list args;
int ret;
/* Determine the length. */
va_start(args, format);
ret = _vscprintf(format, args);
va_end(args);
if (!(0 <= ret && ret < ~(size_t)0)) {
message = NULL;
} else {
/* Allocate a new buffer, with space for the NUL terminator. */
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 (ret != strp_buflen - 1) {
/* This should never happen. */
gpr_free(message);
message = NULL;
}
}
gpr_log_message(file, line, severity, message);
gpr_free(message);
}
/* Simple starter implementation */
void gpr_vlog(const char *file, int line, gpr_log_severity severity,
const char *format, va_list args) {
fprintf(stderr, "%s %s:%d: ", gpr_log_severity_string(severity), file, line);
vfprintf(stderr, format, args);
fputc('\n', stderr);
void gpr_default_log(gpr_log_func_args *args) {
fprintf(stderr, "%s %s:%d: %s\n", gpr_log_severity_string(severity),
args->file, args->line, args->message);
}
#endif

@ -41,4 +41,4 @@
/* compute the hash of key (length len) */
gpr_uint32 gpr_murmur_hash3(const void *key, size_t len, gpr_uint32 seed);
#endif /* __GRPC_INTERNAL_SUPPORT_MURMUR_HASH_H__ */
#endif /* __GRPC_INTERNAL_SUPPORT_MURMUR_HASH_H__ */

@ -36,4 +36,4 @@
/* Internal interfaces between modules within the gpr support library. */
#endif /* __GRPC_INTERNAL_SUPPORT_THD_INTERNAL_H__ */
#endif /* __GRPC_INTERNAL_SUPPORT_THD_INTERNAL_H__ */

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save