This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
kqueue-connect.c
165 lines (140 loc) · 3.73 KB
/
kqueue-connect.c
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
/* Kernel Queue The Complete Guide: kqueue-connect.c: HTTP/1 client
Usage:
$ nc -l 127.0.0.1 64000
$ ./kqueue-connect
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/event.h>
int kq;
int quit;
// the structure associated with a socket descriptor
struct context {
int sk;
void (*rhandler)(struct context *obj);
void (*whandler)(struct context *obj);
int data_offset;
};
void obj_write(struct context *obj);
void obj_read(struct context *obj);
void obj_prepare(struct context *obj)
{
// create and prepare socket
obj->sk = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
assert(obj->sk != -1);
int val = 1;
assert(0 == setsockopt(obj->sk, 0, TCP_NODELAY, (char*)&val, sizeof(int)));
// attach socket to KQ
struct kevent events[2];
EV_SET(&events[0], obj->sk, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, obj);
EV_SET(&events[1], obj->sk, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, obj);
assert(0 == kevent(kq, events, 2, NULL, 0, NULL));
}
void obj_connect(struct context *obj)
{
if (obj->whandler == NULL) {
// begin asynchronous connection
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = ntohs(64000);
char ip4[] = {127,0,0,1};
*(int*)&addr.sin_addr = *(int*)ip4;
int r = connect(obj->sk, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
if (r == 0) {
// connection completed successfully
} else if (errno == EINPROGRESS) {
// connection is in progress
obj->whandler = obj_connect;
return;
} else {
assert(0); // fatal error
}
} else {
assert(errno == 0); // connection is successful
obj->whandler = NULL; // we don't want any more signals from KQ
}
printf("Connected\n");
obj_write(obj);
}
void obj_write(struct context *obj)
{
const char data[] = "GET / HTTP/1.1\r\nHost: hostname\r\nConnection: close\r\n\r\n";
int r = send(obj->sk, data + obj->data_offset, sizeof(data)-1 - obj->data_offset, 0);
if (r > 0) {
// sent some data
obj->data_offset += r;
if (obj->data_offset != sizeof(data)-1) {
// we need to send the complete request
obj_write(obj);
return;
}
obj->whandler = NULL;
} else if (r < 0 && errno == EAGAIN) {
// the socket's write buffer is full
obj->whandler = obj_write;
return;
} else {
assert(0); // fatal error
}
printf("Sent HTTP request. Receiving HTTP response...\n");
obj_read(obj);
}
void obj_read(struct context *obj)
{
char data[64*1024];
int r = recv(obj->sk, data, sizeof(data), 0);
if (r > 0) {
// received some data
printf("%.*s", r, data);
obj_read(obj);
return;
} else if (r == 0) {
// server has finished sending data
} else if (r < 0 && errno == EAGAIN) {
// the socket's read buffer is empty
obj->rhandler = obj_read;
return;
} else {
assert(0); // fatal error
}
quit = 1;
}
void main()
{
// create KQ object
kq = kqueue();
assert(kq != -1);
struct context obj = {};
obj_prepare(&obj);
obj_connect(&obj);
// wait for incoming events from KQ and process them
while (!quit) {
struct kevent events[1];
struct timespec *timeout = NULL; // wait indefinitely
int n = kevent(kq, NULL, 0, events, 1, timeout);
if (n < 0 && errno == EINTR)
continue; // kevent() interrupts when UNIX signal is received
assert(n > 0);
// now process each signalled event
for (int i = 0; i != n; i++) {
struct context *o = events[i].udata;
errno = 0;
if (events[i].flags & EV_EOF)
errno = events[i].fflags;
if (events[i].filter == EVFILT_READ
&& o->rhandler != NULL)
o->rhandler(o); // handle read event
if (events[i].filter == EVFILT_WRITE
&& o->whandler != NULL)
o->whandler(o); // handle write event
}
}
close(obj.sk);
close(kq);
}