1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
const int nBuff = 1024;
extern "C" int DoSrv( char * pIn );
extern "C" int DoSrvMore( char * pOut, size_t nMax );
void PipeServer()
{
HANDLE hPipeToSrv;
HANDLE hPipeFromSrv;
static const TCHAR pipeNameToSrv[] = _T("\\\\.\\pipe\\ToSrvPipe");
hPipeToSrv = CreateNamedPipe(
pipeNameToSrv ,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
PIPE_UNLIMITED_INSTANCES,
nBuff,
nBuff,
50,//Timeout - always send straight away.
NULL);
if( hPipeToSrv == INVALID_HANDLE_VALUE)
return;
static const TCHAR pipeNameFromSrv[] = __T("\\\\.\\pipe\\FromSrvPipe");
hPipeFromSrv = CreateNamedPipe(
pipeNameFromSrv ,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
PIPE_UNLIMITED_INSTANCES,
nBuff,
nBuff,
50,//Timeout - always send straight away.
NULL);
if( hPipeFromSrv == INVALID_HANDLE_VALUE)
return;
BOOL bConnected;
BOOL bSuccess;
DWORD cbBytesRead;
DWORD cbBytesWritten;
CHAR chRequest[ nBuff ];
CHAR chResponse[ nBuff ];
int jj=0;
for(;;)
{
// open to (incoming) pipe first.
printf( "Obtaining pipe\n" );
bConnected = ConnectNamedPipe(hPipeToSrv, NULL) ?
TRUE : (GetLastError()==ERROR_PIPE_CONNECTED );
printf( "Obtained to-srv %i\n", bConnected );
// open from (outgoing) pipe second. This could block if there is no reader.
bConnected = ConnectNamedPipe(hPipeFromSrv, NULL) ?
TRUE : (GetLastError()==ERROR_PIPE_CONNECTED );
printf( "Obtained from-srv %i\n", bConnected );
if( bConnected )
{
for(;;)
{
printf( "About to read\n" );
bSuccess = ReadFile( hPipeToSrv, chRequest, nBuff, &cbBytesRead, NULL);
chRequest[ cbBytesRead] = '\0';
if( !bSuccess || cbBytesRead==0 )
break;
printf( "Rxd %s\n", chRequest );
DoSrv( chRequest );
jj++;
while( true )
{
int nWritten = DoSrvMore( chResponse, nBuff );
if( nWritten <= 1 )
break;
WriteFile( hPipeFromSrv, chResponse, nWritten-1, &cbBytesWritten, NULL);
}
//FlushFileBuffers( hPipeFromSrv );
}
FlushFileBuffers( hPipeToSrv );
DisconnectNamedPipe( hPipeToSrv );
FlushFileBuffers( hPipeFromSrv );
DisconnectNamedPipe( hPipeFromSrv );
break;
}
else
{
CloseHandle( hPipeToSrv );
CloseHandle( hPipeFromSrv );
}
}
CloseHandle( hPipeToSrv );
CloseHandle( hPipeFromSrv );
}
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
const char fifotmpl[] = "/tmp/audacity_script_pipe.%s.%d";
const int nBuff = 1024;
extern "C" int DoSrv( char * pIn );
extern "C" int DoSrvMore( char * pOut, size_t nMax );
void PipeServer()
{
FILE *fromFifo = NULL;
FILE *toFifo = NULL;
int rc;
char buf[nBuff];
char toFifoName[nBuff];
char fromFifoName[nBuff];
sprintf(toFifoName, fifotmpl, "to", getuid());
sprintf(fromFifoName, fifotmpl, "from", getuid());
unlink(toFifoName);
unlink(fromFifoName);
// TODO avoid symlink security issues?
rc = mkfifo(fromFifoName, S_IRWXU) & mkfifo(toFifoName, S_IRWXU);
if (rc < 0)
{
perror("Unable to create fifos");
printf("Ignoring...");
// return;
}
// open to (incoming) pipe first.
toFifo = fopen(toFifoName, "r");
if (toFifo == NULL)
{
perror("Unable to open fifo to server from script");
if (fromFifo != NULL)
fclose(fromFifo);
return;
}
// open from (outgoing) pipe second. This could block if there is no reader.
fromFifo = fopen(fromFifoName, "w");
if (fromFifo == NULL)
{
perror("Unable to open fifo from server to script");
fclose(toFifo);
return;
}
while (fgets(buf, sizeof(buf), toFifo) != NULL)
{
int len = strlen(buf);
if (len <= 1)
{
continue;
}
buf[len - 1] = '\0';
printf("Server received %s\n", buf);
DoSrv(buf);
while (true)
{
len = DoSrvMore(buf, nBuff);
if (len <= 1)
{
break;
}
printf("Server sending %s",buf);
// len - 1 because we do not send the null character
fwrite(buf, 1, len - 1, fromFifo);
}
fflush(fromFifo);
}
printf("Read failed on fifo, quitting\n");
if (toFifo != NULL)
fclose(toFifo);
if (fromFifo != NULL)
fclose(fromFifo);
unlink(toFifoName);
unlink(fromFifoName);
}
#endif
|