| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/iomgr/port.h" |
| 22 |
|
| 23 |
#ifdef GRPC_POSIX_SOCKET_SOCKET_FACTORY |
| 24 |
|
| 25 |
#include <grpc/impl/codegen/grpc_types.h> |
| 26 |
#include <grpc/support/sync.h> |
| 27 |
|
| 28 |
#include "src/core/lib/channel/channel_args.h" |
| 29 |
#include "src/core/lib/gpr/useful.h" |
| 30 |
#include "src/core/lib/iomgr/socket_factory_posix.h" |
| 31 |
|
| 32 |
void grpc_socket_factory_init(grpc_socket_factory* factory, |
| 33 |
const grpc_socket_factory_vtable* vtable) { |
| 34 |
factory->vtable = vtable; |
| 35 |
gpr_ref_init(&factory->refcount, 1); |
| 36 |
} |
| 37 |
|
| 38 |
int grpc_socket_factory_socket(grpc_socket_factory* factory, int domain, |
| 39 |
int type, int protocol) { |
| 40 |
return factory->vtable->socket(factory, domain, type, protocol); |
| 41 |
} |
| 42 |
|
| 43 |
int grpc_socket_factory_bind(grpc_socket_factory* factory, int sockfd, |
| 44 |
const grpc_resolved_address* addr) { |
| 45 |
return factory->vtable->bind(factory, sockfd, addr); |
| 46 |
} |
| 47 |
|
| 48 |
int grpc_socket_factory_compare(grpc_socket_factory* a, |
| 49 |
grpc_socket_factory* b) { |
| 50 |
int c = grpc_core::QsortCompare(a, b); |
| 51 |
if (c != 0) { |
| 52 |
grpc_socket_factory* sma = a; |
| 53 |
grpc_socket_factory* smb = b; |
| 54 |
c = grpc_core::QsortCompare(sma->vtable, smb->vtable); |
| 55 |
if (c == 0) { |
| 56 |
c = sma->vtable->compare(sma, smb); |
| 57 |
} |
| 58 |
} |
| 59 |
return c; |
| 60 |
} |
| 61 |
|
| 62 |
grpc_socket_factory* grpc_socket_factory_ref(grpc_socket_factory* factory) { |
| 63 |
gpr_ref(&factory->refcount); |
| 64 |
return factory; |
| 65 |
} |
| 66 |
|
| 67 |
void grpc_socket_factory_unref(grpc_socket_factory* factory) { |
| 68 |
if (gpr_unref(&factory->refcount)) { |
| 69 |
factory->vtable->destroy(factory); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static void* socket_factory_arg_copy(void* p) { |
| 74 |
return grpc_socket_factory_ref(static_cast<grpc_socket_factory*>(p)); |
| 75 |
} |
| 76 |
|
| 77 |
static void socket_factory_arg_destroy(void* p) { |
| 78 |
grpc_socket_factory_unref(static_cast<grpc_socket_factory*>(p)); |
| 79 |
} |
| 80 |
|
| 81 |
static int socket_factory_cmp(void* a, void* b) { |
| 82 |
return grpc_socket_factory_compare(static_cast<grpc_socket_factory*>(a), |
| 83 |
static_cast<grpc_socket_factory*>(b)); |
| 84 |
} |
| 85 |
|
| 86 |
static const grpc_arg_pointer_vtable socket_factory_arg_vtable = { |
| 87 |
socket_factory_arg_copy, socket_factory_arg_destroy, socket_factory_cmp}; |
| 88 |
|
| 89 |
grpc_arg grpc_socket_factory_to_arg(grpc_socket_factory* factory) { |
| 90 |
return grpc_channel_arg_pointer_create( |
| 91 |
const_cast<char*>(GRPC_ARG_SOCKET_FACTORY), factory, |
| 92 |
&socket_factory_arg_vtable); |
| 93 |
} |
| 94 |
|
| 95 |
#endif |