void send() { BOOST_CHECK_EQUAL( socket_.is_open(), true ); size_t l = 5; socket_.send( boost::asio::buffer( &l, sizeof(size_t) ) ); socket_.send( boost::asio::buffer( "hello", 5 ) ); }
int justine::sampleclient::ShmClient::init ( boost::asio::ip::tcp::socket & socket ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<init guided Binfavágók 1 c>" ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } int id {0}; std::sscanf ( data, "<OK %d", &id ); std::cout.write ( data, length ); std::cout << "Command INIT sent." << std::endl; return id; }
void justine::sampleclient::ShmClient::route2 ( boost::asio::ip::tcp::socket & socket, int id ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<route 6 %d 1348670117 320900594 1348670109 320900801 651365957 2969934868>", id ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } std::cout.write ( data, length ); std::cout << "Command ROUTE2 sent." << std::endl; }
void justine::sampleclient::ShmClient::gangsters ( boost::asio::ip::tcp::socket & socket, int id ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<gangsters " ); length += std::sprintf ( data+length, "%d>", id ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } std::cout.write ( data, length ); std::cout << "Command GANGSTER sent." << std::endl; }
void justine::sampleclient::ShmClient::car ( boost::asio::ip::tcp::socket & socket, int id, unsigned *f, unsigned *t, unsigned* s ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<car " ); length += std::sprintf ( data+length, "%d>", id ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } int idd {0}; std::sscanf ( data, "<OK %d %u %u %u", &idd, f, t, s ); std::cout.write ( data, length ); std::cout << "Command CAR sent." << std::endl; }
std::vector<justine::sampleclient::MyShmClient::Gangster> justine::sampleclient::MyShmClient::gangsters ( boost::asio::ip::tcp::socket & socket, int id, osmium::unsigned_object_id_type cop ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<gangsters " ); length += std::sprintf ( data+length, "%d>", id ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } /* reading all gangsters into a vector */ int idd {0}; unsigned f, t, s; int n {0}; int nn {0}; std::vector<Gangster> gangsters; while ( std::sscanf ( data+nn, "<OK %d %u %u %u>%n", &idd, &f, &t, &s, &n ) == 4 ) { nn += n; gangsters.push_back ( Gangster {idd, f, t, s} ); } std::sort ( gangsters.begin(), gangsters.end(), [this, cop] ( Gangster x, Gangster y ) { return dst ( cop, x.to ) < dst ( cop, y.to ); } ); std::cout.write ( data, length ); std::cout << "Command GANGSTER sent." << std::endl; return gangsters; }
void run_socket_io(boost::asio::ip::tcp::socket& socket) { std::array<char, 1024> read_buffer; std::array<char, 1024> write_buffer; socket.async_read_some(boost::asio::buffer(write_buffer, 1024), [&write_buffer](const boost::system::error_code& ec, std::size_t length) { if (!ec) std::cout.write(write_buffer.data(), length); }); while (std::cin.getline(read_buffer.data(), 1024)) { socket.send( boost::asio::buffer(read_buffer, std::strlen(read_buffer.data()))); } }
std::vector<justine::sampleclient::MyShmClient::Cop> justine::sampleclient::MyShmClient::initcops ( boost::asio::ip::tcp::socket & socket ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<init guided %s 10 c>", m_teamname.c_str() ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } /* reading all gangsters into a vector */ int idd {0}; int f, t; char c; int n {0}; int nn {0}; std::vector<Cop> cops; while ( std::sscanf ( data+nn, "<OK %d %d/%d %c>%n", &idd, &f, &t, &c, &n ) == 4 ) { nn += n; cops.push_back ( idd ); } std::cout.write ( data, length ); std::cout << "Command INIT sent." << std::endl; return cops; }
void justine::sampleclient::MyShmClient::route ( boost::asio::ip::tcp::socket & socket, int id, std::vector<osmium::unsigned_object_id_type> & path ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<route %d %d", path.size(), id ); for ( auto ui: path ) length += std::sprintf ( data+length, " %u", ui ); length += std::sprintf ( data+length, ">" ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } std::cout.write ( data, length ); std::cout << "Command ROUTE sent." << std::endl; }
void send(std::string const& message) { socket.send(boost::asio::buffer(message)); }
void justine::sampleclient::ShmClient::pos ( boost::asio::ip::tcp::socket & socket, int id ) { boost::system::error_code err; size_t length = std::sprintf ( data, "<pos " ); length += std::sprintf ( data+length, "%d %u %u>", id, 2969934868u, 651365957u ); socket.send ( boost::asio::buffer ( data, length ) ); length = socket.read_some ( boost::asio::buffer ( data ), err ); if ( err == boost::asio::error::eof ) { // TODO } else if ( err ) { throw boost::system::system_error ( err ); } std::cout.write ( data, length ); std::cout << "Command POS sent." << std::endl; }
/** * Вызывается всякий раз, когда данные получены. */ static void handleRead( ba::ip::tcp::socket& readFrom, ba::ip::tcp::socket& writeTo, char* readBuffer, size_t bytes, const boost::system::error_code& e ) { #ifdef _DEBUG const std::string data( readBuffer, readBuffer + bytes ); std::cout << data << std::endl; #endif // отправляем полученные данные "другой стороне" writeTo.send( ba::buffer( readBuffer, bytes ) ); // читаем ещё данные с "этой стороны" readFrom.async_read_some( ba::buffer( readBuffer, 1024 ), boost::bind( handleRead, boost::ref( readFrom ), boost::ref( writeTo), readBuffer, ba::placeholders::bytes_transferred, ba::placeholders::error ) ); }