void on_read(mp::wavy::event& e) { try{ while(true) { if(m_pac.execute()) { msgpack::object msg = m_pac.data(); std::shared_ptr<msgpack::zone> z( m_pac.release_zone() ); m_pac.reset(); e.more(); //e.next(); //DEBUG(std::cout << "object received: " << msg << "->"); //DEBUG(lock_mut lock(&mut);); event_handle(fd(), msg, &*z); return; } m_pac.reserve_buffer(8*1024); int read_len = ::read(fd(), m_pac.buffer(), m_pac.buffer_capacity()); if(read_len <= 0) { if(read_len == 0) {throw mp::system_error(errno, "connection closed"); } if(errno == EAGAIN || errno == EINTR) { return; } else { perror("read"); throw mp::system_error(errno, "read error"); } } m_pac.buffer_consumed(read_len); } }catch(...){ DEBUG_OUT("fd:%d ",fd());; perror("exception "); e.remove(); } }
static void feed_file(msgpack::unpacker& pac, const char* path) { std::ifstream fin(path); while(true) { pac.reserve_buffer(32*1024); fin.read(pac.buffer(), pac.buffer_capacity()); if(fin.bad()) { throw std::runtime_error("read failed"); } pac.buffer_consumed(static_cast<size_t>(fin.gcount())); if(fin.fail()) { break; } } }
void QueryChannel::produce(ResponseHeader &hdr, msgpack::unpacker &unpacker) { // Unpack the payload msgpack::unpacked unp; if (unpacker.next(&unp)) { msgpack::object obj = unp.get(); NodeResponseBase type; try { obj.convert(type); if (type.Type == "ack") { NodeAck ack; obj.convert(ack); m_listener->onQueryAck(hdr,ack); } else if (type.Type == "response") { NodeResponse resp; obj.convert(resp); m_listener->onQueryResponse(hdr,resp); } else if (type.Type == "done") { m_listener->onQueryComplete(hdr); // deregister this channel m_ioThread.removeChannel(hdr.Seq); } } catch (std::exception &e) { std::cout << "Unexpected node response type " << type.Type << " " << e.what() << std::endl; } } }
void LogChannel::produce(ResponseHeader &hdr, msgpack::unpacker &unpacker) { LogRecord record; // Unpack the payload msgpack::unpacked unp; if (unpacker.next(&unp)) { msgpack::object obj = unp.get(); record = obj.as<LogRecord>(); } m_listener->onLogRecord(hdr,record); }
void EventChannel::produce(ResponseHeader &hdr, msgpack::unpacker &unpacker) { // Unpack the payload msgpack::unpacked unp; if (unpacker.next(&unp)) { msgpack::object obj = unp.get(); RecordBase type; try { obj.convert(type); if (type.Event == "user") { UserEventRecord rec; obj.convert(rec); m_listener->onUserEventRecord(hdr,rec); return; } else if (type.Event == "query") { QueryRecord rec; obj.convert(rec); m_listener->onQueryEventRecord(hdr,rec); return; } else if (type.Event == "member-join" || type.Event == "member-leave" || type.Event == "member-update" || type.Event == "member-failed" || type.Event == "member-reap") { MemberEventRecord rec; obj.convert(rec); m_listener->onMemberEventRecord(hdr,rec); return; } } catch (std::exception &e) { std::cout << "Unexpected event type " << type.Event << " " << e.what() << std::endl; } } }