Fix calls to ReadFile and WriteFile for asynchronous I/O.
authorEric Kohl <eric.kohl@reactos.org>
Sat, 5 Mar 2005 11:38:48 +0000 (11:38 +0000)
committerEric Kohl <eric.kohl@reactos.org>
Sat, 5 Mar 2005 11:38:48 +0000 (11:38 +0000)
svn path=/trunk/; revision=13825

reactos/lib/rpcrt4/rpc_message.c

index 119af8c..d10e56c 100644 (file)
@@ -265,10 +265,15 @@ RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
     }\r
 \r
     /* transmit packet header */\r
-    if (!WriteFile(Connection->conn, Header, hdr_size, &count, NULL)) {\r
+    ResetEvent(Connection->ovl.hEvent);\r
+    if (!WriteFile(Connection->conn, Header, hdr_size, &count, &Connection->ovl)) {\r
       WARN("WriteFile failed with error %ld\n", GetLastError());\r
       return GetLastError();\r
     }\r
+    if (!GetOverlappedResult(Connection->conn, &Connection->ovl, &count, TRUE)) {\r
+      WARN("GetOverlappedResult failed with error %ld\n", GetLastError());\r
+      return GetLastError();\r
+    }\r
 \r
     /* fragment consisted of header only and is the last one */\r
     if (hdr_size == Header->common.frag_len &&\r
@@ -277,10 +282,15 @@ RPC_STATUS RPCRT4_Send(RpcConnection *Connection, RpcPktHdr *Header,
     }\r
 \r
     /* send the fragment data */\r
-    if (!WriteFile(Connection->conn, buffer_pos, Header->common.frag_len - hdr_size, &count, NULL)) {\r
+    ResetEvent(Connection->ovl.hEvent);\r
+    if (!WriteFile(Connection->conn, buffer_pos, Header->common.frag_len - hdr_size, &count, &Connection->ovl)) {\r
       WARN("WriteFile failed with error %ld\n", GetLastError());\r
       return GetLastError();\r
     }\r
+    if (!GetOverlappedResult(Connection->conn, &Connection->ovl, &count, TRUE)) {\r
+      WARN("GetOverlappedResult failed with error %ld\n", GetLastError());\r
+      return GetLastError();\r
+    }\r
 \r
     Header->common.flags &= ~RPC_FLG_FIRST;\r
   }\r
@@ -309,9 +319,15 @@ RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
   TRACE("(%p, %p, %p)\n", Connection, Header, pMsg);\r
 \r
   /* read packet common header */\r
-  if (!ReadFile(Connection->conn, &common_hdr, sizeof(common_hdr), &dwRead, NULL)) {\r
+  ResetEvent(Connection->ovl.hEvent);\r
+  if (!ReadFile(Connection->conn, &common_hdr, sizeof(common_hdr), &dwRead, &Connection->ovl)) {\r
+    WARN("ReadFile failed with error %ld\n", GetLastError());\r
+    status = RPC_S_PROTOCOL_ERROR;\r
+    goto fail;\r
+  }\r
+  if (!GetOverlappedResult(Connection->conn, &Connection->ovl, &dwRead, TRUE)) {\r
     if (GetLastError() != ERROR_MORE_DATA) {\r
-      WARN("ReadFile failed with error %ld\n", GetLastError());\r
+      WARN("GetOverlappedResult failed with error %ld\n", GetLastError());\r
       status = RPC_S_PROTOCOL_ERROR;\r
       goto fail;\r
     }\r
@@ -339,10 +355,16 @@ RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
   memcpy(*Header, &common_hdr, sizeof(common_hdr));\r
 \r
   /* read the rest of packet header */\r
+  ResetEvent(Connection->ovl.hEvent);\r
   if (!ReadFile(Connection->conn, &(*Header)->common + 1,\r
-                hdr_length - sizeof(common_hdr), &dwRead, NULL)) {\r
+                hdr_length - sizeof(common_hdr), &dwRead, &Connection->ovl)) {\r
+    WARN("ReadFile failed with error %ld\n", GetLastError());\r
+    status = RPC_S_PROTOCOL_ERROR;\r
+    goto fail;\r
+  }\r
+  if (!GetOverlappedResult(Connection->conn, &Connection->ovl, &dwRead, TRUE)) {\r
     if (GetLastError() != ERROR_MORE_DATA) {\r
-      WARN("ReadFile failed with error %ld\n", GetLastError());\r
+      WARN("GetOverlappedResult failed with error %ld\n", GetLastError());\r
       status = RPC_S_PROTOCOL_ERROR;\r
       goto fail;\r
     }\r
@@ -352,6 +374,7 @@ RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
     goto fail;\r
   }\r
 \r
+\r
   /* read packet body */\r
   switch (common_hdr.ptype) {\r
   case PKT_RESPONSE:\r
@@ -379,13 +402,20 @@ RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
       goto fail;\r
     }\r
 \r
-    if (data_length == 0) dwRead = 0; else\r
-    if (!ReadFile(Connection->conn, buffer_ptr, data_length, &dwRead, NULL)) {\r
-      if (GetLastError() != ERROR_MORE_DATA) {\r
+    if (data_length == 0) dwRead = 0; else {\r
+      ResetEvent(Connection->ovl.hEvent);\r
+      if (!ReadFile(Connection->conn, buffer_ptr, data_length, &dwRead, &Connection->ovl)) {\r
         WARN("ReadFile failed with error %ld\n", GetLastError());\r
         status = RPC_S_PROTOCOL_ERROR;\r
         goto fail;\r
       }\r
+      if (!GetOverlappedResult(Connection->conn, &Connection->ovl, &dwRead, TRUE)) {\r
+        if (GetLastError() != ERROR_MORE_DATA) {\r
+          WARN("GetOverlappedResult failed with error %ld\n", GetLastError());\r
+          status = RPC_S_PROTOCOL_ERROR;\r
+          goto fail;\r
+        }\r
+      }\r
     }\r
     if (dwRead != data_length) {\r
       status = RPC_S_PROTOCOL_ERROR;\r
@@ -403,10 +433,16 @@ RPC_STATUS RPCRT4_Receive(RpcConnection *Connection, RpcPktHdr **Header,
       TRACE("next header\n");\r
 \r
       /* read the header of next packet */\r
-      if (!ReadFile(Connection->conn, *Header, hdr_length, &dwRead, NULL)) {\r
+      ResetEvent(Connection->ovl.hEvent);\r
+      if (!ReadFile(Connection->conn, *Header, hdr_length, &dwRead, &Connection->ovl)) {\r
+        WARN("ReadFile failed with error %ld\n", GetLastError());\r
+        status = GetLastError();\r
+        goto fail;\r
+      }\r
+      if (!GetOverlappedResult(Connection->conn, &Connection->ovl, &dwRead, TRUE)) {\r
         if (GetLastError() != ERROR_MORE_DATA) {\r
-          WARN("ReadFile failed with error %ld\n", GetLastError());\r
-          status = GetLastError();\r
+          WARN("GetOverlappedResult failed with error %ld\n", GetLastError());\r
+          status = RPC_S_PROTOCOL_ERROR;\r
           goto fail;\r
         }\r
       }\r