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: 2010-06-09 16:16:36 -0400 (Wed, 09 Jun 2010) $ 00007 Version: $Revision: 6525 $ 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 00042 #ifndef __igtlSocket_h 00043 #define __igtlSocket_h 00044 00045 #include "igtlObject.h" 00046 #include "igtlObjectFactory.h" 00047 #include "igtlMacro.h" 00048 #include "igtlWin32Header.h" 00049 00050 namespace igtl 00051 { 00052 00053 class SocketCollection; 00054 00055 // class IGTL_EXPORT Socket 00056 class IGTLCommon_EXPORT Socket : public Object 00057 { 00058 public: 00059 typedef Socket Self; 00060 typedef Object Superclass; 00061 typedef SmartPointer<Self> Pointer; 00062 typedef SmartPointer<const Self> ConstPointer; 00063 00064 igtlTypeMacro(igtl::Socket, igtl::Object) 00065 igtlNewMacro(igtl::Socket); 00066 00067 public: 00068 00069 // ----- Status API ---- 00070 // Description: 00071 // Check is the socket is alive. 00072 int GetConnected() { return (this->m_SocketDescriptor >=0); } 00073 00074 // Description: 00075 // Close the socket. 00076 void CloseSocket() {this->CloseSocket(this->m_SocketDescriptor);} 00077 00078 // ------ Communication API --- 00079 // Description: 00080 // These methods send data over the socket. 00081 // Returns 1 on success, 0 on error and raises vtkCommand::ErrorEvent. 00082 int Send(const void* data, int length); 00083 00084 // Description: 00085 // Receive data from the socket. 00086 // This call blocks until some data is read from the socket. 00087 // When readFully is set, this call will block until all the 00088 // requested data is read from the socket. 00089 // 0 on error, else number of bytes read is returned. On error, 00090 // vtkCommand::ErrorEvent is raised. 00091 int Receive(void* data, int length, int readFully=1); 00092 00093 // Description: 00094 // Skip reading data from the socket. 00095 // The Skip() call has been newly introduced to the igtlSocket, 00096 // after the class is imported from VTK, thus the call is 00097 // not available in vtkSocket class. 00098 int Skip(int length, int skipFully=1); 00099 00100 protected: 00101 Socket(); 00102 ~Socket(); 00103 00104 void PrintSelf(std::ostream& os) const; 00105 00106 int m_SocketDescriptor; 00107 igtlGetMacro(SocketDescriptor, int); 00108 00109 //BTX 00110 friend class vtkSocketCollection; 00111 //ETX 00112 00113 // Description: 00114 // Creates an endpoint for communication and returns the descriptor. 00115 // -1 indicates error. 00116 int CreateSocket(); 00117 00118 // Description: 00119 // Close the socket. 00120 void CloseSocket(int socketdescriptor); 00121 00122 // Description: 00123 // Binds socket to a particular port. 00124 // Returns 0 on success other -1 is returned. 00125 int BindSocket(int socketdescriptor, int port); 00126 00127 // Description: 00128 // Selects a socket ie. waits for it to change status. 00129 // Returns 1 on success; 0 on timeout; -1 on error. msec=0 implies 00130 // no timeout. 00131 int SelectSocket(int socketdescriptor, unsigned long msec); 00132 00133 // Description: 00134 // Accept a connection on a socket. Returns -1 on error. Otherwise 00135 // the descriptor of the accepted socket. 00136 int Accept(int socketdescriptor); 00137 00138 // Description: 00139 // Listen for connections on a socket. Returns 0 on success. -1 on error. 00140 int Listen(int socketdescriptor); 00141 00142 // Description: 00143 // Connect to a server socket. Returns 0 on success, -1 on error. 00144 int Connect(int socketdescriptor, const char* hostname, int port); 00145 00146 // Description: 00147 // Returns the port to which the socket is connected. 00148 // 0 on error. 00149 int GetPort(int socketdescriptor); 00150 00151 // Description: 00152 // Selects set of sockets. Returns 0 on timeout, -1 on error. 00153 // 1 on success. Selected socket's index is returned thru 00154 // selected_index 00155 static int SelectSockets(const int* sockets_to_select, int size, 00156 unsigned long msec, int* selected_index); 00157 private: 00158 Socket(const Socket&); // Not implemented. 00159 void operator=(const Socket&); // Not implemented. 00160 }; 00161 00162 } 00163 00164 #endif 00165