Ejemplo n.º 1
0
Archivo: tcp4.c Proyecto: fd/telehash-c
// 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;
}
Ejemplo n.º 2
0
bool chunks_newSub(ChunkInfoBlock *chunkInfoBlock, ChunkInfoBlock *containerChunkInfoBlock, uint32 id)
{
  uint64      offset;
  ChunkHeader chunkHeader;

  assert(chunkInfoBlock != NULL);
  assert(containerChunkInfoBlock != NULL);
  assert(containerChunkInfoBlock->mode == CHUNK_MODE_READ);

  /* get current offset */
  if (!chunkInfoBlock->tellFile(chunkInfoBlock->userData,&offset))
  {
    return FALSE;
  }

  /* write chunk header */
  chunkHeader.id   = 0;
  chunkHeader.size = 0;
  if (!chunks_write(chunkInfoBlock,&chunkHeader,CHUNK_HEADER_DEFINITION))
  {
    return FALSE;
  }
  containerChunkInfoBlock->size  += sizeof(ChunkHeader);
  containerChunkInfoBlock->index += sizeof(ChunkHeader);

  /* init */
  chunkInfoBlock->containerChunkInfoBlock = containerChunkInfoBlock;
  chunkInfoBlock->mode                    = CHUNK_MODE_WRITE;
  chunkInfoBlock->offset                  = offset;
  chunkInfoBlock->id                      = id;
  chunkInfoBlock->size                    = 0;
  chunkInfoBlock->index                   = 0;

  return TRUE;
}
Ejemplo n.º 3
0
bool chunks_close(ChunkInfoBlock *chunkInfoBlock)
{
  uint64      oldOffset;
  ChunkHeader chunkHeader;

  assert(chunkInfoBlock != NULL);

  /* write size to chunk-header */
  if (!chunkInfoBlock->tellFile(chunkInfoBlock->userData,&oldOffset))
  {
    return FALSE;
  }
  if (!chunkInfoBlock->seekFile(chunkInfoBlock->userData,chunkInfoBlock->offset))
  {
    chunkInfoBlock->seekFile(chunkInfoBlock->userData,oldOffset);
    return FALSE;
  }
  chunkHeader.id   = chunkInfoBlock->id;
  chunkHeader.size = chunkInfoBlock->size;
  if (!chunks_write(chunkInfoBlock,&chunkHeader,CHUNK_HEADER_DEFINITION))
  {
    chunkInfoBlock->seekFile(chunkInfoBlock->userData,oldOffset);
    return FALSE;
  }
  if (!chunkInfoBlock->seekFile(chunkInfoBlock->userData,oldOffset))
  {
    return FALSE;
  }

  /* set size in container chunk */
  if (chunkInfoBlock->containerChunkInfoBlock != NULL)
  {
    switch (chunkInfoBlock->mode)
    {
      case CHUNK_MODE_WRITE:
        chunkInfoBlock->containerChunkInfoBlock->size  += chunkInfoBlock->size;
        chunkInfoBlock->containerChunkInfoBlock->index += chunkInfoBlock->size;
        break;
      case CHUNK_MODE_READ:
        chunkInfoBlock->containerChunkInfoBlock->index += chunkInfoBlock->size;
        break;
    }
  }

  return TRUE; 
}