86 lines
2.3 KiB
C++
86 lines
2.3 KiB
C++
#include "clientconnectionmanager.h"
|
|
|
|
ClientConnectionManager::ClientConnectionManager(ParameterData pD)
|
|
: pD(pD)
|
|
{
|
|
qDebug("+ClientConnectionManager");
|
|
}
|
|
|
|
ClientConnectionManager::~ClientConnectionManager()
|
|
{
|
|
if(this->isRunning()) {
|
|
this->quit();
|
|
if( ! this->wait(500) ) {
|
|
this->terminate();
|
|
if( ! this->wait(3000) )
|
|
return;//std::cout << "Terminating Thread timeouted: " << __func__ << std::endl;
|
|
}
|
|
}
|
|
qDebug("~ClientConnectionManager");
|
|
}
|
|
|
|
int ClientConnectionManager::startThread()
|
|
{
|
|
if(this->isRunning()) return -1;
|
|
this->start();
|
|
return (this->isRunning()) ? 0 : 1;
|
|
}
|
|
|
|
//Start server on port x
|
|
//connect to <Onelineserver>
|
|
//check if name exist
|
|
//check if max connected are reched
|
|
|
|
//Listen on own server
|
|
//for each incomming connection:
|
|
//start Redirect Threadss
|
|
|
|
void ClientConnectionManager::run()
|
|
{
|
|
std::cout << "run" << std::endl;
|
|
|
|
TCP_SERVER server;
|
|
if( server.startListening(pD.localServerPort) != 0) {
|
|
//error
|
|
std::cout << "start server failed:" << server.getLastError() << std::endl;
|
|
return;
|
|
}
|
|
|
|
while (true) {
|
|
CLIENT * newCli = new CLIENT;
|
|
client_TCP_Lib * newCli2 = new client_TCP_Lib;
|
|
|
|
if( server.acceptClient(*newCli) != 0) {
|
|
//error
|
|
std::cout << "accept failed:" << server.getLastError() << std::endl;
|
|
return;
|
|
}
|
|
|
|
|
|
newCli2->connectTo(pD.RedirectServerIp.toStdString(), pD.RedirectServerPort);
|
|
std::string buffer = ("TYPE=FROM_USER_CONNECTION|SERVERID="+ pD.serverID + "|PASSWORD="+ pD.password+"|FINIDHED=true|").toStdString();
|
|
write(newCli2->getConnectionSocket(), buffer.c_str(), buffer.length() );
|
|
|
|
std::string msg;
|
|
while (msg.find("FINISHED=true|") == std::string::npos) {
|
|
char c;
|
|
read(newCli2->getConnectionSocket(), &c, 1);
|
|
msg.push_back(c);
|
|
}
|
|
std::cout << "Recived: " << msg << std::endl;
|
|
|
|
ForwardSender * redirecting = new ForwardSender[2];
|
|
|
|
redirecting[0].startRedirecting(newCli->getSocket(), newCli2->getConnectionSocket(), 0, true);
|
|
redirecting[1].startRedirecting(newCli2->getConnectionSocket(), newCli->getSocket(), 0, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|