@Christian-Ehrlicher Hmmm I'm not having any luck with this method so far :/
Code below is self contained test. The DataStream is not commitTransaction and thus still waits for more data but never reads it/deals with it.
Socket dies, no more data comes in, handle_ready_read never gets called again, even if I force call again it wont read any more data... :/ Where am I hitting wall here?
import json
import uuid
from functools import partial
from PySide2.QtNetwork import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
class BasicServer(QTcpServer):
s_newJob = Signal(object)
def __init__(self, port: int):
QTcpServer.__init__(self)
self._mStatus = self.listen(QHostAddress.LocalHost, port)
self._mSocketList = {}
self._mJobs = {}
self.newConnection.connect(self._handleNewConnection)
print("Starting logic server on port : ", port, " Status : ", self._mStatus)
self._mTimer = QElapsedTimer()
self._mTimer.start()
def getStatus(self) -> int:
return self._mStatus
def _handleNewConnection(self):
client_socket: QTcpSocket = self.nextPendingConnection()
client_socket.readyRead.connect(partial(self.handle_ready_read, client_socket))
stream = QDataStream(client_socket)
self._mSocketList[client_socket] = [stream, QByteArray()]
client_socket.disconnected.connect(partial(self._handleSocketDisconnect, client_socket))
def handle_ready_read(self, client_socket):
streamItem = self._mSocketList.get(client_socket, None)
if streamItem is None:
return
print("reading?")
stream = streamItem[0]
stream.startTransaction()
streamData = streamItem[1]
stream >> streamData
print(len(streamData))
self._mSocketList[client_socket][1] = streamData
if not stream.commitTransaction():
return
print("IM FINISH! ", streamData)
def _handleSocketDisconnect(self, socketPtr: QTcpSocket):
print("Socket is gone...")
return # for now keep socket "alive"
killPort = socketPtr.peerPort()
if killPort in self._mJobs:
if not self._mJobs[killPort]["emitted"]:
if self._mJobs[killPort]["toDownload"] == 0:
pass
del self._mJobs[killPort]
if socketPtr in self._mSocketList:
del self._mSocketList[socketPtr]
print("Killing socket", killPort, socketPtr)
socketPtr.deleteLater()
def clientTest():
port = 500
socket = QTcpSocket()
# Connect to the server
socket.connectToHost("localhost", port)
msg = str(uuid.uuid4())
for a in range(0, 10):
msg += msg
print(len(msg) / 1024 / 1024)
# Wait for the connection to be established
if socket.waitForConnected(5000):
print("sending...")
# Create a QByteArray to hold the data
data = QByteArray("Hello World!".encode())
jDumped = json.dumps({"hello": "world", "this": [{"this": "is"}, {"awe": "some"}],
"stupidLongStuff": msg,
"END": 1})
data = QByteArray(jDumped.encode())
# Create a QDataStream to write the data
stream = QDataStream(socket)
# stream.setVersion(QDataStream.Qt_5_15)
print("Data size : ", len(data))
stream << data
# stream.writeString("hellooo")
# dataSize = len(data)
# print("sending data size : ", dataSize)
# Write the data to the stream
# stream.writeInt64(dataSize)
# socket.waitForBytesWritten()
# socket.write(struct.pack("l", len(data)))
# socket.write(data.encode())
socket.waitForBytesWritten()
print("Data sent successfully")
else:
print("Error:", socket.errorString())
app = QApplication([])
s = BasicServer(500)
clientTest()
app.exec_()