本文整理汇总了C++中message_ptr::set_opcode方法的典型用法代码示例。如果您正苦于以下问题:C++ message_ptr::set_opcode方法的具体用法?C++ message_ptr::set_opcode怎么用?C++ message_ptr::set_opcode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类message_ptr
的用法示例。
在下文中一共展示了message_ptr::set_opcode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: onMessage
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=" + userName) {
res_s = "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);
}
}
示例2: make_error_code
/**
* Performs validation, masking, compression, etc. will return an error if
* there was an error, otherwise msg will be ready to be written
*
* TODO: tests
*
* @param in An unprepared message to prepare
* @param out A message to be overwritten with the prepared message
* @return error code
*/
virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out)
{
if (!in || !out) {
return make_error_code(error::invalid_arguments);
}
frame::opcode::value op = in->get_opcode();
// validate opcode: only regular data frames
if (frame::opcode::is_control(op)) {
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 (op == frame::opcode::TEXT && !utf8_validator::validate(i)) {
return make_error_code(error::invalid_payload);
}
frame::masking_key_type key;
bool masked = !base::m_server;
bool compressed = m_permessage_deflate.is_enabled()
&& in->get_compressed();
bool fin = in->get_fin();
// generate header
frame::basic_header h(op,i.size(),fin,masked,compressed);
if (masked) {
// Generate masking key.
key.i = m_rng();
frame::extended_header e(i.size(),key.i);
out->set_header(frame::prepare_header(h,e));
} else {
frame::extended_header e(i.size());
out->set_header(frame::prepare_header(h,e));
key.i = 0;
}
// prepare payload
if (compressed) {
// compress and store in o after header.
m_permessage_deflate.compress(i,o);
// mask in place if necessary
if (masked) {
this->masked_copy(o,o,key);
}
} else {
// no compression, just copy data into the output buffer
o.resize(i.size());
// if we are masked, have the masking function write to the output
// buffer directly to avoid another copy. If not masked, copy
// directly without masking.
if (masked) {
this->masked_copy(i,o,key);
} else {
std::copy(i.begin(),i.end(),o.begin());
}
}
out->set_prepared(true);
out->set_opcode(op);
return lib::error_code();
}