Commits (1)
#include "buffer.h"
#include <iostream>
int get_socket_id(BinaryReader &br, int max_count) {
std::string ip = br.read_str();
int get_socket_id(const std::string &ip, int max_count) {
if (ip.find("2::") != 0) {
std::cerr << "Unsupported IP: '"<< ip << "' (only supports 'N-to-N' IPs with '2::' prefix)" << std::endl;
}
......@@ -18,7 +17,7 @@ void DynamicBuffer::check_size(int size) {
if (size >= buffer_size) {
int32_t new_size = ((size / START_BUFFER_SIZE) + 1) * START_BUFFER_SIZE;
char *new_buff = new char[new_size];
for (int32_t i = 0; i < buffer_size; ++i){
for (int32_t i = 0; i < pos; ++i){
new_buff[i] = buffer[i];
}
delete[] buffer;
......@@ -33,7 +32,7 @@ uint64_t BinaryReader::read_u64() {
uint64_t t = 0;
for (int i = 0; i < 8; ++i) {
t <<= 8;
t |= ((uint8_t*)pos)[i];
t |= ((uint8_t*)(pos))[i];
}
pos += 8;
return t;
......@@ -96,7 +95,7 @@ void BinaryWriter::write_u16(uint16_t value) {
((uint8_t*)pos)[1] = (uint8_t) value;
}
void BinaryWriter::write_u8(uint8_t value) {
auto pos = buffer.push_slot(2);
auto pos = buffer.push_slot(1);
*((uint8_t*)pos) = value;
}
void BinaryWriter::write_f64(double value) {
......
......@@ -4,19 +4,15 @@
#include <exception>
struct DynamicBuffer {
static constexpr int32_t START_BUFFER_SIZE = 1024;
char *buffer = nullptr;
char* pos = nullptr;
int32_t buffer_size = 0;
DynamicBuffer() {
buffer = new char[START_BUFFER_SIZE];
buffer_size = START_BUFFER_SIZE;
reset();
pos = 0;
}
void reset() {
pos = buffer;
pos = 0;
}
......@@ -24,9 +20,9 @@ struct DynamicBuffer {
append((char) c);
}
void append(const char c) {
*pos = c;
buffer[pos] = c;
++pos;
check_size(position());
check_size(pos);
}
void append(const char* str) {
while (*str){
......@@ -36,35 +32,46 @@ struct DynamicBuffer {
// Must be a valid index
void go_to(int index) {
this->pos = buffer + index;
this->pos = index;
}
const char *as_terminated_string() {
*pos = '\0';
buffer[pos] = '\0';
return buffer;
}
// Returns the current position in the buffer and increases its content (pos) by "bytes"
char *push_slot(int bytes) {
auto t = pos;
pos += bytes;
check_size(position());
return t;
auto new_pos = pos+bytes;
check_size(new_pos);
pos = new_pos;
return buffer+t;
}
int position() {
return pos-buffer;
return pos;
}
void reserve(int size) {
check_size(size);
}
char *get_buffer() {
return buffer;
}
~DynamicBuffer(){
if (buffer != nullptr) delete[] buffer;
}
private:
static constexpr int32_t START_BUFFER_SIZE = 1024;
char *buffer;
int32_t pos;
int32_t buffer_size;
void check_size(int size);
};
......@@ -112,4 +119,4 @@ public:
}
};
int get_socket_id(BinaryReader &br, int max_count);
\ No newline at end of file
int get_socket_id(const std::string &ip, int max_count);
\ No newline at end of file
......@@ -12,8 +12,6 @@
char JsonWriter::LOCAL_BUFFER[JsonWriter::LOCAL_BUFFER_SIZE];
void JsonWriter::start_object() {
separate();
if (format) offset += TAB;
......
......@@ -84,11 +84,11 @@ void PacketReader::receive() {
//cout << "Waiting for packet header " << endl;
id = -1;
size = 0;
BinaryReader br = {buffer.buffer, 3};
BinaryReader br = {buffer.get_buffer(), 3};
buffer.reserve(3);
size = 3;
socket.read_s(buffer.buffer, 3);
socket.read_s(buffer.get_buffer(), 3);
id = br.read_u8();
auto length = br.read_u16();
//cout << "Got packet header: id=" << id << ", size=" << length << endl;
......@@ -97,7 +97,7 @@ void PacketReader::receive() {
buffer.reserve(size);
// Read data
socket.read_s(buffer.buffer+3, length);
socket.read_s(buffer.get_buffer()+3, length);
}
......@@ -107,7 +107,7 @@ void PacketWriter::send(const Socket &socket) {
auto payload_size = size-3;
buffer.go_to(1);
bw.write_u16(payload_size);
socket.write_s(buffer.buffer, size);
socket.write_s(buffer.get_buffer(), size);
}
......
......@@ -15,7 +15,7 @@ void close_sock(int sock);
class Socket {
int socket;
int max_msg_size;
int64_t max_msg_size;
public:
Socket(int socket) : socket(socket) {
get_max_msg_size();
......@@ -44,11 +44,11 @@ struct PacketReader {
PacketReader(DynamicBuffer &buffer, const Socket &socket) : buffer(buffer), id(-1), size(0), socket(socket) {}
void receive();
BinaryReader getReader() {
return {buffer.buffer+3, payload_size()};
return {buffer.get_buffer()+3, payload_size()};
}
const char* getPayload() {
buffer.buffer[size] = '\0';
return buffer.buffer + 3;
buffer.get_buffer()[size] = '\0';
return buffer.get_buffer() + 3;
}
int payload_size() {
......