关于线程问题

nulifashengdeshi 2006-05-27 08:25:19
代码如下,各位可否帮我详细解释一下:
_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);
}
...全文
215 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
nulifashengdeshi 2006-05-27
  • 打赏
  • 举报
回复
其中的流也帮我解释一下,谢谢

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧