📄 hqdest.c
字号:
#include <windows.h>#include <time.h>#include "main.h"#include "hq.h"#include "pctcp.h"#include "hqdest.h"#include "monitor.h"#include "cl_hq.h"#include "cl_data.h"#include "cl_main.h"CRITICAL_SECTION hqCriticalSection;DEST_INFOS dest_infos;DEST_INFO dest_info[MAX_USER_COUNT];int dest_info_count=0;BOOL fDestInited =FALSE;BOOL gfIsNewsSrcDel=FALSE;HANDLE hThrdScanDest =NULL;HANDLE hNewsManager =NULL; void FreeDest(void);LPDEST_INFO GetDestByUser(LPSTR userID);LPDEST_INFO GetFirstDeadDest(void);int FillDpMain(LPDP_MAIN lpDpMain);extern BOOL run_cancelled,gfReadData;extern int Delay_Send_Hq01(LPDEST_INFO lpDest);extern int UDP_Server_Send_Hq10(LPDEST_INFO);extern int Send_Hq10(LPDEST_INFO lpDest);extern int UDP_Server_Send_Gra10(LPDEST_INFO);extern int Send_Gra10(LPDEST_INFO lpDest);extern int UDP_Server_Send_Dp(int sd, struct sockaddr *lpdest, BOOL fThrd, LPDEST_INFO lpDest);extern int UDP_ReadHq(int sd,char * RecvBuf,int len,struct sockaddr_in dest);extern void HqResetChangedTimes(void);extern void SetHqUsers(int);extern BOOL IsZsRec(int, short);extern int sdHq;extern MONITOR Monitor;extern int RefreshHq(int jys);extern char szDataPath[128],szNewsPath[128],szNewsSrc[128];extern char ZxDataFile[128];extern HFILE hfZx;extern int SendMsg(int sd, LPSTR);extern BOOL ZlibCompressFile(char *infile,char *outfile);BOOL DestInit(void){ fDestInited =TRUE; memset(&dest_infos, 0, sizeof(dest_infos)); memset(&dest_info[0],0,sizeof(DEST_INFO)*MAX_USER_COUNT); dest_infos.lpDestHead =&dest_info[0]; dest_info_count=0; return TRUE;}void DestExit(void){ if(!fDestInited) return; fDestInited =FALSE; if(hThrdScanDest) CloseHandle(hThrdScanDest); FreeDest();}long GetDestCount(void){ return MAKELONG(dest_infos.destCount, dest_infos.aliveCount);}int LockDest(LPDEST_INFO lpDestTmp,int num){ int i; i=0; lpDestTmp->isUsing = TRUE; return TRUE;/* EnterCriticalSection(&hqCriticalSection); do { if(!lpDestTmp->isUsing) { lpDestTmp->isUsing = TRUE; LeaveCriticalSection(&hqCriticalSection); return TRUE; } LeaveCriticalSection(&hqCriticalSection); ShoutBlockingHook(); Sleep(40); EnterCriticalSection(&hqCriticalSection); i++; }while(lpDestTmp->isUsing&&i<num); LeaveCriticalSection(&hqCriticalSection); return FALSE;*/}int xLockDest(LPDEST_INFO lpDestTmp,int num){ int i =0; do { if(!lpDestTmp->isUsing) { lpDestTmp->isUsing = TRUE; return TRUE; } i++; }while(lpDestTmp->isUsing&&i<num); return FALSE;}void UnlockDest(LPDEST_INFO lpDestTmp){// EnterCriticalSection(&hqCriticalSection); lpDestTmp->isUsing=FALSE;// LeaveCriticalSection(&hqCriticalSection);}int HqDestActiveUsr(int sd, struct sockaddr *lpdest, LPSTR userID,int ret){ LPDEST_INFO lpDestTmp; //lpDestTmp1; lpDestTmp =GetDestBySd(sd); if(lpDestTmp ==NULL) return -1; if(!LockDest(lpDestTmp,10)) { ErrMsg(ghWndMain,"来自HqDestActiveUsr:加锁失败!"); return -1; } strcpy(lpDestTmp->userID, userID); lpDestTmp->isDead =FALSE; lpDestTmp->recNum=ret; time(&lpDestTmp->startTime); lpDestTmp->inactive =0; dest_infos.aliveCount++; UnlockDest(lpDestTmp); return ((int)(lpDestTmp->version));}int AddDest(int sd, struct sockaddr *dest, int curJys, int reqType, short *params, short paramsCount,BYTE version){ int i; LPDEST_INFO lpDestNext, lpDestTmp; LPDEST_INFO *lpDestNew =NULL; lpDestNext =dest_infos.lpDestHead; lpDestNew =&dest_infos.lpDestHead; if(paramsCount<0||paramsCount>MAX_PARA_COUNT) { ErrMsg(ghWndMain,"来自AddDest:错误的参数个数!"); return -1; } if(dest_info_count>MAX_USER_COUNT) { ErrMsg(ghWndMain,"来自AddDest:无空闲的地址表项!"); return -1; } lpDestTmp=&dest_info[dest_info_count]; if(dest_info_count>0) dest_info[dest_info_count-1].lpNext=&dest_info[dest_info_count]; dest_info_count++; lpDestTmp->isUsing=FALSE; if(!LockDest(lpDestTmp,10)) { ErrMsg(ghWndMain,"来自AddDest:加锁失败!"); return -1; } memset(lpDestTmp, 0, sizeof(DEST_INFO)); lpDestTmp->sd =sd; lpDestTmp->curJys =curJys; lpDestTmp->reqType =reqType; lpDestTmp->isDead =FALSE; lpDestTmp->inactive =0; if(lpDestTmp->params==NULL) lpDestTmp->params =GlobalLock(GlobalAlloc(GHND,(MAX_PARA_COUNT+1)*sizeof(short))); if(lpDestTmp->params ==NULL) { ErrMsg(ghWndMain,"来自AddDest:不能分配参数内存区!"); UnlockDest(lpDestTmp); return -1; } if(reqType ==REQ_GRA10) { lpDestTmp->graNo =params[0]; lpDestTmp->graEnd =params[1]; } else memcpy(lpDestTmp->params, params, sizeof(short)*paramsCount); if(lpDestTmp->lpCjss ==NULL) lpDestTmp->lpCjss =GlobalLock(GlobalAlloc(GHND,MAX_PARA_COUNT*sizeof(int))); if(lpDestTmp->lpCjss ==NULL) { GlobalUnlock(GlobalHandle(lpDestTmp->params)); GlobalFree(GlobalHandle(lpDestTmp->params)); lpDestTmp->params ==NULL; ErrMsg(ghWndMain,"来自AddDest:不能分配lpCjss参数内存区!"); UnlockDest(lpDestTmp); return -1; } if(reqType ==REQ_HQ10||reqType ==REQ_HQ01) { for(i =0; i<params[0]; i++) { lpDestTmp->lpCjss[i] =HqData[curJys].lpRefData[params[i+1]].cjss; } } else if(reqType ==REQ_GRA10) { if(IsZsRec(curJys, params[0])) { lpDestTmp->lpZsGraph =GlobalLock(GlobalAlloc(GHND,sizeof(ZS_GRAPH))); if(lpDestTmp->lpZsGraph ==NULL) { ErrMsg(ghWndMain,"来自AddDest:不能分配ZS_RGAPH内存区!"); UnlockDest(lpDestTmp); return -1; } memcpy(lpDestTmp->lpZsGraph->Maxmin, MaxMinData[curJys], sizeof(MAXMIN_DATA)*2); } else { lpDestTmp->lpGraph =GlobalLock(GlobalAlloc(GHND,sizeof(GRAPH))); if(lpDestTmp->lpGraph ==NULL) { ErrMsg(ghWndMain,"来自AddDest:不能分配GRAPH内存区!"); UnlockDest(lpDestTmp); return -1; } memcpy(&lpDestTmp->lpGraph->mmp, &MmpData[curJys].lpMmp[params[0]], sizeof(MMP)); } lpDestTmp->cjss =HqData[curJys].lpRefData[params[0]].cjss; } else { ErrMsg(ghWndMain, "来自AddDest:错误的请求!"); UnlockDest(lpDestTmp); return -1; } FillDpMain(&lpDestTmp->DpData[0]); if(version!=0) lpDestTmp->version =version; lpDestTmp->paramsCount =paramsCount; *lpDestNew =lpDestTmp; dest_infos.destCount ++; UnlockDest(lpDestTmp); return 0;}int ChangeDest(int sd, LPDEST_INFO lpDestTmp, struct sockaddr *dest, int curJys, int reqType, short *params, short paramsCount,BYTE version){ int i; if(lpDestTmp ==NULL) return 0; if(!LockDest(lpDestTmp,10)) return -1; lpDestTmp->isDead =FALSE; lpDestTmp->sd =sd; lpDestTmp->curJys =curJys; lpDestTmp->reqType =reqType; lpDestTmp->inactive =0; if(version!=0) lpDestTmp->version=version; if(paramsCount<0||paramsCount>MAX_PARA_COUNT||lpDestTmp->params ==NULL) { ErrMsg(ghWndMain,"来自ChangeDest:错误的参数个数!"); UnlockDest(lpDestTmp); return -1; } if(reqType ==REQ_HQ10||reqType ==REQ_HQ01) { if(params[0]+params[1]+1>HqData[curJys].recCount) { params[0]=HqData[curJys].recCount-params[1]; } for(i =0; i<params[0]; i++) { if(params[i+1]<0||params[i+1]>=HqData[curJys].recCount) { ErrMsg(NULL,"来自ChangeDest:错误的列表参数!"); UnlockDest(lpDestTmp); return -1; } lpDestTmp->lpCjss[i] =HqData[curJys].lpRefData[params[i+1]].cjss; } } else if(reqType ==REQ_GRA10) { if(IsZsRec(curJys, params[0])) { if(lpDestTmp->lpZsGraph ==NULL) { lpDestTmp->lpZsGraph =GlobalLock(GlobalAlloc(GHND,sizeof(ZS_GRAPH))); if(lpDestTmp->lpZsGraph ==NULL) { ErrMsg(ghWndMain, "来自ChangeDest:分配ZS_GARPH内存失败!"); UnlockDest(lpDestTmp); return -1; } } memcpy(lpDestTmp->lpZsGraph->Maxmin, MaxMinData[curJys], sizeof(MAXMIN_DATA)*2); } else { if(lpDestTmp->lpGraph ==NULL) { lpDestTmp->lpGraph =GlobalLock(GlobalAlloc(GHND,sizeof(GRAPH))); if(lpDestTmp->lpGraph ==NULL) { ErrMsg(ghWndMain, "来自ChangeDest:分配GARPH内存失败!"); UnlockDest(lpDestTmp); return -1; } } memcpy(&lpDestTmp->lpGraph->mmp, &MmpData[curJys].lpMmp[params[0]], sizeof(MMP)); } lpDestTmp->cjss =HqData[curJys].lpRefData[params[0]].cjss; } else { UnlockDest(lpDestTmp); ErrMsg(ghWndMain, "来自ChangeDest:错误的请求!"); return -1; } if(reqType ==REQ_GRA10) { lpDestTmp->graNo =params[0]; lpDestTmp->graEnd =params[1]; } else memcpy(lpDestTmp->params, params, sizeof(short)*paramsCount); FillDpMain(&lpDestTmp->DpData[0]); lpDestTmp->paramsCount =paramsCount; UnlockDest(lpDestTmp); return 0;}int CheckDest(int sd, struct sockaddr *dest, int curJys, int reqType, short *params, short paramsCount,BYTE version){ int ret; LPDEST_INFO lpDestTmp; lpDestTmp=GetDestBySd(sd); if(lpDestTmp ==NULL) { lpDestTmp =GetFirstDeadDest(); if(!lpDestTmp) goto add_dest; } ret=ChangeDest(sd, lpDestTmp, dest, curJys, reqType, params, paramsCount,version); return ret;add_dest: ret=AddDest(sd, dest, curJys, reqType, params, paramsCount,version); return ret;}int HqDestSetDeadByPrt(LPDEST_INFO lpDestTmp){ if(lpDestTmp->isDead ==FALSE) { if(--dest_infos.aliveCount <0) dest_infos.aliveCount =0; } shutdown(lpDestTmp->sd,2); closesocket(lpDestTmp->sd); lpDestTmp->sd=0; strcpy(lpDestTmp->userID,""); lpDestTmp->recNum=0; lpDestTmp->isDead =TRUE; lpDestTmp->isUsing=FALSE; lpDestTmp->inactive =0; return 0;}int HqDestSetDeadBySd(int sd){ LPDEST_INFO lpDestTmp;// int i; lpDestTmp=GetDestBySd(sd); if(lpDestTmp!=NULL) { //if(!LockDest(lpDestTmp)) //{ // ErrMsg(ghWndMain,"来自HqDestSetDeadBySd:加锁失败!"); // return -1; //} if(lpDestTmp->isDead ==FALSE) { if(--dest_infos.aliveCount <0) dest_infos.aliveCount =0; } shutdown(lpDestTmp->sd,2); //for(i=0;i<10;i++) //{ // if(closesocket(lpDestTmp->sd)==0) // break; // Sleep(100); //} closesocket(lpDestTmp->sd); lpDestTmp->sd=0; strcpy(lpDestTmp->userID,""); lpDestTmp->recNum=0; lpDestTmp->isDead =TRUE; lpDestTmp->isUsing=FALSE; lpDestTmp->inactive =0; //UnlockDest(lpDestTmp); } return 0;}int HqDestStopBySd(sd){ LPDEST_INFO lpDestTmp;// int i; lpDestTmp=GetDestBySd(sd); if(lpDestTmp!=NULL) { if(lpDestTmp->isDead ==FALSE) { if(--dest_infos.aliveCount <0) dest_infos.aliveCount =0; } shutdown(lpDestTmp->sd,2); //for(i=0;i<10;i++) //{ // if(closesocket(lpDestTmp->sd)==0) // break; // Sleep(100); //} closesocket(lpDestTmp->sd); lpDestTmp->isDead =TRUE; lpDestTmp->sd=0; strcpy(lpDestTmp->userID,""); lpDestTmp->recNum=0; lpDestTmp->isUsing=FALSE; lpDestTmp->inactive =0; } return 0;}LPDEST_INFO GetDestByUser(LPSTR userID){ int i; LPDEST_INFO lpDestTmp; lpDestTmp =dest_infos.lpDestHead; for(i =0; i<dest_infos.destCount; i++) { if(!lpDestTmp) break; if(strcmp(lpDestTmp->userID, userID) ==0) { return lpDestTmp; } lpDestTmp =lpDestTmp->lpNext; } return NULL;}LPDEST_INFO GetDestBySd(int sd){ int i; LPDEST_INFO lpDestTmp; for(i =0; i<dest_info_count; i++) { lpDestTmp=&dest_info[i]; if(!lpDestTmp) break; if(lpDestTmp->sd==sd) { return lpDestTmp; } } return NULL;}LPDEST_INFO GetAnotherUserById(char *Id,int sd){ int i; LPDEST_INFO lpDestTmp; for(i =0; i<dest_info_count; i++) { lpDestTmp=&dest_info[i]; if(!lpDestTmp) break; if(lpDestTmp->sd!=sd&&strncmp(Id,lpDestTmp->userID,6)==0) { return lpDestTmp; } } return NULL;}LPDEST_INFO HqDestSendClose(){ int i; LPDEST_INFO lpDestTmp; for(i =0; i<dest_infos.destCount; i++) { lpDestTmp=&dest_info[i]; if(lpDestTmp) { if(!lpDestTmp->isDead) SendMsg(lpDestTmp->sd, "服务器开始关闭!"); } } for(i =0; i<dest_infos.destCount; i++) { lpDestTmp=&dest_info[i]; if(lpDestTmp) HqDestSetDeadByPrt(lpDestTmp); } return lpDestTmp;}LPDEST_INFO GetFirstDeadDest(void){ int i; LPDEST_INFO lpDestTmp; for(i =0; i<dest_info_count; i++) { lpDestTmp=&dest_info[i]; if(!lpDestTmp) break; if(lpDestTmp->isDead ==TRUE && lpDestTmp-> isUsing==FALSE) { lpDestTmp->recNum =0; strcpy(lpDestTmp->userID,""); return lpDestTmp; } } return NULL;}void DelDeads(void){ LPDEST_INFO lpDestPrev, lpDestNext, lpDestTmp; int i, count =0; if(dest_infos.aliveCount ==dest_infos.destCount) return; lpDestNext =lpDestTmp =dest_infos.lpDestHead; if(lpDestTmp ==NULL) return; for(i =0; i<dest_infos.destCount; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -