Source

crossroads / crossroads / message.py

Full commit
Lynn Rees c33c151 


Lynn Rees 7b504f0 
Lynn Rees 90ac222 
Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 5b38341 

Lynn Rees c33c151 

Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 5b38341 

Lynn Rees c33c151 
Lynn Rees 5b38341 



Lynn Rees c33c151 
Lynn Rees 5b38341 

Lynn Rees c33c151 


Lynn Rees 7b504f0 



Lynn Rees c33c151 
Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 7b504f0 
Lynn Rees 5b38341 

Lynn Rees c33c151 
Lynn Rees 7b504f0 
Lynn Rees 5b38341 





Lynn Rees c33c151 

Lynn Rees 5b38341 
Lynn Rees c33c151 

Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 5b38341 
Lynn Rees c33c151 
Lynn Rees 5b38341 

Lynn Rees 4355211 





Lynn Rees 5b38341 
Lynn Rees 4355211 

Lynn Rees c33c151 
Lynn Rees 4355211 




Lynn Rees c33c151 

Lynn Rees 5b38341 









Lynn Rees c33c151 

Lynn Rees 4355211 


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

from ctypes import sizeof, string_at, c_ubyte, byref, c_size_t, c_char

from stuf.six import PY3, tobytes, tounicode, strings

from .constants import MORE
from . import lowest as xs
from stuf.deep import getcls
from test.test_multiprocessing import c_int


class BaseMessage(object):

    def __init__(self):
        self.more = self.rc = self.dest = self.last_rc = None

    def __bytes__(self):
        if self.dest is None:
            return b''
        return tobytes(string_at(byref(self.dest), self.size))

    def __unicode__(self):
        return tounicode(self)

    __str__ = __unicode__ if PY3 else __bytes__

    @property
    def array(self):
        return bytearray(self.__bytes__())


class Message(BaseMessage):

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

    def __init__(self, size, source=None, char=False):
        super(Message, self).__init__()
        self.src = source
        if source is None:
            self.dest = xs.array(c_char if char else c_ubyte, size)
            self.size = size
        else:
            if isinstance(source, (getcls(self), strings)):
                self.src = source = bytes(source)
            self.size = len(source) if size is None else size
            self.dest = None

    def __len__(self):
        return self.size


class MessageXS(BaseMessage):

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

    def __init__(self, source=None):
        super(MessageXS, self).__init__()
        if source is None:
            self.dest = xs.msg_t()
            self.src = None
            self.rc = xs.msg_init(self.dest)
            self.size = xs.msg_size(self.dest)
        elif isinstance(source, getcls(self)):
            self.src = source.dest
            self.dest = None
            self.size = xs.msg_size(self.src)
        else:
            self.src = xs.msg_t()
            source = bytes(source)
            length = self.size = len(source)
            self.rc = xs.msg_init_size(self.src, length)
            xs.memmove(xs.msg_data(self.src), source, length)

    def __len__(self):
        return self.size

    @property
    def moremsg(self):
        more = c_int()
        more_size = c_size_t(sizeof(more))
        self.rc = xs.getmsgopt(self.dest, MORE, more, more_size)
        value = more.value
        del more_size, more
        return value

    def close(self):
        if self.src is not None:
            self.rc = xs.msg_close(self.src)
        if self.dest is not None:
            self.rc = xs.msg_close(self.dest)