Skip to content

Commit ffda701

Browse files
committed
allowing connections to be initiated multiple times while running - improving reliability
1 parent 4912ec9 commit ffda701

File tree

2 files changed

+111
-74
lines changed

2 files changed

+111
-74
lines changed

src/dds.net-connector-cpp.lib/connector/src/internal/inc/network_client.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ namespace dds {
110110
bool isIOThreadStarted;
111111
std::thread* ioThread;
112112

113+
114+
bool unableToConnectMessageFlag;
115+
bool isSocketCreated;
116+
113117
bool createSocket();
118+
void closeSocket();
114119
void connectWithServer();
115120
bool isDataAvailable(int timeoutSecond = 0, int timeoutMicrosecond = 100);
116121

src/dds.net-connector-cpp.lib/connector/src/internal/src/network_client.cpp

Lines changed: 106 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ dds::net::connector::_internal::
6565
this->tcpPort = 0;
6666
this->isConnected = false;
6767

68+
this->unableToConnectMessageFlag = false;
69+
this->isSocketCreated = false;
70+
6871

6972
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
7073
this->socketFileDescriptor = -1;
@@ -161,16 +164,6 @@ void
161164
{
162165
net->isConnected = false;
163166

164-
//-
165-
//- Creating socket
166-
//-
167-
if (net->createSocket() == false)
168-
{
169-
net->isIOThreadStarted = false;
170-
return;
171-
}
172-
173-
174167
//-
175168
//- The thread functionality
176169
//-
@@ -179,6 +172,11 @@ void
179172
if (net->isConnected == false)
180173
{
181174
net->connectWithServer();
175+
176+
if (net->isConnected == false)
177+
{
178+
sleep_msec(500);
179+
}
182180
}
183181
else
184182
{
@@ -201,6 +199,13 @@ void
201199
if (totalReceived <= 0)
202200
{
203201
net->isConnected = false;
202+
net->closeSocket();
203+
204+
if (net->onDisconnected != nullptr)
205+
{
206+
net->onDisconnected(net->onDisconnectedObj);
207+
}
208+
204209
net->bufferManager->free(bytes);
205210

206211
throw std::exception();
@@ -230,6 +235,7 @@ void
230235
catch (std::exception&)
231236
{
232237
net->isConnected = false;
238+
net->closeSocket();
233239

234240
if (net->onDisconnected != nullptr)
235241
{
@@ -244,14 +250,7 @@ void
244250
}
245251
} // while (isIOThreadStarted)
246252

247-
248-
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
249-
close(net->socketFileDescriptor);
250-
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
251-
closesocket(net->socketFileDescriptor);
252-
#else
253-
#error "Cannot close socket on selected platform"
254-
#endif
253+
net->closeSocket();
255254
}
256255

257256

@@ -405,96 +404,129 @@ bool
405404
dds::net::connector::_internal::
406405
NetworkClient::createSocket()
407406
{
408-
socketFileDescriptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
407+
if (isSocketCreated == false)
408+
{
409+
socketFileDescriptor = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
409410

410411
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
411-
if (socketFileDescriptor == -1)
412+
if (socketFileDescriptor == -1)
412413
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
413-
if (socketFileDescriptor == INVALID_SOCKET)
414+
if (socketFileDescriptor == INVALID_SOCKET)
414415
#else
415416
#error "Cannot check socket validity on selected platform"
416417
#endif
417-
{
418-
std::string msg = "Socket cannot be created for TCP Client being connected with ";
419-
msg += ipv4;
420-
msg += ":";
421-
msg += tcpPort;
418+
{
419+
std::string msg = "Socket cannot be created for TCP Client being connected with ";
420+
msg += ipv4;
421+
msg += ":";
422+
msg += std::to_string(tcpPort);
422423

423-
logger->error(msg.c_str());
424+
logger->error(msg.c_str());
424425

425-
return false;
426+
isSocketCreated = false;
427+
}
428+
429+
isSocketCreated = true;
426430
}
427431

428-
return true;
432+
return isSocketCreated;
429433
}
430434

431435
void
432436
dds::net::connector::_internal::
433-
NetworkClient::connectWithServer()
437+
NetworkClient::closeSocket()
434438
{
435-
int connectionStatus =
436-
::connect(socketFileDescriptor,
437-
(struct sockaddr*)&(targetSocketAddress),
438-
sizeof(targetSocketAddress));
439+
if (isSocketCreated == true)
440+
{
441+
isSocketCreated = false;
439442

440443
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
441-
if (connectionStatus < 0)
444+
close(socketFileDescriptor);
442445
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
443-
if (connectionStatus == SOCKET_ERROR)
446+
closesocket(socketFileDescriptor);
444447
#else
445-
#error "Cannot check socket connection status for selected platform"
448+
#error "Cannot close socket on selected platform"
446449
#endif
447-
{
448-
std::string errorMessage = "Unable to connect to ";
449-
errorMessage += ipv4;
450-
errorMessage += ":";
451-
errorMessage += tcpPort;
452-
453-
logger->error(errorMessage.c_str());
454-
455-
sleep_msec(1000);
456450
}
457-
else
451+
}
452+
453+
void
454+
dds::net::connector::_internal::
455+
NetworkClient::connectWithServer()
456+
{
457+
if (createSocket())
458458
{
459-
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
460-
int status = fcntl(socketFileDescriptor, F_SETFL, fcntl(socketFileDescriptor, F_GETFL, 0) | O_NONBLOCK);
459+
int connectionStatus =
460+
::connect(socketFileDescriptor,
461+
(struct sockaddr*)&(targetSocketAddress),
462+
sizeof(targetSocketAddress));
461463

462-
if (status == -1)
464+
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
465+
if (connectionStatus < 0)
466+
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
467+
if (connectionStatus == SOCKET_ERROR)
468+
#else
469+
#error "Cannot check socket connection status for selected platform"
470+
#endif
463471
{
464-
std::string errorMessage = "Unable to set TCP connection with ";
465-
errorMessage += ipv4;
466-
errorMessage += ":";
467-
errorMessage += tcpPort;
468-
errorMessage += " as non-blocking";
472+
if (unableToConnectMessageFlag == false)
473+
{
474+
unableToConnectMessageFlag = true;
475+
476+
std::string errorMessage = "Unable to connect to ";
477+
errorMessage += ipv4;
478+
errorMessage += ":";
479+
errorMessage += std::to_string(tcpPort);
469480

470-
logger->warning(errorMessage.c_str());
481+
logger->error(errorMessage.c_str());
482+
closeSocket();
483+
}
471484
}
485+
else
486+
{
487+
unableToConnectMessageFlag = false;
488+
489+
#if TARGET_PLATFORM == PLATFORM_GNU_LINUX
490+
int status = fcntl(socketFileDescriptor, F_SETFL, fcntl(socketFileDescriptor, F_GETFL, 0) | O_NONBLOCK);
491+
492+
if (status == -1)
493+
{
494+
std::string errorMessage = "Unable to set TCP connection with ";
495+
errorMessage += ipv4;
496+
errorMessage += ":";
497+
errorMessage += std::to_string(tcpPort);
498+
errorMessage += " as non-blocking";
499+
500+
logger->warning(errorMessage.c_str());
501+
}
472502
#elif TARGET_PLATFORM == PLATFORM_WINDOWS
473-
u_long mode = 1;
474-
int nonBlockingModeResult = ioctlsocket(socketFileDescriptor, FIONBIO, &mode);
503+
u_long mode = 1;
504+
int nonBlockingModeResult = ioctlsocket(socketFileDescriptor, FIONBIO, &mode);
475505

476-
if (nonBlockingModeResult == SOCKET_ERROR)
477-
{
478-
std::string errorMessage = "Unable to set TCP connection with ";
479-
errorMessage += ipv4;
480-
errorMessage += ":";
481-
errorMessage += tcpPort;
482-
errorMessage += " as non-blocking";
506+
if (nonBlockingModeResult == SOCKET_ERROR)
507+
{
508+
std::string errorMessage = "Unable to set TCP connection with ";
509+
errorMessage += ipv4;
510+
errorMessage += ":";
511+
errorMessage += std::to_string(tcpPort);
512+
errorMessage += " as non-blocking";
483513

484-
logger->warning(errorMessage.c_str());
485-
}
514+
logger->warning(errorMessage.c_str());
515+
}
486516
#else
487517
#error "Cannot set socket to non-blocking for selected platform"
488518
#endif
489-
dataToServerQueue->clear();
490-
dataFromServerQueue->clear();
491519

492-
if (onConnected != nullptr)
493-
{
494-
onConnected(onConnectedObj);
495-
}
520+
dataToServerQueue->clear();
521+
dataFromServerQueue->clear();
496522

497-
isConnected = true;
523+
if (onConnected != nullptr)
524+
{
525+
onConnected(onConnectedObj);
526+
}
527+
528+
isConnected = true;
529+
}
498530
}
499531
}
500532

0 commit comments

Comments
 (0)