UnixSocketServer
A Unix domain socket server that listens for connections at a filesystem path.
Unix domain sockets provide fast, secure IPC on the same machine. The server listens at a path and accepts incoming connections, returning SocketConnection objects for each client.
"Basic server that echoes lines back to clients"
| server <UnixSocketServer> conn <SocketConnection> line <String> |
server := UnixSocketServer listenAt: '/tmp/echo.sock'.
[server isRunning] whileTrue: [
conn := server accept.
[conn isClosed not] whileTrue: [
line := conn receiveLine.
conn sendLine: line
]
]
"Server with channel-based accept for concurrent handling"
| server <UnixSocketServer> ch <Channel> conn <SocketConnection> |
server := UnixSocketServer listenAt: '/tmp/app.sock'.
ch := Channel new: 10.
server acceptToChannel: ch.
[server isRunning] whileTrue: [
conn := ch receive.
[conn receiveLine] fork
]
"Server with explicit permissions"
| server <UnixSocketServer> |
server := UnixSocketServer listenAt: '/tmp/secure.sock' mode: 8r660
Class Methods
primitives
uncategorized
Create a server listening at the given filesystem path. Removes stale socket files automatically.
| server <UnixSocketServer> |
server := UnixSocketServer listenAt: '/tmp/test.sock'.
server isRunning "=> true"
Create a server listening at path with the given file mode (permissions). Mode is an integer, e.g. 8r660.
| server <UnixSocketServer> |
server := UnixSocketServer listenAt: '/tmp/test.sock' mode: 8r660
Instance Methods
primitives
uncategorized
Accept a new client connection. Blocks until a client connects. Returns a SocketConnection.
| conn <SocketConnection> line <String> |
conn := server accept.
line := conn receiveLine.
conn close
Spawn a background goroutine that accepts connections and sends them to the given Channel. Returns self immediately.
| ch <Channel> conn <SocketConnection> |
ch := Channel new: 10.
server acceptToChannel: ch.
conn := ch receive
Stop the server and remove the socket file.
server close.
server isClosed "=> true"
True if the server has been closed.
True if the server is accepting connections.
Return the filesystem path of this server's socket.
(UnixSocketServer listenAt: '/tmp/x.sock') path "=> '/tmp/x.sock'"