bool PipeConnection::_createPipes() { std::stringstream pipeName; pipeName << "\\\\.\\pipe\\Collage." << UUID( true ); ConnectionDescriptionPtr desc = new ConnectionDescription; desc->type = CONNECTIONTYPE_NAMEDPIPE; desc->setFilename( pipeName.str( )); ConnectionPtr connection = Connection::create( desc ); _namedPipe = static_cast< NamedPipeConnection* >( connection.get( )); if( !_namedPipe->listen( )) return false; _namedPipe->acceptNB(); connection = Connection::create( desc ); _sibling->_namedPipe = static_cast<NamedPipeConnection*>(connection.get()); if( !_sibling->_namedPipe->connect( )) { _sibling->_namedPipe = 0; return false; } connection = _namedPipe->acceptSync(); _namedPipe = static_cast< NamedPipeConnection* >(connection.get( )); return true; }
WebSocket::UserConnectionPtr Server::UserConnection(ConnectionPtr connection) { if (!connection.get()) return WebSocket::UserConnectionPtr(); for(UserConnectionList::Iterator iter = connections_.Begin(); iter != connections_.End(); ++iter) if ((*iter)->WebSocketConnection().get() == connection.get()) return (*iter); return WebSocket::UserConnectionPtr(); }
/** * @brief 查询MySql里的数据 * @param query 查询语句 * @return 返回一行或一列数据 */ const std::vector<std::string> MySqlConnectionPool::selectData(std::string& query) { std::vector<std::string> resVector; ConnectionPtr connPtr = this->connection(); MYSQL_RES *res; MYSQL_ROW row; if (!connPtr.get()) { std::cout << "selectData connPtr is null" << std::endl; } if (::mysql_ping(&*connPtr)) { std::cout << "mysql ping is error" << std::endl; return resVector; } if (::mysql_real_query(&*connPtr, query.c_str(), (unsigned int)strlen(query.c_str()))) { std::cout << ::mysql_error(&*connPtr) << std::endl; this->releaseConnection(connPtr); return resVector; } res = ::mysql_store_result(&*connPtr); row = ::mysql_fetch_row(res); while (row) { for (int i = 0; i < ::mysql_num_fields(res); ++i) { resVector.push_back(std::string(row[i])); } row = ::mysql_fetch_row(res); } ::mysql_free_result(res); this->releaseConnection(connPtr); return resVector; }
/** * @brief 释放MySql连接 * @param connPtr 指定需释放的连接 */ void MySqlConnectionPool::releaseConnection(ConnectionPtr connPtr) { boost::mutex::scoped_lock lock(_mutex); if (connPtr.get()) { _connContainer.push_back(connPtr); } }
/** * @brief 执行一条sql语句 * @param query 查询语句 * @return 返回是否执行成功 */ const bool MySqlConnectionPool::exec(const std::string& query) { ConnectionPtr connPtr = this->connection(); if (!connPtr.get()) { std::cout << "exec connPtr is null" << std::endl; return false; } if (::mysql_ping(&*connPtr)) { std::cout << "mysql ping is error" << std::endl; return false; } if (::mysql_real_query(&*connPtr, query.c_str(), (unsigned int)strlen(query.c_str()))) { std::cout << ::mysql_error(&*connPtr) << std::endl; this->releaseConnection(connPtr); return false; } this->releaseConnection(connPtr); return true; }
VrpnTrackerRos::VrpnTrackerRos(std::string tracker_name, ConnectionPtr connection, ros::NodeHandle nh) { tracker_remote_ = std::make_shared<vrpn_Tracker_Remote>(tracker_name.c_str(), connection.get()); std::string clean_name = tracker_name; if (clean_name.size() > 0) { int start_subsequent = 1; if (isInvalidFirstCharInName(clean_name[0])) { clean_name = clean_name.substr(1); start_subsequent = 0; } clean_name.erase( std::remove_if( clean_name.begin() + start_subsequent, clean_name.end(), isInvalidSubsequentCharInName ), clean_name.end() ); } init(clean_name, nh, false); }
bool ConnectionSet::_setupFDSet() { // if( !_impl->dirty ) // { //#ifndef _WIN32 // // TODO: verify that poll() really modifies _fdSet, and remove the copy // // if it doesn't. The man page seems to hint that poll changes fds. // _impl->fdSet = _impl->fdSetCopy; //#endif // return true; // } _impl->dirty = false; _impl->fdSet.setSize( 0 ); _impl->fdSetResult.setSize( 0 ); #ifdef _WIN32 // add self connection HANDLE readHandle = _impl->selfConnection->getNotifier(); LBASSERT( readHandle ); _impl->fdSet.append( readHandle ); Result res; res.connection = _impl->selfConnection.get(); _impl->fdSetResult.append( res ); // add regular connections _impl->lock.set(); for( ConnectionsCIter i = _impl->connections.begin(); i != _impl->connections.end(); ++i ) { ConnectionPtr connection = *i; if ( connection->isRead() ) continue; readHandle = connection->getNotifier(); if( !readHandle ) { LBINFO << "Cannot select connection " << connection << ", connection does not provide a read handle" << std::endl; _impl->connection = connection; _impl->lock.unset(); return false; } _impl->fdSet.append( readHandle ); Result result; result.connection = connection.get(); _impl->fdSetResult.append( result ); } for( ThreadsCIter i=_impl->threads.begin(); i != _impl->threads.end(); ++i ) { Thread* thread = *i; readHandle = thread->notifier; LBASSERT( readHandle ); _impl->fdSet.append( readHandle ); Result result; result.thread = thread; _impl->fdSetResult.append( result ); } _impl->lock.unset(); #else // _WIN32 pollfd fd; fd.events = POLLIN; // | POLLPRI; // add self 'connection' fd.fd = _impl->selfConnection->getNotifier(); LBASSERT( fd.fd > 0 ); fd.revents = 0; _impl->fdSet.append( fd ); Result result; result.connection = _impl->selfConnection.get(); _impl->fdSetResult.append( result ); // add regular connections _impl->lock.set(); for( ConnectionsCIter i = _impl->allConnections.begin(); i != _impl->allConnections.end(); ++i ) { ConnectionPtr connection = *i; if ( connection->isRead() ) continue; fd.fd = connection->getNotifier(); if( fd.fd <= 0 ) { LBINFO << "Cannot select connection " << connection << ", connection " << typeid( *connection.get( )).name() << " doesn't have a file descriptor" << std::endl; _impl->connection = connection; _impl->lock.unset(); return false; } LBVERB << "Listening on " << typeid( *connection.get( )).name() << " @" << (void*)connection.get() << std::endl; fd.revents = 0; _impl->fdSet.append( fd ); result.connection = connection.get(); _impl->fdSetResult.append( result ); } _impl->lock.unset(); _impl->fdSetCopy = _impl->fdSet; #endif return true; }