Skip to content

Commit da4b34a

Browse files
authored
Merge pull request #2 from AndreasAakesson/master
Update serialization of new TPC Read buffer
2 parents 6362774 + d247b31 commit da4b34a

2 files changed

Lines changed: 51 additions & 16 deletions

File tree

serialize_tcp.cpp

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ int8_t serialized_tcp::to_state(Connection::State* state) const
4747

4848
struct read_buffer
4949
{
50-
size_t remaining;
51-
size_t offset;
50+
uint32_t seq;
51+
size_t cap = 0;
52+
int32_t head;
53+
int32_t hole;
54+
bool push;
5255

5356
size_t size() const noexcept {
54-
return offset;
55-
}
56-
size_t cap() const noexcept {
57-
return remaining + offset;
57+
return head;
5858
}
5959

6060
char vla[0];
@@ -104,8 +104,27 @@ int Write_queue::deserialize_from(void* addr)
104104
}
105105
return sizeof(serialized_writeq) + len;
106106
}
107+
108+
int Read_buffer::deserialize_from(void* addr)
109+
{
110+
const auto& readq = *reinterpret_cast<read_buffer*>(addr);
111+
112+
// start (seq) and cap is already set in the construction of the Read_buffer
113+
this->head = readq.head;
114+
this->hole = readq.hole;
115+
this->push_seen = readq.push;
116+
117+
if(readq.size() > 0)
118+
std::copy(readq.vla, readq.vla + readq.size(), this->buffer().get());
119+
120+
return sizeof(read_buffer) + readq.size();
121+
}
122+
107123
void Connection::deserialize_from(void* addr)
108124
{
125+
if(this->VERSION != serialized_tcp::VERSION)
126+
throw std::runtime_error{"TCP Serialization version mismatch"};
127+
109128
auto* area = (serialized_tcp*) addr;
110129

111130
/// restore TCP stuff
@@ -139,12 +158,10 @@ void Connection::deserialize_from(void* addr)
139158

140159
/// restore read queue
141160
auto* readq = (read_buffer*) &area->vla[writeq_len];
142-
assert(readq->cap() > 0);
143-
read_request = ReadRequest(readq->cap());
144-
read_request.buffer.offset = readq->offset;
145-
read_request.buffer.remaining = readq->remaining;
146-
if (readq->size() > 0) {
147-
memcpy(read_request.buffer.buffer.get(), readq->vla, readq->size());
161+
if(readq->cap)
162+
{
163+
read_request = std::make_unique<ReadRequest>(readq->cap, readq->seq, nullptr);
164+
read_request->buffer.deserialize_from(readq);
148165
}
149166

150167
if (area->rtx_is_running) {
@@ -189,8 +206,26 @@ int Write_queue::serialize_to(void* addr) const
189206
return sizeof(serialized_writeq) + len;
190207
}
191208

209+
int Read_buffer::serialize_to(void* addr) const
210+
{
211+
auto& readbuf = *reinterpret_cast<read_buffer*>(addr);
212+
213+
readbuf.cap = this->cap;
214+
readbuf.seq = this->start;
215+
readbuf.head = this->head;
216+
readbuf.hole = this->hole;
217+
readbuf.push = this->push_seen;
218+
219+
std::copy(this->buf.get(), this->buf.get() + this->size(), readbuf.vla);
220+
221+
return sizeof(read_buffer) + this->size();
222+
}
223+
192224
int Connection::serialize_to(void* addr) const
193225
{
226+
if(this->VERSION != serialized_tcp::VERSION)
227+
throw std::runtime_error{"TCP Serialization version mismatch"};
228+
194229
auto* area = (serialized_tcp*) addr;
195230

196231
/// serialize TCP stuff
@@ -221,10 +256,7 @@ int Connection::serialize_to(void* addr) const
221256

222257
/// serialize read queue
223258
auto* readq = (read_buffer*) &area->vla[writeq_len];
224-
readq->remaining = read_request.buffer.remaining;
225-
readq->offset = read_request.buffer.offset;
226-
memcpy(readq->vla, read_request.buffer.buffer.get(), readq->size());
227-
int readq_len = sizeof(read_buffer) + readq->size();
259+
int readq_len = (read_request) ? read_request->buffer.serialize_to(readq) : sizeof(read_buffer);
228260

229261
//printf("READ: %u SEND: %u REMAIN: %u STATE: %s\n",
230262
// readq_size(), sendq_size(), sendq_remaining(), cb.to_string().c_str());

serialize_tcp.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
struct serialized_tcp
1010
{
11+
// has to match with tcp::Connection::VERSION
12+
static const int VERSION = 1;
13+
1114
typedef net::tcp::Connection Connection;
1215
typedef net::tcp::port_t port_t;
1316
typedef net::Socket Socket;

0 commit comments

Comments
 (0)