-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmylib.py
More file actions
83 lines (69 loc) · 1.77 KB
/
mylib.py
File metadata and controls
83 lines (69 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import sys
import Command_py.Command_pb2
import random
import struct
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
def UnpackData(msgsize, msgNo, msg):
try:
if msgNo == 157:
return[""]
print(" Rev : msgsize:{} msgNo:{} msgNoName:{}".format(msgsize, msgNo, NoToCmd(msgNo)))
protocol = NoToCmd(msgNo)
if protocol == "ClientCmdNone":
return [""]
else:
if CheckCallBackList(protocol):
return CallBack(protocol)(msg)
else:
print("No Protocol Register CallBack!")
return [""]
except:
return [""]
def PackData(messages,proname):
#print("send Name:{}",proname)
messages_len = len(messages)
No = CmdToNo(proname)
messagespack = struct.pack('<HH', messages_len, No)
messagespack = messagespack + messages
return messagespack
def CmdToNo(cmd):
cmd_c = cmd + "_C"
try:
return Command_py.Command_pb2.CmdNumber.Value(cmd_c)
except ValueError:
cmd_s = cmd + "_S"
try:
return Command_py.Command_pb2.CmdNumber.Value(cmd_s)
except ValueError:
cmd_cs = cmd + "_CS"
try:
return Command_py.Command_pb2.CmdNumber.Value(cmd_cs)
except ValueError:
print(" CmdError:",cmd)
def NoToCmd(no):
try:
return Command_py.Command_pb2.CmdNumber.Name(no)
except:
print(" NoError:",no)
def Sleep(t):
time.sleep(t)
def Ranstr(num):
H = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
salt = ''
for i in range(num):
salt += random.choice(H)
return salt
RevCallBackList = {}
#回调注册
def RegisterCallBack(func_type,func):
RevCallBackList[func_type] = func
def CallBack(func_type):
return RevCallBackList[func_type]
def CheckCallBackList(func_type):
return RevCallBackList.has_key(func_type)