From 66a4e9bf50331e623a1c08da82f2c0431065fe5f Mon Sep 17 00:00:00 2001 From: Lennart Buhl Date: Sun, 3 Nov 2013 16:06:36 +0100 Subject: [PATCH] some refactoring --- src/display.c | 22 +++++++++++----------- src/ger.h | 35 ----------------------------------- src/main.c | 4 ++-- src/net.c | 32 +++++++++++++++----------------- 4 files changed, 28 insertions(+), 65 deletions(-) delete mode 100644 src/ger.h diff --git a/src/display.c b/src/display.c index 6f2dce3..faeba70 100644 --- a/src/display.c +++ b/src/display.c @@ -5,7 +5,6 @@ #include "msg.h" #include "display.h" -#include "ger.h" void setup_display() { initscr(); // ncurses initialization @@ -23,20 +22,20 @@ void cleanup_display() { uint32_t height; // normally 25, may vary char *image; // dimension is width x height */ -statisch nichts print_current_image(konstant struktur message* msg, zahl start, zahl end) { +static void print_current_image(const struct message* msg, int start, int end) { end = 80; // end is ignored and 80 is used, should be handled properly later //move(0,0); // start in the upper left corner - zeichen *pic = msg->image; // get a ptr to the actual image + char *pic = msg->image; // get a ptr to the actual image - fuer (zahl row=0; row < 25; row++) { // iterate over rows - zeichen *original_line = pic + row * msg->width; // pointer to the start of the line to print - zeichen *line = (zeichen*) reserviere(end-start+1); // allocate a line because we modify it + for (int row=0; row < 25; row++) { // iterate over rows + char *original_line = pic + row * msg->width; // pointer to the start of the line to print + char *line = (char*) malloc(end-start+1); // allocate a line because we modify it strcpy(line, original_line + start); // get a line from the right start line[end-start] = '\0'; // terminate it mvprintw(row, 0, "%s", line); // print it - befreie(line); // free it + free(line); // free it } refresh(); // refresh the screen @@ -50,8 +49,8 @@ void callback(const struct message *msg, const struct prog_info *pinfo) { // calculate the actual offset to use //int start = msg->timestamp % msg->width + pinfo->client_offset; - //print_current_image(msg, start, start+80); + prntscreen(msg, pinfo); } @@ -79,6 +78,7 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) { if (!init) { // printf("init start\n"); initscr(); + curs_set(0); // invisible cursor getmaxyx(stdscr, rows, cols); init = 1; @@ -90,16 +90,16 @@ void prntscreen(const struct message *msg, const struct prog_info *pinfo) { int left = right + cols; // for (int y=0; yheight; y++) { for (int x=left-frame; x run in client mode\n"); printf( " off is the column offset to use.\n"); printf( " -p use the specified port\n"); - printf( " -t when in server mode: update times\n"); + printf( " -t when in server mode: update times\n"); printf( " per second. Valid range: 2 - 99\n"); printf( " ignored in client mode\n"); printf( " -w maximum width of the whole field. server only.\n"); - printf( " -i image fielname. server only\n"); + printf( " -i image filename. server only\n"); printf("\n\n"); return -1; } diff --git a/src/net.c b/src/net.c index c7ffaf9..98b0979 100644 --- a/src/net.c +++ b/src/net.c @@ -1,5 +1,6 @@ -#include #include +#include +#include #include // ? #include // geguttenbergt aus bejees networking guide @@ -40,14 +41,14 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) { struct addrinfo hints, *servinfo, *p; int ret; int sockfd; - char portbuf[6]; + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; sprintf(portbuf, "%d", pinfo->port); - if ((ret=getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) { + if ((ret = getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); return 1; } @@ -71,7 +72,7 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) { // Einem Socket muss das Broadcasting explizit erlaubt werden: 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"); exit(1); } @@ -84,19 +85,16 @@ int run_server(const struct prog_info *pinfo, char *img, int w, int h) { t++; t %= pinfo->width; - struct message *outmsg = (struct message *)malloc(sizeof(struct message)); + struct message *outmsg = (struct message *) malloc(sizeof(struct message)); outmsg->timestamp = (uint32_t)t; //(uint32_t)time(NULL); - if ((t % 100) == 0) { - outmsg->width = w; - outmsg->height = h; - outmsg->image = img; - } else { - outmsg->width = 0; - outmsg->height = 0; - outmsg->image = NULL; - } + // send image every 100 frames + bool sendimg = (t % 100) == 0; + outmsg->width = sendimg ? w : 0; + outmsg->height = sendimg ? h : 0; + outmsg->image = sendimg ? img : NULL; + int buflen = getBufferSize(outmsg); - char *outbuf = (char *)malloc(buflen); + char *outbuf = (char *) malloc(buflen); serialize(outbuf, outmsg); if ((numbytes = sendto(sockfd, outbuf, buflen, 0, p->ai_addr, p->ai_addrlen)) == -1) { @@ -143,12 +141,12 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct for (info = servinfo; info != NULL; info = info->ai_next) { if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) { perror("sock"); - continue; + continue; } if (bind(sockfd, info->ai_addr, info->ai_addrlen) == -1) { perror("bind"); - continue; + continue; } break;