BlinkenArea - GitList
Repositories
Blog
Wiki
BlinkenLib
Code
Commits
Branches
Tags
Search
Tree:
bad3255
Branches
Tags
master
v0.1
v0.2
v0.3
v0.3.1
v0.4
v0.4.1
v0.5
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.7.0
v0.7.1
v0.7.10
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.8.0
v0.8.1
BlinkenLib
BlinkenSend.c
BlinkenLib v.0.5.4 (2008-01-10)
Christian Heimke
commited
bad3255
at 2011-07-15 09:05:43
BlinkenSend.c
Blame
History
Raw
/* BlinkenLib * version 0.5.4 date 2008-01-10 * Copyright 2004-2008 Stefan Schuermans <stefan@schuermans.info> * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html * a blinkenarea.org project */ #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #include <winsock2.h> #define close closesocket #define strcasecmp stricmp #else #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #endif #include "BlinkenLib.h" int main( int argCnt, char * * args ) { int i, connected; SOCKET udpSocket; etBlinkenProto proto; unsigned int maxidle, send_cnt, loop_cnt, loop, ui; char txt[64]; unsigned short port; struct sockaddr_in addr; stBlinkenMovie * pMovie; //print info printf( "BlinkenLib - BlinkenSend\n" "version 0.5.4 date 2008-01-10\n" "Copyright 2004-2008 Stefan Schuermans <stefan@schuermans.info>\n" "Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html\n" "a blinkenarea.org project\n\n" ); //print syntax if( argCnt <= 1 ) { printf( "syntax: %s <parameter> [...]\n\n" "parameters:\n" " -s [<ip>:]<port>\n" " source address (defaults to 0.0.0.0:0)\n" " must occur before -d and -i, may only occur once\n" " -d <ip>[:<port>]\n" " destination addess (defaults to 127.0.0.1:2323)\n" " -p [BLP|EBLP|MCUF]\n" " protocol to use (defaults to MCUF)\n" " -t <milliseconds>\n" " set maximum idle time between frames (defaults to 3000, use 0 for infinite)\n" " -n <number>\n" " set number of times to send movies (defaults to 1)\n" " -i <file>\n" " read movie from file (*.blm, *.bmm, *.bml, *.bbm) and send it\n" " -l <number>\n" " loop number of times (defaults to 1, use 0 for forever)\n\n", args[0] ); return 0; } #ifdef WIN32 { WSADATA WsaData; WSAStartup( 0x0101, &WsaData ); } #endif //create udp socket udpSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); if( udpSocket == INVALID_SOCKET ) { printf( "cannot create UDP socket\n" ); #ifdef WIN32 WSACleanup( ); #endif return -1; } //loop loop_cnt = 1; for( loop = 0; loop < loop_cnt || loop_cnt == 0; loop++ ) { //print loop message if( loop_cnt > 1 ) printf( "--- loop %u/%u ---\n", loop + 1, loop_cnt ); if( loop_cnt == 0 ) printf( "--- loop %u ---\n", loop + 1 ); //process parameters connected = 0; proto = BlinkenProtoMcuf; maxidle = 3000; send_cnt = 1; for( i = 1; i < argCnt; i++ ) { //source address if( strcmp( args[i], "-s" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 ) { addr.sin_family = AF_INET; addr.sin_port = htons( port ); addr.sin_addr.s_addr = inet_addr( txt ); if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set source address to \"%s\"\n", args[i] ); } else if( sscanf( args[i], "%hu", &port ) == 1 ) { addr.sin_family = AF_INET; addr.sin_port = htons( port ); addr.sin_addr.s_addr = htonl( INADDR_ANY ); if( bind( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set source address to \"%s\"\n", args[i] ); } else printf( "invalid source address \"%s\"\n", args[i] ); } else printf( "missing source address for \"-s\"\n" ); } //destination address else if( strcmp( args[i], "-d" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%32[0-9.]:%hu", txt, &port ) == 2 ) { addr.sin_family = AF_INET; addr.sin_port = htons( port ); addr.sin_addr.s_addr = inet_addr( txt ); if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set destination address to \"%s\"\n", args[i] ); else connected = 1; } else if( sscanf( args[i], "%32[0-9.]", txt ) == 1 ) { addr.sin_family = AF_INET; addr.sin_port = htons( 2323 ); addr.sin_addr.s_addr = inet_addr( txt ); if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set destination address to \"%s\"\n", args[i] ); else connected = 1; } else printf( "invalid destination address \"%s\"\n", args[i] ); } else printf( "missing destination address for \"-d\"\n" ); } //protocol to use else if( strcmp( args[i], "-p" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( strcasecmp( args[i], "BLP" ) == 0 ) proto = BlinkenProtoBlp; else if( strcasecmp( args[i], "EBLP" ) == 0 ) proto = BlinkenProtoEblp; else if( strcasecmp( args[i], "MCUF" ) == 0 ) proto = BlinkenProtoMcuf; else printf( "unknown protocol \"%s\"\n", args[i] ); } else printf( "missing protocol for \"-p\"\n" ); } //maximum idle time else if( strcmp( args[i], "-t" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%u", &ui ) == 1 ) maxidle = ui; else printf( "invalid maximum idle time value \"%s\"\n", args[i] ); } else printf( "missing maximum idle time value for \"-t\"\n" ); } //number of times to send movies else if( strcmp( args[i], "-n" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%u", &ui ) == 1 && ui > 0 ) send_cnt = ui; else printf( "invalid number \"%s\"\n", args[i] ); } else printf( "missing number for \"-n\"\n" ); } //read movie and send it else if( strcmp( args[i], "-i" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( ! connected ) //try to connect if not yet connected { printf( "no destination address to sent movie \"%s\" to,\n" " using default destination address \"127.0.0.1:2323\"\n", args[i] ); addr.sin_family = AF_INET; addr.sin_port = htons( 2323 ); addr.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); if( connect( udpSocket, (struct sockaddr *)&addr, sizeof( addr ) ) != 0 ) printf( "could not set destination address to \"127.0.0.1:2323\"\n" ); else connected = 1; } if( connected ) { pMovie = BlinkenMovieLoad( args[i] ); if( pMovie == NULL ) printf( "could not read movie \"%s\"\n", args[i] ); else { printf( "movie \"%s\" read\n", args[i] ); for( ui = 0; ui < send_cnt; ui++ ) { printf( "sending movie \"%s\" (%u/%u)...\n", args[i], ui + 1, send_cnt ); BlinkenMovieSend( pMovie, udpSocket, proto, maxidle ); printf( "movie \"%s\" sent\n", args[i] ); } BlinkenMovieFree( pMovie ); } } else printf( "no destination address to sent movie \"%s\" to\n", args[i] ); } else printf( "missing input filename for \"-i\"\n" ); } //number of times to loop else if( strcmp( args[i], "-l" ) == 0 ) { if( i + 1 < argCnt ) { i++; if( sscanf( args[i], "%u", &ui ) == 1 ) loop_cnt = ui; else printf( "invalid number \"%s\"\n", args[i] ); } else printf( "missing number for \"-l\"\n" ); } //unknown parameter else printf( "unknown parameter \"%s\", call without parameters to get help\n", args[i] ); } //for( i ... } //for( loop ... //close socket close( udpSocket ); #ifdef WIN32 WSACleanup( ); #endif return 0; }