OLD | NEW |
| (Empty) |
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 print("You should install UltraJSON, it is much faster!") | |
34 import json | |
35 import os | |
36 import struct | |
37 import zlib | |
38 | |
39 from . import constants | |
40 | |
41 def Send(obj, sock): | |
42 """ | |
43 Sends a JSON encodable object over the specified socket (zlib-compressed). | |
44 """ | |
45 obj = json.dumps(obj) | |
46 compression_level = 2 # 1 = fastest, 9 = best compression | |
47 compressed = zlib.compress(obj, compression_level) | |
48 payload = struct.pack('>i', len(compressed)) + compressed | |
49 sock.sendall(payload) | |
50 | |
51 | |
52 class Receiver(object): | |
53 def __init__(self, sock): | |
54 self.sock = sock | |
55 self.data = StringIO.StringIO() | |
56 self.datalength = 0 | |
57 self._next = self._GetNext() | |
58 | |
59 def IsDone(self): | |
60 return self._next == None | |
61 | |
62 def Current(self): | |
63 return self._next | |
64 | |
65 def Advance(self): | |
66 try: | |
67 self._next = self._GetNext() | |
68 except: | |
69 raise | |
70 | |
71 def _GetNext(self): | |
72 try: | |
73 while self.datalength < constants.SIZE_T: | |
74 try: | |
75 chunk = self.sock.recv(8192) | |
76 except: | |
77 raise | |
78 if not chunk: return None | |
79 self._AppendData(chunk) | |
80 size = self._PopData(constants.SIZE_T) | |
81 size = struct.unpack(">i", size)[0] | |
82 while self.datalength < size: | |
83 try: | |
84 chunk = self.sock.recv(8192) | |
85 except: | |
86 raise | |
87 if not chunk: return None | |
88 self._AppendData(chunk) | |
89 result = self._PopData(size) | |
90 result = zlib.decompress(result) | |
91 result = json.loads(result) | |
92 if result == constants.END_OF_STREAM: | |
93 return None | |
94 return result | |
95 except: | |
96 raise | |
97 | |
98 def _AppendData(self, new): | |
99 self.data.seek(0, os.SEEK_END) | |
100 self.data.write(new) | |
101 self.datalength += len(new) | |
102 | |
103 def _PopData(self, length): | |
104 self.data.seek(0) | |
105 chunk = self.data.read(length) | |
106 remaining = self.data.read() | |
107 self.data.close() | |
108 self.data = StringIO.StringIO() | |
109 self.data.write(remaining) | |
110 assert self.datalength - length == len(remaining) | |
111 self.datalength = len(remaining) | |
112 return chunk | |
OLD | NEW |