This commit is contained in:
malloc 2018-03-08 22:16:44 -06:00
parent 7301f20fb7
commit c62a73544b
4 changed files with 41 additions and 10 deletions

View file

@ -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.
* 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 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.
@ -472,4 +472,4 @@ The indexed list below indicates which byte (first byte being the MSB) contains
5. Minute quantifier. Ranges from 0 to 59.
6. Second quantifier. Ranges from 0 to 59.
In the event that an endpoint cannot evaluate a date required by the protocol as a result of some error, an error sockstamp will be sent in its place. An error sockstamp takes the form of zeroes in all bits. If an endpoint receives a sockstamp where the year quantifier is zero but any other quantifiers are nonzero, there is a communication error and the endpoint must close the connection.
In the event that an endpoint cannot evaluate a date required by the protocol as a result of some error, an error sockstamp will be sent in its place. An error sockstamp takes the form of zeroes in all bits. If an endpoint receives a sockstamp where the year quantifier is zero but any other quantifiers are nonzero, there is a communication error and the endpoint must close the connection.

View file

@ -1,12 +1,23 @@
#ifndef SOSC_INTSOCK_H
#define SOSC_INTSOCK_H
#include "tcpsock.hpp"
namespace sosc {
class IntraConnection {
public:
int Receive(Packet* packet, bool block = false);
bool Send(const Packet& packet);
void Close();
~IntraConnection();
private:
IntraConnection(TcpClient client);
TcpClient client;
std::string buffer;
friend class IntraServer;
};
class IntraServer {

View file

@ -22,33 +22,47 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) {
this->id = data[6];
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) {
if(ptr >= length)
return PCK_ERR;
uint32_t region_length;
switch(raw[ptr]) {
default:
region_lengths.push_back(raw[ptr]);
region_length = raw[ptr];
break;
case 254:
if(ptr + 2 >= length)
return PCK_ERR;
region_lengths.push_back(
net::ntohv<uint16_t>(data, ptr + 1)
);
region_length = net::ntohv<uint16_t>(data, ptr + 1);
break;
case 255:
if(ptr + 4 >= length)
return PCK_ERR;
region_lengths.push_back(
net::ntohv<uint32_t>(data, ptr + 1);
);
region_length = net::ntohv<uint32_t>(data, ptr + 1);
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, ...) {
@ -62,6 +76,7 @@ bool sosc::Packet::Check(int region_count, ...) {
if(length != PCK_ANY && this->regions[i].length() != length)
return false;
}
va_end(args);
return true;
}

View file

@ -1,6 +1,7 @@
#ifndef SOSC_PACKET_H
#define SOSC_PACKET_H
#include <numeric>
#include <vector>
#include <string>
#include <cstdarg>
@ -17,6 +18,10 @@
namespace sosc {
class Packet {
public:
void Clear();
void AddRegion(int index = -1);
std::string ToString() const;
int Parse(const std::string& data, std::string* extra = nullptr);
bool Check(int region_count, ...);