OSDN Git Service

Fixed server and client bugs.
authorHiroaki Yamamoto <admin@hysoftware.net>
Sat, 9 Oct 2010 08:39:14 +0000 (17:39 +0900)
committerHiroaki Yamamoto <admin@hysoftware.net>
Sat, 9 Oct 2010 08:39:14 +0000 (17:39 +0900)
Sending message, server refused it because the message to be sent was not raw data. Moreover, The server event didn't work properly.
I think I fixed this bug, however, to fix and make good code will be needed.

client/tcpclient.cxx
client/tcpclient.h
server/tcpserver.cxx
server/tcpserver.h
ui/jasmine_mainwindow.cxx
ui/jasmine_mainwindow.h

index 5e1a2d4..ffce102 100644 (file)
@@ -4,7 +4,7 @@ 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->senderName=senderName;
        this->timeout_time=TIMEOUT;
        this->want_disconnect=false;
        connect(this,SIGNAL(readyRead()),SLOT(data_available()));
@@ -12,10 +12,10 @@ tcpClient::tcpClient(quint64 buffersize,const QString &senderName,QObject *paren
 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->check_connection_and_wait_connected())return (*this);
-       header head_data(*(this->senderName),msg);
+       if(this->state()!=QAbstractSocket::ConnectedState) return (*this);
+       header head_data(this->senderName,msg);
        QByteArray head_buffer;
-       QDataStream datastream(head_buffer);
+       QDataStream datastream(&head_buffer,QIODevice::WriteOnly);
        datastream<<head_data;
        quint16 size=(quint16)head_buffer.size();
        this->write((char*)&size,sizeof(__typeof__(size))/sizeof(char));
@@ -24,14 +24,16 @@ tcpClient &tcpClient::operator<<(const QString &msg){
        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);
+       /*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->check_connection_and_wait_connected()){return (*this);}
-       header head_data(*(this->senderName),QFileInfo(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();
@@ -40,33 +42,19 @@ tcpClient &tcpClient::operator<<(QFile &file){
        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);
+       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(){
-       if(this->want_disconnect) return false;
+       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_connection_and_wait_connected(){
-       if(this->want_disconnect) return false;
-       if(this->state()!=QAbstractSocket::ConnectingState||this->state()!=QAbstractSocket::ConnectedState){
-               this->setErrorString(tr("This socket haven't connected to somewhere yet."));
-               emit this->error(QAbstractSocket::SocketAccessError);
-               return false;
-       }
-       if(!this->waitForConnected(this->timeout_time)){
-               this->setErrorString(tr("Connection time out"));
-               emit this->error(QAbstractSocket::SocketTimeoutError);
-               return false;
-       }
+       }*/
        return true;
 }
 bool tcpClient::check_and_wait_byte_available(){
index f0ce1b9..5225141 100644 (file)
@@ -3,7 +3,7 @@
 //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:virtual public QTcpSocket{
+       class tcpClient:public QTcpSocket{
                Q_OBJECT
        public:
                tcpClient(quint64 buffersize,const QString &senderName,QObject *parent=NULL);
@@ -19,10 +19,9 @@ namespace network{
                void data_available();
        private:
                quint64 buffer_size,timeout_time;
-               bool check_connection_and_wait_connected(),
-                        check_byte_written_and_wait_written(),
+               bool check_byte_written_and_wait_written(),
                         check_and_wait_byte_available();
                bool want_disconnect;
-               const QString *senderName;
+               QString senderName;
        };
 }
index f04e216..4956577 100644 (file)
@@ -15,23 +15,24 @@ void tcpServer::incomingConnection(int handle){
                delete socket;
                return;
        }
-
-       if(emit this->pending(*socket))
-               emit this->newConnection();
-       else socket->disconnectFromHost();
+       emit (emit this->pending(*socket))?this->newConnection():socket->disconnectFromHost();
 }
 
 serverSocket::serverSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){
        this->buffer_size=buffersize;
        this->canceled=false;
-       connect(this,SIGNAL(disconnected()),SLOT(deleteLataer()));
+       this->event=serverSocket::headsize;
+       connect(this,SIGNAL(disconnected()),SLOT(deleteLater()));
        connect(this,SIGNAL(readyRead()),SLOT(read_data()));
 }
 void serverSocket::read_data(){
-       switch(data){
+       while(this->bytesAvailable()>0){
+               qDebug()<<"Server Event Mode:"<<this->event<<"Server Available bytes:"<<this->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(){
@@ -66,7 +67,7 @@ void serverSocket::header_event(){
        this->event=serverSocket::data;
 }
 void serverSocket::data_event(){
-       if(this->bytesAvailable()<this->buffer_size) return;
+       if(this->bytesAvailable()<this->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;
index 606cc4a..d217de3 100644 (file)
@@ -33,7 +33,7 @@ namespace network{
                QString file_pending(const serverSocket &socket) const;
                QString fileStream_openFailed(const serverSocket &,const QFile::FileError &,const QString &) const;
 
-               void receive_aborted(const serverSocket &,const aborted_reason &);
+               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;
 
index f10bb49..298dcca 100644 (file)
@@ -78,13 +78,28 @@ bool mainWindow::isInMember(const AddressAndPort &peer,bool matchIPOnly){
 //Client behavior
 void mainWindow::on_sendButton_clicked(){
        QList<AddressAndPort> addressList=this->memberList->addressPortList();
-       QVector<tcpClient *> clients(addressList.size());
-       foreach(tcpClient *client,clients) client=new tcpClient(default_buffer_size,this->setting.name(),this);
 #ifdef _OMP
 #pragma omp parallel for
 #endif
-       for(int index=0;index<addressList.size();index++) (*clients[index])<<this->sendTextEditor->html();
+       for(int index=0;index<addressList.size();index++){
+               tcpClient *client=new tcpClient(default_buffer_size,this->setting.name(),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<tcpClient *>(this->sender());
+       qDebug()<<"Error:"<<client->errorString();
+}
+
+void mainWindow::tcpclient_connected(){
+       tcpClient *client=qobject_cast<tcpClient *>(this->sender());
+       (*client)<<this->sendTextEditor->html();
+       client->disconnectFromHost();
+       client->close();
 }
+
 void mainWindow::on_sendFileAction_triggered(){
        //TODO:Send files
 }
@@ -281,10 +296,17 @@ bool mainWindow::tcpserver_pending(const serverSocket &socket){
        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 &)));
        return true;
 }
+void mainWindow::tcpserver_receive_aborted(const serverSocket &socket,const serverSocket::aborted_reason &reason){
+       Q_UNUSED(socket);
+       qDebug()<<"Receive aborted:"<<reason;
+}
+
 void mainWindow::tcpserver_msg_received(const serverSocket &socket,const QString &msg){
-       this->receiveText->append(socket.header_data().senderName()+tr(" says:")+"<br />");
+       this->receiveText->append(socket.header_data().senderName()+"("+socket.peerAddress().toString()+")"+tr(" says:"));
        this->receiveText->append(msg);
        this->receiveText->append("<br />");
 }
index 37d9e86..3563716 100644 (file)
@@ -46,9 +46,12 @@ private slots:
        void invalidLink(const QString &);
        void selectedLink(const QUrl &);
        //These functions are for tcpclient.
+       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 &);
 public slots:
        //These are for opening stuff.
        void openConfig(const QString &);