Qt 4.8
Signals | Public Functions | Protected Functions | Private Functions | Properties | List of all members
QWSServerSocket Class Reference

#include <qwssocket_qws.h>

Inheritance diagram for QWSServerSocket:

Signals

void newConnection ()
 

Public Functions

QWSSocketnextPendingConnection ()
 
 QWSServerSocket (const QString &file, QObject *parent=0)
 
 ~QWSServerSocket ()
 

Protected Functions

void incomingConnection (int socketDescriptor)
 

Private Functions

void init (const QString &file)
 

Properties

QList< int > inboundConnections
 
QMutex ssmx
 

Detailed Description

Definition at line 90 of file qwssocket_qws.h.

Constructors and Destructors

◆ QWSServerSocket()

QWSServerSocket::QWSServerSocket ( const QString file,
QObject parent = 0 
)

Definition at line 175 of file qwssocket_qws.cpp.

177  : QUnixSocketServer(parent)
178 #else
179  : QTcpServer(parent)
180 #endif
181 {
182  init(file);
183 }
void init(const QString &file)
The QTcpServer class provides a TCP-based server.
Definition: qtcpserver.h:61
The QUnixSocketServer class provides a Unix domain socket based server.

◆ ~QWSServerSocket()

QWSServerSocket::~QWSServerSocket ( )

Definition at line 253 of file qwssocket_qws.cpp.

254 {
255 }

Functions

◆ incomingConnection()

void QWSServerSocket::incomingConnection ( int  socketDescriptor)
protected

Definition at line 259 of file qwssocket_qws.cpp.

260 {
261  inboundConnections.append( socketDescriptor );
263 }
QList< int > inboundConnections
void newConnection()
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define emit
Definition: qobjectdefs.h:76

◆ init()

void QWSServerSocket::init ( const QString file)
private

Definition at line 185 of file qwssocket_qws.cpp.

186 {
187 #ifndef QT_NO_SXE
188  QByteArray fn = file.toLocal8Bit();
189  bool result = QUnixSocketServer::listen( fn );
190  if ( !result )
191  {
192  QUnixSocketServer::ServerError err = serverError();
193  switch ( err )
194  {
195  case InvalidPath:
196  qWarning("QWSServerSocket:: invalid path %s", qPrintable(file));
197  break;
198  case ResourceError:
199  case BindError:
200  case ListenError:
201  qWarning("QWSServerSocket:: could not listen on path %s", qPrintable(file));
202  break;
203  default:
204  break;
205  }
206  }
207 #else
208  int backlog = 16; //#####
209 
210 // create socket
211  int s = ::socket(PF_LOCAL, SOCK_STREAM, 0);
212  if (s == -1) {
213  perror("QWSServerSocket::init");
214  qWarning("QWSServerSocket: unable to create socket.");
215  return;
216  }
217 
218  QByteArray fn = file.toLocal8Bit();
219  unlink(fn.constData()); // doesn't have to succeed
220 
221  // bind socket
222  struct sockaddr_un a;
223  memset(&a, 0, sizeof(a));
224  a.sun_family = PF_LOCAL;
225  strncpy(a.sun_path, fn.constData(), sizeof(a.sun_path) - 1);
226  int r = ::bind(s, (struct sockaddr*)&a, SUN_LEN(&a));
227  if (r < 0) {
228  perror("QWSServerSocket::init");
229  qWarning("QWSServerSocket: could not bind to file %s", fn.constData());
230  ::close(s);
231  return;
232  }
233 
234  if (chmod(fn.constData(), 0600) < 0) {
235  perror("QWSServerSocket::init");
236  qWarning("Could not set permissions of %s", fn.constData());
237  ::close(s);
238  return;
239  }
240 
241  // listen
242  if (::listen(s, backlog) == 0) {
243  if (!setSocketDescriptor(s))
244  qWarning( "QWSServerSocket could not set descriptor %d : %s", s, errorString().toLatin1().constData());
245  } else {
246  perror("QWSServerSocket::init");
247  qWarning("QWSServerSocket: could not listen to file %s", fn.constData());
248  ::close(s);
249  }
250 #endif
251 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
long ASN1_INTEGER_get ASN1_INTEGER * a
bool listen(const QByteArray &path)
Tells the server to listen for incoming connections on path.
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
ServerError
The ServerError enumeration represents the errors that can occur during server establishment.
#define qPrintable(string)
Definition: qglobal.h:1750

◆ newConnection

void QWSServerSocket::newConnection ( )
signal

◆ nextPendingConnection()

QWSSocket * QWSServerSocket::nextPendingConnection ( )

Definition at line 266 of file qwssocket_qws.cpp.

267 {
268  QMutexLocker locker( &ssmx );
269  if ( inboundConnections.count() == 0 )
270  return 0;
271  QWSSocket *s = new QWSSocket();
272  s->setSocketDescriptor( inboundConnections.takeFirst() );
273  return s;
274 }
QList< int > inboundConnections
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

Properties

◆ inboundConnections

QList<int> QWSServerSocket::inboundConnections
private

Definition at line 105 of file qwssocket_qws.h.

◆ ssmx

QMutex QWSServerSocket::ssmx
private

Definition at line 104 of file qwssocket_qws.h.


The documentation for this class was generated from the following files: