2 #include <samchon/API.hpp> 4 #include <samchon/protocol/IProtocol.hpp> 5 #include <samchon/protocol/IListener.hpp> 11 #include <boost/asio.hpp> 12 #include <samchon/ByteArray.hpp> 44 std::shared_ptr<boost::asio::ip::tcp::socket> socket;
64 if (socket !=
nullptr && socket->is_open())
76 virtual void replyData(std::shared_ptr<Invoke> invoke)
79 if (i_listener !=
nullptr)
80 i_listener->_Reply_data(invoke);
92 virtual void sendData(std::shared_ptr<Invoke> invoke)
94 std::unique_lock<std::mutex> uk(send_mtx);
97 send_data(invoke->toXML()->toString());
100 for (
size_t i = 0; i < invoke->size(); i++)
101 if (invoke->at(i)->getType() ==
"ByteArray")
102 send_data(invoke->at(i)->referValue<
ByteArray>());
113 virtual void listen_message()
115 std::shared_ptr<Invoke> binary_invoke =
nullptr;
116 std::queue<std::shared_ptr<InvokeParameter>> binary_parameters;
123 size_t content_size = listen_size();
126 if (binary_invoke ==
nullptr)
128 std::shared_ptr<Invoke> invoke = listen_string(content_size);
130 for (
size_t i = 0; i < invoke->size(); i++)
132 std::shared_ptr<InvokeParameter> ¶meter = invoke->at(i);
133 if (parameter->getType() !=
"ByteArray")
136 if (binary_invoke ==
nullptr)
137 binary_invoke = invoke;
138 binary_parameters.push(parameter);
142 if (binary_invoke ==
nullptr)
147 std::shared_ptr<InvokeParameter> parameter = binary_parameters.front();
148 listen_binary(content_size, parameter);
149 binary_parameters.pop();
151 if (binary_parameters.empty() ==
true)
154 std::shared_ptr<Invoke> invoke = binary_invoke;
155 binary_invoke =
nullptr;
162 catch (std::exception &e)
164 std::cout <<
"Reason of disconnection: " << e.what() << std::endl;
175 auto listen_size() ->
size_t 177 std::array<unsigned char, 8> size_header;
178 listen_data(size_header);
181 for (
size_t c = 0; c < size_header.size(); c++)
182 size += size_header[c] << (8 * (size_header.size() - 1 - c));
187 auto listen_string(
size_t size) -> std::shared_ptr<Invoke>
190 std::string data(size, (
char)NULL);
194 std::shared_ptr<Invoke> invoke(
new Invoke());
195 invoke->construct(std::make_shared<library::XML>(data));
200 void listen_binary(
size_t size, std::shared_ptr<InvokeParameter> parameter)
204 data.assign(size, NULL);
210 template <
class Container>
211 void listen_data(Container &data)
213 size_t completed = 0;
215 while (completed < data.size())
217 size_t piece_size = socket->read_some(boost::asio::buffer((
unsigned char*)data.data() + completed, data.size() - completed));
218 completed += piece_size;
225 template <
class Container>
226 void send_data(
const Container &data)
231 socket->write_some(boost::asio::buffer(header));
232 socket->write_some(boost::asio::buffer(data));
virtual void replyData(std::shared_ptr< Invoke > invoke)
virtual void replyData(std::shared_ptr< Invoke >)=0
void writeReversely(const T &val)
Write a data.
Standard message of network I/O.
virtual void sendData(std::shared_ptr< Invoke > invoke)