📄 publicclass.cpp
字号:
#include "publicclass.h"#include <string.h>#include <qdatetime.h>#include <qtextcodec.h>#include <qfile.h>extern CPublicClass g_PublicClass;void SignalProcessDef1(int signum,siginfo_t *info,void *myact){ if(signum==0||!info||!myact) return; SignalProcessDef1x(signum,info,myact); return;}void SignalProcessDef2(int signum,siginfo_t *info,void *myact){ if(signum==0||!info||!myact) return; SignalProcessDef2x(signum,info,myact); return;}CMessageBox::CMessageBox(const QString caption,const QString text,QWidget* parent,unsigned int icon) :QMessageBox(caption,text,(Icon)icon,QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton,QMessageBox::NoButton,parent){ exec();}CMessageBox::CMessageBox(const QString caption,const QString text,int btn0,int btn1,int btn2,QWidget *parent,unsigned int icon) :QMessageBox(caption,text,(Icon)icon,btn0,btn1,btn2,parent){ return;}//*************************信号机制********************************//CSignal :: CSignal(){ return;}CSignal :: ~CSignal(){ return;}bool CSignal :: InstallSignalProcess1(){ struct sigaction act; sigemptyset(&act.sa_mask); act.sa_flags=SA_SIGINFO; act.sa_sigaction=SignalProcessDef1; int ret=sigaction(SIGUSR1,&act,NULL); if(ret<0) return 0; return 1;}bool CSignal :: InstallSignalProcess2(){ struct sigaction act; sigemptyset(&act.sa_mask); act.sa_flags=SA_SIGINFO; act.sa_sigaction=SignalProcessDef2; int ret=sigaction(SIGUSR2,&act,NULL); if(ret<0) return 0; return 1;}bool CSignal :: SendSignal(pid_t pid,int sig,int type,void* ptr,int iSize){ union sigval mysigval; ptr=NULL;iSize=0; mysigval.sival_int=type; int ret=sigqueue(pid,sig,mysigval); if(ret<0) return 0; return 1;}//*************************共享内存*******************************//CSegment :: CSegment(){ return;}CSegment :: ~CSegment(){ return;}int CSegment :: Open_Segment(key_t keyval,int iSize,bool bExcl){ int shmid=0; if(!bExcl) { if((shmid=shmget(keyval,iSize,IPC_CREAT|0660))==-1) return -1; } else { if((shmid=shmget(keyval,iSize,IPC_CREAT|IPC_EXCL))==-1) return -1; } return shmid;}void* CSegment :: Attach_Segment(int shmid){ return ((void*)shmat(shmid,0,0));}int CSegment :: DisAttach_Segment(void* segptr){ return shmdt((char*)segptr);}void CSegment :: Write_Segment(void *segptr,void *pSrc,int iSize){ memcpy(segptr,pSrc,iSize);}void CSegment :: Read_Segment(void *segptr,void *pDes,int iSize){ memcpy(pDes,segptr,iSize);}void CSegment :: Remove_Segment(int shmid){ shmctl(shmid,IPC_RMID,0);}void CSegment :: Change_SegmentMode(int shmid,char *mode){ struct shmid_ds myshmds; shmctl(shmid,IPC_STAT,&myshmds); sscanf(mode,"%ho",&myshmds.shm_perm.mode); shmctl(shmid,IPC_SET,&myshmds);}int CSegment :: WriteSegment(void *pSrc,int iSize,bool bExcl){ int count=0;int shmid=-1; if(!pSrc||iSize<=0) return -1; while(shmid==-1&&count<10) { key_t keyval=_getKeyId(); shmid=Open_Segment(keyval,iSize,bExcl);count++; } if(shmid<0) return -1; void* segptr=Attach_Segment(shmid); if(!segptr) { Remove_Segment(shmid);return -1; } Write_Segment(segptr,pSrc,iSize);DisAttach_Segment(segptr); return shmid;}void CSegment :: ReadSegment(int shmid,void *pDes,int iSize,bool bDelete){ if(shmid<=0) return; void* segptr=Attach_Segment(shmid); if(segptr==NULL) return; if(pDes==NULL) return; Read_Segment(segptr,pDes,iSize); if(bDelete) Remove_Segment(shmid); DisAttach_Segment(segptr);}//*************************消息队列********************************//CMessageQueue :: CMessageQueue(){ return;}CMessageQueue :: ~CMessageQueue(){ return;}int CMessageQueue :: Open_MessageQueue(key_t keyval){ int qid=0; if((qid=msgget(keyval,IPC_CREAT|0660))==-1) { return -1; } return qid;}int CMessageQueue :: Send_Message(int qid,void* qbuf,int iSize){ int result=-1; if((result=msgsnd(qid,qbuf,iSize,0))==-1) return -1; return result;}int CMessageQueue :: Read_Message(int qid,long type,void* qbuf,int iSize){ int result=-1; if((result=msgrcv(qid,qbuf,iSize,type,0))==-1) return -1; return result;}int CMessageQueue :: Peek_Message(int qid,long type){ int result=-1; if((result=msgrcv(qid,NULL,0,type,IPC_NOWAIT))==-1) { if(errno==E2BIG) return 1; } return 0;}int CMessageQueue :: Get_QueueDs(int qid,struct msqid_ds *qbuf){ if(msgctl(qid,IPC_STAT,qbuf)==-1) return -1; return 0;}int CMessageQueue :: Change_QueueMode(int qid,char *mode){ struct msqid_ds tmpbuf; sscanf(mode,"%ho",&tmpbuf.msg_perm.mode); if(msgctl(qid,IPC_SET,&tmpbuf)==-1) return -1; return 0;}int CMessageQueue :: Remove_MessageQueue(int qid){ if(msgctl(qid,IPC_RMID,0)==-1) return -1; return 0;}bool CMessageQueue :: SendMessage(int qid,long DestPid,long DestThread,unsigned int DataType, void* qbuf,int iSize){ st_nodeinfo st;CSegment cs; memset(&st,0x00,sizeof(st_nodeinfo)); if(DestPid==0) return 0; st.mtype=DestPid;st.requestid=getpid();st.m_CurrentThread=DestThread;st.m_DataType=DataType; if(qbuf&&iSize>0) { st.m_SegmentPid=cs.WriteSegment(qbuf,iSize,TRUE); st.m_iSize=iSize; if(st.m_SegmentPid<0) return 0; } if(Send_Message(qid,&st,sizeof(st_nodeinfo))<0) return 0; return 1;}bool CMessageQueue :: SendMessageEx(int qid,long DestPid,long DestThread,unsigned int DataType, void* qbuf,int iSize, unsigned int DataFrom,int iResult,int DisplayFlag, int Level,long TransThread){ st_nodeinfo st;CSegment cs; memset(&st,0x00,sizeof(st_nodeinfo)); if(DestPid==0) return 0; st.mtype=DestPid;st.requestid=getpid();st.m_CurrentThread=DestThread;st.m_DataType=DataType; st.m_DataFrom=DataFrom;st.m_iResult=iResult;st.m_DisplayFlag=DisplayFlag; st.m_Level=Level;st.m_TransThread=TransThread; if(qbuf&&iSize>0) { st.m_SegmentPid=cs.WriteSegment(qbuf,iSize,TRUE); if(st.m_SegmentPid<0) return 0; } if(Send_Message(qid,&st,sizeof(st_nodeinfo))<0) return 0; return 1;}bool CMessageQueue :: SendMessage(int qid,void* ppNode){ st_nodeinfo st;CSegment cs; memset(&st,0x00,sizeof(st_nodeinfo)); CDataNode* pNode=(CDataNode*)ppNode; if(!pNode) return 0; memcpy(&st,&pNode->m_nodeinfo,sizeof(st_nodeinfo)); if(st.mtype==0) return 0; char* buff=NULL; if(st.m_SegmentPid>0&&st.m_iSize>0) { buff=new char[st.m_iSize]; memcpy(buff,(void*)st.m_SegmentPid,st.m_iSize); st.m_SegmentPid=cs.WriteSegment(buff,st.m_iSize,TRUE); if(st.m_SegmentPid<0) return 0; delete[] buff; } if(Send_Message(qid,&st,sizeof(st_nodeinfo))<0) return 0; return 1;}bool CMessageQueue :: ReadMessage(int qid,int pid,st_nodeinfo* msgbuf){ CSegment cs; memset(msgbuf,0x00,sizeof(st_nodeinfo)); if(!Peek_Message(qid,pid)) return FALSE; if(Read_Message(qid,pid,msgbuf,sizeof(st_nodeinfo))<0) return FALSE; char* buff=NULL; if(msgbuf->m_iSize>0&&msgbuf->m_SegmentPid>0) { buff=new char[msgbuf->m_iSize]; //此处不删除,在数据处理线程中删除 cs.ReadSegment(msgbuf->m_SegmentPid,buff,msgbuf->m_iSize); msgbuf->m_SegmentPid=(long)buff; } return TRUE;}/////////////////////////////清除队列中有关某个进程的消息队列///////////////////////////////void CMessageQueue :: ClearMessage(int qid,int pid){ st_nodeinfo st; while(1) { if(!ReadMessage(qid,pid,&st)) break; //有消息,则删除伴随数据 char* buff=NULL; if(st.m_SegmentPid>0) { buff=(char*)st.m_SegmentPid;delete [] buff; } } }//////////////////////////////////////////////////////////////////////////////////////////////////////////////CDeviceNode :: CDeviceNode(){ memset(&m_deviceinfo,0x00,sizeof(st_deviceinfo)); m_ModuleState=0;m_Level=0; return;};CDeviceNode :: ~CDeviceNode(){ return;};void CDeviceNode::AddChildDevice(st_deviceinfo &st){ QString RecordIndex; RecordIndex.sprintf("%s",st.m_RecordIndex); int index=FindDevicebyRecordIndex(RecordIndex); if(index>=0) return; m_ChildDevList.append(st);}//edit by zqp startint CDeviceNode::AddChildDevices(void* dlist,int count,unsigned int ThreadId){ string ParentIndex(m_deviceinfo.m_RecordIndex); st_deviceinfo DevSt; for(int i=0;i<count;i++) { memcpy(&DevSt,(char*)dlist+i*sizeof(st_deviceinfo),sizeof(st_deviceinfo)); strcpy(DevSt.m_ParentIndex, ParentIndex.c_str()); DevSt.m_ThreadId=ThreadId; DevSt.m_SupportCmd=65535; AddChildDevice(DevSt); } return 0;}//edit by zqp endint CDeviceNode::FindDevicebyRecordIndex(QString RecordIndex) { DeviceInfoListType::iterator it;QString sRecordIndex; for(int i=0;i<(int)m_ChildDevList.count();i++) { it=m_ChildDevList.at(i); sRecordIndex.sprintf("%s",(*it).m_RecordIndex); if(sRecordIndex==RecordIndex) return i; } return -1;}void CDeviceNode :: UpdateDevicesThread(void* deviceinfo,int iSize){ st_deviceinfo devst;DeviceInfoListType::iterator it; memcpy(&devst,(char*)deviceinfo,iSize); QString s1;s1.sprintf("%s",devst.m_RecordIndex); QString sRecordIndex,sParentIndex; for(int i=0;i<(int)m_ChildDevList.count();i++) { it=m_ChildDevList.at(i); sRecordIndex.sprintf("%s",(*it).m_RecordIndex); sParentIndex.sprintf("%s",(*it).m_ParentIndex); if(sRecordIndex==s1||sParentIndex==s1) { (*it).m_ThreadId=devst.m_ThreadId; (*it).m_SupportCmd=devst.m_SupportCmd; } }}//add zqp startvoid CDeviceNode :: UpdateChildDevicesThread(void* ParentDevInfo,int iSize){ st_deviceinfo devst;DeviceInfoListType::iterator it; memcpy(&devst,(char*)ParentDevInfo,iSize); string s1(devst.m_RecordIndex); string sRecordIndex,sParentIndex; for(int i=0;i<(int)m_ChildDevList.count();i++) { it=m_ChildDevList.at(i); sRecordIndex = (string)((*it).m_RecordIndex); sParentIndex = (string)((*it).m_ParentIndex); if(sRecordIndex==s1||sParentIndex==s1) { (*it).m_ThreadId=devst.m_ThreadId; (*it).m_SupportCmd=devst.m_SupportCmd; } }}//add zqp end//add by huyongchunint CDeviceNode::FindDeviceByEquipNo(int iEquipNo){ DeviceInfoListType::iterator it; for (int i=0; i<(int)m_ChildDevList.count(); i++) { it = m_ChildDevList.at(i); if (iEquipNo == (int)(*it).m_EquipNo) return i; } return -1;}int CDeviceNode::FindDeviceByFlag(int iFlag /* = 0 */){ DeviceInfoListType::iterator it; for (int i=0; i<(int)m_ChildDevList.count(); i++) { it = m_ChildDevList.at(i); if (iFlag == (*it).m_PlFlag) return i; } return -1;}void CDeviceNode::ResetXunJianFlag(){ DeviceInfoListType::iterator it; for (int i=0; i<(int)m_ChildDevList.count(); i++) { it = m_ChildDevList.at(i); (*it).m_PlFlag = 0; }}//add endCDataNode :: CDataNode(){ return;};CDataNode :: ~CDataNode(){ return;};CNodeManage :: CNodeManage(){ m_DeviceNode.setAutoDelete (TRUE); m_SendNode.setAutoDelete (TRUE); m_RecvNode.setAutoDelete (TRUE); m_DispNode.setAutoDelete (TRUE); return;};CNodeManage :: ~CNodeManage(){ return;};bool CNodeManage :: AddNode(void* pNode,const unsigned int nodetype){ switch(nodetype) { case DEVICENODE: m_DeviceNode.append((CDeviceNode*)pNode); break; case SENDNODE: m_SendNode.append((CDataNode*)pNode); break; case RECVNODE: m_RecvNode.append((CDataNode*)pNode); break; case DISPNODE: m_DispNode.append((CDataNode*)pNode); break; default: break; } return TRUE;}////////////////通过模块名称查找模块接点///////////////////////////////////CDeviceNode* CNodeManage :: FindModuleNode(QString ModuleName,int ModuleState){ CDeviceNode* pNode=NULL; QString Mn;bool bFind=1; for(int i=0;i<(int)m_DeviceNode.count();i++) { bFind=1; pNode=m_DeviceNode.at(i); if(!pNode) continue; Mn.sprintf("%s",pNode->m_deviceinfo.m_ModuleName); bFind=(Mn.lower()==ModuleName.lower()); if(ModuleState>=0) bFind=bFind&&(pNode->m_ModuleState==ModuleState); if(bFind) return pNode; } return NULL;}CDeviceNode* CNodeManage :: FindModuleNode(int pid,int ModuleState){ CDeviceNode* pNode=NULL;bool bFind=1; for(int i=0;i<(int)m_DeviceNode.count();i++) { pNode=m_DeviceNode.at(i); if(!pNode) continue; bFind=(pNode->m_deviceinfo.mtype==pid); if(ModuleState>=0) bFind=bFind&&(pNode->m_ModuleState==ModuleState); if(bFind) return pNode; } return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -