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