1 /*
2  * servsock.cpp - simple wrapper to QServerSocket
3  * Copyright (C) 2003  Justin Karneges
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * either version 2
9    of the License, or (at your option) any later version.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 
22 #include "servsock.h"
23 
24 // CS_NAMESPACE_BEGIN
25 
26 //----------------------------------------------------------------------------
27 // ServSock
28 //----------------------------------------------------------------------------
29 class ServSock::Private
30 {
31 public:
Private()32 	Private() {}
33 
34 	ServSockSignal *serv;
35 };
36 
ServSock(QObject * parent)37 ServSock::ServSock(QObject *parent)
38 :QObject(parent)
39 {
40 	d = new Private;
41 	d->serv = 0;
42 }
43 
~ServSock()44 ServSock::~ServSock()
45 {
46 	stop();
47 	delete d;
48 }
49 
isActive() const50 bool ServSock::isActive() const
51 {
52 	return (d->serv ? true: false);
53 }
54 
listen(quint16 port)55 bool ServSock::listen(quint16 port)
56 {
57 	stop();
58 
59 	d->serv = new ServSockSignal(this);
60 	if(!d->serv->listen(QHostAddress::Any, port)) {
61 		delete d->serv;
62 		d->serv = 0;
63 		return false;
64 	}
65 	connect(d->serv, &ServSockSignal::connectionReady, this, &ServSock::sss_connectionReady);
66 
67 	return true;
68 }
69 
stop()70 void ServSock::stop()
71 {
72 	delete d->serv;
73 	d->serv = 0;
74 }
75 
port() const76 int ServSock::port() const
77 {
78 	if(d->serv)
79 		return d->serv->serverPort();
80 	else
81 		return -1;
82 }
83 
address() const84 QHostAddress ServSock::address() const
85 {
86 	if(d->serv)
87 		return d->serv->serverAddress();
88 	else
89 		return QHostAddress();
90 }
91 
sss_connectionReady(int s)92 void ServSock::sss_connectionReady(int s)
93 {
94 	connectionReady(s);
95 }
96 
97 
98 //----------------------------------------------------------------------------
99 // ServSockSignal
100 //----------------------------------------------------------------------------
ServSockSignal(QObject * parent)101 ServSockSignal::ServSockSignal(QObject *parent)
102 :QTcpServer(parent)
103 {
104 	setMaxPendingConnections(16);
105 }
106 
incomingConnection(int socketDescriptor)107 void ServSockSignal::incomingConnection(int socketDescriptor)
108 {
109 	connectionReady(socketDescriptor);
110 }
111 
112 // CS_NAMESPACE_END
113