?? netreceivers.cpp
字號:
// CCTPNetReceiver - CTP reciever
// CTCPNetReceiver - TCP reciever
// CUDPNetReceiver - UDP reciever
// They partly duplicate each other to allow to customize support of every
// protocol easily
// Implementation file
//
// (c) Lev Naumov, CAMEL Laboratory
// E-mail: camellab@mail.ru
// For more information see http://camel.ifmo.ru or
// http://www.codeproject.com/internet/ctp.asp
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NetBasic.h"
#include "NetReceivers.h"
#include "CTPNet.h"
#include "TCPNet.h"
#include "UDPNet.h"
#include "CTPDemo.h"
#include "CTPStatusDlg.h"
#include "CTPDemoDlg.h"
void CCTPNetReceiver::OnReceive(void* data)
{
// Typecast variables
CCTPReceivedData* rd=(CCTPReceivedData*)data;
CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
// Other necessary variables
CString s="";
char ips[16];
char timestamp[22];
if (rd->command==NET_TEXT) {
// Handle NET_TEXT command
rd->from.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
s.Format("%s CTP> Recieve text from %s: size %d\n",timestamp,ips,rd->size);
dlg->GetDlgItem(IDC_TEXTRECV)->SetWindowText(rd->pBuf);
} else if (rd->command==NET_FILE) {
// Handle NET_FILE command
rd->from.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
s.Format("%s CTP> Recieve file from %s: size %d\n",timestamp,ips,rd->size);
// Ask for filename
CFileDialog fd(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
if (fd.DoModal()==IDOK) {
// Copy recieved data to file
CStdioFile file;
file.Open(fd.GetPathName(),CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
file.Write(rd->pBuf,(UINT)rd->size);
file.Close();
}
}
// Prepend text to log
CString text;
dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
void CCTPNetReceiver::OnError(void* data)
{
// Typecast variables
CCTPErrorInfo* ei=(CCTPErrorInfo*)data;
CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
// Generate error information text
CString s;
char ips[16];
ei->addr.GetString(ips);
s.Format("%s CTP> Error from %s: type %d, WinSocket error code %d, command %d\n",ei->timestamp,ips,ei->type,ei->code,ei->command);
// Prepend text to log
CString text;
dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
void CTCPNetReceiver::OnReceive(void* data)
{
// Typecast variables
CTCPReceivedData* rd=(CTCPReceivedData*)data;
CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
// Other necessary variables
CString s="";
char ips[16];
char timestamp[22];
if (rd->pBuf[0]==0) {
// Handle arrived text (first byte is zero)
rd->from.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
s.Format("%s TCP> Recieve text from %s: size %d\n",timestamp,ips,rd->size);
dlg->GetDlgItem(IDC_TEXTRECV)->SetWindowText(&(rd->pBuf[1]));
} else {
// Handle arrived file (first byte is not zero)
rd->from.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
s.Format("%s TCP> Recieve file from %s: size %d\n",timestamp,ips,rd->size);
// Ask for filename
CFileDialog fd(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
if (fd.DoModal()==IDOK) {
// Copy recieved data to file
CStdioFile file;
file.Open(fd.GetPathName(),CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
file.Write(&(rd->pBuf[1]),(UINT)rd->size-1);
file.Close();
}
}
// Prepend text to log
CString text;
dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
void CTCPNetReceiver::OnError(void* data)
{
// Typecast variables
CTCPErrorInfo* ei=(CTCPErrorInfo*)data;
CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
// Generate error information text
CString s;
char ips[16];
ei->addr.GetString(ips);
s.Format("%s TCP> Error from %s: type %d, WinSocket error code %d\n",ei->timestamp,ips,ei->type,ei->code);
// Prepend text to log
CString text;
dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
void CUDPNetReceiver::OnReceive(void* data)
{
// Typecast variables
CUDPReceivedData* rd=(CUDPReceivedData*)data;
CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
// Other necessary variables
CString s="";
char ips[16];
char timestamp[22];
if (rd->pBuf[0]==0) {
// Handle arrived text (first byte is zero)
rd->from.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
s.Format("%s UDP> Recieve text from %s: size %d\n",timestamp,ips,rd->size);
dlg->GetDlgItem(IDC_TEXTRECV)->SetWindowText(&(rd->pBuf[1]));
} else {
// Handle arrived file (first byte is not zero)
rd->from.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
s.Format("%s UDP> Recieve file from %s: size %d\n",timestamp,ips,rd->size);
// Ask for filename
CFileDialog fd(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
if (fd.DoModal()==IDOK) {
// Copy recieved data to file
CStdioFile file;
file.Open(fd.GetPathName(),CFile::typeBinary|CFile::modeCreate|CFile::modeWrite);
file.Write(&(rd->pBuf[1]),(UINT)rd->size-1);
file.Close();
}
}
// Prepend text to log
CString text;
dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
void CUDPNetReceiver::OnError(void* data)
{
// Typecast variables
CUDPErrorInfo* ei=(CUDPErrorInfo*)data;
CCTPDemoDlg* dlg=(CCTPDemoDlg*)m_pMainWnd;
// Generate error information text
CString s;
char ips[16];
ei->addr.GetString(ips);
s.Format("%s UDP> Error from %s: type %d, WinSocket error code %d\n",ei->timestamp,ips,ei->type,ei->code);
// Prepend text to log
CString text;
dlg->GetDlgItem(IDC_LOG)->GetWindowText(text);
dlg->GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -