diff options
author | ambrop7 <ambrop7@1a93d707-3861-5ebc-ad3b-9740d49b5140> | 2011-01-21 13:48:41 +0300 |
---|---|---|
committer | ambrop7 <ambrop7@1a93d707-3861-5ebc-ad3b-9740d49b5140> | 2011-01-21 13:48:41 +0300 |
commit | d604b6ae51f2c72905532d6d8598d469ce429043 (patch) | |
tree | 2ab0e991cc2730a6529c3cf002adb639de94168c /flow | |
parent | 6953c4d39d425a73a7520e7ce18ee077f3ba805f (diff) |
minor changes
Diffstat (limited to 'flow')
-rw-r--r-- | flow/FragmentProtoDisassembler.c | 87 |
1 files changed, 29 insertions, 58 deletions
diff --git a/flow/FragmentProtoDisassembler.c b/flow/FragmentProtoDisassembler.c index 900a7df..b233e04 100644 --- a/flow/FragmentProtoDisassembler.c +++ b/flow/FragmentProtoDisassembler.c @@ -21,44 +21,30 @@ */ #include <stdint.h> -#include <stdlib.h> +#include <stddef.h> #include <string.h> #include <misc/debug.h> #include <misc/byteorder.h> +#include <misc/minmax.h> #include <flow/FragmentProtoDisassembler.h> static void write_chunks (FragmentProtoDisassembler *o) { + #define IN_AVAIL (o->in_len - o->in_used) + #define OUT_AVAIL ((o->output_mtu - o->out_used) - (int)sizeof(struct fragmentproto_chunk_header)) + ASSERT(o->in_len >= 0) ASSERT(o->out) - ASSERT(o->output_mtu - o->out_used >= sizeof(struct fragmentproto_chunk_header)) - - int in_avail = o->in_len - o->in_used; - int out_avail = (o->output_mtu - o->out_used) - sizeof(struct fragmentproto_chunk_header); + ASSERT(OUT_AVAIL > 0) // write chunks to output packet do { - ASSERT(in_avail >= 0) - ASSERT(!(in_avail == 0) || out_avail >= 0) - - // check if we have space in the output packet - // (if this is a zero input packet, only one chunk is written, which - // is always possible) - if (in_avail > 0 && out_avail <= 0) { - break; - } - // calculate chunk length - int chunk_len = in_avail; - if (chunk_len > out_avail) { - chunk_len = out_avail; - } + int chunk_len = BMIN(IN_AVAIL, OUT_AVAIL); if (o->chunk_mtu > 0) { - if (chunk_len > o->chunk_mtu) { - chunk_len = o->chunk_mtu; - } + chunk_len = BMIN(chunk_len, o->chunk_mtu); } // write chunk header @@ -66,7 +52,7 @@ static void write_chunks (FragmentProtoDisassembler *o) header->frame_id = htol16(o->frame_id); header->chunk_start = htol16(o->in_used); header->chunk_len = htol16(chunk_len); - header->is_last = (chunk_len == in_avail); + header->is_last = (chunk_len == IN_AVAIL); // write chunk data memcpy(o->out + o->out_used + sizeof(struct fragmentproto_chunk_header), o->in + o->in_used, chunk_len); @@ -74,56 +60,38 @@ static void write_chunks (FragmentProtoDisassembler *o) // increment pointers o->in_used += chunk_len; o->out_used += sizeof(struct fragmentproto_chunk_header) + chunk_len; - - in_avail = o->in_len - o->in_used; - out_avail = (o->output_mtu - o->out_used) - sizeof(struct fragmentproto_chunk_header); - } while (in_avail > 0); + } while (IN_AVAIL > 0 && OUT_AVAIL > 0); // have we finished the input packet? - if (in_avail == 0) { + if (IN_AVAIL == 0) { + // set no input packet o->in_len = -1; + + // increment frame ID o->frame_id++; + + // finish input + PacketPassInterface_Done(&o->input); } // should we finish the output packet? - if ( - out_avail < 0 || - (in_avail > 0 && out_avail <= 0) || - o->latency < 0 - ) { - // finish output packet + if (OUT_AVAIL <= 0 || o->latency < 0) { + // set no output packet o->out = NULL; + // stop timer (if it's running) if (o->latency >= 0) { BReactor_RemoveTimer(o->reactor, &o->timer); } + + // finish output + PacketRecvInterface_Done(&o->output, o->out_used); } else { // start timer if we have output and it's not running (output was empty before) if (!BTimer_IsRunning(&o->timer)) { BReactor_SetTimer(o->reactor, &o->timer); } } - - ASSERT(o->in_len < 0 || !o->out) -} - -static void work_chunks (FragmentProtoDisassembler *o) -{ - ASSERT(o->in_len >= 0) - ASSERT(o->out) - - // write input to output - write_chunks(o); - - // finish input packet if needed - if (o->in_len == -1) { - PacketPassInterface_Done(&o->input); - } - - // finish output packet if needed - if (!o->out) { - PacketRecvInterface_Done(&o->output, o->out_used); - } } static void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len) @@ -142,7 +110,7 @@ static void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int return; } - work_chunks(o); + write_chunks(o); } static void input_handler_cancel (FragmentProtoDisassembler *o) @@ -150,6 +118,7 @@ static void input_handler_cancel (FragmentProtoDisassembler *o) ASSERT(o->in_len >= 0) ASSERT(!o->out) + // set no input packet o->in_len = -1; } @@ -167,7 +136,7 @@ static void output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data) return; } - work_chunks(o); + write_chunks(o); } static void timer_handler (FragmentProtoDisassembler *o) @@ -176,8 +145,10 @@ static void timer_handler (FragmentProtoDisassembler *o) ASSERT(o->out) ASSERT(o->in_len = -1) - // finish output packet + // set no output packet o->out = NULL; + + // finish output PacketRecvInterface_Done(&o->output, o->out_used); } |