[wing/nacho/iocp] iocpinput/outputstream: check the result of Read/WriteFile




commit 2a22a17531b1d46ec8ee4eb9fcde4bdd17b10c80
Author: KUNJAN Naik <kunjnaik amazon com>
Date:   Thu Aug 19 09:50:32 2021 +0200

    iocpinput/outputstream: check the result of Read/WriteFile
    
    Otherwise it could happen that the task returns twice if
    the Read/Write returns successful

 wing/wingiocpinputstream.c  | 35 +++++++++++++++++++----------------
 wing/wingiocpoutputstream.c | 35 +++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 32 deletions(-)
---
diff --git a/wing/wingiocpinputstream.c b/wing/wingiocpinputstream.c
index 3f01b0e..9cd5def 100644
--- a/wing/wingiocpinputstream.c
+++ b/wing/wingiocpinputstream.c
@@ -208,7 +208,6 @@ wing_iocp_input_stream_read_async (GInputStream        *stream,
   WingOverlappedData *overlapped;
   WingIocpInputStream *wing_stream;
   WingIocpInputStreamPrivate *priv;
-  int errsv;
   HANDLE handle;
 
   wing_stream = WING_IOCP_INPUT_STREAM (stream);
@@ -245,25 +244,29 @@ wing_iocp_input_stream_read_async (GInputStream        *stream,
 
   wing_thread_pool_io_start (priv->thread_pool_io);
 
-  ReadFile (handle, buffer, (DWORD) count, NULL, (OVERLAPPED *) overlapped);
-  errsv = GetLastError ();
-  if (errsv != NO_ERROR && errsv != ERROR_IO_PENDING)
+  if (!ReadFile (handle, buffer, (DWORD)count, NULL, (OVERLAPPED *)overlapped))
     {
-      gchar *emsg = g_win32_error_message (errsv);
+      int errsv;
 
-      g_task_return_new_error (task, G_IO_ERROR,
-                               g_io_error_from_win32_error (errsv),
-                               "Error reading from handle: %s",
-                               emsg);
-      g_free (emsg);
+      errsv = GetLastError ();
+      if (errsv != NO_ERROR && errsv != ERROR_IO_PENDING)
+        {
+          gchar *emsg = g_win32_error_message (errsv);
 
-      wing_thread_pool_io_cancel (priv->thread_pool_io);
+          g_task_return_new_error (task, G_IO_ERROR,
+                                   g_io_error_from_win32_error (errsv),
+                                   "Error reading from handle: %s",
+                                   emsg);
+          g_free (emsg);
 
-      if (g_task_get_cancellable (task) != NULL)
-        g_cancellable_disconnect (g_task_get_cancellable (task),
-                                  overlapped->cancellable_id);
-      g_object_unref (task);
-      g_slice_free (WingOverlappedData, overlapped);
+          wing_thread_pool_io_cancel (priv->thread_pool_io);
+
+          if (g_task_get_cancellable (task) != NULL)
+            g_cancellable_disconnect (g_task_get_cancellable (task),
+                                      overlapped->cancellable_id);
+          g_object_unref (task);
+          g_slice_free (WingOverlappedData, overlapped);
+        }
     }
 }
 
diff --git a/wing/wingiocpoutputstream.c b/wing/wingiocpoutputstream.c
index 55c035a..7b199a3 100644
--- a/wing/wingiocpoutputstream.c
+++ b/wing/wingiocpoutputstream.c
@@ -208,7 +208,6 @@ wing_iocp_output_stream_write_async (GOutputStream       *stream,
   WingOverlappedData *overlapped;
   WingIocpOutputStream *wing_stream;
   WingIocpOutputStreamPrivate *priv;
-  int errsv;
   HANDLE handle;
 
   wing_stream = WING_IOCP_OUTPUT_STREAM (stream);
@@ -245,25 +244,29 @@ wing_iocp_output_stream_write_async (GOutputStream       *stream,
 
   wing_thread_pool_io_start (priv->thread_pool_io);
 
-  WriteFile (handle, buffer, (DWORD) count, NULL, (OVERLAPPED *) overlapped);
-  errsv = GetLastError ();
-  if (errsv != NO_ERROR && errsv != ERROR_IO_PENDING)
+  if (!WriteFile (handle, buffer, (DWORD)count, NULL, (OVERLAPPED *)overlapped))
     {
-      gchar *emsg = g_win32_error_message (errsv);
+      int errsv;
 
-      g_task_return_new_error (task, G_IO_ERROR,
-                               g_io_error_from_win32_error (errsv),
-                               "Error writing to handle: %s",
-                               emsg);
-      g_free (emsg);
+      errsv = GetLastError ();
+      if (errsv != NO_ERROR && errsv != ERROR_IO_PENDING)
+        {
+          gchar *emsg = g_win32_error_message (errsv);
 
-      wing_thread_pool_io_cancel (priv->thread_pool_io);
+          g_task_return_new_error (task, G_IO_ERROR,
+                                   g_io_error_from_win32_error (errsv),
+                                   "Error writing to handle: %s",
+                                   emsg);
+          g_free (emsg);
 
-      if (g_task_get_cancellable (task) != NULL)
-        g_cancellable_disconnect (g_task_get_cancellable (task),
-                                  overlapped->cancellable_id);
-      g_object_unref (task);
-      g_slice_free (WingOverlappedData, overlapped);
+          wing_thread_pool_io_cancel (priv->thread_pool_io);
+
+          if (g_task_get_cancellable (task) != NULL)
+            g_cancellable_disconnect (g_task_get_cancellable (task),
+                                      overlapped->cancellable_id);
+          g_object_unref (task);
+          g_slice_free (WingOverlappedData, overlapped);
+        }
     }
 }
 


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