UnixSocketServer

Inherits from: Object

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.

Example
"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
    ]
]
Example
"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
]
Example
"Server with explicit permissions"
| server <UnixSocketServer> |
server := UnixSocketServer listenAt: '/tmp/secure.sock' mode: 8r660

Class Methods

primitives

class primListenAt:
class primListenAtMode:mode:

uncategorized

class listenAt:

Create a server listening at the given filesystem path. Removes stale socket files automatically.

Example
| server <UnixSocketServer> |
server := UnixSocketServer listenAt: '/tmp/test.sock'.
server isRunning   "=> true"
class listenAt:mode:

Create a server listening at path with the given file mode (permissions). Mode is an integer, e.g. 8r660.

Example
| server <UnixSocketServer> |
server := UnixSocketServer listenAt: '/tmp/test.sock' mode: 8r660

Instance Methods

primitives

primAccept
primAcceptToChannel:
primClose
primIsClosed
primIsRunning
primPath

uncategorized

accept

Accept a new client connection. Blocks until a client connects. Returns a SocketConnection.

Example
| conn <SocketConnection> line <String> |
conn := server accept.
line := conn receiveLine.
conn close
acceptToChannel:

Spawn a background goroutine that accepts connections and sends them to the given Channel. Returns self immediately.

Example
| ch <Channel> conn <SocketConnection> |
ch := Channel new: 10.
server acceptToChannel: ch.
conn := ch receive
close

Stop the server and remove the socket file.

Example
server close.
server isClosed   "=> true"
isClosed

True if the server has been closed.

isRunning

True if the server is accepting connections.

path

Return the filesystem path of this server's socket.

Example
(UnixSocketServer listenAt: '/tmp/x.sock') path   "=> '/tmp/x.sock'"