summaryrefslogtreecommitdiff
path: root/Ivy/BufferedSocket.cxx
diff options
context:
space:
mode:
authorfcolin2007-02-01 12:53:46 +0000
committerfcolin2007-02-01 12:53:46 +0000
commit863c7be9778c1dab03380ba7d72e015448ab9206 (patch)
tree5b0070b6a6fb33452d9caf923f0bd9fbb43dfac3 /Ivy/BufferedSocket.cxx
parent00546c6eb0c54919615c1e28858403fcd232c06a (diff)
downloadivy-cplusplus-863c7be9778c1dab03380ba7d72e015448ab9206.zip
ivy-cplusplus-863c7be9778c1dab03380ba7d72e015448ab9206.tar.gz
ivy-cplusplus-863c7be9778c1dab03380ba7d72e015448ab9206.tar.bz2
ivy-cplusplus-863c7be9778c1dab03380ba7d72e015448ab9206.tar.xz
Utilisateur : Fcolin Date : 1/06/05 Heure : 18:23 Archivé dans $/Bus/Ivy Commentaire: (vss 14)
Diffstat (limited to 'Ivy/BufferedSocket.cxx')
-rw-r--r--Ivy/BufferedSocket.cxx50
1 files changed, 29 insertions, 21 deletions
diff --git a/Ivy/BufferedSocket.cxx b/Ivy/BufferedSocket.cxx
index 864da25..5f6c994 100644
--- a/Ivy/BufferedSocket.cxx
+++ b/Ivy/BufferedSocket.cxx
@@ -5,6 +5,7 @@
#include "IvyStdAfx.h"
#include "BufferedSocket.h"
+#define BUFFER_SIZE 4096
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
@@ -13,7 +14,9 @@
CBufferedSocket::CBufferedSocket()
{
separator = '\n';
- ptr = buf;
+ buffer_size = BUFFER_SIZE;
+ buffer = (char*)malloc( buffer_size );
+ current_ptr = buffer;
connected = false;
InitializeCriticalSection( &m_CritSection );
}
@@ -29,9 +32,8 @@ void CBufferedSocket::Accept(CBufferedSocket& rConnectedSocket, SOCKADDR* lpSock
}
void CBufferedSocket::OnReceive(int nErrorCode)
{
-
-
- char *ptr_nl;
+ char *ptr_sep;
+ char *ptr;
long nb_to_read = 0;
long nb;
@@ -39,14 +41,20 @@ void CBufferedSocket::OnReceive(int nErrorCode)
int len = sizeof( addr );
/* limitation taille buffer */
- nb_to_read = MAX_BUFFER - ( ptr - buf );
+ nb_to_read = buffer_size - (current_ptr - buffer );
if( nb_to_read == 0 )
{
- TRACE("Erreur message trop long sans LF\n");
- ptr = buf;
- return;
+ buffer_size *= 2; /* twice old size */
+ buffer = (char*)realloc( buffer, buffer_size );
+ if (!buffer )
+ {
+ TRACE("HandleSocket Buffer Memory Alloc Error\n");
+ exit(0);
+ }
+ TRACE( "Buffer Limit reached realloc new size %ld\n", buffer_size );
+ nb_to_read = buffer_size - (current_ptr - buffer );
}
- nb = ReceiveFrom( ptr, nb_to_read, &addr, &len, 0/*MSG_PARTIAL*/ );
+ nb = ReceiveFrom( current_ptr, nb_to_read, &addr, &len, 0/*MSG_PARTIAL*/ );
if ( nb == SOCKET_ERROR )
{
int err = this->GetLastError();
@@ -61,26 +69,26 @@ void CBufferedSocket::OnReceive(int nErrorCode)
return;
}
- ptr += nb;
- ASSERT( ptr < (buf +sizeof( buf )));
- *ptr = '\0';
- ptr = buf;
- while( (ptr_nl = strchr( ptr, '\n' )))
+ current_ptr += nb;
+ ASSERT( current_ptr < (buffer + buffer_size) );
+ ptr = buffer;
+ while ((ptr_sep = (char*)memchr (ptr, separator, current_ptr - ptr )))
{
- *ptr_nl = '\0';
+ *ptr_sep = '\0';
//TRACE("message %s\n", ptr );
OnReceive( ptr );
- ptr = ++ptr_nl;
+ ptr = ++ptr_sep;
}
- if ( *ptr != '\0' )
+ if (ptr < current_ptr )
{ /* recopie ligne incomplete au debut du buffer */
- strcpy( buf, ptr );
- ptr = buf + strlen(buf);
+ len = current_ptr - ptr;
+ memcpy (buffer, ptr, len );
+ current_ptr = buffer + len;
}
else
{
- ptr = buf;
- }
+ current_ptr = buffer;
+ }
}