⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shareserver.cpp

📁 张勇的linQ学习P2P及IM软件的极佳素材代码
💻 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 "shareserver.h"

using namespace std;


ShareServer::ShareServer(TCPSessionBase *tcp)
{
	tcpSession = tcp;
	file = NULL;
	file_id = 0;
}

ShareServer::~ShareServer()
{
	if (file) {
		fclose(file);
		file = NULL;
	}
	if (tcpSession)
		tcpSession->destroy();
}

void ShareServer::destroy()
{
	tcpSession = NULL;
	delete this;
}

void ShareServer::onClose()
{
	if (file) {
		fclose(file);
		file = NULL;
	}
}

void ShareServer::onSend()
{
	if (!file) {
		tcpSession->enableWrite(false);
		return;
	}

	char buf[960];
	int n = fread(buf, 1, sizeof(buf), file);

	if (n > 0) {
		OutPacket out;
		out << file_id;
		out.writeData(buf, n);

		tcpSession->sendPacket(CMD_FILE_DATA, out.data, out.getLength());
	}

	if (n < sizeof(buf)) {
		OutPacket out;
		out << file_id;
		tcpSession->sendPacket(CMD_FILE_FINISHED, out.data, out.getLength());
		tcpSession->enableWrite(false);

		fclose(file);
		file = NULL;
		file_id = 0;
	}
}

void ShareServer::onReceive(uint16 cmd, const char *data, int n)
{
	InPacket in(data, n);

	switch (cmd) {
	case CMD_LS:
		onListDir(in);
		break;

	case CMD_RETR_FILE:
		onRetrFile(in);
		break;

	case CMD_FILE_FINISHED:
		onFileCanceled(in);
		break;
	}
}

void ShareServer::onListDir(InPacket &in)
{
	const char *dir;
	uint32 id;
	in >> id >> dir;

	std::list<ENTRY_INFO> entries;
	readDirectory(entries, dir);

	OutPacket out;
	out << id;
	int n = 0;

	std::list<ENTRY_INFO>::iterator it;
	for (it = entries.begin(); it != entries.end(); ++it) {
		ENTRY_INFO &info = *it;
		if (info.name == "." || info.name == "..")
			continue;

		OutPacket p;
		p << info.name << info.isDir << info.size;

		if (out.getBytesLeft() < p.getLength()) {
			tcpSession->sendPacket(CMD_LS, out.data, out.getLength());
			out.reset();
			out << id;
			n = 0;
		}
		out << p;
		n++;
	}

	if (n > 0)
		tcpSession->sendPacket(CMD_LS, out.data, out.getLength());

	out.reset();
	out << id;
	tcpSession->sendPacket(CMD_LS_FINISHED, out.data, out.getLength());
}

void ShareServer::onRetrFile(InPacket &in)
{
	if (file)
		return;

	const char *name;
	uint32 id;
	in >> id >> name;

	string path;
	if (!getPathName(path, name))
		return;

	file = fopen(path.c_str(), "rb");
	if (!file)
		return;

	file_id = id;
	tcpSession->enableWrite(true);
}

void ShareServer::onFileCanceled(InPacket &in)
{
	uint32 id;
	in >> id;
	if (file_id != id)
		return;

	if (file) {
		fclose(file);
		file = NULL;
		file_id = 0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -