Skip to content
This repository was archived by the owner on Dec 4, 2021. It is now read-only.

Commit 0c66222

Browse files
author
Chris Board
committed
Ensured system socket binding is retried
If the process has just stopped when it restarts, the socket might not be able to bind straight away. It reattepts the bind every 10 seconds until the socket gets free'd for us again.
1 parent 84feb73 commit 0c66222

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

MySQLManager-TunnelPlugin_C++/SocketListener.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,32 @@ void SocketListener::startSocketListener()
3030
{
3131
try
3232
{
33-
if (!this->socketManager.createSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,
34-
StaticSettings::AppSettings::listenSocket, 1024, "127.0.0.1"))
35-
{
36-
}
37-
if (!this->socketManager.bindAndStartListening())
38-
{
39-
}
40-
this->threadStarted = true;
41-
this->threadSocketListener = thread(&SocketListener::socketListenerThread, this);
33+
if (!this->socketManager.createSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,
34+
StaticSettings::AppSettings::listenSocket, 1024, "127.0.0.1"))
35+
{
36+
this->logger->writeToLog("Failed to prepare socket. Cannot continue");
37+
return;
38+
}
39+
40+
if (!this->socketManager.bindAndStartListening())
41+
{
42+
this->logger->writeToLog("Failed to bind socket. Retrying in 10 seconds");
43+
this_thread::sleep_for(chrono::seconds(10));
44+
this->startSocketListener();
45+
}
46+
this->threadStarted = true;
47+
this->threadSocketListener = thread(&SocketListener::socketListenerThread, this);
4248

43-
this->logger->writeToLog("Server socket has been successfully opened", "SocketListener", "startSocketListener");
49+
this->logger->writeToLog("Server socket has been successfully opened", "SocketListener", "startSocketListener");
4450
}
4551
catch (SocketException ex)
4652
{
4753
stringstream logstream;
4854
logstream << "Failed to start socket listener. Error: " << ex.what();
4955
this->logger->writeToLog(logstream.str(), "SocketListener", "startSocketListener");
56+
this->logger->writeToLog("Failed to bind socket. Retrying in 10 seconds");
57+
this_thread::sleep_for(chrono::seconds(10));
58+
this->startSocketListener();
5059
}
5160
catch (std::exception ex)
5261
{

0 commit comments

Comments
 (0)