From: Andrea Zagli Date: Wed, 19 Aug 2015 14:53:53 +0000 (+0200) Subject: Added function ::connect. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=a095a7f741fdd5f12c49a544a8f1db7226c1128d;p=libglib-mldonkey Added function ::connect. --- diff --git a/src/glib-mldonkey.c b/src/glib-mldonkey.c index f9d695a..d0e3a41 100644 --- a/src/glib-mldonkey.c +++ b/src/glib-mldonkey.c @@ -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)); + +} diff --git a/src/glib-mldonkey.h b/src/glib-mldonkey.h index 9379df9..72466cd 100644 --- a/src/glib-mldonkey.h +++ b/src/glib-mldonkey.h @@ -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 diff --git a/tests/glib-mldonkey.c b/tests/glib-mldonkey.c index 7e49052..49d7ddf 100644 --- a/tests/glib-mldonkey.c +++ b/tests/glib-mldonkey.c @@ -23,6 +23,10 @@ int main (int argc, char **argv) { + GlibMldonkey *glibm; + + glibm = glib_mldonkey_new (); + glib_mldonkey_connect (glibm, "10.3.4.200:4001"); return 0; }