GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

;; Network.cake: Provide easy access to the sockets API
(export
(comptime-cond
('Windows
(c-import "<string.h>" "<winsock2.h>" "<stdio.h>"))
(true
(c-import "<string.h>"
"<sys/socket.h>" "<sys/types.h>" "<unistd.h>"
"<netinet/tcp.h>" "<netinet/in.h>" "<netdb.h>")))
(def-type-alias-global socket-type int))
6 months ago
(comptime-cond
('Windows
;; For WORD, MAKEWORD
(c-import "<windows.h>")))
(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")))