Преглед на файлове

refactor(core): add default implementation for write_stream

Convert write_stream from abstract to virtual method in AsyncOutput
interface with a chunked read/write implementation. Remove redundant
write_stream stub from ServerOutput that previously called assert_not_reached.
Billy Barrow преди 3 седмици
родител
ревизия
c33427691a
променени са 2 файла, в които са добавени 10 реда и са изтрити 5 реда
  1. 10 1
      src/Core/AsyncOutput.vala
  2. 0 4
      src/Server/ServerOutput.vala

+ 10 - 1
src/Core/AsyncOutput.vala

@@ -6,7 +6,16 @@ namespace Astralis {
     public interface AsyncOutput : Object {
 
         public abstract async void write(BinaryData data) throws Error;
-        public abstract async void write_stream(InputStream stream) throws Error;
+        public virtual async void write_stream(InputStream stream) throws Error {
+            uint8[] chunk = new uint8[8192];
+            while (true) {
+                ssize_t bytes_read = yield stream.read_async(chunk);
+                if (bytes_read <= 0) {
+                    break;
+                }
+                yield write(new ByteBuffer.from_byte_array(chunk));
+            }
+        }
 
     }
 

+ 0 - 4
src/Server/ServerOutput.vala

@@ -21,10 +21,6 @@ namespace Astralis {
             }
         }
 
-        public async void write_stream (GLib.InputStream stream) {
-            assert_not_reached ();
-        }
-
         internal size_t read_chunk(void* buffer, size_t max_size) {
             if(current_chunk == null && chunks.length == 0) {
                 return 0;