LispCookbook / cl-cookbook

The Common Lisp Cookbook
http://lispcookbook.github.io/cl-cookbook/
Other
930 stars 138 forks source link

Sockets tutorial goes on tangents, used `unwind-protect` instead of macros #506

Open bpseudopod opened 1 year ago

bpseudopod commented 1 year ago

The sockets tutorial is kind of a mess to read. Both sections are weighed down with an extended tangent about the "mistakes" that the author encountered, which are confusing to read and can't be much more helpful than simply explaining how to use the library correctly.

Additionally, the code on display ignores the convenience macros that usocket offers, such as instead implementing equivalent functionality with unwind-protect, requiring that the reader think about conditions and control flow when they should be focusing on the socket API.

This section should be rewritten to be more clear and direct, and should prefer the with- macros defined by the usocket package to unwind-protect.

I'll probably give a shot at rewriting it soon.

NicoSimoski commented 1 month ago

I also stumbled over the unwind-protect and considered a rewrite of the socket-tutorial. The with-macros don't seem to belong to the API though: https://usocket.common-lisp.dev/api-docs.shtml. Are they considered stable or is there a more recent documentation? Here are the functions for TCP without unwind-protect:

(defun create-server (port)
  (usocket:with-socket-listener (socket "127.0.0.1" port)
    (let ((connection (usocket:socket-accept socket :element-type 'character)))
      (format (usocket:socket-stream connection) "Hello World!~%")
      (force-output (usocket:socket-stream connection)))))

(defun create-client (port)
  (usocket:with-client-socket (socket stream "127.0.0.1" port :element-type 'character)
    (usocket:wait-for-input socket)
    (format t "Input is: ~a~%" (read-line stream))))