32 TCPBase::~TCPBase() |
32 TCPBase::~TCPBase() |
33 { |
33 { |
34 } |
34 } |
35 |
35 |
36 TCPBase::TCPBase(bool demoMode) : |
36 TCPBase::TCPBase(bool demoMode) : |
37 m_isDemoMode(demoMode), |
37 m_isDemoMode(demoMode), |
38 IPCSocket(0) |
38 IPCSocket(0) |
39 { |
39 { |
40 if(!IPCServer) { |
40 if(!IPCServer) |
41 IPCServer = new QTcpServer(0); |
41 { |
42 IPCServer->setMaxPendingConnections(1); |
42 IPCServer = new QTcpServer(0); |
43 if (!IPCServer->listen(QHostAddress::LocalHost)) { |
43 IPCServer->setMaxPendingConnections(1); |
44 QMessageBox::critical(0, tr("Error"), |
44 if (!IPCServer->listen(QHostAddress::LocalHost)) |
45 tr("Unable to start the server: %1.") |
45 { |
46 .arg(IPCServer->errorString())); |
46 QMessageBox::critical(0, tr("Error"), |
47 exit(0); // FIXME - should be graceful exit here |
47 tr("Unable to start the server: %1.") |
|
48 .arg(IPCServer->errorString())); |
|
49 exit(0); // FIXME - should be graceful exit here |
|
50 } |
48 } |
51 } |
49 } |
52 ipc_port=IPCServer->serverPort(); |
50 ipc_port=IPCServer->serverPort(); |
|
51 } |
53 } |
52 |
54 |
53 void TCPBase::NewConnection() |
55 void TCPBase::NewConnection() |
54 { |
56 { |
55 if(IPCSocket) { |
57 if(IPCSocket) |
56 // connection should be already finished |
58 { |
57 return; |
59 // connection should be already finished |
58 } |
60 return; |
59 disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
61 } |
60 IPCSocket = IPCServer->nextPendingConnection(); |
62 disconnect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
61 if(!IPCSocket) return; |
63 IPCSocket = IPCServer->nextPendingConnection(); |
62 connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
64 if(!IPCSocket) return; |
63 connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
65 connect(IPCSocket, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
64 SendToClientFirst(); |
66 connect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
67 SendToClientFirst(); |
65 } |
68 } |
66 |
69 |
67 void TCPBase::RealStart() |
70 void TCPBase::RealStart() |
68 { |
71 { |
69 connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
72 connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
70 IPCSocket = 0; |
73 IPCSocket = 0; |
71 |
74 |
72 QProcess * process; |
75 QProcess * process; |
73 process = new QProcess; |
76 process = new QProcess; |
74 connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
77 connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
75 QStringList arguments=getArguments(); |
78 QStringList arguments=getArguments(); |
76 |
79 |
77 // redirect everything written on stdout/stderr |
80 // redirect everything written on stdout/stderr |
78 if(isDevBuild) |
81 if(isDevBuild) |
79 process->setProcessChannelMode(QProcess::ForwardedChannels); |
82 process->setProcessChannelMode(QProcess::ForwardedChannels); |
80 process->start(bindir->absolutePath() + "/hwengine", arguments); |
83 process->start(bindir->absolutePath() + "/hwengine", arguments); |
81 } |
84 } |
82 |
85 |
83 void TCPBase::ClientDisconnect() |
86 void TCPBase::ClientDisconnect() |
84 { |
87 { |
85 disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
88 disconnect(IPCSocket, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
86 onClientDisconnect(); |
89 onClientDisconnect(); |
87 |
90 |
88 if(srvsList.size()==1) srvsList.pop_front(); |
91 if(srvsList.size()==1) srvsList.pop_front(); |
89 emit isReadyNow(); |
92 emit isReadyNow(); |
90 IPCSocket->deleteLater(); |
93 IPCSocket->deleteLater(); |
91 deleteLater(); |
94 deleteLater(); |
92 } |
95 } |
93 |
96 |
94 void TCPBase::ClientRead() |
97 void TCPBase::ClientRead() |
95 { |
98 { |
96 QByteArray readed=IPCSocket->readAll(); |
99 QByteArray readed=IPCSocket->readAll(); |
97 if(readed.isEmpty()) return; |
100 if(readed.isEmpty()) return; |
98 readbuffer.append(readed); |
101 readbuffer.append(readed); |
99 onClientRead(); |
102 onClientRead(); |
100 } |
103 } |
101 |
104 |
102 void TCPBase::StartProcessError(QProcess::ProcessError error) |
105 void TCPBase::StartProcessError(QProcess::ProcessError error) |
103 { |
106 { |
104 QMessageBox::critical(0, tr("Error"), |
107 QMessageBox::critical(0, tr("Error"), |
105 tr("Unable to run engine: %1 (") |
108 tr("Unable to run engine: %1 (") |
106 .arg(error) + bindir->absolutePath() + "/hwengine)"); |
109 .arg(error) + bindir->absolutePath() + "/hwengine)"); |
107 } |
110 } |
108 |
111 |
109 void TCPBase::tcpServerReady() |
112 void TCPBase::tcpServerReady() |
110 { |
113 { |
111 disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
114 disconnect(srvsList.takeFirst(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
112 |
115 |
113 RealStart(); |
116 RealStart(); |
114 } |
117 } |
115 |
118 |
116 void TCPBase::Start() |
119 void TCPBase::Start() |
117 { |
120 { |
118 if(srvsList.isEmpty()) { |
121 if(srvsList.isEmpty()) |
119 srvsList.push_back(this); |
122 { |
120 } else { |
123 srvsList.push_back(this); |
121 connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
124 } |
122 srvsList.push_back(this); |
125 else |
123 return; |
126 { |
124 } |
127 connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
128 srvsList.push_back(this); |
|
129 return; |
|
130 } |
125 |
131 |
126 RealStart(); |
132 RealStart(); |
127 } |
133 } |
128 |
134 |
129 void TCPBase::onClientRead() |
135 void TCPBase::onClientRead() |
130 { |
136 { |
131 } |
137 } |