IOhandler->Write的问题

kagagas 2014-01-21 09:30:11
开发的软件中遇到了一个奇怪的问题,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);
}
}
...全文
281 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kagagas 2014-01-27
  • 打赏
  • 举报
回复
只有妖哥回复,分就给妖哥吧。
kagagas 2014-01-27
  • 打赏
  • 举报
回复
问题搞定了,结贴,与代码和Indy没有关系...
ccrun.com 2014-01-21
  • 打赏
  • 举报
回复
别的不说,先说try的位置,这个是有讲究的。至少: try { TJPEGImage * ScreenJpeg = new TJPEGImage; 应该改成:
TJPEGImage * ScreenJpeg = new TJPEGImage;
try
{
try { ListLog = AddLockLog(4,"连接列表加锁","SendShareDesktop"); TList * ContextList = EMTCPServer->Contexts->LockList(); 改成:
TList * ContextList = EMTCPServer->Contexts->LockList();
try {
ListLog = AddLockLog(4,"连接列表加锁","SendShareDesktop");
TMemoryStream * SendStream = NULL; try { Session->SendLock->Enter(); try { SendStream = new TMemoryStream; 然后再考虑其他的细节。 改成:
TMemoryStream * SendStream = NULL;
Session->SendLock->Enter();
try {
SendStream = new TMemoryStream;
try {

1,317

社区成员

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

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