ssize_t gioReadData (GioEndpoint *endpoint, void *buffer, size_t size, int wait) { ReadDataMethod *method = endpoint->methods->readData; if (!method) return logUnsupportedOperation("readData"); { unsigned char *start = buffer; unsigned char *next = start; while (size) { { unsigned int count = endpoint->input.to - endpoint->input.from; if (count) { if (count > size) count = size; memcpy(next, &endpoint->input.buffer[endpoint->input.from], count); endpoint->input.from += count; next += count; size -= count; continue; } endpoint->input.from = endpoint->input.to = 0; } if (endpoint->input.error) { if (next != start) break; errno = endpoint->input.error; endpoint->input.error = 0; return -1; } { ssize_t result = method(&endpoint->handle, &endpoint->input.buffer[endpoint->input.to], sizeof(endpoint->input.buffer) - endpoint->input.to, (wait? endpoint->options.inputTimeout: 0), 0); if (result > 0) { if (LOG_CATEGORY_FLAG(GENERIC_INPUT)) { logBytes(categoryLogLevel, "generic input", &endpoint->input.buffer[endpoint->input.to], result); } endpoint->input.to += result; wait = 1; } else { if (!result) break; if (errno == EAGAIN) break; endpoint->input.error = errno; } } } if (next == start) errno = EAGAIN; return next - start; } }
void logInputPacket (const void *packet, size_t size) { if (LOG_CATEGORY_FLAG(INPUT_PACKETS)) logPacket("Input Packet", packet, size); }
void logOutputPacket (const void *packet, size_t size) { if (LOG_CATEGORY_FLAG(OUTPUT_PACKETS)) logPacket("Output Packet", packet, size); }