Make threading and networking optional
This commit is contained in:
160
src/Streams/ByteReader.cpp
Normal file
160
src/Streams/ByteReader.cpp
Normal file
@ -0,0 +1,160 @@
|
||||
#include "TessesFramework/Streams/ByteReader.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
Stream* ByteReader::GetStream()
|
||||
{
|
||||
return this->strm;
|
||||
}
|
||||
ByteReader::ByteReader(Stream* strm, bool owns)
|
||||
{
|
||||
this->strm = strm;
|
||||
this->owns = owns;
|
||||
}
|
||||
ByteReader::ByteReader(Stream& strm) : ByteReader(&strm,false)
|
||||
{
|
||||
|
||||
}
|
||||
uint8_t ByteReader::ReadU8()
|
||||
{
|
||||
auto r = this->strm->ReadByte();
|
||||
if(r < 0) throw TextException("End of file");
|
||||
return (uint8_t)r;
|
||||
}
|
||||
uint16_t ByteReader::ReadU16BE()
|
||||
{
|
||||
uint8_t data[2];
|
||||
if(this->strm->ReadBlock(data,2) != 2) throw TextException("End of file");
|
||||
uint16_t n = 0;
|
||||
n |= (uint16_t)data[0] << 8;
|
||||
n |= (uint16_t)data[1];
|
||||
|
||||
return n;
|
||||
}
|
||||
uint16_t ByteReader::ReadU16LE()
|
||||
{
|
||||
uint8_t data[2];
|
||||
if(this->strm->ReadBlock(data,2) != 2) throw TextException("End of file");
|
||||
uint16_t n = 0;
|
||||
n |= (uint16_t)data[0];
|
||||
n |= (uint16_t)data[1] << 8;
|
||||
|
||||
|
||||
return n;
|
||||
}
|
||||
uint32_t ByteReader::ReadU32BE()
|
||||
{
|
||||
|
||||
uint8_t data[4];
|
||||
if(this->strm->ReadBlock(data,4) != 4) throw TextException("End of file");
|
||||
uint32_t n = 0;
|
||||
n |= (uint32_t)data[0] << 24;
|
||||
n |= (uint32_t)data[1] << 16;
|
||||
n |= (uint32_t)data[2] << 8;
|
||||
n |= (uint32_t)data[3];
|
||||
|
||||
return n;
|
||||
}
|
||||
uint32_t ByteReader::ReadU32LE()
|
||||
{
|
||||
uint8_t data[4];
|
||||
if(this->strm->ReadBlock(data,4) != 4) throw TextException("End of file");
|
||||
uint32_t n = 0;
|
||||
n |= (uint32_t)data[0];
|
||||
n |= (uint32_t)data[1] << 8;
|
||||
n |= (uint32_t)data[2] << 16;
|
||||
n |= (uint32_t)data[3] << 24;
|
||||
return n;
|
||||
}
|
||||
uint64_t ByteReader::ReadU64BE()
|
||||
{
|
||||
uint8_t data[8];
|
||||
if(this->strm->ReadBlock(data,8) != 8) throw TextException("End of file");
|
||||
uint64_t n = 0;
|
||||
n |= (uint64_t)data[0] << 56;
|
||||
n |= (uint64_t)data[1] << 48;
|
||||
n |= (uint64_t)data[2] << 40;
|
||||
n |= (uint64_t)data[3] << 32;
|
||||
n |= (uint64_t)data[4] << 24;
|
||||
n |= (uint64_t)data[5] << 16;
|
||||
n |= (uint64_t)data[6] << 8;
|
||||
n |= (uint64_t)data[7];
|
||||
|
||||
return n;
|
||||
}
|
||||
uint64_t ByteReader::ReadU64LE()
|
||||
{
|
||||
uint8_t data[8];
|
||||
if(this->strm->ReadBlock(data,8) != 8) throw TextException("End of file");
|
||||
uint64_t n = 0;
|
||||
n |= (uint64_t)data[0];
|
||||
n |= (uint64_t)data[1] << 8;
|
||||
n |= (uint64_t)data[2] << 16;
|
||||
n |= (uint64_t)data[3] << 24;
|
||||
n |= (uint64_t)data[4] << 32;
|
||||
n |= (uint64_t)data[5] << 40;
|
||||
n |= (uint64_t)data[6] << 48;
|
||||
n |= (uint64_t)data[7] << 56;
|
||||
|
||||
|
||||
return n;
|
||||
}
|
||||
int8_t ByteReader::ReadI8()
|
||||
{
|
||||
auto v=ReadU8();
|
||||
return *(int8_t*)&v;
|
||||
}
|
||||
int16_t ByteReader::ReadI16BE()
|
||||
{
|
||||
auto v=ReadU16BE();
|
||||
return *(int16_t*)&v;
|
||||
}
|
||||
int16_t ByteReader::ReadI16LE()
|
||||
{
|
||||
auto v=ReadU16BE();
|
||||
return *(int16_t*)&v;
|
||||
}
|
||||
int32_t ByteReader::ReadI32BE()
|
||||
{
|
||||
auto v=ReadU32BE();
|
||||
return *(int32_t*)&v;
|
||||
}
|
||||
int32_t ByteReader::ReadI32LE()
|
||||
{
|
||||
auto v=ReadU32LE();
|
||||
return *(int32_t*)&v;
|
||||
}
|
||||
int64_t ByteReader::ReadI64BE()
|
||||
{
|
||||
auto v=ReadU64BE();
|
||||
return *(int64_t*)&v;
|
||||
}
|
||||
int64_t ByteReader::ReadI64LE()
|
||||
{
|
||||
auto v=ReadU64LE();
|
||||
return *(int64_t*)&v;
|
||||
}
|
||||
float ByteReader::ReadF32BE()
|
||||
{
|
||||
auto v=ReadU32BE();
|
||||
return *(float*)&v;
|
||||
}
|
||||
float ByteReader::ReadF32LE()
|
||||
{
|
||||
auto v=ReadU32LE();
|
||||
return *(float*)&v;
|
||||
}
|
||||
double ByteReader::ReadF64BE()
|
||||
{
|
||||
auto v=ReadU64BE();
|
||||
return *(double*)&v;
|
||||
}
|
||||
double ByteReader::ReadF64LE()
|
||||
{
|
||||
auto v=ReadU64LE();
|
||||
return *(double*)&v;
|
||||
}
|
||||
ByteReader::~ByteReader()
|
||||
{
|
||||
if(this->owns) delete this->strm;
|
||||
}
|
||||
}
|
||||
140
src/Streams/ByteWriter.cpp
Normal file
140
src/Streams/ByteWriter.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
#include "TessesFramework/Streams/ByteWriter.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
Stream* ByteWriter::GetStream()
|
||||
{
|
||||
return this->strm;
|
||||
}
|
||||
ByteWriter::ByteWriter(Stream* strm, bool owns)
|
||||
{
|
||||
this->strm = strm;
|
||||
this->owns = owns;
|
||||
}
|
||||
ByteWriter::ByteWriter(Stream& strm) : ByteWriter(&strm,false)
|
||||
{
|
||||
|
||||
}
|
||||
void ByteWriter::WriteU8(uint8_t v)
|
||||
{
|
||||
strm->WriteByte(v);
|
||||
}
|
||||
void ByteWriter::WriteU16BE(uint16_t v)
|
||||
{
|
||||
uint8_t b[2];
|
||||
b[0] = (uint8_t)(v >> 8);
|
||||
b[1] = (uint8_t)v;
|
||||
strm->WriteBlock(b,2);
|
||||
}
|
||||
void ByteWriter::WriteU16LE(uint16_t v)
|
||||
{
|
||||
uint8_t b[2];
|
||||
b[0] = (uint8_t)v;
|
||||
b[1] = (uint8_t)(v >> 8);
|
||||
strm->WriteBlock(b,2);
|
||||
}
|
||||
void ByteWriter::WriteU32BE(uint32_t v)
|
||||
{
|
||||
uint8_t b[4];
|
||||
b[0] = (uint8_t)(v >> 24);
|
||||
b[1] = (uint8_t)(v >> 16);
|
||||
b[2] = (uint8_t)(v >> 8);
|
||||
b[3] = (uint8_t)v;
|
||||
strm->WriteBlock(b,4);
|
||||
}
|
||||
void ByteWriter::WriteU32LE(uint32_t v)
|
||||
{
|
||||
uint8_t b[4];
|
||||
b[0] = (uint8_t)v;
|
||||
b[1] = (uint8_t)(v >> 8);
|
||||
b[2] = (uint8_t)(v >> 16);
|
||||
b[3] = (uint8_t)(v >> 24);
|
||||
|
||||
strm->WriteBlock(b,4);
|
||||
}
|
||||
void ByteWriter::WriteU64BE(uint64_t v)
|
||||
{
|
||||
uint8_t b[8];
|
||||
b[0] = (uint8_t)(v >> 56);
|
||||
b[1] = (uint8_t)(v >> 48);
|
||||
b[2] = (uint8_t)(v >> 40);
|
||||
b[3] = (uint8_t)(v >> 32);
|
||||
b[4] = (uint8_t)(v >> 24);
|
||||
b[5] = (uint8_t)(v >> 16);
|
||||
b[6] = (uint8_t)(v >> 8);
|
||||
b[7] = (uint8_t)v;
|
||||
strm->WriteBlock(b,8);
|
||||
}
|
||||
void ByteWriter::WriteU64LE(uint64_t v)
|
||||
{
|
||||
uint8_t b[8];
|
||||
|
||||
b[0] = (uint8_t)v;
|
||||
b[1] = (uint8_t)(v >> 8);
|
||||
b[2] = (uint8_t)(v >> 16);
|
||||
b[3] = (uint8_t)(v >> 24);
|
||||
b[4] = (uint8_t)(v >> 32);
|
||||
b[5] = (uint8_t)(v >> 40);
|
||||
b[6] = (uint8_t)(v >> 48);
|
||||
b[7] = (uint8_t)(v >> 56);
|
||||
strm->WriteBlock(b,8);
|
||||
}
|
||||
void ByteWriter::WriteI8(int8_t v)
|
||||
{
|
||||
uint8_t data = *(uint8_t*)&v;
|
||||
WriteU8(data);
|
||||
}
|
||||
void ByteWriter::WriteI16BE(int16_t v)
|
||||
{
|
||||
uint16_t data = *(uint16_t*)&v;
|
||||
WriteU16BE(data);
|
||||
}
|
||||
void ByteWriter::WriteI16LE(int16_t v)
|
||||
{
|
||||
uint16_t data = *(uint16_t*)&v;
|
||||
WriteU16LE(data);
|
||||
}
|
||||
void ByteWriter::WriteI32BE(int32_t v)
|
||||
{
|
||||
uint32_t data = *(uint32_t*)&v;
|
||||
WriteU32BE(data);
|
||||
}
|
||||
void ByteWriter::WriteI32LE(int32_t v)
|
||||
{
|
||||
uint32_t data = *(uint32_t*)&v;
|
||||
WriteU32LE(data);
|
||||
}
|
||||
void ByteWriter::WriteI64BE(int64_t v)
|
||||
{
|
||||
uint64_t data = *(uint64_t*)&v;
|
||||
WriteU64BE(data);
|
||||
}
|
||||
void ByteWriter::WriteI64LE(int64_t v)
|
||||
{
|
||||
uint64_t data = *(uint64_t*)&v;
|
||||
WriteU64LE(data);
|
||||
}
|
||||
void ByteWriter::WriteF32BE(float v)
|
||||
{
|
||||
uint32_t data = *(uint32_t*)&v;
|
||||
WriteU32BE(data);
|
||||
}
|
||||
void ByteWriter::WriteF32LE(float v)
|
||||
{
|
||||
uint32_t data = *(uint32_t*)&v;
|
||||
WriteU32LE(data);
|
||||
}
|
||||
void ByteWriter::WriteF64BE(double v)
|
||||
{
|
||||
uint64_t data = *(uint64_t*)&v;
|
||||
WriteU64BE(data);
|
||||
}
|
||||
void ByteWriter::WriteF64LE(double v)
|
||||
{
|
||||
uint64_t data = *(uint64_t*)&v;
|
||||
WriteU64LE(data);
|
||||
}
|
||||
ByteWriter::~ByteWriter()
|
||||
{
|
||||
if(this->owns) delete this->strm;
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,9 @@
|
||||
#include "TessesFramework/Http/HttpUtils.hpp"
|
||||
|
||||
using HttpUtils = Tesses::Framework::Http::HttpUtils;
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_NETWORKING)
|
||||
|
||||
|
||||
#if defined(GEKKO)
|
||||
|
||||
#define ss_family sin_family
|
||||
@ -561,3 +564,110 @@ namespace Tesses::Framework::Streams {
|
||||
NETWORK_SETSOCKOPT(this->sock, SOL_SOCKET, TCP_NODELAY, (const char*)&noDelay2,(socklen_t)sizeof(noDelay2));
|
||||
}
|
||||
}
|
||||
#else
|
||||
namespace Tesses::Framework::Streams {
|
||||
class TcpServer {
|
||||
int32_t sock;
|
||||
bool owns;
|
||||
bool valid;
|
||||
public:
|
||||
TcpServer(int32_t sock,bool owns);
|
||||
TcpServer(uint16_t port, int32_t backlog);
|
||||
TcpServer(std::string ip, uint16_t port, int32_t backlog);
|
||||
NetworkStream* GetStream(std::string& ip, uint16_t& port);
|
||||
~TcpServer();
|
||||
void Close();
|
||||
};
|
||||
TcpServer::TcpServer(int32_t sock,bool owns)
|
||||
{
|
||||
|
||||
}
|
||||
TcpServer::TcpServer(uint16_t port, int32_t backlog)
|
||||
{
|
||||
|
||||
}
|
||||
TcpServer::TcpServer(std::string ip, uint16_t port, int32_t backlog)
|
||||
{
|
||||
|
||||
}
|
||||
NetworkStream* TcpServer::GetStream(std::string& ip, uint16_t& port)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
TcpServer::~TcpServer()
|
||||
{
|
||||
|
||||
}
|
||||
void TcpServer::Close()
|
||||
{
|
||||
|
||||
}
|
||||
bool NetworkStream::EndOfStream() {
|
||||
return true;
|
||||
}
|
||||
bool NetworkStream::CanRead() {
|
||||
return false;
|
||||
}
|
||||
bool NetworkStream::CanWrite() {
|
||||
return false;
|
||||
}
|
||||
|
||||
NetworkStream::NetworkStream(bool ipV6,bool datagram)
|
||||
{
|
||||
|
||||
}
|
||||
NetworkStream::NetworkStream(std::string ipOrFqdn, uint16_t port, bool datagram,bool broadcast,bool supportIPv6)
|
||||
{
|
||||
|
||||
}
|
||||
NetworkStream::NetworkStream(int32_t sock, bool owns)
|
||||
{
|
||||
|
||||
}
|
||||
void NetworkStream::Listen(int32_t backlog)
|
||||
{
|
||||
|
||||
}
|
||||
void NetworkStream::Bind(std::string ip, uint16_t port)
|
||||
{
|
||||
|
||||
}
|
||||
void NetworkStream::SetBroadcast(bool bC)
|
||||
{
|
||||
|
||||
}
|
||||
NetworkStream* NetworkStream::Accept(std::string& ip, uint16_t& port)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
size_t NetworkStream::Read(uint8_t* buff, size_t sz)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t NetworkStream::Write(const uint8_t* buff, size_t sz)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t NetworkStream::ReadFrom(uint8_t* buff, size_t sz, std::string& ip, uint16_t& port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t NetworkStream::WriteTo(const uint8_t* buff, size_t sz, std::string ip, uint16_t port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
std::vector<std::pair<std::string,std::string>> NetworkStream::GetIPs(bool ipV6)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
NetworkStream::~NetworkStream()
|
||||
{
|
||||
|
||||
}
|
||||
void NetworkStream::SetNoDelay(bool noDelay)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user