Archive / / / / network.h
2007-09-23 17:05:32 UTC
previous next
#ifndef NETWORK_H_ #define NETWORK_H_ #include <boost/thread.hpp> #include <boost/utility.hpp> #include <boost/asio.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <list> #include <string> #include <queue> namespace Network { class Manager : private boost::noncopyable { public: // Public methods, called from outside the thread Manager(); ~Manager(); bool connect( std::string server, std::string port); /*void start_stream( std::string stream_type, boost::asio::mutable_buffer buffer, boost::function<void ()> callback);*/ void register_stream_type( std::string stream_type, boost::function<void (unsigned short, boost::asio::mutable_buffer&, boost::function<void ()>&)> callback); /*void send_data( unsigned short stream_id, boost::asio::const_buffer data, unsigned short data_length);*/ private: // Private methods, called from inside the thread void receive_data( const boost::asio::error& error, std::size_t bytes_transferred); private: // Private data boost::scoped_ptr<boost::thread> _thread; boost::asio::io_service _io_service; boost::scoped_ptr<boost::asio::io_service::work> _io_service_work; boost::asio::ip::tcp::socket _socket; boost::mutex _send_data_mutex; boost::array<unsigned char, 2048> _buffer; struct StreamInfo { StreamInfo( boost::asio::mutable_buffer buffer_, boost::function<void ()> callback_) : buffer(buffer_), callback(callback_) {} boost::asio::mutable_buffer buffer; boost::function<void ()> callback; }; std::vector<StreamInfo> _streams; std::queue<StreamInfo> _new_streams; boost::mutex _new_streams_mutex; struct StreamTypeInfo { StreamTypeInfo( std::string stream_type_, boost::function<void (unsigned short, boost::asio::mutable_buffer&, boost::function<void ()>&)> callback_) : stream_type(stream_type_), callback(callback_) {} std::string stream_type; boost::function<void (unsigned short, boost::asio::mutable_buffer&, boost::function<void ()>&)> callback; }; std::list<StreamTypeInfo> _stream_types; boost::mutex _stream_types_mutex; friend class Stream; }; class Stream { public: Stream(); virtual ~Stream(); protected: void connect_stream( std::string string_type, boost::asio::mutable_buffer buffer, boost::function<void ()> callback); void send_data( boost::asio::const_buffer data, unsigned short data_length); private: unsigned short _stream_id; }; extern boost::scoped_ptr<Manager> manager; } #endif /*NETWORK_H_*/