Skip to content

Commit c9905c2

Browse files
committed
Multiple listeners at same port now triggers an error
1 parent 7a43775 commit c9905c2

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Source/IO/Network.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,16 @@ namespace IO
612612

613613
for (const auto &s : data[i].NMEA)
614614
{
615-
if (SendTo((s + "\r\n").c_str()) < 0) {
615+
if (SendTo((s + "\r\n").c_str()) < 0)
616+
{
616617
first_message = true;
617618
if (!persistent)
618619
{
619620
Error() << "TCP feed: requesting termination.";
620621
StopRequest();
621622
}
622623
}
623-
else
624+
else
624625
first_message = false;
625626
}
626627
}
@@ -642,7 +643,7 @@ namespace IO
642643
StopRequest();
643644
}
644645
}
645-
else
646+
else
646647
first_message = false;
647648
}
648649
}
@@ -778,7 +779,11 @@ namespace IO
778779
ss << ", json: " << Util::Convert::toString(fmt) << ".";
779780

780781
Info() << ss.str();
781-
Server::start(port);
782+
783+
if (!Server::start(port))
784+
{
785+
throw std::runtime_error("TCP listener: cannot start server at port " + std::to_string(port) + ".");
786+
}
782787
}
783788

784789
Setting &TCPlistenerStreamer::Set(std::string option, std::string arg)

Source/Library/TCP.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace TCP
6161
{
6262

6363
const int TCP::Server::MAX_CONN;
64+
std::vector<int> Server::active_ports;
6465
// TO DO: create a BaseSocket class and clean up between files Network (AC streamers) and TCP (low level TCP connections)
6566

6667
void ServerConnection::Lock()
@@ -261,6 +262,19 @@ namespace TCP
261262
run_thread.join();
262263
if (sock != -1)
263264
closesocket(sock);
265+
266+
// Remove port from active_ports
267+
if (listening_port != -1)
268+
{
269+
for (auto it = active_ports.begin(); it != active_ports.end(); ++it)
270+
{
271+
if (*it == listening_port)
272+
{
273+
active_ports.erase(it);
274+
break;
275+
}
276+
}
277+
}
264278
}
265279

266280
int Server::numberOfClients()
@@ -473,6 +487,15 @@ namespace TCP
473487

474488
bool Server::start(int port)
475489
{
490+
// Check if port is already in use
491+
for (const auto& p : active_ports)
492+
{
493+
if (p == port)
494+
{
495+
Error() << "TCP Server: port " << port << " is already in use by another server instance";
496+
return false;
497+
}
498+
}
476499

477500
sock = socket(AF_INET, SOCK_STREAM, 0);
478501
if (sock < 0)
@@ -528,6 +551,9 @@ namespace TCP
528551
else
529552
Info() << "TCP Server: start thread at IP " << IP_BIND << " port " << port;
530553

554+
listening_port = port;
555+
active_ports.push_back(port);
556+
531557
run_thread = std::thread(&Server::Run, this);
532558

533559
return true;

Source/Library/TCP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ namespace TCP
110110
int timeout = 30;
111111
bool reuse_port = true;
112112
std::string IP_BIND;
113+
int listening_port = -1;
114+
115+
static std::vector<int> active_ports;
113116

114117
const static int MAX_CONN = 16;
115118
std::array<ServerConnection, MAX_CONN> client;

0 commit comments

Comments
 (0)