Port port_posix to port_windows.

pull/1232/head
Craig Tiller 10 years ago
parent b8fce7f628
commit f7262057e6
  1. 19
      test/core/util/port_windows.c

@ -35,14 +35,13 @@
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
#if defined(GPR_WINSOCK_SOCKET) && defined(GRPC_TEST_PICK_PORT) #if defined(GPR_WINSOCK_SOCKET) && defined(GRPC_TEST_PICK_PORT)
#include "src/core/iomgr/sockaddr_utils.h"
#include "test/core/util/port.h" #include "test/core/util/port.h"
#include <netinet/in.h> #include <process.h>
#include <sys/socket.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
@ -50,7 +49,7 @@
static int is_port_available(int *port, int is_tcp) { static int is_port_available(int *port, int is_tcp) {
const int proto = is_tcp ? IPPROTO_TCP : 0; const int proto = is_tcp ? IPPROTO_TCP : 0;
const int fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto); const SOCKET fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto);
int one = 1; int one = 1;
struct sockaddr_in addr; struct sockaddr_in addr;
socklen_t alen = sizeof(addr); socklen_t alen = sizeof(addr);
@ -64,9 +63,9 @@ static int is_port_available(int *port, int is_tcp) {
} }
/* Reuseaddr lets us start up a server immediately after it exits */ /* Reuseaddr lets us start up a server immediately after it exits */
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one)) < 0) {
gpr_log(GPR_ERROR, "setsockopt() failed: %s", strerror(errno)); gpr_log(GPR_ERROR, "setsockopt() failed: %s", strerror(errno));
close(fd); closesocket(fd);
return 0; return 0;
} }
@ -76,14 +75,14 @@ static int is_port_available(int *port, int is_tcp) {
addr.sin_port = htons(*port); addr.sin_port = htons(*port);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
gpr_log(GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror(errno)); gpr_log(GPR_DEBUG, "bind(port=%d) failed: %s", *port, strerror(errno));
close(fd); closesocket(fd);
return 0; return 0;
} }
/* Get the bound port number */ /* Get the bound port number */
if (getsockname(fd, (struct sockaddr *)&addr, &alen) < 0) { if (getsockname(fd, (struct sockaddr *)&addr, &alen) < 0) {
gpr_log(GPR_ERROR, "getsockname() failed: %s", strerror(errno)); gpr_log(GPR_ERROR, "getsockname() failed: %s", strerror(errno));
close(fd); closesocket(fd);
return 0; return 0;
} }
GPR_ASSERT(alen <= sizeof(addr)); GPR_ASSERT(alen <= sizeof(addr));
@ -95,7 +94,7 @@ static int is_port_available(int *port, int is_tcp) {
GPR_ASSERT(*port == actual_port); GPR_ASSERT(*port == actual_port);
} }
close(fd); closesocket(fd);
return 1; return 1;
} }
@ -120,7 +119,7 @@ int grpc_pick_unused_port(void) {
int port; int port;
try++; try++;
if (try == 1) { if (try == 1) {
port = getpid() % (65536 - 30000) + 30000; port = _getpid() % (65536 - 30000) + 30000;
} else if (try <= NUM_RANDOM_PORTS_TO_PICK) { } else if (try <= NUM_RANDOM_PORTS_TO_PICK) {
port = rand() % (65536 - 30000) + 30000; port = rand() % (65536 - 30000) + 30000;
} else { } else {

Loading…
Cancel
Save