blob: b0435d5a076b36cae9f8b242dde5844379600fac [file] [log] [blame]
Yuchen Zengde3daf52016-10-13 17:26:26 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Yuchen Zengde3daf52016-10-13 17:26:26 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Zengde3daf52016-10-13 17:26:26 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yuchen Zengde3daf52016-10-13 17:26:26 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Zengde3daf52016-10-13 17:26:26 -070016 *
17 */
18
19#include "src/core/lib/iomgr/socket_mutator.h"
20
Mark D. Roth8d5e60b2017-06-09 09:08:23 -070021#include "src/core/lib/channel/channel_args.h"
22
Yuchen Zengde3daf52016-10-13 17:26:26 -070023#include <grpc/impl/codegen/grpc_types.h>
24#include <grpc/support/sync.h>
25#include <grpc/support/useful.h>
26
27void 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
33grpc_socket_mutator *grpc_socket_mutator_ref(grpc_socket_mutator *mutator) {
34 gpr_ref(&mutator->refcount);
35 return mutator;
36}
37
38bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd) {
39 return mutator->vtable->mutate_fd(fd, mutator);
40}
41
Yuchen Zeng78108982016-11-08 14:28:03 -080042int 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 Zengde3daf52016-10-13 17:26:26 -070056void grpc_socket_mutator_unref(grpc_socket_mutator *mutator) {
57 if (gpr_unref(&mutator->refcount)) {
58 mutator->vtable->destory(mutator);
59 }
60}
61
62static void *socket_mutator_arg_copy(void *p) {
Yash Tibrewal52778c42017-09-11 15:00:11 -070063 return grpc_socket_mutator_ref((grpc_socket_mutator *)p);
Yuchen Zengde3daf52016-10-13 17:26:26 -070064}
65
Craig Tillerc5866662016-11-16 15:25:00 -080066static void socket_mutator_arg_destroy(grpc_exec_ctx *exec_ctx, void *p) {
Yash Tibrewal52778c42017-09-11 15:00:11 -070067 grpc_socket_mutator_unref((grpc_socket_mutator *)p);
Yuchen Zengde3daf52016-10-13 17:26:26 -070068}
69
Yuchen Zeng78108982016-11-08 14:28:03 -080070static 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 Zengde3daf52016-10-13 17:26:26 -070074
75static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = {
76 socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp};
77
78grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator) {
Yash Tibrewal533d1182017-09-18 10:48:22 -070079 return grpc_channel_arg_pointer_create((char *)GRPC_ARG_SOCKET_MUTATOR,
80 mutator, &socket_mutator_arg_vtable);
Yuchen Zengde3daf52016-10-13 17:26:26 -070081}