コード例 #1
0
void TCP::accept_new_client() {
    int cli;
    struct sockaddr_in data;
    socklen_t len = sizeof(data);

    cli = accept(sock, (sockaddr*)&data, &len);

    if (cli < 0) {
        perror("accept");
        return;
    }
    client cli_data = {false, cli, data};

    char ak[sizeof(int32_t) + 1 + sizeof(int)] = {SET_ID, 0};
    int tmp = sizeof(int);

    // send id to new client
    memcpy(ak + 1 + sizeof(int32_t), &cli, sizeof(cli));
    memcpy(ak + 1, &tmp, sizeof(tmp));
    this->send(cli, &ak, sizeof(ak));

    // send new client's id to everyone
    ak[0] = NEW_CLIENT;
    memcpy(ak + 1 + sizeof(int32_t), &cli, sizeof(cli));
    sendToEveryone(ak, sizeof(ak), cli);

    // send all clients' id to new one
    for (auto it = clients.begin(); it != clients.end(); ++it) {
        memcpy(ak + 1 + sizeof(int32_t), &(*it), sizeof(*it));
        this->send(cli, &ak, sizeof(ak));
    }
    tmp = 0;
    memcpy(ak + 1 + sizeof(int32_t), &tmp, sizeof(tmp));
    this->send(cli, &ak, sizeof(ak));

    // add new client to pending list
    mutex->lock();
    pending_clients.push_back(cli_data);
    mutex->unlock();
    clients.push_back(cli);
    std::cout << "new client !" << std::endl;
}
コード例 #2
0
ファイル: chathandler.cpp プロジェクト: Ablu/invertika
void ChatHandler::handleAnnounceMessage(ChatClient &client, MessageIn &msg)
{
    std::string text = msg.readString();

    if (!stringFilter->filterContent(text))
    {
        warnPlayerAboutBadWords(client);
        return;
    }

    if (client.accountLevel == AL_ADMIN || client.accountLevel == AL_GM)
    {
        // TODO: b_lindeijer: Shouldn't announcements also have a sender?
        LOG_INFO("ANNOUNCE: " << text);
        MessageOut result(CPMSG_ANNOUNCEMENT);
        result.writeString(text);

        // We send the message to all players in the default channel as it is
        // an announcement.
        sendToEveryone(result);

        // log transaction
        Transaction trans;
        trans.mCharacterId = client.characterId;
        trans.mAction = TRANS_MSG_ANNOUNCE;
        trans.mMessage = "User announced " + text;
        storage->addTransaction(trans);
    }
    else
    {
        MessageOut result(CPMSG_ERROR);
        result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS);
        client.send(result);
        LOG_INFO(client.characterName <<
            " couldn't make an announcement due to insufficient rights.");
    }

}