本文整理汇总了C++中boost::asio::ip::tcp::socket::read_some方法的典型用法代码示例。如果您正苦于以下问题:C++ socket::read_some方法的具体用法?C++ socket::read_some怎么用?C++ socket::read_some使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::asio::ip::tcp::socket
的用法示例。
在下文中一共展示了socket::read_some方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
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;
}
示例2: if
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;
}
示例3: receive
size_t receive(Connection::BufferType & data)
{
boost::system::error_code error;
size_t length = socket.read_some
(
boost::asio::buffer
(
data.data()
,data.size()
)
,error
);
if(!error) return length;
if(error == boost::asio::error::eof)
return length;
else if(error)
throw boost::system::system_error(error);
}
示例4: if
void justine::robocar::Traffic::cmd_session ( boost::asio::ip::tcp::socket client_socket )
{
const int network_buffer_size = 524288;
char data[network_buffer_size]; // TODO buffered write...
try
{
for ( ;; )
{
CarLexer cl;
boost::system::error_code err;
size_t length = client_socket.read_some ( boost::asio::buffer ( data ), err );
if ( err == boost::asio::error::eof )
{
break;
}
else if ( err )
{
throw boost::system::system_error ( err );
}
std::istringstream pdata ( data );
cl.switch_streams ( &pdata );
cl.yylex();
length = 0;
int resp_code = cl.get_errnumber();
int num = cl.get_num();
int id {0};
if ( cl.get_cmd() == 0 )
{
for ( ;; )
{
std::vector<std::shared_ptr<Car>> cars_copy;
{
std::lock_guard<std::mutex> lock ( cars_mutex );
cars_copy = cars;
}
std::stringstream ss;
ss <<
m_time <<
" " <<
m_minutes <<
" " <<
cars_copy.size()
<< std::endl;
/*for ( auto car:cars_copy )
{
car->step();
ss << *car
<< " " << std::endl;
}*/
//atirva
for ( std::vector<std::shared_ptr<Car>>::iterator it=cars_copy.begin(); it != cars_copy.end(); ++it )
{
auto car = *it;
car->step();
ss << *car
<< " " << std::endl;
}
boost::asio::write ( client_socket, boost::asio::buffer ( data, length ) );
length = std::sprintf ( data,
"%s", ss.str().c_str() );
boost::asio::write ( client_socket, boost::asio::buffer ( data, length ) );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 200 ) );
}
}
else if ( cl.get_cmd() <100 )
{
std::lock_guard<std::mutex> lock ( cars_mutex );
for ( int i {0}; i<cl.get_num(); ++i )
{
if ( cl.get_role() =='c' )
id = addCop ( cl );
else
id = addGangster ( cl );
if ( !resp_code )
length += std::sprintf ( data+length,
"<OK %d %d/%d %c>", id, num,
i+1, cl.get_role() );
else
//.........这里部分代码省略.........