当前位置: 首页>>代码示例>>C++>>正文


C++ Join::getNickname方法代码示例

本文整理汇总了C++中Join::getNickname方法的典型用法代码示例。如果您正苦于以下问题:C++ Join::getNickname方法的具体用法?C++ Join::getNickname怎么用?C++ Join::getNickname使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Join的用法示例。


在下文中一共展示了Join::getNickname方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: handleLeaveMessage

void handleLeaveMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Leave message
    Leave *l = new Leave();
    //Decode leave message
    l->decode(inBuffer, size);
    //Search the users list for nickname
    bool deleted = false;

    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = *(it);
        //Remove nickname if it exists
        if (strcmp(l->getNickname(), user->getNickname()) == 0)
        {
            users.erase(it);
            deleted = true;
            break; //Erasing messes up iteration, so may as well just break out
        }
    }
    pthread_mutex_unlock(&userListMutex);

    //Create appropriate confirm message
    Confirm *c = new Confirm();
    c->setResponse(deleted);
    //Encode this message and return
    (*outSize)=c->encode(outBuffer);
    delete l;
    delete c;
}
开发者ID:huwdp,项目名称:Chat-Client-and-Server,代码行数:31,代码来源:main.cpp

示例2: handleJoinMessage

void handleJoinMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Join message
    Join *j = new Join();
    //Decode buffer into join message
    j->decode(inBuffer, size);
    printf("Nickname: %s\n", j->getNickname());
    //Search the users list for nickname
    //If the name exits do not add
    bool accept = true;
    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = *(it);
        if (strcmp(j->getNickname(), user->getNickname()) == 0)
        {
            //Exact match
            accept = false;
        }
    }
    pthread_mutex_unlock(&userListMutex);
    if (accept)
    {
        users.push_back(j);
    }
    else
    {
        delete j;
    }
    //Respond with confirm message
    Confirm *c = new Confirm();

    c->setResponse(accept);
    //Encode the message into the outbuffer and
    //Set the outsize
    (*outSize)=c->encode(outBuffer);
    delete c;
}
开发者ID:huwdp,项目名称:Chat-Client-and-Server,代码行数:38,代码来源:main.cpp

示例3: handleNewMessage

void handleNewMessage(char* inBuffer, int size, char** outBuffer, int *outSize)
{
    //Create a Chat Message
    ChatMessage *m = new ChatMessage();
    //Decode Chat Message
    m->decode(inBuffer, size);
    //Search the users list for nickname
    bool found = false;

    pthread_mutex_lock(&userListMutex);
    for(vector<Join*>::iterator it = users.begin(); it != users.end(); ++it)
    {
        Join *user = (*it);
        //Remove nickname if it exists
        if (strcmp(m->getSender(), user->getNickname()) == 0)
        {
            found = true;
        }
    }
    pthread_mutex_unlock(&userListMutex);

    //If the name exists add the name and message to the messages list
    if (found)
    {
        char* message = new char[strlen(m->getSender()) + 2 + strlen(m->getMessage()) + 1]; //sender + ": " + message + null
        sprintf(message, "%s: %s", m->getSender(), m->getMessage()); //easier than dealing with strcpy

        pthread_mutex_lock(&messageListMutex);
        messageList.push_back(message);
        pthread_mutex_unlock(&messageListMutex);
    }

    //Create appropriate confirm message
    Confirm *c = new Confirm();
    c->setResponse(found);
    //Encode this message and return
    (*outSize)=c->encode(outBuffer);

    delete m;
    delete c;
}
开发者ID:huwdp,项目名称:Chat-Client-and-Server,代码行数:41,代码来源:main.cpp

示例4: on_joinButton_clicked

void ChatClient::on_joinButton_clicked()
{
    Join *j;
    Confirm *c;
    INet4Address *servaddr;
    Connection *myConnection;
    char* buffer; //for encoded messages
    int bytesToSend;
    int bytesToReceive;
    int bytesReceived;

    //Get the strings out of the address, port and nickname
    //boxes and convert to strings;
    QString qip = ui.addresLine->text();
    QString qport = ui.portLine->text();
    QString qnick = ui.nicknameLine->text();

    //Create new connection objects
    servaddr = new INet4Address(qip.toLatin1().data(),qport.toInt());
    myConnection = new Connection(servaddr);

    //Connect to server

    if(myConnection->createSocket() < 0)
    {
        printf("Socket Error\n");
        return;
    }

    if(myConnection->connectSocket() < 0)
    {
        printf("Connect Error\n");
        delete(myConnection);
        delete(servaddr);
        return;
    }

    //Make a join message
    j = new Join();
    j->setNickname(qnick.toLatin1().data());

    //Send join message (as bytes)
    bytesToSend = j->encode(&buffer);

    printf("Buffer: %s, Nick: %s ",buffer, j->getNickname());

    myConnection->writeSize(bytesToSend);
    myConnection->writen(buffer,bytesToSend);

    delete(buffer);

    //Read response - take action!
    //Read byte count
    bytesToReceive = myConnection->readSize();
    buffer = new char[bytesToReceive];

    //Read message
    bytesReceived=myConnection->readn(buffer,bytesToReceive);

    if(bytesReceived < bytesToReceive)
        printf("Server response truncated!");

    //Convert to message (confirm)
    c = new Confirm();
    c->decode(buffer,bytesToReceive);
    //Destroy socket
    myConnection->closeSocket();

    //check if nick accepted
    if(c->getResponse()==true)
    {
        nickAccepted = true;
        ui.addresLine->setEnabled(false);
        ui.portLine->setEnabled(false);
        ui.nicknameLine->setEnabled(false);
        ui.joinButton->setEnabled(false);

        //Turn on the timer
        timer->start();
    }
    else
    {
        nickAccepted = false;
        ui.nicknameLine->setText(tr("Rejected!"));
    }


    delete myConnection;
    delete servaddr;
}
开发者ID:huwdp,项目名称:Chat-Client-and-Server,代码行数:90,代码来源:chatclient.cpp


注:本文中的Join::getNickname方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。