🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #55810

Re: Forecasting the posting's size

#55810
From: Curt Welch
Date: Mon, 31 Dec 2001 02:59
159 lines
7348 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote:
> "Curt Welch" <curt@kcwc.com> wrote in message
> news:20011230161737.549$XI@newsreader.com...
> > Nobody said writing a server would be easy. :)
>
> Dear Curt,I never say it's easy,I just have to do. I have it started.
> With your help,the work gets on favorably. I know I still need your help.
> I don't care any word,I just thanks for any reply.
>
> Here comes the piece of code to handle the posting,anything I have lost?
> //-------------------------------------------------------
> int CBasicClient::receive_article(char *strBuf,int len)
> {
>  char *pInChar;
>  int recved_len;
>  unsigned flag=0;
>  char strStopper[]="\x0d\x0a.\x0d\x0a";
>
>  do
>  {
>      for(pInChar=strBuf;pInChar<strBuf+len-1;pInChar++)
>   {
>    // Read input stream until last char of strStopper matches
>    recved_len=RecvRaw(pInChar,1,0);

Reading a byte at a time from a stream is normally too slow for good
performance.  You will need to move the data in larger chunks to
make it fast (but fast of course is relative idea).  byte-at-a-time might
be fast enough for you needs.

>       if(0==recved_len)
>    {
>         pInChar-- ;
>          break ;
>    }
>      if( *pInChar == strStopper[5-1] && pInChar - strBuf + 1 >=5)

>       {
>     // Assume match until one of the stopper chars mismatches
>     int bMatch = TRUE ;
>     for( int pos = 5 - 1; pos > 0 && bMatch; pos-- )

C is 0 based, not 1 based.  Your code would be a lot clearer if
you learned to think 0 based instead of writting it as 1 based
and subtracting 1 everywhere.

>      if( *( pInChar - 5 + pos ) != strStopper[ pos - 1 ] )
>       bMatch = FALSE ;
>     if( bMatch )
>      break ;
>    }
>   }
>   if (pInChar<strBuf+len-1) //posting is not too large reachs limit,
> return normal code
>    break;
>
>   memmove(strBuf,pInChar-5-1,5-1); //posting is over large,reset Buffer,

Bug 1.  pInChar-5-1 is not the same as pInChar-(5-1).  This will prevent
your code from seeing the ending CR LF . CR LF if it showed up right at the
end of your buffer.

> read the other stream,untill CRLF.CRLF,but return over-large sign back
>   pInChar=strBuf+5-1;

Bug 2.  pInChar gets reset when the for loop starts over so setting it
here is pointless and once again, your code would not correctly spot
the CR LF . CR LF if it happened right at the overflow point.

>   flag++;

Sloppy but unimportant bug:  If the input data overflows enough times, your
flag will wrap back to zero.

Just rename "flag" to "overflow" and set it to TRUE.

>  }while(true);
>   if (0<flag)
>    return -1;    //overflow

coding style issue: you null terminate the buffer below but you
exit above without null terminating it.  That, along with other sloppy
coding styles could lead to a core-dump when the code that calls this
function does something stupid like a strlen() of the buffer even
when the return value is -1.  If you null terminate the buffer normally,
then never exit without the buffer null terminated.

>   *(pInChar+1-5)='\0';    //cut off the ending CRLF.CRLF

Semi-possible-bug: don't use null terminated buffers.  POST data will
  contain
nulls.

Or, you could use a null terminated buffer if you check for nulls and
reject the article if it contains a null.

>   return pInChar-strBuf+1-5;
> }

Bug3: your code won't correctly see ". CR LF" as a null article.

Issue: You do know about dot escaping leading dots don't you?  It might
be ok to ignore them like you have done here, assuming you did that because
you knew what you were doing and not because you didn't know about it...

>
> ///////////////////////////////////////////////////
>
> 1.strBuf is the Buffer memory for cacheing the inputing posting. I have
> allocate a max size of memory equals the posting upper limit such as
> 512*1024,using the following code:

512K is a very small limit.  1MB or 4MB are standard.  And for post data
you should actually set the limit to a 1K or so below that to allow for
header expansion.

>     char *strBuf=new char[max_posting_size];
>
> 2.I If the inputing stream reachs the limit,pInChar will approach the end
> of the strBuf. After exits from for{ ... ...}segment,I know if the
> posting is too big. If this happens, I (1) keep the last 4 received
> bytes(maybe they happen are "CRLF.CR"),(2)reset the point to the
> beginning of the strBuf(regarded the kept 4 bytes),(3)go back to read the
> other data in the input stream and save them in the same strBuf,once by
> once,till "CRLF.CRLF" is got. If the posting is too large,the input data
> will be trash,upon that I can use the same buffer again and again. Once
> it overflow,I dare overlay.
>
> At the end of the function,I return a value for telling what's happened.

You wrote all that in C, you didn't need to repeat it in english.  :) The
approach is valid, but allocating a max-size buffer for every post is
a potential performace hog.  Allocating a smaller size to start with and
expanding only when needed is a more typical approach (but once again
makes the code more complex).

> This works looks correct.

It's not.  People with your level of coding skill don't normally try to
write stuff as complex as a news server.  You tend to write code that
when tested lightly seems to work but when put to use in the real world
crashes on a regular basis.  Typical PC programmer stuff.  But I could
tell that from you first questions posted here even before you showed
us your code.  Good luck with getting a stable server running.

> But I find it's obviously slower than either
> INN or Dnews. I don't know how they act here. Any advice about it will be
> very appreciated.

It's most likely the read-a-byte-at-a-time style of code that's making
it slow.  You need to allocate a larger buffer, and then read as many
bytes as possible with one read (I don't know C++ so maybe it's also
the RecvRaw() function in general that's too slow also).  Then you scan the
bytes you just read to check for a match with the CR LF DOT CR LF.

-- 
Curt Welch                                            http://CurtWelch.Com/
curt@kcwc.com                          Webmaster for http://NewsReader.Com/
Technical Details
Message-ID: <20011230215952.153$nt@newsreader.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!netnews.com!xfer02.netnews.com!feed1.newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
References: <a0mi6f$qqr$1@mail.cn99.com> <20011230161737.549$XI@newsreader.com> <a0o95h$or9$1@mail.cn99.com>

Show Full Headers
Complete Article Headers
Message-ID: <20011230215952.153$nt@newsreader.com>
Subject: Re: Forecasting the posting's size
Date: Mon, 31 Dec 2001 02:59:52 +0000
From: curt@kcwc.com (Curt Welch)
References: <a0mi6f$qqr$1@mail.cn99.com> <20011230161737.549$XI@newsreader.com> <a0o95h$or9$1@mail.cn99.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!netnews.com!xfer02.netnews.com!feed1.newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
Organization: NewsReader.Com
Newsgroups: news.software.nntp
X-Trace: NewsReader.Com ie8@L.W1vMb9gM9Zok6s1pmHkqApyi54X+Cm.TXVNennv.v
X-Face: "p}G*1KH{+F7EYGKLb>ogDguabZ+%,?^epeFB!nzu`)`$=QcvL1KF6<0GH!Tbc!Sqo[|tV5
  %IW48mQf3K=Ci&gZ7]]aazx@]Y-nq!r5{yH/#,?@lDdUDvOfByB2hVW0.@OM%{l/{cT'{w
X-Url: http://CurtWelch.Com/
User-Agent: NewsReader.Com
Lines: 158