Commits

Ning Sun committed fcf46bc

add message I/O functions

Comments (0)

Files changed (1)

     def __init__(self):
         self.header_size = 24
         self.header_codec = ">ihihil"
+        self.body_size = 0
+        self.body = ""
 
     def set_trans_id(self, trans_id):
         self.trans_id = trans_id
 
     def set_request_id(self, request_id):
         self.request_id = request_id
+        
+    def write(self, fobj):
+        message_size = self.header_size + self.body_size
+        header = struct.pack(self.header_codec, message_size, self.version,
+                             self.type, self.content_type, self.request_id, self.trans_id)
+        fobj.write(header+body)
+        
+    def read(self, fobj):
+        message_size_bytes = fobj.read(4)
+        message_size = struct.unpack(">i", message_size_bytes)
+        message_header_and_body = fobj.read(message_size - 4)
+        message_header, message_body = message_header_and_body[:20], message_header_and_body[20:]
+        self.version, self.type, self.content_type, self.request_id, self.trans_id = struct.unpack(">hihil")
+        self.body = message_body
 
 VENUS_MESSAGES = {}
 def message_type(type_code):
 
 @message_type(0x02000001)
 class VenusRequest(VenusMessage):
-    pass
+    def set_body(self, body):
+        self.body = body
+        self.body_size = len(body)
 
 @message_type(0x02000002)
 class VenusResponse(VenusMessage):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.