Ejemplo n.º 1
0
int main( int argc, char * argv[] )
{
    socketdesc sd;         /* socket descriptor index */
    long n;                /* number of bytes received */
    char sendline[MSGSIZE];   /* send buffer */
    char recvline[MSGSIZE+1]; /* recieve buffer */

    /* need an ip address.. localhost or 127.0.0.1 would work for
       our test case */
    if (argc != 2 )
    {
        fprintf( stderr, "echo <ip address>\n" );
        exit(1);
    }

    initudp();
    sd = openudp( FALSE, argv[1], 2007 );

    if ( sd < 0 )
    {
        fprintf( stderr, "error occured while connecting\n" );
        exit(1);
    }

    while ( fgets( sendline, MSGSIZE, stdin ) != NULL )
    {
        sendudp( sd, sendline, strlen(sendline), argv[1] );
      
        n = recvudp( sd, recvline, MSGSIZE, NULL );
        recvline[n] = '\0';
        fputs(recvline, stdout);
    }

    closeudp( sd );

    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char **argv) {
    int udp_port;		/* port to receive parameter updates from */
		
	struct sockaddr_in  cl_addr;
	int clilen,maxclilen=sizeof(cl_addr);
	struct sockaddr_un  ucl_addr;
	int uclilen,umaxclilen=sizeof(ucl_addr);
	int i,n;
	
		
	clilen = maxclilen;
	uclilen = umaxclilen;

    udp_port = -1;
    for (i=1; i < argc; ++i) {
	if (strcmp(argv[i], "-showbytes") == 0) {
	    ShowBytes = TRUE;
	} else if (strcmp(argv[i], "-silent") == 0 ||
		   strcmp(argv[i], "-quiet") == 0) {
	    Silent = TRUE;
	} else if (udp_port != -1) {
	    goto usageError;
	} else {
	    udp_port = atoi(argv[i]);
	    if (udp_port == 0) {
		goto usageError;
	    }
	}
    }

    if (udp_port == -1) {
	usageError:
	    fprintf(stderr, "Usage\n\tdumpOSC portno [-showbytes] [-quiet]\n\t(responds to udp and UNIX packets on that port no)\n");
	    exit(1);
    }


	n = recvfrom(0, mbuf, MAXMESG, 0, &cl_addr, &clilen);
	if(n>0)
	{
		sockfd = 0;
		udp_port = -1;
		Synthmessage(mbuf, n, &cl_addr, clilen,sockfd) ;
	}
	else
	{	sockfd=initudp(udp_port);
		usockfd=unixinitudp(udp_port);
	}

    if (!Silent) {
	printf("dumpOSC version 0.2 (6/18/97 Matt Wright). Unix/UDP Port %d \n", udp_port);	
	printf("Copyright (c) 1992,1996,1997 Regents of the University of California.\n");
    }
	if(sockfd>=0 && usockfd>=0)
	{
		fd_set read_fds, write_fds;
		int nfds;
#define max(a,b) (((a) > (b)) ? (a) : (b))
		nfds = max(sockfd, usockfd)+ 1;
		{
			int j;
			for(j=0;j<npolldevs;++j)
				if(polldevs[j].fd>=nfds)
				{
						nfds = polldevs[j].fd+1;
/*
printf("polldev %d\n", polldevs[j].fd);
*/
				}
		}
/*
		printf("nfds %d\n", nfds);
*/
		caught_sigint = 0;
   		sigset(SIGINT, catch_sigint);       /* set sig handler       */
	
		while(!caught_sigint)
		{
			
			int r;
 	
		back:	

			FD_ZERO(&read_fds);                /* clear read_fds        */
			FD_ZERO(&write_fds);               /* clear write_fds        */
			FD_SET(sockfd, &read_fds); 
			FD_SET(usockfd, &read_fds); 
		       	{
		        	int j;
		        	
		        	for(j=0;j<npolldevs;++j)
		        		FD_SET(polldevs[j].fd, &read_fds);
		        }
 	      
		        r = select(nfds, &read_fds, &write_fds, (fd_set *)0, 
		                        (struct timeval *)0);
		        if (r < 0)  /* select reported an error */
			    goto out;
		        {
			    int j;
			    
			    for(j=0;j<npolldevs;++j)
				    if(FD_ISSET(polldevs[j].fd, &read_fds))
				    (*(polldevs[j].callbackfunction))(polldevs[j].fd,polldevs[j].dummy );
		        }
		        if(FD_ISSET(sockfd, &read_fds))
			{
				clilen = maxclilen;
				while( (n = recvfrom(sockfd, mbuf, MAXMESG, 0, &cl_addr, &clilen)) >0) 
				{
				  int r;
				  /* printf("received UDP packet of length %d\n",  n); */
				  r = Synthmessage(mbuf, n, &cl_addr, clilen, sockfd) ;

				  if( sgi_HaveToQuit()) goto out;
				  if(r>0) goto back;
				  clilen = maxclilen;
				}
			}
		        if(FD_ISSET(usockfd, &read_fds))
			{
				uclilen = umaxclilen;
				while( (n = recvfrom(usockfd, mbuf, MAXMESG, 0, &ucl_addr, &uclilen)) >0) 
				{
				  int r;
				  /* printf("received UNIX packet of length %d\n",  n); */

				  r=Synthmessage(mbuf, n, &ucl_addr, uclilen,usockfd) ;

				  if( sgi_HaveToQuit()) goto out;
				  if(r>0) goto back;
				  uclilen = umaxclilen;
				}
			}
		} /* End of while(!caught_sigint) */
	   closeudp(sockfd);
		
out: ;
	}
	else
		perror("initudp");
	
	return 0;
}
Ejemplo n.º 3
0
void MainLoop(ALport alp, FileDescriptor dacfd, FileDescriptor sockfd, SynthState *v1, SynthState *v2) {
/*    int hwm = 300, lwm = 256; */
    int hwm = 1000, lwm = 800;
    fd_set read_fds, write_fds;

    /* largest file descriptor to search for */    
    int	nfds = BIGGER_OF(dacfd, sockfd) + 1;

    printf("MainLoop: dacfd %d, sockfd %d, nfds %d\n", dacfd, sockfd, nfds);

    time_to_quit = 0;
    sigset(SIGINT, catch_sigint);       /* set sig handler       */

    while(!time_to_quit) {

	/* compute sine wave samples while the sound output buffer is below
	   the high water mark */

	while (ALgetfilled(alp) < hwm) {
	    Synthesize(alp, v1, v2);
	}

	/* Figure out the time tag corresponding to the time in the future that we haven't
	   computed any samples for yet. */
	OSCInvokeAllMessagesThatAreReady(OSCTT_PlusSeconds(OSCTT_CurrentTime(), 
							   ALgetfilled(alp) / the_sample_rate));

	/* set the low water mark, i.e. when we want control from select(2) */
	ALsetfillpoint(alp, OUTPUTQUEUESIZE - lwm);

	/* set up select */
	FD_ZERO(&read_fds);	/* clear read_fds */
	FD_ZERO(&write_fds);	/* clear write_fds */
	FD_SET(dacfd, &write_fds);
	FD_SET(sockfd, &read_fds); 

	FD_SET(0, &read_fds);	/* stdin */

	/* give control back to OS scheduler to put us to sleep until the DAC
	   queue drains and/or a character is available from standard input */

	if (select(nfds, &read_fds, &write_fds, (fd_set * )0, (struct timeval *)0) < 0) {
	    /* select reported an error */
	    perror("bad select"); 
	    goto quit;
	}

	if(FD_ISSET(sockfd, &read_fds)) {
	    ReceivePacket(sockfd);
	}

	/* is there a character in the queue? */
	if (FD_ISSET(0, &read_fds)) {
	    /* this will never block */
	    char c = getchar();

	    if (c == 'q') {
		/* quit */
		break;
	    } else if ((c <= '9') && (c >= '0')) {
		/* tweak frequency */
		v1->f = 440.0 + 100.0 * (c - '0');
	    }
	}
    }
quit:
    ALcloseport(alp);
    closeudp(sockfd);
}