#include "protocol.hpp" #include #include #include #include #include #include namespace fold { void print_total(std::ostream&, const Total&); void print_worker(std::ostream&, const WorkerView&); void print_snapshot(std::ostream&, const Snapshot&); namespace { struct Line { std::string text; bool present=false; bool oversized=false; }; Line read_line(std::istream& input) { Line line; std::array block{}; std::size_t used=0; for (;;) { const auto next=input.get(); if (next == std::char_traits::eof()) { if (used != 0 && !line.oversized) { line.text.append(block.data(),used); } return line; } line.present=true; const char ch=static_cast(next); if (ch == '\n') { if (used != 0 && !line.oversized) { line.text.append(block.data(),used); } return line; } if (line.oversized) { continue; } block[used++]=ch; if (line.text.size()+used > 512) { line.oversized=true; line.text.clear(); used=0; continue; } if (used == block.size()) { line.text.append(block.data(),used); used=0; } } } struct Command { std::string verb; std::vector numbers; }; bool split(const std::string& line, std::vector& tokens) { std::string token; for (unsigned char ch : line) { if (ch == ' ' || ch == '\t') { if (!token.empty()) { tokens.push_back(token); token.clear(); } } else { if (ch < 33 || ch > 126) { return false; } token.push_back(static_cast(ch)); } } if (!token.empty()) { tokens.push_back(token); } return !tokens.empty(); } bool integer(const std::string& text, std::int64_t& output) { if (text.empty()) { return false; } std::size_t at=0; bool negative=false; if (text[0] == '-') { negative=true; at=1; } if (at == text.size()) { return false; } std::uint64_t magnitude=0; const std::uint64_t limit=negative ? 9223372036854775808ULL : 9223372036854775807ULL; while (at < text.size()) { const unsigned char ch=text[at++]; if (ch < '0' || ch > '9') { return false; } const std::uint64_t digit=ch-'0'; if (magnitude > (limit-digit)/10) { return false; } magnitude=magnitude*10+digit; } if (!negative) { output=static_cast(magnitude); } else if (magnitude == 9223372036854775808ULL) { output=std::numeric_limits::min(); } else { output=-static_cast(magnitude); } return true; } int arity(const std::string& verb) { if (verb == "RESET") { return 2; } if (verb == "WRITE") { return 2; } if (verb == "TAKE") { return 2; } if (verb == "BYTE") { return 1; } if (verb == "FINISH") { return 1; } if (verb == "WORKER") { return 1; } if (verb == "CLOSE") { return 0; } if (verb == "TOTAL") { return 0; } if (verb == "SNAP") { return 0; } return -1; } bool parse(const std::string& line, Command& command) { std::vector tokens; if (!split(line,tokens)) { return false; } const int count=arity(tokens.front()); if (count < 0 || tokens.size() != static_cast(count+1)) { return false; } Command parsed; parsed.verb=tokens.front(); for (std::size_t i=1; i 4096) { print_result(output,LIMIT); continue; } Command command; if (line.oversized || !parse(line.text,command)) { print_result(output,SYNTAX); continue; } dispatch(pool,command,output); } return 0; } }