⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 io.c

📁 ftam等标准协议服务器和客户端的源代码。
💻 C
📖 第 1 页 / 共 2 页
字号:
			    pBuff->buffer + pBuff->bufcnt, 			    pBuff->size - pBuff->bufcnt,			    OK); #else /* ISOCONN */	    result = read(client, pBuff->buffer + pBuff->bufcnt, 			  pBuff->size - pBuff->bufcnt); #endif /* ISOCONN */	    if (result < 0) 	    {		if (errno == EWOULDBLOCK)		    *status = 0;		else		    *status = -1;		YieldControlAndReturnNull();	    }	    else if (result == 0)	    {		*status = -1;		YieldControlAndReturnNull();	    }	    i += result;    	    pBuff->bufcnt += result;	}    }    *status = needed;    pBuff->lenLastReq = needed;    /*     *  Check to see if client has at least one whole request in the     *  buffer.  If there is only a partial request, treat like buffer     *  is empty so that select() will be called again and other clients     *  can get into the queue.        */    timesThisConnection++;    if (pBuff->bufcnt + pBuff->buffer >= pBuff->bufptr + needed + sizeof(xReq))     {	request = (xReq *)(pBuff->bufptr + needed);        if ((pBuff->bufcnt + pBuff->buffer) >=             ((char *)request + request_length(request, who)))	    BITSET(ClientsWithInput, client);        else	    YieldControlNoInput();    }    else	YieldControlNoInput();    if (timesThisConnection == MAX_TIMES_PER)	YieldControl();    return((char *)pBuff->bufptr);#undef YieldControlAndReturnNull#undef YieldControlNoInput#undef YieldControl}    /* lookup table for adding padding bytes to data that is read from    	or written to the X socket.  */static int padlength[4] = {0, 3, 2, 1}; /******************** * FlushClient() *    If the client isn't keeping up with us, then we try to continue *    buffering the data and set the apropriate bit in ClientsWritable *    (which is used by WaitFor in the select).  If the connection yields *    a permanent error, or we can't allocate any more space, we then *    close the connection. * **********************/static intFlushClient(who, oc, extraBuf, extraCount)    ClientPtr who;    OsCommPtr oc;    char *extraBuf;    int extraCount; /* do not modify... returned below */{    int connection = oc->fd,    	total, n, i, notWritten, written,	iovCnt = 0;    struct iovec iov[3];    char padBuffer[3];#ifdef ISOCONN    struct TSAPdisconnect tds;#endif /* ISOCONN */    total = 0;    if (oc->count)    {	total += iov[iovCnt].iov_len = oc->count;	iov[iovCnt++].iov_base = (caddr_t)oc->buf;        /* Notice that padding isn't needed for oc->buf since           it is alreay padded by WriteToClient */    }    if (extraCount)    {	total += iov[iovCnt].iov_len = extraCount;	iov[iovCnt++].iov_base = extraBuf;	if (extraCount & 3)	{	    total += iov[iovCnt].iov_len = padlength[extraCount & 3];	    iov[iovCnt++].iov_base = padBuffer;	}    }    notWritten = total;	#ifdef ISOCONN    while ((n = SWritev (connection, iov, iovCnt)) != notWritten)#else /* ISOCONN */    while ((n = writev (connection, iov, iovCnt)) != notWritten)#endif /* ISOCONN */    {#ifdef hpux	if (n == -1 && errno == EMSGSIZE)	    n = swWritev (connection, iov, 2);#endif        if (n > 0)         {	    notWritten -= n;	    for (i = 0; i < iovCnt; i++)            {		if (n > iov[i].iov_len)		{		    n -= iov[i].iov_len;		    iov[i].iov_len = 0;		}		else		{		    iov[i].iov_len -= n;		    iov[i].iov_base += n;		    break;		}	    }	    continue;	}	else if (errno != EWOULDBLOCK)        {#ifdef notdef  	    if (errno != EBADF)		ErrorF("Closing connection %d because write failed\n",			connection);		/* this close will cause the select in WaitForSomething		   to return that the connection is dead, so we can actually		   clean up after the client.  We can't clean up here,		   because the we're in the middle of doing something		   and will probably screw up some data strucutres */#endif#ifdef ISOCONN	    SClose(connection);#else /* ISOCONN */	    close(connection);#endif /* ISOCONN */            MarkClientException(who);	    return(-1);	}	/* If we've arrived here, then the client is stuffed to the gills	   and not ready to accept more.  Make a note of it and buffer	   the rest. */	BITSET(ClientsWriteBlocked, connection);	AnyClientsWriteBlocked = TRUE;	written = total - notWritten;	if (written < oc->count)	{	    if (written > 0)	    {		oc->count -= written;		bcopy((char *)oc->buf + written, (char *)oc->buf, oc->count);		written = 0;	    }	}	else	{	    written -= oc->count;	    oc->count = 0;	}	if (notWritten > oc->bufsize)	{	    /* allocate at least enough to contain it plus one	       OutputBufferSize */	    oc->bufsize = notWritten + OutputBufferSize;	    oc->buf = (unsigned char *)xrealloc(oc->buf, oc->bufsize);	    if (oc->buf == NULL)	    {	outOfMem:#ifdef notdef		ErrorF("Closing connection %d because out of memory\n",			connection);		/* this close will cause the select in WaitForSomething		   to return that the connection is dead, so we can actually		   clean up after the client.  We can't clean up here,		   because the we're in the middle of doing something		   and will probably screw up some data strucutres */#endif#ifdef ISOCONN#ifdef ISODEBUG		fprintf(stderr, "out of mem: closing connection %d\n", 			connection);#endif /* ISODEBUG */		SClose(connection);#else /* ISOCONN */		close(connection);#endif /* ISOCONN */		MarkClientException(who);		oc->count = 0;		oc->bufsize = 0;		return(-1);	    }	}	/* If the amount written extended into the padBuffer, then the	   difference "extraCount - written" may be less than 0 */	if ((n = extraCount - written) > 0)	    bcopy (extraBuf + written, (char *)oc->buf + oc->count, n);	oc->count = notWritten; /* this will include the pad */	return extraCount; /* return only the amount explicitly requested */    }    /* everything was flushed out */    oc->count = 0;    if (oc->bufsize > OutputBufferSize)    {	oc->bufsize = OutputBufferSize;	oc->buf = (unsigned char *)xrealloc(oc->buf, OutputBufferSize);	if (oc->buf == NULL) /* nearly impossible */	    goto outOfMem;    }    return extraCount; /* return only the amount explicitly requested */} /******************** * FlushAllOutput() *    Flush all clients with output.  However, if some client still *    has input in the queue (more requests), then don't flush.  This *    will prevent the output queue from being flushed every time around *    the round robin queue.  Now, some say that it SHOULD be flushed *    every time around, but... * **********************/voidFlushAllOutput(){    register int index, base, mask;    OsCommPtr oc;    register ClientPtr client;    if (! NewOutputPending)	return;    /*     * It may be that some client still has critical output pending,     * but he is not yet ready to receive it anyway, so we will     * simply wait for the select to tell us when he's ready to receive.     */    CriticalOutputPending = FALSE;    NewOutputPending = FALSE;    for (base = 0; base < mskcnt; base++)    {	mask = OutputPending[ base ];	OutputPending[ base ] = 0;	while (mask)	{	    index = ffs(mask) - 1;	    mask &= ~lowbit(mask);	    if ((client = ConnectionTranslation[(32 * base) + index ]) == NULL)		continue;	    if (client->clientGone)		continue;	    oc = (OsCommPtr)client->osPrivate;	    if (GETBIT(ClientsWithInput, client->index))	    {		BITSET(OutputPending, oc->fd); /* set the bit again */		NewOutputPending = TRUE;	    }	    else		FlushClient(client, oc, (char *)NULL, 0);	}    }}voidFlushIfCriticalOutputPending(){    if (CriticalOutputPending)	FlushAllOutput();}voidSetCriticalOutputPending(){    CriticalOutputPending = TRUE;}/***************** * WriteToClient *    Copies buf into ClientPtr.buf if it fits (with padding), else *    flushes ClientPtr.buf and buf to client.  As of this writing, *    every use of WriteToClient is cast to void, and the result *    is ignored.  Potentially, this could be used by requests *    that are sending several chunks of data and want to break *    out of a loop on error.  Thus, we will leave the type of *    this routine as int. *****************/intWriteToClient (who, count, buf)    ClientPtr who;    char *buf;    int count;{    OsCommPtr oc = (OsCommPtr)who->osPrivate;    int padBytes;    if (oc->fd == -1)     {	ErrorF( "OH NO, %d translates to -1\n", oc->fd);	return(-1);    }    if (oc->fd == -2)     {#ifdef notdef	ErrorF( "CONNECTION %d ON ITS WAY OUT\n", oc->fd);#endif	return(-1);    }    padBytes =  padlength[count & 3];    if (oc->count + count + padBytes > oc->bufsize)    {	BITCLEAR(OutputPending, oc->fd);	CriticalOutputPending = FALSE;	NewOutputPending = FALSE;	return FlushClient(who, oc, buf, count);    }    NewOutputPending = TRUE;    BITSET(OutputPending, oc->fd);    bcopy(buf, (char *)oc->buf + oc->count, count);    oc->count += count + padBytes;        return(count);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -