-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistener.cpp
More file actions
73 lines (61 loc) · 1.61 KB
/
Copy pathlistener.cpp
File metadata and controls
73 lines (61 loc) · 1.61 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <QSslSocket>
#include <QDebug>
#include "connection.h"
#include "journal.h"
#include "bitslinger.h"
#include "utils/tcpserver.h"
#include "listener.h"
Listener::Listener(const Listener::Config &config, QObject *parent)
: QObject(parent),
m_journal(0),
m_config(config),
m_server(0)
{
}
void Listener::setConfig(const Listener::Config &config)
{
if (m_config == config)
return;
m_config = config;
if (m_server) {
stopListening();
listen();
}
}
void Listener::setBitSlinger(BitSlinger *slinger)
{
m_slinger = slinger;
m_journal = slinger->journal();
}
void Listener::stopListening()
{
m_server->close();
m_server->deleteLater();
m_server = 0;
}
bool Listener::listen()
{
m_server = new TcpServer(this);
connect(m_server, SIGNAL(newConnection()), this, SLOT(handleConnection()));
bool ok = m_server->listen(m_config.listenAddress, m_config.listenPort);
if (!ok) {
qDebug() << "Unable to listen" << m_server->errorString();
}
return ok;
}
void Listener::handleConnection()
{
while (m_server->hasPendingConnections()) {
qDebug() << "New connection";
QSslSocket *sock = m_server->nextPendingConnection();
Connection *con = new Connection(sock, this);
con->setSslMode(m_config.sslMode);
con->setProxyType(m_config.type);
con->setBitSlinger(m_slinger);
m_journal->addConnection(con);
if (m_config.type == Listener::HttpProxy)
con->startHttpProxy();
else
con->connectToHost(m_config.targetHost, m_config.targetPort);
}
}