/** * Performs validation, masking, compression, etc. will return an error if * there was an error, otherwise msg will be ready to be written */ virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out) { if (!in || !out) { return make_error_code(error::invalid_arguments); } // TODO: check if the message is prepared already // validate opcode if (in->get_opcode() != frame::opcode::text) { return make_error_code(error::invalid_opcode); } std::string& i = in->get_raw_payload(); //std::string& o = out->get_raw_payload(); // validate payload utf8 if (!utf8_validator::validate(i)) { return make_error_code(error::invalid_payload); } // generate header out->set_header(std::string(reinterpret_cast<char const *>(&msg_hdr),1)); // process payload out->set_payload(i); out->append_payload(std::string(reinterpret_cast<char const *>(&msg_ftr),1)); // hybi00 doesn't support compression // hybi00 doesn't have masking out->set_prepared(true); return lib::error_code(); }
void WebClient::onMessage(Client* c, websocketpp::connection_hdl hdl, message_ptr pMsg) { Client::connection_ptr con = c->get_con_from_hdl(hdl); std::string res_s = con->get_resource(); if (res_s != "/codenjoy-contest/ws?user="******"WebClient::onMessage(...) Server answer is not right: " + res_s; throw std::runtime_error(res_s.c_str()); } std::string buffer_got = pMsg->get_payload(); String boardString; #ifdef _WIN32 boardString.resize(MultiByteToWideChar(CP_UTF8, 0, &buffer_got[0], buffer_got.length(), NULL, 0)); MultiByteToWideChar(CP_UTF8, 0, &buffer_got[0], buffer_got.length(), &boardString[0], boardString.capacity()); #else //Assume linux boardString = buffer_got; #endif if (boardString.substr(0, 6) == LL("board=")) { boardString = boardString.substr(6, boardString.length() - 6); while (*boardString.rbegin() == LL('\0')) { boardString.pop_back(); } Board b(boardString); String answer = solver->get(b); std::string utf_answer; #ifdef _WIN32 utf_answer.resize(WideCharToMultiByte(CP_UTF8, 0, &answer[0], answer.length(), NULL, 0, NULL, NULL)); WideCharToMultiByte(CP_UTF8, 0, &answer[0], answer.length(), &utf_answer[0], utf_answer.capacity(), NULL, NULL); if (utf_answer == "") { // This happens if bomberman's still dead if (answer != LL("")) { throw std::runtime_error("WebClient::onMessage(...): Conversion from Char to utf8 error!"); } return; } #else // Assume linux utf_answer = answer; #endif pMsg->set_opcode(websocketpp::frame::opcode::text); pMsg->set_payload(utf_answer); std::cout << "Sending ACTION: " << pMsg->get_payload() << std::endl; websocketpp::lib::error_code err; c->send(hdl, pMsg->get_payload(), pMsg->get_opcode(), err); } }
lib::error_code prepare_close(close::status::value code, std::string const & reason, message_ptr out) const { if (!out) { return lib::error_code(error::invalid_arguments); } std::string val; val.append(1,'\xff'); val.append(1,'\x00'); out->set_payload(val); out->set_prepared(true); return lib::error_code(); }