Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | # Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | # Redistribution and use in source and binary forms, with or without |
| 3 | # modification, are permitted provided that the following conditions are |
| 4 | # met: |
| 5 | # |
| 6 | # * Redistributions of source code must retain the above copyright |
| 7 | # notice, this list of conditions and the following disclaimer. |
| 8 | # * Redistributions in binary form must reproduce the above |
| 9 | # copyright notice, this list of conditions and the following |
| 10 | # disclaimer in the documentation and/or other materials provided |
| 11 | # with the distribution. |
| 12 | # * Neither the name of Google Inc. nor the names of its |
| 13 | # contributors may be used to endorse or promote products derived |
| 14 | # from this software without specific prior written permission. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
| 29 | import cStringIO as StringIO |
| 30 | try: |
| 31 | import ujson as json |
| 32 | except ImportError: |
| 33 | import json |
| 34 | import os |
| 35 | import struct |
| 36 | import zlib |
| 37 | |
| 38 | from . import constants |
| 39 | |
| 40 | def Send(obj, sock): |
| 41 | """ |
| 42 | Sends a JSON encodable object over the specified socket (zlib-compressed). |
| 43 | """ |
| 44 | obj = json.dumps(obj) |
| 45 | compression_level = 2 # 1 = fastest, 9 = best compression |
| 46 | compressed = zlib.compress(obj, compression_level) |
| 47 | payload = struct.pack('>i', len(compressed)) + compressed |
| 48 | sock.sendall(payload) |
| 49 | |
| 50 | |
| 51 | class Receiver(object): |
| 52 | def __init__(self, sock): |
| 53 | self.sock = sock |
| 54 | self.data = StringIO.StringIO() |
| 55 | self.datalength = 0 |
| 56 | self._next = self._GetNext() |
| 57 | |
| 58 | def IsDone(self): |
| 59 | return self._next == None |
| 60 | |
| 61 | def Current(self): |
| 62 | return self._next |
| 63 | |
| 64 | def Advance(self): |
| 65 | try: |
| 66 | self._next = self._GetNext() |
| 67 | except: |
| 68 | raise |
| 69 | |
| 70 | def _GetNext(self): |
| 71 | try: |
| 72 | while self.datalength < constants.SIZE_T: |
| 73 | try: |
| 74 | chunk = self.sock.recv(8192) |
| 75 | except: |
| 76 | raise |
| 77 | if not chunk: return None |
| 78 | self._AppendData(chunk) |
| 79 | size = self._PopData(constants.SIZE_T) |
| 80 | size = struct.unpack(">i", size)[0] |
| 81 | while self.datalength < size: |
| 82 | try: |
| 83 | chunk = self.sock.recv(8192) |
| 84 | except: |
| 85 | raise |
| 86 | if not chunk: return None |
| 87 | self._AppendData(chunk) |
| 88 | result = self._PopData(size) |
| 89 | result = zlib.decompress(result) |
| 90 | result = json.loads(result) |
| 91 | if result == constants.END_OF_STREAM: |
| 92 | return None |
| 93 | return result |
| 94 | except: |
| 95 | raise |
| 96 | |
| 97 | def _AppendData(self, new): |
| 98 | self.data.seek(0, os.SEEK_END) |
| 99 | self.data.write(new) |
| 100 | self.datalength += len(new) |
| 101 | |
| 102 | def _PopData(self, length): |
| 103 | self.data.seek(0) |
| 104 | chunk = self.data.read(length) |
| 105 | remaining = self.data.read() |
| 106 | self.data.close() |
| 107 | self.data = StringIO.StringIO() |
| 108 | self.data.write(remaining) |
| 109 | assert self.datalength - length == len(remaining) |
| 110 | self.datalength = len(remaining) |
| 111 | return chunk |