* Receives a command from the network. * @param p the packet to read from. * @param cp the struct to write the data to. * @return An error message, or std::nullopt there has been no error. */
| 372 | * @return An error message, or std::nullopt there has been no error. |
| 373 | */ |
| 374 | std::optional<std::string_view> NetworkGameSocketHandler::ReceiveCommand(Packet &p, CommandPacket &cp) |
| 375 | { |
| 376 | cp.company = (CompanyID)p.Recv_uint8(); |
| 377 | cp.cmd = static_cast<Commands>(p.Recv_uint16()); |
| 378 | if (!IsValidCommand(cp.cmd)) return "invalid command"; |
| 379 | if (GetCommandFlags(cp.cmd).Test(CommandFlag::Offline)) return "single-player only command"; |
| 380 | cp.err_msg = p.Recv_uint16(); |
| 381 | cp.data = _cmd_dispatch[cp.cmd].Sanitize(p.Recv_buffer()); |
| 382 | |
| 383 | uint8_t callback = p.Recv_uint8(); |
| 384 | if (callback >= _callback_table.size() || _cmd_dispatch[cp.cmd].Unpack[callback] == nullptr) return "invalid callback"; |
| 385 | |
| 386 | cp.callback = _callback_table[callback]; |
| 387 | return std::nullopt; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Sends a command over the network. |
no test coverage detected