views:

45

answers:

1

i need a Buffer class for socket send/recv : 1.memmove/memcpy as less as possible 2.object creation as less as possible 3.the class user need no worry for the extend issue is there anything liks that in the standard library ? or how can i make one ?

i wrote this but i can't use it for socket, because the buffer(from the standard library) is read-only:

import array

class Buffer :
    def __init__( self, len ) :
        self.__array= array.array( 'c', '\0'*len )
        self.__databegin= 0
        self.__datalen= 0

    def getFreespace( self, minlen ) :
        rightSpace= self.__getRightSpace()
        if rightSpace < minlen :
            totalSpace= self.__databegin + rightSpace
            if totalSpace < minlen :
                dlen= minlen - totalSpace
                self.__array.extend( '\0'*dlen )
            self.__array[ 0:self.__datalen ]= self.__array[ self.__databegin:self.__datalen+self.__databegin ]
        return buffer( self.__array, self.__databegin + self.__datalen, self.__getRightSpace() )

    def pushDataLen( self, len ) :
        self.__datalen += len

    def getDataSpace( self ) :
        return buffer( self.__array, self.__databegin, self.__datalen )

    def popDataLen( self, len ) :
        len= min( len, self.__datalen )
        self.__databegin += len
        self.__datalen -= len
        if self.__datalen == 0 :
            self.__databegin= 0

    def __getRightSpace( self ) :
        return len( self.__array ) - self.__databegin - self.__datalen
A: 
import array
class Buffer :
    def __init__( self, len, recvlen ) :
        self.__array= array.array( 'c', '\0'*len )
        self.__recvArray= array.array( 'c', '\0'*recvlen )
        self.__databegin= 0
        self.__datalen= 0
    def getFreeSpace( self, minlen =None ) :
        if minlen != None :
            lennow= len( self.__recvArray )
            if lennow < minlen :
                self.__recvArray.extend( '\0'*(minlen-lennow) )
            assert( len( self.__recvArray ) >= minlen )
        return self.__recvArray
    def pushDataLen( self, datalen ) :
        datalen= min( datalen, len( self.__recvArray ) )
        self.__appendData( self.__recvArray, datalen )
        self.__datalen += datalen
    def pushDataArray( self, data ) :
        datalen= len( data )
        self.__appendData( data, datalen )
        self.__datalen += datalen
    def getDataSpace( self ) :
        return buffer( self.__array, self.__databegin, self.__datalen )
    def popDataLen( self, len ) :
        len= min( len, self.__datalen )
        self.__databegin += len
        self.__datalen -= len
        if self.__datalen == 0 :
            self.__databegin= 0
    def __getRightSpace( self ) :
        return len( self.__array ) - self.__databegin - self.__datalen
    def __appendData( self, data, datalen ) :
        rightSpace= self.__getRightSpace()
        if rightSpace < datalen :
            totalSpace= self.__databegin + rightSpace
            if totalSpace < datalen :
                dlen= datalen - totalSpace
                self.__array.extend( '\0'*dlen )
            self.__array[ 0:self.__datalen ]= self.__array[ self.__databegin:self.__databegin+self.__datalen ]
        assert( self.__getRightSpace() >= datalen )
        rightBegin= self.__databegin + self.__datalen
        self.__array[ rightBegin : rightBegin + datalen ]= data[ :datalen ]
sjinny