From ad6380fbfd67d618f1dcdbc3e2fd57d98e4a5c46 Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Sun, 21 Dec 2008 20:12:05 +0000 Subject: rewrote syncClient and anytun-showtables --- src/anytun-showtables.cpp | 73 +++++++++++++++++------------------ src/syncClient.cpp | 97 ++++++++++++++++------------------------------- src/syncClient.h | 10 ++--- 3 files changed, 71 insertions(+), 109 deletions(-) (limited to 'src') diff --git a/src/anytun-showtables.cpp b/src/anytun-showtables.cpp index d3e8569..0ba8bdb 100644 --- a/src/anytun-showtables.cpp +++ b/src/anytun-showtables.cpp @@ -48,8 +48,9 @@ #include -void output(ConnectionList &cl) +void output() { + ConnectionList &cl(gConnectionList); if( !cl.empty() ) { ConnectionMap::iterator it = cl.getBeginUnlocked(); @@ -74,7 +75,7 @@ void output(ConnectionList &cl) { RoutingMap::iterator it = gRoutingTable.getBeginUnlocked(type); NetworkPrefix pref( it->first ); - std::cout << "Route: " << pref.toString() << "/" << pref.getNetworkPrefixLength() << " -> "; + std::cout << "Route: " << pref.toString() << "/" << (int)pref.getNetworkPrefixLength() << " -> "; mux_t mux = it->second; std::cout << mux << std::endl; gRoutingTable.clear(type); @@ -83,49 +84,43 @@ void output(ConnectionList &cl) } } +void readExactly(size_t toread, std::iostream & result) +{ + size_t hasread = 0; + while (toread > hasread && std::cin.good()) + { + char a[1]; + std::cin.read(a,1); + result.write(a,1); + hasread++; + } +} + +void readAndProcessOne() +{ + size_t message_lenght ; + std::stringstream message_lenght_stream; + readExactly(5,message_lenght_stream); + message_lenght_stream >> message_lenght; + std::stringstream void_stream; + readExactly(1,void_stream); //skip space + std::stringstream sync_command_stream; + readExactly(message_lenght, sync_command_stream); + //std::cout << message_lenght << std::endl; + //std::cout << sync_command_stream.str()<< std::endl; + boost::archive::text_iarchive ia(sync_command_stream); + SyncCommand scom(gConnectionList); + ia >> scom; +} + int main(int argc, char* argv[]) { int ret = 0; - ConnectionList cl; - std::stringstream iss_; - int32_t missing_chars=-1; - int32_t buffer_size_=0; while( std::cin.good() ) { - char c; - std::cin.get(c); - iss_ << c; - buffer_size_++; - while (1) - { - if(missing_chars==-1 && buffer_size_>5) - { - char * buffer = new char [6+1]; - iss_.read(buffer,6); - std::stringstream tmp; - tmp.write(buffer,6); - tmp>>missing_chars; - delete[] buffer; - buffer_size_-=6; - } - else if( missing_chars>0 && missing_chars<=buffer_size_ ) - { - char * buffer = new char [missing_chars+1]; - iss_.read(buffer,missing_chars); - std::stringstream tmp; - tmp.write(buffer,missing_chars); - boost::archive::text_iarchive ia(tmp); - SyncCommand scom(cl); - ia >> scom; - buffer_size_-=missing_chars; - missing_chars=-1; - output(cl); - delete[] buffer; - } - else - break; - } + readAndProcessOne(); + output(); } return ret; } diff --git a/src/syncClient.cpp b/src/syncClient.cpp index c6be0d8..2cc1f91 100644 --- a/src/syncClient.cpp +++ b/src/syncClient.cpp @@ -45,7 +45,7 @@ SyncClient::SyncClient(std::string hostname,std::string port) -:hostname_( hostname),port_(port),missing_chars(-1) +:hostname_( hostname),port_(port) { } @@ -74,20 +74,7 @@ void SyncClient::run() if (!connected) cLog.msg(Log::PRIO_NOTICE) << "sync: connected to " << hostname_ <<":"<< port_; connected=true; - for (;;) - { - boost::array buf; - boost::system::error_code error; - - size_t len = socket.read_some(boost::asio::buffer(buf), error); - - if (error == boost::asio::error::eof) - break; // Connection closed cleanly by peer. - else if (error) - throw boost::system::system_error(error); // Some other error. - - OnRawData(buf.data(), len); - } + readAndProcess(socket); //endless loop } catch (std::exception& e) { @@ -99,59 +86,41 @@ void SyncClient::run() } } -void SyncClient::OnRawData(const char *buf,size_t len) -//void SyncClientSocket::OnLine(const std::string& line) +void SyncClient::readAndProcess(SyncTcpConnection::proto::socket & socket) { ConnectionList & cl_ (gConnectionList); - for(size_t index=0;index> message_lenght; + std::stringstream void_stream; + readExactly(socket,1,void_stream); //skip space + std::stringstream sync_command_stream; + readExactly(socket,message_lenght, sync_command_stream); + //cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<> scom; } - while (1) +} + +void SyncClient::readExactly(SyncTcpConnection::proto::socket & socket,size_t toread, std::iostream & result) +{ + size_t hasread = 0; + while (toread > hasread) { -// cLog.msg(Log::PRIO_NOTICE) << "buffer size "<< buffer_size_ << " missing_chars " << missing_chars; - if(missing_chars==-1 && buffer_size_>5) - { - char * buffer = new char [6+1]; - iss_.read(buffer,6); - std::stringstream tmp; - tmp.write(buffer,6); - tmp>>missing_chars; -// cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<0 && missing_chars<=buffer_size_) - { - char * buffer = new char [missing_chars+1]; - iss_.read(buffer,missing_chars); - std::stringstream tmp; - tmp.write(buffer,missing_chars); -// cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion from " << GetRemoteHostname() <<" \""<> scom; - buffer_size_-=missing_chars; - missing_chars=-1; - delete[] buffer; - } else - break; + //TODO read bigger buffers + boost::array buf; + boost::system::error_code error; + size_t len = socket.read_some(boost::asio::buffer(buf), error); + if (error == boost::asio::error::eof) + break; // Connection closed cleanly by peer. + else if (error) + throw boost::system::system_error(error); // Some other error. + //for (size_t pos=0; possecond; - //cLog.msg(Log::PRIO_NOTICE) << "sync connection #"< #include #include -#include - +//#include +#include "syncTcpConnection.h" class SyncClient { public: @@ -45,12 +45,10 @@ public: void run(); private: - void OnRawData(const char *buf,size_t len); + void readAndProcess(SyncTcpConnection::proto::socket & socket); + void readExactly(SyncTcpConnection::proto::socket & socket,size_t toread, std::iostream &); std::string hostname_; std::string port_; - std::stringstream iss_; - int32_t missing_chars; - int32_t buffer_size_; }; -- cgit v1.2.3