📄 sendrecv.c
字号:
#elif defined(_AIX) || defined(__MVS__)
/* AIX and OS/390 have the same send_file() interface.
*
* subtle differences:
* AIX doesn't update the file ptr but OS/390 does
*
* availability (correctly determined by autoconf):
*
* AIX - version 4.3.2 with APAR IX85388, or version 4.3.3 and above
* OS/390 - V2R7 and above
*/
apr_status_t apr_socket_sendfile(apr_socket_t * sock, apr_file_t * file,
apr_hdtr_t * hdtr, apr_off_t * offset,
apr_size_t * len, apr_int32_t flags)
{
int i, ptr, rv = 0;
void * hbuf=NULL, * tbuf=NULL;
apr_status_t arv;
struct sf_parms parms;
if (!hdtr) {
hdtr = &no_hdtr;
}
/* Ignore flags for now. */
flags = 0;
/* word to the wise: by default, AIX stores files sent by send_file()
* in the network buffer cache... there are supposedly scenarios
* where the most recent copy of the file won't be sent, but I can't
* recreate the potential problem, perhaps because of the way we
* use send_file()... if you suspect such a problem, try turning
* on the SF_SYNC_CACHE flag
*/
/* AIX can also send the headers/footers as part of the system call */
parms.header_length = 0;
if (hdtr && hdtr->numheaders) {
if (hdtr->numheaders == 1) {
parms.header_data = hdtr->headers[0].iov_base;
parms.header_length = hdtr->headers[0].iov_len;
}
else {
for (i = 0; i < hdtr->numheaders; i++) {
parms.header_length += hdtr->headers[i].iov_len;
}
#if 0
/* Keepalives make apr_palloc a bad idea */
hbuf = malloc(parms.header_length);
#else
/* but headers are small, so maybe we can hold on to the
* memory for the life of the socket...
*/
hbuf = apr_palloc(sock->cntxt, parms.header_length);
#endif
ptr = 0;
for (i = 0; i < hdtr->numheaders; i++) {
memcpy((char *)hbuf + ptr, hdtr->headers[i].iov_base,
hdtr->headers[i].iov_len);
ptr += hdtr->headers[i].iov_len;
}
parms.header_data = hbuf;
}
}
else parms.header_data = NULL;
parms.trailer_length = 0;
if (hdtr && hdtr->numtrailers) {
if (hdtr->numtrailers == 1) {
parms.trailer_data = hdtr->trailers[0].iov_base;
parms.trailer_length = hdtr->trailers[0].iov_len;
}
else {
for (i = 0; i < hdtr->numtrailers; i++) {
parms.trailer_length += hdtr->trailers[i].iov_len;
}
#if 0
/* Keepalives make apr_palloc a bad idea */
tbuf = malloc(parms.trailer_length);
#else
tbuf = apr_palloc(sock->cntxt, parms.trailer_length);
#endif
ptr = 0;
for (i = 0; i < hdtr->numtrailers; i++) {
memcpy((char *)tbuf + ptr, hdtr->trailers[i].iov_base,
hdtr->trailers[i].iov_len);
ptr += hdtr->trailers[i].iov_len;
}
parms.trailer_data = tbuf;
}
}
else {
parms.trailer_data = NULL;
}
/* Whew! Headers and trailers set up. Now for the file data */
parms.file_descriptor = file->filedes;
parms.file_offset = *offset;
parms.file_bytes = *len;
/* O.K. All set up now. Let's go to town */
if (sock->netmask & APR_INCOMPLETE_WRITE) {
sock->netmask &= ~APR_INCOMPLETE_WRITE;
goto do_select;
}
do {
rv = send_file(&(sock->socketdes), /* socket */
&(parms), /* all data */
flags); /* flags */
} while (rv == -1 && errno == EINTR);
while (rv == -1 &&
(errno == EAGAIN || errno == EWOULDBLOCK) &&
apr_is_option_set(sock->netmask, APR_SO_TIMEOUT)) {
do_select:
arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
if (arv != APR_SUCCESS) {
*len = 0;
return arv;
}
else {
do {
rv = send_file(&(sock->socketdes), /* socket */
&(parms), /* all data */
flags); /* flags */
} while (rv == -1 && errno == EINTR);
}
}
(*len) = parms.bytes_sent;
#if 0
/* Clean up after ourselves */
if(hbuf) free(hbuf);
if(tbuf) free(tbuf);
#endif
if (rv == -1) {
return errno;
}
if (apr_is_option_set(sock->netmask, APR_SO_TIMEOUT) &&
(parms.bytes_sent < (parms.file_bytes + parms.header_length + parms.trailer_length))) {
sock->netmask |= APR_INCOMPLETE_WRITE;
}
return APR_SUCCESS;
}
#elif defined(__osf__) && defined (__alpha)
/* Tru64's sendfile implementation doesn't work, and we need to make sure that
* we don't use it until it is fixed. If it is used as it is now, it will
* hang the machine and the only way to fix it is a reboot.
*/
#elif defined(HAVE_SENDFILEV)
/* Solaris 8's sendfilev() interface
*
* SFV_FD_SELF refers to our memory space.
*
* Required Sparc patches (or newer):
* 111297-01, 108528-09, 109472-06, 109234-03, 108995-02, 111295-01, 109025-03,
* 108991-13
* Required x86 patches (or newer):
* 111298-01, 108529-09, 109473-06, 109235-04, 108996-02, 111296-01, 109026-04,
* 108992-13
*/
apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file,
apr_hdtr_t *hdtr, apr_off_t *offset,
apr_size_t *len, apr_int32_t flags)
{
apr_status_t rv, arv;
apr_size_t nbytes;
sendfilevec_t *sfv;
int vecs, curvec, i, repeat;
apr_size_t requested_len = 0;
if (!hdtr) {
hdtr = &no_hdtr;
}
/* Ignore flags for now. */
flags = 0;
/* Calculate how much space we need. */
vecs = hdtr->numheaders + hdtr->numtrailers + 1;
sfv = apr_palloc(sock->cntxt, sizeof(sendfilevec_t) * vecs);
curvec = 0;
/* Add the headers */
for (i = 0; i < hdtr->numheaders; i++, curvec++) {
sfv[curvec].sfv_fd = SFV_FD_SELF;
sfv[curvec].sfv_flag = 0;
sfv[curvec].sfv_off = (off_t)hdtr->headers[i].iov_base;
sfv[curvec].sfv_len = hdtr->headers[i].iov_len;
requested_len += sfv[curvec].sfv_len;
}
/* If the len is 0, we skip the file. */
if (*len)
{
sfv[curvec].sfv_fd = file->filedes;
sfv[curvec].sfv_flag = 0;
sfv[curvec].sfv_off = *offset;
sfv[curvec].sfv_len = *len;
requested_len += sfv[curvec].sfv_len;
curvec++;
}
else {
vecs--;
}
/* Add the footers */
for (i = 0; i < hdtr->numtrailers; i++, curvec++) {
sfv[curvec].sfv_fd = SFV_FD_SELF;
sfv[curvec].sfv_flag = 0;
sfv[curvec].sfv_off = (off_t)hdtr->trailers[i].iov_base;
sfv[curvec].sfv_len = hdtr->trailers[i].iov_len;
requested_len += sfv[curvec].sfv_len;
}
/* If the last write couldn't send all the requested data,
* wait for the socket to become writable before proceeding
*/
if (sock->netmask & APR_INCOMPLETE_WRITE) {
sock->netmask &= ~APR_INCOMPLETE_WRITE;
arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
if (arv != APR_SUCCESS) {
*len = 0;
return arv;
}
}
/* Actually do the sendfilev
*
* Solaris may return -1/EAGAIN even if it sent bytes on a non-block sock.
*
* If no bytes were originally sent (nbytes == 0) and we are on a TIMEOUT
* socket (which as far as the OS is concerned is a non-blocking socket),
* we want to retry after waiting for the other side to read the data (as
* determined by poll). Once it is clear to send, we want to retry
* sending the sendfilevec_t once more.
*/
arv = 0;
do {
/* Clear out the repeat */
repeat = 0;
/* socket, vecs, number of vecs, bytes written */
rv = sendfilev(sock->socketdes, sfv, vecs, &nbytes);
if (rv == -1 && errno == EAGAIN) {
if (nbytes) {
rv = 0;
}
else if (!arv &&
apr_is_option_set(sock->netmask, APR_SO_TIMEOUT) == 1) {
apr_status_t t = apr_wait_for_io_or_timeout(NULL, sock, 0);
if (t != APR_SUCCESS) {
*len = 0;
return t;
}
arv = 1;
repeat = 1;
}
}
} while ((rv == -1 && errno == EINTR) || repeat);
if (rv == -1) {
*len = 0;
return errno;
}
/* Update how much we sent */
*len = nbytes;
if (apr_is_option_set(sock->netmask, APR_SO_TIMEOUT) &&
(*len < requested_len)) {
sock->netmask |= APR_INCOMPLETE_WRITE;
}
return APR_SUCCESS;
}
#else
#error APR has detected sendfile on your system, but nobody has written a
#error version of it for APR yet. To get past this, either write apr_sendfile
#error or change APR_HAS_SENDFILE in apr.h to 0.
#endif /* __linux__, __FreeBSD__, __HPUX__, _AIX, __MVS__, Tru64/OSF1 */
/* deprecated */
apr_status_t apr_sendfile(apr_socket_t *sock, apr_file_t *file,
apr_hdtr_t *hdtr, apr_off_t *offset, apr_size_t *len,
apr_int32_t flags)
{
return apr_socket_sendfile(sock, file, hdtr, offset, len, flags);
}
#endif /* APR_HAS_SENDFILE */
/* deprecated */
apr_status_t apr_send(apr_socket_t *sock, const char *buf, apr_size_t *len)
{
return apr_socket_send(sock, buf, len);
}
/* deprecated */
#ifdef HAVE_WRITEV
apr_status_t apr_sendv(apr_socket_t * sock, const struct iovec *vec,
apr_int32_t nvec, apr_size_t *len)
{
return apr_socket_sendv(sock, vec, nvec, len);
}
#endif
/* deprecated */
apr_status_t apr_sendto(apr_socket_t *sock, apr_sockaddr_t *where,
apr_int32_t flags, const char *buf, apr_size_t *len)
{
return apr_socket_sendto(sock, where, flags, buf, len);
}
/* deprecated */
apr_status_t apr_recvfrom(apr_sockaddr_t *from, apr_socket_t *sock,
apr_int32_t flags, char *buf,
apr_size_t *len)
{
return apr_socket_recvfrom(from, sock, flags, buf, len);
}
/* deprecated */
apr_status_t apr_recv(apr_socket_t *sock, char *buf, apr_size_t *len)
{
return apr_socket_recv(sock, buf, len);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -