5_april
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 4.8.2, 2020-04-04T20:29:56. -->
|
||||
<!-- Written by QtCreator 4.8.2, 2020-04-05T14:15:18. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
@@ -11,8 +11,7 @@ ChatWidget::ChatWidget(QWidget *widget)
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ChatWidget::addMsgOldStyle(QString msg, const Peer *who, Qt::Alignment alignment, const QColor &color)
|
||||
void ChatWidget::addMsg(QString msg, Qt::Alignment alignment, const QColor &color, const Peer *colorByPeer)
|
||||
{
|
||||
if (msg.isEmpty())
|
||||
return;
|
||||
@@ -22,7 +21,7 @@ void ChatWidget::addMsgOldStyle(QString msg, const Peer *who, Qt::Alignment alig
|
||||
|
||||
//save old color + set new color
|
||||
QColor color_ = this->textColor();
|
||||
this->setTextColor( (color == Qt::white) ? getColorByPeer(who) : color );
|
||||
this->setTextColor( (colorByPeer) ? getColorByPeer(colorByPeer) : color );
|
||||
|
||||
//print msg + set aligment
|
||||
this->append(msg);
|
||||
@@ -31,51 +30,19 @@ void ChatWidget::addMsgOldStyle(QString msg, const Peer *who, Qt::Alignment alig
|
||||
//set old color + scroll down
|
||||
this->setTextColor(color_);
|
||||
this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
|
||||
|
||||
}
|
||||
|
||||
void ChatWidget::addMsg(QString msg, Qt::Alignment alignment, const QColor &color)
|
||||
void ChatWidget::addSenderNameAndMSG(const Peer *who, QString msg, QString cusomMsg)
|
||||
{
|
||||
if (msg.isEmpty())
|
||||
return;
|
||||
|
||||
//Wichtig, da sonst die ausgewählte stelle gefärbt und verschoben wird
|
||||
this->moveCursor(QTextCursor::End);
|
||||
|
||||
|
||||
//print msg + set aligment
|
||||
this->append(msg);
|
||||
this->setAlignment( alignment );
|
||||
|
||||
//set default QTextFormat
|
||||
this->moveCursor(QTextCursor::End);
|
||||
QTextCursor cursor(this->textCursor());
|
||||
cursor.select(QTextCursor::SelectionType::LineUnderCursor);
|
||||
cursor.setCharFormat(QTextCharFormat());
|
||||
|
||||
this->moveCursor(QTextCursor::End);
|
||||
moveCursor(QTextCursor::MoveOperation::Up);
|
||||
|
||||
//save old color + set new color
|
||||
QColor color_ = this->textColor();
|
||||
this->setTextColor( color );
|
||||
|
||||
moveCursor(QTextCursor::MoveOperation::End);
|
||||
|
||||
//set old color + scroll down
|
||||
this->setTextColor(color_);
|
||||
this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
|
||||
|
||||
}
|
||||
|
||||
void ChatWidget::addSenderName(const Peer *who, QString customText)
|
||||
{
|
||||
this->moveCursor(QTextCursor::End);
|
||||
///---->Name + Time
|
||||
QColor color_ = this->textColor();
|
||||
this->setTextColor( getColorByPeer(who) );
|
||||
|
||||
//print msg + set aligment
|
||||
this->append(QTime::currentTime().toString("hh:mm") + ((who) ? (" ~" + who->getName()) : " Du") + customText +":");
|
||||
this->append(QTime::currentTime().toString("hh:mm") + ((who) ? (" ~" + who->getName()) : " Du") + cusomMsg +":");
|
||||
this->setAlignment( (who) ? Qt::AlignLeft : Qt::AlignRight );
|
||||
|
||||
this->moveCursor(QTextCursor::End);
|
||||
@@ -88,22 +55,31 @@ void ChatWidget::addSenderName(const Peer *who, QString customText)
|
||||
|
||||
//set old color + scroll down
|
||||
this->setTextColor(color_);
|
||||
///<----
|
||||
|
||||
///---> Message
|
||||
//print msg + set aligment
|
||||
this->append(msg);
|
||||
|
||||
//set default QTextFormat
|
||||
this->moveCursor(QTextCursor::End);
|
||||
cursor.select(QTextCursor::SelectionType::LineUnderCursor);
|
||||
cursor.setCharFormat(QTextCharFormat());
|
||||
|
||||
///<----
|
||||
|
||||
this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
|
||||
}
|
||||
|
||||
|
||||
QColor ChatWidget::getColorByPeer(const Peer *peer)
|
||||
{
|
||||
if( ! colorMap.contains(peer) )
|
||||
setNewColorToPeer(peer);
|
||||
return colorMap[peer];
|
||||
return ( colorMap.contains(peer) ) ? colorMap[peer] : setNewColorToPeer(peer);
|
||||
}
|
||||
|
||||
void ChatWidget::setNewColorToPeer(const Peer *peer)
|
||||
QColor ChatWidget::setNewColorToPeer(const Peer *peer)
|
||||
{
|
||||
size_t ColorListLength = (sizeof(colorList)/sizeof(*colorList));
|
||||
colorMap.insert(peer, colorList[ ( colorCounter % ColorListLength ) ] );
|
||||
colorCounter++;
|
||||
return colorMap.insert(peer, colorList[ ( ( colorCounter ++ ) % (sizeof(colorList)/sizeof(*colorList)) ) ] ).value();
|
||||
}
|
||||
|
||||
|
||||
|
@@ -13,14 +13,13 @@ class ChatWidget : public QTextEdit
|
||||
Q_OBJECT
|
||||
public:
|
||||
ChatWidget(QWidget * widget);
|
||||
void addMsgOldStyle(QString msg, const Peer *who,Qt::Alignment alignment = Qt::AlignLeft, const QColor &color = Qt::white);
|
||||
void addMsg(QString msg, Qt::Alignment alignment = Qt::AlignLeft, const QColor &color = Qt::black);
|
||||
void addSenderName(const Peer *who, QString customText = "");
|
||||
void addMsg(QString msg, Qt::Alignment alignment = Qt::AlignLeft, const QColor &color = Qt::black, const Peer *colorByPeer = nullptr);
|
||||
void addSenderNameAndMSG(const Peer *who, QString msg, QString customText = "");
|
||||
|
||||
QColor getColorByPeer(const Peer *peer);
|
||||
|
||||
private:
|
||||
void setNewColorToPeer(const Peer *peer);
|
||||
QColor setNewColorToPeer(const Peer *peer);
|
||||
|
||||
const QColor colorList[14] = { Qt::black, Qt::blue, Qt::darkBlue, Qt::green, Qt::darkGreen,
|
||||
Qt::darkCyan, Qt::red, Qt::darkRed,
|
||||
|
@@ -17,9 +17,8 @@ ConnetionsManager::~ConnetionsManager()
|
||||
this->quit();
|
||||
if( ! this->wait(500) ) {
|
||||
this->terminate();
|
||||
if( ! this->wait(3000) ) {
|
||||
if( ! this->wait(3000) )
|
||||
std::cout << " Stop connectionmanager thread failed" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,24 +62,18 @@ int ConnetionsManager::start_Thread(bool PORT_BASED, std::vector<ConnetionsManag
|
||||
|
||||
void ConnetionsManager::run()
|
||||
{
|
||||
if(startServer() != 0) {
|
||||
emit showMSG("Fatal Error in startServer()");
|
||||
return;
|
||||
}
|
||||
//Start Server
|
||||
if(startServer() != 0)
|
||||
return emit showMSG("Fatal Error in startServer()");
|
||||
|
||||
if(startAllTryConnector() != 0) {
|
||||
emit showMSG("Fatal Error in startAllTryConnector()");
|
||||
return;
|
||||
}
|
||||
|
||||
//Nur noch accepten
|
||||
while ( ! stop ) {
|
||||
if(acceptClient() != 0) {
|
||||
emit showMSG("Fatal Error in acceptClient()");
|
||||
return;
|
||||
}
|
||||
}
|
||||
//TryConnnect to All Peers in List
|
||||
else if(startAllTryConnector() != 0)
|
||||
return emit showMSG("Fatal Error in startAllTryConnector()");
|
||||
|
||||
//Accept all new Peers
|
||||
while ( ! stop )
|
||||
if(acceptClient() != 0)
|
||||
return emit showMSG("Fatal Error in acceptClient()");
|
||||
}
|
||||
|
||||
int ConnetionsManager::startAllTryConnector()
|
||||
@@ -243,7 +236,7 @@ void ConnetionsManager::PeerConnectionGetsError(Peer *who)
|
||||
for (unsigned i = 0; i < Connections.size(); ++i) {
|
||||
if(Connections.at(i) == who && (++check)) {
|
||||
Connections.erase(Connections.begin() + i);
|
||||
std::cout << "DEBUG: Delete Peer, cause getError, with Port: " << who->getPort() << std::endl;
|
||||
//std::cout << "DEBUG: Delete Peer, cause getError, with Port: " << who->getPort() << std::endl;
|
||||
delete who;//->deleteLater();
|
||||
}
|
||||
}
|
||||
|
@@ -24,6 +24,8 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
pcs.push_back(ConnetionsManager::PC("CR01-PC" + std::string((i < 10) ? "0" : "") + std::to_string(i), 4000));
|
||||
}
|
||||
|
||||
pcs.push_back(ConnetionsManager::PC("home.obermui.de", 30356));
|
||||
|
||||
|
||||
if( ! manager->start_Thread(Port_Home_Version, pcs) )
|
||||
exit(-1);
|
||||
@@ -64,18 +66,13 @@ void MainWindow::recvedMSG(Peer *who, QString msg)
|
||||
wrongClientCountCounter++;
|
||||
if(timerForWarningMSG > 100 && (timerForWarningMSG = 0) == 0)
|
||||
if(wrongClientCountCounter > 10 && (wrongClientCountCounter = 0) == 0)
|
||||
ui->chat->addMsg(QString::fromStdString("<WARNUNG> Anzahl Verbindungen stimmt nicht überein...Bitte Starte das Programm neu."), Qt::AlignCenter, "orange" );
|
||||
|
||||
ui->chat->addMsg("<WARNUNG> Anzahl Verbindungen stimmt nicht überein...Bitte Starte das Programm neu.", Qt::AlignCenter, "orange" );
|
||||
|
||||
} else if(what == "MSG") {
|
||||
//ui->chat->append(QString::fromStdString("<" + who->name + ">: " + value) );
|
||||
//std::cout << "Recied MSG: " << value << std::endl;
|
||||
if(oldPrintStyle)
|
||||
ui->chat->addMsgOldStyle( who->getFullName() + " " + QString::fromStdString(value), who );
|
||||
else {
|
||||
ui->chat->addSenderName(who);
|
||||
ui->chat->addMsg(QString::fromStdString(value));
|
||||
}
|
||||
ui->chat->addMsg( who->getFullName() + " " + QString::fromStdString(value), Qt::AlignLeft, nullptr, who );
|
||||
else
|
||||
ui->chat->addSenderNameAndMSG(who, QString::fromStdString(value));
|
||||
|
||||
} else if(what == "JOINED") {
|
||||
who->setName(value);
|
||||
@@ -91,11 +88,9 @@ void MainWindow::recvedMSG(Peer *who, QString msg)
|
||||
|
||||
} else if(what == "PRIVATE_MSG") {
|
||||
if(oldPrintStyle)
|
||||
ui->chat->addMsgOldStyle( "</MSG> " + who->getFullName() + QString::fromStdString(value), who );
|
||||
else {
|
||||
ui->chat->addSenderName(who, " > flüstert dir zu");
|
||||
ui->chat->addMsg(QString::fromStdString(value), Qt::AlignLeft, Qt::darkGray);
|
||||
}
|
||||
ui->chat->addMsg( "</MSG> " + who->getFullName() + QString::fromStdString(value), Qt::AlignLeft, nullptr, who);
|
||||
else
|
||||
ui->chat->addSenderNameAndMSG(who, QString::fromStdString(value)," > flüstert dir zu");
|
||||
|
||||
} else if(what == "JOIN_TIME") {
|
||||
who->setJoinTime(value);
|
||||
@@ -104,13 +99,19 @@ void MainWindow::recvedMSG(Peer *who, QString msg)
|
||||
ui->chat->addMsg(QString::fromStdString("<DEBUG>: " + value), Qt::AlignCenter, "gray" );
|
||||
|
||||
} else if(what == "VERSION") {
|
||||
if(value != VERSION) {
|
||||
if( stoi(value) != VERSION ) {
|
||||
ui->chat->addMsg("<ERROR> Falsche Programmversion: Client " + who->getFullName() + ": '" +
|
||||
QString::fromStdString( value + "' ungleich eigener: '" + VERSION + "'"), Qt::AlignCenter, "orange" );
|
||||
who->send_to("=" ); // SOLANGE KEIN PING PONG => UPDATE TCP
|
||||
if( who->closeSocket() != 0)
|
||||
perror("Closesocket failed.");
|
||||
return;
|
||||
QString::fromStdString( value + "' ungleich eigener: '" + std::to_string(VERSION) + "'"), Qt::AlignCenter, "red" );
|
||||
|
||||
if(stoi(value) < VERSION) {
|
||||
std::cout << " -> Selber höhere Version => Closesocket to " << who->getFullName().toStdString() << std::endl;
|
||||
if(who->isConnected()) {
|
||||
if(who->closeSocket() != 0)
|
||||
perror("Closesocket failed.");
|
||||
} else
|
||||
perror("couldn't Closesocket from wrong version client: not connected");
|
||||
} else
|
||||
std::cout << " -> Selber niedrigere Version => Wait for Closesocket from " << who->getFullName().toStdString() << std::endl;
|
||||
}
|
||||
|
||||
} else if(what == "") {
|
||||
@@ -151,18 +152,16 @@ void MainWindow::on_inputLine_returnPressed()
|
||||
+ manager->getConnectionList().at(i)->getJoinTime() + " VectorIndex: " + QString::number(i));
|
||||
}
|
||||
|
||||
} else if (line.startsWith("/-1", Qt::CaseInsensitive)) {
|
||||
if(manager->getConnectionList().size())
|
||||
this->manager->getConnectionList().at(0)->closeSocket();
|
||||
} else if (line.startsWith("/-", Qt::CaseInsensitive)) {
|
||||
|
||||
|
||||
} else if (line.startsWith("/msg <", Qt::CaseInsensitive)) {
|
||||
line.remove(0, 5);
|
||||
|
||||
int pos = line.indexOf("> ");
|
||||
if(pos == -1) {
|
||||
ui->chat->addMsg("<Console>: Error: Ungültiger Client: '" + line + "'", Qt::AlignCenter, "red");
|
||||
return;
|
||||
}
|
||||
if(pos == -1)
|
||||
return ui->chat->addMsg("<Console>: Error: Ungültiger Client: '" + line + "'", Qt::AlignCenter, "red");
|
||||
|
||||
|
||||
QString recver = line.left(pos + 1);
|
||||
line.remove(0, pos + 2);
|
||||
@@ -174,11 +173,8 @@ void MainWindow::on_inputLine_returnPressed()
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
if( !ok ) {
|
||||
ui->chat->addMsg("<Console>: Error: Unbekannter Client: '" + recver + "'", Qt::AlignCenter, "red");
|
||||
return;
|
||||
}
|
||||
|
||||
if( !ok )
|
||||
return ui->chat->addMsg("<Console>: Error: Unbekannter Client: '" + recver + "'", Qt::AlignCenter, "red");
|
||||
|
||||
} else if (line.startsWith("/setPrintStyle ", Qt::CaseInsensitive)) {
|
||||
if(line.length() > 15) {
|
||||
@@ -187,16 +183,16 @@ void MainWindow::on_inputLine_returnPressed()
|
||||
else if (line[15] == '2')
|
||||
oldPrintStyle = false;
|
||||
else
|
||||
ui->chat->addMsg("<Console>: Error: Unbekannter StyleSheet: " + QString(line.at(15)), Qt::AlignCenter, Qt::red);
|
||||
ui->chat->addMsg("<Console>: Error: Unbekannter StyleSheet: " + QString(line.at(15)), Qt::AlignCenter, "red");
|
||||
} else
|
||||
ui->chat->addMsg("<Console>: Error: StyleSheet ID fehlt.", Qt::AlignCenter, Qt::red);
|
||||
ui->chat->addMsg("<Console>: Error: StyleSheet ID fehlt.", Qt::AlignCenter, "red");
|
||||
|
||||
} else if (line.startsWith("/help", Qt::CaseInsensitive)) {
|
||||
ui->chat->addMsg("Liste:\t/help\t\t-> Gibt diese Liste aus.");
|
||||
ui->chat->addMsg("\t/clear\t\t-> Löscht den Chat (nur bei dir).");
|
||||
ui->chat->addMsg("\t/msg <Client> <msg>\t-> Privatnachricht an diesen Client");
|
||||
ui->chat->addMsg("\t\t\t Tip: Doppelklick auf den Namen rechts in der Liste.");
|
||||
ui->chat->addMsg("\t/setPrintStyle <1/2>\t -> AusgabeAussehen: Style 1 oder 2.");
|
||||
ui->chat->addMsg("\t/setPrintStyle <1/2>\t-> AusgabeAussehen: Style 1 oder 2.");
|
||||
|
||||
|
||||
} else {
|
||||
@@ -206,18 +202,14 @@ void MainWindow::on_inputLine_returnPressed()
|
||||
|
||||
} else {
|
||||
if(oldPrintStyle)
|
||||
ui->chat->addMsgOldStyle("<ICH>: " + line, nullptr, Qt::AlignRight );
|
||||
else {
|
||||
ui->chat->addSenderName(nullptr);
|
||||
ui->chat->addMsg(line, Qt::AlignRight);
|
||||
}
|
||||
ui->chat->addMsg("<ICH>: " + line, Qt::AlignRight );
|
||||
else
|
||||
ui->chat->addSenderNameAndMSG(nullptr, line);
|
||||
//--------------> SEND MESSAGES
|
||||
manager->sendtoAllPeers("MSG=" + line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void MainWindow::on_Connections_itemDoubleClicked(QListWidgetItem *item)
|
||||
{
|
||||
//std::cout << "Item gedoppelklickt: "<<item->text().toStdString() << std::endl;
|
||||
|
114
peer.cpp
114
peer.cpp
@@ -5,6 +5,9 @@ Peer::Peer(const CLIENT &cli)
|
||||
{
|
||||
this->cli_v1.autoCleanUpInTheEnd = false;
|
||||
this->cli_v2.autoCleanUpInTheEnd = false;
|
||||
//Save ip in var, which will be deletet after disconnect...
|
||||
this->ip_connectedTo = cli.getIpAddress();
|
||||
|
||||
}
|
||||
|
||||
Peer::Peer(const client_TCP_Lib &cli, std::string ip_connectedTo, unsigned short port_connectedTo)
|
||||
@@ -22,56 +25,26 @@ Peer::~Peer()
|
||||
this->quit();
|
||||
if( ! this->wait(500) ) {
|
||||
this->terminate();
|
||||
if( ! this->wait(3000) ) {
|
||||
if( ! this->wait(3000) )
|
||||
std::cout << "Terminating Thread timeouted: " << __func__ << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(this->isConnected())
|
||||
if( this->closeSocket() != 0)
|
||||
std::cout << "!-> CloseSocket failed: " << cli_v1.getLastError() << std::endl;
|
||||
if( this->isConnected() && this->closeSocket() != 0)
|
||||
std::cout << "!-> CloseSocket failed: " << cli_v1.getLastError() << std::endl;
|
||||
}
|
||||
|
||||
void Peer::send_to(QString msg)
|
||||
{
|
||||
msg.push_back('|');
|
||||
|
||||
if(isCLIENT) {
|
||||
//std::cout << "send to socket: " << cli_v1.getSocket() << std::endl;
|
||||
if( cli_v1.send_(msg.toStdString().c_str(), static_cast<unsigned>(msg.toStdString().length()) ) <= 0) {
|
||||
std::cout << "emit -> sned failed" << std::endl;
|
||||
|
||||
emit sendFailed(this);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//std::cout << "send to socket: " << cli_v2.getConnectionSocket() << std::endl;
|
||||
if( send(cli_v2.getConnectionSocket(), msg.toStdString().c_str(), static_cast<unsigned>(msg.toStdString().length()), 0) <= 0) {
|
||||
std::cout << "emit -> sned failed" << std::endl;
|
||||
|
||||
emit sendFailed(this);
|
||||
return;
|
||||
}
|
||||
if( ((isCLIENT) ? cli_v1.send_(msg.toStdString().c_str(), static_cast<unsigned>(msg.toStdString().length()) ) :
|
||||
send(cli_v2.getConnectionSocket(), msg.toStdString().c_str(), static_cast<unsigned>(msg.toStdString().length()), 0)) <= 0 ) {
|
||||
std::cerr << "DEBUG: emit -> send failed to: " << this->getFullName().toStdString() << std::endl;
|
||||
emit sendFailed(this);
|
||||
}
|
||||
}
|
||||
|
||||
int Peer::closeSocket()
|
||||
{
|
||||
if(isCLIENT)
|
||||
return cli_v1.closeSocket();
|
||||
else
|
||||
return cli_v2.closeSocket();
|
||||
}
|
||||
|
||||
bool Peer::isConnected()
|
||||
{
|
||||
if(isCLIENT)
|
||||
return cli_v1.isConnected();
|
||||
else
|
||||
return cli_v2.isConnected();
|
||||
}
|
||||
|
||||
int Peer::startReciver()
|
||||
{
|
||||
if(this->isRunning())
|
||||
@@ -83,39 +56,35 @@ int Peer::startReciver()
|
||||
void Peer::sendInfoData(const unsigned short ownServerListeningPort)
|
||||
{
|
||||
this->send_to("PORT=" + QString::number(ownServerListeningPort));
|
||||
this->send_to("VERSION=" + QString(VERSION) );
|
||||
this->send_to("JOIN_TIME=" + QTime::currentTime().toString() );
|
||||
|
||||
this->send_to("JOINED=" + getUserName() );
|
||||
this->send_to("VERSION=" + QString::number(VERSION) );
|
||||
}
|
||||
|
||||
int Peer::closeSocket()
|
||||
{
|
||||
return (isCLIENT) ? cli_v1.closeSocket() : cli_v2.closeSocket();
|
||||
}
|
||||
|
||||
bool Peer::isConnected()
|
||||
{
|
||||
return (isCLIENT) ? cli_v1.isConnected() : cli_v2.isConnected();
|
||||
}
|
||||
|
||||
std::string Peer::getIp() const
|
||||
{
|
||||
if(isCLIENT) {
|
||||
return cli_v1.getIpAddress();
|
||||
} else {
|
||||
return ip_connectedTo;
|
||||
}
|
||||
return ip_connectedTo;
|
||||
}
|
||||
|
||||
|
||||
std::string Peer::getLastError()
|
||||
{
|
||||
if(isCLIENT) {
|
||||
return cli_v1.getLastError();
|
||||
} else {
|
||||
return cli_v2.getLastError();
|
||||
}
|
||||
return (isCLIENT) ? cli_v1.getLastError() : cli_v2.getLastError();
|
||||
}
|
||||
|
||||
unsigned short Peer::getPort() const
|
||||
{
|
||||
if(isCLIENT) {
|
||||
return port_connectedTo;
|
||||
} else {
|
||||
return port_connectedTo;
|
||||
}
|
||||
return port_connectedTo;
|
||||
}
|
||||
|
||||
QString Peer::getName() const
|
||||
@@ -155,39 +124,30 @@ void Peer::setJoinTime(std::string time)
|
||||
|
||||
void Peer::run()
|
||||
{
|
||||
//emit recvdMessage(this, "DEBUG=Start ReciveThread: From: " + QString::fromStdString( this->getIp() )+ ":" + QString::number( this->getPort() ) );
|
||||
std::vector<char> buffer;
|
||||
std::string stringBuffer;
|
||||
char c;
|
||||
|
||||
while ( ! stop ) {
|
||||
buffer.clear();
|
||||
|
||||
ssize_t recvedbytes;
|
||||
char c;
|
||||
do {
|
||||
if( isCLIENT )
|
||||
recvedbytes = cli_v1.recv_(&c, 1);
|
||||
else
|
||||
recvedbytes = recv(cli_v2.getConnectionSocket(), &c, 1, 0);
|
||||
|
||||
if(recvedbytes <= 0) {
|
||||
std::cout << "emit -> rev failed" << std::endl;
|
||||
emit recvdFailed(this);
|
||||
return;
|
||||
if( ( ( isCLIENT ) ? cli_v1.recv_(&c, 1) : recv(cli_v2.getConnectionSocket(), &c, 1, 0) ) <= 0) {
|
||||
std::cerr << "DEBUG: emit -> rev failed from: " << this->getFullName().toStdString() << std::endl;
|
||||
return emit recvdFailed(this);
|
||||
} else
|
||||
buffer.push_back(c);
|
||||
} while (c != '|' && ! stop);
|
||||
|
||||
} while (c != '|');
|
||||
|
||||
if(buffer.size() > 0)
|
||||
if(buffer.data() && buffer.size() > 0) {
|
||||
buffer.pop_back();
|
||||
buffer.push_back('\0');
|
||||
buffer.push_back('\0');
|
||||
|
||||
std::string stringBuffer;
|
||||
if(!buffer.data() || (stringBuffer = std::string(buffer.data())) .find("=") == std::string::npos) {
|
||||
std::cout << "INVALID MSG: '" << buffer.data() << "'" << std::endl;
|
||||
continue;
|
||||
if( (stringBuffer = std::string(buffer.data())) .find("=") == std::string::npos) {
|
||||
std::cout << "INVALID MSG: '" << buffer.data() << "'" << std::endl;
|
||||
continue;
|
||||
} else
|
||||
emit recvdMessage(this, QString::fromStdString(stringBuffer));
|
||||
}
|
||||
|
||||
emit recvdMessage(this, QString::fromStdString(stringBuffer));
|
||||
}
|
||||
emit recvdFailed(this);
|
||||
}
|
||||
|
2
peer.h
2
peer.h
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <QTime>
|
||||
#include <QThread>
|
||||
#define VERSION "2.2"
|
||||
#define VERSION 1
|
||||
|
||||
|
||||
class Peer : public QThread
|
||||
|
@@ -39,6 +39,9 @@ void Connector::run()
|
||||
}
|
||||
|
||||
|
||||
///----------------------------------------------------------------------------------------->
|
||||
|
||||
|
||||
TryConnector::TryConnector(void)
|
||||
: state(UNDEFINED)
|
||||
{
|
||||
@@ -52,9 +55,8 @@ TryConnector::~TryConnector()
|
||||
this->quit();
|
||||
if( ! this->wait(500) ) {
|
||||
this->terminate();
|
||||
if( ! this->wait(3000) ) {
|
||||
if( ! this->wait(3000) )
|
||||
std::cout << "Terminating Thread timeouted: " << __func__ << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -72,13 +74,25 @@ int TryConnector::tryConnectTo(std::string ip, unsigned short port)
|
||||
return this->isRunning() ? 0 : 1;
|
||||
}
|
||||
|
||||
const client_TCP_Lib &TryConnector::client() { return connector.client; }
|
||||
const client_TCP_Lib &TryConnector::client()
|
||||
{
|
||||
return connector.client;
|
||||
}
|
||||
|
||||
const STATE &TryConnector::getState() { return this->state; }
|
||||
const STATE &TryConnector::getState()
|
||||
{
|
||||
return this->state;
|
||||
}
|
||||
|
||||
const std::string &TryConnector::getIp() { return connector.ip; }
|
||||
const std::string &TryConnector::getIp()
|
||||
{
|
||||
return connector.ip;
|
||||
}
|
||||
|
||||
const unsigned short &TryConnector::getPort() { return connector.port; }
|
||||
const unsigned short &TryConnector::getPort()
|
||||
{
|
||||
return connector.port;
|
||||
}
|
||||
|
||||
|
||||
void TryConnector::run()
|
||||
|
Reference in New Issue
Block a user