Merge branch 'master' of ssh://bitsetter.de:8090/netsl

Conflicts:
	src/display.c
	src/display.h
This commit is contained in:
Lennart Buhl 2013-11-02 19:49:11 +01:00
commit 85783dedea
6 changed files with 60 additions and 17 deletions

View File

@ -5,6 +5,7 @@
#include "display.h"
<<<<<<< HEAD
void setup_display() {
initscr(); // ncurses initialization
@ -41,7 +42,7 @@ void print_current_image(message* msg, int start, int end) {
}
void callback(long tst) {
printf("in callback, tst=%ld\n", tst);
void callback(const struct message *msg) {
printf("in callback, tst=%d\n", msg->timestamp);
}

View File

@ -1,11 +1,13 @@
#ifndef __DISPLAY
#define __DISPLAY
#include <stdio.h>
#include "msg.h"
void setup_display();
void cleanup_display();
void callback(long tst);
void
void callback(const struct message *msg);
#endif

View File

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

View File

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

View File

@ -14,9 +14,12 @@
#include <time.h> // nanosleep
#include "misc.h" // prog_info
#include "display.h" // callback
#include "net.h"
// kleiner helfer
// auch von bejee geguttenbergt
//
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
@ -26,6 +29,10 @@ void *get_in_addr(struct sockaddr *sa)
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
// server mode
// pumpt im for(;;) den status ins eth
//
int run_server(const struct prog_info *pinfo) {
struct addrinfo hints, *servinfo, *p;
int ret;
@ -57,18 +64,34 @@ int run_server(const struct prog_info *pinfo) {
struct timespec tim;
tim.tv_sec = 0;
tim.tv_nsec = 500000000;
tim.tv_nsec = 50000000;
// Einem Socket muss das Broadcasting explizit erlaubt werden:
int broadcastPermission = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){
fprintf(stderr, "setsockopt error");
exit(1);
}
//
int t = 0;
for (;;) {
printf("sending...\n");
//printf("sending...\n");
int numbytes;
if ((numbytes = sendto(sockfd, "bla", 3, 0, p->ai_addr, p->ai_addrlen)) == -1) {
// TODO: Hier den Messageblock erstellen und serialisieren.
t++;
t %= 10000;
struct message *outmsg = (struct message *)malloc(sizeof(struct message));
outmsg->timestamp = (uint32_t)t; //(uint32_t)time(NULL);
outmsg->width = 0;
outmsg->height = 0;
outmsg->image = NULL;
int buflen = getBufferSize(outmsg);
char *outbuf = (char *)malloc(buflen);
serialize(outbuf, outmsg);
if ((numbytes = sendto(sockfd, outbuf, buflen, 0, p->ai_addr, p->ai_addrlen)) == -1) {
perror("error sending");
return -44;
}
@ -82,7 +105,11 @@ int run_server(const struct prog_info *pinfo) {
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;
char portbuf[6];
@ -104,7 +131,7 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
struct addrinfo *info;
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) {
if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) {
perror("sock");
@ -138,10 +165,13 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
//printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s));
//printf("listener: packet is %d bytes long\n", numbytes);
buf[numbytes] = '\0';
//buf[numbytes] = '\0';
//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));

View File

@ -2,9 +2,12 @@
# define __NET
#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);
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