Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2017 gRPC authors. |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "src/core/lib/iomgr/port.h" |
| 20 | |
| 21 | #ifdef GRPC_POSIX_SOCKET |
| 22 | |
Mark D. Roth | 8d5e60b | 2017-06-09 09:08:23 -0700 | [diff] [blame] | 23 | #include "src/core/lib/channel/channel_args.h" |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 24 | #include "src/core/lib/iomgr/socket_factory_posix.h" |
| 25 | |
| 26 | #include <grpc/impl/codegen/grpc_types.h> |
| 27 | #include <grpc/support/sync.h> |
| 28 | #include <grpc/support/useful.h> |
| 29 | |
| 30 | void grpc_socket_factory_init(grpc_socket_factory *factory, |
| 31 | const grpc_socket_factory_vtable *vtable) { |
| 32 | factory->vtable = vtable; |
| 33 | gpr_ref_init(&factory->refcount, 1); |
| 34 | } |
| 35 | |
| 36 | int grpc_socket_factory_socket(grpc_socket_factory *factory, int domain, |
| 37 | int type, int protocol) { |
| 38 | return factory->vtable->socket(factory, domain, type, protocol); |
| 39 | } |
| 40 | |
| 41 | int grpc_socket_factory_bind(grpc_socket_factory *factory, int sockfd, |
| 42 | const grpc_resolved_address *addr) { |
| 43 | return factory->vtable->bind(factory, sockfd, addr); |
| 44 | } |
| 45 | |
| 46 | int grpc_socket_factory_compare(grpc_socket_factory *a, |
| 47 | grpc_socket_factory *b) { |
| 48 | int c = GPR_ICMP(a, b); |
| 49 | if (c != 0) { |
| 50 | grpc_socket_factory *sma = a; |
| 51 | grpc_socket_factory *smb = b; |
| 52 | c = GPR_ICMP(sma->vtable, smb->vtable); |
| 53 | if (c == 0) { |
| 54 | c = sma->vtable->compare(sma, smb); |
| 55 | } |
| 56 | } |
| 57 | return c; |
| 58 | } |
| 59 | |
| 60 | grpc_socket_factory *grpc_socket_factory_ref(grpc_socket_factory *factory) { |
| 61 | gpr_ref(&factory->refcount); |
| 62 | return factory; |
| 63 | } |
| 64 | |
| 65 | void grpc_socket_factory_unref(grpc_socket_factory *factory) { |
| 66 | if (gpr_unref(&factory->refcount)) { |
| 67 | factory->vtable->destroy(factory); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void *socket_factory_arg_copy(void *p) { |
Yash Tibrewal | 52778c4 | 2017-09-11 15:00:11 -0700 | [diff] [blame] | 72 | return grpc_socket_factory_ref((grpc_socket_factory *)p); |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void socket_factory_arg_destroy(grpc_exec_ctx *exec_ctx, void *p) { |
Yash Tibrewal | 52778c4 | 2017-09-11 15:00:11 -0700 | [diff] [blame] | 76 | grpc_socket_factory_unref((grpc_socket_factory *)p); |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int socket_factory_cmp(void *a, void *b) { |
| 80 | return grpc_socket_factory_compare((grpc_socket_factory *)a, |
| 81 | (grpc_socket_factory *)b); |
| 82 | } |
| 83 | |
| 84 | static const grpc_arg_pointer_vtable socket_factory_arg_vtable = { |
| 85 | socket_factory_arg_copy, socket_factory_arg_destroy, socket_factory_cmp}; |
| 86 | |
| 87 | grpc_arg grpc_socket_factory_to_arg(grpc_socket_factory *factory) { |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame^] | 88 | return grpc_channel_arg_pointer_create((char *)GRPC_ARG_SOCKET_FACTORY, |
| 89 | factory, &socket_factory_arg_vtable); |
Michael Warres | c46b3eb | 2017-02-22 22:57:04 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #endif |