📄 dbsvr.cpp
字号:
}
//4.发送操作结果flag:int + ret:int+sizeofrecord:int + record0+ record1
*(int *)sendbuf=PMDB_UDPHEADID;
*((int *)sendbuf +1)=nret;
if(pRetResultStruct !=NULL)
{
*((int *)sendbuf +2)=pRetResultStruct->nSize;
}else
*((int *)sendbuf +2)=0;
nsend=send(*(SOCKET*)msg.wParam,sendbuf,sizeof(int)*3,0);
if(nsend == SOCKET_ERROR)
{
// writebuginfo(L"DbSvr err 104\r\n");
goto DBACTION_ERR;
}
pCur=pRetResultStruct;
npacket=0;
while(pCur !=NULL)
{
pNext=pCur->pNext;
nsend=send(*(SOCKET*)msg.wParam,(char *)pCur->pBuf,pCur->nSize,0);
if(nsend == SOCKET_ERROR)
{
FreeResult(&pRetResultStruct);
swprintf(cwTemp,L"Send packet %d,Err=%d",npacket,WSAGetLastError());
// writebuginfo(cwTemp);
goto DBACTION_ERR;
}
pCur=pNext;npacket++;
if(npacket%100 ==0)
Sleep(20);
}
FreeResult(&pRetResultStruct);
closesocket(*(SOCKET*)msg.wParam);
delete (SOCKET*)msg.wParam;
break;
DBACTION_ERR:
// ASSERT(0);
closesocket(*(SOCKET*)msg.wParam);
delete (SOCKET*)msg.wParam;
break;
}
break;
default:
break;
}
}
return 0;
}
bool WaitForDataRecv(SOCKET skClient, int nSec)
{
int nRet;
fd_set RSet,WSet;
struct timeval tv;
tv.tv_sec = nSec;
tv.tv_usec = 0;
FD_ZERO(&RSet);
FD_SET(skClient, &RSet);
FD_ZERO(&WSet);
FD_SET(skClient, &WSet);
nRet=select(skClient,&RSet,NULL, NULL, &tv);
// nRet=select(skClient,&RSet,&WSet, NULL, &tv);
if( nRet== 0)
{//timeout
return false;
}else if( nRet ==SOCKET_ERROR)
{
int nErr=WSAGetLastError();
// WCHAR cwTemp[100];
// swprintf(cwTemp,L" nErr=%d \r\n",nErr);
if(nErr ==WSAEINTR)
{
// swprintf(cwTemp,L"socket has closed! \r\n");
}
// writebuginfo(cwTemp);
return false;
}else
return true;
}
bool IsSocketClose(SOCKET skClient)
{
WSAEVENT hEvent;
hEvent=WSACreateEvent();
if(hEvent ==NULL)
return true;
int nRet=WSAEventSelect(skClient,hEvent,FD_CLOSE);
if(nRet ==SOCKET_ERROR)
{
WSACloseEvent(hEvent);
return true;
}
if( WaitForSingleObject(hEvent,0)==WAIT_OBJECT_0)
{//已经关闭
WSACloseEvent(hEvent);
return true;
}
WSACloseEvent(hEvent);
return FALSE;
}
UINT DbSocketServerThread( LPVOID pParam )
{
SOCKET socket1=*(SOCKET *)pParam;
while (bSockeRun)
{
SOCKET skClient = accept(socket1,NULL, NULL);
if(skClient ==INVALID_SOCKET )
{
MessageBox(NULL,L"Accept Failure!",L"DbSvr!",MB_OK);
break;
}
SOCKET *pSocket=new SOCKET;
*pSocket=skClient;
// MessageBox(NULL,L"Accept Ok!",L"post thread message!",MB_OK);
PostThreadMessage(pThreadDb->m_nThreadID,MSG_DBACTION,((WPARAM )pSocket),0);
// Sleep(500);
}
// writebuginfo(L"exit socket thread\r\n");
return 0;
}
void CALLBACK TimerFunDay( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
//同步处理
// writebuginfo(L"enter database record delete");
writebuginfo("file:%s,line:%d*****database record delete\r\n",__FILE__,__LINE__);
//数据维护计划
CTime tm=CTime::GetCurrentTime();
CTime tm1y,tm6m,tm3m,tm1m;
tm1y=CTime(tm.GetYear()-1,tm.GetMonth(),1,0,0,0);
if(tm.GetMonth()>6)
tm6m=CTime(tm.GetYear(),tm.GetMonth()-6,1,0,0,0);
else
tm6m=CTime(tm.GetYear()-1,tm.GetMonth()+6,1,0,0,0);
if(tm.GetMonth()>3)
tm3m=CTime(tm.GetYear(),tm.GetMonth()-3,1,0,0,0);
else
tm3m=CTime(tm.GetYear()-1,tm.GetMonth()+9,1,0,0,0);
if(tm.GetMonth()>1)
tm1m=CTime(tm.GetYear(),tm.GetMonth()-1,1,0,0,0);
else
tm1m=CTime(tm.GetYear()-1,tm.GetMonth()+11,1,0,0,0);
time_t t1y,t6m,t3m,t1m;
t1y=tm1y.GetTime();
t6m=tm6m.GetTime();
t3m=tm3m.GetTime();
t1m=tm1m.GetTime();
KillTimer(NULL,g_TimerFunDayID);
g_TimerFunDayID=SetTimer(NULL,101,24*60*60*1000,TimerFunDay);
static WCHAR SqlIst[10][150];
int index=0;
//1 进出记录 3mon
swprintf(SqlIst[index++],L"Delete from PassRecord where Time<%d",t3m);
//3 报警通知记录 3mon
swprintf(SqlIst[index++],L"Delete from AlarmNotifyHisTable where AlarmSpingID in (select Idx from AlarmSpringTable where Time<%d )",t3m);
//2 报警记录 3mon
swprintf(SqlIst[index++],L"Delete from AlarmSpringTable where Time<%d",t3m);
//4 开支明细 1year
// OutgoingRecordTable
swprintf(SqlIst[index++],L"Delete from OutgoingRecordTable where RecordTime<%d",t1y);
//5 备忘录 6mon
swprintf(SqlIst[index++],L"Delete from TicklerTable where Time<%d",t6m);
//6 可视对讲记录 记录保存1mon,图片本地保存10次,其余保存在服务器
swprintf(SqlIst[index++],L"Delete from InterPhoneRecordTable where Time<%d",t1m);
//7 可视电话记录 记录保存1mon,图片本地保存10次,其余保存在服务器
swprintf(SqlIst[index++],L"Delete from TelVideoRecordTable where Time<%d",t1m);
//8 语音留言 记录保存3mon,图片本地保存10次,其余保存在服务器
swprintf(SqlIst[index++],L"Delete from VoiceMsgTable where Time<%d",t3m);
//9 短信 保存1mon
swprintf(SqlIst[index++],L"Delete from ShortMsgTable where Time<%d",t1m);
::EnterCriticalSection(&g_DbCritialSection);
if(!g_DataBase.OpenData())
{
// writebuginfo(L"can‘t open database");
writebuginfo("file:%s,line:%d,can‘t open database\r\n",__FILE__,__LINE__);
::LeaveCriticalSection(&g_DbCritialSection);
return ;
}
NodeSql *pFirst,*pNode=NULL,*pNext=NULL;
for(int i=0;i<9;i++)
{
pNext=new NodeSql;
memset(pNext,0,sizeof(NodeSql));
pNext->pSql = SqlIst[i];
if(pNode==NULL)
{
pNode=pNext;
pFirst=pNext;
}else
{
pNode->pNext=pNext;
pNode=pNext;
}
}
BOOL bDel=g_DataBase.ExecuteSql(pFirst);
if(bDel)
{
// writebuginfo(L"OK! database record delete");
}else
{
// writebuginfo(L"failure! database record delete");
}
//删除
while(pFirst)
{
pNode=pFirst;
pFirst=pFirst->pNext;
delete pNode;
}
g_DataBase.CloseData();
::LeaveCriticalSection(&g_DbCritialSection);
// writebuginfo(L"exit database record delete");
writebuginfo("file:%s,line:%d*****exit database record delete=%d\r\n",__FILE__,__LINE__,bDel);
}
/*
功能:定时器函数,执行数据库文件的本地备份
参数:标准,略
返回:无
编写人:吕黄梁
时间:2005-06-16
*/
void CALLBACK TimerFunBackUp( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
// DWORD t1,t2;
// t1=::GetTickCount();
// writebuginfo(L"enter database Backup");
writebuginfo("file:%s,line:%d,enter database Backup\r\n",__FILE__,__LINE__);
::EnterCriticalSection(&g_DbCritialSection);
// writebuginfo(L"begin database Backup");
g_DataBase.CloseData();
// writebuginfo(L"begin database Backup01");
if(!g_DataBase.OpenData())//保证备份前数据库是好的
{
writebuginfo("file:%s,line:%derror database Backup\r\n",__FILE__,__LINE__);
// writebuginfo(L"");
::LeaveCriticalSection(&g_DbCritialSection);
return ;
}
// writebuginfo(L"begin database Backup01");
g_DataBase.CloseData();
// writebuginfo(L"begin database copying");
CopyFile(DATABASE_PMDB,DATABASE_PMDB_BACKUP,FALSE);
// t2=::GetTickCount();
// WCHAR cwTemp[100];
// swprintf(cwTemp,L"tm=%d s",(t2-t1)/1000);
// writebuginfo(cwTemp);
::LeaveCriticalSection(&g_DbCritialSection);
// writebuginfo(L"exit database Backup");
writebuginfo("file:%s,line:%d,database Backup ok\r\n",__FILE__,__LINE__);
}
/*
功能:数据库文件的本地恢复
参数:无
返回:
0 -成功
-1 -失败
编写人:吕黄梁
时间:2005-06-16
*/
int DbRecove()
{
writebuginfo("enter database recove\n");
int nret=0;
CDataBase db;
::EnterCriticalSection(&g_DbCritialSection);
g_DataBase.CloseData();
if(!db.OpenData(DATABASE_PMDB_BACKUP))
{
writebuginfo("can't open the backup database \n");
::LeaveCriticalSection(&g_DbCritialSection);
return -1;
}
db.CloseData();
if(CopyFile(DATABASE_PMDB_BACKUP,DATABASE_PMDB,FALSE))
{
writebuginfo("exit database recove success\n");
nret= 0;
}
else
{
nret= -1;
writebuginfo("exit database recove failure,errid=%d\n",GetLastError());
}
::LeaveCriticalSection(&g_DbCritialSection);
return nret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -