// MyAsyncSocket1.cpp : ʵÏÖÎļþ
|
//
|
|
#include "stdafx.h"
|
#include "SocketFunc.h"
|
//#include "TCP_Server.h"
|
double GetTimeMs()
|
{
|
LARGE_INTEGER perfreq;
|
LARGE_INTEGER percounter1; //,percounter2;
|
double time1; //,time2,timeinter1;
|
QueryPerformanceFrequency(&perfreq);
|
QueryPerformanceCounter(&percounter1);
|
// percounter1.QuadPart=rdtsc();
|
/* _asm {
|
RDTSC;
|
mov percounter1.LowPart,eax;
|
mov percounter1.HighPart,edx;
|
}
|
*/
|
time1=(double)percounter1.QuadPart/perfreq.QuadPart ;
|
// time2=(double)percounter2.QuadPart/perfreq.QuadPart ;
|
// timeinter1=time2-time1;
|
return (time1*1000);
|
};
|
// CMyServerSocket
|
CMyServerSocket::CMyServerSocket()
|
{
|
Idx=9999;
|
SN=0;
|
}
|
CMyServerSocket::~CMyServerSocket()
|
{
|
|
}
|
void CMyServerSocket::OnAccept(int nErrorCode)
|
{
|
CString s1;
|
int j;
|
// s1.Format(_T("Server %d OnAccept Code %d\r\n"),Idx,nErrorCode);
|
// SysLog(s1);
|
CMyDataSocket * MyAsyncSocket2;
|
int k=MySockets.size();
|
int bFound=0,pos=0;;
|
SN++;
|
MyAsyncSocket2=new CMyDataSocket;
|
MyAsyncSocket2->Idx=SN;
|
MyAsyncSocket2->ConnectionStat=MyAsyncSocket2->statConnected;
|
std::shared_ptr<CMyDataSocket> pMySocket(MyAsyncSocket2);
|
MySockets.push_back(pMySocket);
|
|
j=Accept(*MyAsyncSocket2);
|
MyAsyncSocket2->m_cTime=CTime::GetCurrentTime();
|
CString TimeStr=MyAsyncSocket2->m_cTime.Format(_T("%Y-%m-%d %H:%M:%S"));
|
|
CString sip;
|
UINT nPort;
|
MyAsyncSocket2->GetPeerName(sip,nPort);
|
MyAsyncSocket2->sTargetIp=sip;
|
MyAsyncSocket2->nTargetPort=nPort;
|
MyAsyncSocket2->TargetVer.nMainVer=0;
|
MyAsyncSocket2->TargetVer.nSubVer=0;
|
MyAsyncSocket2->Logined=0;
|
|
s1.Format(_T("%d Code %d Accept = %d IP %s : %u %s\r\n"),Idx,nErrorCode,j,sip,nPort,TimeStr);
|
SysLog(s1);
|
CAsyncSocket::OnAccept(nErrorCode);
|
}
|
|
void CMyServerSocket::OnClose(int nErrorCode)
|
{
|
CString s1;
|
s1.Format(_T("Server %d OnClose Code %d \r\n"),Idx,nErrorCode);
|
SysLog(s1);
|
}
|
|
void CMyServerSocket::OnConnect(int nErrorCode)
|
{
|
CString s1;
|
s1.Format(_T("Server %d OnConnect Code %d \r\n"),Idx,nErrorCode);
|
SysLog(s1);
|
}
|
|
int CMyServerSocket::UpdataStat()
|
{
|
CTime time1=CTime::GetCurrentTime();
|
int k=MySockets.size();
|
for (int i=k-1;i>=0;i--)
|
{
|
std::shared_ptr<CMyDataSocket> thissocket= MySockets.at(i);
|
if (thissocket->ConnectionStat == thissocket->statClosed)
|
{
|
CTimeSpan time2;
|
time2=time1-thissocket->m_kTime;
|
if (time2>10)
|
{
|
MySockets.erase(MySockets.begin()+i);
|
}
|
}
|
}
|
return true;
|
}
|
// CMyAsyncSocket1
|
CMyDataSocket::CMyDataSocket()
|
{
|
SocketType=0;
|
Idx=0; SN=0; ConnectionStat=statIdel;
|
nPingInterval=0;nIntervalCount=0;
|
bPinging=0; PingSeq=0; latancy=0;
|
|
TotalSendBytes=TotalRecvBytes=0;
|
SendBytes=LastSendBytes=0;
|
RecvBytes=LastRecvBytes=0;
|
SendBuf.Alloc(SendBufSize);
|
RecvBuf.Alloc(RecvBufSize);
|
bToSendFile=0; bSendingFile=0; bRecvingFile=0;
|
DummyToSend=0;
|
|
}
|
|
CMyDataSocket::~CMyDataSocket()
|
{
|
Close();
|
}
|
|
// CMyAsyncSocket1 ³ÉÔ±º¯Êý
|
stVerInfo MyVerInfo=
|
{
|
1,
|
1,
|
};
|
void CMyDataSocket::OnAccept(int nErrorCode)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà
|
CString s1;
|
s1.Format(_T("%d OnAccept Code %d\r\n"),Idx,nErrorCode);
|
SysLog(s1);
|
CAsyncSocket::OnAccept(nErrorCode);
|
}
|
|
void CMyDataSocket::OnClose(int nErrorCode)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà
|
CString s1;
|
s1.Format(_T("%d OnClose Code %d\r\n"),Idx,nErrorCode);
|
SysLog(s1);
|
ConnectionStat=statBeToClose;
|
CAsyncSocket::OnClose(nErrorCode);
|
ConnectionStat=statClosed;
|
m_kTime=CTime::GetCurrentTime();
|
/*
|
std::shared_ptr <CMyDataSocket1> p1;
|
int k=MySockets.size();
|
for (int i=0;i<k;i++)
|
{
|
if (MySockets.at(i)->Idx==Idx)
|
{
|
// s1.Format(_T("%d ToDelete Self\r\n"),Idx);
|
// SysLog(s1);
|
p1=MySockets.at(i);
|
MySockets.erase(MySockets.begin()+i);
|
break;
|
}
|
}
|
*/
|
}
|
|
int CMyDataSocket::UpdateSpeed(void)
|
{
|
|
if ( nPingInterval>0)
|
{
|
nIntervalCount++;
|
if ( nIntervalCount >= nPingInterval )
|
{
|
nIntervalCount=0;
|
if ( bPinging ) {latancy=999.9; bPinging=0; }
|
DoPing();
|
}
|
}
|
SendSpeed=LastSendBytes+SendBytes;
|
RecvSpeed=LastRecvBytes+RecvBytes;
|
LastSendBytes=SendBytes;
|
LastRecvBytes=RecvBytes;
|
SendBytes=0;
|
RecvBytes=0;
|
return 0;
|
}
|
|
void CMyDataSocket::OnConnect(int nErrorCode)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà
|
SocketType=1;
|
ConnectionStat=statConnected;
|
CString s1;
|
s1.Format(_T("%d OnConnect Code %d\r\n"),Idx,nErrorCode);
|
SysLog(s1);
|
CAsyncSocket::OnConnect(nErrorCode);
|
char sendbuf2[2048];
|
pPacket p2=(pPacket)sendbuf2;
|
int len1=0;
|
len1=MakeDataPacket(p2,cmdVerInfo,1,dataTypeVerInfo,sizeof(MyVerInfo),&MyVerInfo);
|
SendPacket(p2,len1);
|
|
len1=MakeCtrlPacket(p2,cmdGetVersion,1);
|
SendPacket(p2,len1);
|
|
len1=MakeCtrlPacket(p2,cmdLogin,1);
|
SendPacket(p2,len1);
|
|
}
|
|
void CMyDataSocket::OnOutOfBandData(int nErrorCode)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà
|
CString s1;
|
s1.Format(_T("%d OnOutOfBandData Code %d\r\n"),Idx,nErrorCode);
|
SysLog(s1);
|
CAsyncSocket::OnOutOfBandData(nErrorCode);
|
}
|
|
void CMyDataSocket::OnReceive(int nErrorCode)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà
|
|
CString s1;
|
CString s3;
|
CStringA s4;
|
int i=0,j,k;
|
k=0;
|
int MaxRecvLength=65536;
|
CString sRipaddr;
|
UINT rport;
|
do{
|
int roomleft=0;
|
void * precvbuf=RecvBuf.GetWriteBuffer(&roomleft);
|
int thisRecvLength=min(MaxRecvLength,roomleft);
|
j=this->ReceiveFrom(precvbuf,thisRecvLength,sRipaddr,rport);
|
if (j!=SOCKET_ERROR)
|
{RecvBuf.ReleaseWriteBuffer(j);RecvBytes+=j;TotalRecvBytes+=j;}
|
else {RecvBuf.ReleaseWriteBuffer(0);}
|
|
if (j!=SOCKET_ERROR)
|
{
|
do{
|
int RecvBufDataSize=RecvBuf.GetDataSize();
|
if (RecvBufDataSize<sizeof(tagPacket)){break;} //Ê£ÓàÊý¾ÝСÓÚÒ»¸ö°üÍ·´óС£¬Í˳ö
|
pPacket p1;
|
RecvBuf.CopyData(tempRecvbuf1,sizeof(tagPacket));
|
p1=(pPacket)tempRecvbuf1;
|
if (p1->sign==0x55aa) //ÕÒµ½±ê¼Ç
|
{
|
int nDataSize=p1->nDataSize;
|
int nPacketSize=sizeof(tagPacket)+nDataSize; //»ñÈ¡°ü³ß´ç
|
if (RecvBufDataSize<nPacketSize) break; //Ê£ÓàÊý¾ÝСÓÚµ±Ç°°ü´óС£¬Í˳ö
|
int nTailDataSize=RecvBuf.GetTailDataSize();
|
if (nTailDataSize<nPacketSize)
|
{
|
RecvBuf.DelData(sizeof(tagPacket));
|
RecvBuf.PopOut(tempRecvbuf1+sizeof(tagPacket),nDataSize);
|
ProcessPacket(p1);
|
}else
|
{
|
RecvBuf.DelData(sizeof(tagPacket));
|
RecvBuf.PopOut(tempRecvbuf1+sizeof(tagPacket),nDataSize);
|
ProcessPacket(p1);
|
}
|
|
//È¡µÃÁËÒ»¸öÍêÕûµÄÊý¾Ý°ü£¬
|
//s1.Format(_T("%d RecvPacket chn %d cmd %d Size %d DataSize %d\r\n"),Idx,p1->nChannel,p1->nCMD,nPacketSize,nDataSize);
|
//SysLog(s1);
|
}else
|
{
|
//RecvBuf.DelData(1);
|
s1.Format(_T("%d Êý¾ÝÖ¡¸ñʽ´íÎó \r\n"),Idx);
|
for (int i=0;i<sizeof(tagPacket);i++)
|
{
|
s1.AppendFormat(_T("%02X "),(unsigned char)tempRecvbuf1[i]);
|
}
|
for (int i=0;i<sizeof(tagPacket);i++)
|
{
|
if (tempRecvbuf1[i]>=32&&tempRecvbuf1[i]<128)
|
s1.AppendFormat(_T("%C"),(unsigned char)tempRecvbuf1[i]);
|
else s1.Append(_T("."));
|
}
|
s1.AppendFormat(_T("\r\n"));
|
SysLog(s1);
|
RecvBuf.DelData(RecvBufDataSize);
|
}
|
i++;
|
}while (i<10);
|
}else
|
{
|
//CAsyncSocket::OnReceive(nErrorCode);
|
k=GetLastError();
|
//FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,k,0,s2.GetBuffer(2048),2048,NULL);
|
//s2.ReleaseBuffer();
|
s1.Format(_T("%d OnReceive %d Code %d %s:%d result %d err %d\r\n"),Idx,i,nErrorCode,sRipaddr,rport,j,k);
|
SysLog(s1);
|
//break;
|
}
|
}while (0);
|
}
|
|
void CMyDataSocket::OnSend(int nErrorCode)
|
{
|
// TODO: ÔÚ´ËÌí¼ÓרÓôúÂëºÍ/»òµ÷ÓûùÀà
|
// char buf1[60000];
|
int j,k;
|
int MaxSendLength=65536;
|
CString s1;
|
// s1.Format(_T("%d OnSend Code %d CountToSent %d BtS %lld\r\n"),Idx,nErrorCode,CountToSent,BytesToSend);
|
// SysLog(s1);
|
CStringA s1A;
|
if (nErrorCode!=0) {return;}
|
int BytesToSend=SendBuf.GetDataSize();
|
if (nErrorCode==0)
|
{
|
while (BytesToSend>0)
|
{
|
// Sleep(1);
|
int sendlenth=min(MaxSendLength,BytesToSend);
|
int leftdatasize;
|
char * psendbuf=SendBuf.GetReadBuffer(&leftdatasize);
|
int thissendlength=min(sendlenth,leftdatasize);
|
j=this->Send(psendbuf,thissendlength,0);
|
if (j!=SOCKET_ERROR)
|
{
|
TotalSendBytes+=j;
|
SendBytes+=j;
|
BytesToSend-=j;
|
SendBuf.ReleaseReadBuffer(j);
|
int leftdata=SendBuf.GetDataSize();
|
// s1.Format(_T("%d SendBuf %d Left %d toSend %d Total %lld\r\n"),Idx,j,leftdata,BytesToSend,TotalSendBytes);
|
// SysLog(s1);
|
}
|
else
|
{
|
SendBuf.ReleaseReadBuffer(0);
|
k=GetLastError();
|
//CString s2;
|
//FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,k,0,s2.GetBuffer(2048),2048,NULL);
|
//s2.ReleaseBuffer();
|
//s1.Format(_T("Send Error %d %d\r\n"),j,k);
|
//SysLog(s1);
|
if (k==WSAEWOULDBLOCK) break;
|
break;
|
}
|
};
|
while (BytesToSend==0&&bSendingFile)
|
{
|
int leftroom=SendBuf.GetLeftRoomSize();
|
if (leftroom<=(sizeof(tagPacket)+nMaxFileBlockSize))
|
{
|
s1.Format(_T("%d LeftRoom %d < tagPacket Skip\r\n"),Idx,leftroom);
|
SysLog(s1);
|
break;
|
}
|
int ReadSize=min(leftroom-sizeof(tagPacket),nMaxFileBlockSize);
|
j=ContinueReadFile(tempSendbuf1,ReadSize);
|
// nFileSentSize+=j;
|
if (j>0)
|
{
|
k=this->SendTo(tempSendbuf1,j,0);
|
if (k!=SOCKET_ERROR)
|
{
|
TotalSendBytes+=k;
|
SendBytes+=k;
|
// s1.Format(_T("%d Send %d %lld\r\n"),Idx,k,TotalSendBytes);
|
// SysLog(s1);
|
}
|
if (k==SOCKET_ERROR)
|
{
|
SendBuf.PushIn(tempSendbuf1,j);
|
int datasize=SendBuf.GetDataSize();
|
// s1.Format(_T("%d Pushin %d data %d \r\n"),Idx,j,datasize);
|
// SysLog(s1);
|
int l=GetLastError();
|
if (l==WSAEWOULDBLOCK)
|
{
|
break;
|
}
|
break;
|
}
|
}
|
if (j<ReadSize)
|
{
|
bSendingFile=0;
|
m_filesend1.Close();
|
s1.Format(_T("%d Îļþ·¢ËÍÍê±Ï\r\n"),Idx);
|
SysLog(s1);
|
break;
|
}
|
}
|
if (DummyToSend>0)
|
{
|
if (BytesToSend==0)
|
{
|
while (DummyToSend>0)
|
{
|
int leftsize=SendBuf.GetLeftRoomSize();
|
if (leftsize<sizeof(tagPacket)+16) break;
|
int thissize=(int)min(leftsize-sizeof(tagPacket),min(DummyToSend,nMaxDummyBlockSize));
|
if (thissize<=0) break;
|
pPacket p2=(pPacket)tempSendbuf1;
|
int len2=MakeDataPacket(p2,cmdSendDummyData,0,dataFileData,thissize,NULL);
|
k=this->SendTo(tempSendbuf1,len2,0);
|
if (k!=SOCKET_ERROR)
|
{
|
TotalSendBytes+=k;
|
SendBytes+=k;
|
DummyToSend-=thissize;
|
}
|
if (k==SOCKET_ERROR)
|
{
|
SendBuf.PushIn(tempSendbuf1,len2);
|
DummyToSend-=thissize;
|
int l=GetLastError();
|
if (l==WSAEWOULDBLOCK) { break; }
|
break;
|
}
|
|
}
|
}else
|
{
|
while (DummyToSend>0)
|
{
|
int leftsize=SendBuf.GetLeftRoomSize();
|
if (leftsize<sizeof(tagPacket)+16) break;
|
int thissize=(int)min(leftsize-sizeof(tagPacket),min(DummyToSend,nMaxDummyBlockSize));
|
if (thissize<=0) break;
|
pPacket p2=(pPacket)tempSendbuf1;
|
int len2=MakeDataPacket(p2,cmdSendDummyData,0,dataFileData,thissize,NULL);
|
SendBuf.PushIn(tempSendbuf1,len2);
|
DummyToSend-=thissize;
|
}
|
}
|
}
|
}
|
CAsyncSocket::OnSend(nErrorCode);
|
}
|
|
int CMyDataSocket::TriggerSend()
|
{
|
return AsyncSelect();
|
}
|
int CMyDataSocket::DoPing()
|
{
|
if (ConnectionStat==statConnected)
|
{
|
PingSeq++;
|
bPinging=1;
|
pingTime=GetTimeMs();
|
char sendbuf2[2048];
|
pPacket p2=(pPacket)sendbuf2;
|
int len1=MakeCtrlPacket(p2,cmdPing,PingSeq);
|
SendPacket(p2,len1);
|
}
|
return 0;
|
}
|
int CMyDataSocket::SendPacket(pPacket p2, int length)
|
{
|
SendBuf.PushIn(p2,length);
|
TriggerSend();
|
return length;
|
}
|
|
int CMyDataSocket::MakeCtrlPacket(pPacket p2,int Type, int nParam1)
|
{
|
p2->sign=0x55AA;
|
p2->nChannel=0;
|
p2->nCMD=Type;
|
p2->nDataType=dataNone;
|
p2->nDataSize=0;
|
p2->Param1=nParam1;
|
|
return sizeof(tagPacket)+p2->nDataSize;
|
}
|
int CMyDataSocket::MakeDataPacket(pPacket p2,int Type, int nParam1, int DataType, int nDataLen, void * DataBuf)
|
{
|
p2->sign=0x55AA;
|
p2->nChannel=0;
|
p2->nCMD=Type;
|
p2->nDataType=DataType;
|
p2->nDataSize=nDataLen;
|
p2->Param1=nParam1;
|
if (DataBuf!=NULL) memcpy_s(p2->data,1024,DataBuf,nDataLen);
|
|
return sizeof(tagPacket)+p2->nDataSize;
|
}
|
|
int CMyDataSocket::ProcessPacket(pPacket p1)
|
{
|
CString s1;
|
char tempsendbuf2[2048];
|
char databuf2[2048];
|
pPacket p2=(pPacket)tempsendbuf2;
|
int len=0;
|
double time1=GetTimeMs();
|
switch(p1->nCMD)
|
{
|
case cmdNone:
|
break;
|
case cmdPing:
|
len=MakeCtrlPacket(p2,cmdPingReply,p1->Param1);
|
SendPacket(p2,len);
|
break;
|
case cmdPingReply:
|
if (p1->Param1==PingSeq){ latancy=time1-pingTime; bPinging=0;}
|
break;
|
case cmdHeartBeat:
|
break;
|
case cmdMessage:
|
break;
|
case cmdGetVersion:
|
len=MakeDataPacket(p2,cmdVerInfo,1,dataTypeVerInfo,sizeof(stVerInfo),&MyVerInfo);
|
SendPacket(p2,len);
|
break;
|
case cmdVerInfo:
|
TargetVer=*(stVerInfo *)(p1->data);
|
break;
|
case cmdLogin:
|
if (p1->Param1==1)
|
{
|
len=MakeDataPacket(p2,cmdAcceptLogin,1,1,10,databuf2);
|
SendPacket(p2,len);
|
//Logined=1;
|
}
|
break;
|
case cmdLogout:
|
break;
|
case cmdAcceptLogin:
|
Logined=1;
|
break;
|
case cmdSendDummyData:
|
len=MakeCtrlPacket(p2,cmdNone,1);
|
SendPacket(p2,len);
|
break;
|
case cmdSendFile:
|
if (p1->nDataType==dataFileInfo)
|
{
|
pFileInfo pf1=(pFileInfo)p1->data;
|
DoRecvFile(pf1);
|
}
|
break;
|
case cmdAcceptFile:
|
if (bToSendFile) //×¼±¸·¢ËÍÎļþ
|
{
|
if (p1->Param1==0) //¶Ô·½¾Ü¾ø
|
{
|
s1.Format(_T("%d ¶Ô·½¾Ü¾øÎļþ\r\n"),Idx);
|
SysLog(s1);
|
bToSendFile=0;
|
m_filesend1.Close(); //Îļþ¹Ø±Õ;
|
}else if (p1->Param1==1)
|
{
|
s1.Format(_T("%d ¶Ô·½½ÓÊÜÎļþ\r\n"),Idx);
|
SysLog(s1);
|
bToSendFile=0;
|
bSendingFile=1;
|
nSendFileSize=m_filesend1.GetLength();
|
nFileSentSize=0;
|
TriggerSend();
|
}
|
}
|
break;
|
case cmdSendFileData:
|
if (bRecvingFile)
|
{
|
if (p1->nDataType==dataFileData)
|
m_filerecv1.Write(p1->data,p1->nDataSize);
|
nFileRecvedSize+=p1->nDataSize;
|
if (nFileRecvedSize>=nRecvFileSize)
|
{
|
CFileStatus status1;
|
CString sFilePathName;
|
sFilePathName=m_filerecv1.GetFilePath();
|
status1.m_atime=mRecvFileInfo.m_aTime;
|
status1.m_ctime=mRecvFileInfo.m_cTime;
|
status1.m_mtime=mRecvFileInfo.m_mTime;
|
status1.m_attribute=mRecvFileInfo.nFileAttr;
|
status1.m_size=0;
|
status1.m_szFullName[0]=0;
|
m_filerecv1.Close();
|
CFile::SetStatus(sFilePathName,status1);
|
bRecvingFile=0;
|
s1.Format(_T("%d Îļþ½ÓÊÕÍê±Ï\r\n"),Idx);
|
SysLog(s1);
|
}
|
}
|
break;
|
default:
|
break;
|
}
|
return 0;
|
}
|
|
int CMyDataSocket::SendFile(CString sFilePathName)
|
{
|
CString s1;
|
if (ConnectionStat!=statConnected) {return -1;}
|
if (bToSendFile) {return -1;}
|
if (bSendingFile) {return -1;}
|
char tempSendbuf2[2048];
|
CString sFileName;
|
CStringA sFileNameA;
|
pPacket pPacket1;
|
pFileInfo pFileInfo1;
|
pPacket1=(pPacket)tempSendbuf2;
|
pFileInfo1=(pFileInfo)pPacket1->data;
|
|
int j=m_filesend1.Open(sFilePathName,CFile::OpenFlags::modeRead|CFile::OpenFlags::shareDenyNone,&e1);
|
if (j)
|
{
|
sFileName=m_filesend1.GetFileName();
|
SHTCharToAnsi(sFileName,sFileNameA.GetBuffer(2048),2048);
|
sFileNameA.ReleaseBuffer();
|
int len1=(sFileNameA.GetLength()+1+3)&(-4);
|
|
CFileStatus status1;
|
m_filesend1.GetStatus(status1);
|
s1.Format(_T("Size of status1 %d \r\n"),sizeof(status1));
|
s1.AppendFormat(_T("Size of CTime %d \r\n"),sizeof(CTime));
|
s1.AppendFormat(_T("FileSize %lld \r\n"),status1.m_size);
|
s1.AppendFormat(_T("FileName %s \r\n"),status1.m_szFullName);
|
SysLog(s1);
|
pFileInfo1->m_aTime=status1.m_atime;
|
pFileInfo1->m_cTime=status1.m_ctime;
|
pFileInfo1->m_mTime=status1.m_mtime;
|
pFileInfo1->nFileSize=status1.m_size;
|
pFileInfo1->nFileAttr=status1.m_attribute;
|
pFileInfo1->nFileNameLength=len1;
|
strcpy_s(pFileInfo1->m_FileName,len1,sFileNameA);
|
pPacket1->sign=0x55aa;
|
pPacket1->nChannel=0;
|
pPacket1->nCMD=cmdSendFile;
|
pPacket1->Param1=0;
|
pPacket1->nDataType=dataFileInfo;
|
pPacket1->nDataSize=sizeof(tagFileInfo)+len1;
|
int SendLength=sizeof(tagPacket)+pPacket1->nDataSize;
|
s1.Format(_T("Len1 %d DataSize %d PacketSize %d \r\n"),len1,pPacket1->nDataSize,SendLength);
|
SysLog(s1);
|
SendBuf.PushIn(pPacket1,SendLength);
|
TriggerSend();
|
bToSendFile=1;
|
/*
|
int k=Send(pPacket1,SendLength);
|
if (k==SOCKET_ERROR)
|
{
|
k=GetLastError();
|
CString s2;
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,k,0,s2.GetBuffer(2048),2048,NULL);
|
s2.ReleaseBuffer();
|
s1.Format(_T("Send Error %d %d %s\r\n"),j,k,s2);
|
SysLog(s1);
|
return -1;
|
}else
|
{
|
if (k<SendLength)
|
{
|
s1.Format(_T("Send Error,ToSend %d Send %d"),SendLength,k);
|
SysLog(s1);
|
return -1;
|
}else
|
{
|
bToSendFile=1;
|
}
|
}
|
*/
|
return 0;
|
}else
|
{
|
CString sError;
|
e1.GetErrorMessage(sError.GetBuffer(2048),2048);
|
sError.ReleaseBuffer();
|
s1.Format(_T("´ò¿ªÎļþ %s ʧ°Ü %s\r\n"),sFilePathName,sError);
|
SysLog(s1);
|
return -1;
|
}
|
return 0;
|
}
|
|
int CMyDataSocket::ContinueReadFile(char * pFileBuf, int nBlockSize)
|
{
|
CString s1;
|
INT64 FileLeftSize;
|
FileLeftSize=nSendFileSize-nFileSentSize;
|
if (FileLeftSize==0) return 0;
|
int ReadDataLength=(int)min(nBlockSize,FileLeftSize);
|
int SendLength=ReadDataLength+sizeof(tagPacket);
|
pPacket p1;
|
p1=(pPacket)pFileBuf;
|
p1->sign=0x55aa;
|
p1->nChannel=0;
|
p1->nCMD=cmdSendFileData;
|
p1->Param1=0;
|
p1->nDataType=dataFileData;
|
p1->nDataSize=ReadDataLength;
|
m_filesend1.Read(p1->data,ReadDataLength);
|
nFileSentSize+=ReadDataLength;
|
// s1.Format(_T("%d ReadFile %d Total %lld\r\n"),Idx,ReadDataLength,nFileSentSize);
|
// SysLog(s1);
|
return SendLength;
|
}
|
|
int CMyDataSocket::DoRecvFile(pFileInfo pf1)
|
{
|
CString s1;
|
CString sFileName;
|
CStringA sFileNameA;
|
CString sFilePathName;
|
sFileNameA=pf1->m_FileName;
|
INT64 nFileLength=pf1->nFileSize;
|
SHAnsiToTChar(sFileNameA,sFileName.GetBuffer(2048),2048);
|
sFileName.ReleaseBuffer();
|
s1.Format(_T("%d ¶Ô·½·¢À´Îļþ %s Size %lld \r\n"),Idx,sFileName,nFileLength);
|
SysLog(s1);
|
|
|
int bAccept=0; //¾Ü¾ø/½ÓÊÜ 0/1
|
if (bRecvingFile)
|
{
|
bAccept=0; //¾Ü¾ø
|
}else
|
{
|
CFileDialog dialog1(false,_T("*.*"),sFileName,OFN_OVERWRITEPROMPT,_T("*.*|*.*||"),NULL);
|
INT_PTR r=dialog1.DoModal();
|
if (r==IDOK)
|
{
|
CFileException e1;
|
sFilePathName=dialog1.GetPathName();
|
int j=m_filerecv1.Open(sFilePathName,CFile::modeCreate|CFile::modeWrite,&e1);
|
if (j)
|
{
|
bAccept=1; //½ÓÊÜ
|
bRecvingFile=1;
|
mRecvFileInfo.m_aTime=pf1->m_aTime;
|
mRecvFileInfo.m_cTime=pf1->m_cTime;
|
mRecvFileInfo.m_mTime=pf1->m_mTime;
|
mRecvFileInfo.nFileAttr=pf1->nFileAttr;
|
mRecvFileInfo.nFileSize=pf1->nFileSize;
|
nRecvFileSize=pf1->nFileSize;
|
nFileRecvedSize=0;
|
s1.Format(_T("%d ½ÓÊÜ Áí´æΪ %s \r\n"),Idx,sFilePathName);
|
}else
|
{
|
bAccept=0;
|
s1.Format(_T("%d ¾Ü¾ø\r\n"),Idx);
|
}
|
|
}else
|
{
|
bAccept=0; //¾Ü¾ø
|
s1.Format(_T("%d ½ÓÊÕÖУ¬¾Ü¾ø\r\n"),Idx);
|
}
|
}
|
SysLog(s1);
|
char tempsendbuf2[2048];
|
pPacket p2;
|
p2=(pPacket)tempsendbuf2;
|
int len1=MakeCtrlPacket(p2,cmdAcceptFile,bAccept);
|
SendPacket(p2,len1);
|
return 0;
|
}
|
|
int CMyDataSocket::StartSendMass(int Length, int Count)
|
{
|
DummyToSend+=Length*Count;
|
TriggerSend();
|
return 0;
|
}
|
|
CMyPingSocket::CMyPingSocket()
|
{
|
TotalHosts=0;
|
seq=0;
|
}
|
CMyPingSocket::~CMyPingSocket()
|
{
|
|
}
|
int CMyPingSocket::Init()
|
{
|
CString s1;
|
int i=0;
|
if (m_hSocket==INVALID_SOCKET)
|
{
|
i=Socket(SOCK_RAW,63,IPPROTO_ICMP,PF_INET);
|
s1.Format(_T("Create Ping Sock_raw = %d \r\n"),i);
|
SysLog(s1);
|
}
|
return i;
|
}
|
USHORT CMyPingSocket::checksum(USHORT * pBuf,int iSize)
|
{
|
unsigned long cksum=0;
|
while(iSize>1)
|
{
|
cksum+=*pBuf++;
|
iSize-=sizeof(USHORT);
|
}
|
if(iSize)
|
cksum+=*(UCHAR*)pBuf;
|
cksum=(cksum>>16)+(cksum&0xffff);
|
cksum+=(cksum>>16);
|
return (USHORT)(~cksum);
|
}
|
void CMyPingSocket::fill_icmp_data(char * pBuf,int iSize)
|
{
|
char a='a';
|
for (int i=0;i<iSize;i++)
|
{
|
pBuf[i]=a;
|
a++;
|
if (a>='v') {a='a';}
|
}
|
}
|
void CMyPingSocket::OnReceive(int nErrorCode)
|
{
|
CString s1;
|
int RecvLength=65536;
|
CString sRipaddr;
|
UINT rport;
|
int j=0;
|
if (nErrorCode==0)
|
{
|
j=ReceiveFrom(RecvBuf1,RecvLength,sRipaddr,rport);
|
// s1.Format(_T("Recv From %s %d Bytes \r\n"),sRipaddr,j);
|
// SysLog(s1);
|
if (j<sizeof(IpHeader)+sizeof(IcmpHeader)) return;
|
IpHeader * pIp=(IpHeader *)RecvBuf1;
|
IcmpHeader * picmp1=(IcmpHeader *)(&RecvBuf1[0]+sizeof(IpHeader));
|
if (pIp->protocol==IPPROTO_ICMP)
|
{
|
int thisseq=ntohs(picmp1->i_seq);
|
|
double thistime=GetTimeMs();
|
for (int i=0;i<TotalHosts;i++)
|
{
|
if (thisseq==Hosts[i].seq)
|
{
|
Hosts[i].Replied=true;
|
s1.Format(_T("Reply From %s(%s) %d Bytes time %.3fmS TTL %d\r\n"),Hosts[i].sHostName,sRipaddr,j-sizeof(IpHeader)-sizeof(IcmpHeader),thistime-Hosts[i].PingTime,pIp->tt1);
|
SysLog(s1);
|
break;
|
}
|
}
|
|
}
|
}
|
}
|
int CMyPingSocket::DoPings()
|
{
|
//ÏÈ×¼±¸Êý¾Ý
|
CString s1;
|
int datasize=32;
|
IcmpHeader * picmp1=(IcmpHeader *)SendBuf1;
|
picmp1->i_type=8;
|
picmp1->i_code=0;
|
picmp1->i_id=htons(4);
|
fill_icmp_data(picmp1->datas,datasize);
|
|
picmp1->i_seq=htons(seq);
|
picmp1->i_cksum=0;
|
picmp1->i_cksum=checksum((USHORT *)picmp1,sizeof(IcmpHeader)+datasize);
|
|
for (int i=0;i<TotalHosts;i++)
|
{
|
seq++;
|
picmp1->i_seq=htons(seq);
|
picmp1->i_cksum=0;
|
picmp1->i_cksum=checksum((USHORT *)picmp1,sizeof(IcmpHeader)+datasize);
|
Hosts[i].seq=seq;
|
if (Hosts[i].Pinging&&!Hosts[i].Replied)
|
{
|
s1.Format(_T("Request %s TimeOut\r\n"),Hosts[i].sHostName);
|
SysLog(s1);
|
}
|
Hosts[i].Pinging=true;
|
Hosts[i].Replied=false;
|
int j=SendTo(SendBuf1,sizeof(IcmpHeader)+datasize,0,Hosts[i].sHostName,0);
|
Hosts[i].PingTime=GetTimeMs();
|
s1.Format(_T("Ping %s = %d \r\n"),Hosts[i].sHostName,j);
|
// SysLog(s1);
|
}
|
return 0;
|
}
|
int CMyPingSocket::AddHostToList(CString HostAddr)
|
{
|
if (m_hSocket==INVALID_SOCKET) Init();
|
if ( TotalHosts+1 >= 100 ) {return -1;}
|
Hosts[TotalHosts].sHostName = HostAddr;
|
Hosts[TotalHosts].Pinging = false;
|
Hosts[TotalHosts].Replied = false;
|
TotalHosts++;
|
return 0;
|
}
|
int CMyPingSocket::DelHostFromList(int index)
|
{
|
return index;
|
}
|
int CMyPingSocket::EmptyList()
|
{
|
TotalHosts=0;
|
return 0;
|
}
|
|
void CMyUDPSocket1::OnReceive(int nErrorCode)
|
{
|
CString s1;
|
int RecvLength=65536;
|
CString sRipaddr;
|
UINT rport;
|
int j=0;
|
char * recvbuf[65536];
|
if (nErrorCode==0)
|
{
|
j=ReceiveFrom(recvbuf,RecvLength,sRipaddr,rport);
|
// s1.Format(_T("Recv From %s %d Bytes \r\n"),sRipaddr,j);
|
// SysLog(s1);
|
s1.Format(_T("1Recv From %s(%d) %d Bytes\r\n"),sRipaddr,rport,j);
|
SysLog(s1);
|
|
}
|
}
|
|
void CMyUDPSocket2::OnReceive(int nErrorCode)
|
{
|
CString s1;
|
int RecvLength=65536;
|
CString sRipaddr;
|
UINT rport;
|
int j=0;
|
char * recvbuf[65536];
|
if (nErrorCode==0)
|
{
|
j=ReceiveFrom(recvbuf,RecvLength,sRipaddr,rport);
|
// s1.Format(_T("Recv From %s %d Bytes \r\n"),sRipaddr,j);
|
// SysLog(s1);
|
s1.Format(_T("2Recv From %s(%d) %d Bytes\r\n"),sRipaddr,rport,j);
|
SysLog(s1);
|
|
}
|
}
|
|
void CMyUDPSocket3::OnReceive(int nErrorCode)
|
{
|
CString s1;
|
int RecvLength=65536;
|
CString sRipaddr;
|
UINT rport;
|
int j=0;
|
char * recvbuf[65536];
|
if (nErrorCode==0)
|
{
|
j=ReceiveFrom(recvbuf,RecvLength,sRipaddr,rport);
|
// s1.Format(_T("Recv From %s %d Bytes \r\n"),sRipaddr,j);
|
// SysLog(s1);
|
s1.Format(_T("3Recv From %s(%d) %d Bytes\r\n"),sRipaddr,rport,j);
|
SysLog(s1);
|
|
}
|
}
|