Torc  0.1
torchttpserverlistener.cpp
Go to the documentation of this file.
1 /* Class TorcHTTPServerAddress
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2018
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torcnetwork.h"
26 #include "torchttpserverlistener.h"
27 
28 TorcHTTPServerListener::TorcHTTPServerListener(TorcHTTPServer *Parent, const QHostAddress &Address, int Port)
29  : QTcpServer()
30 {
31  if (!Parent)
32  return;
33 
35 
36  (void)Listen(Address, Port);
37 }
38 
40 {
41  close();
42 }
43 
44 bool TorcHTTPServerListener::Listen(const QHostAddress &Address, int Port)
45 {
46  if (!listen(Address, Port))
47  {
48  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to listen on %1").arg(TorcNetwork::IPAddressToLiteral(Address, Port)));
49  return false;
50  }
51 
52  if (QHostAddress::AnyIPv4 == Address)
53  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Listening on port %1 (IPv4 addresses only)").arg(Port));
54  else if (QHostAddress::AnyIPv6 == Address)
55  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Listening on port %1 (IPv6 addresses only)").arg(Port));
56  else if (QHostAddress::Any == Address)
57  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Listening on port %1 (IPv4 and IPv6 addresses)").arg(Port));
58  else
59  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Listening on %1").arg(TorcNetwork::IPAddressToLiteral(Address, Port)));
60  return true;
61 }
62 
63 void TorcHTTPServerListener::incomingConnection(qintptr SocketDescriptor)
64 {
65  emit NewConnection(SocketDescriptor);
66 }
bool Listen(const QHostAddress &Address, int Port=0)
static QString IPAddressToLiteral(const QHostAddress &Address, int Port, bool UseLocalhost=true)
Convert an IP address to a string literal.
void incomingConnection(qintptr SocketDescriptor) overridefinal
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
TorcHTTPServerListener(TorcHTTPServer *Parent, const QHostAddress &Address, int Port=0)
void NewConnection(qintptr SocketDescriptor)
void NewConnection(qintptr SocketDescriptor)
An HTTP server.