Compare commits

...

2 Commits

Author SHA1 Message Date
Fierelier
9243978b0e Better-ish client 2021-04-09 23:46:48 +02:00
Fierelier
1d58d87ef4 "version bump" lol 2021-04-09 23:46:19 +02:00
4 changed files with 54 additions and 23 deletions

View File

@ -82,7 +82,7 @@ def sendResponse(connection,data):
senderThreadSleepMin = 0.0333
senderThreadSleepMax = 1.0
senderThreadSleepIncr = 0.05
senderThreadSleepIncr = 0.01
class senderThread(threading.Thread):
def __init__(self,connectionThread):
@ -110,7 +110,7 @@ class senderThread(threading.Thread):
with self.lock:
sleepTime = self.sleep
print(sleepTime)
#print(sleepTime)
time.sleep(sleepTime)
with self.lock:
@ -153,8 +153,11 @@ class connectionThread(threading.Thread):
print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")")
self.closed = True
def sendResponse(self,data):
with self.lock:
def sendResponse(self,data,lock = True):
if lock == True:
with self.lock:
self.senderThread.addToQueue([sendResponse,[self.connection,data],{}])
else:
self.senderThread.addToQueue([sendResponse,[self.connection,data],{}])
def run(self):

View File

@ -0,0 +1,29 @@
moduleDepends([
p("[text server]","[api]","commands"),
])
global textSend
def textSend(self,command,args):
if len(args) < 2:
return ["error","nonfatal","syntax","Correct syntax: " +command+ ",<user>,<command>,[argument 1],[argument 2],..."]
user = args[0].lower()
if len(user) < 1:
return ["error","nonfatal","name_too_short","Needs to be at least 1 character in length."]
me = ""
with self.lock:
me = self.user
if not me:
return ["error","nonfatal","not_logged_in"]
with threadsLock:
for threadId in threads:
thread = threads[threadId]
with thread.lock:
if thread.user != user: continue
thread.sendResponse(textListToCommand(["send",me] + args[1:]).encode("utf-8"),lock = False)
return ["ok"]
textCommandAddHandler("send",textSend)

View File

@ -33,14 +33,6 @@ def textOnRequest(event,self,requestLength):
text = data.decode("utf-8")
print(":".join(map(str,self.address))+ " > " +text)
if text == "close":
with threadsLock:
global close
close = True
self.closeThread()
return True
response = textCommandRun(self,textCommandToList(text))
print("response: " +textListToCommand(response))
self.sendResponse(textListToCommand(response).encode("utf-8"))

View File

@ -19,6 +19,17 @@ sp = pUp(s)
# script start
import socket
import threading
class receiverThread(threading.Thread):
def __init__(self,connection):
threading.Thread.__init__(self)
self.connection = connection
def run(self):
while True:
response = getResponse(connection).decode("utf-8")
print("server: " +response)
def sendRequest(connection,data):
connection.sendall(len(data).to_bytes(4,"big") + data)
@ -39,23 +50,19 @@ def main():
global connection
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("127.0.0.1",21779))
thread = receiverThread(connection)
thread.start()
while True:
text = input("data: ")
text = input()
data = text.encode("utf-8")
connection.settimeout(15)
sendRequest(connection,data)
response = getResponse(connection).decode("utf-8")
print("server: " +response)
connection.settimeout(None)
if text == "exit":
connection.close()
break
if text == "close":
connection.close()
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("127.0.0.1",21779))
connection.close()
break
if __name__ == '__main__':
main()