summaryrefslogtreecommitdiff
path: root/i/simulotron/src
diff options
context:
space:
mode:
authorhaller2005-12-03 17:21:26 +0000
committerhaller2005-12-03 17:21:26 +0000
commit6e48eaad66126f83480729a38dca1897f2066c11 (patch)
treef1b57ae0d85fc45a22b4fcf1aaad17a3f8eb5113 /i/simulotron/src
parent8becc9cd13865719346d029a263e9ad4143915c6 (diff)
* Ajout de autoheader dans le autogen
* Splittage de Socket
Diffstat (limited to 'i/simulotron/src')
-rw-r--r--i/simulotron/src/socket/Makefile.incl.am4
-rw-r--r--i/simulotron/src/socket/socket_client.cc (renamed from i/simulotron/src/socket/socket.cc)80
-rw-r--r--i/simulotron/src/socket/socket_client.hh (renamed from i/simulotron/src/socket/socket.hh)23
-rw-r--r--i/simulotron/src/socket/socket_server.cc39
-rw-r--r--i/simulotron/src/socket/socket_server.hh18
-rw-r--r--i/simulotron/src/socket/test_socket.cc9
6 files changed, 93 insertions, 80 deletions
diff --git a/i/simulotron/src/socket/Makefile.incl.am b/i/simulotron/src/socket/Makefile.incl.am
index 0cbe7bc..cc27283 100644
--- a/i/simulotron/src/socket/Makefile.incl.am
+++ b/i/simulotron/src/socket/Makefile.incl.am
@@ -1,5 +1,7 @@
address_S = socket/address.cc socket/address.hh
-socket_S = socket/socket.cc socket/socket.hh $(address_S) $(errno_exception_S)
+socket_S = socket/socket_server.cc socket/socket_server.hh \
+ socket/socket_client.cc socket/socket_client.hh \
+ $(address_S) $(errno_exception_S)
check_PROGRAMS += test_socket
test_socket_SOURCES = socket/test_socket.cc $(socket_S)
diff --git a/i/simulotron/src/socket/socket.cc b/i/simulotron/src/socket/socket_client.cc
index 0e7f819..3b7b05b 100644
--- a/i/simulotron/src/socket/socket.cc
+++ b/i/simulotron/src/socket/socket_client.cc
@@ -1,11 +1,12 @@
+#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>
-#include <iostream>
-#include "socket/socket.hh"
-#include "utils/errno_exception.hh"
-#include "socket/address.hh"
-Socket::Socket(const Address & address)
+SocketClient::SocketClient(const Address & address)
{
// Crée le file descriptor du socket
socket_ = socket (PF_INET, SOCK_STREAM, 0);
@@ -17,60 +18,26 @@ Socket::Socket(const Address & address)
throw errno_exception("Impossible d'assigner la chausette", errno);
}
-Socket::Socket(int socket)
+SocketClient::SocketClient(SocketServer & socketServer)
{
- socket_ = socket;
+ socket_ = socketServer.accept();
+ if (socket_ < 0)
+ throw errno_exception("Problème lors de la construction d'une chaussette cliente par accept", errno);
}
-Socket::Socket(Socket & socket)
-{
- socket_ = socket.socket_;
- socket.socket_ = -1;
-}
-
-Socket::~Socket(void)
+SocketClient::~SocketClient(void)
{
if(socket_ >= 0)
::close(socket_);
}
-Socket & Socket::operator= (Socket & socket)
-{
- socket_ = socket.socket_;
- socket.socket_ = -1;
-}
-
-void Socket::connect(const Address & address)
+void SocketClient::connect(const Address & address)
{
if (::connect (socket_, address.getSockaddr(), sizeof(sockaddr_in)) < 0)
throw errno_exception("Impossible de mettre la chaussette", errno);
}
-void Socket::listen(int maxQueue)
-{
- if(::listen(socket_, maxQueue) < 0 )
- throw errno_exception("Impossible d'écouter la mer dans la chaussette", errno);
-}
-
-Socket Socket::accept(Address & address)
-{
- sockaddr addr;
- socklen_t length = sizeof(sockaddr_in);
-
- int socket = ::accept(socket_, &addr, &length);
- if (socket < 0)
- throw errno_exception("Chaussette bloqué à la douane", errno);
- address = Address(&addr, length);
-
- return Socket(socket);
-}
-
-void Socket::close(void)
-{
- ::close(socket_);
-}
-
-std::string Socket::read(void)
+std::string SocketClient::read(void)
{
std::string str;
int charReaded;
@@ -79,14 +46,14 @@ std::string Socket::read(void)
{
charReaded = ::read(socket_, buffer, BUFFER_SIZE - 1);
if(charReaded < 0)
- throw errno_exception("Socket: problème de lecture", errno);
+ throw errno_exception("SocketClient: problème de lecture", errno);
buffer[charReaded] = 0;
str += buffer;
}while(charReaded == BUFFER_SIZE - 1);
return str;
}
-void Socket::write(const std::string & str)
+void SocketClient::write(const std::string & str)
{
char buffer[BUFFER_SIZE];
unsigned int pos = 0;
@@ -100,23 +67,20 @@ void Socket::write(const std::string & str)
buffer[size] = 0;
retval = ::write(socket_, buffer, size);
if(retval < 0)
- throw errno_exception("Socket: erreur d'écriture (write()) ", errno);
+ throw errno_exception("SocketClient: erreur d'écriture (write()) ", errno);
}
}
-char Socket::getChar(void)
+char SocketClient::getChar(void)
{
char getted;
- ::read(socket_, &getted, 1);
+ if(::read(socket_, &getted, 1) < 0)
+ throw errno_exception("SocketClient: erreur d'écriture (write()) ", errno);
return getted;
}
-void Socket::putChar(char c)
-{
- ::write(socket_, &c, 1);
-}
-
-int Socket::getFD(void)
+void SocketClient::putChar(char c)
{
- return socket_;
+ if(::write(socket_, &c, 1) < 0)
+ throw errno_exception("SocketClient: erreur d'écriture (write()) ", errno);
}
diff --git a/i/simulotron/src/socket/socket.hh b/i/simulotron/src/socket/socket_client.hh
index 92fddfd..112a4dd 100644
--- a/i/simulotron/src/socket/socket.hh
+++ b/i/simulotron/src/socket/socket_client.hh
@@ -1,7 +1,8 @@
#include <string>
class Address;
+class SocketServer;
-class Socket
+class SocketClient
{
private:
/// Ze socket
@@ -11,23 +12,13 @@ class Socket
static const int BUFFER_SIZE = 256;
public:
/// Constructeur
- Socket(const Address & address);
- /// Constructeur à partir d'un fd
- Socket(int socket);
- /// Constructeur par recopie
- Socket(Socket & socket);
+ SocketClient(const Address & address);
+ /// Constructeur accept
+ SocketClient(SocketServer & socketServer);
/// Destructeur
- ~Socket(void);
- /// operateur d'affectation
- Socket & operator= (Socket & socket);
+ ~SocketClient(void);
/// Connect le client au serveur
void connect(const Address & address);
- /// Ecoute le port
- void listen(int maxQueue);
- /// accepte une connection
- Socket accept(Address & address);
- /// coupe la chaussette
- void Socket::close(void);
/// lit du socket
std::string read(void);
/// écrit dans le socket
@@ -36,6 +27,4 @@ class Socket
char getChar(void);
/// écrit un charactère dans le socket
void putChar(char c);
- /// Drapeau connecté
- bool isConnected(void);
};
diff --git a/i/simulotron/src/socket/socket_server.cc b/i/simulotron/src/socket/socket_server.cc
new file mode 100644
index 0000000..c214ca7
--- /dev/null
+++ b/i/simulotron/src/socket/socket_server.cc
@@ -0,0 +1,39 @@
+#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)
+{
+ // Crée le file descriptor du socket
+ socket_ = socket (PF_INET, SOCK_STREAM, 0);
+ if(socket_ < 0)
+ throw errno_exception ("La chaussette n'a pas pu être tricoté", errno);
+
+ // On bind le socket
+ if(::bind (socket_, address.getSockaddr(), sizeof(*address.getSockaddr())) < 0)
+ throw errno_exception("Impossible d'assigner la chausette", errno);
+}
+
+SocketServer::~SocketServer(void)
+{
+ if(socket_ >= 0)
+ ::close(socket_);
+}
+
+void SocketServer::listen(int maxQueue)
+{
+ if(::listen(socket_, maxQueue) < 0 )
+ throw errno_exception("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("Chaussette bloqué à la douane", errno);
+
+ return socket;
+}
diff --git a/i/simulotron/src/socket/socket_server.hh b/i/simulotron/src/socket/socket_server.hh
new file mode 100644
index 0000000..bac6687
--- /dev/null
+++ b/i/simulotron/src/socket/socket_server.hh
@@ -0,0 +1,18 @@
+#include <string>
+class Address;
+
+class SocketServer
+{
+ private:
+ /// Ze socket
+ int socket_;
+ public:
+ /// Constructeur
+ SocketServer(const Address & address);
+ /// Destructeur
+ ~SocketServer(void);
+ /// Ecoute le port
+ void listen(int maxQueue);
+ /// accepte une connection et renvoie le fd
+ int accept(void);
+};
diff --git a/i/simulotron/src/socket/test_socket.cc b/i/simulotron/src/socket/test_socket.cc
index 4a44e6e..66ad30f 100644
--- a/i/simulotron/src/socket/test_socket.cc
+++ b/i/simulotron/src/socket/test_socket.cc
@@ -1,5 +1,6 @@
-#include "socket/socket.hh"
#include "socket/address.hh"
+#include "socket/socket_server.hh"
+#include "socket/socket_client.hh"
#include "utils/errno_exception.hh"
#include <iostream>
@@ -50,11 +51,11 @@ void * testSockServ(void * CaSertARien)
try
{
// Création de la socket serveur;
- Socket sockServ(Address(4242));
+ SocketServer sockServ(Address(4242));
// Mise en écoute du socket serveur
sockServ.listen(12);
// Acceptation d'une connection
- Socket sockFork = sockServ.accept(adr2);
+ SocketClient sockFork(sockServ);
// Reception d'un message
strDest = sockFork.read();
if(strDepart != strDest)
@@ -92,7 +93,7 @@ void * testSockClient(void * CaSertARien)
try
{
// Création de la socket client
- Socket sockClient(adr);
+ SocketClient sockClient(adr);
// Demande de connection
sockClient.connect(Address(std::string("localhost"), 4242));
// Ecriture du client dans le serveur