some refactoring
This commit is contained in:
parent
38ca85a7b3
commit
2aae8fbc4c
|
@ -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) {
|
void callback(const struct message *msg, const struct prog_info *pinfo) {
|
||||||
printf("in callback, tst=%d\n", msg->timestamp);
|
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);
|
print_current_image(msg, start, start+80);
|
||||||
|
|
||||||
|
|
16
src/main.c
16
src/main.c
|
@ -13,19 +13,19 @@
|
||||||
|
|
||||||
int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
|
int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
|
||||||
pinfo->mode = MODE_SERVER;
|
pinfo->mode = MODE_SERVER;
|
||||||
pinfo->client_num = 1;
|
pinfo->client_offset = 1;
|
||||||
pinfo->port = 4711;
|
pinfo->port = 4711;
|
||||||
pinfo->fps = 10;
|
pinfo->fps = 10;
|
||||||
|
|
||||||
if (argc <= 1) {
|
if (argc <= 1) {
|
||||||
printf("usage: %s [-s|-c <off>] [-p <port>] [-t <fps>]\n", argv[0]);
|
printf("usage: %s [-s|-c <off>] [-p <port>] [-t <fps>]\n", argv[0]);
|
||||||
printf("where:\n -s run in server mode\n");
|
printf("where:\n -s run in server mode\n");
|
||||||
printf( " -c <num> run in client mode\n");
|
printf( " -c <off> run in client mode\n");
|
||||||
printf( " num is the column offset to use.\n");
|
printf( " off is the column offset to use.\n");
|
||||||
printf( " -p <port> use the specified port\n");
|
printf( " -p <port> use the specified port\n");
|
||||||
printf( " -t <fps> when in server mode: update <fps> times\n");
|
printf( " -t <fps> when in server mode: update <fps> times\n");
|
||||||
printf( " per second. Valid range: 2 - 99\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( " -v \n");
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -42,8 +42,8 @@ int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
|
||||||
printf("client number not specified\n");
|
printf("client number not specified\n");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
pinfo->client_num = (int)strtol(argv[++i], NULL, 10);
|
pinfo->client_offset = (int)strtol(argv[++i], NULL, 10);
|
||||||
if (pinfo->client_num <= 0) {
|
if (pinfo->client_offset <= 0) {
|
||||||
printf("invalid client number!\n");
|
printf("invalid client number!\n");
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
@ -89,10 +89,10 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
printf("port: %d\n", prog_info.port);
|
printf("port: %d\n", prog_info.port);
|
||||||
if (prog_info.mode == MODE_SERVER) {
|
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);
|
ret = run_server(&prog_info);
|
||||||
} else {
|
} 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);
|
ret = run_client(&prog_info, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#ifndef __MISC
|
#ifndef __MISC
|
||||||
# define __MISC
|
#define __MISC
|
||||||
|
|
||||||
struct prog_info {
|
struct prog_info {
|
||||||
int mode;
|
int mode;
|
||||||
int client_num;
|
int client_offset;
|
||||||
int port;
|
int port;
|
||||||
int fps;
|
int fps;
|
||||||
} prog_info;
|
} prog_info;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef __MSG
|
#ifndef __MSG
|
||||||
# define __MSG
|
#define __MSG
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -34,3 +34,4 @@ void serialize_message(struct message *msg,Buffer *b);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
26
src/net.c
26
src/net.c
|
@ -22,11 +22,11 @@
|
||||||
//
|
//
|
||||||
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) {
|
||||||
return &(((struct sockaddr_in*)sa)->sin_addr);
|
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);
|
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));
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||||
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
||||||
perror("talker: socket");
|
perror("talker: socket");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ int run_server(const struct prog_info *pinfo) {
|
||||||
|
|
||||||
// Einem Socket muss das Broadcasting explizit erlaubt werden:
|
// 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);
|
||||||
}
|
}
|
||||||
|
@ -115,8 +115,8 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct
|
||||||
char portbuf[6];
|
char portbuf[6];
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_INET; // IPv4
|
hints.ai_family = AF_INET; // IPv4
|
||||||
hints.ai_socktype = SOCK_DGRAM; // UDP
|
hints.ai_socktype = SOCK_DGRAM; // UDP
|
||||||
hints.ai_flags = AI_PASSIVE;
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
sprintf(portbuf, "%d", pinfo->port);
|
sprintf(portbuf, "%d", pinfo->port);
|
||||||
|
@ -159,8 +159,8 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct
|
||||||
char buf[10000];
|
char buf[10000];
|
||||||
do {
|
do {
|
||||||
if ((numbytes = recvfrom(sockfd, buf, 9999 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
|
if ((numbytes = recvfrom(sockfd, buf, 9999 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
|
||||||
perror("recvfrom");
|
perror("recvfrom");
|
||||||
exit(1);
|
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));
|
//printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s));
|
||||||
|
|
11
src/net.h
11
src/net.h
|
@ -1,13 +1,14 @@
|
||||||
#ifndef __NET
|
#ifndef __NET
|
||||||
# define __NET
|
#define __NET
|
||||||
|
|
||||||
#include <sys/socket.h> // struct sockaddr
|
#include <sys/socket.h> // struct sockaddr
|
||||||
#include "misc.h" // prog_info
|
#include "misc.h" // prog_info
|
||||||
#include "msg.h" // message serialization
|
#include "msg.h" // message serialization
|
||||||
#include "display.h" // callback
|
#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)(const struct message *, const struct prog_info *));
|
int run_client(const struct prog_info *pinfo,void(*framecallback)(const struct message *, const struct prog_info *));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user