Beispiel #1
0
static void
pipe_read_test (int filetype, const char *ext)
{	static short data [PIPE_TEST_LEN] ;
	static char buffer [256] ;
	static char filename [256] ;

	SNDFILE	*outfile ;
	SF_INFO sfinfo ;
	int k, retval ;

	snprintf (filename, sizeof (filename), "pipe_in.%s", ext) ;
	print_test_name ("pipe_read_test", filename) ;

	sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
	sfinfo.channels = 1 ;
	sfinfo.samplerate = 44100 ;

	for (k = 0 ; k < PIPE_TEST_LEN ; k++)
		data [k] = PIPE_INDEX (k) ;

	outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, __LINE__) ;
	test_writef_short_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
	sf_close (outfile) ;

	snprintf (buffer, sizeof (buffer), "cat %s | ./stdin_test %s ", filename, ext) ;
	if ((retval = system (buffer)) != 0)
	{	retval = WEXITSTATUS (retval) ;
		printf ("\n\n    Line %d : pipe test returned error for file type \"%s\".\n\n", __LINE__, ext) ;
		exit (retval) ;
		} ;

	unlink (filename) ;
	puts ("ok") ;

	return ;
} /* pipe_read_test */
Beispiel #2
0
void *StreamClient(void *pipefd)
{
  int sockfd_connect=-1;
  fd_set readfds, readfds_temp;
  char buf[HTTP_LEN]={0};
  char buf1[HTTP_LEN]={0};
  char str1[PORT_LEN]={0}, str2[PORT_LEN]={0};
  int length=0;
  int reqfd=0, resfd=0;
  ip_t ip_remote = ip[REMOTE][TCP];
  port_t port_remote = port[REMOTE][TCP];
  struct timeval timeout={10,0}, timeout_temp={0};
  int ret=0;

  reqfd = ((int*)pipefd)[PIPE_INDEX(REQUEST,READ)];
  resfd = ((int*)pipefd)[PIPE_INDEX(RESPONSE,WRITE)];

  sockfd_connect = EstablishConnect(ip_remote, port_remote, SOCK_STREAM);

  FD_ZERO(&readfds);

  FD_SET_P(sockfd_connect, &readfds);

  FD_SET_P(reqfd, &readfds);

  while(1) {

    timeout_temp = timeout;
    readfds_temp = readfds;

    ret = select(FD_SETSIZE, &readfds_temp, NULL, NULL, NULL);
    handle_error_nn(ret, 1, "TRANSMIT","select() %s",strerror(errno));

    if(ret == 0) {
      log4c_cdn(mycat, error, "TRANSMIT", "select() timeout");
      exit(-1);
    }

    //monitor sockfd_connect
    if(FD_ISSET(sockfd_connect, &readfds_temp)) {
      memset(buf,0,sizeof(buf));
      length = read(sockfd_connect, buf, sizeof(buf)-1);
      handle_error_nn(length, 0, "TRANSMIT","read() %s",strerror(errno));
      log4c_cdn(mycat, info, "TRANSMIT", "receiving <== connect=%d, length=%d", sockfd_connect, length);
      log4c_cdn(mycat, debug, "TRANSMIT", "Packet content is\n%s",buf);

      if(length <= 0) {
        ret = sd_close(sockfd_connect);
        handle_error_nn(ret, 1, "TRANSMIT","close() %s",strerror(errno));
        FD_CLR_P(sockfd_connect, &readfds);

        sockfd_connect = EstablishConnect(ip_remote, port_remote, SOCK_STREAM);
        FD_SET_P(sockfd_connect, &readfds);
      }
      else if(length == (sizeof(buf)-1)) {
        log4c_cdn(mycat, error, "socket", "http response is too large to store");
        exit(-1);
      }
      else {
        //deal with http packet
        length = write(resfd, buf, strlen(buf));
        handle_error_nn(length, 1, "TRANSMIT","write() %s",strerror(errno));
        log4c_cdn(mycat, info, "TRANSMIT", "sending ==> resfd=%d, length=%d", resfd,length);
      }

    }
    //monitor request fd
    /*else*/ if(FD_ISSET(reqfd, &readfds_temp)) {
      memset(buf,0,sizeof(buf));
      length = read(reqfd, buf, sizeof(buf)-1);
      handle_error_nn(length, 1, "TRANSMIT","read() %s",strerror(errno));
      log4c_cdn(mycat, info, "TRANSMIT", "receiving <== reqfd=%d length=%d", reqfd, length);

      if(length == 0) {
        ret = sd_close(reqfd);
        handle_error_nn(ret, 1, "TRANSMIT","close() %s",strerror(errno));
        FD_CLR_P(reqfd, &readfds);
      }
      else {
        strcpy(buf1, buf);
        strrpl(buf, buf1, "close", "keep-alive");
        strcpy(buf1, buf);
        strrpl(buf, buf1, "Close", "keep-alive");
        strcpy(buf1, buf);
        strrpl(buf, buf1, ip[LOCAL][TCP], server);
        strcpy(buf1, buf);
        sprintf(str1,"%hd",port[LOCAL][TCP]);
        sprintf(str2,"%hd",port[REMOTE][TCP]);
        strrpl(buf, buf1, str1, str2);

        length = write(sockfd_connect, buf, strlen(buf));
        handle_error_nn(length, 1, "TRANSMIT","write() %s",strerror(errno));
        log4c_cdn(mycat, info, "TRANSMIT", "sending ==> connect=%d, length=%d", sockfd_connect,length);
        log4c_cdn(mycat, debug, "TRANSMIT", "Packet content is\n%s",buf);
      }

    }
  }
  return NULL;
}
Beispiel #3
0
static	void
stdin_test	(int typemajor, int count)
{	static	short	data [BUFFER_LEN] ;

	SNDFILE		*file ;
	SF_INFO		sfinfo ;
	int			k, total, err ;

	if (typemajor == SF_FORMAT_RAW)
	{	sfinfo.samplerate	= 44100 ;
		sfinfo.format		= SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
		sfinfo.channels		= 1 ;
		sfinfo.frames		= 0 ;
	}
	else
		memset (&sfinfo, 0, sizeof (sfinfo)) ;

#if USE_WINDOWS_API
	if ((file = sf_open_fd (fileno (stdin), SFM_READ, &sfinfo, SF_TRUE)) == NULL)
#else
	if ((file = sf_open_fd (STDIN_FILENO, SFM_READ, &sfinfo, SF_TRUE)) == NULL)
#endif
	{	fprintf (stderr, "sf_open_fd failed with error : ") ;
		puts (sf_strerror (NULL)) ;
		dump_log_buffer (NULL) ;
		exit (1) ;
	} ;

	err = sf_error (file) ;
	if (err != SF_ERR_NO_ERROR)
	{	printf ("Line %d : unexpected error : %s\n", __LINE__, sf_error_number (err)) ;
		exit (1) ;
	} ;

	if ((sfinfo.format & SF_FORMAT_TYPEMASK) != typemajor)
	{	fprintf (stderr, "\n\nError : File type doesn't match.\n") ;
		exit (1) ;
	} ;

	if (sfinfo.samplerate != 44100)
	{	fprintf (stderr, "\n\nError : Sample rate (%d) should be 44100\n", sfinfo.samplerate) ;
		exit (1) ;
	} ;

	if (sfinfo.channels != 1)
	{	fprintf (stderr, "\n\nError : Channels (%d) should be 1\n", sfinfo.channels) ;
		exit (1) ;
	} ;

	if (sfinfo.frames < count)
	{	fprintf (stderr, "\n\nError : Sample count (%ld) should be %d\n", (long) sfinfo.frames, count) ;
		exit (1) ;
	} ;

	total = 0 ;
	while ((k = sf_read_short (file, data + total, BUFFER_LEN - total)) > 0)
		total += k ;

	if (total != count)
	{	fprintf (stderr, "\n\nError : Expected %d frames, read %d.\n", count, total) ;
		exit (1) ;
	} ;

	for (k = 0 ; k < total ; k++)
		if (data [k] != PIPE_INDEX (k))
		{	printf ("\n\nError : data [%d] == %d, should have been %d.\n\n", k, data [k], k) ;
			exit (1) ;
		} ;

	sf_close (file) ;

	return ;
} /* stdin_test */
Beispiel #4
0
static void
useek_pipe_rw_double (const char * ext, SF_INFO * psfinfo_write, SF_INFO * psfinfo_read)
{	static double buffer [PIPE_TEST_LEN] ;
	static double data [PIPE_TEST_LEN] ;
	SNDFILE *outfile ;
	SNDFILE *infile_piped ;

	int k, status = 0 ;
	int pipefd [2] ;
	pid_t pida ;

	for (k = 0 ; k < PIPE_TEST_LEN ; k++)
		data [k] = PIPE_INDEX (k) ;

	/*
	** Create the pipe.
	*/
	pipe (pipefd) ;

	/*
	** Attach the write end of the pipe to be written to.
	*/
	if ((outfile = sf_open_fd (pipefd [1], SFM_WRITE, psfinfo_write, SF_TRUE)) == NULL)
	{	printf ("\n\n%s %d : unable to create unseekable pipe for write type \"%s\".\n", __func__, __LINE__, ext) ;
		printf ("\t%s\n\n", sf_strerror (outfile)) ;
		exit (1) ;
		} ;

	if (sf_error (outfile) != SF_ERR_NO_ERROR)
	{	printf ("\n\n%s %d : unable to open unseekable pipe for write type \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	/*
	** Attach the read end of the pipe to be read from.
	*/
	if ((infile_piped = sf_open_fd (pipefd [0], SFM_READ, psfinfo_read, SF_TRUE)) == NULL)
	{	printf ("\n\n%s %d : unable to create unseekable pipe for read type. \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	if (sf_error (infile_piped) != SF_ERR_NO_ERROR)
	{	printf ("\n\n%s %d : unable to open unseekable pipe for read type \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	/* Fork a child process that will write directly into the pipe. */
	if ((pida = fork ()) == 0) /* child process */
	{	test_writef_double_or_die (outfile, 0, data, PIPE_TEST_LEN, __LINE__) ;
		exit (0) ;
		} ;

	/* In the parent process, read from the pipe and compare what is read
	** to what is written, if they match everything went as planned.
	*/
	test_readf_double_or_die (infile_piped, 0, buffer, PIPE_TEST_LEN, __LINE__) ;
	if (memcmp (buffer, data, sizeof (buffer)) != 0)
	{	printf ("\n\n%s %d : unseekable pipe test failed for file type \"%s\".\n\n", __func__, __LINE__, ext) ;
		exit (1) ;
		} ;

	/* Wait for the child process to return. */
	waitpid (pida, &status, 0) ;
	status = WEXITSTATUS (status) ;
	sf_close (outfile) ;
	sf_close (infile_piped) ;

	if (status != 0)
	{	printf ("\n\n%s %d : status of child process is %d for file type %s.\n\n", __func__, __LINE__, status, ext) ;
		exit (1) ;
		} ;

	return ;
} /* useek_pipe_rw_double */