Source

crossroads / crossroads / message.py

Full commit
# -*- coding: utf-8 -*-
'''Crossroads.IO messages.'''

from ctypes import sizeof, string_at, c_ubyte

from stuf.six import PY3, tobytes, tounicode

from . import lowest as xs


class BaseMsg(object):

    def __init__(self, size, source=None):
        self.src = source
        self.size = size
        self.more = self.rc = self.dest = None

    def __bytes__(self, *args, **kwargs):
        if self.dest is not None:
            return tobytes(string_at(self.dest, len(self)), 'latin-1')
        return b''

    def __unicode__(self, *args, **kwargs):
        return tounicode(tobytes(self, 'utf-8'))

    __str__ = __unicode__ if PY3 else __bytes__


class Message(BaseMsg):

    __slots__ = 'src dest rc more size'.split()

    def __init__(self, size, source=None):
        super(Message, self).__init__(size, source)
        if source is None:
            self.dest = xs.array(c_ubyte, size)

    def __len__(self):
        if self.size is not None:
            return self.size
        if self.src is not None:
            return len(self.src)
        return sizeof(self.dest)


class MessageXS(BaseMsg):

    __slots__ = 'src dest last_rc more ref'.split()

    def __init__(self, data=None):
        super(MessageXS, self).__init__(32, data)
        self.dest = self.ref = dest = xs.msg_t()
        if data is None:
            self.rc = xs.msg_init(dest)
        else:
            self.rc = xs.msg_init_size(dest, 32)
            xs.msg_init_size(dest, 32)
            xs.memmove(xs.msg_data(dest), data, 32)

    def __len__(self):
        return sizeof(self.dest)

    def close(self):
        self.rc = xs.msg_close(self.dest)


#class BaseMultipart(object):
#
#    def __init__(self, size, source=None):
#        self.src = source
#        self.more = self.rc = self.dest = None
#        self.parts = []
#        self.__add__ = self.parts.append
#
#    def __bytes__(self, *args, **kwargs):
#        if self.dest is not None:
#            return b''.join(tobytes(
#                string_at(byref(i), sizeof(i)), 'latin-1') for i in self.parts
#            )
#        return b''
#
#    def __unicode__(self, *args, **kwargs):
#        return tounicode(tobytes(self, 'utf-8'))
#
#    __str__ = __unicode__ if PY3 else __bytes__
#
#    @property
#    def dest(self):
#        return self.parts[-1]
#class Multipart(BaseMultipart):
#
#    def __init__(self, size, source=None):
#        super(Multipart, self).__init__(size, source)
#
#    def __len__(self):
#
#        return len(self.src) if self.dest is None else sizeof(self.dest)
#
#    @property
#    def ref(self):
#        return None if self.dest is None else byref(self.dest)
#
#
#class XSMultipart(BaseMultipart):
#
#    def __init__(self, size, source=None):
#        super(XSMultipart, self).__init__(size, source)
#
#    def __len__(self):
#        return sizeof(self.dest)
#
#    def close(self):
#        self.last_rc = sum(xs.msg_close(i) for i in self.parts)