]> saetta.ns0.it Git - libglib-mldonkey/commitdiff
Added function ::connect.
authorAndrea Zagli <andrea.zagli@email.it>
Wed, 19 Aug 2015 14:53:53 +0000 (16:53 +0200)
committerAndrea Zagli <andrea.zagli@email.it>
Wed, 19 Aug 2015 14:53:53 +0000 (16:53 +0200)
src/glib-mldonkey.c
src/glib-mldonkey.h
tests/glib-mldonkey.c

index f9d695a2ec35d3e19879bb1d9e2c86da4481290d..d0e3a41b33ac561728a6df171f643d1b63b1c98d 100644 (file)
@@ -45,12 +45,15 @@ static void glib_mldonkey_get_property (GObject *object,
                                GValue *value,
                                GParamSpec *pspec);
 
+static void glib_mldonkey_dump_reply (GlibMldonkey *glibmldonkey);
+
 #define GLIB_MLDONKEY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GLIB_TYPE_MLDONKEY, GlibMldonkeyPrivate))
 
 typedef struct _GlibMldonkeyPrivate GlibMldonkeyPrivate;
 struct _GlibMldonkeyPrivate
        {
-               gpointer nothing;
+               GSocketClient *sclient;
+               GSocketConnection *sconn;
        };
 
 G_DEFINE_TYPE (GlibMldonkey, glib_mldonkey, G_TYPE_OBJECT)
@@ -90,6 +93,8 @@ glib_mldonkey_init (GlibMldonkey *glib_mldonkey)
 {
        GlibMldonkeyPrivate *priv = GLIB_MLDONKEY_GET_PRIVATE (glib_mldonkey);
 
+       priv->sclient = NULL;
+       priv->sconn = NULL;
 }
 
 /**
@@ -142,6 +147,43 @@ GlibMldonkey
        return glib_mldonkey;
 }
 
+gboolean
+glib_mldonkey_connect (GlibMldonkey *glibmldonkey, const gchar *host_port)
+{
+       GError *error;
+
+       GlibMldonkeyPrivate *priv = GLIB_MLDONKEY_GET_PRIVATE (glibmldonkey);
+
+       priv->sclient = g_socket_client_new ();
+       g_socket_client_set_enable_proxy (priv->sclient, FALSE);
+
+       error = NULL;
+       priv->sconn = g_socket_client_connect_to_host (priv->sclient,
+                                                                                                  host_port,
+                                                                                                  4001,
+                                                                                                  NULL,
+                                                                                                  &error);
+       if (priv->sconn == NULL
+               || error != NULL)
+               {
+                       g_warning ("Unable to connect to «%s»: %s.",
+                                          host_port,
+                                          error != NULL && error->message != NULL ? error->message : "no details");
+                       g_object_unref (priv->sclient);
+                       return FALSE;
+               }
+
+       if (g_socket_connection_is_connected (priv->sconn))
+               {
+                       glib_mldonkey_dump_reply (glibmldonkey);
+                       return TRUE;
+               }
+       else
+               {
+                       return FALSE;
+               }
+}
+
 /* PRIVATE */
 static void
 glib_mldonkey_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
@@ -170,3 +212,57 @@ glib_mldonkey_get_property (GObject *object, guint property_id, GValue *value, G
                                break;
                }
 }
+
+static void
+glib_mldonkey_dump_reply (GlibMldonkey *glibmldonkey)
+{
+       GInputStream *istream;
+       guint32 buf;
+       GError *error;
+
+       GlibMldonkeyPrivate *priv = GLIB_MLDONKEY_GET_PRIVATE (glibmldonkey);
+
+       istream = g_io_stream_get_input_stream (G_IO_STREAM (priv->sconn));
+
+       /* read first 4 byte, the content size */
+       g_input_stream_read_all (istream,
+                                                        &buf,
+                                                        4,
+                                                        NULL,
+                                                        NULL,
+                                                        &error);
+       g_message ("content size: %d",GLONG_FROM_LE(buf));
+
+       /* read next 2 byte, the function opcode */
+       g_input_stream_read_all (istream,
+                                                        &buf,
+                                                        2,
+                                                        NULL,
+                                                        NULL,
+                                                        &error);
+       g_message ("function opcode: %d",GLONG_FROM_LE(buf));
+
+       /* function 0 */
+       g_input_stream_read_all (istream,
+                                                        &buf,
+                                                        4,
+                                                        NULL,
+                                                        NULL,
+                                                        &error);
+       g_message ("maximum protocol version: %d",GLONG_FROM_LE(buf));
+       g_input_stream_read_all (istream,
+                                                        &buf,
+                                                        4,
+                                                        NULL,
+                                                        NULL,
+                                                        &error);
+       g_message ("maximum opcode gui: %d",GLONG_FROM_LE(buf));
+       g_input_stream_read_all (istream,
+                                                        &buf,
+                                                        4,
+                                                        NULL,
+                                                        NULL,
+                                                        &error);
+       g_message ("maximum opcode core: %d",GLONG_FROM_LE(buf));
+
+}
index 9379df900c48bc339e7d346b57210b2e19026428..72466cd17b4c9ec572bef107c5a7e59c87e7955e 100644 (file)
@@ -58,6 +58,8 @@ GType glib_mldonkey_get_type (void) G_GNUC_CONST;
 
 GlibMldonkey *glib_mldonkey_new (void);
 
+gboolean glib_mldonkey_connect (GlibMldonkey *glibmldonkey, const gchar *host_port);
+
 
 G_END_DECLS
 
index 7e490526a93da79c36a3faf5d380a3c9ee2077cd..49d7ddf2f46a70f1ccdc4995bad620fe89a855b2 100644 (file)
 int
 main (int argc, char **argv)
 {
+       GlibMldonkey *glibm;
+
+       glibm = glib_mldonkey_new ();
+       glib_mldonkey_connect (glibm, "10.3.4.200:4001");
 
        return 0;
 }