aboutsummaryrefslogtreecommitdiff
path: root/src/platform/windows/win_impl.h
blob: b8741b52f92647d6d56b2fb90aab38b7a3e050ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#ifndef PLATFORM_WIN_IMPL_H
#define PLATFORM_WIN_IMPL_H

#ifdef NNG_PLATFORM_WINDOWS

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

// These headers must be included first.
#include <windows.h>
#include <winsock2.h>

#include <mswsock.h>
#include <process.h>
#include <ws2tcpip.h>

#include "core/list.h"

// These types are provided for here, to permit them to be directly inlined
// elsewhere.

struct nni_plat_thr {
	void (*func)(void *);
	void * arg;
	HANDLE handle;
	DWORD  id;
};

struct nni_plat_mtx {
	SRWLOCK srl;
	DWORD   owner;
	int     init;
};

struct nni_plat_cv {
	CONDITION_VARIABLE cv;
	PSRWLOCK           srl;
};

struct nni_atomic_flag {
	unsigned f;
};

// 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
// completion port) to know anything about the event itself.  We also use
// this to pass back status and counts to the routine, which may not be
// conveyed in the OVERLAPPED directly.
typedef struct nni_win_event     nni_win_event;
typedef struct nni_win_event_ops nni_win_event_ops;

struct nni_win_event_ops {
	int (*wev_start)(nni_win_event *, nni_aio *);
	void (*wev_finish)(nni_win_event *, nni_aio *);
	void (*wev_cancel)(nni_win_event *);
};
struct nni_win_event {
	OVERLAPPED        olpd;
	void *            ptr;
	nni_mtx           mtx;
	nni_cv            cv;
	unsigned          run : 1;
	unsigned          fini : 1;
	unsigned          closed : 1;
	unsigned          count;
	int               status;
	nni_list          aios;
	nni_aio *         active;
	nni_win_event_ops ops;
};

typedef struct nni_win_io nni_win_io;
typedef void (*nni_win_io_cb)(nni_win_io *, int, size_t);

struct nni_win_io {
	OVERLAPPED    olpd;
	HANDLE        f;
	void *        ptr;
	nni_aio *     aio;
	nni_win_io_cb cb;
};

struct nni_plat_flock {
	HANDLE h;
};

extern int nni_win_error(int);

extern int  nni_win_event_init(nni_win_event *, nni_win_event_ops *, void *);
extern void nni_win_event_fini(nni_win_event *);
extern void nni_win_event_submit(nni_win_event *, nni_aio *);
extern void nni_win_event_close(nni_win_event *);
extern void nni_win_event_complete(nni_win_event *, int);

extern int nni_win_iocp_register(HANDLE);

extern int  nni_win_tcp_conn_init(nni_tcp_conn **, SOCKET);
extern void nni_win_tcp_conn_set_addrs(
    nni_tcp_conn *, const SOCKADDR_STORAGE *, const SOCKADDR_STORAGE *);

extern int  nni_win_io_sysinit(void);
extern void nni_win_io_sysfini(void);

extern int  nni_win_iocp_sysinit(void);
extern void nni_win_iocp_sysfini(void);

extern int  nni_win_ipc_sysinit(void);
extern void nni_win_ipc_sysfini(void);

extern int  nni_win_tcp_sysinit(void);
extern void nni_win_tcp_sysfini(void);

extern int  nni_win_udp_sysinit(void);
extern void nni_win_udp_sysfini(void);

extern int  nni_win_resolv_sysinit(void);
extern void nni_win_resolv_sysfini(void);

extern int  nni_win_io_init(nni_win_io *, nni_win_io_cb, void *);
extern void nni_win_io_fini(nni_win_io *);

extern int nni_win_io_register(HANDLE);

extern int nni_win_sockaddr2nn(nni_sockaddr *, const SOCKADDR_STORAGE *);
extern int nni_win_nn2sockaddr(SOCKADDR_STORAGE *, const nni_sockaddr *);

#define NNG_PLATFORM_DIR_SEP "\\"

#endif // NNG_PLATFORM_WINDOWS

#endif // PLATFORM_WIN_IMPL_H