Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Connection.h
- *
- * Created on: 2011-02-25
- * Author: cdunphy
- */
- #ifndef CONNECTION_H_
- #define CONNECTION_H_
- #include <vector>
- #include <string>
- #include <sstream>
- #include <stdexcept>
- #include <boost/thread.hpp>
- #include <boost/shared_ptr.hpp>
- namespace shaw_rsc
- {
- class Connection;
- const std::size_t BUF_SIZE = 128;
- /*
- * This is the type of reference we will
- * provide to the clients.
- */
- typedef boost::shared_ptr<Connection> ConnPtr;
- typedef std::vector<char> DataBuffer;
- typedef DataBuffer::iterator DB_Iter;
- typedef DataBuffer::const_iterator DB_CIter;
- // This is the mode we are using for the connection
- enum Mode {
- CLIENT,
- SERVER
- };
- /*
- * This is a generic class that allows data to be read or
- * written to using a connection. This is quite abstract
- * and it can be used both for file operations and for
- * network operations.
- */
- class Connection
- {
- public:
- Connection() { }
- virtual ~Connection() { }
- /*
- * This method writes the current contents of the data buffer
- * to the connected resource. Be sure to set the right data
- * in the buffer by calling the setData method first.
- *
- * The number of bytes written is returned.
- */
- virtual std::size_t write(const DataBuffer& data) = 0;
- /*
- * This method reads data from the connected resource and stores
- * it in our data buffer which we pass in by reference.
- * Please note that it clears whatever data was in the buffer prior to
- * reading.
- *
- * The number of bytes read is returned.
- */
- virtual std::size_t read(DataBuffer& data) = 0;
- virtual const std::string str() const = 0;
- };
- inline std::vector<unsigned char> convert_data_to_unsigned(const DataBuffer& data)
- {
- return std::vector<unsigned char>(data.begin(), data.end());
- }
- inline std::string dataBufferToStr(const DataBuffer& data)
- {
- return std::string(data.begin(), data.end());
- }
- }
- #endif /* CONNECTION_H_ */
- /*
- * AsioConnection.h
- *
- * Created on: 2011-04-08
- * Author: cdunphy
- *
- * All classes that want to use the ASIO io_service
- * and deadline timers will want to subclass this.
- */
- #ifndef ASIOCONNECTION_H_
- #define ASIOCONNECTION_H_
- #include "Connection.h"
- #include <boost/shared_ptr.hpp>
- #include <boost/asio.hpp>
- #include <boost/array.hpp>
- #include <boost/bind.hpp>
- namespace shaw_rsc
- {
- /*
- * This exception throws if there is a timeout when connecting
- * to a remote socket.
- */
- struct SocketTimeoutException : public std::runtime_error
- {
- SocketTimeoutException(const std::string& msg) : std::runtime_error(msg)
- { }
- }
- ;
- /*
- * This is the root class of every Connection
- * class that wants to make use of boost asio.
- */
- class AsioConnection : public Connection
- {
- public:
- AsioConnection(
- int c_timeout,
- int r_timeout
- ) : Connection(),
- conn_timeout_int(c_timeout),
- read_timeout_int(r_timeout),
- conn_timeout(c_timeout),
- read_timeout(r_timeout),
- io_service(),
- strand(io_service),
- deadline(strand.get_io_service()),
- error()
- {
- reset_deadline();
- }
- const boost::system::error_code& getError() const
- {
- return error;
- }
- int get_read_timeout() const
- {
- return read_timeout_int;
- }
- int get_conn_timeout() const
- {
- return conn_timeout_int;
- }
- /*
- * These are the callback handlers for our asynchronous
- * IO operations.
- */
- void handle_write(const boost::system::error_code& ec,
- std::size_t len,
- boost::system::error_code* out_ec,
- std::size_t* out_len)
- {
- *out_ec = ec;
- *out_len = len;
- }
- /*
- * These are the callback handlers for our asynchronous
- * IO operations.
- */
- void handle_send(const boost::system::error_code& ec,
- std::size_t len,
- boost::system::error_code* out_ec,
- std::size_t* out_len)
- {
- *out_ec = ec;
- *out_len = len;
- }
- void handle_read(const boost::system::error_code& ec,
- std::size_t len,
- boost::system::error_code* out_ec,
- std::size_t* out_len)
- {
- *out_ec = ec;
- *out_len = len;
- }
- void handle_receive(const boost::system::error_code& ec,
- std::size_t len,
- boost::system::error_code* out_ec,
- std::size_t* out_len)
- {
- *out_ec = ec;
- *out_len = len;
- }
- void handle_connect(const boost::system::error_code& ec,
- boost::system::error_code* out_ec)
- {
- *out_ec = ec;
- }
- protected:
- int conn_timeout_int;
- int read_timeout_int;
- boost::posix_time::seconds conn_timeout;
- boost::posix_time::seconds read_timeout;
- boost::asio::io_service io_service;
- boost::asio::strand strand;
- boost::asio::deadline_timer deadline;
- boost::system::error_code error;
- void reset_deadline()
- {
- // No deadline is required until the first socket operation is started. We
- // set the deadline to positive infinity so that the actor takes no action
- // until a specific deadline is set.
- deadline.expires_at(boost::posix_time::pos_infin);
- }
- };
- }
- #endif /* ASIOCONNECTION_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement