forked from libigl/libigl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcpviewer.py
More file actions
80 lines (64 loc) · 2.08 KB
/
Copy pathtcpviewer.py
File metadata and controls
80 lines (64 loc) · 2.08 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
import socket
import threading
import pyigl as igl
import array
import time
HOST = 'localhost' # Symbolic name meaning all available interfaces
PORT = 50008 # Arbitrary non-privileged port
def worker(viewer,lock,s):
print("TCP iglviewer server listening on port " + str(PORT))
try:
while True:
conn, addr = s.accept()
lock.acquire()
slist = []
while True:
buf = conn.recv(10000000)
if not buf:
break
slist.append(buf.decode('unicode_internal','ignore'))
conn.close()
data = ''.join(slist)
temp = list(data)
isempty = viewer.data.V.rows() == 0
viewer.data.deserialize(temp)
if isempty and viewer.data.V.rows() != 0:
viewer.core.align_camera_center(viewer.data.V,viewer.data.F)
lock.release()
except:
s.close()
return
class TCPViewer(igl.viewer.Viewer):
def launch(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
a = array.array('u',self.data.serialize())
s.sendall(a)
s.close()
except:
print("Failed to open socket, is tcpviewer running?")
if __name__ == "__main__": # The main script is a server
## Try to open the socket first
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((HOST, PORT))
except:
print("Failed to bind, port already used.")
exit(1)
s.listen(1)
viewer = igl.viewer.Viewer()
lock = threading.Lock()
t = threading.Thread(target=worker, args=(viewer,lock,s,))
t.setDaemon(True)
t.start()
viewer.core.is_animating = True
# viewer.data.dirty = int(0x03FF)
viewer.launch_init(True,False)
done = False
while not done:
lock.acquire()
done = not viewer.launch_rendering(False)
lock.release()
time.sleep(0.000001) # DO NOT REMOVE ME
viewer.launch_shut()