OSDN Git Service

Please ignore this commit.
[greensite/jasmine.git] / network / tcpnetwork.cxx
1 #include "tcpnetwork.h"
2 #include <cstring>
3 #include "../structures/header.h"
4
5 using namespace network;
6 using namespace enc_hash;
7 using namespace structures;
8 using namespace std;
9
10
11 void tcpSocket::disconnectFromHostImplementation(){
12     this->canceled=true;
13     QTcpSocket::disconnectFromHost();
14 }
15 bool tcpSocket::check_canceled(){
16     if(this->canceled){
17         this->setErrorString(tr("The process has been canceled by user."));
18         emit this->error(QAbstractSocket::UnknownSocketError);
19         return true;
20     }
21     return false;
22 }
23 void tcpSocket::error_occured(const QAbstractSocket::SocketError err){
24     Q_UNUSED(err)
25     qDebug()<<"Error("<<this<<")"<<this->errorString();
26     this->abort();
27
28 }
29 void tcpSocket::cancel(){this->canceled=true;}
30 QString tcpSocket::path_to_save() const{return this->where_to_save;}
31 header tcpSocket::header_data() const{return this->head_data;}
32
33 /* Threaded TCP socket implementation. */
34 QString threadedTcpSocket::senderName(){
35     QString senderName;
36     this->locks[threadedTcpSocket::SenderName].lockForRead();;
37         senderName=this->_senderName;
38     this->locks[threadedTcpSocket::SenderName].unlock();
39     return senderName;
40 }
41 quint64 threadedTcpSocket::buffersize(){
42     quint64 size;
43     this->locks[threadedTcpSocket::BufferSize].lockForRead();
44         size=this->_buffersize;
45     this->locks[threadedTcpSocket::BufferSize].unlock();
46     return size;
47 }
48 AddressAndPort threadedTcpSocket::peerAddr(){
49     this->locks[threadedTcpSocket::AddrAndPort].lockForRead();
50     AddressAndPort r=this->_addrPort;
51     this->locks[threadedTcpSocket::AddrAndPort].unlock();
52     return r;
53 }
54 void threadedTcpSocket::to(const AddressAndPort &to){
55     this->locks[threadedTcpSocket::AddrAndPort].lockForWrite();
56         this->_addrPort=to;
57     this->locks[threadedTcpSocket::AddrAndPort].unlock();
58 }
59
60 int threadedTcpSocket::socketDescriptor(){
61     int desc;
62     this->locks[threadedTcpSocket::Descriptor].lockForRead();
63         desc=this->_descriptor;
64     this->locks[threadedTcpSocket::Descriptor].unlock();
65     return desc;
66 }
67
68 void threadedTcpSocket::senderName(const QString &name){
69     this->locks[threadedTcpSocket::SenderName].lockForWrite();
70         this->_senderName=name;
71     this->locks[threadedTcpSocket::SenderName].unlock();
72 }
73 void threadedTcpSocket::buffersize(const quint64 size){
74     this->locks[threadedTcpSocket::BufferSize].lockForWrite();;
75         this->_buffersize=size;
76     this->locks[threadedTcpSocket::BufferSize].unlock();
77 }
78 void threadedTcpSocket::socketDescriptor(const int descriptor){
79     this->locks[threadedTcpSocket::Descriptor].lockForWrite();
80         this->_descriptor=descriptor;
81     this->locks[threadedTcpSocket::Descriptor].unlock();
82 }
83 QIODevice::OpenMode threadedTcpSocket::readWriteMode(){
84     QIODevice::OpenMode mode;
85     this->locks[threadedTcpSocket::RWMode].lockForRead();
86         mode=this->open_mode;
87     this->locks[threadedTcpSocket::RWMode].unlock();
88     return mode;
89 }
90 void threadedTcpSocket::readWriteMode(const QIODevice::OpenMode openMode){
91     this->locks[threadedTcpSocket::RWMode].lockForWrite();
92         this->open_mode=openMode;
93     this->locks[threadedTcpSocket::RWMode].unlock();
94 }
95 header threadedTcpSocket::head_data(){
96     this->locks[threadedTcpSocket::HeaderData].lockForRead();
97         header head=this->_header;
98     this->locks[threadedTcpSocket::HeaderData].unlock();
99     return head;
100 }
101
102 void threadedTcpSocket::run(){
103 #ifdef DEBUG
104     qDebug()<<"threadedTcpSocket has been created and running.";
105 #endif
106     qRegisterMetaType<QAbstractSocket::SocketState>("SocketState");
107     qRegisterMetaType<QAbstractSocket::SocketError>("SocketError");
108
109     this->locks[threadedTcpSocket::BufferSize].lockForRead();
110     this->locks[threadedTcpSocket::SenderName].lockForRead();
111     tcpSocket *socket=
112             (this->mode==threadedTcpSocket::Client)?
113             new tcpSocket(this->_senderName,this->_buffersize):
114             new tcpSocket(this->_buffersize);
115     this->locks[threadedTcpSocket::BufferSize].unlock();
116     this->locks[threadedTcpSocket::SenderName].unlock();
117     /*Common signals*/
118     connect(socket,SIGNAL(connected()),SLOT(host_connected()),Qt::BlockingQueuedConnection);
119     connect(socket,SIGNAL(connected()),SIGNAL(connected()));
120 #ifdef DEBUG
121     connect(socket,SIGNAL(disconnected()),SLOT(host_disconnected()));
122 #endif
123     connect(socket,SIGNAL(disconnected()),SIGNAL(disconnected()));
124
125     connect(socket,SIGNAL(hostFound()),SIGNAL(hostFound()));
126     connect(socket,SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &,QAuthenticator*)),
127             SIGNAL(proxyAuthenticationRequired(const QNetworkProxy &,QAuthenticator*)));
128     connect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),
129             SIGNAL(stateChanged(QAbstractSocket::SocketState)));
130
131     int descriptor;
132     this->locks[threadedTcpSocket::Mode].lockForRead();
133     switch(this->mode){
134     case threadedTcpSocket::Client:
135         connect(socket,SIGNAL(sentData()),SIGNAL(sentData()));
136         connect(socket,SIGNAL(sentData()),SLOT(quit()));
137         connect(socket,SIGNAL(file_header_sent()),SIGNAL(file_header_sent()));
138         connect(socket,SIGNAL(sending_file_progress(quint64)),SIGNAL(sending_file_progress(quint64)));
139         this->locks[threadedTcpSocket::AddrAndPort].lockForRead();
140             socket->connectToHost(this->_addrPort.first,this->_addrPort.second);
141         this->locks[threadedTcpSocket::AddrAndPort].unlock();
142         break;
143     case threadedTcpSocket::Session:
144         this->locks[threadedTcpSocket::Descriptor].lockForRead();
145             descriptor=this->_descriptor;
146         this->locks[threadedTcpSocket::Descriptor].unlock();
147         connect(socket,SIGNAL(msg_received(const QString &)),SIGNAL(msg_received(const QString &)));
148         connect(socket,SIGNAL(msg_received(const QString &)),SLOT(quit()));
149
150         connect(socket,SIGNAL(file_receive_progress(const quint64)),SIGNAL(file_receive_progress(const quint64)));
151         connect(socket,SIGNAL(file_saved()),SIGNAL(file_saved()));
152         connect(socket,SIGNAL(file_saved()),SLOT(quit()));
153         connect(socket,SIGNAL(header_received()),SLOT(header_received()));
154         socket->setSocketDescriptor(descriptor);
155         this->_addrPort.first=socket->peerAddress();
156         this->_addrPort.second=socket->peerPort();
157         if(!emit this->pending()) socket->disconnectFromHost();
158         break;
159     }
160     this->locks[threadedTcpSocket::Mode].unlock();
161     if(this->exec()==0){
162         socket->disconnectFromHost();
163         return;
164     }
165     //Cleanup
166     socket->abort();
167 }
168
169 void threadedTcpSocket::host_connected(){
170     tcpSocket *socket=qobject_cast<tcpSocket *>(this->sender());
171 #ifdef DEBUG
172     qDebug()<<"Client:"<<socket->peerAddress().toString()<<"has been connected properly.";
173 #endif
174     this->locks[threadedTcpSocket::Msg].lockForRead();
175     if(this->_msg.isNull()){
176         this->locks[threadedTcpSocket::File].lockForRead();
177         (*socket)<<this->_file;
178         this->locks[threadedTcpSocket::File].unlock();
179     }else{
180         this->locks[threadedTcpSocket::Msg].lockForRead();
181         (*socket)<<this->_msg;
182         this->locks[threadedTcpSocket::Msg].unlock();
183     }
184     this->locks[threadedTcpSocket::Msg].unlock();
185 }
186
187 #ifdef DEBUG
188 void threadedTcpSocket::host_disconnected(){
189     this->locks[threadedTcpSocket::Mode].lockForRead();
190     qDebug()<<"Socket("<<((this->mode==threadedTcpSocket::Session)?"Session":"Client")<<"):Connection closed successful.";
191     this->locks[threadedTcpSocket::Mode].unlock();
192 }
193 #endif