Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

NamedPipe.cpp

Go to the documentation of this file.
00001 #include <string.h>
00002 #include "OSDependent/NamedPipe.h"
00003 
00004 #define NPIPE_PREFIX    "\\\\.\\pipe\\"
00005 #define NPIPE_MAXSZ     256
00006 
00007 NamedPipe::NamedPipe() :
00008 _pipe(NULL)
00009 {
00010 }
00011 
00012 NamedPipe::~NamedPipe() 
00013 {
00014   if (_is_server)
00015     DisconnectNamedPipe(_pipe);
00016   CloseHandle(_pipe);
00017 }
00018 
00019 long 
00020 NamedPipe::write(char*buffer, long size) 
00021 {
00022   DWORD n = 0;
00023   BOOL ret = 0;
00024 
00025   if (!_is_server || ConnectNamedPipe(_pipe,NULL))
00026     ret = WriteFile(_pipe,buffer,size,&n,NULL);
00027 
00028   if (!ret)
00029      n = -1;
00030 
00031   return (short)n;
00032 }
00033 
00034 long 
00035 NamedPipe::read(char*buffer, long size) 
00036 {
00037   DWORD n = 0;
00038   BOOL ret = 0;
00039   if (!_is_server || ConnectNamedPipe(_pipe,NULL))
00040     ret = ReadFile(_pipe,buffer,size,&n,NULL);
00041 
00042   if (!ret)
00043     n = -1;
00044   return n;
00045 }
00046    
00047 void 
00048 NamedPipe::flush() 
00049 {
00050   FlushFileBuffers(_pipe);
00051 }
00052 
00053 int
00054 NamedPipe::create(const char* name, int mode) 
00055 {
00056   char pname[NPIPE_MAXSZ];
00057   sprintf(pname, "%s%s", NPIPE_PREFIX, name);
00058 
00059   _is_server = 1;
00060   DWORD access_mode = 0;
00061   if (mode & NPIPE_READ)
00062     access_mode |= PIPE_ACCESS_INBOUND;
00063   if (mode & NPIPE_WRITE)
00064     access_mode |= PIPE_ACCESS_OUTBOUND;
00065 
00066   if (timeout == -1)
00067     timeout = 10000; //in milliseconds
00068 
00069   _pipe = CreateNamedPipe(pname,
00070                           access_mode,
00071                           PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,
00072                           1,
00073                           4000, 4000,
00074                           timeout,
00075                           NULL);
00076 
00077   if (_pipe == NULL || _pipe == INVALID_HANDLE_VALUE)
00078     return -1;
00079 
00080   return 0;
00081 }
00082 
00083 int
00084 NamedPipe::connect(const char* name, int mode, long timeout) 
00085 {
00086   char pname[NPIPE_MAXSZ];
00087   sprintf(pname, "%s%s", NPIPE_PREFIX, name);
00088 
00089   _is_server = 0;
00090   DWORD access_mode = 0;
00091   if (mode & NPIPE_READ)
00092     access_mode |= GENERIC_READ;
00093   if (mode & NPIPE_WRITE)
00094     access_mode |= GENERIC_WRITE;
00095 
00096   short tries = 0;
00097   do {
00098     _pipe = CreateFile(pname,access_mode,0,NULL,OPEN_EXISTING,0,NULL);
00099     tries ++;
00100   }
00101   while((_pipe==NULL || _pipe == INVALID_HANDLE_VALUE) &&
00102        (timeout != -1) && (Sleep(timeout/10), tries <= 10));
00103 
00104   if (_pipe == NULL || _pipe == INVALID_HANDLE_VALUE)
00105     return -1;
00106 
00107   return 0;
00108 }
00109 

Generated on Mon Oct 7 09:33:29 2002 for Gaia by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001