Service.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:24k
- #include "stdafx.h"
- #include "Server.h"
- #include "Service.h"
- #include "passwrd.h"
- safe_vector<PCLIENT_STATE> m_omiclientstates;
- bool ModifyClientState(string clienthdid, bool state)
- {
- if(!theApp.m_dbcont.IsActived()) return false;
-
- CADODataSet rs;
- rs.SetConnection(theApp.m_dbcont);
-
- char sql[MAX_SQL_SIZE];
- memset(sql, 0x0, sizeof(sql));
- sprintf(sql, SQL::SERVER_UPDATE_AGSTATE, (state ? 1 : 0), clienthdid.c_str());
- if( rs.Open(sql) )
- {
- for(int i = 0; i < m_omiclientstates.size(); ++i)
- {
- PCLIENT_STATE pitem = m_omiclientstates[i];
- if(NULL == pitem)
- {
- m_omiclientstates.pop_at(i--);
- continue;
- }
- if(!pitem->clienthdid.compare(clienthdid.c_str()))
- {
- pitem->interval = 0;
- break;
- }
- }
- return true;
- }
- return false;
- }
- //update the client state list.
- void UpdateClientState(void)
- {
- if(!theApp.m_dbcont.IsActived())
- {
- //TRACE0("The db connection is not active yetn");
- return ;
- }
- try
- {
- for(int i = 0; i < m_omiclientstates.size(); ++i)
- {
- PCLIENT_STATE pitem = m_omiclientstates[i];
- if(NULL == pitem)
- {
- m_omiclientstates.pop_at(i--);
- continue;
- }
- pitem->interval++;
-
- //update the client database state flag.
- if(pitem->interval > 300)
- {
- theApp.m_syslog.Print(LL_INFO, "UpdateClientState : 侦测到 [%s] 客户端连接状态改变...n", pitem->clienthdid.c_str());
- //restore to zero.
- pitem->interval = 0;
- ModifyClientState(pitem->clienthdid, false);
- }
- }
- }
- catch (...)
- {
- return ;
- }
- }
- //为以后扩充用---部门根据IP地址来划分
- int GetClientDepart(string &clienthdid)
- {
- return -1;
- }
- /********************************************************
- * Class Name : CService. *
- * Purpose : Service objects manager class. *
- * File Name : Service.h / Service.cpp *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-06-01 *
- ********************************************************/
- CService::CService()
- {
- m_bopened = false;
- m_omiservice = NULL;
- }
- CService::~CService()
- {
- this->Stop();
- }
- bool CService::IsOpened(void)
- {
- return m_bopened;
- }
- bool CService::Start(const int serverport)
- {
- theApp.m_syslog.Print(LL_INFO, "CService::Start : 开始创建服务对象...n");
- if(NULL == this->m_omiservice)
- {
- this->m_omiservice = new CServiceThread;
- if(!this->m_omiservice->Create(this, false, serverport))
- {
- theApp.m_syslog.Print(LL_ERROR, "CService::Start : 创建服务对象主线程失败!!!n");
- this->m_bopened = false;
- return false;
- }
- theApp.m_syslog.Print(LL_INFO, "CService::Start : 创建服务对象主线程成功!!!n");
- }
- theApp.m_syslog.Print(LL_INFO, "CService::Start : 创建服务对象成功!!!n");
-
- this->m_bopened = true;
- return true;
- }
- void CService::Stop()
- {
- theApp.m_syslog.Print(LL_INFO, "CService::Stop : 开始销毁服务对象...n");
- if(NULL != this->m_omiservice)
- {
- this->m_omiservice->Stop();
- _DELETE(this->m_omiservice);
- theApp.m_syslog.Print(LL_INFO, "CService::Stop : 服务对象主线程已销毁!!!n");
- }
- theApp.m_syslog.Print(LL_INFO, "CService::Stop : 销毁服务对象成功!!!n");
- this->m_bopened = false;
- }
- /********************************************************
- * Class Name : CServiceThread. *
- * Purpose : Main service thread class. *
- * File Name : Service.h / Service.cpp *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-06-01 *
- ********************************************************/
- CServiceThread::CServiceThread()
- {
- this->m_service = NULL;
- this->m_svrsock = NULL;
- }
- CServiceThread::~CServiceThread()
- {
- this->OnStop();
- }
- //create the acceptor socket listening thread.
- bool CServiceThread::Create(CService *pService, bool bCreateAndSuspended, const uint unSvrPort)
- {
- assert( NULL != pService && NULL == this->m_svrsock );
-
- theApp.m_syslog.Print(LL_INFO, "CServiceThread::Create : 开始创建服务主线程...n");
- this->m_service = pService;
- //create the listening server socket object.
- this->m_svrsock = new CServiceSocket();
- this->m_svrsock->Set_Socket_Address(unSvrPort);
- try{
- if ( this->m_svrsock->Create() )
- {
- if(this->m_svrsock->Open())
- theApp.m_syslog.Print(LL_INFO, "CServiceThread::Create : 创建客户端Socket连接成功!!!n");
- else
- theApp.m_syslog.Print(LL_ERROR, "CServiceThread::Create : 创建客户端Socket连接失败...n");
- }
- }
- catch ( CNetSockException *ex ) {
- theApp.m_syslog.Print(LL_ERROR, "CServiceThread::Create : 创建客户端Socket连接发生异常, ErrorCode = %dn", ex->GetSockErrCode());
- _DELETE( this->m_svrsock );
- throw ex;
- return false;
- }
- return CThread::Create(bCreateAndSuspended, NULL);
- }
- //overrides execute function.
- void CServiceThread::Execute()
- {
- while(!this->CanBeTerminate())
- {
- UpdateClientState();
-
- Sleep(100);
- }
- }
- //start, stop, pause and resume event process functions.
- void CServiceThread::OnStart()
- {
- }
- void CServiceThread::OnStop()
- {
- //close the server socket
- if ( this->m_svrsock && this->m_svrsock->Open(false) )
- {
- _DELETE( this->m_svrsock );
- theApp.m_syslog.Print(LL_INFO, "CServiceThread::OnStop : 服务主线程退出!!!n");
- }
- }
- void CServiceThread::OnSuspend()
- {
- assert(NULL != this->m_svrsock);
- theApp.m_syslog.Print(LL_INFO, "CServiceThread::OnSuspend : 服务主线程挂起!!!n");
- }
- void CServiceThread::OnResume()
- {
- assert(NULL != this->m_svrsock);
- theApp.m_syslog.Print(LL_INFO, "CServiceThread::OnStop : 服务主线程恢复运行!!!n");
- }
- /********************************************************
- * Class Name : CServiceSocket. *
- * Purpose : Service socket object class. *
- * File Name : Service.h / Service.cpp *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-06-01 *
- ********************************************************/
- CServiceSocket::CServiceSocket()
- {
- }
- CServiceSocket::~CServiceSocket()
- {
- theApp.m_syslog.Print(LL_INFO, "CServiceSocket::~CServiceSocket : 开始销毁服务端Socket...n");
- //destroy the vector.
- for(int i = 0; i = (int)m_omicltthreads.size() > 0; )
- {
- CClientThread *pthread = m_omicltthreads[i-1];
- if(NULL == pthread)
- {
- m_omicltthreads.pop_at(i-1);
- continue;
- }
-
- if(pthread->GetSockObj()->Open(false))
- {
- theApp.m_syslog.Print(LL_INFO, "CServiceSocket::~CServiceSocket : 销毁 [%s] 客户端处理线程.n",
- pthread->GetSockObj()->Get_RemoteHost_IP().c_str());
- pthread->Stop();
- _DELETE(pthread);
- m_omicltthreads.pop_back();
- }
- }
- //destroy the list.
- for(i = 0; i = m_omiclientstates.size() > 0; )
- {
- PCLIENT_STATE pitem = m_omiclientstates[i-1];
- if(NULL == pitem)
- {
- m_omiclientstates.pop_at(i-1);
- continue;
- }
- _DELETE(pitem);
- m_omiclientstates.pop_back();
- }
- theApp.m_syslog.Print(LL_INFO, "CServiceSocket::~CServiceSocket : 销毁服务端Socket结束!!!n");
- }
- //===============================================================
- //
- // Process the client's connected event and update clients list.
- //
- //===============================================================
- void CServiceSocket::OnClientConnect(CXWinSocket *pClientSocket)
- {
- CXWinServerSocket::OnClientConnect(pClientSocket);
- if(NULL == pClientSocket) return ;
- string sremoteip = pClientSocket->Get_RemoteHost_IP();
- theApp.m_syslog.Print(LL_INFO, "CServiceSocket::OnClientConnect : 侦听到远程客户端 [%s] 连接...n", sremoteip.c_str());
- if(!theApp.m_dbcont.IsActived())
- {
- theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientConnect : 数据库连接异常,客户端 [%s] 连接被异常终止.n", sremoteip.c_str());
- pClientSocket->Open(false);
- return ;
- }
- //@1---检查该客户端是否已经存在于列表中,如果存在则直接关闭Socket返回.
- CClientThread *pthread = NULL;
- for(int i = 0; i < m_omicltthreads.size(); ++i)
- {
- pthread = m_omicltthreads[i];
- if(NULL == pthread)
- {
- m_omicltthreads.pop_at(i--);
- continue;
- }
- if( pthread->GetSockObj()->Get_RemoteHost_IP() == pClientSocket->Get_RemoteHost_IP() ||
- pthread->GetSockObj()->Get_RemoteHost_Name() == pClientSocket->Get_RemoteHost_Name())
- {
- theApp.m_syslog.Print(LL_WARNING, "CServiceSocket::OnClientConnect : [%s] 客户端已在服务器注册,连接终止!!!n", sremoteip.c_str());
- pClientSocket->Open(false);
- return ;
- }
- }
- //@3---为客户端创建处理线程
-
- theApp.m_syslog.Print(LL_INFO, "CServiceSocket::OnClientConnect : 准备为 [%s] 客户端创建处理线程...n", sremoteip.c_str());
- string remotename = pClientSocket->Get_RemoteHost_Name(), remoteip = pClientSocket->Get_RemoteHost_IP();
- pthread = new CClientThread(pClientSocket);
- if(pthread->Create()){
-
- m_omicltthreads.push_back(pthread);
- theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientConnect : 为 [%s] 客户端创建处理线程成功!!!n", sremoteip.c_str());
- }
- else{
- //如果创建处理线程失败则恢复数据库状态标志
- ModifyClientState(remoteip, false);
- _DELETE(pthread);
- pClientSocket->Open(false);
- theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientConnect : 为 [%s] 客户端创建处理线程失败!!!n", sremoteip.c_str());
- }
- }
- //=============================================================================
- //
- // Process the client's disconnected event and update clients list.
- //
- //=============================================================================
- void CServiceSocket::OnClientClose(CXWinSocket *pClientSocket)
- {
- if(NULL == pClientSocket) return ;
- try
- {
- string sremoteip;
-
- CClientThread *pthread = NULL;
- for(int i = 0; i < m_omicltthreads.size(); ++i)
- {
- pthread = m_omicltthreads[i];
- if(NULL == pthread)
- {
- m_omicltthreads.pop_at(i--);
- continue;
- }
- if(pthread->GetSockObj()->Get_Socket_Handle() == pClientSocket->Get_Socket_Handle())
- {
- //@1
- sremoteip = pthread->m_sremoteip;
- for(int j = m_omiclientstates.size(); j > 0; --j)
- {
- PCLIENT_STATE pitem = m_omiclientstates[j-1];
- if(NULL == pitem)
- {
- m_omiclientstates.pop_at(j-1);
- continue;
- }
- if(0 == pitem->clienthdid.compare(pthread->m_sclienthdid.c_str()))
- {
- if(!ModifyClientState(pitem->clienthdid, false))
- {
- theApp.m_syslog.Print(LL_ERROR, "CServiceSocket::OnClientClose : 修改 [%s] 客户端状态失败.n", pitem->clienthdid.c_str());
- return ;
- }
- _DELETE(pitem);
-
- m_omiclientstates.pop_at(j-1);
-
- break;
- }
- }
- //@2
- pthread->Stop();
- m_omicltthreads.pop_at(i);
- _DELETE(pthread);
-
- break;
- }
- }
- theApp.m_syslog.Print(LL_INFO, "CServiceSocket::OnClientClose : 客户端 [%s] 已端开.n", sremoteip.c_str());
- CXWinServerSocket::OnClientClose(pClientSocket);
- }
- catch (...) {
- CXWinServerSocket::OnClientClose(pClientSocket);
- }
- }
- /********************************************************
- * Class Name : CClientThread. *
- * Purpose : Client processing thread class. *
- * File Name : Service.h / Service.cpp *
- *------------------------------------------------------*
- * Author : Devia Lee. Date: 2004-06-01 *
- ********************************************************/
- CClientThread::CClientThread(CXWinSocket *pClientSocket)
- {
- assert(NULL != pClientSocket && pClientSocket->Socket_IsOpened());
- this->m_pcltsock = pClientSocket;
- this->m_bfirststate = true;
- }
- CClientThread::~CClientThread()
- {
- }
- //send answer to peer.
- bool CClientThread::SendAnswer(RAGPACKTYPE akp, bool bSuccessfully)
- {
- KAGENTPACK agkpack;
- int size = sizeof(agkpack);
-
- memset(&agkpack, 0x0, size);
- agkpack.type = KAGPACKTYPE(akp);
- agkpack.ret = bSuccessfully ? K_OK : K_FAILED;
-
- //for(int i = 0; i < 3; ++i)
- {
- int sended = this->m_pcltsock->SendBuf(&agkpack, size);
- if(-1 == sended)
- {
- theApp.m_syslog.Print(LL_ERROR, "CClientThread::SendAnswer : 发送客户端应答 [%s] 发生Socket异常,客户端处理线程终止!!!n", this->m_sremoteip.c_str());
- this->Stop();
- return false;
- }
- else if(size == sended)
- {
- return true;
- }
- }
- return false;
- }
- //overrides execute function.
- void CClientThread::Execute()
- {
- RAGENTPACK agrpack;
- int size = 0;
-
- this->m_sremotehost = this->m_pcltsock->Get_RemoteHost_Name();
- this->m_sremoteip = this->m_pcltsock->Get_RemoteHost_IP();
- theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 客户端 [%s] 线程开始运行...n", this->m_sremoteip.c_str());
- while(!this->CanBeTerminate() && theApp.m_dbcont.IsActived() &&
- m_pcltsock->Socket_IsValid() && m_pcltsock->Socket_IsOpened())
- {
- size = sizeof(agrpack);
- memset(&agrpack, 0x0, size);
- //@1---recv the package head.
- if(size != m_pcltsock->ReceiveBuf(&agrpack, size))
- {
- Sleep(10);
- continue;
- }
- else if(agrpack.type != RAG_STATE &&
- agrpack.type != RAG_VERIFYPWD &&
- agrpack.type != RAG_EXECUSRSQL)
- {
- Sleep(10);
- continue;
- }
- //@2---recv the package extra data.
- if(agrpack.size > 0)
- {
- agrpack.data = new char[agrpack.size+1];
- memset(agrpack.data, 0x0, agrpack.size+1);
-
- if(agrpack.size != m_pcltsock->ReceiveBuf(agrpack.data, agrpack.size))
- {
- delete []agrpack.data;
-
- Sleep(10);
- continue;
- }
- }
- //@4---process the client's requirements.
- switch(agrpack.type){
- case RAG_STATE:
- {
- theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 接收到客户端 [%s] 状态包...n", this->m_sremoteip.c_str());
- this->ProcessRAGState(agrpack);
- }
- break;
-
- case RAG_VERIFYPWD:
- {
- theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 接收到客户端 [%s] 用户身份校验包...n", this->m_sremoteip.c_str());
- this->ProcessRAGAuthor(agrpack);
- }
- break;
- case RAG_EXECUSRSQL:
- {
- theApp.m_syslog.Print(LL_INFO, "CClientThread::Execute : 接收到客户端 [%s] SQL请求包...n", this->m_sremoteip.c_str());
- this->ProcessRAGUsrSql(agrpack);
- }
- break;
- }
- //free the memory space
- if(agrpack.size > 0)
- {
- delete []agrpack.data;
- }
- Sleep(10);
- }
- }
- //=============================================================================
- //
- // Process the client's timed state requirement and update the database info.
- //
- //=============================================================================
- void CClientThread::ProcessRAGState(RAGENTPACK &agrpack)
- {
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGState : 开始处理客户端 [%s] 状态请求包...n", this->m_sremoteip.c_str());
- //////////////////////////////////////////////////////////////////////////
-
- //get hostname and hardware id.
- char *p = (char*)agrpack.data;
- char *phdid = strchr(p, '|');
- if(!phdid) return ;
- char *phostname = phdid + 1;
- if(!phostname) return ;
- *phdid = ' '; phdid = p;
- //////////////////////////////////////////////////////////////////////////
-
- string remotename = phostname,
- remoteip = this->m_sremoteip;
- this->m_sremotehost = (char*)phostname;
- bool occurerror = false, bsuccessfully = true;
-
- char sql[MAX_SQL_SIZE];
- char *pshdid = new char[strlen(phdid)+1];
-
- memset(pshdid, ' ', strlen(phdid)+1);
- _snprintf(pshdid, agrpack.size, "%s", phdid);
-
- m_sclienthdid = pshdid;
- sprintf(sql, SQL::SERVER_UPDATE_AGINFO, remotename.c_str(), remoteip.c_str(), 1, pshdid);
- CADODataSet rs;
- try
- {
- //@1---更新该客户端状态
- if(this->m_bfirststate)
- {
- PCLIENT_STATE pitem = new CLIENT_STATE;
- pitem->clienthdid = pshdid;
- pitem->interval = 0;
-
- m_omiclientstates.push_back(pitem);
- this->m_bfirststate = false;
- }
- ModifyClientState(pshdid, true);
- //@2---看该客户端是否已经在数据库中注册
-
- rs.SetConnection(theApp.m_dbcont);
-
- if(!rs.Open(sql))
- {
- occurerror = true; bsuccessfully = false;
- goto ACTION;
- }
- rs.Close();
-
- sprintf(sql, SQL::SERVER_GET_AGENTISREG, pshdid);
- if(!rs.Open(sql))
- {
- occurerror = true; bsuccessfully = false;
- goto ACTION;
- }
- //如果该客户端没有注册过则注册之...
- if(rs.Fields().FieldByName("CLTCOUNT").getValue().AsInteger() == 0)
- {
- rs.Close();
- sprintf(sql, SQL::SERVER_AGENT_REGISTER, remotename.c_str(), remoteip.c_str(), pshdid, GetClientDepart(remoteip), 1, remotename.c_str());
- if(!rs.Open(sql))
- {
- occurerror = true; bsuccessfully = false;
- goto ACTION;
- }
- }
- bsuccessfully = ModifyClientState(pshdid, true);
- ACTION:
- this->SendAnswer(agrpack.type, bsuccessfully);
- if(occurerror)
- {
- theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGState : 处理发生错误, [%s] 线程自动终止!!!n", this->m_sremoteip.c_str());
- this->Stop();
- }
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGState : 客户端 [%s] 状态请求处理完毕.n", this->m_sremoteip.c_str());
- if(pshdid) { delete []pshdid; pshdid = NULL; }
- }
- catch (CADOException *ex) {
- theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGState : 处理时发生ADO异常, [%s] 线程自动终止!!!n", this->m_sremoteip.c_str());
-
- if(pshdid) { delete []pshdid; pshdid = NULL; }
- this->SendAnswer(agrpack.type, false);
-
- this->Stop();
-
- delete ex;
- }
- }
- //============================================================
- //
- // Process the client's authentication requirement and
- // return the result(OK or FAILED).
- //
- //============================================================
- void CClientThread::ProcessRAGAuthor(RAGENTPACK &agrpack)
- {
- string sremoteip = this->m_sremoteip;
-
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGAuthor : 开始处理客户端 [%s] 用户身份校验请求包...n", sremoteip.c_str());
- bool bsuccessful = false;
- if(!this->m_pcltsock || !this->m_pcltsock->Socket_IsOpened()) return ;
- #define JUNKUSRNAME "@#$&^!fack@you@ ^R~"
- #define JUNKPASSWRD "@!@#Z$&^"
- //TRACE0((char*)agrpack.data);
- AUTHORINFO authorinfo;
- memset(&authorinfo, 0x0, sizeof(AUTHORINFO));
- memcpy(&authorinfo, agrpack.data, agrpack.size);
- //unencrypting the user name.
- vncDecryptBytes((unsigned char*)authorinfo.username, ENCRYPTKEY);
- //unencrypting the password
- CPasswrd::ToText passwrd_encry(authorinfo.passwrd);
- char username[MAX_USR_SIZE+1], passwrd[MAXPWLEN+1], sql[MAX_SQL_SIZE];
- memset(username, ' ', sizeof(username));
- memcpy(username, authorinfo.username, MAX_USR_SIZE);
- memset(passwrd, ' ', sizeof(passwrd));
- memcpy(passwrd, (const char*)passwrd_encry, MAXPWLEN);
- CADODataSet rs;
- try
- {
- rs.SetConnection(theApp.m_dbcont);
-
- sprintf(sql, SQL::SERVER_VERIFY_SYSUSR, username, passwrd);
- if(!rs.Open(sql))
- {
- memcpy(authorinfo.username, JUNKUSRNAME, MAX_USR_SIZE);
- memcpy(authorinfo.passwrd, JUNKPASSWRD, MAXPWLEN);
- goto ACTION;
- }
- //if not exist then send an junk info.
- if(0 == rs.Fields().FieldByName("SYSUSRCNT").getValue().AsInteger())
- {
- memcpy(authorinfo.username, JUNKUSRNAME, MAX_USR_SIZE);
- memcpy(authorinfo.passwrd, JUNKPASSWRD, MAXPWLEN);
- }
- else
- {
- vncEncryptBytes((unsigned char*)username, ENCRYPTKEY);
- memset(&authorinfo, 0x0, sizeof(authorinfo));
- CPasswrd::FromText encryor(passwrd);
- memcpy(authorinfo.username, username, MAX_USR_SIZE);
- memcpy(authorinfo.passwrd, (const char*)encryor, MAXPWLEN);
-
- bsuccessful = true;
- }
- rs.Close();
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGAuthor : 客户端 [%s] 用户身份校验请求处理完毕.n", sremoteip.c_str());
- }
- catch (CADOException *ex) {
-
- theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGAuthor :处理时发生ADO异常!!!n");
- delete ex;
- }
- ACTION:
- if(this->SendAnswer(RAG_VERIFYPWD, bsuccessful) && bsuccessful)
- {
- if(-1 == this->m_pcltsock->SendBuf(&authorinfo, sizeof(AUTHORINFO)))
- {
- theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGAuthor : 处理时发生Socket异常, [%s] 自动退出!!!n", sremoteip.c_str());
- this->Stop();
- }
- }
- }
- //==========================================================================
- //
- // Process the client's SQL requirement and return the recordset file(XML).
- //
- //==========================================================================
- void CClientThread::ProcessRAGUsrSql(RAGENTPACK &agrpack)
- {
- if(!this->m_pcltsock || !this->m_pcltsock->Socket_IsOpened()) return ;
- string sremoteip = this->m_sremoteip;
-
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 开始处理客户端 [%s] SQL处理请求包...n", sremoteip.c_str());
-
- //@1
- if(!this->SendAnswer(agrpack.type)) return;
-
- EXECSQLINFO execsqlinfo;
- memcpy(&execsqlinfo, agrpack.data, sizeof(EXECSQLINFO));
- //@2---query the dataset and transfer to the client.
- CADODataSet rs;
- try
- {
- rs.SetConnection(theApp.m_dbcont);
- if(rs.Open((const char*)execsqlinfo.sql))
- {
- //if the recordset has exists some records then
- //return the recordset xml file to client peer.
- if(execsqlinfo.needretval)
- {
- //@1---save record set into xml file.
- char szRSXMLFile[_MAX_PATH];
- memset(szRSXMLFile, 0x0, sizeof(szRSXMLFile));
-
- theApp._GetTempFileName("xml", szRSXMLFile);
- rs.SaveToXMLFile((char*)szRSXMLFile);
- rs.Close();
-
- //@2---send the xml file and delete the temporary xml file.
- if(!SendFile((char*)&szRSXMLFile[0]))
- {
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 客户端 [%s] SQL处理中文件发送失败!!!n", sremoteip.c_str());
- }
- else
- {
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 客户端 [%s] SQL处理中文件发送成功!!!n", sremoteip.c_str());
- }
- DeleteFile((LPCSTR)szRSXMLFile);
- }
- this->SendAnswer(RAG_EXECUSRSQL, true);
- theApp.m_syslog.Print(LL_INFO, "CClientThread::ProcessRAGUsrSql : 客户端 [%s] SQL处理请求处理完毕.n", sremoteip.c_str());
- }
- }
- catch (CADOException *ex) {
-
- theApp.m_syslog.Print(LL_ERROR, "CClientThread::ProcessRAGUsrSql : 处理时发生ADO异常!!!n");
- this->SendAnswer(RAG_EXECUSRSQL, false);
- delete ex;
- }
- }
- //==========================================================================
- //
- // Process the client's SQL requirement and return the recordset file(XML).
- // (sub procedure for sending the recordset file(XML).)
- //
- //==========================================================================
- bool CClientThread::SendFile(LPCSTR lpszFile)
- {
- if(!this->m_pcltsock || !this->m_pcltsock->Socket_IsOpened()) return false;
- //@1---open the file.
- HANDLE hFile = CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(INVALID_HANDLE_VALUE == hFile)
- {
- //TRACE0("Failed to open the specialed filen");
- return false;
- }
- //@2--send the file size.
- DWORD sizehigh = 0, sizelower = 0;
- sizelower = GetFileSize(hFile, &sizehigh);
- char cRes = 'O';
- int size = 0, errtimes = 0;
- do{
- size = sizeof(DWORD);
- if(size != this->m_pcltsock->SendBuf(&sizelower, size)) ++errtimes;
- else{
- //@3--wait for client...
- size = sizeof(char);
-
- if(size != this->m_pcltsock->ReceiveBuf(&cRes, size)) ++errtimes;
- else break;
- }
- }
- while(errtimes < 3);
- //3.1
- if(errtimes == 3)
- {
- //TRACE0("Failed to send the file size infon");
- CloseHandle(hFile);
- return false;
- }
- //@4
- int nsended = 0, maxsize = 0;
- DWORD dwleft = sizelower, dwread = 0;
- char buf[MAX_NET_PACKET];
- //need encrypting process???
- while(dwleft > 0)
- {
- memset((char*)buf, 0x0, sizeof(buf));
- maxsize = (dwleft > MAX_NET_PACKET) ? MAX_NET_PACKET : dwleft;
- ReadFile(hFile, buf, maxsize, &dwread, NULL);
- XorEncryptStream(buf, dwread);
- nsended = this->m_pcltsock->SendBuf(buf, dwread);
- if(nsended < 0)
- {
- CloseHandle(hFile);
- return false;
- }
- //wait for client...
- size = sizeof(char);
- if(size != this->m_pcltsock->ReceiveBuf(&cRes, size))
- {
- //TRACE0("Failed to send the file size infon");
- CloseHandle(hFile);
- return false;
- }
- if('O' == cRes){
- dwleft -= nsended;
- }
- else{
- CloseHandle(hFile);
- return false;
- }
- }
- CloseHandle(hFile);
- return true;
- }