Skip to content

Commit

Permalink
Various Windows fixes.
Browse files Browse the repository at this point in the history
-) using dupenv_s instead of getenv_s and calling strdup ourselves.
-) few impossible-to-obtain if checks.
-) various signed/unsigned casting.
-) using time_t instead of time32_t
-) checking output of FormatMessage for failures.
-) don't redefine _WIN32_WINNT without undefining it first.
-) fixed msvc's interlocked casting.
-) renamed AddPort to AddListeningPort.
-) added protobuf's third_party includes to search path.
-) added a missing definition for inet_ntop in mingw32.
-) removed useless declarations.
  • Loading branch information
Nicolas Noble authored and nicolasnoble committed Mar 19, 2015
1 parent 6cf02ed commit cfd6073
Show file tree
Hide file tree
Showing 33 changed files with 88 additions and 64 deletions.
2 changes: 1 addition & 1 deletion examples/pubsub/publisher_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class PublisherTest : public ::testing::Test {
int port = grpc_pick_unused_port_or_die();
server_address_ << "localhost:" << port;
ServerBuilder builder;
builder.AddPort(server_address_.str(), grpc::InsecureServerCredentials());
builder.AddListeningPort(server_address_.str(), grpc::InsecureServerCredentials());
builder.RegisterService(&service_);
server_ = builder.BuildAndStart();

Expand Down
2 changes: 1 addition & 1 deletion examples/pubsub/subscriber_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class SubscriberTest : public ::testing::Test {
int port = grpc_pick_unused_port_or_die();
server_address_ << "localhost:" << port;
ServerBuilder builder;
builder.AddPort(server_address_.str(), grpc::InsecureServerCredentials());
builder.AddListeningPort(server_address_.str(), grpc::InsecureServerCredentials());
builder.RegisterService(&service_);
server_ = builder.BuildAndStart();

Expand Down
2 changes: 1 addition & 1 deletion include/grpc++/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Server GRPC_FINAL : private CallHook,
bool RegisterAsyncService(AsynchronousService* service);
void RegisterAsyncGenericService(AsyncGenericService* service);
// Add a listening port. Can be called multiple times.
int AddPort(const grpc::string& addr, ServerCredentials* creds);
int AddListeningPort(const grpc::string& addr, ServerCredentials* creds);
// Start the server.
bool Start();

Expand Down
6 changes: 3 additions & 3 deletions include/grpc++/server_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class ServerBuilder {
void RegisterAsyncGenericService(AsyncGenericService* service);

// Add a listening port. Can be called multiple times.
void AddPort(const grpc::string& addr,
std::shared_ptr<ServerCredentials> creds,
int* selected_port = nullptr);
void AddListeningPort(const grpc::string& addr,
std::shared_ptr<ServerCredentials> creds,
int* selected_port = nullptr);

// Set the thread pool used for running appliation rpc handlers.
// Does not take ownership.
Expand Down
26 changes: 18 additions & 8 deletions include/grpc/support/atm_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,31 @@ static __inline int gpr_atm_no_barrier_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
/* InterlockedCompareExchangePointerNoFence() not available on vista or
windows7 */
#ifdef GPR_ARCH_64
return o == (gpr_atm)InterlockedCompareExchangeAcquire64(p, n, o);
return o == (gpr_atm)InterlockedCompareExchangeAcquire64((volatile LONGLONG *) p,
(LONGLONG) n, (LONGLONG) o);
#else
return o == (gpr_atm)InterlockedCompareExchangeAcquire(p, n, o);
return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG *) p,
(LONG) n, (LONG) o);
#endif
}

static __inline int gpr_atm_acq_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
#ifdef GPR_ARCH_64
return o == (gpr_atm)InterlockedCompareExchangeAcquire64(p, n, o);
return o == (gpr_atm)InterlockedCompareExchangeAcquire64((volatile LONGLONG) p,
(LONGLONG) n, (LONGLONG) o);
#else
return o == (gpr_atm)InterlockedCompareExchangeAcquire(p, n, o);
return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG *) p,
(LONG) n, (LONG) o);
#endif
}

static __inline int gpr_atm_rel_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
#ifdef GPR_ARCH_64
return o == (gpr_atm)InterlockedCompareExchangeRelease64(p, n, o);
return o == (gpr_atm)InterlockedCompareExchangeRelease64((volatile LONGLONG *) p,
(LONGLONG) n, (LONGLONG) o);
#else
return o == (gpr_atm)InterlockedCompareExchangeRelease(p, n, o);
return o == (gpr_atm)InterlockedCompareExchangeRelease((volatile LONG *) p,
(LONG) n, (LONG) o);
#endif
}

Expand All @@ -101,11 +107,15 @@ static __inline gpr_atm gpr_atm_full_fetch_add(gpr_atm *p, gpr_atm delta) {
#ifdef GPR_ARCH_64
do {
old = *p;
} while (old != (gpr_atm)InterlockedCompareExchange64(p, old + delta, old));
} while (old != (gpr_atm)InterlockedCompareExchange64((volatile LONGLONG *) p,
(LONGLONG) old + delta,
(LONGLONG) old));
#else
do {
old = *p;
} while (old != (gpr_atm)InterlockedCompareExchange(p, old + delta, old));
} while (old != (gpr_atm)InterlockedCompareExchange((volatile LONG *) p,
(LONG) old + delta,
(LONG) old));
#endif
return old;
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/iomgr/sockaddr_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
#include <winsock2.h>
#include <mswsock.h>

#ifdef __MINGW32__
/* mingw seems to be missing that definition. */
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
#endif

#endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKADDR_WIN32_H */
3 changes: 0 additions & 3 deletions src/core/iomgr/tcp_server_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
#define INIT_PORT_CAP 2
#define MIN_SAFE_ACCEPT_QUEUE_SIZE 100

static gpr_once s_init_max_accept_queue_size;
static int s_max_accept_queue_size;

/* one listening port */
typedef struct server_port {
gpr_uint8 addresses[sizeof(struct sockaddr_in6) * 2 + 32];
Expand Down
4 changes: 2 additions & 2 deletions src/core/iomgr/tcp_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static void win_notify_on_read(grpc_endpoint *ep,
tcp->read_slice = gpr_slice_malloc(8192);

buffer.len = GPR_SLICE_LENGTH(tcp->read_slice);
buffer.buf = GPR_SLICE_START_PTR(tcp->read_slice);
buffer.buf = (char *)GPR_SLICE_START_PTR(tcp->read_slice);

gpr_log(GPR_DEBUG, "win_notify_on_read: calling WSARecv without overlap");
status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags,
Expand Down Expand Up @@ -284,7 +284,7 @@ static grpc_endpoint_write_status win_write(grpc_endpoint *ep,

for (i = 0; i < tcp->write_slices.count; i++) {
buffers[i].len = GPR_SLICE_LENGTH(tcp->write_slices.slices[i]);
buffers[i].buf = GPR_SLICE_START_PTR(tcp->write_slices.slices[i]);
buffers[i].buf = (char *)GPR_SLICE_START_PTR(tcp->write_slices.slices[i]);
}

gpr_log(GPR_DEBUG, "win_write: calling WSASend without overlap");
Expand Down
15 changes: 9 additions & 6 deletions src/core/support/env_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,24 @@
#ifdef GPR_WIN32

#include "src/core/support/env.h"
#include "src/core/support/string.h"

#include <stdlib.h>

#include <grpc/support/alloc.h>
#include <grpc/support/log.h>

char *gpr_getenv(const char *name) {
size_t required_size;
size_t size;
char *result = NULL;
char *duplicated;
errno_t err;

getenv_s(&required_size, NULL, 0, name);
if (required_size == 0) return NULL;
result = gpr_malloc(required_size);
getenv_s(&required_size, result, required_size, name);
return result;
err = _dupenv_s(&result, &size, name);
if (err) return NULL;
duplicated = gpr_strdup(result);
free(result);
return duplicated;
}

void gpr_setenv(const char *name, const char *value) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/support/file_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) {
if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end;

end:
if (result && tmp_filename) {
if (result && tmp_filename_out) {
*tmp_filename_out = gpr_tchar_to_char(tmp_filename);
}

Expand Down
8 changes: 5 additions & 3 deletions src/core/support/log_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <grpc/support/log.h>
#include <grpc/support/time.h>

#include "src/core/support/string.h"
#include "src/core/support/string_win32.h"

void gpr_log(const char *file, int line, gpr_log_severity severity,
Expand All @@ -55,7 +56,7 @@ void gpr_log(const char *file, int line, gpr_log_severity severity,
va_start(args, format);
ret = _vscprintf(format, args);
va_end(args);
if (!(0 <= ret && ret < ~(size_t)0)) {
if (ret < 0) {
message = NULL;
} else {
/* Allocate a new buffer, with space for the NUL terminator. */
Expand All @@ -66,7 +67,7 @@ void gpr_log(const char *file, int line, gpr_log_severity severity,
va_start(args, format);
ret = vsnprintf_s(message, strp_buflen, _TRUNCATE, format, args);
va_end(args);
if (ret != strp_buflen - 1) {
if ((size_t)ret != strp_buflen - 1) {
/* This should never happen. */
gpr_free(message);
message = NULL;
Expand All @@ -90,7 +91,7 @@ void gpr_default_log(gpr_log_func_args *args) {
strcpy(time_buffer, "error:strftime");
}

fprintf(stderr, "%s%s.%09u %5u %s:%d] %s\n",
fprintf(stderr, "%s%s.%09u %5lu %s:%d] %s\n",
gpr_log_severity_string(args->severity), time_buffer,
(int)(now.tv_nsec), GetCurrentThreadId(),
args->file, args->line, args->message);
Expand All @@ -105,6 +106,7 @@ char *gpr_format_message(DWORD messageid) {
NULL, messageid,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)(&tmessage), 0, NULL);
if (status == 0) return gpr_strdup("Unable to retreive error string");
message = gpr_tchar_to_char(tmessage);
LocalFree(tmessage);
return message;
Expand Down
6 changes: 4 additions & 2 deletions src/core/support/string_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

#include <grpc/support/alloc.h>

#include "src/core/support/string.h"

int gpr_asprintf(char **strp, const char *format, ...) {
va_list args;
int ret;
Expand All @@ -53,7 +55,7 @@ int gpr_asprintf(char **strp, const char *format, ...) {
va_start(args, format);
ret = _vscprintf(format, args);
va_end(args);
if (!(0 <= ret && ret < ~(size_t)0)) {
if (ret < 0) {
*strp = NULL;
return -1;
}
Expand All @@ -69,7 +71,7 @@ int gpr_asprintf(char **strp, const char *format, ...) {
va_start(args, format);
ret = vsnprintf_s(*strp, strp_buflen, _TRUNCATE, format, args);
va_end(args);
if (ret == strp_buflen - 1) {
if ((size_t)ret == strp_buflen - 1) {
return ret;
}

Expand Down
1 change: 1 addition & 0 deletions src/core/support/sync_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#ifdef GPR_WIN32

#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#include <windows.h>
#include <grpc/support/log.h>
Expand Down
4 changes: 2 additions & 2 deletions src/core/support/time_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

gpr_timespec gpr_now(void) {
gpr_timespec now_tv;
struct __timeb32 now_tb;
_ftime32_s(&now_tb);
struct _timeb now_tb;
_ftime_s(&now_tb);
now_tv.tv_sec = now_tb.time;
now_tv.tv_nsec = now_tb.millitm * 1000000;
return now_tv;
Expand Down
9 changes: 6 additions & 3 deletions src/cpp/common/call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ CallOpBuffer::CallOpBuffer()
initial_metadata_count_(0),
initial_metadata_(nullptr),
recv_initial_metadata_(nullptr),
recv_initial_metadata_arr_{0, 0, nullptr},
send_message_(nullptr),
send_message_buffer_(nullptr),
send_buf_(nullptr),
Expand All @@ -58,15 +57,19 @@ CallOpBuffer::CallOpBuffer()
client_send_close_(false),
recv_trailing_metadata_(nullptr),
recv_status_(nullptr),
recv_trailing_metadata_arr_{0, 0, nullptr},
status_code_(GRPC_STATUS_OK),
status_details_(nullptr),
status_details_capacity_(0),
send_status_(nullptr),
trailing_metadata_count_(0),
trailing_metadata_(nullptr),
cancelled_buf_(0),
recv_closed_(nullptr) {}
recv_closed_(nullptr) {
memset(&recv_trailing_metadata_arr_, 0, sizeof(recv_trailing_metadata_arr_));
memset(&recv_initial_metadata_arr_, 0, sizeof(recv_initial_metadata_arr_));
recv_trailing_metadata_arr_.metadata = nullptr;
recv_initial_metadata_arr_.metadata = nullptr;
}

void CallOpBuffer::Reset(void* next_return_tag) {
return_tag_ = next_return_tag;
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ void Server::RegisterAsyncGenericService(AsyncGenericService* service) {
service->server_ = this;
}

int Server::AddPort(const grpc::string& addr, ServerCredentials* creds) {
int Server::AddListeningPort(const grpc::string& addr,
ServerCredentials* creds) {
GPR_ASSERT(!started_);
return creds->AddPortToServer(addr, server_);
}
Expand Down
8 changes: 4 additions & 4 deletions src/cpp/server/server_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ void ServerBuilder::RegisterAsyncGenericService(AsyncGenericService* service) {
generic_service_ = service;
}

void ServerBuilder::AddPort(const grpc::string& addr,
std::shared_ptr<ServerCredentials> creds,
int* selected_port) {
void ServerBuilder::AddListeningPort(const grpc::string& addr,
std::shared_ptr<ServerCredentials> creds,
int* selected_port) {
ports_.push_back(Port{addr, creds, selected_port});
}

Expand Down Expand Up @@ -99,7 +99,7 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
server->RegisterAsyncGenericService(generic_service_);
}
for (auto& port : ports_) {
int r = server->AddPort(port.addr, port.creds.get());
int r = server->AddListeningPort(port.addr, port.creds.get());
if (!r) return nullptr;
if (port.selected_port != nullptr) {
*port.selected_port = r;
Expand Down
6 changes: 3 additions & 3 deletions src/csharp/Grpc.Core.Tests/ClientServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void UnaryCall()
ServerServiceDefinition.CreateBuilder(serviceName)
.AddMethod(unaryEchoStringMethod, HandleUnaryEchoString).Build());

int port = server.AddPort(host + ":0");
int port = server.AddListeningPort(host + ":0");
server.Start();

using (Channel channel = new Channel(host + ":" + port))
Expand All @@ -97,7 +97,7 @@ public void UnaryCallPerformance()
ServerServiceDefinition.CreateBuilder(serviceName)
.AddMethod(unaryEchoStringMethod, HandleUnaryEchoString).Build());

int port = server.AddPort(host + ":0");
int port = server.AddListeningPort(host + ":0");
server.Start();

using (Channel channel = new Channel(host + ":" + port))
Expand All @@ -117,7 +117,7 @@ public void UnknownMethodHandler()
server.AddServiceDefinition(
ServerServiceDefinition.CreateBuilder(serviceName).Build());

int port = server.AddPort(host + ":0");
int port = server.AddListeningPort(host + ":0");
server.Start();

using (Channel channel = new Channel(host + ":" + port))
Expand Down
2 changes: 1 addition & 1 deletion src/csharp/Grpc.Core.Tests/ServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void StartAndShutdownServer()
GrpcEnvironment.Initialize();

Server server = new Server();
server.AddPort("localhost:0");
server.AddListeningPort("localhost:0");
server.Start();
server.ShutdownAsync().Wait();

Expand Down
4 changes: 2 additions & 2 deletions src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public static ServerSafeHandle NewServer(CompletionQueueSafeHandle cq, IntPtr ar
return grpcsharp_server_create(cq, args);
}

public int AddPort(string addr)
public int AddListeningPort(string addr)
{
return grpcsharp_server_add_http2_port(this, addr);
}

public int AddPort(string addr, ServerCredentialsSafeHandle credentials)
public int AddListeningPort(string addr, ServerCredentialsSafeHandle credentials)
{
return grpcsharp_server_add_secure_http2_port(this, addr, credentials);
}
Expand Down
8 changes: 4 additions & 4 deletions src/csharp/Grpc.Core/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ public void AddServiceDefinition(ServerServiceDefinition serviceDefinition)
}

// only call before Start()
public int AddPort(string addr)
public int AddListeningPort(string addr)
{
return handle.AddPort(addr);
return handle.AddListeningPort(addr);
}

// only call before Start()
public int AddPort(string addr, ServerCredentials credentials)
public int AddListeningPort(string addr, ServerCredentials credentials)
{
using (var nativeCredentials = credentials.ToNativeCredentials())
{
return handle.AddPort(addr, nativeCredentials);
return handle.AddListeningPort(addr, nativeCredentials);
}
}

Expand Down
Loading

0 comments on commit cfd6073

Please sign in to comment.