Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define _WIN32_WINNT 0x0600
- #define MAX_LEN 16384
- #include <boost\asio.hpp>
- #include <boost\bind.hpp>
- #include <boost\thread.hpp>
- using namespace boost::asio;
- using boost::asio::ip::tcp;
- class BitStream;
- class CClient
- {
- public:
- CClient(io_service &io);
- ~CClient(void);
- tcp::socket& GetSocket() { return m_socket; }
- void ConnectToHost(char* IP,unsigned int Port);
- bool SendServerMessage(BitStream *bs);
- protected:
- virtual void OnConnectFailed();
- virtual void OnConnected();
- virtual void OnDisconnect();
- virtual void OnMessage(BitStream Data);
- virtual void OnSendFailed();
- private:
- char m_buffer[MAX_LEN];
- unsigned __int64 NumberOfBytesSended;
- unsigned __int64 NumberOfBytesReaded;
- tcp::socket m_socket;
- void handle_connect(const boost::system::error_code& error);
- void handle_read(const boost::system::error_code& error,size_t transferred);
- void handle_write(const boost::system::error_code& error,size_t transferred);
- };
- #include "Client.h"
- #include "BitStream.h"
- #include <iostream>
- #include <windows.h>
- using namespace std;
- CClient::CClient(io_service &io) : m_socket(io), NumberOfBytesReaded(0), NumberOfBytesSended(0)
- {
- memset(m_buffer,0,sizeof(m_buffer));
- }
- CClient::~CClient(void)
- {
- }
- void CClient::ConnectToHost(char* IP,unsigned int Port)
- {
- tcp::endpoint endpoint(ip::address::from_string(IP),Port);
- m_socket.async_connect(endpoint,boost::bind(&CClient::handle_connect,this,boost::asio::placeholders::error));
- }
- bool CClient::SendServerMessage(BitStream *bs)
- {
- if(!m_socket.is_open()) return true;
- 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));
- return true;
- }
- void CClient::handle_connect(const boost::system::error_code& error)
- {
- if(error)
- {
- OnConnectFailed();
- return;
- }
- 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));
- OnConnected();
- }
- void CClient::handle_read(const boost::system::error_code& error,size_t transferred)
- {
- if(error)
- {
- OnDisconnect();
- return;
- }
- NumberOfBytesReaded += transferred;
- BitStream tmp(m_buffer,transferred,true);
- OnMessage(tmp);
- 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));
- }
- void CClient::handle_write(const boost::system::error_code& error,size_t transferred)
- {
- if(error)
- {
- OnSendFailed();
- return;
- }
- NumberOfBytesSended += transferred;
- }
- void CClient::OnConnected()
- {
- cout << "Verbunden" << endl;
- }
- void CClient::OnConnectFailed()
- {
- cout << "Verbindung konnte nicht aufgebaut werden!" << endl;
- }
- void CClient::OnDisconnect()
- {
- cout << "Verbindung getrennt / verloren" << endl;
- }
- void CClient::OnMessage(BitStream Data)
- {
- cout << "Nachricht erhalten" << endl;
- }
- void CClient::OnSendFailed()
- {
- cout << "Senden fehlgeschlagen!" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement