C:/Development/boost_sandbox/rpc/boost/rpc/registry_server.hpp

00001 // Copyright Stjepan Rajko 2007. Use, modification and
00002 // distribution is subject to the Boost Software License, Version
00003 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
00004 // http://www.boost.org/LICENSE_1_0.txt)
00005 
00006 #ifndef BOOST_RPC_REGISTRY_SERVER
00007 #define BOOST_RPC_REGISTRY_SERVER
00008 
00009 #include <boost/rpc/config.hpp>
00010 #include <boost/rpc/detail/expanding_buffer.hpp>
00011 #include <boost/rpc/registry.hpp>
00012 #include <boost/rpc/call_options.hpp>
00013 #include <boost/rpc/detail/protocol.hpp>
00014 #include <boost/shared_ptr.hpp>
00015 #include <sstream>
00016 
00017 namespace boost {
00018 namespace rpc {
00019 
00021 
00023 template<typename Registry>
00024 class registry_server
00025 {
00026 public:
00027     typedef typename Registry::id_type id_type;
00028     typedef typename Registry::archive_type archive_type;
00029 
00031     registry_server(Registry &reg, boost::shared_ptr<boost::asio::ip::tcp::socket> sock)
00032         : marshal_buffer(BOOST_RPC_RPC_BUFFER_SIZE),
00033         stream(std::ios::in | std::ios::out | std::ios::binary),
00034         registry_(reg), socket_ptr(sock)
00035     {
00036        prepare_async_read();
00037        protocol::write_message(*socket_ptr, protocol::ok_message);
00038     }
00039 private:
00040     void prepare_async_read()
00041     {
00042         boost::asio::async_read(*socket_ptr, request_header.as_buffer(),
00043             boost::bind(&registry_server::read_header, this,
00044             boost::asio::placeholders::error));
00045     }
00046     void read_header(const boost::system::error_code& error)
00047     {
00048         if (!error)
00049         {
00050             boost::asio::async_read(*socket_ptr,
00051                 boost::asio::buffer(marshal_buffer, request_header.marshal_size),
00052                 boost::bind(&registry_server::read_marshal, this, boost::asio::placeholders::error));
00053             marshal_buffer.guarantee(request_header.marshal_size);
00054         }
00055     }
00056     void read_marshal(const boost::system::error_code& error)
00057     {
00058         std::string result;
00059         protocol::call_footer footer;
00060         boost::asio::read(*socket_ptr, footer.as_buffer(), boost::asio::transfer_all());
00061      
00062         registry_(std::string(marshal_buffer, request_header.marshal_size),
00063             footer.options, &result);
00064 
00065         if (footer.options.marshal_option >= call_options::completed_only)
00066         {
00067             request_header.marshal_size = static_cast<protocol::marshal_size_type>(result.size());
00068             socket_ptr->send(request_header.as_buffer());
00069         }
00070         if (footer.options.marshal_option >= call_options::return_only)
00071         {
00072             socket_ptr->send(boost::asio::buffer(result));
00073         }
00074         // prepare next read
00075         prepare_async_read();
00076     }
00077 
00078     protocol::call_header request_header;
00079     detail::expanding_buffer marshal_buffer;
00080     std::stringstream stream;
00081 
00082     Registry &registry_;
00083     boost::shared_ptr<boost::asio::ip::tcp::socket> socket_ptr;
00084 };
00085 
00086 } // namespace rpc
00087 } // namespace boost
00088 
00089 #endif // BOOST_RPC_REGISTRY_SERVER

Generated on Sun May 20 15:47:56 2007 by  doxygen 1.5.2