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) { ...@@ -44,10 +44,10 @@ void Replay::WriteHeader(ReplayHeader& header) {
fflush(fp); fflush(fp);
#endif #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) if(!is_recording)
return; return;
if (length < 0 || (int)(pwrite - replay_data) + length > MAX_REPLAY_SIZE) if ((pwrite - replay_data) + length > MAX_REPLAY_SIZE)
return; return;
std::memcpy(pwrite, data, length); std::memcpy(pwrite, data, length);
pwrite += length; pwrite += length;
...@@ -119,7 +119,7 @@ bool Replay::OpenReplay(const wchar_t* name) { ...@@ -119,7 +119,7 @@ bool Replay::OpenReplay(const wchar_t* name) {
if(!rfp) if(!rfp)
return false; return false;
pdata = replay_data; data_position = 0;
is_recording = false; is_recording = false;
is_replaying = false; is_replaying = false;
replay_size = 0; replay_size = 0;
...@@ -209,26 +209,17 @@ bool Replay::ReadName(wchar_t* data) { ...@@ -209,26 +209,17 @@ bool Replay::ReadName(wchar_t* data) {
BufferIO::CopyWStr(buffer, data, 20); BufferIO::CopyWStr(buffer, data, 20);
return true; return true;
} }
bool Replay::ReadData(void* data, int length) { bool Replay::ReadData(void* data, size_t length) {
if(!is_replaying) if(!is_replaying)
return false; return false;
if (length < 0) if (data_position + length > replay_size) {
return false;
if ((int)(pdata - replay_data) + length > (int)replay_size) {
is_replaying = false; is_replaying = false;
return false; return false;
} }
std::memcpy(data, pdata, length); std::memcpy(data, &replay_data[data_position], length);
pdata += length; data_position += length;
return true; return true;
} }
template<typename T>
T Replay::ReadValue() {
T ret{};
if (!ReadData(&ret, sizeof ret))
return -1;
return ret;
}
int Replay::ReadInt32() { int Replay::ReadInt32() {
return ReadValue<int32_t>(); return ReadValue<int32_t>();
} }
...@@ -239,7 +230,7 @@ char Replay::ReadInt8() { ...@@ -239,7 +230,7 @@ char Replay::ReadInt8() {
return ReadValue<char>(); return ReadValue<char>();
} }
void Replay::Rewind() { void Replay::Rewind() {
pdata = replay_data; data_position = 0;
} }
} }
...@@ -34,7 +34,11 @@ public: ...@@ -34,7 +34,11 @@ public:
// record // record
void BeginRecord(); void BeginRecord();
void WriteHeader(ReplayHeader& header); 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 WriteInt32(int data, bool flush = true);
void WriteInt16(short data, bool flush = true); void WriteInt16(short data, bool flush = true);
void WriteInt8(char data, bool flush = true); void WriteInt8(char data, bool flush = true);
...@@ -50,9 +54,13 @@ public: ...@@ -50,9 +54,13 @@ public:
bool ReadNextResponse(unsigned char resp[]); bool ReadNextResponse(unsigned char resp[]);
bool ReadName(wchar_t* data); bool ReadName(wchar_t* data);
//void ReadHeader(ReplayHeader& header); //void ReadHeader(ReplayHeader& header);
bool ReadData(void* data, int length); bool ReadData(void* data, size_t length);
template<typename T> template<typename T>
T ReadValue(); T Read() {
T ret{};
ReadData(&ret, sizeof(T));
return ret;
}
int ReadInt32(); int ReadInt32();
short ReadInt16(); short ReadInt16();
char ReadInt8(); char ReadInt8();
...@@ -71,7 +79,7 @@ private: ...@@ -71,7 +79,7 @@ private:
unsigned char* replay_data; unsigned char* replay_data;
size_t replay_size{}; size_t replay_size{};
unsigned char* pwrite{}; unsigned char* pwrite{};
unsigned char* pdata{}; size_t data_position{};
bool is_recording{}; bool is_recording{};
bool is_replaying{}; 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