-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinterop.py
160 lines (133 loc) · 4.04 KB
/
interop.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import socket, traceback, threading, queue
from .log import log
class Interop:
def __init__(self, on_recv, on_connect):
self.readWorker = None
self.readWorkerStopEvent = None
self.readBuffer = bytes()
self.socket = None
self.on_connect = on_connect
self.on_recv = on_recv
self.sendWorker = None
self.sendQueue = queue.Queue()
self.sendDataEvent = None
self.sendWorkerStopEvent = None
def isConnected(self):
isConnected = self.socket != None
return isConnected
def connect(self, quiet=False):
try:
host = "localhost"
port = 12122
if not quiet:
log().info("Connecting to %s:%d" % (host, port))
tmpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tmpSocket.connect((host, port))
except OSError:
if not quiet:
log().info("Failed to connect to %s:%d" % (host, port))
tmpSocket.close()
return False
self.socket = tmpSocket
self.startPullMessages()
self.startSendMessages()
self.on_connect()
log().info("Connected to Fuse")
return True
def send(self, type, msg):
if not self.isConnected():
connect()
if not self.isConnected():
return
self.sendQueue.put((type, msg), True)
self.sendDataEvent.set()
def startSendMessages(self):
self.sendDataEvent = threading.Event()
self.sendWorkerStopEvent = threading.Event()
self.sendWorker = threading.Thread(target = self.sendMessages)
self.sendWorker.daemon = True
self.sendWorker.start()
def sendMessages(self):
try:
while not self.sendWorkerStopEvent.is_set():
while not self.sendQueue.empty():
msgTupple = self.sendQueue.get_nowait()
type = msgTupple[0]
msg = msgTupple[1]
msgInBytes = bytes(type + "\n" + str(len(msg)) + "\n" + msg, "UTF-8")
self.socket.sendall(msgInBytes)
self.sendDataEvent.wait()
self.sendDataEvent.clear()
except:
self.disconnect()
def stopSendMessages(self):
self.sendWorkerStopEvent.set()
self.sendDataEvent.set()
def startPullMessages(self):
self.readWorkerStopEvent = threading.Event()
self.readWorker = threading.Thread(target = self.pullMessages)
self.readWorker.daemon = True
self.readWorker.start()
def stopPullMessages(self):
self.readWorkerStopEvent.set()
def pullMessages(self):
try:
while not self.readWorkerStopEvent.is_set():
tmpData = self.socket.recv(4096)
if len(tmpData) == 0:
log().info("Lost connection")
self.disconnect()
return
self.readBuffer = self.readBuffer + tmpData
self.parseReadData()
except:
self.disconnect()
return
def parseReadData(self):
strData = self.readBuffer.decode("utf-8")
firstNewLine = strData.find("\n")
secondNewLine = strData.find("\n", firstNewLine+1)
if firstNewLine <= 0 or secondNewLine <= 0:
return
typeStr = strData[:firstNewLine]
lengthStr = strData[firstNewLine+1:secondNewLine]
length = self.parseLength(lengthStr)
if length == -1:
self.readBuffer = b"";
return
sizeOfTypeStr = len(bytes(typeStr, "utf-8")) + 1
sizeOfLengthStr = len(bytes(lengthStr, "utf-8")) + 1
if len(self.readBuffer) - sizeOfLengthStr - sizeOfTypeStr < length:
return
tmpStr = self.readBuffer[sizeOfLengthStr + sizeOfTypeStr:length + sizeOfLengthStr + sizeOfTypeStr]
message = (typeStr, tmpStr.decode("utf-8"))
self.readBuffer = self.readBuffer[sizeOfLengthStr + length + sizeOfTypeStr:]
self.on_recv(message)
self.parseReadData()
def parseLength(self, lenStr):
try:
return int(lenStr)
except ValueError:
log().info("Couldn't parse packet length, got " + lenStr)
return -1
def disconnect(self):
log().info("Disconnecting")
if self.readWorkerStopEvent != None:
self.stopPullMessages()
if self.sendWorkerStopEvent != None:
self.stopSendMessages()
try:
if self.socket != None:
self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()
except:
pass
finally:
self.socket = None
self.readWorkerStopEvent = None
self.readWorker = None
self.readBuffer = bytes()
self.sendWorkerStopEvent = None
self.sendWorker = None
self.sendDataEvent = None
log().info("Disconnected")