Loading slc/trunk/src/connection.cc +22 −4 Original line number Diff line number Diff line Loading @@ -12,6 +12,10 @@ Connection::Connection() : socket_(0) {} bool Connection::connected() const { return socket_ != 0; } void Connection::start() { // make connection connect_(O.server.c_str(), O.port); Loading @@ -20,6 +24,8 @@ void Connection::start() { // send pass info } void Connection::stop() { Loading @@ -35,13 +41,18 @@ void Connection::connect_(const char *addr, int port) { // retrieve remote host info h = gethostbyname(addr); if (h == 0) throw Error(ERR_NET, (string) "gethostbyname(" + addr + "): " + hstrerror(h_errno)); if (h == 0) { S << "Unable to resolve: " << addr << ": " << hstrerror(h_errno) << ".\n"; return; } // create socket socket_ = socket(PF_INET, SOCK_STREAM, 0); if (socket_ == -1) throw Error(ERR_NET, (string) "socket: " + strerror(errno)); if (socket_ == -1) { S << "Unable to create socket: " << strerror(errno) << ".\n"; socket_ = 0; return; } daddr.sin_family = AF_INET; daddr.sin_port = htons(port); Loading Loading @@ -83,3 +94,10 @@ void Connection::sendln(const char* buf, size_t len) { write(socket_, buf, len); write(socket_, "\n", 1); } string Connection::recvln() { char buf[MAX_LINE_SIZE]; read(socket_, buf, MAX_LINE_SIZE); return buf; } slc/trunk/src/connection.hh +2 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,8 @@ class Connection { public: Connection(); bool connected() const; void start(); void stop(); void send(const string &data); Loading slc/trunk/src/screen.cc +16 −4 Original line number Diff line number Diff line Loading @@ -284,6 +284,15 @@ 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 @@ -510,20 +519,23 @@ void Screen::cmd_exec(const char *s) { exit(0); } else if (!strcmp(s, "/logout")) { C.stop(); S << s << "\n"; C.stop(); } else if (!strcmp(s, "/login")) { C.start(); S << s << "\n"; C.start(); } else S << s << ": Unkown command.\n"; } // String is a remote command else { if (C.connected()) { C.sendln(s, strlen(s)); msg_println(s); S << ">> " << s << "\n"; } else S << "Unable to send: " << s << ". Try to reconnect with /login !\n"; } } Loading slc/trunk/src/screen.hh +1 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ public: void msg_println(int i); void msg_print(const string &s); void msg_print(int i); void socket_reader(); private: void create_windows(); Loading slc/trunk/src/slc.cc +18 −1 Original line number Diff line number Diff line #include <getopt.h> #include <pthread.h> #include "slc.hh" #include "options.hh" Loading @@ -13,6 +14,8 @@ Options O; History H; Connection C; void main2(); /*! ** Program entry point ** Loading @@ -23,6 +26,8 @@ Connection C; */ int main(int argc, char *argv[]) { pthread_t t; try { // get argvline options O.load(argc, argv); Loading @@ -42,9 +47,12 @@ int main(int argc, char *argv[]) // Showing dialog screen S.dialog(); // start connexion. Launch a thread. // start connexion C.start(); // Start reader thread pthread_create(&t, 0, (void *(*)(void *)) main2, 0); // Treat keyboards events S.eventsloop(); Loading @@ -59,3 +67,12 @@ int main(int argc, char *argv[]) return 0; } /*! ** Entry point of the second thread which is used to ** print data sent by server */ void main2() { S.socket_reader(); } Loading
slc/trunk/src/connection.cc +22 −4 Original line number Diff line number Diff line Loading @@ -12,6 +12,10 @@ Connection::Connection() : socket_(0) {} bool Connection::connected() const { return socket_ != 0; } void Connection::start() { // make connection connect_(O.server.c_str(), O.port); Loading @@ -20,6 +24,8 @@ void Connection::start() { // send pass info } void Connection::stop() { Loading @@ -35,13 +41,18 @@ void Connection::connect_(const char *addr, int port) { // retrieve remote host info h = gethostbyname(addr); if (h == 0) throw Error(ERR_NET, (string) "gethostbyname(" + addr + "): " + hstrerror(h_errno)); if (h == 0) { S << "Unable to resolve: " << addr << ": " << hstrerror(h_errno) << ".\n"; return; } // create socket socket_ = socket(PF_INET, SOCK_STREAM, 0); if (socket_ == -1) throw Error(ERR_NET, (string) "socket: " + strerror(errno)); if (socket_ == -1) { S << "Unable to create socket: " << strerror(errno) << ".\n"; socket_ = 0; return; } daddr.sin_family = AF_INET; daddr.sin_port = htons(port); Loading Loading @@ -83,3 +94,10 @@ void Connection::sendln(const char* buf, size_t len) { write(socket_, buf, len); write(socket_, "\n", 1); } string Connection::recvln() { char buf[MAX_LINE_SIZE]; read(socket_, buf, MAX_LINE_SIZE); return buf; }
slc/trunk/src/connection.hh +2 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,8 @@ class Connection { public: Connection(); bool connected() const; void start(); void stop(); void send(const string &data); Loading
slc/trunk/src/screen.cc +16 −4 Original line number Diff line number Diff line Loading @@ -284,6 +284,15 @@ 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 @@ -510,20 +519,23 @@ void Screen::cmd_exec(const char *s) { exit(0); } else if (!strcmp(s, "/logout")) { C.stop(); S << s << "\n"; C.stop(); } else if (!strcmp(s, "/login")) { C.start(); S << s << "\n"; C.start(); } else S << s << ": Unkown command.\n"; } // String is a remote command else { if (C.connected()) { C.sendln(s, strlen(s)); msg_println(s); S << ">> " << s << "\n"; } else S << "Unable to send: " << s << ". Try to reconnect with /login !\n"; } } Loading
slc/trunk/src/screen.hh +1 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ public: void msg_println(int i); void msg_print(const string &s); void msg_print(int i); void socket_reader(); private: void create_windows(); Loading
slc/trunk/src/slc.cc +18 −1 Original line number Diff line number Diff line #include <getopt.h> #include <pthread.h> #include "slc.hh" #include "options.hh" Loading @@ -13,6 +14,8 @@ Options O; History H; Connection C; void main2(); /*! ** Program entry point ** Loading @@ -23,6 +26,8 @@ Connection C; */ int main(int argc, char *argv[]) { pthread_t t; try { // get argvline options O.load(argc, argv); Loading @@ -42,9 +47,12 @@ int main(int argc, char *argv[]) // Showing dialog screen S.dialog(); // start connexion. Launch a thread. // start connexion C.start(); // Start reader thread pthread_create(&t, 0, (void *(*)(void *)) main2, 0); // Treat keyboards events S.eventsloop(); Loading @@ -59,3 +67,12 @@ int main(int argc, char *argv[]) return 0; } /*! ** Entry point of the second thread which is used to ** print data sent by server */ void main2() { S.socket_reader(); }