void send_all(const T& value) { MessagePtr msg = std::make_shared<Message>(sizeof(T)); msg->set_body_length(sizeof(T)); std::memcpy(msg->body(), &value, sizeof(T)); msg->encode_header(); send_all(msg); }
void send(int conn_idx, const T& value) { // hack // MessagePtr msg = std::make_shared<Message>(sizeof(T)); MessagePtr msg = scene_write_msg; msg->set_body_length(sizeof(T)); std::memcpy(msg->body(), &value, sizeof(T)); msg->encode_header(); send(conn_idx, msg); }
void Client::write(MessagePtr msg) { if (!mRunning) return; // encodes message before sending msg->encode(); // append the message boost::lock_guard<boost::mutex> lock(mWMutex); mWriteQ.push_back(msg); boost::lock_guard<boost::mutex> slock(mSMutex); mSent.push_back(std::string(msg->body())); // notify of new message mWCond.notify_one(); }
void Client::onReadBody(MessagePtr msg, const error_code& error) { // make sure everything was successful if (!error && msg->decode()) { // list of found messages bool found = false; boost::unique_lock<boost::mutex> slock(mSMutex); for (auto iter = mSent.begin(); iter != mSent.end() && !found; ++iter) { if (!strcmp(iter->c_str(), msg->body())) { found = true; mSent.erase(iter); } } // prints the body if (!found) { printf("%s\n", msg->body()); if(0==strncmp(msg->body(),"/banksy",7)) { printf("%s\n", " .s$$$Ss."); printf("%s\n", " .8, $$$. _. . ..sS$$$$$' ...,.;"); printf("%s\n", "o. ,@.. 88 =.$'$' ' ..sS$$$$$$$$$$$$s. _;''"); printf("%s\n", " @@@.@@@. .88. ` ` ""l. .sS$$.._.sS$$$$$$$$$$$$S'''"); printf("%s\n", " .@@@q@@.8888o. .s$$$$$$$$$$$$$$$$$$$$$'"); printf("%s\n", " .:`@@@@33333. .>$$$$$$$$$$$$$$$$$$$$'"); printf("%s\n", " .: `@@@@333' ..>$$$$$$$$$$$$$$$$$$$'"); printf("%s\n", " : `@@333. `., s$$$$$$$$$$$$$$$$$'"); printf("%s\n", " : `@33 $$ S.s$$$$$$$$$$$$$$$$$'"); printf("%s\n", " .S `Y ..` ,'$' `$$$$$$$$$$$$$$"); printf("%s\n", " $s . ..S$s, . .`$$$$$$$$$$$$."); printf("%s\n", " $s ., ,s ,$$$$,,sS$s.$$$$$$$$$$$$$."); printf("%s\n", " / /$$SsS.s. ..s$$$$$$$$$$$$$$$$$$$$$$$$$."); printf("%s\n", " /`.`$$$$$dN.ssS$$'`$$$$$$$$$$$$$$$$$$$$$$$."); printf("%s\n", " / `$$$$$$$$$' `$$$$$$$$$$$$$$$$$$$$$$."); printf("%s\n", " / | `S$$S$' `$$$$$$$$$$$$$$$$$$$$$$."); printf("%s\n", " / / $$$$$$$$$$$$$$$$$$$$$."); printf("%s\n", " `$$$$$$$$$$$$$$$$$$$$$s."); printf("%s\n", " $$$'' .?T$$$$$$$"); printf("%s\n", " .$' ... ?$$# "); printf("%s\n", " ! -=S$$$$$s"); printf("%s\n", " .! -=s$$' `$=-_ :"); printf("%s\n", " , .$$$' `$, .|"); printf("%s\n", " , .$$$' . ,"); printf("%s\n", " , ..$$$'"); printf("%s\n", " .s$$$' `s ."); printf("%s\n", " . .s$$$$' $s. ..$s"); printf("%s\n", " . .s$$$$' `$s=s$$$"); printf("%s\n", " .$$$$' , $$s"); } } // start listening for the next message listen(); } else // error { printf("Error reading body\n"); kill(); } }