Skip to content
Snippets Groups Projects
Commit 4202e16b authored by Ruud Overeem's avatar Ruud Overeem
Browse files

BugID: 679

Fixed bug in TCPSocket that caused a 'disconnect' when the 'connect' call
returned the error EISCONN (already connected).
This can occure when you call open twice on the same port.
parent cbd94da0
No related branches found
No related tags found
No related merge requests found
......@@ -119,8 +119,7 @@ bool GCFTCPPort::open()
{
// already connect?
if (isConnected()) {
LOG_ERROR(formatString("Port %s already open.",
makeServiceName().c_str()));
LOG_ERROR(formatString("Port %s already open.", makeServiceName().c_str()));
return (false);
}
......@@ -246,6 +245,7 @@ void GCFTCPPort::serviceRegistered(unsigned int result, unsigned int portNumber)
void GCFTCPPort::serviceInfo(unsigned int result, unsigned int portNumber, const string& host)
{
ASSERT(SAP == getType());
if (result == SB_UNKNOWN_SERVICE) {
LOG_DEBUG(formatString ("Cannot connect the local SAP [%s] "
"to remote (M)SPP [%s:%s]. Try again!!!",
......@@ -259,6 +259,7 @@ void GCFTCPPort::serviceInfo(unsigned int result, unsigned int portNumber, const
if (result == SB_NO_ERROR) {
_portNumber = portNumber;
_host = host;
LOG_DEBUG(formatString ("Can now connect the local SAP [%s] "
"to remote (M)SPP [%s:%s@%s:%d].",
makeServiceName().c_str(),
......
......@@ -35,12 +35,9 @@
#include <stdio.h>
#include <errno.h>
namespace LOFAR
{
namespace GCF
{
namespace TM
{
namespace LOFAR {
namespace GCF {
namespace TM {
GTMTCPSocket::GTMTCPSocket(GCFTCPPort& port) :
GTMFile(port)
......@@ -52,96 +49,108 @@ GTMTCPSocket::~GTMTCPSocket()
close();
}
//
// send(buf, count)
//
ssize_t GTMTCPSocket::send(void* buf, size_t count)
{
if (_fd > -1) {
ssize_t countLeft(count);
ssize_t written(0);
do {
written = ::write(_fd, ((char*)buf) + (count - countLeft), countLeft);
if (written == -1) {
if (errno != EINTR) {
LOG_WARN(LOFAR::formatString (
"send, error: %s",
strerror(errno)));
return -1;
}
}
else {
countLeft -= written;
}
} while (countLeft > 0);
return count;
}
else {
LOG_WARN("send, error: Socket not opend");
return -1;
}
if (_fd < 0) {
LOG_WARN("send, error: Socket not opened");
return (-1);
}
ssize_t countLeft(count);
ssize_t written(0);
do {
written = ::write(_fd, ((char*)buf) + (count - countLeft), countLeft);
if (written == -1) {
if (errno != EINTR) {
LOG_WARN(LOFAR::formatString ( "send, error: %s", strerror(errno)));
return -1;
}
}
else {
countLeft -= written;
}
} while (countLeft > 0);
return count;
}
//
// recv(buf, count)
//
ssize_t GTMTCPSocket::recv(void* buf, size_t count)
{
if (_fd > -1) {
ssize_t countLeft(count);
ssize_t received(0);
do {
received = ::read(_fd, ((char*)buf) + (count - countLeft), countLeft);
if (received == -1) {
if (errno != EINTR) {
LOG_WARN(formatString (
"recv, error: %s",
strerror(errno)));
return -1;
}
}
else {
countLeft -= received;
}
} while (countLeft > 0);
return count;
}
else {
LOG_WARN("recv, error: Socket not opend");
return -1;
}
if (_fd < 0) {
LOG_WARN("recv, error: Socket not opend");
return (-1);
}
ssize_t countLeft(count);
ssize_t received(0);
do {
received = ::read(_fd, ((char*)buf) + (count - countLeft), countLeft);
if (received == -1) {
if (errno != EINTR) {
LOG_WARN(formatString ( "recv, error: %s", strerror(errno)));
return -1;
}
}
else {
countLeft -= received;
}
} while (countLeft > 0);
return count;
}
//
// open
//
bool GTMTCPSocket::open(unsigned int /*portNumber*/)
{
ASSERT(_fd == -1);
_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_fd < 0) {
LOG_WARN(formatString ( "::socket, error: %s", strerror(errno)));
close();
}
return (_fd > -1);
LOG_TRACE_COND_STR("open:fd=" << _fd << ", port=" << _port.getName());
if (_fd >= 0) { // already open?
return (true);
}
_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (_fd < 0) {
LOG_WARN(formatString ( "::socket, error: %s", strerror(errno)));
close();
}
return (_fd > -1);
}
//
// connect(portnr, host)
//
bool GTMTCPSocket::connect(unsigned int portNumber, const string& host)
{
bool result(false);
if (_fd >= -1) {
struct sockaddr_in serverAddr;
struct hostent *hostinfo;
hostinfo = gethostbyname(host.c_str());
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
serverAddr.sin_port = htons(portNumber);
result = (::connect(_fd, (struct sockaddr *)&serverAddr,
sizeof(struct sockaddr_in)) == 0);
if (result) {
ASSERT(_pHandler);
_pHandler->registerFile(*this);
}
else {
LOG_WARN(formatString ( "connect, error: %s", strerror(errno)));
close();
}
}
return result;
LOG_TRACE_COND_STR("connect:fd=" << _fd << ", port=" << _port.getName());
struct sockaddr_in serverAddr;
struct hostent *hostinfo;
hostinfo = gethostbyname(host.c_str());
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
serverAddr.sin_port = htons(portNumber);
if (::connect(_fd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr_in)) != 0) {
if (errno != EISCONN) { // already connected is OK
LOG_WARN(formatString ( "connect, error: %s", strerror(errno)));
close();
return (false);
}
}
ASSERT(_pHandler);
_pHandler->registerFile(*this);
return (true);
}
} // namespace TM
} // namespace GCF
} // namespace LOFAR
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment