Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenpiperead.cpp
Go to the documentation of this file.
00001 // 00002 // piperead.cpp 00003 // 00004 // Martin Fuchs, 30.11.2003 00005 // 00006 // Jan Roeloffzen, 26.1.2010 00007 // Pipe client, based on msdn example 00008 00009 00010 #define WIN32_LEAN_AND_MEAN 00011 #include <errno.h> 00012 #include <windows.h> 00013 #include <stdio.h> 00014 00015 #define PIPEREAD_VERSION "0.3" 00016 #define PIPEREAD_NOPIPE (-101) 00017 00018 // This definition currently missing in MinGW. 00019 #ifndef FILE_FLAG_FIRST_PIPE_INSTANCE 00020 #define FILE_FLAG_FIRST_PIPE_INSTANCE 0x00080000 00021 #endif 00022 00023 00024 #define BUFSIZE 1024 00025 00026 static void print_error(DWORD win32_error) 00027 { 00028 fprintf(stderr, "WIN32 error %lu\n", win32_error); 00029 } 00030 00031 static int pipeServer(char *path) 00032 { 00033 HANDLE hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL); 00034 00035 if (hPipe == INVALID_HANDLE_VALUE) { 00036 print_error(GetLastError()); 00037 return 1; 00038 } 00039 00040 for(;;) { 00041 DWORD read; 00042 BYTE buffer[BUFSIZE]; 00043 00044 if (!ReadFile(hPipe, buffer, sizeof(buffer), &read, NULL)) { 00045 DWORD error = GetLastError(); 00046 00047 if (error == ERROR_PIPE_LISTENING) { 00048 Sleep(1000); 00049 } else if (error == ERROR_BROKEN_PIPE) { 00050 CloseHandle(hPipe); 00051 00052 hPipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX|FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_WAIT|PIPE_TYPE_BYTE, 1, 4096, 4096, 30000, NULL); 00053 00054 if (hPipe == INVALID_HANDLE_VALUE) { 00055 fprintf(stderr,"INVALID_HANDLE_VALUE\n"); 00056 print_error(GetLastError()); 00057 return 1; 00058 } 00059 } else { 00060 fprintf(stderr,"error %lu\n",error); 00061 print_error(error); 00062 break; 00063 } 00064 } 00065 00066 if (read) 00067 fwrite(buffer, read, 1, stdout); 00068 } 00069 00070 if (!CloseHandle(hPipe)) 00071 print_error(GetLastError()); 00072 00073 return 0; 00074 } 00075 00076 00077 static int pipeClient(char *path) 00078 { 00079 HANDLE hPipe=INVALID_HANDLE_VALUE; 00080 TCHAR chBuf[BUFSIZE]; 00081 BOOL fSuccess = FALSE; 00082 DWORD cbRead; 00083 DWORD Err; 00084 int res = 0; 00085 00086 setvbuf(stdout, NULL, _IONBF, 0); 00087 while (1) { 00088 hPipe = CreateFile( 00089 path, // pipe name 00090 GENERIC_READ, 00091 0, // no sharing 00092 NULL, // default security attributes 00093 OPEN_EXISTING, // opens existing pipe 00094 0, // default attributes 00095 NULL); // no template file 00096 00097 // Break if the pipe handle is valid. 00098 if (hPipe != INVALID_HANDLE_VALUE) 00099 break; 00100 00101 // Exit if an error other than ERROR_PIPE_BUSY occurs. 00102 Err = GetLastError(); 00103 if (Err != ERROR_PIPE_BUSY) { 00104 if (ERROR_FILE_NOT_FOUND == Err) 00105 { 00106 res = PIPEREAD_NOPIPE; 00107 return res; 00108 } 00109 else 00110 { 00111 fprintf(stderr,"Could not open pipe %s. Error=%lu\n", path, Err ); 00112 res = -1; 00113 } 00114 break; 00115 } 00116 00117 // All pipe instances are busy, so wait for 20 seconds. 00118 if ( ! WaitNamedPipe(path, 20000)) { 00119 fprintf(stderr,"Could not open pipe: 20 second wait timed out."); 00120 res = -2; 00121 break; 00122 } 00123 } 00124 00125 if (!res) do { 00126 fSuccess = ReadFile( 00127 hPipe, // pipe handle 00128 chBuf, // buffer to receive reply 00129 BUFSIZE, // size of buffer 00130 &cbRead, // number of bytes read 00131 NULL); // not overlapped 00132 00133 if ( ! fSuccess ) { 00134 Err = GetLastError(); 00135 if ( Err == ERROR_MORE_DATA ) { 00136 fSuccess = TRUE; 00137 } else { 00138 fprintf(stderr, "ReadFile: Error %lu \n", Err ); 00139 res = -9; 00140 break; 00141 } 00142 } 00143 00144 fwrite(chBuf,1,cbRead,stdout); 00145 } while ( fSuccess); 00146 00147 if ( ! fSuccess) { 00148 fprintf(stderr, "ReadFile from pipe failed. Error=%lu\n", GetLastError() ); 00149 } 00150 00151 if (hPipe != INVALID_HANDLE_VALUE) 00152 CloseHandle(hPipe); 00153 00154 return res; 00155 00156 } 00157 00158 static int fileClient(const char *path) 00159 { 00160 int res = 0; 00161 FILE *fin; 00162 int c; 00163 00164 setvbuf(stdout, NULL, _IONBF, 0); 00165 if (!(fin = fopen(path, "r"))) { 00166 fprintf(stderr,"Could not fopen %s (%s)\n", path, strerror(errno) ); 00167 return -1; 00168 } 00169 00170 while ((c = fgetc(fin)) != EOF) { 00171 fputc(c, stdout); 00172 } 00173 00174 fclose(fin); 00175 return res; 00176 } 00177 00178 void usage(void) 00179 { 00180 fprintf(stderr, "piperead " PIPEREAD_VERSION "\n\n"); 00181 fprintf(stderr, "Usage: piperead [-c] <named pipe>\n"); 00182 fprintf(stderr, "-c means Client mode\n"); 00183 fprintf(stderr, "Example: piperead -c \\\\.\\pipe\\kdbg | log2lines -c\n\n"); 00184 } 00185 00186 int main(int argc, char** argv) 00187 { 00188 char path[MAX_PATH]; 00189 const char* pipe_name; 00190 const char* clientMode; 00191 int res = 0; 00192 00193 pipe_name = "com_1"; 00194 clientMode = NULL; 00195 switch (argc) { 00196 case 3: 00197 clientMode = *++argv; 00198 if (strcmp(clientMode,"-c") != 0) { 00199 clientMode = NULL; 00200 fprintf(stderr,"Invalid option: %s\n", clientMode); 00201 res = -6; 00202 } 00203 //fall through 00204 case 2: 00205 pipe_name = *++argv; 00206 if (strcmp(pipe_name,"-h") == 0) { 00207 res = -7; 00208 } 00209 break; 00210 default: 00211 res = -8; 00212 break; 00213 } 00214 if (res) { 00215 usage(); 00216 return res; 00217 } 00218 00219 if ( pipe_name[0] == '\\' ) { 00220 //assume caller specified full path 00221 sprintf(path, "%s", pipe_name); 00222 } else { 00223 sprintf(path, "\\\\.\\pipe\\%s", pipe_name); 00224 } 00225 00226 if ( clientMode ) { 00227 res = pipeClient(path); 00228 if (res == PIPEREAD_NOPIPE) { 00229 res = fileClient(pipe_name); 00230 } 00231 } else { 00232 res = pipeServer(path); 00233 } 00234 00235 return res; 00236 } Generated on Fri May 25 2012 04:36:10 for ReactOS by
1.7.6.1
|