The source repository about simple p2p messenger, which can be run on various systems.
リビジョン | 6393daebbd38f9f978e032065131e58c86cdeceb (tree) |
---|---|
日時 | 2010-10-09 17:39:14 |
作者 | Hiroaki Yamamoto <admin@hyso...> |
コミッター | Hiroaki Yamamoto |
Fixed server and client bugs.
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.
@@ -4,7 +4,7 @@ using namespace network; | ||
4 | 4 | using namespace structures; |
5 | 5 | tcpClient::tcpClient(quint64 buffersize,const QString &senderName,QObject *parent):QTcpSocket(parent){ |
6 | 6 | this->buffer_size=buffersize; |
7 | - this->senderName=&senderName; | |
7 | + this->senderName=senderName; | |
8 | 8 | this->timeout_time=TIMEOUT; |
9 | 9 | this->want_disconnect=false; |
10 | 10 | connect(this,SIGNAL(readyRead()),SLOT(data_available())); |
@@ -12,10 +12,10 @@ tcpClient::tcpClient(quint64 buffersize,const QString &senderName,QObject *paren | ||
12 | 12 | quint64 tcpClient::timeout()const {return this->timeout_time;} |
13 | 13 | void tcpClient::timeout(const quint64 time){this->timeout_time=time;} |
14 | 14 | tcpClient &tcpClient::operator<<(const QString &msg){ |
15 | - if(!this->check_connection_and_wait_connected())return (*this); | |
16 | - header head_data(*(this->senderName),msg); | |
15 | + if(this->state()!=QAbstractSocket::ConnectedState) return (*this); | |
16 | + header head_data(this->senderName,msg); | |
17 | 17 | QByteArray head_buffer; |
18 | - QDataStream datastream(head_buffer); | |
18 | + QDataStream datastream(&head_buffer,QIODevice::WriteOnly); | |
19 | 19 | datastream<<head_data; |
20 | 20 | quint16 size=(quint16)head_buffer.size(); |
21 | 21 | this->write((char*)&size,sizeof(__typeof__(size))/sizeof(char)); |
@@ -24,14 +24,16 @@ tcpClient &tcpClient::operator<<(const QString &msg){ | ||
24 | 24 | if(!this->check_byte_written_and_wait_written())return (*this); |
25 | 25 | QByteArray array=msg.toUtf8(); |
26 | 26 | QBuffer memoryStream(&array,this); |
27 | - while(this->write(memoryStream.read(this->buffer_size))>0); | |
28 | - if(this->check_byte_written_and_wait_written())return (*this); | |
27 | + /*while(this->write(memoryStream.read(this->buffer_size))>0) | |
28 | + if(!this->check_byte_written_and_wait_written())return (*this);*/ | |
29 | + this->write(array); | |
30 | + if(!this->check_byte_written_and_wait_written())return (*this); | |
29 | 31 | emit this->sentData(); |
30 | 32 | return (*this); |
31 | 33 | } |
32 | 34 | tcpClient &tcpClient::operator<<(QFile &file){ |
33 | - if(!this->check_connection_and_wait_connected()){return (*this);} | |
34 | - header head_data(*(this->senderName),QFileInfo(file)); | |
35 | + if(!this->state()!=QAbstractSocket::ConnectedState)return (*this); | |
36 | + header head_data(this->senderName,QFileInfo(file)); | |
35 | 37 | QByteArray head_buffer; |
36 | 38 | QDataStream datastream(head_buffer); |
37 | 39 | quint16 size=(quint16)head_buffer.size(); |
@@ -40,33 +42,19 @@ tcpClient &tcpClient::operator<<(QFile &file){ | ||
40 | 42 | if(!this->check_byte_written_and_wait_written())return (*this); |
41 | 43 | this->write(head_buffer); |
42 | 44 | if(!this->check_byte_written_and_wait_written())return (*this); |
43 | - while(this->write(file.read(this->buffer_size))>0); | |
44 | - if(!this->check_byte_written_and_wait_written())return (*this); | |
45 | + while(this->write(file.read(this->buffer_size))>0) | |
46 | + if(!this->check_byte_written_and_wait_written())return (*this); | |
45 | 47 | emit this->sentData(); |
46 | 48 | return (*this); |
47 | 49 | } |
48 | 50 | bool tcpClient::check_byte_written_and_wait_written(){ |
49 | - if(this->want_disconnect) return false; | |
51 | + this->flush(); | |
52 | + /*if(this->want_disconnect) return false; | |
50 | 53 | if(!this->waitForBytesWritten(this->timeout_time)){ |
51 | 54 | this->setErrorString(tr("The data couldn't be written.")); |
52 | 55 | emit this->error(QAbstractSocket::UnknownSocketError); |
53 | 56 | return false; |
54 | - } | |
55 | - return true; | |
56 | -} | |
57 | - | |
58 | -bool tcpClient::check_connection_and_wait_connected(){ | |
59 | - if(this->want_disconnect) return false; | |
60 | - if(this->state()!=QAbstractSocket::ConnectingState||this->state()!=QAbstractSocket::ConnectedState){ | |
61 | - this->setErrorString(tr("This socket haven't connected to somewhere yet.")); | |
62 | - emit this->error(QAbstractSocket::SocketAccessError); | |
63 | - return false; | |
64 | - } | |
65 | - if(!this->waitForConnected(this->timeout_time)){ | |
66 | - this->setErrorString(tr("Connection time out")); | |
67 | - emit this->error(QAbstractSocket::SocketTimeoutError); | |
68 | - return false; | |
69 | - } | |
57 | + }*/ | |
70 | 58 | return true; |
71 | 59 | } |
72 | 60 | bool tcpClient::check_and_wait_byte_available(){ |
@@ -3,7 +3,7 @@ | ||
3 | 3 | //By default, timeout (i.e. connection timeout,sending timeout, and receiving timeout) is 10000 ms. which is 10 sec. |
4 | 4 | #define TIMEOUT 10000 |
5 | 5 | namespace network{ |
6 | - class tcpClient:virtual public QTcpSocket{ | |
6 | + class tcpClient:public QTcpSocket{ | |
7 | 7 | Q_OBJECT |
8 | 8 | public: |
9 | 9 | tcpClient(quint64 buffersize,const QString &senderName,QObject *parent=NULL); |
@@ -19,10 +19,9 @@ namespace network{ | ||
19 | 19 | void data_available(); |
20 | 20 | private: |
21 | 21 | quint64 buffer_size,timeout_time; |
22 | - bool check_connection_and_wait_connected(), | |
23 | - check_byte_written_and_wait_written(), | |
22 | + bool check_byte_written_and_wait_written(), | |
24 | 23 | check_and_wait_byte_available(); |
25 | 24 | bool want_disconnect; |
26 | - const QString *senderName; | |
25 | + QString senderName; | |
27 | 26 | }; |
28 | 27 | } |
@@ -15,23 +15,24 @@ void tcpServer::incomingConnection(int handle){ | ||
15 | 15 | delete socket; |
16 | 16 | return; |
17 | 17 | } |
18 | - | |
19 | - if(emit this->pending(*socket)) | |
20 | - emit this->newConnection(); | |
21 | - else socket->disconnectFromHost(); | |
18 | + emit (emit this->pending(*socket))?this->newConnection():socket->disconnectFromHost(); | |
22 | 19 | } |
23 | 20 | |
24 | 21 | serverSocket::serverSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){ |
25 | 22 | this->buffer_size=buffersize; |
26 | 23 | this->canceled=false; |
27 | - connect(this,SIGNAL(disconnected()),SLOT(deleteLataer())); | |
24 | + this->event=serverSocket::headsize; | |
25 | + connect(this,SIGNAL(disconnected()),SLOT(deleteLater())); | |
28 | 26 | connect(this,SIGNAL(readyRead()),SLOT(read_data())); |
29 | 27 | } |
30 | 28 | void serverSocket::read_data(){ |
31 | - switch(data){ | |
29 | + while(this->bytesAvailable()>0){ | |
30 | + qDebug()<<"Server Event Mode:"<<this->event<<"Server Available bytes:"<<this->bytesAvailable(); | |
31 | + switch(this->event){ | |
32 | 32 | case serverSocket::headsize:this->size_event(); break; |
33 | 33 | case serverSocket::header: this->header_event(); break; |
34 | 34 | case serverSocket::data: this->data_event(); break; |
35 | + } | |
35 | 36 | } |
36 | 37 | } |
37 | 38 | void serverSocket::size_event(){ |
@@ -66,7 +67,7 @@ void serverSocket::header_event(){ | ||
66 | 67 | this->event=serverSocket::data; |
67 | 68 | } |
68 | 69 | void serverSocket::data_event(){ |
69 | - if(this->bytesAvailable()<this->buffer_size) return; | |
70 | + if(this->bytesAvailable()<this->header_data().datasize()) return; | |
70 | 71 | |
71 | 72 | quint64 final_readsize=this->head_data.datasize()%this->buffer_size, |
72 | 73 | read_count=(this->head_data.datasize()-final_readsize)/this->buffer_size; |
@@ -33,7 +33,7 @@ namespace network{ | ||
33 | 33 | QString file_pending(const serverSocket &socket) const; |
34 | 34 | QString fileStream_openFailed(const serverSocket &,const QFile::FileError &,const QString &) const; |
35 | 35 | |
36 | - void receive_aborted(const serverSocket &,const aborted_reason &); | |
36 | + void receive_aborted(const serverSocket &,const serverSocket::aborted_reason &); | |
37 | 37 | void msg_received(const serverSocket &socket,const QString &) const; |
38 | 38 | void data_broken(const serverSocket &socket) const; |
39 | 39 |
@@ -78,13 +78,28 @@ bool mainWindow::isInMember(const AddressAndPort &peer,bool matchIPOnly){ | ||
78 | 78 | //Client behavior |
79 | 79 | void mainWindow::on_sendButton_clicked(){ |
80 | 80 | QList<AddressAndPort> addressList=this->memberList->addressPortList(); |
81 | - QVector<tcpClient *> clients(addressList.size()); | |
82 | - foreach(tcpClient *client,clients) client=new tcpClient(default_buffer_size,this->setting.name(),this); | |
83 | 81 | #ifdef _OMP |
84 | 82 | #pragma omp parallel for |
85 | 83 | #endif |
86 | - for(int index=0;index<addressList.size();index++) (*clients[index])<<this->sendTextEditor->html(); | |
84 | + for(int index=0;index<addressList.size();index++){ | |
85 | + tcpClient *client=new tcpClient(default_buffer_size,this->setting.name(),this); | |
86 | + connect(client,SIGNAL(connected()),SLOT(tcpclient_connected())); | |
87 | + connect(client,SIGNAL(error(const QAbstractSocket::SocketError &)),SLOT(tcpclient_error(const QAbstractSocket::SocketError &))); | |
88 | + client->connectToHost(addressList[index].first,addressList[index].second); | |
89 | + } | |
90 | +} | |
91 | +void mainWindow::tcpclient_error(const QAbstractSocket::SocketError &error){ | |
92 | + tcpClient *client=qobject_cast<tcpClient *>(this->sender()); | |
93 | + qDebug()<<"Error:"<<client->errorString(); | |
94 | +} | |
95 | + | |
96 | +void mainWindow::tcpclient_connected(){ | |
97 | + tcpClient *client=qobject_cast<tcpClient *>(this->sender()); | |
98 | + (*client)<<this->sendTextEditor->html(); | |
99 | + client->disconnectFromHost(); | |
100 | + client->close(); | |
87 | 101 | } |
102 | + | |
88 | 103 | void mainWindow::on_sendFileAction_triggered(){ |
89 | 104 | //TODO:Send files |
90 | 105 | } |
@@ -281,10 +296,17 @@ bool mainWindow::tcpserver_pending(const serverSocket &socket){ | ||
281 | 296 | connect(&socket, |
282 | 297 | SIGNAL(msg_received(const serverSocket &,const QString &)), |
283 | 298 | SLOT(tcpserver_msg_received(const serverSocket &,const QString &))); |
299 | + connect(&socket,SIGNAL(receive_aborted(const serverSocket &,const serverSocket::aborted_reason &)), | |
300 | + SLOT(tcpserver_receive_aborted(const serverSocket &,const serverSocket::aborted_reason &))); | |
284 | 301 | return true; |
285 | 302 | } |
303 | +void mainWindow::tcpserver_receive_aborted(const serverSocket &socket,const serverSocket::aborted_reason &reason){ | |
304 | + Q_UNUSED(socket); | |
305 | + qDebug()<<"Receive aborted:"<<reason; | |
306 | +} | |
307 | + | |
286 | 308 | void mainWindow::tcpserver_msg_received(const serverSocket &socket,const QString &msg){ |
287 | - this->receiveText->append(socket.header_data().senderName()+tr(" says:")+"<br />"); | |
309 | + this->receiveText->append(socket.header_data().senderName()+"("+socket.peerAddress().toString()+")"+tr(" says:")); | |
288 | 310 | this->receiveText->append(msg); |
289 | 311 | this->receiveText->append("<br />"); |
290 | 312 | } |
@@ -46,9 +46,12 @@ private slots: | ||
46 | 46 | void invalidLink(const QString &); |
47 | 47 | void selectedLink(const QUrl &); |
48 | 48 | //These functions are for tcpclient. |
49 | + void tcpclient_connected(); | |
50 | + void tcpclient_error(const QAbstractSocket::SocketError &error); | |
49 | 51 | //These functions are for tcpserver. |
50 | 52 | bool tcpserver_pending(const serverSocket &); |
51 | 53 | void tcpserver_msg_received(const serverSocket &,const QString &); |
54 | + void tcpserver_receive_aborted(const serverSocket &,const serverSocket::aborted_reason &); | |
52 | 55 | public slots: |
53 | 56 | //These are for opening stuff. |
54 | 57 | void openConfig(const QString &); |