summaryrefslogtreecommitdiff
path: root/net9p.c
blob: 6587498e202d89efda992505b55ea69feac7ccfa (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
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "net9p.h"

void net9p_listen_cr(void *_arg) {
	(void)_arg;
	printf("listen initializing...\n");
	cr_begin();
	printf("listen running...\n");

	union {
		struct sockaddr_in in;
		struct sockaddr gen;
	} addr;
	memset(&addr, 0, sizeof addr);
	addr.in.sin_family = AF_INET;
	addr.in.sin_port = htons(9001);
	
	int fd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0);
	if (fd < 0)
		error(1, errno, "socket");
	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
		error(1, errno, "setsockopt");
	if (bind(fd, &addr.gen, sizeof addr) < 0)
		error(1, errno, "bind");
	if (listen(fd, 5) < 0)
		error(1, errno, "listen");

	int conn = 9;
	if (!coroutine_add(net9p_worker_cr, &conn))
		error(1, 0, "coroutine_add(net9p_worker_cr, &%d)", conn);
	printf("im back...\n");
	for (int i = 0; i < 10; i++) {
		cr_yield();
	}

	cr_end();
}

void net9p_worker_cr(void *_arg) {
	int fd = *((int *)_arg);
	printf("worker %zu initializing...\n", cr_getcid());
	cr_begin();

	printf("worker %zu running...\n", cr_getcid());
	//close(fd);

	cr_end();
}