Esempio n. 1
0
void TCPClient::disconnected()
{
    if (DISPLAY_TCPCLIENT_DEBUG_MESSAGES)
        qDebug() << "Client" << getSocketDescriptor() << "was disconnected from the server.";
    isConnectedToHost = false;
    hostAddress = "";
    emit clientDisconnected(this, (tcpSocket->error() == QAbstractSocket::RemoteHostClosedError));
}
Esempio n. 2
0
void UdpSocket::autoConnect()
{
	if(!_autoConnect) return;
	if(!_serverInfo || !_socketDescriptor || _socketDescriptor->descriptor == -1) getSocketDescriptor();
}
Esempio n. 3
0
void UdpSocket::open()
{
	if(!_serverInfo || !_socketDescriptor || _socketDescriptor->descriptor == -1) getSocketDescriptor();
}
Esempio n. 4
0
void UPnP::listen()
{
    try
    {
        getSocketDescriptor();
        _out.printInfo("Info: Started listening.");

        _lastAdvertisement = BaseLib::HelperFunctions::getTimeSeconds();
        char buffer[1024];
        int32_t bytesReceived = 0;
        struct sockaddr_in si_other;
        socklen_t slen = sizeof(si_other);
        fd_set readFileDescriptor;
        timeval timeout;
        int32_t nfds = 0;
        BaseLib::Http http;
        while(!_stopServer)
        {
            try
            {
                if(!_serverSocketDescriptor || _serverSocketDescriptor->descriptor == -1)
                {
                    if(_stopServer) break;
                    std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                    getSocketDescriptor();
                    continue;
                }

                timeout.tv_sec = 0;
                timeout.tv_usec = 100000;
                FD_ZERO(&readFileDescriptor);
                {
                    auto fileDescriptorGuard = GD::bl->fileDescriptorManager.getLock();
                    fileDescriptorGuard.lock();
                    nfds = _serverSocketDescriptor->descriptor + 1;
                    if(nfds <= 0)
                    {
                        fileDescriptorGuard.unlock();
                        _out.printError("Error: Socket closed (1).");
                        GD::bl->fileDescriptorManager.shutdown(_serverSocketDescriptor);
                    }
                    FD_SET(_serverSocketDescriptor->descriptor, &readFileDescriptor);
                }

                bytesReceived = select(nfds, &readFileDescriptor, NULL, NULL, &timeout);
                if(bytesReceived == 0)
                {
                    if(BaseLib::HelperFunctions::getTimeSeconds() - _lastAdvertisement >= 60) sendNotify();
                    continue;
                }
                if(bytesReceived != 1)
                {
                    _out.printError("Error: Socket closed (2).");
                    GD::bl->fileDescriptorManager.shutdown(_serverSocketDescriptor);
                }

                bytesReceived = recvfrom(_serverSocketDescriptor->descriptor, buffer, 1024, 0, (struct sockaddr *)&si_other, &slen);
                if(bytesReceived <= 0) continue;
                http.reset();
                http.process(buffer, bytesReceived, false);
                if(http.isFinished()) processPacket(http);
            }
            catch(const std::exception& ex)
            {
                _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
            }
            catch(BaseLib::Exception& ex)
            {
                _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
            }
            catch(...)
            {
                _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
            }
        }
    }
    catch(const std::exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(BaseLib::Exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(...)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    }
    GD::bl->fileDescriptorManager.shutdown(_serverSocketDescriptor);
}