Advertisement
agrippa1994

Boost async TCP Client

Jan 4th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #pragma once
  2. #define     _WIN32_WINNT    0x0600
  3. #define     MAX_LEN     16384
  4.  
  5. #include <boost\asio.hpp>
  6. #include <boost\bind.hpp>
  7. #include <boost\thread.hpp>
  8.  
  9. using namespace boost::asio;
  10. using       boost::asio::ip::tcp;
  11.  
  12. class BitStream;
  13.  
  14. class CClient
  15. {
  16. public:
  17.     CClient(io_service &io);
  18.     ~CClient(void);
  19.  
  20.     tcp::socket&            GetSocket() { return m_socket; }
  21.     void                    ConnectToHost(char* IP,unsigned int Port);
  22.     bool                SendServerMessage(BitStream *bs);
  23. protected:
  24.     virtual void            OnConnectFailed();
  25.     virtual void            OnConnected();
  26.     virtual void            OnDisconnect();
  27.     virtual void            OnMessage(BitStream Data);
  28.     virtual void            OnSendFailed();
  29. private:
  30.     char                m_buffer[MAX_LEN];
  31.     unsigned __int64        NumberOfBytesSended;
  32.     unsigned __int64        NumberOfBytesReaded;
  33.     tcp::socket         m_socket;
  34.  
  35.     void                handle_connect(const boost::system::error_code& error);
  36.     void                handle_read(const boost::system::error_code& error,size_t transferred);
  37.     void                handle_write(const boost::system::error_code& error,size_t transferred);
  38. };
  39.  
  40.  
  41.  
  42.  
  43.  
  44. #include "Client.h"
  45. #include "BitStream.h"
  46.  
  47.  
  48. #include <iostream>
  49. #include <windows.h>
  50. using namespace std;
  51.  
  52. CClient::CClient(io_service &io) :  m_socket(io), NumberOfBytesReaded(0), NumberOfBytesSended(0)
  53. {
  54.     memset(m_buffer,0,sizeof(m_buffer));
  55. }
  56.  
  57.  
  58. CClient::~CClient(void)
  59. {
  60.  
  61. }
  62.  
  63.  
  64. void CClient::ConnectToHost(char* IP,unsigned int Port)
  65. {
  66.     tcp::endpoint endpoint(ip::address::from_string(IP),Port);
  67.     m_socket.async_connect(endpoint,boost::bind(&CClient::handle_connect,this,boost::asio::placeholders::error));
  68. }
  69.  
  70. bool CClient::SendServerMessage(BitStream *bs)
  71. {
  72.     if(!m_socket.is_open()) return true;
  73.     m_socket.async_write_some(boost::asio::buffer(bs->GetData(),bs->GetNumberOfBytesUsed()),boost::bind(&CClient::handle_write,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
  74.     return true;
  75. }
  76.  
  77. void CClient::handle_connect(const boost::system::error_code& error)
  78. {
  79.     if(error)
  80.     {
  81.         OnConnectFailed();
  82.         return;
  83.     }
  84.     m_socket.async_read_some(boost::asio::buffer(m_buffer,MAX_LEN),boost::bind(&CClient::handle_read,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
  85.     OnConnected();
  86. }
  87.  
  88. void CClient::handle_read(const boost::system::error_code& error,size_t transferred)
  89. {
  90.     if(error)
  91.     {
  92.         OnDisconnect();
  93.         return;
  94.     }
  95.     NumberOfBytesReaded += transferred;
  96.  
  97.     BitStream tmp(m_buffer,transferred,true);
  98.     OnMessage(tmp);
  99.  
  100.     m_socket.async_read_some(boost::asio::buffer(m_buffer,MAX_LEN),boost::bind(&CClient::handle_read,this,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
  101. }
  102.  
  103. void CClient::handle_write(const boost::system::error_code& error,size_t transferred)
  104. {
  105.     if(error)
  106.     {
  107.         OnSendFailed();
  108.         return;
  109.     }
  110.  
  111.     NumberOfBytesSended += transferred;
  112. }
  113.  
  114. void CClient::OnConnected()
  115. {
  116.     cout << "Verbunden" << endl;
  117. }
  118.  
  119. void CClient::OnConnectFailed()
  120. {
  121.     cout << "Verbindung konnte nicht aufgebaut werden!" << endl;
  122. }
  123.  
  124. void CClient::OnDisconnect()
  125. {
  126.     cout << "Verbindung getrennt / verloren" << endl;
  127. }
  128.  
  129. void CClient::OnMessage(BitStream Data)
  130. {
  131.     cout << "Nachricht erhalten" << endl;
  132. }
  133.  
  134. void CClient::OnSendFailed()
  135. {
  136.     cout << "Senden fehlgeschlagen!" << endl;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement