fixes async sendfile aio

Tue, 09 Jan 2018 15:02:24 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 09 Jan 2018 15:02:24 +0100
branch
aio
changeset 184
a2a15ad6e4b9
parent 172
5580517faafc
child 185
b4d7ccf4e06d

fixes async sendfile

src/server/public/nsapi.h file | annotate | diff | comparison | revisions
src/server/safs/service.c file | annotate | diff | comparison | revisions
src/server/safs/service.h file | annotate | diff | comparison | revisions
--- a/src/server/public/nsapi.h	Sat Feb 18 13:27:25 2017 +0100
+++ b/src/server/public/nsapi.h	Tue Jan 09 15:02:24 2018 +0100
@@ -1140,7 +1140,7 @@
 
 // new types
 typedef struct aiocb_s {
-    SYS_FILE filedes;
+    SYS_FILE     filedes;
     void         *buf;
     size_t       nbytes;
     off_t        offset;
--- a/src/server/safs/service.c	Sat Feb 18 13:27:25 2017 +0100
+++ b/src/server/safs/service.c	Tue Jan 09 15:02:24 2018 +0100
@@ -43,7 +43,6 @@
 
 #include <errno.h>
 
-
 /*
  * prepares servicing a file
  *
@@ -318,12 +317,17 @@
 
 static int send_bytes(AsyncSendRange *asr, WSBool *completed) {
     *completed = FALSE;
-    if(send_buf(asr->out, asr->header, asr->headerlen, &asr->headerpos)) {
-        if(net_errno(asr->out) == EAGAIN) {
-            return 0;
-        } else {
-            asr->error = TRUE;
-            return 1;
+    if(asr->header) {
+        if(send_buf(asr->out, asr->header, asr->headerlen, &asr->headerpos)) {
+            if(net_errno(asr->out) == EAGAIN) {
+                return 0;
+            } else {
+                asr->error = TRUE;
+                return 1;
+            }
+        }
+        if(asr->headerpos >= asr->headerlen) {
+            asr->header = NULL;
         }
     }
     
@@ -339,6 +343,8 @@
     if(!asr->read_complete) {
         // write completed => new asynchronous read
         asr->aio->offset += asr->aio->result;
+        size_t length = asr->end - asr->offset;
+        asr->aio->nbytes = AIO_BUF_SIZE < length ? AIO_BUF_SIZE : length;
         if(system_aio_read(asr->aio)) {
             asr->error = TRUE;
             return 1;
@@ -352,12 +358,13 @@
     AsyncSendRange *asr = event->cookie;
     asr->read_inprogress = FALSE;
     asr->wpos = 0;
-    if(asr->error) {
+    asr->offset += asr->aio->result;
+    if(asr->error || asr->aio->result < 0) {
         return 0;
     }
     
     int ret = 1;
-    if(asr->aio->result == 0) {
+    if(asr->aio->result == 0 || asr->offset >= asr->end) {
         asr->read_complete = TRUE;
         ret = 0;
     }
@@ -380,16 +387,15 @@
 static int send_range_writeevent(EventHandler *ev, Event *event) {
     AsyncSendRange *asr = event->cookie;
     if(asr->error) {
-        return 0;
+        return 1;
     }
     
     WSBool completed;
     if(send_bytes(asr, &completed)) {
-        return 0;
+        return 1;
     }
     
-    if(completed && asr->read_complete) {
-        // everything completed
+    if(completed) {
         return 0;
     }
     
@@ -433,7 +439,8 @@
     asr->in = fd;
     asr->out = sn->csd;
     asr->offset = offset;
-    asr->length = length;
+    asr->end = offset + length;
+    //asr->length = length;
     asr->pos = offset;
     asr->read_complete = FALSE;
     asr->read_inprogress = FALSE;
--- a/src/server/safs/service.h	Sat Feb 18 13:27:25 2017 +0100
+++ b/src/server/safs/service.h	Tue Jan 09 15:02:24 2018 +0100
@@ -52,7 +52,8 @@
     SYS_FILE  in;
     SYS_NETFD out;
     off_t     offset;
-    off_t     length;
+    off_t     end;
+    //off_t     length;
     off_t     pos;
     char      *header;
     int       headerlen;

mercurial