git-svn-id: svn://db.shs.com.ru/pip@53 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#include "pip.h"
|
|
#include "pifiletransfer.h"
|
|
#include "pidatatransfer.h"
|
|
|
|
|
|
class UDPFileTransfer: public PITimer {
|
|
PIOBJECT_SUBCLASS(UDPFileTransfer, PITimer)
|
|
public:
|
|
UDPFileTransfer(const PIString &src_ip_port, const PIString &dst_ip_port) {
|
|
eth.setReadAddress(src_ip_port);
|
|
eth.setSendAddress(dst_ip_port);
|
|
//ft.setPacketSize(65000);
|
|
CONNECTU(&ft, sendRequest, this, ftsend);
|
|
CONNECTU(&ft, sendFilesStarted, this, ftevent);
|
|
CONNECTU(&ft, receiveFilesStarted, this, ftevent);
|
|
CONNECTU(&ft, sendFilesFinished, this, ftevent);
|
|
CONNECTU(&ft, receiveFilesFinished, this, ftevent);
|
|
CONNECTU(ð, threadedReadEvent, this, received);
|
|
start(50);
|
|
eth.setParameter(PIEthernet::SeparateSockets);
|
|
eth.startThreadedRead();
|
|
}
|
|
|
|
void startSend(const PIString &file) {
|
|
ft.send(file);
|
|
}
|
|
|
|
PIFileTransfer ft;
|
|
|
|
private:
|
|
PIEthernet eth;
|
|
|
|
void tick(void *, int) {
|
|
if (ft.isSending() || ft.isReceiving()) ftevent();
|
|
}
|
|
|
|
EVENT_HANDLER(void, ftevent) {
|
|
piCout << ft.stateString()
|
|
<< "(" << PIString::readableSize(ft.bytesFileCur()) << "/" << PIString::readableSize(ft.bytesFileAll()) << ", "
|
|
<< PIString::readableSize(ft.bytesCur()) << "/" << PIString::readableSize(ft.bytesAll()) << ")";
|
|
}
|
|
|
|
EVENT_HANDLER1(void, ftsend, PIByteArray &, data) {
|
|
eth.send(data);
|
|
}
|
|
|
|
EVENT_HANDLER2(void, received, uchar * , readed, int, size) {
|
|
PIByteArray ba(readed, size);
|
|
ft.received(ba);
|
|
}
|
|
};
|
|
|
|
int main (int argc, char * argv[]) {
|
|
if (!(argc == 3 || argc == 4)) {
|
|
piCout << "UDP File Transfer";
|
|
piCout << "USE: pift src_ip:port dst_ip:port [filename]";
|
|
return 0;
|
|
}
|
|
PIKbdListener kbd;
|
|
kbd.enableExitCapture();
|
|
PIString src = argv[1];
|
|
PIString dst = argv[2];
|
|
UDPFileTransfer f(src, dst);
|
|
piCout << "work directory" << f.ft.directory().absolutePath() << ", listen on" << src << ", send to" << dst;
|
|
if (argc == 4) {
|
|
PIString file = argv[3];
|
|
piCout << "send file" << file;
|
|
f.startSend(file);
|
|
return 0;
|
|
} else {
|
|
piCout << "wait for receiving";
|
|
}
|
|
WAIT_FOR_EXIT
|
|
return 0;
|
|
|
|
}
|
|
|