File: dispatch_io_muxed.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (280 lines) | stat: -rw-r--r-- 7,197 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) 2019 Apple Inc. All rights reserved.
 *
 * @APPLE_APACHE_LICENSE_HEADER_START@
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @APPLE_APACHE_LICENSE_HEADER_END@
 */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#elif defined(_WIN32)
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <Windows.h>
#endif

#include <dispatch/dispatch.h>

#include <bsdtests.h>
#include "dispatch_test.h"

#if !defined(_WIN32)
#define closesocket(x) close(x)
#endif

static void
test_file_muxed(void)
{
	printf("\nFile Muxed\n");

#if defined(_WIN32)
	const char *temp_dir = getenv("TMP");
	if (!temp_dir) {
		temp_dir = getenv("TEMP");
	}
	if (!temp_dir) {
		test_ptr_notnull("temporary directory", temp_dir);
		test_stop();
	}
	const char *path_separator = "\\";
#else
	const char *temp_dir = getenv("TMPDIR");
	if (!temp_dir) {
		temp_dir = "/tmp";
	}
	const char *path_separator = "/";
#endif
	char *path = NULL;
	(void)asprintf(&path, "%s%sdispatchtest_io.XXXXXX", temp_dir, path_separator);
	dispatch_fd_t fd = mkstemp(path);
	if (fd == -1) {
		test_errno("mkstemp", errno, 0);
		test_stop();
	}
	if (unlink(path) == -1) {
		test_errno("unlink", errno, 0);
		test_stop();
	}
#if defined(_WIN32)
	free(path);
#endif
	dispatch_test_fd_write(fd, "test", 4);
	dispatch_test_fd_lseek(fd, 0, SEEK_SET);

	dispatch_group_t g = dispatch_group_create();
	dispatch_group_enter(g);
	dispatch_group_enter(g);

	dispatch_source_t reader = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
			(uintptr_t)fd, 0, dispatch_get_global_queue(0, 0));
	test_ptr_notnull("dispatch_source_create", reader);
	assert(reader);
	dispatch_source_set_event_handler(reader, ^{
		dispatch_source_cancel(reader);
	});
	dispatch_source_set_cancel_handler(reader, ^{
		dispatch_release(reader);
		dispatch_group_leave(g);
	});

	dispatch_source_t writer = dispatch_source_create(
			DISPATCH_SOURCE_TYPE_WRITE, (uintptr_t)fd, 0,
			dispatch_get_global_queue(0, 0));
	test_ptr_notnull("dispatch_source_create", writer);
	assert(writer);
	dispatch_source_set_event_handler(writer, ^{
		dispatch_source_cancel(writer);
	});
	dispatch_source_set_cancel_handler(writer, ^{
		dispatch_release(writer);
		dispatch_group_leave(g);
	});

	dispatch_resume(reader);
	dispatch_resume(writer);

	test_group_wait(g);
	dispatch_release(g);
	dispatch_test_fd_close(fd);
}

static void
test_stream_muxed(dispatch_fd_t serverfd, dispatch_fd_t clientfd)
{
	dispatch_group_t g = dispatch_group_create();
	dispatch_group_enter(g);
	dispatch_group_enter(g);

	dispatch_source_t reader = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
			(uintptr_t)serverfd, 0, dispatch_get_global_queue(0, 0));
	test_ptr_notnull("dispatch_source_create", reader);
	assert(reader);
	dispatch_source_set_event_handler(reader, ^{
		dispatch_source_cancel(reader);
	});
	dispatch_source_set_cancel_handler(reader, ^{
		dispatch_release(reader);
		dispatch_group_leave(g);
	});

	dispatch_source_t writer = dispatch_source_create(
			DISPATCH_SOURCE_TYPE_WRITE, (uintptr_t)serverfd, 0,
			dispatch_get_global_queue(0, 0));
	test_ptr_notnull("dispatch_source_create", writer);
	assert(writer);
	dispatch_source_set_event_handler(writer, ^{
		dispatch_source_cancel(writer);
	});
	dispatch_source_set_cancel_handler(writer, ^{
		dispatch_release(writer);
		dispatch_group_leave(g);
	});

	dispatch_resume(reader);
	dispatch_resume(writer);

	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC),
			dispatch_get_global_queue(0, 0), ^{
		dispatch_group_enter(g);
		char buf[512] = {0};
		ssize_t n = dispatch_test_fd_write(clientfd, buf, sizeof(buf));
		if (n < 0) {
			test_errno("write error", errno, 0);
			test_stop();
		}
		test_sizet("num written", (size_t)n, sizeof(buf));
		dispatch_group_leave(g);
	});

	test_group_wait(g);
	dispatch_release(g);
}

static void
test_socket_muxed(void)
{
	printf("\nSocket Muxed\n");

	int listenfd = -1, serverfd = -1, clientfd = -1;
	struct sockaddr_in addr;
	socklen_t addrlen;

	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd == -1) {
		test_errno("socket()", errno, 0);
		test_stop();
	}
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	addr.sin_port = 0;
	addrlen = sizeof(addr);
	if (bind(listenfd, (struct sockaddr *)&addr, addrlen) == -1) {
		test_errno("bind()", errno, 0);
		test_stop();
	}
	if (listen(listenfd, 3) == -1) {
		test_errno("listen()", errno, 0);
		test_stop();
	}
	if (getsockname(listenfd, (struct sockaddr *)&addr, &addrlen) == -1) {
		test_errno("getsockname()", errno, 0);
		test_stop();
	}

	clientfd = socket(AF_INET, SOCK_STREAM, 0);
	if (clientfd == -1) {
		test_errno("socket()", errno, 0);
		test_stop();
	}
	if (connect(clientfd, (struct sockaddr *)&addr, addrlen)) {
		test_errno("connect()", errno, 0);
		test_stop();
	}

	serverfd = accept(listenfd, (struct sockaddr *)&addr, &addrlen);
	if (serverfd == -1) {
		test_errno("accept()", errno, 0);
		test_stop();
	}

	test_stream_muxed((dispatch_fd_t)serverfd, (dispatch_fd_t)clientfd);

	closesocket(clientfd);
	closesocket(serverfd);
	closesocket(listenfd);
}

#if defined(_WIN32)
static void
test_pipe_muxed(void)
{
	printf("\nDuplex Pipe Muxed\n");

	wchar_t name[64];
	swprintf(name, sizeof(name), L"\\\\.\\pipe\\dispatch_io_muxed_%lu",
			GetCurrentProcessId());
	HANDLE server = CreateNamedPipeW(name,
			PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_TYPE_BYTE,
			/* nMaxInstances */ 1, /* nOutBufferSize */ 0x1000,
			/* nInBufferSize */ 0x1000, /* nDefaultTimeOut */ 0,
			/* lpSecurityAttributes */ NULL);
	if (server == INVALID_HANDLE_VALUE) {
		test_ptr_not("CreateNamedPipe", server, INVALID_HANDLE_VALUE);
		test_stop();
	}
	HANDLE client = CreateFileW(name, GENERIC_READ | GENERIC_WRITE,
			/* dwShareMode */ 0, /* lpSecurityAttributes */ NULL, OPEN_EXISTING,
			/* dwFlagsAndAttributes */ 0, /* hTemplateFile */ NULL);
	if (client == INVALID_HANDLE_VALUE) {
		test_ptr_not("CreateFile", client, INVALID_HANDLE_VALUE);
		test_stop();
	}

	test_stream_muxed((dispatch_fd_t)server, (dispatch_fd_t)client);

	CloseHandle(client);
	CloseHandle(server);
}
#endif

int
main(void)
{
	dispatch_test_start("Dispatch IO Muxed");
#if defined(_WIN32)
	WSADATA wsa;
	int err = WSAStartup(MAKEWORD(2, 2), &wsa);
	if (err != 0) {
		fprintf(stderr, "WSAStartup failed with %d\n", err);
		test_stop();
	}
#endif
	dispatch_async(dispatch_get_main_queue(), ^{
		test_file_muxed();
		test_socket_muxed();
#if defined(_WIN32)
		test_pipe_muxed();
#endif
		test_stop();
	});
	dispatch_main();
}