summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--i/marvin/src/socket/server_socket.hh5
-rw-r--r--i/marvin/src/socket/socket_client.cc117
-rw-r--r--i/marvin/src/socket/socket_client.hh66
-rw-r--r--i/marvin/src/socket/socket_databuffer.cc25
-rw-r--r--i/marvin/src/socket/socket_databuffer.hh33
-rw-r--r--i/marvin/src/socket/socket_server.cc90
-rw-r--r--i/marvin/src/socket/socket_server.hh56
7 files changed, 390 insertions, 2 deletions
diff --git a/i/marvin/src/socket/server_socket.hh b/i/marvin/src/socket/server_socket.hh
index 995ad01..159aa65 100644
--- a/i/marvin/src/socket/server_socket.hh
+++ b/i/marvin/src/socket/server_socket.hh
@@ -32,8 +32,6 @@ class ServerSocket {
private:
/// L'identificateur du socket.
int socket_;
- /// Bind le serveur sur un port d'écoute.
- void bind (int port);
public:
/// Constructeur par défaut.
ServerSocket (int port);
@@ -43,5 +41,8 @@ class ServerSocket {
int accept (Address &a) const;
/// Accepte une nouvelle connexion.
int accept (void) const;
+ private:
+ /// Bind le serveur sur un port d'écoute.
+ void bind (int port);
};
#endif // server_socket_hh
diff --git a/i/marvin/src/socket/socket_client.cc b/i/marvin/src/socket/socket_client.cc
new file mode 100644
index 0000000..9dd19fa
--- /dev/null
+++ b/i/marvin/src/socket/socket_client.cc
@@ -0,0 +1,117 @@
+/* socket_client.cc - Définition de la classe des sockets client */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2005 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+#include "socket/socket_client.hh"
+#include "socket/socket_server.hh"
+#include "socket/address.hh"
+#include "utils/errno_exception.hh"
+
+#include <sys/socket.h>
+#include <cerrno>
+
+SocketClient::SocketClient (void)
+{
+ // Crée le file descriptor du socket
+ socket_ = socket (PF_INET, SOCK_STREAM, 0);
+ if (socket_ < 0)
+ throw errno_exception ("Socket: La chaussette n'a pas pu être tricoté", errno);
+}
+
+SocketClient::SocketClient (SocketServer & socketServer)
+{
+ socket_ = socketServer.accept ();
+ if (socket_ < 0)
+ throw errno_exception ("Socket: Problème lors de la construction d'une chaussette cliente par accept", errno);
+}
+
+SocketClient::~SocketClient (void)
+{
+ if (socket_ >= 0)
+ ::close (socket_);
+}
+
+void
+SocketClient::connect (const Address & address)
+{
+ if (::connect (socket_, address.getSockaddr (), sizeof (sockaddr_in)) < 0)
+ throw errno_exception ("Socket: Impossible de mettre la chaussette", errno);
+}
+
+void
+SocketClient::connect (const std::string & address, int port)
+{
+ connect (Address(address, port));
+}
+
+bool
+SocketClient::read (std::string & strReaded, bool bloquant)
+{
+ int charReaded;
+ char buffer[BUFFER_SIZE];
+ // Vérifie si il y a quelque chose à lire et retourne si non
+ if (!bloquant)
+ {
+ fd_set fds;
+ timeval tv = {0, 0};
+ FD_ZERO(&fds);
+ FD_SET(socket_, &fds);
+ if(select(socket_ + 1, &fds, NULL, NULL, &tv) == 0)
+ return false;
+ }
+ strReaded.clear();
+ do
+ {
+ charReaded = ::read (socket_, buffer, BUFFER_SIZE - 1);
+ if (charReaded < 0)
+ throw errno_exception ("SocketClient: problème de lecture", errno);
+ //buffer[charReaded] = 0;
+ //str += buffer;
+ strReaded.append(buffer, charReaded);
+ }while (charReaded == BUFFER_SIZE - 1);
+ return true;
+}
+
+void
+SocketClient::write (const std::string & str)
+{
+ char buffer[BUFFER_SIZE];
+ unsigned int pos = 0;
+ int retval = 0;
+
+ while (str.size () != pos)
+ {
+ int size = str.size () - pos < BUFFER_SIZE - 1 ? str.size () - pos:BUFFER_SIZE - 1;
+ str.copy (buffer, size, pos);
+ pos += size;
+ retval = ::write (socket_, buffer, size);
+ if (retval < 0)
+ throw errno_exception ("SocketClient: erreur d'écriture (write()) ", errno);
+ }
+}
+
+//int
+//SocketClient::getFD(void)
+//{
+// return socket_;
+//}
diff --git a/i/marvin/src/socket/socket_client.hh b/i/marvin/src/socket/socket_client.hh
new file mode 100644
index 0000000..56d7afa
--- /dev/null
+++ b/i/marvin/src/socket/socket_client.hh
@@ -0,0 +1,66 @@
+#ifndef socket_client_hh
+#define socket_client_hh
+/* socket_client.hh - Classe des socket client */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2005 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+
+#include "utils/non_copyable.hh"
+
+#include <string>
+
+class Address;
+class SocketServer;
+
+class SocketClient :public NonCopyable
+{
+ private:
+ /// Ze socket
+ int socket_;
+ /// constantes
+ /// Taille max du buffer de lecture/ecriture
+ static const int BUFFER_SIZE = 256;
+ public:
+ /// Constructeur
+ SocketClient (void);
+ /// Constructeur accept
+ SocketClient (SocketServer & socketServer);
+ /// Destructeur
+ ~SocketClient (void);
+ /// Connect le client au serveur
+ void connect (const Address & address);
+ /// Connect le client au serveur en se chargeant de créer Address
+ void connect (const std::string & address, int port);
+ /// lit du socket
+ bool read (std::string & strReaded, bool bloquant);
+ /// écrit dans le socket
+ void write (const std::string & str);
+ /// lit un charactère du socket
+ //char getChar (bool bloquant);
+ /// é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/marvin/src/socket/socket_databuffer.cc b/i/marvin/src/socket/socket_databuffer.cc
new file mode 100644
index 0000000..8267ddd
--- /dev/null
+++ b/i/marvin/src/socket/socket_databuffer.cc
@@ -0,0 +1,25 @@
+// 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.h"
+
diff --git a/i/marvin/src/socket/socket_databuffer.hh b/i/marvin/src/socket/socket_databuffer.hh
new file mode 100644
index 0000000..64dac86
--- /dev/null
+++ b/i/marvin/src/socket/socket_databuffer.hh
@@ -0,0 +1,33 @@
+#ifndef socket_databuffer_hh
+#define socket_databuffer_hh
+// socket_databuffer.hh
+// 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>
+// }}}
+
+/// Class s'occupant en particulier de récupérer les DataBuffer via le réseau
+/// TCP/IP
+class SocketDataBuffer : public SocketClient
+{
+};
+
+#endif // socket_databuffer_hh
diff --git a/i/marvin/src/socket/socket_server.cc b/i/marvin/src/socket/socket_server.cc
new file mode 100644
index 0000000..6cb8798
--- /dev/null
+++ b/i/marvin/src/socket/socket_server.cc
@@ -0,0 +1,90 @@
+/* socket_serveur.cc - Définition de la classe des sockets client */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2005 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+
+#include "socket/socket_server.hh"
+#include "utils/errno_exception.hh"
+#include "socket/address.hh"
+
+#include <sys/socket.h>
+#include <cerrno>
+
+SocketServer::SocketServer (const Address & address)
+{
+ initSocket(address);
+}
+
+SocketServer::SocketServer (const std::string & address, int port)
+{
+ if(address.empty())
+ initSocket(Address(port));
+ else
+ initSocket(Address(address, port));
+}
+
+SocketServer::~SocketServer (void)
+{
+ if (socket_ >= 0)
+ ::close (socket_);
+}
+
+ void
+SocketServer::listen (int maxQueue)
+{
+ if (::listen (socket_, maxQueue) < 0 )
+ throw errno_exception ("Socket: Impossible d'écouter la mer dans la chaussette", errno);
+}
+
+ int
+SocketServer::accept (void)
+{
+ int socket = ::accept (socket_, 0, 0);
+ if (socket < 0)
+ throw errno_exception ("Socket: Chaussette bloqué à la douane", errno);
+
+ return socket;
+}
+
+int
+SocketServer::getFD (void) const
+{
+ return socket_;
+}
+
+void
+SocketServer::initSocket(const Address & address)
+{
+ static const int reuse_s = 1;
+ // Crée le file descriptor du socket
+ socket_ = socket (PF_INET, SOCK_STREAM, 0);
+ if (socket_ < 0)
+ throw errno_exception ("Socket: La chaussette n'a pas pu être tricoté", errno);
+ // On règle la réusabilité du socket
+ if (setsockopt (socket_, SOL_SOCKET, SO_REUSEADDR,
+ &reuse_s, sizeof (int)) == -1)
+ throw errno_exception ("Socket: Erreur setsockopt : reusable", errno);
+ // On bind le socket
+ if (::bind (socket_, address.getSockaddr (), sizeof (*address.getSockaddr ())) < 0)
+ throw errno_exception ("Socket: Impossible d'assigner la chausette", errno);
+}
diff --git a/i/marvin/src/socket/socket_server.hh b/i/marvin/src/socket/socket_server.hh
new file mode 100644
index 0000000..f2a022e
--- /dev/null
+++ b/i/marvin/src/socket/socket_server.hh
@@ -0,0 +1,56 @@
+#ifndef socket_server_hh
+#define socket_server_hh
+/* socket_server.hh - Classe des sockets serveur */
+/* Simulotron - Programme de simulation de robot {{{
+ *
+ * Copyright (C) 2005 Nicolas Haller
+ *
+ * Robot APB Team/Efrei 2005.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * 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.
+ *
+ * }}} */
+
+#include "utils/non_copyable.hh"
+
+#include <string>
+class Address;
+
+class SocketServer : public NonCopyable
+{
+ private:
+ /// Ze socket
+ int socket_;
+ public:
+ /// Constructeur
+ SocketServer (const Address & address);
+ /// Constructeur s'occupant de Address
+ SocketServer (const std::string & address, int port);
+ /// Destructeur
+ ~SocketServer (void);
+ /// Ecoute le port
+ void listen (int maxQueue);
+ /// accepte une connection et renvoie le fd
+ int accept (void);
+ /// récupère le FD du socket
+ int getFD (void) const;
+ private:
+ /// Initialise le socket
+ void initSocket(const Address & address);
+};
+
+#endif //socket_server_hh