00001 // MouseClient.cpp: implementation of the MouseClient class. 00004 00005 00006 #include "stdafx.h" 00007 #include "MouseClient.h" 00008 00010 // Construction/Destruction 00012 00017 MouseClient::MouseClient(const TString &name) 00018 :PClient( TString( TEXT("MOUSE") ) + name, false) 00019 { 00020 semName = TString( TEXT("MOUSESEM") ) + name; 00021 hasMessage = NULL; 00022 } 00023 00027 MouseClient::~MouseClient() 00028 { 00029 if(hasMessage != NULL) 00030 CloseHandle(hasMessage); 00031 } 00032 00034 // Member Functions 00036 00037 DWORD MouseClient::connect() 00038 { 00039 DWORD success = PClient::connect(); 00040 00041 if( PC_CONNECTED == success ) { 00042 hasMessage = OpenSemaphore(SEMAPHORE_ALL_ACCESS, false, semName); 00043 if( hasMessage == NULL ){ 00044 PClient::disconnect(); 00045 success = PC_FAILED; 00046 } 00047 } 00048 00049 return success; 00050 } 00051 00052 void MouseClient::disconnect() 00053 { 00054 if(hasMessage != NULL) 00055 CloseHandle(hasMessage); 00056 PClient::disconnect(); 00057 } 00058 00063 DWORD MouseClient::deQueueMessage(mouseMessage *message) 00064 { 00065 if( !isConnected() ){ 00066 return MI_NOT_CONNECTED; 00067 } 00068 00069 DWORD numBytesRead; 00070 00071 if( message == NULL ) { 00072 return MI_INVALID_PTR; 00073 00074 } else if( WAIT_OBJECT_0 == WaitForSingleObject(hasMessage, 0) ) { 00075 00076 if( ReadFile( 00077 hPipe, // Read from 00078 message, // data buffer 00079 sizeof(mouseMessage), // number of bytes to read 00080 &numBytesRead, // number of bytes read 00081 NULL) ) { 00082 00083 if( numBytesRead != sizeof(mouseMessage) ) { 00084 return MI_UNKNOWN_ERROR; 00085 } else { 00086 return MI_SUCCESS; 00087 } 00088 } else if(ERROR_READ_FAULT == GetLastError()) { 00089 disconnect(); 00090 /* \todo decide what to do (throw exception?) */ 00091 } 00092 return MI_UNKNOWN_ERROR; 00093 } else { 00094 return MI_NO_MESSAGE; 00095 } 00096 }