00001
00004
00005
00006 #include "StdAfx.h"
00007 #include "PClient.h"
00008 #include "errorCodes.h"
00009
00011
00013
00020 PClient::PClient(const TString &name, const bool isOut)
00021 : Pipe( name, isOut )
00022 {
00023 hPipe = INVALID_HANDLE_VALUE;
00024 }
00025
00029 PClient::~PClient()
00030 {
00031 disconnect();
00032 }
00033
00035
00037
00042 void PClient::disconnect()
00043 {
00044 if(hPipe != INVALID_HANDLE_VALUE){
00045 CloseHandle(hPipe);
00046 hPipe = INVALID_HANDLE_VALUE;
00047 }
00048 ResetEvent(serverStayConnected);
00049 }
00050
00054 DWORD PClient::connect()
00055 {
00056 if( isConnected() )
00057 return PC_ALREADY_CONNECTED;
00058
00059 if( WaitForSingleObject(clientStayConnected, 0) == WAIT_OBJECT_0 ){
00060 return PC_NOT_RESET;
00061 }
00062
00063 hPipe = CreateFile(
00064 pipeName,
00065 isOutbound ? GENERIC_WRITE : GENERIC_READ,
00066 0,
00067 NULL,
00068 OPEN_EXISTING,
00069 0,
00070 NULL);
00071
00072 if (hPipe == INVALID_HANDLE_VALUE){
00073 if (GetLastError() == ERROR_PIPE_BUSY)
00074 return PC_BUSY;
00075 else
00076 return PC_FAILED;
00077 } else
00078 return PC_CONNECTED;
00079 }
00080
00088 bool PClient::isConnected()
00089 {
00090 if( WaitForSingleObject(serverStayConnected, 0) == WAIT_OBJECT_0 ){
00091 this->disconnect();
00092 }
00093
00094 return hPipe != INVALID_HANDLE_VALUE;
00095 }