| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- /**
- * @file StreamRecvConnector.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 <stddef.h>
- #include <misc/debug.h>
- #include <flow/StreamRecvConnector.h>
- static int output_handler_recv (StreamRecvConnector *o, uint8_t *data, int data_avail)
- {
- ASSERT(o->out_avail == -1)
- ASSERT(!o->input || !o->in_blocking)
- ASSERT(data_avail > 0)
-
- // if we have no input, remember output data
- if (!o->input) {
- o->out_avail = data_avail;
- o->out = data;
- return 0;
- }
-
- // try to receive data
- int res;
- while (1) {
- DEAD_ENTER_N(obj, o->dead)
- DEAD_ENTER_N(inp, o->input_dead)
- res = StreamRecvInterface_Receiver_Recv(o->input, data, data_avail);
- DEAD_LEAVE_N(obj, o->dead);
- DEAD_LEAVE_N(inp, o->input_dead);
- if (DEAD_KILLED_N(obj)) {
- return -1;
- }
- if (DEAD_KILLED_N(inp)) {
- if (!o->input) {
- // lost input
- o->out_avail = data_avail;
- o->out = data;
- return 0;
- }
- // got a new input, retry
- continue;
- }
- break;
- };
-
- ASSERT(res >= 0)
- ASSERT(res <= data_avail)
-
- if (res == 0) {
- // input blocking
- o->out_avail = data_avail;
- o->out = data;
- o->in_blocking = 1;
- return 0;
- }
-
- return res;
- }
- static void input_handler_done (StreamRecvConnector *o, int data_len)
- {
- ASSERT(o->out_avail > 0)
- ASSERT(o->input)
- ASSERT(o->in_blocking)
- ASSERT(data_len > 0)
- ASSERT(data_len <= o->out_avail)
-
- // have no output packet
- o->out_avail = -1;
-
- // input not blocking any more
- o->in_blocking = 0;
-
- // allow output to receive more packets
- StreamRecvInterface_Done(&o->output, data_len);
- return;
- }
- static void job_handler (StreamRecvConnector *o)
- {
- ASSERT(o->input)
- ASSERT(!o->in_blocking)
- ASSERT(o->out_avail > 0)
-
- // try to receive data
- DEAD_ENTER_N(obj, o->dead)
- DEAD_ENTER_N(inp, o->input_dead)
- int res = StreamRecvInterface_Receiver_Recv(o->input, o->out, o->out_avail);
- DEAD_LEAVE_N(obj, o->dead);
- DEAD_LEAVE_N(inp, o->input_dead);
- if (DEAD_KILLED_N(obj)) {
- return;
- }
- if (DEAD_KILLED_N(inp)) {
- // lost current input. Do nothing here.
- // If we gained a new one, its own job is responsible for it.
- return;
- }
-
- ASSERT(res >= 0)
- ASSERT(res <= o->out_avail)
-
- if (res == 0) {
- // input blocking
- o->in_blocking = 1;
- return;
- }
-
- // have no output packet
- o->out_avail = -1;
-
- // allow output to receive more data
- StreamRecvInterface_Done(&o->output, res);
- return;
- }
- void StreamRecvConnector_Init (StreamRecvConnector *o, BPendingGroup *pg)
- {
- // init dead var
- DEAD_INIT(o->dead);
-
- // init output
- StreamRecvInterface_Init(&o->output, (StreamRecvInterface_handler_recv)output_handler_recv, o);
-
- // have no output packet
- o->out_avail = -1;
-
- // have no input
- o->input = NULL;
-
- // init continue job
- BPending_Init(&o->continue_job, pg, (BPending_handler)job_handler, o);
-
- // init debug object
- DebugObject_Init(&o->d_obj);
- }
- void StreamRecvConnector_Free (StreamRecvConnector *o)
- {
- // free debug object
- DebugObject_Free(&o->d_obj);
-
- // free continue job
- BPending_Free(&o->continue_job);
-
- // free input dead var
- if (o->input) {
- DEAD_KILL(o->input_dead);
- }
-
- // free output
- StreamRecvInterface_Free(&o->output);
-
- // free dead var
- DEAD_KILL(o->dead);
- }
- StreamRecvInterface * StreamRecvConnector_GetOutput (StreamRecvConnector *o)
- {
- return &o->output;
- }
- void StreamRecvConnector_ConnectInput (StreamRecvConnector *o, StreamRecvInterface *input)
- {
- ASSERT(!o->input)
-
- // set input
- o->input = input;
-
- // init input
- StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
-
- // init input dead var
- DEAD_INIT(o->input_dead);
-
- // set input not blocking
- o->in_blocking = 0;
-
- // if we have an input packet, set continue job
- if (o->out_avail > 0) {
- BPending_Set(&o->continue_job);
- }
- }
- void StreamRecvConnector_DisconnectInput (StreamRecvConnector *o)
- {
- ASSERT(o->input)
-
- // unset continue job (in case it wasn't called yet)
- BPending_Unset(&o->continue_job);
-
- // free dead var
- DEAD_KILL(o->input_dead);
-
- // set no input
- o->input = NULL;
- }
|