It is currently Mon Dec 17, 2018 11:33 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Trying to convert some c code ...
PostPosted: Wed Nov 07, 2018 2:02 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jun 06, 2003 2:41 pm
Posts: 175
Location: Belgium
Hi,

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

Code:
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:
PrototypeC.i sg_httpres_sendbinary(*res, *buf, size, *content_type, status.l)


but it isn't working.

Thanks

_________________

LuckyLuke


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Wed Nov 07, 2018 2:30 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 293
Location: Germany, Homburg (Saar)
Try this:
Code:
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.

_________________
Electronics, Crazy & Interesting Stuff, all that with text, image and sound? Click here!

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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Wed Nov 07, 2018 3:13 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jun 06, 2003 2:41 pm
Posts: 175
Location: Belgium
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Wed Nov 07, 2018 5:27 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 293
Location: Germany, Homburg (Saar)
Maybe the library works with threads. In this case I would recommend to enable the thread-safe flag in the compiler options.

_________________
Electronics, Crazy & Interesting Stuff, all that with text, image and sound? Click here!

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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Wed Nov 07, 2018 8:04 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4051
Location: Germany
I would use:
Code:
PrototypeC.l Protosg_httpres_sendbinary(*res, *buf, size.l, content_type.p-utf8, status.l)


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Sun Nov 11, 2018 1:36 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jun 06, 2003 2:41 pm
Posts: 175
Location: Belgium
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:
/**
 * 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


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Sun Nov 11, 2018 9:28 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3346
Location: New Zealand
Declare it as procedureC
Code:
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

_________________
Got winter blues?
Enjoy a Caravan Trip into, "The Land of Grey and Pink", wine and punk weed optional!
https://www.youtube.com/watch?v=9hmFzGTxod4


Top
 Profile  
Reply with quote  
 Post subject: Re: Trying to convert some c code ...
PostPosted: Mon Nov 12, 2018 8:40 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Jun 06, 2003 2:41 pm
Posts: 175
Location: Belgium
It's working fine now. Thank you everyone !

_________________

LuckyLuke


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: useful and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye