From 1a3eb206c07e834e513a18453689efab7b9dd19a Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 12:53:46 +0000 Subject: Utilisateur : Fcolin Date : 1/06/05 Heure : 18:23 Archivé dans $/Bus/Ivy Commentaire: (vss 14) --- Bus/Ivy/BufferedSocket.cxx | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/Bus/Ivy/BufferedSocket.cxx b/Bus/Ivy/BufferedSocket.cxx index 864da25..5f6c994 100644 --- a/Bus/Ivy/BufferedSocket.cxx +++ b/Bus/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; + } } -- cgit v1.1