-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline.js
64 lines (60 loc) · 2 KB
/
baseline.js
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
const { epoll } = just.library('epoll')
const { http } = just.library('http')
const { sys } = just.library('sys')
const { net } = just.library('net')
const { parseRequests } = http
const { EPOLLIN, EPOLLERR, EPOLLHUP } = epoll
const { close, recv, send, accept, setsockopt, socket, bind, listen } = net
const { fcntl } = sys
const { loop } = just.factory
const { F_GETFL, F_SETFL } = just.sys
const { IPPROTO_TCP, O_NONBLOCK, TCP_NODELAY, SO_KEEPALIVE, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR, SO_REUSEPORT, SOCK_NONBLOCK } = just.net
function onSocketEvent (fd, event) {
if (event & EPOLLERR || event & EPOLLHUP) {
loop.remove(fd)
close(fd)
return
}
const bytes = recv(fd, buf, 0, 65536)
if (bytes > 0) {
const [remaining, count] = parseRequests(buf, bytes, 0, answer)
if (count > 1) {
if (count > maxPipeline) {
close(fd)
return
}
send(fd, r200, count * r200Len, 0)
return
}
send(fd, r200, r200Len, 0)
return
}
if (bytes < 0) just.error('recv error')
loop.remove(fd)
close(fd)
}
function onConnect (fd, event) {
if (event & EPOLLERR || event & EPOLLHUP) {
loop.remove(fd)
close(fd)
return
}
const newfd = accept(fd)
setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, 0)
setsockopt(newfd, SOL_SOCKET, SO_KEEPALIVE, 0)
loop.add(newfd, onSocketEvent)
const flags = fcntl(newfd, F_GETFL, 0) | O_NONBLOCK
fcntl(newfd, F_SETFL, flags)
loop.update(newfd, EPOLLIN | EPOLLERR | EPOLLHUP)
}
const answer = [0, 0]
const maxPipeline = 1024
const r200 = ArrayBuffer.fromString(`HTTP/1.1 200 OK\r\nServer: j\r\nDate: ${(new Date()).toUTCString()}\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, World!`.repeat(maxPipeline))
const r200Len = r200.byteLength / maxPipeline
const buf = new ArrayBuffer(65536)
const fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 1)
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, 1)
bind(fd, '127.0.0.1', 3000)
listen(fd, 1024)
loop.add(fd, onConnect)