-
Notifications
You must be signed in to change notification settings - Fork 0
/
igtlTCPConnectorServerOIGTL.cxx
230 lines (184 loc) · 5.63 KB
/
igtlTCPConnectorServerOIGTL.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*=========================================================================
Program:
Module: $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlSocket.cxx $
Language: C++
Date: $Date: 2010-06-09 16:16:36 -0400 (Wed, 09 Jun 2010) $
Version: $Revision: 6525 $
Copyright (c) Brigham and Women's Hospital. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "igtlSocket.h"
#include <string.h>
#include "igtlTCPConnectorServerOIGTL.h"
#include "igtlOSUtil.h"
#include "igtlTransformMessage.h"
namespace igtl
{
//-----------------------------------------------------------------------------
TCPConnectorServerOIGTL::TCPConnectorServerOIGTL()
{
}
//-----------------------------------------------------------------------------
TCPConnectorServerOIGTL::~TCPConnectorServerOIGTL()
{
}
//-----------------------------------------------------------------------------
void TCPConnectorServerOIGTL::PrintSelf(std::ostream& os) const
{
this->Superclass::PrintSelf(os);
}
//-----------------------------------------------------------------------------
int TCPConnectorServerOIGTL::PushMessage(igtl::MessageBase * message)
{
if (this->Status == STATUS_CONNECTED)
{
if (this->Socket.IsNull())
{
// Do nothing.
return 1;
}
int r = this->Socket->Send(message->GetPackPointer(), message->GetPackSize());
if (r)
{
this->SetDataSentFlag();
return 1;
}
else
{
//std::cerr << "TCPConnectorServerOIGTL::PushMessage() : Connection Lost." << std::endl;
return 0;
}
}
// TODO: Does ReceiveMessage() in MonitorThreadFunction() returns 0 in this case?
return 0;
}
//-----------------------------------------------------------------------------
int TCPConnectorServerOIGTL::Initialize()
{
if (TCPConnectorServer::Initialize())
{
this->HeaderMsg = igtl::MessageHeader::New();
return 1;
}
this->HeaderMsg = NULL;
return 0;
}
//-----------------------------------------------------------------------------
int TCPConnectorServerOIGTL::ReceiveMessage()
{
//std::cerr << "TCPConnectorServerOIGTL::ReceiveMessage() : Waiting for messages." << std::endl;
// Initialize receive buffer
//this->HeaderMsg->InitPack();
igtl::MessageHeader::Pointer header;
header = igtl::MessageHeader::New();
header->InitPack();
if (this->Socket.IsNotNull() && this->Socket->GetConnected())
{
int r = this->Socket->Receive(header->GetPackPointer(),
header->GetPackSize());
if (r != header->GetPackSize())
{
if (r == 0)
{
// Connection closed
return 0;
}
// Wrong message but continue to communicate
//std::cerr << "TCPConnectorServerOIGTL::ReceiveMessage() : Illegal message." << std::endl;
return 1;
}
}
else
{
return 0;
}
// Deserialize the header
header->Unpack();
// Check data type and receive data body
if (strcmp(header->GetDeviceType(), "TRANSFORM") == 0)
{
return ReceiveTransform(header);
}
//else if (strcmp(header->GetDeviceType(), "POSITION") == 0)
// {
// ReceivePosition(socket, header);
// }
//else if (strcmp(header->GetDeviceType(), "IMAGE") == 0)
// {
// ReceiveImage(socket, header);
// }
//else if (strcmp(header->GetDeviceType(), "STATUS") == 0)
// {
// ReceiveStatus(socket, header);
// }
//else if (strcmp(header->GetDeviceType(), "POINT") == 0)
// {
// ReceivePoint(socket, header);
// }
//else if (strcmp(header->GetDeviceType(), "STRING") == 0)
// {
// ReceiveString(socket, header);
// }
//else if (strcmp(header->GetDeviceType(), "BIND") == 0)
// {
// ReceiveBind(socket, header);
// }
else
{
// if the data type is unknown, skip reading.
//std::cerr << "Receiving : " << header->GetDeviceType() << std::endl;
if (this->Socket.IsNotNull() && this->Socket->GetConnected())
{
this->Socket->Skip(header->GetBodySizeToRead(), 0);
}
}
return 1;
}
//-----------------------------------------------------------------------------
int TCPConnectorServerOIGTL::Finalize()
{
if (TCPConnectorServer::Finalize())
{
return 1;
}
return 0;
}
//-----------------------------------------------------------------------------
int TCPConnectorServerOIGTL::ReceiveTransform(igtl::MessageHeader * header)
{
//std::cerr << "TCPConnectorServerOIGTL::ReceiveTransform() : Receiving TRANSFORM data type." << std::endl;
// Create a message buffer to receive transform data
igtl::TransformMessage::Pointer transMsg;
transMsg = igtl::TransformMessage::New();
transMsg->SetMessageHeader(header);
transMsg->AllocatePack();
// Receive transform data from the socket
if (this->Socket.IsNotNull() && this->Socket->GetConnected())
{
int r = this->Socket->Receive(transMsg->GetPackBodyPointer(), transMsg->GetPackBodySize());
if (r == 0)
{
// Connection closed.
return 0;
}
}
else
{
return 0;
}
// Deserialize the transform data
// If you want to skip CRC check, call Unpack() without argument.
int c = transMsg->Unpack(1);
if (c & igtl::MessageHeader::UNPACK_BODY) // if CRC check is OK
{
if (this->OutputConnector.IsNotNull())
{
this->OutputConnector->PushMessage(transMsg);
}
return 1;
}
return 1;
}
} // End of igtl namespace