From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
123 lines
2.9 KiB
C
123 lines
2.9 KiB
C
#include "stream.h"
|
|
#include <string.h>
|
|
void ring_reset(byte_ring *ring)
|
|
{
|
|
memset(ring->bytes, 0, sizeof ring->bytes);
|
|
ring->head = 0;
|
|
ring->size = 0;
|
|
}
|
|
size_t ring_space(const byte_ring *ring)
|
|
{
|
|
return STREAM_CAPACITY - ring->size;
|
|
}
|
|
size_t ring_copy(const byte_ring *ring, unsigned char *out, size_t limit)
|
|
{
|
|
size_t count = ring->size;
|
|
size_t index;
|
|
if (count > limit) {
|
|
count = limit;
|
|
}
|
|
for (index = 0; index < count; ++index) {
|
|
size_t source = (ring->head + index) % STREAM_CAPACITY;
|
|
out[index] = ring->bytes[source];
|
|
}
|
|
return count;
|
|
}
|
|
void ring_append(byte_ring *ring, const unsigned char *data, size_t count)
|
|
{
|
|
size_t index;
|
|
for (index = 0; index < count; ++index) {
|
|
size_t target = (ring->head + ring->size) % STREAM_CAPACITY;
|
|
ring->bytes[target] = data[index];
|
|
ring->size += 1;
|
|
}
|
|
}
|
|
void ring_drop(byte_ring *ring, size_t count)
|
|
{
|
|
ring->head = (ring->head + count) % STREAM_CAPACITY;
|
|
ring->size -= count;
|
|
if (ring->size == 0) {
|
|
ring->head = 0;
|
|
}
|
|
}
|
|
void stream_init(stream *value, stream_port port)
|
|
{
|
|
memset(value, 0, sizeof *value);
|
|
value->port = port;
|
|
}
|
|
static void clear_connection(stream *value)
|
|
{
|
|
ring_reset(&value->outgoing);
|
|
ring_reset(&value->incoming);
|
|
value->read_end = 0;
|
|
value->write_end = 0;
|
|
value->last_error = 0;
|
|
value->accepted = 0;
|
|
value->received = 0;
|
|
value->delivered = 0;
|
|
}
|
|
int stream_open(stream *value)
|
|
{
|
|
if (value->opened) {
|
|
return STREAM_BUSY;
|
|
}
|
|
clear_connection(value);
|
|
value->opened = 1;
|
|
value->generation += 1;
|
|
return STREAM_OK;
|
|
}
|
|
int stream_close(stream *value)
|
|
{
|
|
if (!value->opened) {
|
|
return STREAM_CLOSED;
|
|
}
|
|
clear_connection(value);
|
|
value->opened = 0;
|
|
return STREAM_OK;
|
|
}
|
|
int stream_queue(stream *value, const unsigned char *bytes, size_t count)
|
|
{
|
|
if (count > STREAM_CAPACITY) {
|
|
return STREAM_INVALID;
|
|
}
|
|
if (!value->opened || value->write_end) {
|
|
return STREAM_CLOSED;
|
|
}
|
|
if (count > ring_space(&value->outgoing)) {
|
|
return STREAM_FULL;
|
|
}
|
|
ring_append(&value->outgoing, bytes, count);
|
|
return STREAM_OK;
|
|
}
|
|
int stream_take(stream *value, unsigned char *bytes, size_t count, size_t *taken)
|
|
{
|
|
*taken = 0;
|
|
if (count > STREAM_CAPACITY) {
|
|
return STREAM_INVALID;
|
|
}
|
|
if (!value->opened) {
|
|
return STREAM_CLOSED;
|
|
}
|
|
*taken = ring_copy(&value->incoming, bytes, count);
|
|
ring_drop(&value->incoming, *taken);
|
|
value->delivered += *taken;
|
|
return STREAM_OK;
|
|
}
|
|
int stream_finish(stream *value)
|
|
{
|
|
int result;
|
|
if (!value->opened || value->write_end) {
|
|
return STREAM_CLOSED;
|
|
}
|
|
if (value->outgoing.size != 0) {
|
|
return STREAM_BUSY;
|
|
}
|
|
result = value->port.shutdown_write(value->port.context);
|
|
if (result < 0) {
|
|
value->last_error = errno;
|
|
return STREAM_FAULT;
|
|
}
|
|
value->write_end = 1;
|
|
return STREAM_OK;
|
|
}
|