Advertisement
Guest User

Connection.h AsioConnection.h

a guest
Apr 30th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. /*
  2.  * Connection.h
  3.  *
  4.  *  Created on: 2011-02-25
  5.  *      Author: cdunphy
  6.  */
  7.  
  8. #ifndef CONNECTION_H_
  9. #define CONNECTION_H_
  10.  
  11. #include <vector>
  12. #include <string>
  13. #include <sstream>
  14. #include <stdexcept>
  15.  
  16. #include <boost/thread.hpp>
  17. #include <boost/shared_ptr.hpp>
  18.  
  19. namespace shaw_rsc
  20. {
  21.  
  22. class Connection;
  23.  
  24. const std::size_t BUF_SIZE = 128;
  25.  
  26. /*
  27.  * This is the type of reference we will
  28.  * provide to the clients.
  29.  */
  30. typedef boost::shared_ptr<Connection> ConnPtr;
  31.  
  32. typedef std::vector<char> DataBuffer;
  33. typedef DataBuffer::iterator DB_Iter;
  34. typedef DataBuffer::const_iterator DB_CIter;
  35.  
  36. // This is the mode we are using for the connection
  37. enum Mode {
  38.     CLIENT,
  39.     SERVER
  40. };
  41.  
  42. /*
  43.  * This is a generic class that allows data to be read or
  44.  * written to using a connection.  This is quite abstract
  45.  * and it can be used both for file operations and for
  46.  * network operations.
  47.  */
  48. class Connection
  49. {
  50. public:
  51.     Connection() { }
  52.     virtual ~Connection() { }
  53.  
  54.     /*
  55.      * This method writes the current contents of the data buffer
  56.      * to the connected resource.  Be sure to set the right data
  57.      * in the buffer by calling the setData method first.
  58.      *
  59.      * The number of bytes written is returned.
  60.      */
  61.     virtual std::size_t write(const DataBuffer& data) = 0;
  62.  
  63.     /*
  64.      * This method reads data from the connected resource and stores
  65.      * it in our data buffer which we pass in by reference.
  66.      * Please note that it clears whatever data was in the buffer prior to
  67.      * reading.
  68.      *
  69.      * The number of bytes read is returned.
  70.      */
  71.     virtual std::size_t read(DataBuffer& data) = 0;
  72.  
  73.     virtual const std::string str() const = 0;
  74. };
  75.  
  76. inline std::vector<unsigned char> convert_data_to_unsigned(const DataBuffer& data)
  77. {
  78.     return std::vector<unsigned char>(data.begin(), data.end());
  79. }
  80.  
  81. inline std::string dataBufferToStr(const DataBuffer& data)
  82. {
  83.     return std::string(data.begin(), data.end());
  84. }
  85.  
  86. }
  87.  
  88. #endif /* CONNECTION_H_ */
  89.  
  90. /*
  91.  * AsioConnection.h
  92.  *
  93.  *  Created on: 2011-04-08
  94.  *      Author: cdunphy
  95.  *
  96.  * All classes that want to use the ASIO io_service
  97.  * and deadline timers will want to subclass this.
  98.  */
  99.  
  100.  
  101.  
  102. #ifndef ASIOCONNECTION_H_
  103. #define ASIOCONNECTION_H_
  104.  
  105. #include "Connection.h"
  106.  
  107. #include <boost/shared_ptr.hpp>
  108. #include <boost/asio.hpp>
  109. #include <boost/array.hpp>
  110. #include <boost/bind.hpp>
  111.  
  112. namespace shaw_rsc
  113. {
  114.  
  115. /*
  116.  * This exception throws if there is a timeout when connecting
  117.  * to a remote socket.
  118.  */
  119. struct SocketTimeoutException : public std::runtime_error
  120. {
  121.     SocketTimeoutException(const std::string& msg) : std::runtime_error(msg)
  122.     { }
  123. }
  124. ;
  125.  
  126. /*
  127.  * This is the root class of every Connection
  128.  * class that wants to make use of boost asio.
  129.  */
  130. class AsioConnection : public Connection
  131. {
  132. public:
  133.     AsioConnection(
  134.         int c_timeout,
  135.         int r_timeout
  136.     ) : Connection(),
  137.             conn_timeout_int(c_timeout),
  138.             read_timeout_int(r_timeout),
  139.             conn_timeout(c_timeout),
  140.             read_timeout(r_timeout),
  141.             io_service(),
  142.             strand(io_service),
  143.             deadline(strand.get_io_service()),
  144.             error()
  145.     {
  146.         reset_deadline();
  147.     }
  148.  
  149.     const boost::system::error_code& getError() const
  150.     {
  151.         return error;
  152.     }
  153.  
  154.     int get_read_timeout() const
  155.     {
  156.         return read_timeout_int;
  157.     }
  158.     int get_conn_timeout() const
  159.     {
  160.         return conn_timeout_int;
  161.     }
  162.  
  163.     /*
  164.         * These are the callback handlers for our asynchronous
  165.         * IO operations.
  166.         */
  167.     void handle_write(const boost::system::error_code& ec,
  168.                       std::size_t len,
  169.                       boost::system::error_code* out_ec,
  170.                       std::size_t* out_len)
  171.     {
  172.         *out_ec = ec;
  173.         *out_len = len;
  174.     }
  175.  
  176.     /*
  177.        * These are the callback handlers for our asynchronous
  178.        * IO operations.
  179.        */
  180.     void handle_send(const boost::system::error_code& ec,
  181.                      std::size_t len,
  182.                      boost::system::error_code* out_ec,
  183.                      std::size_t* out_len)
  184.     {
  185.         *out_ec = ec;
  186.         *out_len = len;
  187.     }
  188.  
  189.     void handle_read(const boost::system::error_code& ec,
  190.                      std::size_t len,
  191.                      boost::system::error_code* out_ec,
  192.                      std::size_t* out_len)
  193.     {
  194.         *out_ec = ec;
  195.         *out_len = len;
  196.     }
  197.  
  198.     void handle_receive(const boost::system::error_code& ec,
  199.                         std::size_t len,
  200.                         boost::system::error_code* out_ec,
  201.                         std::size_t* out_len)
  202.     {
  203.         *out_ec = ec;
  204.         *out_len = len;
  205.     }
  206.  
  207.     void handle_connect(const boost::system::error_code& ec,
  208.                         boost::system::error_code* out_ec)
  209.     {
  210.         *out_ec = ec;
  211.     }
  212.  
  213. protected:
  214.     int conn_timeout_int;
  215.     int read_timeout_int;
  216.     boost::posix_time::seconds conn_timeout;
  217.     boost::posix_time::seconds read_timeout;
  218.     boost::asio::io_service io_service;
  219.     boost::asio::strand strand;
  220.     boost::asio::deadline_timer deadline;
  221.     boost::system::error_code error;
  222.  
  223.     void reset_deadline()
  224.     {
  225.         // No deadline is required until the first socket operation is started. We
  226.         // set the deadline to positive infinity so that the actor takes no action
  227.         // until a specific deadline is set.
  228.         deadline.expires_at(boost::posix_time::pos_infin);
  229.     }
  230. };
  231.  
  232. }
  233.  
  234.  
  235. #endif /* ASIOCONNECTION_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement