From 30d6478760705769ae09213d077af3aa1aa2e9b0 Mon Sep 17 00:00:00 2001 From: Hiroaki Yamamoto Date: Sun, 10 Oct 2010 14:55:46 +0900 Subject: [PATCH] Merged tcpClient into tcpSocket. Until now, tcpSocket was a session socket for tcpServer, and tcpClient exists aside from that. If tcpClient continues to exist, the size of libnetwork may get larger than merged one. --- CMakeLists.txt | 27 ++++--- client/tcpclient.cxx | 84 -------------------- client/tcpclient.h | 27 ------- jasmine.pro | 9 +-- network/tcpnetwork.cxx | 194 ++++++++++++++++++++++++++++++++++++++++++++++ network/tcpnetwork.h | 64 +++++++++++++++ server/tcpserver.cxx | 130 ------------------------------- server/tcpserver.h | 60 -------------- ui/editdelegate.cxx | 2 +- ui/jasmine_mainwindow.cxx | 34 ++++---- ui/jasmine_mainwindow.h | 9 +-- ui/memberlist.cxx | 5 +- 12 files changed, 300 insertions(+), 345 deletions(-) delete mode 100644 client/tcpclient.cxx delete mode 100644 client/tcpclient.h create mode 100644 network/tcpnetwork.cxx create mode 100644 network/tcpnetwork.h delete mode 100644 server/tcpserver.cxx delete mode 100644 server/tcpserver.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 874a6a5..88109f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,9 +23,8 @@ set(structures ) #Mocs -set(serverclient_h - server/tcpserver.h - client/tcpclient.h +set(network_h + network/tcpnetwork.h ) set(ui_h ui/about.h @@ -44,9 +43,8 @@ set(ui ) #The implementation files -set(serverclient_src - server/tcpserver.cxx - client/tcpclient.cxx +set(network_src + network/tcpnetwork.cxx ) set(ui_src ui/about.cxx @@ -62,7 +60,7 @@ set(res res/main.res ) -qt4_wrap_cpp(serverclient_moc ${serverclient_h}) +qt4_wrap_cpp(network_moc ${network_h}) qt4_wrap_cpp(ui_moc ${ui_h}) qt4_wrap_ui(ui_moc ${ui}) @@ -70,15 +68,15 @@ qt4_add_resources(res_moc ${res}) if(WIN32) if(OPENMP_FOUND) add_executable(jasmine WIN32 - ${main} ${ui_moc} ${ui_src} ${serverclient_moc} - ${serverclient_src} ${structures} ${res_moc} + ${main} ${ui_moc} ${ui_src} ${network_moc} + ${network_src} ${structures} ${res_moc} ${validator} ${ported_rmd6}) set_property(TARGET jasmine PROPERTY COMPLILE_FLAGS ${OpenMP_CXX_FLAGS} APPEND) target_link_libraries(jasmine gomp ${QT_LIBRARIES}) else(OPENMP_FOUND) add_executable(jasmine WIN32 - ${main} ${ui_moc} ${ui_src} ${serverclient_moc} - ${serverclient_src} ${structures} ${res_moc} + ${main} ${ui_moc} ${ui_src} ${network_moc} + ${network_src} ${structures} ${res_moc} ${validator} ${ported_rmd6}) target_link_libraries(jasmine ${QT_LIBRARIES}) endif(OPENMP_FOUND) @@ -88,7 +86,7 @@ else(WIN32) add_library(resource SHARED ${res_moc}) add_library(structures SHARED ${structures}) - add_library(serverclient SHARED ${serverclient_moc} ${serverclient_src}) + add_library(network SHARED ${network_moc} ${network_src}) add_library(ui SHARED ${ui_moc} ${ui_src}) add_executable(jasmine ${main}) @@ -99,12 +97,13 @@ else(WIN32) target_link_libraries(structures ${QT_LIBRARIES}) target_link_libraries(structures qripemd160 ${QT_LIBRARIES}) - target_link_libraries(serverclient structures validator ${QT_LIBRARIES}) - target_link_libraries(ui resource serverclient ${QT_LIBRARIES}) + target_link_libraries(network structures validator ${QT_LIBRARIES}) + target_link_libraries(ui resource network ${QT_LIBRARIES}) target_link_libraries(jasmine ui ${QT_LIBRARIES}) if(OPENMP_FOUND) set_property(TARGET ui PROPERTY COMPILE_FLAGS ${OpenMP_CXX_FLAGS} APPEND) + #set_property(TARGET network PROPERTY COMPLE_FLAGS ${OpenMP_CXX_FLAGS} APPEND) target_link_libraries(ui gomp) endif(OPENMP_FOUND) endif(WIN32) diff --git a/client/tcpclient.cxx b/client/tcpclient.cxx deleted file mode 100644 index ffce102..0000000 --- a/client/tcpclient.cxx +++ /dev/null @@ -1,84 +0,0 @@ -#include "tcpclient.h" -#include "../structures/header.h" -using namespace network; -using namespace structures; -tcpClient::tcpClient(quint64 buffersize,const QString &senderName,QObject *parent):QTcpSocket(parent){ - this->buffer_size=buffersize; - this->senderName=senderName; - this->timeout_time=TIMEOUT; - this->want_disconnect=false; - connect(this,SIGNAL(readyRead()),SLOT(data_available())); -} -quint64 tcpClient::timeout()const {return this->timeout_time;} -void tcpClient::timeout(const quint64 time){this->timeout_time=time;} -tcpClient &tcpClient::operator<<(const QString &msg){ - if(this->state()!=QAbstractSocket::ConnectedState) return (*this); - header head_data(this->senderName,msg); - QByteArray head_buffer; - QDataStream datastream(&head_buffer,QIODevice::WriteOnly); - datastream<write((char*)&size,sizeof(__typeof__(size))/sizeof(char)); - if(!this->check_byte_written_and_wait_written())return (*this); - this->write(head_buffer); - if(!this->check_byte_written_and_wait_written())return (*this); - QByteArray array=msg.toUtf8(); - QBuffer memoryStream(&array,this); - /*while(this->write(memoryStream.read(this->buffer_size))>0) - if(!this->check_byte_written_and_wait_written())return (*this);*/ - this->write(array); - if(!this->check_byte_written_and_wait_written())return (*this); - emit this->sentData(); - return (*this); -} -tcpClient &tcpClient::operator<<(QFile &file){ - if(!this->state()!=QAbstractSocket::ConnectedState)return (*this); - header head_data(this->senderName,QFileInfo(file)); - QByteArray head_buffer; - QDataStream datastream(head_buffer); - quint16 size=(quint16)head_buffer.size(); - datastream<write((char*)&size,sizeof(__typeof__(size))/sizeof(char)); - if(!this->check_byte_written_and_wait_written())return (*this); - this->write(head_buffer); - if(!this->check_byte_written_and_wait_written())return (*this); - while(this->write(file.read(this->buffer_size))>0) - if(!this->check_byte_written_and_wait_written())return (*this); - emit this->sentData(); - return (*this); -} -bool tcpClient::check_byte_written_and_wait_written(){ - this->flush(); - /*if(this->want_disconnect) return false; - if(!this->waitForBytesWritten(this->timeout_time)){ - this->setErrorString(tr("The data couldn't be written.")); - emit this->error(QAbstractSocket::UnknownSocketError); - return false; - }*/ - return true; -} -bool tcpClient::check_and_wait_byte_available(){ - if(this->want_disconnect) return false; - if(!this->waitForReadyRead(this->timeout_time)){ - this->setErrorString(tr("The data couldn't be read.")); - emit this->error(QAbstractSocket::UnknownSocketError); - return false; - } - return true; -} -void tcpClient::disconnectFromHostImplementation(){ - this->want_disconnect=true; - QTcpSocket::disconnectFromHost(); -} - -void tcpClient::data_available(){ - this->check_and_wait_byte_available(); - QByteArray receive_data=this->read(1); - switch(receive_data[0]){ - case 0x00: - this->disconnectFromHost(); - break; - default: - return; - } -} diff --git a/client/tcpclient.h b/client/tcpclient.h deleted file mode 100644 index 5225141..0000000 --- a/client/tcpclient.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include -//By default, timeout (i.e. connection timeout,sending timeout, and receiving timeout) is 10000 ms. which is 10 sec. -#define TIMEOUT 10000 -namespace network{ - class tcpClient:public QTcpSocket{ - Q_OBJECT - public: - tcpClient(quint64 buffersize,const QString &senderName,QObject *parent=NULL); - tcpClient &operator<<(const QString &), - &operator<<(QFile &); - quint64 timeout() const; - void timeout(const quint64); - protected: - void disconnectFromHostImplementation(); - signals: - void sentData(); - private slots: - void data_available(); - private: - quint64 buffer_size,timeout_time; - bool check_byte_written_and_wait_written(), - check_and_wait_byte_available(); - bool want_disconnect; - QString senderName; - }; -} diff --git a/jasmine.pro b/jasmine.pro index 505f3fd..f2553e6 100644 --- a/jasmine.pro +++ b/jasmine.pro @@ -4,8 +4,7 @@ DEPENDPATH += . \ ui \ ui/uifiles \ res \ - client \ - server \ + network \ ported_rmd6 \ INCLUDEPATH \ += \ @@ -23,8 +22,7 @@ HEADERS += ported_rmd6/basis.h \ ui/rtfeditor.h \ validator/ipaddressvalidator.h \ structures/header.h \ - server/tcpserver.h \ - client/tcpclient.h \ + network/tcpnetwork.h \ app_version.h \ settings.h \ definition.h @@ -46,7 +44,6 @@ SOURCES += ported_rmd6/basis.cxx \ ui/rtfeditor.cxx \ validator/ipaddressvalidator.cxx \ structures/header.cxx \ - server/tcpserver.cxx \ - client/tcpclient.cxx \ + network/tcpnetwork.cxx \ settings.cxx \ main.cxx diff --git a/network/tcpnetwork.cxx b/network/tcpnetwork.cxx new file mode 100644 index 0000000..c667778 --- /dev/null +++ b/network/tcpnetwork.cxx @@ -0,0 +1,194 @@ +#include "tcpnetwork.h" +#include +#include "../structures/header.h" + +using namespace network; +using namespace enc_hash; +using namespace structures; +using namespace std; +tcpServer::tcpServer(quint64 buffersize, QObject *parent):QTcpServer(parent){this->buffersize=buffersize;} +void tcpServer::incomingConnection(int handle){ + tcpSocket *socket=new tcpSocket(this->buffersize,this); + + if(!socket->setSocketDescriptor(handle)){ + emit this->socket_error(*socket); + delete socket; + return; + } + emit (emit this->pending(*socket))?this->newConnection():socket->disconnectFromHost(); +} + +tcpSocket::tcpSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){ + this->buffer_size=buffersize; + this->canceled=false; + this->event=tcpSocket::headsize; + connect(this,SIGNAL(disconnected()),SLOT(deleteLater())); + connect(this,SIGNAL(readyRead()),SLOT(read_data())); +} +tcpSocket::tcpSocket(const QString &senderName,quint64 buffersize,QObject *parent):QTcpSocket(parent){ + this->buffer_size=buffersize; + this->senderName=senderName; +} +void tcpSocket::read_data(){ + while(this->bytesAvailable()>0){ + qDebug()<<"Server Event Mode:"<event<<"Server Available bytes:"<bytesAvailable(); + switch(this->event){ + case tcpSocket::headsize: this->size_event(); break; + case tcpSocket::header_receive: this->header_event(); break; + case tcpSocket::data: this->data_event(); break; + } + } +} +void tcpSocket::size_event(){ + if(this->bytesAvailable()<2) return; + char size_buf[2]; + this->read(size_buf,sizeof(size_buf)); + memcpy(&this->header_size,size_buf, + (sizeof(quint16)>sizeof(__typeof__(size_buf)))?sizeof(quint16):sizeof(size_buf) + ); + this->event=tcpSocket::header_receive; +} +void tcpSocket::header_event(){ + if(this->bytesAvailable()header_size) return; + else{ + QByteArray data=this->read(this->header_size); + QDataStream datastream(data); + datastream>>this->head_data; + if(this->head_data==structures::header()){ + this->setErrorString(tr("The header is empty.")); + emit this->error(QAbstractSocket::UnknownSocketError); + this->disconnectFromHost(); + return; + } + if(!this->head_data.fileName().isEmpty()){ + this->where_to_save=(emit this->file_pending()); + if(this->where_to_save.isEmpty()){ + this->write(QByteArray(1,0x00)); + + this->setErrorString(tr("The filename is empty.")); + emit this->error(QAbstractSocket::UnknownSocketError); + + this->disconnectFromHost(); + return; + } + } + } + this->event=tcpSocket::data; +} +void tcpSocket::data_event(){ + if(this->bytesAvailable()header_data().datasize()) return; + + quint64 final_readsize=this->head_data.datasize()%this->buffer_size, + read_count=(this->head_data.datasize()-final_readsize)/this->buffer_size; + + if(this->head_data.fileName().isEmpty()){ + QByteArray msg; + for(quint64 count=0;countcheck_canceled_then_abort())return; + msg+=this->read(this->buffer_size); + } + if(this->check_canceled_then_abort())return; + msg+=this->read(final_readsize); + + rmd6 generator; + if(this->head_data.ripemd160()==generator.compute_hash(msg)) + emit this->msg_received(QString::fromUtf8(msg.data())); + else{ + this->setErrorString(tr("The data has been broken.")); + emit this->error(QAbstractSocket::UnknownSocketError); + this->disconnectFromHost(); + } + }else{ + streamopen: + if(this->where_to_save.isEmpty()){ + this->setErrorString(tr("The filename is empty.")); + emit this->error(QAbstractSocket::UnknownSocketError); + this->disconnectFromHost(); + return; + } + QFile file(this->where_to_save,this); + if(!file.open(QIODevice::Truncate|QIODevice::WriteOnly)){ + this->where_to_save=emit this->fileStream_openFailed(file.error(),file.errorString()); + goto streamopen; + } + for(quint64 count=0;countcheck_canceled_then_abort())return; + file.write(this->read(this->buffer_size)); + emit this->file_receive_progress(file.pos()); + } + if(this->check_canceled_then_abort())return; + file.write(this->read(final_readsize)); + emit this->file_receive_progress(file.pos()); + file.close(); + + rmd6 generator; + if(this->head_data.ripemd160()==generator.compute_hash(file)) + emit this->file_saved(); + else{ + this->setErrorString(tr("The file has been broken.")); + emit this->error(QAbstractSocket::UnknownSocketError); + this->disconnectFromHost(); + } + } + this->disconnectFromHost(); +} + +bool tcpSocket::check_canceled_then_abort(){ + if(this->canceled){ + this->setErrorString(tr("The process has been canceled by user.")); + emit this->error(QAbstractSocket::UnknownSocketError); + this->abort(); + return true; + } + return false; +} +void tcpSocket::disconnectFromHostImplementation(){ + this->canceled=true; + QTcpSocket::disconnectFromHost(); +} +tcpSocket &tcpSocket::operator<<(const QString &msg){ + if(this->state()!=QAbstractSocket::ConnectedState) return (*this); + this->head_data=header(this->senderName,msg); + QByteArray tmp_buffer; + QDataStream datastream(&tmp_buffer,QIODevice::WriteOnly); + datastream<head_data; + quint16 size=(quint16)tmp_buffer.size(); + this->write((char*)&size,sizeof(__typeof__(size))/sizeof(char)); + if(!this->flush())return (*this); + this->write(tmp_buffer); + if(!this->flush())return (*this); + tmp_buffer.clear(); + tmp_buffer=msg.toUtf8(); + QBuffer memoryStream(&tmp_buffer,this); + if(!memoryStream.open(QIODevice::ReadOnly)){ + this->setErrorString("Memory Stream couldn't open."); + emit this->error(QAbstractSocket::UnknownSocketError); + return (*this); + } + while(this->write(memoryStream.read(this->buffer_size))>0) + if(!this->flush())return (*this); + memoryStream.close(); + + emit this->sentData(); + return (*this); +} +tcpSocket &tcpSocket::operator<<(QFile &file){ + if(!this->state()!=QAbstractSocket::ConnectedState)return (*this); + this->head_data=header(this->senderName,QFileInfo(file)); + QByteArray tmp_buffer; + QDataStream datastream(tmp_buffer); + quint16 size=(quint16)tmp_buffer.size(); + datastream<head_data; + this->write((char*)&size,sizeof(__typeof__(size))/sizeof(char)); + if(!this->flush())return (*this); + this->write(tmp_buffer); + if(!this->flush())return (*this); + tmp_buffer.clear(); + while(this->write(file.read(this->buffer_size))>0) + if(!this->flush())return (*this); + emit this->sentData(); + return (*this); +} +void tcpSocket::cancel(){this->canceled=true;} +QString tcpSocket::path_to_save() const{return this->where_to_save;} +header tcpSocket::header_data() const{return this->head_data;} diff --git a/network/tcpnetwork.h b/network/tcpnetwork.h new file mode 100644 index 0000000..082bac0 --- /dev/null +++ b/network/tcpnetwork.h @@ -0,0 +1,64 @@ +#pragma once +#include +#include "../definition.h" +#include "../structures/header.h" +#include "../definition.h" +#include "../ported_rmd6/rmd6.h" + +#define default_bandwidth 0x400 +namespace network{ + class tcpSocket; + + class tcpServer:virtual public QTcpServer{ + Q_OBJECT + public: + tcpServer(quint64 buffersize=default_bandwidth,QObject *parent=NULL); + signals: + bool pending(const tcpSocket &) const; + void socket_error(const tcpSocket &) const; + protected: + void incomingConnection(int handle); + private: + quint64 buffersize; + }; + + class tcpSocket:public QTcpSocket{ + Q_OBJECT + public: + //This constructor is for server. it works as a session. + tcpSocket(quint64 buffersize=default_bandwidth,QObject *parent=NULL); + //This constructor is for client. it works as a client. + tcpSocket(const QString &senderName,quint64 buffersize=default_buffer_size,QObject *parent=NULL); + QString path_to_save() const; + structures::header header_data() const; + + tcpSocket &operator<<(const QString &), + &operator<<(QFile &); + signals: + QString file_pending() const; + QString fileStream_openFailed(const QFile::FileError &,const QString &) const; + + void msg_received(const QString &) const; + + void file_receive_progress(const quint64 streamPos) const; + void file_saved() const; + void sentData(); + private slots: + void read_data(); + void cancel(); + protected: + void disconnectFromHostImplementation(); + private: + enum mode{headsize,header_receive,data} event; + void size_event(); + void header_event(); + void data_event(); + bool check_canceled_then_abort(); + //The size of header needs to be larger than 0, and smaller than or equal to 0xFFFF. + quint16 header_size,buffer_size,timeout_time; + structures::header head_data; + bool canceled; + QString where_to_save; + QString senderName; + }; +} diff --git a/server/tcpserver.cxx b/server/tcpserver.cxx deleted file mode 100644 index 4956577..0000000 --- a/server/tcpserver.cxx +++ /dev/null @@ -1,130 +0,0 @@ -#include "tcpserver.h" -#include -#include "../structures/header.h" - -using namespace network; -using namespace enc_hash; -using namespace structures; -using namespace std; -tcpServer::tcpServer(quint64 buffersize, QObject *parent):QTcpServer(parent){this->buffersize=buffersize;} -void tcpServer::incomingConnection(int handle){ - serverSocket *socket=new serverSocket(this->buffersize,this); - - if(!socket->setSocketDescriptor(handle)){ - emit this->socket_error(*socket); - delete socket; - return; - } - emit (emit this->pending(*socket))?this->newConnection():socket->disconnectFromHost(); -} - -serverSocket::serverSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){ - this->buffer_size=buffersize; - this->canceled=false; - this->event=serverSocket::headsize; - connect(this,SIGNAL(disconnected()),SLOT(deleteLater())); - connect(this,SIGNAL(readyRead()),SLOT(read_data())); -} -void serverSocket::read_data(){ - while(this->bytesAvailable()>0){ - qDebug()<<"Server Event Mode:"<event<<"Server Available bytes:"<bytesAvailable(); - switch(this->event){ - case serverSocket::headsize:this->size_event(); break; - case serverSocket::header: this->header_event(); break; - case serverSocket::data: this->data_event(); break; - } - } -} -void serverSocket::size_event(){ - if(this->bytesAvailable()<2) return; - char size_buf[2]; - this->read(size_buf,sizeof(size_buf)); - memcpy(&this->header_size,size_buf, - (sizeof(quint16)>sizeof(char))?sizeof(quint16):sizeof(size_buf) - ); - this->event=serverSocket::header; -} -void serverSocket::header_event(){ - if(this->bytesAvailable()header_size) return; - else{ - QByteArray data=this->read(this->header_size); - QDataStream datastream(data); - datastream>>this->head_data; - if(this->head_data==structures::header()){ - this->abort_receive(serverSocket::header_invalid); - return; - } - if(!this->head_data.fileName().isEmpty()){ - this->where_to_save=(emit this->file_pending((*this))); - if(this->where_to_save.isEmpty()){ - this->write(QByteArray(1,0x00)); - emit this->receive_aborted((*this),serverSocket::empty_filename_specified); - this->disconnectFromHost(); - return; - } - } - } - this->event=serverSocket::data; -} -void serverSocket::data_event(){ - if(this->bytesAvailable()header_data().datasize()) return; - - quint64 final_readsize=this->head_data.datasize()%this->buffer_size, - read_count=(this->head_data.datasize()-final_readsize)/this->buffer_size; - - if(this->head_data.fileName().isEmpty()){ - QByteArray msg; - for(quint64 count=0;countcheck_canceled_then_abort())return; - msg+=this->read(this->buffer_size); - } - this->check_canceled_then_abort(); - msg+=this->read(final_readsize); - - rmd6 generator; - if(this->head_data.ripemd160()==generator.compute_hash(msg)) - emit this->msg_received((*this),QString::fromUtf8(msg.data())); - else emit this->data_broken((*this)); - }else{ - streamopen: - if(this->where_to_save.isEmpty()){ - this->abort_receive(serverSocket::empty_filename_specified); - return; - } - QFile file(this->where_to_save,this); - if(!file.open(QIODevice::Truncate|QIODevice::WriteOnly)){ - this->where_to_save=emit this->fileStream_openFailed((*this),file.error(),file.errorString()); - goto streamopen; - } - for(quint64 count=0;countcheck_canceled_then_abort())return; - file.write(this->read(this->buffer_size)); - emit this->file_receive_progress(file.pos(),(*this)); - } - this->check_canceled_then_abort(); - file.write(this->read(final_readsize)); - emit this->file_receive_progress(file.pos(),(*this)); - file.close(); - - rmd6 generator; - if(this->head_data.ripemd160()==generator.compute_hash(file)) - emit this->file_saved((*this)); - else emit this->file_broken((*this)); - } - this->disconnectFromHost(); -} -bool serverSocket::check_canceled_then_abort(){ - if(this->canceled){ - this->abort_receive(serverSocket::user); - return true; - } - return false; -} -void serverSocket::abort_receive(aborted_reason reason){ - this->write(QByteArray(1,0x00)); - emit this->receive_aborted((*this),reason); - this->disconnectFromHost(); -} -void serverSocket::cancel(){this->canceled=true;} -QString serverSocket::path_to_save() const{return this->where_to_save;} -header serverSocket::header_data() const{return this->head_data;} diff --git a/server/tcpserver.h b/server/tcpserver.h deleted file mode 100644 index d217de3..0000000 --- a/server/tcpserver.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once -#include -#include "../definition.h" -#include "../structures/header.h" -#include "../definition.h" -#include "../ported_rmd6/rmd6.h" - -#define default_bandwidth 0x400 -namespace network{ - class serverSocket; - - class tcpServer:virtual public QTcpServer{ - Q_OBJECT - public: - tcpServer(quint64 buffersize=default_bandwidth,QObject *parent=NULL); - signals: - bool pending(const serverSocket &) const; - void socket_error(const serverSocket &) const; - protected: - void incomingConnection(int handle); - private: - quint64 buffersize; - }; - - class serverSocket:virtual public QTcpSocket{ - Q_OBJECT - public: - serverSocket(quint64 buffersize=default_bandwidth,QObject *parent=NULL); - enum aborted_reason{header_invalid,stream_open_failed,empty_filename_specified,user}; - QString path_to_save() const; - structures::header header_data() const; - signals: - QString file_pending(const serverSocket &socket) const; - QString fileStream_openFailed(const serverSocket &,const QFile::FileError &,const QString &) const; - - void receive_aborted(const serverSocket &,const serverSocket::aborted_reason &); - void msg_received(const serverSocket &socket,const QString &) const; - void data_broken(const serverSocket &socket) const; - - void file_receive_progress(const quint64 streamPos,const serverSocket &socket) const; - void file_saved(const serverSocket &socket) const; - void file_broken(const serverSocket &socket) const; - private slots: - void read_data(); - void cancel(); - private: - enum mode{headsize,header,data} event; - void size_event(); - void header_event(); - void data_event(); - void abort_receive(aborted_reason reason); - bool check_canceled_then_abort(); - //The size of header needs to be larger than 0, and smaller than or equal to 0xFFFF. - quint16 header_size; - quint64 buffer_size; - structures::header head_data; - bool canceled; - QString where_to_save; - }; -} diff --git a/ui/editdelegate.cxx b/ui/editdelegate.cxx index 3e4e98d..1ce1c6f 100644 --- a/ui/editdelegate.cxx +++ b/ui/editdelegate.cxx @@ -3,7 +3,7 @@ MemberListEditDelegate::MemberListEditDelegate(QObject *parent): QItemDelegate(parent){} QWidget *MemberListEditDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option,const QModelIndex &index)const{ QLineEdit *editor=qobject_cast(QItemDelegate::createEditor(parent,option,index)); - editor->setValidator(new IPAddressValidator(true,editor)); + editor->setValidator(new IPAddressValidator(false,editor)); connect(editor,SIGNAL(editingFinished()),SLOT(edit_finished())); return editor; } diff --git a/ui/jasmine_mainwindow.cxx b/ui/jasmine_mainwindow.cxx index 298dcca..6741b73 100644 --- a/ui/jasmine_mainwindow.cxx +++ b/ui/jasmine_mainwindow.cxx @@ -53,7 +53,7 @@ mainWindow::mainWindow(){ connect(this->sendTextEditor,SIGNAL(invalidLink(const QString &)),SLOT(invalidLink(const QString &))); connect(this->sendTextEditor,SIGNAL(sendTriggered()),this->sendButton,SLOT(click())); - connect(this->mainServer,SIGNAL(pending(const serverSocket &)),SLOT(tcpserver_pending(const serverSocket &))); + connect(this->mainServer,SIGNAL(pending(const tcpSocket &)),SLOT(tcpserver_pending(const tcpSocket &))); } mainWindow::~mainWindow(){} void mainWindow::closeEvent(QCloseEvent *event){ @@ -82,19 +82,20 @@ void mainWindow::on_sendButton_clicked(){ #pragma omp parallel for #endif for(int index=0;indexsetting.name(),this); + tcpSocket *client=new tcpSocket(this->setting.name(),default_buffer_size,this); connect(client,SIGNAL(connected()),SLOT(tcpclient_connected())); connect(client,SIGNAL(error(const QAbstractSocket::SocketError &)),SLOT(tcpclient_error(const QAbstractSocket::SocketError &))); client->connectToHost(addressList[index].first,addressList[index].second); } } void mainWindow::tcpclient_error(const QAbstractSocket::SocketError &error){ - tcpClient *client=qobject_cast(this->sender()); + tcpSocket *client=qobject_cast(this->sender()); qDebug()<<"Error:"<errorString(); + client->close(); } void mainWindow::tcpclient_connected(){ - tcpClient *client=qobject_cast(this->sender()); + tcpSocket *client=qobject_cast(this->sender()); (*client)<sendTextEditor->html(); client->disconnectFromHost(); client->close(); @@ -291,22 +292,23 @@ void mainWindow::selectedLink(const QUrl &link){ settings mainWindow::app_setting() const{return this->setting;} //Implementations for main server. -bool mainWindow::tcpserver_pending(const serverSocket &socket){ +bool mainWindow::tcpserver_pending(const tcpSocket &socket){ if(!this->isInMember(AddressAndPort(socket.peerAddress(),0),true)) return false; connect(&socket, - SIGNAL(msg_received(const serverSocket &,const QString &)), - SLOT(tcpserver_msg_received(const serverSocket &,const QString &))); - connect(&socket,SIGNAL(receive_aborted(const serverSocket &,const serverSocket::aborted_reason &)), - SLOT(tcpserver_receive_aborted(const serverSocket &,const serverSocket::aborted_reason &))); + SIGNAL(msg_received(const QString &)), + SLOT(tcpserver_msg_received(const QString &))); + connect(&socket,SIGNAL(error(const QAbstractSocket::SocketError &)), + SLOT(tcpserver_error(const QAbstractSocket::SocketError &))); return true; } -void mainWindow::tcpserver_receive_aborted(const serverSocket &socket,const serverSocket::aborted_reason &reason){ - Q_UNUSED(socket); - qDebug()<<"Receive aborted:"<(this->sender()); + qDebug()<<"Receive aborted:"<errorString(); } -void mainWindow::tcpserver_msg_received(const serverSocket &socket,const QString &msg){ - this->receiveText->append(socket.header_data().senderName()+"("+socket.peerAddress().toString()+")"+tr(" says:")); - this->receiveText->append(msg); - this->receiveText->append("
"); +void mainWindow::tcpserver_msg_received(const QString &msg){ + tcpSocket *sender=qobject_cast(this->sender()); + this->receiveText->append(sender->header_data().senderName()+"("+sender->peerAddress().toString()+")"+tr(" says:")); + this->receiveText->append(msg+"\n"); + //this->receiveText->append("
"); } diff --git a/ui/jasmine_mainwindow.h b/ui/jasmine_mainwindow.h index 3563716..0e0bdcf 100644 --- a/ui/jasmine_mainwindow.h +++ b/ui/jasmine_mainwindow.h @@ -5,8 +5,7 @@ #include "rtfeditor.h" #include "../settings.h" #include "../validator/ipaddressvalidator.h" -#include "../server/tcpserver.h" -#include "../client/tcpclient.h" +#include "../network/tcpnetwork.h" using namespace structures; using namespace network; @@ -49,9 +48,9 @@ private slots: void tcpclient_connected(); void tcpclient_error(const QAbstractSocket::SocketError &error); //These functions are for tcpserver. - bool tcpserver_pending(const serverSocket &); - void tcpserver_msg_received(const serverSocket &,const QString &); - void tcpserver_receive_aborted(const serverSocket &,const serverSocket::aborted_reason &); + bool tcpserver_pending(const tcpSocket &); + void tcpserver_msg_received(const QString &); + void tcpserver_error(const QAbstractSocket::SocketError &); public slots: //These are for opening stuff. void openConfig(const QString &); diff --git a/ui/memberlist.cxx b/ui/memberlist.cxx index 37d49d7..acfde87 100644 --- a/ui/memberlist.cxx +++ b/ui/memberlist.cxx @@ -20,8 +20,9 @@ QList MemberList::addressPortList() const{ QList ret; for(int row=0;rowrowCount();row++){ QStringList address_port_str=this->item(row,AddressIndex)->text().split(":"); - if(address_port_str.size()<2||address_port_str[1].isEmpty()) address_port_str[1]=QString("%1").arg(default_port); - AddressAndPort address_port(QHostAddress(address_port_str[0]),address_port_str[1].isEmpty()?default_port:address_port_str[1].toUShort()); + if(address_port_str.size()<2) address_port_str<