slice.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <node.h>
  34. #include <nan.h>
  35. #include <grpc/slice.h>
  36. #include <grpc/support/alloc.h>
  37. #include "slice.h"
  38. #include "byte_buffer.h"
  39. namespace grpc {
  40. namespace node {
  41. using Nan::Persistent;
  42. using v8::Local;
  43. using v8::String;
  44. using v8::Value;
  45. namespace {
  46. void SliceFreeCallback(char *data, void *hint) {
  47. grpc_slice *slice = reinterpret_cast<grpc_slice*>(hint);
  48. grpc_slice_unref(*slice);
  49. delete slice;
  50. }
  51. void string_destroy_func(void *user_data) {
  52. delete reinterpret_cast<Nan::Utf8String*>(user_data);
  53. }
  54. void buffer_destroy_func(void *user_data) {
  55. delete reinterpret_cast<PersistentValue*>(user_data);
  56. }
  57. } // namespace
  58. grpc_slice CreateSliceFromString(const Local<String> source) {
  59. Nan::HandleScope scope;
  60. Nan::Utf8String *utf8_value = new Nan::Utf8String(source);
  61. return grpc_slice_new_with_user_data(**utf8_value, source->Length(),
  62. string_destroy_func, utf8_value);
  63. }
  64. grpc_slice CreateSliceFromBuffer(const Local<Value> source) {
  65. // Prerequisite: ::node::Buffer::HasInstance(source)
  66. Nan::HandleScope scope;
  67. return grpc_slice_new_with_user_data(::node::Buffer::Data(source),
  68. ::node::Buffer::Length(source),
  69. buffer_destroy_func,
  70. new PersistentValue(source));
  71. }
  72. Local<String> CopyStringFromSlice(const grpc_slice slice) {
  73. Nan::EscapableHandleScope scope;
  74. if (GRPC_SLICE_LENGTH(slice) == 0) {
  75. return scope.Escape(Nan::EmptyString());
  76. }
  77. return scope.Escape(Nan::New<String>(
  78. const_cast<char *>(reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(slice))),
  79. GRPC_SLICE_LENGTH(slice)).ToLocalChecked());
  80. }
  81. Local<Value> CreateBufferFromSlice(const grpc_slice slice) {
  82. Nan::EscapableHandleScope scope;
  83. grpc_slice *slice_ptr = new grpc_slice;
  84. *slice_ptr = grpc_slice_ref(slice);
  85. return scope.Escape(MakeFastBuffer(Nan::NewBuffer(
  86. const_cast<char *>(reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(*slice_ptr))),
  87. GRPC_SLICE_LENGTH(*slice_ptr), SliceFreeCallback, slice_ptr).ToLocalChecked()));
  88. }
  89. } // namespace node
  90. } // namespace grpc