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

Connect by non-blocking socket

This commit is contained in:
Sergey Yagovtsev 2016-09-27 23:00:49 +03:00
parent 4a05cafab4
commit fd9a172b9b
3 changed files with 136 additions and 97 deletions

View File

@ -52,7 +52,8 @@ public:
CONNECTION_STATE_UNKNOWN,
CONNECTION_STATE_SUCCESS,
CONNECTION_STATE_DISCONNECTED
CONNECTION_STATE_DISCONNECTED,
CONNECTION_STATE_IN_PROGRESS
};
private:

View File

@ -131,17 +131,17 @@ EasyMainWindow::EasyMainWindow() : Parent()
toolbar->addWidget(new QLabel(" IP:"));
m_ipEdit = new QLineEdit();
m_ipEdit->setMaximumWidth(100);
QRegExp rx("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
m_ipEdit->setInputMask("000.000.000.000;");
m_ipEdit->setValidator(new QRegExpValidator(rx, m_ipEdit));
m_ipEdit->setText("127.0.0.1");
m_ipEdit->setFixedWidth(m_ipEdit->fontMetrics().width(QString("255.255.255.255")) + 20);
toolbar->addWidget(m_ipEdit);
toolbar->addWidget(new QLabel(" Port:"));
m_portEdit = new QLineEdit();
m_portEdit->setMaximumWidth(80);
m_portEdit->setValidator(new QIntValidator(1024, 65536, m_portEdit));
m_portEdit->setValidator(new QIntValidator(1, 65535, m_portEdit));
m_portEdit->setText(QString::number(::profiler::DEFAULT_PORT));
toolbar->addWidget(m_portEdit);

View File

@ -19,6 +19,7 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#include "profiler/easy_socket.h"
#include <string.h>
#include <thread>
#ifdef _WIN32
#pragma comment (lib, "Ws2_32.lib")
@ -80,6 +81,8 @@ void EasySocket::flush()
#ifdef _WIN32
m_socket = 0;
m_replySocket = 0;
#else
wsaret = 0;
#endif
}
@ -98,10 +101,12 @@ void EasySocket::checkResult(int result)
error_code = WSAGetLastError();
const int CONNECTION_ABORTED = WSAECONNABORTED;
const int CONNECTION_RESET = WSAECONNRESET;
const int CONNECTION_IN_PROGRESS = WSAEINPROGRESS;
#else
error_code = errno;
const int CONNECTION_ABORTED = ECONNABORTED;
const int CONNECTION_RESET = ECONNRESET;
const int CONNECTION_IN_PROGRESS = EINPROGRESS;
#endif
switch(error_code)
@ -110,6 +115,10 @@ void EasySocket::checkResult(int result)
case CONNECTION_RESET:
m_state = CONNECTION_STATE_DISCONNECTED;
break;
case CONNECTION_IN_PROGRESS:
m_state = CONNECTION_STATE_IN_PROGRESS;
break;
default:
break;
}
@ -132,7 +141,9 @@ void EasySocket::init()
return;
setBlocking(m_socket,true);
#ifndef _WIN32
wsaret = 1;
#endif
int opt = 1;
setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
}
@ -240,8 +251,35 @@ int EasySocket::connect()
return -1;
//fprintf(stderr,"ERROR, no such host\n");
}
int res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
setBlocking(m_socket,false);
int counter = 0;
int sleepMs = 20;
int waitSec = 1;
int waitMs = waitSec*1000/sleepMs;
int res = 0;
while(counter++ < waitMs)
{
res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
checkResult(res);
if (res == 0)
break;
if (m_state == CONNECTION_STATE_IN_PROGRESS)
{
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs));
continue;
}
if(m_state != CONNECTION_STATE_IN_PROGRESS && m_state != CONNECTION_STATE_SUCCESS )
break;
}
setBlocking(m_socket,true);
if(res == 0){
struct timeval tv;