From 686c1ebee948a20193df1411d478693a29a0f658 Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Mon, 17 Nov 2008 23:21:42 +0000 Subject: added OnConnect Callback --- src/anytun.cpp | 2 ++ src/syncOnConnect.hpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/syncServer.cpp | 1 + src/syncServer.h | 4 +++- src/syncTcpConnection.cpp | 42 ++---------------------------------------- src/syncTcpConnection.h | 2 ++ 6 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 src/syncOnConnect.hpp diff --git a/src/anytun.cpp b/src/anytun.cpp index 4bb6e14..a2407e2 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -71,6 +71,7 @@ #ifndef ANYTUN_NOSYNC #include "syncServer.h" #include "syncClient.h" +#include "syncOnConnect.hpp" #endif #include "threadParam.h" @@ -217,6 +218,7 @@ void syncListener(SyncQueue * queue ) { asio::io_service io_service; SyncServer server(io_service,asio::ip::tcp::endpoint(asio::ip::tcp::v4(), gOpt.getLocalSyncPort())); + server.onConnect=boost::bind(syncOnConnect,_1); queue->setSyncServerPtr(&server); io_service.run(); } diff --git a/src/syncOnConnect.hpp b/src/syncOnConnect.hpp new file mode 100644 index 0000000..35aa93a --- /dev/null +++ b/src/syncOnConnect.hpp @@ -0,0 +1,45 @@ + +void syncOnConnect(SyncTcpConnection * connptr) +{ + ConnectionList & cl_(gConnectionList); + ConnectionMap::iterator cit = cl_.getBeginUnlocked(); + for (;cit!=cl_.getEndUnlocked();++cit) + { + std::ostringstream sout; + boost::archive::text_oarchive oa(sout); + const SyncCommand scom(cl_,cit->first); + oa << scom; + std::stringstream lengthout; + lengthout << std::setw(5) << std::setfill('0') << sout.str().size()<< ' '; + connptr->Send(lengthout.str()); + connptr->Send(sout.str()); + } + //TODO Locking here + RoutingMap::iterator it = gRoutingTable.getBeginUnlocked(); + for (;it!=gRoutingTable.getEndUnlocked();++it) + { + NetworkPrefix tmp(it->first); + std::ostringstream sout; + boost::archive::text_oarchive oa(sout); + const SyncCommand scom(tmp); + oa << scom; + std::stringstream lengthout; + lengthout << std::setw(5) << std::setfill('0') << sout.str().size()<< ' '; + connptr->Send(lengthout.str()); + connptr->Send(sout.str()); + } + //TODO Locking here + RtpSessionMap::iterator rit = gRtpSessionTable.getBeginUnlocked(); + for (;rit!=gRtpSessionTable.getEndUnlocked();++rit) + { + std::ostringstream sout; + boost::archive::text_oarchive oa(sout); + const SyncCommand scom(rit->first); + oa << scom; + std::stringstream lengthout; + lengthout << std::setw(5) << std::setfill('0') << sout.str().size()<< ' '; + connptr->Send(lengthout.str()); + connptr->Send(sout.str()); + } +} + diff --git a/src/syncServer.cpp b/src/syncServer.cpp index ea6df4b..1468d74 100644 --- a/src/syncServer.cpp +++ b/src/syncServer.cpp @@ -32,6 +32,7 @@ void SyncServer::handle_accept(SyncTcpConnection::pointer new_connection, { if (!error) { + new_connection->onConnect=onConnect; new_connection->start(); start_accept(); } diff --git a/src/syncServer.h b/src/syncServer.h index 0899d26..092ad16 100644 --- a/src/syncServer.h +++ b/src/syncServer.h @@ -5,6 +5,8 @@ #include #include #include +#include + #include #include #include "syncTcpConnection.h" @@ -15,7 +17,7 @@ class SyncServer { public: SyncServer(asio::io_service& io_service, asio::ip::tcp::endpoint tcp_endpoint ); - + boost::function onConnect; std::list conns_; void send(std::string message); private: diff --git a/src/syncTcpConnection.cpp b/src/syncTcpConnection.cpp index 185781f..dbd9ac0 100644 --- a/src/syncTcpConnection.cpp +++ b/src/syncTcpConnection.cpp @@ -21,47 +21,9 @@ void SyncTcpConnection::start() { - ConnectionList & cl_(gConnectionList); - ConnectionMap::iterator cit = cl_.getBeginUnlocked(); - for (;cit!=cl_.getEndUnlocked();++cit) - { - std::ostringstream sout; - boost::archive::text_oarchive oa(sout); - const SyncCommand scom(cl_,cit->first); - oa << scom; - std::stringstream lengthout; - lengthout << std::setw(5) << std::setfill('0') << sout.str().size()<< ' '; - Send(lengthout.str()); - Send(sout.str()); - } - //TODO Locking here - RoutingMap::iterator it = gRoutingTable.getBeginUnlocked(); - for (;it!=gRoutingTable.getEndUnlocked();++it) - { - NetworkPrefix tmp(it->first); - std::ostringstream sout; - boost::archive::text_oarchive oa(sout); - const SyncCommand scom(tmp); - oa << scom; - std::stringstream lengthout; - lengthout << std::setw(5) << std::setfill('0') << sout.str().size()<< ' '; - Send(lengthout.str()); - Send(sout.str()); - } - //TODO Locking here - RtpSessionMap::iterator rit = gRtpSessionTable.getBeginUnlocked(); - for (;rit!=gRtpSessionTable.getEndUnlocked();++rit) - { - std::ostringstream sout; - boost::archive::text_oarchive oa(sout); - const SyncCommand scom(rit->first); - oa << scom; - std::stringstream lengthout; - lengthout << std::setw(5) << std::setfill('0') << sout.str().size()<< ' '; - Send(lengthout.str()); - Send(sout.str()); - } + onConnect(this); } + void SyncTcpConnection::Send(std::string message) { asio::async_write(socket_, asio::buffer(message), diff --git a/src/syncTcpConnection.h b/src/syncTcpConnection.h index 5caf1a0..1ef9bb7 100644 --- a/src/syncTcpConnection.h +++ b/src/syncTcpConnection.h @@ -2,6 +2,7 @@ #define _SYNCTCPCONNECTION_H_ #include #include +#include #include #include @@ -15,6 +16,7 @@ public: { return pointer(new SyncTcpConnection(io_service)); }; + boost::function onConnect; asio::ip::tcp::socket& socket(); void start(); void Send(std::string message); -- cgit v1.2.3