📄 unibus.h
字号:
#ifndef __UniBus_h__
#define __UniBus_h__
#include "UniVar.h"
#define MSG_COMMAND 2 //command flag
#define MSG_EVENT 1 //event flag
#define MSG_RESPONSE 0 //responce flag
#define MSG_SYNCMD 3 //command flag
#define EVT_WORK_TIMER 0x40000001 //time event ehwn a work set it before,snet by timer,no parameter
#define EVT_CMD_TIMEOUT 0x40000002 //timeout event,it is set by sender,sent by timer,no parameter
#define EVT_WORK_CANCEL 0x40000003 //command cancel event ,sent by command sender,no parameter
#define EVT_CMD_STOPCMD 0x40000004 //command stoped event ,sent by command receivor,no parameter
#define EVT_CMD_BUSYCMD 0x40000005 //receivor busy event ,sent by command receivor,no parameter
#define EVT_CMD_LOCKSCS 0x40000006 //receivor busy event ,sent by command receivor,no parameter
#define EVT_CMD_VALIDRSV 0x40000007 //the receivor is invalid
#define EVT_COMM_STATUS 0x40000008 //comm dll status changed
#define EVT_COMM_DATA 0x40000009 //comm dll data arrived
#define EVT_REMOTEMSG_DATA 0x4000000a //comm dll data for send
#define EVT_GET_MODNAME 0x4000000b //remote get name
#define EVT_NEED_REPORT 0x4000000c //
#define EVT_CANT_SEND 0x4000000d //can't send
#define EVT_NOTIFY_LINE 0x4000000e //on/off line notify
#define MAXNUMOFEMPTYSLOT 10//the empty data slot number is limited by 6 bit,the max value is 64
#define MAX_ROAD_NUMBER 256//the max number of road in one bus,
//the road message is placed in a message's m_btBusRoad which is composited by 6 bit,
//so the 64 is needed
#define MAX_BUS_SENDER 10240 //queue length of bus
#define BUS_MANAGE_TIMER 250 //update the bus time,every time when timer come,the bus time is updated by bus manager
#define TIME_ELEAPS 200 //the time flake in one bus
#define COPYLOGILE 0xf0000000
#define PACKTYPEMARK 0xf0000000
#define PACKFLAGMARK 0x0f000000
#define PACKLENGTHMARK 0x00ffffff
#define SYSTEMPACK 0x00000000
#define INSTRPACK 0x10000000
#define FILEPACK 0x20000000
#define CODEPACKFLAG 0x1000000
#define ZAPPACKFLAG 0x2000000
#define CMD_TASK_RUNTASK 0x41000003
#define CMD_TASK_FILEBLOCK 0x41000005
#define CMD_TASK_DLLPROXY 0x41000004
#define NUM_OF_BUS_SEG 5
#define EVT_EXCUTE_REMOTE 0x19711218
#define EVT_EXCUTE_OVER 0x19721218
#define EVT_EXCUTE_APPLY 0x19731218
enum COMMANDRESULT {LINE_CONTINUE,LINE_JUMP,LINE_PAUSE,LINE_END} ;
class CAsynWorker;
class AFX_EXT_CLASS CUniMsg
{
public:
DWORD m_dwReceiveMode;
CAsynWorker * m_pReceivor;
DWORD m_dwSendMode;
CAsynWorker * m_pSender;
DWORD m_dwCommand;
short int m_iNumOfTV;
BYTE m_btIsTimeLimited:2,
m_btMsgType:2,
m_btEmptySlotNum:4;
BYTE m_btBusRoad;
long m_iTimeStamp;
DWORD m_dwIdent;
HANDLE m_hKey;
int m_ierror;
UniVar * m_pTV;
CUniMsg(){
memset(&m_dwReceiveMode,0,sizeof(CUniMsg));
};
~CUniMsg(){
clearptv();
};
CUniMsg(CUniMsg * pMsg);
static unsigned short CRC_Check(int Bytes_Num,char * Data_In);
void WritetMessageToBuffer(UniVar & uvBuffer,int flag=1);
BOOL ReadMessageFromBuffer(UniVar & uvBuffer);
static DWORD GetReceiveModFromBuffer(UniVar & uvBuffer);
void Clear(){
clearptv();
memset(&m_dwReceiveMode,0,sizeof(CUniMsg));
};
void SetMessage(DWORD dwSendMode,DWORD dwReceiveMode,CAsynWorker * pSender,CAsynWorker * pReceivor,DWORD dwCommand,BYTE btMsgType,UniVar uvi=InValidVar,...);
void SetMessage(CAsynWorker * pSender,CAsynWorker * pReceivor,DWORD dwCommand,BYTE btMsgType,UniVar uvi=InValidVar,...);
const CUniMsg & operator =(const CUniMsg & Msg);
int Add(UniVar tv);
void Add(UniVar * tv,int size){
for(int i=0;i<size;i++){
Add(tv[i]);
}
};
int InsertAt(UniVar tv,int iPos);
int RemoveAt(UniVar & tv,int iPos);
inline int GetSize(){return m_iNumOfTV;};
inline DWORD GetMsgIdent(){return m_dwIdent;};
inline UniVar & GetAt(int iPos){
ASSERT(m_btEmptySlotNum < MAXNUMOFEMPTYSLOT);
ASSERT(iPos>=0&&iPos<m_iNumOfTV);
return m_pTV[iPos];
};
inline void RemoveAll(){
clearptv();
m_pTV=NULL;
m_iNumOfTV=0;
m_btEmptySlotNum=0;
};
inline UniVar & operator[](int iPos){
ASSERT(m_btEmptySlotNum < MAXNUMOFEMPTYSLOT);
ASSERT(iPos>=0&&iPos<m_iNumOfTV);
return m_pTV[iPos];
};
inline clearptv(){
if(m_pTV){
for(int j=0;j<m_iNumOfTV;j++){
if(m_pTV[j].GetDataLength()){
m_pTV[j]=0;
}
}
UniVar::DeleteBlock(m_pTV);
}
}
int SafeGetAt(UniVar & tv,int iPos){
ASSERT(m_btEmptySlotNum < MAXNUMOFEMPTYSLOT);
if(iPos<0||iPos>=m_iNumOfTV)return -1;
tv=m_pTV[iPos];
return iPos;
};
};
enum LOGTYPE {eventLog,warningLog,errorLog,exceptionLog};
class CDataBus;
class AFX_EXT_CLASS CAsynWorker{
CAsynWorker * m_pThis;
protected:
CDataBus * m_pBus;
public:
BYTE m_btCurRoad,
m_btPointRoad,
m_btIsPointed,
m_btNumberOfRoad;
CUniMsg * m_pCmdReceive;
CUniMsg * m_pCmdSend;
void PointeRoad(int iNum=1,int iTimeOut=0,BOOL bAuto=TRUE);
void PointeRoad(BYTE btRoad){m_btPointRoad=m_btIsPointed=btRoad;m_btNumberOfRoad=0;};
void UnPointeRoad();
virtual void QueueMenuberTimeOut(CUniMsg *pMsg){};
CAsynWorker(CDataBus * pBus=NULL);
~CAsynWorker();
void SetBus(CDataBus * pBus);
BOOL IsValid(){
try{
if(!this)return FALSE;
if(m_pThis!=this)return FALSE;
return TRUE;
}
catch(...){
return FALSE;
}
}
inline CDataBus * GetBus(){return m_pBus;};
virtual void OnMsgReceive(CUniMsg * pMsg);//通常情况下,不要重载这个函数
void CancelCommand();
void StopResponse();
HANDLE LockWorker(DWORD dwId=0,CAsynWorker * pWorker=NULL);
void ApplyWorkerLock(DWORD dwId=0,CAsynWorker * pWorker=NULL);
void UnlockWorker(HANDLE hKey,CAsynWorker * pWorker=NULL,DWORD dwID=0);
BOOL IsLocked(CAsynWorker * pWorker=NULL);
DWORD GetModId();
void ClearMessage(){
CUniMsg * pTemp=m_pCmdReceive;
m_pCmdReceive=NULL;
try{
delete pTemp;
}
catch(...){}
pTemp=m_pCmdSend;
m_pCmdSend=NULL;
try{
delete pTemp;
}
catch(...){}
};
DWORD SendMsg(CUniMsg * pMsg,int iroad=-1,HANDLE hKey=NULL,int iTimerCount=-1);
BOOL SendMsg(DWORD & dwIdent,CUniMsg * pMsg,int iroad=-1,HANDLE hKey=NULL,int iTimerCount=-1);
DWORD SendCmd(CAsynWorker * pReceivor,
DWORD dwCommand,
int iroad=-1,
HANDLE hKey=NULL,
int iTimerCount=-1,
UniVar uvi=InValidVar,...);
DWORD SendCmd(DWORD dwReceiveMode,CAsynWorker * pReceivor,
DWORD dwCommand,
int iroad=-1,
HANDLE hKey=NULL,
int iTimerCount=-1,
UniVar uvi=InValidVar,...);
DWORD SendResp(CAsynWorker * pReceivor,
DWORD dwCommand,
int iroad=-1,
UniVar uvi=InValidVar,...);
DWORD SendResp(DWORD dwReceiveMode,CAsynWorker * pReceivor,
DWORD dwCommand,
int iroad=-1,
UniVar uvi=InValidVar,...);
DWORD SendEvent(CAsynWorker * pReceivor,
DWORD dwCommand,
HANDLE hKey=NULL,
int iroad=-1,
UniVar uvi=InValidVar,...);
DWORD SendEvent(DWORD dwReceiveMode,CAsynWorker * pReceivor,
DWORD dwCommand,
HANDLE hKey=NULL,
int iroad=-1,
UniVar uvi=InValidVar,...);
void SimpleAnswerCommand(CUniMsg * pMsg)
{
SendResp(pMsg->m_dwSendMode,pMsg->m_pSender,pMsg->m_dwCommand,pMsg->m_btBusRoad);
}
void GetRoadRight();
void ReleaseRoadRight();
void AddTime(int iNumTimeflake,DWORD dwIdent=0,bool bDirectCall=false,bool bForeverCall=false,bool bTimeRoad=true);
void CancelTime(DWORD dwIdent=0);
void ResetTime(DWORD dwIdent=0);
void LogEvent(LOGTYPE logtype,int icommand,int ilevel,char * lpszdescripe,char * lpLogFileName=NULL);
long GetTimeStamp();
BOOL Register(int idx);
void UnRegister();
CAsynWorker * GetRegisterObject(int idx,DWORD dwModId=0);
void AddErrorMsg(int id,char * Msg);
char * GetErrorMsg(int id);
BOOL SynMsgProc(CUniMsg * pCmdSend,CUniMsg * pCmdReceive);
enum SYNMSGRESULT {synnowdo,asynnowdo,asyndolater};
BOOL SetSynMsgReturn(DWORD dwModId,CAsynWorker * pAsWorker,DWORD dwCommand,UniVar uvi=InValidVar,...);
BOOL SetSynMsgReturn(DWORD dwModId,CAsynWorker * pAsWorker,DWORD dwCommand,UniVar * pVar,int iNum);
void ProcSynMsg(CUniMsg * pMsg);
inline void GetTime(CTime & tm){tm=CTime(GetTimeStamp());};
virtual void OnEvtCancel(CUniMsg * pMsg){};
virtual void OnMsgCmd(CUniMsg *);
virtual void OnMsgResp(CUniMsg * resp,CUniMsg * cmd){};
virtual void OnMsgEvent(CUniMsg *){};
virtual void OnLockSuccess(HANDLE hkey,CAsynWorker * pWorker,DWORD dwID){};
virtual void OnTimer(int iNumTimeflake,DWORD dwIdent){};
virtual void OnSynMsg(CUniMsg * pCmdSend,CUniMsg * pCmdReceive){pCmdReceive->m_ierror=-4;};
virtual SYNMSGRESULT OnAsynMsg(CUniMsg * pCmdSend,CUniMsg * pCmdReceive){return synnowdo;};
virtual COMMANDRESULT ExcuteDirect(DWORD dwCommand,DWORD dwObj,DWORD dwModId,int iNum,UniVar * pVar){return LINE_CONTINUE;};
virtual void OnExcuteOver(int ierror,DWORD dwCommand,UniVar * pVar,int iNum){};
};
struct WORKERTIME{
CAsynWorker * pWorker;
int iCurrent;
int iMaxCount;
char cIsForeverCall;
char cIsTimeRoad;
char cIsDirectCall;
char cIsCmdTimeOut;
DWORD dwIdent;
};
class BusRoad;
class CRoadListTimer:public CAsynWorker
{
public :
CRoadListTimer(int iSec,BusRoad * pRoad);
~CRoadListTimer();
virtual void OnTimer(int iNumTimeflake,DWORD dwIdent);
BusRoad * m_pRoad;
int m_iSec;
};
struct MSGLISTDATA{
HANDLE m_hMutex;
HANDLE m_hSemaphore;
int m_iGetOnNum;
int m_iGetOffNum;
int m_iRejectNum;
CAsynWorker * m_pOwner;
CRoadListTimer * pTimer;
CList<CUniMsg *,CUniMsg * > m_ListMsg;
BOOL m_bExit;
int m_btExitThread;
};
class BusRoad{
public:
BusRoad * m_pThis;
MSGLISTDATA * m_pData;
CDataBus * m_pBus;
CArray<CUniMsg * ,CUniMsg *> * m_pOwnMsg;
int m_bCreateData;
int m_iStatus;
int m_iRoad;
int m_iError;
HANDLE m_hFlowMutex;
CWinThread * m_pThread;
CUniMsg * m_pCurMsg;
void SetBus(CDataBus * pBus){m_pBus=pBus;};
BusRoad(int iroad,MSGLISTDATA * pD=NULL,CAsynWorker * pO=NULL);
~BusRoad();
#define INVALID_IDENT 0x00000000
DWORD SendMessage(CUniMsg * pMsg,BOOL bFromHead=FALSE);
bool CancelCmd(CUniMsg * pMsg);
BOOL IsValid(){
try{
if(!this)return FALSE;
if(m_pThis!=this)return FALSE;
return TRUE;
}
catch(...){
return FALSE;
}
}
static UINT Drive(LPVOID lpVoid);
};
struct BUSTIMER{
UINT uIdTimer;
CDataBus * p;
};
class AFX_EXT_CLASS WorkerLock{
public:
WorkerLock * m_pThis;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -