Beispiel #1
0
void IrcBot::Start()
{

	sock.async_connect(*endpoint_iterator, [this](boost::system::error_code ec)
	{
		ConnectHandler(ec);
	});
}
MythSocket::MythSocket(
    qt_socket_fd_t socket, MythSocketCBs *cb, bool use_shared_thread) :
    ReferenceCounter(QString("MythSocket(%1)").arg(socket)),
    m_tcpSocket(new QTcpSocket()),
    m_thread(NULL),
    m_socketDescriptor(-1),
    m_peerPort(-1),
    m_callback(cb),
    m_useSharedThread(use_shared_thread),
    m_disableReadyReadCallback(false),
    m_connected(false),
    m_dataAvailable(0),
    m_isValidated(false),
    m_isAnnounced(false)
{
    LOG(VB_SOCKET, LOG_INFO, LOC + QString("MythSocket(%1, 0x%2) ctor")
        .arg(socket).arg((intptr_t)(cb),0,16));

    // Use direct connections so m_tcpSocket can be used
    // in the handlers safely since they will be running
    // in the same thread as all other m_tcpSocket users.

    connect(m_tcpSocket,  SIGNAL(connected()),
            this, SLOT(ConnectHandler()),
            Qt::DirectConnection);
    connect(m_tcpSocket,  SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(ErrorHandler(QAbstractSocket::SocketError)),
            Qt::DirectConnection);
    connect(m_tcpSocket,  SIGNAL(aboutToClose()),
            this, SLOT(AboutToCloseHandler()));
    connect(m_tcpSocket,  SIGNAL(disconnected()),
            this, SLOT(DisconnectHandler()),
            Qt::DirectConnection);
    connect(m_tcpSocket,  SIGNAL(readyRead()),
            this, SLOT(ReadyReadHandler()),
            Qt::DirectConnection);

    connect(this, SIGNAL(CallReadyRead()),
            this, SLOT(CallReadyReadHandler()),
            Qt::QueuedConnection);

    if (socket != -1)
    {
        m_tcpSocket->setSocketDescriptor(
            socket, QAbstractSocket::ConnectedState,
            QAbstractSocket::ReadWrite);

        ConnectHandler(); // already called implicitly above?
    }

    if (!use_shared_thread)
    {
        m_thread = new MThread(QString("MythSocketThread(%1)").arg(socket));
        m_thread->start();
    }
    else
    {
        QMutexLocker locker(&s_thread_lock);
        if (!s_thread)
        {
            s_thread = new MThread("SharedMythSocketThread");
            s_thread->start();
        }
        m_thread = s_thread;
        s_thread_cnt++;
    }

    m_tcpSocket->moveToThread(m_thread->qthread());
    moveToThread(m_thread->qthread());
}