Skip to content

Instantly share code, notes, and snippets.

@anxiousmodernman
Last active July 4, 2018 00:07
Show Gist options
  • Save anxiousmodernman/606a6fd66aeca4d258a49521bed6568d to your computer and use it in GitHub Desktop.
Save anxiousmodernman/606a6fd66aeca4d258a49521bed6568d to your computer and use it in GitHub Desktop.
server.tcl
#!/usr/bin/env jimsh
set SOCKET /tmp/ipc.sock
set client [socket unix $SOCKET]
set msg [list db put foo baz]
variable msg2 {
db put blah 1
db put blah 99
db get blah
}
puts [list $msg2]
$client puts $msg2
$client flush
$client gets reply
puts $reply
$client gets reply
puts $reply
$client gets reply
puts $reply
$client close
#!/usr/bin/env jimsh
package require sled
sled db /tmp/sled-jimtcl.db
set SOCKET /tmp/ipc.sock
if {[file exists $SOCKET]} {
puts "Error: socket exists. Removing..."
file delete $SOCKET
}
proc serve {path} {
variable f {}
set f [socket unix.server $path]
$f readable {
set client [$f accept]
puts "accepting conn..."
set cmds [list]
while {[$client gets buf] > -1} {
if {[string length [string trim $buf]] > 0} {
puts "received: $buf"
set computed [eval $buf]
puts "computed: $computed"
$client puts $computed
}
}
$client close
}
# We block here.
# A call to `vwait` enters the eventloop. `vwait` processes
# events until the named (global) variable changes or all
# event handlers are removed. The variable need not exist
# beforehand. If there are no event handlers defined, `vwait`
# returns immediately.
vwait done
}
signal handle SIGINT
try -signal {
serve $SOCKET
vwait done
} on signal {sig} {
# we aren't checking what $sig is, since we only handle SIGINT
file delete $SOCKET
puts "\nGoodbye."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment