Skip to content

Commit 3db35bd

Browse files
committed
Changes for #217
1 parent 5fafd44 commit 3db35bd

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

cpp_utils/HttpServer.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,16 @@ class HttpServerTask: public Task {
181181
*/
182182
void run(void* data) {
183183
m_pHttpServer = (HttpServer*)data; // The passed in data is an instance of an HttpServer.
184-
m_pHttpServer->m_sockServ.setPort(m_pHttpServer->m_portNumber);
185-
m_pHttpServer->m_sockServ.setSSL(m_pHttpServer->m_useSSL);
186-
m_pHttpServer->m_sockServ.start();
184+
m_pHttpServer->m_socket.setSSL(m_pHttpServer->m_useSSL);
185+
m_pHttpServer->m_socket.listen(m_pHttpServer->m_portNumber);
187186
ESP_LOGD("HttpServerTask", "Listening on port %d", m_pHttpServer->getPort());
188187
Socket clientSocket;
189188
while(1) { // Loop forever.
190189

191190
ESP_LOGD("HttpServerTask", "Waiting for new peer client");
192-
Memory::checkIntegrity();
191+
//Memory::checkIntegrity();
193192
try {
194-
clientSocket = m_pHttpServer->m_sockServ.waitForNewClient(); // Block waiting for a new external client connection.
193+
clientSocket = m_pHttpServer->m_socket.accept(); // Block waiting for a new external client connection.
195194
}
196195
catch(std::exception &e) {
197196
ESP_LOGE("HttpServerTask", "Caught an exception waiting for new client!");
@@ -353,7 +352,7 @@ void HttpServer::stop() {
353352
// that is listening for incoming connections. That will then shutdown all the other
354353
// activities.
355354
ESP_LOGD(LOG_TAG, ">> stop");
356-
m_sockServ.stop();
355+
m_socket.close();
357356
ESP_LOGD(LOG_TAG, "<< stop");
358357
} // stop
359358

cpp_utils/HttpServer.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "SockServ.h"
1717
#include "HttpRequest.h"
1818
#include "HttpResponse.h"
19-
#include "SockServ.h"
19+
//#include "SockServ.h"
2020
#include <regex>
2121

2222
class HttpServerTask;
@@ -89,7 +89,8 @@ class HttpServer {
8989
std::string m_rootPath; // Root path into the file system.
9090
bool m_useSSL; // Is this server listening on an HTTPS port?
9191
bool m_directoryListing; // Should we list directory content?
92-
SockServ m_sockServ; // Server socket.
92+
//SockServ m_sockServ; // Server socket.
93+
Socket m_socket;
9394
}; // HttpServer
9495

9596
#endif /* COMPONENTS_CPP_UTILS_HTTPSERVER_H_ */

cpp_utils/SockServ.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ SockServ::~SockServ() {
6565
try {
6666
while(1) {
6767
ESP_LOGD(LOG_TAG, "Waiting on accept")
68-
Socket tempSock = pSockServ->m_serverSocket.accept(pSockServ->getSSL());
68+
Socket tempSock = pSockServ->m_serverSocket.accept();
6969
if (!tempSock.isValid()) {
7070
continue;
7171
}

cpp_utils/Socket.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ Socket::~Socket() {
5656
/**
5757
* @brief Accept a new socket.
5858
*/
59-
Socket Socket::accept(bool useSSL) {
59+
Socket Socket::accept() {
6060
struct sockaddr addr;
6161
getBind(&addr);
62-
ESP_LOGD(LOG_TAG, ">> accept: Accepting on %s; sockFd: %d, using SSL: %d", addressToString(&addr).c_str(), m_sock, useSSL);
62+
ESP_LOGD(LOG_TAG, ">> accept: Accepting on %s; sockFd: %d, using SSL: %d", addressToString(&addr).c_str(), m_sock, getSSL());
6363

64-
struct sockaddr_in clientAddress;
65-
socklen_t clientAddressLength = sizeof(clientAddress);
66-
int clientSockFD = ::lwip_accept_r(m_sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
64+
int clientSockFD = ::lwip_accept_r(m_sock, nullptr, nullptr);
6765
if (clientSockFD == -1) {
6866
SocketException se(errno);
6967
ESP_LOGE(LOG_TAG, "accept(): %s, m_sock=%d", strerror(errno), m_sock);
@@ -73,7 +71,7 @@ Socket Socket::accept(bool useSSL) {
7371
ESP_LOGD(LOG_TAG, " - accept: Received new client!: sockFd: %d", clientSockFD);
7472
Socket newSocket;
7573
newSocket.m_sock = clientSockFD;
76-
if (useSSL) {
74+
if (getSSL()) {
7775
newSocket.setSSL(true);
7876
newSocket.m_sslSock.fd = clientSockFD;
7977
newSocket.sslHandshake();
@@ -252,7 +250,7 @@ bool Socket::isValid() {
252250
/**
253251
* @brief Create a listening socket.
254252
* @param [in] port The port number to listen upon.
255-
* @param [in] isDatagram True if we are listening on a datagram.
253+
* @param [in] isDatagram True if we are listening on a datagram. The default is false.
256254
*/
257255
void Socket::listen(uint16_t port, bool isDatagram) {
258256
ESP_LOGD(LOG_TAG, ">> listen: port: %d, isDatagram: %d", port, isDatagram);

cpp_utils/Socket.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Socket {
5656
Socket();
5757
virtual ~Socket();
5858

59-
Socket accept(bool useSSL=false);
59+
Socket accept();
6060
static std::string addressToString(struct sockaddr* addr);
6161
void bind(uint16_t port, uint32_t address);
6262
void close();
@@ -99,7 +99,7 @@ class SocketInputRecordStreambuf : public std::streambuf {
9999
~SocketInputRecordStreambuf();
100100
int_type underflow();
101101
private:
102-
char *m_buffer;
102+
char* m_buffer;
103103
Socket m_socket;
104104
size_t m_dataLength;
105105
size_t m_bufferSize;

0 commit comments

Comments
 (0)