Я сделал static-lib. И я создал эти три класса в
Класс подключения
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <memory>
#include "ByteBuffer.h"
class Connection: public boost::enable_shared_from_this<Connection>
{
public:
typedef boost::shared_ptr<Connection> pointer;
explicit Connection(boost::asio::io_service& io_service);
virtual ~Connection();
boost::asio::ip::tcp::socket& socket();
virtual void OnConnected()=0;
virtual void Send(std::shared_ptr<uint8_t> buffer, int length);
void Receive();
void Disconnect();
bool connected;
protected:
virtual void OnReceived(ByteBuffer &b) = 0;
private:
void handle_Receive(const boost::system::error_code& error, std::size_t bytes_transferred );
void handle_Send(const boost::system::error_code& error, std::size_t bytes_transferred);
boost::asio::ip::tcp::socket socket_;
bool disconnecting;
boost::array<uint8_t, 1000> read_buffer_;
};
#endif
#include "Connection.h"
Connection::Connection(boost::asio::io_service& io_service)
:socket_(io_service),disconnecting(false),connected(false){}
Connection::~Connection(){}
boost::asio::ip::tcp::socket& Connection::socket(){
return socket_;
}
void Connection::Send(std::shared_ptr<uint8_t> buf, int length){
boost::asio::async_write(socket_,boost::asio::buffer(buf.get(),length),
boost::bind(&Connection::handle_Send, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void Connection::handle_Send(const boost::system::error_code& error, std::size_t bytes_transferred){
}
void Connection::Receive(){
boost::asio::async_read(socket_,boost::asio::buffer(this->read_buffer_),
boost::bind(&Connection::handle_Receive, shared_from_this(),boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
}
void Connection::handle_Receive(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if(!error)
{
if(bytes_transferred <=0){
this->Disconnect();
}else{
ByteBuffer b ((std :: shared_ptr) this-> read_buffer_.data (), this-> read_buffer_.size ());
этом-> OnReceived (б);
this-> Receive ();}
}
}
void Connection::Disconnect()
{
if (!disconnecting) {
boost::system::error_code ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send,ec);
socket_.close(ec);
disconnecting = true;
std::cout<<"disconnected"<<std::endl;
}
}
ConnectionFactory class
#pragma once
#include "Connection.h"
class ConnectionFactory
{
public:
ConnectionFactory(void);
virtual ~ConnectionFactory(void);
virtual Connection::pointer create(boost::asio::io_service& io_service) = 0;
};
#include "ConnectionFactory.h"
ConnectionFactory::ConnectionFactory(void)
{
}
ConnectionFactory::~ConnectionFactory(void)
{
}
Класс сервера
#ifndef _SERVER_H_
#define _SERVER_H_
#include "Connection.h"
#include "ConnectionFactory.h"
class Server
{
public:
Server(boost::asio::io_service& io_service , std::string ip,short port,boost::shared_ptr<ConnectionFactory> factory);
~Server();
private:
void start_accept();
void handle_accept(boost::shared_ptr<Connection> conn,const boost::system::error_code& error);
boost::shared_ptr<ConnectionFactory> m_factory;
boost::asio::io_service &io_service;
boost::asio::ip::tcp::acceptor acceptor_;
};
#endif
#include "Server.h"
Server::Server(boost::asio::io_service& io_service,std::string ip,short port,boost::shared_ptr<ConnectionFactory> factory)
:io_service(io_service), acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(ip.data()), port)){
m_factory = factory;
start_accept();
std::cout<<"Socket accepting connections..."<<std::endl;
}
Server::~Server()
{
}
void Server::start_accept(){
boost::shared_ptr<Connection> conn = m_factory->create(this->io_service);
acceptor_.async_accept(conn->socket(),
boost::bind(&Server::handle_accept, this,conn,boost::asio::placeholders::error));
}
void Server::handle_accept(boost::shared_ptr<Connection> conn,const boost::system::error_code& error){
if (!error){
std::cout<<"on connected"<<std::endl;
conn->OnConnected();
conn->Receive();
start_accept();
}
//conn->Disconnect();
}
и я избавился от static-lib и использовал эти классы, и он отлично работает
в моем main.cpp
#include <iostream>
#include "auth_proto.h"
#include <Server.h>
#include <ConnectionFactory.h>
#include "AuthConnectionFactory.h"
using namespace std;
int main()
{
Auth_Setup();
try
{
boost::asio::io_service io_service;
boost::shared_ptr<ConnectionFactory> fact (new AuthConnectionFactory(io_service));
Server s(io_service,"5.113.195.156",9959,fact);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
Я действительно не понимаю, что здесь не так, когда дело касается функции Receive (), она получает данные от клиента, но не вызывает метод handle_Receive (), поэтому я могу использовать эти данные, поведение, которое жду, что он должен вызвать handle_Receive (), чтобы я мог передать данные в ByteBuffer и использовать их, но этого не происходит ......