inline void throw_system_error(system::error_code ec) { if (ec) { if (ec.category() == runtime_error_category()) { throw runtime_system_error(ec, ec.message()); } if (ec.category() == logic_error_category()) { throw logic_system_error(ec, ec.message()); } throw system::system_error(ec, ec.message()); } }
void CProtocol::on_empty_buffer(const system::error_code& error, std::size_t bytes) { empty_buffer_[bytes] = 0; std::string reply(empty_buffer_); if (bytes > 0) std::cout << "on_empty_buffer: " << bytes << " " << reply << std::endl; buffer_empty_ = true; if(error != 0) std::cout << "on_empty_buffer error: " << error << " message: "<< error.message() << std::endl; }
void Peer::handle_read(const system::error_code& e, std::size_t bytes_transferred) { log_trace("args error: %s, bytes: %d", e.message(), bytes_transferred); if (!e) { _activity = true; string rx; for(int i = 0; i < bytes_transferred; i++) rx.push_back(_buffer[i]); vRecv += rx; // Now call the parser: bool fRet = false; loop { boost::tuple<boost::tribool, CDataStream::iterator> parser_result = _msgParser.parse(_chain, _message, vRecv.begin(), vRecv.end()); tribool result = get<0>(parser_result); vRecv.erase(vRecv.begin(), get<1>(parser_result)); if (result) { if (_messageHandler.handleMessage(this, _message) ) fRet = true; } else if (!result) { log_warn("Peer %s sending bogus - disconnecting", addr.toString()); _peerManager.post_stop(shared_from_this()); }// continue; // basically, if we get a false result, we should consider to disconnect from the Peer! else break; } // now if fRet is true, something were processed by the filters - we want to send to the peers / we check for which vSends cointains stuff and the we run if (fRet && nVersion > 0) { // first reply reply(); // then trickle Peers peers = _peerManager.getAllPeers(); size_t rand = GetRand(peers.size()); for (Peers::iterator peer = peers.begin(); peer != peers.end(); ++peer) if(rand-- == 0) { (*peer)->trickle(); break; } // then broadcast for (Peers::iterator peer = peers.begin(); peer != peers.end(); ++peer) (*peer)->broadcast(); // now write to the peers with non-empty vSend buffers for (Peers::iterator peer = peers.begin(); peer != peers.end(); ++peer) { (*peer)->flush(); } } // then wait for more data _socket.async_read_some(buffer(_buffer), boost::bind(&Peer::handle_read, shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred)); // async_read(_socket, _recv, boost::bind(&Peer::handle_read, shared_from_this(), asio::placeholders::error, asio::placeholders::bytes_transferred)); }
void CTcpSocket::OnSend( const system::error_code &ec, size_t nByteTransferred ) { if (ec) { LOGError(ec.message().c_str()); DoClose(); return ; } m_wSendLength -= nByteTransferred; if (m_wSendLength > 0) { LOGError("理论来讲,这里是进不来的!"); // 理论来讲,这里是进不来的。 memcpy(m_szSendBuffer, m_szSendBuffer + nByteTransferred, m_wSendLength); // 发送数据 m_pSock->async_write_some(buffer(m_szSendBuffer, m_wSendLength), bind(&CTcpSocket::OnSend, this, placeholders::error, placeholders::bytes_transferred)); } else if (0 == m_wSendLength) { // 拷贝数据 std::list<MsgData>::iterator it = m_listMsgData.begin(); for (; it!=m_listMsgData.end(); ) { MsgData &msgData = (*it); if (MAX_SEND_BUFFER_LENGTH - m_wSendLength >= msgData.wLength) { USHORT *pPacketLength = (USHORT *)m_szSendBuffer; *pPacketLength = msgData.wLength; m_wSendLength += sizeof(USHORT); memcpy(m_szSendBuffer + m_wSendLength, msgData.szData, msgData.wLength); m_wSendLength += msgData.wLength; it = m_listMsgData.erase(it); } else { break; } } // 发送数据 m_pSock->async_write_some(buffer(m_szSendBuffer, m_wSendLength), bind(&CTcpSocket::OnSend, this, placeholders::error, placeholders::bytes_transferred)); } else { LOGError("异常错误!"); } }
void Proxy::handle_write(const system::error_code& e, size_t bytes_transferred) { if (!e) { // ignore } else if (e != error::operation_aborted) { log_error("Proxy write error %s, disconnecting...", e.message()); // forward the error to the connection handler callback _connection_handler(e); } }
void Sony_Remote_Camera_Implementation::Handle_Write_HTTP_Request(bool mode_liveview, bool event_thread, const system::error_code& err) { asio::ip::tcp::socket& s = mode_liveview ? socket_liveview : (event_thread ? socket_event_listener : socket_options); asio::streambuf& buf = mode_liveview ? tcp_response_liveview : (event_thread ? tcp_response_events : tcp_response_options); if (!err) { boost::asio::async_read_until(s, buf, "\r\n", boost::bind(&Sony_Remote_Camera_Implementation::Handle_Read_Status_Line, this, mode_liveview, event_thread, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } else { throw(ios_base::failure(err.message())); } }
void callback(system::error_code ec) { std::cout << __PRETTY_FUNCTION__ << "\n"; if (ec != asio::error::operation_aborted) { std::cout << " - continuing wait - keeps the object alive (as it should)\n"; tim.expires_from_now(posix_time::seconds(2)); tim.async_wait(bind(&MySessionThing::callback, shared_from_this(), asio::placeholders::error)); } std::cout << " - " << ec.message() << ": exiting handler - also implicitly releases the lock on MySessionThing\n"; // only if the new async_wait has *not* been posted, this will cause the destructor to run }
void Client::on_send(system::error_code err, size_t bytesTransferred, uint8_t* p, size_t packetLength) { unused_parameter(bytesTransferred); unused_parameter(packetLength); delete[] p; if(err) return Transmitter::disconnect(format("Send failed: %2%") % err.message()); assert(bytesTransferred == packetLength); }
void TcpServer::handle_accept(const system::error_code& error) { if(!acceptor_.is_open()) { debug->warning(1, type, "TCP Socket, port %i - acceptor is not open!", port); return; } if(!error) new_connection->start(); else debug->warning(1, type, "TCP Socket, port %i - error: %s, error code: %i", error.message().c_str(), error.value()); start_accept(); }
// read_callback - passed information about bytes in read_buf to handle_by_protocol method. void read_callback(size_t bytes_transferred,const system::error_code& error) { if (error || !bytes_transferred) { if(log_level) std::cerr << "read callback error:" << error << ": " << error.message() << std::endl; return; } long packet_found = data_received(read_buf,bytes_transferred); if(!packet_found) { initiate_read(); } }
void CProtocol::on_receive_reply(const system::error_code& error, std::size_t bytes) { cmd_response_[bytes] = 0; std::string reply(cmd_response_); std::cout << "<- " << reply << std::endl; if (bytes != active_command_.expected_sizeof_reply()) return; if(running_) active_command_.handle_reply(reply); // signal command finished command_ready_ = true; if(error != 0) std::cout << "on_receive_reply error: " << error << " message: "<< error.message() << " bytes: " << bytes << std::endl; }
//接受新的请求 void accept_handler(const system::error_code& e, sock_ptr s) { if (e) { cout << e.message() << endl; return; } //添加 clientSock.push_back(s); cout << "client:" << s->remote_endpoint().address() << " port:" << s->remote_endpoint().port() << endl; s->async_write_some(buffer("hello asio"), boost::bind(&server::write_handle, this, boost::asio::placeholders::error, s)); //继续异步请求 start(); }
void on_recv(const system::error_code &error) { if(error) { cerr << "recv error: "<< error.message() << "\n"; close(); return; } else { cerr << _buf ; } _stream.async_read_some(buffer(_buf,sizeof(_buf)), boost::bind(&OCS_Stream::on_recv,this,placeholders::error)); }
void Proxy::handle_proxy_connect(const system::error_code& e) { // write the if (!e && _socket->is_open()) { // connected successfully to the proxy server Socks4::Request req(Socks4::Request::connect, _endpoint, ""); async_write(*_socket, req.buffers(), boost::bind(&Proxy::handle_write, this, asio::placeholders::error, asio::placeholders::bytes_transferred)); // this async_read returns only when the buffer has been filled async_read(*_socket, _reply.buffers(), boost::bind(&Proxy::handle_proxy_reply, this, asio::placeholders::error, asio::placeholders::bytes_transferred)); } else { log_error("Failed connect to proxy server: \"%s\" to: %s", e.message(), lexical_cast<string>(_server)); _connection_handler(e); } }
void Peer::check_activity(const system::error_code& e) { if (!e) { if(!_activity) _peerManager.post_stop(shared_from_this()); else { _activity = false; _suicide.expires_from_now(posix_time::seconds(_suicide_timeout)); // 90 minutes of activity once we have started up _suicide.async_wait(boost::bind(&Peer::check_activity, this, asio::placeholders::error)); } } else if (e != error::operation_aborted) { log_info("Boost deadline timer error in Peer: %s\n", e.message().c_str()); } // we ignore abort errors - they are generated due to timer cancels }
void on_recv(const system::error_code &error) { if(error) { cerr << "recv error: "<< error.message() << "\n"; close(); return; } else { std::istream is(&_buf); string line; getline(is, line); _timer.cancel(); //cancel timeout after recveive cmd //cerr << line ; } }
void Proxy::handle_proxy_reply(const system::error_code& e, size_t bytes_transferred) { if (!e && _reply.success()) { // buffers are now filled - check if it was successfull _connection_handler(e); } else if (e != error::operation_aborted) { log_error("Proxy write error %s, disconnecting...", e.message()); // forward the error to the connection handler callback _connection_handler(e); } else if (!_reply.success()) { log_error("Proxy connection error - status code %d...", _reply.status()); system::error_code err(_reply.status(), __application_category); _connection_handler(err); } else _connection_handler(e); }
void Listener::handle_accept(list<Peer*>::iterator new_peer_it, const system::error_code& error) { if (!error) { shared_ptr<Peer> new_peer(*new_peer_it); network->add_peer(new_peer); pending_peers.erase(new_peer_it); DEBUG("Peer at address " << new_peer->get_address() << " has joined!"); start_accept(); } else { DEBUG("Listener::handle_accept: " << error.message()); } }
void Acceptor::OnAccept(const system::error_code & _errorCode) { if (_errorCode) { if (_errorCode != asio::error::operation_aborted) { BOOST_LOG_TRIVIAL(error) << _errorCode.message(); } return; } else { //если обработалось без ошибки rServer.CreateConnection(std::move(socket)); } socket = asio::ip::tcp::socket(rService); Start();//снова начинаем прослушку }
void ChatClient::handle_resolve(const system::error_code& err, tcp::resolver::iterator endpoint_iterator) { if (!err) { // Attempt a connection to the first endpoint in the list. Each endpoint // will be tried until we successfully establish a connection. tcp::endpoint endpoint = *endpoint_iterator; endpoint.port(6667); if (_proxy) _proxy(_socket).async_connect(endpoint, boost::bind(&ChatClient::handle_connect, this, asio::placeholders::error, ++endpoint_iterator)); else _socket.async_connect(endpoint, boost::bind(&ChatClient::handle_connect, this, asio::placeholders::error, ++endpoint_iterator)); } else { log_warn("Error: %s\n", err.message().c_str()); _socket.close(); _mode = wait_for_notice; tcp::resolver::query query(_server, "irc"); // should we remove irc as service type ? _resolver.async_resolve(query, boost::bind(&ChatClient::handle_resolve, this, asio::placeholders::error, asio::placeholders::iterator)); } }
void Peer::show_activity(const system::error_code& e) { if (!e) { if(!_activity) { // send a ping - we might get a pong then // Keep-alive ping. We send a nonce of zero because we don't use it anywhere // right now. uint64_t nonce = 0; if (nVersion > BIP0031_VERSION) PushMessage("ping", nonce); else PushMessage("ping"); } _suicide.expires_from_now(posix_time::seconds(_heartbeat_timeout)); // show activity each 30 minutes _suicide.async_wait(boost::bind(&Peer::show_activity, this, asio::placeholders::error)); } else if (e != error::operation_aborted) { log_info("Boost deadline timer error in Peer: %s\n", e.message().c_str()); } // we ignore abort errors - they are generated due to timer cancels }
void CTcpSocket::OnRecv( const system::error_code& ec, size_t nByteTransferred ) { if (ec) { LOGError(ec.message().c_str()); DoClose(); return ; } m_wHaveRecvLength += nByteTransferred; // 黏包解析 USHORT wReadLength = 0; //解析长度修改都是完整消息包 while (true) { const char *pPacketHead = m_szRecvBuffer + wReadLength; USHORT wRemainLength = m_wHaveRecvLength - wReadLength; USHORT wReadPacketLength = ReadPacket(pPacketHead, wRemainLength); if (wReadPacketLength > 0) { wReadLength += wReadPacketLength; } else { if ((wRemainLength > 0) && (wRemainLength != m_wHaveRecvLength)) { memcpy(m_szRecvBuffer, m_szRecvBuffer+wReadLength, wRemainLength); } m_wHaveRecvLength = wRemainLength; break; } } DoRecv(); }
void CProtocol::on_write(const system::error_code& error, std::size_t bytes) { //signal on error std::cout << "on_write " << bytes << std::endl; if(error != 0) std::cout << "on_write error: " << error << " message: "<< error.message() << " bytes: " << bytes << std::endl; }
void fail(const string& method, const system::error_code& e) { cerr << "[ERROR] " << method << ": " << e.message() << endl; }
static std::string convert ( const system::error_code & ec ) { return str ( format ( "%s (%i)" ) % ec.message () % ec.value () ); }