git-svn-id: svn://db.shs.com.ru/pip@4 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
138
src/io/pifiletransfer.h
Normal file
138
src/io/pifiletransfer.h
Normal file
@@ -0,0 +1,138 @@
|
||||
#ifndef PIFILETRANSFER_H
|
||||
#define PIFILETRANSFER_H
|
||||
|
||||
#include "pidir.h"
|
||||
#include "picrc.h"
|
||||
|
||||
#define PIFILETRANSFER_VERSION 1
|
||||
|
||||
class PIFileTransfer : public PIObject
|
||||
{
|
||||
PIOBJECT(PIFileTransfer)
|
||||
public:
|
||||
PIFileTransfer();
|
||||
~PIFileTransfer();
|
||||
|
||||
enum ReplyCode {Unknown = 0, Success, Invalid, Break};
|
||||
enum PacketType {pt_Data = 1, pt_Reply, pt_SendRequest};//, pt_SendResult};
|
||||
|
||||
|
||||
struct PacketHeader {
|
||||
union {
|
||||
struct {
|
||||
char sig[3]; // PFT
|
||||
uchar version;
|
||||
};
|
||||
uint raw_sig;
|
||||
};
|
||||
int type; // PacketType
|
||||
uint session_id;
|
||||
uint id;
|
||||
bool check_sig() {
|
||||
if (sig[0] != sign[0] || sig[1] != sign[1] || sig[2] != sign[2] || version != PIFILETRANSFER_VERSION) return false;
|
||||
return true;
|
||||
}
|
||||
// uint crc;
|
||||
};
|
||||
|
||||
struct PartHeader {
|
||||
uint id;
|
||||
uint total_size;
|
||||
uint part_start;
|
||||
uint part_size;
|
||||
};
|
||||
|
||||
struct EntryInfo {
|
||||
EntryInfo() {
|
||||
fstart = fsize = 0;
|
||||
parts = part_index = 0;
|
||||
}
|
||||
PIFile::FileInfo entry;
|
||||
llong fstart;
|
||||
llong fsize;
|
||||
uint parts;
|
||||
uint part_index;
|
||||
};
|
||||
|
||||
//bool send(const PIFile & file);
|
||||
bool send(const PIString & file);
|
||||
// bool send(const PIStringList &files);
|
||||
bool send(PIFile::FileInfo entry) {return send(PIVector<PIFile::FileInfo>() << entry);}
|
||||
bool send(PIVector<PIFile::FileInfo> entries);
|
||||
|
||||
void stopSend();
|
||||
void stopReceive();
|
||||
|
||||
bool isSending() const {return is_sending;}
|
||||
bool isReceiving() const {return is_receiving;}
|
||||
|
||||
void setPacketSize(int size) {max_packet_size = size;}
|
||||
int packetSize() const {return max_packet_size;}
|
||||
|
||||
void setTimeout(double sec) {timeout_ = sec;}
|
||||
double timeout() const {return timeout_;}
|
||||
|
||||
void setDirectory(const PIDir &d) {dir = d;}
|
||||
void setDirectory(const PIString &path) {dir.setDir(path);}
|
||||
PIDir directory() const {return dir;}
|
||||
|
||||
const PIString & stateString() const {return state_string;}
|
||||
llong bytesTotalAll() const {return bytes_total_all;}
|
||||
llong bytesTotalCur() const {return bytes_total_cur;}
|
||||
llong bytesFileAll() const {return bytes_file_all;}
|
||||
llong bytesFileCur() const {return bytes_file_cur;}
|
||||
const PIString * stateString_ptr() const {return &state_string;}
|
||||
const llong * bytesTotalAll_ptr() const {return &bytes_total_all;}
|
||||
const llong * bytesTotalCur_ptr() const {return &bytes_total_cur;}
|
||||
const llong * bytesFileAll_ptr() const {return &bytes_file_all;}
|
||||
const llong * bytesFileCur_ptr() const {return &bytes_file_cur;}
|
||||
|
||||
EVENT(startReceive)
|
||||
EVENT1(receiveRequest, bool *, ok)
|
||||
EVENT1(finishReceive, bool, ok)
|
||||
EVENT(startSend)
|
||||
EVENT1(finishSend, bool, ok)
|
||||
|
||||
EVENT1(sendRequest, PIByteArray &, data)
|
||||
EVENT_HANDLER1(void, received, PIByteArray &, data);
|
||||
|
||||
private:
|
||||
static const char sign[];
|
||||
int max_packet_size, min_packet_size, fileinfo_size;
|
||||
bool break_, is_sending, is_receiving;
|
||||
double timeout_;
|
||||
|
||||
PIVector<PIVector<EntryInfo> > session;
|
||||
PIVector<ReplyCode> replies;
|
||||
PIString state_string;
|
||||
llong bytes_total_all, bytes_total_cur, bytes_file_all, bytes_file_cur;
|
||||
PacketHeader header;
|
||||
PIDir dir;
|
||||
PIFile work_file;
|
||||
CRC_16 crc;
|
||||
|
||||
bool sendFiles(PIVector<PIFile::FileInfo> files);
|
||||
int checkSession();
|
||||
void buildSession(PIVector<PIFile::FileInfo> files);
|
||||
void sendBreak(int session_id);
|
||||
void sendReply(ReplyCode reply);
|
||||
bool getSendRequest();
|
||||
void processData(int id, PIByteArray &data);
|
||||
PIByteArray buildPacket(int id);
|
||||
bool finish_send(bool ok);
|
||||
void finish_receive(bool ok);
|
||||
};
|
||||
|
||||
inline PIByteArray & operator <<(PIByteArray & s, const PIFileTransfer::PacketHeader & v) {s << v.raw_sig << v.type << v.session_id << v.id; return s;}
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PIFileTransfer::PacketHeader & v) {s >> v.raw_sig >> v.type >> v.session_id >> v.id; return s;}
|
||||
|
||||
inline PIByteArray & operator <<(PIByteArray & s, const PIFileTransfer::PartHeader & v) {s << v.id << v.total_size << v.part_start << v.part_size; return s;}
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PIFileTransfer::PartHeader & v) {s >> v.id >> v.total_size >> v.part_start >> v.part_size; return s;}
|
||||
|
||||
inline PIByteArray & operator <<(PIByteArray & s, const PIFileTransfer::EntryInfo & v) {s << v.entry.path << v.entry.size << v.entry.time_modification << v.entry.time_access << int(v.entry.flags)
|
||||
<< v.entry.id_user << v.entry.id_group << v.fstart; return s;}
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PIFileTransfer::EntryInfo & v) {s >> v.entry.path >> v.entry.size >> v.entry.time_modification >> v.entry.time_access >> (int&)(v.entry.flags)
|
||||
>> v.entry.id_user >> v.entry.id_group >> v.fstart; return s;}
|
||||
inline PICout operator <<(PICout s, const PIFileTransfer::EntryInfo & v) {s.setControl(0, true); s << "FileInfo(\"" << v.entry.path << "\", " << PIString::fromNumber(v.entry.flags, 16) << PIString::readableSize(v.entry.size) << " b | " << PIString::readableSize(v.fsize) << " b)"; s.restoreControl(); return s;}
|
||||
|
||||
#endif // PIFILETRANSFER_H
|
||||
Reference in New Issue
Block a user