📄 filesession.cpp
字号:
/***************************************************************************
* *
* 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. *
* *
* copyright : (C) 2003 by Zhang Yong *
* email : z-yong163@163.com *
***************************************************************************/
#include "filesession.h"
#include <string.h>
enum {
FILE_CMD_INFO = 0x1000,
FILE_CMD_RECEIVE,
FILE_CMD_DATA,
};
FileSession::FileSession(TCPSessionBase *tcp)
{
tcpSession = tcp;
file = NULL;
fileSize = 0;
}
FileSession::~FileSession()
{
if (file)
fclose(file);
if (tcpSession)
tcpSession->destroy();
}
void FileSession::destroy()
{
tcpSession = NULL;
delete this;
}
bool FileSession::sendFileInfo(const char *path, const char *name)
{
file = fopen(path, "rb");
if (!file)
return false;
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
OutPacket out;
out << name << fileSize;
tcpSession->sendPacket(FILE_CMD_INFO, out.data, out.getLength());
return true;
}
void FileSession::onSend()
{
if (!file) {
tcpSession->enableWrite(false);
return;
}
char buf[960];
int n = fread(buf, 1, sizeof(buf), file);
if (n > 0) {
OutPacket out;
out.writeData(buf, n);
if (tcpSession->sendPacket(FILE_CMD_DATA, out.data, out.getLength()))
onFileProgress(n);
}
if (n < sizeof(buf)) {
tcpSession->enableWrite(false);
fclose(file);
file = NULL;
onFileComplete();
}
}
void FileSession::onClose()
{
if (file) {
fclose(file);
file = NULL;
}
onFileComplete();
}
void FileSession::onReceive(uint16 cmd, const char *data, int n)
{
InPacket in(data, n);
switch (cmd) {
case FILE_CMD_INFO:
onFileInfo(in);
break;
case FILE_CMD_RECEIVE:
onFileReceive(in);
break;
case FILE_CMD_DATA:
onFileData(in);
break;
}
}
void FileSession::onFileInfo(InPacket &in)
{
const char *name;
in >> name >> fileSize;
const char *path = getPathName(name, fileSize);
if (!path || file)
return;
file = fopen(path, "wb");
free((char *) path);
if (file)
tcpSession->sendPacket(FILE_CMD_RECEIVE, NULL, 0);
}
void FileSession::onFileReceive(InPacket &in)
{
if (file)
tcpSession->enableWrite(true);
}
void FileSession::onFileData(InPacket &in)
{
if (!file)
return;
int n;
const char *data = in.readData(n);
if (data && fwrite(data, n, 1, file) == 1)
onFileProgress(n);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -