diff --git a/enet/Http.cpp b/enet/Http.cpp index 9af5314..e4c99cd 100644 --- a/enet/Http.cpp +++ b/enet/Http.cpp @@ -26,7 +26,7 @@ enet::Http::~Http() { } bool enet::Http::connect() { - if (m_connection.getConnectionStatus() == enet::Tcp::statusLink) { + if (m_connection.getConnectionStatus() == enet::Tcp::status::link) { return true; } if (m_connection.link() == false) { @@ -56,7 +56,7 @@ std::string enet::Http::getReceiveHeaderProperties(const std::string& _key) { } bool enet::Http::reset() { - if (m_connection.getConnectionStatus() != enet::Tcp::statusLink) { + if (m_connection.getConnectionStatus() != enet::Tcp::status::link) { m_connection.unlink(); } m_receiveData.clear(); @@ -95,7 +95,7 @@ bool enet::Http::receiveData() { char data[1025]; int32_t len = 1; bool headerEnded = false; - while ( m_connection.getConnectionStatus() == enet::Tcp::statusLink + while ( m_connection.getConnectionStatus() == enet::Tcp::status::link && len > 0) { len = m_connection.read(data, 1024); // TODO : Parse header ... @@ -132,7 +132,7 @@ bool enet::Http::receiveData() { } } } - if (m_connection.getConnectionStatus() != enet::Tcp::statusLink) { + if (m_connection.getConnectionStatus() != enet::Tcp::status::link) { ENET_WARNING("server disconnected"); return false; } diff --git a/enet/Tcp.cpp b/enet/Tcp.cpp index 6516efc..aba0522 100644 --- a/enet/Tcp.cpp +++ b/enet/Tcp.cpp @@ -22,7 +22,7 @@ enet::Tcp::Tcp() : m_host("127.0.0.1"), m_port(23191), m_server(false), - m_status(statusUnlink) { + m_status(status::unlink) { } @@ -46,7 +46,7 @@ void enet::Tcp::setHostNane(const std::string& _name) { if (_name == m_host) { return; } - if (m_status == statusLink) { + if (m_status == status::link) { ENET_ERROR("Can not change parameter while connection is started"); return; } @@ -57,7 +57,7 @@ void enet::Tcp::setPort(uint16_t _port) { if (_port == m_port) { return; } - if (m_status == statusLink) { + if (m_status == status::link) { ENET_ERROR("Can not change parameter while connection is started"); return; } @@ -68,7 +68,7 @@ void enet::Tcp::setServer(bool _status) { if (_status == m_server) { return; } - if (m_status == statusLink) { + if (m_status == status::link) { ENET_ERROR("Can not change parameter while connection is started"); return; } @@ -76,7 +76,7 @@ void enet::Tcp::setServer(bool _status) { } bool enet::Tcp::link() { - if (m_status == statusLink) { + if (m_status == status::link) { ENET_ERROR("Connection is already started"); return false; } @@ -104,9 +104,11 @@ bool enet::Tcp::link() { bcopy((char *)server->h_addr, (char *)&servAddr.sin_addr.s_addr, server->h_length); servAddr.sin_port = htons(m_port); ENET_INFO("Start connexion ..."); - if (connect(m_socketIdClient,(struct sockaddr *) &servAddr,sizeof(servAddr)) != 0) { + if (connect(m_socketIdClient, (struct sockaddr *)&servAddr,sizeof(servAddr)) != 0) { if(errno != EINPROGRESS) { - if(errno != ENOENT && errno != EAGAIN && errno != ECONNREFUSED) { + if( errno != ENOENT + && errno != EAGAIN + && errno != ECONNREFUSED) { ENET_ERROR("ERROR connecting on : errno=" << errno << "," << strerror(errno)); } close(m_socketIdClient); @@ -123,7 +125,7 @@ bool enet::Tcp::link() { ENET_ERROR("ERROR connecting ... (after all try)"); return false; } else { - m_status = statusLink; + m_status = status::link; ENET_DEBUG("Connection done"); } } else { @@ -164,7 +166,7 @@ bool enet::Tcp::link() { m_socketId = -1; return false; } else { - m_status = statusLink; + m_status = status::link; ENET_DEBUG("Connection done"); } } @@ -184,28 +186,30 @@ bool enet::Tcp::unlink() { close(m_socketId); m_socketId = -1; } - m_status = statusUnlink; + m_status = status::unlink; return true; } int32_t enet::Tcp::read(void* _data, int32_t _maxLen) { - if (m_status != statusLink) { + if (m_status != status::link) { ENET_ERROR("Can not read on unlink connection"); return -1; } int32_t size = ::read(m_socketIdClient, _data, _maxLen); - if ( size != _maxLen - && errno != 0) { + if ( size != 0 + && errno == 2) { + // simply the socket en empty + } else if (errno != 0) { ENET_ERROR("PB when reading data on the FD : request=" << _maxLen << " have=" << size << ", erno=" << errno << "," << strerror(errno)); - m_status = statusError; + m_status = status::error; return -1; } return size; } int32_t enet::Tcp::write(const void* _data, int32_t _len) { - if (m_status != statusLink) { + if (m_status != status::link) { ENET_ERROR("Can not write on unlink connection"); return -1; } @@ -213,9 +217,8 @@ int32_t enet::Tcp::write(const void* _data, int32_t _len) { if ( size != _len && errno != 0) { ENET_ERROR("PB when writing data on the FD : request=" << _len << " have=" << size << ", erno=" << errno << "," << strerror(errno)); - m_status = statusError; + m_status = status::error; return -1; } return size; } - diff --git a/enet/Tcp.h b/enet/Tcp.h index d48e65b..03970e0 100644 --- a/enet/Tcp.h +++ b/enet/Tcp.h @@ -67,10 +67,10 @@ namespace enet { return m_server; } public: - enum status { - statusUnlink, - statusLink, - statusError + enum class status { + unlink, + link, + error }; private: enum status m_status; //!< current connection status diff --git a/test/main-client.cpp b/test/main-client.cpp index 6e33004..dd8c18f 100644 --- a/test/main-client.cpp +++ b/test/main-client.cpp @@ -30,7 +30,7 @@ int main(int _argc, const char *_argv[]) { // client mode ... enet::Tcp connection; connection.setHostNane("127.0.0.1"); - connection.setPort(31234); + connection.setPort(31235); connection.setServer(false); TEST_INFO("CLIENT connect ..."); if (connection.link() == false) { @@ -43,6 +43,10 @@ int main(int _argc, const char *_argv[]) { char data[1024]; int32_t len = connection.read(data, 1024); TEST_INFO("read len=" << len << " data='" << data << "'"); + //if (data[len-1] == '2') { + int32_t lenWrite = connection.write("get pair value"); + TEST_INFO("write len=" << lenWrite); + //} iii++; } if (iii>=10000) { diff --git a/test/main-server.cpp b/test/main-server.cpp index 2011acc..8e68268 100644 --- a/test/main-server.cpp +++ b/test/main-server.cpp @@ -30,7 +30,7 @@ int main(int _argc, const char *_argv[]) { // server mode ... enet::Tcp connection; connection.setHostNane("127.0.0.1"); - connection.setPort(31234); + connection.setPort(31235); connection.setServer(true); TEST_INFO("SERVER connect ..."); if (connection.link() == false) { @@ -41,6 +41,11 @@ int main(int _argc, const char *_argv[]) { while (connection.getConnectionStatus() == enet::Tcp::statusLink) { int32_t len = connection.write("plop" + etk::to_string(iii)); TEST_INFO("write len=" << len); + char data[1024]; + len = connection.read(data, 1024); + if (len > 0) { + TEST_INFO("read len=" << len << " data='" << data << "'"); + } iii++; } if (iii>=1000000) {