aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--remote-process/main.cpp6
-rw-r--r--remote-processor/Message.cpp35
-rw-r--r--remote-processor/Message.h12
-rw-r--r--remote-processor/RemoteProcessorServer.cpp43
4 files changed, 61 insertions, 35 deletions
diff --git a/remote-process/main.cpp b/remote-process/main.cpp
index 85514ca..bcf825b 100644
--- a/remote-process/main.cpp
+++ b/remote-process/main.cpp
@@ -100,7 +100,8 @@ bool sendAndDisplayCommand(CConnectionSocket &connectionSocket, CRequestMessage
{
string strError;
- if (!requestMessage.serialize(&connectionSocket, true, strError)) {
+ if (requestMessage.serialize(&connectionSocket, true, strError)
+ != CRequestMessage::success) {
cerr << "Unable to send command to target: " << strError << endl;
return false;
@@ -108,7 +109,8 @@ bool sendAndDisplayCommand(CConnectionSocket &connectionSocket, CRequestMessage
///// Get answer
CAnswerMessage answerMessage;
- if (!answerMessage.serialize(&connectionSocket, false, strError)) {
+ if (answerMessage.serialize(&connectionSocket, false, strError)
+ != CRequestMessage::success) {
cerr << "Unable to received answer from target: " << strError << endl;
return false;
diff --git a/remote-processor/Message.cpp b/remote-processor/Message.cpp
index db24d80..8591847 100644
--- a/remote-processor/Message.cpp
+++ b/remote-processor/Message.cpp
@@ -124,7 +124,7 @@ uint32_t CMessage::getRemainingDataSize() const
}
// Send/Receive
-bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
+CMessage::Result CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
{
if (bOut) {
@@ -142,8 +142,10 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->write(&uiSyncWord, sizeof(uiSyncWord))) {
- strError += string("Sync write failed: ") + strerror(errno);
- return false;
+ if (pSocket->hasPeerDisconnected()) {
+ return peerDisconnected;
+ }
+ return error;
}
// Size
@@ -152,21 +154,21 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->write(&uiSize, sizeof(uiSize))) {
strError += string("Size write failed: ") + strerror(errno);
- return false;
+ return error;
}
// Msg Id
if (!pSocket->write(&_ucMsgId, sizeof(_ucMsgId))) {
strError += string("Msg write failed: ") + strerror(errno);
- return false;
+ return error;
}
// Data
if (!pSocket->write(_pucData, _uiDataSize)) {
strError = string("Data write failed: ") + strerror(errno);
- return false;
+ return error;
}
// Checksum
@@ -175,7 +177,7 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->write(&ucChecksum, sizeof(ucChecksum))) {
strError = string("Checksum write failed: ") + strerror(errno);
- return false;
+ return error;
}
} else {
@@ -185,14 +187,17 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->read(&uiSyncWord, sizeof(uiSyncWord))) {
strError = string("Sync read failed: ") + strerror(errno);
- return false;
+ if (pSocket->hasPeerDisconnected()) {
+ return peerDisconnected;
+ }
+ return error;
}
// Check Sync word
if (uiSyncWord != SYNC_WORD) {
strError = "Sync word incorrect";
- return false;
+ return error;
}
// Size
@@ -201,14 +206,14 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->read(&uiSize, sizeof(uiSize))) {
strError = string("Size read failed: ") + strerror(errno);
- return false;
+ return error;
}
// Msg Id
if (!pSocket->read(&_ucMsgId, sizeof(_ucMsgId))) {
strError = string("Msg id read failed: ") + strerror(errno);
- return false;
+ return error;
}
// Data
@@ -220,7 +225,7 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->read(_pucData, _uiDataSize)) {
strError = string("Data read failed: ") + strerror(errno);
- return false;
+ return error;
}
// Checksum
@@ -229,20 +234,20 @@ bool CMessage::serialize(CSocket* pSocket, bool bOut, string& strError)
if (!pSocket->read(&ucChecksum, sizeof(ucChecksum))) {
strError = string("Checksum read failed: ") + strerror(errno);
- return false;
+ return error;
}
// Compare
if (ucChecksum != computeChecksum()) {
strError = "Received checksum != computed checksum";
- return false;
+ return error;
}
// Collect data in derived
collectReceivedData();
}
- return true;
+ return success;
}
// Checksum
diff --git a/remote-processor/Message.h b/remote-processor/Message.h
index 4f22977..feafc83 100644
--- a/remote-processor/Message.h
+++ b/remote-processor/Message.h
@@ -43,6 +43,12 @@ public:
CMessage();
virtual ~CMessage();
+ enum Result {
+ success,
+ peerDisconnected,
+ error
+ };
+
/** Write or read the message on pSocket.
*
* @param[in,out] pSocket is the socket on wich IO operation will be made.
@@ -51,9 +57,11 @@ public:
* @param[out] strError on failure, a string explaining the error,
* on success, undefined.
*
- * @return true on success, false on failure.
+ * @return success if a correct message could be recv/send
+ * peerDisconnected if the peer disconnected before the first socket access.
+ * error if the message could not be read/write for any other reason
*/
- bool serialize(CSocket* pSocket, bool bOut, std::string &strError);
+ Result serialize(CSocket* pSocket, bool bOut, std::string &strError);
protected:
// Msg Id
diff --git a/remote-processor/RemoteProcessorServer.cpp b/remote-processor/RemoteProcessorServer.cpp
index 487379e..8c66109 100644
--- a/remote-processor/RemoteProcessorServer.cpp
+++ b/remote-processor/RemoteProcessorServer.cpp
@@ -30,6 +30,7 @@
#include "RemoteProcessorServer.h"
#include "ListeningSocket.h"
#include <iostream>
+#include <memory>
#include <assert.h>
#include <poll.h>
#include <unistd.h>
@@ -147,15 +148,15 @@ void CRemoteProcessorServer::run()
// New connection
void CRemoteProcessorServer::handleNewConnection()
{
- CSocket* pClientSocket = _pListeningSocket->accept();
+ const auto_ptr<CSocket> clientSocket(_pListeningSocket->accept());
- if (!pClientSocket) {
+ if (clientSocket.get() == NULL) {
return;
}
// Set timeout
- pClientSocket->setTimeout(5000);
+ clientSocket->setTimeout(5000);
// Process all incoming requests from the client
while (true) {
@@ -166,12 +167,18 @@ void CRemoteProcessorServer::handleNewConnection()
string strError;
///// Receive command
- if (!requestMessage.serialize(pClientSocket, false, strError)) {
-
- if (!pClientSocket->hasPeerDisconnected()) {
- cout << "Error while receiving message: " << strError << endl;
- }
- break;
+ CRequestMessage::Result res;
+ res = requestMessage.serialize(clientSocket.get(), false, strError);
+
+ switch (res) {
+ case CRequestMessage::error:
+ cout << "Error while receiving message: " << strError << endl;
+ // fall through
+ case CRequestMessage::peerDisconnected:
+ // Consider peer disconnection as normal, no log
+ return; // Bail out
+ case CRequestMessage::success:
+ break; // No error, continue
}
// Actually process the request
@@ -195,13 +202,17 @@ void CRemoteProcessorServer::handleNewConnection()
CAnswerMessage answerMessage(strResult, bSuccess);
///// Send answer
- if (!answerMessage.serialize(pClientSocket, true, strError)) {
-
- // Bail out
- cout << "Error while sending message: " << strError << endl;
- break;
+ res = answerMessage.serialize(clientSocket.get(), true, strError);
+
+ switch (res) {
+ case CRequestMessage::peerDisconnected:
+ // Peer should not disconnect while waiting for an answer
+ // Fall through to log the error and bail out
+ case CRequestMessage::error:
+ cout << "Error while receiving message: " << strError << endl;
+ return; // Bail out
+ case CRequestMessage::success:
+ break; // No error, continue
}
}
- // Remove client socket
- delete pClientSocket;
}