Example #1
0
void sclient::init(const char* serverip, const short serverport) {
#ifdef _WIN32
    /* Initialisiere TCP für Windows ("winsock") */
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD(2, 0);
    if (WSAStartup(wVersionRequested, &wsaData) != 0){
	  eprintf("Error while init winsock\n");
      exit(1);
    }
	// Check version
	if (wsaData.wVersion != wVersionRequested)
	{
		eprintf("\nWinSock version not supported\n");
		return;
	}
#endif
	//eprintf("opening socket...\n");
	sfd = socket(PF_INET, SOCK_DGRAM, 0);
#ifdef _WIN32
	if ( sfd == SOCKET_ERROR) { eperror("ERROR: socket() call returned SOCKET_ERROR"); }
#else	
	if ( sfd < 0) { eperror("ERROR: socket() call returned %d", sfd); }
#endif

	//eprintf("  creating struct sockaddr_in svraddr...\n");
	svraddr.sin_family = AF_INET;
	svraddr.sin_port = htons(serverport);
#ifdef _WIN32
    unsigned long addr;
    if ((addr = inet_addr(serverip)) != INADDR_NONE) {
        /* serverip ist eine numerische IP-Adresse */
        memcpy( (char *)&svraddr.sin_addr, &addr, sizeof(addr));
    }
#else
	inet_aton(serverip, &(svraddr.sin_addr));
#endif

	//eprintf("  connect socket...\n");
	int connerr = connect(sfd, (struct sockaddr*)&svraddr, sizeof(svraddr));
	if (connerr != 0) { eperror("ERROR: connect returned %d:",connerr); }
	//fprintf(stderr,"connected to %s:%d\n", inet_ntoa(svraddr.sin_addr), ntohs(svraddr.sin_port));

	DefaultSendname[0]=0;
	
	for ( int i=0; i<MAX_CALLBACKS; i++ ) {	// clear callback registrations;
		rxcallback[i]=NULL;
	}
}
Example #2
0
static void handle_attribute(struct uade_attribute **attributelist,
			     const struct epconfattr *attr,
			     char *item, size_t len, size_t lineno)
{
	struct uade_attribute *a;
	char *str, *endptr;
	int success = 0;

	if (item[len] != '=') {
		__android_log_print(ANDROID_LOG_VERBOSE, "UADE", "Invalid song item: %s\n", item);
		return;
	}
	str = item + len + 1;

	if ((a = calloc(1, sizeof *a)) == NULL)
		eperror("No memory for song attribute.\n");

	switch (attr->t) {
	case UA_DOUBLE:
		a->d = strtod(str, &endptr);
		if (*endptr == 0)
			success = 1;
		break;
	case UA_INT:
		a->i = strtol(str, &endptr, 10);
		if (*endptr == 0)
			success = 1;
		break;
	case UA_STRING:
		a->s = strdup(str);
		if (a->s == NULL)
			eperror("Out of memory allocating string option for song\n");
		success = 1;
		break;
	default:
		__android_log_print(ANDROID_LOG_VERBOSE, "UADE", "Unknown song option: %s\n",
			item);
		break;
	}

	if (success) {
		a->type = attr->e;
		a->next = *attributelist;
		*attributelist = a;
	} else {
		__android_log_print(ANDROID_LOG_VERBOSE, "UADE", "Invalid song option: %s\n", item);
		free(a);
	}
}
Example #3
0
void set_fl (signed fd, signed flags) 

{
	signed value;
	if ((value = fcntl (fd, F_GETFL, 0)) < 0) 
	{
		eperror ("fcntl F_GETFL failed.");
		exit (1);
	}
	value |= flags;
	if (fcntl (fd, F_SETFL, value) < 0) 
	{
		eperror ("fcntl F_SETFL failed.");
		exit (1);
	}
	return;
}
Example #4
0
File: server.c Project: acceso/gbot
static void
connectto (struct _server *server)
{
	struct addrinfo hints, *res = NULL;
	struct in6_addr serveraddr;
	char port[6];
	int rc;

	memset (&hints, 0, sizeof (hints));
	hints.ai_flags = AI_NUMERICSERV;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if (inet_pton (AF_INET, server->hostname, &serveraddr) == 1) {
		hints.ai_family = AF_INET;
		hints.ai_flags |= AI_NUMERICHOST;
	} else if (inet_pton (AF_INET6, server->hostname, &serveraddr) == 1) {
		hints.ai_family = AF_INET6;
		hints.ai_flags |= AI_NUMERICHOST;
	}

	snprintf (port, 6, "%d", server->port);

	rc = getaddrinfo (server->hostname, port, &hints, &res);
	if (rc != 0) {
		fprintf (stderr, "Host not found: %s\n", gai_strerror (rc));
		exit (EXIT_FAILURE);
	}

	server->sock = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
	if (server->sock < 0)
		eperror ("socket");

	if (connect (server->sock, res->ai_addr, res->ai_addrlen) < 0)
		eperror ("connect");

	if (res != NULL)
		freeaddrinfo (res);

	return;
}
Example #5
0
int sclient::recvpacket(packet_t& packet) {
	packet.addrlen=sizeof(packet.address);
	errno=0;
	packet.length = recvfrom(sfd, (char*)packet.raw, packet_t::MAX_LENGTH, 0, (struct sockaddr*)&(packet.address), &(packet.addrlen));
	if (errno != 0 || packet.length < 0 ) {
		eperror("recvfrom() returned %d", packet.length);
		packet.length = -1;
	}
	// packet is always stored locally in host byteorder
	packet.type = ntohs(packet.type);
	packet.namelength = ntohs(packet.namelength);
	return packet.length;
}
Example #6
0
int sclient::sendpacket(packet_t& packet) {
	int ret;
	// packet is always sent in network byteorder
	packet.type = htons(packet.type);
	packet.namelength = htons(packet.namelength);
	if ((ret=send(sfd, (const char*)packet.raw, packet.length, 0)) < 0) {
		eperror("ERROR: local send() error");
	}
	// packet convert bytes back to keep packet constant
	packet.type = ntohs(packet.type);
	packet.namelength = ntohs(packet.namelength);
	return ret;
}
int
main(int argc, char **argv)
{
	char *file = NULL;
	int have_video = 0, have_audio = 0;
	unsigned h = 0, w = 0;
	float fps = 0.0;
	unsigned ch = 0, hz = 0;
	struct audiobuf abuf;
	int end = 0;
	SDL_Surface *display = NULL;
	SDL_Overlay *ovl = NULL;
	SDL_Event event;
	mm_file media;

	if (argc > 1)
		file = argv[1];
	else
		usage(argv[0]);

	if (mm_open(&media, file) <= 0)
		eprintf("No audio or video in `%s'\n", file);

	if (mm_video_info(&media, &w, &h, &fps) >= 0)
	{
		printf("Video data: %dx%d, %gfps\n", w, h, fps);
		have_video = 1;
	}

	if (mm_audio_info(&media, &ch, &hz) >= 0)
	{
		printf("Audio data: %s, %dHz\n", (ch == 1) ? "mono" : "stereo", hz);
		have_audio = 1;
	}

	if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
		eprintf("Sdl init failed: %s\n", SDL_GetError());

	if (have_video)
	{
		if ((display = SDL_SetVideoMode(w, h, 24,
					SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
			eprintf("SDL_SetVideoMode: %s\n", SDL_GetError());

		if ((ovl = SDL_CreateYUVOverlay(w, h,
					SDL_YV12_OVERLAY, display)) == NULL)
			eprintf("SDL_CreateYUVOverlay: %s\n", SDL_GetError());
	}

	if (have_audio)
	{
		int bytes;
		int bps;
		double tdiff;
		SDL_AudioSpec desired;

		desired.channels = ch;
		desired.freq = hz;
		desired.format = AUDIO_U8;
		bps = ch * 1;
		desired.samples = 4096;
		desired.callback = audio_cb;
		desired.userdata = &abuf;
		if (SDL_OpenAudio(&desired, NULL) < 0)
			eprintf("SDL_OpenAudio: %s\n", SDL_GetError());

		abuf.size = 4 * 4096;
		abuf.off = 0;
		abuf.bytes = 0;
		abuf.buf = xmalloc(abuf.size);

		tdiff = get_time();
		while ((bytes = mm_decode_audio(&media,
					abuf.buf + abuf.bytes, abuf.size - abuf.bytes)) > 0)
		{
			abuf.bytes += bytes;
			if (abuf.size - abuf.bytes <= 4096)
				abuf.buf = xrealloc(abuf.buf, abuf.size *= 2);
		};
		if (bytes < 0)
			eperror("convert_audio");

		printf("Decoding: %.3f seconds\n", get_time() - tdiff);
		printf("Audio: %d samples, %.2f seconds\n", abuf.bytes / bps,
			(double) (abuf.bytes) / bps / desired.freq);

		SDL_PauseAudio(0);
	}

	while (!end)
	{
		while (SDL_PollEvent(&event))
			switch (event.type)
			{
				case SDL_QUIT:
					end = 1;
					break;
				case SDL_KEYDOWN:
					if (event.key.keysym.sym == SDLK_q
						|| event.key.keysym.sym == SDLK_ESCAPE)
						end = 1;
					break;
				default:
					break;
			}

		if (have_video && !end)
		{
			static double oldt, newt;

			if (mm_decode_video(&media, ovl) > 0)
			{
				SDL_Rect r = { 0, 0, w, h };
				newt = 1 / fps + oldt - get_time();
				if (newt > 0)
					SDL_Delay(newt * 1000);
				SDL_DisplayYUVOverlay(ovl, &r);
				oldt = get_time();
			}
			else
				end = 1;
		}

		if (have_audio && abuf.bytes <= 0)
			end = 1;

		if (!have_video)
			SDL_Delay(100);
	}

	if (have_audio)
	{
		SDL_PauseAudio(1);
		free(abuf.buf);
	}

	if (have_video)
	{
		SDL_FreeYUVOverlay(ovl);
	}

	SDL_Quit();
	mm_close(&media);

	return EXIT_SUCCESS;
}
Example #8
0
int main(int argc, char* argv[]) {
	// init vars and signal handlers
	packet_t packet;
	if ( signal(SIGTERM, sigINT_handler) == SIG_ERR ) {
		eprintf("ERROR: could not set up signal handler for SIGTERM!\n");
		return(-1);
	}
	if ( signal(SIGINT, sigINT_handler) == SIG_ERR ) {
		eprintf("ERROR: could not set up signal handler for SIGINT!\n");
		return(-1);
	}
	// connect sclient
	sclient me;
	if (!me.isOK()) {
		eprintf("could not initialize sclient!\n");
		return -1;
	}
	eprintf("%s initialized\n", argv[0]);
	
	// subscribe data channel
	packet.setName("/thpc/raw_data");
	packet.setData("", 0);
	packet.type = PKT_SUBSCRIBE;
	me.sendpacket(packet);
	
	me.setid("thpc_split");
	
	// select() call setup
	fd_set* readfds = new fd_set();
	fd_set* writefds= new fd_set();
        fd_set* errorfds= new fd_set();
	fd_set* allfds  = new fd_set();
	int maxfd;
	FD_ZERO(allfds);
	//FD_SET(STDIN_FILENO, allfds);
	FD_ZERO(writefds);
	FD_ZERO(errorfds);
	FD_SET(me.getfd(), allfds);
	//maxfd=STDIN_FILENO;
	//if (me.getfd() > maxfd) maxfd=me.getfd();
	maxfd=me.getfd();
	
	//
	// MAIN LOOP
	//
	int noready;
	struct timeval timeout;
	while ( ! wantexit ) {
		memcpy(readfds, allfds, sizeof(fd_set));
		timeout.tv_sec = SELECT_TIMEOUT;
		timeout.tv_usec = 0;
		// SELECT CALL
		noready = select(maxfd+1, readfds, writefds, errorfds, &timeout);

		if        ( noready < 0 ) {	///// ERROR
			if (errno == EINTR) {
				if (wantexit) {
					eprintf("terminating due to SIGINT\n");
					break;
				}
				continue;
			} else {
				eperror("%s:%d: WARNING: terminating due to select() failure", __FILE__, __LINE__);
			}
			break;
		} else if ( noready > 0 ) {	///// DATA
			if ( FD_ISSET(me.getfd(), readfds) ) {
				packet_t rxpacket;
				// read packet
				if ( me.recvpacket(rxpacket) < 0 ) {
					if (errno == ECONNREFUSED) {
						eprintf("\nERROR: SERVER IS DOWN OR REFUSED CONNECTION. ABORTING.\n\n");
						break;
					}
					eprintf("WARNING: Received erroneous packet! Discarding.\n");
					continue;
				}

				time_t sec;
				double T1,H1,T2,H2,P1,P2;
				if (sscanf(rxpacket.data(), "%d %lf %lf %lf %lf %lf %lf",
					&sec, &T1, &H1, &T2, &H2, &P1, &P2) != 7) {
					eprintf("could not parse line.");
				}
				eprintf("time:%d T1=%lf, H1=%lf, T2=%lf, H2=%lf, P1=%lf, P2=%lf\n",
					sec, T1,H1,T2,H2,P1,P2);
				sendpacket(&me, "/thpc/Temp1", sec, T1);
				sendpacket(&me, "/thpc/Humi1", sec, H1);
				sendpacket(&me, "/thpc/Temp2", sec, T2);
				sendpacket(&me, "/thpc/Humi2", sec, H2);
				sendpacket(&me, "/thpc/Pressure1", sec, P1);
				sendpacket(&me, "/thpc/Pressure2", sec, P2);
			}
		} else { //if ( noready== 0 ) {	///// TIMEOUT
			eprintf("no signal.\n");
		}
	} // end main loop
	
	eprintf("program end.\n");
	return 0;
}
Example #9
0
static struct eagleplayer *get_eagleplayer(const char *extension,
					   struct eagleplayerstore *ps)
{
	struct eagleplayermap *uf = ps->map;
	struct eagleplayermap *f;
	struct eagleplayermap key = {.extension = (char *)extension };

	f = bsearch(&key, uf, ps->nextensions, sizeof(uf[0]), ufcompare);
	if (f == NULL)
		return NULL;

	return f->player;
}

/* Read eagleplayer.conf. */
static struct eagleplayerstore *read_eagleplayer_conf(const char *filename)
{
	FILE *f;
	struct eagleplayer *p;
	size_t allocated;
	size_t lineno = 0;
	struct eagleplayerstore *ps = NULL;
	size_t exti;
	size_t i, j;
	int epwarning;

	f = fopen(filename, "r");
	if (f == NULL)
		goto error;

	ps = calloc(1, sizeof ps[0]);
	if (ps == NULL)
		eperror("No memory for ps.");

	allocated = 16;
	if ((ps->players = malloc(allocated * sizeof(ps->players[0]))) == NULL)
		eperror("No memory for eagleplayer.conf file.\n");

	while (1) {
		char **items;
		size_t nitems;

		items = read_and_split_lines(&nitems, &lineno, f, UADE_WS_DELIMITERS);
		if (items == NULL)
			break;

		assert(nitems > 0);

		if (ps->nplayers == allocated) {
			allocated *= 2;
			ps->players = realloc(ps->players, allocated * sizeof(ps->players[0]));
			if (ps->players == NULL)
				eperror("No memory for players.");
		}

		p = &ps->players[ps->nplayers];
		ps->nplayers++;

		memset(p, 0, sizeof p[0]);

		p->playername = strdup(items[0]);
		if (p->playername == NULL)
			uadeerror("No memory for playername.\n");

		for (i = 1; i < nitems; i++) {

			if (strncasecmp(items[i], "prefixes=", 9) == 0) {
				char prefixes[UADE_LINESIZE];
				char *prefixstart = items[i] + 9;
				char *sp, *s;
				size_t pos;

				assert(p->nextensions == 0 && p->extensions == NULL);

				p->nextensions = 0;
				strlcpy(prefixes, prefixstart,
					sizeof(prefixes));
				sp = prefixes;
				while ((s = strsep(&sp, OPTION_DELIMITER)) != NULL) {
					if (*s == 0)
						continue;
					p->nextensions++;
				}

				p->extensions =
				    malloc((p->nextensions +
					    1) * sizeof(p->extensions[0]));
				if (p->extensions == NULL)
					eperror("No memory for extensions.");

				pos = 0;
				sp = prefixstart;
				while ((s = strsep(&sp, OPTION_DELIMITER)) != NULL) {
					if (*s == 0)
						continue;

					p->extensions[pos] = strdup(s);
					if (s == NULL)
						eperror("No memory for prefix.");
					pos++;
				}
				p->extensions[pos] = NULL;
				assert(pos == p->nextensions);

				continue;
			}

			if (strncasecmp(items[i], "comment:", 7) == 0)
				break;

			if (uade_song_and_player_attribute(&p->attributelist, &p->flags, items[i], lineno))
				continue;

			__android_log_print(ANDROID_LOG_VERBOSE, "UADE", "Unrecognized option: %s\n", items[i]);
		}

		for (i = 0; items[i] != NULL; i++)
			free(items[i]);

		free(items);
	}

	fclose(f);

	if (ps->nplayers == 0) {
		free(ps->players);
		free(ps);
		return NULL;
	}

	for (i = 0; i < ps->nplayers; i++)
		ps->nextensions += ps->players[i].nextensions;

	ps->map = malloc(sizeof(ps->map[0]) * ps->nextensions);
	if (ps->map == NULL)
		eperror("No memory for extension map.");

	exti = 0;
	epwarning = 0;
	for (i = 0; i < ps->nplayers; i++) {
		p = &ps->players[i];
		if (p->nextensions == 0) {
			if (epwarning == 0) {
				__android_log_print(ANDROID_LOG_VERBOSE, "UADE",
					"uade warning: %s eagleplayer lacks prefixes in "
					"eagleplayer.conf, which makes it unusable for any kind of "
					"file type detection. If you don't want name based file type "
					"detection for a particular format, use content_detection "
					"option for the line in eagleplayer.conf.\n",
					ps->players[i].playername);
				epwarning = 1;
			}
			continue;
		}
		for (j = 0; j < p->nextensions; j++) {
			assert(exti < ps->nextensions);
			ps->map[exti].player = p;
			ps->map[exti].extension = p->extensions[j];
			exti++;
		}
	}

	assert(exti == ps->nextensions);

	/* Make the extension map bsearch() ready */
	qsort(ps->map, ps->nextensions, sizeof(ps->map[0]), ufcompare);

	return ps;

 error:
	if (ps)
		free(ps->players);
	free(ps);
	if (f != NULL)
		fclose(f);
	return NULL;
}