What should I do on FFI side to achieve the same? I tried to just allocate some memory (via malloc) and pass a pointer to that instead but it doesn't seem to work. I also tried to create hopefully correct struct! and pass addr-of to func but it doesn't seem to get set. In first case I can't know for sure, since I can't see what the address holds after the call, but the later c function that uses the addr behaves as if it weren't correctly set.
... I have a function that you give pointer to char and it sets the string to it. Is there a way to get to the containing c-string on that address from rebol side after it sets it?
int uv_ip4_name(const struct sockaddr_in* src, char* dst, size_t size)
But I see some new things in your example about struct and a buffer which might help me understand this.
... I figured out (2) at least in this case. the trick was making a buffer out of struct and a func I never saw before values of. Haven't figured out what reflect does, although I suspect. It creates a core dump when I try to use it.
name: make struct! [s [uint8 [128]]]
uv-ip4-name addr-of addr addr-of name 128
print name
print to-string values of name
//edit
first problem is solved too. The problem was that ubuntu has old libuv .. I recompiled the latest one myself and now binding to port works.
I wanted to play with libuv for a while already. It has more multiplatform functionalites than just networking. And since @hostilefork said that rebol networking could be improved I wanted to see what style of API I would be getting to if I start from totally different perspective.
Currently I get negative length at read callback, and can't figure out why. A special variant of question no. (2) is again actual for another case:
const char* uv_strerror(int err);
returns the description of error as a pointer. How can I turn this to string to print it in rebol? "Values of" doesn't work on this. I tried to set return value to struct, "uv-err-name: make-routine libuv "uv_err_name" [ n [ int32 ] return: [ struct! [s [uint8 [128]]] ] ]" but I don't get what I would like to. I can't find in source code of libev what -105 should mean. It looks error codes aren't defined in classic way.
The problem maybe starts one callback before: alloc-buffer is called before on_read like this:
Glad you're looking into this and are such a self-starter.
I'm a little busy right now with New Years and travel to really be able to help the next few days. But I will say my opinion that the way to go for most of these bindings/extensions is probably to avoid FFI, if you are comfortable with C. The way we're leaning is to let you establish a module as some C code and some Rebol code.
For example, look at how the ODBC is done. You write a REBNATIVE(your_function) and you can embed the spec in C comments (spacing matters!):
Then, you put together any supporting Rebol code, the text of which is compressed and put in the DLL along with the compiled C:
While the ergonomics of the build system and the exact way this all fits together needs work (imo), the idea is on the right track. There are a lot of advantages of going this route instead of trying to do FFI--your extension will not depend on the FFI (itself an extension, which is mostly factored out). It will be a bit faster. And you'll have a nicer time debugging directly with a C debugger.
As I said, I'm a little busy at the moment. But if this is a real focus for you and you're willing to make some amount of commitment to working on it then I can get involved supporting you on it.