Page 1 of 1

Trying to convert some c code ...

Posted: Wed Nov 07, 2018 2:02 pm
by LuckyLuke
Hi,

Can someone help me with the translation of this c code to purebasic please ?

Code: Select all

int sg_httpres_sendbinary	(	struct sg_httpres * 	res,
void * 	buf,
size_t 	size,
const char * 	content_type,
unsigned int 	status 
)


I've tried to use this

Code: Select all

PrototypeC.i sg_httpres_sendbinary(*res, *buf, size, *content_type, status.l)
but it isn't working.

Thanks

Re: Trying to convert some c code ...

Posted: Wed Nov 07, 2018 2:30 pm
by NicTheQuick
Try this:

Code: Select all

PrototypeC.l sg_httpres_sendbinary(*res, *buf, size.l, *content_type, status.l)
And try also with Prototype instead of PrototypeC.

size_t can be long or integer depending on the compiler implementation as I know.

Re: Trying to convert some c code ...

Posted: Wed Nov 07, 2018 3:13 pm
by LuckyLuke
Thanks for this fast reply ! The command is working now, but another error appeared ...
When the callback function is called, It crashes at the end of the procedure.

I'm trying to see if I can use libsagui (https://github.com/risoflora/libsagui/releases) to implement an embedded webserver.

This code works until you try to visit the url http://localhost:8080/

Code: Select all

PrototypeC.i Protosg_httpsrv_new(*cb, *cls)
PrototypeC.l Protosg_httpres_sendbinary(*res, *buf, size.l, *content_type, status.l)
PrototypeC.b Protosg_httpsrv_listen(*srv, port, tread.b)
PrototypeC.i Protosg_httpreq_method(*req)

Global sg_httpres_send.Protosg_httpres_sendbinary
Global sg_httpreq_method.Protosg_httpreq_method

hHandle = OpenLibrary(#PB_Any, "libsagui-1.dll")
If hHandle
;    ExamineLibraryFunctions(hHandle)
;    While NextLibraryFunction()
;      Debug LibraryFunctionName()  
;    Wend  

  sg_httpsrv_new.Protosg_httpsrv_new =  GetFunction(hHandle, "sg_httpsrv_new")
  sg_httpres_send.Protosg_httpres_sendbinary =  GetFunction(hHandle, "sg_httpres_sendbinary")
  sg_httpsrv_listen.Protosg_httpsrv_listen = GetFunction(hHandle, "sg_httpsrv_listen")
  sg_httpreq_method.Protosg_httpreq_method = GetFunction(hHandle, "sg_httpreq_method")
    
EndIf



Procedure req_cb(*cls, *req, *res)
  a$ = "<html><head><title>Hello world</title></head><body>Hello world</body></html>"
  
  lena = Len(a$)
  b$ = "text/html; charset=utf-8"
  
  
  Debug PeekS(sg_httpreq_method(*req), -1, #PB_UTF8)
  ;Return 0 = ok else error
  Debug  sg_httpres_send(*res, @a$, 0, @b$, 200)

EndProcedure

 *srv = sg_httpsrv_new(@req_cb(), #Null)
 If sg_httpsrv_listen(*srv, 8080, #False) = #False
   Debug "ERROR"
 EndIf
  
 OpenConsole()
 PrintN("Server is running")
 PrintN("this will crash ... http://localhost:8080/")
 Input()
  
 CloseLibrary(hHandle)

Re: Trying to convert some c code ...

Posted: Wed Nov 07, 2018 5:27 pm
by NicTheQuick
Maybe the library works with threads. In this case I would recommend to enable the thread-safe flag in the compiler options.

Re: Trying to convert some c code ...

Posted: Wed Nov 07, 2018 8:04 pm
by infratec
I would use:

Code: Select all

PrototypeC.l Protosg_httpres_sendbinary(*res, *buf, size.l, content_type.p-utf8, status.l)

Re: Trying to convert some c code ...

Posted: Sun Nov 11, 2018 1:36 pm
by LuckyLuke
I tried to use the threadsafe flag, but still no luck ...

I keep on getting Invalid memory access at the Endprocedure line of the req_cb procedure.

Definition of the functions I try to use:

Code: Select all

/**
 * Callback signature used to handle requests and responses.
 * \param[out] cls User-defined closure.
 * \param[out] req Request handle.
 * \param[out] res Response handle.
 */
typedef void (*sg_httpreq_cb)(void *cls, struct sg_httpreq *req, struct sg_httpres *res);
/**
 * Creates a new HTTP server handle.
 * \param[in] cb Callback to handle requests and responses.
 * \param[in] cls User-defined closure.
 * \return New HTTP server handle.
 * \retval NULL If the \pr{cb} is null and sets the `errno` to `EINVAL`.
 */
SG_EXTERN struct sg_httpsrv *sg_httpsrv_new(sg_httpreq_cb cb, void *cls)
__SG_MALLOC;
/**
 * Starts the HTTP server.
 * \param[in] srv Server handle.
 * \param[in] port Port for listening to connections.
 * \param[in] threaded Enable/disable the threaded model. If `true`, the server creates one thread per connection.
 * \return `true` if the server is started, `false` otherwise. If \pr{srv} is null, sets the `errno` to `EINVAL`.
 * \note If port is `0`, the operating system will assign randomly an unused port.
 */
SG_EXTERN bool sg_httpsrv_listen(struct sg_httpsrv *srv, uint16_t port, bool threaded);
/**
 * Sends a binary content to the client.
 * \param[in] res Response handle.
 * \param[in] buf Binary content.
 * \param[in] size Content size.
 * \param[in] content_type `Content-Type` of the content.
 * \param[in] status HTTP status code.
 * \retval 0 - Success.
 * \retval EINVAL - Invalid argument.
 * \retval EALREADY - Operation already in progress.
 * \warning It exits the application if called when no memory space is available.
 */
SG_EXTERN int sg_httpres_sendbinary(struct sg_httpres *res, void *buf, size_t size, const char *content_type,
                                    unsigned int status);
I hope the get this working to create a small rest api server ...

Thanks.

Re: Trying to convert some c code ...

Posted: Sun Nov 11, 2018 9:28 pm
by idle
Declare it as procedureC

Code: Select all

ProcedureC req_cb(*cls, *req, *res)
  a$ = "<html><head><title>Hello world</title></head><body>Hello world</body></html>"
  
  lena = Len(a$)
  b$ = "text/html; charset=utf-8"
  
  
  Debug PeekS(sg_httpreq_method(*req), -1, #PB_UTF8)
  ;Return 0 = ok else error
  Debug  sg_httpres_send(*res, @a$, 0, @b$, 200)

EndProcedure

you could also try civetserver
viewtopic.php?f=27&t=70995

Re: Trying to convert some c code ...

Posted: Mon Nov 12, 2018 8:40 am
by LuckyLuke
It's working fine now. Thank you everyone !