P
Arcoth schrieb:
Ich nehme also an, dass die Funktionsparameter Referenzen sind
Richtig. An sich möchte ich einen beliebigen Handler an einen boost-asio-Callback "durchschleifen". Und dort soll der Nutzer eben wie er möchte bind-Objekte oder eben auch einfache FUnktionen übergeben können.
/******************************************************************************/
/* adds a packet to the sending queue */
/******************************************************************************/
template<typename T>
void putMessage(const CBasicMessage& msg, T&& errorHandler)
{
//we can't give msg to the lambda because the post call returns immediately before the invocation
//therefore we create the packet here and copy it into the lambda, the packet itself contains a smart pointer to the buffer
m_socket.get_io_service().post(boost::bind(&CMessageChannel::putPacket<T>,
this,
Packet(msg.getMsgType(), msg.getLength(), boost::const_pointer_cast<CBasicMessage::CByteBuffer>(msg.getByteBuffer())),
errorHandler));
}
Für diese Methode gibt es noch eine zweite Variante, welche um boost::bind-Objekte ein boost::protect legt.
putPacket wird innerhalb des ioServices abgearbeitet und fügt das Paket in die Warteschlange hinzu:
template<typename T>
void putPacket(Packet& packet, T& errorHandler)
{
bool writeInProgess = !m_outgoing.empty();
m_outgoing.push_back(std::move(packet));
if (!writeInProgess)
{
startAsyncWrite(errorHandler);
}
};
startAsyncWrite startet dann den Schreibvorgang und übergibt den Handler per bind mit an die Callback-Methode, welche boost asio dann aufruft:
template<typename T>
void CMessageChannel::startAsyncWrite(T&& errorHandler)
{
//fill buffer with header information
auto& packet = m_outgoing.front();
m_headerOutBuffer.putInt(packet.size, 0);
m_headerOutBuffer.putShort(packet.type, 4);
//write it
auto& buf = m_headerOutBuffer.getBuffer();
boost::asio::async_write(m_socket, boost::asio::buffer(buf.begin(),
buf.size()),
boost::bind(&CMessageChannel::handleAsyncWriteHeader<T>, this, _1, _2, std::forward<T>(errorHandler)));
Und dabei tritt eben das oben genannte Problem auf.
VG
Pellaeon
}