@@ -45,56 +45,69 @@ def handle(self):
4545 print ("=== socket closed ===" )
4646 break
4747 print ("received: {}" .format (data ))
48- try :
49- decoded = json .loads (data )
50- except ValueError :
51- print ("json decoding failed" )
52- decoded = [- 1 , '' ]
53-
54- # Send a response if the sequence number is positive.
55- if decoded [0 ] >= 0 :
56- if decoded [1 ] == 'hello!' :
57- # simply send back a string
58- response = "got it"
59- elif decoded [1 ] == 'make change' :
60- # Send two ex commands at the same time, before replying to
61- # the request.
62- cmd = '["ex","call append(\\ "$\\ ",\\ "added1\\ ")"]'
63- cmd += '["ex","call append(\\ "$\\ ",\\ "added2\\ ")"]'
64- print ("sending: {}" .format (cmd ))
65- thesocket .sendall (cmd .encode ('utf-8' ))
66- response = "ok"
67- elif decoded [1 ] == 'eval-works' :
68- # Send an eval request. We ignore the response.
69- cmd = '["eval","\\ "foo\\ " . 123", -1]'
70- print ("sending: {}" .format (cmd ))
71- thesocket .sendall (cmd .encode ('utf-8' ))
72- response = "ok"
73- elif decoded [1 ] == 'eval-fails' :
74- # Send an eval request that will fail.
75- cmd = '["eval","xxx", -2]'
76- print ("sending: {}" .format (cmd ))
77- thesocket .sendall (cmd .encode ('utf-8' ))
78- response = "ok"
79- elif decoded [1 ] == 'eval-result' :
80- # Send back the last received eval result.
81- response = last_eval
82- elif decoded [1 ] == '!quit!' :
83- # we're done
84- sys .exit (0 )
85- elif decoded [1 ] == '!crash!' :
86- # Crash!
87- 42 / 0
88- else :
89- response = "what?"
9048
91- encoded = json .dumps ([decoded [0 ], response ])
92- print ("sending: {}" .format (encoded ))
93- thesocket .sendall (encoded .encode ('utf-8' ))
94-
95- # Negative numbers are used for "eval" responses.
96- elif decoded [0 ] < 0 :
97- last_eval = decoded
49+ # We may receive two messages at once. Take the part up to the
50+ # matching "]" (recognized by finding "][").
51+ while data != '' :
52+ splitidx = data .find ('][' )
53+ if splitidx < 0 :
54+ todo = data
55+ data = ''
56+ else :
57+ todo = data [:splitidx + 1 ]
58+ data = data [splitidx + 1 :]
59+ print ("using: {}" .format (todo ))
60+
61+ try :
62+ decoded = json .loads (todo )
63+ except ValueError :
64+ print ("json decoding failed" )
65+ decoded = [- 1 , '' ]
66+
67+ # Send a response if the sequence number is positive.
68+ if decoded [0 ] >= 0 :
69+ if decoded [1 ] == 'hello!' :
70+ # simply send back a string
71+ response = "got it"
72+ elif decoded [1 ] == 'make change' :
73+ # Send two ex commands at the same time, before replying to
74+ # the request.
75+ cmd = '["ex","call append(\\ "$\\ ",\\ "added1\\ ")"]'
76+ cmd += '["ex","call append(\\ "$\\ ",\\ "added2\\ ")"]'
77+ print ("sending: {}" .format (cmd ))
78+ thesocket .sendall (cmd .encode ('utf-8' ))
79+ response = "ok"
80+ elif decoded [1 ] == 'eval-works' :
81+ # Send an eval request. We ignore the response.
82+ cmd = '["eval","\\ "foo\\ " . 123", -1]'
83+ print ("sending: {}" .format (cmd ))
84+ thesocket .sendall (cmd .encode ('utf-8' ))
85+ response = "ok"
86+ elif decoded [1 ] == 'eval-fails' :
87+ # Send an eval request that will fail.
88+ cmd = '["eval","xxx", -2]'
89+ print ("sending: {}" .format (cmd ))
90+ thesocket .sendall (cmd .encode ('utf-8' ))
91+ response = "ok"
92+ elif decoded [1 ] == 'eval-result' :
93+ # Send back the last received eval result.
94+ response = last_eval
95+ elif decoded [1 ] == '!quit!' :
96+ # we're done
97+ sys .exit (0 )
98+ elif decoded [1 ] == '!crash!' :
99+ # Crash!
100+ 42 / 0
101+ else :
102+ response = "what?"
103+
104+ encoded = json .dumps ([decoded [0 ], response ])
105+ print ("sending: {}" .format (encoded ))
106+ thesocket .sendall (encoded .encode ('utf-8' ))
107+
108+ # Negative numbers are used for "eval" responses.
109+ elif decoded [0 ] < 0 :
110+ last_eval = decoded
98111
99112 thesocket = None
100113
0 commit comments