Source

crossroads / crossroads / message.py

Diff from to

crossroads/message.py

 # -*- coding: utf-8 -*-
 '''Crossroads.IO messages.'''
 
-from ctypes import sizeof, string_at, c_ubyte, byref, c_size_t, c_char
+from ctypes import sizeof, string_at, c_ubyte, byref, c_size_t, c_char, c_int
 
 from stuf.six import PY3, tobytes, tounicode, strings
 
+from . import lowest as xs
 from .constants import MORE
-from . import lowest as xs
-from stuf.deep import getcls
-from test.test_multiprocessing import c_int
 
+SLOTS = 'msg rc more size last_rc'.split()
 
-class BaseMessage(object):
+class Msg(object):
 
     def __init__(self):
-        self.more = self.rc = self.dest = self.last_rc = None
+        self.more = self.rc = self.msg = self.last_rc = None
 
     def __bytes__(self):
-        if self.dest is None:
+        if self.msg is None:
             return b''
-        return tobytes(string_at(byref(self.dest), self.size))
+        return tobytes(string_at(byref(self.msg), self.size))
 
     def __unicode__(self):
         return tounicode(self)
 
+    def __len__(self):
+        return self.size
+
     __str__ = __unicode__ if PY3 else __bytes__
 
     @property
         return bytearray(self.__bytes__())
 
 
-class Message(BaseMessage):
+class RecvMsg(Msg):
 
-    __slots__ = 'src dest rc more size last_rc'.split()
+    __slots__ = SLOTS
 
-    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
+    def __init__(self, size, char=False):
+        super(RecvMsg, self).__init__()
+        self.msg = xs.array(c_char if char else c_ubyte, size)
+        self.size = size
+
+
+class SendMsg(Msg):
+
+    __slots__ = SLOTS
+
+    def __init__(self, size, source):
+        super(SendMsg, self).__init__()
+        if isinstance(source, (Msg, strings)):
+            self.msg = source = bytes(source)
         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
+            self.msg = source
+        self.size = len(source) if size is None else size
 
-    def __len__(self):
-        return self.size
 
+class MsgXS(Msg):
 
-class MessageXS(BaseMessage):
+    def close(self):
+        self.rc = xs.msg_close(self.msg)
 
-    __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)
+class RecvMsg32(MsgXS):
 
-    def __len__(self):
-        return self.size
+    __slots__ = SLOTS
+
+    def __init__(self):
+        super(RecvMsg32, self).__init__()
+        self.msg = msg = xs.msg_t()
+        self.rc = xs.msg_init(msg)
 
     @property
     def moremsg(self):
         more = c_int()
         more_size = c_size_t(sizeof(more))
-        self.rc = xs.getmsgopt(self.dest, MORE, more, more_size)
+        self.rc = xs.getmsgopt(self.msg, 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)
+
+class SendMsg32(MsgXS):
+
+    __slots__ = SLOTS
+
+    def __init__(self, source):
+        super(SendMsg32, self).__init__()
+        if isinstance(source, Msg):
+            self.msg = source.msg
+        else:
+            self.msg = xs.msg_t()
+            source = bytes(source)
+            length = len(source)
+            self.rc = xs.msg_init_size(self.msg, length)
+            xs.memmove(xs.msg_data(self.msg), source, length)