Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [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 |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [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. |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "src/core/lib/iomgr/socket_mutator.h" |
| 20 | |
Mark D. Roth | 8d5e60b | 2017-06-09 09:08:23 -0700 | [diff] [blame] | 21 | #include "src/core/lib/channel/channel_args.h" |
| 22 | |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 23 | #include <grpc/impl/codegen/grpc_types.h> |
| 24 | #include <grpc/support/sync.h> |
| 25 | #include <grpc/support/useful.h> |
| 26 | |
| 27 | void grpc_socket_mutator_init(grpc_socket_mutator *mutator, |
| 28 | const grpc_socket_mutator_vtable *vtable) { |
| 29 | mutator->vtable = vtable; |
| 30 | gpr_ref_init(&mutator->refcount, 1); |
| 31 | } |
| 32 | |
| 33 | grpc_socket_mutator *grpc_socket_mutator_ref(grpc_socket_mutator *mutator) { |
| 34 | gpr_ref(&mutator->refcount); |
| 35 | return mutator; |
| 36 | } |
| 37 | |
| 38 | bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd) { |
| 39 | return mutator->vtable->mutate_fd(fd, mutator); |
| 40 | } |
| 41 | |
Yuchen Zeng | 7810898 | 2016-11-08 14:28:03 -0800 | [diff] [blame] | 42 | int grpc_socket_mutator_compare(grpc_socket_mutator *a, |
| 43 | grpc_socket_mutator *b) { |
| 44 | int c = GPR_ICMP(a, b); |
| 45 | if (c != 0) { |
| 46 | grpc_socket_mutator *sma = a; |
| 47 | grpc_socket_mutator *smb = b; |
| 48 | c = GPR_ICMP(sma->vtable, smb->vtable); |
| 49 | if (c == 0) { |
| 50 | c = sma->vtable->compare(sma, smb); |
| 51 | } |
| 52 | } |
| 53 | return c; |
| 54 | } |
| 55 | |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 56 | void grpc_socket_mutator_unref(grpc_socket_mutator *mutator) { |
| 57 | if (gpr_unref(&mutator->refcount)) { |
| 58 | mutator->vtable->destory(mutator); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static void *socket_mutator_arg_copy(void *p) { |
Yash Tibrewal | 52778c4 | 2017-09-11 15:00:11 -0700 | [diff] [blame] | 63 | return grpc_socket_mutator_ref((grpc_socket_mutator *)p); |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 66 | static void socket_mutator_arg_destroy(grpc_exec_ctx *exec_ctx, void *p) { |
Yash Tibrewal | 52778c4 | 2017-09-11 15:00:11 -0700 | [diff] [blame] | 67 | grpc_socket_mutator_unref((grpc_socket_mutator *)p); |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Yuchen Zeng | 7810898 | 2016-11-08 14:28:03 -0800 | [diff] [blame] | 70 | static int socket_mutator_cmp(void *a, void *b) { |
| 71 | return grpc_socket_mutator_compare((grpc_socket_mutator *)a, |
| 72 | (grpc_socket_mutator *)b); |
| 73 | } |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 74 | |
| 75 | static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = { |
| 76 | socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp}; |
| 77 | |
| 78 | grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator) { |
Yash Tibrewal | 533d118 | 2017-09-18 10:48:22 -0700 | [diff] [blame^] | 79 | return grpc_channel_arg_pointer_create((char *)GRPC_ARG_SOCKET_MUTATOR, |
| 80 | mutator, &socket_mutator_arg_vtable); |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 81 | } |