1 from threading import RLock
\r
5 class Channel(object):
\r
6 """a channel transfers packages over a stream. a package is any blob of data,
\r
7 up to 4GB in size. channels are gauranteed to be thread-safe"""
\r
8 HEADER_FORMAT = ">L" # byte order must be the same at both sides!
\r
9 HEADER_SIZE = struct.calcsize(HEADER_FORMAT)
\r
11 def __init__(self, stream):
\r
13 self.stream = stream
\r
16 return "<%s(%r)>" % (self.__class__.__name__, self.stream)
\r
18 def send(self, data):
\r
19 """sends a package"""
\r
22 header = struct.pack(self.HEADER_FORMAT, len(data))
\r
23 self.stream.write(header + data)
\r
28 """receives a package (blocking)"""
\r
31 length, = struct.unpack(self.HEADER_FORMAT, self.stream.read(self.HEADER_SIZE))
\r
32 return self.stream.read(length)
\r
37 return self.stream.close()
\r
40 return self.stream.fileno()
\r
42 def is_available(self):
\r
43 return self.stream.is_available()
\r
46 return self.stream.wait()
\r