1 #include "tcpnetwork.h"
3 #include "../structures/header.h"
5 using namespace network;
6 using namespace enc_hash;
7 using namespace structures;
11 void tcpSocket::disconnectFromHostImplementation(){
13 QTcpSocket::disconnectFromHost();
15 bool tcpSocket::check_canceled(){
17 this->setErrorString(tr("The process has been canceled by user."));
18 emit this->error(QAbstractSocket::UnknownSocketError);
23 void tcpSocket::error_occured(const QAbstractSocket::SocketError err){
25 qDebug()<<"Error("<<this<<")"<<this->errorString();
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;}
33 /* Threaded TCP socket implementation. */
34 QString threadedTcpSocket::senderName(){
36 this->locks[threadedTcpSocket::SenderName].lockForRead();;
37 senderName=this->_senderName;
38 this->locks[threadedTcpSocket::SenderName].unlock();
41 quint64 threadedTcpSocket::buffersize(){
43 this->locks[threadedTcpSocket::BufferSize].lockForRead();
44 size=this->_buffersize;
45 this->locks[threadedTcpSocket::BufferSize].unlock();
48 AddressAndPort threadedTcpSocket::peerAddr(){
49 this->locks[threadedTcpSocket::AddrAndPort].lockForRead();
50 AddressAndPort r=this->_addrPort;
51 this->locks[threadedTcpSocket::AddrAndPort].unlock();
54 void threadedTcpSocket::to(const AddressAndPort &to){
55 this->locks[threadedTcpSocket::AddrAndPort].lockForWrite();
57 this->locks[threadedTcpSocket::AddrAndPort].unlock();
60 int threadedTcpSocket::socketDescriptor(){
62 this->locks[threadedTcpSocket::Descriptor].lockForRead();
63 desc=this->_descriptor;
64 this->locks[threadedTcpSocket::Descriptor].unlock();
68 void threadedTcpSocket::senderName(const QString &name){
69 this->locks[threadedTcpSocket::SenderName].lockForWrite();
70 this->_senderName=name;
71 this->locks[threadedTcpSocket::SenderName].unlock();
73 void threadedTcpSocket::buffersize(const quint64 size){
74 this->locks[threadedTcpSocket::BufferSize].lockForWrite();;
75 this->_buffersize=size;
76 this->locks[threadedTcpSocket::BufferSize].unlock();
78 void threadedTcpSocket::socketDescriptor(const int descriptor){
79 this->locks[threadedTcpSocket::Descriptor].lockForWrite();
80 this->_descriptor=descriptor;
81 this->locks[threadedTcpSocket::Descriptor].unlock();
83 QIODevice::OpenMode threadedTcpSocket::readWriteMode(){
84 QIODevice::OpenMode mode;
85 this->locks[threadedTcpSocket::RWMode].lockForRead();
87 this->locks[threadedTcpSocket::RWMode].unlock();
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();
95 header threadedTcpSocket::head_data(){
96 this->locks[threadedTcpSocket::HeaderData].lockForRead();
97 header head=this->_header;
98 this->locks[threadedTcpSocket::HeaderData].unlock();
102 void threadedTcpSocket::run(){
104 qDebug()<<"threadedTcpSocket has been created and running.";
106 qRegisterMetaType<QAbstractSocket::SocketState>("SocketState");
107 qRegisterMetaType<QAbstractSocket::SocketError>("SocketError");
109 this->locks[threadedTcpSocket::BufferSize].lockForRead();
110 this->locks[threadedTcpSocket::SenderName].lockForRead();
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();
118 connect(socket,SIGNAL(connected()),SLOT(host_connected()),Qt::BlockingQueuedConnection);
119 connect(socket,SIGNAL(connected()),SIGNAL(connected()));
121 connect(socket,SIGNAL(disconnected()),SLOT(host_disconnected()));
123 connect(socket,SIGNAL(disconnected()),SIGNAL(disconnected()));
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)));
132 this->locks[threadedTcpSocket::Mode].lockForRead();
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();
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()));
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();
160 this->locks[threadedTcpSocket::Mode].unlock();
162 socket->disconnectFromHost();
169 void threadedTcpSocket::host_connected(){
170 tcpSocket *socket=qobject_cast<tcpSocket *>(this->sender());
172 qDebug()<<"Client:"<<socket->peerAddress().toString()<<"has been connected properly.";
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();
180 this->locks[threadedTcpSocket::Msg].lockForRead();
181 (*socket)<<this->_msg;
182 this->locks[threadedTcpSocket::Msg].unlock();
184 this->locks[threadedTcpSocket::Msg].unlock();
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();