/[gcl]/gcl/o/nsocket.c
ViewVC logotype

Diff of /gcl/o/nsocket.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by camm, Fri Jul 12 22:00:49 2002 UTC revision 1.4 by camm, Sat Jul 20 07:10:55 2002 UTC
# Line 137  main(argc,argv) Line 137  main(argc,argv)
137   */   */
138    
139  static int  static int
140  CreateSocketAddress(sockaddrPtr, host, port)  CreateSocketAddress(struct sockaddr_in *sockaddrPtr, char *host, int port)
141      struct sockaddr_in *sockaddrPtr;    /* Socket address */                                          /* Socket address */
142      char *host;                         /* Host.  NULL implies INADDR_ANY */                                          /* Host.  NULL implies INADDR_ANY */
143      int port;                           /* Port number */                                          /* Port number */
144  {  {
145      struct hostent *hostent;            /* Host database entry */      struct hostent *hostent;            /* Host database entry */
146      struct in_addr addr;                /* For 64/32 bit madness */      struct in_addr addr;                /* For 64/32 bit madness */
# Line 186  CreateSocketAddress(sockaddrPtr, host, p Line 186  CreateSocketAddress(sockaddrPtr, host, p
186    
187  /* return -1 on failure, or else an fd */  /* return -1 on failure, or else an fd */
188  int  int
189  CreateSocket(port, host, server, myaddr, myport, async)  CreateSocket(int port, char *host, int server, char *myaddr, int myport, int async)
190      int port;                   /* Port number to open. */                                  /* Port number to open. */
191      char *host;                 /* Name of host on which to open port.                                  /* Name of host on which to open port.
192                                   * NULL implies INADDR_ANY */                                   * NULL implies INADDR_ANY */
193      int server;                 /* 1 if socket should be a server socket,                                  /* 1 if socket should be a server socket,
194                                   * else 0 for a client socket. */                                   * else 0 for a client socket. */
195      char *myaddr;               /* Optional client-side address */                                  /* Optional client-side address */
196      int myport;                 /* Optional client-side port */                                  /* Optional client-side port */
197      int async;                  /* If nonzero and creating a client socket,                                  /* If nonzero and creating a client socket,
198                                   * attempt to do an async connect. Otherwise                                   * attempt to do an async connect. Otherwise
199                                   * do a synchronous connect or bind. */                                   * do a synchronous connect or bind. */
200  {  {
# Line 354  object sock; Line 354  object sock;
354  { struct sockaddr_in sockname;  { struct sockaddr_in sockname;
355   int size = sizeof(struct sockaddr_in);   int size = sizeof(struct sockaddr_in);
356   struct hostent *hostEntPtr;   struct hostent *hostEntPtr;
357   object address,host,port;   object address,host;
358    
359   check_socket(sock);   check_socket(sock);
360   if (getsockname(SOCKET_FD(sock), (struct sockaddr *) &sockname, &size)   if (getsockname(SOCKET_FD(sock), (struct sockaddr *) &sockname, &size)
# Line 380  object sock; Line 380  object sock;
380       the channel is setto blocking or nonblocking mode.       the channel is setto blocking or nonblocking mode.
381  */    */  
382    
383  DEFUN("SET-BLOCKING",int,fSset_blocking,SI,2,2,NONE,IO,OO,OO,OO,  DEFUN("SET-BLOCKING",object,fSset_blocking,SI,2,2,NONE,OO,OO,OO,OO,
384        "Set blocking on if MODE is T otherwise off.  Return 0 if succeeds. Otherwise the error number.")(sock,setBlocking)        "Set blocking on if MODE is T otherwise off.  Return 0 if succeeds. Otherwise the error number.")(sock,setBlocking)
385        object sock;        object sock;
386        object setBlocking;        object setBlocking;
387  {  {
388        int setting;        int setting;
389        int fd ;        int fd ;
       FILE *fp;  
390     AGAIN:     AGAIN:
391        check_stream(sock);        check_stream(sock);
392        /* set our idea of whether blocking on or off        /* set our idea of whether blocking on or off
# Line 409  DEFUN("SET-BLOCKING",int,fSset_blocking, Line 408  DEFUN("SET-BLOCKING",int,fSset_blocking,
408            }            }
409          else          else
410          {          {
411            int x1 = fSset_blocking(STREAM_INPUT_STREAM(sock),setBlocking);            int x1 = fix(fSset_blocking(STREAM_INPUT_STREAM(sock),setBlocking));
412            int x2 = fSset_blocking(STREAM_OUTPUT_STREAM(sock),setBlocking);            int x2 = fix(fSset_blocking(STREAM_OUTPUT_STREAM(sock),setBlocking));
413            /* if either is negative result return negative. (ie fail)            /* if either is negative result return negative. (ie fail)
414               If either is positive return positive (ie fail)               If either is positive return positive (ie fail)
415               Zero result means both ok.  (ie succeed)               Zero result means both ok.  (ie succeed)
416               */                     */      
417                        
418            return (x1 < 0 || x2 < 0 ? -2 : x1 > 0  ? x1 : x2);            return make_fixnum(x1 < 0 || x2 < 0 ? -2 : x1 > 0  ? x1 : x2);
419          }          }
420        }        }
421                    
422        if (sock->sm.sm_fp == NULL)        if (sock->sm.sm_fp == NULL)
423          return -2;          return make_fixnum(-2);
424        fd = SOCKET_FD(sock);        fd = SOCKET_FD(sock);
425    
426    
# Line 433  DEFUN("SET-BLOCKING",int,fSset_blocking, Line 432  DEFUN("SET-BLOCKING",int,fSset_blocking,
432        setting |= O_NONBLOCK;        setting |= O_NONBLOCK;
433      }      }
434      if (fcntl(fd, F_SETFL, setting) < 0) {      if (fcntl(fd, F_SETFL, setting) < 0) {
435          return errno;          return make_fixnum(errno);
436      }      }
437  #endif  #endif
438    
# Line 441  DEFUN("SET-BLOCKING",int,fSset_blocking, Line 440  DEFUN("SET-BLOCKING",int,fSset_blocking,
440      if (setBlocking != Cnil) {      if (setBlocking != Cnil) {
441          setting = 0;          setting = 0;
442          if (ioctl(fd, (int) FIONBIO, &setting) == -1) {          if (ioctl(fd, (int) FIONBIO, &setting) == -1) {
443              return errno;              return make_fixnum(errno);
444          }          }
445      } else {      } else {
446          setting = 1;          setting = 1;
447          if (ioctl(fd, (int) FIONBIO, &setting) == -1) {          if (ioctl(fd, (int) FIONBIO, &setting) == -1) {
448              return errno;              return make_fixnum(errno);
449          }          }
450      }      }
451  #endif  #endif
452    return 0;    return make_fixnum(0);
453  }  }
454    
455  /* with 2 args return the function if any.  /* with 2 args return the function if any.
# Line 480  joe(int x) { return x; } Line 479  joe(int x) { return x; }
479    get a character from FP but block, if it would return    get a character from FP but block, if it would return
480    the EOF, but the stream is not closed.    the EOF, but the stream is not closed.
481  */    */  
482  getOneChar(fp)  int
483       FILE *fp;  getOneChar(FILE *fp)
484  {  {
485    fd_set readfds;    fd_set readfds;
486    struct timeval timeout;    struct timeval timeout;
487    int fd= fileno(fp);    int fd= fileno(fp);
   int ch;  
488    int high;    int high;
489    /*  fprintf(stderr,"<socket 0x%x>",fp);    /*  fprintf(stderr,"<socket 0x%x>",fp);
490    fflush(stderr); */    fflush(stderr); */
491    fprintf(stderr,"in getOneChar, fd=%d,fp=0x%x",fd,fp);    fprintf(stderr,"in getOneChar, fd=%d,fp=%p",fd,fp);
492    fflush(stderr);    fflush(stderr);
493    if (fd == 0)    if (fd == 0)
494     { joe(fd);     { joe(fd);
# Line 507  getOneChar(fp) Line 505  getOneChar(fp)
505    if (high > 0)    if (high > 0)
506      {      {
507        int ch ;        int ch ;
508        fprintf(stderr,"in getOneChar, fd=%d,fp=0x%x",fd,fp);        fprintf(stderr,"in getOneChar, fd=%d,fp=%p",fd,fp);
509        fflush(stderr);        fflush(stderr);
510        ch = getc(fp);        ch = getc(fp);
511        if ( ch != EOF || feof(fp) ) {        if ( ch != EOF || feof(fp) ) {
# Line 534  getOneChar(fp) Line 532  getOneChar(fp)
532  #define dprintf(s,arg)  #define dprintf(s,arg)
533  #endif      #endif    
534            
535    void
536  ungetCharGclSocket(c,strm)  ungetCharGclSocket(int c, object strm)
537       int c;       /* the character to unget */                    /* the character to unget */
538       object strm; /* stream */                    /* stream */
539  {  object bufp = SOCKET_STREAM_BUFFER(strm);  {  object bufp = SOCKET_STREAM_BUFFER(strm);
540    if (c == EOF) return;    if (c == EOF) return;
541    dprintf("pushing back %c\n",c);    dprintf("pushing back %c\n",c);
# Line 571  ungetCharGclSocket(c,strm) Line 569  ungetCharGclSocket(c,strm)
569   */   */
570    
571  int  int
572  TcpOutputProc(fd, buf, toWrite, errorCodePtr)  TcpOutputProc(int fd, char *buf, int toWrite, int *errorCodePtr)
573       int fd;            /* Socket state. */                          /* Socket state. */
574      char *buf;                          /* The data buffer. */                                          /* The data buffer. */
575      int toWrite;                        /* How many bytes to write? */                                          /* How many bytes to write? */
576      int *errorCodePtr;                  /* Where to store error code. */                                          /* Where to store error code. */
577  {  {
578      int written;      int written;
579    
# Line 588  TcpOutputProc(fd, buf, toWrite, errorCod Line 586  TcpOutputProc(fd, buf, toWrite, errorCod
586      return -1;      return -1;
587  }  }
588    
589  tcpCloseSocket(fd)  void
590    tcpCloseSocket(int fd)
591  {  {
592    close(fd);    close(fd);
593    
594  }  }
595    
596  doReverse(s,n)  void
597       char *s;  doReverse(char *s, int n)
598  { char *p=&s[n-1];  { char *p=&s[n-1];
599    int m = n/2;    int m = n/2;
600    while (--m>=0) {    while (--m>=0) {
# Line 615  doReverse(s,n) Line 614  doReverse(s,n)
614    Side Effects:  The buffer may be filled, and the fill pointer    Side Effects:  The buffer may be filled, and the fill pointer
615    of the buffer may be changed.    of the buffer may be changed.
616   */   */
617  getCharGclSocket(strm,block)  int
618    object strm;  getCharGclSocket(object strm, object block)
   object block;  
619  {  {
620    object bufp = SOCKET_STREAM_BUFFER(strm);    object bufp = SOCKET_STREAM_BUFFER(strm);
621    if (bufp->ust.ust_fillp > 0) {    if (bufp->ust.ust_fillp > 0) {

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26