00001 /*========================================================================= 00002 00003 Program: Open IGT Link Library 00004 Module: $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlSocket.h $ 00005 Language: C++ 00006 Date: $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $ 00007 Version: $Revision: 3460 $ 00008 00009 Copyright (c) Insight Software Consortium. All rights reserved. 00010 00011 This software is distributed WITHOUT ANY WARRANTY; without even 00012 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00013 PURPOSE. See the above copyright notices for more information. 00014 00015 =========================================================================*/ 00016 /*========================================================================= 00017 00018 Program: Visualization Toolkit 00019 Module: $RCSfile: igtlSocket.h,v $ 00020 00021 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00022 All rights reserved. 00023 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00024 00025 This software is distributed WITHOUT ANY WARRANTY; without even 00026 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00027 PURPOSE. See the above copyright notice for more information. 00028 00029 =========================================================================*/ 00030 /*========================================================================= 00031 00032 Program: Open ITK Link Library 00033 Module: $RCSfile: itkImage.h,v $ 00034 Language: C++ 00035 Date: $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $ 00036 Version: $Revision: 3460 $ 00037 00038 Copyright (c) Insight Software Consortium. All rights reserved. 00039 00040 This software is distributed WITHOUT ANY WARRANTY; without even 00041 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00042 PURPOSE. See the above copyright notices for more information. 00043 00044 =========================================================================*/ 00045 00057 #ifndef __igtlSocket_h 00058 #define __igtlSocket_h 00059 00060 #include "igtlObject.h" 00061 #include "igtlObjectFactory.h" 00062 #include "igtlMacro.h" 00063 #include "igtlWin32Header.h" 00064 00065 namespace igtl 00066 { 00067 00068 class SocketCollection; 00069 00070 // class IGTL_EXPORT Socket 00071 class IGTLCommon_EXPORT Socket : public Object 00072 { 00073 public: 00074 typedef Socket Self; 00075 typedef Object Superclass; 00076 typedef SmartPointer<Self> Pointer; 00077 typedef SmartPointer<const Self> ConstPointer; 00078 00079 igtlTypeMacro(igtl::Socket, igtl::Object) 00080 igtlNewMacro(igtl::Socket); 00081 00082 void PrintSelf(std::ostream& os); 00083 00084 public: 00085 00086 // ----- Status API ---- 00087 // Description: 00088 // Check is the socket is alive. 00089 int GetConnected() { return (this->m_SocketDescriptor >=0); } 00090 00091 // Description: 00092 // Close the socket. 00093 void CloseSocket() {this->CloseSocket(this->m_SocketDescriptor);} 00094 00095 // ------ Communication API --- 00096 // Description: 00097 // These methods send data over the socket. 00098 // Returns 1 on success, 0 on error and raises vtkCommand::ErrorEvent. 00099 int Send(const void* data, int length); 00100 00101 // Description: 00102 // Receive data from the socket. 00103 // This call blocks until some data is read from the socket. 00104 // When readFully is set, this call will block until all the 00105 // requested data is read from the socket. 00106 // 0 on error, else number of bytes read is returned. On error, 00107 // vtkCommand::ErrorEvent is raised. 00108 int Receive(void* data, int length, int readFully=1); 00109 00110 // Description: 00111 // Skip reading data from the socket. 00112 // The Skip() call has been newly introduced to the igtlSocket, 00113 // after the class is imported from VTK, thus the call is 00114 // not available in vtkSocket class. 00115 int Skip(int length, int skipFully=1); 00116 00117 protected: 00118 Socket(); 00119 ~Socket(); 00120 00121 int m_SocketDescriptor; 00122 igtlGetMacro(SocketDescriptor, int); 00123 00124 //BTX 00125 friend class vtkSocketCollection; 00126 //ETX 00127 00128 // Description: 00129 // Creates an endpoint for communication and returns the descriptor. 00130 // -1 indicates error. 00131 int CreateSocket(); 00132 00133 // Description: 00134 // Close the socket. 00135 void CloseSocket(int socketdescriptor); 00136 00137 // Description: 00138 // Binds socket to a particular port. 00139 // Returns 0 on success other -1 is returned. 00140 int BindSocket(int socketdescriptor, int port); 00141 00142 // Description: 00143 // Selects a socket ie. waits for it to change status. 00144 // Returns 1 on success; 0 on timeout; -1 on error. msec=0 implies 00145 // no timeout. 00146 int SelectSocket(int socketdescriptor, unsigned long msec); 00147 00148 // Description: 00149 // Accept a connection on a socket. Returns -1 on error. Otherwise 00150 // the descriptor of the accepted socket. 00151 int Accept(int socketdescriptor); 00152 00153 // Description: 00154 // Listen for connections on a socket. Returns 0 on success. -1 on error. 00155 int Listen(int socketdescriptor); 00156 00157 // Description: 00158 // Connect to a server socket. Returns 0 on success, -1 on error. 00159 int Connect(int socketdescriptor, const char* hostname, int port); 00160 00161 // Description: 00162 // Returns the port to which the socket is connected. 00163 // 0 on error. 00164 int GetPort(int socketdescriptor); 00165 00166 // Description: 00167 // Selects set of sockets. Returns 0 on timeout, -1 on error. 00168 // 1 on success. Selected socket's index is returned thru 00169 // selected_index 00170 static int SelectSockets(const int* sockets_to_select, int size, 00171 unsigned long msec, int* selected_index); 00172 private: 00173 Socket(const Socket&); // Not implemented. 00174 void operator=(const Socket&); // Not implemented. 00175 }; 00176 00177 } 00178 00179 #endif 00180