// 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: // }}} #include "socket_text.hh" #include "server_socket.hh" #include "address.hh" #include "data/data_buffer.hh" #include #include /// 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 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 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 &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; } } }