00001 // ***************************************************************************** 00002 // oaSocket.h 00003 // 00004 // This file contains the definition for the oaSocket class, and its public 00005 // supporting classes. The oaSocket utility class implements an object for 00006 // managing a TCP stream socket. 00007 // 00008 // The supporting classes include: 00009 // 00010 // oaSocket 00011 // This class includes operations and data for using sockets. 00012 // 00013 // oaClientSocket 00014 // This class includes operations and data common to client side sockets. 00015 // 00016 // oaServerSocket 00017 // This class includes operations and data common to server side sockets. 00018 // 00019 // oaFDSet 00020 // This class manages a file descriptor set, which is used for select 00021 // operations. 00022 // 00023 // ***************************************************************************** 00024 // Except as specified in the OpenAccess terms of use of Cadence or Silicon 00025 // Integration Initiative, this material may not be copied, modified, 00026 // re-published, uploaded, executed, or distributed in any way, in any medium, 00027 // in whole or in part, without prior written permission from Cadence. 00028 // 00029 // Copyright 2002-2005 Cadence Design Systems, Inc. 00030 // All Rights Reserved. 00031 // 00032 // $Author: icftcm $ 00033 // $Revision: #1 $ 00034 // $Date: 2010/08/09 $ 00035 // $State: Exp $ 00036 // ***************************************************************************** 00037 // ***************************************************************************** 00038 00039 00040 00041 #if !defined(oaSocket_P) 00042 #define oaSocket_P 00043 00044 00045 00046 // ***************************************************************************** 00047 // Nested includes 00048 // ***************************************************************************** 00049 #include "oaString.h" 00050 #if defined(OA_WINDOWS) 00051 #include <WinSock2.h> 00052 #include <Windows.h> 00053 #else 00054 #include <netdb.h> 00055 #include <sys/socket.h> 00056 #include <arpa/inet.h> 00057 #include <sys/param.h> 00058 #include <unistd.h> 00059 #include <dlfcn.h> 00060 #include <sys/socket.h> 00061 #include <netinet/in.h> 00062 #include <sys/ipc.h> 00063 #endif 00064 00065 #if defined(HPUX) 00066 #include <dl.h> 00067 #endif 00068 00069 #include <errno.h> 00070 00071 00072 00073 // ***************************************************************************** 00074 // Declare and define types in the OpenAccess namespace. 00075 // ***************************************************************************** 00076 BEGIN_OA_NAMESPACE 00077 00078 00079 00080 // ***************************************************************************** 00081 // Forward Class Declarations 00082 // ***************************************************************************** 00083 class oaString; 00084 00085 00086 00087 // ***************************************************************************** 00088 // Defines 00089 // ***************************************************************************** 00090 #if !defined(OA_WINDOWS) 00091 #define oacHostNameLength MAXHOSTNAMELEN 00092 #else 00093 #define oacHostNameLength 256 00094 #endif 00095 00096 #define oacInvalidSocket UINT_MAX 00097 #define oacInvalidSocketAddress UINT_MAX 00098 #define oacSocketError -1 00099 00100 00101 00102 // ***************************************************************************** 00103 // oaSocket 00104 // ***************************************************************************** 00105 class OA_BASE_DLL_API oaSocket { 00106 public: 00107 oaSocket(); 00108 ~oaSocket(); 00109 00110 oaBoolean isOpen() const; 00111 oaSocketD getSocketD() const; 00112 oaUInt4 getPort() const; 00113 00114 void setAddress(oaUInt4 sockAddr); 00115 void setAddress(const char *address); 00116 void setPort(oaUInt4 port); 00117 00118 static void sendMsg(oaSocketD sd, 00119 oaUInt4 len, 00120 const char *msg); 00121 static oaUInt4 recvMsg(oaSocketD sd, 00122 oaUInt4 len, 00123 char *msg); 00124 static void close(oaSocketD sd); 00125 static void getHostName(oaString &hostName); 00126 00127 protected: 00128 static void initialize(); 00129 static void finalize(); 00130 00131 oaSocketD sd; 00132 sockaddr_in addr; 00133 00134 static const oaUInt4 domain; 00135 static const oaUInt4 type; 00136 static const oaUInt4 protocol; 00137 }; 00138 00139 00140 00141 // ***************************************************************************** 00142 // oaClientSocket 00143 // ***************************************************************************** 00144 class OA_BASE_DLL_API oaClientSocket : public oaSocket { 00145 public: 00146 oaBoolean connect(const char *address, 00147 oaUInt4 port); 00148 oaBoolean isPortNumAvailable(const char *address, 00149 oaUInt4 port); 00150 void close(); 00151 00152 static void setConnectionTimeout(oaUInt4 timeoutVal); 00153 static oaUInt4 getConnectionTimeout(); 00154 00155 private: 00156 oaBoolean connectWithTimeout(); 00157 00158 static oaUInt4 s_connectionTimeout; 00159 }; 00160 00161 00162 00163 // ***************************************************************************** 00164 // oaServerSocket 00165 // ***************************************************************************** 00166 class OA_BASE_DLL_API oaServerSocket : public oaSocket { 00167 public: 00168 oaServerSocket(oaUInt4 port); 00169 00170 oaSocketD accept(); 00171 void bind(); 00172 void listen(); 00173 00174 private: 00175 static const oaUInt4 listenQ; 00176 }; 00177 00178 00179 00180 // ***************************************************************************** 00181 // oaFDSet 00182 // ***************************************************************************** 00183 class OA_BASE_DLL_API oaFDSet { 00184 public: 00185 oaFDSet(); 00186 00187 void add(oaUInt4 fd); 00188 void remove(oaUInt4 fd); 00189 00190 oaUInt4 getNextReady(); 00191 oaUInt4 select(oaUInt4 sec); 00192 00193 private: 00194 fd_set fdSet; 00195 oaUInt4 maxFD; 00196 oaUInt4 minFD; 00197 }; 00198 00199 00200 00201 END_OA_NAMESPACE 00202 00203 #endif
Copyright © 2002 - 2010 Cadence Design Systems, Inc.
All Rights Reserved.