Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port of QTcpServer/QTcpSocket to Qt5.
  • Loading branch information
daniel-kristjansson authored and jyavenard committed Mar 8, 2013
1 parent 57e7dc8 commit a1a4b53
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/libmythbase.pro
Expand Up @@ -74,7 +74,7 @@ inc.files += mythcdrom.h autodeletedeque.h dbutil.h mythdeque.h
inc.files += referencecounter.h mythcommandlineparser.h mthread.h mthreadpool.h
inc.files += filesysteminfo.h hardwareprofile.h bonjourregister.h serverpool.h
inc.files += plist.h bswap.h signalhandling.h ffmpeg-mmx.h mythdate.h
inc.files += mythplugin.h mythpluginapi.h
inc.files += mythplugin.h mythpluginapi.h mythqtcompat.h

# This stuff is not Qt5 compatible..
contains(QT_VERSION, ^4\\.[0-9]\\..*) {
Expand Down
10 changes: 10 additions & 0 deletions mythtv/libs/libmythbase/mythqtcompat.h
@@ -0,0 +1,10 @@
#ifndef MYTH_QT_COMPAT_H_
#define MYTH_QT_COMPAT_H_

#if (QT_VERSION >= 0x050000)
typedef qintptr qt_socket_fd_t;
#else
typedef int qt_socket_fd_t;
#endif

#endif // MYTH_QT_COMPAT_H_
5 changes: 3 additions & 2 deletions mythtv/libs/libmythbase/mythsocket.cpp
Expand Up @@ -73,7 +73,8 @@ static QString to_sample(const QByteArray &payload)
return sample;
}

MythSocket::MythSocket(int socket, MythSocketCBs *cb, bool use_shared_thread) :
MythSocket::MythSocket(
qt_socket_fd_t socket, MythSocketCBs *cb, bool use_shared_thread) :
ReferenceCounter(QString("MythSocket(%1)").arg(socket)),
m_tcpSocket(new QTcpSocket()),
m_thread(NULL),
Expand Down Expand Up @@ -112,7 +113,7 @@ MythSocket::MythSocket(int socket, MythSocketCBs *cb, bool use_shared_thread) :
this, SLOT(CallReadyReadHandler()),
Qt::QueuedConnection);

if (socket > -1)
if (socket != -1)
{
m_tcpSocket->setSocketDescriptor(
socket, QAbstractSocket::ConnectedState,
Expand Down
5 changes: 3 additions & 2 deletions mythtv/libs/libmythbase/mythsocket.h
Expand Up @@ -10,6 +10,7 @@

#include "referencecounter.h"
#include "mythsocket_cb.h"
#include "mythqtcompat.h"
#include "mythbaseexp.h"
#include "mthread.h"

Expand All @@ -29,7 +30,7 @@ class MBASE_PUBLIC MythSocket : public QObject, public ReferenceCounter
friend class MythSocketManager;

public:
MythSocket(int socket = -1, MythSocketCBs *cb = NULL,
MythSocket(qt_socket_fd_t socket = -1, MythSocketCBs *cb = NULL,
bool use_shared_thread = false);

bool ConnectToHost(const QString &hostname, quint16 port);
Expand Down Expand Up @@ -98,7 +99,7 @@ class MBASE_PUBLIC MythSocket : public QObject, public ReferenceCounter
QTcpSocket *m_tcpSocket; // only set in ctor
MThread *m_thread; // only set in ctor
mutable QMutex m_lock;
int m_socketDescriptor; // protected by m_lock
qt_socket_fd_t m_socketDescriptor; // protected by m_lock
QHostAddress m_peerAddress; // protected by m_lock
int m_peerPort; // protected by m_lock
MythSocketCBs *m_callback; // only set in ctor
Expand Down
10 changes: 7 additions & 3 deletions mythtv/libs/libmythbase/serverpool.cpp
Expand Up @@ -61,7 +61,7 @@ PrivTcpServer::PrivTcpServer(QObject *parent) : QTcpServer(parent)
{
}

void PrivTcpServer::incomingConnection(int socket)
void PrivTcpServer::incomingConnection(qt_socket_fd_t socket)
{
emit newConnection(socket);
}
Expand Down Expand Up @@ -386,9 +386,13 @@ bool ServerPool::listen(QList<QHostAddress> addrs, quint16 port,
server->setProxy(m_proxy);
server->setMaxPendingConnections(m_maxPendingConn);

#if (QT_VERSION >= 0x050000)
connect(server, &PrivTcpServer::newConnection,
this, &ServerPool::newTcpConnection);
#else
connect(server, SIGNAL(newConnection(int)),
this, SLOT(newTcpConnection(int)));

#endif
if (server->listen(*it, m_port))
{
LOG(VB_GENERAL, LOG_INFO, QString("Listening on TCP %1:%2")
Expand Down Expand Up @@ -567,7 +571,7 @@ qint64 ServerPool::writeDatagram(const QByteArray &datagram,
return writeDatagram(datagram.data(), datagram.size(), addr, port);
}

void ServerPool::newTcpConnection(int socket)
void ServerPool::newTcpConnection(qt_socket_fd_t socket)
{
QTcpSocket *qsock = new QTcpSocket(this);
qsock->setSocketDescriptor(socket);
Expand Down
8 changes: 5 additions & 3 deletions mythtv/libs/libmythbase/serverpool.h
Expand Up @@ -9,6 +9,7 @@
#include <QUdpSocket>
#include <QStringList>

#include "mythqtcompat.h"
#include "mythbaseexp.h"

/** \class ServerPool
Expand All @@ -33,10 +34,10 @@ class PrivTcpServer : public QTcpServer
~PrivTcpServer() {};

signals:
void newConnection(int socket);
void newConnection(qt_socket_fd_t socket);

protected:
void incomingConnection(int socket);
void incomingConnection(qt_socket_fd_t socket);
};

class MBASE_PUBLIC ServerPool : public QObject
Expand Down Expand Up @@ -92,7 +93,8 @@ class MBASE_PUBLIC ServerPool : public QObject

protected slots:
virtual void newUdpDatagram(void);
virtual void newTcpConnection(int socket);

virtual void newTcpConnection(qt_socket_fd_t socket);

private:
static void SelectDefaultListen(bool force=false);
Expand Down
9 changes: 7 additions & 2 deletions mythtv/libs/libmythprotoserver/mythsocketmanager.cpp
Expand Up @@ -50,7 +50,7 @@ MythServer::MythServer(QObject *parent) : ServerPool(parent)
{
}

void MythServer::newTcpConnection(int socket)
void MythServer::newTcpConnection(qt_socket_fd_t socket)
{
emit newConnection(socket);
}
Expand Down Expand Up @@ -97,12 +97,17 @@ bool MythSocketManager::Listen(int port)
return false;
}

#if (QT_VERSION >= 0x050000)
connect(m_server, &MythServer::newConnection,
this, &MythSocketManager::newConnection);
#else
connect(m_server, SIGNAL(newConnection(int)),
this, SLOT(newConnection(int)));
#endif
return true;
}

void MythSocketManager::newConnection(int sd)
void MythSocketManager::newConnection(qt_socket_fd_t sd)
{
QMutexLocker locker(&m_socketListLock);
m_socketList.insert(new MythSocket(sd, this));
Expand Down
7 changes: 4 additions & 3 deletions mythtv/libs/libmythprotoserver/mythsocketmanager.h
Expand Up @@ -13,6 +13,7 @@
// MythTV
#include "socketrequesthandler.h"
#include "sockethandler.h"
#include "mythqtcompat.h"
#include "mthreadpool.h"
#include "mythsocket.h"
#include "serverpool.h"
Expand All @@ -24,10 +25,10 @@ class MythServer : public ServerPool
MythServer(QObject *parent=0);

signals:
void newConnection(int socketDescriptor);
void newConnection(qt_socket_fd_t socket);

protected slots:
virtual void newTcpConnection(int socket);
virtual void newTcpConnection(qt_socket_fd_t socket);
};

class PROTOSERVER_PUBLIC MythSocketManager : public QObject, public MythSocketCBs
Expand All @@ -53,7 +54,7 @@ class PROTOSERVER_PUBLIC MythSocketManager : public QObject, public MythSocketCB
bool Listen(int port);

public slots:
void newConnection(int sd);
void newConnection(qt_socket_fd_t sd);

private:
void ProcessRequestWork(MythSocket *socket);
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythupnp/httpserver.cpp
Expand Up @@ -122,7 +122,7 @@ QScriptEngine* HttpServer::ScriptEngine()
//
/////////////////////////////////////////////////////////////////////////////

void HttpServer::newTcpConnection(int nSocket)
void HttpServer::newTcpConnection(qt_socket_fd_t nSocket)
{
m_threadPool.startReserved(
new HttpWorker(*this, nSocket),
Expand Down Expand Up @@ -238,7 +238,7 @@ void HttpServer::DelegateRequest(HTTPRequest *pRequest)
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

HttpWorker::HttpWorker(HttpServer &httpServer, int sock) :
HttpWorker::HttpWorker(HttpServer &httpServer, qt_socket_fd_t sock) :
m_httpServer(httpServer), m_socket(sock), m_socketTimeout(10000)
{
m_socketTimeout = 1000 *
Expand Down
8 changes: 5 additions & 3 deletions mythtv/libs/libmythupnp/httpserver.h
Expand Up @@ -30,6 +30,7 @@
#include <QList>

// MythTV headers
#include "mythqtcompat.h"
#include "serverpool.h"
#include "httprequest.h"
#include "mthreadpool.h"
Expand Down Expand Up @@ -106,7 +107,7 @@ class UPNP_PUBLIC HttpServer : public ServerPool

QScriptEngine *ScriptEngine(void);

virtual void newTcpConnection(int socket); // QTcpServer
virtual void newTcpConnection(qt_socket_fd_t socket); // QTcpServer

QString GetSharePath(void) const
{ // never modified after creation, so no need to lock
Expand Down Expand Up @@ -135,13 +136,14 @@ class UPNP_PUBLIC HttpServer : public ServerPool
class HttpWorker : public QRunnable
{
public:
HttpWorker(HttpServer &httpServer, int sock);

HttpWorker(HttpServer &httpServer, qt_socket_fd_t sock);

virtual void run(void);

protected:
HttpServer &m_httpServer;
int m_socket;
qt_socket_fd_t m_socket;
int m_socketTimeout;
};

Expand Down

0 comments on commit a1a4b53

Please sign in to comment.