PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/gw6c-messaging/src/windows/pipeio.cc

https://github.com/h4ck3rm1k3/gw6c-debian
C++ | 202 lines | 57 code | 28 blank | 117 comment | 12 complexity | 531db9bfe10ae028a308868551971014 MD5 | raw file
  1. // **************************************************************************
  2. // $Id: pipeio.cc,v 1.4 2008/01/08 19:34:01 cnepveu Exp $
  3. //
  4. // Copyright (c) 2007 Hexago Inc. All rights reserved.
  5. //
  6. // For license information refer to CLIENT-LICENSE.TXT
  7. //
  8. // Description:
  9. // Windows implementation of the PipeIO class.
  10. //
  11. // Author: Charles Nepveu
  12. //
  13. // Creation Date: November 2006
  14. // __________________________________________________________________________
  15. // **************************************************************************
  16. #include <gw6cmessaging/pipeio.h>
  17. #include <windows.h>
  18. #include <assert.h>
  19. namespace gw6cmessaging
  20. {
  21. // --------------------------------------------------------------------------
  22. // Function : PipeIO constructor
  23. //
  24. // Description:
  25. // Will initialize a new PipeIO object.
  26. //
  27. // Arguments: (none)
  28. //
  29. // Return values: (N/A)
  30. //
  31. // --------------------------------------------------------------------------
  32. PipeIO::PipeIO( void ) :
  33. IPCServent()
  34. {
  35. }
  36. // --------------------------------------------------------------------------
  37. // Function : PipeIO destructor
  38. //
  39. // Description:
  40. // Will clean-up space allocated during object lifetime.
  41. //
  42. // Arguments: (none)
  43. //
  44. // Return values: (N/A)
  45. //
  46. // --------------------------------------------------------------------------
  47. PipeIO::~PipeIO( void )
  48. {
  49. }
  50. // --------------------------------------------------------------------------
  51. // Function : CanRead
  52. //
  53. // Description:
  54. // Will verify if a Read operation will be blocking. This is done by
  55. // verifying if there's data available for a read operation.
  56. //
  57. // Arguments:
  58. // bCanRead: boolean [OUT], Boolean indicating if there's data to read.
  59. //
  60. // Return values:
  61. // GW6CM_UIS__NOERROR: On successful peek
  62. // GW6CM_UIS_PEEKPIPEFAILED: Upon an IO error on the peek.
  63. //
  64. // --------------------------------------------------------------------------
  65. error_t PipeIO::CanRead( bool& bCanRead ) const
  66. {
  67. error_t retCode = GW6CM_UIS__NOERROR;
  68. DWORD nBytesAvailable;
  69. // Verify IPC handle.
  70. assert( m_Handle != INVALID_HANDLE_VALUE );
  71. // Take a peek at the pipe to see if we've got stuff to read.
  72. if( PeekNamedPipe( m_Handle, NULL, 0, NULL, &nBytesAvailable, NULL ) == 0 )
  73. {
  74. // PeekNamedPipe failed.
  75. retCode = GW6CM_UIS_PEEKPIPEFAILED;
  76. nBytesAvailable = 0;
  77. }
  78. // Set whether we can read or not.
  79. bCanRead = (nBytesAvailable > 0);
  80. // Return operation result.
  81. return retCode;
  82. }
  83. // --------------------------------------------------------------------------
  84. // Function : CanWrite
  85. //
  86. // Description:
  87. // Will verify if a Write operation will be blocking.
  88. //
  89. // Arguments:
  90. // bCanWrite: boolean [OUT], Boolean indicating if it's possible to write.
  91. //
  92. // Return values:
  93. // GW6CM_UIS__NOERROR: On successful peek
  94. // GW6CM_UIS_PEEKPIPEFAILED: Upon an IO error on the peek.
  95. //
  96. // --------------------------------------------------------------------------
  97. error_t PipeIO::CanWrite( bool& bCanWrite ) const
  98. {
  99. error_t retCode = GW6CM_UIS__NOERROR;
  100. DWORD dummy;
  101. // Verify IPC handle.
  102. assert( m_Handle != INVALID_HANDLE_VALUE );
  103. bCanWrite = true;
  104. // This (dummy) call to PeekNamedPipe will verify if the handle is valid.
  105. if( PeekNamedPipe( m_Handle, NULL, 0, NULL, &dummy, NULL ) == 0 )
  106. {
  107. // PeekNamedPipe failed.
  108. retCode = GW6CM_UIS_PEEKPIPEFAILED;
  109. bCanWrite = false;
  110. }
  111. // -----------------------------------------------------------
  112. // Writing should not block, except if send buffer is full...
  113. // ... and there's no way of knowing that.
  114. // -----------------------------------------------------------
  115. // Return operation result.
  116. return retCode;
  117. }
  118. // --------------------------------------------------------------------------
  119. // Function : Read
  120. //
  121. // Description:
  122. // Will attempt to receive data from the pipe.
  123. //
  124. // Arguments:
  125. // pvReadBuffer: void* [OUT], The receiving buffer.
  126. // nBufferSize: int [IN], The size in bytes allocated for read at the
  127. // receive buffer.
  128. //
  129. // Return values:
  130. // GW6CM_UIS__NOERROR: Operation successful.
  131. // Any other value is an error.
  132. //
  133. // --------------------------------------------------------------------------
  134. error_t PipeIO::Read( void* pvReadBuffer, const uint32_t nBufferSize, uint32_t& nRead )
  135. {
  136. // Verify IPC handle.
  137. assert( m_Handle != INVALID_HANDLE_VALUE );
  138. // Read from pipe.
  139. if( ReadFile( m_Handle, pvReadBuffer, (DWORD)nBufferSize, (DWORD*)(&nRead), NULL ) == 0 )
  140. {
  141. return GW6CM_UIS_READPIPEFAILED;
  142. }
  143. // Operation successful.
  144. return GW6CM_UIS__NOERROR;
  145. }
  146. // --------------------------------------------------------------------------
  147. // Function : Write
  148. //
  149. // Description:
  150. // Will attempt to write data to the pipe.
  151. //
  152. // Arguments:
  153. // pvData: void* [IN], The receiving buffer.
  154. // nDataSize: uint32_t [IN], The size in bytes to write.
  155. // nWritten: uint32_t [OUT], The number of bytes written.
  156. //
  157. // Return values:
  158. // GW6CM_UIS__NOERROR: Operation successful.
  159. // Any other value is an error.
  160. //
  161. // --------------------------------------------------------------------------
  162. error_t PipeIO::Write( const void* pvData, const uint32_t nDataSize, uint32_t& nWritten )
  163. {
  164. // Verify IPC handle.
  165. assert( m_Handle != INVALID_HANDLE_VALUE );
  166. // Write to pipe.
  167. if( WriteFile( m_Handle, pvData, (DWORD)nDataSize, (DWORD*)(&nWritten), NULL ) == 0 )
  168. {
  169. return GW6CM_UIS_WRITEPIPEFAILED;
  170. }
  171. // Operation successful.
  172. return GW6CM_UIS__NOERROR;
  173. }
  174. }