hgomez 2004/07/27 06:46:16 Added: ajp/ajplib/test ajp_link.c Log: Some socket io (ad minima) Revision Changes Path 1.1 jakarta-tomcat-connectors/ajp/ajplib/test/ajp_link.c Index: ajp_link.c =================================================================== /* Copyright 1999-2004 The Apache Software Foundation
* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ajp.h" /** Resolve host **/ int ajp_idef_resolve(ajp_env_t *env, char *host, apr_port_t port, ajp_idef_t * idef) { int err; err = apr_sockaddr_info_get(&idef->addr, host, APR_UNSPEC, port, 0, env->pool); if (err != APR_SUCCESS) { return err; } return 0; } /** Create (connect) instance link **/ int ajp_ilink_connect(ajp_env_t *env, ajp_idef_t * idef, ajp_ilink_t *link) { apr_sockaddr_t *remote_sa = idef->addr; int ndelay = idef->ndelay; int keepalive = idef->keepalive; apr_socket_t *sock; apr_status_t ret; apr_int32_t timeout = (apr_int32_t) (idef->timeout * APR_USEC_PER_SEC); char msg[128]; int connected = 0; while (remote_sa && !connected) { if ((ret = apr_socket_create(&sock, remote_sa->family, SOCK_STREAM, #if (APR_MAJOR_VERSION > 0) APR_PROTO_TCP, #endif env->pool)) != APR_SUCCESS) { if (remote_sa->next) { fprintf(stderr, "ajp_ilink_connect() error %d creating socket to %s\n", ret, idef->host); } else { fprintf(stderr, "ajp_ilink_connect() error %d creating socket to %s, no more addr\n", ret, idef->host); } remote_sa = remote_sa->next; continue; } /* store the socket information */ link->sock = sock; /* no requests yet */ link->requests = 0; fprintf(stdout, "ajp_ilink_connect() create tcp socket %d\n", sock); /* the default timeout (0) will set the socket to blocking with infinite timeouts. */ if (timeout <= 0) apr_socket_timeout_set(sock, -1); else apr_socket_timeout_set(sock, timeout); /* make the connection out of the socket */ do { ret = apr_socket_connect(sock, remote_sa); } while (APR_STATUS_IS_EINTR(ret)); /* if an error occurred, loop round and try again */ if (ret != APR_SUCCESS) { apr_socket_close(sock); if (remote_sa->next) { fprintf(stderr, "ajp_ilink_connect() attempt to connect to %pI (%s) failed %d\n", remote_sa, idef->host, ret); } else { fprintf(stderr, "ajp_ilink_connect() attempt to connect to %pI (%s) failed %d, no more addr\n", remote_sa, idef->host, ret); } remote_sa = remote_sa->next; continue; } connected = 1; } if (!connected) { apr_socket_close(sock); return -1; } /* enable the use of keep-alive packets on TCP connection */ if (keepalive) { int set = 1; if ((ret = apr_socket_opt_set(sock, APR_SO_KEEPALIVE, set)) != APR_SUCCESS) { apr_socket_close(sock); fprintf(stderr, "ajp_ilink_connect() keepalive failed %d %s\n", ret, apr_strerror(ret, msg, sizeof(msg))); return -1; } } /* Disable the Nagle algorithm if ndelay is set */ if (ndelay) { int set = 1; if ((ret = apr_socket_opt_set(sock, APR_TCP_NODELAY, set)) != APR_SUCCESS) { apr_socket_close(sock); fprintf(stderr, "ajp_ilink_connect() nodelay failed %d %s\n", ret, apr_strerror(ret, msg, sizeof(msg))); return -1; } } fprintf(stdout, "ajp_ilink_connect(), sock = %d\n", sock); return 0; } /** Close instance link **/ int ajp_ilink_close(ajp_env_t *env, ajp_ilink_t *link) { apr_socket_t *sd; apr_status_t rc; sd = link->sock; if (sd == NULL) return -1; fprintf(stdout, "ajp_ilink_close() closing sock = %d after %d requests\n", sd, link->requests); rc = apr_socket_close(sd); return rc; } int ajp_ilink_send(ajp_env_t *env, ajp_ilink_t *link, ajp_msg_t *msg) { char *b; int len; apr_socket_t *sock; apr_status_t stat; apr_size_t length; char data[128]; sock = link->sock; if (sock == NULL) return -1; ajp_msg_end(env, msg); len = msg->len; b = msg->buf; length = (apr_size_t) len; do { apr_size_t written = length; stat = apr_socket_send(sock, b, &written); if (stat != APR_SUCCESS) { fprintf(stderr, "ajp_ilink_send() send failed %d %s\n", stat, apr_strerror(stat, data, sizeof(data))); return -3; /* -2 is not possible... */ } length -= written; b += written; } while (length); return 0; } static int ajp_ilink_readN(ajp_env_t *env, ajp_ilink_t *link, char * buf, apr_size_t len) { apr_socket_t *sock; apr_size_t length; apr_status_t stat; int rdlen; sock = link->sock; if (sock == NULL) return -1; rdlen = 0; length = len; while (rdlen < len) { stat = apr_socket_recv(sock, buf + rdlen, &length); if (stat == APR_EOF) return -1; /* socket closed. */ else if (APR_STATUS_IS_EAGAIN(stat)) continue; else if (stat != APR_SUCCESS) return -1; /* any error. */ rdlen += length; length = (apr_size_t) (len - rdlen); } return rdlen; } int ajp_ilink_receive(ajp_env_t *env, ajp_ilink_t *link, ajp_msg_t *msg) { int hlen = msg->headerLen; int blen; int rc; ajp_ilink_channel_apr_readN(env, link, msg->buf, hlen); blen = ajp_msg_check_header(env, msg); if (blen < 0) { fprintf(stderr, "ajp_ilink_receive() received bad header\n"); return -1; } rc = jk2_channel_apr_readN(env, link, msg->buf + hlen, blen); if (rc < 0) { fprintf(stderr, "ajp_ilink_receive() error while receiving message body %d %d\n", rc, errno); return -2; } fprintf(stdout, "ajp_ilink_receive() received packet len=%d type=%d\n", blen, (int)msg->buf[hlen]); return 0; } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]