[DEV] some update
This commit is contained in:
parent
d8d002b669
commit
dd3db3eea6
@ -26,7 +26,7 @@ enet::Http::~Http() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool enet::Http::connect() {
|
bool enet::Http::connect() {
|
||||||
if (m_connection.getConnectionStatus() == enet::Tcp::statusLink) {
|
if (m_connection.getConnectionStatus() == enet::Tcp::status::link) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (m_connection.link() == false) {
|
if (m_connection.link() == false) {
|
||||||
@ -56,7 +56,7 @@ std::string enet::Http::getReceiveHeaderProperties(const std::string& _key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool enet::Http::reset() {
|
bool enet::Http::reset() {
|
||||||
if (m_connection.getConnectionStatus() != enet::Tcp::statusLink) {
|
if (m_connection.getConnectionStatus() != enet::Tcp::status::link) {
|
||||||
m_connection.unlink();
|
m_connection.unlink();
|
||||||
}
|
}
|
||||||
m_receiveData.clear();
|
m_receiveData.clear();
|
||||||
@ -95,7 +95,7 @@ bool enet::Http::receiveData() {
|
|||||||
char data[1025];
|
char data[1025];
|
||||||
int32_t len = 1;
|
int32_t len = 1;
|
||||||
bool headerEnded = false;
|
bool headerEnded = false;
|
||||||
while ( m_connection.getConnectionStatus() == enet::Tcp::statusLink
|
while ( m_connection.getConnectionStatus() == enet::Tcp::status::link
|
||||||
&& len > 0) {
|
&& len > 0) {
|
||||||
len = m_connection.read(data, 1024);
|
len = m_connection.read(data, 1024);
|
||||||
// TODO : Parse header ...
|
// 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");
|
ENET_WARNING("server disconnected");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
37
enet/Tcp.cpp
37
enet/Tcp.cpp
@ -22,7 +22,7 @@ enet::Tcp::Tcp() :
|
|||||||
m_host("127.0.0.1"),
|
m_host("127.0.0.1"),
|
||||||
m_port(23191),
|
m_port(23191),
|
||||||
m_server(false),
|
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) {
|
if (_name == m_host) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_status == statusLink) {
|
if (m_status == status::link) {
|
||||||
ENET_ERROR("Can not change parameter while connection is started");
|
ENET_ERROR("Can not change parameter while connection is started");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ void enet::Tcp::setPort(uint16_t _port) {
|
|||||||
if (_port == m_port) {
|
if (_port == m_port) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_status == statusLink) {
|
if (m_status == status::link) {
|
||||||
ENET_ERROR("Can not change parameter while connection is started");
|
ENET_ERROR("Can not change parameter while connection is started");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ void enet::Tcp::setServer(bool _status) {
|
|||||||
if (_status == m_server) {
|
if (_status == m_server) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_status == statusLink) {
|
if (m_status == status::link) {
|
||||||
ENET_ERROR("Can not change parameter while connection is started");
|
ENET_ERROR("Can not change parameter while connection is started");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ void enet::Tcp::setServer(bool _status) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool enet::Tcp::link() {
|
bool enet::Tcp::link() {
|
||||||
if (m_status == statusLink) {
|
if (m_status == status::link) {
|
||||||
ENET_ERROR("Connection is already started");
|
ENET_ERROR("Connection is already started");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -104,9 +104,11 @@ bool enet::Tcp::link() {
|
|||||||
bcopy((char *)server->h_addr, (char *)&servAddr.sin_addr.s_addr, server->h_length);
|
bcopy((char *)server->h_addr, (char *)&servAddr.sin_addr.s_addr, server->h_length);
|
||||||
servAddr.sin_port = htons(m_port);
|
servAddr.sin_port = htons(m_port);
|
||||||
ENET_INFO("Start connexion ...");
|
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 != EINPROGRESS) {
|
||||||
if(errno != ENOENT && errno != EAGAIN && errno != ECONNREFUSED) {
|
if( errno != ENOENT
|
||||||
|
&& errno != EAGAIN
|
||||||
|
&& errno != ECONNREFUSED) {
|
||||||
ENET_ERROR("ERROR connecting on : errno=" << errno << "," << strerror(errno));
|
ENET_ERROR("ERROR connecting on : errno=" << errno << "," << strerror(errno));
|
||||||
}
|
}
|
||||||
close(m_socketIdClient);
|
close(m_socketIdClient);
|
||||||
@ -123,7 +125,7 @@ bool enet::Tcp::link() {
|
|||||||
ENET_ERROR("ERROR connecting ... (after all try)");
|
ENET_ERROR("ERROR connecting ... (after all try)");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
m_status = statusLink;
|
m_status = status::link;
|
||||||
ENET_DEBUG("Connection done");
|
ENET_DEBUG("Connection done");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -164,7 +166,7 @@ bool enet::Tcp::link() {
|
|||||||
m_socketId = -1;
|
m_socketId = -1;
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
m_status = statusLink;
|
m_status = status::link;
|
||||||
ENET_DEBUG("Connection done");
|
ENET_DEBUG("Connection done");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,28 +186,30 @@ bool enet::Tcp::unlink() {
|
|||||||
close(m_socketId);
|
close(m_socketId);
|
||||||
m_socketId = -1;
|
m_socketId = -1;
|
||||||
}
|
}
|
||||||
m_status = statusUnlink;
|
m_status = status::unlink;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int32_t enet::Tcp::read(void* _data, int32_t _maxLen) {
|
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");
|
ENET_ERROR("Can not read on unlink connection");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int32_t size = ::read(m_socketIdClient, _data, _maxLen);
|
int32_t size = ::read(m_socketIdClient, _data, _maxLen);
|
||||||
if ( size != _maxLen
|
if ( size != 0
|
||||||
&& errno != 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));
|
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 -1;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t enet::Tcp::write(const void* _data, int32_t _len) {
|
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");
|
ENET_ERROR("Can not write on unlink connection");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -213,9 +217,8 @@ int32_t enet::Tcp::write(const void* _data, int32_t _len) {
|
|||||||
if ( size != _len
|
if ( size != _len
|
||||||
&& errno != 0) {
|
&& errno != 0) {
|
||||||
ENET_ERROR("PB when writing data on the FD : request=" << _len << " have=" << size << ", erno=" << errno << "," << strerror(errno));
|
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 -1;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +67,10 @@ namespace enet {
|
|||||||
return m_server;
|
return m_server;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
enum status {
|
enum class status {
|
||||||
statusUnlink,
|
unlink,
|
||||||
statusLink,
|
link,
|
||||||
statusError
|
error
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
enum status m_status; //!< current connection status
|
enum status m_status; //!< current connection status
|
||||||
|
@ -30,7 +30,7 @@ int main(int _argc, const char *_argv[]) {
|
|||||||
// client mode ...
|
// client mode ...
|
||||||
enet::Tcp connection;
|
enet::Tcp connection;
|
||||||
connection.setHostNane("127.0.0.1");
|
connection.setHostNane("127.0.0.1");
|
||||||
connection.setPort(31234);
|
connection.setPort(31235);
|
||||||
connection.setServer(false);
|
connection.setServer(false);
|
||||||
TEST_INFO("CLIENT connect ...");
|
TEST_INFO("CLIENT connect ...");
|
||||||
if (connection.link() == false) {
|
if (connection.link() == false) {
|
||||||
@ -43,6 +43,10 @@ int main(int _argc, const char *_argv[]) {
|
|||||||
char data[1024];
|
char data[1024];
|
||||||
int32_t len = connection.read(data, 1024);
|
int32_t len = connection.read(data, 1024);
|
||||||
TEST_INFO("read len=" << len << " data='" << data << "'");
|
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++;
|
iii++;
|
||||||
}
|
}
|
||||||
if (iii>=10000) {
|
if (iii>=10000) {
|
||||||
|
@ -30,7 +30,7 @@ int main(int _argc, const char *_argv[]) {
|
|||||||
// server mode ...
|
// server mode ...
|
||||||
enet::Tcp connection;
|
enet::Tcp connection;
|
||||||
connection.setHostNane("127.0.0.1");
|
connection.setHostNane("127.0.0.1");
|
||||||
connection.setPort(31234);
|
connection.setPort(31235);
|
||||||
connection.setServer(true);
|
connection.setServer(true);
|
||||||
TEST_INFO("SERVER connect ...");
|
TEST_INFO("SERVER connect ...");
|
||||||
if (connection.link() == false) {
|
if (connection.link() == false) {
|
||||||
@ -41,6 +41,11 @@ int main(int _argc, const char *_argv[]) {
|
|||||||
while (connection.getConnectionStatus() == enet::Tcp::statusLink) {
|
while (connection.getConnectionStatus() == enet::Tcp::statusLink) {
|
||||||
int32_t len = connection.write("plop" + etk::to_string(iii));
|
int32_t len = connection.write("plop" + etk::to_string(iii));
|
||||||
TEST_INFO("write len=" << len);
|
TEST_INFO("write len=" << len);
|
||||||
|
char data[1024];
|
||||||
|
len = connection.read(data, 1024);
|
||||||
|
if (len > 0) {
|
||||||
|
TEST_INFO("read len=" << len << " data='" << data << "'");
|
||||||
|
}
|
||||||
iii++;
|
iii++;
|
||||||
}
|
}
|
||||||
if (iii>=1000000) {
|
if (iii>=1000000) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user