diff options
-rw-r--r-- | server/include/server.h | 65 | ||||
-rw-r--r-- | server/src/server.cpp | 153 |
2 files changed, 218 insertions, 0 deletions
diff --git a/server/include/server.h b/server/include/server.h new file mode 100644 index 0000000..e672bc3 --- /dev/null +++ b/server/include/server.h @@ -0,0 +1,65 @@ +/* + Copyright © 2015 Gluzskiy Alexandr (sss) + + This file is part of Unknown Download Manager (UDM). + + UDM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + UDM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with UDM. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#ifndef SERVER_H +#define SERVER_H + +#include <boost/asio.hpp> + +class client_msg; +class server_msg; + +class server_session +{ + public: + server_session(boost::asio::io_service &s); + void run(); + void send_message(server_msg *msg); + boost::asio::ip::tcp::socket& socket(); + virtual ~server_session(); + protected: + private: + void handle_read(const boost::system::error_code& error, size_t bytes_transferred); + void handle_write(const boost::system::error_code& error); + void handle_write_no_read(const boost::system::error_code& error); + void handle_command(client_msg *msg); + + const int buf_size = 512; + char *recv_data_; + boost::asio::ip::tcp::socket socket_; +}; + +class server +{ +public: + server(boost::asio::io_service& io_service, short port); + +private: + void start_accept(); + void handle_accept(server_session* new_session, const boost::system::error_code& error); + + boost::asio::io_service& io_service_; + boost::asio::ip::tcp::acceptor acceptor_; +}; + + + +#endif // SERVER_H diff --git a/server/src/server.cpp b/server/src/server.cpp new file mode 100644 index 0000000..7f2b20d --- /dev/null +++ b/server/src/server.cpp @@ -0,0 +1,153 @@ +/* + Copyright © 2015 Gluzskiy Alexandr (sss) + + This file is part of Unknown Download Manager (UDM). + + UDM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + UDM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with UDM. If not, see <http://www.gnu.org/licenses/>. + +*/ + + + +#include <boost/bind.hpp> +#include "server.h" +#include "utilities.h" + +#include "../../protocol/udm.pb.h" + +server_session::server_session(boost::asio::io_service &s) : socket_(s) +{ +} +boost::asio::ip::tcp::socket& server_session::socket() +{ + return socket_; +} + + +void server_session::run() +{ + recv_data_ = new char[4]; + boost::asio::async_read(socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&server_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + +} + +void server_session::handle_read(const boost::system::error_code& error, size_t bytes_transferred) +{ + if (!error) + { + std::string s; +// bool parsed = false; + unsigned size = ntohl(*(int32_t*)recv_data_); + delete [] recv_data_; + char *buf = new char[size]; + boost::system::error_code ec; + socket_.read_some(boost::asio::buffer(buf, size), ec); + if(ec) + { + + } + //TODO: check for error + s.append(buf, size); + delete [] buf; + if(size != s.length()) + { + delete this; + return; + } + //TODO: + client_msg msg; + if(msg.ParseFromString(s)) + { + //parsed = true; + handle_command(&msg); + recv_data_ = new char[4]; + boost::asio::async_read(socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&server_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + } +// if(!parsed) + else + { +// BOOST_LOG_TRIVIAL(error)<<"failed to parse client message"; + delete this; //close connection + return; + } + } + else + { + delete this; + } +} + +void server_session::handle_command(client_msg *msg) +{ + switch(msg->type()) + { + default: + break; + } +} + +void server_session::send_message(server_msg *msg) +{ + int size = 0; + std::shared_ptr<char*> ptr = pack_msg(msg, &size); + boost::asio::async_write(socket_, boost::asio::buffer(*ptr, size), boost::bind(&server_session::handle_write, this, boost::asio::placeholders::error)); +} + +void server_session::handle_write(const boost::system::error_code& error) +{ + if(!error) + { + //TODO: + } + else + { + //TODO: handle error + } +// recv_data_ = new char[4]; +// boost::asio::async_read(socket_, boost::asio::buffer(recv_data_, 4), boost::bind(&server_session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); +} + + +server_session::~server_session() +{ + //dtor +} + + +server::server(boost::asio::io_service& io_service, short port) : io_service_(io_service), acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) +{ + start_accept(); +} + +void server::start_accept() +{ + server_session* new_session = new server_session(io_service_); + acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error)); +} + +void server::handle_accept(server_session* new_session, const boost::system::error_code& error) +{ + if (!error) + { + new_session->run(); + } + else + { + delete new_session; + } + start_accept(); +} + + + |