Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/lib/iomgr/socket_mutator.h" |
| 35 | |
| 36 | #include <grpc/impl/codegen/grpc_types.h> |
| 37 | #include <grpc/support/sync.h> |
| 38 | #include <grpc/support/useful.h> |
| 39 | |
| 40 | void grpc_socket_mutator_init(grpc_socket_mutator *mutator, |
| 41 | const grpc_socket_mutator_vtable *vtable) { |
| 42 | mutator->vtable = vtable; |
| 43 | gpr_ref_init(&mutator->refcount, 1); |
| 44 | } |
| 45 | |
| 46 | grpc_socket_mutator *grpc_socket_mutator_ref(grpc_socket_mutator *mutator) { |
| 47 | gpr_ref(&mutator->refcount); |
| 48 | return mutator; |
| 49 | } |
| 50 | |
| 51 | bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd) { |
| 52 | return mutator->vtable->mutate_fd(fd, mutator); |
| 53 | } |
| 54 | |
Yuchen Zeng | 7810898 | 2016-11-08 14:28:03 -0800 | [diff] [blame] | 55 | int grpc_socket_mutator_compare(grpc_socket_mutator *a, |
| 56 | grpc_socket_mutator *b) { |
| 57 | int c = GPR_ICMP(a, b); |
| 58 | if (c != 0) { |
| 59 | grpc_socket_mutator *sma = a; |
| 60 | grpc_socket_mutator *smb = b; |
| 61 | c = GPR_ICMP(sma->vtable, smb->vtable); |
| 62 | if (c == 0) { |
| 63 | c = sma->vtable->compare(sma, smb); |
| 64 | } |
| 65 | } |
| 66 | return c; |
| 67 | } |
| 68 | |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 69 | void grpc_socket_mutator_unref(grpc_socket_mutator *mutator) { |
| 70 | if (gpr_unref(&mutator->refcount)) { |
| 71 | mutator->vtable->destory(mutator); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void *socket_mutator_arg_copy(void *p) { |
| 76 | return grpc_socket_mutator_ref(p); |
| 77 | } |
| 78 | |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 79 | static void socket_mutator_arg_destroy(grpc_exec_ctx *exec_ctx, void *p) { |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 80 | grpc_socket_mutator_unref(p); |
| 81 | } |
| 82 | |
Yuchen Zeng | 7810898 | 2016-11-08 14:28:03 -0800 | [diff] [blame] | 83 | static int socket_mutator_cmp(void *a, void *b) { |
| 84 | return grpc_socket_mutator_compare((grpc_socket_mutator *)a, |
| 85 | (grpc_socket_mutator *)b); |
| 86 | } |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 87 | |
| 88 | static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = { |
| 89 | socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp}; |
| 90 | |
| 91 | grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator) { |
| 92 | grpc_arg arg; |
| 93 | arg.type = GRPC_ARG_POINTER; |
| 94 | arg.key = GRPC_ARG_SOCKET_MUTATOR; |
| 95 | arg.value.pointer.vtable = &socket_mutator_arg_vtable; |
| 96 | arg.value.pointer.p = mutator; |
| 97 | return arg; |
| 98 | } |