ambrop7 15 жил өмнө
parent
commit
1d4818a332
2 өөрчлөгдсөн 47 нэмэгдсэн , 19 устгасан
  1. 40 18
      flow/PacketCopier.c
  2. 7 1
      flow/PacketCopier.h

+ 40 - 18
flow/PacketCopier.c

@@ -31,21 +31,18 @@ static int input_handler_send (PacketCopier *o, uint8_t *data, int data_len)
     ASSERT(o->in_len == -1)
     ASSERT(data_len >= 0)
     
-    if (!o->out_have) {
+    if (!o->out_have || o->out_got_len >= 0) {
         o->in_len = data_len;
         o->in = data;
+        o->in_got = 0;
         return 0;
     }
     
     memcpy(o->out, data, data_len);
     
-    o->out_have = 0;
+    o->out_got_len = data_len;
     
-    DEAD_ENTER(o->dead)
-    PacketRecvInterface_Done(&o->output, data_len);
-    if (DEAD_LEAVE(o->dead)) {
-        return -1;
-    }
+    BPending_Set(&o->continue_job_output);
     
     return 1;
 }
@@ -62,29 +59,46 @@ static int output_handler_recv (PacketCopier *o, uint8_t *data, int *data_len)
 {
     ASSERT(!o->out_have)
     
-    if (o->in_len < 0) {
+    if (o->in_len < 0 || o->in_got) {
         o->out_have = 1;
         o->out = data;
+        o->out_got_len = -1;
         return 0;
     }
     
-    int len = o->in_len;
+    memcpy(data, o->in, o->in_len);
+    
+    o->in_got =  1;
     
-    memcpy(data, o->in, len);
+    BPending_Set(&o->continue_job_input);
+    
+    *data_len = o->in_len;
+    return 1;
+}
+
+static void input_job_handler (PacketCopier *o)
+{
+    ASSERT(o->in_len >= 0)
+    ASSERT(o->in_got)
     
     o->in_len = -1;
     
-    DEAD_ENTER(o->dead)
     PacketPassInterface_Done(&o->input);
-    if (DEAD_LEAVE(o->dead)) {
-        return -1;
-    }
+    return;
+}
+
+static void output_job_handler (PacketCopier *o)
+{
+    ASSERT(o->out_have)
+    ASSERT(o->out_got_len >= 0)
     
-    *data_len = len;
-    return 1;
+    o->out_have = 0;
+    
+    PacketRecvInterface_Done(&o->output, o->out_got_len);
+    return;
 }
 
-void PacketCopier_Init (PacketCopier *o, int mtu)
+void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg)
 {
     ASSERT(mtu >= 0)
     
@@ -104,6 +118,10 @@ void PacketCopier_Init (PacketCopier *o, int mtu)
     // set no output packet
     o->out_have = 0;
     
+    // init continue jobs
+    BPending_Init(&o->continue_job_input, pg, (BPending_handler)input_job_handler, o);
+    BPending_Init(&o->continue_job_output, pg, (BPending_handler)output_job_handler, o);
+    
     // init debug object
     DebugObject_Init(&o->d_obj);
 }
@@ -112,7 +130,11 @@ void PacketCopier_Free (PacketCopier *o)
 {
     // free debug object
     DebugObject_Free(&o->d_obj);
-
+    
+    // free continue jobs
+    BPending_Free(&o->continue_job_output);
+    BPending_Free(&o->continue_job_input);
+    
     // free output
     PacketRecvInterface_Free(&o->output);
     

+ 7 - 1
flow/PacketCopier.h

@@ -30,6 +30,7 @@
 #include <stdint.h>
 
 #include <misc/dead.h>
+#include <system/BPending.h>
 #include <flow/PacketPassInterface.h>
 #include <flow/PacketRecvInterface.h>
 
@@ -45,8 +46,12 @@ typedef struct {
     PacketRecvInterface output;
     int in_len;
     uint8_t *in;
+    int in_got;
     int out_have;
     uint8_t *out;
+    int out_got_len;
+    BPending continue_job_input;
+    BPending continue_job_output;
 } PacketCopier;
 
 /**
@@ -54,8 +59,9 @@ typedef struct {
  * 
  * @param o the object
  * @param mtu maximum packet size. Must be >=0.
+ * @param pg pending group
  */
-void PacketCopier_Init (PacketCopier *o, int mtu);
+void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg);
 
 /**
  * Frees the object.