2022-02-23 23:08:28 -05:00
|
|
|
#include "wrap-string.hpp"
|
|
|
|
|
#include "wrap-vector.hpp"
|
2022-04-16 02:26:32 -04:00
|
|
|
#include "util.hpp"
|
|
|
|
|
#include "drivenengine.hpp"
|
2026-02-24 22:36:01 -05:00
|
|
|
#include "animqueue.hpp"
|
2023-10-18 17:23:05 -04:00
|
|
|
#include "base-buffer.hpp"
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
#include <string_view>
|
2022-02-25 19:57:23 -05:00
|
|
|
#include <utility>
|
2022-02-17 20:02:08 -05:00
|
|
|
#include <cstring>
|
2023-02-14 13:14:18 -05:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <cassert>
|
2021-10-04 17:45:18 -04:00
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
DrivenEngineReg *DrivenEngineReg::All;
|
2022-02-17 20:02:08 -05:00
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
DrivenEngineReg::DrivenEngineReg(const char *n, DrivenEngineMaker fn) {
|
|
|
|
|
name = n;
|
|
|
|
|
maker = fn;
|
|
|
|
|
next = All;
|
|
|
|
|
All = this;
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// DrivenEngine private methods
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
int DrivenEngine::find_unused_chid() {
|
2025-12-18 15:28:05 -05:00
|
|
|
// Note: channel ID zero is never used. Channel ID 0 used to be
|
|
|
|
|
// reserved for the stdio channel. When we eliminated the stdio
|
|
|
|
|
// channel, we blocked off ID 0 permanently.
|
2023-02-14 13:14:18 -05:00
|
|
|
for (int i = 0; i < DRV_MAX_CHAN; i++) {
|
|
|
|
|
int id = next_unused_chid_++;
|
|
|
|
|
if (next_unused_chid_ == DRV_MAX_CHAN) next_unused_chid_ = 1;
|
|
|
|
|
if (channels_[id] == nullptr) return id;
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
2023-02-14 13:14:18 -05:00
|
|
|
assert(false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Channel *DrivenEngine::get_chid(int chid) const {
|
2025-12-18 15:28:05 -05:00
|
|
|
assert(chid != 0);
|
2023-02-14 13:14:18 -05:00
|
|
|
assert(unsigned(chid) < DRV_MAX_CHAN);
|
|
|
|
|
assert(channels_[chid].get() != nullptr);
|
|
|
|
|
return channels_[chid].get();
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 22:36:01 -05:00
|
|
|
static DrivenEngine *make_engine(std::string_view kind, EngineWrapper *w) {
|
2022-02-25 19:57:23 -05:00
|
|
|
for (auto reg = DrivenEngineReg::All; reg != nullptr; reg=reg->next) {
|
2023-02-14 13:14:18 -05:00
|
|
|
if (kind == reg->name) {
|
2026-02-24 22:36:01 -05:00
|
|
|
UniqueDrivenEngine result = reg->maker(w);
|
2023-02-14 13:14:18 -05:00
|
|
|
return result.release();
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-10-04 17:45:18 -04:00
|
|
|
|
2023-10-18 14:22:35 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2025-12-15 22:42:02 -05:00
|
|
|
// A std::ostream that sends its output to a StreamBuffer.
|
|
|
|
|
//
|
|
|
|
|
// This is not currently used so it's commented out. But if it's
|
|
|
|
|
// needed, it works fine: it can be ressurected.
|
2023-10-18 14:22:35 -04:00
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2025-12-15 22:42:02 -05:00
|
|
|
// class StreamBufferWriter : public std::streambuf, public eng::opnew {
|
|
|
|
|
// private:
|
|
|
|
|
// StreamBuffer *target_;
|
|
|
|
|
// public:
|
|
|
|
|
// StreamBufferWriter(StreamBuffer *t) : target_(t) {}
|
2023-10-18 14:22:35 -04:00
|
|
|
|
2025-12-15 22:42:02 -05:00
|
|
|
// virtual int_type overflow(int_type c) {
|
|
|
|
|
// if (c != EOF) {
|
|
|
|
|
// target_->write_uint8(c);
|
|
|
|
|
// }
|
|
|
|
|
// return c;
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// class StreamBufferOStream : public std::ostream, public eng::opnew {
|
|
|
|
|
// private:
|
|
|
|
|
// StreamBufferWriter writer_;
|
|
|
|
|
// public:
|
|
|
|
|
// StreamBufferOStream(StreamBuffer *t) : std::ostream(nullptr), writer_(t) {
|
|
|
|
|
// rdbuf(&writer_);
|
|
|
|
|
// }
|
|
|
|
|
// virtual ~StreamBufferOStream() {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
2023-10-18 14:22:35 -04:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Class Channel
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2026-02-21 18:47:54 -05:00
|
|
|
Channel::Channel(int chid, int port, const eng::string &target, bool stop) {
|
2021-10-04 17:45:18 -04:00
|
|
|
chid_ = chid;
|
|
|
|
|
port_ = port;
|
|
|
|
|
closed_ = false;
|
|
|
|
|
target_ = target;
|
2022-01-05 12:50:16 -05:00
|
|
|
stop_driver_ = stop;
|
2021-10-12 12:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 16:45:47 -05:00
|
|
|
std::string_view Channel::peek_outgoing() const {
|
2026-02-21 18:47:54 -05:00
|
|
|
return sb_out_.view();
|
2021-11-04 13:40:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Channel::sent_outgoing(int nbytes) {
|
2026-02-21 18:47:54 -05:00
|
|
|
sb_out_.read_bytes(nbytes);
|
2021-11-04 13:40:42 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// DrivenEngine Client-Side API
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2021-10-04 17:45:18 -04:00
|
|
|
|
2021-10-08 21:03:52 -04:00
|
|
|
void DrivenEngine::listen_port(int port) {
|
2023-02-14 13:14:18 -05:00
|
|
|
assert(listen_ports_.size() < DRV_MAX_LISTEN_PORTS);
|
2022-02-17 20:02:08 -05:00
|
|
|
listen_ports_.push_back(port);
|
2021-10-08 21:03:52 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-04 17:45:18 -04:00
|
|
|
double DrivenEngine::get_clock() {
|
|
|
|
|
return clock_;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
SharedChannel DrivenEngine::new_outgoing_channel(const eng::string &target) {
|
2021-10-07 14:58:20 -04:00
|
|
|
int chid = find_unused_chid();
|
2022-02-17 20:02:08 -05:00
|
|
|
new_outgoing_.push_back(chid);
|
2026-02-21 18:47:54 -05:00
|
|
|
SharedChannel result = eng::make_shared<Channel>(chid, 0, target, stop_driver_);
|
2022-01-05 12:50:16 -05:00
|
|
|
channels_[chid] = result;
|
|
|
|
|
return result;
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-01-05 12:50:16 -05:00
|
|
|
SharedChannel DrivenEngine::new_incoming_channel() {
|
2021-10-04 17:45:18 -04:00
|
|
|
if (accepted_channels_.empty()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
2022-01-05 12:50:16 -05:00
|
|
|
SharedChannel result = std::move(accepted_channels_.back());
|
2021-10-04 17:45:18 -04:00
|
|
|
accepted_channels_.pop_back();
|
2021-11-23 14:38:08 -05:00
|
|
|
return result;
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
|
2021-10-04 17:45:18 -04:00
|
|
|
void DrivenEngine::stop_driver() {
|
|
|
|
|
stop_driver_ = true;
|
2023-02-14 13:14:18 -05:00
|
|
|
for (int i = 0; i < DRV_MAX_CHAN; i++) {
|
2022-01-05 12:50:16 -05:00
|
|
|
if (channels_[i] != nullptr) {
|
|
|
|
|
channels_[i]->stop_driver_ = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
DrivenEngine::DrivenEngine() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DrivenEngine::~DrivenEngine() {}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// LOGFILE EVENT IDS.
|
|
|
|
|
//
|
|
|
|
|
// There's one event ID for each mutator, plus one for 'release'.
|
|
|
|
|
//
|
|
|
|
|
// There are no event IDs for getters, these aren't considered loggable events.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
enum DrvAction {
|
|
|
|
|
PLAY_INITIALIZE,
|
|
|
|
|
PLAY_CLEAR_NEW_OUTGOING,
|
|
|
|
|
PLAY_SENT_OUTGOING,
|
|
|
|
|
PLAY_RECV_INCOMING,
|
|
|
|
|
PLAY_NOTIFY_CLOSE,
|
|
|
|
|
PLAY_NOTIFY_ACCEPT,
|
2024-09-03 21:56:53 -04:00
|
|
|
PLAY_UPDATE,
|
2025-06-13 21:03:13 -04:00
|
|
|
PLAY_ACCESS,
|
2023-02-14 13:14:18 -05:00
|
|
|
PLAY_RELEASE,
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-16 21:32:40 -04:00
|
|
|
[[maybe_unused]] inline static const char *action_string(DrvAction act) {
|
2023-02-14 13:14:18 -05:00
|
|
|
switch(act) {
|
|
|
|
|
case PLAY_INITIALIZE: return "PLAY_INITIALIZE";
|
|
|
|
|
case PLAY_CLEAR_NEW_OUTGOING: return "PLAY_CLEAR_NEW_OUTGOING";
|
|
|
|
|
case PLAY_SENT_OUTGOING: return "PLAY_SENT_OUTGOING";
|
|
|
|
|
case PLAY_RECV_INCOMING: return "PLAY_RECV_INCOMING";
|
|
|
|
|
case PLAY_NOTIFY_CLOSE: return "PLAY_NOTIFY_CLOSE";
|
|
|
|
|
case PLAY_NOTIFY_ACCEPT: return "PLAY_NOTIFY_ACCEPT";
|
2024-09-03 21:56:53 -04:00
|
|
|
case PLAY_UPDATE: return "PLAY_UPDATE";
|
2025-06-13 21:03:13 -04:00
|
|
|
case PLAY_ACCESS: return "PLAY_ACCESS";
|
2023-02-14 13:14:18 -05:00
|
|
|
case PLAY_RELEASE: return "PLAY_RELEASE";
|
|
|
|
|
default: return "unknown";
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-16 21:32:40 -04:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// RLOG and WLOG, functions to read and write binary data to logfiles.
|
|
|
|
|
//
|
|
|
|
|
// After doing an rlog operation, you should check the stream
|
|
|
|
|
// for "good" to find out if there was any error.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2026-02-22 22:46:54 -05:00
|
|
|
class PlayLogfile : public std::ofstream {
|
2023-07-25 16:46:56 -04:00
|
|
|
using std::ofstream::ofstream;
|
2026-02-22 22:46:54 -05:00
|
|
|
using DS = DataSerializer<PlayLogfile>;
|
2023-07-25 16:46:56 -04:00
|
|
|
public:
|
|
|
|
|
void write_bytes(const char *n, size_t size) { write(n, size); }
|
2026-02-22 19:26:31 -05:00
|
|
|
void raise_integer_truncated() {
|
2023-07-25 16:46:56 -04:00
|
|
|
fprintf(stderr, "number exceeds allowable size\n");
|
|
|
|
|
std::abort();
|
|
|
|
|
}
|
2026-02-22 22:46:54 -05:00
|
|
|
void write_uint8(uint64_t data) { DS(this).write_uint8(data); }
|
|
|
|
|
void write_uint32(uint64_t data) { DS(this).write_uint32(data); }
|
|
|
|
|
void write_uint64(uint64_t data) { DS(this).write_uint64(data); }
|
|
|
|
|
void write_int64(int64_t data) { DS(this).write_int64(data); }
|
|
|
|
|
void write_double(double data) { DS(this).write_double(data); }
|
|
|
|
|
void write_string(std::string_view s){ DS(this).write_string(s); }
|
2023-07-25 16:46:56 -04:00
|
|
|
void write_short_string(std::string_view v) {
|
|
|
|
|
assert(v.size() < DRV_SHORTSTRING_SIZE);
|
|
|
|
|
write_string(v);
|
|
|
|
|
}
|
|
|
|
|
void write_cmd_hash(DrvAction act, uint32_t hash) {
|
|
|
|
|
write_uint8(act);
|
|
|
|
|
write_uint32(hash);
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
2023-07-25 16:46:56 -04:00
|
|
|
};
|
2021-10-07 14:58:20 -04:00
|
|
|
|
2026-02-22 22:46:54 -05:00
|
|
|
class ReplayLogfile : public std::ifstream {
|
2023-07-25 16:46:56 -04:00
|
|
|
using std::ifstream::ifstream;
|
2026-02-22 22:46:54 -05:00
|
|
|
using DD = DataDeserializer<ReplayLogfile>;
|
2023-07-25 16:46:56 -04:00
|
|
|
public:
|
2026-02-22 23:56:48 -05:00
|
|
|
using string_type = std::string;
|
2023-09-11 11:58:26 -04:00
|
|
|
void read_bytes_into(char *n, size_t size) {
|
|
|
|
|
read(n, size);
|
|
|
|
|
if (!good()) {
|
|
|
|
|
memset(n, 0, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-25 16:46:56 -04:00
|
|
|
void raise_string_too_long() {
|
|
|
|
|
fprintf(stderr, "string in logfile is too long");
|
|
|
|
|
std::abort();
|
|
|
|
|
}
|
2026-02-22 22:46:54 -05:00
|
|
|
uint8_t read_uint8() { return DD(this).read_uint8(); }
|
|
|
|
|
uint32_t read_uint32() { return DD(this).read_uint32(); }
|
|
|
|
|
uint64_t read_uint64() { return DD(this).read_uint64(); }
|
|
|
|
|
int64_t read_int64() { return DD(this).read_int64(); }
|
|
|
|
|
double read_double() { return DD(this).read_double(); }
|
|
|
|
|
size_t read_length() { return DD(this).read_length(); }
|
|
|
|
|
std::string read_string() { return DD(this).read_string(); }
|
2023-07-25 16:46:56 -04:00
|
|
|
std::string_view read_short_string(EngineWrapper *w) {
|
|
|
|
|
size_t size = read_length();
|
|
|
|
|
assert(size <= DRV_SHORTSTRING_SIZE);
|
|
|
|
|
if (size > 0) read(w->databuffer, size);
|
|
|
|
|
if (!good()) return std::string_view();
|
|
|
|
|
return std::string_view(w->databuffer, size);
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
2023-07-25 16:46:56 -04:00
|
|
|
};
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// reset_wrapper
|
|
|
|
|
//
|
|
|
|
|
// Shut down a EngineWrapper, store an optional error message.
|
|
|
|
|
//
|
|
|
|
|
// release
|
|
|
|
|
//
|
|
|
|
|
// Shut down an EngineWrapper cleanly, with no error message, and
|
|
|
|
|
// log the step if the logfile is open.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static void reset_wrapper(EngineWrapper *w, const char *format, ...) {
|
|
|
|
|
va_list argp;
|
|
|
|
|
va_start(argp, format);
|
|
|
|
|
memset(w->error, 0, DRV_ERRMSG_SIZE);
|
|
|
|
|
vsnprintf(w->error, DRV_ERRMSG_SIZE, format, argp);
|
|
|
|
|
w->error[DRV_ERRMSG_SIZE - 1] = 0;
|
|
|
|
|
|
|
|
|
|
if (w->wlog != nullptr) {
|
|
|
|
|
w->wlog->close();
|
|
|
|
|
delete w->wlog;
|
|
|
|
|
w->wlog = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (w->rlog != nullptr) {
|
|
|
|
|
w->rlog->close();
|
|
|
|
|
delete w->rlog;
|
|
|
|
|
w->rlog = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (w->engine != nullptr) {
|
|
|
|
|
delete w->engine;
|
|
|
|
|
w->engine = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void release(EngineWrapper *w) {
|
|
|
|
|
if (w->wlog != nullptr) {
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_RELEASE, eng::memhash());
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
reset_wrapper(w, "");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// DRIVER Methods: Getters
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void DrivenEngine::drv_get_listen_ports(uint32_t *nports, const uint32_t **ports) const {
|
|
|
|
|
*nports = listen_ports_.size();
|
|
|
|
|
*ports = &listen_ports_[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrivenEngine::drv_get_new_outgoing(uint32_t *nchids, const uint32_t **chids) const {
|
|
|
|
|
*nchids = new_outgoing_.size();
|
|
|
|
|
*chids = &new_outgoing_[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *DrivenEngine::drv_get_target(uint32_t chid) const {
|
|
|
|
|
return get_chid(chid)->target_.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrivenEngine::drv_get_channel_released(uint32_t chid) const {
|
2022-01-05 12:50:16 -05:00
|
|
|
return channels_[chid].use_count() == 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
void DrivenEngine::drv_get_outgoing(uint32_t chid, uint32_t *len, const char **data) const {
|
|
|
|
|
std::string_view v = get_chid(chid)->peek_outgoing();
|
|
|
|
|
*len = v.size();
|
|
|
|
|
*data = v.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrivenEngine::drv_get_outgoing_empty(uint32_t chid) const {
|
|
|
|
|
std::string_view v = get_chid(chid)->peek_outgoing();
|
|
|
|
|
return (v.size() == 0);
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
double DrivenEngine::drv_get_clock() const {
|
|
|
|
|
return clock_;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 23:35:47 -05:00
|
|
|
bool DrivenEngine::drv_get_have_prints() const {
|
|
|
|
|
return have_prints_;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
bool DrivenEngine::drv_get_rescan_lua_source() const {
|
|
|
|
|
return rescan_lua_source_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DrivenEngine::drv_get_stop_driver() const {
|
|
|
|
|
return stop_driver_;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 20:57:47 -04:00
|
|
|
int64_t DrivenEngine::drv_get_actor_id() const {
|
2026-02-21 19:27:42 -05:00
|
|
|
return actor_id_;
|
2023-07-24 17:19:25 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:21:44 -04:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// DRIVER Methods: Mutators
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DrivenEngine::drv_clear_new_outgoing() {
|
|
|
|
|
new_outgoing_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrivenEngine::drv_sent_outgoing(uint32_t chid, uint32_t nbytes) {
|
2021-11-04 13:40:42 -04:00
|
|
|
return get_chid(chid)->sent_outgoing(nbytes);
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
void DrivenEngine::drv_recv_incoming(uint32_t chid, uint32_t nbytes, const char *bytes) {
|
|
|
|
|
if (nbytes > 0) {
|
2023-05-18 17:14:55 -04:00
|
|
|
std::string_view sbytes(bytes, nbytes);
|
2021-10-12 12:46:11 -04:00
|
|
|
Channel *ch = get_chid(chid);
|
2026-02-21 18:47:54 -05:00
|
|
|
ch->sb_in_.write_bytes(sbytes);
|
2021-10-05 12:54:37 -04:00
|
|
|
}
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
void DrivenEngine::drv_notify_close(uint32_t chid, uint32_t len, const char *data) {
|
2021-10-12 13:54:08 -04:00
|
|
|
Channel *ch = get_chid(chid);
|
|
|
|
|
ch->closed_ = true;
|
2023-02-14 13:14:18 -05:00
|
|
|
ch->error_ = std::string(data, len);
|
2022-01-05 12:50:16 -05:00
|
|
|
channels_[chid].reset();
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
uint32_t DrivenEngine::drv_notify_accept(uint32_t port) {
|
2021-10-07 14:58:20 -04:00
|
|
|
int chid = find_unused_chid();
|
2026-02-21 18:47:54 -05:00
|
|
|
channels_[chid] = eng::make_shared<Channel>(chid, port, "", stop_driver_);
|
2022-01-05 12:50:16 -05:00
|
|
|
accepted_channels_.push_back(channels_[chid]);
|
2021-10-04 17:45:18 -04:00
|
|
|
return chid;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 21:56:53 -04:00
|
|
|
void DrivenEngine::drv_update(double clock) {
|
2023-02-14 13:14:18 -05:00
|
|
|
clock_ = clock;
|
|
|
|
|
event_update();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 21:03:13 -04:00
|
|
|
void DrivenEngine::drv_access(AccessKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk) {
|
2024-09-03 22:44:22 -04:00
|
|
|
// This next line is a hack, because the DrivenEngine is not supposed to care about 'kind'.
|
2025-06-13 21:03:13 -04:00
|
|
|
if (kind == AccessKind::INVOKE_LUA_SOURCE) rescan_lua_source_ = false;
|
2024-09-04 21:00:47 -04:00
|
|
|
call_function_retpk_.clear();
|
2025-06-13 21:03:13 -04:00
|
|
|
event_access(kind, place, std::string_view(datapk, datapklen), &call_function_retpk_);
|
2024-09-04 21:00:47 -04:00
|
|
|
if (retpklen != nullptr) {
|
|
|
|
|
std::string_view view = call_function_retpk_.view();
|
|
|
|
|
*retpklen = view.size();
|
|
|
|
|
*retpk = view.data();
|
|
|
|
|
}
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// C Wrappers: Mutators
|
|
|
|
|
//
|
|
|
|
|
// The wrapper for a mutator consists of two parts: the wrapper which is used at
|
|
|
|
|
// 'play' time, and the wrapper which is used at 'replay' time.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 19:58:26 -04:00
|
|
|
static void play_initialize(EngineWrapper *w, const char *engtype, const char *logfn) {
|
2023-02-14 13:14:18 -05:00
|
|
|
if (w->engine != nullptr) {
|
|
|
|
|
return reset_wrapper(w, "Cannot initialize wrapper, it's already initialized.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear the error message.
|
|
|
|
|
memset(w->error, 0, DRV_ERRMSG_SIZE);
|
|
|
|
|
|
|
|
|
|
// Open the logfile, if any is specified.
|
|
|
|
|
if ((logfn != nullptr) && (logfn[0] != 0)) {
|
|
|
|
|
w->wlog = new PlayLogfile(logfn, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
|
|
|
|
|
if (!w->wlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "Could not open replay log for writing: %s", logfn);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
w->wlog = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have a logfile, then log this initialization.
|
|
|
|
|
if (w->wlog != nullptr) {
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_INITIALIZE, eng::memhash());
|
2025-06-16 19:58:26 -04:00
|
|
|
w->wlog->write_string(engtype);
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
// Create the engine of the appropriate type.
|
2026-02-24 22:36:01 -05:00
|
|
|
w->engine = make_engine(engtype, w);
|
2023-02-14 13:14:18 -05:00
|
|
|
if (w->engine == nullptr) {
|
2025-06-16 19:58:26 -04:00
|
|
|
return reset_wrapper(w, "No such driven engine type: %s", engtype);
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
2022-02-17 20:02:08 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
static void replay_initialize(EngineWrapper *w) {
|
|
|
|
|
assert(w->rlog != nullptr);
|
2025-06-16 19:58:26 -04:00
|
|
|
std::string engtype = w->rlog->read_string();
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_initialize");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the engine.
|
2026-02-24 22:36:01 -05:00
|
|
|
w->engine = make_engine(engtype.c_str(), w);
|
2023-02-14 13:14:18 -05:00
|
|
|
if (w->engine == nullptr) {
|
2025-06-16 19:58:26 -04:00
|
|
|
return reset_wrapper(w, "No such driven engine type: %s", engtype.c_str());
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
2021-10-08 16:38:10 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void play_clear_new_outgoing(EngineWrapper *w) {
|
|
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_CLEAR_NEW_OUTGOING, eng::memhash());
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
w->engine->drv_clear_new_outgoing();
|
2021-10-05 12:54:37 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
static void replay_clear_new_outgoing(EngineWrapper *w) {
|
|
|
|
|
w->engine->drv_clear_new_outgoing();
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void play_sent_outgoing(EngineWrapper *w, uint32_t chid, uint32_t nbytes) {
|
|
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
|
|
|
|
uint32_t ndata; const char *data;
|
|
|
|
|
w->engine->drv_get_outgoing(chid, &ndata, &data);
|
|
|
|
|
assert(nbytes <= ndata);
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_SENT_OUTGOING, eng::memhash());
|
|
|
|
|
w->wlog->write_uint32(chid);
|
|
|
|
|
w->wlog->write_uint32(nbytes);
|
|
|
|
|
w->wlog->write_uint64(SpookyHash::QkHash64(data, nbytes));
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
w->engine->drv_sent_outgoing(chid, nbytes);
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
static void replay_sent_outgoing(EngineWrapper *w) {
|
2023-07-25 16:46:56 -04:00
|
|
|
uint32_t chid = w->rlog->read_uint32();
|
|
|
|
|
uint32_t nbytes = w->rlog->read_uint32();
|
|
|
|
|
uint64_t hash = w->rlog->read_uint64();
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_sent_outgoing");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t ndata; const char *data;
|
|
|
|
|
w->engine->drv_get_outgoing(chid, &ndata, &data);
|
|
|
|
|
if ((nbytes > ndata) || (hash != SpookyHash::QkHash64(data, nbytes))) {
|
|
|
|
|
return reset_wrapper(w, "nondeterministic in replay_sent_outgoing");
|
|
|
|
|
}
|
2023-02-20 16:02:18 -05:00
|
|
|
if (w->replay_cb_sent_outgoing != nullptr) {
|
|
|
|
|
w->replay_cb_sent_outgoing(w->replay_cb_vp, chid, ndata, data);
|
|
|
|
|
}
|
2023-02-14 13:14:18 -05:00
|
|
|
w->engine->drv_sent_outgoing(chid, nbytes);
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void play_recv_incoming(EngineWrapper *w, uint32_t chid, uint32_t len, const char *data) {
|
|
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_RECV_INCOMING, eng::memhash());
|
|
|
|
|
w->wlog->write_uint32(chid);
|
|
|
|
|
w->wlog->write_short_string(std::string_view(data, len));
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
w->engine->drv_recv_incoming(chid, len, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void replay_recv_incoming(EngineWrapper *w) {
|
2023-07-25 16:46:56 -04:00
|
|
|
uint32_t chid = w->rlog->read_uint32();
|
|
|
|
|
std::string_view data = w->rlog->read_short_string(w);
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_recv_incoming");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w->engine->drv_recv_incoming(chid, data.size(), data.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
2021-10-04 17:45:18 -04:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
static void play_notify_close(EngineWrapper *w, uint32_t chid, uint32_t len, const char *data) {
|
|
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_NOTIFY_CLOSE, eng::memhash());
|
|
|
|
|
w->wlog->write_uint32(chid);
|
|
|
|
|
w->wlog->write_string(std::string_view(data, len));
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w->engine->drv_notify_close(chid, len, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void replay_notify_close(EngineWrapper *w) {
|
2023-07-25 16:46:56 -04:00
|
|
|
uint32_t chid = w->rlog->read_uint32();
|
|
|
|
|
std::string message = w->rlog->read_string();
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_notify_close");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w->engine->drv_notify_close(chid, message.size(), message.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t play_notify_accept(EngineWrapper *w, uint32_t port) {
|
|
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_NOTIFY_ACCEPT, eng::memhash());
|
|
|
|
|
w->wlog->write_uint32(port);
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return w->engine->drv_notify_accept(port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void replay_notify_accept(EngineWrapper *w) {
|
2023-07-25 16:46:56 -04:00
|
|
|
uint32_t port = w->rlog->read_uint32();
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_notify_accept");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w->engine->drv_notify_accept(port);
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
|
|
|
2024-09-03 21:56:53 -04:00
|
|
|
static void play_update(EngineWrapper *w, double clock) {
|
2023-02-14 13:14:18 -05:00
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
2024-09-03 21:56:53 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_UPDATE, eng::memhash());
|
2023-07-25 16:46:56 -04:00
|
|
|
w->wlog->write_double(clock);
|
2023-02-14 13:14:18 -05:00
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 21:56:53 -04:00
|
|
|
w->engine->drv_update(clock);
|
2021-10-04 17:45:18 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-03 21:56:53 -04:00
|
|
|
static void replay_update(EngineWrapper *w) {
|
2023-07-25 16:46:56 -04:00
|
|
|
double clock = w->rlog->read_double();
|
2023-02-14 13:14:18 -05:00
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_event_update");
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 21:56:53 -04:00
|
|
|
w->engine->drv_update(clock);
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
|
|
|
|
|
2025-06-13 21:03:13 -04:00
|
|
|
void play_access(EngineWrapper *w, AccessKind kind, int64_t place, uint32_t datapklen, const char *datapk, uint32_t *retpklen, const char **retpk) {
|
2024-08-28 22:02:25 -04:00
|
|
|
assert(w->rlog == nullptr);
|
|
|
|
|
if (w->wlog != nullptr) {
|
2025-06-13 21:03:13 -04:00
|
|
|
w->wlog->write_cmd_hash(PLAY_ACCESS, eng::memhash());
|
2024-09-02 21:48:24 -04:00
|
|
|
w->wlog->write_uint8(int64_t(kind));
|
2024-08-28 22:02:25 -04:00
|
|
|
w->wlog->write_int64(place);
|
|
|
|
|
w->wlog->write_string(std::string_view(datapk, datapklen));
|
|
|
|
|
w->wlog->flush();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 21:03:13 -04:00
|
|
|
w->engine->drv_access(kind, place, datapklen, datapk, retpklen, retpk);
|
2024-08-28 22:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 21:03:13 -04:00
|
|
|
void replay_access(EngineWrapper *w) {
|
|
|
|
|
AccessKind kind = AccessKind(w->rlog->read_uint8());
|
2024-08-28 22:02:25 -04:00
|
|
|
int64_t place = w->rlog->read_int64();
|
|
|
|
|
std::string srcpack = w->rlog->read_string();
|
|
|
|
|
if (!w->rlog->good()) {
|
2025-06-13 21:03:13 -04:00
|
|
|
return reset_wrapper(w, "replay log corrupt in replay_access");
|
2024-08-28 22:02:25 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-13 21:03:13 -04:00
|
|
|
w->engine->drv_access(kind, place, srcpack.size(), srcpack.c_str(), nullptr, nullptr);
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 17:19:25 -04:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Replay Core
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void replaycore_initialize(EngineWrapper *w, const char *logfn) {
|
|
|
|
|
if (w->engine != nullptr) {
|
|
|
|
|
return reset_wrapper(w, "Cannot initialize wrapper, it's already initialized.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear the error message.
|
|
|
|
|
memset(w->error, 0, DRV_ERRMSG_SIZE);
|
|
|
|
|
|
|
|
|
|
// Open the logfile.
|
|
|
|
|
w->rlog = new ReplayLogfile(logfn, std::ios_base::in | std::ios_base::binary);
|
|
|
|
|
if (!w->rlog->good()) {
|
|
|
|
|
return reset_wrapper(w, "Could not open replay log for reading: %s", logfn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read one step from the logfile, and make sure it's an initialize step.
|
2023-07-25 16:46:56 -04:00
|
|
|
uint8_t code = w->rlog->read_uint8();
|
|
|
|
|
int hash = w->rlog->read_uint32();
|
2023-02-14 13:14:18 -05:00
|
|
|
if (!w->rlog->good()) {
|
2023-02-20 16:02:18 -05:00
|
|
|
return reset_wrapper(w, "logfile corrupt in initial step");
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
if (hash != eng::memhash()) {
|
|
|
|
|
return reset_wrapper(w, "nondeterminism detected in initial step");
|
|
|
|
|
}
|
|
|
|
|
if (code != PLAY_INITIALIZE) {
|
|
|
|
|
return reset_wrapper(w, "replay log doesn't begin with initialize step");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replay the initialize step from the logfile.
|
|
|
|
|
// Doing this immediately, rather than waiting for the driver
|
|
|
|
|
// to call 'step', enforces the invariant that after calling
|
|
|
|
|
// initialize, there's an engine.
|
|
|
|
|
replay_initialize(w);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void replaycore_step(EngineWrapper *w) {
|
|
|
|
|
if (w->rlog == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 16:46:56 -04:00
|
|
|
uint8_t code = w->rlog->read_uint8();
|
2023-02-20 16:02:18 -05:00
|
|
|
if (w->rlog->eof()) {
|
|
|
|
|
return reset_wrapper(w, "logfile terminated abruptly");
|
|
|
|
|
}
|
2023-07-25 16:46:56 -04:00
|
|
|
int hash = w->rlog->read_uint32();
|
2023-02-14 13:14:18 -05:00
|
|
|
if (!w->rlog->good()) {
|
2023-02-20 16:02:18 -05:00
|
|
|
return reset_wrapper(w, "logfile corrupt in replay step");
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
if (hash != eng::memhash()) {
|
|
|
|
|
return reset_wrapper(w, "nondeterminism detected");
|
|
|
|
|
}
|
|
|
|
|
switch (code) {
|
|
|
|
|
case PLAY_CLEAR_NEW_OUTGOING: replay_clear_new_outgoing(w); return;
|
|
|
|
|
case PLAY_SENT_OUTGOING: replay_sent_outgoing(w); return;
|
|
|
|
|
case PLAY_RECV_INCOMING: replay_recv_incoming(w); return;
|
|
|
|
|
case PLAY_NOTIFY_CLOSE: replay_notify_close(w); return;
|
|
|
|
|
case PLAY_NOTIFY_ACCEPT: replay_notify_accept(w); return;
|
2024-09-03 21:56:53 -04:00
|
|
|
case PLAY_UPDATE: replay_update(w); return;
|
2025-06-13 21:03:13 -04:00
|
|
|
case PLAY_ACCESS: replay_access(w); return;
|
2023-02-14 13:14:18 -05:00
|
|
|
case PLAY_RELEASE: release(w); return;
|
|
|
|
|
default: return reset_wrapper(w, "Replay log corrupt in command dispatcher");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Wrapper Initialization
|
|
|
|
|
//
|
|
|
|
|
// To access the engine across a DLL boundary, you first use
|
|
|
|
|
// GetProcAddress or dlsym to fetch the addresses of 'init_play_engine'
|
|
|
|
|
// and 'init_replay_engine'. Then, you use those two functions to
|
|
|
|
|
// initialize a EngineWrapper or a EngineWrapper, which contain the addresses
|
|
|
|
|
// of all the other functions you need. These are the only two functions
|
|
|
|
|
// marked 'DLLEXPORT', all other functions are exported from the DLL
|
|
|
|
|
// indirectly.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2026-02-24 22:36:01 -05:00
|
|
|
static int64_t empty_id_list_ = 0;
|
|
|
|
|
|
|
|
|
|
void DrivenEngine::unexpose_world_to_driver(EngineWrapper *w) {
|
|
|
|
|
w->world = nullptr;
|
2026-02-25 03:43:44 -05:00
|
|
|
w->get_tangibles_near = [](EngineWrapper *, uint64_t, double, double, double, uint32_t *count, int64_t **ids) {
|
|
|
|
|
*count = 0;
|
|
|
|
|
*ids = &empty_id_list_;
|
|
|
|
|
};
|
|
|
|
|
w->get_animation_queues = [](EngineWrapper *, uint32_t count, const int64_t *, uint32_t *lengths, const char **strings) {
|
|
|
|
|
for (int i = 0; i < int(count); i++) {
|
|
|
|
|
lengths[i] = 0;
|
|
|
|
|
strings[i] = "";
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-02-24 22:36:01 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-25 03:43:44 -05:00
|
|
|
void DrivenEngine::init_engine_wrapper(EngineWrapper *w) {
|
2023-02-14 13:14:18 -05:00
|
|
|
memset(w, 0, sizeof(EngineWrapper));
|
|
|
|
|
|
2026-02-25 03:43:44 -05:00
|
|
|
w->get_listen_ports = [](EngineWrapper *w, uint32_t *nports, const uint32_t **ports) {
|
|
|
|
|
w->engine->drv_get_listen_ports(nports, ports);
|
|
|
|
|
};
|
|
|
|
|
w->get_new_outgoing = [](EngineWrapper *w, uint32_t *nchanids, const uint32_t **chanids) {
|
|
|
|
|
w->engine->drv_get_new_outgoing(nchanids, chanids);
|
|
|
|
|
};
|
|
|
|
|
w->get_target = [](EngineWrapper *w, uint32_t chid) {
|
|
|
|
|
return w->engine->drv_get_target(chid);
|
|
|
|
|
};
|
|
|
|
|
w->get_channel_released = [](EngineWrapper *w, uint32_t chid) {
|
|
|
|
|
return w->engine->drv_get_channel_released(chid);
|
|
|
|
|
};
|
|
|
|
|
w->get_outgoing = [](EngineWrapper *w, uint32_t chid, uint32_t *len, const char **data) {
|
|
|
|
|
w->engine->drv_get_outgoing(chid, len, data);
|
|
|
|
|
};
|
|
|
|
|
w->get_outgoing_empty = [](EngineWrapper *w, uint32_t chid) {
|
|
|
|
|
return w->engine->drv_get_outgoing_empty(chid);
|
|
|
|
|
};
|
|
|
|
|
w->get_clock = [](EngineWrapper *w) {
|
|
|
|
|
return w->engine->drv_get_clock();
|
|
|
|
|
};
|
|
|
|
|
w->get_have_prints = [](EngineWrapper *w) {
|
|
|
|
|
return w->engine->drv_get_have_prints();
|
|
|
|
|
};
|
|
|
|
|
w->get_rescan_lua_source = [](EngineWrapper *w) {
|
|
|
|
|
return w->engine->drv_get_rescan_lua_source();
|
|
|
|
|
};
|
|
|
|
|
w->get_stop_driver = [](EngineWrapper *w) {
|
|
|
|
|
return w->engine->drv_get_stop_driver();
|
|
|
|
|
};
|
|
|
|
|
w->get_actor_id = [](EngineWrapper *w) {
|
|
|
|
|
return w->engine->drv_get_actor_id();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unexpose_world_to_driver(w);
|
2026-02-24 22:36:01 -05:00
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
w->play_initialize = play_initialize;
|
|
|
|
|
w->play_clear_new_outgoing = play_clear_new_outgoing;
|
|
|
|
|
w->play_sent_outgoing = play_sent_outgoing;
|
|
|
|
|
w->play_recv_incoming = play_recv_incoming;
|
|
|
|
|
w->play_notify_close = play_notify_close;
|
|
|
|
|
w->play_notify_accept = play_notify_accept;
|
2024-09-03 21:56:53 -04:00
|
|
|
w->play_update = play_update;
|
2025-06-13 21:03:13 -04:00
|
|
|
w->play_access = play_access;
|
2023-02-14 13:14:18 -05:00
|
|
|
|
|
|
|
|
w->replay_initialize = replaycore_initialize;
|
|
|
|
|
w->replay_step = replaycore_step;
|
|
|
|
|
|
2023-04-05 18:41:03 -04:00
|
|
|
w->hook_dprint = util::hook_dprint;
|
2023-02-14 13:14:18 -05:00
|
|
|
w->release = release;
|
2026-02-25 03:43:44 -05:00
|
|
|
}
|
2023-02-14 13:14:18 -05:00
|
|
|
|
2025-01-20 18:54:05 -05:00
|
|
|
#if defined(__linux__)
|
|
|
|
|
#define DLLEXPORT __attribute__((visibility("default")))
|
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
|
#define DLLEXPORT __declspec(dllexport)
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-02-14 13:14:18 -05:00
|
|
|
extern "C" {
|
|
|
|
|
DLLEXPORT void init_engine_wrapper(EngineWrapper *w) {
|
2026-02-25 03:43:44 -05:00
|
|
|
DrivenEngine::init_engine_wrapper(w);
|
2023-02-14 13:14:18 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 03:43:44 -05:00
|
|
|
|