Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
6.6 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 06/11/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgReportSocket.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgReportSocket.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgReportSocket.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. CLgReportSocket::CLgReportSocket()
  22. {
  23. m_pBuffer = new BYTE[1<<20];
  24. m_nCount = 0;
  25. m_OutputStream.Create();
  26. m_liTransferTX.QuadPart = m_liTransferRX.QuadPart = 0;
  27. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgReportSocket::CLgReportSocket" );
  28. }
  29. CLgReportSocket::~CLgReportSocket()
  30. {
  31. m_OutputStream.Destroy();
  32. if( m_pBuffer )
  33. {
  34. delete m_pBuffer;
  35. m_pBuffer = 0;
  36. }
  37. m_nCount = 0;
  38. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgReportSocket::~CLgReportSocket" );
  39. }
  40. void CLgReportSocket::OnClose( int nErrorCode )
  41. {
  42. CLgSocket::OnClose( nErrorCode );
  43. // clear internal buffers
  44. m_OutputStream.Clear();
  45. m_nCount = 0;
  46. }
  47. void CLgReportSocket::OnReceive( int nErrorCode )
  48. {
  49. if( nErrorCode )
  50. {
  51. m_nCount = 0;
  52. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::OnReceive"), _T("Error %d received"), nErrorCode );
  53. return;
  54. }
  55. CLgSocket::OnReceive( nErrorCode );
  56. if( !m_pBuffer )
  57. return;
  58. while( TRUE )
  59. {
  60. // read packet header
  61. const INT cDescriptionSize = sizeof(LG_REPORT_PACKET);
  62. if( m_nCount < cDescriptionSize )
  63. {
  64. INT nSize = cDescriptionSize - m_nCount;
  65. INT nRead = CLgSocket::Receive( m_pBuffer + m_nCount, nSize, 0 );
  66. if( nRead < 0 )
  67. {
  68. CLgSocket::OnReceive( nErrorCode );
  69. return;
  70. }
  71. // store statistic
  72. m_liTransferRX.QuadPart += nRead;
  73. m_nCount += nRead;
  74. if( nRead < nSize )
  75. {
  76. CLgSocket::OnReceive( nErrorCode );
  77. return;
  78. }
  79. }
  80. // read packet
  81. INT nPacketSize = ((PLG_REPORT_PACKET)m_pBuffer)->nLength;
  82. if( m_nCount < nPacketSize )
  83. {
  84. INT nSize = nPacketSize - m_nCount;
  85. INT nRead = CLgSocket::Receive( m_pBuffer + m_nCount, nSize, 0 );
  86. if( nRead < 0 )
  87. {
  88. CLgSocket::OnReceive( nErrorCode );
  89. return;
  90. }
  91. // store statistic
  92. m_liTransferRX.QuadPart += nRead;
  93. m_nCount += nRead;
  94. if( nRead < nSize )
  95. {
  96. CLgSocket::OnReceive( nErrorCode );
  97. return;
  98. }
  99. }
  100. Dispatch( (PLG_REPORT_PACKET)m_pBuffer );
  101. m_nCount = 0;
  102. }
  103. }
  104. void CLgReportSocket::OnSend( int nErrorCode )
  105. {
  106. CLgLocker Locker( &m_OutputStream );
  107. // write into socket
  108. while( TRUE )
  109. {
  110. // read from stream
  111. BYTE pBuffer[MAX_PATH];
  112. INT nCount = m_OutputStream.Peek( pBuffer, MAX_PATH );
  113. if( !nCount )
  114. break;
  115. // write to stream
  116. INT nWritten = CLgSocket::Send( pBuffer, nCount );
  117. if( nWritten <= 0 )
  118. break;
  119. // validate buffer
  120. m_OutputStream.Skip( nWritten );
  121. // store statistic info
  122. m_liTransferTX.QuadPart += nWritten;
  123. }
  124. CLgSocket::OnSend( nErrorCode );
  125. }
  126. int CLgReportSocket::Send( const void* lpBuf, int nBufLen, int nFlags )
  127. {
  128. CLgLocker Locker( &m_OutputStream );
  129. // check availible space
  130. if( m_OutputStream.GetFree() < nBufLen )
  131. {
  132. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send1"), _T("Buffer space is not enough for new data (free %d, but it needs %d)"), m_OutputStream.GetFree(), nBufLen );
  133. return -1;
  134. }
  135. // write into internal buffer
  136. INT nWritten = m_OutputStream.Put( (PBYTE)lpBuf, nBufLen );
  137. if( nWritten != nBufLen )
  138. {
  139. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("m_OutputStream.Put failed (%d instead of %d)"), nWritten, nBufLen );
  140. return -1;
  141. }
  142. // write into socket
  143. INT nSend = 0;
  144. while( TRUE )
  145. {
  146. // read from stream
  147. BYTE pBuffer[MAX_PATH];
  148. INT nCount = m_OutputStream.Peek( pBuffer, MAX_PATH );
  149. if( !nCount )
  150. break;
  151. // write to stream
  152. nWritten = CLgSocket::Send( pBuffer, nCount );
  153. if( nWritten <= 0 )
  154. {
  155. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, _T("CLgReportSocket::Send"), _T("CLgSocket::Send failed (%d instead of %d)"), nWritten, nCount );
  156. break;
  157. }
  158. // validate buffer
  159. m_OutputStream.Skip( nWritten );
  160. nSend += nWritten;
  161. }
  162. // store statistic info
  163. m_liTransferTX.QuadPart += nSend;
  164. return nSend;
  165. }
  166. int CLgReportSocket::Send2( const void* lpBuf1, int nBufLen1, const void* lpBuf2, int nBufLen2 )
  167. {
  168. CLgLocker Locker( &m_OutputStream );
  169. // check availible space
  170. if( m_OutputStream.GetFree() < (nBufLen1 + nBufLen2) )
  171. {
  172. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("Buffer space is not enough for new data (free %d, but it needs %d)"), m_OutputStream.GetFree(), nBufLen1 + nBufLen2 );
  173. return -1;
  174. }
  175. // write first block into internal buffer
  176. INT nWritten1 = m_OutputStream.Put( (PBYTE)lpBuf1, nBufLen1 );
  177. if( nWritten1 != nBufLen1 )
  178. {
  179. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("m_OutputStream.Put for block 1 failed (%d instead of %d)"), nWritten1, nBufLen1 );
  180. return -1;
  181. }
  182. // write second block into internal buffer
  183. INT nWritten2 = m_OutputStream.Put( (PBYTE)lpBuf2, nBufLen2 );
  184. if( nWritten2 != nBufLen2 )
  185. {
  186. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Send2"), _T("m_OutputStream.Put for block 2 failed (%d instead of %d)"), nWritten2, nBufLen2 );
  187. return -1;
  188. }
  189. // write into socket
  190. INT nSend = 0;
  191. while( TRUE )
  192. {
  193. // read from stream
  194. BYTE pBuffer[MAX_PATH];
  195. INT nCount = m_OutputStream.Peek( pBuffer, MAX_PATH );
  196. if( !nCount )
  197. break;
  198. // write to stream
  199. INT nWritten = CLgSocket::Send( pBuffer, nCount );
  200. if( nWritten <= 0 )
  201. {
  202. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_WARNING, this, _T("CLgReportSocket::Send2"), _T("CLgSocket::Send failed (%d instead of %d)"), nWritten, nCount );
  203. break;
  204. }
  205. // validate buffer
  206. m_OutputStream.Skip( nWritten );
  207. nSend += nWritten;
  208. }
  209. // store statistic info
  210. m_liTransferTX.QuadPart += nSend;
  211. return nSend;
  212. }
  213. BOOL CLgReportSocket::GetTransferInfo( PLARGE_INTEGER pTX, PLARGE_INTEGER pRX )
  214. {
  215. if( pTX )
  216. *pTX = m_liTransferTX;
  217. if( pRX )
  218. *pRX = m_liTransferRX;
  219. return TRUE;
  220. }
  221. void CLgReportSocket::Dispatch( PLG_REPORT_PACKET pPacket )
  222. {
  223. if( !pPacket )
  224. return;
  225. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportSocket::Dispatch"), _T("Received packed with unknown type %d"), pPacket->bType );
  226. }