Loading slc/trunk/src/connection.cc +35 −8 Original line number Diff line number Diff line Loading @@ -10,13 +10,18 @@ #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; return socket_ > 0; } void Connection::start() { if (connected()) return; // make connection connect_(O.server.c_str(), O.port); Loading @@ -24,11 +29,13 @@ void Connection::start() { // send pass info // Start reader thread pthread_create(&g_thread_reader, 0, thread_reader, 0); } void Connection::stop() { if (!connected()) return; disconnect_(); } Loading @@ -47,12 +54,15 @@ void Connection::connect_(const char *addr, int port) { } // 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); Loading @@ -63,18 +73,26 @@ void Connection::connect_(const char *addr, int port) { << ") on port " << port << "...\n"; // connect if (connect(socket_, (struct sockaddr *) &daddr, sizeof daddr) == -1) throw Error(ERR_NET, strerror(errno)); 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); return; } S << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr) << ") on port " << port << "...\n"; } void Connection::disconnect_() { if (socket_ == 0) return; assert(socket_ > 0); pthread_mutex_lock(&mtx_socket); close(socket_); socket_ = 0; pthread_mutex_unlock(&mtx_socket); } Loading @@ -83,7 +101,10 @@ void Connection::send(const string &data) { } 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) { Loading @@ -91,13 +112,19 @@ void Connection::sendln(const string &data) { } 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); char buf[MAX_LINE_SIZE]; pthread_mutex_lock(&mtx_socket); read(socket_, buf, MAX_LINE_SIZE); pthread_mutex_unlock(&mtx_socket); return buf; } slc/trunk/src/screen.cc +1 −10 Original line number Diff line number Diff line Loading @@ -284,15 +284,6 @@ void Screen::msg_print(int i) { msg_buffer_ << i; } /*! ** Read and print socket ** */ void Screen::socket_reader() { while (1) msg_println(C.recvln()); } /* ********************************************************************************* Loading Loading @@ -365,7 +356,7 @@ void Screen::msg_draw() { strncpy(msg_win_buffer_, (*line).c_str() + print_offset, COLS - ((i == 1) ? 2 : 4)); msg_win_buffer_[COLS - ((i == 1) ? 2 : 4)] = 0; // print line into screen mvwprintw(msg_, free_lines--, 1, (i == 1) ? "%s" : "> %s", msg_win_buffer_); mvwprintw(msg_, free_lines--, 1, (i == 1) ? "%s" : "| %s", msg_win_buffer_); } } } Loading slc/trunk/src/slc.cc +6 −9 Original line number Diff line number Diff line #include <getopt.h> #include <pthread.h> #include "slc.hh" #include "options.hh" Loading @@ -14,7 +13,7 @@ Options O; History H; Connection C; void main2(); pthread_t g_thread_reader; /*! ** Program entry point Loading @@ -26,8 +25,6 @@ void main2(); */ int main(int argc, char *argv[]) { pthread_t t; try { // get argvline options O.load(argc, argv); Loading @@ -50,9 +47,6 @@ int main(int argc, char *argv[]) // start connexion C.start(); // Start reader thread pthread_create(&t, 0, (void *(*)(void *)) main2, 0); // Treat keyboards events S.eventsloop(); Loading @@ -73,6 +67,9 @@ int main(int argc, char *argv[]) ** Entry point of the second thread which is used to ** print data sent by server */ void main2() { S.socket_reader(); void *thread_reader(void *) { while (C.connected()) S << "<< " << C.recvln() << "\n"; pthread_exit(0); return 0; } slc/trunk/src/slc.hh +12 −1 Original line number Diff line number Diff line #ifndef SLC_HH # define SLC_HH # define _REENTRANT # include <cassert> # include <ios> # include <iostream> Loading Loading @@ -39,7 +41,7 @@ static const string RCV_DATA = "<< "; static const string SND_DATA = ">> "; // ----------------------------------------------------------------------------- // Global // Global var // ----------------------------------------------------------------------------- class Options; Loading @@ -52,4 +54,13 @@ extern Screen S; extern History H; extern Connection C; extern pthread_t g_thread_reader; // ----------------------------------------------------------------------------- // Global func // ----------------------------------------------------------------------------- int main(int, char **); void *thread_reader(void *); #endif Loading
slc/trunk/src/connection.cc +35 −8 Original line number Diff line number Diff line Loading @@ -10,13 +10,18 @@ #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; return socket_ > 0; } void Connection::start() { if (connected()) return; // make connection connect_(O.server.c_str(), O.port); Loading @@ -24,11 +29,13 @@ void Connection::start() { // send pass info // Start reader thread pthread_create(&g_thread_reader, 0, thread_reader, 0); } void Connection::stop() { if (!connected()) return; disconnect_(); } Loading @@ -47,12 +54,15 @@ void Connection::connect_(const char *addr, int port) { } // 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); Loading @@ -63,18 +73,26 @@ void Connection::connect_(const char *addr, int port) { << ") on port " << port << "...\n"; // connect if (connect(socket_, (struct sockaddr *) &daddr, sizeof daddr) == -1) throw Error(ERR_NET, strerror(errno)); 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); return; } S << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr) << ") on port " << port << "...\n"; } void Connection::disconnect_() { if (socket_ == 0) return; assert(socket_ > 0); pthread_mutex_lock(&mtx_socket); close(socket_); socket_ = 0; pthread_mutex_unlock(&mtx_socket); } Loading @@ -83,7 +101,10 @@ void Connection::send(const string &data) { } 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) { Loading @@ -91,13 +112,19 @@ void Connection::sendln(const string &data) { } 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); char buf[MAX_LINE_SIZE]; pthread_mutex_lock(&mtx_socket); read(socket_, buf, MAX_LINE_SIZE); pthread_mutex_unlock(&mtx_socket); return buf; }
slc/trunk/src/screen.cc +1 −10 Original line number Diff line number Diff line Loading @@ -284,15 +284,6 @@ void Screen::msg_print(int i) { msg_buffer_ << i; } /*! ** Read and print socket ** */ void Screen::socket_reader() { while (1) msg_println(C.recvln()); } /* ********************************************************************************* Loading Loading @@ -365,7 +356,7 @@ void Screen::msg_draw() { strncpy(msg_win_buffer_, (*line).c_str() + print_offset, COLS - ((i == 1) ? 2 : 4)); msg_win_buffer_[COLS - ((i == 1) ? 2 : 4)] = 0; // print line into screen mvwprintw(msg_, free_lines--, 1, (i == 1) ? "%s" : "> %s", msg_win_buffer_); mvwprintw(msg_, free_lines--, 1, (i == 1) ? "%s" : "| %s", msg_win_buffer_); } } } Loading
slc/trunk/src/slc.cc +6 −9 Original line number Diff line number Diff line #include <getopt.h> #include <pthread.h> #include "slc.hh" #include "options.hh" Loading @@ -14,7 +13,7 @@ Options O; History H; Connection C; void main2(); pthread_t g_thread_reader; /*! ** Program entry point Loading @@ -26,8 +25,6 @@ void main2(); */ int main(int argc, char *argv[]) { pthread_t t; try { // get argvline options O.load(argc, argv); Loading @@ -50,9 +47,6 @@ int main(int argc, char *argv[]) // start connexion C.start(); // Start reader thread pthread_create(&t, 0, (void *(*)(void *)) main2, 0); // Treat keyboards events S.eventsloop(); Loading @@ -73,6 +67,9 @@ int main(int argc, char *argv[]) ** Entry point of the second thread which is used to ** print data sent by server */ void main2() { S.socket_reader(); void *thread_reader(void *) { while (C.connected()) S << "<< " << C.recvln() << "\n"; pthread_exit(0); return 0; }
slc/trunk/src/slc.hh +12 −1 Original line number Diff line number Diff line #ifndef SLC_HH # define SLC_HH # define _REENTRANT # include <cassert> # include <ios> # include <iostream> Loading Loading @@ -39,7 +41,7 @@ static const string RCV_DATA = "<< "; static const string SND_DATA = ">> "; // ----------------------------------------------------------------------------- // Global // Global var // ----------------------------------------------------------------------------- class Options; Loading @@ -52,4 +54,13 @@ extern Screen S; extern History H; extern Connection C; extern pthread_t g_thread_reader; // ----------------------------------------------------------------------------- // Global func // ----------------------------------------------------------------------------- int main(int, char **); void *thread_reader(void *); #endif