• R/O
  • HTTP
  • SSH
  • HTTPS

jasmine: コミット

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.

変更サマリ

差分

--- a/client/tcpclient.cxx
+++ b/client/tcpclient.cxx
@@ -4,7 +4,7 @@ using namespace network;
44 using namespace structures;
55 tcpClient::tcpClient(quint64 buffersize,const QString &senderName,QObject *parent):QTcpSocket(parent){
66 this->buffer_size=buffersize;
7- this->senderName=&senderName;
7+ this->senderName=senderName;
88 this->timeout_time=TIMEOUT;
99 this->want_disconnect=false;
1010 connect(this,SIGNAL(readyRead()),SLOT(data_available()));
@@ -12,10 +12,10 @@ tcpClient::tcpClient(quint64 buffersize,const QString &senderName,QObject *paren
1212 quint64 tcpClient::timeout()const {return this->timeout_time;}
1313 void tcpClient::timeout(const quint64 time){this->timeout_time=time;}
1414 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);
1717 QByteArray head_buffer;
18- QDataStream datastream(head_buffer);
18+ QDataStream datastream(&head_buffer,QIODevice::WriteOnly);
1919 datastream<<head_data;
2020 quint16 size=(quint16)head_buffer.size();
2121 this->write((char*)&size,sizeof(__typeof__(size))/sizeof(char));
@@ -24,14 +24,16 @@ tcpClient &tcpClient::operator<<(const QString &msg){
2424 if(!this->check_byte_written_and_wait_written())return (*this);
2525 QByteArray array=msg.toUtf8();
2626 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);
2931 emit this->sentData();
3032 return (*this);
3133 }
3234 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));
3537 QByteArray head_buffer;
3638 QDataStream datastream(head_buffer);
3739 quint16 size=(quint16)head_buffer.size();
@@ -40,33 +42,19 @@ tcpClient &tcpClient::operator<<(QFile &file){
4042 if(!this->check_byte_written_and_wait_written())return (*this);
4143 this->write(head_buffer);
4244 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);
4547 emit this->sentData();
4648 return (*this);
4749 }
4850 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;
5053 if(!this->waitForBytesWritten(this->timeout_time)){
5154 this->setErrorString(tr("The data couldn't be written."));
5255 emit this->error(QAbstractSocket::UnknownSocketError);
5356 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+ }*/
7058 return true;
7159 }
7260 bool tcpClient::check_and_wait_byte_available(){
--- a/client/tcpclient.h
+++ b/client/tcpclient.h
@@ -3,7 +3,7 @@
33 //By default, timeout (i.e. connection timeout,sending timeout, and receiving timeout) is 10000 ms. which is 10 sec.
44 #define TIMEOUT 10000
55 namespace network{
6- class tcpClient:virtual public QTcpSocket{
6+ class tcpClient:public QTcpSocket{
77 Q_OBJECT
88 public:
99 tcpClient(quint64 buffersize,const QString &senderName,QObject *parent=NULL);
@@ -19,10 +19,9 @@ namespace network{
1919 void data_available();
2020 private:
2121 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(),
2423 check_and_wait_byte_available();
2524 bool want_disconnect;
26- const QString *senderName;
25+ QString senderName;
2726 };
2827 }
--- a/server/tcpserver.cxx
+++ b/server/tcpserver.cxx
@@ -15,23 +15,24 @@ void tcpServer::incomingConnection(int handle){
1515 delete socket;
1616 return;
1717 }
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();
2219 }
2320
2421 serverSocket::serverSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){
2522 this->buffer_size=buffersize;
2623 this->canceled=false;
27- connect(this,SIGNAL(disconnected()),SLOT(deleteLataer()));
24+ this->event=serverSocket::headsize;
25+ connect(this,SIGNAL(disconnected()),SLOT(deleteLater()));
2826 connect(this,SIGNAL(readyRead()),SLOT(read_data()));
2927 }
3028 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){
3232 case serverSocket::headsize:this->size_event(); break;
3333 case serverSocket::header: this->header_event(); break;
3434 case serverSocket::data: this->data_event(); break;
35+ }
3536 }
3637 }
3738 void serverSocket::size_event(){
@@ -66,7 +67,7 @@ void serverSocket::header_event(){
6667 this->event=serverSocket::data;
6768 }
6869 void serverSocket::data_event(){
69- if(this->bytesAvailable()<this->buffer_size) return;
70+ if(this->bytesAvailable()<this->header_data().datasize()) return;
7071
7172 quint64 final_readsize=this->head_data.datasize()%this->buffer_size,
7273 read_count=(this->head_data.datasize()-final_readsize)/this->buffer_size;
--- a/server/tcpserver.h
+++ b/server/tcpserver.h
@@ -33,7 +33,7 @@ namespace network{
3333 QString file_pending(const serverSocket &socket) const;
3434 QString fileStream_openFailed(const serverSocket &,const QFile::FileError &,const QString &) const;
3535
36- void receive_aborted(const serverSocket &,const aborted_reason &);
36+ void receive_aborted(const serverSocket &,const serverSocket::aborted_reason &);
3737 void msg_received(const serverSocket &socket,const QString &) const;
3838 void data_broken(const serverSocket &socket) const;
3939
--- a/ui/jasmine_mainwindow.cxx
+++ b/ui/jasmine_mainwindow.cxx
@@ -78,13 +78,28 @@ bool mainWindow::isInMember(const AddressAndPort &peer,bool matchIPOnly){
7878 //Client behavior
7979 void mainWindow::on_sendButton_clicked(){
8080 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);
8381 #ifdef _OMP
8482 #pragma omp parallel for
8583 #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();
87101 }
102+
88103 void mainWindow::on_sendFileAction_triggered(){
89104 //TODO:Send files
90105 }
@@ -281,10 +296,17 @@ bool mainWindow::tcpserver_pending(const serverSocket &socket){
281296 connect(&socket,
282297 SIGNAL(msg_received(const serverSocket &,const QString &)),
283298 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 &)));
284301 return true;
285302 }
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+
286308 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:"));
288310 this->receiveText->append(msg);
289311 this->receiveText->append("<br />");
290312 }
--- a/ui/jasmine_mainwindow.h
+++ b/ui/jasmine_mainwindow.h
@@ -46,9 +46,12 @@ private slots:
4646 void invalidLink(const QString &);
4747 void selectedLink(const QUrl &);
4848 //These functions are for tcpclient.
49+ void tcpclient_connected();
50+ void tcpclient_error(const QAbstractSocket::SocketError &error);
4951 //These functions are for tcpserver.
5052 bool tcpserver_pending(const serverSocket &);
5153 void tcpserver_msg_received(const serverSocket &,const QString &);
54+ void tcpserver_receive_aborted(const serverSocket &,const serverSocket::aborted_reason &);
5255 public slots:
5356 //These are for opening stuff.
5457 void openConfig(const QString &);
旧リポジトリブラウザで表示