void pair_send(pipe_t pipe, lob_t packet, link_t link) { net_loopback_t pair = (net_loopback_t)pipe->arg; if(!pair || !packet || !link) return; LOG("pair pipe from %s",link->id->hashname); if(link->mesh == pair->a) mesh_receive(pair->b,packet,pipe); else if(link->mesh == pair->b) mesh_receive(pair->a,packet,pipe); else lob_free(packet); }
// do all chunk/socket stuff pipe_t tcp4_flush(pipe_t pipe) { int len; lob_t packet; uint8_t buf[256]; pipe_tcp4_t to = tcp4_to(pipe); if(!to) return NULL; if(chunks_len(to->chunks)) { while((len = write(to->client, chunks_write(to->chunks), chunks_len(to->chunks))) > 0) { chunks_written(to->chunks, len); LOG("wrote %d bytes to %s",len,pipe->id); } } while((len = read(to->client, buf, 256)) > 0) { LOG("reading %d bytes from %s",len,pipe->id); chunks_read(to->chunks, buf, len); } // any incoming full packets can be received while((packet = chunks_receive(to->chunks))) mesh_receive(to->net->mesh, packet, pipe); if(len < 0 && errno != EWOULDBLOCK && errno != EINPROGRESS) { LOG("socket error to %s: %s",pipe->id,strerror(errno)); close(to->client); to->client = 0; } return pipe; }
// do all chunk/socket stuff pipe_t serial_flush(pipe_t pipe) { int ret, count; uint8_t len; lob_t packet; uint8_t c1; pipe_serial_t to; if(!pipe || !(to = (pipe_serial_t)pipe->arg)) return NULL; count = 0; while((ret = to->read()) >= 0) { count++; c1 = ret; util_chunks_read(to->chunks, &c1, 1); } if(count) { LOG("read %d bytes from %s",count,pipe->id); } // any incoming full packets can be received while((packet = util_chunks_receive(to->chunks))) mesh_receive(to->net->mesh, packet, pipe); // write the next waiting chunk while((len = util_chunks_len(to->chunks))) { if((ret = to->write(util_chunks_write(to->chunks), len)) > 0) { LOG("wrote %d size chunk to %s",ret,pipe->id); // blocks till next incoming chunk is read before sending more util_chunks_written(to->chunks,len); }else{ LOG("chunk write failed, %d != %d",ret,len); // TODO, write a full chunk of zeros to clear any line errors and reset state? break; } } return pipe; }