steady_clock::time_point steady_clock::now(system::error_code & ec) { timespec ts; if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) ) { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::steady_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } return time_point(duration( static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec)); }
void process_clock::now( process_times & times_, system::error_code & ec ) { tms tm; clock_t c = ::times( &tm ); if ( c == clock_t(-1) ) // error { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); times_.real = times_.system = times_.user = nanoseconds(-1); } } else { times_.real = microseconds(c); times_.system = microseconds(tm.tms_stime + tm.tms_cstime); times_.user = microseconds(tm.tms_utime + tm.tms_cutime); if ( chrono_detail::tick_factor() != -1 ) { if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } times_.real *= chrono_detail::tick_factor(); times_.user *= chrono_detail::tick_factor(); times_.system *= chrono_detail::tick_factor(); } else { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); times_.real = times_.user = times_.system = nanoseconds(-1); } } } }
process_cpu_clock::time_point process_cpu_clock::now( system::error_code & ec ) { tms tm; clock_t c = ::times( &tm ); if ( c == clock_t(-1) ) // error { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } else { if ( chrono_detail::tick_factor() != -1 ) { time_point::rep r( c*chrono_detail::tick_factor(), (tm.tms_utime + tm.tms_cutime)*chrono_detail::tick_factor(), (tm.tms_stime + tm.tms_cstime)*chrono_detail::tick_factor()); return time_point(duration(r)); } else { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } } }
process_real_cpu_clock::time_point process_real_cpu_clock::now( system::error_code & ec) { tms tm; clock_t c = ::times( &tm ); if ( c == clock_t(-1) ) // error { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } else { if ( chrono_detail::tick_factor() != -1 ) { if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } return time_point( microseconds(c)*chrono_detail::tick_factor()); } else { if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock" )); } else { ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } } }
process_cpu_clock::time_point process_cpu_clock::now( system::error_code & ec ) { // note that Windows uses 100 nanosecond ticks for FILETIME boost::detail::win32::FILETIME_ creation, exit, user_time, system_time; #ifdef UNDER_CE // Windows CE does not support GetProcessTimes assert( 0 && "GetProcessTimes not supported under Windows CE" ); return time_point(); #else if ( boost::detail::win32::GetProcessTimes( boost::detail::win32::GetCurrentProcess(), &creation, &exit, &system_time, &user_time ) ) { if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } time_point::rep r( steady_clock::now().time_since_epoch().count(), ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32) | user_time.dwLowDateTime ) * 100, ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32) | system_time.dwLowDateTime ) * 100 ); return time_point(duration(r)); } else { boost::detail::win32::DWORD_ cause = boost::detail::win32::GetLastError(); if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( cause, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_cpu_clock" )); } else { ec.assign( cause, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } #endif }
process_cpu_clock::time_point process_cpu_clock::now( system::error_code & ec ) { tms tm; clock_t c = ::times( &tm ); if ( c == clock_t(-1) ) // error { if (::boost::chrono::is_throws(ec)) { boost::throw_exception( system::system_error( errno, ::boost::system::system_category(), "chrono::process_clock" )); } else { ec.assign( errno, ::boost::system::system_category() ); return time_point(); } } else { if ( chrono_detail::tick_factor() != -1 ) { time_point::rep r( c*chrono_detail::tick_factor(), (tm.tms_utime + tm.tms_cutime)*chrono_detail::tick_factor(), (tm.tms_stime + tm.tms_cstime)*chrono_detail::tick_factor()); return time_point(duration(r)); } else { if (::boost::chrono::is_throws(ec)) { boost::throw_exception( system::system_error( errno, ::boost::system::system_category(), "chrono::process_clock" )); } else { ec.assign( errno, ::boost::system::system_category() ); return time_point(); } } } }
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()); } }
bool basic_io_device::would_block(system::error_code const &e) { int code = e.value(); bool block = 0 #ifdef EAGAIN || code==EAGAIN #endif #ifdef EINPROGRESS || code == EINPROGRESS #endif #ifdef EWOULDBLOCK || code == EWOULDBLOCK #endif #ifdef WSAEAGAIN || code==WSAEAGAIN #endif #ifdef WSAEINPROGRESS || code == WSAEINPROGRESS #endif #ifdef WSAEWOULDBLOCK || code == WSAEWOULDBLOCK #endif ; return block; }
process_cpu_clock::time_point process_cpu_clock::now( system::error_code & ec ) { // note that Windows uses 100 nanosecond ticks for FILETIME autoboost::detail::winapi::FILETIME_ creation, exit, user_time, system_time; if ( autoboost::detail::winapi::GetProcessTimes( autoboost::detail::winapi::GetCurrentProcess(), &creation, &exit, &system_time, &user_time ) ) { if (!AUTOBOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count() , ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32) | user_time.dwLowDateTime ) * 100, ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32) | system_time.dwLowDateTime ) * 100 ); return time_point(duration(r)); } else { autoboost::detail::winapi::DWORD_ cause = autoboost::detail::winapi::GetLastError(); if (AUTOBOOST_CHRONO_IS_THROWS(ec)) { autoboost::throw_exception( system::system_error( cause, AUTOBOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_cpu_clock" )); } else { ec.assign( cause, AUTOBOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } }
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 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 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 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 run_timer::report( system::error_code & ec ) { m_reported = true; if ( m_format.empty() ) m_format = chrono_detail::default_format(); process_times times; elapsed( times, ec ); if (ec) return; if ( BOOST_CHRONO_IS_THROWS(ec) ) { chrono_detail::show_time( times, m_format.c_str(), m_places, m_os ); } else // non-throwing { try { chrono_detail::show_time( times, m_format.c_str(), m_places, m_os ); if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } } catch (...) // eat any exceptions { BOOST_ASSERT( 0 && "error reporting not fully implemented yet" ); if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::run_timer" )); } else { ec.assign(system::errc::success, BOOST_CHRONO_SYSTEM_CATEGORY); } } } }
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 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 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); }
process_system_cpu_clock::time_point process_system_cpu_clock::now( system::error_code & ec) { // note that Windows uses 100 nanosecond ticks for FILETIME boost::detail::win32::FILETIME_ creation, exit, user_time, system_time; if ( boost::detail::win32::GetProcessTimes( boost::detail::win32::GetCurrentProcess(), &creation, &exit, &system_time, &user_time ) ) { if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } return time_point(duration( ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32) | system_time.dwLowDateTime) * 100 )); } else { boost::detail::win32::DWORD_ cause = boost::detail::win32::GetLastError(); if (BOOST_CHRONO_IS_THROWS(ec)) { boost::throw_exception( system::system_error( cause, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock" )); } else { ec.assign( cause, BOOST_CHRONO_SYSTEM_CATEGORY ); return time_point(); } } }
// 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 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(); }
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; }
process_system_cpu_clock::time_point process_system_cpu_clock::now(system::error_code & ec) { tms tm; clock_t c = ::times(&tm); if (c == clock_t(-1)) // error { if (BOOST_CHRONO_IS_THROWS(ec)) { pdalboost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock")); } else { ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY); return time_point(); } } else { long factor = chrono_detail::tick_factor(); if (factor != -1) { if (!BOOST_CHRONO_IS_THROWS(ec)) { ec.clear(); } return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime) * factor)); } else { if (BOOST_CHRONO_IS_THROWS(ec)) { pdalboost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock")); } else { ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY); return time_point(); } } } }
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 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 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 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)); } }