From 40de4ad32bf8d6b285903628dfc1bcfd0c1a708b Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 4 Aug 2015 08:49:06 +0300 Subject: server: bits of log client-qt: implemented basic message dispatcher ability to send message (all untested) --- client-qt/udm-client-qt/client_session.cpp | 102 ++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 3 deletions(-) (limited to 'client-qt/udm-client-qt/client_session.cpp') diff --git a/client-qt/udm-client-qt/client_session.cpp b/client-qt/udm-client-qt/client_session.cpp index 56eb88d..60f462c 100644 --- a/client-qt/udm-client-qt/client_session.cpp +++ b/client-qt/udm-client-qt/client_session.cpp @@ -23,6 +23,31 @@ #include #include +std::shared_ptr pack_data(const std::string &buf, int *size_) +{ + BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__; + std::shared_ptr ptr = std::make_shared(new char[buf.length() + 4]); + int32_t size = buf.length(); + *size_ = size + 4; + (*ptr)[3] = size & 0xff; + (*ptr)[2] = (size>>8) & 0xff; + (*ptr)[1] = (size>>16) & 0xff; + (*ptr)[0] = (size>>24) & 0xff; + char *fck = (*ptr)+4; + memcpy(fck, buf.data(), buf.length()); + return ptr; +} + +std::shared_ptr pack_msg(client_msg *msg, int *size_) +{ + BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__; + BOOST_LOG_TRIVIAL(trace)<<"packing message:\n"<DebugString(); + std::string msg_buf; + msg->SerializeToString(&msg_buf); + return pack_data(msg_buf, size_); +} + + client_session::client_session(QObject *parent, boost::asio::io_service *io_service) : QObject(parent), io_service_(*io_service), socket_(*io_service) { @@ -41,21 +66,92 @@ void client_session::client_connect(QString host, int port) boost::asio::ip::tcp::endpoint ep; ep.port(port); ep.address(boost::asio::ip::address::from_string(host.toStdString())); - socket_.async_connect(ep, boost::bind(&client_session::connect_handler, this, boost::asio::placeholders::error)); + socket_.async_connect(ep, boost::bind(&client_session::handle_connect, this, boost::asio::placeholders::error)); } -void client_session::connect_handler(const boost::system::error_code &e) +void client_session::handle_connect(const boost::system::error_code &e) { BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"< ptr = pack_msg(&msg, &size); + boost::asio::async_write(socket_, boost::asio::buffer(*ptr, size), boost::bind(&client_session::handle_write, this, boost::asio::placeholders::error)); +} + +void client_session::handle_write(const boost::system::error_code& error) +{ + if(error) + { + //TODO: handle error + } + } void client_session::run_io_service() -- cgit v1.2.3