@@ -107,7 +107,11 @@ void Socket::flush() {
107107
108108SocketServer::~SocketServer () { this ->disableServer (); }
109109
110- void SocketServer::onPostReload () {
110+ void SocketServer::onReload (QObject* oldInstance) {
111+ if (auto * old = qobject_cast<SocketServer*>(oldInstance)) {
112+ old->disableServer ();
113+ }
114+
111115 this ->postReload = true ;
112116 if (this ->isActivatable ()) this ->enableServer ();
113117}
@@ -152,6 +156,8 @@ bool SocketServer::isActivatable() {
152156void SocketServer::enableServer () {
153157 this ->disableServer ();
154158
159+ qCDebug (logSocket) << " Enabling socket server" << this << " at" << this ->mPath ;
160+
155161 this ->server = new QLocalServer (this );
156162 QObject::connect (
157163 this ->server ,
@@ -160,31 +166,38 @@ void SocketServer::enableServer() {
160166 &SocketServer::onNewConnection
161167 );
162168
169+ if (QFile::remove (this ->mPath )) {
170+ qCWarning (logSocket) << " Deleted existing file at" << this ->mPath << " to create socket" ;
171+ }
172+
163173 if (!this ->server ->listen (this ->mPath )) {
164- qWarning ( ) << " could not start socket server at" << this ->mPath ;
174+ qCWarning (logSocket ) << " Could not start socket server at" << this ->mPath ;
165175 this ->disableServer ();
166176 }
167177
168178 this ->activeTarget = false ;
179+ this ->activePath = this ->mPath ;
169180 emit this ->activeStatusChanged ();
170181}
171182
172183void SocketServer::disableServer () {
173184 auto wasActive = this ->server != nullptr ;
174185
175- if (this ->server != nullptr ) {
186+ if (wasActive) {
187+ qCDebug (logSocket) << " Disabling socket server" << this << " at" << this ->activePath ;
176188 for (auto * socket: this ->mSockets ) {
177189 socket->deleteLater ();
178190 }
179191
180192 this ->mSockets .clear ();
193+ this ->server ->close ();
181194 this ->server ->deleteLater ();
182195 this ->server = nullptr ;
183- }
184196
185- if (this ->mPath != nullptr ) {
186- if (QFile::exists (this ->mPath ) && !QFile::remove (this ->mPath )) {
187- qWarning () << " failed to delete socket file at" << this ->mPath ;
197+ if (!this ->activePath .isEmpty ()) {
198+ if (QFile::exists (this ->activePath ) && !QFile::remove (this ->activePath )) {
199+ qWarning () << " Failed to delete socket file at" << this ->activePath ;
200+ }
188201 }
189202 }
190203
0 commit comments