Initial project import

This commit is contained in:
Agila Forge Importer committed 2026-07-13 15:20:32 +00:00
commit 23f2d29e32
11957 files changed
+1754577

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import os
import os.notify
import net
import time
// This example demonstrates a single threaded TCP server using os.notify to be
// notified of events on file descriptors. You can connect to the server using
// netcat in separate shells, for example: `nc localhost 9001`
fn main() {
$if !linux {
eprintln('This example only works on Linux')
exit(1)
}
// create TCP listener
mut listener := net.listen_tcp(.ip, 'localhost:9001')!
defer {
listener.close() or {}
}
addr := listener.addr()!
eprintln('Listening on ${addr}')
eprintln('Type `stop` to stop the server')
// create file descriptor notifier
mut notifier := notify.new()!
defer {
notifier.close() or {}
}
notifier.add(os.stdin().fd, .read)!
notifier.add(listener.sock.handle, .read)!
for {
for event in notifier.wait(time.infinite) {
match event.fd {
listener.sock.handle {
// someone is trying to connect
eprint('trying to connect.. ')
if conn := listener.accept() {
notifier.add(conn.sock.handle, .read | .peer_hangup) or {
eprintln('error adding to notifier: ${err}')
return
}
eprintln('connected')
} else {
eprintln('unable to accept: ${err}')
}
}
0 {
// stdin
s, _ := os.fd_read(event.fd, 10)
if s == 'stop\n' {
eprintln('stopping')
return
}
}
else {
// remote connection
if event.kind.has(.peer_hangup) {
notifier.remove(event.fd) or {
eprintln('error removing from notifier: ${err}')
return
}
eprintln('remote disconnected')
} else {
s, _ := os.fd_read(event.fd, 10)
os.fd_write(event.fd, s)
}
}
}
}
}
}