diff --git a/protocol.md b/protocol.md index 0b6559f..6f806e5 100644 --- a/protocol.md +++ b/protocol.md @@ -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. \ No newline at end of file +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. diff --git a/server/src/sock/intrasock.hpp b/server/src/sock/intrasock.hpp index ee51766..22ec09f 100644 --- a/server/src/sock/intrasock.hpp +++ b/server/src/sock/intrasock.hpp @@ -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 { diff --git a/server/src/sock/packet.cpp b/server/src/sock/packet.cpp index 20fce5c..8156baf 100644 --- a/server/src/sock/packet.cpp +++ b/server/src/sock/packet.cpp @@ -22,33 +22,47 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) { this->id = data[6]; std::vector 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(data, ptr + 1) - ); + region_length = net::ntohv(data, ptr + 1); break; case 255: if(ptr + 4 >= length) return PCK_ERR; - region_lengths.push_back( - net::ntohv(data, ptr + 1); - ); + region_length = net::ntohv(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; } diff --git a/server/src/sock/packet.hpp b/server/src/sock/packet.hpp index 4790ebb..c5192e6 100644 --- a/server/src/sock/packet.hpp +++ b/server/src/sock/packet.hpp @@ -1,6 +1,7 @@ #ifndef SOSC_PACKET_H #define SOSC_PACKET_H +#include #include #include #include @@ -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, ...);