#include <asio.hpp>
#include <functional>
#include <iostream>
#include <vector>
#include "connection.hpp"
#include <boost/serialization/vector.hpp>
#include "stock.hpp"
namespace s11n_example {
class server
{
public:
server(asio::io_context& io_context, unsigned short port)
: acceptor_(io_context,
asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))
{
stock s;
s.code = "ABC";
s.name = "A Big Company";
s.open_price = 4.56;
s.high_price = 5.12;
s.low_price = 4.33;
s.last_price = 4.98;
s.buy_price = 4.96;
s.buy_quantity = 1000;
s.sell_price = 4.99;
s.sell_quantity = 2000;
stocks_.push_back(s);
s.code = "DEF";
s.name = "Developer Entertainment Firm";
s.open_price = 20.24;
s.high_price = 22.88;
s.low_price = 19.50;
s.last_price = 19.76;
s.buy_price = 19.72;
s.buy_quantity = 34000;
s.sell_price = 19.85;
s.sell_quantity = 45000;
stocks_.push_back(s);
connection_ptr new_conn(new connection(acceptor_.get_executor()));
acceptor_.async_accept(new_conn->socket(),
std::bind(&server::handle_accept, this,
asio::placeholders::error, new_conn));
}
void handle_accept(const std::error_code& e, connection_ptr conn)
{
if (!e)
{
conn->async_write(stocks_,
std::bind(&server::handle_write, this,
asio::placeholders::error, conn));
}
connection_ptr new_conn(new connection(acceptor_.get_executor()));
acceptor_.async_accept(new_conn->socket(),
std::bind(&server::handle_accept, this,
asio::placeholders::error, new_conn));
}
void handle_write(const std::error_code& e, connection_ptr conn)
{
}
private:
asio::ip::tcp::acceptor acceptor_;
std::vector<stock> stocks_;
};
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: server <port>" << std::endl;
return 1;
}
unsigned short port = std::stoi(argv[1]);
asio::io_context io_context;
s11n_example::server server(io_context, port);
io_context.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}