举例:
1.定义一个结构保存连接参数, 保存到线程的Data
TBSNetThreadParam = packed record //定义网络通信线程所需要的参数结构
eCommType : TBSThreadType; //协议类型(tcp/udp)
sRemoteIP : string[15]; //对方的ip地址
pThread : Pointer; //Pointer to thread
end;
PBSNetThreadParam = ^TBSNetThreadParam;
2.
procedure TBSCustomComm.TCPServerConnect(AThread: TIdPeerThread);
var
NewParam: PBSNetThreadParam;
begin
GetMem(NewParam, SizeOf(TBSNetThreadParam));
NewParam.eCommType := ttTCPServer;
NewParam.sRemoteIP := AThread.Connection.Socket.Binding.PeerIP;
NewParam.hMsgHandle := mMsgHandle;
NewParam.eWorkStatus := nsNormal;
NewParam.hThreadID := AThread.ThreadID;
NewParam.pThread := AThread;
AThread.Data:=TObject(NewParam);
try
FThreadList.LockList.Add(NewParam);
finally
FThreadList.UnlockList;
end;
//告诉Client,线程已启动
AThread.Connection.WriteLn(GStack.LocalAddress);
end;
procedure TBSCustomComm.TCPServerDisconnect(AThread: TIdPeerThread);
var
ActThreadParam: PBSNetThreadParam;
begin
ActThreadParam := PBSNetThreadParam(AThread.Data);
try
FThreadList.LockList.Remove(ActThreadParam);
finally
FThreadList.UnlockList;
end;
FreeMem(ActThreadParam);
AThread.Data := nil;
end;