Samchon Framework for CPP  1.0.0
FlashPolicyServer.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <boost/asio.hpp>
5 #include <thread>
6 #include <samchon/library/XML.hpp>
7 
8 namespace samchon
9 {
10 namespace library
11 {
12  class XML;
13 };
14 namespace protocol
15 {
24  {
25  private:
29  std::shared_ptr<library::XML> policy;
30 
31  public:
39  (
40  std::make_shared<library::XML>
41  (
42  std::string() +
43  "<cross-domain-policy>\n" +
44  " <allow-access-from domain='*' to-ports='*' />\n" +
45  "</cross-domain-policy>"
46  )
47  )
48  {
49  };
50 
56  FlashPolicyServer(std::shared_ptr<library::XML> policy)
57  {
58  this->policy = policy;
59  };
60 
64  void open(int port = 843)
65  {
66  boost::asio::io_service ioService;
67  boost::asio::ip::tcp::endpoint endPoint(boost::asio::ip::tcp::v4(), port);
68  boost::asio::ip::tcp::acceptor acceptor(ioService, endPoint);
69 
70  while (true)
71  {
72  auto socket = std::make_shared<boost::asio::ip::tcp::socket>(ioService);
73  acceptor.accept(*socket);
74 
75  std::thread(&FlashPolicyServer::accept, this, socket).detach();
76  }
77  };
78 
82  auto getPolicy() const -> std::shared_ptr<library::XML>
83  {
84  return policy;
85  };
86 
87  void setPolicy(std::shared_ptr<library::XML> xml)
88  {
89  this->policy = xml;
90  };
91 
92  private:
98  void accept(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
99  {
100  std::string data = "<?xml version=\"1.0\"?>\n" + policy->toString();
101  std::vector<unsigned char> piece;
102  boost::system::error_code error;
103 
104  while (true)
105  {
106  piece.assign(1000, NULL);
107  socket->read_some(boost::asio::buffer(piece), error);
108 
109  boost::system::error_code error;
110  socket->write_some(boost::asio::buffer(data), error);
111 
112  break;
113  }
114  socket->close();
115  };
116  };
117 };
118 };
FlashPolicyServer(std::shared_ptr< library::XML > policy)
Construct from custom policy_.
std::shared_ptr< library::XML > policy
cross-domain-policy_
auto getPolicy() const -> std::shared_ptr< library::XML >
Get policy.
void open(int port=843)
Open server.
void accept(std::shared_ptr< boost::asio::ip::tcp::socket > socket)
Accept client.