-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
35 lines (33 loc) · 983 Bytes
/
util.py
File metadata and controls
35 lines (33 loc) · 983 Bytes
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
import socket
import time
def receiveData(conn) -> bytearray:
data = bytearray()
while len(data) < 4:
try:
packet = conn.recv(4 - len(data))
except BlockingIOError:
time.sleep(0.01)
continue
if not packet:
raise Exception("Socket closed")
data.extend(packet)
recv_data_size = int.from_bytes(data, byteorder='little')
# print(f"Receiving {recv_data_size} bytes of audio...")
data = bytearray()
while len(data) < recv_data_size:
try:
packet = conn.recv(recv_data_size - len(data))
except BlockingIOError:
time.sleep(0.01)
continue
if not packet:
raise Exception("Socket closed")
data.extend(packet)
# print("... received.")
return data
def sendData(conn, data):
send_data_size = len(data)
# print("Sending {} bytes of audio...".format(send_data_size))
data_len = send_data_size.to_bytes(4, "little")
conn.sendall(data_len)
conn.sendall(data)