GCC Code Coverage Report


Directory: ./
File: libs/io/igtl/patch/igtlSocket.h
Date: 2024-03-28 16:25:37
Exec Total Coverage
Lines: 1 1 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 // NOLINTBEGIN
2 /*=========================================================================
3
4 Program: The OpenIGTLink Library
5 Language: C++
6 Web page: http://openigtlink.org/
7
8 Copyright (c) Insight Software Consortium. All rights reserved.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notices for more information.
13
14 =========================================================================*/
15 /*=========================================================================
16
17 Program: Visualization Toolkit
18 Module: $RCSfile: igtlSocket.h,v $
19
20 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
21 All rights reserved.
22 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
23
24 This software is distributed WITHOUT ANY WARRANTY; without even
25 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26 PURPOSE. See the above copyright notice for more information.
27
28 =========================================================================*/
29
30 /** \class Socket
31 * \brief BSD socket encapsulation.
32 *
33 * This abstract class encapsulates a BSD socket.
34 * It provides an API for basic socket operations.
35 *
36 * This class was largely based on the igtlSocket class
37 * from the Visualization Toolkit VTK.
38 *
39 */
40
41
42 // NOTE: This is a patched version to give a public access to the socket descriptor.
43 // This header must be included first BEFORE official igtlSocket.h
44
45 #ifndef __igtlSocket_h
46 #define __igtlSocket_h
47
48 #include "igtlObject.h"
49 #include "igtlObjectFactory.h"
50 #include "igtlMacro.h"
51 #include "igtlWin32Header.h"
52
53
54 #if defined(_WIN32) && !defined(__CYGWIN__)
55 #else
56 #include <sys/time.h>
57 #endif
58
59
60 namespace igtl
61 {
62
63 class SocketCollection;
64
65 /// class IGTL_EXPORT Socket
66 class IGTLCommon_EXPORT Socket : public Object
67 {
68 public:
69 typedef Socket Self;
70 typedef Object Superclass;
71 typedef SmartPointer<Self> Pointer;
72 typedef SmartPointer<const Self> ConstPointer;
73
74 igtlTypeMacro(igtl::Socket, igtl::Object)
75 igtlNewMacro(igtl::Socket);
76
77 /// Check is the socket is alive.
78
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
18 int GetConnected() const { return (this->m_SocketDescriptor >=0); }
79
80 /// Close the socket.
81 void CloseSocket() {
82 this->CloseSocket(this->m_SocketDescriptor);
83 this->m_SocketDescriptor = -1;
84 }
85
86 /// These methods send data over the socket.
87 /// Returns 1 on success, 0 on error and raises vtkCommand::ErrorEvent.
88 /// SIGPIPE or other signal may be raised on systems (e.g., Sun Solaris) where
89 /// MSG_NOSIGNAL flag is not supported for the socket send method.
90 int Send(const void* data, int length);
91
92 /// Receive data from the socket.
93 /// This call blocks until some data is read from the socket, unless timeout is set
94 /// by SetTimeout() or SetReceiveTimeout().
95 /// When the readFully flag is set, this call will block until all the requested data is
96 /// read from the socket. The readFully flag will be ignored if the timeout is active.
97 /// 0 on error, -1 on timeout, else number of bytes read is returned.
98 int Receive(void* data, int length, int readFully=1);
99
100 /// Set sending/receiving timeout for the existing socket in millisecond.
101 /// This function should be called after opening the socket.
102 int SetTimeout(int timeout);
103
104 /// Set reciving timeout for the existing socket in millisecond.
105 /// This function should be called after opening the socket.
106 int SetReceiveTimeout(int timeout);
107
108 /// Set sending timeout for the existing socket in millisecond.
109 /// This function should be called after opening the socket.
110 int SetSendTimeout(int timeout);
111
112 /// Set (psuedo) non-blocking mode for recv(). When sw=1, the time out is set to
113 /// minimum value (1 microsecond in UNIX, 1 millisecond in Windows) for receiving.
114 int SetReceiveBlocking(int sw);
115
116 /// Set (psuedo) non-blocking mode for recv(). When sw=1, the time out is set to
117 /// minimum value (1 microsecond in UNIX, 1 millisecond in Windows) for sending.
118 int SetSendBlocking(int sw);
119
120 /// Get socket address
121 int GetSocketAddressAndPort(std::string& address, int & port);
122
123 /// Skip reading data from the socket.
124 /// The Skip() call has been newly introduced to the igtlSocket,
125 /// after the class is imported from VTK, thus the call is
126 /// not available in vtkSocket class.
127 int Skip(int length, int skipFully=1);
128
129 int m_SocketDescriptor;
130 igtlGetMacro(SocketDescriptor, int);
131
132 protected:
133 Socket();
134 ~Socket() override;
135
136 void PrintSelf(std::ostream& os) const override;
137
138 //BTX
139 friend class vtkSocketCollection;
140 //ETX
141
142 /// Creates an endpoint for communication and returns the descriptor.
143 /// -1 indicates error.
144 int CreateSocket();
145
146 /// Close the socket.
147 void CloseSocket(int socketdescriptor);
148
149 /// Binds socket to a particular port.
150 /// Returns 0 on success other -1 is returned.
151 int BindSocket(int socketdescriptor, int port);
152
153 /// Selects a socket ie. waits for it to change status.
154 /// Returns 1 on success; 0 on timeout; -1 on error. msec=0 implies
155 /// no timeout.
156 int SelectSocket(int socketdescriptor, unsigned long msec);
157
158 /// Accept a connection on a socket. Returns -1 on error. Otherwise
159 /// the descriptor of the accepted socket.
160 int Accept(int socketdescriptor);
161
162 /// Listen for connections on a socket. Returns 0 on success. -1 on error.
163 int Listen(int socketdescriptor);
164
165 /// Connect to a server socket. Returns 0 on success, -1 on error.
166 int Connect(int socketdescriptor, const char* hostname, int port);
167
168 /// Returns the port to which the socket is connected.
169 /// 0 on error.
170 int GetPort(int socketdescriptor);
171
172 /// Selects set of sockets. Returns 0 on timeout, -1 on error.
173 /// 1 on success. Selected socket's index is returned thru
174 /// selected_index
175 static int SelectSockets(const int* sockets_to_select, int size,
176 unsigned long msec, int* selected_index);
177
178 private:
179 Socket(const Socket&); // Not implemented.
180 void operator=(const Socket&); // Not implemented.
181
182 #if defined(_WIN32) && !defined(__CYGWIN__)
183 DWORD m_SendTimeout;
184 DWORD m_ReceiveTimeout;
185 DWORD m_OrigSendTimeout;
186 DWORD m_OrigReceiveTimeout;
187 #else
188 struct timeval m_SendTimeout;
189 struct timeval m_ReceiveTimeout;
190 struct timeval m_OrigSendTimeout;
191 struct timeval m_OrigReceiveTimeout;
192 #endif
193 int m_SendTimeoutFlag;
194 int m_ReceiveTimeoutFlag;
195
196 };
197
198 }
199
200 #endif
201
202 // NOLINTEND
203