MCPcopy Create free account
hub / github.com/PolyMC/PolyMC / sendMessage

Method sendMessage

libraries/LocalPeer/src/LocalPeer.cpp:159–202  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

157
158
159bool LocalPeer::sendMessage(const QByteArray &message, int timeout)
160{
161 if (!isClient())
162 return false;
163
164 QLocalSocket socket;
165 bool connOk = false;
166 int tries = 2;
167 for(int i = 0; i < tries; i++) {
168 // Try twice, in case the other instance is just starting up
169 socket.connectToServer(socketName);
170 connOk = socket.waitForConnected(timeout/2);
171 if (!connOk && i < (tries - 1))
172 {
173 std::this_thread::sleep_for(std::chrono::milliseconds(250));
174 }
175 }
176 if (!connOk)
177 {
178 return false;
179 }
180
181 QByteArray uMsg(message);
182 QDataStream ds(&socket);
183
184 ds.writeBytes(uMsg.constData(), uMsg.size());
185 if(!socket.waitForBytesWritten(timeout))
186 {
187 return false;
188 }
189
190 // wait for 'ack'
191 if(!socket.waitForReadyRead(timeout))
192 {
193 return false;
194 }
195
196 // make sure we got 'ack'
197 if(!(socket.read(qstrlen(ack)) == ack))
198 {
199 return false;
200 }
201 return true;
202}
203
204
205void LocalPeer::receiveConnection()

Callers 1

ApplicationMethod · 0.80

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected