📄 tsengineqt.cpp
字号:
#include "qstring.h"
#include <qt\q3ptrlist.h>
//#include <QList>
#include <QtGui/QApplication>
#include <afxcmn.h>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
//#include <qt\qtsoap.h>
#include <QUdpSocket.h>
#include <qhostinfo_p.h>
#include "netUtility.h"
//#include "xstring.h"
#include <atmi.h> /* TUXEDO 头文件*/
//#include <fml32.h>
#include "CTuxedoThread.h"
#include "tsengineqt.h"
#include "cmyapp.h"
#ifndef _TM_DEFINED
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#define _TM_DEFINED
#endif
TUXCALLBACK tuxCallBack;
TSEngineQT app;
CMyApp myApp;
Utility netut;
QApplication *a;
QTimer g_QTimer;
QMutex g_Mutex;
QWaitCondition g_WaitCondi;
Q3PtrList<CTuxedoThread> TuxThreadsList;
Q3PtrList<CTuxedoThread> CallsList;
//QList<CTuxedoThread*> TuxThreadsList;
CTuxedoThread g_TuxTh;
long g_hWnd;
long g_TimerID;
bool g_bInited;
bool g_bIniting;
bool g_bIdeMode;
int g_nInitTimes;
bool g_bNeedReconn;
bool g_bExit;
bool g_bAsync;
FUNTUXEDO fun;
typedef struct ntp {
char Flag;
char PeerClockStratum;
char PeerPollingInterval;
char PeerClockPrecision;
char RootDelay[4];
char ClockDispersion[4];
char ReferenceClock[4];
char ReferenceClockUpdateTime[8];
char OriginateTimeStamp[8];
char ReceiveTimeStamp[8];
char TransmitTimeStamp[8];
} tagNTP;
TSEngineQT::TSEngineQT()
{
int i=3;
g_hWnd=0;
g_TimerID=0;
g_bInited=false;
g_bIniting=false;
g_bIdeMode=false;
fun.hWtuxws32=0;
fun.tpinit =0;
fun.tpcall =0;
fun.tpterm =0;
g_nInitTimes=0;
g_bNeedReconn=false;
g_bExit=false;
/* MSG msg;
while (1)
{
GetMessage(&msg, NULL, 0, 0);
DispatchMessage(&msg);
} */
}
void TSEngineQT::Inited(int i)
{
int j=3;
tuxCallBack(0,0,0);
}
void TSEngineQT::OnTimer()
{
int j=4;
}
TSEngineQT::~TSEngineQT()
{
g_bExit=true;
/*while( 1)
{
// if(!g_TuxTh.isRunning())
// break;
Sleep(50);
}
Sleep(300);*/
for(int i=TuxThreadsList.count()-1; i>=0;i--)
{
CTuxedoThread * tuxThread = TuxThreadsList.at(i);
tuxThread->terminate();
delete tuxThread;
}
TuxThreadsList.clear();
}
void __stdcall TimerProc(HWND hWnd,unsigned int nMsg,unsigned int nTimerid,unsigned long dwTime);
int __stdcall TuxFmlGetRcvFld(long ChannelID,long FldID,long nRow, char * pValue,long * pLen)
{
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next())
{
if (pThr->m_ID==ChannelID)
{ pTuxThread=pThr;
break;
}
}
if(pTuxThread)
{
return pTuxThread->TuxFmlGetRcvFld(FldID,nRow, pValue,pLen);
}
else
return ERROR_THREAD_NOT_EXIST;
//return g_TuxTh.TuxFmlGetRcvFld(FldID,nRow, pValue,pLen);
}
int __stdcall TuxInit( char * WSNADDR,char *userName,char *pwd,char * cltName,char * grpName,bool bAsync,long hWnd, TUXCALLBACK cbAddress)
{
if(!fun.hWtuxws32)
{
fun.hLibfml32 = LoadLibraryA("libfml32.dll");
if (fun.hLibfml32 == NULL)
{
g_TuxTh.m_msgDesc ="Tuxedo FML library not found";
return ERROR_FML_LIB_NOT_FOUND;
}
(FARPROC &)fun.Fchg32=(FARPROC)GetProcAddress(fun.hLibfml32,"Fchg32");
(FARPROC &)fun.Fget32=(FARPROC)GetProcAddress(fun.hLibfml32,"Fget32");
//(FARPROC &)fun.Foccur32=(FARPROC)GetProcAddress(fun.hLibfml32,"Foccur32");
if (fun.Fchg32 == NULL || fun.Fget32 == NULL)
{
FreeLibrary(fun.hLibfml32);
fun.hLibfml32 = NULL;
g_TuxTh.m_msgDesc ="Tuxedo fml library version invalid";
return ERROR_FML_LIB_VERSION_INVALID;
}
fun.hWtuxws32 = LoadLibraryA("wtuxws32.dll");
if (fun.hWtuxws32==NULL)
{ FreeLibrary(fun.hLibfml32);
fun.hLibfml32 = NULL;
g_TuxTh.m_msgDesc ="Tuxedo library not found";
return ERROR_TUX_LIB_NOT_FOUND;
}
(FARPROC &)fun.tpcall=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpcall");
(FARPROC &)fun.tpalloc=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpalloc");
(FARPROC &)fun.tpfree=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpfree");
(FARPROC &)fun.tpinit=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpinit");
(FARPROC &)fun.tpterm=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpterm");
(FARPROC &)fun.tuxputenv=(FARPROC)GetProcAddress(fun.hWtuxws32,"tuxputenv");
(FARPROC &)fun.tpstrerror=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpstrerror");
(FARPROC &)fun.tperrordetail=(FARPROC)GetProcAddress(fun.hWtuxws32,"tperrordetail");
(FARPROC &)fun.tpstrerrordetail=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpstrerrordetail");
(FARPROC &)fun.tpacall=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpacall");
(FARPROC &)fun.tpgetrply=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpgetrply");
(FARPROC &)fun.tpcancel=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpcancel");
(FARPROC &)fun.tpabort=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpabort");
(FARPROC &)fun.tpbegin=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpbegin");
(FARPROC &)fun.tpcommit=(FARPROC)GetProcAddress(fun.hWtuxws32,"tpcommit");
//(FARPROC &)fun.tperrno=(FARPROC)GetProcAddress(fun.hWtuxws32,"tperrno");
if (fun.tpcall == NULL || fun.tpalloc == NULL ||fun.tpfree == NULL || fun.tpinit == NULL ||fun.tpterm == NULL||fun.tuxputenv==NULL||fun.tpstrerror==NULL)
{ FreeLibrary(fun.hLibfml32);
fun.hLibfml32 = NULL;
FreeLibrary(fun.hWtuxws32);
fun.hWtuxws32 = NULL;
g_TuxTh.m_msgDesc ="Tuxedo version invalid";
return ERROR_TUX_LIB_VERSION_INVALID;
}
}
tuxCallBack=cbAddress;
g_hWnd=hWnd;
int r=0;
if (g_bInited)
{
return ERROR_TUX_ALREADY_INITED;
}
else if (!g_bIniting)
{ g_bIniting=true;
g_bAsync=bAsync;
g_TuxTh.TuxInit( WSNADDR,userName,pwd,cltName,grpName,bAsync,cbAddress);
g_TuxTh.m_bForInitOrCall=true;
if (bAsync)
g_TuxTh.start();
else
r=g_TuxTh.RunInit();
}
return r;
}
int __stdcall TuxFmlAllocBuf(long ChannelID, long nSndBufLen,long nRcvBufLen)
{
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if (pThr->m_ID==ChannelID)
pTuxThread=pThr;
}
if(!pTuxThread)
{
g_Mutex.lock();
pTuxThread=new CTuxedoThread;
TuxThreadsList.append( pTuxThread);
pTuxThread->m_ID=ChannelID;
g_Mutex.unlock();
}
return pTuxThread->TuxFmlAllocBuf(nSndBufLen, nRcvBufLen);
}
int __stdcall TuxFmlFreeBuf(long ChannelID)
{
//return g_TuxTh.TuxFmlFreeBuf();
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if (pThr->m_ID==ChannelID)
{ pTuxThread=pThr;
break;
}
}
if(pTuxThread)
{
pTuxThread->m_ID=-1;
return pTuxThread->TuxFmlFreeBuf();
}
else
return ERROR_THREAD_NOT_EXIST;
}
int __stdcall TuxFmlSetSndFld(long ChannelID,long FldID,long nRow, char * pValue,long nLen)
{
//return g_TuxTh.TuxFmlSetSndFld(FldID,nRow,(char*) pValue,nLen);
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if (pThr->m_ID==ChannelID)
{ pTuxThread=pThr;
break;
}
}
if(pTuxThread)
{
return pTuxThread->TuxFmlSetSndFld(FldID,nRow,(char*) pValue,nLen);
}
else
return ERROR_THREAD_NOT_EXIST;
}
int __stdcall TuxFmlCall(long ChannelID,char * SrvName,bool bAsync)
{
// g_TuxTh.TuxFmlSetupCall(SrvName);
// return g_TuxTh.TuxFmlRunCall();
// if(!g_bInited)
// return ERROR_TUX_NOT_INITED;
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if (pThr->m_ID==ChannelID)
pTuxThread=pThr;
}
if(!pTuxThread)
return ERROR_THREAD_NOT_EXIST;
if(pTuxThread->m_bCalling)
return ERROR_FML_CALL_ALREADY_RUNNING;
pTuxThread->m_bCalling=true;
pTuxThread->TuxFmlSetupCall(SrvName);
if(g_bAsync)
{
g_Mutex.lock();
CallsList.append (pTuxThread);
g_Mutex.unlock();
return 0;
}
else
{
int r=pTuxThread->TuxFmlRunCall();
if(r==12)
{
fun.tpterm();
r=g_TuxTh.RunInit();
pTuxThread->m_ErrID=r;
pTuxThread->m_msgDesc=g_TuxTh.m_msgDesc;
if (!r)
{
r=pTuxThread->TuxFmlRunCall();
}
}
pTuxThread->m_bJustFinished=true;
pTuxThread->m_bCalling=false;
return r;
}
}
int __stdcall TuxInitDone()
{
if(g_bIniting)
return 0;
else
return 1;
}
int __stdcall TuxCallDone(long ChannelID)
{
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if (pThr->m_ID==ChannelID)
{ pTuxThread=pThr;
break;
}
}
if(!pTuxThread)
return 0;
if(pTuxThread->m_bCalling)
return 0;
else
return 1;
}
int __stdcall TuxGetInitErrID()
{
CTuxedoThread * pTuxThread=&g_TuxTh;
if(pTuxThread)
{
return pTuxThread->m_ErrID;
}
else
{
return ERROR_THREAD_NOT_EXIST;
}
}
int __stdcall TuxGetInitErrDesc(char **psErrDesc,long * pLen)
{
CTuxedoThread * pTuxThread=&g_TuxTh;
if(pTuxThread)
{
QByteArray ba= pTuxThread->m_msgDesc.toLocal8Bit();
strcpy(*psErrDesc,ba.data());
*pLen=pTuxThread->m_msgDesc.size();
return 0;
}
else
{
return ERROR_THREAD_NOT_EXIST;
}
}
int __stdcall TuxGetCallErrID(long ChannelID)
{
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next())
{
if (pThr->m_ID==ChannelID)
{ pTuxThread=pThr;
break;
}
}
if(pTuxThread)
{
return pTuxThread->m_ErrID;
}
else
{
return ERROR_THREAD_NOT_EXIST;
}
}
int __stdcall TuxGetCallErrDesc(long ChannelID,char **psErrDesc,long * pLen)
{
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next())
{
if (pThr->m_ID==ChannelID)
{ pTuxThread=pThr;
break;
}
}
if(pTuxThread)
{
QByteArray ba= pTuxThread->m_msgDesc.toLocal8Bit();
strcpy(*psErrDesc,ba.data());
*pLen=pTuxThread->m_msgDesc.size();
return 0;
}
else
{
return ERROR_THREAD_NOT_EXIST;
}
}
int __stdcall TuxStrCall(char * SrvName, char *inputBuf,char **outputBuf,long * outputLen,bool bAsync)
{
if(bAsync && tuxCallBack)
{
if(!g_TimerID)
g_TimerID=SetTimer(0,1,500,TimerProc);
}
CTuxedoThread * pTuxThread=&g_TuxTh;
/*
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if(!pThr->isRunning())
{
pTuxThread=pThr;
}
}
if(!pTuxThread)
{
pTuxThread=new CTuxedoThread;
TuxThreadsList.append( pTuxThread);
}
*/
pTuxThread=&g_TuxTh;
pTuxThread->m_bForInitOrCall=false;
pTuxThread->m_bAsync=bAsync;
pTuxThread->TuxSetupCall( SrvName,inputBuf,outputBuf, outputLen);
if (bAsync)
{ pTuxThread->start();
return 0;
}
else
{
return pTuxThread->TuxStrRunCall();
}
// if(tuxCallBack)
// tuxCallBack(1,1,"ok");
return 0;
}
int __stdcall TuxTerm()
{
CTuxedoThread *pThr;
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
pThr->TuxFmlFreeBuf();
}
if(!g_bInited)
return 0;
if(g_bAsync)
{
g_bExit=true;
while( 1)
{
if(!g_TuxTh.isRunning())
break;
Sleep(50);
}
g_bExit=false;
}
else
{
fun.tpterm();
}
g_bInited=false;
return 0;
}
void __stdcall TimerProc(HWND hWnd,unsigned int nMsg,unsigned int nTimerid,unsigned long dwTime)
{
int i=1;
CTuxedoThread *pThr;
CTuxedoThread * pTuxThread=0;
if(!tuxCallBack)
{
KillTimer(0,nTimerid);
return;
}
for ( pThr=TuxThreadsList.first();pThr; pThr=TuxThreadsList.next() )
{
if(!pThr->isRunning())
{
if(pThr->m_bJustFinished)
{
pThr->m_bJustFinished=false;
QByteArray ba= pThr->m_msgDesc.toLocal8Bit();
long saddr=(long)ba.data();
long slen=ba.length();
if(pThr->m_bForInitOrCall )
{
tuxCallBack( pThr->m_msgID,saddr,slen);
}
else
{
tuxCallBack( pThr->m_msgID,saddr,slen);
}
}
}
}
}
/*
int __stdcall GetNtpTime(char *sIP,long port,char ** psTime)
{
CSocket SendSocket;
ntp SendBuf;
int Port = 123; //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -