diff --git a/src/core/lib/iomgr/socket_mutator.c b/src/core/lib/iomgr/socket_mutator.c index 3f66cb100da..8b1efb6bab0 100644 --- a/src/core/lib/iomgr/socket_mutator.c +++ b/src/core/lib/iomgr/socket_mutator.c @@ -52,6 +52,20 @@ bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd) { return mutator->vtable->mutate_fd(fd, mutator); } +int grpc_socket_mutator_compare(grpc_socket_mutator *a, + grpc_socket_mutator *b) { + int c = GPR_ICMP(a, b); + if (c != 0) { + grpc_socket_mutator *sma = a; + grpc_socket_mutator *smb = b; + c = GPR_ICMP(sma->vtable, smb->vtable); + if (c == 0) { + c = sma->vtable->compare(sma, smb); + } + } + return c; +} + void grpc_socket_mutator_unref(grpc_socket_mutator *mutator) { if (gpr_unref(&mutator->refcount)) { mutator->vtable->destory(mutator); @@ -66,7 +80,10 @@ static void socket_mutator_arg_destroy(void *p) { grpc_socket_mutator_unref(p); } -static int socket_mutator_cmp(void *a, void *b) { return GPR_ICMP(a, b); } +static int socket_mutator_cmp(void *a, void *b) { + return grpc_socket_mutator_compare((grpc_socket_mutator *)a, + (grpc_socket_mutator *)b); +} static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = { socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp}; diff --git a/src/core/lib/iomgr/socket_mutator.h b/src/core/lib/iomgr/socket_mutator.h index 5200f52d3a1..2f5b6c248e9 100644 --- a/src/core/lib/iomgr/socket_mutator.h +++ b/src/core/lib/iomgr/socket_mutator.h @@ -45,6 +45,8 @@ extern "C" { typedef struct { /** Mutates the socket opitons of \a fd */ bool (*mutate_fd)(int fd, grpc_socket_mutator *mutator); + /** Compare socket mutator \a a and \a b */ + int (*compare)(grpc_socket_mutator *a, grpc_socket_mutator *b); /** Destroys the socket mutator instance */ void (*destory)(grpc_socket_mutator *mutator); } grpc_socket_mutator_vtable; @@ -65,6 +67,9 @@ grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator); /** Perform the file descriptor mutation operation of \a mutator on \a fd */ bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd); +/** Compare if \a a and \a b are the same mutator or have same settings */ +int grpc_socket_mutator_compare(grpc_socket_mutator *a, grpc_socket_mutator *b); + grpc_socket_mutator *grpc_socket_mutator_ref(grpc_socket_mutator *mutator); void grpc_socket_mutator_unref(grpc_socket_mutator *mutator); diff --git a/test/core/iomgr/socket_utils_test.c b/test/core/iomgr/socket_utils_test.c index b3363b2be2e..7eee2d1d102 100644 --- a/test/core/iomgr/socket_utils_test.c +++ b/test/core/iomgr/socket_utils_test.c @@ -45,6 +45,7 @@ #include #include #include +#include #include "src/core/lib/iomgr/socket_mutator.h" #include "test/core/util/test_config.h" @@ -76,8 +77,15 @@ static void destroy_test_mutator(grpc_socket_mutator *mutator) { gpr_free(m); } -static const grpc_socket_mutator_vtable mutator_vtable = {mutate_fd, - destroy_test_mutator}; +static int compare_test_mutator(grpc_socket_mutator *a, + grpc_socket_mutator *b) { + struct test_socket_mutator *ma = (struct test_socket_mutator *)a; + struct test_socket_mutator *mb = (struct test_socket_mutator *)b; + return GPR_ICMP(ma->option_value, mb->option_value); +} + +static const grpc_socket_mutator_vtable mutator_vtable = { + mutate_fd, compare_test_mutator, destroy_test_mutator}; int main(int argc, char **argv) { int sock; diff --git a/test/cpp/common/channel_arguments_test.cc b/test/cpp/common/channel_arguments_test.cc index e048108f0f5..c2cf350b37d 100644 --- a/test/cpp/common/channel_arguments_test.cc +++ b/test/cpp/common/channel_arguments_test.cc @@ -34,6 +34,7 @@ #include #include +#include #include #include "src/core/lib/iomgr/socket_mutator.h" @@ -62,13 +63,17 @@ bool test_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) { return tsm->MutateFd(fd); } +int test_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) { + return GPR_ICMP(a, b); +} + void test_mutator_destroy(grpc_socket_mutator* mutator) { TestSocketMutator* tsm = (TestSocketMutator*)mutator; delete tsm; } -grpc_socket_mutator_vtable test_mutator_vtable = {test_mutator_mutate_fd, - test_mutator_destroy}; +grpc_socket_mutator_vtable test_mutator_vtable = { + test_mutator_mutate_fd, test_mutator_compare, test_mutator_destroy}; // // TestSocketMutator implementation