[geary/mjog/imap-connection-fixes: 97/110] Update Geary.Imap.Deserializer



commit 7a7cf25a336fbfa52fade1cf2772b90084234490
Author: Michael Gratton <mike vee net>
Date:   Sun Dec 29 13:04:07 2019 +1030

    Update Geary.Imap.Deserializer
    
    Remove converter since it is unused, ensure DataOutputStream is closed
    when the deserialiser is stopped. Rename dins property to be something
    more descriptive.

 src/engine/imap/transport/imap-deserializer.vala | 43 +++++++-------
 src/engine/util/util-stream.vala                 | 74 ------------------------
 2 files changed, 20 insertions(+), 97 deletions(-)
---
diff --git a/src/engine/imap/transport/imap-deserializer.vala 
b/src/engine/imap/transport/imap-deserializer.vala
index a9042265..2ad9120b 100644
--- a/src/engine/imap/transport/imap-deserializer.vala
+++ b/src/engine/imap/transport/imap-deserializer.vala
@@ -1,7 +1,9 @@
-/* Copyright 2016 Software Freedom Conservancy Inc.
+/*
+ * Copyright 2016 Software Freedom Conservancy Inc.
+ * Copyright 2019 Michael Gratton <mike vee net>
  *
  * This software is licensed under the GNU Lesser General Public License
- * (version 2.1 or later).  See the COPYING file in this distribution.
+ * (version 2.1 or later). See the COPYING file in this distribution.
  */
 
 /**
@@ -72,7 +74,7 @@ public class Geary.Imap.Deserializer : BaseObject {
         state_to_string, event_to_string);
 
     private string identifier;
-    private DataInputStream dins;
+    private DataInputStream input;
     private Geary.State.Machine fsm;
 
     private ListParameter context;
@@ -81,7 +83,6 @@ public class Geary.Imap.Deserializer : BaseObject {
 
     private Cancellable? cancellable = null;
     private Nonblocking.Semaphore closed_semaphore = new Nonblocking.Semaphore();
-    private Geary.Stream.MidstreamConverter midstream = new Geary.Stream.MidstreamConverter("Deserializer");
     private StringBuilder? current_string = null;
     private size_t literal_length_remaining = 0;
     private Geary.Memory.GrowableBuffer? block_buffer = null;
@@ -141,14 +142,12 @@ public class Geary.Imap.Deserializer : BaseObject {
     public signal void end_of_stream();
 
 
-    public Deserializer(string identifier, InputStream ins) {
+    public Deserializer(string identifier, GLib.InputStream input) {
         this.identifier = identifier;
 
-        ConverterInputStream cins = new ConverterInputStream(ins, midstream);
-        cins.set_close_base_stream(false);
-        dins = new DataInputStream(cins);
-        dins.set_newline_type(DataStreamNewlineType.CR_LF);
-        dins.set_close_base_stream(false);
+        this.input = new GLib.DataInputStream(input);
+        this.input.set_close_base_stream(false);
+        this.input.set_newline_type(CR_LF);
 
         Geary.State.Mapping[] mappings = {
             new Geary.State.Mapping(State.TAG, Event.CHAR, on_tag_char),
@@ -212,15 +211,6 @@ public class Geary.Imap.Deserializer : BaseObject {
         reset_params();
     }
 
-    /**
-     * Install a custom Converter into the input stream.
-     *
-     * Can be used for decompression, decryption, and so on.
-     */
-    public bool install_converter(Converter converter) {
-        return midstream.install(converter);
-    }
-
     /**
      * Begin deserializing IMAP responses from the input stream.
      *
@@ -254,6 +244,7 @@ public class Geary.Imap.Deserializer : BaseObject {
 
         // wait for outstanding I/O to exit
         yield closed_semaphore.wait_async();
+        yield this.input.close_async(GLib.Priority.DEFAULT, null);
         Logging.debug(Logging.Flag.DESERIALIZER, "[%s] Deserializer closed", to_string());
     }
 
@@ -281,7 +272,9 @@ public class Geary.Imap.Deserializer : BaseObject {
     private void next_deserialize_step() {
         switch (get_mode()) {
             case Mode.LINE:
-                dins.read_line_async.begin(ins_priority, cancellable, on_read_line);
+                this.input.read_line_async.begin(
+                    ins_priority, cancellable, on_read_line
+                );
             break;
 
             case Mode.BLOCK:
@@ -295,7 +288,9 @@ public class Geary.Imap.Deserializer : BaseObject {
                 current_buffer = block_buffer.allocate(
                     size_t.min(MAX_BLOCK_READ_SIZE, literal_length_remaining));
 
-                dins.read_async.begin(current_buffer, ins_priority, cancellable, on_read_block);
+                this.input.read_async.begin(
+                    current_buffer, ins_priority, cancellable, on_read_block
+                );
             break;
 
             case Mode.FAILED:
@@ -311,7 +306,9 @@ public class Geary.Imap.Deserializer : BaseObject {
     private void on_read_line(Object? source, AsyncResult result) {
         try {
             size_t bytes_read;
-            string? line = dins.read_line_async.end(result, out bytes_read);
+            string? line = this.input.read_line_async.end(
+                result, out bytes_read
+            );
             if (line == null) {
                 Logging.debug(Logging.Flag.DESERIALIZER, "[%s] line EOS", to_string());
 
@@ -335,7 +332,7 @@ public class Geary.Imap.Deserializer : BaseObject {
         try {
             // Zero-byte literals are legal (see note in next_deserialize_step()), so EOS only
             // happens when actually pulling data
-            size_t bytes_read = dins.read_async.end(result);
+            size_t bytes_read = this.input.read_async.end(result);
             if (bytes_read == 0 && literal_length_remaining > 0) {
                 Logging.debug(Logging.Flag.DESERIALIZER, "[%s] block EOS", to_string());
 
diff --git a/src/engine/util/util-stream.vala b/src/engine/util/util-stream.vala
index 2cffb05a..18df50fb 100644
--- a/src/engine/util/util-stream.vala
+++ b/src/engine/util/util-stream.vala
@@ -41,80 +41,6 @@ namespace Geary.Stream {
             yield write_all_async(outs, new Memory.StringBuffer(str), cancellable);
     }
 
-
-    public class MidstreamConverter : BaseObject, Converter {
-        public uint64 total_bytes_read { get; private set; default = 0; }
-        public uint64 total_bytes_written { get; private set; default = 0; }
-        public uint64 converted_bytes_read { get; private set; default = 0; }
-        public uint64 converted_bytes_written { get; private set; default = 0; }
-
-        public bool log_performance { get; set; default = false; }
-
-        private string name;
-        private Converter? converter = null;
-
-        public MidstreamConverter(string name) {
-            this.name = name;
-        }
-
-        public bool install(Converter converter) {
-            if (this.converter != null)
-                return false;
-
-            this.converter = converter;
-
-            return true;
-        }
-
-        public ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags,
-                                       out size_t bytes_read, out size_t bytes_written) throws Error {
-            if (converter != null) {
-                ConverterResult result = converter.convert(inbuf, outbuf, flags, out bytes_read, out 
bytes_written);
-
-                total_bytes_read += bytes_read;
-                total_bytes_written += bytes_written;
-
-                converted_bytes_read += bytes_read;
-                converted_bytes_written += bytes_written;
-
-                if (log_performance && (bytes_read > 0 || bytes_written > 0)) {
-                    double pct = (converted_bytes_read > converted_bytes_written)
-                    ? (double) converted_bytes_written / (double) converted_bytes_read
-                    : (double) converted_bytes_read / (double) converted_bytes_written;
-                    debug("%s read/written: %s/%s (%lld%%)", name, converted_bytes_read.to_string(),
-                          converted_bytes_written.to_string(), (long) (pct * 100.0));
-                }
-
-                return result;
-            }
-
-            // passthrough
-            size_t copied = size_t.min(inbuf.length, outbuf.length);
-            if (copied > 0)
-                GLib.Memory.copy(outbuf, inbuf, copied);
-
-            bytes_read = copied;
-            bytes_written = copied;
-
-            total_bytes_read += copied;
-            total_bytes_written += copied;
-
-            if ((flags & ConverterFlags.FLUSH) != 0)
-                return ConverterResult.FLUSHED;
-
-            if ((flags & ConverterFlags.INPUT_AT_END) != 0)
-                return ConverterResult.FINISHED;
-
-            return ConverterResult.CONVERTED;
-        }
-
-        public void reset() {
-            if (converter != null)
-                converter.reset();
-        }
-    }
-
-
     /**
      * Adaptor from a GMime stream to a GLib OutputStream.
      */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]