Loading sld/trunk/Makefile.am +2 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ sld_SOURCES= src/sld.cc \ src/daemon.cc \ src/log.cc \ src/sll/error.cc \ src/sll/buffer.cc \ src/sll/connection.cc \ src/sll/cypher.cc Loading @@ -30,6 +31,7 @@ noinst_HEADER= src/sld.hh \ src/log.hh \ src/sll/slm.hh \ src/sll/error.hh \ src/sll/buffer.hh \ src/sll/connection.hh \ src/sll/connection.hxx \ src/sll/cypher.hh Loading sld/trunk/src/sll/connection.cc +107 −38 Original line number Diff line number Diff line Loading @@ -44,7 +44,8 @@ unsigned long int Connection::getconnid() { * * @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) { Connection::Connection(int fd, bool bufferized) : socket_fd_(fd), local_port_(-1), remote_port_(-1), id_(0), bufferized_(bufferized) { pthread_mutex_init(&c_mutex_, 0); pthread_mutex_init(&r_mutex_, 0); pthread_mutex_init(&w_mutex_, 0); Loading Loading @@ -74,14 +75,8 @@ void Connection::disconnect() { throw Error(ERR_NET, "No connection established but trying to disconnect"); } // lock all 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_); } Loading Loading @@ -282,7 +277,7 @@ Connection *Connection::accept() { /** * Send data on @param buf of size @param len on socket */ void Connection::send(const char* buf, size_t len) { void Connection::send(const void* buf, size_t len) { // lock mutex for operation pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); Loading @@ -297,24 +292,59 @@ void Connection::send(const char* buf, size_t len) { throw Error(ERR_NET, "No connection established but trying to send data"); } // send is a write operation int ret = ::send(socket_fd_, buf, len, MSG_NOSIGNAL); // call send try { send_(socket_fd, buf, len); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&r_mutex_); disconnect_(); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); throw; } // release the mutex pthread_mutex_unlock(&w_mutex_); } /** * Free read buffer and send write buffer if connected * */ void Connection::flush() { if (!bufferized_) return; // treat error if (ret == -1) { int errno__ = errno; // lock mutex for operation pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); // retreive socket_fd and free conn mutex int socket_fd = socket_fd_; pthread_mutex_unlock(&c_mutex_); // test socket validity if (socket_fd < 0) { pthread_mutex_unlock(&w_mutex_); throw Error(ERR_NET, "No connection established but trying to flush data"); } // call send try { send_(socket_fd, rbuf_.c_str(), rbuf_.length()); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&r_mutex_); disconnect_(); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); if (errno__ == ECONNRESET || errno__ == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else throw Error(ERR_NET, "send: " + (string) strerror(errno__)); throw; } // clean read buffer rbuf_.clear(); // release the mutex pthread_mutex_unlock(&w_mutex_); } Loading @@ -322,9 +352,53 @@ void Connection::send(const char* buf, size_t len) { /** * Receive raw data */ char *Connection::recv(size_t size) { assert(0); return 0; int Connection::recv(void *buf, size_t size) { // lock mutex for operation pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&r_mutex_); int socket_fd = socket_fd_; pthread_mutex_unlock(&c_mutex_); // test socket validity if (socket_fd < 0) { pthread_mutex_unlock(&r_mutex_); throw Error(ERR_NET, "No connection established but trying to receive line"); } int ret = 0; if (rbuf_.size() > 0) { size_t msize = (rbuf.length() > size) ? size : rbuf.length(); memcpy(buf, rbuf_.c_str(), msize); ret += msize; rbuf_.remove(0, msize); } if (ret < size) { static char local_buf[MAX_LINE_SIZE]; try { ret = recv_(socket_fd, local_buf, MAX_LINE_SIZE); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); *local_buf = 0; disconnect_(); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); throw; } local_buf[ret] = 0; } // add read data in buffer rbuf_ += local_buf; } while (1); return ret; } /** Loading Loading @@ -356,22 +430,17 @@ string Connection::recvln() { // read data static char local_buf[MAX_LINE_SIZE]; int ret = ::recv(socket_fd, local_buf, MAX_LINE_SIZE, 0); if (ret == -1 || ret == 0) { try { recv_(socket_fd, local_buf, MAX_LINE_SIZE); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); *local_buf = 0; disconnect_(); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); *local_buf = 0; if (ret == 0) throw Error(ERR_NET, "Connection reset by peer"); else throw Error(ERR_NET, (string) "recvln: " + strerror(errno)); throw; } local_buf[ret] = 0; Loading @@ -379,11 +448,7 @@ string Connection::recvln() { rbuf_ += local_buf; } while (1); assert(1); } void Connection::flush() { assert(0); abort(); } string Connection::getlocalip() { Loading Loading @@ -509,10 +574,6 @@ int Connection::getremoteport() { return port; } unsigned long int Connection::getid() { return id_; } int Connection::getsocket() { // get socket is a conn op pthread_mutex_lock(&c_mutex_); Loading @@ -525,6 +586,14 @@ int Connection::getsocket() { return socket_fd; } unsigned long int Connection::getid() const { return id_; } bool Connection::isbufferized() const { return bufferized_; } /******************************************************************************* ** Protected method ******************************************************************************/ sld/trunk/src/sll/connection.hh +11 −5 Original line number Diff line number Diff line Loading @@ -28,7 +28,7 @@ public: // public methods public: Connection(int fd = -1); Connection(int fd = -1, bool bufferized = false); virtual ~Connection(); void connect(const char *addr, int port); Loading @@ -40,12 +40,12 @@ public: Connection *accept(); // send methods void send(const char* buf, size_t len); void send(const void* buf, size_t len); inline void send(const string &data); inline void sendln(const string &data); // recv methods char *recv(size_t size); int recv(void *buf, size_t size); string recvln(); // buffer methods Loading @@ -60,15 +60,19 @@ public: string getremotehostname(); int getremoteport(); unsigned long int getid(); int getsocket(); unsigned long int getid() const; bool isbufferized() const; // protected methods protected: inline void socket_(); inline void disconnect_(); inline void send_(int socket_fd, const void *buf, size_t size); inline int recv_(int socket_fd, void *buf, size_t size); inline void setlocalip_(); inline void setlocalport_(); inline void setremoteip_(); Loading @@ -90,6 +94,8 @@ protected: unsigned long int id_; bool bufferized_; string rbuf_; // read buffer string wbuf_; // write buffer Loading sld/trunk/src/sll/connection.hxx +30 −1 Original line number Diff line number Diff line Loading @@ -70,10 +70,13 @@ void Connection::sendln(const string &data) { * No mutex used ! */ void Connection::disconnect_() { assert(socket_fd_ >= 0); if (socket_fd_ < 0) return; close(socket_fd_); socket_fd_ = -1; rbuf_.clear(); wbuf_.clear(); } /** Loading @@ -91,6 +94,32 @@ void Connection::socket_() { throw Error(ERR_NET, (string) "Unable to set socket options: " + strerror(errno)); } /** * Send @param size byte of @param buf into socket @param socket * No mutex used! */ void Connection::send_(int socket_fd, const void *buf, size_t size) { if (::send(socket_fd, buf, size, MSG_NOSIGNAL)) { if (errno == ECONNRESET || errno == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else throw Error(ERR_NET, "send: " + (string) strerror(errno)); } } /** * Send @param size byte of @param buf into socket @param socket * No mutex used! */ int Connection::recv_(int socket_fd, void *buf, size_t size) { int ret = ::recv(socket_fd, buf, size, 0); if (ret == 0) throw Error(ERR_NET, "Connection reset by peer"); else if (ret == -1) throw Error(ERR_NET, (string) "recvln: " + strerror(errno)); return ret; } /** * Set local ip * No mutex used ! Loading sll/trunk/connection.cc +2 −1 Original line number Diff line number Diff line Loading @@ -323,7 +323,8 @@ void Connection::send(const char* buf, size_t len) { * Receive raw data */ char *Connection::recv(size_t size) { assert(0); abort(); size = size + 1; return 0; } Loading Loading
sld/trunk/Makefile.am +2 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ sld_SOURCES= src/sld.cc \ src/daemon.cc \ src/log.cc \ src/sll/error.cc \ src/sll/buffer.cc \ src/sll/connection.cc \ src/sll/cypher.cc Loading @@ -30,6 +31,7 @@ noinst_HEADER= src/sld.hh \ src/log.hh \ src/sll/slm.hh \ src/sll/error.hh \ src/sll/buffer.hh \ src/sll/connection.hh \ src/sll/connection.hxx \ src/sll/cypher.hh Loading
sld/trunk/src/sll/connection.cc +107 −38 Original line number Diff line number Diff line Loading @@ -44,7 +44,8 @@ unsigned long int Connection::getconnid() { * * @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) { Connection::Connection(int fd, bool bufferized) : socket_fd_(fd), local_port_(-1), remote_port_(-1), id_(0), bufferized_(bufferized) { pthread_mutex_init(&c_mutex_, 0); pthread_mutex_init(&r_mutex_, 0); pthread_mutex_init(&w_mutex_, 0); Loading Loading @@ -74,14 +75,8 @@ void Connection::disconnect() { throw Error(ERR_NET, "No connection established but trying to disconnect"); } // lock all 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_); } Loading Loading @@ -282,7 +277,7 @@ Connection *Connection::accept() { /** * Send data on @param buf of size @param len on socket */ void Connection::send(const char* buf, size_t len) { void Connection::send(const void* buf, size_t len) { // lock mutex for operation pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); Loading @@ -297,24 +292,59 @@ void Connection::send(const char* buf, size_t len) { throw Error(ERR_NET, "No connection established but trying to send data"); } // send is a write operation int ret = ::send(socket_fd_, buf, len, MSG_NOSIGNAL); // call send try { send_(socket_fd, buf, len); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&r_mutex_); disconnect_(); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); throw; } // release the mutex pthread_mutex_unlock(&w_mutex_); } /** * Free read buffer and send write buffer if connected * */ void Connection::flush() { if (!bufferized_) return; // treat error if (ret == -1) { int errno__ = errno; // lock mutex for operation pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); // retreive socket_fd and free conn mutex int socket_fd = socket_fd_; pthread_mutex_unlock(&c_mutex_); // test socket validity if (socket_fd < 0) { pthread_mutex_unlock(&w_mutex_); throw Error(ERR_NET, "No connection established but trying to flush data"); } // call send try { send_(socket_fd, rbuf_.c_str(), rbuf_.length()); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&r_mutex_); disconnect_(); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); if (errno__ == ECONNRESET || errno__ == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else throw Error(ERR_NET, "send: " + (string) strerror(errno__)); throw; } // clean read buffer rbuf_.clear(); // release the mutex pthread_mutex_unlock(&w_mutex_); } Loading @@ -322,9 +352,53 @@ void Connection::send(const char* buf, size_t len) { /** * Receive raw data */ char *Connection::recv(size_t size) { assert(0); return 0; int Connection::recv(void *buf, size_t size) { // lock mutex for operation pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&r_mutex_); int socket_fd = socket_fd_; pthread_mutex_unlock(&c_mutex_); // test socket validity if (socket_fd < 0) { pthread_mutex_unlock(&r_mutex_); throw Error(ERR_NET, "No connection established but trying to receive line"); } int ret = 0; if (rbuf_.size() > 0) { size_t msize = (rbuf.length() > size) ? size : rbuf.length(); memcpy(buf, rbuf_.c_str(), msize); ret += msize; rbuf_.remove(0, msize); } if (ret < size) { static char local_buf[MAX_LINE_SIZE]; try { ret = recv_(socket_fd, local_buf, MAX_LINE_SIZE); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); *local_buf = 0; disconnect_(); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); throw; } local_buf[ret] = 0; } // add read data in buffer rbuf_ += local_buf; } while (1); return ret; } /** Loading Loading @@ -356,22 +430,17 @@ string Connection::recvln() { // read data static char local_buf[MAX_LINE_SIZE]; int ret = ::recv(socket_fd, local_buf, MAX_LINE_SIZE, 0); if (ret == -1 || ret == 0) { try { recv_(socket_fd, local_buf, MAX_LINE_SIZE); } catch (...) { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); *local_buf = 0; disconnect_(); pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); *local_buf = 0; if (ret == 0) throw Error(ERR_NET, "Connection reset by peer"); else throw Error(ERR_NET, (string) "recvln: " + strerror(errno)); throw; } local_buf[ret] = 0; Loading @@ -379,11 +448,7 @@ string Connection::recvln() { rbuf_ += local_buf; } while (1); assert(1); } void Connection::flush() { assert(0); abort(); } string Connection::getlocalip() { Loading Loading @@ -509,10 +574,6 @@ int Connection::getremoteport() { return port; } unsigned long int Connection::getid() { return id_; } int Connection::getsocket() { // get socket is a conn op pthread_mutex_lock(&c_mutex_); Loading @@ -525,6 +586,14 @@ int Connection::getsocket() { return socket_fd; } unsigned long int Connection::getid() const { return id_; } bool Connection::isbufferized() const { return bufferized_; } /******************************************************************************* ** Protected method ******************************************************************************/
sld/trunk/src/sll/connection.hh +11 −5 Original line number Diff line number Diff line Loading @@ -28,7 +28,7 @@ public: // public methods public: Connection(int fd = -1); Connection(int fd = -1, bool bufferized = false); virtual ~Connection(); void connect(const char *addr, int port); Loading @@ -40,12 +40,12 @@ public: Connection *accept(); // send methods void send(const char* buf, size_t len); void send(const void* buf, size_t len); inline void send(const string &data); inline void sendln(const string &data); // recv methods char *recv(size_t size); int recv(void *buf, size_t size); string recvln(); // buffer methods Loading @@ -60,15 +60,19 @@ public: string getremotehostname(); int getremoteport(); unsigned long int getid(); int getsocket(); unsigned long int getid() const; bool isbufferized() const; // protected methods protected: inline void socket_(); inline void disconnect_(); inline void send_(int socket_fd, const void *buf, size_t size); inline int recv_(int socket_fd, void *buf, size_t size); inline void setlocalip_(); inline void setlocalport_(); inline void setremoteip_(); Loading @@ -90,6 +94,8 @@ protected: unsigned long int id_; bool bufferized_; string rbuf_; // read buffer string wbuf_; // write buffer Loading
sld/trunk/src/sll/connection.hxx +30 −1 Original line number Diff line number Diff line Loading @@ -70,10 +70,13 @@ void Connection::sendln(const string &data) { * No mutex used ! */ void Connection::disconnect_() { assert(socket_fd_ >= 0); if (socket_fd_ < 0) return; close(socket_fd_); socket_fd_ = -1; rbuf_.clear(); wbuf_.clear(); } /** Loading @@ -91,6 +94,32 @@ void Connection::socket_() { throw Error(ERR_NET, (string) "Unable to set socket options: " + strerror(errno)); } /** * Send @param size byte of @param buf into socket @param socket * No mutex used! */ void Connection::send_(int socket_fd, const void *buf, size_t size) { if (::send(socket_fd, buf, size, MSG_NOSIGNAL)) { if (errno == ECONNRESET || errno == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else throw Error(ERR_NET, "send: " + (string) strerror(errno)); } } /** * Send @param size byte of @param buf into socket @param socket * No mutex used! */ int Connection::recv_(int socket_fd, void *buf, size_t size) { int ret = ::recv(socket_fd, buf, size, 0); if (ret == 0) throw Error(ERR_NET, "Connection reset by peer"); else if (ret == -1) throw Error(ERR_NET, (string) "recvln: " + strerror(errno)); return ret; } /** * Set local ip * No mutex used ! Loading
sll/trunk/connection.cc +2 −1 Original line number Diff line number Diff line Loading @@ -323,7 +323,8 @@ void Connection::send(const char* buf, size_t len) { * Receive raw data */ char *Connection::recv(size_t size) { assert(0); abort(); size = size + 1; return 0; } Loading