Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "serv.h"
- #include "ui_serv.h"
- #include <QHostAddress>
- #include <QLabel>
- #include <QNetworkInterface>
- #include <QClipboard>
- serv::serv(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::serv)
- {
- ui->setupUi(this);
- _serv = new QTcpServer(this);
- connect(_serv,SIGNAL(newConnection()) ,this,SLOT(newCon()));
- ui->pushButton_3->setEnabled(false);
- ui->listWidget->addItem("Напишите /help для подсказки");
- }
- //---------------------------------
- static QVector<QString> bannedNames;
- static QVector<QTcpSocket*> sok ;
- static QVector<QTcpSocket*> banned;
- static QVector<QString> names;
- //-------------------------------------
- serv::~serv()
- {
- delete ui;
- }
- //------------------------------------------------------------------------------------------------
- void serv::newCon() {
- ui->pushButton->setEnabled(true);
- QTcpSocket* _soke = _serv->nextPendingConnection();
- QByteArray arr1;
- QDataStream _key(&arr1,QIODevice::WriteOnly);
- QString key = shifKey(getKey());
- _key << 0 << (quint8)1 << key;
- _key.device()->seek(0);
- _key << int(arr1.size() - sizeof(quint8) - sizeof(int));
- _soke->write(arr1);
- for(QTcpSocket* ban:banned)
- {
- if(ban == _soke)
- {
- _soke->write(f_message(3,"Вы забанены."));
- return;
- }
- }
- sok.push_back(_soke);
- connect(_soke,SIGNAL(readyRead()),this,SLOT(readyR()));
- connect(_soke,SIGNAL(disconnected()),this,SLOT(disCon()));
- }
- void serv::disCon(){
- auto i = sok.begin();
- auto _i = names.begin();
- int a = 0;
- QTcpSocket* soke = static_cast<QTcpSocket*>(sender());
- for(QTcpSocket* _sok:sok) {
- if(soke == _sok) {sok.erase(i+a);
- ui->listWidget->addItem(names[a] + " отключился.");
- names.erase(_i+a) ;break;}
- a++;
- }
- if(sok.size() == 0)
- ui->pushButton->setEnabled(false);
- }
- //-----------------------------------------------------------------------------------------------------------
- void serv::readyR()
- {
- QTcpSocket* _sock = static_cast<QTcpSocket*>(sender());
- QDataStream mes(_sock);
- if(_size == 0) {
- if(_sock->bytesAvailable() < (int)sizeof(int))
- return;
- else{
- mes >> _size;
- }
- }
- if(command == 0 ){
- if(_sock->bytesAvailable() < (int)sizeof(quint8))
- return;
- else{
- mes >> command;
- }
- }
- if(_sock->bytesAvailable() < _size && !messageAccept)
- return;
- else if (!messageAccept){
- mes >> messageInp;
- _size = 0;
- messageAccept = true;
- }
- QByteArray arr1;
- QDataStream _key(&arr1,QIODevice::WriteOnly);
- if (messageAccept && command == 2) {
- ui->listWidget->addItem(deShif(messageInp));
- command = 0;
- messageInp.clear();
- for(int i=0;i < sok.size();i++)
- {
- if(sok[i] == _sock)
- continue;
- sok[i]->write(f_message(2,deShif(messageInp)));
- messageAccept = false;
- }
- }
- if (messageAccept && command == 1)
- {
- names.push_back(deShif(messageInp));
- for(int i=0;i < sok.size();i++)
- {
- if(sok[i] == _sock)
- {
- continue;
- }
- sok[i]->write(f_message(2,deShif(messageInp) + " подключился."));
- }
- command = 0;
- ui->listWidget->addItem(deShif(messageInp) + " подключился.");
- messageInp.clear();
- messageAccept = false;
- }
- }
- //--------------------------------------------------------------------
- void serv::help(QTcpSocket* soket = nullptr)
- {
- QString information = "/unban<имя> - разбанить \n\
- /ban<имя> - забанить \n\
- /kick<имя> - кикнуть \n\
- /list - список пользователей \n\
- /copyip - скопировать IPv4 адресс \n\
- /help - помощь";
- if(soket == nullptr)
- {
- ui->listWidget->addItem(information);
- }
- else
- soket->write(f_message(2,information));
- return;
- }
- //==========================================================================
- QString serv::ban(QString _txt)
- {
- QString _message;
- bool userFindet = false;
- for(int i=0;i < sok.size();i++)
- {
- if(_txt.mid(6,_txt.size()-6) == names[i])
- {
- banned.push_back(sok[i]);
- bannedNames.push_back(names[i]);
- sok[i]->write(f_message(3,"Вас забанили"));
- _message = names[i] + " забанен.";
- ui->listWidget->addItem(_message);
- userFindet = true;
- }
- }
- if(userFindet)
- noCommand = false;
- else
- {
- ui->listWidget->addItem("Пользователь не найден.");
- return "error";
- }
- return _message;
- }
- //===============================================================================
- QString serv::kick(QString _txt)
- {
- QString _message;
- bool userFindet = false;
- for(int i=0;i++;)
- {
- if(_txt.mid(7,_txt.size()-7) == names[i])
- {
- sok[i]->write(f_message(3,"Вас кикнули"));
- _message = names[i] + " кикнут.";
- ui->listWidget->addItem(_message);
- userFindet = true;
- }
- }
- if(userFindet)
- noCommand = false;
- else
- {
- ui->listWidget->addItem("Пользователь не найден.");
- return "error";
- }
- return _message;
- }
- //====================================================================================
- QString serv::unban(QString _txt)
- {
- QString _message;
- bool userFindet = false;
- for(int i=0;i++;)
- {
- if(_txt.mid(8,_txt.size()-8) == bannedNames[i])
- {
- auto iter = banned.begin();
- banned.erase(iter + i);
- auto iter2 = bannedNames.begin();
- bannedNames.erase(iter2 + i);
- _message = names[i] + " разбанен.";
- ui->listWidget->addItem(_message);
- userFindet = true;
- }
- }
- if(userFindet)
- noCommand = false;
- else
- {
- ui->listWidget->addItem("Пользователь не найден.");
- return "error";
- }
- return _message;
- }
- void serv::list(QTcpSocket* soket = nullptr)
- {
- QString listUsers = "Список пользователей:\n";
- for(QString name : names)
- listUsers.push_back(name + "\n");
- if(soket == nullptr)
- {
- ui->listWidget->addItem(listUsers);
- return;
- }
- else
- soket->write(f_message(2,listUsers));
- }
- void serv::on_pushButton_clicked()
- {
- noCommand = true;
- QString _txt = ui->textEdit->toPlainText();
- QString _message = myName + ": " + _txt;
- if(_txt == "/help")
- help();
- else if(_txt == "/copyip")
- {
- if(!_ip.isNull())
- {
- QClipboard *clip = QApplication::clipboard();
- clip->setText(_ip.toString());
- ui->listWidget->addItem("Скопировано!");
- return;
- }
- else
- {
- ui->listWidget->addItem("Ошибка!");
- return;
- }
- }
- else if (_txt.mid(0,4) == "/ban")
- {
- _message = ban(_txt);
- if(_message == "error")
- return;
- }
- else if (_txt.mid(0,5) == "/kick")
- {
- _message = kick(_txt);
- if(_message == "error")
- return;
- }
- else if(_txt == "/list")
- {
- ui->listWidget->addItem("Список пользователей:");
- for(QString name : names)
- ui->listWidget->addItem(name);
- return;
- }
- else if(_txt.mid(0,6) == "/unban")
- {
- _message = unban(_txt);
- if(_message == "error")
- return;
- }
- if(shif(_message) == _message)
- return;
- if(_message == myName + ": ")
- return;
- ui->textEdit->clear();
- QString message = _message;
- bool norm = true;
- if(_message.size() > 30)
- message.clear();
- for(int i=0;i<_message.size()/30 && _message.size() > 30;i++,norm = true)
- {
- if((_message.size() - message.size() + i) < 30)
- {
- message.push_back(_message.mid(i*30,_message.size() - (i * 30)));
- break;
- }
- for(int a=0;a<_message.mid(i,30).size();a++)
- {
- if(_message.mid(i*30,30)[a] == "\n")
- norm = false;
- }
- if(norm)
- {
- message.push_back(_message.mid(i*30,30) + "\n");
- }
- }
- for(QTcpSocket* _socket:sok)
- {
- _socket->write(f_message(2,message));
- }
- if(noCommand);
- ui->listWidget->addItem(message);
- }
- void serv::on_pushButton_2_clicked()
- {
- if(!(ui->lineEdit->text().isEmpty()))
- {
- if(shif(ui->lineEdit->text()) == ui->lineEdit->text())
- return;
- const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
- for (const QHostAddress &address: QNetworkInterface::allAddresses())
- {
- if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
- {
- _ip = address; break;
- }
- }
- _serv->listen();
- ui->listWidget->addItem( "Порт: " + QString::number(_serv->serverPort()) + "\nIP4: " + _ip.toString() );
- myName = ui->lineEdit->text();
- ui->pushButton_3->setEnabled(true);
- ui->pushButton_2->setEnabled(false);
- ui->lineEdit->setEnabled(false);
- }
- else
- {
- QMessageBox::critical(nullptr,"Ошибка","Укажите имя");
- return;
- }
- }
- void serv::on_pushButton_3_clicked()
- {
- ui->pushButton_3->setEnabled(false);
- ui->pushButton_2->setEnabled(true);
- ui->lineEdit->setEnabled(true);
- _serv->close();
- for(QTcpSocket *_soke:sok)
- _soke->write(f_message(3,"Сервер отключён."));
- sok.clear();
- names.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement