Trying to convert some c code ...

Just starting out? Need help? Post your questions and find answers here.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Trying to convert some c code ...

Post 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

LuckyLuke
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Trying to convert some c code ...

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Re: Trying to convert some c code ...

Post 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)

LuckyLuke
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Trying to convert some c code ...

Post by NicTheQuick »

Maybe the library works with threads. In this case I would recommend to enable the thread-safe flag in the compiler options.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Trying to convert some c code ...

Post by infratec »

I would use:

Code: Select all

PrototypeC.l Protosg_httpres_sendbinary(*res, *buf, size.l, content_type.p-utf8, status.l)
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Re: Trying to convert some c code ...

Post 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.

LuckyLuke
User avatar
idle
Always Here
Always Here
Posts: 5096
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Trying to convert some c code ...

Post 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
Windows 11, Manjaro, Raspberry Pi OS
Image
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Re: Trying to convert some c code ...

Post by LuckyLuke »

It's working fine now. Thank you everyone !

LuckyLuke
Post Reply