std::size_t Message::read(const ConstBuffer& buf) { return read(buf.str()); // refactor }
std::size_t Packet::read(const ConstBuffer& buf) { DebugN(this) << "Read raw packet: " << buf.str() << endl; assert(buf.size() > 0); // Reset all data _frame = Frame::Unknown; _type = Type::Unknown; _id = -1; _nsp = "/"; _message = ""; _size = -1; BitReader reader(buf); // look up frame type char frame[2] = {'\0'}; reader.get(frame, 1); _frame = static_cast<Packet::Frame>(atoi(frame));//alternative: std::stoi(std::string(frame, 1)) DebugN(this) << "Read raw packet: atoi(&frame): " << atoi(frame) << endl; if (_frame == Packet::Frame::Message) { // look up packet type char type[2] = {'\0'}; //; // reader.get(type, 1); _type = static_cast<Packet::Type>(atoi(type));//std::stoi(std::string(type, 1)) // if (_type < TypeMin || _type > TypeMax) { // WarnN(this) << "Invalid message type: " << _type << endl; // return false; // } // TraceN(this) << "Parse type: " << type << ": " << typeString() << endl; } // look up attachments if type binary (not implemented) // look up namespace (if any) if (reader.peek() == '/') { reader.readToNext(_nsp, ','); } // look up id if (reader.available() && isdigit(reader.peek())) { char next; std::string id; reader.get(&next, 1); while (reader.available() && isdigit(next)) { id += next; reader.get(&next, 1); } _id = util::strtoi<int>(id); } // look up json data // TODO: Take into account joined messages if (reader.available()) { std::string temp; reader.get(temp, reader.available()); json::Value json; json::Reader reader; if (reader.parse(temp, json)) { if (json.isArray()) { if (json.size() < 2) { _event = "message"; _message = json::stringify(json[0], true); } else { assert(json[0].isString()); _event = json[0].asString(); _message = json::stringify(json[1], true); } } else if (json.isObject()) { _message = json::stringify(json, true); } } } _size = reader.position(); // DebugN(this) << "Parse success: " << toString() << endl; return _size; }
ssize_t Packet::read(const ConstBuffer& buf) { LTrace("Read raw packet: ", buf.str()) assert(buf.size() > 0); // Reset all data _frame = Frame::Unknown; _type = Type::Unknown; _id = -1; _nsp = "/"; _message = ""; _size = 0; BitReader reader(buf); // parse frame type char frame[2] = {'\0'}; reader.get(frame, 1); _frame = static_cast<Packet::Frame>(atoi(frame)); // std::stoi(std::string(frame, 1)) if (_frame == Packet::Frame::Message) { // parse packet type char type[2] = {'\0'}; reader.get(type, 1); _type = static_cast<Packet::Type>(atoi(type)); // std::stoi(std::string(type, 1)) // if (_type < TypeMin || _type > TypeMax) { // LWarn("Invalid message type: ", _type) // return false; // } // LTrace("Parse type: ", type, ": ", typeString()) } // parse attachments if type binary (not implemented) // parse namespace (if any) if (reader.peek() == '/') { reader.readToNext(_nsp, ','); } // parse id reader.readNextNumber((unsigned int&)_id); // parse json data // TODO: Take into account joined messages if (reader.available()) { std::string temp; reader.get(temp, reader.available()); json::value json = json::value::parse(temp.begin(), temp.end()); if (json.is_array()) { if (json.size() < 2) { _event = "message"; _message = json[0].dump(); } else { assert(json[0].is_string()); _event = json[0].get<std::string>(); _message = json[1].dump(); } } else if (json.is_object()) { _message = json.dump(); } } _size = reader.position(); // LDebug("Parse success: ", toString()) return _size; }