diff --git a/CEthernetLayer.cpp b/CEthernetLayer.cpp index 29f5e1d..41e7948 100644 --- a/CEthernetLayer.cpp +++ b/CEthernetLayer.cpp @@ -64,6 +64,7 @@ BOOL CEthernetLayer::Send(unsigned char* ppayload, int nlength, unsigned short t BOOL CEthernetLayer::Receive(unsigned char* ppayload) { + PETHERNET_HEADER pFrame = (PETHERNET_HEADER)ppayload; BOOL bSuccess = FALSE; @@ -92,6 +93,16 @@ BOOL CEthernetLayer::Receive(unsigned char* ppayload) bSuccess = GetUpperLayer(0)->Receive(pFrame->enet_data); } + if (pFrame->enet_type == FILE_TYPE) + { + if (IsBroadcast(pFrame->enet_dstaddr)) + { + ((CFileAppLayer::LPFILE_APP)pFrame->enet_data)->fapp_type = CFileAppLayer::CHAT_MESSAGE_BROADCAST; + } + + bSuccess = GetUpperLayer(1)->Receive(pFrame->enet_data); + } + return bSuccess; } diff --git a/CFileAppLayer.cpp b/CFileAppLayer.cpp index 8659e69..cb0aef2 100644 --- a/CFileAppLayer.cpp +++ b/CFileAppLayer.cpp @@ -71,18 +71,21 @@ UINT CFileAppLayer::F_Sendthr(LPVOID Fileobj) { FApplayer->m_sHeader.fapp_totlen = (unsigned long)dwFileSize; FApplayer->m_sHeader.fapp_type = DATA_TYPE_BEGIN; FApplayer->m_sHeader.fapp_seq_num = 0; - FApplayer->bSEND = FApplayer->Send((unsigned char*)&(FApplayer->filename), 12 + (dwWrite > FAPP_DATA_SIZE ? FAPP_DATA_SIZE : dwWrite)); + //memset(FApplayer->m_sHeader.fapp_data, 0, copyLength + 1); + memcpy(FApplayer->m_sHeader.fapp_data,FApplayer->filename, copyLength + 1); //파일명 못받는 오류해결 + FApplayer->bSEND = FApplayer->Send((unsigned char*)&(FApplayer->m_sHeader), 13 + copyLength); Sleep(30); FApplayer->p_Progress->SetPos(dwFileSize / FAPP_DATA_SIZE); // 송신과정 6 if (dwFileSize <= FAPP_DATA_SIZE) { //송신과정 2 - FApplayer->m_sHeader.fapp_type = DATA_TYPE_CONT; + FApplayer->m_sHeader.fapp_type = DATA_TYPE_END; //modify FApplayer->m_sHeader.fapp_seq_num = 1; ReadFile(hFile, FApplayer->m_sHeader.fapp_data, dwFileSize, &dwWrite, NULL); if (dwWrite != FAPP_DATA_SIZE) return -1; //real send FApplayer->bSEND = FALSE; + FApplayer->m_sHeader.fapp_totlen = dwFileSize; //modify FApplayer->bSEND = FApplayer->Send((unsigned char*)&(FApplayer->m_sHeader), 12 + (dwWrite > FAPP_DATA_SIZE ? FAPP_DATA_SIZE : dwWrite)); Sleep(30); } @@ -93,110 +96,68 @@ UINT CFileAppLayer::F_Sendthr(LPVOID Fileobj) { CloseHandle(hFile); return FApplayer->bSEND; } -BOOL CFileAppLayer::DoFragmentation_f(CFileAppLayer* FileApplayer,HANDLE hfile, DWORD Filesize) { //Limited to 0x00 0x01 0x02 unlike DoFragmentation_c +BOOL CFileAppLayer::DoFragmentation_f(CFileAppLayer* FileApplayer, HANDLE hfile, DWORD Filesize) { DWORD dwWrite = 0, dwRead; DWORD total_size = Filesize; DWORD sent_size = 0; - unsigned long seq = 1; - unsigned char buffer[FAPP_DATA_SIZE]; - - while (sent_size < total_size) { - - DWORD dwToRead = min(FAPP_DATA_SIZE, total_size - sent_size); - - if (ReadFile(hfile, buffer, dwToRead, &dwWrite, NULL) && dwWrite > 0) { - - if (sent_size + dwWrite == Filesize) - FileApplayer->m_sHeader.fapp_type = DATA_TYPE_END; //set 0x03 - else if(sent_size == 0) - FileApplayer->m_sHeader.fapp_type = DATA_TYPE_CONT; //set 0x02 - - FileApplayer->m_sHeader.fapp_seq_num = seq++; //set seq_num - memcpy(FileApplayer->m_sHeader.fapp_data, buffer, dwWrite); //set data - //real send - FileApplayer->bSEND = FALSE; - FileApplayer->bSEND = FileApplayer->Send((unsigned char*)&(FileApplayer->m_sHeader), 12 + (dwWrite > FAPP_DATA_SIZE ? FAPP_DATA_SIZE : dwWrite)); - Sleep(30); - //continue to work - sent_size += dwWrite; - FileApplayer->p_Progress->SetPos(seq); //송신과정 6 - } - } + unsigned long seq = 1; + int cnt = 0; + int remainder = Filesize % FAPP_DATA_SIZE; // 나머지 + int quot = Filesize / FAPP_DATA_SIZE + (remainder ? 1 : 0); // 몫, 나머지가 있으면 +1 + unsigned char buffer[99999]; + ReadFile(hfile, buffer, total_size, &dwRead, NULL); // dwRead는 실제로 읽은 바이트 수 + + do { + DWORD bytesToSend = (cnt == quot - 1) ? remainder : FAPP_DATA_SIZE; // 마지막 패킷이면 나머지, 아니면 정해진 크기 + FileApplayer->m_sHeader.fapp_type = (cnt == quot - 1) ? DATA_TYPE_END : DATA_TYPE_CONT; // 마지막이면 DATA_TYPE_END, 아니면 DATA_TYPE_CONT + FileApplayer->m_sHeader.fapp_seq_num = seq++; + memcpy(FileApplayer->m_sHeader.fapp_data, &buffer[cnt * FAPP_DATA_SIZE], bytesToSend); // 현재 조각 복사 + FileApplayer->m_sHeader.fapp_totlen = bytesToSend; // 현재 보낼 크기 설정 + + // 실제 전송 + FileApplayer->bSEND = FileApplayer->Send((unsigned char*)&(FileApplayer->m_sHeader), 12 + bytesToSend); + Sleep(30); + + // 진행률 업데이트 + FileApplayer->p_Progress->SetPos(seq); + + sent_size += bytesToSend; // 전송된 크기 갱신 + cnt++; // 다음 패킷을 위해 카운트 증가 + + } while (cnt < quot); + return FileApplayer->bSEND; } + BOOL CFileAppLayer::Send(unsigned char* frame, int size) { bSEND= FALSE; - bSEND = ((CEthernetLayer*)(mp_UnderLayer))->Send((unsigned char*)&m_sHeader, size, FILE_TYPE); + bSEND = ((CEthernetLayer*)(mp_UnderLayer))->Send(frame, size, FILE_TYPE); return bSEND; } -BOOL CFileAppLayer::Receive(unsigned char* frame) { //수신과정 1에 대해 첫 번째 조각은 무조건 filename - //수신과정 2는 수행될 필요가 없다. - //수신과정 3,4에 관해 각각 실행 - //수신과정 5가 정말 수신과정에서 수행되어야하는 게 맞나? - LPFILE_APP payload = (LPFILE_APP)frame; - - static HANDLE hFile = INVALID_HANDLE_VALUE; - - if (payload->fapp_type == DATA_TYPE_BEGIN) { - CString file_name; - file_name.Format(_T("%s"), payload->fapp_data); - hFile = CreateFile(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile == INVALID_HANDLE_VALUE) { - // 파일을 열 수 없다면 에러 메시지를 출력하고 함수를 종료한다. - AfxMessageBox(_T("File can't be opend")); - return FALSE; - } - //파일 포인터를 이용한 크기 설정 - LARGE_INTEGER liSize; - liSize.QuadPart = payload->fapp_totlen; - if (!SetFilePointerEx(hFile, liSize, NULL, FILE_BEGIN)) { - AfxMessageBox(_T("Failed to set file pointer")); - CloseHandle(hFile); - hFile = INVALID_HANDLE_VALUE; - return FALSE; - } - if (!SetEndOfFile(hFile)) { - AfxMessageBox(_T("Failed to set end of file")); - CloseHandle(hFile); - hFile = INVALID_HANDLE_VALUE; - return FALSE; - } - //file pointer initialize - liSize.QuadPart = 0; - if (!SetFilePointerEx(hFile, liSize, NULL, FILE_BEGIN)) { - AfxMessageBox(_T("Failed to reset file pointer")); - CloseHandle(hFile); - hFile = INVALID_HANDLE_VALUE; - return FALSE; - } +BOOL CFileAppLayer::Receive(unsigned char* frame) { + LPFILE_APP payload = (LPFILE_APP)frame; + if (payload->fapp_type == DATA_TYPE_BEGIN) { + CString file_name; + file_name.Format(_T("%s"), payload->fapp_data); + WriteFile.Open(file_name, CFile::modeCreate | CFile::modeWrite); + WriteFile.SetLength(payload->fapp_totlen); + } + else { + WriteFile.Seek((payload->fapp_seq_num - 1) * FAPP_DATA_SIZE, CFile::begin); + WriteFile.Write(payload->fapp_data, payload->fapp_totlen); + if (payload->fapp_type == DATA_TYPE_END) { + WriteFile.Close(); + AfxMessageBox(_T("Success")); } + } + return TRUE; +} - else { - LARGE_INTEGER liPos; - liPos.QuadPart = payload->fapp_seq_num * FAPP_DATA_SIZE; - SetFilePointerEx(hFile, liPos, NULL, FILE_BEGIN); - if (!SetFilePointerEx(hFile, liPos, NULL, FILE_BEGIN)) { - AfxMessageBox(_T("Failed to set file pointer")); - return FALSE; - } - DWORD dwWritten; - ::WriteFile(hFile, payload->fapp_data,FAPP_DATA_SIZE, &dwWritten, NULL); //이 부분 어떻게 해결하지 binary data니,멤버 변수 새로 도입하는 수 밖에? - - if (payload->fapp_type == DATA_TYPE_END) { - CloseHandle(hFile); - hFile = INVALID_HANDLE_VALUE; - AfxMessageBox(_T("Success!")); - } - } - - - return TRUE; - } diff --git a/CFileAppLayer.h b/CFileAppLayer.h index 4731233..43fc62c 100644 --- a/CFileAppLayer.h +++ b/CFileAppLayer.h @@ -40,12 +40,17 @@ class CFileAppLayer BOOL Send(unsigned char* frame, int size); - BOOL Receive(unsigned char* ppayload); + BOOL Receive(unsigned char* frame); void SetFilepath(CString Path); static UINT F_Sendthr(LPVOID Filepath); void SetProgressCtrl(CProgressCtrl* p); static BOOL DoFragmentation_f(CFileAppLayer* FileApplayer,HANDLE hfile,DWORD Filesize); static UINT FileThread(LPVOID pParam); + // Used for indicating message type. + enum : unsigned short { + CHAT_MESSAGE_NORMAL, + CHAT_MESSAGE_BROADCAST + }; protected: FILE_APP m_sHeader; enum { @@ -53,5 +58,6 @@ class CFileAppLayer DATA_TYPE_CONT = 0x01, DATA_TYPE_END = 0x02 }; + }; #endif // !defined(AFX_FILELAYER_H__D67222B3_1B00_4C77_84A4_CEF6D572E181__INCLUDED_) \ No newline at end of file diff --git a/CNILayer.cpp b/CNILayer.cpp index 82973f7..2018788 100644 --- a/CNILayer.cpp +++ b/CNILayer.cpp @@ -61,7 +61,7 @@ BOOL CNILayer::GetMacAddress(char* deviceName, CNILayer::PhysicalAddress* outAdd PPACKET_OID_DATA oidData = (PPACKET_OID_DATA)malloc(sizeof(PACKET_OID_DATA) + 6); if (oidData == nullptr) { - + return false; } diff --git a/DDABONG.jpg b/DDABONG.jpg new file mode 100644 index 0000000..0e5016e Binary files /dev/null and b/DDABONG.jpg differ diff --git a/Debug/cfileapplayer.obj.enc b/Debug/cfileapplayer.obj.enc new file mode 100644 index 0000000..8f7e0d3 Binary files /dev/null and b/Debug/cfileapplayer.obj.enc differ diff --git a/Debug/ipc2023.ilk b/Debug/ipc2023.ilk index f19f7af..31d4844 100644 Binary files a/Debug/ipc2023.ilk and b/Debug/ipc2023.ilk differ diff --git a/Debug/ipc2023.log b/Debug/ipc2023.log index c478d64..205c7f8 100644 --- a/Debug/ipc2023.log +++ b/Debug/ipc2023.log @@ -1,5 +1,7 @@  CFileAppLayer.cpp E:\MFC_ChattingProgram\CFileAppLayer.cpp(67,55): warning C4244: '인수': 'DWORD'에서 'short'(으)로 변환하면서 데이터가 손실될 수 있습니다. E:\MFC_ChattingProgram\CFileAppLayer.cpp(51,24): warning C4101: 'dwRead' :참조되지 않은 지역 변수입니다. -E:\MFC_ChattingProgram\CFileAppLayer.cpp(97,24): warning C4101: 'dwRead' :참조되지 않은 지역 변수입니다. + 라이브러리를 검색하고 있습니다. + C:\Users\김도현\Desktop\WpdPack_4_1_2\WpdPack\Lib\wpcap.lib 검색 중: + 라이브러리가 검색되었습니다. ipc2023.vcxproj -> E:\MFC_ChattingProgram\Debug\ipc2023.exe diff --git a/Debug/ipc2023.pdb b/Debug/ipc2023.pdb index c6e4201..75a5c6d 100644 Binary files a/Debug/ipc2023.pdb and b/Debug/ipc2023.pdb differ diff --git a/Debug/ipc2023.res b/Debug/ipc2023.res index c9a9ab7..7e9bd6f 100644 Binary files a/Debug/ipc2023.res and b/Debug/ipc2023.res differ diff --git a/Debug/ipc2023.tlog/CL.command.1.tlog b/Debug/ipc2023.tlog/CL.command.1.tlog index 7ef890b..1b7a637 100644 Binary files a/Debug/ipc2023.tlog/CL.command.1.tlog and b/Debug/ipc2023.tlog/CL.command.1.tlog differ diff --git a/Debug/ipc2023.tlog/CL.read.1.tlog b/Debug/ipc2023.tlog/CL.read.1.tlog index 376d153..882cf90 100644 Binary files a/Debug/ipc2023.tlog/CL.read.1.tlog and b/Debug/ipc2023.tlog/CL.read.1.tlog differ diff --git a/Debug/ipc2023.tlog/CL.write.1.tlog b/Debug/ipc2023.tlog/CL.write.1.tlog index 5f0e108..6b87a41 100644 Binary files a/Debug/ipc2023.tlog/CL.write.1.tlog and b/Debug/ipc2023.tlog/CL.write.1.tlog differ diff --git a/Debug/ipc2023.tlog/link.read.1.tlog b/Debug/ipc2023.tlog/link.read.1.tlog index d328b7d..40b8dd0 100644 Binary files a/Debug/ipc2023.tlog/link.read.1.tlog and b/Debug/ipc2023.tlog/link.read.1.tlog differ diff --git a/Debug/vc143.idb b/Debug/vc143.idb index 91f21f7..0c69b83 100644 Binary files a/Debug/vc143.idb and b/Debug/vc143.idb differ diff --git a/Debug/vc143.pdb b/Debug/vc143.pdb index feb5909..16e8b68 100644 Binary files a/Debug/vc143.pdb and b/Debug/vc143.pdb differ diff --git a/ipc2023.aps b/ipc2023.aps index be6d17d..be0fdbd 100644 Binary files a/ipc2023.aps and b/ipc2023.aps differ diff --git a/ipc2023.rc b/ipc2023.rc index 0a29b22..1dbca4b 100644 Binary files a/ipc2023.rc and b/ipc2023.rc differ diff --git a/ipc2023Dlg.cpp b/ipc2023Dlg.cpp index b2183da..fc1735a 100644 --- a/ipc2023Dlg.cpp +++ b/ipc2023Dlg.cpp @@ -490,6 +490,7 @@ void Cipc2023Dlg::OnClickedJpg() { // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다. ((CButton*)GetDlgItem(txt))->SetCheck(0); + m_strFilter = _T("JPEG Files (*.jpg)|*.jpg||"); } @@ -497,6 +498,7 @@ void Cipc2023Dlg::OnClickedTxt() { // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다. ((CButton*)GetDlgItem(jpg))->SetCheck(0); + m_strFilter = _T("Text Files (*.txt)|*.txt||"); } @@ -504,7 +506,7 @@ void Cipc2023Dlg::OnBnClickedButtonAddfile() { UpdateData(TRUE); - CFileDialog dlg(true, _T("*.*"), NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, _T("All Files(*.*)|*.*|"), NULL); + CFileDialog dlg(true, NULL, NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, m_strFilter, NULL); if (dlg.DoModal() == IDOK) { m_Filepath = dlg.GetPathName(); //IDC_EDIT_FilePath컨트롤의 멤버변수 @@ -515,7 +517,8 @@ void Cipc2023Dlg::OnBnClickedButtonAddfile() void Cipc2023Dlg::OnClickedButtonSendfile() -{ +{ + m_Progress.SetPos(0); //begin Thread AfxBeginThread(m_File->F_Sendthr, m_File); } diff --git a/ipc2023Dlg.h b/ipc2023Dlg.h index b201af7..b13c822 100644 --- a/ipc2023Dlg.h +++ b/ipc2023Dlg.h @@ -92,6 +92,7 @@ class Cipc2023Dlg : public CDialogEx, public CBaseLayer CListBox m_ListChat; CString m_Filepath; CProgressCtrl m_Progress;//progress bar + CString m_strFilter = _T("All Files(*.*)|*.*|"); // 파일 확장자 지정 afx_msg void OnBnClickedCheckToall(); // The combobox containing available device list CComboBox deviceComboBox; diff --git a/stdafx.h b/stdafx.h index f568ba6..0b62755 100644 --- a/stdafx.h +++ b/stdafx.h @@ -36,8 +36,8 @@ sizeof(unsigned char) * 2 ) #define APP_DATA_SIZE 1496 -#define CHAT_TYPE 0x2080 -#define FILE_TYPE 0x2090 +#define CHAT_TYPE 0x8020 +#define FILE_TYPE 0x9020 #define FAPP_DATA_SIZE 1488