home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / other / irc / common / packet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-14  |  3.6 KB  |  123 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, common/packet.c
  3.  *   Copyright (C) 1990  Jarkko Oikarinen and
  4.  *                       University of Oulu, Computing Center
  5.  *
  6.  *   This program is free software; you can redistribute it and/or modify
  7.  *   it under the terms of the GNU General Public License as published by
  8.  *   the Free Software Foundation; either version 1, or (at your option)
  9.  *   any later version.
  10.  *
  11.  *   This program is distributed in the hope that it will be useful,
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *   GNU General Public License for more details.
  15.  *
  16.  *   You should have received a copy of the GNU General Public License
  17.  *   along with this program; if not, write to the Free Software
  18.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #ifndef lint
  22. static  char sccsid[] = "@(#)packet.c    2.12 1/30/94 (C) 1988 University of Oulu, \
  23. Computing Center and Jarkko Oikarinen";
  24. #endif
  25.  
  26. #include "struct.h"
  27. #include "common.h"
  28. #include "sys.h"
  29. #include "msg.h"
  30. #include "h.h"
  31.  
  32. /*
  33. ** dopacket
  34. **    cptr - pointer to client structure for which the buffer data
  35. **           applies.
  36. **    buffer - pointr to the buffer containing the newly read data
  37. **    length - number of valid bytes of data in the buffer
  38. **
  39. ** Note:
  40. **    It is implicitly assumed that dopacket is called only
  41. **    with cptr of "local" variation, which contains all the
  42. **    necessary fields (buffer etc..)
  43. */
  44. int    dopacket(cptr, buffer, length)
  45. Reg3    aClient *cptr;
  46. char    *buffer;
  47. Reg4    int    length;
  48. {
  49.     Reg1    char    *ch1;
  50.     Reg2    char    *ch2;
  51.     aClient    *acpt = cptr->acpt;
  52.  
  53.     me.receiveB += length; /* Update bytes received */
  54.     cptr->receiveB += length;
  55.     if (cptr->receiveB > 1023)
  56.         {
  57.         cptr->receiveK += (cptr->receiveB >> 10);
  58.         cptr->receiveB &= 0x03ff;    /* 2^10 = 1024, 3ff = 1023 */
  59.         }
  60.     if (acpt != &me)
  61.         {
  62.         acpt->receiveB += length;
  63.         if (acpt->receiveB > 1023)
  64.             {
  65.             acpt->receiveK += (acpt->receiveB >> 10);
  66.             acpt->receiveB &= 0x03ff;
  67.             }
  68.         }
  69.     else if (me.receiveB > 1023)
  70.         {
  71.         me.receiveK += (me.receiveB >> 10);
  72.         me.receiveB &= 0x03ff;
  73.         }
  74.     ch1 = cptr->buffer + cptr->count;
  75.     ch2 = buffer;
  76.     while (--length >= 0)
  77.         {
  78.         *ch1 = *ch2++;
  79.         /*
  80.          * Yuck.  Stuck.  To make sure we stay backward compatible,
  81.          * we must assume that either CR or LF terminates the message
  82.          * and not CR-LF.  By allowing CR or LF (alone) into the body
  83.          * of messages, backward compatibility is lost and major
  84.          * problems will arise. - Avalon
  85.          */
  86.         if (*ch1 == '\n' || *ch1 == '\r')
  87.             {
  88.             if (ch1 == cptr->buffer)
  89.                 continue; /* Skip extra LF/CR's */
  90.             *ch1 = '\0';
  91.             me.receiveM += 1; /* Update messages received */
  92.             cptr->receiveM += 1;
  93.             if (cptr->acpt != &me)
  94.                 cptr->acpt->receiveM += 1;
  95.             cptr->count = 0; /* ...just in case parse returns with
  96.                      ** FLUSH_BUFFER without removing the
  97.                      ** structure pointed by cptr... --msa
  98.                      */
  99.             if (parse(cptr, cptr->buffer, ch1, msgtab) ==
  100.                 FLUSH_BUFFER)
  101.                 /*
  102.                 ** FLUSH_BUFFER means actually that cptr
  103.                 ** structure *does* not exist anymore!!! --msa
  104.                 */
  105.                 return FLUSH_BUFFER;
  106. #ifndef CLIENT_COMPILE
  107.             /*
  108.             ** Socket is dead so exit (which always returns with
  109.             ** FLUSH_BUFFER here).  - avalon
  110.             */
  111.             if (cptr->flags & FLAGS_DEADSOCKET)
  112.                 return exit_client(cptr, cptr, &me,
  113.                            "Dead Socket");
  114. #endif
  115.             ch1 = cptr->buffer;
  116.             }
  117.         else if (ch1 < cptr->buffer + (sizeof(cptr->buffer)-1))
  118.             ch1++; /* There is always room for the null */
  119.         }
  120.     cptr->count = ch1 - cptr->buffer;
  121.     return 0;
  122. }
  123.