95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
global select
|
|
import select
|
|
global time
|
|
import time
|
|
|
|
global clientLoopIn
|
|
def clientLoopIn(self):
|
|
cmd = getResponse(self.connection,1024).decode("utf-8")
|
|
if cmd == False: return
|
|
cmd = commandToList(cmd)
|
|
args = {}
|
|
q = False
|
|
for arg in cmd[1:]:
|
|
argSplit = arg.split("=",1)
|
|
args[argSplit[0]] = argSplit[1]
|
|
|
|
if not "channel" in args: args["channel"] = "default"
|
|
if not "channel-password" in args: args["channel-password"] = ""
|
|
if not "delay" in args: args["delay"] = 0.1
|
|
if not "user-password" in args: args["user-password"] = ""
|
|
args["delay"] = float(args["delay"])
|
|
if args["delay"] < minDelay: args["delay"] = minDelay
|
|
if args["delay"] > maxDelay: args["delay"] = maxDelay
|
|
|
|
if not authenticate(args["user"],args["user-password"]): return
|
|
|
|
with clientDataLock:
|
|
setClientData(self.cID,"type",cmd[0])
|
|
setClientData(self.cID,"args",args)
|
|
|
|
if cmd[0] == "broadcast":
|
|
setClientData(self.cID,"buffer",{})
|
|
setClientData(self.cID,"bufferPacket",-1)
|
|
|
|
if cmd[0] == "watch":
|
|
q = queue.Queue()
|
|
setClientData(self.cID,"queue",q)
|
|
|
|
if cmd[0] == "broadcast":
|
|
buffer = getClientData(self.cID,"buffer")
|
|
packet = -1
|
|
packetMin = 0
|
|
bufferSize = 0
|
|
lastReceived = time.process_time()
|
|
while True:
|
|
args["delay"] = 0
|
|
data = self.connection.recv(connBuffer)
|
|
if data == b"": return
|
|
with clientDataLock:
|
|
dataSize = len(data)
|
|
if dataSize > maxBuffer: return
|
|
bufferSize += dataSize + bufferCost
|
|
while bufferSize > maxBuffer:
|
|
bufferSize -= len(buffer[str(packetMin)]) - bufferCost
|
|
del buffer[str(packetMin)]
|
|
packetMin += 1
|
|
packet += 1
|
|
buffer[str(packet)] = data
|
|
setClientData(self.cID,"bufferPacket",packet)
|
|
with clientsLock:
|
|
for cID in clients:
|
|
if getClientData(cID,"type") == "watch" and getClientData(cID,"args")["user"] == args["user"] and getClientData(cID,"args")["channel"] == args["channel"] and getClientData(cID,"args")["channel-password"] == args["channel-password"]:
|
|
getClientData(cID,"queue").put("")
|
|
|
|
now = time.process_time()
|
|
timeSpent = now - lastReceived
|
|
wait = args["delay"] - timeSpent
|
|
if wait > 0: time.sleep(wait)
|
|
lastReceived = now
|
|
|
|
if cmd[0] == "watch":
|
|
packet = -1
|
|
watchID = False
|
|
data = b""
|
|
with clientDataLock:
|
|
with clientsLock:
|
|
for cID in clients:
|
|
if getClientData(cID,"args")["user"] == args["user"] and getClientData(cID,"type") == "broadcast":
|
|
watchID = cID
|
|
packet = getClientData(cID,"bufferPacket")
|
|
|
|
if watchID == False: return
|
|
if packet == -1:
|
|
q.get(timeout=timeout)
|
|
with clientDataLock:
|
|
packet = getClientData(watchID,"bufferPacket")
|
|
data = getClientData(watchID,"buffer")[str(packet)]
|
|
self.connection.sendall(data)
|
|
|
|
while True:
|
|
q.get(timeout=timeout)
|
|
packet += 1
|
|
with clientDataLock:
|
|
data = getClientData(watchID,"buffer")[str(packet)]
|
|
self.connection.sendall(data) |