Modified test code for fifo to work on Windows.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5685 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2012-10-16 16:43:44 +00:00
parent 0d4489433b
commit 6ab765a96e

View File

@ -4,7 +4,8 @@
// Usage:
// test_fifo -i [movie file]
//
// The program creates a named pipe /tmp/fifo.avi, to be played with e.g. mplayer
// The program creates a named pipe /tmp/fifo.avi on Linux, \\.\pipe\fifo.avi on Windows to be played with e.g. mplayer
// On Windows the mplayer needs the argument -noidx, because the pipe is not seekable
//
#include <sys/types.h>
#include <sys/stat.h>
@ -16,13 +17,21 @@
#include <stdexcept>
#include <stdio.h>
#include <errno.h>
#ifdef WIN32
#include <windows.h>
#endif
#ifdef WIN32
#define myFIFO TEXT("\\\\.\\pipe\\fifo.avi")
#else
#define myFIFO "/tmp/fifo.avi"
#endif
int main(int argc,char *argv[])
{
try
{
int status, num, fifo;
std::string movie_name ;
argstream as(argc,argv) ;
@ -32,6 +41,11 @@ int main(int argc,char *argv[])
as.defaultErrorHandling() ;
#ifdef WIN32
HANDLE fifo = CreateNamedPipe(myFIFO, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_BYTE | PIPE_WAIT , 2, 1024, 1024, 2000, NULL);
#else
int status, num, fifo;
umask(0);
if (mkfifo(myFIFO, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP))
throw std::runtime_error("Cannot create fifo") ;
@ -39,13 +53,30 @@ int main(int argc,char *argv[])
fifo = open(myFIFO, O_WRONLY);
std::cerr << "Opening fifo = " << fifo << " with file name " << myFIFO << std::endl;
#endif
#ifdef WIN32
if(fifo == INVALID_HANDLE_VALUE)
#else
if(fifo < 0)
#endif
{
printf("\nCan't open fifo: %s \n", strerror(errno));
return 0;
}
#ifdef WIN32
printf("pipe=%p\n", fifo);
printf("connect...\n");
bool connected = ConnectNamedPipe(fifo, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if (!connected) {
printf("ConnectNamedPipe failed. error=%ld\n", GetLastError());
return -1;
}
printf("...connected\n");
#endif
static const int SIZE = 1024*1024 ; // 1 MB
void *membuf = malloc(SIZE) ;
@ -62,8 +93,15 @@ int main(int argc,char *argv[])
if(n == 0)
break ;
if(write(fifo,membuf,SIZE) != n)
#ifdef WIN32
DWORD dwNumberOfBytesWritten;
if (!WriteFile(fifo, membuf, n, &dwNumberOfBytesWritten, NULL) || dwNumberOfBytesWritten != (DWORD) n) {
printf("WriteFile failed. error=%ld\n", GetLastError());
#else
if(write(fifo,membuf,SIZE) != n) {
#endif
throw std::runtime_error("Could not write chunk to fifo") ;
}
std::cerr << "Streamed chunk " << chunk << std::endl;
if(n < SIZE)
@ -75,10 +113,20 @@ int main(int argc,char *argv[])
fclose(f) ;
free(membuf) ;
std::cerr << "Sleeping 60 sec." << std::endl;
sleep(60) ;
#ifdef WIN32
// FlushFileBuffers(fifo);
DisconnectNamedPipe(fifo);
#endif
std::cerr << "Sleeping 60 sec." << std::endl;
#ifdef WIN32
Sleep(60000) ;
CloseHandle(fifo);
#else
sleep(60) ;
close(fifo) ;
#endif
return 0 ;
}
catch(std::exception& e)