Search code examples
cmultithreadingglibasyncsocketgio

Glib/Gio Asynchronous or Threaded UDP Server


I have currently a synchronous UDP application receiving messages.

The code :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>
#include <gio/gio.h>


int main(argc,argv)
int argc;
char ** argv;{


char buf[256], *ptr, sep[] = "| ";
GError * error = NULL;


  GSocket * socket;
  GSocketAddress *gsockAddr, *gfromAddr;
  guint16 udp_port = 1500;
  //Creates socket udp ipv4
  socket = g_socket_new(G_SOCKET_FAMILY_IPV4,
                    G_SOCKET_TYPE_DATAGRAM,
                    G_SOCKET_PROTOCOL_UDP,
                    &error);
  g_assert(error == NULL);

  if (socket == NULL) {
    g_print("ERROR");
    exit(1);
  }
  //sockaddr struct like
  gsockAddr = G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port));

  if(gsockAddr == NULL){
    g_error("Error socket\n");
    exit(1);
  }
//
if (g_socket_bind (socket, gsockAddr, TRUE, NULL) == FALSE){

  g_print("Error bind\n");
  exit(1);

}

int bytes = g_socket_receive_from (socket,
                       &gfromAddr,
                       buf,
                       255,
                       NULL,
                       &error);

if (bytes == -1) {
  g_warning ("Failed to receive from socket: %s", error->message);
  g_error_free (error);
  return TRUE;
}

g_message("Server receive: %s", buf);


guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(gfromAddr));

g_print("...from    %s(%d)\n",g_inet_address_to_string(g_inet_socket_address_get_address(G_INET_SO    CKET_ADDRESS(gfromAddr))), (int) port);

exit(0);

}

So, I want to make the receive operation, non-blocking instead of blocking. I want to make it either ansynchronous, or/and threaded so that, meanwhile, I could do other operations related to the application I want to develop.

But I did not suceed to make it like I want. I tried to use GLib IO Channels, but I can not make it works. The processus is waiting, but only because of the Main Loop (I can not telnet the application).

The code :

#include <gio/gio.h>
#include <glib.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BLOCK_SIZE 1024

static gboolean
gio_read_socket (GIOChannel     *channel,
                 GIOCondition    condition,
                 gpointer        data)
{

    char buf[1024];
    gsize bytes_read;
    GError *error = NULL;

    if (condition & G_IO_HUP) return FALSE; /* this channel is done */

    g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read,
            &error);
    g_assert (error == NULL);

    buf[bytes_read] = '\0';

    g_print ("%s", buf);

    return TRUE;
}

int
main (int argc, char **argv)
{

  GSocket * s_udp;
  GError *err = NULL;
  guint16 udp_port = 5556;

  s_udp = g_socket_new(G_SOCKET_FAMILY_IPV4,
                    G_SOCKET_TYPE_DATAGRAM,
                    G_SOCKET_PROTOCOL_UDP,
                    &err);
  g_assert(err == NULL);

  if (s_udp == NULL) {
    g_print("ERROR");
    exit(1);
  }

  g_socket_bind(s_udp,
               G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port)),
              TRUE,
              &err);

  g_assert(err == NULL);

  int fd = g_socket_get_fd(s_udp);
  GIOChannel* channel = g_io_channel_unix_new(fd);
  guint source = g_io_add_watch(channel, G_IO_IN,
                              (GIOFunc) gio_read_socket, NULL);


  g_io_channel_unref(channel);

  GMainLoop *loop = g_main_loop_new(NULL, FALSE);

  g_main_loop_run(loop);
  g_main_loop_unref(loop);

}

I am quite a beginner with GLib/Gio, and I think I am doing wrong with the IO Channels. I would like to add it to the main loop as an event, so that I could use my callback function. Maybe there is a simpler way to do that.

Besides, I have a TCP asynchronous and threaded server that is working, but I did not find how to do the same with UDP (using a GThreadedSocketService and creating a socket listener, then adding the service to the main loop. Easy as pie with TCP).

Do you have any idea how to proceed ? If you know how to do but only with the basic API socket, I still take it ! Thanks.


Solution

  • I figure it out.

    I am indeed quite a beginner. Because, when I wanted to test my udp application (the second code block), I used telnet to connect to it and try to send messages. However, we can not telnet udp applications of course...

    So I tried with a simple udp sender (I used Glib/Gio for it by the way) instead of telnet and it worked, perfectly non-blocking and reusable. I did make some changes but basically, it is the same. I put an idle function to show you how non-blocking it is, whether this can help someone one day.

    My simple Glib/Gio UDP app, non blocking :

    #include <gio/gio.h>
    #include <glib.h>
    
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define BLOCK_SIZE 1024
    
    static gboolean
    gio_read_socket (GIOChannel     *channel,
                     GIOCondition    condition,
                     gpointer        data)
    {
    
        char buf[1024];
        gsize bytes_read;
        GError *error = NULL;
    
        if (condition & G_IO_HUP) return FALSE; /* this channel is done */
    
        g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read,
                &error);
        g_assert (error == NULL);
    
        buf[bytes_read] = '\0';
    
        g_print ("%s", buf);
    
        int *a = data;
        *a = *a + 1;
    
        return TRUE;
    }
    
    gboolean
    idleCpt (gpointer user_data){
    
       int *a = user_data;
       g_print("%d\n", *a);
       sleep(1);
    
       return TRUE;
    }
    int
    main (int argc, char **argv)
    {
    
      GSocket * s_udp;
      GError *err = NULL;
      int idIdle = -1, dataI = 0;
      guint16 udp_port = 1505;
      GSocketAddress * gsockAddr = G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port));
      s_udp = g_socket_new(G_SOCKET_FAMILY_IPV4,
                        G_SOCKET_TYPE_DATAGRAM,
                        G_SOCKET_PROTOCOL_UDP,
                        &err);
      g_assert(err == NULL);
    
      if (s_udp == NULL) {
        g_print("ERREUR");
        exit(1);
      }
    
    
      if (g_socket_bind (s_udp, gsockAddr, TRUE, NULL) == FALSE){
    
       g_print("Erreur bind\n");
       exit(1);
    
      }
    
      g_assert(err == NULL);
    
      int fd = g_socket_get_fd(s_udp);
    
      GIOChannel* channel = g_io_channel_unix_new(fd);
      guint source = g_io_add_watch(channel, G_IO_IN,
                                  (GIOFunc) gio_read_socket, &dataI);
    
      g_io_channel_unref(channel);
    
    
       GMainLoop *loop = g_main_loop_new(NULL, FALSE);
       idIdle = g_idle_add(idleCpt, &dataI);
       g_main_loop_run(loop);
    }
    

    The code is not perfect, there is a lot of optimisations to make, but we can do nice things from that I think. If you want to see my udp sender, just ask.