From 795aebbee77bb74d8792df96dfe1aa79ec9548fc Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 10 Jul 2017 15:02:38 -0700 Subject: Give up on uncrustify; switch to clang-format. --- src/platform/windows/win_clock.c | 7 +- src/platform/windows/win_debug.c | 40 +++----- src/platform/windows/win_impl.h | 48 ++++----- src/platform/windows/win_iocp.c | 57 +++++----- src/platform/windows/win_ipc.c | 211 ++++++++++++++++---------------------- src/platform/windows/win_net.c | 34 +++--- src/platform/windows/win_pipe.c | 27 ++--- src/platform/windows/win_rand.c | 11 +- src/platform/windows/win_resolv.c | 75 ++++++-------- src/platform/windows/win_thread.c | 42 +++----- 10 files changed, 230 insertions(+), 322 deletions(-) (limited to 'src/platform/windows') diff --git a/src/platform/windows/win_clock.c b/src/platform/windows/win_clock.c index 9064b5e2..5a874a71 100644 --- a/src/platform/windows/win_clock.c +++ b/src/platform/windows/win_clock.c @@ -15,16 +15,14 @@ nni_time nni_plat_clock(void) { // We are limited by the system clock, but that is ok. - return (GetTickCount64()*1000); + return (GetTickCount64() * 1000); } - void nni_plat_usleep(nni_duration dur) { uint64_t exp; - // Convert duration to msec, rounding up. dur += 999; dur /= 1000; @@ -43,10 +41,9 @@ nni_plat_usleep(nni_duration dur) } } - #else // Suppress empty symbols warnings in ranlib. int nni_win_clock_not_used = 0; -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS diff --git a/src/platform/windows/win_debug.c b/src/platform/windows/win_debug.c index 43db0505..bc3d5bfa 100644 --- a/src/platform/windows/win_debug.c +++ b/src/platform/windows/win_debug.c @@ -12,8 +12,8 @@ #ifdef PLATFORM_WINDOWS #include -#include #include +#include #include void @@ -22,14 +22,12 @@ nni_plat_abort(void) abort(); } - void nni_plat_println(const char *message) { (void) fprintf(stderr, "%s\n", message); } - const char * nni_plat_strerror(int errnum) { @@ -39,25 +37,16 @@ nni_plat_strerror(int errnum) return (strerror(errnum)); } - // Win32 has its own error codes, but these ones it shares with POSIX. static struct { - int sys_err; - int nng_err; -} -nni_plat_errnos[] = { - { ENOENT, NNG_ENOENT }, - { EINTR, NNG_EINTR }, - { EINVAL, NNG_EINVAL }, - { ENOMEM, NNG_ENOMEM }, - { EACCES, NNG_EPERM }, - { EAGAIN, NNG_EAGAIN }, - { EBADF, NNG_ECLOSED }, - { EBUSY, NNG_EBUSY }, - { ENAMETOOLONG, NNG_EINVAL }, - { EPERM, NNG_EPERM }, - { EPIPE, NNG_ECLOSED }, - { 0, 0 } // must be last + int sys_err; + int nng_err; +} nni_plat_errnos[] = { + { ENOENT, NNG_ENOENT }, { EINTR, NNG_EINTR }, { EINVAL, NNG_EINVAL }, + { ENOMEM, NNG_ENOMEM }, { EACCES, NNG_EPERM }, { EAGAIN, NNG_EAGAIN }, + { EBADF, NNG_ECLOSED }, { EBUSY, NNG_EBUSY }, + { ENAMETOOLONG, NNG_EINVAL }, { EPERM, NNG_EPERM }, + { EPIPE, NNG_ECLOSED }, { 0, 0 } // must be last }; int @@ -77,15 +66,14 @@ nni_plat_errno(int errnum) return (NNG_ESYSERR + errnum); } - // Windows has infinite numbers of error codes it seems. We only bother // with the ones that are relevant to us (we think). Note that there is // no overlap between errnos and GetLastError values. static struct { - int win_err; - int nng_err; -} -nni_win_errnos[] = { + int win_err; + int nng_err; +} nni_win_errnos[] = { + // clang-format off { ERROR_FILE_NOT_FOUND, NNG_ENOENT }, { ERROR_ACCESS_DENIED, NNG_EPERM }, { ERROR_INVALID_HANDLE, NNG_ECLOSED }, @@ -106,6 +94,7 @@ nni_win_errnos[] = { { WAIT_TIMEOUT, NNG_ETIMEDOUT }, // Must be Last!! { 0, 0 }, + // clang-format on }; // This converts a Windows API error (from GetLastError()) to an @@ -127,7 +116,6 @@ nni_win_error(int errnum) return (NNG_ESYSERR + errnum); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index 6ce38250..234acc35 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -16,10 +16,10 @@ #define WIN32_LEAN_AND_MEAN #endif -#include -#include #include #include +#include +#include #include #include "core/list.h" @@ -27,7 +27,7 @@ // These types are provided for here, to permit them to be directly inlined // elsewhere. -typedef struct nni_win_event nni_win_event; +typedef struct nni_win_event nni_win_event; // nni_win_event is used with io completion ports. This allows us to get // to a specific completion callback without requiring the poller (in the @@ -35,50 +35,50 @@ typedef struct nni_win_event nni_win_event; // this to pass back status and counts to the routine, which may not be // conveyed in the OVERLAPPED directly. struct nni_win_event { - OVERLAPPED olpd; - HANDLE h; - void * ptr; - nni_cb cb; - nni_list aios; + OVERLAPPED olpd; + HANDLE h; + void * ptr; + nni_cb cb; + nni_list aios; }; struct nni_plat_thr { - void (*func)(void *); - void * arg; - HANDLE handle; + void (*func)(void *); + void * arg; + HANDLE handle; }; struct nni_plat_mtx { SRWLOCK srl; - DWORD owner; - int init; + DWORD owner; + int init; }; struct nni_plat_cv { - CONDITION_VARIABLE cv; - PSRWLOCK srl; + CONDITION_VARIABLE cv; + PSRWLOCK srl; }; extern int nni_win_error(int); extern int nni_winsock_error(int); -extern int nni_win_event_init(nni_win_event *, nni_cb, void *, HANDLE); -extern void nni_win_event_fini(nni_win_event *); -extern int nni_win_event_reset(nni_win_event *); +extern int nni_win_event_init(nni_win_event *, nni_cb, void *, HANDLE); +extern void nni_win_event_fini(nni_win_event *); +extern int nni_win_event_reset(nni_win_event *); extern OVERLAPPED *nni_win_event_overlapped(nni_win_event *); -extern void nni_win_event_cancel(nni_win_event *); +extern void nni_win_event_cancel(nni_win_event *); extern int nni_win_iocp_register(HANDLE); -extern int nni_win_iocp_sysinit(void); +extern int nni_win_iocp_sysinit(void); extern void nni_win_iocp_sysfini(void); -extern int nni_win_ipc_sysinit(void); +extern int nni_win_ipc_sysinit(void); extern void nni_win_ipc_sysfini(void); -extern int nni_win_resolv_sysinit(void); +extern int nni_win_resolv_sysinit(void); extern void nni_win_resolv_sysfini(void); -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS -#endif // PLATFORM_WIN_IMPL_H +#endif // PLATFORM_WIN_IMPL_H diff --git a/src/platform/windows/win_iocp.c b/src/platform/windows/win_iocp.c index 381da64f..7244b360 100644 --- a/src/platform/windows/win_iocp.c +++ b/src/platform/windows/win_iocp.c @@ -11,7 +11,7 @@ #ifdef PLATFORM_WINDOWS -#define NNI_WIN_IOCP_NTHREADS 4 +#define NNI_WIN_IOCP_NTHREADS 4 #include // Windows IO Completion Port support. We basically creaet a single @@ -19,33 +19,33 @@ // to the port on an as needed basis. We use a single IO completion // port for pretty much everything. -static HANDLE nni_win_global_iocp = NULL; +static HANDLE nni_win_global_iocp = NULL; static nni_thr nni_win_iocp_thrs[NNI_WIN_IOCP_NTHREADS]; static nni_mtx nni_win_iocp_mtx; static void nni_win_iocp_handler(void *arg) { - HANDLE iocp; - DWORD cnt; - ULONG_PTR key; - OVERLAPPED *olpd; + HANDLE iocp; + DWORD cnt; + ULONG_PTR key; + OVERLAPPED * olpd; nni_win_event *evt; - int status; - BOOL rv; + int status; + BOOL rv; NNI_ARG_UNUSED(arg); iocp = nni_win_global_iocp; for (;;) { - key = 0; - olpd = NULL; + key = 0; + olpd = NULL; status = 0; - cnt = 0; + cnt = 0; - rv = GetQueuedCompletionStatus(iocp, &cnt, &key, &olpd, - INFINITE); + rv = GetQueuedCompletionStatus( + iocp, &cnt, &key, &olpd, INFINITE); if (rv == FALSE) { if (olpd == NULL) { @@ -62,7 +62,6 @@ nni_win_iocp_handler(void *arg) } } - int nni_win_iocp_register(HANDLE h) { @@ -72,23 +71,21 @@ nni_win_iocp_register(HANDLE h) return (0); } - int nni_win_event_init(nni_win_event *evt, nni_cb cb, void *ptr, HANDLE h) { - ZeroMemory(&evt->olpd, sizeof (evt->olpd)); + ZeroMemory(&evt->olpd, sizeof(evt->olpd)); evt->olpd.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (evt->olpd.hEvent == NULL) { return (nni_win_error(GetLastError())); } nni_aio_list_init(&evt->aios); evt->ptr = ptr; - evt->cb = cb; - evt->h = h; + evt->cb = cb; + evt->h = h; return (0); } - void nni_win_event_fini(nni_win_event *evt) { @@ -98,7 +95,6 @@ nni_win_event_fini(nni_win_event *evt) } } - int nni_win_event_reset(nni_win_event *evt) { @@ -108,18 +104,16 @@ nni_win_event_reset(nni_win_event *evt) return (0); } - OVERLAPPED * nni_win_event_overlapped(nni_win_event *evt) { return (&evt->olpd); } - void nni_win_event_cancel(nni_win_event *evt) { - int rv; + int rv; DWORD cnt; // Try to cancel the event... @@ -146,23 +140,22 @@ nni_win_event_cancel(nni_win_event *evt) GetOverlappedResult(evt->h, &evt->olpd, &cnt, TRUE); } - int nni_win_iocp_sysinit(void) { HANDLE h; - int i; - int rv; + int i; + int rv; - h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, - NNI_WIN_IOCP_NTHREADS); + h = CreateIoCompletionPort( + INVALID_HANDLE_VALUE, NULL, 0, NNI_WIN_IOCP_NTHREADS); if (h == NULL) { return (nni_win_error(GetLastError())); } nni_win_global_iocp = h; for (i = 0; i < NNI_WIN_IOCP_NTHREADS; i++) { - rv = nni_thr_init(&nni_win_iocp_thrs[i], nni_win_iocp_handler, - NULL); + rv = nni_thr_init( + &nni_win_iocp_thrs[i], nni_win_iocp_handler, NULL); if (rv != 0) { goto fail; } @@ -186,11 +179,10 @@ fail: return (rv); } - void nni_win_iocp_sysfini(void) { - int i; + int i; HANDLE h; if ((h = nni_win_global_iocp) != NULL) { @@ -203,7 +195,6 @@ nni_win_iocp_sysfini(void) nni_mtx_fini(&nni_win_iocp_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_ipc.c b/src/platform/windows/win_ipc.c index f6601f93..237ded78 100644 --- a/src/platform/windows/win_ipc.c +++ b/src/platform/windows/win_ipc.c @@ -20,31 +20,30 @@ static void nni_win_ipc_send_start(nni_plat_ipc_pipe *); static void nni_win_ipc_recv_start(nni_plat_ipc_pipe *); struct nni_plat_ipc_pipe { - HANDLE p; - nni_win_event recv_evt; - nni_win_event send_evt; - nni_mtx mtx; - nni_list readq; - nni_list writeq; + HANDLE p; + nni_win_event recv_evt; + nni_win_event send_evt; + nni_mtx mtx; + nni_list readq; + nni_list writeq; }; struct nni_plat_ipc_ep { - char path[256]; - int mode; - int started; - nni_list aios; - HANDLE p; // accept side only - nni_win_event acc_evt; // accept side only - nni_mtx mtx; // accept side only - nni_list_node node; // conn side uses this + char path[256]; + int mode; + int started; + nni_list aios; + HANDLE p; // accept side only + nni_win_event acc_evt; // accept side only + nni_mtx mtx; // accept side only + nni_list_node node; // conn side uses this }; - static int nni_win_ipc_pipe_init(nni_plat_ipc_pipe **pipep, HANDLE p) { nni_plat_ipc_pipe *pipe; - int rv; + int rv; if ((pipe = NNI_ALLOC_STRUCT(pipe)) == NULL) { return (NNG_ENOMEM); @@ -71,7 +70,6 @@ nni_win_ipc_pipe_init(nni_plat_ipc_pipe **pipep, HANDLE p) return (0); } - static void nni_win_ipc_send_cancel(nni_aio *aio) { @@ -83,15 +81,14 @@ nni_win_ipc_send_cancel(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) { - nni_win_event *evt = &pipe->send_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - int rv = 0; - nni_aio *aio; - DWORD cnt; + nni_win_event *evt = &pipe->send_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + int rv = 0; + nni_aio * aio; + DWORD cnt; if (GetOverlappedResult(pipe->p, olpd, &cnt, TRUE) == FALSE) { rv = nni_win_error(GetLastError()); @@ -110,7 +107,7 @@ nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) if (aio->a_iov[0].iov_len == 0) { int i; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } aio->a_niov--; } @@ -127,16 +124,15 @@ nni_win_ipc_send_finish(nni_plat_ipc_pipe *pipe) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_send_start(nni_plat_ipc_pipe *pipe) { - void *buf; - DWORD len; - int rv; - nni_win_event *evt = &pipe->send_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - nni_aio *aio = nni_list_first(&pipe->writeq); + void * buf; + DWORD len; + int rv; + nni_win_event *evt = &pipe->send_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + nni_aio * aio = nni_list_first(&pipe->writeq); NNI_ASSERT(aio != NULL); NNI_ASSERT(aio->a_niov > 0); @@ -157,8 +153,8 @@ nni_win_ipc_send_start(nni_plat_ipc_pipe *pipe) // scrambling the data anyway. Note that Windows named pipes do // not appear to support scatter/gather, so we have to process // each element in turn. - buf = aio->a_iov[0].iov_buf; - len = (DWORD) aio->a_iov[0].iov_len; + buf = aio->a_iov[0].iov_buf; + len = (DWORD) aio->a_iov[0].iov_len; olpd = nni_win_event_overlapped(evt); // We limit ourselves to writing 16MB at a time. Named Pipes @@ -190,7 +186,6 @@ fail: nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_send_cb(void *arg) { @@ -201,12 +196,11 @@ nni_win_ipc_send_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *pipe, nni_aio *aio) { nni_win_event *evt = &pipe->send_evt; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_start(aio, nni_win_ipc_send_cancel, pipe)) != 0) { @@ -229,7 +223,6 @@ nni_plat_ipc_pipe_send(nni_plat_ipc_pipe *pipe, nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_recv_cancel(nni_aio *aio) { @@ -241,15 +234,14 @@ nni_win_ipc_recv_cancel(nni_aio *aio) nni_mtx_unlock(&pipe->mtx); } - static void nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) { - nni_win_event *evt = &pipe->recv_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - int rv = 0; - nni_aio *aio; - DWORD cnt; + nni_win_event *evt = &pipe->recv_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + int rv = 0; + nni_aio * aio; + DWORD cnt; if (GetOverlappedResult(pipe->p, olpd, &cnt, TRUE) == FALSE) { rv = nni_win_error(GetLastError()); @@ -268,7 +260,7 @@ nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) if (aio->a_iov[0].iov_len == 0) { int i; for (i = 1; i < aio->a_niov; i++) { - aio->a_iov[i-1] = aio->a_iov[i]; + aio->a_iov[i - 1] = aio->a_iov[i]; } aio->a_niov--; } @@ -285,16 +277,15 @@ nni_win_ipc_recv_finish(nni_plat_ipc_pipe *pipe) nni_aio_finish(aio, rv, aio->a_count); } - static void nni_win_ipc_recv_start(nni_plat_ipc_pipe *pipe) { - void *buf; - DWORD len; - int rv; - nni_win_event *evt = &pipe->recv_evt; - OVERLAPPED *olpd = nni_win_event_overlapped(evt); - nni_aio *aio = nni_list_first(&pipe->readq); + void * buf; + DWORD len; + int rv; + nni_win_event *evt = &pipe->recv_evt; + OVERLAPPED * olpd = nni_win_event_overlapped(evt); + nni_aio * aio = nni_list_first(&pipe->readq); NNI_ASSERT(aio != NULL); NNI_ASSERT(aio->a_niov > 0); @@ -315,8 +306,8 @@ nni_win_ipc_recv_start(nni_plat_ipc_pipe *pipe) // scrambling the data anyway. Note that Windows named pipes do // not appear to support scatter/gather, so we have to process // each element in turn. - buf = aio->a_iov[0].iov_buf; - len = (DWORD) aio->a_iov[0].iov_len; + buf = aio->a_iov[0].iov_buf; + len = (DWORD) aio->a_iov[0].iov_len; olpd = nni_win_event_overlapped(evt); // We limit ourselves to writing 16MB at a time. Named Pipes @@ -348,7 +339,6 @@ fail: nni_aio_finish(aio, rv, 0); } - static void nni_win_ipc_recv_cb(void *arg) { @@ -359,12 +349,11 @@ nni_win_ipc_recv_cb(void *arg) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *pipe, nni_aio *aio) { nni_win_event *evt = &pipe->send_evt; - int rv; + int rv; nni_mtx_lock(&pipe->mtx); if ((rv = nni_aio_start(aio, nni_win_ipc_recv_cancel, pipe)) != 0) { @@ -386,7 +375,6 @@ nni_plat_ipc_pipe_recv(nni_plat_ipc_pipe *pipe, nni_aio *aio) nni_win_ipc_recv_start(pipe); } - void nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *pipe) { @@ -400,7 +388,6 @@ nni_plat_ipc_pipe_close(nni_plat_ipc_pipe *pipe) nni_mtx_unlock(&pipe->mtx); } - void nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *pipe) { @@ -412,13 +399,12 @@ nni_plat_ipc_pipe_fini(nni_plat_ipc_pipe *pipe) NNI_FREE_STRUCT(pipe); } - int nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) { - const char *path; + const char * path; nni_plat_ipc_ep *ep; - int rv; + int rv; if (strncmp(url, "ipc://", strlen("ipc://")) != 0) { return (NNG_EADDRINVAL); @@ -427,7 +413,7 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { return (NNG_ENOMEM); } - ZeroMemory(ep, sizeof (ep)); + ZeroMemory(ep, sizeof(ep)); if ((rv = nni_mtx_init(&ep->mtx)) != 0) { NNI_FREE_STRUCT(ep); return (rv); @@ -437,17 +423,16 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const char *url, int mode) NNI_LIST_NODE_INIT(&ep->node); nni_aio_list_init(&ep->aios); - (void) snprintf(ep->path, sizeof (ep->path), "\\\\.\\pipe\\%s", path); + (void) snprintf(ep->path, sizeof(ep->path), "\\\\.\\pipe\\%s", path); *epp = ep; return (0); } - int nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) { - int rv; + int rv; HANDLE p; nni_mtx_lock(&ep->mtx); @@ -463,12 +448,11 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) // We create the first named pipe, and we make sure that it is // properly ours. p = CreateNamedPipeA(ep->path, - PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | - FILE_FLAG_FIRST_PIPE_INSTANCE, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | - PIPE_REJECT_REMOTE_CLIENTS, - PIPE_UNLIMITED_INSTANCES, - 4096, 4096, 0, NULL); + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | + FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL); if (p == INVALID_HANDLE_VALUE) { if ((rv = GetLastError()) == ERROR_ACCESS_DENIED) { rv = NNG_EADDRINUSE; @@ -486,7 +470,7 @@ nni_plat_ipc_ep_listen(nni_plat_ipc_ep *ep) goto failed; } - ep->p = p; + ep->p = p; ep->started = 1; nni_mtx_unlock(&ep->mtx); return (0); @@ -501,16 +485,15 @@ failed: return (rv); } - static void nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) { - nni_win_event *evt = &ep->acc_evt; - DWORD nbytes; - int rv; + nni_win_event * evt = &ep->acc_evt; + DWORD nbytes; + int rv; nni_plat_ipc_pipe *pipe; - nni_aio *aio; - HANDLE newp, oldp; + nni_aio * aio; + HANDLE newp, oldp; // Note: This should be called with the ep lock held, and only when // the ConnectNamedPipe has finished. @@ -541,17 +524,16 @@ nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) } newp = CreateNamedPipeA(ep->path, - PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, - PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | - PIPE_REJECT_REMOTE_CLIENTS, - PIPE_UNLIMITED_INSTANCES, - 4096, 4096, 0, NULL); + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | + PIPE_REJECT_REMOTE_CLIENTS, + PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL); if (newp == INVALID_HANDLE_VALUE) { rv = nni_win_error(GetLastError()); DisconnectNamedPipe(ep->p); return; } - oldp = ep->p; + oldp = ep->p; ep->p = newp; if ((rv = nni_win_ipc_pipe_init(&pipe, oldp)) != 0) { @@ -564,7 +546,6 @@ nni_win_ipc_acc_finish(nni_plat_ipc_ep *ep) nni_aio_finish(aio, 0, 0); } - static void nni_win_ipc_acc_cb(void *arg) { @@ -575,7 +556,6 @@ nni_win_ipc_acc_cb(void *arg) nni_mtx_unlock(&ep->mtx); } - static void nni_win_ipc_acc_cancel(nni_aio *aio) { @@ -587,11 +567,10 @@ nni_win_ipc_acc_cancel(nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - void nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) { - int rv; + int rv; nni_win_event *evt = &ep->acc_evt; nni_mtx_lock(&ep->mtx); @@ -628,34 +607,32 @@ nni_plat_ipc_ep_accept(nni_plat_ipc_ep *ep, nni_aio *aio) nni_mtx_unlock(&ep->mtx); } - // So Windows IPC is a bit different on the client side. There is no // support for asynchronous connection, but we can fake it with a single // thread that runs to establish the connection. That thread will run // keep looping, sleeping for 10 ms between attempts. It performs non-blocking // attempts to connect. -typedef struct nni_win_ipc_conn_work nni_win_ipc_conn_work; +typedef struct nni_win_ipc_conn_work nni_win_ipc_conn_work; struct nni_win_ipc_conn_work { - nni_list waiters; - nni_list workers; - nni_mtx mtx; - nni_cv cv; - nni_thr thr; - int exit; + nni_list waiters; + nni_list workers; + nni_mtx mtx; + nni_cv cv; + nni_thr thr; + int exit; }; static nni_win_ipc_conn_work nni_win_ipc_connecter; - static void nni_win_ipc_conn_thr(void *arg) { nni_win_ipc_conn_work *w = arg; - nni_plat_ipc_ep *ep; - nni_plat_ipc_pipe *pipe; - nni_aio *aio; - HANDLE p; - int rv; + nni_plat_ipc_ep * ep; + nni_plat_ipc_pipe * pipe; + nni_aio * aio; + HANDLE p; + int rv; nni_mtx_lock(&w->mtx); for (;;) { @@ -674,10 +651,9 @@ nni_win_ipc_conn_thr(void *arg) continue; } nni_list_remove(&ep->aios, aio); - p = CreateFileA(ep->path, - GENERIC_READ | GENERIC_WRITE, - 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, - NULL); + p = CreateFileA(ep->path, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, + NULL); if (p == INVALID_HANDLE_VALUE) { switch ((rv = GetLastError())) { @@ -686,12 +662,12 @@ nni_win_ipc_conn_thr(void *arg) nni_list_prepend(&ep->aios, aio); break; case ERROR_FILE_NOT_FOUND: - nni_aio_finish(aio, NNG_ECONNREFUSED, - 0); + nni_aio_finish( + aio, NNG_ECONNREFUSED, 0); break; default: - nni_aio_finish(aio, nni_win_error(rv), - 0); + nni_aio_finish( + aio, nni_win_error(rv), 0); break; } } else { @@ -723,12 +699,11 @@ nni_win_ipc_conn_thr(void *arg) nni_mtx_unlock(&w->mtx); } - static void nni_win_ipc_conn_cancel(nni_aio *aio) { - nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; - nni_plat_ipc_ep *ep = aio->a_prov_data; + nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; + nni_plat_ipc_ep * ep = aio->a_prov_data; nni_mtx_lock(&w->mtx); nni_aio_list_remove(aio); @@ -738,7 +713,6 @@ nni_win_ipc_conn_cancel(nni_aio *aio) nni_mtx_unlock(&w->mtx); } - void nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) { @@ -762,7 +736,6 @@ nni_plat_ipc_ep_connect(nni_plat_ipc_ep *ep, nni_aio *aio) nni_mtx_unlock(&w->mtx); } - void nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) { @@ -776,12 +749,11 @@ nni_plat_ipc_ep_fini(nni_plat_ipc_ep *ep) NNI_FREE_STRUCT(ep); } - void nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) { nni_win_ipc_conn_work *w = &nni_win_ipc_connecter; - nni_aio *aio; + nni_aio * aio; switch (ep->mode) { case NNI_EP_MODE_DIAL: @@ -811,11 +783,10 @@ nni_plat_ipc_ep_close(nni_plat_ipc_ep *ep) } } - int nni_win_ipc_sysinit(void) { - int rv; + int rv; nni_win_ipc_conn_work *worker = &nni_win_ipc_connecter; NNI_LIST_INIT(&worker->workers, nni_plat_ipc_ep, node); @@ -835,7 +806,6 @@ nni_win_ipc_sysinit(void) return (0); } - void nni_win_ipc_sysfini(void) { @@ -850,7 +820,6 @@ nni_win_ipc_sysfini(void) nni_mtx_fini(&worker->mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_net.c b/src/platform/windows/win_net.c index 2a77d97f..19491d02 100644 --- a/src/platform/windows/win_net.c +++ b/src/platform/windows/win_net.c @@ -14,36 +14,33 @@ #include static LPFN_CONNECTEX nni_win_connectex; -static LPFN_ACCEPTEX nni_win_acceptex; - +static LPFN_ACCEPTEX nni_win_acceptex; struct nni_plat_tcp_pipe { - SOCKET s; - nni_win_event recv_evt; - nni_win_event send_evt; - WSAOVERLAPPED recv_olpd; - WSAOVERLAPPED send_olpd; + SOCKET s; + nni_win_event recv_evt; + nni_win_event send_evt; + WSAOVERLAPPED recv_olpd; + WSAOVERLAPPED send_olpd; }; - struct nni_plat_tcp_ep { - SOCKET s; - nni_win_event evt; - WSAOVERLAPPED olpd; + SOCKET s; + nni_win_event evt; + WSAOVERLAPPED olpd; // We have to lookup some function pointers using ioctls. Winsock, // gotta love it. - LPFN_CONNECTEX connectex; - LPFN_ACCEPTEX acceptex; + LPFN_CONNECTEX connectex; + LPFN_ACCEPTEX acceptex; }; - // Windows has infinite numbers of error codes it seems. static struct { - int wsa_err; - int nng_err; -} -nni_plat_wsa_errnos[] = { + int wsa_err; + int nng_err; +} nni_plat_wsa_errnos[] = { + // clang-format off { WSA_INVALID_HANDLE, NNG_ECLOSED }, { WSA_NOT_ENOUGH_MEMORY, NNG_ENOMEM }, { WSA_INVALID_PARAMETER, NNG_EINVAL }, @@ -106,6 +103,7 @@ nni_plat_wsa_errnos[] = { // Eliding all the QoS related errors. // Must be Last!! { 0, 0 }, + // clang-format on }; diff --git a/src/platform/windows/win_pipe.c b/src/platform/windows/win_pipe.c index 22886faa..ed6149c0 100644 --- a/src/platform/windows/win_pipe.c +++ b/src/platform/windows/win_pipe.c @@ -24,17 +24,17 @@ nni_plat_pipe_open(int *wfdp, int *rfdp) SOCKET wfd = INVALID_SOCKET; struct sockaddr_in addr; - socklen_t alen; - int one; - ULONG yes; - int rv; + socklen_t alen; + int one; + ULONG yes; + int rv; - ZeroMemory(&addr, sizeof (addr)); + ZeroMemory(&addr, sizeof(addr)); // Restrict our bind to the loopback address. We bind to an // ephemeral port. - addr.sin_family = AF_INET; - addr.sin_port = 0; + addr.sin_family = AF_INET; + addr.sin_port = 0; addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); afd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -42,15 +42,14 @@ nni_plat_pipe_open(int *wfdp, int *rfdp) goto fail; } - // Make sure we have exclusive address use... one = 1; - if (setsockopt(afd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - (char *) (&one), sizeof (one)) != 0) { + if (setsockopt(afd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (char *) (&one), + sizeof(one)) != 0) { goto fail; } - alen = sizeof (addr); + alen = sizeof(addr); if (bind(afd, (struct sockaddr *) &addr, alen) != 0) { goto fail; } @@ -110,7 +109,6 @@ fail: return (rv); } - void nni_plat_pipe_raise(int wfd) { @@ -119,7 +117,6 @@ nni_plat_pipe_raise(int wfd) send((SOCKET) wfd, &c, 1, 0); } - void nni_plat_pipe_clear(int rfd) { @@ -128,13 +125,12 @@ nni_plat_pipe_clear(int rfd) for (;;) { // Completely drain the pipe, but don't wait. This coalesces // events somewhat. - if (recv((SOCKET) rfd, buf, sizeof (buf), 0) <= 0) { + if (recv((SOCKET) rfd, buf, sizeof(buf), 0) <= 0) { return; } } } - void nni_plat_pipe_close(int wfd, int rfd) { @@ -142,7 +138,6 @@ nni_plat_pipe_close(int wfd, int rfd) closesocket((SOCKET) rfd); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_rand.c b/src/platform/windows/win_rand.c index a1e381d8..38c86572 100644 --- a/src/platform/windows/win_rand.c +++ b/src/platform/windows/win_rand.c @@ -21,18 +21,17 @@ nni_plat_seed_prng(void *buf, size_t bufsz) // The rand_s routine uses RtlGenRandom to get high quality // pseudo random numbers (i.e. numbers that should be good enough // for use with crypto keying.) - while (bufsz > sizeof (val)) { + while (bufsz > sizeof(val)) { rand_s(&val); - memcpy(buf, &val, sizeof (val)); - buf = (((char *) buf) + sizeof (val)); - bufsz -= sizeof (val); + memcpy(buf, &val, sizeof(val)); + buf = (((char *) buf) + sizeof(val)); + bufsz -= sizeof(val); } } - #else // Suppress empty symbols warnings in ranlib. int nni_win_rand_not_used = 0; -#endif // PLATFORM_WINDOWS +#endif // PLATFORM_WINDOWS diff --git a/src/platform/windows/win_resolv.c b/src/platform/windows/win_resolv.c index 3d37d546..44d00c34 100644 --- a/src/platform/windows/win_resolv.c +++ b/src/platform/windows/win_resolv.c @@ -7,7 +7,6 @@ // found online at https://opensource.org/licenses/MIT. // - #include "core/nng_impl.h" #ifdef PLATFORM_WINDOWS @@ -23,25 +22,23 @@ // changed with this define. #ifndef NNG_WIN_RESOLV_CONCURRENCY -#define NNG_WIN_RESOLV_CONCURRENCY 4 +#define NNG_WIN_RESOLV_CONCURRENCY 4 #endif - static nni_taskq *nni_win_resolv_tq = NULL; -static nni_mtx nni_win_resolv_mtx; +static nni_mtx nni_win_resolv_mtx; -typedef struct nni_win_resolv_item nni_win_resolv_item; +typedef struct nni_win_resolv_item nni_win_resolv_item; struct nni_win_resolv_item { - int family; - int passive; - const char * name; - const char * serv; - int proto; - nni_aio * aio; - nni_taskq_ent tqe; + int family; + int passive; + const char * name; + const char * serv; + int proto; + nni_aio * aio; + nni_taskq_ent tqe; }; - static void nni_win_resolv_finish(nni_win_resolv_item *item, int rv) { @@ -52,7 +49,6 @@ nni_win_resolv_finish(nni_win_resolv_item *item, int rv) NNI_FREE_STRUCT(item); } - static void nni_win_resolv_cancel(nni_aio *aio) { @@ -69,7 +65,6 @@ nni_win_resolv_cancel(nni_aio *aio) NNI_FREE_STRUCT(item); } - static int nni_win_gai_errno(int rv) { @@ -97,16 +92,15 @@ nni_win_gai_errno(int rv) } } - static void nni_win_resolv_task(void *arg) { nni_win_resolv_item *item = arg; - nni_aio *aio = item->aio; - struct addrinfo hints; - struct addrinfo *results; - struct addrinfo *probe; - int i, rv; + nni_aio * aio = item->aio; + struct addrinfo hints; + struct addrinfo * results; + struct addrinfo * probe; + int i, rv; results = NULL; @@ -116,13 +110,13 @@ nni_win_resolv_task(void *arg) case AF_UNSPEC: // We treat these all as IP addresses. The service and the // host part are split. - memset(&hints, 0, sizeof (hints)); + memset(&hints, 0, sizeof(hints)); if (item->passive) { hints.ai_flags |= AI_PASSIVE; } hints.ai_flags |= AI_ADDRCONFIG; hints.ai_protocol = item->proto; - hints.ai_family = item->family; + hints.ai_family = item->family; if (item->family == AF_INET6) { hints.ai_flags |= AI_V4MAPPED; } @@ -152,27 +146,27 @@ nni_win_resolv_task(void *arg) aio->a_addrs = NNI_ALLOC_STRUCTS(aio->a_addrs, aio->a_naddrs); if (aio->a_addrs == NULL) { aio->a_naddrs = 0; - rv = NNG_ENOMEM; + rv = NNG_ENOMEM; break; } i = 0; for (probe = results; probe != NULL; probe = probe->ai_next) { - struct sockaddr_in *sin; + struct sockaddr_in * sin; struct sockaddr_in6 *sin6; - nng_sockaddr *sa = &aio->a_addrs[i]; + nng_sockaddr * sa = &aio->a_addrs[i]; switch (probe->ai_addr->sa_family) { case AF_INET: sin = (void *) probe->ai_addr; sa->s_un.s_in.sa_family = NNG_AF_INET; - sa->s_un.s_in.sa_port = sin->sin_port; - sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; + sa->s_un.s_in.sa_port = sin->sin_port; + sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr; i++; break; case AF_INET6: sin6 = (void *) probe->ai_addr; sa->s_un.s_in6.sa_family = NNG_AF_INET6; - sa->s_un.s_in6.sa_port = sin6->sin6_port; + sa->s_un.s_in6.sa_port = sin6->sin6_port; memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16); i++; @@ -200,13 +194,12 @@ nni_win_resolv_task(void *arg) nni_mtx_unlock(&nni_win_resolv_mtx); } - static void -nni_win_resolv_ip(const char *host, const char *serv, int passive, - int family, int proto, nni_aio *aio) +nni_win_resolv_ip(const char *host, const char *serv, int passive, int family, + int proto, nni_aio *aio) { nni_win_resolv_item *item; - int rv; + int rv; if ((aio->a_naddrs != 0) && (aio->a_addrs != NULL)) { NNI_FREE_STRUCTS(aio->a_addrs, aio->a_naddrs); @@ -231,10 +224,10 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, } // NB: host and serv must remain valid until this is completed. item->passive = passive; - item->name = host; - item->serv = serv; - item->proto = proto; - item->aio = aio; + item->name = host; + item->serv = serv; + item->proto = proto; + item->aio = aio; nni_mtx_lock(&nni_win_resolv_mtx); // If we were stopped, we're done... @@ -251,15 +244,13 @@ nni_win_resolv_ip(const char *host, const char *serv, int passive, nni_mtx_unlock(&nni_win_resolv_mtx); } - void -nni_plat_tcp_resolv(const char *host, const char *serv, int family, - int passive, nni_aio *aio) +nni_plat_tcp_resolv( + const char *host, const char *serv, int family, int passive, nni_aio *aio) { nni_win_resolv_ip(host, serv, passive, family, IPPROTO_TCP, aio); } - int nni_win_resolv_sysinit(void) { @@ -275,7 +266,6 @@ nni_win_resolv_sysinit(void) return (0); } - void nni_win_resolv_sysfini(void) { @@ -286,7 +276,6 @@ nni_win_resolv_sysfini(void) nni_mtx_fini(&nni_win_resolv_mtx); } - #else // Suppress empty symbols warnings in ranlib. diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index bc2d7a0c..693516bd 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -22,7 +22,6 @@ nni_alloc(size_t sz) return (v); } - void nni_free(void *b, size_t z) { @@ -30,7 +29,6 @@ nni_free(void *b, size_t z) HeapFree(GetProcessHeap(), 0, b); } - int nni_plat_mtx_init(nni_plat_mtx *mtx) { @@ -39,28 +37,24 @@ nni_plat_mtx_init(nni_plat_mtx *mtx) return (0); } - void nni_plat_mtx_fini(nni_plat_mtx *mtx) { mtx->init = 0; } - void nni_plat_mtx_lock(nni_plat_mtx *mtx) { AcquireSRWLockExclusive(&mtx->srl); } - void nni_plat_mtx_unlock(nni_plat_mtx *mtx) { ReleaseSRWLockExclusive(&mtx->srl); } - int nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) { @@ -69,49 +63,43 @@ nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) return (0); } - void nni_plat_cv_wake(nni_plat_cv *cv) { WakeAllConditionVariable(&cv->cv); } - void nni_plat_cv_wait(nni_plat_cv *cv) { (void) SleepConditionVariableSRW(&cv->cv, cv->srl, INFINITE, 0); } - int nni_plat_cv_until(nni_plat_cv *cv, nni_time until) { nni_time now; - DWORD msec; - BOOL ok; + DWORD msec; + BOOL ok; now = nni_plat_clock(); if (now > until) { msec = 0; } else { // times are in usec, but win32 wants millis - msec = (DWORD) (((until - now) + 999)/1000); + msec = (DWORD)(((until - now) + 999) / 1000); } ok = SleepConditionVariableSRW(&cv->cv, cv->srl, msec, 0); return (ok ? 0 : NNG_ETIMEDOUT); } - void nni_plat_cv_fini(nni_plat_cv *cv) { } - -static unsigned int __stdcall -nni_plat_thr_main(void *arg) +static unsigned int __stdcall nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; @@ -119,25 +107,22 @@ nni_plat_thr_main(void *arg) return (0); } - int nni_plat_thr_init(nni_plat_thr *thr, void (*fn)(void *), void *arg) { thr->func = fn; - thr->arg = arg; + thr->arg = arg; // We could probably even go down to 8k... but crypto for some // protocols might get bigger than this. 1MB is waaay too big. - thr->handle = (HANDLE) _beginthreadex(NULL, 16384, - nni_plat_thr_main, thr, STACK_SIZE_PARAM_IS_A_RESERVATION, - NULL); + thr->handle = (HANDLE) _beginthreadex(NULL, 16384, nni_plat_thr_main, + thr, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL); if (thr->handle == NULL) { - return (NNG_ENOMEM); // Best guess... + return (NNG_ENOMEM); // Best guess... } return (0); } - void nni_plat_thr_fini(nni_plat_thr *thr) { @@ -149,23 +134,22 @@ nni_plat_thr_fini(nni_plat_thr *thr) } } - int nni_plat_init(int (*helper)(void)) { - static LONG inited = 0; - int rv; + static LONG inited = 0; + int rv; static SRWLOCK lock = SRWLOCK_INIT; if (inited) { - return (0); // fast path + return (0); // fast path } AcquireSRWLockExclusive(&lock); if (!inited) { WSADATA data; - WORD ver; + WORD ver; ver = MAKEWORD(2, 2); if (WSAStartup(MAKEWORD(2, 2), &data) != 0) { if ((LOBYTE(data.wVersion) != 2) || @@ -195,7 +179,6 @@ out: return (rv); } - void nni_plat_fini(void) { @@ -205,7 +188,6 @@ nni_plat_fini(void) WSACleanup(); } - #else // Suppress empty symbols warnings in ranlib. -- cgit v1.2.3-70-g09d2