Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Source/igtlBindMessage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ int BindMessage::UnpackContent()

}

igtl_bind_free_info(&bind_info);

return 1;
}

Expand Down
16 changes: 8 additions & 8 deletions Source/igtlMessageHandlerMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ using igtl::SmartPointer;
igtlTypeMacro(classname, ::igtl::MessageHandler); \
igtlNewMacro(classname); \
public: \
virtual const char* GetMessageType() \
virtual const char* GetMessageType() override \
{ \
return this->m_Message->GetDeviceType(); \
} \
virtual int Process(messagetype*, datatype*); \
igtl_uint64 ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) \
igtl_uint64 ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) override \
{ \
if (pos == 0) /* New body */ \
{ \
this->m_Message->SetMessageHeader(header); \
this->m_Message->AllocateBuffer(); \
} \
bool timeout(false); \
bool error(false); \
igtl_uint64 s = socket->Receive((void*)((char*)this->m_Message->GetBufferBodyPointer()+pos), \
this->m_Message->GetBufferBodySize()-pos, timeout); \
if (timeout) /* Time out */ \
this->m_Message->GetBufferBodySize()-pos, error); \
if (error) /* Not time out */ \
{ \
return pos; \
return -1; \
} \
if (s+pos >= this->m_Message->GetBufferBodySize()) \
{ \
Expand Down Expand Up @@ -114,12 +114,12 @@ using igtl::SmartPointer;
{ \
return this->m_Message->GetMessageType(); \
} \
virtual const char* GetMessageType() \
virtual const char* GetMessageType() override \
{ \
return this->m_Message->GetDeviceType(); \
} \
virtual int Process(messagetype*, datatype*); \
igtl_uint64 ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) \
igtl_uint64 ReceiveMessage(::igtl::Socket* socket, ::igtl::MessageBase* header, int pos) override \
{ \
if (pos == 0) /* New body */ \
{ \
Expand Down
21 changes: 17 additions & 4 deletions Source/igtlNDArrayMessage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ NDArrayMessage::NDArrayMessage():

NDArrayMessage::~NDArrayMessage()
{
if (m_Array != NULL)
{
delete m_Array;
m_Array = NULL;
}
}


Expand All @@ -165,16 +170,20 @@ int NDArrayMessage::SetArray(int type, ArrayBase * a)

if (a)
{
this->m_Array = a;
return 1;
if (this->m_Array)
{
delete this->m_Array;
}

this->m_Array = a;
return 1;
}
else
{
return 0;
}
}


igtlUint64 NDArrayMessage::CalculateContentBufferSize()
{
igtlUint64 dataSize;
Expand Down Expand Up @@ -231,6 +240,8 @@ int NDArrayMessage::PackContent()
memcpy(info.array, this->m_Array->GetRawArray(), this->m_Array->GetRawArraySize());
igtl_ndarray_pack(&info, this->m_Content, IGTL_TYPE_PREFIX_NONE);

igtl_ndarray_free_info(&info);

return 1;
}

Expand Down Expand Up @@ -285,7 +296,9 @@ int NDArrayMessage::UnpackContent()

this->m_Array->SetSize(size);
memcpy(this->m_Array->GetRawArray(), info.array, this->m_Array->GetRawArraySize());


igtl_ndarray_free_info(&info);

return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions Source/igtlNDArrayMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class IGTLCommon_EXPORT ArrayBase

protected:
ArrayBase();
~ArrayBase();

public:
virtual ~ArrayBase();

/// Sets the size of the N-D array. Returns non-zero value, if success.
int SetSize(IndexType size);
Expand All @@ -47,7 +47,7 @@ class IGTLCommon_EXPORT ArrayBase
IndexType GetSize() { return this->m_Size; };

/// Gets the dimension of the N-D array.
int GetDimension() { return this->m_Size.size(); };
int GetDimension() { return static_cast<int>(this->m_Size.size()); };

/// Sets an array from a byte array. Size and dimension must be specified prior to
/// calling the SetArray() function.
Expand Down
33 changes: 19 additions & 14 deletions Source/igtlSessionManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ int SessionManager::ProcessMessage()
this->m_Header->InitBuffer();

// Receive generic header from the socket
bool timeout(false);
igtl_uint64 r = this->m_Socket->Receive(this->m_Header->GetBufferPointer(), this->m_Header->GetBufferSize(), timeout, 0);
bool error(false);
igtl_uint64 r = this->m_Socket->Receive(this->m_Header->GetBufferPointer(), this->m_Header->GetBufferSize(), error, 0);
if (r == 0)
{
this->m_CurrentReadIndex = 0;
Expand All @@ -197,7 +197,7 @@ int SessionManager::ProcessMessage()
if (r != this->m_Header->GetBufferSize())
{
// Only a part of header has arrived.
if (timeout) // timeout
if (error) // Error, not timeout
{
this->m_CurrentReadIndex = 0;
}
Expand All @@ -213,9 +213,9 @@ int SessionManager::ProcessMessage()
else if (this->m_CurrentReadIndex < IGTL_HEADER_SIZE)
{
// Message transfer was interrupted in the header
bool timeout(false);
bool error(false);
igtl_uint64 r = this->m_Socket->Receive((void*)((char*)this->m_Header->GetBufferPointer()+this->m_CurrentReadIndex),
this->m_Header->GetBufferSize()-this->m_CurrentReadIndex, timeout, 0);
this->m_Header->GetBufferSize()-this->m_CurrentReadIndex, error, 0);
if (r == 0)
{
this->m_CurrentReadIndex = 0;
Expand Down Expand Up @@ -290,15 +290,15 @@ int SessionManager::ProcessMessage()

igtl_uint64 r = this->m_CurrentMessageHandler->ReceiveMessage(this->m_Socket, this->m_Header,
this->m_CurrentReadIndex-IGTL_HEADER_SIZE);
if (r == this->m_Header->GetBodySizeToRead())
{
this->m_CurrentReadIndex = 0;
this->m_HeaderDeserialized = 0;
}
else
{
this->m_CurrentReadIndex += IGTL_HEADER_SIZE + r;
}

if (r == this->m_Header->GetBodySizeToRead() || r == -1 /*error*/)
{
ResetMessageProcessing();
}
else // Only a part of the body has arrived
{
this->m_CurrentReadIndex = IGTL_HEADER_SIZE + r;
}

return 1;
}
Expand All @@ -317,5 +317,10 @@ int SessionManager::PushMessage(MessageBase* message)
}
}

void SessionManager::ResetMessageProcessing()
{
this->m_CurrentReadIndex = 0;
this->m_HeaderDeserialized = 0;
}

}
1 change: 1 addition & 0 deletions Source/igtlSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class IGTLCommon_EXPORT SessionManager: public Object
int Disconnect();
int ProcessMessage();
int PushMessage(MessageBase*);
void ResetMessageProcessing();

protected:
SessionManager();
Expand Down
27 changes: 16 additions & 11 deletions Source/igtlSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ int Socket::Send(const void* data, igtlUint64 length)
}

//-----------------------------------------------------------------------------
igtlUint64 Socket::Receive(void* data, igtlUint64 length, bool& timeout, int readFully/*=1*/)
igtlUint64 Socket::Receive(void* data, igtlUint64 length, bool& error, int readFully/*=1*/)
{
if (!this->GetConnected())
{
timeout = false;
error = false;
return 0;
}

Expand All @@ -365,16 +365,15 @@ igtlUint64 Socket::Receive(void* data, igtlUint64 length, bool& timeout, int rea
#if defined(_WIN32) && !defined(__CYGWIN__)
int trys = 0;
#endif

int n = recv(this->m_SocketDescriptor, buffer + total, (length > std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : length - total), 0);

int readLength = (length > std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : length - total);
int n = recv(this->m_SocketDescriptor, buffer + total, readLength, 0);
#if defined(_WIN32) && !defined(__CYGWIN__)
if(n == 0)
{
// On long messages, Windows recv sometimes fails with WSAENOBUFS, but
// will work if you try again.
int error = WSAGetLastError();
if ((error == WSAENOBUFS) && (trys++ < 1000))
int lastError = WSAGetLastError();
if ((lastError == WSAENOBUFS) && (trys++ < 1000))
{
Sleep(1);
continue;
Expand All @@ -385,8 +384,13 @@ igtlUint64 Socket::Receive(void* data, igtlUint64 length, bool& timeout, int rea
else if (n < 0)
{
// TODO: Need to check if this means timeout.
timeout = true;
return 0;
int errCode = WSAGetLastError();
if (errCode != WSAETIMEDOUT)
{
error = true;
return 0;
}
return total;
}
#else
if(n == 0) // Disconnected
Expand All @@ -397,13 +401,14 @@ igtlUint64 Socket::Receive(void* data, igtlUint64 length, bool& timeout, int rea
else if (n < 0) // Error (including time out)
{
// TODO: If it is time-out, errno == EAGAIN
timeout = true;
error = true;
return 0;
}
#endif

total += n;
} while(readFully && total < length);

} while(readFully && total < length);
return total;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/igtlSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class IGTLCommon_EXPORT Socket : public Object
/// read from the socket. The readFully flag will be ignored if the timeout is active.
/// 0 on error, else number of bytes read is returned.
/// On timeout, the timeout variable will be set to true and the return value is ignored
igtlUint64 Receive(void* data, igtlUint64 length, bool& timeout, int readFully=1);
igtlUint64 Receive(void* data, igtlUint64 length, bool& error, int readFully=1);

/// Set sending/receiving timeout for the existing socket in millisecond.
/// This function should be called after opening the socket.
Expand Down