ed_key c_netuser::get_public_key_resp(ip::tcp::socket &socket_) { DBG_MTX(dbg_mtx, "START"); assert(socket_.is_open()); boost::system::error_code ec; char header[2]; DBG_MTX(dbg_mtx, "read header"); size_t pkresp = socket_.read_some(buffer(header, 2), ec); DBG_MTX(dbg_mtx, "pk: " << pkresp << ":[" <<header[0] << header[1] << "]"); DBG_MTX(dbg_mtx, "read public key size"); size_t pub_key_size = 4; uint32_t key_size = 0; size_t recieved_bytes = socket_.read_some(buffer(&key_size, pub_key_size), ec); DBG_MTX(dbg_mtx, "size:" << recieved_bytes << ":[" <<key_size << "]"); assert(recieved_bytes == pub_key_size); const std::unique_ptr<unsigned char[]> pub_key_data(new unsigned char[key_size]); DBG_MTX(dbg_mtx, "read public key data"); recieved_bytes = socket_.read_some(buffer(pub_key_data.get(), key_size), ec); DBG_MTX(dbg_mtx, "data:" << recieved_bytes << ":[" << ed_key(pub_key_data.get(),recieved_bytes) << "]"); assert(recieved_bytes == key_size); ed_key pub_key(pub_key_data.get(), key_size); DBG_MTX(dbg_mtx, "END"); return pub_key; }
~asio_connection() { #if !defined(_WIN32_WINNT) || (_WIN32_WINNT >= 0x0600) socket_.cancel(); #endif //socket_.shutdown(socket_base::shutdown_both); socket_.close(); }
void c_netuser::send_public_key_req(ip::tcp::socket &socket_) { DBG_MTX(dbg_mtx, "send public key request"); assert(socket_.is_open()); boost::system::error_code ec; char pubkey_send_req[2] = {'p','k'}; size_t sendbytes = socket_.write_some(boost::asio::buffer(pubkey_send_req, request_type_size),ec); DBG_MTX(dbg_mtx, "pk: " << sendbytes << ":[" << pubkey_send_req[0] << pubkey_send_req[1] << "]"); }
void c_netuser::send_coin(ip::tcp::socket &socket_, const std::string &coin_data) { DBG_MTX(dbg_mtx, "START"); assert(socket_.is_open()); boost::system::error_code ec; char header[2] = {'$', 't'}; socket_.write_some(buffer(header, 2), ec); uint32_t coin_data_size = coin_data.size(); socket_.write_some(buffer(&coin_data_size, 4), ec); socket_.write_some(buffer(coin_data), ec); }
virtual void close() { if (!closed) closed = 1; if (clientSocket->is_open()) { clientSocket->cancel(); clientSocket->close(); } if (serverSocket.is_open()) { serverSocket.cancel(); serverSocket.close(); } }
inline void cia_client::do_write(boost::shared_ptr<chat_message> ch_msg) { //BOOST_LOG_SEV(cia_g_logger, Debug) << "当前m_chat_msg_queue队列剩余元素:" << m_chat_msg_queue.Size(); ptr self = shared_from_this(); //BOOST_LOG_SEV(cia_g_logger, Debug) << "异步发送的数据为:" << std::endl; //<< ch_msg->m_procbuffer_msg.DebugString(); //BOOST_LOG_SEV(cia_g_logger, Debug) << "rn id:" << ch_msg->m_procbuffer_msg.transid(); if (!ch_msg->parse_to_cia_msg()) { BOOST_LOG_SEV(cia_g_logger, Debug) << "解析待发送的报文出错"; //m_chat_msg_queue.Put(ch_msg); return; } m_sock_.async_send(boost::asio::buffer(ch_msg->data(), ch_msg->length()), [this, self, ch_msg](boost::system::error_code ec, std::size_t /*length*/){ if (ec){ BOOST_LOG_SEV(cia_g_logger, Debug) << "异步发送数据回调函数,检测到异常, 关闭此客户端socket, 错误码" << ec; stop(); } else { //BOOST_LOG_SEV(cia_g_logger, Debug) << "已成功异步发送数据, 数据的transid:" << ch_msg->m_procbuffer_msg.transid(); } //m_chat_msg_queue.Put(ch_msg); }); }
void set_socket_recv_bufsize(int buffer_size) { boost::asio::socket_base::receive_buffer_size recv_bufsize_option(buffer_size); socket_.set_option(recv_bufsize_option); //std::cout << "set_socket_recv_buffer_size(): " << buffer_size << " bytes" << std::endl; }
void do_write_some() { //auto self(this->shared_from_this()); socket_.async_write_some(boost::asio::buffer(data_, packet_size_), [this](const boost::system::error_code & ec, std::size_t bytes_written) { if (!ec) { // If get a circle of ping-pong, we count the query one time. do_query_counter(); if ((uint32_t)bytes_written != packet_size_) { std::cout << "asio_connection::do_write_some(): async_write(), bytes_written = " << bytes_written << " bytes." << std::endl; } do_read_some(); } else { // Write error log std::cout << "asio_connection::do_write_some() - Error: (code = " << ec.value() << ") " << ec.message().c_str() << std::endl; stop(true); } } ); }
void Connect(const std::string& host, uint16 port) { using namespace std::placeholders; _socket.async_connect(ip::tcp::endpoint(ip::address::from_string(host), port) , std::bind(&Impl::OnConnected, this, _1)); }
future<void> session(ip::tcp::socket s, size_t block_size) { vector<char> buf(block_size); s.set_option(ip::tcp::no_delay(true)); for (;;) { auto n = co_await async_read_some(s, buffer(buf.data(), block_size)); n = co_await async_write(s, buffer(buf.data(), n)); } }
int get_socket_recv_bufsize() const { boost::asio::socket_base::receive_buffer_size recv_bufsize_option; socket_.get_option(recv_bufsize_option); //std::cout << "receive_buffer_size: " << recv_bufsize_option.value() << " bytes" << std::endl; return recv_bufsize_option.value(); }
// Constructor Connection(std::string server_url) : io_service_(), socket(io_service_) { tcp::resolver resolver(io_service_); tcp::resolver::query query(server_url, "4444"); // EDIT PORT NUMBER HERE tcp::resolver::iterator iterator = resolver.resolve(query); socket.connect(*iterator); }
string c_netuser::recv_coin(ip::tcp::socket &socket_) { DBG_MTX(dbg_mtx, "START"); assert(socket_.is_open()); boost::system::error_code ec; size_t recieved_bytes = 0; uint32_t coin_size = 0; recieved_bytes = socket_.read_some(buffer(&coin_size, 4), ec); DBG_MTX(dbg_mtx, "get " << recieved_bytes << " bytes"); assert(recieved_bytes == 4); const std::unique_ptr<char[]> coin_data(new char[coin_size]); recieved_bytes = socket_.read_some(buffer(coin_data.get(), coin_size), ec); assert(recieved_bytes == coin_size); return std::string(coin_data.get(), coin_size); }
void c_netuser::send_public_key_resp(ip::tcp::socket &socket_) { DBG_MTX(dbg_mtx, "START"); assert(socket_.is_open()); boost::system::error_code ec; char header[2] = {'p', 'k'}; DBG_MTX(dbg_mtx, "send header"); socket_.write_some(buffer(header, 2), ec); //uint32_t packet_size = ed25519_sizes::pub_key_size; ed_key packet = get_public_key(); uint32_t packet_size = packet.size(); // TODO send binary data DBG_MTX(dbg_mtx,"send public key size" << "[" << packet_size << "]"); socket_.write_some(boost::asio::buffer(&packet_size, 4), ec); DBG_MTX(dbg_mtx,"send public key data" << "[" << packet << "]"); socket_.write_some(boost::asio::buffer(packet.c_str(), packet_size), ec); DBG_MTX(dbg_mtx,"end of sending public key"); }
void c_netuser::server_read(ip::tcp::socket socket_) { DBG_MTX(dbg_mtx, "START"); assert(socket_.is_open()); boost::system::error_code ec; DBG_MTX(dbg_mtx,"server read"); while (!ec && !m_stop_flag) { char header[2] = {0, 0}; socket_.read_some(buffer(header, 2), ec); if (header[0] == 'p' && header[1] == 'k') { send_public_key_resp(socket_); } else if (header[0] == '$' && header[1] == 't') { std::string coin_data = recv_coin(socket_); recieve_from_packet(coin_data); } } socket_.close(); }
void cia_client::stop() { if (!m_started_) return; m_started_ = false; m_sock_.close(); m_deal_ch_msg_group.interrupt_all(); ptr self = shared_from_this(); auto it = std::find(clients.begin(), clients.end(), self); clients.erase(it); BOOST_LOG_SEV(cia_g_logger, Debug) << "客户端socket已经调用stop函数关闭"; //x m_config_server->set_started(true); // 为了防止网络情况异常, 造成服务端关闭连接后重置此值为2, 通讯端保证此值为1 }
channel::channel(ip::tcp::socket& input, ip::tcp::socket& output, session& parent_session, const time_duration& input_timeout, bool first_input_stat) : input(input) , output(output) , input_timer(input.io_service()) , input_timeout(input_timeout) , pipe_size(0) , parent_session(parent_session) , input_handler(boost::bind(&channel::finished_waiting_input, this, placeholders::error(), placeholders::bytes_transferred())) , output_handler(boost::bind(&channel::finished_waiting_output,this, placeholders::error(), placeholders::bytes_transferred())) , current_state(created) , first_input(first_input_stat) { if (pipe2(pipe, O_NONBLOCK) == -1) { perror("pipe2"); throw boost::system::errc::make_error_code(static_cast<boost::system::errc::errc_t> (errno)); } }
void do_read_some() { socket_.async_read_some(boost::asio::buffer(data_, packet_size_), [this](const boost::system::error_code & ec, std::size_t received_bytes) { if ((uint32_t)received_bytes != packet_size_) { std::cout << "asio_connection::do_read_some(): async_read(), received_bytes = " << received_bytes << " bytes." << std::endl; } if (!ec) { // A successful request, can be used to statistic qps do_write_some(); } else { // Write error log std::cout << "asio_connection::do_read_some() - Error: (code = " << ec.value() << ") " << ec.message().c_str() << std::endl; stop(true); } } ); }
void do_write(ip::tcp::socket & sock, char * buff, size_t size) { if ( !started() ) return; sock.async_write_some(buffer(buff,size), MEM_FN3(on_write,ref(sock),_1,_2)); }
// Destructor ~Connection() { socket.close(); }
size_t read_complete(ip::tcp::socket & sock, const error_code & err, size_t bytes) { if ( sock.available() > 0) return sock.available(); return bytes > 0 ? 0 : 1; }
virtual void shuttle() { serverSocket.async_connect(destination, boost::bind(&RawBridge::handleConnect, this, Bridge::getSmartPointer(), placeholders::error)); }