removed code i have written learning boost::asio.

This commit is contained in:
Kjistóf 2016-12-03 19:37:47 +01:00
parent f6fb183649
commit e03a3e9aa3
5 changed files with 0 additions and 188 deletions

View File

@ -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<char>(is), std::istreambuf_iterator<char>());
if (ec)
throw std::runtime_error("Networking error: " + ec.message());
process_data(data);
}
message::~message() {}

View File

@ -1,20 +0,0 @@
#pragma once
#include <boost/asio.hpp>
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();
};

View File

@ -1,69 +0,0 @@
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>
#include <ctime>
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<char> alldata;
std::array<char, 4096> 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<io_service::work>(ioservice);
ioservice.run();
return EXIT_SUCCESS;
}

View File

@ -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() {}

View File

@ -1,26 +0,0 @@
#pragma once
#include "message.h"
#include <string>
#include <iterator>
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;
};