📄 jk_channel_apr_socket.c
字号:
/* if an error occurred, loop round and try again */ if (ret != APR_SUCCESS) { apr_socket_close(sock); env->l->jkLog(env, env->l, remote_sa->next ? JK_LOG_DEBUG : JK_LOG_ERROR, "channelApr.open() attempt to connect to %pI (%s) failed %d\n", remote_sa, socketInfo->host, ret); remote_sa = remote_sa->next; continue; } connected = 1; } if (!connected) { apr_socket_close(sock); return JK_ERR; } /* enable the use of keep-alive packets on TCP connection */ if(keepalive) { int set = 1; if((ret = apr_setsocketopt(sock, APR_SO_KEEPALIVE, set)) != APR_SUCCESS ) { apr_socket_close(sock); env->l->jkLog(env, env->l, JK_LOG_ERROR, "channelApr.open() keepalive failed %d %s\n", ret, apr_strerror( ret, msg, sizeof(msg) ) ); return JK_ERR; } } /* Disable the Nagle algorithm if ndelay is set */ if(ndelay) { int set = 1; if((ret = apr_setsocketopt(sock, APR_TCP_NODELAY, set)) != APR_SUCCESS ) { apr_socket_close(sock); env->l->jkLog(env, env->l, JK_LOG_ERROR, "channelApr.open() nodelay failed %d %s\n", ret, apr_strerror( ret, msg, sizeof(msg) ) ); return JK_ERR; } } if( ch->mbean->debug > 0 ) env->l->jkLog(env, env->l, JK_LOG_DEBUG, "channelApr.open(), sock = %d\n", sock); return JK_OK;}/** close the socket ( was: jk2_close_socket )*/static int JK_METHOD jk2_channel_apr_close(jk_env_t *env,jk_channel_t *ch, jk_endpoint_t *endpoint){ apr_socket_t *sd; apr_status_t rc; sd=endpoint->channelData; if( sd==NULL ) return JK_ERR; endpoint->channelData=NULL; /* XXX check it. */ endpoint->sd=-1; /* nothing else to clean, the socket_data was allocated ouf of * endpoint's pool */ rc=apr_socket_close(sd); return rc;}/** send a long message * @param sd opened socket. * @param b buffer containing the data. * @param len length to send. * @return -2: send returned 0 ? what this that ? * -3: send failed. * >0: total size send. * @bug this fails on Unixes if len is too big for the underlying * protocol. * @was: jk_tcp_socket_sendfull */static int JK_METHOD jk2_channel_apr_send(jk_env_t *env, jk_channel_t *ch, jk_endpoint_t *endpoint, jk_msg_t *msg) { char *b; int len; apr_socket_t *sock; apr_status_t stat; apr_size_t length; char data[128]; int sent=0; sock=endpoint->channelData; if( sock==NULL ) return JK_ERR; msg->end( env, msg ); len=msg->len; b=msg->buf; length = (apr_size_t) len; do { apr_size_t written = length; stat = apr_send(sock, b, &written); if (stat!= APR_SUCCESS) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "jk2_channel_apr_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 JK_OK;}/** receive len bytes. * @param sd opened socket. * @param b buffer to store the data. * @param len length to receive. * @return -1: receive failed or connection closed. * >0: length of the received data. * Was: tcp_socket_recvfull */static int JK_METHOD jk2_channel_apr_readN( jk_env_t *env, jk_channel_t *ch, jk_endpoint_t *endpoint, char *b, int len ) { apr_socket_t *sock; apr_size_t length; apr_status_t stat; int rdlen; sock=endpoint->channelData; if( sock==NULL ) return JK_ERR; rdlen = 0; length = (apr_size_t)len; while (rdlen < len) { stat = apr_recv(sock, b + 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;}/** receive len bytes. * @param sd opened socket. * @param b buffer to store the data. * @param len length to receive. * @return -1: receive failed or connection closed. * >0: length of the received data. * Was: tcp_socket_recvfull */static int JK_METHOD jk2_channel_apr_recv( jk_env_t *env, jk_channel_t *ch, jk_endpoint_t *endpoint, jk_msg_t *msg ){ int hlen=msg->headerLength; int blen; int rc; jk2_channel_apr_readN( env, ch, endpoint, msg->buf, hlen ); blen=msg->checkHeader( env, msg, endpoint ); if( blen < 0 ) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "channelApr.receive(): Bad header\n" ); return JK_ERR; } rc= jk2_channel_apr_readN( env, ch, endpoint, msg->buf + hlen, blen); if(rc < 0) { env->l->jkLog(env, env->l, JK_LOG_ERROR, "channelApr.receive(): Error receiving message body %d %d\n", rc, errno); return JK_ERR; } env->l->jkLog(env, env->l, JK_LOG_INFO, "channelApr.receive(): Received len=%d type=%d\n", blen, (int)msg->buf[hlen]); return JK_OK;}int JK_METHOD jk2_channel_apr_socket_factory(jk_env_t *env, jk_pool_t *pool, jk_bean_t *result, const char *type, const char *name){ jk_channel_t *ch; ch=(jk_channel_t *)pool->calloc(env, pool, sizeof( jk_channel_t)); ch->_privatePtr= (jk_channel_apr_private_t *) pool->calloc( env, pool, sizeof( jk_channel_apr_private_t)); ch->recv= jk2_channel_apr_recv; ch->send= jk2_channel_apr_send; ch->open= jk2_channel_apr_open; ch->close= jk2_channel_apr_close; ch->is_stream=JK_TRUE; result->setAttribute= jk2_channel_apr_setProperty; ch->mbean=result; result->object= ch; result->init= jk2_channel_apr_init; ch->workerEnv=env->getByName( env, "workerEnv" ); ch->workerEnv->addChannel( env, ch->workerEnv, ch ); return JK_OK;}#else /* HAS_APR */int JK_METHOD jk2_channel_apr_socket_factory(jk_env_t *env, jk_pool_t *pool, jk_bean_t *result, const char *type, const char *name){ result->disabled=1; result->object= NULL; return JK_OK;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -