Permalink
Browse files

Port of QTcpServer/QTcpSocket to Qt5.

  • Loading branch information...
daniel-kristjansson authored and jyavenard committed Feb 28, 2013
1 parent 57e7dc8 commit a1a4b53e8f1efe05ac2eb24421706b84d811ea5e
@@ -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]\\..*) {
@@ -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_
@@ -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),
@@ -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,
@@ -10,6 +10,7 @@
#include "referencecounter.h"
#include "mythsocket_cb.h"
#include "mythqtcompat.h"
#include "mythbaseexp.h"
#include "mthread.h"
@@ -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);
@@ -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
@@ -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);
}
@@ -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")
@@ -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);
@@ -9,6 +9,7 @@
#include <QUdpSocket>
#include <QStringList>
#include "mythqtcompat.h"
#include "mythbaseexp.h"
/** \class ServerPool
@@ -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
@@ -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);
@@ -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);
}
@@ -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));
@@ -13,6 +13,7 @@
// MythTV
#include "socketrequesthandler.h"
#include "sockethandler.h"
#include "mythqtcompat.h"
#include "mthreadpool.h"
#include "mythsocket.h"
#include "serverpool.h"
@@ -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
@@ -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);
@@ -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),
@@ -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 *
@@ -30,6 +30,7 @@
#include <QList>
// MythTV headers
#include "mythqtcompat.h"
#include "serverpool.h"
#include "httprequest.h"
#include "mthreadpool.h"
@@ -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
@@ -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;
};

0 comments on commit a1a4b53

Please sign in to comment.