;; Network.cake: Provide easy access to the sockets API (export (comptime-cond ('Windows (c-import "" "" "")) (true (c-import "" "" "" "" "" "" ""))) (def-type-alias-global socket-type int)) (comptime-cond ('Windows ;; For WORD, MAKEWORD (c-import ""))) (defun network-initialize (&return int) (comptime-cond ('Windows (var requested-version WORD (MAKEWORD 2 2)) (var sockets-data WSADATA) (return (WSAStartup requested-version (addr sockets-data))))) (return 0)) (defun network-shutdown () (comptime-cond ('Windows (WSACleanup)))) (comptime-cond ('Windows (import "CHelpers.cake") (def-c-function-alias close closesocket) ;; TODO: This sucks; I should use dedicated read-socket functions to not break file reads (defmacro read (socket any buffer any buffer-size any) (tokenize-push output (recv (token-splice socket) (type-cast (token-splice buffer) (* char)) (token-splice buffer-size) 0)) (return true)) (defmacro write (socket any buffer any buffer-size any) (tokenize-push output (send (token-splice socket) (type-cast (token-splice buffer) (* (const char))) (token-splice buffer-size) 0)) (return true)) (add-static-link-objects "Ws2_32.lib")))