Пример #1
0
static PRInt32 PR_CALLBACK SocketRecv(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
PRIntervalTime timeout)
{
	PRInt32 rv;
	PRThread *me = _PR_MD_CURRENT_THREAD();

	if (_PR_PENDING_INTERRUPT(me)) {
		me->flags &= ~_PR_INTERRUPT;
		PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
		return -1;
	}
	if (_PR_IO_PENDING(me)) {
		PR_SetError(PR_IO_PENDING_ERROR, 0);
		return -1;
	}

	PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv: fd=%p osfd=%d buf=%p amount=%d",
		    						fd, fd->secret->md.osfd, buf, amount));
	rv = _PR_MD_RECV(fd, buf, amount, flags, timeout);
	PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d",
		rv, PR_GetError(), PR_GetOSError()));
	return rv;
}
static PRInt32 PR_CALLBACK SocketRecv(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
PRIntervalTime timeout)
{
	PRInt32 rv;
	PRThread *me = _PR_MD_CURRENT_THREAD();

	if ((flags != 0) && (flags != PR_MSG_PEEK)) {
		PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
		return -1;
	}
	if (_PR_PENDING_INTERRUPT(me)) {
		me->flags &= ~_PR_INTERRUPT;
		PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
		return -1;
	}
	if (_PR_IO_PENDING(me)) {
		PR_SetError(PR_IO_PENDING_ERROR, 0);
		return -1;
	}

	PR_LOG(_pr_io_lm, PR_LOG_MAX,
		("recv: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d flags=%d",
		fd, fd->secret->md.osfd, buf, amount, flags));

#ifdef _PR_HAVE_PEEK_BUFFER
	if (fd->secret->peekBytes != 0) {
		rv = (amount < fd->secret->peekBytes) ?
			amount : fd->secret->peekBytes;
		memcpy(buf, fd->secret->peekBuffer, rv);
		if (flags == 0) {
			/* consume the bytes in the peek buffer */
			fd->secret->peekBytes -= rv;
			if (fd->secret->peekBytes != 0) {
				memmove(fd->secret->peekBuffer,
					fd->secret->peekBuffer + rv,
					fd->secret->peekBytes);
			}
		}
		return rv;
	}

	/* allocate peek buffer, if necessary */
	if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) {
		PR_ASSERT(0 == fd->secret->peekBytes);
		/* impose a max size on the peek buffer */
		if (amount > _PR_PEEK_BUFFER_MAX) {
			amount = _PR_PEEK_BUFFER_MAX;
		}
		if (fd->secret->peekBufSize < amount) {
			if (fd->secret->peekBuffer) {
				PR_Free(fd->secret->peekBuffer);
			}
			fd->secret->peekBufSize = amount;
			fd->secret->peekBuffer = PR_Malloc(amount);
			if (NULL == fd->secret->peekBuffer) {
				fd->secret->peekBufSize = 0;
				PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
				return -1;
			}
		}
	}
#endif

	rv = _PR_MD_RECV(fd, buf, amount, flags, timeout);
	PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d",
		rv, PR_GetError(), PR_GetOSError()));

#ifdef _PR_HAVE_PEEK_BUFFER
	if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) {
		if (rv > 0) {
			memcpy(fd->secret->peekBuffer, buf, rv);
			fd->secret->peekBytes = rv;
		}
	}
#endif

	return rv;
}