Skip to content

Commit 137f3d9

Browse files
committed
Fixes for #355
1 parent c909117 commit 137f3d9

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

cpp_utils/HttpServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class HttpServerTask: public Task {
131131
void run(void* data) {
132132
m_pHttpServer = (HttpServer*)data; // The passed in data is an instance of an HttpServer.
133133
m_pHttpServer->m_socket.setSSL(m_pHttpServer->m_useSSL);
134-
m_pHttpServer->m_socket.listen(m_pHttpServer->m_portNumber);
134+
m_pHttpServer->m_socket.listen(m_pHttpServer->m_portNumber, false /* is datagram */, true /* Allow address reuse */);
135135
ESP_LOGD("HttpServerTask", "Listening on port %d", m_pHttpServer->getPort());
136136
Socket clientSocket;
137137
while(1) { // Loop forever.

cpp_utils/Socket.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,10 @@ bool Socket::isValid() {
259259
* @param [in] isDatagram True if we are listening on a datagram. The default is false.
260260
* @return Returns 0 on success.
261261
*/
262-
int Socket::listen(uint16_t port, bool isDatagram) {
262+
int Socket::listen(uint16_t port, bool isDatagram, bool reuseAddress) {
263263
ESP_LOGD(LOG_TAG, ">> listen: port: %d, isDatagram: %d", port, isDatagram);
264264
createSocket(isDatagram);
265+
setReuseAddress(reuseAddress);
265266
int rc = bind(port, 0);
266267
if (rc != 0) {
267268
ESP_LOGE(LOG_TAG, "<< listen: Error in bind: %s", strerror(errno));
@@ -285,14 +286,19 @@ bool Socket::operator <(const Socket& other) const {
285286
return m_sock < other.m_sock;
286287
}
287288

288-
int Socket::setSocketOption(int option, char* value, size_t len)
289+
290+
/**
291+
* @brief Set the socket option.
292+
*/
293+
int Socket::setSocketOption(int option, void* value, size_t len)
289294
{
290-
int res = setsockopt(m_sock, SOL_SOCKET, option, value, len);
295+
int res = ::setsockopt(m_sock, SOL_SOCKET, option, value, len);
291296
if(res < 0) {
292297
ESP_LOGE(LOG_TAG, "%X : %d", option, errno);
293298
}
294299
return res;
295-
}
300+
} // setSocketOption
301+
296302

297303
/**
298304
* @brief Socket timeout.
@@ -479,6 +485,18 @@ void Socket::sendTo(const uint8_t* data, size_t length, struct sockaddr* pAddr)
479485
} // sendTo
480486

481487

488+
/**
489+
* @brief Flag the socket address as re-usable.
490+
* @param [in] value True to mark the address as re-usable, false otherwise.
491+
*/
492+
void Socket::setReuseAddress(bool value) {
493+
ESP_LOGD(LOG_TAG, ">> setReuseAddress: %d", value);
494+
int val = value?1:0;
495+
setSocketOption(SO_REUSEADDR, &val, sizeof(val));
496+
ESP_LOGD(LOG_TAG, "<< setReuseAddress");
497+
} // setReuseAddress
498+
499+
482500
/**
483501
* @brief Flag the socket as using SSL
484502
* @param [in] sslValue True if we wish to use SSL.
@@ -651,3 +669,5 @@ SocketInputRecordStreambuf::int_type SocketInputRecordStreambuf::underflow() {
651669
SocketException::SocketException(int myErrno) {
652670
m_errno = myErrno;
653671
}
672+
673+

cpp_utils/Socket.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ class Socket {
6868
int connect(struct in_addr address, uint16_t port);
6969
int connect(char* address, uint16_t port);
7070
int createSocket(bool isDatagram = false);
71-
int setSocketOption(int option, char* value, size_t len);
71+
void setReuseAddress(bool value);
72+
int setSocketOption(int option, void* value, size_t len);
7273
int setTimeout(uint32_t seconds);
7374
void getBind(struct sockaddr* pAddr);
7475
int getFD() const;
7576
bool getSSL() const;
7677
bool isValid();
77-
int listen(uint16_t port, bool isDatagram=false);
78+
int listen(uint16_t port, bool isDatagram=false, bool reuseAddress=false);
7879
bool operator<(const Socket& other) const;
7980
std::string readToDelim(std::string delim);
8081
size_t receive(uint8_t* data, size_t length, bool exact=false);

0 commit comments

Comments
 (0)