From e03a3e9aa31cd3d34738473520583455915644c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sat, 3 Dec 2016 19:37:47 +0100 Subject: [PATCH] removed code i have written learning boost::asio. --- message.cpp | 41 ---------------------------- message.h | 20 -------------- server.cpp | 69 ------------------------------------------------ text_message.cpp | 32 ---------------------- text_message.h | 26 ------------------ 5 files changed, 188 deletions(-) delete mode 100644 message.cpp delete mode 100644 message.h delete mode 100644 server.cpp delete mode 100644 text_message.cpp delete mode 100644 text_message.h diff --git a/message.cpp b/message.cpp deleted file mode 100644 index d3ab141..0000000 --- a/message.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "message.h" - -using namespace boost; - - - -void message::send(boost::asio::ip::tcp::socket& socket) -{ - system::error_code ec; - - asio::write(socket, asio::buffer(get_message_length() + get_message()), ec); - - if (ec) - throw std::runtime_error("Networking error: " + ec.message()); -} - -void message::receive(boost::asio::ip::tcp::socket& socket) -{ - system::error_code ec; - asio::streambuf sb; - std::istream is(&sb); - std::string data; - - asio::read(socket, sb, asio::transfer_exactly(sizeof(size_t)), ec); - is >> data; - size_t len = std::stoull(data); - - asio::read(socket, sb, asio::transfer_exactly(len), ec); - data.clear(); - data.reserve(len); - is.clear(); - data = std::string(std::istreambuf_iterator(is), std::istreambuf_iterator()); - - if (ec) - throw std::runtime_error("Networking error: " + ec.message()); - - process_data(data); -} - - -message::~message() {} \ No newline at end of file diff --git a/message.h b/message.h deleted file mode 100644 index 0a6bef5..0000000 --- a/message.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - - - -class message -{ -protected: - virtual std::string get_message_length() = 0; - virtual std::string get_message() = 0; - virtual void process_data(std::string&) = 0; - -public: - - void send(boost::asio::ip::tcp::socket&); - void receive(boost::asio::ip::tcp::socket&); - - virtual ~message(); -}; diff --git a/server.cpp b/server.cpp deleted file mode 100644 index 849a604..0000000 --- a/server.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -using namespace boost::asio; -using namespace boost::asio::ip; - - - -io_service ioservice; -tcp::endpoint tcpendpoint{tcp::v4(), 2222}; -tcp::acceptor tcpacceptor{ioservice, tcpendpoint}; -tcp::socket sock{ioservice}; -std::vector alldata; -std::array data; - - -void read_handler(const boost::system::error_code& ec, std::size_t transferred) -{ - if (!ec) - { - std::cout.write(data.data(), transferred); - for (int i = 0; i < transferred; ++i) - alldata.push_back(data[i]); - sock.async_read_some(buffer(data), read_handler); - } -} - -void write_handler(const boost::system::error_code& ec, std::size_t transferred) -{ - if (!ec) - { - tcp::endpoint remoteendpoint = sock.remote_endpoint(); - address ip = remoteendpoint.address(); - - std::cout << transferred - << " bytes transferred to " - << ip.to_string() << ":" << remoteendpoint.port() - << std::endl; - - sock.async_read_some(buffer(data), read_handler); - } -} - -void accept_handler(const boost::system::error_code& ec) -{ - if (!ec) - { - std::time_t now = std::time(nullptr); - std::string time(std::ctime(&now)); - async_write(sock, buffer(time), write_handler); - } -} - - -int main() -{ - tcpacceptor.listen(); - tcpacceptor.async_accept(sock, accept_handler); - - // work, so the server does not stop listening - auto work = std::make_shared(ioservice); - ioservice.run(); - - return EXIT_SUCCESS; -} \ No newline at end of file diff --git a/text_message.cpp b/text_message.cpp deleted file mode 100644 index 0f0c8a8..0000000 --- a/text_message.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "text_message.h" - -using namespace boost; - - - -text_message::text_message() {} - - -text_message::text_message(std::string text):_text(text) {} - - -std::string text_message::get_text() -{ return _text; } - - -std::string text_message::get_message_length() -{ - auto sizetext = std::to_string(_text.size()); - return std::string(sizeof(size_t) - sizetext.size(), '0').append(sizetext); -} - - -std::string text_message::get_message() -{ return _text; } - - -void text_message::process_data(std::string& string) -{ _text = string; } - - -text_message::~text_message() {} diff --git a/text_message.h b/text_message.h deleted file mode 100644 index bf98fe5..0000000 --- a/text_message.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "message.h" -#include -#include - - - -class text_message : public message -{ -private: - std::string _text; - -protected: - virtual std::string get_message_length() override; - virtual std::string get_message() override; - virtual void process_data(std::string&) override; - -public: - text_message(); - text_message(std::string); - - std::string get_text(); - - virtual ~text_message() override; -};