-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqaydsshagentserver.cpp
43 lines (37 loc) · 1.11 KB
/
qaydsshagentserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "qaydsshagentserver.h"
#include "qaydsshagentclient.h"
#include <QTcpServer>
#include <QDebug>
#include <QTcpSocket>
QAydSshAgentServer::QAydSshAgentServer(int port, QObject *parent)
: QObject(parent)
{
}
bool QAydSshAgentServer::listen(int port)
{
if(m_server) {
m_server->close();
m_server->deleteLater();
}
m_server = new QTcpServer(this);
QObject::connect(m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
if(!m_server->listen(QHostAddress::Any, port)) {
qInfo() << m_server->errorString();
return false;
}
return true;
}
void QAydSshAgentServer::onNewConnection()
{
while(m_server->hasPendingConnections()) {
QTcpSocket *tcp = m_server->nextPendingConnection();
QAydSshAgentClient *client = new QAydSshAgentClient(tcp, this);
QObject::connect(client, SIGNAL(destroyed()), this, SLOT(onClientDestroyed()));
m_clients.append(client);
}
}
void QAydSshAgentServer::onClientDestroyed()
{
QAydSshAgentClient *client = qobject_cast<QAydSshAgentClient*>(sender());
m_clients.removeAll(client);
}