Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/SessionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "utils/Logger.h"
#include "eip/EncapsPacket.h"
#include "eip/EncapsPacketFactory.h"
#if defined (__unix__) || defined(__APPLE__)
#include <unistd.h>
#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <winsock2.h>
#endif

namespace eipScanner {

Expand All @@ -17,7 +22,7 @@ namespace eipScanner {
using eip::EncapsPacketFactory;
using eip::EncapsStatusCodes;

SessionInfo::SessionInfo(const std::string &host, int port, const std::chrono::milliseconds &timeout)
SessionInfo::SessionInfo(const std::string &host, int port, const std::chrono::milliseconds &timeout) try
: _socket{sockets::EndPoint(host, port), timeout}
, _sessionHandle{0} {
_socket.setRecvTimeout(timeout);
Expand All @@ -33,6 +38,20 @@ namespace eipScanner {
_sessionHandle = packet.getSessionHandle();
Logger(LogLevel::INFO) << "Registered session " << _sessionHandle;
}
catch (std::exception& e) {
int sockedFd = this->_socket.getSocketFd();
if (sockedFd > 0)
{
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
shutdown(sockedFd, SD_BOTH);
closesocket(sockedFd);
#else
shutdown(sockedFd, SHUT_RDWR);
close(sockedFd);
#endif
}
throw e;
}

SessionInfo::SessionInfo(const std::string &host, int port)
: SessionInfo(host, port, std::chrono::milliseconds(1000)) {
Expand Down Expand Up @@ -76,4 +95,4 @@ namespace eipScanner {
return _socket.getRemoteEndPoint();
}

}
}