home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / tcpsrv12.exe / TCPSRV12.TAR / sockio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-28  |  2.0 KB  |  111 lines

  1. /* sockio.c */
  2. /*
  3.  
  4.  * Copyright 1994 A.Oliver De Guzman
  5.  * All rights reserved
  6.  
  7.  *   Permission is granted to any individual to copy, use, and/or
  8.  * distribute this software provided that the distribution retains this
  9.  * entire copyright notice. No part of this software may be used and/or
  10.  * sold for profit or used with any commercial product.
  11.  
  12.  * DISCLAIMER:
  13.  *   This software comes with NO WARRANTIES of any kind. In no event
  14.  * will the author be liable for any financial, physical, moral, and/or
  15.  * mental damages incurred directly or indirectly by the use or intent
  16.  * to use of this software.
  17.  
  18.  */
  19.  
  20. #ifndef __TURBOC__
  21. #include <sys/time.h>
  22. #endif
  23. #include <sys/types.h>
  24. #include "dfcss.h"
  25. #include "sock.h"
  26.  
  27. int ASockWrite(sd, buff)
  28. #ifdef __TURBOC__
  29. tcp_Socket *sd;
  30. #else
  31. int sd;
  32. #endif
  33. char *buff;
  34. {
  35.     unsigned char slen[3];
  36.     short len;
  37.     int n;
  38.  
  39.     len = MIN(strlen(buff), 0xFFFF);
  40.  
  41.     slen[0] = (char) len & 0x00FF;
  42.     slen[1] = (char) (len >> 8) & 0x00FF;
  43.  
  44.     if (SockWrite(sd, slen, 2) < 0) return(-1);
  45.     if ((n=SockWrite(sd, buff, strlen(buff))) < 0) return(-1);
  46.     return(n);
  47. }
  48.  
  49. int ASockRead(sd, buff)
  50. #ifdef __TURBOC__
  51. tcp_Socket *sd;
  52. #else
  53. int sd;
  54. #endif
  55. char *buff;
  56. {
  57.     unsigned char slen[3];
  58.     short len;
  59.     int nread;
  60.  
  61.     SockRead(sd, slen, 2);
  62.  
  63.     len = ((((int) slen[1])<<8) & 0xFF00) | ((int) slen[0] & 0x00FF);
  64.     if ((nread = SockRead(sd, buff, len)) < 0) return(-1);
  65.     buff[nread] = '\0';
  66.     return(nread);
  67. }
  68.  
  69.  
  70. #ifdef __TURBOC__
  71. int NSockRead(sd, buff)
  72. tcp_Socket *sd;
  73. char *buff;
  74. {
  75.     int nread;
  76.     if (sock_dataready(sd)) {
  77.         nread = ASockRead(sd, buff);
  78.         if (nread != -1) buff[nread] = '\0';
  79.         return(nread);
  80.     }
  81.     return(0);
  82. }
  83. #else
  84.  
  85. int NSockRead(sd, buff)
  86. int sd;
  87. char *buff;
  88. {
  89.     struct timeval tv;
  90.     fd_set rfds, trfds;
  91.     int maxfd;
  92.     int nread;
  93.  
  94.     FD_ZERO(&rfds);
  95.     FD_SET(sd, &rfds);
  96.     maxfd = sd +1;
  97.     tv.tv_sec = 0;
  98.     tv.tv_usec = 100;
  99.     trfds = rfds;
  100.     if (select(maxfd, &trfds, NULL, NULL, &tv) < 0)
  101.         perror("select:");
  102.  
  103.     if (FD_ISSET(sd, &trfds)){
  104.         nread = ASockRead(sd, buff);
  105.         if (nread != -1) buff[nread] = '\0';
  106.         return(nread);
  107.     }
  108.     return(0);
  109. }
  110. #endif
  111.