Article View: borland.public.cppbuilder.internet.socket
Article #18Re: Missed UDP packets
From: Colin B Maharaj
Date: Mon, 28 Jul 2008 14:57
Date: Mon, 28 Jul 2008 14:57
225 lines
6609 bytes
6609 bytes
How to use...(actual code is below)...
#include "CircBuff.h"
TCircBuff *Buff1 = new TCircBuff; // Declaration....
//---------------------------------------------------------------------------
// In Server Code - UDP Reader
//---------------------------------------------------------------------------
void __fastcall TDataModule1::UDPServer1UDPRead(TObject *Sender,
TStream *AData, TIdSocketHandle *ABinding)
{
if (Buff1) // does our object exists?
{
Buff1->tmpl = AData->Size;
AData->ReadBuffer(Buff1->tmps, Buff1->tmpl);
Buff1->AddTmp();
}
}
//---------------------------------------------------------------------------
// In Consumer Code, e.g. A Form with a Timer and a Memo
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
unsigned char s[4096]; // you may want to declare this in a heap...
int len;
Timer1->Enabled = false;
if (Buff1->Count>0)
{
Memo1->Lines->Add("----------------------");
Buff1->GetElement(s, len);
AnsiString B = s;
Memo1->Lines->Add(B); // place in a memo or something...
}
Timer1->Enabled = true;
}
==================================CIRCBUFF.H================================
//--------------------------------------------------------------------------
#ifndef CircBuffH
#define CircBuffH
// You may need to change these accordingly
#define TOTALPACKETS 512
#define PACKETSIZE 4096
struct packetstruct
{
unsigned char *s;
int l;
};
class TCircBuff
{
private:
volatile int front, rear, count;
packetstruct * buffer;
CRITICAL_SECTION GlobalCriticalSection;
void memcpy2(unsigned char *d, unsigned char *s, int l);
public:
__fastcall TCircBuff();
__fastcall ~TCircBuff();
char *tmps;
int tmpl;
void GetElement(unsigned char *ss, int & ll);
void SetElement(unsigned char *ss, int ll);
int FGetCount(void) { return count; };
void AddTmp();
__property int Count = { read=FGetCount };
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#endif
=============================end-of-CIRCBUFF.H===============================
================================CIRCBUFF.CPP=================================
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "CircBuff.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TCircBuff::TCircBuff()
{
buffer = new packetstruct [TOTALPACKETS];
tmps = new unsigned char [PACKETSIZE];
for (int i=0; i<TOTALPACKETS; i++)
{
buffer[i].s = new unsigned char [PACKETSIZE];
buffer[i].l = 0;
}
front=0;
rear=0;
count=0;
InitializeCriticalSection(&GlobalCriticalSection);
}
//---------------------------------------------------------------------------
__fastcall TCircBuff::~TCircBuff()
{
for (int i=0; i<TOTALPACKETS; i++)
{
delete []buffer[i].s;
}
delete []buffer;
delete []tmps;
DeleteCriticalSection(&GlobalCriticalSection);
}
//---------------------------------------------------------------------------
void TCircBuff::GetElement(unsigned char *ss, int & ll)
{
EnterCriticalSection(&GlobalCriticalSection);
if (count>0)
{
memcpy2(tmps, buffer[front].s, PACKETSIZE);
tmpl = buffer[front].l;
count--;
front++;
if (front == TOTALPACKETS) front=0;
}
else
{
ZeroMemory(tmps, PACKETSIZE);
tmpl=0;
}
memcpy2(ss, tmps, PACKETSIZE);
ll = tmpl;
LeaveCriticalSection(&GlobalCriticalSection);
}
//---------------------------------------------------------------------------
void TCircBuff::SetElement(unsigned char *ss, int ll)
{
EnterCriticalSection(&GlobalCriticalSection);
memcpy2(buffer[rear].s, ss,PACKETSIZE);
buffer[rear].l = ll;
if (count < TOTALPACKETS)
{
count++;
rear++;
if (rear == TOTALPACKETS)
{
rear = 0;
}
}
LeaveCriticalSection(&GlobalCriticalSection);
}
//---------------------------------------------------------------------------
void TCircBuff::AddTmp()
{
SetElement(tmps, tmpl);
}
//---------------------------------------------------------------------------
void TCircBuff::memcpy2(unsigned char *d, unsigned char *s, int l)
{
if (!d) return;
if (!s) return;
while (l--) *d++ = *s++;
}
//---------------------------------------------------------------------------
==============================end-of-CIRCBUFF.CPP============================
Dave K. wrote:
> Colin, thanks for the suggestion. If you could post your code I would
> greatly appreciate it.
>
> Thanks
> Dave
>
>
> Colin B Maharaj wrote:
>> Dave, what I do is to create a circular buffer
>> then take the entire UDP packet and place it in this
>> circular buffer.
>> Will post code on demand....
>>
>>
>>
>> Dave K. wrote:
>>> Hello,
>>> I am using the Indy UDP server component in an application, and am
>>> experiencing quite a few missed packets
>>> Dave
Technical Details
Message-ID:
<488dde3f@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<488b52f1$1@newsgroups.borland.com> <488bc2d2$1@newsgroups.borland.com> <488d3b78@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,fe1f171b8b3e63a7 X-Google-Attributes: gid11508e,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 28 Jul 2008 10:57:02 -0400 From: Colin B Maharaj <noreply@myhost.com> User-Agent: Thunderbird 2.0.0.16 (Windows/20080708) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: Missed UDP packets References: <488b52f1$1@newsgroups.borland.com> <488bc2d2$1@newsgroups.borland.com> <488d3b78@newsgroups.borland.com> In-Reply-To: <488d3b78@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 080728-0, 07/28/2008), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 190.58.103.195 Message-ID: <488dde3f@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1217257023 190.58.103.195 (28 Jul 2008 07:57:03 -0700) Lines: 225 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail