📄 packetfile.cpp
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006 Daniel Lindstr鰉 and Daniel Nilsson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "StdAfx.h"
#include "PacketFile.h"
#include "packetid.h"
#include <string.h>
PacketFile::PacketFile(void){
filesize = 0;
this->filename[0] = '\0';
}
PacketFile::PacketFile(WCHAR *filename, bool rotate){
FILE *fp = NULL;
this->rotate = rotate;
this->filename[0] = '\0';
this->filesize = 0;
if(filename != NULL){
wcsncpy(this->filename, filename, 255);
if(NULL != (fp = _wfopen(this->filename, L"rb"))){
fseek(fp, 0, SEEK_END);
this->filesize = ftell(fp);
fclose(fp);
}
else{
this->filename[0] = '\0';
}
}
}
PacketFile::~PacketFile(void){
}
void PacketFile::read(Socket *s, int size){
char buf[1024];
int n = 0, totsize = 0;
FILE *fp;
s->receiveInt(&this->filesize);
if(this->filesize == 0)
this->filename[0] = '\0';
else{
s->receiveString(this->filename, 256);
s->receiveByte((char *)&this->rotate);
if(NULL == (fp = _wfopen(this->filename, L"wb"))){
wprintf(L"Opening '%s' failed discarding %d bytes.\n", filename, filesize);
s->discardData(filesize);
this->filename[0] = '\0';
this->filesize = 0;
}
else{
while(totsize < this->filesize){
n = s->receiveData(buf, min(1024, filesize-totsize));
totsize += n;
fwrite(buf, n, 1, fp);
}
fclose(fp);
}
}
}
int PacketFile::getSize(void){
if (filesize == 0) {
return 4;
} else {
return 9 + 2*(int)wcslen(this->filename)+(int)this->filesize;
}
}
void PacketFile::write(Socket *s){
char buf[1024];
WCHAR *fname;
int size;
FILE *fp;
fname = wcsrchr(this->filename, L'\\');
if(fname == NULL)
fname = this->filename;
else
fname++;
fp = _wfopen(this->filename, L"rb");
s->sendInt(this->filesize);
if(fp != NULL){
s->sendString(fname);
s->sendByte(this->rotate ? 1 : 0);
while(!feof(fp)){
size = (int)fread(buf, 1, 1024, fp);
s->sendData(buf, size);
}
fclose(fp);
}
}
int PacketFile::getId(void){
return(PACKET_FILE);
}
WCHAR *PacketFile::getFilename(void){
return(this->filename);
}
bool PacketFile::getRotate(void){
return(this->rotate);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -