Commit a5071fc0 authored by Sven Robertz's avatar Sven Robertz
Browse files

added dummy reader/writer on top of UDP, in preparation of a datagram protocol over raw ethernet

parent 5185e5a4
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_udp_reader_writer.h>
#include "gen/simple.h"
//#include "/home/sven/slask/UDP/udp_hack.h"
#include "udp_hack.h"
static void handle_simple_TwoInts(simple_TwoInts *v,void *context) {
printf("Got TwoInts. a=%d, b=%d\n", v->a, v->b);
}
static void handle_simple_IntString(simple_IntString *v,void *context) {
printf("Got IntString. x=%d, s=%s\n", v->x, v->s);
}
int main(int argc, char *argv[]) {
server_context_t *fd;
struct labcomm_decoder *decoder;
void *context = NULL;
int i, j;
char *filename = argv[1];
fd = server_init();
decoder = labcomm_decoder_new(labcomm_udp_reader, fd);
if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return 1;
}
labcomm_decoder_register_simple_TwoInts(decoder, handle_simple_TwoInts, context);
labcomm_decoder_register_simple_IntString(decoder, handle_simple_IntString, context);
printf("Decoding:\n");
labcomm_decoder_run(decoder);
printf("--- End Of File ---:\n");
labcomm_decoder_free(decoder);
server_exit(fd);
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_udp_reader_writer.h>
#include "gen/simple.h"
//#include "/home/sven/slask/UDP/udp_hack.h"
#include "udp_hack.h"
int main(int argc, char *argv[]) {
server_context_t *fd;
struct labcomm_encoder *encoder;
int i, j;
char *filename = argv[1];
fd = client_init();
encoder = labcomm_encoder_new(labcomm_udp_writer, fd);
labcomm_encoder_register_simple_TwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
simple_IntString is;
is.x = 24;
is.s = "Hello, LabComm!";
printf("Encoding IntString, x=%d, s=%s\n", is.x, is.s);
labcomm_encode_simple_IntString(encoder, &is);
simple_TwoInts ti;
ti.a = 13;
ti.b = 37;
printf("Encoding TwoInts, a=%d, b=%d\n", ti.a, ti.b);
labcomm_encode_simple_TwoInts(encoder, &ti);
client_exit(fd);
}
#include <errno.h>
#include <unistd.h>
#include "labcomm.h"
#include "/home/sven/slask/UDP/udp_hack.h"
#define BUFFER_SIZE 2048
int labcomm_udp_reader(
labcomm_reader_t *r,
labcomm_reader_action_t action)
{
int result = -EINVAL;
server_context_t *ctx = r->context;
switch (action) {
case labcomm_reader_alloc: {
r->data = malloc(BUFFER_SIZE);
if (r->data) {
r->data_size = BUFFER_SIZE;
result = r->data_size;
} else {
r->data_size = 0;
result = -ENOMEM;
}
r->count = 0;
r->pos = 0;
} break;
case labcomm_reader_start:
case labcomm_reader_continue: {
if (r->pos < r->count) {
result = r->count - r->pos;
} else {
int err;
r->pos = 0;
err=udp_recv(ctx, r->data, r->data_size);
if (err <= 0) {
r->count = 0;
result = -1;
} else {
r->count = err;
result = r->count - r->pos;
}
}
} break;
case labcomm_reader_end: {
result = 0;
} break;
case labcomm_reader_free: {
free(r->data);
r->data = 0;
r->data_size = 0;
r->count = 0;
r->pos = 0;
result = 0;
} break;
}
return result;
}
int labcomm_udp_writer(
labcomm_writer_t *w,
labcomm_writer_action_t action)
{
int result = 0;
//int *fd = w->context;
server_context_t *ctx = w->context;
switch (action) {
case labcomm_writer_alloc: {
w->data = malloc(BUFFER_SIZE);
if (! w->data) {
result = -ENOMEM;
w->data_size = 0;
w->count = 0;
w->pos = 0;
} else {
w->data_size = BUFFER_SIZE;
w->count = BUFFER_SIZE;
w->pos = 0;
}
} break;
case labcomm_writer_free: {
free(w->data);
w->data = 0;
w->data_size = 0;
w->count = 0;
w->pos = 0;
} break;
case labcomm_writer_start: {
w->pos = 0;
} break;
case labcomm_writer_continue: {
result = udp_send(ctx, w->data, w->pos);
w->pos = 0;
} break;
case labcomm_writer_end: {
result = udp_send(ctx, w->data, w->pos);
w->pos = 0;
} break;
case labcomm_writer_available: {
result = w->count - w->pos;
} break;
}
return result;
}
#include "labcomm.h"
extern int labcomm_udp_reader(
labcomm_reader_t *r,
labcomm_reader_action_t action);
extern int labcomm_udp_writer(
labcomm_writer_t *r,
labcomm_writer_action_t action);
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 9930
void diep(char *s)
{
perror(s);
exit(1);
}
typedef struct {
struct sockaddr_in si_me, si_other;
int s, i, slen;
char buf[BUFLEN];
} server_context_t;
server_context_t* server_init(void)
{
server_context_t* ctx;
ctx = malloc(sizeof(server_context_t));
if(!ctx)
diep("malloc");
ctx->slen=sizeof(struct sockaddr_in);
if ((ctx->s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &ctx->si_me, 0, sizeof(ctx->si_me));
ctx->si_me.sin_family = AF_INET;
ctx->si_me.sin_port = htons(PORT);
ctx->si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(ctx->s, &ctx->si_me, sizeof(ctx->si_me))==-1)
diep("bind");
return ctx;
}
int udp_recv(server_context_t *ctx, char *buf, int len){
int l;
if ((l=recvfrom(ctx->s, buf, len, 0, &ctx->si_other, &ctx->slen))==-1)
diep("recvfrom()");
return l;
// printf("Received packet from %s:%d\nData: %s\n\n",
// inet_ntoa(ctx->si_other.sin_addr), ntohs(ctx->si_other.sin_port), ctx->buf);
}
int server_run(server_context_t *ctx){
for (ctx->i=0; ctx->i<NPACK; ctx->i++) {
int l = udp_recv(ctx, ctx->buf, BUFLEN);
printf("Received %d bytes\n",l);
printf("Received data: %s\n",
ctx->buf);
}
}
int server_exit(server_context_t *ctx) {
close(ctx->s);
free(ctx);
return 0;
}
#define SRV_IP "127.0.0.1"
server_context_t* client_init(void)
{
server_context_t *ctx = malloc(sizeof(server_context_t));
if(!ctx)
diep("malloc");
ctx->slen=sizeof(struct sockaddr_in);
if ((ctx->s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((char *) &ctx->si_other, 0, sizeof(ctx->si_other));
ctx->si_other.sin_family = AF_INET;
ctx->si_other.sin_port = htons(PORT);
if (inet_aton(SRV_IP, &ctx->si_other.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
return ctx;
}
int udp_send(server_context_t* ctx, char *s, int len)
{
int result;
if ( (result=sendto(ctx->s, s, len, 0, &(ctx->si_other), ctx->slen))==-1)
diep("sendto()");
return result;
}
int client_run(server_context_t* ctx)
{
for (ctx->i=0; ctx->i<NPACK; ctx->i++) {
printf("Sending packet %d\n", ctx->i);
// sprintf(ctx->buf, "This is packet %d\n", ctx->i);
// udp_send(ctx);
udp_send(ctx, "Hello, world", 12);
}
return 0;
}
int client_exit(server_context_t* ctx)
{
close(ctx->s);
free(ctx);
return 0;
}
struct hidden;
typedef struct hidden server_context_t;
int udp_send(server_context_t*, char*, int);
int udp_recv(server_context_t*, char*, int);
server_context_t* server_init(void);
int server_run(server_context_t *ctx);
int server_exit(server_context_t *ctx);
server_context_t* client_init(void);
//int client_run()server_context_t* ;
//int client_free(vserver_context_t*);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment