From bbd5d85d068502f7c0afd5f86ce1cb674053b8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Wed, 16 Jan 2008 14:25:37 +0000 Subject: [PATCH] Utilisation de sll --- slc/trunk/Makefile.am | 20 +- slc/trunk/src/connection.cc | 179 ------------ slc/trunk/src/connection.hh | 43 --- slc/trunk/src/history.cc | 1 - slc/trunk/src/screen.cc | 7 +- slc/trunk/src/slc.cc | 26 +- slc/trunk/src/slc.hh | 35 +-- slc/trunk/src/sll/connection.cc | 467 +++++++++++++++++++++++++++++++ slc/trunk/src/sll/connection.hh | 96 +++++++ slc/trunk/src/sll/connection.hxx | 49 ++++ slc/trunk/src/{ => sll}/error.cc | 26 +- slc/trunk/src/{ => sll}/error.hh | 23 +- slc/trunk/src/sll/slm.hh | 61 ++++ 13 files changed, 736 insertions(+), 297 deletions(-) delete mode 100644 slc/trunk/src/connection.cc delete mode 100644 slc/trunk/src/connection.hh create mode 100644 slc/trunk/src/sll/connection.cc create mode 100644 slc/trunk/src/sll/connection.hh create mode 100644 slc/trunk/src/sll/connection.hxx rename slc/trunk/src/{ => sll}/error.cc (80%) rename slc/trunk/src/{ => sll}/error.hh (74%) create mode 100644 slc/trunk/src/sll/slm.hh diff --git a/slc/trunk/Makefile.am b/slc/trunk/Makefile.am index a27163c..939ddd8 100644 --- a/slc/trunk/Makefile.am +++ b/slc/trunk/Makefile.am @@ -16,18 +16,22 @@ bin_PROGRAMS= slc -slc_SOURCES= src/slc.hh \ - src/slc.cc \ +slc_SOURCES= src/slc.cc \ src/options.cc \ + src/engine.cc \ + src/screen.cc \ + src/history.cc \ + src/sll/connection.cc \ + src/sll/error.cc + +noinst_SOURCES= src/slc.hh \ src/options.hh \ + src/engine.hh \ src/screen.hh \ - src/screen.cc \ - src/connection.hh \ - src/connection.cc \ src/history.hh \ - src/history.cc \ - src/error.hh \ - src/error.cc + src/sll/error.hh \ + src/sll/connection.hh + CLEANFILES= *~ '\#*' .*.swp .*~ diff --git a/slc/trunk/src/connection.cc b/slc/trunk/src/connection.cc deleted file mode 100644 index 98a08e9..0000000 --- a/slc/trunk/src/connection.cc +++ /dev/null @@ -1,179 +0,0 @@ -/* - This file is part of SLC. - Copyright (C) 2008 Sebastien LUTTRINGER - - SLC 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; version 2 of the License. - - SLC 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 SLC; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include -#include -#include -#include -#include -#include - -#include "slc.hh" -#include "options.hh" -#include "connection.hh" -#include "screen.hh" -#include "error.hh" - -pthread_mutex_t mtx_socket = PTHREAD_MUTEX_INITIALIZER; - -Connection::Connection() : socket_(0) {} - -bool Connection::connected() const { - return socket_ > 0; -} - -void Connection::start() { - if (connected()) - return; - - // make connection - connect_(O.server.c_str(), O.port); - - // send login info - - // send pass info - - // Start reader thread - pthread_create(&g_thread_reader, 0, thread_reader, 0); -} - -void Connection::stop() { - if (!connected()) - return; - disconnect_(); -} - -void Connection::connect_(const char *addr, int port) { - struct sockaddr_in daddr; - struct hostent *h; - - // Check no existing connexion - assert(socket_ == 0); - - // retrieve remote host info - h = gethostbyname(addr); - if (h == 0) { - S << "Unable to resolve: " << addr << ": " << hstrerror(h_errno) << ".\n"; - return; - } - - // create socket - pthread_mutex_lock(&mtx_socket); - socket_ = socket(PF_INET, SOCK_STREAM, 0); - if (socket_ == -1) { - S << "Unable to create socket: " << strerror(errno) << ".\n"; - socket_ = 0; - pthread_mutex_unlock(&mtx_socket); - return; - } - pthread_mutex_unlock(&mtx_socket); - - daddr.sin_family = AF_INET; - daddr.sin_port = htons(port); - daddr.sin_addr = *((struct in_addr *) h->h_addr); - memset(daddr.sin_zero, '\0', sizeof daddr.sin_zero); - - S << "Connecting to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr) - << ") on port " << port << "...\n"; - - // connect - pthread_mutex_lock(&mtx_socket); - int con = connect(socket_, (struct sockaddr *) &daddr, sizeof daddr); - pthread_mutex_unlock(&mtx_socket); - if (con == -1) { - disconnect_(); - S << "Unable to connect: " << strerror(errno) << ".\n"; - return; - } - - S << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr) - << ") on port " << port << ".\n"; -} - -void Connection::disconnect_() { - assert(socket_ > 0); - - pthread_mutex_lock(&mtx_socket); - close(socket_); - socket_ = 0; - pthread_mutex_unlock(&mtx_socket); -} - - -void Connection::send(const string &data) { - send(data.c_str(), data.length()); -} - -void Connection::send(const char* buf, size_t len) { - assert(socket_ > 0); - pthread_mutex_lock(&mtx_socket); - write(socket_, buf, len); - pthread_mutex_unlock(&mtx_socket); -} - -void Connection::sendln(const string &data) { - sendln(data.c_str(), data.length()); -} - -void Connection::sendln(const char* buf, size_t len) { - assert(socket_ > 0); - - pthread_mutex_lock(&mtx_socket); - write(socket_, buf, len); - write(socket_, "\n", 1); - pthread_mutex_unlock(&mtx_socket); -} - -string Connection::recvln() { - assert(socket > 0); - - do { - // check EOL char - size_t offset = rbuf_.find('\n'); - if (offset != string::npos) { - string s = rbuf_.substr(0, offset); - rbuf_.erase(0, offset + 1); - return s; - } - - // wait incoming data - static struct pollfd spfd[1] = {{socket_, POLLIN|POLLPRI|POLLHUP|POLLERR, 0}}; - poll(spfd, 1, -1); - - // lock before read - pthread_mutex_lock(&mtx_socket); - - // read data - static char local_buf[MAX_LINE_SIZE]; - int ret = ::recv(socket_, local_buf, MAX_LINE_SIZE, MSG_DONTWAIT); - - // unlock - pthread_mutex_unlock(&mtx_socket); - - if (ret == -1) { - disconnect_(); - *local_buf = 0; - } - else local_buf[ret] = 0; - - // add read data in buffer - rbuf_ += local_buf; - - } while (1); - assert(1); -} diff --git a/slc/trunk/src/connection.hh b/slc/trunk/src/connection.hh deleted file mode 100644 index cc7891f..0000000 --- a/slc/trunk/src/connection.hh +++ /dev/null @@ -1,43 +0,0 @@ -/* - This file is part of SLC. - Copyright (C) 2008 Sebastien LUTTRINGER - - SLC 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; version 2 of the License. - - SLC 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 SLC; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef CONNECTION_HH -# define CONNECTION_HH - -class Connection { -public: - Connection(); - - - bool connected() const; - void start(); - void stop(); - void send(const string &data); - void send(const char* buf, size_t len); - void sendln(const string &data); - void sendln(const char* buf, size_t len); - string recvln(); -private: - void connect_(const char *addr, int port); - void disconnect_(); -private: - int socket_; - string rbuf_; -}; - -#endif diff --git a/slc/trunk/src/history.cc b/slc/trunk/src/history.cc index 5ddfa8b..4b15088 100644 --- a/slc/trunk/src/history.cc +++ b/slc/trunk/src/history.cc @@ -18,7 +18,6 @@ #include "slc.hh" #include "history.hh" -#include "error.hh" History::History() : max_size_(100) {} diff --git a/slc/trunk/src/screen.cc b/slc/trunk/src/screen.cc index e8c1c7d..7821bbf 100644 --- a/slc/trunk/src/screen.cc +++ b/slc/trunk/src/screen.cc @@ -23,9 +23,8 @@ #include "slc.hh" #include "screen.hh" #include "options.hh" -#include "error.hh" #include "history.hh" -#include "connection.hh" +#include "sll/connection.hh" /* ********************************************************************************* @@ -529,11 +528,11 @@ void Screen::cmd_exec(const char *s) { } else if (!strcmp(s, "/logout")) { S << s << "\n"; - C.stop(); + //C.stop(); } else if (!strcmp(s, "/login")) { S << s << "\n"; - C.start(); + //C.start(); } else S << s << ": Unkown command.\n"; diff --git a/slc/trunk/src/slc.cc b/slc/trunk/src/slc.cc index 75d1b8f..fa0af39 100644 --- a/slc/trunk/src/slc.cc +++ b/slc/trunk/src/slc.cc @@ -16,15 +16,16 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include - #include "slc.hh" #include "options.hh" #include "screen.hh" #include "history.hh" -#include "error.hh" -#include "connection.hh" +#include "sll/connection.hh" +#include "engine.hh" + +#include +Engine E; Screen S; Options O; History H; @@ -52,20 +53,11 @@ int main(int argc, char *argv[]) // load cmd history H.load(O.history_file); - // Create graphic env - S.init(); - - // Showing login screen. Retrieve user/pass. - S.login(); - - // Showing dialog screen - S.dialog(); - - // start connexion - C.start(); + // Starting engine + E.on(); - // Treat keyboards events - S.eventsloop(); + // Stoping engine + E.off(); // Save history H.save(O.history_file); diff --git a/slc/trunk/src/slc.hh b/slc/trunk/src/slc.hh index ac955c5..061e9c1 100644 --- a/slc/trunk/src/slc.hh +++ b/slc/trunk/src/slc.hh @@ -19,41 +19,13 @@ #ifndef SLC_HH # define SLC_HH -# define _REENTRANT - -# include -# include -# include -# include -# include -# include -# include - -typedef std::string string; -typedef std::stringstream sstream; -typedef std::ostream ostream; -typedef std::ifstream ifstream; -typedef std::ofstream ofstream; - -enum { - ERR_OK = 0, - ERR_USAGE = 1, - ERR_BADPARAM = 2, - ERR_FILE = 3, - ERR_NET = 4, - ERR_PROTO = 5, - ERR_AUTH = 6, - ERR_SCREENSZ = 7, - ERR_NOMEM = 41, - ERR_UNKNOWN = 42 -}; +# include "sll/slm.hh" +# include "sll/error.hh" // ----------------------------------------------------------------------------- // Gonstant // ----------------------------------------------------------------------------- -static const string VERSION = "1"; -static const int MAX_LINE_SIZE = 512; static const int MAX_CONF_LINE_SIZE = 2048; static const string RCV_DATA = "<< "; static const string SND_DATA = ">> "; @@ -66,11 +38,13 @@ class Options; class Screen; class History; class Connection; +class Engine; extern Options O; extern Screen S; extern History H; extern Connection C; +extern Engine E; extern pthread_t g_thread_reader; @@ -80,5 +54,6 @@ extern pthread_t g_thread_reader; int main(int, char **); void *thread_reader(void *); +void login(); #endif diff --git a/slc/trunk/src/sll/connection.cc b/slc/trunk/src/sll/connection.cc new file mode 100644 index 0000000..a3ee8ca --- /dev/null +++ b/slc/trunk/src/sll/connection.cc @@ -0,0 +1,467 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL 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; version 2 of the License. + + SLL 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 SLL; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "slm.hh" +#include "connection.hh" +#include "error.hh" + +#include +#include +#include +#include +#include +#include + +/******************************************************************************* + ** Class method + ******************************************************************************/ +unsigned long int Connection::getconnid() { + static unsigned long int id = 1; + return id++; +} + +/******************************************************************************* + ** Public method + ******************************************************************************/ + +/** + * Constructor + * + * @param fd socket of the connection (-1) is not exist + */ +Connection::Connection(int fd) : socket_fd_(fd), local_port_(-1), remote_port_(-1), id_(0) { + pthread_mutex_init(&c_mutex_, 0); + pthread_mutex_init(&r_mutex_, 0); + pthread_mutex_init(&w_mutex_, 0); + + if (socket_fd_ >= 0) { + id_ = getconnid(); + setallinfo_(); + } +} + +/** + * Destructor + */ +Connection::~Connection() { + pthread_mutex_destroy(&c_mutex_); + pthread_mutex_destroy(&r_mutex_); + pthread_mutex_destroy(&w_mutex_); +} + +/** + * Create connection with @param addr on port @param port + */ +void Connection::connect(const char *addr, int port) { + if (socket_fd_ >= 0) + throw Error(ERR_NET, "Connection already established but trying to connect"); + + if (port <= 0) + throw Error(ERR_NET, "Trying to Connect on a bad port number"); + + pthread_mutex_lock(&c_mutex_); + pthread_mutex_lock(&r_mutex_); + pthread_mutex_lock(&w_mutex_); + + // retrieve remote host info + struct hostent *h = gethostbyname(addr); + if (h == 0) { + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "Unable to resolve: " + addr + ": " + hstrerror(h_errno)); + } + + // create socket + try { socket_(); } + catch (...) { + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); + throw; + } + + // fill sockaddr + struct sockaddr_in daddr; + daddr.sin_family = AF_INET; + daddr.sin_port = htons(port); + daddr.sin_addr = *((struct in_addr *) h->h_addr); + memset(daddr.sin_zero, '\0', sizeof daddr.sin_zero); + + // connect + if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1) { + disconnect_(); + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "Unable to connect to " + addr + ": " + hstrerror(h_errno)); + } + + // set infos + setallinfo_(); + + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); +} + +/** + * Listen on @param port with queue size of max @param max + */ +void Connection::listen(int port, int max) { + if (socket_fd_ >= 0) + throw Error(ERR_NET, "Connection already established but trying to listen"); + + // lock + pthread_mutex_lock(&c_mutex_); + pthread_mutex_lock(&r_mutex_); + pthread_mutex_lock(&w_mutex_); + + // create socket + try { socket_(); } + catch (...) { + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); + throw; + } + + // fill sockaddr + struct sockaddr_in saddr; + + saddr.sin_family = AF_INET; + saddr.sin_port = htons(port); + saddr.sin_addr.s_addr = INADDR_ANY; + memset(saddr.sin_zero, '\0', sizeof saddr.sin_zero); + + // bind on socket + if (bind(socket_fd_, (struct sockaddr *)&saddr, sizeof saddr) == -1) { + disconnect_(); + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "Unable to bind: " + strerror(errno)); + } + + // listen on socket + if (::listen(socket_fd_, max) == -1) { + disconnect_(); + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "Unable to listen: " + strerror(errno)); + } + + // set all infos + setallinfo_(); + + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); +} + +/** + * Disconnect socket + */ +void Connection::disconnect() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to disconnect"); + + // lock all mutex + pthread_mutex_lock(&c_mutex_); + pthread_mutex_lock(&r_mutex_); + pthread_mutex_lock(&w_mutex_); + + disconnect_(); + + pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&c_mutex_); +} + +/** + * Return the connection sate + * Cannot be const, du to mutex locking + * + * @return see description + */ +bool Connection::connected() { + // get connection mutex + + pthread_mutex_lock(&c_mutex_); + + bool ret = (socket_fd_ >= 0); + + pthread_mutex_unlock(&c_mutex_); + + return ret; +} + +/** + * Accept new connection on a listening socket + * + * @return null on error, else a new connection + */ +Connection *Connection::accept() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to accept"); + + struct sockaddr_in r_addr; + socklen_t r_sin_size = sizeof r_addr; + int r_fd; + + pthread_mutex_lock(&r_mutex_); + pthread_mutex_lock(&w_mutex_); + + if ((r_fd = ::accept(socket_fd_, (struct sockaddr *) &r_addr, &r_sin_size)) == -1) { + pthread_mutex_unlock(&c_mutex_); + throw Error(ERR_NET, (string) "accept: " + strerror(errno)); + } + + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); + + return new Connection(r_fd); +} + +/** + * Send data on @param buf of size @param len on socket + */ +void Connection::send(const char* buf, size_t len) { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to send data"); + + pthread_mutex_lock(&w_mutex_); + int ret = ::send(socket_fd_, buf, len, 0); + pthread_mutex_unlock(&w_mutex_); + + if (ret == -1 && errno == ECONNRESET) + throw Error(ERR_NET, "Connection reset by peer"); + if (ret == -1) + throw Error(ERR_NET, "send: " + (string) strerror(errno)); +} + +/** + * Send data on @param buf of size @param len followed by '\n' on socket + */ +void Connection::sendln(const char* buf, size_t len) { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to send line"); + + pthread_mutex_lock(&w_mutex_); + // write data + int ret = ::send(socket_fd_, buf, len, 0); + if (ret == -1) { + pthread_mutex_unlock(&w_mutex_); + if (errno == ECONNRESET) + throw Error(ERR_NET, "Connection reset by peer"); + throw Error(ERR_NET, "sendln: " + (string) strerror(errno)); + } + + // write '\n' + ret = ::send(socket_fd_, "\n", 1, 0); + pthread_mutex_unlock(&w_mutex_); + if (ret == -1) + throw Error(ERR_NET, "sendln: " + (string) strerror(errno)); +} + +/** + * Receive a line + */ +string Connection::recvln() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to receive line"); + + pthread_mutex_lock(&r_mutex_); + do { + // check EOL char + size_t offset = rbuf_.find('\n'); + if (offset != string::npos) { + string s = rbuf_.substr(0, offset); + rbuf_.erase(0, offset + 1); + pthread_mutex_unlock(&r_mutex_); + return s; + } + + // do poll fd here ? + + // read data + static char local_buf[MAX_LINE_SIZE]; + int ret = ::recv(socket_fd_, local_buf, MAX_LINE_SIZE, 0); + + if (ret == 0) { + pthread_mutex_unlock(&r_mutex_); + throw Error(ERR_NET, "Connection reset by peer"); + } + + if (ret == -1) { + *local_buf = 0; + + // unlock before error + pthread_mutex_unlock(&r_mutex_); + + throw Error(ERR_NET, (string) "recvln: " + strerror(errno)); + } + else local_buf[ret] = 0; + + // add read data in buffer + rbuf_ += local_buf; + + } while (1); + assert(1); +} + +string Connection::getlocalip() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to get local ip"); + + pthread_mutex_lock(&c_mutex_); + if (local_ip_.empty()) + setlocalip_(); + string ip = local_ip_; + pthread_mutex_unlock(&c_mutex_); + return ip; +} + +int Connection::getlocalport() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to get local port"); + + pthread_mutex_lock(&c_mutex_); + if (local_port_ == -1) + setlocalport_(); + int port = local_port_; + pthread_mutex_unlock(&c_mutex_); + return port; +} + +string Connection::getremoteip() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to get remote ip"); + + pthread_mutex_lock(&c_mutex_); + if (remote_ip_.empty()) + setremoteip_(); + string ip = remote_ip_; + pthread_mutex_unlock(&c_mutex_); + return ip; +} + +int Connection::getremoteport() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to get remote port"); + + pthread_mutex_lock(&c_mutex_); + if (remote_port_ == -1) + setremoteport_(); + int port = remote_port_; + pthread_mutex_unlock(&c_mutex_); + return port; +} + +unsigned long int Connection::getid() { + return id_; +} + +int Connection::getsocket() { + if (socket_fd_ < 0) + throw Error(ERR_NET, "No connection established but trying to get socket fd"); + + pthread_mutex_lock(&c_mutex_); + int ret = socket_fd_; + pthread_mutex_unlock(&c_mutex_); + return ret; +} + +/******************************************************************************* + ** Protected method + ******************************************************************************/ + +/** + * Reserve a socket + * No mutex used ! + */ +void Connection::socket_() { + assert(socket_fd_ == -1); + int yes = 1; + + if ((socket_fd_ = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) + throw Error(ERR_NET, (string) "Unable to open socket: " + strerror(errno)); + + if (::setsockopt(socket_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) + throw Error(ERR_NET, (string) "Unable to set socket options: " + strerror(errno)); +} + +/** + * Free a socket + * No mutex used ! + */ +void Connection::disconnect_() { + assert(socket_fd_ >= 0); + + close(socket_fd_); + socket_fd_ = -1; +} + +/** + * Set local ip + * No mutex used ! + */ +void Connection::setlocalip_() { + struct sockaddr_in addr; + socklen_t len = sizeof addr; + + ::getsockname(socket_fd_, (struct sockaddr*) &addr, &len); + local_ip_ = inet_ntoa(addr.sin_addr); +} + +/** + * Set local port + * No mutex used ! + */ +void Connection::setlocalport_() { + struct sockaddr_in addr; + socklen_t len = sizeof addr; + + ::getsockname(socket_fd_, (struct sockaddr*) &addr, &len); + local_port_ = ntohs(addr.sin_port); +} + +/** + * Set remote ip + * No mutex used ! + */ +void Connection::setremoteip_() { + struct sockaddr_in addr; + socklen_t len = sizeof addr; + + ::getpeername(socket_fd_, (struct sockaddr*) &addr, &len); + remote_ip_ = inet_ntoa(addr.sin_addr); +} + +/** + * Set remote port + * No mutex used ! + */ +void Connection::setremoteport_() { + struct sockaddr_in addr; + socklen_t len = sizeof addr; + + ::getpeername(socket_fd_, (struct sockaddr*) &addr, &len); + remote_port_ = ntohs(addr.sin_port); +} diff --git a/slc/trunk/src/sll/connection.hh b/slc/trunk/src/sll/connection.hh new file mode 100644 index 0000000..194cd85 --- /dev/null +++ b/slc/trunk/src/sll/connection.hh @@ -0,0 +1,96 @@ +/* + This file is part of SLS. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLS 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; version 2 of the License. + + SLS 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 SLS; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef CONNECTION_HH +# define CONNECTION_HH + +# include + +class Connection { + // class methods +public: + unsigned long int getconnid(); + + // public methods +public: + Connection(int fd = -1); + virtual ~Connection(); + + void connect(const char *addr, int port); + void listen(int port, int max); + + void disconnect(); + bool connected(); + + Connection *accept(); + + // send methods + inline void send(const string &data); + void send(const char* buf, size_t len); + inline void sendln(const string &data); + void sendln(const char* buf, size_t len); + + // recv methods + string recvln(); + + // info methods + string getlocalip(); + int getlocalport(); + + string getremoteip(); + int getremoteport(); + + unsigned long int getid(); + + int getsocket(); + + // protected methods +protected: + void socket_(); + void disconnect_(); + + inline void setallinfo_(); + void setlocalip_(); + void setlocalport_(); + void setremoteip_(); + void setremoteport_(); + + // Protected datas +protected: + int socket_fd_; // connection socket + + // storage of info about connection + int local_port_; + int remote_port_; + + string local_ip_; + string remote_ip_; + + unsigned long int id_; + + string rbuf_; // read buffer + string wbuf_; // write buffer + + pthread_mutex_t c_mutex_; // connection mutex + pthread_mutex_t r_mutex_; // read mutex + pthread_mutex_t w_mutex_; // write mutex +}; + +# include "connection.hxx" + +#endif diff --git a/slc/trunk/src/sll/connection.hxx b/slc/trunk/src/sll/connection.hxx new file mode 100644 index 0000000..937ae30 --- /dev/null +++ b/slc/trunk/src/sll/connection.hxx @@ -0,0 +1,49 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL 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; version 2 of the License. + + SLL 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 SLLo; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/******************************************************************************* + ** Public method + ******************************************************************************/ + +/** + * Send @param data on socket + */ +void Connection::send(const string &data) { + send(data.c_str(), data.length()); +} + +/** + * Send @param data followed by '\n' on socket + */ +void Connection::sendln(const string &data) { + sendln(data.c_str(), data.length()); +} + +/******************************************************************************* + ** Protected method + ******************************************************************************/ + +/** + * Set all information about a connection + */ +void Connection::setallinfo_() { + setlocalip_(); + setlocalport_(); + setremoteip_(); + setremoteport_(); +} diff --git a/slc/trunk/src/error.cc b/slc/trunk/src/sll/error.cc similarity index 80% rename from slc/trunk/src/error.cc rename to slc/trunk/src/sll/error.cc index b56f6bf..429335f 100644 --- a/slc/trunk/src/error.cc +++ b/slc/trunk/src/sll/error.cc @@ -1,18 +1,18 @@ /* - This file is part of SLC. + This file is part of SLL. Copyright (C) 2008 Sebastien LUTTRINGER - SLC is free software; you can redistribute it and/or modify + SLL 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; version 2 of the License. - SLC is distributed in the hope that it will be useful, + SLL 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 SLC; if not, write to the Free Software + along with SLL; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -75,6 +75,23 @@ void Error::code(int v) val_ = v; } +/*! +** Concatenate @param s before error message +*/ +void Error::prefix(const string &s) { + string buf = s + msg_.str(); + + msg_.str(""); + msg_ << buf; +} + +/*! +** Concatenate @param s to errror message +*/ +void Error::suffix(const string &s) { + msg_ << s; +} + /*! ** Copy operator */ @@ -124,3 +141,4 @@ Error &operator>>(Error &e, const string &s) e.msg_ << buf; return e; } + diff --git a/slc/trunk/src/error.hh b/slc/trunk/src/sll/error.hh similarity index 74% rename from slc/trunk/src/error.hh rename to slc/trunk/src/sll/error.hh index 8b10cc0..9d9286e 100644 --- a/slc/trunk/src/error.hh +++ b/slc/trunk/src/sll/error.hh @@ -1,46 +1,47 @@ /* - This file is part of SLC. + This file is part of SLL. Copyright (C) 2008 Sebastien LUTTRINGER - SLC is free software; you can redistribute it and/or modify + SLL 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; version 2 of the License. - SLC is distributed in the hope that it will be useful, + SLL 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 SLC; if not, write to the Free Software + along with SLL; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef ERROR_HH # define ERROR_HH -# include -# include -# include - -typedef std::string string; -typedef std::stringstream sstream; -typedef std::ostream ostream; +# include "slm.hh" class Error { public: Error(int i, string s = ""); Error(const Error &e); + void print() const; string message() const; + + void suffix(const string &); + void prefix(const string &); + int code() const; void code(int); + Error &operator=(const Error &rhs); friend ostream &operator<<(ostream &o, const Error &e); friend Error &operator<<(Error &e, const string &s); friend Error &operator>>(Error &e, const string &s); friend Error &operator<<(Error &e, int val); + protected: sstream msg_; int val_; diff --git a/slc/trunk/src/sll/slm.hh b/slc/trunk/src/sll/slm.hh new file mode 100644 index 0000000..97d6271 --- /dev/null +++ b/slc/trunk/src/sll/slm.hh @@ -0,0 +1,61 @@ +/* + This file is part of SLL. + Copyright (C) 2008 Sebastien LUTTRINGER + + SLL 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; version 2 of the License. + + SLL 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 SLL; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef SLM_HH +# define SLM_HH + +# define _REENTRANT + +# include +# include +# include +# include +# include + +typedef std::string string; +typedef std::stringstream sstream; +typedef std::ostream ostream; +typedef std::ifstream ifstream; +typedef std::ofstream ofstream; + +enum { + ERR_NO = 0, + ERR_USAGE = 1, + ERR_BADPARAM = 2, + ERR_FILE = 3, + ERR_NET = 4, + ERR_PROTO = 5, + ERR_AUTH = 6, + ERR_DB = 7, + ERR_SRV = 8, + ERR_THREAD = 9, + ERR_PARSE = 10, + ERR_SCREENSZ = 11, + ERR_NOMEM = 41, + ERR_UNKNOWN = 42 +}; + +// ----------------------------------------------------------------------------- +// Gonstant +// ----------------------------------------------------------------------------- + +static const string VERSION = (string) "testing.\nCompiled: " + __DATE__ + " " + __TIME__ + "."; +//static const string VERSION = "1.0"; +static const int MAX_LINE_SIZE = 512; + +#endif -- GitLab