1 #include <samchon/protocol/IClient.hpp> 7 #include <boost/asio.hpp> 8 #include <samchon/ByteArray.hpp> 9 #include <samchon/WeakString.hpp> 11 #include <samchon/protocol/Invoke.hpp> 12 #include <samchon/library/XML.hpp> 25 auto IClient::BUFFER_SIZE() const ->
size_t {
return 10000; }
33 sendMtx =
new mutex();
37 if (socket !=
nullptr)
45 void IClient::listen()
49 std::shared_ptr<Invoke> ba_invoke(
nullptr);
54 boost::system::error_code error;
56 piece.assign(BUFFER_SIZE(), NULL);
57 size_t size = socket->read_some(boost::asio::buffer(piece), error);
62 if(ba_invoke ==
nullptr)
63 handleString(piece, str, ba_invoke, size);
65 handleBinary(piece, str, ba_invoke, size);
68 void IClient::handleString(
ByteArray &piece,
string &str, shared_ptr<Invoke> &baInvoke,
size_t size)
70 static size_t CLOSED_PARENTHESIS = string(
"</invoke>").size();
76 list<shared_ptr<Invoke>> invokeList;
79 string &pieceString = piece.
read<
string>();
80 str.append(pieceString);
83 vector<WeakString> &wstrArray = wstr.
betweens(
"<invoke ",
"</invoke>");
85 for (
size_t i = 0; i < wstrArray.size(); i++)
87 string &message =
"<invoke " + wstrArray[i].str() +
"</invoke>";
89 shared_ptr<Invoke> invoke(
new Invoke() );
90 invoke->construct(make_shared<XML>(message));
92 invokeList.push_back(invoke);
208 if (invokeList.empty() ==
true)
215 str = move(str.substr(str.rfind(
"</invoke>") + CLOSED_PARENTHESIS));
218 auto last_it = --invokeList.end();
219 for (
auto it = invokeList.begin(); it != last_it; it++)
223 shared_ptr<Invoke> &lastInvoke = *last_it;
224 for (
size_t i = 0; i < lastInvoke->size(); i++)
227 if (lastInvoke->at(i)->getType() ==
"ByteArray")
229 baInvoke = lastInvoke;
234 (pieceString.size() - (pieceString.rfind(
"</invoke>") + CLOSED_PARENTHESIS))
238 handleBinary(piece, str, baInvoke, size);
244 _replyData(lastInvoke);
246 void IClient::handleBinary(
ByteArray &piece,
string &str, shared_ptr<Invoke> &invoke,
size_t size)
253 size_t param_index = 0;
256 for (
size_t i = 0; i < invoke->size(); i++)
257 if (invoke->at(i)->getType() ==
"ByteArray")
261 if (ba.size() < ba.capacity())
271 if(byteArray ==
nullptr)
275 size_t totalSize = byteArray->capacity();
276 size_t leftSize = totalSize - byteArray->size();
277 size_t writeSize = std::min(size - position, leftSize);
283 piece.begin() + position, piece.begin() + (position + writeSize)
288 if (byteArray->size() < byteArray->capacity())
292 for(
size_t i = param_index + 1; i < invoke->size(); i++)
293 if(invoke->at(i)->getType() ==
"ByteArray")
303 handleString(piece, str, invoke, size);
309 void IClient::sendData(shared_ptr<Invoke> invoke)
311 string &data = invoke->toXML()->toString();
312 boost::system::error_code error;
314 unique_lock<mutex> uk(*sendMtx);
315 socket->write_some(boost::asio::buffer(data), error);
319 cout << error.message() << endl;
323 for (
size_t i = 0; i < invoke->size(); i++)
324 if (invoke->at(i)->getType() ==
"ByteArray")
327 socket->write_some(boost::asio::buffer(byteArray), error);
333 void IClient::_replyData(shared_ptr<Invoke> invoke)
auto getPosition() const -> size_t
Get position.
auto read() const -> T
Read data.
Package of network protocol and libraries.
Standard message of network I/O.
void setPosition(size_t)
Set poisition.
auto betweens(const WeakString &start={}, const WeakString &end={}) const -> std::vector< WeakString >
Generates substrings.
Top level namespace of products built from samchon.
A string class only references characeters, reference only.