def toString(self): stream = io.BytesIO() serialize.writeVariant(stream, self.type) serialize.writeVariant(stream, self.session) serialize.writeString(stream, self.name) content = stream.getvalue() stream.close() return content
def toString(self): stream = io.BytesIO() serialize.writeVariant(stream, self.type) serialize.writeVariant(stream, self.session) serialize.writeString(stream, self.client_key) serialize.writeString(stream, self.digest) serialize.writeVariant(stream, self.sender) serialize.writeString(stream, self.name) serialize.writeString(stream, self.ip) serialize.writeVariant(stream, self.port) content = stream.getvalue() stream.close() return content
def toString(self): stream = io.BytesIO() ##write basic attrib ##msg id serialize.writeVariant(stream, self.id) ##type&success ##bin: 6bit type | 1bit success if self.success: stream.write(chr((self.type<<1)|1)) else: stream.write(chr((self.type<<1)|0)) ##sender&receiver serialize.writeString(stream, self.sender) serialize.writeString(stream, self.receiver) serialize.writeVariant(stream, self.session) serialize.writeVariant(stream, self.sequence) serialize.writeVariant(stream, self.transaction) serialize.writeVariant(stream, self.timestamp) self.writeParams(stream) content = stream.getvalue() stream.close() return content
def toString(self): stream = io.BytesIO() serialize.writeVariant(stream, self.type) serialize.writeVariant(stream, self.session) if self.success: success = 1 else: success = 0 if self.need_digest: bool_value = success << 1|1 else: bool_value = success << 1|0 serialize.writeVariant(stream, bool_value) serialize.writeString(stream, self.client_key) serialize.writeString(stream, self.server_key) serialize.writeVariant(stream, self.sender) serialize.writeString(stream, self.name) serialize.writeString(stream, self.ip) serialize.writeVariant(stream, self.port) content = stream.getvalue() stream.close() return content
def writeParams(self, stream): for value_type in self.params.keys(): for key in self.params[value_type]: value = self.params[value_type][key] ## 2bytes: 11 bits key, 5 bits type key_value = key << 5|value_type stream.write(chr(key_value >> 8)) stream.write(chr(key_value&0xFF)) if self.param_type_uint == value_type: ##write uint params if not (isinstance(value, int) or isinstance(value, long)): raise Exception("write param:invalid uint value for key %d, msg %d"%(key, self.id)) serialize.writeVariant(stream, value) elif self.param_type_bool == value_type: if not isinstance(value, int): raise Exception("write param:invalid bool value for key %d, msg %d"%(key, self.id)) ##write bool params if value: serialize.writeVariant(stream, 1) else: serialize.writeVariant(stream, 0) elif self.param_type_int == value_type: if not (isinstance(value, int) or isinstance(value, long)): raise Exception("write param:invalid int value for key %d, msg %d"%(key, self.id)) ##write int params serialize.writeVariant(stream, serialize.zigzagEncode(value)) elif self.param_type_string == value_type: if not isinstance(value, str): raise Exception("write param:invalid string value for key %d, msg %d"%(key, self.id)) ##write string params serialize.writeString(stream, value) elif self.param_type_float == value_type: if not (isinstance(value, float) or isinstance(value, int) or isinstance(value, long)): raise Exception("write param:invalid float value for key %d, msg %d"%(key, self.id)) ##write float serialize.writeFloat(stream, value) elif self.param_type_uint_array == value_type: ##write uint array if not isinstance(value, list): raise Exception("write param:invalid uint array for key %d, msg %d"%(key, self.id)) ##write count count = len(value) serialize.writeVariant(stream, count) for uint_value in value: if not (isinstance(uint_value, int) or isinstance(uint_value, long)): raise Exception("write param:invalid uint item for key %d, msg %d"%(key, self.id)) serialize.writeVariant(stream, uint_value) elif self.param_type_string_array == value_type: ##write string array if not isinstance(value, list): raise Exception("write param:invalid string array for key %d, msg %d"%(key, self.id)) ##write count count = len(value) serialize.writeVariant(stream, count) for string_value in value: if not isinstance(string_value, str): raise Exception("write param:invalid string item for key %d, msg %d, params:%s"%(key, self.id, self.params)) serialize.writeString(stream, string_value) elif self.param_type_float_array == value_type: ##write float array if not isinstance(value, list): raise Exception("write param:invalid float array for key %d, msg %d"%(key, self.id)) ##write count count = len(value) serialize.writeVariant(stream, count) for float_value in value: if not (isinstance(float_value, float) or isinstance(float_value, int) or isinstance(float_value, long)): raise Exception("write param:invalid float item for key %d, msg %d"%(key, self.id)) serialize.writeFloat(stream, float_value) elif self.param_type_uint_array_array == value_type: ##write uint array array if not isinstance(value, list): raise Exception("write param:invalid uint array array for key %d, msg %d"%(key, self.id)) ##write count array_count = len(value) serialize.writeVariant(stream, array_count) for sub_array in value: if not isinstance(sub_array, list): raise Exception("write param:invalid uint sub array for key %d, msg %d"%(key, self.id)) sub_count = len(sub_array) serialize.writeVariant(stream, sub_count) for uint_value in sub_array: if not (isinstance(uint_value, int) or isinstance(uint_value, long)): raise Exception("write param:invalid uint sub item for key %d, msg %d"%(key, self.id)) serialize.writeVariant(stream, uint_value) elif self.param_type_string_array_array == value_type: ##write string array array if not isinstance(value, list): raise Exception("write param:invalid string array array for key %d, msg %d"%(key, self.id)) ##write count array_count = len(value) serialize.writeVariant(stream, array_count) for sub_array in value: if not isinstance(sub_array, list): raise Exception("write param:invalid string sub array for key %d, msg %d"%(key, self.id)) sub_count = len(sub_array) serialize.writeVariant(stream, sub_count) for string_value in sub_array: if not isinstance(string_value, str): raise Exception("write param:invalid string sub item for key %d, msg %d, params:%s"%( key, self.id, self.params)) serialize.writeString(stream, string_value) elif self.param_type_float_array_array == value_type: ##write float array array if not isinstance(value, list): raise Exception("write param:invalid float array array for key %d, msg %d"%(key, self.id)) ##write count array_count = len(value) serialize.writeVariant(stream, array_count) for sub_array in value: if not isinstance(sub_array, list): raise Exception("write param:invalid float sub array for key %d, msg %d"%(key, self.id)) sub_count = len(sub_array) serialize.writeVariant(stream, sub_count) for float_value in sub_array: if not (isinstance(float_value, float) or isinstance(float_value, int) or isinstance(float_value, long)): raise Exception("write param:invalid float sub item for key %d, msg %d"%(key, self.id)) serialize.writeFloat(stream, float_value)