本文整理汇总了C++中socket_ptr类的典型用法代码示例。如果您正苦于以下问题:C++ socket_ptr类的具体用法?C++ socket_ptr怎么用?C++ socket_ptr使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了socket_ptr类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: on_async_connect_timed
// 异步连接回调(带超时)
void on_async_connect_timed(error_code const& ec, socket_ptr sp
, endpoint addr, boost::posix_time::time_duration timed)
{
if (ec)
{
// 错误
ec_ = ec;
async_connecting_.reset();
notify_onconnect();
}
else if (sp->lowest_layer().remote_endpoint() == sp->lowest_layer().local_endpoint())
{
// 回环假链接, 重试.
async_connecting_.reset();
if (!async_connect_timed(addr, timed))
{
if (!ec_) ec_ = make_error_code(errc::reconnect_error);
notify_onconnect();
}
return ;
}
else
{
// 连接成功, 握手
async_handshake(sp, addr);
}
}
开发者ID:linxuanwei,项目名称:Bex,代码行数:28,代码来源:client.hpp
示例2: connection
void Server::connection(socket_ptr _sock) {
try {
CLOG_INFO << "Connection, peer IP: " << _sock->remote_endpoint().address().to_string() << endl;
Operation op;
receive(_sock, op);
if ( op == Operation::CREATE ) {
Counter(_sock, this);
}
else if ( op == Operation::INFO_DEVICE ||
op == Operation::LIST_DEVICES ||
op == Operation::CMD_STATUS ||
op == Operation::READ_DEVICE ) {
Info(_sock, op, this);
}
else {
send<int>(_sock, (int)Retval::ERROR);
}
_sock->close();
}
catch (exception& e) {
CLOG_ERROR << "Exception in thread: " << e.what() << endl;
}
}
开发者ID:mdolz,项目名称:PMLib,代码行数:25,代码来源:server.cpp
示例3: on_async_connect
// 异步连接回调
void on_async_connect(error_code const& ec, socket_ptr sp, endpoint addr)
{
if (ec)
{
// 错误
ec_ = ec;
async_connecting_.reset();
notify_onconnect(); // 通知连接结果
}
else if (sp->lowest_layer().remote_endpoint() == sp->lowest_layer().local_endpoint())
{
// 回环假链接, 重试.
async_connecting_.reset();
if (!async_connect(addr))
{
if (!ec_) ec_ = make_error_code(errc::reconnect_error);
notify_onconnect();
}
}
else
{
// 连接成功, 握手
async_handshake(sp, addr);
}
}
开发者ID:linxuanwei,项目名称:Bex,代码行数:26,代码来源:client.hpp
示例4: async_connect_handler
/// 连接回调
void tcp_connector::async_connect_handler( boost::system::error_code const& ec
, socket_ptr pSock, FactoryFunc fact_func, ConnectHandler const& handler
, options_ptr opts, tcp::resolver::iterator resolver_it )
{
tcp_session_ptr ptr;
boost::system::error_code remote_ec, local_ec;
if (!ec && pSock->remote_endpoint(remote_ec) != pSock->local_endpoint(local_ec)
&& !remote_ec && !local_ec)
ptr = fact_func(pSock, session_initialized(session_id(), group_id(), opts));
else if (tcp::resolver::iterator() != resolver_it)
{
boost::system::error_code close_ec;
pSock->close(close_ec);
if (close_ec)
pSock.reset(new socket(m_ios));
tcp::endpoint endpoint = *resolver_it++;
pSock->async_connect(endpoint
, boost::bind(&tcp_connector::async_connect_handler, this, placeholders::error
, pSock, fact_func, handler, opts, resolver_it ));
return ;
}
handler(ptr, ec);
}
开发者ID:darcyg,项目名称:Bex,代码行数:26,代码来源:tcp_connector.cpp
示例5: disconnected
void Server::disconnected(socket_ptr & socket)
{
boost::lock_guard<boost::mutex> lock(sockets_mut_);
mark_container::iterator it = std::find(marks_.begin(), marks_.end(), socket->native_handle());
if (it == marks_.end())
marks_.push_back(socket->native_handle());
}
开发者ID:Surogate,项目名称:ai-playground,代码行数:8,代码来源:Server.cpp
示例6: disconnectClient
void disconnectClient(socket_ptr clientSock)
{
auto position = find(clientList->begin(), clientList->end(), clientSock);
clientSock->shutdown(tcp::socket::shutdown_both);
clientSock->close();
clientList->erase(position);
cout << "Client Disconnected! " << clientList->size() << " total clients" << endl;
}
开发者ID:LudwikJaniuk,项目名称:chat_test,代码行数:11,代码来源:server.cpp
示例7: handle_accept
void network_impl::handle_accept(socket_ptr socket)
{
tcp::endpoint remote_endpoint = socket->remote_endpoint();
log_debug() << "New incoming connection from "
<< remote_endpoint.address().to_string();
channel_handle chanid = create_channel(socket);
kernel_->handle_connect(chanid);
socket.reset(new tcp::socket(*service()));
acceptor_->async_accept(*socket,
std::bind(&network_impl::handle_accept, shared_from_this(),
socket));
}
开发者ID:arorts,项目名称:libbitcoin,代码行数:12,代码来源:network.cpp
示例8: listen_endpoint
void multicast_communication::market_data_receiver::connect_socket_( socket_ptr& socket, const common::address& to )
{
using boost::asio::ip::udp;
using boost::asio::ip::address;
using boost::asio::ip::multicast::join_group;
udp::endpoint listen_endpoint( address::from_string( "0.0.0.0" ), to.port() );
socket->open( listen_endpoint.protocol() );
socket->set_option( udp::socket::reuse_address( true ) );
socket->bind( listen_endpoint );
socket->set_option( join_group( address::from_string( to.ip() ) ) );
}
开发者ID:sidorovis,项目名称:cpp_craft_1013,代码行数:13,代码来源:market_data_receiver.cpp
示例9: inboundLoop
void Client::inboundLoop(socket_ptr sock, string_ptr prompt) {
int sizeBufRd = 0;
char buff[1024] = {0};
while( true){
if( sock->available()){
sizeBufRd = sock->read_some( buffer( buff, sizeInput));
string_ptr msg( new string( buff, sizeBufRd));
pMsgQueue->push( msg);
}
// boost::this_thread::sleep( boost::posix_time::millisec(100));
}
}
开发者ID:fengzhyuan,项目名称:INET,代码行数:14,代码来源:chatClient.cpp
示例10: on_overtime
// 超时回调
void on_overtime(error_code const& ec, socket_ptr sp, errc error_enum)
{
if (ec)
return ;
// 超时了
error_code lec;
sp->lowest_layer().cancel(lec);
sp->lowest_layer().shutdown(socket_base::shutdown_both, lec);
sp->lowest_layer().close(lec);
async_connecting_.reset();
ec_ = make_error_code(error_enum);
notify_onconnect();
}
开发者ID:linxuanwei,项目名称:Bex,代码行数:15,代码来源:client.hpp
示例11: writeLoop
void writeLoop(socket_ptr sock, string_ptr prompt)
{
char inputBuf[inputSize] = {0};
for(;;)
{
//cin.getline(inputBuf, inputSize);
//inputMsg = *prompt + (string)inputBuf + '\n';
mainMutex.lock();
if(!sendMessage.empty())
{
sendMessage = *prompt + sendMessage;
cout << sendMessage << sendMessage.empty() << endl;
//sock->write_some(buffer(inputMsg, inputSize));
sock->write_some(buffer(sendMessage, inputSize));
}
if(sendMessage.find("exit") != string::npos){
mainMutex.unlock();
exit(1);
}
sendMessage.clear();
memset(inputBuf,0,inputSize);
mainMutex.unlock();
boost::this_thread::sleep( boost::posix_time::millisec(500));
}
}
开发者ID:kasimsuzen,项目名称:CSE395-Project-1,代码行数:29,代码来源:client.cpp
示例12: session
/*****************************************************************************
* fiber function per server connection
*****************************************************************************/
void session( socket_ptr sock) {
try {
for (;;) {
char data[max_length];
boost::system::error_code ec;
std::size_t length = sock->async_read_some(
boost::asio::buffer( data),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
print( tag(), ": handled: ", std::string(data, length));
boost::asio::async_write(
* sock,
boost::asio::buffer( data, length),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
}
print( tag(), ": connection closed");
} catch ( std::exception const& ex) {
print( tag(), ": caught exception : ", ex.what());
}
}
开发者ID:danieljames,项目名称:fiber,代码行数:32,代码来源:autoecho.cpp
示例13: writeLoop
void writeLoop(socket_ptr sock, string_ptr prompt)
{
string inputMsg;
int i = 1;
for(;;)
{
inputMsg = *prompt;
mainMutex.lock();
if(!sendMessage.empty())
{
cerr << "Message sent:" << sendMessage <<":\n";
inputMsg += sendMessage;
sock->write_some(buffer(inputMsg, inputSize));
//sendMessage.clear();
if(i==10)
{
sendMessage.clear();
i=1;
}else
++i;
}
mainMutex.unlock();
if(inputMsg.find("exit") != string::npos)
return;
inputMsg.clear();
boost::this_thread::sleep( boost::posix_time::millisec(1000));
}
}
开发者ID:kasimsuzen,项目名称:CSE395-Project-1,代码行数:30,代码来源:network.cpp
示例14: session
void session(socket_ptr sock)
{
for (;;)
{
char data[max_length];
::memset(data, 0, max_length);
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
// boost::asio::write(*sock, boost::asio::buffer(data, length));
for (int i = 0; i < g_clinets.size(); ++i)
{
boost::asio::write(*g_clinets[i], boost::asio::buffer(data, length));
}
std::cout << "Client Data is :";
std::cout.write(data, length);
std::cout << "\n";
}
}
开发者ID:Kallru,项目名称:sdh,代码行数:26,代码来源:AsioServer.cpp
示例15: ReadLine
static int ReadLine(socket_ptr sock, std::string &tmp)
{
boost::system::error_code ec;
boost::asio::streambuf response;
try {
SOCKET native_sock = sock->native();
int result = SOCKET_ERROR;
if (INVALID_SOCKET != native_sock)
{
DWORD tv=2000;
result = setsockopt(native_sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv,sizeof(DWORD) );
// result should be zero
}
size_t len = boost::asio::read_until(*sock, response, "\n", ec);
if(len==0)
return -1;
if(ec)
throw boost::system::system_error(ec); //
if (ec == boost::asio::error::eof)
throw boost::system::system_error(ec); //
std::istream response_stream(&response);
getline(response_stream, tmp);
}
catch(std::exception & e)
{
std::cout<< "ReadLine Exception " << e.what() << "\n";
return -1;
}
return tmp.size();
}
开发者ID:johnmichaloski,项目名称:MTConnectToolbox,代码行数:34,代码来源:SocketBackEnd.cpp
示例16: session
void session( socket_ptr sock) {
try {
std::cout << "handler request" << std::endl;
for (;;) {
char data[max_length];
boost::system::error_code ec;
std::size_t length = sock->async_read_some(
boost::asio::buffer( data),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
boost::asio::async_write(
* sock,
boost::asio::buffer( data, length),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
}
} catch ( std::exception const& e) {
std::cerr << "Exception in fiber: " << e.what() << "\n";
}
}
开发者ID:flavioroth,项目名称:boost-fiber,代码行数:28,代码来源:echo_server_yield.cpp
示例17: session
void session( socket_ptr sock) {
boost::fibers::fiber::id id = boost::this_fiber::get_id();
std::cout << "fiber " << id << " : echo-handler started" << std::endl;
try {
for (;;) {
char data[max_length];
boost::system::error_code ec;
std::size_t length = sock->async_read_some(
boost::asio::buffer( data),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
std::cerr << "fiber " << id << " : error occured : " << ec.message() << std::endl;
return;
}
boost::asio::async_write(
* sock,
boost::asio::buffer( data, length),
boost::fibers::asio::yield[ec]);
if ( ec == boost::asio::error::eof) {
break; //connection closed cleanly by peer
} else if ( ec) {
throw boost::system::system_error( ec); //some other error
}
}
} catch ( boost::fibers::fiber_interrupted const&) {
std::cout << "fiber " << id << " : interrupted" << std::endl;
} catch ( std::exception const& ex) {
std::cout << "fiber " << id << " : catched exception : " << ex.what() << std::endl;
}
std::cout << "fiber " << id << " terminates" << std::endl;
}
开发者ID:tempbottle,项目名称:boost-fiber,代码行数:33,代码来源:echo_server.cpp
示例18: handleAccept
void StreamServer::handleAccept(socket_ptr socket)
{
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(socket->native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(socket->native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
logS(kLogNotice) << "StreamServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl;
shared_ptr<StreamSession> session = make_shared<StreamSession>(this, socket);
{
std::lock_guard<std::mutex> mlock(sessionsMutex_);
session->setBufferMs(settings_.bufferMs);
session->start();
sessions_.insert(session);
}
startAccept();
}
开发者ID:clonejo,项目名称:snapcast,代码行数:17,代码来源:streamServer.cpp
示例19: handle_accept
void Server::handle_accept(socket_ptr & socket, boost::system::error_code const & error)
{
if (!error)
{
boost::lock_guard<boost::mutex> lock(sockets_mut_);
std::clog << "[LOG] new connection from : " << socket->remote_endpoint().address().to_string() << std::endl;
sockets_[socket->native_handle()] = socket;
set_handle_read(socket, error);
if (!synchronize_.empty())
synchronize_(socket);
}
else
{
std::cerr << "Error : " << error.message() << std::endl;
}
start();
}
开发者ID:Surogate,项目名称:ai-playground,代码行数:17,代码来源:Server.cpp
示例20: inboundLoop
void inboundLoop(socket_ptr sock, string_ptr prompt)
{
int bytesRead = 0;
char readBuf[1024] = {0};
for(;;)
{
if(sock->available())
{
bytesRead = sock->read_some(buffer(readBuf, inputSize));
string_ptr msg(new string(readBuf, bytesRead));
messageQueue->push(msg);
}
boost::this_thread::sleep( boost::posix_time::millisec(1000));
}
}
开发者ID:kasimsuzen,项目名称:CSE395-Project-1,代码行数:18,代码来源:client.cpp
注:本文中的socket_ptr类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论