summaryrefslogtreecommitdiff
path: root/i/simulotron/src/gs/gs_transmitter.cc
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron/src/gs/gs_transmitter.cc')
-rw-r--r--i/simulotron/src/gs/gs_transmitter.cc72
1 files changed, 46 insertions, 26 deletions
diff --git a/i/simulotron/src/gs/gs_transmitter.cc b/i/simulotron/src/gs/gs_transmitter.cc
index 9f53963..abdfd0f 100644
--- a/i/simulotron/src/gs/gs_transmitter.cc
+++ b/i/simulotron/src/gs/gs_transmitter.cc
@@ -40,40 +40,60 @@ GSTransmitter::GSTransmitter(const std::string & address, int port)
socket_.connect(address, port);
}
-/// Renvoie 0 quand un message est récupéré, -1 sinon
-int
-GSTransmitter::getGS(GSMessage & gsm)
+/// Renvoie true quand un message est récupéré, false sinon
+bool
+GSTransmitter::getGS(GSMessage & gsm, bool bloquant)
{
///\bug Pas portable le code, suppose des int à 4 octect
- fd_set fds;
- timeval tv = {0, 0};
unsigned int size = 0;
+ bool GSCompleted;
- FD_ZERO(&fds);
- FD_SET(socket_.getFD(), &fds);
- //On lit le socket que si y'a quelque chose à lire
- ///\bug C'est top ca?
- if(select(socket_.getFD() + 1, &fds, NULL, NULL, &tv) > 0)
+ do
{
- std::string strRead (socket_.read ());
+ GSCompleted = true;
+ std::string strRead;
+ socket_.read(strRead, bloquant);
strBrut_ += strRead;
- }
- // Si la chaine à moins de 4 octect, y'a pas de message
- if (strBrut_.size() < 4)
- return -1;
- // On récupère la taille de la gs grace au 4 premier octect
- size = static_cast<int> (strBrut_[0]) << 24
- | static_cast<int> (strBrut_[1]) << 16
- | static_cast<int> (strBrut_[2]) << 8
- | static_cast<int> (strBrut_[3]);
- if(size > strBrut_.size() - 4)
- return -1;
- gsm.appendGS (strBrut_.substr(4, size).data(), size);
- strBrut_.erase (0, size + 4);
- return 0;
+ // Si la chaine à moins de 4 octect, y'a pas de message
+ if (strBrut_.size() < 4)
+ {
+ if(!bloquant)
+ return false;
+ else
+ GSCompleted = false;
+ }
+ }while (!GSCompleted);
+ do
+ {
+ GSCompleted = true;
+
+ // On récupère la taille de la gs grace au 4 premier octect
+ size = static_cast<int> (strBrut_[0]) << 24
+ | static_cast<int> (strBrut_[1]) << 16
+ | static_cast<int> (strBrut_[2]) << 8
+ | static_cast<int> (strBrut_[3]);
+ if(size > strBrut_.size() - 4)
+ {
+ if(!bloquant)
+ return false;
+ else
+ GSCompleted = false;
+ }
+ else
+ {
+ gsm.clear();
+ gsm.appendGS (strBrut_.substr(4, size).data(), size);
+ strBrut_.erase (0, size + 4);
+ return true;
+ }
+ std::string strRead;
+ socket_.read(strRead, bloquant);
+ strBrut_ += strRead;
+ }while (!GSCompleted);
+ return true;
}
-void
+ void
GSTransmitter::putGS(const GSMessage & gsm)
{
///\bug Pas portable le code, suppose des int à 4 octect