summaryrefslogtreecommitdiff
path: root/i/simulotron/src
diff options
context:
space:
mode:
Diffstat (limited to 'i/simulotron/src')
-rw-r--r--i/simulotron/src/Makefile.am7
-rw-r--r--i/simulotron/src/main.cc5
-rw-r--r--i/simulotron/src/socket/socket.cc41
-rw-r--r--i/simulotron/src/socket/socket.hh29
4 files changed, 82 insertions, 0 deletions
diff --git a/i/simulotron/src/Makefile.am b/i/simulotron/src/Makefile.am
new file mode 100644
index 0000000..2970e77
--- /dev/null
+++ b/i/simulotron/src/Makefile.am
@@ -0,0 +1,7 @@
+bin_PROGRAMS = simulotron
+simulotron_SOURCES = main.cc \
+ socket/socket.cc socket/socket.hh
+
+simulotron_CXXFLAGS = -g -Wall -fmessage-length=0
+simulotron_LDADD =
+INCLUDES = -I../../src
diff --git a/i/simulotron/src/main.cc b/i/simulotron/src/main.cc
new file mode 100644
index 0000000..2992206
--- /dev/null
+++ b/i/simulotron/src/main.cc
@@ -0,0 +1,5 @@
+
+int main(void)
+{
+ return 0;
+}
diff --git a/i/simulotron/src/socket/socket.cc b/i/simulotron/src/socket/socket.cc
new file mode 100644
index 0000000..2f7139c
--- /dev/null
+++ b/i/simulotron/src/socket/socket.cc
@@ -0,0 +1,41 @@
+#include <sys/socket.h>
+
+Socket::Socket(const std::string & address, int port, bool block)
+{
+ // 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);
+}
+
+void Socket::connect(const Address & address)
+{
+ if (connect (socket_, address.getSockaddr(), sizeof(*address.getSockaddr()) < 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;
+
+ int socket = accept(socket_, &addr, &length);
+
+ if (socket < 0)
+ throw errno_exception("Chaussette bloqué à la douane", errno);
+ address = Address(addr, length);
+
+ return Socket(socket);
+}
+
+
diff --git a/i/simulotron/src/socket/socket.hh b/i/simulotron/src/socket/socket.hh
new file mode 100644
index 0000000..c1b47df
--- /dev/null
+++ b/i/simulotron/src/socket/socket.hh
@@ -0,0 +1,29 @@
+class Socket
+{
+ private:
+ /// Ze socket
+ int socket_;
+ public:
+ /// Constructeur
+ Socket(const Address & adress, bool block = false);
+ /// Constructeur à partir d'un fd
+ Socket(int socket);
+ /// Connect le client au serveur
+ void connect(void);
+ /// Ecoute le port
+ void listen(void);
+ /// accepte une connection
+ void accept(void);
+ /// lit du socket
+ void read(void);
+ /// écrit dans le socket
+ void write(void);
+ /// lit un charactère du socket
+ void getChar(void);
+ /// écrit un charactère dans le socket
+ void putChar(void);
+ /// Drapeau connecté
+ bool isConnected(void);
+ private:
+ void bind();
+};