void onResolveSuccess(const boost::system::error_code& error, typename resolver::iterator remoteEndpoint, const shared_ptr<Resolver>& self) { scheduler::cancel(m_resolveTimeout); if (error) { if (error == boost::system::errc::operation_canceled) return; return m_onError("Remote endpoint hostname or port cannot be resolved: " + error.category().message(error.value())); } typename resolver::iterator end; for (; remoteEndpoint != end; ++remoteEndpoint) { if (m_addressSelector(typename Protocol::endpoint(*remoteEndpoint).address())) return m_onSuccess(*remoteEndpoint); } m_onError("No endpoint matching the specified address selector found"); }
void onResolveSuccess(const boost::system::error_code& error, BoostResolver::iterator remoteEndpoint, const shared_ptr<Resolver>& self) { m_scheduler.cancelEvent(m_resolveTimeout); if (error) { if (error == boost::system::errc::operation_canceled) { return; } return m_onError("Remote endpoint hostname or port cannot be resolved: " + error.category().message(error.value())); } BoostResolver::iterator end; for (; remoteEndpoint != end; ++remoteEndpoint) { IpAddress address(EndPoint(*remoteEndpoint).address()); if (m_addressSelector(address)) { return m_onSuccess(address); } } m_onError("No endpoint matching the specified address selector found"); }
void onResolveResult(const boost::system::error_code& error, iterator it, const shared_ptr<Resolver>& self) { m_scheduler.cancelEvent(m_resolveTimeout); // ensure the Resolver isn't destructed while callbacks are still pending, see #2653 m_resolver.get_io_service().post(bind([] (const shared_ptr<Resolver>&) {}, self)); if (error) { if (error == boost::asio::error::operation_aborted) return; if (m_onError) m_onError("Hostname cannot be resolved: " + error.message()); return; } it = selectAddress(it); if (it != iterator() && m_onSuccess) { m_onSuccess(it->endpoint().address()); } else if (m_onError) { m_onError("No endpoints match the specified address selector"); } }
void onResolveError(const std::string& errorInfo, const shared_ptr<Resolver>& self) { m_resolver.cancel(); m_onError(errorInfo); }
/** * Handle an error having been detected. */ void ESP_RestClient::_handleError() { // Close the connection to the partner and then invoke the callback function // if one exists of the error callback. //LOG("_handleError"); _close(); if (m_onError != NULL) { //LOG("Calling error handler"); m_onError(); } } // End of _handleError
void onResolveTimeout(const shared_ptr<Resolver>& self) { m_resolver.cancel(); // ensure the Resolver isn't destructed while callbacks are still pending, see #2653 m_resolver.get_io_service().post(bind([] (const shared_ptr<Resolver>&) {}, self)); if (m_onError) m_onError("Hostname resolution timed out"); }
/// 压入数据 void tcp_packet_parser::Impl::sputn( char * buf, std::size_t len ) { BOOST_ASSERT(m_buffer); BOOST_ASSERT(m_pWrapper); const std::size_t head_size = m_pWrapper->size_of(); while (len) { if (0 == m_pos) { // 缓冲区中无数据, 直接在上层缓冲区中尝试组包. if (len < head_size) { // 不足一个封包头, 写入缓冲区即可 memcpy(m_buffer, buf, len); m_pos += len; return ; } else { // 有完整的封包头, 进一步检测是否有完整的封包. std::size_t packet_len = m_pWrapper->buf_size(buf) + head_size; if (packet_len > m_size) { // 封包长错误 if (m_onError) m_onError(); return ; } if (len < packet_len) { // 不足一个封包 memcpy(m_buffer, buf, len); m_pos += len; return ; } else { // 有完整的封包, 直接处理. m_pWrapper->onPacket(buf, packet_len); buf += packet_len; len -= packet_len; continue; } } } else if (m_pos < head_size) { // 缓冲区中有数据, 但不足一个封包头, 尝试拼足一个封包头 std::size_t delta = head_size - m_pos; std::size_t cpy_size = (std::min)(delta, len); memcpy(m_buffer + m_pos, buf, cpy_size); m_pos += cpy_size; buf += cpy_size; len -= cpy_size; continue; } else { // 缓冲区中有完整的封包头 std::size_t packet_len = m_pWrapper->buf_size(m_buffer) + head_size; if (packet_len > m_size) { // 封包长错误 if (m_onError) m_onError(); m_pos = 0; return ; } std::size_t delta = packet_len - m_pos; if (delta > len) { // 无法拼出一个完整的封包 memcpy(m_buffer + m_pos, buf, len); m_pos += len; return ; } else { // 可以拼出一个完整的封包 memcpy(m_buffer + m_pos, buf, delta); m_pWrapper->onPacket(m_buffer, m_pos + delta); m_pos = 0; buf += delta; len -= delta; continue; } } } }