IOhandler->Write的问题
开发的软件中遇到了一个奇怪的问题,TIdTCPServer->Contexts->LockList()之后,用其中的连接信息给每个终端发送一张图像,但是总是有BUG,软件在使用时不定期的卡死,我记的日志就显示就在下面这个函数中,不定期的出现不能TIdTCPServer->Contexts->UnlockList()的现象(因为我的日志会在正常UnlockList之后删除),日志里都是在这个地方卡住,然后别的模块用到TIdTCPServer->Contexts->LockList()之后就一直排队不动了。。。。。。,求各位帮忙指点......
SendShareDesktop(UnicodeString MeetingID,UnicodeString UserID,TJPEGImage * Screen)throw(Exception) {
RecorderSet SendRS;
try {
TJPEGImage * ScreenJpeg = new TJPEGImage;
ScreenJpeg->Assign(Screen);
BaseRecorder * SendR = GetCustomRecorder(SHARE_DESKTOP_DATA_REQUEST);
SendR->Field("user_id")->AsString = UserID;
SendR->Field("screen")->AsJpeg = ScreenJpeg;
SendRS.AddRecorder(SendR);
}
catch(Exception & e) {
throw Exception("SendShareDesktop(1):"+e.Message);
}
int ListLog,ListCountLog;
try {
ListLog = AddLockLog(4,"连接列表加锁","SendShareDesktop");
TList * ContextList = EMTCPServer->Contexts->LockList();
ListCountLog = AddLockLog(5,"连接列表数量","SendShareDesktop:"+UnicodeString(ContextList->Count));
for(int i=0;i<ContextList->Count;i++) {
TIdContext * AContext = (TIdContext *)ContextList->Items[i];
UserSession * Session = (UserSession*)AContext->Data;
UnicodeString PeerIP = AContext->Connection->Socket->Binding->PeerIP;
if(Session) {
try {
SendEMPkg(AContext,SHARE_DESKTOP_DATA_REQUEST,SendRS.GetPkg(),SendRS.GetPkgLen());
}
catch(Exception & e) {
AddLog(SYS_WARNING,SQLString("SendShareDesktop(2):发送消息失败"),Session->IP);
}
}
else {
AddLog(SYS_WARNING,SQLString("SendShareDesktop(3):无Session信息,PeerIP:"+PeerIP),"EM_SERVICE");
}
}
}
__finally {
EMTCPServer->Contexts->UnlockList();
DeleteLockLog(ListCountLog);
DeleteLockLog(ListLog);
}
}
用到的SendEMPkg//---------------------------------------------------------------------------
void TXMServerService::SendEMPkg(TIdContext *AContext,unsigned OP,unsigned char * Pkg,int Size)throw(Exception)
{
UserSession * Session = (UserSession*)AContext->Data;
UnicodeString PeerIP = AContext->Connection->Socket->Binding->PeerIP;
if(Session && !PeerIP.IsEmpty() && AContext->Connection->Connected()) {
EMHeader Header;
Header.OP = OP;
Header.DataSize = Size;
HeaderCheck(&Header);
TMemoryStream * SendStream = NULL;
try {
Session->SendLock->Enter();
try {
SendStream = new TMemoryStream;
SendStream->Write(&Header,sizeof(Header));
SendStream->Write(Pkg,Size);
SendStream->Position = 0;
AContext->Connection->IOHandler->Write(SendStream,SendStream->Size);
}
catch(Exception & e) {
Session->Connected = false;
throw Exception("SendEMPkg(1):"+e.Message);
}
}
__finally {
if(SendStream)
delete SendStream;
Session->SendLock->Leave();
}
}
else {
throw Exception("SendEMPkg(2):Session信息丢失,PeerIP:"+PeerIP);
}
}