Sign up

Gio Async read/write of sockets compilation issue

Using a Gio.SocketClient one can connect asynchronously to a server using connectAsync. This requires supplying a function with the signature extern(C) void connectionCallback(GObject* object, GAsyncResult* result, void * userData). This function is supposed to call connectFinish to complete the data collection. In C this is easy as the parameter to connectFinish is a GAsyncResult*, exactly the result parameter to connectCallback. However the signature in D requires the parameter be an AsyncResultIF. So the question is how does one create an instance of an AsyncResultIF from a GAsyncResult* in D. The subsidiary question is why does this have to be done in D when it is not needed in C?

Re: Gio Async read/write of sockets compilation issue

On 17-05-2020 18:53, Russel Winder wrote:

Using a Gio.SocketClient one can connect asynchronously to a server using connectAsync. This requires supplying a function with the signature extern(C) void connectionCallback(GObject* object, GAsyncResult* result, void * userData). This function is supposed to call connectFinish to complete the data collection. In C this is easy as the parameter to connectFinish is a GAsyncResult*, exactly the result parameter to connectCallback. However the signature in D requires the parameter be an AsyncResultIF. So the question is how does one create an instance of an AsyncResultIF from a GAsyncResult* in D. The subsidiary question is why does this have to be done in D when it is not needed in C?

The callbacks using the unwrapped c parameters is something that needs
some work.

To get a D object from a C pointer you can use:

ObjectG.getDObject!AsyncResultIF(your_result);

Re: Gio Async read/write of sockets compilation issue

Given the GAsyncResult* parameter result I did:

auto resultData = ObjectG.getDObject!AsyncResultIF(result);
assert(resultData !is null);
writelnUt("@@@@@@  connectionCallback ", resultData.getSourceObject, ", ", resultData.getUserData);

which seems to work fine. :-) However when I call:

auto connection = client.connectFinish(resultData);

I am still getting a SIGSEGV. :-(

It seems that the AsyncResultIF generated from the GAsyncResult* is not a real AsynResultIF.

Re: Gio Async read/write of sockets compilation issue

On 18-05-2020 11:51, Russel Winder wrote:

Given the GAsyncResult* parameter result I did:

auto resultData = ObjectG.getDObject!AsyncResultIF(result);
assert(resultData !is null);
writelnUt("@@@@@@  connectionCallback ", resultData.getSourceObject, ", ", resultData.getUserData);

which seems to work fine. :-) However when I call:

auto connection = client.connectFinish(resultData);

I am still getting a SIGSEGV. :-(

It seems that the AsyncResultIF generated from the GAsyncResult* is not a real AsynResultIF.

It looks like it should work, Is client also working fine.
This being a callback the client may need to be passed in the user_data
parameter.

Re: Gio Async read/write of sockets compilation issue

The SocketClient is, I believe, being passed to the callback via the GObject* sourceObject parameter. Passing it via the userData would, I believe, be duplication.

Re: Gio Async read/write of sockets compilation issue

In Python 3, using PyGobject, the callback signature seems to be:

def connection_callback(client: Gio.SocketClient, task: Gio.Task, user_data):

but maybe there is extra jiggery-pokery going on under the covers to deliver a Task?

Re: Gio Async read/write of sockets compilation issue

Hummm… the core of my problem was the type of sourceObject, I clearly have to convert the GObject* to a SocketClient thus:

auto client = ObjectG.getDObject!SocketClient(cast(GSocketClient*)object);

annoying, but it seems to work. I still have problems in that things are not working, but I suspect it is now more to do with the actual I/O than the D/C interworking.

Re: Gio Async read/write of sockets compilation issue

OK, I think I have it all working. :-)

Re: Gio Async read/write of sockets compilation issue

On 19-05-2020 20:35, Russel Winder wrote:

OK, I think I have it all working. :-)

Great :)