📄 proxydlg.cpp
字号:
// ProxyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Proxy.h"
#include "ProxyDlg.h"
#include <direct.h>
#include "ExportFuncs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CProxyDlg *pDlg;
BOOL IsGprsOnline;
BOOL IsSmsOnline;
int RetryTimes;
void AttachCbFunc(DWORD);
void startup_cb_func(DWORD ret_code);
void rev_sm_cb_func(CStringArray& sm_array);
/////////////////////////////////////////////////////////////////////////////
// CProxyDlg dialog
CProxyDlg::CProxyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CProxyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CProxyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hTrayIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_TrayIcon.m_bIsWindowNormal = true;
}
void CProxyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProxyDlg)
DDX_Control(pDX, IDC_EDIT_STATUS, m_Status);
DDX_Control(pDX, IDC_EDIT_COUNT, m_Count);
DDX_Control(pDX, IDC_LIST_VEHICLE, m_List);
DDX_Control(pDX, IDOK, m_Connect);
DDX_Control(pDX, IDC_BUTTON_QUERY, m_Query);
DDX_Control(pDX, IDC_BUTTON_ENABLE, m_Enable);
DDX_Control(pDX, IDC_BUTTON_DISABLE, m_Disable);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProxyDlg, CDialog)
//{{AFX_MSG_MAP(CProxyDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_CLOSE()
ON_WM_CREATE()
ON_COMMAND(IDD_HIDE, OnHide)
ON_COMMAND(IDD_SHOW, OnShow)
ON_UPDATE_COMMAND_UI(IDD_SHOW, OnUpdateShow)
ON_UPDATE_COMMAND_UI(IDD_HIDE, OnUpdateHide)
ON_COMMAND(IDD_EXIT, OnExit)
ON_BN_CLICKED(IDC_BUTTON_DISABLE, OnButtonDisable)
ON_BN_CLICKED(IDC_BUTTON_ENABLE, OnButtonEnable)
ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
ON_MESSAGE(UM_SERVER,Tcp_Accept)
ON_MESSAGE(UM_RECV,Tcp_Read)
ON_MESSAGE(WM_TRAYNOTIFY,OnTrayNotification)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProxyDlg message handlers
BOOL CProxyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
pDlg=this;
if (!Variable_Init())
return FALSE;
SetTimer(1,60000,NULL);
m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_UNDERLINEHOT|LVS_REPORT);
m_List.InsertColumn(0,"所在分控ID",LVCFMT_LEFT,80,2);
m_List.InsertColumn(1,"车机类型",LVCFMT_LEFT,80,2);
m_List.InsertColumn(2,"车机个数",LVCFMT_LEFT,80,2);
/* CMenu menu;
if(!menu.LoadMenu(m_TrayIcon.m_NotifyIconData.uID))
return true;
CMenu* pSubMenu = menu.GetSubMenu(0);
if(!pSubMenu)
return true;*/
::PostMessage(m_TrayIcon.m_NotifyIconData.hWnd,WM_COMMAND,IDD_HIDE,0);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CProxyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CProxyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CProxyDlg::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case 1:
SendStatusQuery();
break;
case 2:
ShowWindow(SW_HIDE);
KillTimer(2);
break;
}
CDialog::OnTimer(nIDEvent);
}
BOOL CProxyDlg::ReadConfig()
{
char strTemp[81];
char Dir[150];
DWORD LenDir=sizeof(Dir);
GetCurrentDirectory(LenDir,Dir);
strcat(Dir,"\\Proxy.cfg");
LenDir=sizeof(Dir);
TcpLst.TcpTotal=0;
GetPrivateProfileString("Server","IP","",strTemp,sizeof(strTemp),Dir);
if((strlen(strTemp)>20)||(strlen(strTemp)==0))
{
AfxMessageBox("配置文件 <Proxy.Cfg> 内容错误!");
return FALSE;
}
strcpy(TcpLst.TcpMember[0].ServerIpAddr,strTemp);
GetPrivateProfileString("Server","Port","",strTemp,sizeof(strTemp),Dir);
if((strlen(strTemp)>20)||(strlen(strTemp)==0))
{
AfxMessageBox("配置文件 <Proxy.Cfg> 内容错误!");
return FALSE;
}
TcpLst.TcpMember[0].ServerPort=atoi(strTemp);
TcpLst.TcpMember[0].ServerSocketfd=-1;
TcpLst.TcpTotal++;
bWriteLog=TRUE;
return TRUE;
}
BOOL CProxyDlg::Variable_Init()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
return FALSE;
}
if (!ReadConfig())
return FALSE;
if (bWriteLog)
{
//////// 初始化日志文件和目录 //////////////
DWORD Len;
Len=sizeof(LogDir);
GetCurrentDirectory(Len,LogDir);
strcat(LogDir,"\\log");
_mkdir(LogDir);
if(!Daylog_Init())
return FALSE;
}
if (bWriteLog)
{
fprintf(daylogfd,"\n------------------------------------------\n");
fprintf(daylogfd,"----- System Start -----\n");
fprintf(daylogfd,"------------------------------------------\n");
fflush(daylogfd);
}
// if (!Tcp_Init())
// return FALSE;
ConvertIp();
return TRUE;
}
BOOL CProxyDlg::Daylog_Init()
{
if (bWriteLog)
{
struct tm *tmp;
time_t sectime;
char tempfilestr[10],LogFileName[210];
FILE *fd;
sectime=time(NULL);
tmp=localtime(§ime);
sprintf(tempfilestr,"%d.log",(tmp->tm_yday*100+tmp->tm_hour));
strcpy(LogFileName,LogDir);
strcat(LogFileName,"\\");
strcat(LogFileName,tempfilestr);
LastLogHour=tmp->tm_hour;
if((fd=fopen(LogFileName,"a+"))==NULL)
{
perror("fopen");
fflush(stdout);
return FALSE ;
}else
{
daylogfd=fd;
return TRUE;
}
}
return TRUE;
}
void CProxyDlg::Daylog_Check()
{
if (bWriteLog)
{
struct tm *tmp;
time_t sectime;
char tempfilestr[10],LogFileName[210];
FILE *fd;
sectime=time(NULL);
tmp=localtime(§ime);
if(LastLogHour==tmp->tm_hour)
return;
LastLogHour=tmp->tm_hour;
/////////////////////////////////////////////////
// construct a new log file
/////////////////////////////////////////////////
sprintf(tempfilestr,"%d.log",(tmp->tm_yday*100+tmp->tm_hour));
strcpy(LogFileName,LogDir);
strcat(LogFileName,"\\");
strcat(LogFileName,tempfilestr);
if((fd=fopen(LogFileName,"a+"))==NULL)
{
return ;
}else
{
fclose(daylogfd);
daylogfd=fd;
}
/////////////////////////////////////////////////
// delete file 48 hours ago
/////////////////////////////////////////////////
if(tmp->tm_yday<=1)
{
sprintf(tempfilestr,"%d.log",((365+tmp->tm_yday-2)*100+tmp->tm_hour));
}else
{
sprintf(tempfilestr,"%d.log",((tmp->tm_yday-2)*100+tmp->tm_hour));
}
strcpy(LogFileName,LogDir);
strcat(LogFileName,"\\");
strcat(LogFileName,tempfilestr);
if(fd=fopen(LogFileName,"r"))
{
fclose(fd);
remove(LogFileName);
}
}
}
void CProxyDlg::Daylog_Close()
{
if (bWriteLog)
{
if(daylogfd!=NULL)
fclose(daylogfd);
}
}
void CProxyDlg::Variable_Release()
{
Tcp_Close();
if (bWriteLog)
{
fprintf(daylogfd,"\n------------------------------------------\n");
fprintf(daylogfd,"----- System Quit -----\n");
fprintf(daylogfd,"------------------------------------------\n");
fflush(daylogfd);
}
Daylog_Close();
}
void CProxyDlg::OnClose()
{
//Variable_Release();
CDialog::OnClose();
}
BOOL CProxyDlg::Tcp_Init()
{
/* int i;
SOCKADDR_IN server[MAX_SERVER_LIST];
for(i=0;i<ServerLst.ServerTotal;i++)
{
server[i].sin_port=htons(ServerLst.TcpServerMember[i].ListenPort);
ServerLst.TcpServerMember[i].Socketfd = socket(AF_INET,SOCK_STREAM,0);//建立套接口
server[i].sin_family = AF_INET; //协议
server[i].sin_addr.s_addr = INADDR_ANY; //---本地主机地址
if(bind(ServerLst.TcpServerMember[i].Socketfd, (SOCKADDR *)&server[i], sizeof(server[i]))==SOCKET_ERROR)
{
AfxMessageBox("Bind Error!!!");
return FALSE;
}
listen(ServerLst.TcpServerMember[i].Socketfd, 64);//建立连接:侦听
if(WSAAsyncSelect(ServerLst.TcpServerMember[i].Socketfd, m_hWnd, UM_SERVER, FD_ACCEPT)==SOCKET_ERROR)
closesocket(ServerLst.TcpServerMember[i].Socketfd);
}
TcpLst.TcpTotal=0;*/
return TRUE;
}
void CProxyDlg::Tcp_Close()
{
int i;
for(i=0;i<TcpLst.TcpTotal;i++)
{
if(TcpLst.TcpMember[i].ServerSocketfd!=-1)
{
closesocket(TcpLst.TcpMember[i].ServerSocketfd);
}
}
return ;
}
void CProxyDlg::Tcp_Read(UINT wParam, LONG lParam)
{
int buflen;
int ret=100;
char buf[2*MAX_LEN];
int i;
for(i=0;i<MAX_TCP_LIST;i++)
{
if (TcpLst.TcpMember[i].ServerSocketfd==(int)wParam)
{
break;
}
}
if(i==MAX_TCP_LIST)
return;
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
{
if((buflen=recv(TcpLst.TcpMember[i].ServerSocketfd,
buf, MAX_LEN, 0))==SOCKET_ERROR)
{
WSAAsyncSelect(TcpLst.TcpMember[i].ServerSocketfd,m_hWnd,0,0);
closesocket(TcpLst.TcpMember[i].ServerSocketfd);
TcpLst.TcpMember[i].ServerSocketfd=-1;
m_Connect.EnableWindow(TRUE);
}else
{
DoEvent(buf,buflen);
}
}
break;
case FD_CLOSE:
sprintf(DataStr,"Description : peer %s is down(%d)",
TcpLst.TcpMember[i].ServerIpAddr,i);
SaveInformation(DataStr);
WSAAsyncSelect(TcpLst.TcpMember[i].ServerSocketfd,m_hWnd,0,0);
closesocket(TcpLst.TcpMember[i].ServerSocketfd);
TcpLst.TcpMember[i].ServerSocketfd=-1;
m_Connect.EnableWindow(TRUE);
break;
default:
if(WSAGETSELECTERROR(lParam)!=0)
{
WSAAsyncSelect(TcpLst.TcpMember[i].ServerSocketfd,m_hWnd,0,0);
closesocket(TcpLst.TcpMember[i].ServerSocketfd);
TcpLst.TcpMember[i].ServerSocketfd=-1;
m_Connect.EnableWindow(TRUE);
}
break;
}
return;
}
void CProxyDlg::SaveInformation(char strData[])
{
if (bWriteLog)
{
GetCurrentTimeStr();
if(daylogfd!=NULL)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -