summaryrefslogtreecommitdiff
path: root/i/chuck/src/socket/socket_databuffer.cc
diff options
context:
space:
mode:
authorbecquet2007-05-10 18:49:20 +0000
committerbecquet2007-05-10 18:49:20 +0000
commit8f486613be58ced269db1d437e560c16558604e8 (patch)
tree41e94b2122a118cb06abf6fc2a0038cd1dfbec4a /i/chuck/src/socket/socket_databuffer.cc
parent4daa2c76c2a028e4b2c8ab379e7d1e0f535a0a31 (diff)
Création de chuck, le programme du robot 2007.
Diffstat (limited to 'i/chuck/src/socket/socket_databuffer.cc')
-rw-r--r--i/chuck/src/socket/socket_databuffer.cc97
1 files changed, 97 insertions, 0 deletions
diff --git a/i/chuck/src/socket/socket_databuffer.cc b/i/chuck/src/socket/socket_databuffer.cc
new file mode 100644
index 0000000..1b4258c
--- /dev/null
+++ b/i/chuck/src/socket/socket_databuffer.cc
@@ -0,0 +1,97 @@
+// socket_databuffer.cc
+// marvin - programme du robot 2006. {{{
+//
+// Copyright (C) 2006 Nicolas Haller
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Contact :
+// Web: http://perso.efrei.fr/~haller/
+// Email: <nicolas@boiteameuh.org>
+// }}}
+#include "socket_databuffer.hh"
+
+#include <stdexcept>
+#include <iostream>
+
+// Constructeur
+SocketDataBuffer::SocketDataBuffer(void)
+ :SocketClient(), dbSize_(0) // TODO Ça sert à quelque chose où il le fait implicitement?
+{
+}
+
+// Constructeur accept
+SocketDataBuffer::SocketDataBuffer(SocketServer & socketServer)
+ :SocketClient(socketServer), dbSize_(0)
+{
+}
+
+// Récupère un DataBuffer
+// false en valeur de retour avec bloquant = true est un motif de licenciement
+bool
+SocketDataBuffer::read(DataBuffer & dbReaded, bool bloquant)
+{
+ std::string strReceive;
+ do
+ {
+ SocketClient::read(strReceive, bloquant);
+ strBuffer_.append(strReceive);
+ // On regarde si on a assez de donnée pour regarder si on a une en-tête
+ if(strBuffer_.size() > sizeof(unsigned))
+ {
+ dbSize_ = 0;
+ dbSize_ = static_cast<unsigned>(strBuffer_[0]) << 24
+ | static_cast<unsigned>(strBuffer_[1]) << 16
+ | static_cast<unsigned>(strBuffer_[2]) << 8
+ | static_cast<unsigned>(strBuffer_[3]);
+ // On regarde si on a suffisemment de données pour constituer le DB
+ if(dbSize_ <= strBuffer_.size() - 4)
+ {
+ dbBuffer_.write(reinterpret_cast<const uint8_t *>(strBuffer_.c_str() + 4)
+ , dbSize_);
+ dbReaded.swap(dbBuffer_);
+ dbBuffer_.clear();
+ strBuffer_.erase(0, dbSize_ + 4);
+ return true;
+ }
+ }
+ }while(bloquant);
+ return false;
+}
+
+// Ecrit un DB dans le socket
+void
+SocketDataBuffer::write(DataBuffer & db)
+{
+ // On met une en-tête de 4 octects déterminant la taille de la DB
+ unsigned dbSize = db.size();
+ char entete[4];
+ entete[0] = static_cast<char> (dbSize >> 24);
+ entete[1] = static_cast<char> (dbSize >> 16);
+ entete[2] = static_cast<char> (dbSize >> 8);
+ entete[3] = static_cast<char> (dbSize);
+
+ SocketClient::write(std::string(entete,4));
+ // On récupère les données de la dataBuffer
+ uint8_t * donnees = new uint8_t[dbSize];
+ db.read(donnees, dbSize);
+ if (db.size() != 0)
+ throw std::runtime_error("ERREUR DE CODAGE dans SocketDataBuffer"
+ "::Write");
+ // On envoie le tout et on nettoie l'allocation
+ SocketClient::write(std::string(reinterpret_cast<const char *>(donnees),
+ dbSize));
+ delete [] donnees;
+}