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

Add checkResult function for socket

This commit is contained in:
Sergey Yagovtsev 2016-09-18 16:43:43 +03:00
parent c316734e8f
commit 6939f053c9
3 changed files with 53 additions and 8 deletions

View File

@ -48,6 +48,9 @@ public:
CONNECTION_STATE_DISCONNECTED CONNECTION_STATE_DISCONNECTED
}; };
private: private:
void checkResult(int result);
#ifdef _WIN32 #ifdef _WIN32
typedef SOCKET socket_t; typedef SOCKET socket_t;
#else #else

View File

@ -121,7 +121,6 @@ EasyMainWindow::EasyMainWindow() : Parent(), m_treeWidget(nullptr), m_graphicsVi
m_hostString->setInputMask("000.000.000.000;"); m_hostString->setInputMask("000.000.000.000;");
m_hostString->setValidator(&regValidator); m_hostString->setValidator(&regValidator);
m_hostString->setText("127.0.0.1"); m_hostString->setText("127.0.0.1");
//m_hostString->setText("192.224.4.109");
fileToolBar->addWidget(m_hostString); fileToolBar->addWidget(m_hostString);
@ -342,7 +341,12 @@ void EasyMainWindow::listen()
bytes = m_easySocket.receive(buffer, buffer_size); bytes = m_easySocket.receive(buffer, buffer_size);
if(bytes == -1) if(bytes == -1)
{ {
isListen = false; if(m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED)
{
isListen = false;
}
seek = 0;
bytes = 0;
continue; continue;
} }
seek = 0; seek = 0;
@ -350,6 +354,11 @@ void EasyMainWindow::listen()
char *buf = &buffer[seek]; char *buf = &buffer[seek];
if(bytes == 0){
isListen = false;
continue;
}
if (bytes > 0) if (bytes > 0)
{ {
profiler::net::Message* message = (profiler::net::Message*)buf; profiler::net::Message* message = (profiler::net::Message*)buf;
@ -434,8 +443,11 @@ void EasyMainWindow::listen()
if(bytes == -1) if(bytes == -1)
{ {
isListen = false; if(m_easySocket.state() == EasySocket::CONNECTION_STATE_DISCONNECTED)
neededSize = 0; {
isListen = false;
neededSize = 0;
}
continue; continue;
} }
@ -461,7 +473,7 @@ void EasyMainWindow::listen()
} }
} }
m_easySocket.setState(EasySocket::CONNECTION_STATE_DISCONNECTED);
delete [] buffer; delete [] buffer;
} }

View File

@ -21,6 +21,7 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#ifndef _WIN32 #ifndef _WIN32
#include <strings.h> #include <strings.h>
#include <errno.h>
int EasySocket::bind(uint16_t portno) int EasySocket::bind(uint16_t portno)
{ {
@ -45,25 +46,34 @@ EasySocket::~EasySocket()
int EasySocket::send(const void *buf, size_t nbyte) int EasySocket::send(const void *buf, size_t nbyte)
{ {
if(m_replySocket <= 0) return -1; if(m_replySocket <= 0) return -1;
return ::write(m_replySocket,buf,nbyte); int res = ::write(m_replySocket,buf,nbyte);
checkResult(res);
return res;
} }
int EasySocket::receive(void *buf, size_t nbyte) int EasySocket::receive(void *buf, size_t nbyte)
{ {
if(m_replySocket <= 0) return -1; if(m_replySocket <= 0) return -1;
return ::read(m_replySocket,buf,nbyte); int res = ::read(m_replySocket,buf,nbyte);
checkResult(res);
return res;
} }
int EasySocket::listen(int count) int EasySocket::listen(int count)
{ {
if(m_socket < 0 ) return -1; if(m_socket < 0 ) return -1;
return ::listen(m_socket,count); int res = ::listen(m_socket,count);
checkResult(res);
return res;
} }
int EasySocket::accept() int EasySocket::accept()
{ {
if(m_socket < 0 ) return -1; if(m_socket < 0 ) return -1;
m_replySocket = ::accept(m_socket,nullptr,nullptr); m_replySocket = ::accept(m_socket,nullptr,nullptr);
checkResult(m_replySocket);
return m_replySocket; return m_replySocket;
} }
@ -104,6 +114,26 @@ int EasySocket::connect()
} }
return res; return res;
} }
#include <string.h>
void EasySocket::checkResult(int result)
{
if(result >= 0){
m_state = CONNECTION_STATE_SUCCESS;
return;
}else if(result == -1){
//printf("Errno: %s\n", strerror(errno));
switch(errno){
case ECONNABORTED:
case ECONNRESET:
m_state = CONNECTION_STATE_SUCCESS;
break;
default:
break;
}
}
}
#else #else
#pragma comment (lib, "Ws2_32.lib") #pragma comment (lib, "Ws2_32.lib")