~WSClientImpl() override { ws_.close({}); stream_.close(); work_ = boost::none; thread_.join(); }
inline connection::~connection() { socket_m.close(); read_thread_m.join(); send_thread_m.join(); }
//=========================================================================== void CHttpClient::DoConnect ( const boost::system::error_code & err, boost::asio::ip::tcp::resolver::iterator iter ) { if (err) { if (iter != boost::asio::ip::tcp::resolver::iterator()) { m_socket.close(); boost::asio::ip::tcp::endpoint endPoint = *iter; auto handler = boost::bind( &CHttpClient::DoConnect, this, boost::asio::placeholders::error, ++iter ); m_socket.async_connect(endPoint, handler); } else { printf("%s\n", __FUNCTION__); cout<<"Error: "<<err.message()<<endl; } return; } auto handler = boost::bind( &CHttpClient::DoWriteRequest, this, boost::asio::placeholders::error ); boost::asio::async_write(m_socket, m_request, handler); }
int Run() { // ----------- Thread_TcpCommClient_Recv ----------- int iParam1 = 0; pthread_t pthread_TcpCommClient_Recv; if(pthread_create(&pthread_TcpCommClient_Recv, NULL, Thread_TcpCommClient_Recv, (void*)this) != 0) { printf("Failed:Thread_TcpCommClient_Recv\n"); } else { printf("Initiate:Thread_TcpCommClient_Recv\n"); usleep(100000); } // ----------- Thread_TcpCommClient_Send ----------- int iParam2 = 0; pthread_t pthread_TcpCommClient_Send; if(pthread_create(&pthread_TcpCommClient_Send, NULL, Thread_TcpCommClient_Send, (void*)this) != 0) { printf("Failed:Thread_TcpCommClient_Send\n"); } else { printf("Initiate:Thread_TcpCommClient_Send\n"); usleep(100000); } while( ros::ok() ) { ros::spinOnce(); usleep(30000); } if( m_Socket.is_open() ) { m_Socket.close(); } return 0; }
void doReadHeader(){ boost::asio::async_read(_socket, boost::asio::buffer(_data, sizeof(MktDataGlobalHeaderMsg) + sizeof(char)), [this](boost::system::error_code ec, std::size_t length) { if (!ec) { MktDataGlobalHeaderMsg* header = new MktDataGlobalHeaderMsg; memcpy((void*)header, (void*)(_data), sizeof(MktDataGlobalHeaderMsg)); char* msgType = new char; memcpy((void*)msgType, (void*)(_data + sizeof(MktDataGlobalHeaderMsg)), sizeof(char)); std::cout << " Packet#: " << ++pcount << std::endl; doReadBody(header, msgType); } else { std::cout << "Exception comes - Socket Closed. " << std::endl; _socket.close(); } }); }
virtual void disconnect() { try { socket_.close(); } catch (std::exception& e) { } }
void close_socket() { BOOST_CHECK_NO_THROW( socket_.close() ); BOOST_CHECK_NO_THROW( acceptor_.close() ); }
~Client() { if (mSocket.is_open()) mSocket.close(); }
void doReadBody(MktDataGlobalHeaderMsg* header, char* msgType){ switch(*msgType){ case 'N': case 'M': case 'X': case 'G': case 'H': case 'J': { boost::asio::async_read(_socket, boost::asio::buffer(_data, sizeof(MktDataOrderMsg)), [this, &header, &msgType](boost::system::error_code ec, std::size_t length) { if(!ec){ MktDataOrderMsg* body = new MktDataOrderMsg; memcpy((void*)body, (void*)_data, sizeof(MktDataOrderMsg)); Order::SharedPtr order(new SimpleOrder(*header, *msgType, *body)); { std::string str; try{ LatencyChecker<> lc(str); _orderBookManager.addOrder(order); }catch(const std::runtime_error& err){ std::cout << " Exception " << err.what() << std::endl; } order->toString(str); _orderBookManager.printOrderBookForSymbol(str, body->_toeknNo); _logger.log(std::move(str)); } delete body; doReadHeader(); }else{ std::cout << "Exception comes - Socket Closed. " << std::endl; _socket.close(); } }); } break; case 'T': case 'K': { boost::asio::async_read(_socket, boost::asio::buffer(_data, sizeof(MktDataTradeMsg)), [this, &header, &msgType](boost::system::error_code ec, std::size_t length) { if(!ec){ MktDataTradeMsg* body = new MktDataTradeMsg; memcpy((void*)body, (void*)_data, sizeof(MktDataTradeMsg)); Trade::SharedPtr trade(new SimpleTrade(*header, *msgType, *body)); { std::string str; try{ LatencyChecker<> lc(str); _orderBookManager.addTrade(trade); }catch(const std::runtime_error& err){ std::cout << " Exception " << err.what() << std::endl; } trade->toString(str); _orderBookManager.printOrderBookForSymbol(str, body->_toeknNo); _logger.log(std::move(str)); } delete body; doReadHeader(); }else{ std::cout << "Exception comes - Socket Closed. " << std::endl; _socket.close(); } }); } break; default: std::cout << " Error: Unknown Packet comes.... " << std::endl; break; } }
void Server::refuseAccept(boost::asio::ip::tcp::socket socket) { socket.close(); }