Advertisement
Guest User

Untitled

a guest
May 31st, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #ifndef _CONNECTION_H_
  2. #define _CONNECTION_H_
  3.  
  4. #include "ByteBuffer.h"
  5. #include <ctime>
  6. #include <iostream>
  7. #include <string>
  8. #include <stdint.h>
  9. #include <boost/bind.hpp>
  10. #include <memory>
  11. #include <boost/enable_shared_from_this.hpp>
  12. #include <boost/asio.hpp>
  13.  
  14. class Connection: public boost::enable_shared_from_this<Connection>
  15. {
  16. public:
  17.   typedef std::shared_ptr<Connection> pointer;
  18.  
  19.   explicit Connection(boost::asio::io_service& io_service);
  20.   virtual ~Connection();
  21.   boost::asio::ip::tcp::socket& socket();
  22.  
  23.   virtual void OnConnected()=0;
  24.   virtual void Send(std::shared_ptr<uint8_t> buffer, int length);
  25.   bool Receive();
  26.   void Disconnect();
  27.  
  28. protected:
  29.     virtual void OnReceived(ByteBuffer &b) = 0;
  30.  
  31. private:
  32.   void handler(const boost::system::error_code& error, std::size_t bytes_transferred );
  33.   boost::asio::ip::tcp::socket socket_;
  34.   bool disconnecting;
  35. };
  36.  
  37.  
  38. #endif
  39.  
  40.  
  41.  
  42. #include "Connection.h"
  43. using namespace std;
  44.  
  45. Connection::Connection(boost::asio::io_service& io_service)
  46.     : socket_(io_service),disconnecting(false)
  47.   {
  48.      
  49.   }
  50.  
  51. Connection::~Connection()
  52. {
  53.     //dtor
  54. }
  55.  
  56.  
  57. boost::asio::ip::tcp::socket& Connection::socket(){
  58.     return socket_;
  59. }
  60.  
  61. void Connection::Send(std::shared_ptr<uint8_t> buf, int length){
  62.  
  63.     boost::asio::async_write(socket_,boost::asio::buffer(buf.get(),length),
  64.             boost::bind(&Connection::handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  65. }
  66.  
  67. bool Connection::Receive(){
  68.     std::vector<uint8_t> buf(1000);
  69.     boost::asio::async_read(socket_,boost::asio::buffer(buf,1000),
  70.             boost::bind(&Connection::handler, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  71.  
  72.  
  73.     int rcvlen=buf.size();
  74.     ByteBuffer b((std::shared_ptr<uint8_t>)buf.data(),rcvlen);
  75.     if(rcvlen <= 0){
  76.         buf.clear();
  77.         this->Disconnect();
  78.         return false;
  79.     }
  80.     OnReceived(b);
  81.     buf.clear();
  82.     return true;
  83. }
  84.        
  85. void Connection::Disconnect()
  86. {
  87.     if (!disconnecting) {
  88.         boost::system::error_code ec;
  89.         socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send,ec);
  90.         socket_.close(ec);
  91.         disconnecting = true;
  92.     }
  93. }
  94.  
  95.  
  96. void Connection::handler(const boost::system::error_code& error, std::size_t bytes_transferred ){
  97.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement