smol
This commit is contained in:
parent
7301f20fb7
commit
c62a73544b
4 changed files with 41 additions and 10 deletions
|
@ -10,7 +10,7 @@ All references to the 'byte' in this document refers to individual 8-bit octets,
|
||||||
|
|
||||||
Because the body of the packet is a sequence of many different regions of byte data that is not delimited, it is necessary for the header of the packet to determine boundaries for the regions of data.
|
Because the body of the packet is a sequence of many different regions of byte data that is not delimited, it is necessary for the header of the packet to determine boundaries for the regions of data.
|
||||||
|
|
||||||
* The first two bytes will always be 0xB0 and 0X0B. If this is not set properly, the endpoint must close the connection.
|
* The first two bytes will always be 0xB0 and 0x0B. If this is not set properly, the endpoint must close the connection.
|
||||||
* The next four bytes are the total length of the entire packet, including the whole header.
|
* The next four bytes are the total length of the entire packet, including the whole header.
|
||||||
* The seventh byte is the packet id, the meanings of which are defined in the [_Packet IDs_](#packet-ids) section.
|
* The seventh byte is the packet id, the meanings of which are defined in the [_Packet IDs_](#packet-ids) section.
|
||||||
* The eighth byte is the number of byte regions in the packet.
|
* The eighth byte is the number of byte regions in the packet.
|
||||||
|
|
|
@ -1,12 +1,23 @@
|
||||||
#ifndef SOSC_INTSOCK_H
|
#ifndef SOSC_INTSOCK_H
|
||||||
#define SOSC_INTSOCK_H
|
#define SOSC_INTSOCK_H
|
||||||
|
|
||||||
|
#include "tcpsock.hpp"
|
||||||
|
|
||||||
namespace sosc {
|
namespace sosc {
|
||||||
class IntraConnection {
|
class IntraConnection {
|
||||||
public:
|
public:
|
||||||
|
int Receive(Packet* packet, bool block = false);
|
||||||
|
bool Send(const Packet& packet);
|
||||||
|
|
||||||
|
void Close();
|
||||||
|
~IntraConnection();
|
||||||
private:
|
private:
|
||||||
|
IntraConnection(TcpClient client);
|
||||||
|
|
||||||
|
TcpClient client;
|
||||||
|
std::string buffer;
|
||||||
|
|
||||||
|
friend class IntraServer;
|
||||||
};
|
};
|
||||||
|
|
||||||
class IntraServer {
|
class IntraServer {
|
||||||
|
|
|
@ -22,33 +22,47 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) {
|
||||||
|
|
||||||
this->id = data[6];
|
this->id = data[6];
|
||||||
std::vector<uint32_t> region_lengths;
|
std::vector<uint32_t> region_lengths;
|
||||||
int region_count = data[7], ptr = 8;
|
uint32_t region_count = data[7],
|
||||||
|
ptr = 8, body_length = 0;
|
||||||
for(int i = 0; i < region_count; ++i) {
|
for(int i = 0; i < region_count; ++i) {
|
||||||
if(ptr >= length)
|
if(ptr >= length)
|
||||||
return PCK_ERR;
|
return PCK_ERR;
|
||||||
|
|
||||||
|
uint32_t region_length;
|
||||||
switch(raw[ptr]) {
|
switch(raw[ptr]) {
|
||||||
default:
|
default:
|
||||||
region_lengths.push_back(raw[ptr]);
|
region_length = raw[ptr];
|
||||||
break;
|
break;
|
||||||
case 254:
|
case 254:
|
||||||
if(ptr + 2 >= length)
|
if(ptr + 2 >= length)
|
||||||
return PCK_ERR;
|
return PCK_ERR;
|
||||||
|
|
||||||
region_lengths.push_back(
|
region_length = net::ntohv<uint16_t>(data, ptr + 1);
|
||||||
net::ntohv<uint16_t>(data, ptr + 1)
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case 255:
|
case 255:
|
||||||
if(ptr + 4 >= length)
|
if(ptr + 4 >= length)
|
||||||
return PCK_ERR;
|
return PCK_ERR;
|
||||||
|
|
||||||
region_lengths.push_back(
|
region_length = net::ntohv<uint32_t>(data, ptr + 1);
|
||||||
net::ntohv<uint32_t>(data, ptr + 1);
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
region_lengths.push_back(region_length);
|
||||||
|
body_length += region_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(body_length - ptr != 0)
|
||||||
|
return PCK_ERR;
|
||||||
|
|
||||||
|
for(int i = 0; i < region_count; ++i) {
|
||||||
|
this->regions[i] = data.substr(ptr, region_lengths[i]);
|
||||||
|
ptr += region_lengths[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(length > expected_length)
|
||||||
|
*extra = data.substr(expected_length);
|
||||||
|
|
||||||
|
return PCK_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sosc::Packet::Check(int region_count, ...) {
|
bool sosc::Packet::Check(int region_count, ...) {
|
||||||
|
@ -62,6 +76,7 @@ bool sosc::Packet::Check(int region_count, ...) {
|
||||||
if(length != PCK_ANY && this->regions[i].length() != length)
|
if(length != PCK_ANY && this->regions[i].length() != length)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef SOSC_PACKET_H
|
#ifndef SOSC_PACKET_H
|
||||||
#define SOSC_PACKET_H
|
#define SOSC_PACKET_H
|
||||||
|
|
||||||
|
#include <numeric>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
@ -17,6 +18,10 @@
|
||||||
namespace sosc {
|
namespace sosc {
|
||||||
class Packet {
|
class Packet {
|
||||||
public:
|
public:
|
||||||
|
void Clear();
|
||||||
|
void AddRegion(int index = -1);
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
int Parse(const std::string& data, std::string* extra = nullptr);
|
int Parse(const std::string& data, std::string* extra = nullptr);
|
||||||
bool Check(int region_count, ...);
|
bool Check(int region_count, ...);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue