3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/iomgr/port.h"
23 #ifdef GRPC_POSIX_SOCKET_SOCKET_FACTORY
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gpr/useful.h"
27 #include "src/core/lib/iomgr/socket_factory_posix.h"
29 #include <grpc/impl/codegen/grpc_types.h>
30 #include <grpc/support/sync.h>
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);
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);
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);
48 int grpc_socket_factory_compare(grpc_socket_factory* a,
49 grpc_socket_factory* b) {
50 int c = GPR_ICMP(a, b);
52 grpc_socket_factory* sma = a;
53 grpc_socket_factory* smb = b;
54 c = GPR_ICMP(sma->vtable, smb->vtable);
56 c = sma->vtable->compare(sma, smb);
62 grpc_socket_factory* grpc_socket_factory_ref(grpc_socket_factory* factory) {
63 gpr_ref(&factory->refcount);
67 void grpc_socket_factory_unref(grpc_socket_factory* factory) {
68 if (gpr_unref(&factory->refcount)) {
69 factory->vtable->destroy(factory);
73 static void* socket_factory_arg_copy(void* p) {
74 return grpc_socket_factory_ref(static_cast<grpc_socket_factory*>(p));
77 static void socket_factory_arg_destroy(void* p) {
78 grpc_socket_factory_unref(static_cast<grpc_socket_factory*>(p));
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));
86 static const grpc_arg_pointer_vtable socket_factory_arg_vtable = {
87 socket_factory_arg_copy, socket_factory_arg_destroy, socket_factory_cmp};
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);