summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/socket/socket_text.cc
diff options
context:
space:
mode:
authordufourj2005-04-29 20:45:52 +0000
committerdufourj2005-04-29 20:45:52 +0000
commitc50955765bfd033c8be61bff6132a1b2ba5c2eb3 (patch)
tree588c26c6c566f450182f491caa950c0694430fd6 /2005/i/robert/src/socket/socket_text.cc
parent416e03aa4360008a39d2b9c452ce51b1cc0e7149 (diff)
Ajout des fichers du dossier socket pour le réseau
* Address : gestion des structures d'adresse pour les sockets, * SocketText : socket qui écrit en texte sur le réseau, * SocketServer : serveur qui écoute sur un port. La classe SocketText ne gére pas les sockets non bloquants. Les fonctions recv/sent sont donc à revoir.
Diffstat (limited to '2005/i/robert/src/socket/socket_text.cc')
-rw-r--r--2005/i/robert/src/socket/socket_text.cc166
1 files changed, 166 insertions, 0 deletions
diff --git a/2005/i/robert/src/socket/socket_text.cc b/2005/i/robert/src/socket/socket_text.cc
new file mode 100644
index 0000000..9c94509
--- /dev/null
+++ b/2005/i/robert/src/socket/socket_text.cc
@@ -0,0 +1,166 @@
+// socket_text.cc
+// robert - programme du robot 2005. {{{
+//
+// Copyright (C) 2005 Dufour Jérémy
+//
+// 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: %WEB%
+// Email: <dufourj@efrei.fr>
+// }}}
+
+#include "socket_text.hh"
+
+#include "server_socket.hh"
+#include "address.hh"
+#include "data/data_buffer.hh"
+
+#include <sstream>
+#include <stdexcept>
+
+/// Constructeur du serveur
+SocketText::SocketText (ServerSocket &ss)
+ : sendBufSize_ (0), recvTaille_ (0)
+{
+ socket_ = ss.accept ();
+}
+
+/// Constructeur du serveur avec addresse
+SocketText::SocketText (ServerSocket &ss, Address &a)
+ : sendBufSize_ (0), recvTaille_ (0)
+{
+ socket_ = ss.accept (a);
+}
+
+/// Constructeur du client
+SocketText::SocketText (const Address &a)
+ : sendBufSize_ (0), recvTaille_ (0)
+{
+ // Création du socket
+ socket_ = socket (AF_INET, SOCK_STREAM, 0);
+ if (socket_ == -1)
+ throw std::runtime_error ("Erreur de création de socket");
+ // Connexion au serveur
+ if (connect (socket_, a.getSockaddr (), sizeof (sockaddr_in)) == -1)
+ throw std::runtime_error ("Erreur de connexion au serveur");
+}
+
+/// Envoie d'un buffer de données.
+SocketText &
+SocketText::operator<< (const DataBuffer &d)
+{
+ DataBuffer db (d);
+ unsigned size = db.size ();
+ std::vector <char> v;
+ v.resize (size);
+ db.read ((uint8_t*) &v[0], size);
+ send (&v[0], size);
+ return *this;
+}
+
+/// Reception d'un buffer de données.
+SocketText &
+SocketText::operator>> (DataBuffer &d)
+{
+ std::vector <char> v;
+ recv (v);
+ d.write ((uint8_t*) &v[0], v.size ());
+ return *this;
+}
+
+/// Envoie d'un tableau de données (data) de taille size.
+int
+SocketText::send (const char *data, const int size)
+{
+ int ss;
+ int status;
+ int dataSend = 0;
+ // Encodage, ss = taille (message + header)
+ ss = size + encodeMsg (data, size);
+ do
+ {
+ status = ::send (socket_, &sendBuf_[0], ss - dataSend, MSG_NOSIGNAL);
+ dataSend += status;
+ } while (status >= 0 && size >= dataSend);
+ return status < 0 ? -1 : dataSend;
+}
+
+/// Encodage du message (rajout du header)
+int
+SocketText::encodeMsg (const char *data, const int size)
+{
+ // Création du header
+ std::ostringstream o;
+ o << "H";
+ if (!(o << size ))
+ throw std::runtime_error ("Erreur de convertion int -> string");
+ o << "H";
+ std::string s = o.str ();
+ int ss = s.size ();
+ // Re-Allocation du buffer d'envoye
+ sendBuf_.resize (ss + size);
+ // Copie du header
+ strncpy (&sendBuf_[0], s.c_str (), ss);
+ // Copie du message
+ strncpy (&sendBuf_[ss], data, size);
+ return ss;
+}
+
+/// Reception de données dans un vecteur.
+void SocketText::recv (std::vector<char> &v)
+{
+ // FIXME C'est KK
+ int status;
+ static unsigned int pos = 0;
+ static unsigned int posHeader = 0;
+ // 0 : pas reçu, 1 : partiellement reçu, 2 : reçu
+ static int headerStatus = 0;
+
+ // On n'a pas reçu la taille encore
+ if (headerStatus != 2)
+ {
+ // Récupération des données une par une.
+ do
+ {
+ status = ::recv (socket_, &headBuf_[posHeader], 1, MSG_NOSIGNAL);
+ if (headBuf_[posHeader++] == 'H')
+ headerStatus ++;
+ } while (status > 0 && headerStatus != 2);
+ }
+ // Récupération de la taille du message du header
+ if (headerStatus == 2 && recvTaille_ == 0)
+ {
+ recvTaille_ = atoi (&headBuf_[1]);
+ pos = 0;
+ recvBuf_.resize (recvTaille_);
+ }
+ if (recvTaille_)
+ {
+ // Récupération du message lui-même
+ do
+ {
+ status = ::recv (socket_, &recvBuf_[pos], recvTaille_,
+ MSG_NOSIGNAL);
+ if (status > 0)
+ pos += status;
+ } while (status > 0 && pos < recvTaille_);
+ if (pos == recvTaille_)
+ {
+ v = recvBuf_;
+ headerStatus = posHeader = recvTaille_ = 0;
+ }
+ }
+}