Commit 80187919 authored by Chen Bill's avatar Chen Bill

Replay: add read/write template

parent 56247c6d
......@@ -44,10 +44,10 @@ void Replay::WriteHeader(ReplayHeader& header) {
fflush(fp);
#endif
}
void Replay::WriteData(const void* data, int length, bool flush) {
void Replay::WriteData(const void* data, size_t length, bool flush) {
if(!is_recording)
return;
if (length < 0 || (int)(pwrite - replay_data) + length > MAX_REPLAY_SIZE)
if ((pwrite - replay_data) + length > MAX_REPLAY_SIZE)
return;
std::memcpy(pwrite, data, length);
pwrite += length;
......@@ -119,7 +119,7 @@ bool Replay::OpenReplay(const wchar_t* name) {
if(!rfp)
return false;
pdata = replay_data;
data_position = 0;
is_recording = false;
is_replaying = false;
replay_size = 0;
......@@ -209,26 +209,17 @@ bool Replay::ReadName(wchar_t* data) {
BufferIO::CopyWStr(buffer, data, 20);
return true;
}
bool Replay::ReadData(void* data, int length) {
bool Replay::ReadData(void* data, size_t length) {
if(!is_replaying)
return false;
if (length < 0)
return false;
if ((int)(pdata - replay_data) + length > (int)replay_size) {
if (data_position + length > replay_size) {
is_replaying = false;
return false;
}
std::memcpy(data, pdata, length);
pdata += length;
std::memcpy(data, &replay_data[data_position], length);
data_position += length;
return true;
}
template<typename T>
T Replay::ReadValue() {
T ret{};
if (!ReadData(&ret, sizeof ret))
return -1;
return ret;
}
int Replay::ReadInt32() {
return ReadValue<int32_t>();
}
......@@ -239,7 +230,7 @@ char Replay::ReadInt8() {
return ReadValue<char>();
}
void Replay::Rewind() {
pdata = replay_data;
data_position = 0;
}
}
......@@ -34,7 +34,11 @@ public:
// record
void BeginRecord();
void WriteHeader(ReplayHeader& header);
void WriteData(const void* data, int length, bool flush = true);
void WriteData(const void* data, size_t length, bool flush = true);
template<typename T>
void Write(T data, bool flush = true) {
WriteData(&data, sizeof(T), flush);
}
void WriteInt32(int data, bool flush = true);
void WriteInt16(short data, bool flush = true);
void WriteInt8(char data, bool flush = true);
......@@ -50,9 +54,13 @@ public:
bool ReadNextResponse(unsigned char resp[]);
bool ReadName(wchar_t* data);
//void ReadHeader(ReplayHeader& header);
bool ReadData(void* data, int length);
bool ReadData(void* data, size_t length);
template<typename T>
T ReadValue();
T Read() {
T ret{};
ReadData(&ret, sizeof(T));
return ret;
}
int ReadInt32();
short ReadInt16();
char ReadInt8();
......@@ -71,7 +79,7 @@ private:
unsigned char* replay_data;
size_t replay_size{};
unsigned char* pwrite{};
unsigned char* pdata{};
size_t data_position{};
bool is_recording{};
bool is_replaying{};
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment