开发者

need example of boost:asio async server with receiver and acceptor

开发者 https://www.devze.com 2023-04-02 12:13 出处:网络
I have been looked alot of good exmaple but i didnt find if some one can help please help me, this is my current class

I have been looked alot of good exmaple but i didnt find if some one can help please help me, this is my current class

#include "StdAfx.h"
#include "TNetwork.h"
//-----------------------------------------------------------------------------------------------------------------------

CNetwork::CNetwork(io_service & io_service) : m_IoService(io_service), m_Acceptor(new ip::tcp::acceptor(io_service)) 
{
    m_AcceptMutex.initialize();
}
//-----------------------------------------------------------------------------------------------------------------------

void CNetwork::setConnector(void (*THISFUNC) (int))
{
    m_Connector = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------

void CNetwork::setReceiver(void (*THISFUNC) (int, u_char * , int))
{
    m_Receiver = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------

void CNetwork::setDisconnec开发者_如何学编程tor(void (*THISFUNC) (int))
{
    m_Disconnector = THISFUNC;
}
//-----------------------------------------------------------------------------------------------------------------------

bool CNetwork::start(USHORT Port)
{
    DWORD dwStartTick = GetTickCount();
    // ----
    printf("[%s][Starting ...]\n", __FUNCTION__);
    // ----
    ip::tcp::endpoint selfpoint(ip::tcp::v4(), Port);
    // ----
    m_Acceptor->open(selfpoint.protocol());
    m_Socket = new ip::tcp::socket(m_IoService);
    // ----
    try
    {
        m_Acceptor->bind(selfpoint);
        m_Acceptor->listen();
    }
    catch(std::exception & e)
    {
        printf("[%s][%s]\n", __FUNCTION__, e.what());
        // ----
        return false;
    }
    // ----
    printf("[%s][Started OK , within %u milisecond(s)]\n", __FUNCTION__, (GetTickCount() - dwStartTick));
    // ----
    ip::tcp::socket * temp = new ip::tcp::socket(m_IoService); 
    m_Acceptor->async_accept( * temp, boost::bind(& CNetwork::acceptor, this, temp, boost::asio::placeholders::error));
    // ----
    return true;
}
//-----------------------------------------------------------------------------------------------------------------------

void CNetwork::acceptor(ip::tcp::socket * socket, const boost::system::error_code & error)
{
    if(error <= 0)
    {
        m_AcceptMutex.lock();
        // ----
        printf("[%s][New connection : %d.]\n", __FUNCTION__, m_Clients.size());
        m_Clients.push_back(socket);
        // ----
        if(m_Connector != NULL)
        {
            m_Connector(m_Clients.size());
        }
        // ----
        m_AcceptMutex.unlock();
        // ----
        //socket->async_receive(boost::asio::buffer(m_buffers), 
    }
    else
    {
        printf("[%s][%s]\n", __FUNCTION__, error.message());
    }
    // ----
    ip::tcp::socket * temp = new ip::tcp::socket(m_IoService); 
    // ----
    // ----
    m_Acceptor->async_accept( * temp, boost::bind(& CNetwork::acceptor, this, temp, boost::asio::placeholders::error));
}
//-----------------------------------------------------------------------------------------------------------------------

but i dont know how to receive and how i will do send to all client list

someone has a good a exmaple please share it , or explain me how to continue my class im new with boost i trying to leran it.


I don't know how to receive?

use the async_read() free function.

and how I will do send to all client list?

iterate through your m_Clients container and invoke async_write() on each socket.


unrelated, but don't do this

m_AcceptMutex.lock();
// ...
m_AcceptMutex.unlock();

Doing so is error prone and not exception safe. Use RAII here to unlock the mutex in a destructor. You're already using boost, so use boost::mutex::scoped_lock.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号