📄 gk.cxx
字号:
//////////////////////////////////////////////////////////////////
//
// main.cxx for ZX Intface97
//
// History:
// 2001.10.24 Start it. (shencan@263.net)
//
// History
// 2001.12.31 Add the bcp error judgement. (zoohoo@163.com)
// It's different between sybase and mssql .
// Commented now
//
// History
// 2002.01.10 Add the Database interface. (zoohoo@163.com)
// Use ODBC API
// History
// 2002.02.05 Fix the bugs about the service name. (zoohoo@163.com)
//
// History
// 2002.04.29 Add the watchdog service to restart the interface97.
// So the socket needed. (zoohoo@163.com)
//
// History
// 2002.05.17 Modify program name from <Intfdb> to <Intfdb>
// The condition compile be canceled, and you can choose
// different version by configure file. (zoohoo@163.com)
//////////////////////////////////////////////////////////////////
#if (_MSC_VER >= 1200)
#pragma warning( disable : 4800 ) // one performance warning off
#pragma warning( disable : 4786 ) // warning about too long debug symbol off
#endif
#ifndef WIN32
#include <signal.h>
#endif
#include "gk.h"
#include "global.h"
#include"windows.h"
#include "fileintf_js.h"
//////////////////////////////////////////////////////////////////////////
//the next is added by btrmg for jiangsu socket 2005.01.23
//////////////////////////////////////////////////////////////////////////
//#define SERVPORT 13456
#define cilentnum 20
#define MAXDATASIZE 200
//#include "dbsmgw.h"
//#include "odbcFunc.h"
int DbLogin();
int DbLogoff();
int GetValueFromODBC();
int GetValueFromODBC_ZZ(); //郑州
int GetValueFromFTP();
int GetValueFromFTP_SH(); //上海
int GetValueFromFTP_JS(); //江苏
int GetValueFromMsg(PString line, int socketno);//江苏Socket
PMutex ShutdownMutex;
extern SOCKET vRxSock[20];
// shutdown by GkStatus thread is a problem! canot call this function.
void ShutdownHandler(void)
{
// we may get one shutdown signal for every thread; make sure we
// delete objects only once
CGlobal::Instance()->m_isShutdown = 1;
if ( ShutdownMutex.WillBlock() )
return;
ShutdownMutex.Wait();
return;
}
void KillStatusThread()
{
ShutdownMutex.Signal();
}
#ifdef WIN32
BOOL WINAPI WinCtrlHandlerProc(DWORD dwCtrlType)
{
RTRACE(1, "Gatekeeper shutdown");
ShutdownHandler();
//exit(0); // if we don't exit(), this handler gets called again and again - strange...
return TRUE;
};
#else
void UnixShutdownHandler(int sig)
{
RTRACE(1, "Gatekeeper shutdown (signal " << sig << ")");
ShutdownHandler();
//exit(0);
};
#endif
#ifdef SERVICE
int main(int argc, char ** argv, char ** envp)
{
PProcess::PreInitialise(argc, argv, envp);
static Gatekeeper instance;
return instance._main();
}
Gatekeeper::Gatekeeper()
: PServiceProcess("Zhongxing Co.", "Intfdb", VERSION, 0, BetaCode, 1)
{}
void Gatekeeper::OnStop()
{
RTRACE(1, "Gatekeeper shutdown");
ShutdownHandler();
// delete singleton objects
RTRACE(3, "Deleting global reference tables");
}
#else
PCREATE_PROCESS(Gatekeeper)
Gatekeeper::Gatekeeper()
: PProcess("ZhongXing Co.", "Intfdb", VERSION, 0, BetaCode, 1)
{}
#endif
//////////////////////////////////////////////////////////////////////////
//added by btrmg for jiangsu 97 socket
//////////////////////////////////////////////////////////////////////////
DWORD WINAPI msgdeal(LPVOID lpThreadParameter)
{
int recvbytes,cilentno,wCount;
char buf[MAXDATASIZE];
char sendbuf[MAXDATASIZE];
memset(buf,0,sizeof(buf));
memset(sendbuf,0,sizeof(sendbuf));
cilentno=int(lpThreadParameter);
wCount=0;
// CGlobal global;
while(1)
{
recvbytes=recv(vRxSock[cilentno], buf, MAXDATASIZE, 0);
if (recvbytes<=0)
{
wCount++;
if (wCount>0x32)
{
closesocket(vRxSock[cilentno]);
vRxSock[cilentno]=-1;
RTRACE(2,"the connent is disconnected");
return 0;
}
continue;
}
sendbuf[recvbytes]='\0';
if(recvbytes != 69)//不等61,包格式错误
{
RTRACE(2,"recieve wrong record:");
RTRACE(2,buf);
SYSTEMTIME lpTime;
//memset(lpTime,0,sizeof(LPSYSTEMTIME));
GetLocalTime(&lpTime);
char mTime[20];
sprintf(mTime,"%d",lpTime.wYear);
//strcat(mTime,"-");
char tmp[30]="";
if(lpTime.wMonth <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wMonth);
strcat(mTime,tmp);
if(lpTime.wDay <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wDay);
strcat(mTime,tmp);
if(lpTime.wHour <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wHour);
strcat(mTime,tmp);
if(lpTime.wMinute < 10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wMinute);
strcat(mTime,tmp);
if(lpTime.wSecond < 10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wSecond);
strcat(mTime,tmp);
char sendtmp[MAXDATASIZE];
memset(sendtmp,0,MAXDATASIZE);
sprintf(sendtmp,"%s","#00044");
//memcpy(sendtmp + strlen(sendtmp),buf+6,20);
if(strlen(buf)<=27)
memcpy(sendtmp+strlen(sendtmp),buf+6,strlen(buf)-7);
else
memcpy(sendtmp + strlen(sendtmp),buf+6,20);
strcat(sendtmp,"003");//处理失败
strcat(sendtmp,mTime);//
strcat(sendtmp,"%");
send(vRxSock[cilentno],sendtmp,44,0);
memset (sendtmp,0,sizeof(sendtmp));
memset(buf,0,sizeof(buf) );
}
else
{
int ret=GetValueFromMsg(buf,cilentno);
if(ret !=0)
{
//写出系统错误
SYSTEMTIME lpTime;
//memset(lpTime,0,sizeof(LPSYSTEMTIME));
GetLocalTime(&lpTime);
char mTime[20];
sprintf(mTime,"%d",lpTime.wYear);
//strcat(mTime,"-");
char tmp[30]="";
if(lpTime.wMonth <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wMonth);
strcat(mTime,tmp);
if(lpTime.wDay <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wDay);
strcat(mTime,tmp);
if(lpTime.wHour <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wHour);
strcat(mTime,tmp);
if(lpTime.wMinute < 10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wMinute);
strcat(mTime,tmp);
if(lpTime.wSecond < 10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wSecond);
strcat(mTime,tmp);
char sendtmp[MAXDATASIZE];
memset(sendtmp,0,MAXDATASIZE);
sprintf(sendtmp,"%s","#00044");
memcpy(sendtmp + strlen(sendtmp),buf+6,20);
strcat(sendtmp,"003");//
strcat(sendtmp,mTime);//
strcat(sendtmp,"%");
send(vRxSock[cilentno],sendtmp,44,0);
RTRACE(2,"recieve wrong record:");
//global.Rtrim(buf,recvbytes);
RTRACE(2,buf);
}
else
{
SYSTEMTIME lpTime;
//memset(lpTime,0,sizeof(LPSYSTEMTIME));
GetLocalTime(&lpTime);
char mTime[20];
sprintf(mTime,"%d",lpTime.wYear);
//strcat(mTime,"-");
char tmp[30]="";
if(lpTime.wMonth <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wMonth);
strcat(mTime,tmp);
if(lpTime.wDay <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wDay);
strcat(mTime,tmp);
if(lpTime.wHour <10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wHour);
strcat(mTime,tmp);
if(lpTime.wMinute < 10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wMinute);
strcat(mTime,tmp);
if(lpTime.wSecond < 10)
strcat(mTime,"0");
sprintf(tmp,"%d",lpTime.wSecond);
strcat(mTime,tmp);
char sendtmp[MAXDATASIZE];
memset(sendtmp,0,MAXDATASIZE);
sprintf(sendtmp,"%s","#00044");
memcpy(sendtmp + strlen(sendtmp),buf+6,20);
strcat(sendtmp,"002");
strcat(sendtmp,mTime);//
strcat(sendtmp,"%");
send(vRxSock[cilentno],sendtmp,44,0);
}
}
}
return 1;
}
void Gatekeeper::Main()
{
int sockfd,cilent;
unsigned long dsa;
char hostname[128];
struct sockaddr_in my_addr,remote_addr; /* 本机地址信息 */
struct hostent *ipaddr;
// install signal handlers for cleanup on shutdown
#ifdef WIN32
SetConsoleCtrlHandler(WinCtrlHandlerProc, TRUE);
#else
signal(SIGTERM, UnixShutdownHandler);
signal(SIGINT, UnixShutdownHandler);
signal(SIGQUIT, UnixShutdownHandler);
#endif
CGlobal::Instance();
DbLogin();
RTRACE(1, "\n------------------------------Start server------------------------------\n");
if(CGlobal::Instance()->m_areacode == 6)//Socket
{
memset(vRxSock,-1,20);
if(gethostname(hostname,128)==0)
{
ipaddr=gethostbyname(hostname);
}
if((sockfd=socket(AF_INET, SOCK_STREAM, 0))==-1)
{
RTRACE(2,"create socket error!");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(CGlobal::Instance()->m_serverport);
my_addr.sin_addr = *((struct in_addr *)ipaddr->h_addr);
memset(&(my_addr.sin_zero),0,8);
if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
RTRACE(2,"bind error!");
exit(1);
}
if (listen(sockfd, cilentnum) == -1)
{
RTRACE(2,"listen error!");
exit(1);
}
int sin_size = sizeof(struct sockaddr_in);
for(;;)
{
if((cilent=accept(sockfd,(struct sockaddr *)&remote_addr,
&sin_size)) == -1)
continue;
for(int i=0;i<cilentnum;i++)
{
if(vRxSock[i]==-1)
{
vRxSock[i]=cilent;
HANDLE hThread;
hThread=CreateThread(NULL,0,msgdeal,LPVOID(i),0,&dsa);
if(hThread==NULL)
{
RTRACE(2,"create thread error!");
closesocket(cilent);
vRxSock[i]=-1;
}
break;
}
if(i>17)
{
for(int j=0;j<i;j++)
{
if(send(vRxSock[j],"the is test socket",18,0)==-1)
{
closesocket(vRxSock[j]);
vRxSock[j]=-1;
}
}
for(int k=0;k<i;k++)
{
if(vRxSock[k]==-1)
{
vRxSock[k]=cilent;
break;
}
}
}
}
}
}
else
{
for( ; ; )
{
int re;
//#ifndef HENAN
if(CGlobal::Instance()->m_areacode == 0) //normal area
re = GetValueFromODBC();
//#else
else if(CGlobal::Instance()->m_areacode == 1) //Henan
re = GetValueFromODBC_ZZ();
else if(CGlobal::Instance()->m_areacode == 2) //Shenzhen
re = GetValueFromFTP();
else if(CGlobal::Instance()->m_areacode == 3) //ShangHai
re = GetValueFromFTP_SH();
else if(CGlobal::Instance()->m_areacode == 4) //JiangShu
re = GetValueFromFTP_JS();
//#endif /*HENAN*/
// int HandleFtp();
// int re = HandleFtp();
// the result of bcp is corrected in sybase database
// Treat with failed records of AdsiInfoRecord
// #ifndef MSSQL
// TreatFailFile();
// #endif
if ( re == 0 )
{
for ( int loop = 0; loop < CGlobal::Instance()->m_irrFrequency; loop++)
{
if ( CGlobal::Instance()->m_isShutdown == 1 )
break;
else
Sleep(1000);
}
if ( CGlobal::Instance()->m_isShutdown == 1 )
break;
else
continue;
}
else
break;
}
}
ShutdownMutex.Wait(); // wait for shutdown!
RTRACE(3, "Deleting global reference tables");
DbLogoff();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -