📄 unibus.cpp
字号:
CTime tm;
pCBus->GetTime(tm);
cs.Format("%04ld-%02ld-%02ld %02ld:%02ld:%02ld %s %08ld %05ld %s\r\n",
tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond(),
ptLog->logtype==eventLog?"EVENT ":
ptLog->logtype==warningLog?"WARNING ":
ptLog->logtype==errorLog?"ERROR ":"EXCEPTION",
ptLog->icommand,ptLog->ilevel,ptLog->uvdescripe.uv.iv?(char *)ptLog->uvdescripe:"");
cf.SeekToEnd();
cf.Write(cs,cs.GetLength());
cf.Close();
}
}
delete ptLog;
ptLog=NULL;
}
}
catch(...){
TRACE("LOG THREAD EXCEPTION CATCH %lx",pCBus);
if(pCBus->IsValid(pCBus)){
if(ptLog)delete ptLog;
pCBus->m_iStatus=THREAD_STATUS_ERROR;
pCBus->m_iError++;
pCBus->m_pThread=AfxBeginThread(CDataBus::WriteLog,(LPVOID)pCBus);
}
return 0;
}
};
UINT CDataBus::Timer(LPVOID lpVoid){
CDataBus * pCBus=(CDataBus *)lpVoid;
if(!pCBus->IsValid(pCBus)){
TRACE("INVALIDATE THREAD:%lx\n",pCBus);
return 0;
}
LOGST * ptLog=NULL;
pCBus->m_iTime=time(NULL);
try{
while(!pCBus->m_bExit){
DWORD dwd1=GetTickCount();
pCBus->OnTimer();
pCBus->m_iTime=time(NULL);
dwd1=GetTickCount()-dwd1;
if(dwd1<pCBus->m_iBusTimer){
if(!pCBus->m_bExit)
Sleep(pCBus->m_iBusTimer-dwd1);
}
};
return 0;
}
catch(...){
TRACE("LOG THREAD EXCEPTION CATCH %lx",pCBus);
if(pCBus->IsValid(pCBus)){
pCBus->m_pTimer=AfxBeginThread(CDataBus::Timer,(LPVOID)pCBus);
}
return 0;
}
};
void CDataBus::OnTimer()
{
CDataBus * pCBus=this;
if(pCBus->IsValid(pCBus)){
EnterCriticalSection(&pCBus->m_hTimeMutex);
try{
POSITION pos=pCBus->m_TimeWorkList.GetHeadPosition(),pos1;
pos1=pos;
while(pos){
WORKERTIME & ft=pCBus->m_TimeWorkList.GetNext(pos);
ft.iCurrent--;
if(ft.iCurrent<0){
if(ft.pWorker->IsValid()){
if(ft.cIsDirectCall){
ft.pWorker->OnTimer(ft.iMaxCount,ft.dwIdent);
}
else {
CUniMsg um;
um.m_pSender=NULL;
um.m_pReceivor=ft.pWorker;
um.m_btMsgType=MSG_EVENT;
if(ft.cIsCmdTimeOut){
um.m_dwIdent=ft.dwIdent;
um.m_dwCommand=EVT_CMD_TIMEOUT;
}
else{
um.m_dwIdent=ft.dwIdent;
um.m_ierror=ft.iMaxCount;
um.m_dwCommand=EVT_WORK_TIMER;
}
if(ft.cIsTimeRoad){
pCBus->m_pRoad[pCBus->m_iTimeRoad]->SendMessage(&um);
}
else{
pCBus->SendMessage(&um,pCBus->m_iTimeRoad);
}
}
if(ft.cIsForeverCall){
ft.iCurrent=ft.iMaxCount;
// TRACE("for ever %ld\n",ft.iMaxCount);
}
else {
DWORD dwid=ft.dwIdent+(DWORD)ft.pWorker;
pCBus->m_TimePos.RemoveKey((void *)dwid);
pCBus->m_TimeWorkList.RemoveAt(pos1);
}
}
else {
DWORD dwid=ft.dwIdent+(DWORD)ft.pWorker;
pCBus->m_TimePos.RemoveKey((void *)dwid);
pCBus->m_TimeWorkList.RemoveAt(pos1);
}
}
pos1=pos;
};
}
catch(...){
//
}
LeaveCriticalSection(&pCBus->m_hTimeMutex);
}
};
DWORD CAsynWorker::SendCmd(CAsynWorker * pReceivor,DWORD dwCommand,int iroad,HANDLE hKey,int iTimerCount,UniVar uvi,...){
CUniMsg um;
um.m_dwReceiveMode=0;
um.m_pReceivor=pReceivor;
um.m_dwCommand=dwCommand;
um.m_btMsgType=MSG_COMMAND;
va_list varl;
va_start(varl,iTimerCount);
UniVar uv;
while(1){
uv=va_arg(varl,UniVar);
if(UniVar::ValidVar(&uv))
um.Add(uv);
else break;
};
va_end(varl);
return SendMsg(&um,iroad,hKey,iTimerCount);
};
DWORD CAsynWorker::SendCmd(DWORD dwReceiveMode,CAsynWorker * pReceivor,DWORD dwCommand,int iroad,HANDLE hKey,int iTimerCount,UniVar uvi,...)
{
CUniMsg um;
um.m_dwReceiveMode=dwReceiveMode;
um.m_pReceivor=pReceivor;
um.m_dwCommand=dwCommand;
um.m_btMsgType=MSG_COMMAND;
va_list varl;
va_start(varl,iTimerCount);
UniVar uv;
while(1){
uv=va_arg(varl,UniVar);
if(UniVar::ValidVar(&uv))
um.Add(uv);
else break;
};
va_end(varl);
return SendMsg(&um,iroad,hKey,iTimerCount);
}
DWORD CAsynWorker::SendResp(CAsynWorker * pReceivor,DWORD dwCommand,int iroad,UniVar uvi,...)
{
CUniMsg um;
um.m_dwReceiveMode=0;
um.m_pReceivor=pReceivor;
um.m_dwCommand=dwCommand;
um.m_btMsgType=MSG_RESPONSE;
va_list varl;
va_start(varl,iroad);
UniVar uv;
while(1){
uv=va_arg(varl,UniVar);
if(UniVar::ValidVar(&uv))
um.Add(uv);
else break;
};
va_end(varl);
return SendMsg(&um,iroad);
};
DWORD CAsynWorker::SendResp(DWORD dwReceiveMode,CAsynWorker * pReceivor,DWORD dwCommand,int iroad,UniVar uvi,...)
{
CUniMsg um;
um.m_dwReceiveMode=dwReceiveMode;
um.m_pReceivor=pReceivor;
um.m_dwCommand=dwCommand;
um.m_btMsgType=MSG_RESPONSE;
va_list varl;
va_start(varl,iroad);
UniVar uv;
while(1){
uv=va_arg(varl,UniVar);
if(UniVar::ValidVar(&uv))
um.Add(uv);
else break;
};
va_end(varl);
return SendMsg(&um,iroad);
}
DWORD CAsynWorker::SendEvent(CAsynWorker * pReceivor,DWORD dwCommand,HANDLE hKey,int iroad,UniVar uvi,...){
CUniMsg um;
um.m_dwReceiveMode=0;
um.m_pReceivor=pReceivor;
um.m_dwCommand=dwCommand;
um.m_btMsgType=MSG_EVENT;
va_list varl;
va_start(varl,iroad);
UniVar uv;
while(1){
uv=va_arg(varl,UniVar);
if(UniVar::ValidVar(&uv))
um.Add(uv);
else break;
};
va_end(varl);
return SendMsg(&um,iroad,hKey);
};
DWORD CAsynWorker::SendEvent(DWORD dwReceiveMode,CAsynWorker * pReceivor,DWORD dwCommand,HANDLE hKey,int iroad,UniVar uvi,...)
{
CUniMsg um;
um.m_dwReceiveMode=dwReceiveMode;
um.m_pReceivor=pReceivor;
um.m_dwCommand=dwCommand;
um.m_btMsgType=MSG_EVENT;
va_list varl;
va_start(varl,iroad);
UniVar uv;
while(1){
uv=va_arg(varl,UniVar);
if(UniVar::ValidVar(&uv))
um.Add(uv);
else break;
};
va_end(varl);
return SendMsg(&um,iroad,hKey);
}
BOOL CAsynWorker::Register(int idx)
{
if(!m_pBus)return FALSE;
return m_pBus->Register(idx,this);
}
void CAsynWorker::UnRegister()
{
if(!m_pBus)return ;
m_pBus->UnRegister(this);
}
CAsynWorker * CAsynWorker::GetRegisterObject(int idx,DWORD dwModId)
{
if(!m_pBus)return NULL;
return m_pBus->GetRegisterObject(idx,dwModId);
}
void CAsynWorker::AddErrorMsg(int id,char * Msg)
{
if(!m_pBus)return;
m_pBus->AddErrorMsg(id,Msg);
}
char * CAsynWorker::GetErrorMsg(int id)
{
if(!m_pBus)return NULL;
return m_pBus->GetErrorMsg(id);
}
void CDataBus::AddTime(CAsynWorker * pWorker,int iNumTimeflake,DWORD dwIdent,bool bDirectCall,bool bForeverCall,bool bCmdTimeOut,bool bTimeRoad)
{
if(!CDataBus::IsValid(this))return ;
DWORD dwPosIdx=dwIdent+(DWORD)pWorker;
POSITION pos;
BOOL bSetPos=TRUE;
EnterCriticalSection(&m_hTimeMutex);
if(m_TimePos.Lookup((void *)dwPosIdx,(void * &)pos)){
WORKERTIME & ft=m_TimeWorkList.GetAt(pos);
if(ft.pWorker==pWorker){
ft.iCurrent=iNumTimeflake;
ft.iMaxCount=iNumTimeflake;
ft.cIsDirectCall=bDirectCall;
ft.cIsForeverCall=bForeverCall;
ft.cIsTimeRoad=bTimeRoad;
ft.cIsCmdTimeOut=bCmdTimeOut;
LeaveCriticalSection(&m_hTimeMutex);
// TRACE("Add Timer suc1\n");
return ;
}
else {
POSITION pos1;
pos1=pos=m_TimeWorkList.GetHeadPosition();
while(pos){
WORKERTIME & ft=m_TimeWorkList.GetNext(pos);
if(ft.pWorker==pWorker&&ft.dwIdent==dwIdent){
ft.iCurrent=iNumTimeflake;
ft.iMaxCount=iNumTimeflake;
ft.cIsDirectCall=bDirectCall;
ft.cIsForeverCall=bForeverCall;
ft.cIsCmdTimeOut=bCmdTimeOut;
ft.cIsTimeRoad=bTimeRoad;
LeaveCriticalSection(&m_hTimeMutex);
// TRACE("Add Timer suc2\n");
return ;
}
pos1=pos;
};
}
bSetPos=FALSE;
}
WORKERTIME st;
st.iCurrent=iNumTimeflake;
st.pWorker=pWorker;
st.iMaxCount=iNumTimeflake;
st.cIsDirectCall=bDirectCall;
st.cIsForeverCall=bForeverCall;
st.cIsTimeRoad=bTimeRoad;
st.cIsCmdTimeOut=bCmdTimeOut;
st.dwIdent=dwIdent;
pos=m_TimeWorkList.AddTail(st);
if(bSetPos){
m_TimePos.SetAt((void *)dwPosIdx,(void * )pos);
}
else{
}
LeaveCriticalSection(&m_hTimeMutex);
};
void CDataBus::ResetTime(CAsynWorker * pWorker,DWORD dwIdent)
{
if(!CDataBus::IsValid(this))return ;
DWORD dwPosIdx=dwIdent+(DWORD)pWorker;
POSITION pos;
EnterCriticalSection(&m_hTimeMutex);
if(m_TimePos.Lookup((void *)dwPosIdx,(void * &)pos)){
WORKERTIME & ft=m_TimeWorkList.GetAt(pos);
if(ft.pWorker==pWorker){
ft.iCurrent=ft.iMaxCount;
LeaveCriticalSection(&m_hTimeMutex);
return ;
}
}
POSITION pos1;
pos1=pos=m_TimeWorkList.GetHeadPosition();
while(pos){
WORKERTIME & ft=m_TimeWorkList.GetNext(pos);
if(ft.pWorker==pWorker&&ft.dwIdent==dwIdent){
ft.iCurrent=ft.iMaxCount;
LeaveCriticalSection(&m_hTimeMutex);
return ;
}
pos1=pos;
};
LeaveCriticalSection(&m_hTimeMutex);
};
void CDataBus::CancelTime(CAsynWorker * pWorker,DWORD dwIdent,BOOL bCmdTimeOut)
{
if(!CDataBus::IsValid(this))return ;
DWORD dwPosIdx=dwIdent+(DWORD)pWorker;
POSITION pos=0;
EnterCriticalSection(&m_hTimeMutex);
if(m_TimePos.Lookup((void *)dwPosIdx,(void * &)pos)){
WORKERTIME & ft=m_TimeWorkList.GetAt(pos);
if(ft.pWorker==pWorker){
m_TimeWorkList.RemoveAt(pos);
m_TimePos.RemoveKey((void *)dwPosIdx);
LeaveCriticalSection(&m_hTimeMutex);
return ;
}
}
POSITION pos1;
pos1=pos=m_TimeWorkList.GetHeadPosition();
while(pos){
WORKERTIME & ft=m_TimeWorkList.GetNext(pos);
if(ft.pWorker==pWorker&&ft.dwIdent==dwIdent){
m_TimeWorkList.RemoveAt(pos1);
LeaveCriticalSection(&m_hTimeMutex);
return ;
}
pos1=pos;
};
LeaveCriticalSection(&m_hTimeMutex);
};
DWORD CDataBus::SendMessage(CUniMsg * pMsg,HANDLE hKey,int timeout)
{
try {
if(!CDataBus::IsValid(this))return 0;
//send to remote
int itflag=0;
DWORD iret=0;
if(pMsg->m_dwSendMode==0||pMsg->m_dwSendMode==m_unMod.dwModId){
pMsg->m_dwSendMode=m_unMod.dwModId;
pMsg->m_iTimeStamp=GetTimeStamp();
if(pMsg->m_btMsgType==MSG_COMMAND){
EnterCriticalSection(&m_hNumMutex);
if(!m_dwIdCount)m_dwIdCount=1;
pMsg->m_dwIdent=m_dwIdCount++;
LeaveCriticalSection(&m_hNumMutex);
if(timeout>0&&pMsg->m_pSender){
AddTime(pMsg->m_pSender,timeout,pMsg->m_dwIdent,false,false,true,true);
itflag=1;
}
}
if(pMsg->m_dwReceiveMode==0||pMsg->m_dwReceiveMode==m_unMod.dwModId){
if(!((WorkerLock*)hKey)->IsValid((WorkerLock*)hKey))pMsg->m_hKey=NULL;
else pMsg->m_hKey=hKey;
}
}
if(pMsg->m_dwReceiveMode!=0&&pMsg->m_dwReceiveMode!=m_unMod.dwModId){
//pRemoteControl
pMsg->m_hKey=hKey;
UniVar uv;
pMsg->WritetMessageToBuffer(uv);
CUniMsg um;
um.SetMessage(NULL,pRemoteControl,EVT_REMOTEMSG_DATA,MSG_EVENT,
UniVar(pMsg->m_dwReceiveMode),
uv,
UniVar(uv.GetDataLength()),
UniVar(0),
InValidVar);
if(!m_pRoad[pRemoteControl->m_btPointRoad]->SendMessage(&um)){
if(itflag){
CancelTime(pMsg->m_pSender,pMsg->m_dwIdent,true);
}
return 0;
}
else {
return 1;
}
}
else {
if(!pMsg->m_pReceivor->IsValid()){
pMsg->m_pReceivor=this->GetRegisterObject((int)pMsg->m_pReceivor);
if(!pMsg->m_pReceivor){
if(itflag){
CancelTime(pMsg->m_pSender,pMsg->m_dwIdent,true);
}
return 0;
}
}
int iroad=m_iDefaultRoad;
if(pMsg->m_pReceivor->m_btIsPointed){
iroad=pMsg->m_pReceivor->m_btPointRoad;
}
pMsg->m_btBusRoad=(BYTE)iroad;
if(m_pRoad[iroad]->IsValid()){
if(m_pRoad[iroad]->SendMessage(pMsg)){
return 1;
}
}
if(itflag){
CancelTime(pMsg->m_pSender,pMsg->m_dwIdent,true);
}
return 0;
}
}
catch(...)
{
LogEvent(exceptionLog,pMsg->m_dwCommand,0,"Command Send error","Except.txt");
return 0;
}
}
DWORD CDataBus::SendMessage(CUniMsg * pMsg,int iRoad,HANDLE hKey,int timeout)
{
try {
if(!CDataBus::IsValid(this))return 0;
int itflag=0;
if(pMsg->m_dwSendMode==0||pMsg->m_dwSendMode==m_unMod.dwModId){
pMsg->m_dwSendMode=m_unMod.dwModId;
pMsg->m_iTimeStamp=GetTimeStamp();
if(pMsg->m_btMsgType==MSG_COMMAND){
EnterCriticalSection(&m_hNumMutex);
if(!m_dwIdCount)m_dwIdCount=1;
pMsg->m_dwIdent=m_dwIdCount++;
LeaveCriticalSection(&m_hNumMutex);
if(timeout>0&&pMsg->m_pSender){
AddTime(pMsg->m_pSender,timeout,pMsg->m_dwIdent,false,false,true,true);
itflag=1;
}
}
if(pMsg->m_dwReceiveMode==0||pMsg->m_dwReceiveMode==m_unMod.dwModId){
if(!((WorkerLock*)hKey)->IsValid((WorkerLock*)hKey))pMsg->m_hKey=NULL;
else pMsg->m_hKey=hKey;
}
}
//send to remote
if(pMsg->m_dwReceiveMode!=0||pMsg->m_dwReceiveMode!=m_unMod.dwModId){
//pRemoteControl
//EVT_REMOTEMSG_DATA
pMsg->m_hKey=hKey;
UniVar uv;
pMsg->WritetMessageToBuffer(uv);
CUniMsg um;
um.SetMessage(NULL,pRemoteControl,EVT_REMOTEMSG_DATA,MSG_EVENT,UniVar(pMsg->m_dwReceiveMode),uv,UniVar(uv.GetDataLength()),InValidVar);
if(!m_pRoad[pRemoteControl->m_btPointRoad]->SendMessage(&um)){
if(itflag){
CancelTime(pMsg->m_pSender,pMsg->m_dwIdent,true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -