Permalink
Browse files

Move unserialization code to extension

Now we're getting only qDebug representation of messages, but since
Quassel-defined types are defined in the extension, it may help with
unserializing them, once we figure out how to convert QVariants
to equivalent Ruby objects in the extension.
  • Loading branch information...
1 parent a11a331 commit 12c95d204a4c034ab2be794720af32515bd556c8 @hagabaka committed Feb 23, 2012
Showing with 42 additions and 6 deletions.
  1. +5 −4 connection.rb
  2. +37 −2 ext/quassel_types.cpp
View
9 connection.rb
@@ -34,10 +34,11 @@ def when_message_received(&block)
if @expected_length
# received the length, get the message
receive_data(@expected_length) do |data|
- variant = Qt::Variant.new
- reader = Qt::DataStream.new(Qt::ByteArray.new(data))
- reader >> variant
- message = Helper.ruby_value(variant)
+ #variant = Qt::Variant.new
+ #reader = Qt::DataStream.new(Qt::ByteArray.new(data))
+ #reader >> variant
+ #message = Helper.ruby_value(variant)
+ message = QuasselTypes.unserialize(data)
p data
p @expected_length
@expected_length = nil
View
39 ext/quassel_types.cpp
@@ -1,4 +1,39 @@
+#include <ruby.h>
+#include <ruby/encoding.h>
+#include <QByteArray>
+#include <QDataStream>
+#include <QString>
+#include <QDebug>
+
+VALUE QuasselTypes;
extern "C" {
- void Init_QuasselTypes() {
- }
+ void Init_QuasselTypes();
}
+VALUE unserialize(VALUE self, VALUE string);
+
+
+void Init_QuasselTypes() {
+ QuasselTypes = rb_define_module("QuasselTypes");
+ rb_define_singleton_method(QuasselTypes, "unserialize", (VALUE (*)(...))unserialize, 1);
+}
+
+VALUE unserialize(VALUE self, VALUE string)
+{
+ Check_Type(string, T_STRING);
+
+ QByteArray byte_array(RSTRING_PTR(string), RSTRING_LEN(string));
+ QDataStream data_stream(byte_array);
+ QVariant variant;
+ data_stream >> variant;
+
+ // FIXME return a Ruby object for the variant instead of its qDebug
+ QString output;
+ QDebug debug(&output);
+
+ debug << variant;
+
+ VALUE result = rb_str_new2(output.toUtf8().data());
+ rb_enc_associate_index(string, rb_enc_find_index("UTF-8"));
+ return result;
+}
+

0 comments on commit 12c95d2

Please sign in to comment.