summaryrefslogtreecommitdiff
path: root/i/simulotron
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron')
-rw-r--r--i/simulotron/src/gs/gs_transmitter.cc25
-rw-r--r--i/simulotron/src/socket/socket_client.cc6
-rw-r--r--i/simulotron/src/socket/socket_client.hh2
-rw-r--r--i/simulotron/src/socket/socket_server.cc6
-rw-r--r--i/simulotron/src/socket/socket_server.hh2
5 files changed, 35 insertions, 6 deletions
diff --git a/i/simulotron/src/gs/gs_transmitter.cc b/i/simulotron/src/gs/gs_transmitter.cc
index e9b6648..0a0fd44 100644
--- a/i/simulotron/src/gs/gs_transmitter.cc
+++ b/i/simulotron/src/gs/gs_transmitter.cc
@@ -27,6 +27,8 @@
#include "gs/gs_message.hh"
#include "socket/socket_client.hh"
+#include <sys/select.h>
+
GSTransmitter::GSTransmitter(SocketClient & socket)
:socket_(socket)
{
@@ -36,29 +38,40 @@ GSTransmitter::GSTransmitter(SocketClient & socket)
int
GSTransmitter::getGS(GSMessage & gsm)
{
- ///BUG Pas portable le code, suppose des int à 4 octect
+ ///\bug Pas portable le code, suppose des int à 4 octect
+ fd_set fds;
+ timeval tv = {0, 0};
unsigned int size = 0;
- std::string strRead (socket_.read ());
- strBrut_ += strRead;
+
+ 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)
+ {
+ std::string strRead (socket_.read ());
+ strBrut_ += strRead;
+ }
+ // 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;
- strRead = strBrut_.substr (4, size);
+ gsm.writeGS (strBrut_.substr(4, size).data(), size);
strBrut_.erase (0, size + 4);
- gsm.writeGS (strRead.data (), size);
return 0;
}
void
GSTransmitter::putGS(const GSMessage & gsm)
{
- ///BUG Pas portable le code, suppose des int à 4 octect
+ ///\bug Pas portable le code, suppose des int à 4 octect
std::string gs(gsm.getGS ());
char bsize[4];
int size = gs.size();
+ // On transfore la taille de la gs en 4 octect qui seront envoyé en premier
bsize[0] = static_cast<char> (size >> 24);
bsize[1] = static_cast<char> (size >> 16);
bsize[2] = static_cast<char> (size >> 8);
diff --git a/i/simulotron/src/socket/socket_client.cc b/i/simulotron/src/socket/socket_client.cc
index ef26e70..b8bdbc3 100644
--- a/i/simulotron/src/socket/socket_client.cc
+++ b/i/simulotron/src/socket/socket_client.cc
@@ -112,3 +112,9 @@ SocketClient::putChar (char c)
if (::write (socket_, &c, 1) < 0)
throw errno_exception ("SocketClient: erreur d'écriture (write()) ", errno);
}
+
+int
+SocketClient::getFD(void)
+{
+ return socket_;
+}
diff --git a/i/simulotron/src/socket/socket_client.hh b/i/simulotron/src/socket/socket_client.hh
index a024bec..de58bc5 100644
--- a/i/simulotron/src/socket/socket_client.hh
+++ b/i/simulotron/src/socket/socket_client.hh
@@ -54,6 +54,8 @@ class SocketClient
char getChar (void);
/// écrit un charactère dans le socket
void putChar (char c);
+ /// Récupère le FD du socket
+ int getFD (void);
};
#endif //socket_client_hh
diff --git a/i/simulotron/src/socket/socket_server.cc b/i/simulotron/src/socket/socket_server.cc
index 6014171..662abe0 100644
--- a/i/simulotron/src/socket/socket_server.cc
+++ b/i/simulotron/src/socket/socket_server.cc
@@ -68,3 +68,9 @@ SocketServer::accept (void)
return socket;
}
+
+int
+SocketServer::getFD (void)
+{
+ return socket_;
+}
diff --git a/i/simulotron/src/socket/socket_server.hh b/i/simulotron/src/socket/socket_server.hh
index 3b95949..30868e8 100644
--- a/i/simulotron/src/socket/socket_server.hh
+++ b/i/simulotron/src/socket/socket_server.hh
@@ -41,6 +41,8 @@ class SocketServer
void listen (int maxQueue);
/// accepte une connection et renvoie le fd
int accept (void);
+ /// récupère le FD du socket
+ int getFD (void);
};
#endif //socket_server_hh