Kaynağa Gözat

flow: add StreamPacketSender

ambrop7 14 yıl önce
ebeveyn
işleme
b7d4a7cd88
3 değiştirilmiş dosya ile 154 ekleme ve 0 silme
  1. 1 0
      flow/CMakeLists.txt
  2. 83 0
      flow/StreamPacketSender.c
  3. 70 0
      flow/StreamPacketSender.h

+ 1 - 0
flow/CMakeLists.txt

@@ -24,5 +24,6 @@ add_library(flow
     LineBuffer.c
     SingleStreamSender.c
     SingleStreamReceiver.c
+    StreamPacketSender.c
 )
 target_link_libraries(flow base)

+ 83 - 0
flow/StreamPacketSender.c

@@ -0,0 +1,83 @@
+/**
+ * @file StreamPacketSender.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * This file is part of BadVPN.
+ * 
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ * 
+ * BadVPN is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <misc/debug.h>
+
+#include "StreamPacketSender.h"
+
+static void input_handler_send (StreamPacketSender *o, uint8_t *data, int data_len)
+{
+    DebugObject_Access(&o->d_obj);
+    ASSERT(data_len > 0)
+    
+    // limit length to MTU and remember
+    if (data_len > o->output_mtu) {
+        o->sending_len = o->output_mtu;
+    } else {
+        o->sending_len = data_len;
+    }
+    
+    // send
+    PacketPassInterface_Sender_Send(o->output, data, o->sending_len);
+}
+
+static void output_handler_done (StreamPacketSender *o)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    // done
+    StreamPassInterface_Done(&o->input, o->sending_len);
+}
+
+void StreamPacketSender_Init (StreamPacketSender *o, PacketPassInterface *output, BPendingGroup *pg)
+{
+    ASSERT(PacketPassInterface_GetMTU(output) > 0)
+    
+    // init arguments
+    o->output = output;
+    
+    // remember output MTU
+    o->output_mtu = PacketPassInterface_GetMTU(output);
+    
+    // init input
+    StreamPassInterface_Init(&o->input, (StreamPassInterface_handler_send)input_handler_send, o, pg);
+    
+    // init output
+    PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
+    
+    DebugObject_Init(&o->d_obj);
+}
+
+void StreamPacketSender_Free (StreamPacketSender *o)
+{
+    DebugObject_Free(&o->d_obj);
+    
+    // free input
+    StreamPassInterface_Free(&o->input);
+}
+
+StreamPassInterface * StreamPacketSender_GetInput (StreamPacketSender *o)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    return &o->input;
+}

+ 70 - 0
flow/StreamPacketSender.h

@@ -0,0 +1,70 @@
+/**
+ * @file StreamPacketSender.h
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * This file is part of BadVPN.
+ * 
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ * 
+ * BadVPN is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef BADVPN_STREAMPACKETSENDER_H
+#define BADVPN_STREAMPACKETSENDER_H
+
+#include <base/DebugObject.h>
+#include <flow/StreamPassInterface.h>
+#include <flow/PacketPassInterface.h>
+
+/**
+ * Object which breaks an input stream into output packets. The resulting
+ * packets will have positive length, and, when concatenated, will form the
+ * original stream.
+ * 
+ * Input is with {@link StreamPassInterface}.
+ * Output is with {@link PacketPassInterface}.
+ */
+typedef struct {
+    PacketPassInterface *output;
+    int output_mtu;
+    StreamPassInterface input;
+    int sending_len;
+    DebugObject d_obj;
+} StreamPacketSender;
+
+/**
+ * Initializes the object.
+ * 
+ * @param o the object
+ * @param output output interface. Its MTU must be >0.
+ * @param pg pending group we live in
+ */
+void StreamPacketSender_Init (StreamPacketSender *o, PacketPassInterface *output, BPendingGroup *pg);
+
+/**
+ * Frees the object.
+ * 
+ * @param o the object
+ */
+void StreamPacketSender_Free (StreamPacketSender *o);
+
+/**
+ * Returns the input interface.
+ * 
+ * @param o the object
+ * @return input interface
+ */
+StreamPassInterface * StreamPacketSender_GetInput (StreamPacketSender *o);
+
+#endif