You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by mt...@apache.org on 2006/07/18 09:19:27 UTC

svn commit: r422996 - in /tomcat/connectors/trunk/jk/native/common: jk_ajp12_worker.c jk_ajp_common.c jk_ajp_common.h jk_connect.c jk_connect.h jk_global.h jk_sockbuf.h

Author: mturk
Date: Tue Jul 18 00:19:27 2006
New Revision: 422996

URL: http://svn.apache.org/viewvc?rev=422996&view=rev
Log:
Add jk_sock_t that is SOCKET on WIN32 and int on POSIX.
This enables to deal with newest Microsoft PSDKs where
SOCKET is defined as unsigned int pointer.
Also added macros for testing the socket instead simply
comparing the value with integer.

Modified:
    tomcat/connectors/trunk/jk/native/common/jk_ajp12_worker.c
    tomcat/connectors/trunk/jk/native/common/jk_ajp_common.c
    tomcat/connectors/trunk/jk/native/common/jk_ajp_common.h
    tomcat/connectors/trunk/jk/native/common/jk_connect.c
    tomcat/connectors/trunk/jk/native/common/jk_connect.h
    tomcat/connectors/trunk/jk/native/common/jk_global.h
    tomcat/connectors/trunk/jk/native/common/jk_sockbuf.h

Modified: tomcat/connectors/trunk/jk/native/common/jk_ajp12_worker.c
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_ajp12_worker.c?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_ajp12_worker.c (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_ajp12_worker.c Tue Jul 18 00:19:27 2006
@@ -51,7 +51,7 @@
 {
     ajp12_worker_t *worker;
 
-    int sd;
+    jk_sock_t sd;
     jk_sockbuf_t sb;
 
     jk_endpoint_t endpoint;
@@ -105,11 +105,11 @@
 
             jk_log(l, JK_LOG_DEBUG, "In jk_endpoint_t::service, sd = %d",
                    p->sd);
-            if (p->sd >= 0) {
+            if (IS_VALID_SOCKET(p->sd)) {
                 break;
             }
         }
-        if (p->sd >= 0) {
+        if (IS_VALID_SOCKET(p->sd)) {
 
             jk_sb_open(&p->sb, p->sd);
             if (ajpv12_handle_request(p, s, l)) {
@@ -134,7 +134,7 @@
     jk_log(l, JK_LOG_DEBUG, "Into jk_endpoint_t::done");
     if (e && *e && (*e)->endpoint_private) {
         ajp12_endpoint_t *p = (*e)->endpoint_private;
-        if (p->sd > 0) {
+        if (IS_VALID_SOCKET(p->sd)) {
             jk_close_socket(p->sd);
         }
         free(p);
@@ -201,7 +201,7 @@
         ajp12_endpoint_t *p =
             (ajp12_endpoint_t *) malloc(sizeof(ajp12_endpoint_t));
         if (p) {
-            p->sd = -1;
+            p->sd = JK_INVALID_SOCKET;
             p->worker = pThis->worker_private;
             p->endpoint.endpoint_private = p;
             p->endpoint.service = service;

Modified: tomcat/connectors/trunk/jk/native/common/jk_ajp_common.c
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_ajp_common.c?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_ajp_common.c (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_ajp_common.c Tue Jul 18 00:19:27 2006
@@ -716,12 +716,12 @@
 {
     JK_TRACE_ENTER(l);
 
-    if (ae->sd > 0) {
+    if (IS_VALID_SOCKET(ae->sd)) {
         jk_shutdown_socket(ae->sd);
         if (JK_IS_DEBUG_LEVEL(l))
             jk_log(l, JK_LOG_DEBUG,
                    "closed socket with sd = %d", ae->sd);
-        ae->sd = -1;
+        ae->sd = JK_INVALID_SOCKET;
     }
 
     jk_close_pool(&(ae->pool));
@@ -741,22 +741,22 @@
     int sock = ae->sd;
 
     /* Mark existing endpoint socket as closed */
-    ae->sd = -1;
+    ae->sd = JK_INVALID_SOCKET;
     JK_ENTER_CS(&aw->cs, rc);
     if (rc) {
         unsigned int i;
         for (i = 0; i < aw->ep_cache_sz; i++) {
             /* Find cache slot with usable socket */
-            if (aw->ep_cache[i] && aw->ep_cache[i]->sd != -1) {
+            if (aw->ep_cache[i] && IS_VALID_SOCKET(aw->ep_cache[i]->sd)) {
                 ae->sd = aw->ep_cache[i]->sd;
-                aw->ep_cache[i]->sd = -1;
+                aw->ep_cache[i]->sd = JK_INVALID_SOCKET;
                 break;
             }
         }
         JK_LEAVE_CS(&aw->cs, rc);
     }
     /* Close previous socket */
-    if (sock > 0)
+    if (IS_VALID_SOCKET(sock))
         jk_close_socket(sock);
 }
 
@@ -859,7 +859,7 @@
                             ae->worker->keepalive,
                             ae->worker->socket_timeout,
                             ae->worker->socket_buf, l);
-    if (ae->sd >= 0) {
+    if (IS_VALID_SOCKET(ae->sd)) {
         if (JK_IS_DEBUG_LEVEL(l)) {
             jk_log(l, JK_LOG_DEBUG,
                    "Connected socket %d to (%s)",
@@ -1170,7 +1170,7 @@
     /*
      * First try to reuse open connections...
      */
-    while ((ae->sd > 0)) {
+    while (IS_VALID_SOCKET(ae->sd)) {
         int rc = 0;
         err = 0;
         if (ae->worker->socket_timeout) {
@@ -1179,7 +1179,7 @@
                        "Socket %d is not connected any more (errno=%d)",
                        ae->sd, errno);
                 jk_close_socket(ae->sd);
-                ae->sd = -1;
+                ae->sd = JK_INVALID_SOCKET;
                 err++;
             }
         }
@@ -1218,7 +1218,7 @@
     /*
      * If we failed to reuse a connection, try to reconnect.
      */
-    if (ae->sd < 0) {
+    if (!IS_VALID_SOCKET(ae->sd)) {
         if (err) {
             /* XXX: If err is set, the tomcat is either dead or disconnected */
             jk_log(l, JK_LOG_INFO,
@@ -1247,7 +1247,7 @@
         else {
             /* Close the socket if unable to connect */
             jk_close_socket(ae->sd);
-            ae->sd = -1;
+            ae->sd = JK_INVALID_SOCKET;
             jk_log(l, JK_LOG_INFO,
                    "Error connecting to the backend server.");
             JK_TRACE_EXIT(l);
@@ -1279,7 +1279,7 @@
         if (ajp_connection_tcp_send_message(ae, op->post, l) != JK_TRUE) {
             /* Close the socket if unable to send request */
             jk_close_socket(ae->sd);
-            ae->sd = -1;
+            ae->sd = JK_INVALID_SOCKET;
             jk_log(l, JK_LOG_ERROR, "Error resending request body (%d)",
                    postlen);
             JK_TRACE_EXIT(l);
@@ -1888,7 +1888,7 @@
                 JK_TRACE_EXIT(l);
                 return JK_FALSE;
             }
-            p->ep_cache[i]->sd = -1;
+            p->ep_cache[i]->sd = JK_INVALID_SOCKET;
             p->ep_cache[i]->reuse = JK_FALSE;
             p->ep_cache[i]->last_access = now;
             jk_open_pool(&(p->ep_cache[i]->pool), p->ep_cache[i]->buf,
@@ -2082,11 +2082,11 @@
 
         JK_ENTER_CS(&w->cs, rc);
         if (rc) {
-            int i, sock = -1;
+            int i, sock = JK_INVALID_SOCKET;
 
             if (p->sd > 0 && !p->reuse) {
                 sock  = p->sd;
-                p->sd = -1;
+                p->sd = JK_INVALID_SOCKET;
             }
             for(i = w->ep_cache_sz - 1; i >= 0; i--) {
                 if (w->ep_cache[i] == NULL) {
@@ -2100,7 +2100,7 @@
             if (w->cache_timeout > 0)
                 p->last_access = time(NULL);
             JK_LEAVE_CS(&w->cs, rc);
-            if (sock >= 0)
+            if (IS_VALID_SOCKET(sock))
                 jk_shutdown_socket(sock);
             if (i >= 0) {
                 if (JK_IS_DEBUG_LEVEL(l))
@@ -2211,13 +2211,13 @@
             unsigned int i, n = 0, cnt = 0;
             /* Count opended slots */
             for (i = 0; i < aw->ep_cache_sz; i++) {
-                if (aw->ep_cache[i] && aw->ep_cache[i]->sd >= 0)
+                if (aw->ep_cache[i] && IS_VALID_SOCKET(aw->ep_cache[i]->sd))
                     cnt++;
             }
             /* Handle worker cache and recycle timeouts */
             for (i = 0; i < aw->ep_cache_sz; i++) {
                 /* Skip the closed sockets */
-                if (aw->ep_cache[i] && aw->ep_cache[i]->sd >= 0) {
+                if (aw->ep_cache[i] && IS_VALID_SOCKET(aw->ep_cache[i]->sd)) {
                     int elapsed = (int)difftime(now, aw->ep_cache[i]->last_access);
                     if ((aw->cache_timeout > 0) && (elapsed > aw->cache_timeout)) {
                         time_t rt = 0;

Modified: tomcat/connectors/trunk/jk/native/common/jk_ajp_common.h
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_ajp_common.h?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_ajp_common.h (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_ajp_common.h Tue Jul 18 00:19:27 2006
@@ -299,7 +299,7 @@
 
     int proto;              /* PROTOCOL USED AJP13/AJP14 */
 
-    int sd;
+    jk_sock_t sd;
     int reuse;
 
     jk_endpoint_t endpoint;

Modified: tomcat/connectors/trunk/jk/native/common/jk_connect.c
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_connect.c?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_connect.c (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_connect.c Tue Jul 18 00:19:27 2006
@@ -81,7 +81,7 @@
     return 0;
 }
 
-static int sononblock(int sd)
+static int sononblock(jk_sock_t sd)
 {
 #ifndef WIN32
     int fd_flags;
@@ -111,7 +111,7 @@
 
 #if defined (WIN32) || (defined(NETWARE) && defined(__NOVELL_LIBC__))
 /* WIN32 implementation */
-static int nb_connect(int sock, struct sockaddr *addr, int timeout)
+static int nb_connect(jk_sock_t sock, struct sockaddr *addr, int timeout)
 {
     int rc;
     if (timeout < 1)
@@ -161,7 +161,7 @@
 
 #elif !defined(NETWARE)
 /* POSIX implementation */
-static int nb_connect(int sock, struct sockaddr *addr, int timeout)
+static int nb_connect(jk_sock_t sock, struct sockaddr *addr, int timeout)
 {
     int rc = 0;
 
@@ -210,7 +210,7 @@
 }
 #else
 /* NETWARE implementation - blocking for now */
-static int nb_connect(int sock, struct sockaddr *addr, int timeout)
+static int nb_connect(jk_sock_t sock, struct sockaddr *addr, int timeout)
 {
     return connect(sock, addr, sizeof(struct sockaddr_in));
 }
@@ -294,11 +294,11 @@
 
 /** connect to Tomcat */
 
-int jk_open_socket(struct sockaddr_in *addr, int keepalive,
-                   int timeout, int sock_buf, jk_logger_t *l)
+jk_sock_t jk_open_socket(struct sockaddr_in *addr, int keepalive,
+                         int timeout, int sock_buf, jk_logger_t *l)
 {
     char buf[32];
-    int sock;
+    jk_sock_t sock;
     int set = 1;
     int ret = 0;
 #ifdef SO_LINGER
@@ -308,12 +308,13 @@
     JK_TRACE_ENTER(l);
 
     sock = socket(AF_INET, SOCK_STREAM, 0);
-    if (sock < 0) {
+    if (!IS_VALID_SOCKET(sock)) {
         JK_GET_SOCKET_ERRNO();
         jk_log(l, JK_LOG_ERROR,
                "socket() failed with errno=%d", errno);
         JK_TRACE_EXIT(l);
-        return -1;
+        return JK_INVALID_SOCKET;
+;
     }
     /* Disable Nagle algorithm */
     if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (SET_TYPE)&set,
@@ -322,7 +323,7 @@
                 "failed setting TCP_NODELAY with errno=%d", errno);
         jk_close_socket(sock);
         JK_TRACE_EXIT(l);
-        return -1;
+        return JK_INVALID_SOCKET;
     }
     if (JK_IS_DEBUG_LEVEL(l))
         jk_log(l, JK_LOG_DEBUG,
@@ -335,7 +336,7 @@
                    "failed setting SO_KEEPALIVE with errno=%d", errno);
             jk_close_socket(sock);
             JK_TRACE_EXIT(l);
-            return -1;
+            return JK_INVALID_SOCKET;
         }
         if (JK_IS_DEBUG_LEVEL(l))
             jk_log(l, JK_LOG_DEBUG,
@@ -352,7 +353,7 @@
                     "failed setting SO_SNDBUF with errno=%d", errno);
             jk_close_socket(sock);
             JK_TRACE_EXIT(l);
-            return -1;
+            return JK_INVALID_SOCKET;
         }
         set = sock_buf;
         /* Set socket receive buffer size */
@@ -363,7 +364,7 @@
                     "failed setting SO_RCVBUF with errno=%d", errno);
             jk_close_socket(sock);
             JK_TRACE_EXIT(l);
-            return -1;
+            return JK_INVALID_SOCKET;
         }
         if (JK_IS_DEBUG_LEVEL(l))
             jk_log(l, JK_LOG_DEBUG,
@@ -405,7 +406,7 @@
                 "failed setting SO_NOSIGPIPE with errno=%d", errno);
         jk_close_socket(sock);
         JK_TRACE_EXIT(l);
-        return -1;
+        return JK_INVALID_SOCKET;
     }
 #endif
 #ifdef SO_LINGER
@@ -418,7 +419,7 @@
                 "failed setting SO_LINGER with errno=%d", errno);
         jk_close_socket(sock);
         JK_TRACE_EXIT(l);
-        return -1;
+        return JK_INVALID_SOCKET;
     }
 #endif
     /* Tries to connect to Tomcat (continues trying while error is EINTR) */
@@ -445,7 +446,7 @@
                "connect to %s failed with errno=%d",
                jk_dump_hinfo(addr, buf), errno);
         jk_close_socket(sock);
-        sock = -1;
+        sock = JK_INVALID_SOCKET;
     }
     else {
         if (JK_IS_DEBUG_LEVEL(l))
@@ -458,7 +459,7 @@
 
 /** close the socket */
 
-int jk_close_socket(int s)
+int jk_close_socket(jk_sock_t s)
 {
 #if defined(WIN32) || (defined(NETWARE) && defined(__NOVELL_LIBC__))
     if (s != INVALID_SOCKET)
@@ -483,7 +484,7 @@
 #define SHUT_WR 0x01
 #endif
 #endif
-int jk_shutdown_socket(int s)
+int jk_shutdown_socket(jk_sock_t s)
 {
     unsigned char dummy[512];
     int nbytes;
@@ -540,7 +541,7 @@
  * @bug       this fails on Unixes if len is too big for the underlying
  *             protocol.
  */
-int jk_tcp_socket_sendfull(int sd, const unsigned char *b, int len)
+int jk_tcp_socket_sendfull(jk_sock_t sd, const unsigned char *b, int len)
 {
     int sent = 0;
     int wr;
@@ -574,7 +575,7 @@
  * @return    <0: receive failed or connection closed.
  *            >0: length of the received data.
  */
-int jk_tcp_socket_recvfull(int sd, unsigned char *b, int len)
+int jk_tcp_socket_recvfull(jk_sock_t sd, unsigned char *b, int len)
 {
     int rdlen = 0;
     int rd;
@@ -619,7 +620,7 @@
 }
 
 #if defined(WIN32) || (defined(NETWARE) && defined(__NOVELL_LIBC__))
-int jk_is_socket_connected(int sock)
+int jk_is_socket_connected(jk_sock_t sock)
 {
     fd_set fd;
     struct timeval tv;
@@ -646,7 +647,7 @@
     }
 }
 #else
-int jk_is_socket_connected(int sock)
+int jk_is_socket_connected(jk_sock_t sock)
 {
     char test_buffer[1];
     int  rd;

Modified: tomcat/connectors/trunk/jk/native/common/jk_connect.h
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_connect.h?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_connect.h (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_connect.h Tue Jul 18 00:19:27 2006
@@ -40,20 +40,20 @@
 
 int jk_resolve(const char *host, int port, struct sockaddr_in *rc);
 
-int jk_open_socket(struct sockaddr_in *addr, int keepalive,
-                   int timeout, int sock_buf, jk_logger_t *l);
+jk_sock_t jk_open_socket(struct sockaddr_in *addr, int keepalive,
+                         int timeout, int sock_buf, jk_logger_t *l);
 
-int jk_close_socket(int s);
+int jk_close_socket(jk_sock_t s);
 
-int jk_shutdown_socket(int s);
+int jk_shutdown_socket(jk_sock_t s);
 
-int jk_tcp_socket_sendfull(int sd, const unsigned char *b, int len);
+int jk_tcp_socket_sendfull(jk_sock_t sd, const unsigned char *b, int len);
 
-int jk_tcp_socket_recvfull(int sd, unsigned char *b, int len);
+int jk_tcp_socket_recvfull(jk_sock_t sd, unsigned char *b, int len);
 
 char *jk_dump_hinfo(struct sockaddr_in *saddr, char *buf);
 
-int jk_is_socket_connected(int sd);
+int jk_is_socket_connected(jk_sock_t sd);
 
 void jk_sleep_def(void);
 

Modified: tomcat/connectors/trunk/jk/native/common/jk_global.h
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_global.h?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_global.h (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_global.h Tue Jul 18 00:19:27 2006
@@ -297,6 +297,16 @@
 #define USE_VSPRINTF
 #endif
 
+#if defined(WIN32) || (defined(NETWARE) && defined(__NOVELL_LIBC__))
+typedef SOCKET jk_sock_t;
+#define IS_VALID_SOCKET(s) ((s) != INVALID_SOCKET)
+#define JK_INVALID_SOCKET  INVALID_SOCKET
+#else
+typedef int jk_sock_t;
+#define IS_VALID_SOCKET(s) ((s) > 0)
+#define JK_INVALID_SOCKET  (-1)
+#endif
+
 #ifdef __cplusplus
 }
 #endif                          /* __cplusplus */

Modified: tomcat/connectors/trunk/jk/native/common/jk_sockbuf.h
URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jk/native/common/jk_sockbuf.h?rev=422996&r1=422995&r2=422996&view=diff
==============================================================================
--- tomcat/connectors/trunk/jk/native/common/jk_sockbuf.h (original)
+++ tomcat/connectors/trunk/jk/native/common/jk_sockbuf.h Tue Jul 18 00:19:27 2006
@@ -27,9 +27,9 @@
 struct jk_sockbuf
 {
     char buf[SOCKBUF_SIZE];
-    unsigned start;
-    unsigned end;
-    int sd;
+    unsigned int start;
+    unsigned int end;
+    jk_sock_t sd;
 };
 typedef struct jk_sockbuf jk_sockbuf_t;
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org