Add grpc_socket_mutator_compare

pull/8335/head
Yuchen Zeng 8 years ago
parent fdfe41fb03
commit 7810898658
  1. 19
      src/core/lib/iomgr/socket_mutator.c
  2. 5
      src/core/lib/iomgr/socket_mutator.h
  3. 12
      test/core/iomgr/socket_utils_test.c
  4. 9
      test/cpp/common/channel_arguments_test.cc

@ -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};

@ -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);

@ -45,6 +45,7 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
#include <grpc/support/useful.h>
#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;

@ -34,6 +34,7 @@
#include <grpc++/support/channel_arguments.h>
#include <grpc/grpc.h>
#include <grpc/support/useful.h>
#include <gtest/gtest.h>
#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

Loading…
Cancel
Save