litex_server: allow multiple clients to connect to the same server
This commit is contained in:
parent
8a69a47e7b
commit
a760322fbd
|
@ -1,5 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from litex.soc.tools.remote.etherbone import EtherbonePacket, EtherboneRecord, EtherboneWrites
|
from litex.soc.tools.remote.etherbone import EtherbonePacket, EtherboneRecord, EtherboneWrites
|
||||||
|
@ -10,6 +11,7 @@ class RemoteServer(EtherboneIPC):
|
||||||
def __init__(self, comm, port=1234):
|
def __init__(self, comm, port=1234):
|
||||||
self.comm = comm
|
self.comm = comm
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.lock = False
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
if hasattr(self, "socket"):
|
if hasattr(self, "socket"):
|
||||||
|
@ -19,7 +21,7 @@ class RemoteServer(EtherboneIPC):
|
||||||
try:
|
try:
|
||||||
self.socket.bind(("localhost", self.port + i))
|
self.socket.bind(("localhost", self.port + i))
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
print("tcp port: {:d}".format(self.port + i))
|
print("tcp port: {:d}".format(self.port + i))
|
||||||
self.socket.listen(1)
|
self.socket.listen(1)
|
||||||
|
@ -49,11 +51,18 @@ class RemoteServer(EtherboneIPC):
|
||||||
|
|
||||||
record = packet.records.pop()
|
record = packet.records.pop()
|
||||||
|
|
||||||
# writes:
|
# wait for lock
|
||||||
|
while self.lock:
|
||||||
|
time.sleep(0.01)
|
||||||
|
|
||||||
|
# set lock
|
||||||
|
self.lock = True
|
||||||
|
|
||||||
|
# handle writes:
|
||||||
if record.writes != None:
|
if record.writes != None:
|
||||||
self.comm.write(record.writes.base_addr, record.writes.get_datas())
|
self.comm.write(record.writes.base_addr, record.writes.get_datas())
|
||||||
|
|
||||||
# reads
|
# handle reads
|
||||||
if record.reads != None:
|
if record.reads != None:
|
||||||
reads = []
|
reads = []
|
||||||
for addr in record.reads.get_addrs():
|
for addr in record.reads.get_addrs():
|
||||||
|
@ -67,14 +76,19 @@ class RemoteServer(EtherboneIPC):
|
||||||
packet.records = [record]
|
packet.records = [record]
|
||||||
packet.encode()
|
packet.encode()
|
||||||
self.send_packet(client_socket, packet)
|
self.send_packet(client_socket, packet)
|
||||||
|
|
||||||
|
# release lock
|
||||||
|
self.lock = False
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
print("Disconnect")
|
print("Disconnect")
|
||||||
client_socket.close()
|
client_socket.close()
|
||||||
|
|
||||||
def start(self):
|
def start(self, nthreads):
|
||||||
self.serve_thread = threading.Thread(target=self._serve_thread)
|
for i in range(nthreads):
|
||||||
self.serve_thread.setDaemon(True)
|
self.serve_thread = threading.Thread(target=self._serve_thread)
|
||||||
self.serve_thread.start()
|
self.serve_thread.setDaemon(True)
|
||||||
|
self.serve_thread.start()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -121,7 +135,7 @@ def main():
|
||||||
|
|
||||||
server = RemoteServer(comm)
|
server = RemoteServer(comm)
|
||||||
server.open()
|
server.open()
|
||||||
server.start()
|
server.start(4)
|
||||||
try:
|
try:
|
||||||
import time
|
import time
|
||||||
while True: time.sleep(100)
|
while True: time.sleep(100)
|
||||||
|
|
Loading…
Reference in New Issue