Begin rewrite of socket connections

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2020-06-03 14:15:09 -05:00
parent fa6e5d8213
commit 66406ed739
2 changed files with 53 additions and 54 deletions

View File

@ -13,10 +13,11 @@ MyVirtual = pyxinput.vController()
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ip, port)) client.connect((ip, port))
while True: while True:
from_server = client.recv(8144) from_server = client.makefile(mode='rb')
#Decode Sever message #Decode Sever message
decodedServerData = pickle.loads(from_server) unpickler = pickle.Unpickler(from_server)
print("Raw Data:", str(decodedServerData)) decodedServerData = unpickler.load()
print("Raw Data:", decodedServerData)
# pyxinput will only accept values one at a time, so we need to apply the itme in the dictionary one by one # pyxinput will only accept values one at a time, so we need to apply the itme in the dictionary one by one
for event, state in decodedServerData.items(): for event, state in decodedServerData.items():
MyVirtual.set_value(event, state) MyVirtual.set_value(event, state)

100
Server.py
View File

@ -18,57 +18,55 @@ controllerData = ""
controllerDataDict = {'AxisLx': 0, 'AxisLy': 0, 'AxisRx': 0, 'AxisRy': 0, 'BtnBack': 0, 'BtnStart': 0, 'BtnA': 0, 'BtnB': 0, 'BtnX': 0, 'BtnY': 0, 'BtnThumbL': 0, 'BtnThumbR': 0, 'BtnShoulderL': 0, 'BtnShoulderR': 0, 'Dpad': 0, 'TriggerL': 0, 'TriggerR': 0} controllerDataDict = {'AxisLx': 0, 'AxisLy': 0, 'AxisRx': 0, 'AxisRy': 0, 'BtnBack': 0, 'BtnStart': 0, 'BtnA': 0, 'BtnB': 0, 'BtnX': 0, 'BtnY': 0, 'BtnThumbL': 0, 'BtnThumbR': 0, 'BtnShoulderL': 0, 'BtnShoulderR': 0, 'Dpad': 0, 'TriggerL': 0, 'TriggerR': 0}
#Lookup table to convert values from the "inputs" library #Lookup table to convert values from the "inputs" library
lookup_table = {'ABS_X': 'AxisLx', 'ABS_Y': 'AxisLy', 'ABS_RX': 'AxisRx', 'ABS_RY': 'AxisRy', 'BTN_SELECT': 'BtnBack', 'BTN_START': 'BtnStart', 'BTN_SOUTH': 'BtnA', 'BTN_EAST': 'BtnB', 'BTN_NORTH': 'BtnX', 'BTN_WEST': 'BtnY', 'BTN_THUMBL': 'BtnThumbL', 'BTN_THUMBR': 'BtnThumbR', 'BTN_TL': 'BtnShoulderL', 'BTN_TR': 'BtnShoulderR', 'ABS_Z': 'TriggerL', 'ABS_RZ': 'TriggerR'} lookup_table = {'ABS_X': 'AxisLx', 'ABS_Y': 'AxisLy', 'ABS_RX': 'AxisRx', 'ABS_RY': 'AxisRy', 'BTN_SELECT': 'BtnBack', 'BTN_START': 'BtnStart', 'BTN_SOUTH': 'BtnA', 'BTN_EAST': 'BtnB', 'BTN_NORTH': 'BtnX', 'BTN_WEST': 'BtnY', 'BTN_THUMBL': 'BtnThumbL', 'BTN_THUMBR': 'BtnThumbR', 'BTN_TL': 'BtnShoulderL', 'BTN_TR': 'BtnShoulderR', 'ABS_Z': 'TriggerL', 'ABS_RZ': 'TriggerR'}
print("Waiting for connection...") #print("Waiting for connection...")
def sendData(): def sendData():
#Pickle for transmittion #Pickle for transmittion
encodedControllerData = pickle.dumps(controllerDataDict) encodedControllerData = pickle.Pickler(controllerDataDict)
#print(encodedControllerData) #print(encodedControllerData)
conn.send(encodedControllerData) pickler.dump(encodedControllerData)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: file.flush()
s.bind((ip, port)) #Create Socket
s.listen(1) conn = socket.create_server((ip, port))
#s.setblocking(False) file = conn.makefile(mode='wb')
conn, addr = s.accept() pickler = pickle.Pickler(file)
with conn: #print(colored('Connected by', 'red'), addr)
print(colored('Connected by', 'red'), addr) print(controllerDataDict)
with conn:
while True:
events = get_gamepad()
for event in events:
#controllerDataTuple = event.ev_type, event.code, event.state
controllerDataTuple = event.code, event.state
controllerData = controllerDataTuple
#print("controllerData is a", type(controllerData))
print(controllerData)
print(controllerDataDict) print(controllerDataDict)
while True: #If event.code is SYN_REPORT, ignore it
events = get_gamepad() if event.code == "SYN_REPORT":
for event in events: continue
#controllerDataTuple = event.ev_type, event.code, event.state if event.code == "BTN_MODE":
controllerDataTuple = event.code, event.state print(colored('The home button is unimplemented on PYXinput, ignoring', 'red'))
controllerData = controllerDataTuple continue
#print("controllerData is a", type(controllerData)) #Ugh, PYXinput and inputs handle the Dpad in the most annoying way possible
print(controllerData) elif event.code == "ABS_HAT0X":
print(controllerDataDict) #if ABS_HAT0X is 0, then there is no input on the dpad
#If event.code is SYN_REPORT, ignore it if event.state == 0:
if event.code == "SYN_REPORT": controllerDataDict['Dpad'] = 0 #No input
continue if event.state == 1:
if event.code == "BTN_MODE": controllerDataDict['Dpad'] = 8 #Right
print(colored('The home button is unimplemented on PYXinput, ignoring', 'red')) if event.state == -1:
continue controllerDataDict['Dpad'] = 4 #Left
#Ugh, PYXinput and inputs handle the Dpad in the most annoying way possible sendData()
elif event.code == "ABS_HAT0X": elif event.code == "ABS_HAT0Y":
#if ABS_HAT0X is 0, then there is no input on the dpad #if ABS_HAT0Y is 0, then there is no input on the dpad
if event.state == 0: if event.state == 0:
controllerDataDict['Dpad'] = 0 #No input controllerDataDict['Dpad'] = 0 #No input
if event.state == 1: if event.state == 1:
controllerDataDict['Dpad'] = 8 #Right controllerDataDict['Dpad'] = 2 #Down
if event.state == -1: if event.state == -1:
controllerDataDict['Dpad'] = 4 #Left controllerDataDict['Dpad'] = 1 #Up
sendData() sendData()
elif event.code == "ABS_HAT0Y": else:
#if ABS_HAT0Y is 0, then there is no input on the dpad # Add values to controllerDataDict
if event.state == 0: controllerDataDict[lookup_table[str(event.code)]] = event.state
controllerDataDict['Dpad'] = 0 #No input sendData()
if event.state == 1:
controllerDataDict['Dpad'] = 2 #Down
if event.state == -1:
controllerDataDict['Dpad'] = 1 #Up
sendData()
else:
# Add values to controllerDataDict
controllerDataDict[lookup_table[str(event.code)]] = event.state
sendData()
#Execute everything
controllerLoop()