2021-01-02 13:10:00 -05:00
|
|
|
#include "packetstream.h"
|
|
|
|
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
|
|
|
|
2023-05-21 17:36:51 -04:00
|
|
|
#include "uv.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
typedef struct _tf_packetstream_t {
|
|
|
|
tf_packetstream_onreceive_t* onreceive;
|
|
|
|
void* onreceive_user_data;
|
|
|
|
uv_pipe_t stream;
|
|
|
|
char* buffer;
|
|
|
|
size_t buffer_size;
|
2022-02-05 19:46:34 -05:00
|
|
|
size_t buffer_capacity;
|
2021-01-02 13:10:00 -05:00
|
|
|
bool destroyed;
|
|
|
|
} tf_packetstream_t;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_packetstream_t* tf_packetstream_create()
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_packetstream_t* impl = tf_malloc(sizeof(tf_packetstream_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
*impl = (tf_packetstream_t) { 0 };
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_packetstream_destroy(tf_packetstream_t* stream)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
stream->onreceive = NULL;
|
|
|
|
stream->onreceive_user_data = NULL;
|
|
|
|
stream->destroyed = true;
|
2022-02-05 19:46:34 -05:00
|
|
|
if (stream->buffer)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(stream->buffer);
|
2022-02-05 19:46:34 -05:00
|
|
|
stream->buffer = NULL;
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (stream->stream.data)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_close(stream);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(stream);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _packetstream_allocate(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buffer)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
buffer->base = tf_malloc(suggested_size);
|
2021-01-02 13:10:00 -05:00
|
|
|
buffer->len = suggested_size;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _packetstream_process_messages(tf_packetstream_t* stream)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int packet_type = 0;
|
|
|
|
size_t length = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
while (stream->buffer_size >= sizeof(packet_type) + sizeof(length))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
memcpy(&packet_type, stream->buffer, sizeof(packet_type));
|
|
|
|
memcpy(&length, stream->buffer + sizeof(packet_type), sizeof(length));
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (stream->buffer_size >= sizeof(packet_type) + sizeof(length) + length)
|
|
|
|
{
|
|
|
|
if (stream->onreceive)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
stream->onreceive(packet_type, stream->buffer + sizeof(length) + sizeof(packet_type), length, stream->onreceive_user_data);
|
|
|
|
}
|
|
|
|
size_t consumed = sizeof(length) + sizeof(packet_type) + length;
|
|
|
|
memmove(stream->buffer, stream->buffer + consumed, stream->buffer_size - consumed);
|
|
|
|
stream->buffer_size -= consumed;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _packetstream_on_read(uv_stream_t* handle, ssize_t count, const uv_buf_t* buffer)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_t* stream = handle->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (count >= 0)
|
|
|
|
{
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2022-02-05 19:46:34 -05:00
|
|
|
char* write_buffer = stream->buffer;
|
|
|
|
if (stream->buffer_size + count > stream->buffer_capacity)
|
|
|
|
{
|
|
|
|
if (!stream->buffer_capacity)
|
|
|
|
{
|
|
|
|
stream->buffer_capacity = 256;
|
|
|
|
}
|
|
|
|
while (stream->buffer_capacity < stream->buffer_size + count)
|
|
|
|
{
|
|
|
|
stream->buffer_capacity *= 2;
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
write_buffer = tf_realloc(write_buffer, stream->buffer_capacity);
|
2022-02-05 19:46:34 -05:00
|
|
|
}
|
|
|
|
if (write_buffer)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-02-05 19:46:34 -05:00
|
|
|
memcpy(write_buffer + stream->buffer_size, buffer->base, count);
|
|
|
|
stream->buffer = write_buffer;
|
2021-01-02 13:10:00 -05:00
|
|
|
stream->buffer_size += count;
|
|
|
|
}
|
|
|
|
_packetstream_process_messages(stream);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_close(stream);
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(buffer->base);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_packetstream_start(tf_packetstream_t* stream)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
stream->stream.data = stream;
|
|
|
|
uv_read_start((uv_stream_t*)&stream->stream, _packetstream_allocate, _packetstream_on_read);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _packetstream_on_write(uv_write_t* request, int status)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
void tf_packetstream_send(tf_packetstream_t* stream, int packet_type, const char* begin, size_t length)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t buffer_length = sizeof(uv_write_t) + sizeof(packet_type) + sizeof(length) + length;
|
2022-06-04 13:04:51 -04:00
|
|
|
uv_write_t* request = tf_malloc(buffer_length);
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(request, 0, sizeof(uv_write_t));
|
|
|
|
char* buffer = (char*)(request + 1);
|
|
|
|
memcpy(buffer, &packet_type, sizeof(packet_type));
|
|
|
|
memcpy(buffer + sizeof(packet_type), &length, sizeof(length));
|
2021-10-10 17:51:38 -04:00
|
|
|
if (length)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
memcpy(buffer + sizeof(packet_type) + sizeof(length), begin, length);
|
|
|
|
}
|
|
|
|
uv_buf_t write_buffer;
|
|
|
|
write_buffer.base = buffer;
|
|
|
|
write_buffer.len = sizeof(packet_type) + sizeof(length) + length;
|
|
|
|
uv_write(request, (uv_stream_t*)&stream->stream, &write_buffer, 1, _packetstream_on_write);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_packetstream_set_on_receive(tf_packetstream_t* stream, tf_packetstream_onreceive_t* callback, void* user_data)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
stream->onreceive = callback;
|
|
|
|
stream->onreceive_user_data = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_packetstream_handle_closed(uv_handle_t* handle)
|
|
|
|
{
|
|
|
|
tf_packetstream_t* packetstream = handle->data;
|
|
|
|
handle->data = NULL;
|
2022-02-05 19:46:34 -05:00
|
|
|
if (packetstream->buffer)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(packetstream->buffer);
|
2022-02-05 19:46:34 -05:00
|
|
|
packetstream->buffer = NULL;
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (packetstream->destroyed)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(packetstream);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_packetstream_close(tf_packetstream_t* stream)
|
|
|
|
{
|
|
|
|
if (stream->stream.data && !uv_is_closing((uv_handle_t*)&stream->stream))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_close((uv_handle_t*)&stream->stream, _tf_packetstream_handle_closed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
uv_pipe_t* tf_packetstream_get_pipe(tf_packetstream_t* stream)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return &stream->stream;
|
|
|
|
}
|