0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-27 08:41:02 +08:00

Fix disconnect problem on downloading

This commit is contained in:
Sergey Yagovtsev 2016-09-16 22:51:15 +03:00
parent 82079f43da
commit c316734e8f
3 changed files with 49 additions and 9 deletions

View File

@ -39,6 +39,15 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
class PROFILER_API EasySocket
{
public:
enum ConnectionState
{
CONNECTION_STATE_UNKNOWN,
CONNECTION_STATE_SUCCESS,
CONNECTION_STATE_DISCONNECTED
};
private:
#ifdef _WIN32
typedef SOCKET socket_t;
#else
@ -58,7 +67,7 @@ class PROFILER_API EasySocket
#endif
ConnectionState m_state = CONNECTION_STATE_UNKNOWN;
public:
EasySocket();
~EasySocket();
@ -71,6 +80,9 @@ public:
bool setAddress(const char* serv, uint16_t port);
int connect();
void setState(ConnectionState state){m_state=state;}
ConnectionState state() const{return m_state;}
};
#endif // EASY________SOCKET_________H

View File

@ -335,10 +335,16 @@ void EasyMainWindow::listen()
int bytes = 0;
static auto timeBegin = std::chrono::system_clock::now();
while (true)
bool isListen = true;
while (isListen)
{
if ((bytes - seek) == 0){
bytes = m_easySocket.receive(buffer, buffer_size);
if(bytes == -1)
{
isListen = false;
continue;
}
seek = 0;
}
@ -399,7 +405,8 @@ void EasyMainWindow::listen()
m_receivedProfileData.clear();
//loadFile(QString(tempfilename.c_str()));
m_recFrames = false;
return;
isListen = false;
continue;
}
break;
case profiler::net::MESSAGE_TYPE_REPLY_BLOCKS:
@ -424,6 +431,14 @@ void EasyMainWindow::listen()
while (neededSize > 0)
{
bytes = m_easySocket.receive(buffer, buffer_size);
if(bytes == -1)
{
isListen = false;
neededSize = 0;
continue;
}
buf = &buffer[0];
int toWrite = std::min(bytes, neededSize);
m_receivedProfileData.write(buf, toWrite);
@ -446,6 +461,8 @@ void EasyMainWindow::listen()
}
}
m_easySocket.setState(EasySocket::CONNECTION_STATE_DISCONNECTED);
delete [] buffer;
}
void TcpReceiverThread::run()
@ -623,6 +640,8 @@ void EasyMainWindow::onCollapseAllClicked(bool)
void EasyMainWindow::onConnectClicked(bool)
{
if(m_isConnected)
return;
int res = m_easySocket.setAddress(m_hostString->text().toStdString().c_str(), m_portString->text().toUShort());
res = m_easySocket.connect();
@ -721,6 +740,13 @@ void EasyMainWindow::onCaptureClicked(bool)
m_downloading = false;
if(m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED)
{
QMessageBox::warning(this,"Warning" ,"Application was disconnected",QMessageBox::Close);
m_isConnected = false;
return;
}
std::string tempfilename = "test_rec.prof";
loadFile(QString(tempfilename.c_str()));
/*m_isConnected = (m_server->state() == QTcpSocket::ConnectedState);

View File

@ -36,12 +36,6 @@ int EasySocket::bind(uint16_t portno)
EasySocket::EasySocket()
{
m_socket = socket(AF_INET, SOCK_STREAM, 0);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
//setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
}
EasySocket::~EasySocket()
@ -98,6 +92,14 @@ int EasySocket::connect()
}
int res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
if(res == 0){
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
m_replySocket = m_socket;
}
return res;