Add transport module: TCP framing, thread-per-connection, inbound limit
transport_server_create/start: binds TCP, spawns accept thread, closes excess inbound connections when max_connections is reached. transport_connect: outbound TCP, spawns read thread before returning. transport_send_frame: packs 8-byte header with serial put_*, then writes header + payload under a per-connection mutex (thread-safe). Read thread: reads header, validates payload_length <= max_payload, mallocs payload, calls on_frame (callback owns and must free payload). On error or disconnect calls on_disconnect then frees conn. transport_cli: server mode echoes received frames; client mode sends 3 test frames and prints echoes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
ROOT := $(abspath ../..)
|
||||
include $(ROOT)/common.mk
|
||||
|
||||
CLI_BUILD = $(BUILD)/cli
|
||||
COMMON_OBJ = $(BUILD)/common/error.o
|
||||
MEDIA_CTRL_OBJ = $(BUILD)/media_ctrl/media_ctrl.o
|
||||
V4L2_CTRL_OBJ = $(BUILD)/v4l2_ctrl/v4l2_ctrl.o
|
||||
CLI_BUILD = $(BUILD)/cli
|
||||
COMMON_OBJ = $(BUILD)/common/error.o
|
||||
MEDIA_CTRL_OBJ = $(BUILD)/media_ctrl/media_ctrl.o
|
||||
V4L2_CTRL_OBJ = $(BUILD)/v4l2_ctrl/v4l2_ctrl.o
|
||||
SERIAL_OBJ = $(BUILD)/serial/serial.o
|
||||
TRANSPORT_OBJ = $(BUILD)/transport/transport.o
|
||||
|
||||
.PHONY: all clean modules
|
||||
|
||||
all: modules $(CLI_BUILD)/media_ctrl_cli $(CLI_BUILD)/v4l2_ctrl_cli
|
||||
all: modules \
|
||||
$(CLI_BUILD)/media_ctrl_cli \
|
||||
$(CLI_BUILD)/v4l2_ctrl_cli \
|
||||
$(CLI_BUILD)/transport_cli
|
||||
|
||||
modules:
|
||||
$(MAKE) -C $(ROOT)/src/modules/common
|
||||
$(MAKE) -C $(ROOT)/src/modules/media_ctrl
|
||||
$(MAKE) -C $(ROOT)/src/modules/v4l2_ctrl
|
||||
$(MAKE) -C $(ROOT)/src/modules/serial
|
||||
$(MAKE) -C $(ROOT)/src/modules/transport
|
||||
|
||||
$(CLI_BUILD)/media_ctrl_cli: media_ctrl_cli.c $(COMMON_OBJ) $(MEDIA_CTRL_OBJ) | $(CLI_BUILD)
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
@@ -21,8 +28,14 @@ $(CLI_BUILD)/media_ctrl_cli: media_ctrl_cli.c $(COMMON_OBJ) $(MEDIA_CTRL_OBJ) |
|
||||
$(CLI_BUILD)/v4l2_ctrl_cli: v4l2_ctrl_cli.c $(COMMON_OBJ) $(V4L2_CTRL_OBJ) | $(CLI_BUILD)
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
$(CLI_BUILD)/transport_cli: transport_cli.c $(COMMON_OBJ) $(SERIAL_OBJ) $(TRANSPORT_OBJ) | $(CLI_BUILD)
|
||||
$(CC) $(CFLAGS) -o $@ $^ -lpthread
|
||||
|
||||
$(CLI_BUILD):
|
||||
mkdir -p $@
|
||||
|
||||
clean:
|
||||
rm -f $(CLI_BUILD)/media_ctrl_cli $(CLI_BUILD)/v4l2_ctrl_cli
|
||||
rm -f \
|
||||
$(CLI_BUILD)/media_ctrl_cli \
|
||||
$(CLI_BUILD)/v4l2_ctrl_cli \
|
||||
$(CLI_BUILD)/transport_cli
|
||||
|
||||
159
dev/cli/transport_cli.c
Normal file
159
dev/cli/transport_cli.c
Normal file
@@ -0,0 +1,159 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "transport.h"
|
||||
#include "error.h"
|
||||
|
||||
/* -- server ---------------------------------------------------------------- */
|
||||
|
||||
static void server_on_frame(struct Transport_Conn *conn,
|
||||
struct Transport_Frame *frame, void *userdata)
|
||||
{
|
||||
(void)userdata;
|
||||
|
||||
printf("recv type=0x%04x channel=%u length=%u",
|
||||
frame->message_type, frame->channel_id, frame->payload_length);
|
||||
|
||||
if (frame->payload_length > 0) {
|
||||
uint32_t show = frame->payload_length < 8 ? frame->payload_length : 8;
|
||||
printf(" [");
|
||||
for (uint32_t i = 0; i < show; i++) {
|
||||
printf("%02x", frame->payload[i]);
|
||||
}
|
||||
if (frame->payload_length > 8) { printf("..."); }
|
||||
printf("]");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
struct App_Error err = transport_send_frame(conn,
|
||||
frame->message_type, frame->channel_id,
|
||||
frame->payload, frame->payload_length);
|
||||
|
||||
if (!APP_IS_OK(err)) {
|
||||
fprintf(stderr, "echo failed (errno %d)\n", err.detail.syscall.err_no);
|
||||
}
|
||||
|
||||
free(frame->payload);
|
||||
}
|
||||
|
||||
static void server_on_connect(struct Transport_Conn *conn, void *userdata) {
|
||||
(void)conn; (void)userdata;
|
||||
printf("client connected\n");
|
||||
}
|
||||
|
||||
static void server_on_disconnect(struct Transport_Conn *conn, void *userdata) {
|
||||
(void)conn; (void)userdata;
|
||||
printf("client disconnected\n");
|
||||
}
|
||||
|
||||
static void cmd_server(int argc, char **argv) {
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: transport_cli server <port> [max_connections]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uint16_t port = (uint16_t)atoi(argv[0]);
|
||||
int max_conn = argc >= 2 ? atoi(argv[1]) : 8;
|
||||
|
||||
struct Transport_Server_Config config = {
|
||||
.port = port,
|
||||
.max_connections = max_conn,
|
||||
.max_payload = TRANSPORT_DEFAULT_MAX_PAYLOAD,
|
||||
.on_frame = server_on_frame,
|
||||
.on_connect = server_on_connect,
|
||||
.on_disconnect = server_on_disconnect,
|
||||
.userdata = NULL,
|
||||
};
|
||||
|
||||
struct Transport_Server *server;
|
||||
struct App_Error err = transport_server_create(&server, &config);
|
||||
if (!APP_IS_OK(err)) {
|
||||
fprintf(stderr, "transport_server_create: errno %d\n", err.detail.syscall.err_no);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
err = transport_server_start(server);
|
||||
if (!APP_IS_OK(err)) {
|
||||
fprintf(stderr, "transport_server_start: errno %d\n", err.detail.syscall.err_no);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("listening on port %u max_connections=%d\n", port, max_conn);
|
||||
pause();
|
||||
}
|
||||
|
||||
/* -- client ---------------------------------------------------------------- */
|
||||
|
||||
static void client_on_frame(struct Transport_Conn *conn,
|
||||
struct Transport_Frame *frame, void *userdata)
|
||||
{
|
||||
(void)conn; (void)userdata;
|
||||
printf("echo type=0x%04x channel=%u length=%u\n",
|
||||
frame->message_type, frame->channel_id, frame->payload_length);
|
||||
free(frame->payload);
|
||||
}
|
||||
|
||||
static void client_on_disconnect(struct Transport_Conn *conn, void *userdata) {
|
||||
(void)conn; (void)userdata;
|
||||
printf("disconnected\n");
|
||||
}
|
||||
|
||||
static void cmd_client(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: transport_cli client <host> <port>\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const char *host = argv[0];
|
||||
uint16_t port = (uint16_t)atoi(argv[1]);
|
||||
|
||||
struct Transport_Conn *conn;
|
||||
struct App_Error err = transport_connect(&conn, host, port,
|
||||
TRANSPORT_DEFAULT_MAX_PAYLOAD,
|
||||
client_on_frame,
|
||||
client_on_disconnect,
|
||||
NULL);
|
||||
|
||||
if (!APP_IS_OK(err)) {
|
||||
fprintf(stderr, "transport_connect: errno %d\n", err.detail.syscall.err_no);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("connected to %s:%u\n", host, port);
|
||||
|
||||
for (uint16_t i = 0; i < 3; i++) {
|
||||
uint8_t payload[] = { 0xde, 0xad, 0xbe, 0xef, (uint8_t)i };
|
||||
err = transport_send_frame(conn, 0x0001, i, payload, sizeof(payload));
|
||||
if (!APP_IS_OK(err)) {
|
||||
fprintf(stderr, "send failed on frame %u (errno %d)\n",
|
||||
i, err.detail.syscall.err_no);
|
||||
break;
|
||||
}
|
||||
printf("sent type=0x0001 channel=%u length=5\n", i);
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
transport_conn_close(conn);
|
||||
}
|
||||
|
||||
/* -- main ------------------------------------------------------------------ */
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: transport_cli <server|client> ...\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "server") == 0) {
|
||||
cmd_server(argc - 2, argv + 2);
|
||||
} else if (strcmp(argv[1], "client") == 0) {
|
||||
cmd_client(argc - 2, argv + 2);
|
||||
} else {
|
||||
fprintf(stderr, "unknown command: %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user