コード例 #1
0
void ServerThread::readyRead()
{
        if(!isInitialized)
    {
        initialize();
        return;
    }

    QDataStream input(s);
    s->waitForReadyRead();
    MessageEnvelop e;
    try
    {
        input >> e;
    }
    catch(MessageException e)
    {
        qDebug() << "Hackers are strong "
                 << "But I'm stronger";
        return;
        //sendError("Telnet Cannot kill me");
    }

    switch (e.getRequestType())
    {
    case SEND_LOGIN_TO_SERVER:
    case PING:
    case OK:
        return;
    case REQUEST_CALL_TO_CLIENT_FROM_SERVER:
        qDebug() << "Connection init";
        return requestCall(e.getName());
    case REQUEST_CLIENT_LIST_FROM_SERVER:
        return sendError("The List will be sent M'kay");
    case SEND_DENIED_RESPONSE_TO_COMMUNICATION:
        sendConnectionDenied();
        return;
    case SEND_SUCCESS_RESPONSE_TO_COMMUNICATION:
        qDebug() << "Connection Granted";
        ConnectionGranted(e.getPassword());
        return;

    case SEND_LOGOUT_REQUEST:
        logout();
        return;
    case END_OF_CALL_TO_CLIENT:
    case ERROR_SERVER_RESPONSE:
        return sendError("Only me can communicate on port 666 MUHEHEHE");
    default:
        return sendError("Unrecognized messsage type M'kay");
    }


}
コード例 #2
0
void ServerThread::initialize()
{
    isInitialized = true;
    MessageEnvelop e;
    QDataStream str(s);
    try
    {
        str >> e;
    }
    catch(MessageException e)
    {

        return;
    }

    qDebug() << e.getRequestType() << " initialize on data " << e.getName();

    if(e.getRequestType() == REGISTER_TO_SERVER)
    {
        registerNewClient(e);
        return;
    }

    if(e.getRequestType() != SEND_LOGIN_TO_SERVER)
    {
        sendError("The Connection is bad Mkay. You should try it again M'Kay");
        emit error(s->error());
        //s->deleteLater();
        //emit disconnected();
        return;
    }
    bool result = false;
    try
    {
        result = verify(e.getName(), e.getPassword());
    }
    catch (SqlConnection::SqlException e)
    {
        sendError("The username is not in the database M'kay");
        emit error(s->error());
        return;
    }

    if(!result)
    {
        sendError("The password is bad M'kay");
        emit error(s->error());
        return;
    }

    ConnectedClient * c = new ConnectedClient(s->peerAddress(), e.getName(),
                                              s->peerPort(), this);

    qDebug() << "The host " << e.getName()
             << " on address " <<   s->peerAddress() << " on port " << s->peerPort();
    c->init(this);
    qDebug() << "opened connection to " << e.getName();
    if(!list->addClient(c))
    {
        sendError("The connection already exists M'kay");
        emit error(s->error());
    }
    this->c = c;
    this->name = e.getName();
    list->callAllClients();



}