ModBusTCP with some fixes
Diff: HTTPD.cpp
- Revision:
- 1:b724fdb741e7
- Parent:
- 0:d18dff347122
--- a/HTTPD.cpp Wed Nov 13 01:58:04 2013 +0000 +++ b/HTTPD.cpp Thu Jun 15 20:17:24 2017 +0000 @@ -26,39 +26,46 @@ _handler_count = 0; } -int HTTPD::start (int port) { +int HTTPD::start (NetworkStack *ns, int port) { int i; + m_ns = ns; + for (i = 0; i < HTTPD_MAX_CLIENTS; i ++) { _state[i].buf = new CircBuffer<char>(HTTPD_BUF_SIZE); - _state[i].thread = new Thread(child, (void*)i, osPriorityNormal, HTTPD_STACK_SIZE); - _state[i].client = new TCPSocketConnection; + _state[i].thread = new Thread(osPriorityNormal, HTTPD_STACK_SIZE); + _state[i].client = new TCPSocket(); + _state[i].thread->start(callback(child, (void*)i)); } + #ifdef HTTPD_ENABLE_CLOSER _state[HTTPD_MAX_CLIENTS].thread = new Thread(closer, (void*)HTTPD_MAX_CLIENTS, osPriorityNormal, 128); - _state[HTTPD_MAX_CLIENTS].client = new TCPSocketConnection; + _state[HTTPD_MAX_CLIENTS].client = new TCPSocket(m_ns); #endif + _server.open(m_ns); _server.bind(port); + _server.set_blocking(true); _server.listen(); - _daemon = new Thread(daemon, NULL, osPriorityNormal, 256); + _daemon = new Thread(osPriorityNormal, HTTPD_STACK_SIZE); + _daemon->start(HTTPD::daemon); return 0; } -void HTTPD::daemon (void const *args) { +void HTTPD::daemon () { HTTPD *httpd = HTTPD::getInstance(); int i, t = 0; INFO("Wait for new connection...\r\n"); for (;;) { if (t >= 0) { - if (httpd->_server.accept(*httpd->_state[t].client) == 0) { + if (httpd->_server.accept(httpd->_state[t].client) == 0) { INFO("accept %d\r\n", t); httpd->_state[t].thread->signal_set(1); } } else { #ifdef HTTPD_ENABLE_CLOSER - if (httpd->_server.accept(*httpd->_state[HTTPD_MAX_CLIENTS].client) == 0) { + if (httpd->_server.accept(httpd->_state[HTTPD_MAX_CLIENTS].client) == 0) { INFO("accept x\r\n"); httpd->_state[HTTPD_MAX_CLIENTS].thread->signal_set(1); } @@ -82,19 +89,27 @@ for (;;) { Thread::signal_wait(1); - httpd->_state[id].mode = MODE_REQUEST; httpd->_state[id].buf->flush(); httpd->_state[id].keepalive = 0; - INFO("Connection from %s\r\n", httpd->_state[id].client->get_address()); - httpd->_state[id].client->set_blocking(false, HTTPD_TIMEOUT); + INFO("Connection from client\r\n"); +// INFO("Connection from %s\r\n", httpd->_state[id].client->get_ip_address()); + + httpd->_state[id].client->set_blocking(false); + httpd->_state[id].client->set_timeout(HTTPD_TIMEOUT); + for (;;) { - if (! httpd->_state[id].client->is_connected()) break; + //if (! httpd->_state[id].client->is_connected()) break; - n = httpd->_state[id].client->receive(buf, sizeof(buf)); - if (n < 0) break; + n = httpd->_state[id].client->recv(buf, sizeof(buf)); + + if (n < 0 ) { + printf("HTTPD::child breaking n = %d\r\n", n); + break; + } buf[n] = 0; -// DBG("Recv %d '%s'", n, buf); + //DBG("Recv %d ", n); + DBG("Recv %d '%s'", n, buf); for (i = 0; i < n; i ++) { httpd->recvData(id, buf[i]); @@ -102,7 +117,8 @@ } httpd->_state[id].client->close(); - INFO("Close %s\r\n", httpd->_state[id].client->get_address()); + INFO("Closed client connection\r\n"); + //INFO("Close %s\r\n", httpd->_state[id].client->get_ip_address()); } } @@ -114,7 +130,8 @@ Thread::signal_wait(1); httpd->_state[id].client->close(); - INFO("Close %s\r\n", httpd->_state[id].client->get_address()); + INFO("Closed client connection\r\n"); + //INFO("Close %s\r\n", httpd->_state[id].client->get_ip_address()); } }