简单的重构
diff --git a/dubbo/codec/decoder.py b/dubbo/codec/decoder.py
index 4b2c3d6..085b0c5 100644
--- a/dubbo/codec/decoder.py
+++ b/dubbo/codec/decoder.py
@@ -416,9 +416,9 @@
return str(self.__data)
-def get_body_length(response_head):
+def parse_response_head(response_head):
"""
- 计算出响应体的长度
+ 对响应头部的字节做解析
:param response_head:
:return:
"""
diff --git a/dubbo/codec/encoder.py b/dubbo/codec/encoder.py
index 7942a44..49aee4d 100644
--- a/dubbo/codec/encoder.py
+++ b/dubbo/codec/encoder.py
@@ -167,6 +167,11 @@
@staticmethod
def _encode_bool(value):
+ """
+ 对bool类型进行编码
+ :param value:
+ :return:
+ """
result = []
if value:
result.append(ord('T'))
@@ -176,8 +181,14 @@
@staticmethod
def _encode_int(value):
+ """
+ 对整数进行编码
+ :param value:
+ :return:
+ """
result = []
# 超出int类型范围的值则转化为long类型
+ # 这里问题在于对于落在int范围内的数字,我们无法判断其是long类型还是int类型,所以一律认为其是int类型
if value > MAX_INT_32 or value < MIN_INT_32:
result.append(ord('L'))
result.extend(list(bytearray(struct.pack('!q', value))))
@@ -202,6 +213,11 @@
@staticmethod
def _encode_float(value):
+ """
+ 对浮点类型进行编码
+ :param value:
+ :return:
+ """
result = []
int_value = int(value)
if int_value == value:
@@ -265,6 +281,11 @@
return result
def _encode_str(self, value):
+ """
+ 对一个字符串进行编码
+ :param value:
+ :return:
+ """
result = []
# 在进行网络传输操作时一律使用unicode进行操作
if isinstance(value, str):
@@ -284,6 +305,11 @@
return result
def _encode_object(self, value):
+ """
+ 对一个对象进行编码
+ :param value:
+ :return:
+ """
result = []
path = value.get_path()
field_names = value.keys()
@@ -310,6 +336,11 @@
return result
def _encode_list(self, value):
+ """
+ 对一个列表进行编码
+ :param value:
+ :return:
+ """
result = []
length = len(value)
if length == 0:
@@ -373,6 +404,7 @@
# 列表(list)类型,不可以使用tuple替代
elif isinstance(value, list):
return self._encode_list(value)
+ # null
elif value is None:
return [ord('N')]
else:
diff --git a/dubbo/connection/connections.py b/dubbo/connection/connections.py
index 8fa38e3..9f6ca83 100644
--- a/dubbo/connection/connections.py
+++ b/dubbo/connection/connections.py
@@ -7,7 +7,7 @@
from struct import unpack, pack
from dubbo.codec.encoder import Request
-from dubbo.codec.decoder import Response, get_body_length
+from dubbo.codec.decoder import Response, parse_response_head
from dubbo.common.constants import CLI_HEARTBEAT_RES_HEAD, CLI_HEARTBEAT_TAIL, CLI_HEARTBEAT_REQ_HEAD, \
TIMEOUT_CHECK_INTERVAL, TIMEOUT_IDLE, TIMEOUT_MAX_TIMES
from dubbo.common.exceptions import DubboResponseException, DubboRequestTimeoutException
@@ -116,7 +116,7 @@
return
try:
- heartbeat, body_length = get_body_length(head)
+ heartbeat, body_length = parse_response_head(head)
except DubboResponseException as e: # 这里是dubbo的内部异常,与response中的业务异常不一样
logger.exception(e)
body_length = unpack('!i', head[12:])[0]
@@ -298,7 +298,6 @@
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
self.__sock = sock
- self.__lock = threading.Lock()
# Event是Condition的简单实现版本
self.__event = threading.Event()