wip: start implementing the packet stream as a giostream subclass

This commit is contained in:
Gergely Polonkai 2019-01-22 15:14:56 +01:00
parent c293bd845e
commit 5622ac7ae9
7 changed files with 244 additions and 1244 deletions

View File

@ -13,5 +13,13 @@ gtk = dependency('gtk+-3.0', version: gtk_required)
json_glib = dependency('json-glib-1.0', version: json_glib_required) json_glib = dependency('json-glib-1.0', version: json_glib_required)
libsodium = dependency('libsodium', version: libsodium_required) libsodium = dependency('libsodium', version: libsodium_required)
confinc = include_directories('.')
cdata = configuration_data()
cdata.set_quoted('GETTEXT_PACKAGE', 'ssb-gtk')
config_h = configure_file(output: 'config.h',
configuration: cdata)
subdir('data') subdir('data')
subdir('ssb-gtk') subdir('ssb-gtk')

View File

@ -3,9 +3,11 @@ sources = [
'ssb-app.c', 'ssb-app.c',
'ssb-window.c', 'ssb-window.c',
'ssb-profile.c', 'ssb-profile.c',
'ssb-packet-stream.c',
'ssb-scuttler.c', 'ssb-scuttler.c',
] ]
executable('ssb-gtk', sources, ssb_resources, executable('ssb-gtk', sources, ssb_resources,
dependencies: [glib, gtk, json_glib, libsodium], dependencies: [glib, gtk, json_glib, libsodium],
include_directories: [confinc],
install: true) install: true)

View File

@ -1,6 +1,6 @@
#include "ssb-app.h" #include "ssb-app.h"
#include "ssb-window.h" #include "ssb-window.h"
#include "sbot.h" #include "ssb-scuttler.h"
struct _SsbApp { struct _SsbApp {
GtkApplication parent_instance; GtkApplication parent_instance;
@ -94,7 +94,8 @@ connect_finished(GObject *source, GAsyncResult *res, gpointer user_data)
SsbScuttler *scuttler = SSB_SCUTTLER(source); SsbScuttler *scuttler = SSB_SCUTTLER(source);
SsbApp *app = user_data; SsbApp *app = user_data;
GError *err = NULL; GError *err = NULL;
gboolean connect_successful = ssb_scuttler_connect_finish(scuttler, res, &err); gboolean
connect_successful = ssb_scuttler_connect_finish(scuttler, res, &err);
ssb_scuttler_whoami_async(scuttler, NULL, whoami_finished, app); ssb_scuttler_whoami_async(scuttler, NULL, whoami_finished, app);
} }

198
ssb-gtk/ssb-packet-stream.c Normal file
View File

@ -0,0 +1,198 @@
#include "config.h"
#include <glib/gi18n-lib.h>
#include "ssb-packet-stream.h"
#define DEFAULT_SHS_CAP_KEY "\xd4\xa1\xcb\x88\xa6\x6f\x02\xf8\xdb\x63\x5c\xe2\x64\x41\xcc\x5d" \
"\xac\x1b\x08\x42\x0c\xea\xac\x23\x08\x39\xb7\x55\x84\x5a\x9f\xfb"
static void ssb_packet_stream_initable_interface_init(GInitableIface *iface);
static GInitableIface *ssb_packet_stream_parent_initable_iface;
typedef struct {
GIOStream *base_io_stream;
GInputStream *input_stream;
GOutputStream *output_stream;
} SsbPacketStreamPrivate;
G_DEFINE_TYPE_WITH_CODE(SsbPacketStream, ssb_packet_stream, G_TYPE_IO_STREAM,
G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE, ssb_packet_stream_initable_interface_init)
G_ADD_PRIVATE(SsbPacketStream));
enum {
PROP_0,
PROP_BASE_IO_STREAM,
PROP_COUNT
};
static GParamSpec *properties[PROP_COUNT];
static GInputStream *
ssb_packet_stream_get_input_stream(GIOStream *iostream)
{
SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(
SSB_PACKET_STREAM(iostream));
return priv->input_stream;
}
static GOutputStream *
ssb_packet_stream_get_output_stream(GIOStream *iostream)
{
SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(
SSB_PACKET_STREAM(iostream));
return priv->output_stream;
}
static gboolean
ssb_packet_stream_close_fn(GIOStream *iostream, GCancellable *cancellable, GError **error)
{
// TODO: How to do this?
return TRUE;
}
static void
close_thread(GTask *task, gpointer object, gpointer task_data, GCancellable *cancellable)
{
GIOStream *iostream = object;
GError *err = NULL;
if (!ssb_packet_stream_close(st))
}
static void
ssb_packet_stream_close_async(GIOStream *iostream,
GIOPriority io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task = g_task_new(stream, cancellable, callback, user_data);
g_task_set_source_tag(task, ssb_packet_stream_close_async);
g_task_set_priority(task, io_priority);
g_task_run_in_thread(task, close_thread);
g_object_unref(task);
}
static gboolean
ssb_packet_stream_close_finish(GIOStream *stream, GAsyncResult *result, GError **error)
{
g_return_val_if_fail(g_task_is_valid(result, stream), FALSE);
return g_task_propagate_boolean(G_TASK(result), error);
}
static void
ssb_packet_stream_get_property(GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(
SSB_PACKET_STREAM(gobject));
switch (prop_id) {
case PROP_BASE_IO_STREAM:
g_value_set_object(value, priv->base_io_stream);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
}
}
static void
ssb_packet_stream_set_property(GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(
SSB_PACKET_STREAM(gobject));
switch (prop_id) {
case PROP_BASE_IO_STREAM:
g_clear_object(&(priv->base_io_stream));
priv->base_io_stream = g_value_dup_object(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
}
}
static void
ssb_packet_stream_finalize(GObject *gobject)
{
SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(
SSB_PACKET_STREAM(gobject));
g_clear_object(&(priv->base_io_stream));
}
static void
ssb_packet_stream_init(SsbPacketStream *stream)
{
SsbPacketStreamPrivate *priv = ssb_packet_stream_get_instance_private(stream);
priv->base_io_stream = NULL;
}
static void
ssb_packet_stream_class_init(SsbPacketStreamClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
GIOStreamClass *iostream_class = G_IO_STREAM_CLASS(klass);
gobject_class->get_property = ssb_packet_stream_get_property;
gobject_class->set_property = ssb_packet_stream_set_property;
gobject_class->finalize = ssb_packet_stream_finalize;
iostream_class->get_input_stream = ssb_packet_stream_get_input_stream;
iostream_class->get_output_stream = ssb_packet_stream_get_output_stream;
iostream_class->close_fn = ssb_packet_stream_close_fn;
iostream_class->close_async = ssb_packet_stream_close_async;
iostream_class->close_finish = ssb_packet_stream_close_finish;
properties[PROP_BASE_IO_STREAM] = g_param_spec_object(
"base-io-stream", P_("Base IOStream"), P_("The GIOStream that the connection wraps"),
G_TYPE_IO_STREAM,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(gobject_class, PROP_COUNT, properties);
}
static gboolean
ssb_packet_stream_initable_init(GInitable *initable, GCancellable *cancellable, GError **error)
{
if (!ssb_packet_stream_parent_initable_iface->init(initable, cancellable, error)) {
return FALSE;
}
return TRUE;
}
static void
ssb_packet_stream_initable_interface_init(GInitableIface *iface)
{
ssb_packet_stream_parent_initable_iface = g_type_interface_peek_parent(iface);
iface->init = ssb_packet_stream_initable_init;
}
GIOStream *
ssb_packet_stream_new(GIOStream *base_io_stream, GError **error)
{
GObject *conn;
conn = g_initable_new(SSB_TYPE_PACKET_STREAM, NULL, error,
"base-io-stream", base_io_stream,
NULL);
return G_IO_STREAM(conn);
}

View File

@ -0,0 +1,21 @@
#ifndef __SSB_PACKET_STREAM_H__
# define __SSB_PACKET_STREAM_H__
# include <glib-object.h>
# include <gio/gio.h>
G_BEGIN_DECLS
# define SSB_TYPE_PACKET_STREAM (ssb_packet_stream_get_type())
G_DECLARE_DERIVABLE_TYPE(SsbPacketStream, ssb_packet_stream, SSB, PACKET_STREAM, GIOStream)
struct _SsbPacketStreamClass {
GIOStreamClass parent_class;
};
GIOStream *ssb_packet_stream_new(GIOStream *io_stream, GError **error);
G_END_DECLS
#endif /* __SSB_PACKET_STREAM_H__ */

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
#ifndef __SBOT_H__ #ifndef __SSB_SCUTTLER_H__
# define __SBOT_H__ # define __SSB_SCUTTLER_H__
# include <glib-object.h> # include <glib-object.h>
# include <gio/gio.h> # include <gio/gio.h>
@ -16,10 +16,10 @@ typedef enum {
SSB_SCUTTLER_ERROR_SEND, SSB_SCUTTLER_ERROR_SEND,
SSB_SCUTTLER_ERROR_NOTCONNECTED, SSB_SCUTTLER_ERROR_NOTCONNECTED,
SSB_SCUTTLER_ERROR_READ, SSB_SCUTTLER_ERROR_READ,
SSB_SCUTTLER_ERROR_CRYPT, SSB_SCUTTLER_ERROR_CRYPTO,
SSB_SCUTTLER_ERROR_DECRYPT,
SSB_SCUTTLER_ERROR_RESPONSE_TOO_LARGE, SSB_SCUTTLER_ERROR_RESPONSE_TOO_LARGE,
SSB_SCUTTLER_ERROR_STREAM_END, SSB_SCUTTLER_ERROR_STREAM_END,
SSB_SCUTTLER_ERROR_CONFIG_ERROR,
} SsbScuttlerError; } SsbScuttlerError;
G_BEGIN_DECLS G_BEGIN_DECLS
@ -45,4 +45,4 @@ void ssb_scuttler_whoami_async(SsbScuttler *scuttler,
G_END_DECLS G_END_DECLS
#endif /* __SBOT_H__ */ #endif /* __SSB_SCUTTLER_H__ */