关于线程问题
代码如下,各位可否帮我详细解释一下:
_fastcall ServerThread::ServerThread(TServerClientWinSocket* socket)
: TServerClientThread(true, socket) //suspend thread first(or CreateSuspended), resume it after some initiation
{
MaxWaitTime = 5000;
//ErrListStr = new TStringList; ErrListStr->Clear();
FreeOnTerminate = true;
Resume();
}
//---------------------------------------------------------------------------
void __fastcall ServerThread::ClientExecute()
{
DWORD dwTick;
long i, n, nSize = 0, nLen = 0;
char buf[1024];
bool StreamCreated = false, TimeOut = false;
AnsiString UserID, UserReason, UserPCNo, Flag;
try{
WskStream = new TWinSocketStream(ClientSocket, MaxWaitTime + 1000);//创建一个流对象
StreamCreated = true;//流创建成功
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
////get flag string
memset(buf, '\0', 1024);
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
buf[nLen - 1] = n;
if (nLen == 16) break;
}
}
Flag = buf;
if (Flag!="4950c9e862c1f5bd" && Flag!="62325b0a9cb0f152") //not login, not logoff
{
if (StreamCreated) delete WskStream;
ProcessData(-1, "", "unknown incoming connection: ", ""); //********
return;
}
////get the length of "User Longin ID" -> store in nSize
nLen = 0; n = 0; i = 0; nSize = 0;
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
nSize += n * pow(2, 8 * (nLen - 1));
if (nLen == 4) break;
}
}
////get "User Longin ID"
nLen = 0;
memset(buf, '\0', 1024);
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
buf[nLen - 1] = n;
if (nLen == nSize) break;
}
}
UserID = buf;
if (Flag == "62325b0a9cb0f152") //logoff flag
{
if (StreamCreated) delete WskStream;
ProcessData(0, UserID, "", ""); //********
return;
}
////get the length of "User Longin Reason" -> store in nSize
nLen = 0; n = 0; i = 0; nSize = 0;
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
nSize += n * pow(2, 8 * (nLen - 1));
if (nLen == 4) break;
}
}
////get "User Longin Reason"
nLen = 0;
memset(buf, '\0', 1024);
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
buf[nLen - 1] = n;
if (nLen == nSize) break;
}
}
UserReason = buf;
////get the length of "PC No." -> store in nSize
nLen = 0; n = 0; i = 0; nSize = 0;
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
nSize += n * pow(2, 8 * (nLen - 1));
if (nLen == 4) break;
}
}
////get "PC No."
nLen = 0;
memset(buf, '\0', 1024);
dwTick = GetTickCount();
while(!TimeOut)
{
if (GetTickCount() - dwTick >= MaxWaitTime) {TimeOut = true; break;}
if (!WskStream->WaitForData(MaxWaitTime)) {TimeOut = true; break;}
i = WskStream->Read(&n, 1);
if (i == 1)
{
nLen++;
buf[nLen - 1] = n;
if (nLen == nSize) break;
}
}
UserPCNo = buf;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (!TimeOut)
{
WaitForSingleObject(hThreadLock, INFINITE);
ProcessData(1, UserID, UserReason, UserPCNo); //********
ReleaseMutex(hThreadLock);
}
}
catch(Exception &e)
{
ProcessData(-1, "", "connection error: " + e.Message + ": ", ""); //********
}
if (StreamCreated) delete WskStream;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
SocketThread = new ServerThread(ClientSocket);
}