bool LocalSocketPrivate_Unix::connectToServer(const QString& filename) { // qDebug("[%p] LocalSocketPrivate_Unix::connectToServer()", this); // Try to open socket quintptr socketDescriptor = ::socket(AF_UNIX, SOCK_STREAM, 0); if(socketDescriptor < 1) { setError(QStringLiteral("Cannot open socket: %1").arg(strerror(errno))); return false; } struct sockaddr_un serv_addr; serv_addr.sun_family = AF_UNIX; QByteArray fn(filename.toLocal8Bit()); memcpy(serv_addr.sun_path, fn.constData(), fn.length() + 1); if(::connect(socketDescriptor, (sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { setError(QStringLiteral("Cannot connect to server: %1").arg(strerror(errno))); return false; } return setSocketDescriptor(socketDescriptor); }
bool QWSSocket::connectToLocalFile(const QString &file) { #ifndef QT_NO_SXE bool result = QUnixSocket::connect( file.toLocal8Bit() ); if ( !result ) { perror( "QWSSocketAuth::connectToLocalFile could not connect:" ); emit error(QAbstractSocket::ConnectionRefusedError); return false; } return true; #else // create socket int s = ::socket(PF_LOCAL, SOCK_STREAM, 0); // connect to socket struct sockaddr_un a; memset(&a, 0, sizeof(a)); a.sun_family = PF_LOCAL; strncpy(a.sun_path, file.toLocal8Bit().constData(), sizeof(a.sun_path) - 1); int r = ::connect(s, (struct sockaddr*)&a, SUN_LEN(&a)); if (r == 0) { setSocketDescriptor(s); } else { perror("QWSSocket::connectToLocalFile could not connect:"); ::close(s); emit error(ConnectionRefusedError); return false; } #endif return true; }
bool TThreadApplicationServer::start(bool debugMode) { if (isListening()) { return true; } bool res = loadLibraries(); if (!res) { if (debugMode) { tSystemError("Failed to load application libraries."); return false; } else { tSystemWarn("Failed to load application libraries."); } } if (listenSocket <= 0 || !setSocketDescriptor(listenSocket)) { tSystemError("Failed to set socket descriptor: %d", listenSocket); return false; } // instantiate if (!debugMode) { TSystemBus::instantiate(); TPublisher::instantiate(); } TUrlRoute::instantiate(); TSqlDatabasePool::instantiate(); TKvsDatabasePool::instantiate(); TStaticInitializeThread::exec(); return true; }
void HttpServer :: incomingConnection(qintptr socketDescriptor) { auto socket = new QTcpSocket (this); connect (socket, SIGNAL(readyRead()), this, SLOT(readSocket())); connect (socket, SIGNAL(disconnected()), this, SLOT(disconnectSocket())); socket->setSocketDescriptor(socketDescriptor); }
void TcpClientManager::manage() // Slot appelé par started() lors de l'exécution d'un nouveau thread { // Récupère le QTcpSocket du client à l'aide de son descripteur if(! setSocketDescriptor(_socketDescriptor) ) { emit sig_finished(); return; } // TODO : Signaler intruderEjected si ce n'est pas un reader puis quitter QString clientAddress = peerAddress().toString(); if(bddisNotAReaderAddress(clientAddress)) { abort(); emit sig_intruderEjected(clientAddress); emit sig_finished(); return; } Reader* reader = bddgetReaderByAddress(clientAddress); reader->isConnected(true); bddsetReader(reader); emit sig_readerConnected(); // TODO : Signaler readerDetected si c'est un reader // emit sig_readerConnected(Reader*); }
void CGroupClient::setSocket(qintptr socketDescriptor) { if (setSocketDescriptor(socketDescriptor) == false) { // failure ... what to do? qDebug( "Connection failed. Native socket not recognized by CGroupClient."); } setConnectionState(Connected); }
void CGroupClient::setSocket(int socketDescriptor) { if (setSocketDescriptor(socketDescriptor) == false) { // failure ... what to do? print_debug(DEBUG_GROUP, "Connection failed. Native socket not recognized by CGroupClient."); } setConnectionState(Connected); }
Connection::Connection(int socketDescriptor, const QString &cert, const QString &key) : serverMode(true), readSize(0) { setSocketDescriptor(socketDescriptor); setLocalCertificate(cert); setPrivateKey(key); init(); handshake(); }
MTcpSocket::MTcpSocket(int pSocketDescriptor, QObject *pParent) : QTcpSocket(pParent), _socketStream(this), _readState(ReadReady), _nextPacketSize(-1), _nextPacketType(StandardPacketTypes::UNKNOWN), _mutex() { _socketStream.setVersion(QDataStream::Qt_4_4); setSocketDescriptor(pSocketDescriptor); init(); }
void LocalHTTPServer::incomingConnection(qintptr descriptor) { auto client = new QTcpSocket(this); QObject::connect(client, &QTcpSocket::readyRead, [client, this] { readClient(client); }); QObject::connect(client, &QTcpSocket::disconnected, client, &QObject::deleteLater); client->setSocketDescriptor(descriptor); }
void QstProtocol::setSocket( int socket ) { if (debug_on) { qDebug() << ( QString("%1 QstProtocol::setSocket(socket=%2)"). arg(uniqueId()). arg(socket).toLatin1()); } setSocketDescriptor( socket ); rx_timer.start(); testConnection(); }
void AbstractSocket::close() { if (socketDescriptor() == -1) return; #ifndef WIN32 shutdown(socketDescriptor(), SHUT_RDWR); ::close(socketDescriptor()); #else closesocket(socketDescriptor()); //WSACleanup(); #endif setSocketDescriptor(-1); }
/** Called by the server: */ bool TCPClient::initializeServerSideClient(qint32 socketDescriptor) { if (isConnectedToHost) { emit errorWarning(tr("Client cannot be initialized because it is already connected to the host")); return false; } declareAndConnectSockets(); setSocketDescriptor(socketDescriptor); if (DISPLAY_TCPCLIENT_DEBUG_MESSAGES) qDebug() << "Server-side client" << socketDescriptor << "initialized."; isServerSideClient = true; return true; }
sslConnection::sslConnection( int socketDescriptor, QObject *parent ) : QSslSocket( parent ) { if( !setSocketDescriptor( socketDescriptor ) ) { qDebug() << "Couldn't set socket descriptor"; deleteLater(); return; } setPeerVerifyMode(QSslSocket::VerifyPeer); setLocalCertificate( "mycert.pem" ); setPrivateKey("mycert.pem"); startServerEncryption(); }
void QtUnixServerSocket::setPath(const QString &path) { path_.clear(); int sock = ::socket(PF_UNIX, SOCK_STREAM, 0); if (sock != -1) { struct sockaddr_un addr; ::memset(&addr, 0, sizeof(struct sockaddr_un)); addr.sun_family = AF_UNIX; ::unlink(path.toLatin1().constData()); // ### This might need to be changed unsigned int pathlen = strlen(path.toLatin1().constData()); if (pathlen > sizeof(addr.sun_path)) pathlen = sizeof(addr.sun_path); ::memcpy(addr.sun_path, path.toLatin1().constData(), pathlen); if ((::bind(sock, (struct sockaddr *)&addr, SUN_LEN(&addr)) != -1) && (::listen(sock, 5) != -1)) { setSocketDescriptor(sock); path_ = path; } } }
bool QtUnixSocket::connectTo(const QString &path) { bool ret = false; int sock = ::socket(PF_UNIX, SOCK_STREAM, 0); if (sock != -1) { struct sockaddr_un addr; ::memset(&addr, 0, sizeof(struct sockaddr_un)); addr.sun_family = AF_UNIX; size_t pathlen = strlen(path.toLatin1().constData()); pathlen = qMin(pathlen, sizeof(addr.sun_path)); ::memcpy(addr.sun_path, path.toLatin1().constData(), pathlen); int err = ::connect(sock, (struct sockaddr *)&addr, SUN_LEN(&addr)); if (err != -1) { setSocketDescriptor(sock); ret = true; } else { ::close(sock); } } return ret; }
int AbstractSocket::create() { if (d->socketDescriptor != -1) return 0; int domain = PF_INET; int type = SOCK_STREAM; if (d->socketType == UdpSocket) type = SOCK_DGRAM; else if (d->socketType == UdsSocket) { domain = AF_UNIX; } int fd = (int)socket(domain, type, 0); if (fd == -1) return -1; #ifndef WIN32 ::fcntl(fd, F_SETFD, FD_CLOEXEC); #endif setSocketDescriptor(fd); return 0; }
bool TcpServer::listen(const IpAddress& address, uint16_t port) { if(address.isNull()) return false; int fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); inet_pton(AF_INET, address.toString().c_str(), &addr.sin_addr); if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) { setServerAddress(address); setServerPort(port); setSocketDescriptor(fd); if(::listen(fd, SOMAXCONN) == 0) return true; } return false; }
OftSocket::OftSocket(Client *client, int socketDescriptor, QObject *parent) : QTcpSocket(parent), m_client(client) { setSocketDescriptor(socketDescriptor); init(); }
void QWSServerSocket::init(const QString &file) { #ifndef QT_NO_SXE QByteArray fn = file.toLocal8Bit(); bool result = QUnixSocketServer::listen( fn ); if ( !result ) { QUnixSocketServer::ServerError err = serverError(); switch ( err ) { case InvalidPath: qWarning("QWSServerSocket:: invalid path %s", qPrintable(file)); break; case ResourceError: case BindError: case ListenError: qWarning("QWSServerSocket:: could not listen on path %s", qPrintable(file)); break; default: break; } } #else int backlog = 16; //##### // create socket int s = ::socket(PF_LOCAL, SOCK_STREAM, 0); if (s == -1) { perror("QWSServerSocket::init"); qWarning("QWSServerSocket: unable to create socket."); return; } QByteArray fn = file.toLocal8Bit(); unlink(fn.constData()); // doesn't have to succeed // bind socket struct sockaddr_un a; memset(&a, 0, sizeof(a)); a.sun_family = PF_LOCAL; strncpy(a.sun_path, fn.constData(), sizeof(a.sun_path) - 1); int r = ::bind(s, (struct sockaddr*)&a, SUN_LEN(&a)); if (r < 0) { perror("QWSServerSocket::init"); qWarning("QWSServerSocket: could not bind to file %s", fn.constData()); ::close(s); return; } if (chmod(fn.constData(), 0600) < 0) { perror("QWSServerSocket::init"); qWarning("Could not set permissions of %s", fn.constData()); ::close(s); return; } // listen if (::listen(s, backlog) == 0) { if (!setSocketDescriptor(s)) qWarning( "QWSServerSocket could not set descriptor %d : %s", s, errorString().toLatin1().constData()); } else { perror("QWSServerSocket::init"); qWarning("QWSServerSocket: could not listen to file %s", fn.constData()); ::close(s); } #endif }
bool TApplicationServer::open() { T_TRACEFUNC(); if (!isListening()) { quint16 port = Tf::app()->appSettings().value("ListenPort").toUInt(); int sock = nativeListen(QHostAddress::Any, port); if (sock > 0 && setSocketDescriptor(sock)) { tSystemDebug("listen successfully. port:%d", port); } else { tSystemError("Failed to set socket descriptor: %d", sock); nativeClose(sock); return false; } } // Loads libraries if (!libLoaded) { // Sets work directory QString libPath = Tf::app()->libPath(); if (QDir(libPath).exists()) { // To resolve the symbols in the app libraries QDir::setCurrent(libPath); } else { tSystemError("lib directory not found"); return false; } QStringList filter; #if defined(Q_OS_WIN) filter << "controller.dll" << "view.dll"; #elif defined(Q_OS_DARWIN) filter << "libcontroller.dylib" << "libview.dylib"; #elif defined(Q_OS_UNIX) filter << "libcontroller.so" << "libview.so"; #else filter << "libcontroller.*" << "libview.*"; #endif QDir controllerDir("."); QStringList list = controllerDir.entryList(filter, QDir::Files); for (QStringListIterator i(list); i.hasNext(); ) { QString path = controllerDir.absoluteFilePath(i.next()); QLibrary lib(path); if (lib.load()) { tSystemDebug("Library loaded: %s", qPrintable(path)); libLoaded = true; } else { tSystemError("%s", qPrintable(lib.errorString())); } } } QDir::setCurrent(Tf::app()->webRootPath()); TUrlRoute::instantiate(); TSqlDatabasePool::instantiate(); switch (Tf::app()->multiProcessingModule()) { case TWebApplication::Thread: { TStaticInitializeThread *initializer = new TStaticInitializeThread(); initializer->start(); initializer->wait(); delete initializer; break; } case TWebApplication::Prefork: { TStaticInitializer *initializer = new TStaticInitializer(); initializer->start(); delete initializer; break; } default: break; } return true; }
QgsHelpContextSocket::QgsHelpContextSocket( int socket, QObject *parent ) : QTcpSocket( parent ) { connect( this, SIGNAL( readyRead() ), SLOT( readClient() ) ); setSocketDescriptor( socket ); }
bool TApplicationServer::open() { T_TRACEFUNC(""); if (!isListening()) { quint16 port = Tf::app()->appSettings().value("ListenPort").toUInt(); int sock = nativeListen(QHostAddress::Any, port); if (sock > 0 && setSocketDescriptor(sock)) { tSystemDebug("listen successfully. port:%d", port); } else { tSystemError("Failed to set socket descriptor: %d", sock); nativeClose(sock); return false; } } // Loads libraries if (!libLoaded) { // Sets work directory QString libPath = Tf::app()->libPath(); if (QDir(libPath).exists()) { // To resolve the symbols in the app libraries QDir::setCurrent(libPath); } else { tSystemError("lib directory not found"); return false; } QStringList libs; #if defined(Q_OS_WIN) libs << "controller" << "view"; #else libs << "libcontroller" << "libview"; #endif for (QStringListIterator it(libs); it.hasNext(); ) { QLibrary lib(it.next()); if (lib.load()) { tSystemDebug("Library loaded: %s", qPrintable(lib.fileName())); libLoaded = true; } else { tSystemError("%s", qPrintable(lib.errorString())); } } QStringList controllers = TActionController::availableControllers(); tSystemDebug("Available controllers: %s", qPrintable(controllers.join(" "))); } QDir::setCurrent(Tf::app()->webRootPath()); TUrlRoute::instantiate(); TSqlDatabasePool::instantiate(); switch (Tf::app()->multiProcessingModule()) { case TWebApplication::Thread: { TStaticInitializeThread *initializer = new TStaticInitializeThread(); initializer->start(); initializer->wait(); delete initializer; break; } case TWebApplication::Prefork: { TStaticInitializer *initializer = new TStaticInitializer(); initializer->start(); delete initializer; break; } default: break; } return true; }
void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) { Q_D(QLocalSocket); if (state() == ConnectedState || state() == ConnectingState) { setErrorString(tr("Trying to connect while connection is in progress")); emit error(QLocalSocket::OperationError); return; } d->error = QLocalSocket::UnknownSocketError; d->errorString = QString(); d->state = ConnectingState; emit stateChanged(d->state); if (name.isEmpty()) { d->error = QLocalSocket::ServerNotFoundError; setErrorString(QLocalSocket::tr("%1: Invalid name").arg(QLatin1String("QLocalSocket::connectToServer"))); d->state = UnconnectedState; emit error(d->error); emit stateChanged(d->state); return; } QString pipePath = QLatin1String("\\\\.\\pipe\\"); if (name.startsWith(pipePath)) d->fullServerName = name; else d->fullServerName = pipePath + name; // Try to open a named pipe HANDLE localSocket; forever { DWORD permissions = (openMode & QIODevice::ReadOnly) ? GENERIC_READ : 0; permissions |= (openMode & QIODevice::WriteOnly) ? GENERIC_WRITE : 0; localSocket = CreateFile((const wchar_t *)d->fullServerName.utf16(), // pipe name permissions, 0, // no sharing NULL, // default security attributes OPEN_EXISTING, // opens existing pipe FILE_FLAG_OVERLAPPED, NULL); // no template file if (localSocket != INVALID_HANDLE_VALUE) break; DWORD error = GetLastError(); // It is really an error only if it is not ERROR_PIPE_BUSY if (ERROR_PIPE_BUSY != error) { break; } // All pipe instances are busy, so wait until connected or up to 5 seconds. if (!WaitNamedPipe((const wchar_t *)d->fullServerName.utf16(), 5000)) break; } if (localSocket == INVALID_HANDLE_VALUE) { d->setErrorString(QLatin1String("QLocalSocket::connectToServer")); d->fullServerName = QString(); return; } // we have a valid handle d->serverName = name; if (setSocketDescriptor((qintptr)localSocket, ConnectedState, openMode)) { d->handle = localSocket; emit connected(); } }