Gio::SocketClient examples

Can somebody explain how to work with async sockets API?

  1. What exactly method connect_to_uri_finish does, when it callback function already running on request completed. Looks that it wanted just to get data from Gio::AsyncResult argument or/and send some signal to the parental class, that indicates to other members the request has been processed by user.

  2. Must I apply similar methods to get_output_stream()->write_async and get_input_stream()->read_async results?

  3. How to read_async by length or line by line?

  4. What is the difference between read_async and read_async_all - seems that first one useful for #3 but read_async returns entire the response yet

Thanks for any examples or the tutorial links!

With all the GIO async functions, you can think of similar to async futures. With the _async() function you usually schedule a task, the call-back gives you the notification that the task is done and with finish() you retrieve the result of the scheduled operation. Just because you received a call-back, you most of the times do not know if the task was successfully done or had an error. So while some tasks may be fine to be run without calling finish() or even without any call-back at all passed to _async(), you usually want to check.

To read by line, you need to wrap the InputStream in an DataInputStream (glibmm: Gio::DataInputStream Class Reference), which provides you with several _read_line() methods.

1 Like