🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #4432

Re: Remy Could you have a look

#4432
From: Remy Lebeau (TeamB)
Date: Wed, 02 Feb 2005 22:19
121 lines
4432 bytes
"mike" <mfbrowne@earthlink.nnet> wrote in message
news:4200e6a4$1@newsgroups.borland.com...

> Could you have a look at this code sample and see if in
> your opinion I am approaching the problem correctly.

In my opinion, you are not.

>  while ( true )

Here is the major caveat - for a non-blocking socket, when the OnRead event
is triggered, you need to read all available data from the socket and then
wait for the next OnRead event to be triggered before you can read again.
That means that you should maintain a secondary buffer for all incoming
data, and then parse your packets from that buffer when they are actually
available.

>    NbrBytesRead = Socket->ReceiveBuf( &Size, sizeof(int) ) ;

You are not making sure that sizeof(int) number of bytes are actually
available before reading them.  If ReceiveBuf() returns a value <
sizeof(int) then your Size value will not be complete.  Using the secondary
buffer approach can fix that.

>        memset( buffer, 0x00, sizeof( buffer ) ) ;

The buffer variable is a pointer, so sizeof(buffer) will always return
sizeof(char*), which is currently 4.  It will not return the allocated size
like you are expecting.


With all of that said, try something like the following instead:


    #define OFFSET_MEM(mem, pos) \
        &((LPBYTE)(mem))[(pos)]

    class TMyMemoryStream : public TMemoryStream
    {
    public:
        __fastcall TMyMemoryStream() : TMemoryStream() {}

        void* __fastcall Expand(int Count)
        {
            int OldSize = Size;
            Size = (OldSize + Count);
            return OFFSET_MEM(Memory, OldSize);
        }

        char* __fastcall ReadPacket()
        {
            int iSize = Size;
            if( (iSize-Position) >= sizeof(int) )
            {
                int PacketSize = 0;
                Read(&PacketSize, sizeof(int));
                if( (iSize-Position) >= PacketSize )
                {
                    char *packet = new char[Size];
                    Read(packet, Size);
                    return packet;
                }
                Seek(-sizeof(int), soFromCurrent);
            }
            return NULL;
        }
    };


    TMyMemoryStream *IncomingBuffer;

    __fastcall TStdPopWin::TStdPopWin(TComponent *Owner)
        : TForm(Owner)
    {
        IncomingBuffer = new TMyMemoryStream;
    }

    __fastcall TStdPopWin::~TStdPopWin()
    {
        delete IncomingBuffer;
    }

    void __fastcall TStdPopWin::ClientSocket1Conect(TObject *Sender,
TCustomWinSocket *Socket)
    {
        IncomingBuffer->Clear();
    }

    void __fastcall TStdPopWin::ClientSocket1Disconect(TObject *Sender,
TCustomWinSocket *Socket)
    {
        IncomingBuffer->Clear();
    }

    void __fastcall TStdPopWin::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
    {
        int BytesToReceive = Socket->ReceiveLength();
        if( BytesToReceive <= 0 )
            return;

        void *NewMemory = IncomingBuffer->Expand(BytesToReceive);

        int BytesRead = Socket->ReceiveBuf(NewMemory, BytesToReceive);
        if( BytesRead <=0 )
            return;

        while( true )
        {
            char *packet = IncomingBuffer->ReadPacket();
            if( packet == NULL )
                break;

            // do Something with packet...
            delete[] packet;
        }
    }


Gambit
Technical Details
Message-ID: <4201512e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.tele.dk!news.tele.dk!small.news.tele.dk!ltu.se!luth.se!38.119.100.82.MISMATCH!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4200e6a4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,8d9554d7f46a1c91
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4200e6a4$1@newsgroups.borland.com>
Subject: Re: Remy Could you have a look
Date: Wed, 2 Feb 2005 14:19:23 -0800
Lines: 123
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4201512e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1107382574 68.69.17.41 (2 Feb 2005 14:16:14 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.tele.dk!news.tele.dk!small.news.tele.dk!ltu.se!luth.se!38.119.100.82.MISMATCH!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail