some refactoring

This commit is contained in:
Lennart Buhl 2013-11-02 21:16:12 +01:00
parent 38ca85a7b3
commit 2aae8fbc4c
8 changed files with 37 additions and 35 deletions

View File

@ -46,8 +46,8 @@ static void print_current_image(const struct message* msg, int start, int end) {
void callback(const struct message *msg, const struct prog_info *pinfo) {
printf("in callback, tst=%d\n", msg->timestamp);
int start;
// calculate the actual offset to use
int start = msg->timestamp % msg->width + pinfo->client_offset;
print_current_image(msg, start, start+80);

View File

@ -13,19 +13,19 @@
int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
pinfo->mode = MODE_SERVER;
pinfo->client_num = 1;
pinfo->client_offset = 1;
pinfo->port = 4711;
pinfo->fps = 10;
if (argc <= 1) {
printf("usage: %s [-s|-c <off>] [-p <port>] [-t <fps>]\n", argv[0]);
printf("where:\n -s run in server mode\n");
printf( " -c <num> run in client mode\n");
printf( " num is the column offset to use.\n");
printf( " -c <off> run in client mode\n");
printf( " off is the column offset to use.\n");
printf( " -p <port> use the specified port\n");
printf( " -t <fps> when in server mode: update <fps> times\n");
printf( " per second. Valid range: 2 - 99\n");
printf( " no use in client mode\n");
printf( " ignored in client mode\n");
printf( " -v \n");
printf("\n\n");
return -1;
@ -42,8 +42,8 @@ int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
printf("client number not specified\n");
return -2;
}
pinfo->client_num = (int)strtol(argv[++i], NULL, 10);
if (pinfo->client_num <= 0) {
pinfo->client_offset = (int)strtol(argv[++i], NULL, 10);
if (pinfo->client_offset <= 0) {
printf("invalid client number!\n");
return -3;
}
@ -89,10 +89,10 @@ int main(int argc, char **argv) {
printf("port: %d\n", prog_info.port);
if (prog_info.mode == MODE_SERVER) {
printf("runnin in SERVER mode @%d FPS\n", prog_info.fps);
printf("running in SERVER mode @%d FPS\n", prog_info.fps);
ret = run_server(&prog_info);
} else {
printf("running in CLIENT mode, using client number %d\n", prog_info.client_num);
printf("running in CLIENT mode, using client number %d\n", prog_info.client_offset);
ret = run_client(&prog_info, callback);
}

View File

@ -1,11 +1,12 @@
#ifndef __MISC
# define __MISC
#define __MISC
struct prog_info {
int mode;
int client_num;
int client_offset;
int port;
int fps;
} prog_info;
#endif

View File

@ -90,4 +90,3 @@ int main() {
}
*/

View File

@ -1,5 +1,5 @@
#ifndef __MSG
# define __MSG
#define __MSG
#include <stdint.h>
@ -34,3 +34,4 @@ void serialize_message(struct message *msg,Buffer *b);
*/
#endif

View File

@ -22,11 +22,11 @@
//
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
@ -46,14 +46,14 @@ int run_server(const struct prog_info *pinfo) {
sprintf(portbuf, "%d", pinfo->port);
if ((ret=getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
return 1;
return 1;
}
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("talker: socket");
continue;
}
perror("talker: socket");
continue;
}
break;
}
@ -68,7 +68,7 @@ int run_server(const struct prog_info *pinfo) {
// 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);
}
@ -115,8 +115,8 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct
char portbuf[6];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_DGRAM; // UDP
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_DGRAM; // UDP
hints.ai_flags = AI_PASSIVE;
sprintf(portbuf, "%d", pinfo->port);
@ -159,8 +159,8 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct
char buf[10000];
do {
if ((numbytes = recvfrom(sockfd, buf, 9999 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
perror("recvfrom");
exit(1);
}
//printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s));

View File

@ -1,13 +1,14 @@
#ifndef __NET
# define __NET
#define __NET
#include <sys/socket.h> // struct sockaddr
#include "misc.h" // prog_info
#include "msg.h" // message serialization
#include "display.h" // callback
#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)(const struct message *, const struct prog_info *));
#endif