00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __UT_NetSocket__
00022 #define __UT_NetSocket__
00023
00024 #include "UT_API.h"
00025 #ifdef WIN32
00026 #include <winsock.h>
00027 #endif
00028 #include <SYS/SYS_Time.h>
00029 #include "UT_SysClone.h"
00030
00031 class UT_WorkBuffer;
00032
00033 class UT_API UT_NetSocket
00034 {
00035 public:
00036 enum
00037 {
00038 UT_CONNECT_SUCCESS = 0,
00039
00040 UT_WOULD_BLOCK = -1,
00041 UT_BAD_ADDRESS = -2,
00042 UT_CONNECT_FAILED = -3,
00043 UT_ERROR_OCCURED = -4,
00044 UT_WRONG_SOCKET = -5,
00045 UT_NO_CONNECTION = -6
00046 };
00047
00048 enum
00049 {
00050 UT_SHUTDOWN_RECEIVE = 0,
00051 UT_SHUTDOWN_SEND = 1,
00052 UT_SHUTDOWN_BOTH = 2
00053 };
00054
00055 static int getPortByService(const char *service, const char *proto="tcp",
00056 int default_port = 0);
00057
00058 static void getHostName(char *name, int max);
00059
00060
00061 static int getHostAddress(unsigned char address[4],
00062 const char *hostname = 0);
00063
00064
00065
00066 static bool getHostNameByAlias(UT_String &host, const char *alias = NULL);
00067
00068
00069 static int mapToUnprivilegedPort(int port);
00070
00071
00072
00073
00074
00075
00076
00077 static bool sendCommandAndGetResult(int port,
00078 const char *command,
00079 UT_WorkBuffer &response,
00080 const char *host_name = 0,
00081 bool remap_privileged_ports = true);
00082
00083 static bool nonBlockingSendCommandAndGetResult(int port,
00084 const char *command,
00085 UT_WorkBuffer &response,
00086 const char *host_name = 0,
00087 bool remap_privileged_ports = true);
00088
00089
00090
00091
00092
00093
00094
00095
00096 static UT_NetSocket *newSocket(int port, bool blocking = false,
00097 bool portisonlyhint = false);
00098 static UT_NetSocket *newSocketFromAddr(
00099 const char *address,
00100 int port,
00101 bool blocking = false,
00102 int localport = -1,
00103 int forcesocket=0);
00104 static void fdZero(fd_set *set);
00105 static void fdSet(int fd, fd_set *set);
00106 static void fdClr(int fd, fd_set *set);
00107 static int fdIsSet(int fd, fd_set *set);
00108
00109
00110
00111
00112
00113 static int select(int fd, fd_set *r_set, fd_set *w_set,
00114 fd_set *e_set, SYS_TimeVal *tv,
00115 bool forcesocket=false);
00116
00117 static int selectInMS(int maxfd,
00118 fd_set *r_set,
00119 fd_set *w_set,
00120 fd_set *e_set,
00121 int timeoutms = 0,
00122 bool forcesocket = false)
00123 {
00124 SYS_TimeVal tv;
00125 tv.tv_sec = timeoutms/1000;
00126 tv.tv_usec = (timeoutms*1000) % 1000000;
00127 return select(maxfd, r_set, w_set, e_set, &tv, forcesocket);
00128 }
00129 bool isSocketSelected(fd_set &set);
00130
00131 void addToFDSet(fd_set *set, int &maxfd);
00132
00133
00134 virtual ~UT_NetSocket();
00135
00136
00137
00138
00139
00140
00141 virtual UT_NetSocket *accept(int blocking, int &condition);
00142
00143
00144
00145
00146
00147
00148
00149 virtual int connect(int timeout_ms = 0);
00150
00151
00152 virtual int close();
00153
00154 virtual int shutdown(int type);
00155
00156
00157
00158
00159
00160 virtual int write(const void *data,int len,int *numWritten = 0);
00161
00162
00163
00164
00165 virtual int dataAvailable(int timeout=0);
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 virtual int read(void *data, int len, int *numRead = 0,
00180 int timeout_ms = -1);
00181
00182
00183 virtual int read(UT_WorkBuffer &data, int timeout_ms = -1,
00184 bool wait_for_null_terminator = false);
00185
00186
00187 virtual int peek(void *data, int len, int timeout_ms = -1);
00188
00189
00190 virtual int flushRead();
00191 virtual int flushWrite();
00192
00193
00194 void terminateOnConnectionLost(bool term = true);
00195
00196
00197
00198
00199
00200
00201
00202 const char *getAddress() const
00203 { return myAddressName ? myAddressName:""; }
00204
00205 int getPort() const
00206 { return myAddressPort; }
00207
00208
00209
00210 virtual int getRemoteSocket(char *host, int &port) const;
00211
00212
00213 virtual int setBlocking(bool blocking);
00214
00215
00216 virtual int isValid();
00217
00218
00219 int isConnected();
00220
00221
00222 int getSocket() const;
00223
00224 uint32 getRemoteIP4() const { return myRemoteIP4; }
00225 bool getRemoteIP4(int ip[4]);
00226 uint32 getLocalIP4() const;
00227 bool getLocalIP4(int ip[4]);
00228
00229 protected:
00230
00231
00232 int closeInetSocket();
00233
00234
00235 UT_NetSocket(UT_NetSocket *netsocket, int socket, bool blocking);
00236 UT_NetSocket(int port, bool blocking = false, bool portisonlyhint=false);
00237 UT_NetSocket(const char *address, int port, bool blocking = false,
00238 int localport = -1);
00239
00240
00241 UT_NetSocket();
00242
00243
00244
00245
00246
00247
00248 int waitForDataOrTimeout( int timeout_ms );
00249
00250 private:
00251
00252
00253
00254 int connectOrTimeout(struct sockaddr_in *address,
00255 int address_length, int timeout_ms);
00256
00257
00258
00259 int doConnect( int socket, struct sockaddr_in *address,
00260 int address_length );
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 int checkDataStatus(bool *fd_read, bool *fd_write,
00272 bool *fd_error, int timeout_ms);
00273
00274
00275
00276 static bool commonGetHostNameByAlias( UT_String &host,
00277 const char *alias );
00278
00279 protected:
00280
00281 char *myAddressName;
00282 int myAddressPort;
00283 int mySocket;
00284 uint32 myRemoteIP4;
00285
00286
00287 unsigned char myIsServer :1,
00288 myConnected :1,
00289 myShmFlag :1,
00290 myTermOnLost :1,
00291 myIsBlocking :1;
00292 };
00293
00294 class UT_API UT_AutoSocketDeleter
00295 {
00296 public:
00297 UT_AutoSocketDeleter(UT_NetSocket *socket) : mySocket(socket) {}
00298 ~UT_AutoSocketDeleter() { delete mySocket; }
00299 private:
00300 UT_NetSocket *mySocket;
00301 };
00302
00303 #endif