diff mercurial/wireprotoframing.py @ 37725:3ea8323d6f95

wireprotov2: change command response protocol to include a leading map The error handling mechanism for the new wire protocol isn't very well-defined. This commit takes us a step in the right direction by introducing a leading CBOR map for command responses. This map will contain an overall result of the command. Currently, the map indicates whether the command was overall successful or if an error occurred. And if an error occurred, that error is present in the map. There is still a dedicated error frame. My intent is to use that for protocol-level errors and for errors that are encountered after the initial response frame has been sent. This will be clarified in a later commit. Differential Revision: https://phab.mercurial-scm.org/D3385
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 14 Apr 2018 15:19:36 -0700
parents deff7cf7eefd
children 0c184ca594bb
line wrap: on
line diff
--- a/mercurial/wireprotoframing.py	Sat Apr 14 14:37:23 2018 -0700
+++ b/mercurial/wireprotoframing.py	Sat Apr 14 15:19:36 2018 -0700
@@ -354,16 +354,27 @@
 
     Returns a generator of bytearrays.
     """
+    # Automatically send the overall CBOR response map.
+    overall = cbor.dumps({b'status': b'ok'}, canonical=True)
+    if len(overall) > maxframesize:
+        raise error.ProgrammingError('not yet implemented')
 
-    # Simple case of a single frame.
-    if len(data) <= maxframesize:
+    # Simple case where we can fit the full response in a single frame.
+    if len(overall) + len(data) <= maxframesize:
         flags = FLAG_COMMAND_RESPONSE_EOS
         yield stream.makeframe(requestid=requestid,
                                typeid=FRAME_TYPE_COMMAND_RESPONSE,
                                flags=flags,
-                               payload=data)
+                               payload=overall + data)
         return
 
+    # It's easier to send the overall CBOR map in its own frame than to track
+    # offsets.
+    yield stream.makeframe(requestid=requestid,
+                           typeid=FRAME_TYPE_COMMAND_RESPONSE,
+                           flags=FLAG_COMMAND_RESPONSE_CONTINUATION,
+                           payload=overall)
+
     offset = 0
     while True:
         chunk = data[offset:offset + maxframesize]