📄 io.c
字号:
/***********************************************************Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,and the Massachusetts Institute of Technology, Cambridge, Massachusetts. All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the names of Digital or MIT not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* $XConsortium: io.c,v 1.49 88/09/06 15:50:44 jim Exp $ *//***************************************************************** * i/o functions * * WriteToClient, ReadRequestFromClient * *****************************************************************/#include <stdio.h>#include "Xos.h"#include "Xmd.h"#include <errno.h>#include <sys/param.h>#include <sys/types.h>#include <sys/uio.h>#include "X.h"#include "Xproto.h"#include "os.h"#include "osdep.h"#include "opaque.h"#include "dixstruct.h"#include "misc.h"#ifdef ISOCONN#include <isode/tsap.h>#endif /* ISOCONN */extern long ClientsWithInput[];extern long ClientsWriteBlocked[];extern long OutputPending[];extern long OutputBufferSize;extern ClientPtr ConnectionTranslation[];extern Bool NewOutputPending;extern Bool AnyClientsWriteBlocked;static Bool CriticalOutputPending;static int timesThisConnection = 0;extern int errno;#define request_length(req, cli) ((cli->swapped ? \ lswaps((req)->length) : (req)->length) << 2)#define MAX_TIMES_PER 10#ifdef ISOCONN/* * Convenience Routines */TWriteToClient(sd, buf, len)int sd, len;char *buf;{ struct TSAPdisconnect tds; struct TSAPdisconnect *td = &tds; if (TDataRequest(sd, buf, len, td) == NOTOK){ if (errno != EWOULDBLOCK) fprintf(stderr, "TWriteToClient: %s\n", td->td_reason); }}/* * sd = transport descriptor * iov is iovec of iovcnt buffers */TWritevToClient(sd, iov, iovcnt)int sd, iovcnt;struct iovec *iov;{ int i, ret, tot = 0; struct udvec uv[64], *uvp = uv; struct TSAPdisconnect tds; struct TSAPdisconnect *td = &tds;/* * Grotty hack */ if (iovcnt >= 64) { fprintf(stderr, "Oh Spaghettio\n"); return -1; } for(i=0; i<iovcnt; i++, uvp++, iov++) { uvp->uv_base = iov->iov_base; uvp->uv_len = iov->iov_len; tot += iov->iov_len; } uvp->uv_base = NULL; uvp->uv_len = 0; ret = TWriteRequest (sd, uv, td); if (ret == NOTOK) {#ifdef ISODEBUG if (errno != EWOULDBLOCK) if (isodexbug) fprintf(stderr, "TWritevToCl: %s\n", TErrString(td->td_reason));#endif /* ISODEBUG */ return ret; } else {#ifdef ISODEBUG if (isodexbug) fprintf(stderr, "TWritevToCl to %d: %d\n", sd, tot);#endif /* ISODEBUG */ return tot; }}TAcceptFromClient(fd, vecp, vec)int fd;int vecp;char **vec;{ struct TSAPdisconnect tds; struct TSAPdisconnect *td = &tds; struct TSAPstart tsts; struct TSAPstart *tst = &tsts; if (TInit(vecp, vec, tst, td) == NOTOK) { Error(TErrString(td->td_reason)); Error("TInit"); return -1; } if (TConnResponse(tst->ts_sd, NULLTA, tst->ts_expedited, NULL, 0, &(tst->ts_qos), td) == NOTOK) { Error(TErrString(td->td_reason)); Error("TConnResponse"); return -1; } return tst->ts_sd;}TDiscFromClient(fd)int fd;{ struct TSAPdisconnect tds; if (TDiscRequest(fd, NULLCP, 0, &tds)==NOTOK) fprintf(stderr, "TDisc Failed %s\n", TErrString(tds.td_reason));}#endif /* ISOCONN *//***************************************************************** * ReadRequestFromClient * Returns one request from client. If the client misbehaves, * returns NULL. The dispatcher closes down all misbehaving clients. * * client: index into bit array returned from WaitForSomething() * * status: status is set to * > 0 the number of bytes in the request if the read is sucessful * = 0 if action would block (entire request not ready) * < 0 indicates an error (probably client died) * * oldbuf: * To facilitate buffer management (e.g. on multi-processor * systems), the diX layer must tell the OS layer when it is * done with a request, so the parameter oldbuf is a pointer * to a request that diX is finished with. In the * sample implementation, which is single threaded, * oldbuf is ignored. We assume that when diX calls * ReadRequestFromClient(), the previous buffer is finished with. * * The returned string returned must be contiguous so that it can be * cast in the dispatcher to the correct request type. Because requests * are variable length, ReadRequestFromClient() must look at the first 4 * bytes of a request to determine the length (the request length is * always the 3rd byte in the request). * * Note: in order to make the server scheduler (WaitForSomething()) * "fair", the ClientsWithInput mask is used. This mask tells which * clients have FULL requests left in their buffers. Clients with * partial requests require a read. Basically, client buffers * are drained before select() is called again. But, we can't keep * reading from a client that is sending buckets of data (or has * a partial request) because others clients need to be scheduled. *****************************************************************/ConnectionInput inputBuffers[MAXSOCKS]; /* buffers for clients *//*ARGSUSED*/char *ReadRequestFromClient(who, status, oldbuf) ClientPtr who; int *status; /* read at least n from client */ char *oldbuf;{#define YieldControl() \ { isItTimeToYield = TRUE; \ timesThisConnection = 0; }#define YieldControlNoInput() \ { YieldControl(); \ BITCLEAR(ClientsWithInput, client); }#define YieldControlAndReturnNull() \ { YieldControlNoInput(); \ return((char *) NULL ); } OsCommPtr oc = (OsCommPtr)who->osPrivate; int client = oc->fd; int result, gotnow, needed; register ConnectionInput *pBuff; register xReq *request; /* ignore oldbuf, just assume we're done with prev. buffer */ if (client == -1) { ErrorF( "OH NO, %d translates to -1\n", who); return((char *)NULL); } pBuff = &inputBuffers[client]; pBuff->bufptr += pBuff->lenLastReq; pBuff->lenLastReq = 0; /* handle buffer empty or full case first */ if ((pBuff->bufptr - pBuff->buffer) >= pBuff->bufcnt) {#ifdef ISOCONN result = SRead(client, pBuff->buffer, pBuff->size, OK);#else /* ISOCONN */ result = read(client, pBuff->buffer, pBuff->size);#endif /* ISOCONN */ if (result < 0) { if (errno == EWOULDBLOCK) *status = 0; else *status = -1; YieldControlAndReturnNull(); } else if (result == 0) { *status = -1; YieldControlAndReturnNull(); } else { pBuff->bufcnt = result; /* free up some space after huge requests */ if ((pBuff->size > BUFWATERMARK) && (result < BUFSIZE)) { pBuff->size = BUFSIZE; pBuff->buffer = (char *)xrealloc(pBuff->buffer, pBuff->size); } pBuff->bufptr = pBuff->buffer; } } /* now look if there is enough in the buffer */ request = (xReq *)pBuff->bufptr; gotnow = pBuff->bufcnt + pBuff->buffer - pBuff->bufptr; if (gotnow < sizeof(xReq)) needed = sizeof(xReq) - gotnow; else { needed = request_length(request, who); if (needed > MAXBUFSIZE) { *status = -1; YieldControlAndReturnNull(); } if (needed <= 0) needed = sizeof(xReq); } /* if the needed amount won't fit in what's remaining, move everything to the front of the buffer. If the entire header isn't available, move what's there too */ if ((pBuff->bufptr + needed - pBuff->buffer > pBuff->size) || (gotnow < sizeof(xReq))) { bcopy(pBuff->bufptr, pBuff->buffer, gotnow); pBuff->bufcnt = gotnow; if (needed > pBuff->size) { pBuff->size = needed; pBuff->buffer = (char *)xrealloc(pBuff->buffer, needed); } pBuff->bufptr = pBuff->buffer; } /* don't have a full header */ if (gotnow < sizeof(xReq)) { while (pBuff->bufcnt + pBuff->buffer - pBuff->bufptr < sizeof(xReq)) {#ifdef ISOCONN result = SRead(client, 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(); } if (result == 0) { *status = -1; YieldControlAndReturnNull(); } pBuff->bufcnt += result; } request = (xReq *)pBuff->bufptr; gotnow = pBuff->bufcnt + pBuff->buffer - pBuff->bufptr; needed = request_length(request, who); if (needed <= 0) needed = sizeof(xReq); if (needed > pBuff->size) { pBuff->size = needed; pBuff->buffer = (char *)xrealloc(pBuff->buffer, needed); } pBuff->bufptr = pBuff->buffer; } if (gotnow < needed ) { int i, wanted; wanted = needed - gotnow; i = 0; while (i < wanted) {#ifdef ISOCONN result = SRead(client,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -