From 5c30f1cca06a8b42ded32f122784eba44dd50f95 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Wed, 25 Apr 2018 14:16:37 -0700 Subject: [PATCH] Tweaks to existing components for CFStream --- src/core/lib/iomgr/error_apple.h | 31 ++++++++++++ src/core/lib/iomgr/error_apple.mm | 49 +++++++++++++++++++ src/core/lib/slice/slice_buffer.cc | 18 +++++-- src/core/lib/transport/transport.cc | 2 +- .../GRPCClient/private/GRPCCompletionQueue.m | 12 ++++- 5 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 src/core/lib/iomgr/error_apple.h create mode 100644 src/core/lib/iomgr/error_apple.mm diff --git a/src/core/lib/iomgr/error_apple.h b/src/core/lib/iomgr/error_apple.h new file mode 100644 index 00000000000..f266192239a --- /dev/null +++ b/src/core/lib/iomgr/error_apple.h @@ -0,0 +1,31 @@ +/* + * + * Copyright 2018 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GRPC_CORE_LIB_IOMGR_ERROR_APPLE_H +#define GRPC_CORE_LIB_IOMGR_ERROR_APPLE_H + +#ifdef GPR_APPLE +// Create an error from Apple Core Foundation CFError object +#define GRPC_ERROR_CREATE_FROM_CFERROR(error, desc) \ + grpc_error_create_from_cferror(__FILE__, __LINE__, \ + static_cast((error)), (desc)) +grpc_error* grpc_error_create_from_cferror(const char* file, int line, + void* arg, const char* desc); +#endif + +#endif /* GRPC_CORE_LIB_IOMGR_ERROR_APPLE_H */ diff --git a/src/core/lib/iomgr/error_apple.mm b/src/core/lib/iomgr/error_apple.mm new file mode 100644 index 00000000000..f39108a5cb6 --- /dev/null +++ b/src/core/lib/iomgr/error_apple.mm @@ -0,0 +1,49 @@ +/* + * + * Copyright 2018 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include + +#ifdef GPR_APPLE +#import + +#include +#include + +#include "src/core/lib/iomgr/error.h" + +#define MAX_ERROR_DESCRIPTION 256 + +grpc_error* grpc_error_create_from_cferror(const char* file, int line, void* arg, + const char* custom_desc) { + CFErrorRef error = static_cast(arg); + char buf_domain[MAX_ERROR_DESCRIPTION], buf_desc[MAX_ERROR_DESCRIPTION]; + char* error_msg; + CFErrorDomain domain = CFErrorGetDomain((error)); + CFIndex code = CFErrorGetCode((error)); + CFStringRef desc = CFErrorCopyDescription((error)); + CFStringGetCString(domain, buf_domain, MAX_ERROR_DESCRIPTION, kCFStringEncodingUTF8); + CFStringGetCString(desc, buf_desc, MAX_ERROR_DESCRIPTION, kCFStringEncodingUTF8); + gpr_asprintf(&error_msg, "%s (error domain:%s, code:%ld, description:%s)", custom_desc, + buf_domain, code, buf_desc); + CFRelease(desc); + grpc_error* return_error = + grpc_error_create(file, line, grpc_slice_from_copied_string(error_msg), NULL, 0); + gpr_free(error_msg); + return return_error; +} +#endif // GPR_APPLE diff --git a/src/core/lib/slice/slice_buffer.cc b/src/core/lib/slice/slice_buffer.cc index fd56997388c..1f1c08b1594 100644 --- a/src/core/lib/slice/slice_buffer.cc +++ b/src/core/lib/slice/slice_buffer.cc @@ -333,14 +333,26 @@ void grpc_slice_buffer_trim_end(grpc_slice_buffer* sb, size_t n, size_t slice_len = GRPC_SLICE_LENGTH(slice); if (slice_len > n) { sb->slices[idx] = grpc_slice_split_head(&slice, slice_len - n); - grpc_slice_buffer_add_indexed(garbage, slice); + if (garbage) { + grpc_slice_buffer_add_indexed(garbage, slice); + } else { + grpc_slice_unref_internal(slice); + } return; } else if (slice_len == n) { - grpc_slice_buffer_add_indexed(garbage, slice); + if (garbage) { + grpc_slice_buffer_add_indexed(garbage, slice); + } else { + grpc_slice_unref_internal(slice); + } sb->count = idx; return; } else { - grpc_slice_buffer_add_indexed(garbage, slice); + if (garbage) { + grpc_slice_buffer_add_indexed(garbage, slice); + } else { + grpc_slice_unref_internal(slice); + } n -= slice_len; sb->count = idx; } diff --git a/src/core/lib/transport/transport.cc b/src/core/lib/transport/transport.cc index 6b41e4b37e5..d34affb0456 100644 --- a/src/core/lib/transport/transport.cc +++ b/src/core/lib/transport/transport.cc @@ -184,7 +184,7 @@ void grpc_transport_set_pops(grpc_transport* transport, grpc_stream* stream, nullptr) { transport->vtable->set_pollset_set(transport, stream, pollset_set); } else { - abort(); + // No-op for empty pollset } } diff --git a/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m b/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m index 57dbde8d040..7d14a1c9786 100644 --- a/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m +++ b/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m @@ -20,6 +20,14 @@ #import +#ifdef GRPC_CFSTREAM +const grpc_completion_queue_attributes kCompletionQueueAttr = {GRPC_CQ_CURRENT_VERSION, + GRPC_CQ_NEXT, GRPC_CQ_NON_POLLING}; +#else +const grpc_completion_queue_attributes kCompletionQueueAttr = { + GRPC_CQ_CURRENT_VERSION, GRPC_CQ_NEXT, GRPC_CQ_DEFAULT_POLLING}; +#endif + @implementation GRPCCompletionQueue + (instancetype)completionQueue { @@ -33,7 +41,9 @@ - (instancetype)init { if ((self = [super init])) { - _unmanagedQueue = grpc_completion_queue_create_for_next(NULL); + _unmanagedQueue = grpc_completion_queue_create( + grpc_completion_queue_factory_lookup(&kCompletionQueueAttr), &kCompletionQueueAttr, NULL); + //_unmanagedQueue = grpc_completion_queue_create_for_next(NULL); // This is for the following block to capture the pointer by value (instead // of retaining self and doing self->_unmanagedQueue). This is essential