From 29c2c49b078171c2b9d86cdee08475c4742a00a0 Mon Sep 17 00:00:00 2001 From: SongWei Date: Fri, 14 Feb 2020 14:17:10 +1100 Subject: [PATCH] c --- c/CMakeLists.txt | 3 +++ c/lib.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ server.lisp | 45 ++++++++++++++++++++---------------- utils.lisp | 38 ++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 20 deletions(-) create mode 100644 c/CMakeLists.txt create mode 100644 c/lib.c diff --git a/c/CMakeLists.txt b/c/CMakeLists.txt new file mode 100644 index 0000000..8402fac --- /dev/null +++ b/c/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(leoext SHARED lib.c) + +set_target_properties(leoext PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") diff --git a/c/lib.c b/c/lib.c new file mode 100644 index 0000000..4c8e27b --- /dev/null +++ b/c/lib.c @@ -0,0 +1,60 @@ +#include +#include + +__attribute__((dllexport)) void RVExtension(char *output, int outputSize, const char *function); +__attribute__((dllexport)) int RVExtensionArgs(char *output, int outputSize, const char *function, const char **argv, int argc); +__attribute__((dllexport)) void RVExtensionVersion(char *output, int outputSize); + +int strncpy_safe(char *output, const char *src, int size) +{ + int i; + size--; + for (i = 0; i < size && src[i] != '\0'; i++) + { + output[i] = src[i]; + } + output[i] = '\0'; + return i; +} + +void RVExtension(char *output, int outputSize, const char *function) +{ + strncpy_safe(output, function, outputSize); + printf("RVExtention: test call\n"); +} + +int RVExtensionArgs(char *output, int outputSize, + const char *function, + const char **argv, int argc) +{ + CURL *curl; + CURLcode res; + + /* get a curl handle */ + curl = curl_easy_init(); + if(curl) { + /* First set the URL that is about to receive our POST. This URL can + just as well be a https:// URL if that is what should receive the + data. */ + curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:5000/post-test"); + /* Now specify the POST data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "Arma3 request!"); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + + /* always cleanup */ + curl_easy_cleanup(curl); + } + return 0; +} + +void RVExtensionVersion(char *output, int outputSize) +{ + curl_global_init(CURL_GLOBAL_ALL); + strncpy_safe(output, "Leo-Extension v0.1", outputSize); +} diff --git a/server.lisp b/server.lisp index ff86cc4..ad8ba35 100644 --- a/server.lisp +++ b/server.lisp @@ -2,6 +2,7 @@ (ql:quickload :flexi-streams) (ql:quickload :file-types) (ql:quickload :cl-ppcre) +(load "utils.lisp") (defparameter +static-directory+ "~/arma3/static/") @@ -30,6 +31,13 @@ `(200 (:content-type ,mime-string) ,file-path)) (route-not-found)))) +(defun route-display-post (env) + (let* ((decoded-stream + (flex:make-flexi-stream (getf env :raw-body) :external-format :utf-8)) + (result (read-string-stream decoded-stream))) + (format t "~&post body:~A~%" result) + (route-hello-world nil))) + (defun dispatch (request-path dispatch-table) ;; path: request url ;; dispatch-table: '(matcher function) @@ -47,29 +55,26 @@ (defparameter +dispatch-table+ `(("^/$" ,#'route-hello-world) + ("^/post-test$" ,#'route-display-post) (,(concatenate 'string "^" +static-prefix+ ".*$") ,#'serve-static-file) (nil ,#'route-not-found))) -(require :sb-sprof) - -(sb-sprof:with-profiling - (:max-samples 2000 - :report :flat - :loop nil) - (handler-case - (woo:run - (lambda (env) - ;;(print env) - ;;(print (type-of env)) - (if (eq :post (getf env :request-method)) - (let* ((post-stream (getf env :raw-body)) - (char-stream (flexi-streams:make-flexi-stream - post-stream - :external-format :utf-8))))) +(if (not (find-package 'swank)) +(woo:run + (lambda (env) + ;;(print env) + ;;(print (type-of env)) + (if (eq :post (getf env :request-method)) + (let* ((post-stream (getf env :raw-body)) + (char-stream (flexi-streams:make-flexi-stream + post-stream + :external-format :utf-8))))) ;(format t (read-line char-stream)))) ;(format t "dispatching...") - (let ((route-function (dispatch (getf env :request-uri) +dispatch-table+))) - ;(format t "select route: ~A~%" route-function) - (funcall route-function env)))) - (condition () nil))) + (let ((route-function (dispatch (getf env :request-uri) +dispatch-table+))) + (format t "method:~A uri:~A route: ~A~%" + (getf env :request-method) + (getf env :request-uri) + route-function) + (funcall route-function env))))) diff --git a/utils.lisp b/utils.lisp index 2c9623d..f1fbeed 100644 --- a/utils.lisp +++ b/utils.lisp @@ -4,3 +4,41 @@ :element-type '(unsigned-byte 8)))) (read-sequence buffer stream) buffer))) + +(defun make-dynamic-string (&optional length) + (make-array length :adjustable t + :fill-pointer 0 + :element-type 'character)) +;; (let ((s (make-dynamic-string))) +;; (vector-push-extend #\a s) +;; (vector-push-extend #\b s) +;; s) + +(defmacro mvb-let* (bindings &body body) + (let* ((exp (car bindings)) + (vars (butlast exp)) + (multi-val-exp (car (last exp))) + (rest-bindings (cdr bindings))) + (if rest-bindings + `(multiple-value-bind ,vars ,multi-val-exp + (mvb-let* ,rest-bindings ,@body)) + `(multiple-value-bind ,vars ,multi-val-exp + ,@body)))) +;; (defun test-binding-0 () +;; (values 1 2)) +;; (defun test-binding-1 () +;; (values 1 2 3)) +;; (mvb-let* ((a b (test-binding-0)) +;; (c d e (test-binding-1))) +;; (list a b c d e)) + +(defun read-string-stream (stream) + (let* ((buffer (make-string 5)) + (result-list '())) + (do ((read-count (read-sequence buffer stream) + (read-sequence buffer stream))) + ((= read-count 0) nil) + (push (subseq buffer 0 read-count) result-list)) + (format nil "~{~a~}" (reverse result-list)))) +;; (with-input-from-string (s "0123456789012") +;; (read-string-stream s))