toaruos/userspace/tests/test-pex.c

67 lines
1.8 KiB
C
Raw Normal View History

2014-04-02 11:26:15 +04:00
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
typedef struct packet {
uintptr_t source;
size_t size;
uint8_t data[];
} packet_t;
#define MAX_PACKET_SIZE 1024
#define PACKET_SIZE (sizeof(packet_t) + MAX_PACKET_SIZE)
typedef struct server_write_header {
uintptr_t target;
uint8_t data[];
} header_t;
int main(int argc, char * argv[]) {
FILE * server = fopen("/dev/pex/testex", "a+");
FILE * client = fopen("/dev/pex/testex", "r+");
/* Output is unbuffered so we can write packets discreetly */
setbuf(server, NULL);
setbuf(client, NULL);
2014-04-02 11:47:41 +04:00
fprintf(stderr, "[server = %p]\n", server);
fprintf(stderr, "[client = %p]\n", client);
2014-04-02 11:26:15 +04:00
2014-04-02 11:47:41 +04:00
fprintf(stderr, "Client is sending 'Hello World'.\n");
2014-04-02 11:26:15 +04:00
fprintf(client, "Hello World!");
2014-04-02 11:42:35 +04:00
{
packet_t * p = malloc(PACKET_SIZE);
memset(p, 0x00, PACKET_SIZE);
2014-04-02 11:47:41 +04:00
fprintf(stderr, "Server is reading up to %d...\n", PACKET_SIZE);
2014-04-02 11:42:35 +04:00
size_t size = read(fileno(server), p, PACKET_SIZE);
2014-04-02 11:47:41 +04:00
fprintf(stderr, "Server received a packet of size %d (%d) from client [0x%x]\n", p->size, size, p->source);
fprintf(stderr, "Packet contents: %s\n", p->data);
2014-04-02 11:42:35 +04:00
free(p);
}
{
header_t * broadcast = malloc(sizeof(header_t) + MAX_PACKET_SIZE);
broadcast->target = 0;
2014-04-02 11:47:41 +04:00
fprintf(stderr, "Server is sending broadcast response: 'Hello everyone!\\n'\n");
2014-04-02 11:42:35 +04:00
size_t size = sprintf(broadcast->data, "Hello everyone!\n") + 1;
fwrite(broadcast, 1, sizeof(header_t) + size, server);
free(broadcast);
}
char out[MAX_PACKET_SIZE];
memset(out, 0, MAX_PACKET_SIZE);
2014-04-02 11:47:41 +04:00
fprintf(stderr, "Client is reading up to %d...\n", MAX_PACKET_SIZE);
2014-04-02 11:42:35 +04:00
size_t size = read(fileno(client), out, MAX_PACKET_SIZE);
2014-04-02 11:47:41 +04:00
fprintf(stderr, "Client received response from server (size=%d) %s\n", size, out);
2014-04-02 11:26:15 +04:00
fclose(client);
fclose(server);
return 0;
}