📄 shareclient.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 "shareclient.h"
#include <assert.h>
ShareClient::ShareClient(TCPSessionBase *tcp)
{
tcpSession = tcp;
file = NULL;
file_id = ls_id = 0;
}
ShareClient::~ShareClient()
{
if (file) {
fclose(file);
file = NULL;
}
if (tcpSession)
tcpSession->destroy();
}
void ShareClient::destroy()
{
tcpSession = NULL;
delete this;
}
void ShareClient::onClose()
{
if (file) {
fclose(file);
file = NULL;
}
}
void ShareClient::listDirectory(const char *dir)
{
OutPacket out;
out << ++ls_id << dir;
tcpSession->sendPacket(CMD_LS, out.data, out.getLength());
}
bool ShareClient::downloadFile(const char *dst, const char *src)
{
assert(file == NULL);
file = fopen(dst, "wb");
if (file == NULL)
return false;
OutPacket out;
out << ++file_id << src;
tcpSession->sendPacket(CMD_RETR_FILE, out.data, out.getLength());
return true;
}
void ShareClient::cancelFile()
{
if (!file)
return;
OutPacket out;
out << file_id;
tcpSession->sendPacket(CMD_FILE_FINISHED, out.data, out.getLength());
fclose(file);
file = NULL;
file_id = 0;
}
void ShareClient::onReceive(uint16 cmd, const char *data, int n)
{
InPacket in(data, n);
switch (cmd) {
case CMD_FILE_DATA:
onFileData(in);
break;
case CMD_LS:
onListDir(in);
break;
case CMD_LS_FINISHED:
onListDirFinished(in);
break;
case CMD_FILE_FINISHED:
onFileFinished(in);
break;
}
}
void ShareClient::onListDir(InPacket &in)
{
uint32 id;
in >> id;
if (ls_id != id)
return;
std::list<ENTRY_INFO> entries;
while (true) {
ENTRY_INFO info;
in >> info.name >> info.isDir >> info.size;
if (info.name.empty())
break;
entries.push_back(info);
};
onListDir(entries);
}
void ShareClient::onListDirFinished(InPacket &in)
{
uint32 id;
in >> id;
if (ls_id != id)
return;
onListDirFinished();
}
void ShareClient::onFileData(InPacket &in)
{
uint32 id;
in >> id;
if (!file || file_id != id)
return;
int n;
const char *data = in.readData(n);
if (data && fwrite(data, n, 1, file) == 1)
onFileProgress(n);
}
void ShareClient::onFileFinished(InPacket &in)
{
uint32 id;
in >> id;
if (!file || file_id != id)
return;
fclose(file);
file = NULL;
file_id = 0;
onFileFinished();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -