void AcceptSession::OnRecvBody(SocketPtr socket_ptr, const boost::system::error_code& error) { if (!error) { m_network_mgr->OnRecv(socket_ptr); if (socket_ptr->GetSocket().is_open()) { socket_ptr->ChangeBufferSize(MESSAGE_HEADER_LENGTH); boost::asio::async_read(socket_ptr->GetSocket(), boost::asio::buffer(socket_ptr->GetBuffer(), MESSAGE_HEADER_LENGTH), boost::bind(&AcceptSession::OnRecvHead, this, socket_ptr, boost::asio::placeholders::error)); } } else { if (error == boost::asio::error::eof) { std::stringstream ss; ss << "AcceptSession::OnRecvBody Error : " << error.message(); DebugMessage::GetInstance()->ShowMessage(DebugMessageType::BASE_NETWORK, ss.str()); } if (NULL != m_network_mgr && socket_ptr->GetSocket().is_open()) m_network_mgr->OnDisconnect(socket_ptr); } }
bool Downloader::downloadSoap(const std::string& guid) { try { SocketPtr sock = connect("www.photosynth.net"); //prepare header + content std::stringstream content; content << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"<<std::endl; content << "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"<<std::endl; content << " <soap12:Body>"<<std::endl; content << " <GetCollectionData xmlns=\"http://labs.live.com/\">"<<std::endl; content << " <collectionId>"<<guid<<"</collectionId>"<<std::endl; content << " <incrementEmbedCount>false</incrementEmbedCount>"<<std::endl; content << " </GetCollectionData>"<<std::endl; content << " </soap12:Body>"<<std::endl; content << "</soap12:Envelope>"<<std::endl; std::stringstream header; header << "POST /photosynthws/PhotosynthService.asmx HTTP/1.1"<<std::endl; header << "Host: photosynth.net"<<std::endl; header << "Content-Type: application/soap+xml; charset=utf-8"<<std::endl; header << "Content-Length: "<<content.str().size()<<std::endl; header << ""<<std::endl; //send header + content boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(header.str().c_str()), boost::asio::transfer_all(), error); boost::asio::write(*sock, boost::asio::buffer(content.str().c_str()), boost::asio::transfer_all(), error); sock->shutdown(tcp::socket::shutdown_send); //read the response boost::asio::streambuf response; while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error)); sock->close(); std::istream response_stream(&response); std::string line; //skip header from response while (std::getline(response_stream, line) && line != "\r"); //put soap response into xml std::stringstream xml; while (std::getline(response_stream, line)) xml << line << std::endl; //save soap response if (!saveAsciiFile(Parser::createFilePath(guid, Parser::soapFilename), xml.str())) return false; } catch (std::exception& e) { std::cerr << e.what() << std::endl; return false; } return true; }
void Run() { std::cout << "StatusServer: START " << std::endl; active_ = true; gnuradar::StatusMessage status_msg; while( active_ ) { status_msg.set_name("status"); status_msg.set_head(pcModel_->Head() ); status_msg.set_tail(pcModel_->Tail() ); status_msg.set_depth( pcModel_->Depth()); status_msg.set_over_flow(pcModel_->OverFlow() ); status_msg.set_bytes_per_buffer( pcModel_->BytesPerBuffer() ); std::string data; status_msg.SerializeToString(&data); zmq::message_t zmq_msg(data.size()); memcpy ((void *) zmq_msg.data(), data.c_str(), data.size()); socket_->send (zmq_msg); Sleep(thread::MSEC, gnuradar::constants::STATUS_REFRESH_RATE_MSEC); } std::cout << "StatusServer: STOP " << std::endl; socket_->close(); }
void TelnetAppender::SocketHandler::run() { while(!done) { try { SocketPtr newClient = serverSocket.accept(); SocketOutputStreamPtr os = newClient->getOutputStream(); if(connections.size() < MAX_CONNECTIONS) { connections.push_back(newClient); writers.push_back(os); StringBuffer oss; oss << _T("TelnetAppender v1.0 (") << connections.size() << _T(" active connections)\r\n\r\n"); print(os, oss.str()); os->flush(); } else { print(os, _T("Too many connections.\r\n")); os->flush(); newClient->close(); } } catch(Exception& e) { LogLog::error(_T("Encountered error while in SocketHandler loop."), e); } } }
void Downloader::downloadAllThumbFiles(const std::string& guid, const JsonInfo& info) { for (unsigned int i=0; i<info.thumbs.size(); ++i) { char buf[10]; sprintf(buf, "%08d", i); std::stringstream filename; filename << "thumbs/" << buf << ".jpg"; std::string filepath = Parser::createFilePath(guid, filename.str()); if (!bf::exists(filepath)) { std::string host = extractHost(info.thumbs[i].url); SocketPtr sock = connect(host); std::string header = createGETHeader(removeHost(info.thumbs[i].url, host), host); //send GET request boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(header.c_str()), boost::asio::transfer_all(), error); //read the response (header + content) boost::asio::streambuf response; while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error)); std::istream response_stream(&response); int length = getContentLength(response_stream); saveBinFile(filepath, response_stream, length); sock->close(); } } }
/////////////////////////////////////////////////////////////////////// // Starts Request Server in Thread. /////////////////////////////////////////////////////////////////////// void Run() { active_ = true; std::string reply_msg; std::string data; gnuradar::ControlMessage request_msg; gnuradar::ResponseMessage response_msg; zmq::message_t request; while( active_ ) { // Wait for next request from client. socket_->recv (&request); // Parse message from wire. request_msg.ParseFromString( zmq_helper::FormatString(request) ); // Execute client request response_msg = this->ExecuteRequest( request_msg ); // Send reply this->SendResponse( response_msg ); } std::cout << "CLOSING REQUEST SERVER SOCKET" << std::endl; socket_->close(); }
void TileDownloader::downloadPictureTile(unsigned int pictureIndex, unsigned int tileIndex) { TileInfo& tileInfo = mPictures[pictureIndex].tiles[tileIndex]; std::string url = tileInfo.url; std::string filepath = tileInfo.filePath; std::string host = DownloadHelper::extractHost(url); SocketPtr sock = DownloadHelper::connect(mService, host); std::string header = DownloadHelper::createGETHeader(DownloadHelper::removeHost(url, host), host); //send GET request boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(header.c_str()), boost::asio::transfer_all(), error); //read the response (header + content) boost::asio::streambuf response; while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error)); std::istream response_stream(&response); int length = DownloadHelper::getContentLength(response_stream); tileInfo.size = (unsigned int) length; DownloadHelper::saveBinFile(filepath, response_stream, length); sock->close(); }
void NetworkManager::AcceptNewConnection() { NetworkThread& worker = get_network_thread_for_new_connection(); SocketPtr connection = CreateSocket(worker); acceptor_->async_accept(connection->socket(), boost::bind(&NetworkManager::OnNewConnection, this, connection, boost::asio::placeholders::error)); }
void send_data( const SocketPtr &in_socket, const std::string &in_msg ) { if( in_socket->get_is_connected() ) { MemInfo info( in_msg.c_str(), in_msg.length() + 1 ); in_socket->send_data( info ); } }
void OnAccept(const AsyncResultPtr &asyncResult) { try { SocketPtr listenSocket = asyncResult->asyncState; SocketPtr acceptSocket = listenSocket->EndAccept(asyncResult); acceptSocket->BeginReceive(BufferPtr(8192, NoRebindTag()), &OnReceive, acceptSocket); listenSocket->BeginAccept(&OnAccept, listenSocket); } catch (const std::exception &) { } }
int main() { try { SocketPtr listenSocket; listenSocket->Bind(7777); listenSocket->Listen(); listenSocket->BeginAccept(&OnAccept, listenSocket); Sleep(INFINITE); } catch (const std::exception &) { } }
void Downloader::downloadAllBinFiles(const std::string& guid, Parser* parser) { std::string host = extractHost(parser->getSoapInfo().collectionRoot); SocketPtr sock = connect(host); boost::asio::socket_base::keep_alive keepAlive(true); sock->set_option(keepAlive); std::stringstream headers; std::vector<std::string> filenames; int nbCoorSystem = parser->getNbCoordSystem(); for (unsigned int i=0; i<parser->getNbCoordSystem(); ++i) { int nbPointCloud = parser->getNbPointCloud(i); for (unsigned int j=0; j<parser->getNbPointCloud(i); ++j) { std::stringstream filename; filename << "bin/points_"<< i << "_" << j << ".bin"; if (!bf::exists(Parser::createFilePath(guid, filename.str()))) { filenames.push_back(filename.str()); std::stringstream url; url << parser->getSoapInfo().collectionRoot << filename.str().substr(4); headers << createGETHeader(removeHost(url.str(), host), host); } } } if (filenames.size() > 0) { //send GET request boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(headers.str().c_str()), boost::asio::transfer_all(), error); //read the response (header + content) boost::asio::streambuf response; while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error)); //read all response header and content std::istream response_stream(&response); for (unsigned int i=0; i<filenames.size(); ++i) { int length = getContentLength(response_stream); saveBinFile(Parser::createFilePath(guid, filenames[i]), response_stream, length); } } sock->close(); }
void SendResponse( gnuradar::ResponseMessage& msg ){ std::string serial_msg; msg.SerializeToString(&serial_msg); zmq::message_t reply (serial_msg.size()); memcpy ((void *) reply.data (), serial_msg.c_str(), serial_msg.size()); socket_->send (reply); }
void OnReceive(const AsyncResultPtr &asyncResult) { try { SocketPtr acceptSocket = asyncResult->asyncState; BufferPtr buffer = asyncResult->buffer; size_t size = acceptSocket->EndReceive(asyncResult); if (size != 0) { buffer->Resize(size); acceptSocket->BeginSend(buffer); acceptSocket->BeginReceive(BufferPtr(8192, NoRebindTag()), &OnReceive, acceptSocket); } else { acceptSocket->BeginDisconnect(); } } catch (const std::exception &) { } }
//----------------------------------------------------------------------- String string(const SocketPtr & x) { if (!x) return String(); auto socket = x->getSocket(); if (INVALID_SOCKET == socket) return "INVALID_SOCKET"; return string(((PTRNUMBER)(socket))); }
bool NetworkManager::OnSocketOpen(const SocketPtr& socket) { NetworkThread& thread = socket->owner(); thread.AddSocket(socket); return true; }
StatusServer( zmq::context_t& ctx, const std::string& ipAddr, PcModelPtr pcModel ) : pcModel_(pcModel) { socket_ = SocketPtr( new zmq::socket_t(ctx, ZMQ_PUB)); //socket_->setsockopt( ZMQ_HWM, &gnuradar::constants::HWM, sizeof(gnuradar::constants::HWM)); socket_->bind (ipAddr.c_str()); std::cout << "STATUS ADDRESS : " << ipAddr << std::endl; }
void operator()(u_long size, u_long error) { // ¸´ÖÆListen socketÊôÐÔ UpdateAcceptContext context(acceptor_); remoteSocket_->SetOption(context); handler_(/*size, */error, std::tr1::cref(remoteSocket_)); }
void Poll::addFD(SocketPtr sock, PollHandler handler) { static struct epoll_event ev; { std::unique_lock<std::mutex> lock(mutex_); handlers_[sock->native()] = std::make_tuple(sock, handler); } ev.events = /*EPOLLET |*/ EPOLLIN | EPOLLOUT; ev.data.fd = sock->native(); int ret = epoll_ctl(fd_, EPOLL_CTL_ADD, sock->native(), &ev); if (ret != 0) { THROW_SYSTEM_ERROR(); } }
//------------------------------------------------------------------------- void SocketSet::delegateGone(SocketPtr socket) { ZS_THROW_BAD_STATE_IF(mPollingSocketsWithDelegateGoneCount >= mPollingCount) // can never grow larger than the polling count mPollingSocketsWithDelegateGone[mPollingSocketsWithDelegateGoneCount] = socket; ++mPollingSocketsWithDelegateGoneCount; ZS_LOG_TRACE(log("delegate gone") + ZS_PARAM("handle", socket->getSocket()) + ZS_PARAM("gone count", mPollingSocketsWithDelegateGoneCount)) }
void NetworkManager::OnNewConnection(SocketPtr connection, const boost::system::error_code& error) { if (error) { sLog.outError("Error accepting new client connection!"); return; } if (!connection->Open()) { sLog.outError("Unable to start new client connection!"); connection->CloseSocket(); return; } AcceptNewConnection(); }
void SocketProvider::GetExtensionFunctionPtr(const SocketPtr &socket, GUID *guid, LPVOID pfn) { BufferPtr inBuffer(sizeof(GUID), NoRebindTag()); BufferPtr outBuffer(sizeof(void *), NoRebindTag()); RtlMoveMemory(inBuffer->Pointer(), guid, sizeof(GUID)); socket->IOControl(SIO_GET_EXTENSION_FUNCTION_POINTER, inBuffer, outBuffer); RtlMoveMemory(pfn, outBuffer->Pointer(), sizeof(void *)); }
void DataServer::on_client_created( const SocketPtr &in_client ) { in_client->signal_data_received().connect( boost::bind( std::mem_fn(&DataServer::on_data_received), this, _1, _2)); m_server->signal_client_created().connect( boost::bind( std::mem_fn( &DataServer::on_client_disconnect), this, _1 )); }
UDPListener::UDPListener(int port, const std::string& ip) : acceptNewConnections(false) { SocketPtr socket; if (UDPListener::TryBindSocket(port, &socket, ip)) { boost::asio::socket_base::non_blocking_io socketCommand(true); socket->io_control(socketCommand); mySocket = socket; SetAcceptingConnections(true); } if (IsAcceptingConnections()) { LOG("[UDPListener] successfully bound socket on port %i", port); } else { handleerror(NULL, "[UDPListener] error: unable to bind UDP port, see log for details.", "Network error", MBF_OK | MBF_EXCL); } }
bool WorldSocketMgr::OnSocketOpen( const SocketPtr& sock ) { // set some options here if (m_SockOutKBuff >= 0 && !sock->SetSendBufferSize( m_SockOutKBuff )) { sLog.outError("WorldSocketMgr::OnSocketOpen set_option SO_SNDBUF"); return false; } // Set TCP_NODELAY. if (m_UseNoDelay && !sock->EnableTCPNoDelay( m_UseNoDelay )) { sLog.outError("WorldSocketMgr::OnSocketOpen: peer().set_option TCP_NODELAY errno = %s", ACE_OS::strerror(errno)); return false; } sock->SetOutgoingBufferSize( static_cast<size_t>(m_SockOutUBuff) ); return NetworkManager::OnSocketOpen( sock ); }
SocketPtr Downloader::connect(const std::string& url) { tcp::resolver resolver(*(mService)); tcp::resolver::query query(url, "http"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::resolver::iterator end; SocketPtr socket = SocketPtr(new tcp::socket(*(mService))); boost::system::error_code error = boost::asio::error::host_not_found; while (error && endpoint_iterator != end) { socket->close(); socket->connect(*endpoint_iterator++, error); } if (error) throw boost::system::system_error(error); return socket; }
void AcceptSession::OnAccept(SocketPtr socket_ptr, const boost::system::error_code& error) { if (!error) { m_network_mgr->OnAccept(socket_ptr); //socket_ptr->ChangeBufferSize(MESSAGE_HEADER_LENGTH); socket_ptr->ChangeBufferSize(MESSAGE_HEADER_LENGTH); boost::asio::async_read(socket_ptr->GetSocket(), boost::asio::buffer(socket_ptr->GetBuffer(), MESSAGE_HEADER_LENGTH), boost::bind(&AcceptSession::OnRecvHead, this, socket_ptr, boost::asio::placeholders::error)); this->AsyncListen(); } else { std::stringstream ss; ss << "AcceptSession::OnAccept Error : " << error.message(); DebugMessage::GetInstance()->ShowMessage(DebugMessageType::BASE_NETWORK, ss.str()); } }
void AcceptSession::OnRecvHead(SocketPtr socket_ptr, const boost::system::error_code& error) { if (!error) { char *read_buff = socket_ptr->GetBuffer(); MessageHeader body_length = *(MessageHeader*)(read_buff); socket_ptr->ChangeBufferSize(body_length); boost::asio::async_read(socket_ptr->GetSocket(), boost::asio::buffer(socket_ptr->GetBuffer(), body_length), boost::bind(&AcceptSession::OnRecvBody, this, socket_ptr, boost::asio::placeholders::error)); } else { std::stringstream ss; ss << "AcceptSession::OnRecvHead Error : " << error.message(); DebugMessage::GetInstance()->ShowMessage(DebugMessageType::BASE_NETWORK, ss.str()); if (NULL != m_network_mgr && socket_ptr->GetSocket().is_open()) m_network_mgr->OnDisconnect(socket_ptr); } }
bool TileDownloader::downloadCollection(const std::string& collectionFilePath, const std::string& collectionUrl) { try { std::string host = DownloadHelper::extractHost(collectionUrl); SocketPtr sock = DownloadHelper::connect(mService, host); std::string header = DownloadHelper::createGETHeader(DownloadHelper::removeHost(collectionUrl, host), host); //send GET request boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(header.c_str()), boost::asio::transfer_all(), error); //read the response (header + content) boost::asio::streambuf response; while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error)); sock->close(); //skip header from response std::istream response_stream(&response); std::string line; while (std::getline(response_stream, line) && line != "\r"); //put collection into collectionText std::stringstream collectionText; while (std::getline(response_stream, line)) collectionText << line << std::endl; //save collection if (!DownloadHelper::saveAsciiFile(collectionFilePath, collectionText.str())) return false; } catch (std::exception& e) { std::cout << e.what() << std::endl; return false; } return true; }
bool Downloader::downloadJson(const std::string& guid, const std::string& jsonUrl) { try { std::string host = extractHost(jsonUrl); SocketPtr sock = connect(host); std::string header = createGETHeader(removeHost(jsonUrl, host), host); //send GET request boost::system::error_code error; boost::asio::write(*sock, boost::asio::buffer(header.c_str()), boost::asio::transfer_all(), error); //read the response (header + content) boost::asio::streambuf response; while (boost::asio::read(*sock, response, boost::asio::transfer_at_least(1), error)); sock->close(); //skip header from response std::istream response_stream(&response); std::string line; while (std::getline(response_stream, line) && line != "\r"); //put json into jsonText std::stringstream jsonText; while (std::getline(response_stream, line)) jsonText << line << std::endl; //save json if (!saveAsciiFile(Parser::createFilePath(guid, Parser::jsonFilename), jsonText.str())) return false; } catch (std::exception& e) { std::cerr << e.what() << std::endl; return false; } return true; }