transformed callback func to use message struct instead of timestamp

This commit is contained in:
tkarrass 2013-11-02 19:21:09 +01:00
parent 936c9009e4
commit 1ba0eb9cd0
6 changed files with 40 additions and 12 deletions

View File

@ -1,6 +1,6 @@
#include "display.h" #include "display.h"
void callback(long tst) { void callback(const struct message *msg) {
printf("in callback, tst=%ld\n", tst); printf("in callback, tst=%d\n", msg->timestamp);
} }

View File

@ -2,7 +2,8 @@
#define __DISPLAY #define __DISPLAY
#include <stdio.h> #include <stdio.h>
#include "msg.h"
void callback(long tst); void callback(const struct message *msg);
#endif #endif

View File

@ -13,6 +13,7 @@ void serialize (char *buf, struct message *msg) {
memcpy(&buf[0], &msg->timestamp, 4); memcpy(&buf[0], &msg->timestamp, 4);
memcpy(&buf[4], &msg->width, 4); memcpy(&buf[4], &msg->width, 4);
memcpy(&buf[8], &msg->height, 4); memcpy(&buf[8], &msg->height, 4);
if (msg->width * msg->height)
memcpy(&buf[12], msg->image, msg->width * msg->height); memcpy(&buf[12], msg->image, msg->width * msg->height);
} }
@ -20,8 +21,12 @@ void deserialize (struct message *msg, const char *buf) {
memcpy(&msg->timestamp, &buf[0], 4); memcpy(&msg->timestamp, &buf[0], 4);
memcpy(&msg->width, &buf[4], 4); memcpy(&msg->width, &buf[4], 4);
memcpy(&msg->height, &buf[8], 4); memcpy(&msg->height, &buf[8], 4);
if (msg->width * msg->height) {
msg->image = (char*) malloc(msg->width * msg->height); msg->image = (char*) malloc(msg->width * msg->height);
memcpy(msg->image, &buf[12], msg->width * msg->height); memcpy(msg->image, &buf[12], msg->width * msg->height);
} else {
msg->image = NULL;
}
} }
/* Buffer ist nicht erforderlich /* Buffer ist nicht erforderlich

View File

@ -1,6 +1,8 @@
#ifndef __MSG #ifndef __MSG
# define __MSG # define __MSG
#include <stdint.h>
//typedef struct Buffer { //typedef struct Buffer {
// int size; // int size;
// void *data; // void *data;

View File

@ -14,9 +14,12 @@
#include <time.h> // nanosleep #include <time.h> // nanosleep
#include "misc.h" // prog_info #include "net.h"
#include "display.h" // callback
// kleiner helfer
// auch von bejee geguttenbergt
//
void *get_in_addr(struct sockaddr *sa) void *get_in_addr(struct sockaddr *sa)
{ {
if (sa->sa_family == AF_INET) { if (sa->sa_family == AF_INET) {
@ -26,6 +29,10 @@ void *get_in_addr(struct sockaddr *sa)
return &(((struct sockaddr_in6*)sa)->sin6_addr); return &(((struct sockaddr_in6*)sa)->sin6_addr);
} }
// server mode
// pumpt im for(;;) den status ins eth
//
int run_server(const struct prog_info *pinfo) { int run_server(const struct prog_info *pinfo) {
struct addrinfo hints, *servinfo, *p; struct addrinfo hints, *servinfo, *p;
int ret; int ret;
@ -59,15 +66,18 @@ int run_server(const struct prog_info *pinfo) {
tim.tv_sec = 0; tim.tv_sec = 0;
tim.tv_nsec = 500000000; tim.tv_nsec = 500000000;
// Einem Socket muss das Broadcasting explizit erlaubt werden:
int broadcastPermission = 1; int broadcastPermission = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){ if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){
fprintf(stderr, "setsockopt error"); fprintf(stderr, "setsockopt error");
exit(1); exit(1);
} }
//
for (;;) { for (;;) {
printf("sending...\n"); printf("sending...\n");
int numbytes; int numbytes;
// TODO: Hier den Messageblock erstellen und serialisieren.
if ((numbytes = sendto(sockfd, "bla", 3, 0, p->ai_addr, p->ai_addrlen)) == -1) { if ((numbytes = sendto(sockfd, "bla", 3, 0, p->ai_addr, p->ai_addrlen)) == -1) {
perror("error sending"); perror("error sending");
return -44; return -44;
@ -82,7 +92,11 @@ int run_server(const struct prog_info *pinfo) {
return 0; return 0;
} }
int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
// lauschangriff:
// einfach mal in den äther horchen und alle messages rausnehmen die wo da gibt.
//
int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct message *) ) {
struct addrinfo hints, *servinfo; struct addrinfo hints, *servinfo;
char portbuf[6]; char portbuf[6];
@ -104,7 +118,7 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
struct addrinfo *info; struct addrinfo *info;
int sockfd; int sockfd;
//char s[INET6_ADDRSTRLEN]; // mal drin lassen: falls die ip der gegenstelle relevant werden sollte: char s[INET6_ADDRSTRLEN];
for (info = servinfo; info != NULL; info = info->ai_next) { for (info = servinfo; info != NULL; info = info->ai_next) {
if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) { if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) {
perror("sock"); perror("sock");
@ -141,7 +155,10 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
buf[numbytes] = '\0'; buf[numbytes] = '\0';
//printf("listener: packet contains \"%s\"\n", buf); //printf("listener: packet contains \"%s\"\n", buf);
framecallback(123); struct message *msg = (struct message *)malloc(sizeof(struct message));
deserialize(msg, buf);
framecallback(msg);
} while (strncmp(buf, "exit", 10000)); } while (strncmp(buf, "exit", 10000));

View File

@ -2,9 +2,12 @@
# define __NET # define __NET
#include <sys/socket.h> // struct sockaddr #include <sys/socket.h> // struct sockaddr
#include "misc.h" // prog_info
#include "msg.h" // message serialization
#include "display.h" // callback
void *get_in_addr(struct sockaddr *sa); void *get_in_addr(struct sockaddr *sa);
int run_server(const struct prog_info *pinfo); int run_server(const struct prog_info *pinfo);
int run_client(const struct prog_info *pinfo,void(*framecallback)(long)); int run_client(const struct prog_info *pinfo,void(*framecallback)(const struct message *));
#endif #endif