📄 xstreams.c
字号:
perror(buf); return(-1); } write(fd,SUCCESS,1); _XsTypeOfStream[fd] = _XsTypeOfStream[ufd]; PRMSG("ConnectLocalClient(%d) return success\n", ufd,0); return(fd);}static void dummy (sig)int sig;{}_XsCallLocalServer(host, idisplay, nettype)char *host;int idisplay;#ifdef SVR4struct netconfig *nettype;#elsechar *nettype;#endif{ char buf[MAX_AUTO_BUF_LEN]; char *listener; int type = X_LOCAL_STREAM; int fd; PRMSG("Calling CallLocalServer(%s)\n", host,0); sprintf(_dispno, "%d", idisplay); dispno = _dispno; /* * Open channel to server */ #ifdef SVR4 if (strncmp("NAMED", (char *) getenv("XLOCAL"), 5) == 0) { named = 1; type = X_NAMED_STREAM; listener = NAMED_LISTENER; sprintf(buf, "%s.%d", listener, idisplay); if((fd = OpenNamedServer(buf)) < 0) { PRMSG("Cannot open %s\n", buf,0); #ifdef DEBUG perror("XIO"); /* Sorry, but I don't have the dpy handy */ #endif return(-1); } }/* Enhanced Application Compatibility Support *//* End Enhanced Application Compatibility Support */#endif if(!named) { type = X_LOCAL_STREAM; listener = LOCAL_LISTENER; sprintf(buf, "%s.%d", listener, idisplay); PRMSG("buf for local listener %s\n",buf,0); if((fd = OpenLocalServer(buf)) < 0) { PRMSG("Cannot open %s\n", buf,0);#ifdef DEBUG perror("XIO"); /* Sorry, but I don't have the dpy handy */#endif return(-1); } } _XsTypeOfStream[fd] = type; if (_XsInputBuffer[fd].DataBuffer == NULL) if ((_XsInputBuffer[fd].DataBuffer = (char *) malloc(BUFFERSIZE)) == NULL) { errno = ENOMEM; perror("Client can't connect to local server"); return (-1); } _XsInputBuffer[fd].LastBytePtr = 0; _XsInputBuffer[fd].FirstBytePtr = 0; PRMSG("Calling CallLocalServer(%s) return success\n", host,fd); return(fd);}#ifdef SVR4static intOpenNamedServer(node)char *node;{ int fld; PRMSG("Calling 4.0 -- opening (%s)\n", node,0); fld = open(node, O_RDWR); if(fld <0) { fprintf(stderr,"OpenNamedServer failed:%s\n", sys_errlist[errno]); return(-1); } if(isastream(fld) != 1) { fprintf(stderr,"OpenNamedServer failed: %s is not a NamedStream\n", node); return(-1); } return (fld);}/* Enhanced Application Compatibility Support */static int_XsOpenSpServer(node)char *node;{}/* End Enhanced Application Compatibility Support */#endif /* SVR4 */static intOpenLocalServer(node) char *node;{ int server, fd, c; char buf[MAX_AUTO_BUF_LEN], *slave; PFV savef; if ((server = open (node, O_RDWR)) < 0) {#ifdef DEBUG fprintf(stderr, "open(%s) failed\n", node); perror(" ");#endif return(-1); } /* * Open streams based pipe and get slave name */ if ((fd = open (ptmx, O_RDWR)) < 0) { close (server); PRMSG("Cannot open %s\n", ptmx, 0); return(-1); } grantpt (fd); unlockpt (fd); if (! (slave = (char *) ptsname(fd))) { close(fd); close(server); PRMSG("Cannot get slave pt-name", 0, 0); return(-1); } if (chmod(slave, 0666) < 0) { close(fd); close(server); PRMSG("Cannot chmod %s\n", buf,0); return(-1); } c = strlen (slave); buf[0] = c; sprintf(&buf[1], slave); /* * write slave name to server */ write(server, buf, c+1); close (server); /* * wait for server to respond */ savef = signal (SIGALRM, dummy); alarm (CONNECT_TIMEOUT); if (read (fd, &c, 1) != 1) { fprintf(stderr, "No reply from the server.\n"); close(fd); fd = -1; } alarm (0); signal (SIGALRM, savef); return(fd);}#ifdef DEBUGstatic dumpBytes (len, data) int len; char *data;{ int i; fprintf(stderr, "%d: ", len); for (i = 0; i < len; i++) fprintf(stderr, "%02x ", data[i] & 0377); fprintf(stderr, "\n");}#endif#define XSTREAMS_COMPILE /* magic symbol to avoid lint problems */#ifdef SVR4#include "nameaddr.c"#else#include "nameserver.c"#endif#undef XSTREAMS_COMPILE_XsReadLocalStream(fd, buf, count, do_buffering)int fd;char *buf;int count;int do_buffering;{ int amount; InputBuffer *ioptr = &_XsInputBuffer[fd]; if (do_buffering == NO_BUFFERING){ amount = read(fd, buf, count); return (amount); } if (ioptr->LastBytePtr <= ioptr->FirstBytePtr) { errno = 0; if(count > BUFFERSIZE) { ioptr->LastBytePtr = ioptr->FirstBytePtr = 0; amount = read(fd, buf, count); return(amount); } ioptr->LastBytePtr = read(fd, ioptr->DataBuffer, BUFFERSIZE); ioptr->FirstBytePtr = 0; } if (ioptr->LastBytePtr > 0) { amount = ioptr->LastBytePtr - ioptr->FirstBytePtr; amount = amount > count ? count : amount; memcpy(buf, &ioptr->DataBuffer[ioptr->FirstBytePtr], amount); ioptr->FirstBytePtr += amount; return amount; } else { return (ioptr->LastBytePtr); }}_XsWriteLocalStream(fd, buf, count)int fd;char *buf;int count;{/* obsolete code */ return (write(fd, buf, count));}_XsCloseLocalStream(fd)int fd;{/* obvsolete code */ return (close(fd));} _XsConnectTliClient(sfd,MoreConnections)int sfd;char * MoreConnections;{ register char type = _XsTypeOfStream[sfd]; register struct listenQue *freeq, *pendq; freeq = &Network.FreeList[type]; pendq = &Network.PendingQue[type]; PRMSG("Calling ConnectTliClient(%d)\n", sfd,0); LookForEvents(freeq, pendq, sfd); return (CheckListenQue(freeq, pendq, sfd, MoreConnections));}static voidcheckNewEvent(fd)int fd;{ int t; t = t_look(fd); switch(t) { case T_DATA : fprintf(stderr, "T_DATA received\n"); break; case T_EXDATA : fprintf(stderr, "T_EXDATA received\n"); break; case T_DISCONNECT : t_rcvdis(fd, NULL); fprintf(stderr, "T_DISCONNECT received\n"); break; case T_ERROR : fprintf(stderr, "T_ERROR received\n"); break; case T_UDERR : fprintf(stderr, "T_UDERR received\n"); break; case T_ORDREL : fprintf(stderr, "T_ORDREL received\n"); break; }}_XsReadTliStream(fd, buf, count, do_buffering)int fd;char *buf;int count;int do_buffering;{ int n; int flags;/* obsolete code */}_XsWriteTliStream(fd, buf, count)int fd;char *buf;int count;{/* obsolete code */}static voidOnError(sig)int sig;{}_XsCloseTliStream(fd)int fd;{/* obsolete code */}/* * LookForEvents: handle an asynchronous event */static voidLookForEvents(FreeHead, PendHead, fd)struct listenQue *FreeHead;struct listenQue *PendHead;int fd;{ int address; short port, nf; struct t_discon disc; register struct listenCall *current; register struct t_call *call; int t; char buf[MAX_AUTO_BUF_LEN]; int flag, i; if((t = t_look(fd)) < 0) { PRMSG("t_look failed. t_errno %d\n", t_errno,0); return; } switch (t) { case 0: PRMSG("t_look 0\n",0,0); break; /* no return */ case T_LISTEN: PRMSG("t_look T_LISTEN\n",0,0); current = deQue(FreeHead); call = current->CurrentCall; if (t_listen(fd, call) < 0) { PRMSG("t_listen failed\n",0,0); return; } Que(PendHead, current, ~CLEAR); PRMSG("incoming call seq # %d", call->sequence,0); break; case T_DISCONNECT: PRMSG("t_look T_DISCONNECT\n",0,0); if (t_rcvdis(fd, &disc) < 0) { PRMSG("Received T_DISCONNECT but t_rcvdis failed\n",0,0); exit(1); } PRMSG("incoming disconnect seq # %d", disc.sequence,0); RemoveCall(FreeHead, PendHead, &disc); t_close(fd); _XsTypeOfStream[fd] = -1; break; case T_DATA : if((i = t_rcv(fd, buf, MAX_AUTO_BUF_LEN, &flag)) > 0) break; default: PRMSG("t_look default %o %x\n", t, t); break; }}/* * CheckListenQue: try to accept a connection */static intCheckListenQue(FreeHead, PendHead, fd, MoreConnections)struct listenQue *FreeHead;struct listenQue *PendHead;int fd;char * MoreConnections;{ register struct listenCall *current; register struct t_call *call; int pid, nfd, n; char *retptr, *ptr; int address; short port, nf; PRMSG( "in CheckListenQue",0,0); if (!(EMPTY(PendHead))) { current = deQue(PendHead); call = current->CurrentCall; PRMSG( "try to accept #%d", call->sequence,0); if((nfd = OpenVirtualCircuit(fd)) < 0) { PRMSG( "OpenVirtualCircuit failed\n",0,0); Que(FreeHead, current, CLEAR); *MoreConnections = !EMPTY(PendHead); return(-1); /* let transport provider generate disconnect */ } n = t_accept(fd, nfd, call); if (n < 0){ PRMSG( "t_accept failed\n",0,0); if (t_errno == TLOOK) { t_close(nfd); PRMSG( "t_accept collision",0,0); PRMSG( "save call #%d", call->sequence,0); pQue(PendHead, current); *MoreConnections = !EMPTY(PendHead); return(-1); } else { PRMSG( "t_accept failed but not t_look\n",0,0); t_close(nfd); Que(FreeHead, current, CLEAR); *MoreConnections = !EMPTY(PendHead); return(-1); } } _XsTypeOfStream[nfd] = _XsTypeOfStream[fd]; retptr = NULL; if( GetNetworkInfo (nfd, Network._net[_XsTypeOfStream[fd]], ConvertTliCallToName, addtliheader(call), &retptr, NULL) <= 0) { retptr = NULL; } ptr = NULL; if(retptr != NULL) { ptr = retptr; retptr += sizeof(xHostEntry); } GetNetworkInfo (nfd, Network._net[_XsTypeOfStream[fd]], PEER_ALLOC, &retptr, NULL); if(ptr != NULL) Xfree(ptr); PRMSG( "Accepted call %d", call->sequence,0); PRMSG("Channel %d is opened\n", nfd,0); Que(FreeHead, current, CLEAR);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -