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

📄 xstreams.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
                (void) ioctl(nfd, I_POP, "timod");		if(ioctl(nfd, I_PUSH, "tirdwr") < 0)		{                 	t_close(nfd);                        return(-1);		}		PRMSG( "Accepted call %d", call->sequence,0);		PRMSG("Channel %d is opened\n", nfd,0);		*MoreConnections = !EMPTY(PendHead);		return(nfd);	}		*MoreConnections = !EMPTY(PendHead);	return(-1);}/* * ClearCall:	clear out a call structure */static voidClearCall(call)struct t_call *call;{	call->sequence = 0;	call->addr.len = 0;	call->opt.len = 0;	call->udata.len = 0;	memset(call->addr.buf, 0, call->addr.maxlen);	memset(call->opt.buf, 0, call->opt.maxlen);	memset(call->udata.buf, 0, call->udata.maxlen);}/* * RemoveCall: remove call from pending list */static voidRemoveCall(freeq, pendq, disc)struct listenQue *freeq;struct listenQue *pendq;struct t_discon *disc;{	register struct listenCall *p, *oldp;	PRMSG( "Removing call, sequence # is %d", disc->sequence,0);	if (EMPTY(pendq)) {		disc->sequence = -1;		return;	}	p = pendq->QueHead;	oldp = (struct listenCall *) NULL;	while (p) {		if (p->CurrentCall->sequence == disc->sequence) {			if (oldp == (struct listenCall *) NULL) {				pendq->QueHead = p->NextCall;				if (pendq->QueHead == (struct listenCall *) NULL) {					pendq->QueTail = (struct listenCall *) NULL;				}			}			else if (p == pendq->QueTail) {				oldp->NextCall = p->NextCall;				pendq->QueTail = oldp;			}			else {				oldp->NextCall = p->NextCall;			}			Que(freeq, p, CLEAR);			disc->sequence = -1;			return;		}		oldp = p;		p = p->NextCall;	}	disc->sequence = -1;	return;}static intnameserver(fd, nettype, service, arg1, arg2, arg3)    int       fd;#ifdef SVR4    struct netconfig *nettype;#else    char     *nettype;#endif     int	      service;    char     **arg1, **arg2;    int	     *arg3;{    char	*ptr;    int	n;    int	type;	if (fd >= 0)	    type = _XsTypeOfStream[fd];	else	    type = X_TLI_STREAM;	if(type < X_TLI_STREAM || type >= Network._nnets)	{		if(type == X_LOCAL_STREAM || type == X_NAMED_STREAM)			return(0);		if(fd >= 0)    		{		PRMSG("in nameserver type %d unknown d\n", type, fd);		return(-1);		}    	}    if(nettype == NULL)	nettype = Network._net[type];    switch(service){      case	OpenDaemonConnection :#ifdef SVR4 	return(InitializeNetPath());#else	if(NameServer < 0 )	    NameServer = OpenLocalServer(NAME_SERVER_NODE);	return(NameServer);#endif /* SVR4 */      case	ConvertTliCallToName :      case	ConvertNetAddrToName :      case	ConvertNameToNetAddr :      case	ConvertNameToTliBind :      case	ConvertNameToTliCall :	if((n = CallTheNameServer(service, nettype, arg1, arg2, arg3)) < 0)	    return(-1);	return(n);      case	PEER_NAME  :	if( fd < Network._npeers )	{	    *arg2 = Network._peer[fd];	    return(1);	}	return(-1);      case	PEER_ALLOC :	if(fd >= Network._npeers)	    return(-1);	if(*arg1 == NULL){	    n = 0;	}	else	n = strlen(*arg1);	Network._peerlen[fd] = n;	if(n > 0){	    if(Network._peerlen[fd] > UNAME_LENGTH)		Network._peerlen[fd] = UNAME_LENGTH;	    bcopy(*arg1, Network._peer[fd], Network._peerlen[fd]);	    Network._peer[fd][Network._peerlen[fd]] = '\0';	}	else {		    Network._peer[fd][0] = '\0';	}	return(1);				      case	PEER_FREE  :	if(fd < Network._npeers && Network._peer[fd] != NULL)	{	    Network._peer[fd][0] = '\0';	    Network._peerlen[fd] = 0;	}	return(1);    }}static	int	_hlen = 0;static	char	*_hptr = NULL;static char	**addheader(string, len)char	*string;int	len;{	int	n, m, p;	char	*ptr;	n = len;	m = n + sizeof(xHostEntry);	p = m + 2 * sizeof(int);		if(p > _hlen){		if(_hptr == NULL)			_hptr = malloc(p);		else	_hptr = realloc(_hptr, p);		}	if(_hptr == NULL){		fprintf(stderr, "addheader(): malloc failed\n");		exit(1);		}	else if(p > _hlen)		_hlen = p;			ptr = _hptr;		*(int *) ptr = m;	ptr += sizeof(int);	*(int *) ptr = 1;	ptr += sizeof(int);	((xHostEntry *) ptr)-> length = n;	ptr += sizeof(xHostEntry);	memcpy(ptr, string, n);		return(&_hptr);}static char	**addtliheader(call)struct t_call *call;{		char	*ptr;	int	a, o, u;	int	ra, ro, ru, rentlen;	a = call->addr.len;	o = call->opt.len;	u = call->udata.len;	ra = ((a + sizeof(xHostEntry) +3) >>2) << 2;	ro = ((o + sizeof(xHostEntry) +3) >>2) << 2;	ru = ((u + sizeof(xHostEntry) +3) >>2) << 2;	rentlen = ra + ro + ru + 2 * sizeof(int);	if(rentlen > _hlen){		if(_hptr == NULL)                        _hptr = malloc(rentlen);		else	_hptr = realloc(_hptr, rentlen);		}	if(_hptr == NULL){		fprintf(stderr, "addheader(): malloc failed\n");		exit(1);		}	else if(rentlen > _hlen)		_hlen = rentlen;        ptr = _hptr;	        *(int *) ptr = rentlen - 2 * sizeof(int);	ptr += sizeof(int);	*(int *) ptr = 1;	ptr += sizeof(int);	((xHostEntry *) ptr)-> length = a;	if(a > 0){		memcpy(ptr + sizeof(xHostEntry), call->addr.buf, a);		}		ptr += ra;	((xHostEntry *) ptr)-> length = o;	if(o > 0)		memcpy(ptr + sizeof(xHostEntry), call->opt.buf, o);		ptr += ro;	((xHostEntry *) ptr)-> length = u;	if(u > 0){		memcpy(ptr + sizeof(xHostEntry), call->udata.buf, u);                }	return(&_hptr);}int _XBytesReadable (fd, ptr)int fd;int * ptr;{	int inbuf;	int n;	int flg;	InputBuffer *ioptr = &_XsInputBuffer[fd];	inbuf = ioptr->LastBytePtr - ioptr->FirstBytePtr;        if (inbuf >= SIZEOF(xReply))	{		*ptr = inbuf;		return (0);	}	if (ioptr->FirstBytePtr > 0)	{		/* move tidbit to front of buffer */		bcopy(&ioptr->DataBuffer[ioptr->FirstBytePtr],		      ioptr->DataBuffer, inbuf);		/* Adjust pointers in buffer to reflect move */		ioptr->LastBytePtr = inbuf;		ioptr->FirstBytePtr = 0;	}	if (inbuf < 0)	{		inbuf = 0;		ioptr->LastBytePtr = 0;	}	/* Read no more than number of bytes left in buffer */        errno = 0;   	n = read(fd, &ioptr->DataBuffer[inbuf], BUFFERSIZE-inbuf);	if (n > 0)	{		ioptr->LastBytePtr += n;		*ptr = ioptr->LastBytePtr;		return (0);	}	else	{		if (errno == EWOULDBLOCK)		{			*ptr = ioptr->LastBytePtr;			return (0);		}		else		{			if (n == 0 )			{				errno = EPIPE;				return (-1);			}			else			{				if (errno != EINTR)					return (-1);				else				{					*ptr = ioptr->LastBytePtr;					return (0);				}			}		}	}}#ifndef SVR4#include <sys/poll.h>#define POLLERROR		(POLLHUP | POLLNVAL | POLLERR)#define PFD(fds, i, x) { 	if (fds) 		if (ev & (x)) 			BITSET (fds, i); 		else 			BITCLEAR (fds, i); }#define ERROR(x) { 	errno = x; 	return -1; }/*	simulate BSD select system call with SYSV poll system call	note that efds parameter is not fully supported (or understood)*/extern long ulimit();intpollselect (nfds, rfds, wfds, efds, timeout)int nfds;unsigned long *rfds;unsigned long *wfds;unsigned long *efds;struct timeval *timeout;{	int i, rc, ev, timevalue;	struct pollfd pfds[NOFILES_MAX];	static long _NOFILE = 0;	PRMSG("in pollselect\n", 0,0);	if (_NOFILE == 0) {		_NOFILE = ulimit(4, (long)0);		if (_NOFILE > NOFILES_MAX)			_NOFILE = NOFILES_MAX;	} 	if (nfds > _NOFILE)		nfds = _NOFILE;   /* make poll happy */	for (i = 0; i < nfds; i++)	{		ev = 0;		if (rfds && GETBIT (rfds, i)) ev |= POLLIN;		if (wfds && GETBIT (wfds, i)) ev |= POLLOUT;		if (ev || (efds && GETBIT (efds, i)))			pfds[i].fd = i;		else			pfds[i].fd = -1;		pfds[i].events = ev;	}	if (timeout)		timevalue = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;	else		timevalue = -1;	while (1)	{	   rc = poll (pfds, (unsigned long)nfds, timevalue);	   if(rc<0 && errno == EAGAIN)		continue;	   else	break;	}	if(rc>0)	{		if (!efds)			for (i = 0; i < nfds; ++i)			{				ev = pfds[i].revents;				if (ev & POLLERROR)					ERROR (EBADF);			}		for (i = 0; i < nfds; ++i)		{			ev = pfds[i].revents;			PFD (rfds, i, POLLIN);			PFD (wfds, i, POLLOUT);			PFD (efds, i, POLLERROR);		}	} 	if(rc==0)	{		i = (nfds+ 7)/8;		if ( rfds != NULL)			memset((char *) rfds, 0, i);		if ( wfds != NULL)			memset((char *) wfds, 0, i);		if ( efds != NULL)			memset((char *) efds, 0, i);		}		return rc;}#define SELECT	pollselect#else#define SELECT	select#endif	/* ndef SVR4 *//*	The following routine is used for USL Compatibility between *	Operating System versions of SVR4.0 and SVR3.2. In System *	V Release 3.2 the select call is not available, and the *	pollselect is used to poll for connections. In SVR4.0 the * 	system supplies select and it is used. However, the arguments *	to these routines are  not identical so this routine  * 	takes care of operating system differences by calling the * 	proper os dependent routine. It is called (by USL) from the *	server, Xlib  and Xt.*/int_XSelect(nfds, r_mask, w_mask, e_mask, timeout)  int			nfds;  unsigned long		*r_mask, *w_mask, *e_mask;  struct timeval	*timeout;{  int			retval = 0;  int			count = 0;  int			i;  unsigned long		save_mask[MSKCNT];  static struct timeval	notime;/*PRMSG("IN XSelect from Xstreams nfds=%d r_mask=%0x\n",nfds ,r_mask);*/  if (r_mask) {    CLEARBITS(save_mask);    for (i = 0; i < nfds; ++i) {      if (GETBIT(r_mask, i)      && (_XsInputBuffer[i].LastBytePtr - _XsInputBuffer[i].FirstBytePtr) > 0) {        BITCLEAR(r_mask, i);        BITSET(save_mask, i);        ++count;      }    }  }  if (count) {    if (_XANYSET(r_mask)    || (w_mask && _XANYSET(w_mask))    || (e_mask && _XANYSET(e_mask))) {      retval = SELECT(nfds, r_mask, w_mask, e_mask, &notime);    }    for (i = 0; i < nfds; ++i) {      if (GETBIT(save_mask, i)) {        BITSET(r_mask, i);      }    }    return retval < 0 ? retval : retval + count;  } else {    return SELECT(nfds, r_mask, w_mask, e_mask, timeout);  }} 	/* XSelect() */#else /* not STREAMSCONN */#ifndef lintstatic int dummy;	/* prevent ranlibs from complaining */#endif#endif /* STREAMSCONN */

⌨️ 快捷键说明

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