TcpDomainConnection::TcpDomainConnection(boost::asio::io_service* ioService, DomainErrorCallback&& errorCallback) :
        DomainSocket{*ioService},
        DomainEndPoint{},
        _mutex{},
        mOutQueue{},
		_errorCallback{ std::move(errorCallback) },
        _readBuffer{}
	{
            auto cb = [this](uint8_t* bufPtr, size_t len, std::function<void(size_t)>&&handler) {
				auto myHandler = move(handler);
                auto boostHandler = [this, myHandler](const boost::system::error_code& ec,
                              std::size_t bytes_transferred) {
                    if (bytes_transferred == 0 || ec != 0) {
						auto me = this->shared_from_this();
						_readBuffer.EndReadSome();
						_errorCallback(me, ec);
                        return;
                    }
                    
                    myHandler(bytes_transferred);
                    
                };

                AsyncReadSome(bufPtr, len, std::move(boostHandler));

            };
            
            IoCircularBuffer bufWithCb{ cb };
            _readBuffer = std::move(bufWithCb);
    }
Esempio n. 2
0
/**
 *  Report an error message on a channel
 *  @param  message             the error message
 *  @param  notifyhandler       should the channel-wide handler also be called?
 */
void ChannelImpl::reportError(const char *message, bool notifyhandler)
{
    // change state
    _state = state_closed;
    _synchronous = false;
    
    // the queue of messages that still have to sent can be emptied now
    // (we do this by moving the current queue into an unused variable)
    auto queue(std::move(_queue));

    // we are going to call callbacks that could destruct the channel
    Monitor monitor(this);

    // call the oldest
    if (_oldestCallback)
    {
        // copy the callback (so that it can not be destructed during
        // the "reportError" call
        auto cb = _oldestCallback;
        
        // call the callback
        auto next = cb->reportError(message);

        // leap out if channel no longer exists
        if (!monitor.valid()) return;

        // set the oldest callback
        _oldestCallback = next;
    }

    // clean up all deferred other objects
    while (_oldestCallback)
    {
        // copy the callback (so that it can not be destructed during
        // the "reportError" call
        auto cb = _oldestCallback;

        // call the callback
        auto next = cb->reportError("Channel is in error state");

        // leap out if channel no longer exists
        if (!monitor.valid()) return;

        // set the oldest callback
        _oldestCallback = next;
    }

    // all callbacks have been processed, so we also can reset the pointer to the newest
    _newestCallback = nullptr;

    // inform handler
    if (notifyhandler && _errorCallback) _errorCallback(message);

    // leap out if object no longer exists
    if (!monitor.valid()) return;

    // the connection now longer has to know that this channel exists,
    // because the channel ID is no longer in use
    if (_connection) _connection->remove(this);
}
	void TcpDomainConnection::ConnectHandler(std::shared_ptr<TcpDomainConnection> conn, boost::system::error_code ec)
	{
		if (ec)
		{
			_errorCallback(conn, ec);
			return;
		}

		_connectCallback(shared_from_this());
	}
	void TcpPeerConnection::ConnectHandler(std::shared_ptr<TcpPeerConnection> conn, boost::system::error_code ec)
	{
		if (ec)
		{
			_errorCallback(conn, ec);
			return;
		}

		UpperLayerHandleConnect();
	}
    void TcpSslConnection::HandleHandshake(const boost::system::error_code ec) {
        if (ec)
        {
            _errorCallback(shared_from_this(), ec);
            return;
        }
        
        std::cout << "SSL handle handshake" << std::endl;
        _connectCallback(shared_from_this());

    }