#include "timer_bank.hpp" #include #include #include namespace timers { namespace { constexpr int INVALID = -22; using Word = std::uint64_t; using Bytes = std::vector; struct Request { unsigned opcode = 0; unsigned order = 0; std::array words{}; int first = 0; int second = 0; unsigned flags = 0; }; std::size_t arity(unsigned opcode) { switch (opcode) { case 1: return 2; case 2: return 1; case 3: return 1; case 4: return 5; case 5: return 2; case 6: return 1; case 7: return 3; case 8: return 2; case 9: return 2; case 10: return 2; case 11: return 1; case 12: return 0; case 13: return 1; case 14: return 0; default: return 99; } } Word decode_word(const Bytes &bytes, std::size_t offset, unsigned order) { Word value = 0; if (order == 0) { for (unsigned index = 0; index < 8; ++index) { value |= static_cast(bytes[offset + index]) << (index * 8); } } else { for (unsigned index = 0; index < 8; ++index) { value = (value << 8) | bytes[offset + index]; } } return value; } void append_word(Bytes &bytes, Word value, unsigned order) { if (order == 0) { for (unsigned index = 0; index < 8; ++index) { bytes.push_back(static_cast(value >> (index * 8))); } } else { for (unsigned index = 0; index < 8; ++index) { bytes.push_back(static_cast(value >> ((7 - index) * 8))); } } } bool as_int(Word value, int &out) { const Word positive_max = static_cast(std::numeric_limits::max()); const Word negative_min = static_cast( static_cast(std::numeric_limits::min())); if (value <= positive_max) { out = static_cast(value); return true; } if (value < negative_min) { return false; } const Word magnitude = ~value + 1; out = static_cast(-static_cast(magnitude)); return true; } bool as_unsigned(Word value, unsigned &out) { if (value > std::numeric_limits::max()) { return false; } out = static_cast(value); return true; } bool decode_request(const Bytes &bytes, Request &request) { if (bytes.size() < 8) { return false; } if (bytes[0] != 0x54 || bytes[1] != 0x42 || bytes[2] != 1 || bytes[4] > 1 || bytes[5] != 0) { return false; } const unsigned opcode = bytes[3]; const std::size_t count = arity(opcode); if (count > request.words.size()) { return false; } const std::size_t payload_length = static_cast(bytes[6]) | (static_cast(bytes[7]) << 8); if (payload_length != count * 8 || bytes.size() != 8 + payload_length) { return false; } Request parsed; parsed.opcode = opcode; parsed.order = bytes[4]; for (std::size_t index = 0; index < count; ++index) { parsed.words[index] = decode_word(bytes, 8 + index * 8, parsed.order); } switch (opcode) { case 1: if (!as_int(parsed.words[0], parsed.first) || !as_unsigned(parsed.words[1], parsed.flags)) { return false; } break; case 2: case 3: case 7: case 8: case 10: case 11: case 13: if (!as_int(parsed.words[0], parsed.first)) { return false; } break; case 4: if (!as_int(parsed.words[0], parsed.first) || !as_unsigned(parsed.words[1], parsed.flags)) { return false; } break; case 5: if (!as_int(parsed.words[0], parsed.first)) { return false; } break; case 9: if (!as_int(parsed.words[0], parsed.first) || !as_int(parsed.words[1], parsed.second)) { return false; } break; case 6: case 12: case 14: break; default: return false; } request = parsed; return true; } void append_setting(std::vector &words, const Setting &setting) { words.push_back(setting.remaining); words.push_back(setting.interval); words.push_back(setting.armed); } void append_inspection(std::vector &words, const Inspection &inspection) { words.push_back(inspection.clock); words.push_back(inspection.nonblock); words.push_back(inspection.remaining); words.push_back(inspection.interval); words.push_back(inspection.armed); words.push_back(inspection.pending); words.push_back(inspection.canceled); words.push_back(inspection.references); } Inspection repeated_inspection(Word nonce) { return Inspection{nonce, nonce, nonce, nonce, nonce, nonce, nonce, nonce}; } std::vector dispatch(Bank &bank, const Request &request) { std::vector words(1, 0); const std::array &args = request.words; int result = 0; switch (request.opcode) { case 1: result = bank.create(request.first, request.flags); break; case 2: result = bank.duplicate(request.first); break; case 3: result = bank.close(request.first); break; case 4: { Setting old{args[4], args[4], args[4]}; result = bank.set(request.first, request.flags, args[2], args[3], old); append_setting(words, old); break; } case 5: result = bank.advance(request.first, args[1]); break; case 6: result = bank.jump(args[0]); break; case 7: { Word value = args[2]; result = bank.read(request.first, args[1], value); words.push_back(value); break; } case 8: { Inspection out = repeated_inspection(args[1]); result = bank.inspect(request.first, out); append_inspection(words, out); break; } case 9: result = bank.set_nonblock(request.first, request.second); break; case 10: { Setting out{args[1], args[1], args[1]}; result = bank.get(request.first, out); append_setting(words, out); break; } case 11: result = bank.poll(request.first); break; case 12: words.push_back(static_cast(bank.open_count())); words.push_back(static_cast(bank.object_count())); break; case 13: words.push_back(bank.clock_value(request.first)); break; case 14: bank.reset(); break; default: result = INVALID; break; } words[0] = static_cast(static_cast(result)); return words; } Bytes encode_response(const Request &request, const std::vector &words) { const std::size_t length = words.size() * 8; Bytes bytes; bytes.reserve(8 + length); bytes.push_back(0x54); bytes.push_back(0x42); bytes.push_back(1); bytes.push_back(static_cast(request.opcode | 0x80)); bytes.push_back(static_cast(request.order)); bytes.push_back(0); bytes.push_back(static_cast(length)); bytes.push_back(static_cast(length >> 8)); for (Word word : words) { append_word(bytes, word, request.order); } return bytes; } } int transact(Bank &bank, const std::vector &request, std::vector &response) { Request decoded; if (!decode_request(request, decoded)) { return INVALID; } const std::vector words = dispatch(bank, decoded); response = encode_response(decoded, words); return 0; } }