?? telnet.cpp
字號:
// telnet.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "telnet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DllExport __declspec(dllexport)
/*********************************************************
// the name of this protocol, you must change this to
// your protocol's name
*********************************************************/
static char m_strProtocolName[32]="Telnet(WinGate)";
/////////////////////////////////////////////////////////////////////////////
// CTelnetApp
BEGIN_MESSAGE_MAP(CTelnetApp, CWinApp)
//{{AFX_MSG_MAP(CTelnetApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTelnetApp construction
CTelnetApp::CTelnetApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CTelnetApp object
CTelnetApp theApp;
BOOL CTelnetApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// Read the config
CFile file;
if(file.Open("protocol\\telnet.cfg", CFile::modeRead|CFile::typeBinary))
{
CArchive arc(&file, CArchive::load);
TRY
{
arc>>m_dlgOptions.m_nConnectTime;
arc>>m_dlgOptions.m_nVerifyTime;
}
CATCH_ALL(e)
{
AfxMessageBox("Config file error!");
}
END_CATCH_ALL
arc.Close();
file.Close();
}
return CWinApp::InitInstance();
}
int CTelnetApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
for(int i=0; i<m_SocketArray.GetSize(); i++)
{
SOCKETITEM *pSocket=(SOCKETITEM *)m_SocketArray[i];
delete pSocket;
}
m_SocketArray.RemoveAll();
// Save the config
CFile file;
if(file.Open("protocol\\telnet.cfg", CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
{
CArchive arc(&file, CArchive::store);
arc<<m_dlgOptions.m_nConnectTime;
arc<<m_dlgOptions.m_nVerifyTime;
arc.Close();
file.Close();
}
return CWinApp::ExitInstance();
}
LPCTSTR CTelnetApp::GetProtocolName(void)
{
return m_strProtocolName;
}
void CTelnetApp::SetProperties(void)
{
m_dlgOptions.DoModal();
}
BOOL CTelnetApp::AddSocket(SOCKET hSocket)
{
SOCKETITEM *pNewSocket=new SOCKETITEM;
pNewSocket->hSocket = hSocket;
pNewSocket->nBytesReceived = 0;
pNewSocket->nBytesToSend = 0;
m_SocketArray.Add(pNewSocket);
return TRUE;
}
BOOL CTelnetApp::DelSocket(SOCKET hSocket)
{
for(int i=0; i<m_SocketArray.GetSize(); i++)
{
SOCKETITEM *pSocket=(SOCKETITEM *)m_SocketArray[i];
if(pSocket->hSocket == hSocket)
{
delete pSocket;
m_SocketArray.RemoveAt(i);
break;
}
}
return TRUE;
}
SOCKETITEM *CTelnetApp::FindSocket(SOCKET hSocket)
{
for(int i=0; i<m_SocketArray.GetSize(); i++)
{
SOCKETITEM *pSocket=(SOCKETITEM *)m_SocketArray[i];
if(pSocket->hSocket == hSocket)return pSocket;
}
return NULL;
}
// DLL call handlers
DllExport LPCTSTR EXPORT GetProtocolName(void)
{
return theApp.GetProtocolName();
}
DllExport void EXPORT SetProperties(void)
{
theApp.SetProperties();
}
DllExport void EXPORT GetTimeOutTime(UINT &nConnectTime, UINT &nVerifyTime)
{
theApp.GetTimeOutTime(nConnectTime, nVerifyTime);
}
DllExport BOOL EXPORT AddSocket(SOCKET hSocket)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return theApp.AddSocket(hSocket);
}
DllExport BOOL EXPORT DelSocket(SOCKET hSocket)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return theApp.DelSocket(hSocket);
}
DllExport void EXPORT OnConnect(SOCKET hSocket)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theApp.OnConnect(hSocket);
}
DllExport int EXPORT OnSend(SOCKET hSocket)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return theApp.OnSend(hSocket);
}
DllExport int EXPORT OnReceive(SOCKET hSocket)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return theApp.OnReceive(hSocket);
}
DllExport void EXPORT OnClose(SOCKET hSocket)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theApp.OnClose(hSocket);
}
/*********************************************************
// The code below can be modified to implement your protocol
*********************************************************/
void CTelnetApp::GetTimeOutTime(UINT &nConnectTime, UINT &nVerifyTime)
{
nConnectTime = m_dlgOptions.m_nConnectTime;
nVerifyTime = m_dlgOptions.m_nVerifyTime;
}
void CTelnetApp::OnConnect(SOCKET hSocket)
{
SOCKETITEM *pSocket=FindSocket(hSocket);
if(pSocket != NULL)
{
// Do something
}
}
int CTelnetApp::OnSend(SOCKET hSocket)
{
SOCKETITEM *pSocket=FindSocket(hSocket);
if(pSocket != NULL)
{
if(pSocket->nBytesToSend > 0)
{
int len=send(pSocket->hSocket, (const char *)pSocket->SendBuf.GetData(), pSocket->nBytesToSend, 0);
if(len != SOCKET_ERROR)
{
pSocket->SendBuf.RemoveAt(0, len);
pSocket->nBytesToSend -= len;
if(pSocket->nBytesToSend == 0)return 0;
else return 1;
}
else if(WSAGetLastError() == WSAEWOULDBLOCK)return 0;
}
else
return 0;
}
return -1;
}
int CTelnetApp::OnReceive(SOCKET hSocket)
{
SOCKETITEM *pSocket=FindSocket(hSocket);
if(pSocket != NULL)
{
CByteArray buf;
buf.SetSize(64000);
int len=recv(pSocket->hSocket, (char *)buf.GetData(), 60000, 0);
if(len != SOCKET_ERROR)
{
pSocket->nBytesReceived += len;
buf.SetSize(len);
pSocket->ReceiveBuf.Append(buf);
pSocket->ReceiveBuf.Add(0);
// Analyse the incoming data
if(pSocket->nBytesReceived >= 17)
{
char *pBuf=(char *)pSocket->ReceiveBuf.GetData();
char *p=strstr(pBuf, "WinGate>");
if(p != NULL)return 6;
else return 9;
}
pSocket->ReceiveBuf.SetSize(pSocket->nBytesReceived);
return 0;
}
else if(WSAGetLastError() == WSAEWOULDBLOCK)return 0;
}
return 8;
}
void CTelnetApp::OnClose(SOCKET hSocket)
{
SOCKETITEM *pSocket=FindSocket(hSocket);
if(pSocket != NULL)
{
// Do something here
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -