Removes channel_id from the header. All message-specific identifiers (stream_id, request_id, etc.) now live at the start of the payload, interpreted by each message type handler. A relay seeing an unknown type can skip or forward it using only payload_length, with no knowledge of the payload structure. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
160 lines
4.1 KiB
C
160 lines
4.1 KiB
C
#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 length=%u",
|
|
frame->message_type, 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->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 length=%u\n",
|
|
frame->message_type, 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 (int i = 0; i < 3; i++) {
|
|
uint8_t payload[] = { 0xde, 0xad, 0xbe, 0xef, (uint8_t)i };
|
|
err = transport_send_frame(conn, 0x0001, payload, sizeof(payload));
|
|
if (!APP_IS_OK(err)) {
|
|
fprintf(stderr, "send failed on frame %d (errno %d)\n",
|
|
i, err.detail.syscall.err_no);
|
|
break;
|
|
}
|
|
printf("sent type=0x0001 length=5\n");
|
|
}
|
|
|
|
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;
|
|
}
|