port_listen
lib callCapy
function port_listen(path : string) u64
Description
port_listen() binds a unix socket the service owns and accepts connections on it. Each accepted connection arrives as SERVICE:ACCEPT with port and connection, a port handle of its own. Bytes on an accepted connection arrive as SERVICE:DATA with that connection's handle, and its close arrives as SERVICE:EXIT.
A socket file already present at the path makes the bind fail, so a service removes a stale file before listening and never takes over a live one. The listening port is closed with port_close() or at the end of the service, which removes the file.
Parameters
path : the unix socket path to listen on
Return Values
a port handle, or zero when the socket could not be bound
Errors
A path that cannot be bound yields zero. Outside a service worker the call traps.
Example
Capy
var listener : u64 = 0
function SERVICE:START(props : any)
{
listener = port_listen("/run/bearer/agents.sock")
if listener == 0
{
print("listen failed\n")
}
}
function SERVICE:DATA(props : any)
{
port_write(u64(props.port), string(props.data))
}
function SERVICE:STOP(props : any)
{
port_close(listener)
}
Output
No output