| 51 | } |
| 52 | |
| 53 | err_t TcpServer::onAccept(tcp_pcb* clientTcp, err_t err) |
| 54 | { |
| 55 | // Anti DDoS :-) |
| 56 | if(system_get_free_heap_size() < minHeapSize) { |
| 57 | debug_w("\r\n\r\nCONNECTION DROPPED\r\n\t(free heap: %u)\r\n\r\n", system_get_free_heap_size()); |
| 58 | return ERR_MEM; |
| 59 | } |
| 60 | |
| 61 | // Obey any requested connection limit |
| 62 | if(maxConnections != 0 && connections.count() >= maxConnections) { |
| 63 | debug_w("\r\n\r\nCONNECTION DROPPED\r\n\t(Existing connections: %u)\r\n\r\n", connections.count()); |
| 64 | return ERR_ABRT; |
| 65 | } |
| 66 | |
| 67 | #ifdef NETWORK_DEBUG |
| 68 | debug_d("onAccept, tcp: %p, state: %d K=%d, Free heap size=%u", clientTcp, err, connections.count(), |
| 69 | system_get_free_heap_size()); |
| 70 | #endif |
| 71 | |
| 72 | if(err != ERR_OK) { |
| 73 | //closeTcpConnection(clientTcp, nullptr); |
| 74 | return err; |
| 75 | } |
| 76 | |
| 77 | TcpConnection* client = createClient(clientTcp); |
| 78 | if(client == nullptr) { |
| 79 | return ERR_MEM; |
| 80 | } |
| 81 | client->setTimeOut(keepAlive); |
| 82 | |
| 83 | if(useSsl) { |
| 84 | if(!sslCreateSession()) { |
| 85 | delete client; |
| 86 | return ERR_ABRT; |
| 87 | } |
| 88 | if(!ssl->onAccept(client, clientTcp)) { |
| 89 | delete client; |
| 90 | return ERR_ABRT; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | client->setDestroyedDelegate(TcpConnectionDestroyedDelegate(&TcpServer::onClientDestroy, this)); |
| 95 | |
| 96 | connections.add(client); |
| 97 | debug_d("Opening connection. Total connections: %d", connections.count()); |
| 98 | |
| 99 | onClient(reinterpret_cast<TcpClient*>(client)); |
| 100 | |
| 101 | return ERR_OK; |
| 102 | } |
| 103 | |
| 104 | void TcpServer::onClient(TcpClient* client) |
| 105 | { |
no test coverage detected