00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: socket__ew_8h-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.2 2000/03/05 21:54:17 lombard 00011 * Added definition of INADDR_NONE for Solaris 00012 * Added `include <errno.h>' for Solaris so EINPROGRESS would be defined 00013 * Placed `ifndef min' around min to prevent redefinition errors 00014 * Added missing prototype for recv_all(). 00015 * 00016 * Revision 1.1 2000/02/14 20:05:54 lucky 00017 * Initial revision 00018 * 00019 * 00020 */ 00021 00022 #ifndef SOCKET_EW_H 00023 #define SOCKET_EW_H 00024 00025 /**************************************************************/ 00026 /* #defines */ 00027 /**************************************************************/ 00028 #define SOCKET_CLOSE_IMMEDIATELY_EW 0 00029 #define SOCKET_CLOSE_GRACEFULLY_EW -1 00030 #define SOCKET_CLOSE_SIMPLY_EW -2 00031 00032 #ifdef _WINNT 00033 #include <windows.h> 00034 #include <winsock2.h> 00035 # define WOULDBLOCK_EW WSAEWOULDBLOCK 00036 # define CONNECT_WOULDBLOCK_EW WSAEWOULDBLOCK 00037 typedef unsigned __int64 Time_ew; 00038 # define TIMEOUT_ADJUSTMENT_ew 10000 /* Convert from millisec. to 00039 100 nanosec. */ 00040 #endif /* _WINNT */ 00041 00042 /* #ifdef __sun __sun isn't defined on x86Solaris 2.6 */ 00043 # ifdef _SOLARIS 00044 # include <sys/filio.h> 00045 # include <sys/types.h> 00046 # include <sys/times.h> 00047 # include <sys/param.h> 00048 # include <errno.h> /* Needed so EINPROGRESS is defined */ 00049 # define SOCKET int 00050 # define ioctlsocket ioctl 00051 # define closesocket close 00052 # define FAR 00053 # define INVALID_SOCKET -1 00054 # define SOCKET_ERROR -1 00055 # define WOULDBLOCK_EW EWOULDBLOCK 00056 # define CONNECT_WOULDBLOCK_EW EINPROGRESS 00057 typedef unsigned int Time_ew; 00058 # define TIMEOUT_ADJUSTMENT_ew 1/1000 /* Convert from millisec. to sec. */ 00059 #ifndef INADDR_NONE 00060 #define INADDR_NONE 0xffffffff /* should be in <netinet/in.h> */ 00061 #endif 00062 #endif /* _SOLARIS */ 00063 00064 #ifndef min 00065 #define min(a, b) (((a) < (b)) ? (a) : (b)) 00066 #endif 00067 00068 #define MAXSENDSIZE_EW 8192/*4096*/ 00069 00070 #include <earthworm.h> 00071 /**************************************************************/ 00072 /* EXTERNS */ 00073 /**************************************************************/ 00074 extern int SELECT_TIMEOUT_SECONDS; 00075 extern int SELECT_TIMEOUT_uSECONDS; 00076 extern int EW_SOCKET_DEBUG; 00077 00078 /**************************************************************/ 00079 /* Socket based function prototypes */ 00080 /**************************************************************/ 00081 00082 int socketGetError_ew(); 00083 /* socketGetError_ew() returns the error code for the most 00084 recent socket error. 00085 */ 00086 00087 SOCKET accept_ew (SOCKET s, struct sockaddr FAR* addr, int FAR* addrlen, 00088 int timeout); 00089 /* accept_ew() attempts to accept a connection on a socket. 00090 timeout is the length of time in millisec. that accept_ew() 00091 will wait before returning. Timeout is measure from the 00092 point after the initial accept() call. Pass -1 for infinite 00093 accept_ew to block. If a successful 00094 connection is not accepted before the timeout expires, or 00095 if an error occurs, the function returns INVALID_SOCKET. 00096 If the latest socket error was WOULDBLOCK_EW, then 00097 no connections were requested during the timeout period. 00098 Caller can call socketGetError_ew() for details about 00099 any failures. 00100 */ 00101 00102 00103 int bind_ew (SOCKET s, struct sockaddr FAR* name, int namelen ); 00104 /* bind_ew() attempts to bind the socket s to a name/port number. 00105 Caller can call socketGetError_ew() for details about any failures. 00106 */ 00107 00108 00109 int closesocket_ew(SOCKET s,int HowToClose); 00110 /* closesocket_ew() closes the socket s. HowToClose indicates 00111 whether the socket should be closed gracefully or immediately. 00112 Use SOCKET_CLOSE_IMMEDIATELY_EW or SOCKET_CLOSE_GRACEFULLY_EW 00113 to indicate closure method. Caller can call socketGetError_ew() 00114 for details about any failures. 00115 */ 00116 00117 00118 int connect_ew(SOCKET s, struct sockaddr FAR* name, 00119 int namelen, int timeout); 00120 /* connect_ew() attempts to create a socket connection during a 00121 period specified by timeout. If it succeeds it returns a 00122 successful condition. If it fails either due to a network 00123 error, or a timeout, it closes the socket and returns an error. 00124 *Note: The timeout clock starts after connect_ew() calls 00125 connect(), not when connect_ew() starts. 00126 Caller can call socketGetError_ew() for details about any 00127 failures. 00128 */ 00129 00130 00131 int listen_ew (SOCKET s, int backlog ); 00132 /* listen_ew() signals the mysterious protocol stack god, that the 00133 socket is ready to accept connections. 00134 Caller can call socketGetError_ew() for details about any failures. 00135 */ 00136 00137 00138 int recv_ew (SOCKET s,char FAR* buf,int len,int flags, int timeout); 00139 /* recv_ew attempts to receive data on a connection oriented scoket. 00140 timeout is the length of time in millisec. that the recv_ew() 00141 will wait before returning(if no data is received), after making 00142 the initial recv() call. If data (or a shutdown request) is not 00143 received before the timeout expires, or if an error occurs, the 00144 function returns SOCKET_ERROR. If the latest socket error is 00145 WOULDBLOCK_EW, then no data was received during the timeout 00146 period. As soon as data is received, the function returns. 00147 The function does not attempt to completely fill the buffer 00148 before returning. 00149 If (-1) is passed for timeout_msec, then recv_ew() reverts to a blocking 00150 recv() call. 00151 Caller can call socketGetError_ew() for details about any failures. 00152 */ 00153 00154 00155 int recv_all (SOCKET s,char FAR* buf,int len,int flags, int timeout_msec); 00156 /* recv_all attempts to receive data on a connection oriented scoket. 00157 timeout is the length of time in millisec. that the recv_ew() will wait 00158 before returning(if no data is received), after making the initial recv() 00159 call. 00160 00161 if timeout_msec > 0, recv_all() returns when the sooner of two things 00162 happens: 00163 1. The timeout measured in millisec. from the time of the first 00164 send() call, expires; 00165 2. "len" bytes of data are received. 00166 recv_all() returns the number of bytes of data received, or SOCKET_ERROR 00167 on error. The caller is responsible for noting any discrepencies in the 00168 difference between the number of bytes requested to be sent, and the 00169 number of reported bytes sent. If there is a discrepency, then a timeout 00170 occured. Caller can call socketGetError_ew() for details about any 00171 failures. 00172 if timeout_msec == -1, recv_all() sets the socket to blocking and returns 00173 when: 00174 1. "len" bytes of data are received. 00175 2. EOF is detected by recv returning 0 bytes. 00176 */ 00177 00178 int recvfrom_ew (SOCKET s, char FAR* buf, int len, int flags, 00179 struct sockaddr FAR* from, int FAR* fromlen, 00180 int timeout); 00181 /* recvfrom_ew() is similar to recv_ew(), except used for datagram 00182 sockets. timeout is specified in milliseconds. Caller can call 00183 socketGetError_ew() for details about any failures. 00184 */ 00185 00186 00187 int select_ew (int nfds, fd_set FAR * readfds, fd_set FAR * writefds, 00188 fd_set FAR * exceptfds, 00189 int timeout); 00190 /* select_ew() determines the state of sets of sockets, by 00191 calling select(). Timeout is in milliseconds, and is 00192 converted by select_ew to the select() timeout structure, and 00193 passed on (to select()). 00194 Caller can call socketGetError_ew() for details about any failures. 00195 */ 00196 00197 00198 int send_ew ( SOCKET s, const char FAR * buf, int len, int flags, 00199 int timeout); 00200 /* send_ew() returns when the sooner of two things happens: 00201 1. The timeout measured in millisec. from the time of 00202 the first send() call, expires; 00203 2. All of the data provided by the caller is sent. 00204 send_ew() returns the number of bytes of data sent, or 00205 SOCKET_ERROR on error. The caller is responsible for noting 00206 any discrepencies in the difference between the number of bytes 00207 requested to be sent, and the number of reported bytes sent. If 00208 there is a discrepency, then a timeout occured. 00209 Caller can call socketGetError_ew() for details about any failures. 00210 */ 00211 00212 00213 int sendto_ew (SOCKET s, const char FAR * buf, int len, 00214 int flags, const struct sockaddr FAR * to, 00215 int tolen, int timeout); 00216 /* sendto_ew() is similar to send_ew(), except used for datagram 00217 sockets. timeout is specified in milliseconds. Caller can call 00218 socketGetError_ew() for details about any failures. 00219 */ 00220 00221 00222 SOCKET socket_ew (int af, int type, int protocol); 00223 /* socket_ew() allocates a socket 00224 descriptor and associated resources. It first calls socket(), 00225 and then sets the socket descriptor to non-blocking mode. 00226 No network I/O occurs. 00227 Caller can call socketGetError_ew() for details about any failures. 00228 */ 00229 00230 00231 00232 void SocketSysInit( void ); 00233 /********************** SocketSysInit ******************** 00234 * Initialize the socket system * 00235 * We are using Windows socket version 2.2. * 00236 *********************************************************/ 00237 00238 00239 /**************************************************************/ 00240 /* SOCKET_ew utility function prototypes */ 00241 /**************************************************************/ 00242 00243 00244 int setSocket_ewSelectTimeout(unsigned int Timeout); 00245 /* setSocket_ewSelectTimeout() sets the timeout period 00246 passed to select() calls made internally within the 00247 SOCKET_ew routines. The timeout period is in 00248 milliseconds. 00249 */ 00250 00251 00252 int setSocket_ewDebug(int debug); 00253 /* setSocket_ewDebug() turns debugging on or off for 00254 the SOCKET_ew routines. 00255 */ 00256 00257 00258 Time_ew GetTime_ew(); 00259 00260 #endif /* SOCKET_EW_H : don't include file if already included*/ 00261