Message info
 
To:kde-commits@kde.org From:Andre Heinecke Subject:[soprano] client: Remove windows specific clientconnection Date:Mon, 7 May 2012 22:35:47 +0200 (CEST)
 

Git commit fa62829f4ae7dc8553eaa80e46a7496d347b384c by Andre Heinecke.
Committed on 08/05/2012 at 00:30.
Pushed by aheinecke into branch 'master'.

Remove windows specific clientconnection

The new code added by sebastian is cleaner and simpler and
we should use it on Windows too. Benefits of this should be obvious.

Using Windsock instead was actually way easier then I would
have expected so there is also no additional dependency.

REVIEW: 104872

M +5 -0 client/CMakeLists.txt
M +0 -53 client/clientconnection.cpp
M +0 -37 client/clientconnection_p.h
M +0 -43 client/localsocketclient.cpp
M +0 -3 client/localsocketclient.h
M +14 -2 client/socket.cpp
M +0 -4 client/socket.h
M +0 -16 client/socketstream.cpp

http://commits.kde.org/soprano/fa62829f4ae7dc8553eaa80e46a7496d347b384c

diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index c1fa916..2689568 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -75,6 +75,11 @@ target_link_libraries(
${QT_QTXML_LIBRARY}
soprano
)
+
+if(WIN32)
+ target_link_libraries(sopranoclient ws2_32)
+endif(WIN32)
+
if(BUILD_DBUS_SUPPORT)
target_link_libraries(
sopranoclient
diff --git a/client/clientconnection.cpp b/client/clientconnection.cpp
index 3565f4d..74da78c 100644
--- a/client/clientconnection.cpp
+++ b/client/clientconnection.cpp
@@ -45,67 +45,24 @@ namespace {
}


-#ifdef Q_OS_WIN
-Soprano::Client::SocketHandler::SocketHandler( ClientConnectionPrivate* client, Socket* socket )
- : QObject(),
- m_client( client ),
- m_socket( socket )
-{
-}
-
-
-Soprano::Client::SocketHandler::~SocketHandler()
-{
- QMutexLocker lock( &m_client->socketMutex );
- m_client->sockets.removeAll( m_socket );
- delete m_socket;
-}
-#endif
-
Soprano::Client::ClientConnection::ClientConnection( QObject* parent )
: QObject( parent ),
d( new ClientConnectionPrivate() )
{
-#ifndef Q_OS_WIN
d->socket = 0;
-#endif
}


Soprano::Client::ClientConnection::~ClientConnection()
{
-#ifdef Q_OS_WIN
- d->socketMutex.lock();
- // the sockets need to be deleted in their respective threads.
- // this is what d->socketStorage does. We only close them here.
- // FIXME: QThreadStorage does NOT delete the local data in its destructor!
- foreach( QIODevice* socket, d->sockets ) {
- socket->close();
- }
- d->socketMutex.unlock();
-#else
delete d->socket;
-#endif
delete d;
}


Soprano::Socket *Soprano::Client::ClientConnection::getSocket()
{
-#ifdef Q_OS_WIN
- if ( isConnected() ) {
- return d->socketStorage.localData()->socket();
- }
- else if ( Socket* socket = newConnection() ) {
- d->socketMutex.lock();
- SocketHandler* cleaner = new SocketHandler( d, socket );
- d->sockets.append( socket );
- d->socketMutex.unlock();
- d->socketStorage.setLocalData( cleaner );
- }
-#else
return d->socket;
-#endif
}


@@ -806,26 +763,16 @@ bool Soprano::Client::ClientConnection::checkProtocolVersion()

bool Soprano::Client::ClientConnection::connect()
{
-#ifndef Q_OS_WIN
if(!d->socket) {
d->socket = newConnection();
}
-#endif
return( getSocket() != 0 );
}


bool Soprano::Client::ClientConnection::isConnected()
{
-#ifdef Q_OS_WIN
- return ( d->socketStorage.hasLocalData() &&
- isConnected( d->socketStorage.localData()->socket() ) );
-#else
return( d->socket != 0 && d->socket->isConnected() );
-#endif
}

#include "clientconnection.moc"
-#ifdef Q_OS_WIN
-#include "clientconnection_p.moc"
-#endif
diff --git a/client/clientconnection_p.h b/client/clientconnection_p.h
index 003472f..9ff9b17 100644
--- a/client/clientconnection_p.h
+++ b/client/clientconnection_p.h
@@ -22,51 +22,14 @@
#ifndef _SOPRANO_SERVER_CLIENT_CONNECTION_P_H_
#define _SOPRANO_SERVER_CLIENT_CONNECTION_P_H_

-#include <QtCore/QMutex>
-#include <QtCore/QList>
-#include <QtCore/QThreadStorage>
-#include <QtCore/QIODevice>
#include "socket.h"

-class QThread;
-
namespace Soprano {
namespace Client {
-#ifdef Q_OS_WIN
- class ClientConnectionPrivate;
-
- /**
- * A simple wrapper class which makes sure that
- * sockets are closed and deleted once their
- * spawning thread goes down.
- */
- class SocketHandler : public QObject
- {
- Q_OBJECT
-
- public:
- SocketHandler( ClientConnectionPrivate*, QIODevice* socket );
- ~SocketHandler();
-
- QIODevice* socket() const { return m_socket; }
- void close() { m_socket->close(); }
-
- private:
- ClientConnectionPrivate* m_client;
- QIODevice* m_socket;
- };
-#endif
-
class ClientConnectionPrivate
{
public:
-#ifndef Q_OS_WIN
Socket* socket;
-#else
- QList<QIODevice*> sockets;
- QMutex socketMutex;
- QThreadStorage<Soprano::Client::SocketHandler*> socketStorage;
-#endif
};
}
}
diff --git a/client/localsocketclient.cpp b/client/localsocketclient.cpp
index cea06bc..f3579ff 100644
--- a/client/localsocketclient.cpp
+++ b/client/localsocketclient.cpp
@@ -29,12 +29,6 @@
#include <QtCore/QThread>
#include <QtNetwork/QLocalSocket>

-#ifdef Q_OS_WIN
-Q_DECLARE_METATYPE( QLocalSocket::LocalSocketError )
-Q_DECLARE_METATYPE( QAbstractSocket::SocketError )
-Q_DECLARE_METATYPE( QAbstractSocket::SocketState )
-#endif
-
namespace Soprano {
namespace Client {
class LocalSocketClientConnection : public ClientConnection
@@ -45,9 +39,6 @@ namespace Soprano {

protected:
Socket *newConnection();
-#ifdef Q_OS_WIN
- bool isConnected( Socket* );
-#endif

private:
QString m_socketPath;
@@ -71,34 +62,16 @@ namespace Soprano {
path = QDir::homePath() + QLatin1String( "/.soprano/socket" );
}

-#ifndef Q_OS_WIN
LocalSocket* socket = new LocalSocket;
if ( socket->open( path ) ) {
return socket;
}
else {
setError( socket->lastError() );
-#else
- QLocalSocket* socket = new QLocalSocket;
- socket->connectToServer( path, QIODevice::ReadWrite );
- if ( socket->waitForConnected() ) {
- QObject::connect( socket, SIGNAL( error( QLocalSocket::LocalSocketError ) ),
- parent(), SLOT( _s_localSocketError( QLocalSocket::LocalSocketError ) ) );
- return socket;
- }
- else {
- setError( socket->errorString() );
-#endif
delete socket;
return 0;
}
}
-
-#ifdef Q_OS_WIN
- bool LocalSocketClientConnection::isConnected( Socket *device ) {
- return( device ? static_cast<QLocalSocket*>( device )->state() == QLocalSocket::ConnectedState : false );
- }
-#endif
}
}

@@ -111,28 +84,12 @@ public:
}

LocalSocketClientConnection* connection;
-#ifdef Q_OS_WIN
- void _s_localSocketError( QLocalSocket::LocalSocketError );
-#endif
};

-#ifdef Q_OS_WIN
-void Soprano::Client::LocalSocketClient::Private::_s_localSocketError( QLocalSocket::LocalSocketError error )
-{
- qDebug() << "local socket error:" << error;
-}
-#endif
-
-
Soprano::Client::LocalSocketClient::LocalSocketClient( QObject* parent )
: QObject( parent ),
d( new Private() )
{
-#ifdef Q_OS_WIN
- qRegisterMetaType<QLocalSocket::LocalSocketError>();
- qRegisterMetaType<QAbstractSocket::SocketError>();
- qRegisterMetaType<QAbstractSocket::SocketState>();
-#endif
}


diff --git a/client/localsocketclient.h b/client/localsocketclient.h
index a31c694..d97aed0 100644
--- a/client/localsocketclient.h
+++ b/client/localsocketclient.h
@@ -120,9 +120,6 @@ namespace Soprano {
private:
class Private;
Private* const d;
-#ifdef Q_OS_WIN
- Q_PRIVATE_SLOT( d, void _s_localSocketError( QLocalSocket::LocalSocketError ) )
-#endif
};
}
}
diff --git a/client/socket.cpp b/client/socket.cpp
index da547d9..7fd32d2 100644
--- a/client/socket.cpp
+++ b/client/socket.cpp
@@ -22,8 +22,6 @@
#include "socket.h"

#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
@@ -31,6 +29,20 @@
#include <QtCore/QFile>
#include <QtCore/QDebug>

+#ifndef Q_OS_WIN
+#include <sys/socket.h>
+#include <sys/un.h>
+#else
+#include <winsock2.h>
+/* POSIX requires only at least 100 bytes */
+#define UNIX_PATH_LEN 108
+
+struct sockaddr_un {
+ unsigned short sun_family; /* address family AF_LOCAL/AF_UNIX */
+ char sun_path[UNIX_PATH_LEN]; /* 108 bytes of socket address */
+};
+#endif
+
Soprano::Socket::Socket( SOCKET_HANDLE fd )
: m_handle( fd ),
m_mutex( QMutex::Recursive )
diff --git a/client/socket.h b/client/socket.h
index 5cdc00b..f4a44fa 100644
--- a/client/socket.h
+++ b/client/socket.h
@@ -29,9 +29,6 @@
typedef int SOCKET_HANDLE;

namespace Soprano {
-#ifdef Q_OS_WIN
- typedef QIODevice Socket;
-#else
/**
* A thread-safe socket without the QObject overhead of Qt's own socket
* implementations.
@@ -97,7 +94,6 @@ namespace Soprano {

QString m_path;
};
-#endif
}

#endif
diff --git a/client/socketstream.cpp b/client/socketstream.cpp
index dfd8713..42645ad 100644
--- a/client/socketstream.cpp
+++ b/client/socketstream.cpp
@@ -33,17 +33,13 @@
Soprano::SocketStream::SocketStream( Soprano::Socket* dev )
: m_device( dev )
{
-#ifndef Q_OS_WIN
m_device->lock();
-#endif
}


Soprano::SocketStream::~SocketStream()
{
-#ifndef Q_OS_WIN
m_device->unlock();
-#endif
}


@@ -60,11 +56,7 @@ bool Soprano::SocketStream::writeByteArray( const QByteArray& a )
int x = qMin( 1024U, len-cnt );
int r = m_device->write( a.data()+cnt, x );
if ( r < 0 ) {
-#ifdef Q_OS_WIN
- setError( Error::Error( QString( "Failed to write string after %1 of %2 bytes (%3)." ).arg( cnt ).arg( len ).arg( m_device->errorString() ) ) );
-#else
setError(m_device->lastError());
-#endif
return false;
}
cnt += r;
@@ -255,11 +247,7 @@ bool Soprano::SocketStream::read( char* data, qint64 size )
setError( Error::Error( QString( "Failed to read after %1 of %2 bytes (%3)." )
.arg( cnt )
.arg( size )
-#ifdef Q_OS_WIN
- .arg( m_device->errorString ) ) );
-#else
.arg( m_device->lastError().message() ) ) );
-#endif
return false;
}
else if ( r == 0 && size > 0 ) {
@@ -267,11 +255,7 @@ bool Soprano::SocketStream::read( char* data, qint64 size )
setError( Error::Error( QString( "Timeout when reading after %1 of %2 bytes (%3)." )
.arg( cnt )
.arg( size )
-#ifdef Q_OS_WIN
- .arg( m_device->errorString ) ) );
-#else
.arg( m_device->lastError().message() ) ) );
-#endif
return false;
}
}