Hauptseite | Klassenhierarchie | Alphabetische Liste | Auflistung der Klassen | Auflistung der Dateien | Klassen-Elemente

TCPIPCommSend.cpp

00001 //***************************************************************************** 00002 // project : ANALYTICA Class Library 00003 // part : Communication class for simple TCP/IP 00004 // file name : TCPIPCommSend.h 00005 // version : $Id: TCPIPCommSend.cpp,v 1.4 2001/07/17 06:55:23 AxelSchmidt Exp $ 00006 // author : Udo Weber 00007 // platform : MS VC++ 6.0 00008 // 00009 //----------------------------------------------------------------------------- 00010 // Description: 00011 // ============= 00012 // implementation of the class CTCPIPComm to encapsulate simple TCP/IP 00013 // communications 00014 // Part : Sending of messages 00015 // 00016 //----------------------------------------------------------------------------- 00017 // Revisionhistory: 00018 // $Log: TCPIPCommSend.cpp,v $ 00019 // Revision 1.4 2001/07/17 06:55:23 AxelSchmidt 00020 // class destructor hangs if Thead is not already started 00021 // 00022 // Revision 1.3 2001/07/05 16:17:58 IngoFroehlich 00023 // receive now always byte-wise form socket 00024 // 00025 // 20.07.1998 UWe Some bug fixes 00026 // 20.04.1998 UWe first version 00027 //***************************************************************************** 00028 00029 //---- includes --------------------------------------------------------------- 00030 #include "stdafx.h" 00031 00032 #include "TCPIPCommSend.h" 00033 00034 00035 //---- defines/const ---------------------------------------------------------- 00036 00037 00038 00039 //---- classes ----------------------------------------------------------------- 00040 00041 00042 //****************************************************************************** 00043 // Methods of class 'CTCPIPCommSend' 00044 //****************************************************************************** 00045 00046 // Static attributes and operations 00047 00048 00049 //------------------------------------------------------------------------------ 00050 // Description: 00051 // Constructs the class CTCPIPCommSend and creates a thread for sending 00052 // of messages 00053 // 00054 // Input: 00055 // MaxSendBufferLen : Maximum length for sending messages 00056 // 00057 // Output: ---- 00058 // 00059 // Return: ---- 00060 // 00061 //------------------------------------------------------------------------------ 00062 CTCPIPCommSend::CTCPIPCommSend( const long MaxSendBufferLen ) 00063 { 00064 m_pSendBuffer = NULL; 00065 m_hWritePipe = NULL; 00066 m_hReadPipe = NULL; 00067 m_hEventTerminateFinished = NULL; 00068 m_ThreadHandle = NULL; 00069 00070 if ( MaxSendBufferLen > 0 ) 00071 { 00072 InitializeClass( MaxSendBufferLen ); 00073 } 00074 } 00075 00076 void CTCPIPCommSend::InitializeClass( const long MaxSendBufferLen ) 00077 { 00078 if ( m_pSendBuffer != NULL ) 00079 { 00080 // Initialization already done, ignore call 00081 return; 00082 } 00083 00084 m_Terminating = false; 00085 m_MaxSendBufferLen = MaxSendBufferLen; 00086 m_pSendBuffer = new char[m_MaxSendBufferLen]; 00087 m_SendCnt = 0; 00088 00089 00090 CreatePipe( &m_hReadPipe, // Read Pipe Handle 00091 &m_hWritePipe, // Write Pipe Handle 00092 NULL, // Security Attributes 00093 0 ); // Default Size 00094 00095 m_hEventTerminateFinished = CreateEvent( NULL, // Security Attributes 00096 FALSE, // Automatic Reset 00097 FALSE, // Initial to non signaled 00098 NULL ); // Name of Semaphore 00099 00100 00101 // Create the connect thread 00102 m_ThreadHandle = CreateThread( NULL, // Security Attributes 00103 0, // Default Stack size 00104 (LPTHREAD_START_ROUTINE)SendThread, // Thread function 00105 this, // Pointer to this class 00106 0, // Thread starts immediatly 00107 &m_ThreadID ); // Returns the Thread ID 00108 00109 } 00110 00111 //------------------------------------------------------------------------------ 00112 // Description: 00113 // Destructs the CTCPIPCommSend class 00114 // 00115 // Input: ---- 00116 // 00117 // Output: ---- 00118 // 00119 // Return: ---- 00120 // 00121 //------------------------------------------------------------------------------ 00122 CTCPIPCommSend::~CTCPIPCommSend() 00123 { 00124 m_Terminating = true; 00125 00126 // Terminate the thread indirect 00127 CloseHandle( m_hWritePipe ); 00128 CloseHandle( m_hReadPipe ); 00129 00130 // wait until send thread has terminated 00131 if ( m_ThreadHandle != NULL ) //ASc, 17.07.2001, wait, if Thread is started 00132 { 00133 WaitForSingleObject( m_hEventTerminateFinished, INFINITE ); 00134 00135 CloseHandle( m_ThreadHandle ); 00136 } 00137 00138 delete [] m_pSendBuffer; 00139 } 00140 00141 00142 00143 //------------------------------------------------------------------------------ 00144 // Description: 00145 // Performs the general initialization of the class parameters 00146 // 00147 // Input: 00148 // Socket : The socket handle to be used for the communication 00149 // 00150 // Output: ---- 00151 // 00152 // Return: ---- 00153 // 00154 //------------------------------------------------------------------------------ 00155 void CTCPIPCommSend::Initialize( const SOCKET Socket ) 00156 { 00157 // Save some parameters 00158 m_Socket = Socket; 00159 } 00160 00161 00162 //------------------------------------------------------------------------------ 00163 // Description: 00164 // Sends the given data to the Send thread 00165 // 00166 // Input: 00167 // pData : Pointer to the data to be transmitted 00168 // DataLen : Length of the data to be transmitted 00169 // 00170 // Output: ---- 00171 // 00172 // Return: 00173 // TRUE if function call was successful 00174 // 00175 //------------------------------------------------------------------------------ 00176 bool CTCPIPCommSend::Send( const void *pData, 00177 const long DataLen ) 00178 { 00179 DWORD BytesWritten; 00180 //long BytesToSend; 00181 00182 00183 // Send data to the send thread 00184 // Prepend first the length of the message 00185 00186 // printf( "S%d.%d-", ++m_SendCnt, DataLen ); !!!!!!!!!!!!! 00187 /* 00188 BytesToSend = DataLen + sizeof( DataLen ); 00189 00190 char *pBuffer = new char[ BytesToSend ]; 00191 00192 *(long *)pBuffer = DataLen; 00193 memcpy( pBuffer + sizeof( DataLen ), pData, DataLen ); 00194 */ 00195 /****** 00196 // Send the data to the socket interface 00197 if ( send( m_Socket, 00198 (const char *)pBuffer, 00199 BytesToSend, 00200 0 ) == SOCKET_ERROR ) 00201 { 00202 m_LastSocketError = WSAGetLastError(); 00203 if ( ! Terminating() ) 00204 { 00205 SendDisInd(); 00206 } 00207 } 00208 *****/ 00209 00210 /****/ 00211 00212 // Send the data via pipe to the send thread 00213 if ( ! WriteFile( m_hWritePipe, pData, DataLen, &BytesWritten, NULL ) ) 00214 { 00215 m_LastSocketError = GetLastError(); 00216 00217 // delete [] pBuffer; 00218 return false; 00219 } 00220 /****/ 00221 00222 // delete [] pBuffer; 00223 00224 return true; 00225 00226 } 00227 00228 00229 00230 00231 //------------------------------------------------------------------------------ 00232 // Description: 00233 // Thread starting routine 00234 // 00235 // Input: 00236 // pClass : Pointer to the local class 00237 // 00238 // Output: ---- 00239 // 00240 // Return: ---- 00241 // 00242 //------------------------------------------------------------------------------ 00243 void CTCPIPCommSend::SendThread( CTCPIPCommSend *pClass ) 00244 { 00245 DWORD BytesToSend; 00246 char *pData; 00247 00248 00249 // Loop until thread should be terminated 00250 // 1. Listen to the pipe and wait for messages to be sent 00251 // 2. Transmit the message calling the socket function send 00252 // goto 1. 00253 for ( ; ; ) 00254 { 00255 // Wait for the Request to establish/await the connection 00256 if ( ! ReadFile( pClass->m_hReadPipe, 00257 pClass->m_pSendBuffer, 00258 pClass->m_MaxSendBufferLen, 00259 &BytesToSend, 00260 NULL ) ) 00261 { 00262 break; // Failes if the pipe is closed (during terminating procedure) 00263 } 00264 00265 pData = (char *)pClass->m_pSendBuffer; 00266 00267 // ASc, 05.07.2001, For non-blocking socket, all data is sent (!!!! I hope so) 00268 00269 // Send the data to the socket interface 00270 if ( send( pClass->m_Socket, 00271 (const char *)pData, 00272 BytesToSend, 00273 0 ) == SOCKET_ERROR ) 00274 { 00275 pClass->m_LastSocketError = WSAGetLastError(); 00276 if ( ! pClass->Terminating() ) 00277 { 00278 pClass->SendDisInd(); 00279 } 00280 } 00281 } 00282 00283 SetEvent( pClass->m_hEventTerminateFinished ); 00284 } 00285 00286 00287 00288

Erzeugt am Tue Mar 11 14:25:07 2008 für AnalyticaTCPIP-COMMclass von doxygen 1.3.7