Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CEthernetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
141 changes: 51 additions & 90 deletions CFileAppLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}

8 changes: 7 additions & 1 deletion CFileAppLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ 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 {
DATA_TYPE_BEGIN = 0x00,
DATA_TYPE_CONT = 0x01,
DATA_TYPE_END = 0x02
};

};
#endif // !defined(AFX_FILELAYER_H__D67222B3_1B00_4C77_84A4_CEF6D572E181__INCLUDED_)
2 changes: 1 addition & 1 deletion CNILayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading