This repository has been archived by the owner on May 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsendhoudini.py
executable file
·156 lines (132 loc) · 3.5 KB
/
sendhoudini.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
#! /usr/bin/env python
# Copyright (C) 2015 Arthur Yidi
# License: BSD Simplified
"""
Pipe In - Chop
http://www.sidefx.com/docs/current/nodes/chop/pipein
"""
import sys
import struct
import socket
import time
from threading import Thread
from math import sin, cos
from array import array
from random import random
ESC = chr(170) #0xAA
NUL = chr(0)
COMMAND = {
'value': 1,
'upload': 2,
'names': 3,
'disconnect': 4,
'refresh': 5,
'script': 6
}
threads = []
running = True
def send(c, binary):
for b in binary:
if b == ESC:
c.send(b)
c.send(b)
def sendValue(c, data):
sendReset(c)
send(c, struct.pack('!qq', COMMAND['value'], len(data)))
for d in data:
send(c, struct.pack('!d', d))
def sendUpload(c, length, rate, start, channels, data):
sendReset(c)
send(c, struct.pack('!qqddq',
COMMAND['upload'],
length,
rate,
start,
channels))
for d in data:
send(c, struct.pack('!d', d))
def sendNames(c, names):
sendReset(c)
send(c, struct.pack('!qq', COMMAND['names'], len(names)))
for name in names:
(name, chunks) = padString(name)
send(c, struct.pack('!q', chunks))
send(c, name)
def disconnect(c, channels, data):
sendReset(c)
send(c, struct.pack('!q', COMMAND['disconnect']))
def sendRefresh(c, delay):
sendReset(c)
send(c, struct.pack('!qq', COMMAND['refresh'], delay))
def sendScript(c, script):
sendReset(c)
(script, chunks) = padString(script)
send(c, struct.pack('!qq', COMMAND['script'], chunks))
send(c, script)
def sendReset(c):
c.send(struct.pack('8s', (ESC + NUL) * 4))
def padString(s):
chars = len(s)
chunks = chars / 8
padding = chars % 8
if padding:
chunks += 1
s += NUL * (8 - padding)
return (s, chunks)
def connection(client, worker):
while running:
try:
worker(client)
except socket.error:
client.close()
return
client.shutdown(socket.SHUT_RDWR)
time.sleep(0.15)
client.close()
def example(client):
sendNames(client, ['tx', 'ty'])
t = 0
while running:
sendValue(client, [sin(t), cos(t)])
t += 0.03
time.sleep(0.03)
class HoudiniConnection(Thread):
def __init__(self, port, worker):
Thread.__init__(self)
self.socket = socket.socket()
self.port = port
self.worker = worker
def run(self):
global running
s = self.socket
host = 'localhost'
port = self.port
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print('Starting server %s:%d' % (host, port))
try:
s.bind((host, port))
s.listen(1)
except socket.error as e:
print('Unable to bind socket:', e)
s.close()
return
while running:
try:
client, addr = s.accept()
except socket.error:
break
print('Connected', addr)
t = Thread(target=connection, args=[client, self.worker])
t.start()
threads.append(t)
running = False
for t in threads:
t.join()
s.close()
def close(self):
self.socket.close()
if __name__ == '__main__':
pipe = HoudiniConnection(5000, example)
pipe.start()
raw_input('Press Any Key to Stop.\n')
pipe.close()