Merge remote-tracking branch 'origin/main'
# Conflicts: # include/Sockets/TcpSocket.hpp # main.cpp # src/Sockets/TcpSocket_Old.cpp
This commit is contained in:
164
include/Demos/MProto.hpp
Normal file
164
include/Demos/MProto.hpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#pragma once
|
||||
|
||||
#include <Sockets/Endianness.hpp>
|
||||
#include <Sockets/TcpSocket.hpp>
|
||||
#include <jlog/Logger.hpp>
|
||||
#include <sstream>
|
||||
//#include <fstream>
|
||||
//#include <iostream>
|
||||
|
||||
enum MProtoMessageID : u8 {
|
||||
THandShake = 100,
|
||||
RHandShake = 101,
|
||||
TPingPong = 102,
|
||||
RPingPong = 103,
|
||||
};
|
||||
|
||||
|
||||
struct MProtoCall {
|
||||
u8 type;
|
||||
std::string ping; // TPingPong, RPingPong
|
||||
};
|
||||
|
||||
const u32 MAXMSGLEN = 20;
|
||||
|
||||
|
||||
std::vector<u8> u16tou8(u16 u) {
|
||||
u8 n[2];
|
||||
*(u16*)&n = u;
|
||||
return {n[0], n[1]};
|
||||
};
|
||||
|
||||
std::vector<u8> u32tou8(u32 u) {
|
||||
u8 n[4];
|
||||
*(u32*)&n = u;
|
||||
return {n[0], n[1], n[2], n[3]};
|
||||
}
|
||||
|
||||
std::vector<u8> strtou8(std::string s) {
|
||||
return {s.begin(), s.end()};
|
||||
}
|
||||
|
||||
|
||||
std::vector<u8> MProtoCall_Serialize(MProtoCall mpc) {
|
||||
std::vector<u8> msg;
|
||||
//u8 sz[4];
|
||||
if (mpc.type == THandShake || mpc.type == RHandShake) {
|
||||
u32 sz = 1;
|
||||
sz = Socket::Endianness::HostToNetworkOrder(sz);
|
||||
std::vector<u8> szu8 = u32tou8(sz);
|
||||
msg.insert(msg.end(), szu8.begin(), szu8.end());
|
||||
msg.push_back(mpc.type);
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
if (mpc.type == TPingPong || mpc.type == RPingPong) {
|
||||
u32 sz = mpc.ping.size() + 2 + 1;
|
||||
u16 strsz = mpc.ping.size();
|
||||
|
||||
sz = Socket::Endianness::HostToNetworkOrder(sz);
|
||||
strsz = Socket::Endianness::HostToNetworkOrder(strsz);
|
||||
|
||||
std::vector<u8> szu8 = u32tou8(sz);
|
||||
std::vector<u8> strszu8 = u16tou8(strsz);
|
||||
std::vector<u8> stru8 = strtou8(mpc.ping);
|
||||
|
||||
msg.insert(msg.end(), szu8.begin(), szu8.end());
|
||||
msg.push_back(mpc.type);
|
||||
msg.insert(msg.end(), strszu8.begin(), strszu8.end());
|
||||
msg.insert(msg.end(), stru8.begin(), stru8.end());
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
class MProtoServer {
|
||||
private:
|
||||
struct MessageLogger : private jlog::ConsoleLogger {
|
||||
MessageLogger(std::string ctx, Color4 transcolor) : ConsoleLogger("MProto::" + ctx) {
|
||||
this->timestampColor = Colors::Gray;
|
||||
this->contextColor = transcolor;
|
||||
this->locationColor = Colors::Gray;
|
||||
this->pointerColor = transcolor;
|
||||
this->messageColor = Colors::White;
|
||||
this->IncludeLocation = false;
|
||||
}
|
||||
void operator() (std::string msg) {
|
||||
ConsoleLogger::Log(msg);
|
||||
}
|
||||
};
|
||||
|
||||
MessageLogger TLog {"T", Colors::Green};
|
||||
MessageLogger RLog {"R", Colors::Oranges::Coral};
|
||||
public:
|
||||
using Socket_p = std::shared_ptr<Socket::TcpSocket>;
|
||||
Socket_p sock;
|
||||
Socket_p client;
|
||||
public:
|
||||
MProtoServer(const char * ip) : sock(new Socket::TcpSocket(ip)) {}
|
||||
public:
|
||||
void Serve() {
|
||||
try {
|
||||
sock->Bind(42068);
|
||||
sock->Listen(5);
|
||||
client = sock->Accept();
|
||||
Accept();
|
||||
} catch(std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Accept() {
|
||||
sock->Close();
|
||||
u8 data[MAXMSGLEN];
|
||||
memset(&data, 0, MAXMSGLEN);
|
||||
while (client->Receive(data, MAXMSGLEN, 0)) {
|
||||
//std::cout << data << std::endl;
|
||||
//u32 sz = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
//std::cout << std::format("{}", Socket::Endianness::NetworkToHostOrder(sz)) << std::endl;
|
||||
//jlog::Debug("This dick");
|
||||
if (data[4] == THandShake) {
|
||||
TLog(std::format("Recieved: {} Code: {}", "THandShake", (u8)THandShake));
|
||||
//logger.RX("THandShake");
|
||||
memset(&data, 0, MAXMSGLEN);
|
||||
//data[0] = RHandShake;
|
||||
//logger.TX("RHandShake");
|
||||
//RLog(std::format("Sent: {} Code: {}", "RHandShake", (u8)RHandShake));
|
||||
//client->Send(data, sizeof data, 0);
|
||||
MProtoCall c{RHandShake};
|
||||
//MProtoCall c{RPingPong, "BALLS"};
|
||||
std::vector<u8> d = MProtoCall_Serialize(c);
|
||||
//std::cout << d.data() << std::endl;
|
||||
client->Send(d.data(), d.size(), 0);
|
||||
RLog(std::format("Sent: {} Code: {}", "RHandShake", (u8)RHandShake));
|
||||
} else if (data[4] == TPingPong) {
|
||||
TLog(std::format("Recieved: {} Code: {}", "TPingPong", (u8)TPingPong));
|
||||
//u32 sz = data[0] | data[1] | data[2] | data[3];
|
||||
//std::cout << std::format("{}", sz) << std::endl;
|
||||
u32 sz = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
sz = Socket::Endianness::NetworkToHostOrder(sz);
|
||||
|
||||
u16 strsz = data[5] | (data[6] << 8);
|
||||
strsz = Socket::Endianness::NetworkToHostOrder(strsz);
|
||||
|
||||
std::cout << std::format("cunt {} {}", sz, strsz);
|
||||
std::ostringstream str;
|
||||
for (u16 i=7; i<strsz+7; i++) {
|
||||
str << data[i];
|
||||
}
|
||||
std::cout << str.str() << std::endl;
|
||||
MProtoCall c{RPingPong, str.str()};
|
||||
std::vector<u8> d = MProtoCall_Serialize(c);
|
||||
client->Send(d.data(), d.size(), 0);
|
||||
RLog(std::format("Sent: {} Code: {}", "RPingPong", (u8)RPingPong));
|
||||
}
|
||||
memset(&data, 0, MAXMSGLEN);
|
||||
}
|
||||
|
||||
client->Close();
|
||||
}
|
||||
};
|
18
main.cpp
18
main.cpp
@@ -1,10 +1,10 @@
|
||||
#include <Sockets/TcpSocket_Old.hpp>
|
||||
#include <Sockets/TcpSocket.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include "Sockets/UdpSocket_Old.hpp"
|
||||
#include "Sockets/UdpSocket.hpp"
|
||||
#include <Sockets/Endianness.hpp>
|
||||
#include <jlog/Logger.hpp>
|
||||
#include <Event.h>
|
||||
@@ -43,12 +43,12 @@ using namespace Socket;
|
||||
class TCPServer {
|
||||
public:
|
||||
ServerLogger logger;
|
||||
using Socket_p = std::shared_ptr<TcpSocket_Old>;
|
||||
using Socket_p = std::shared_ptr<TcpSocket>;
|
||||
//Event<Socket_p> OnServe;
|
||||
Event<Socket_p> OnAccept;
|
||||
Event<Socket_p> OnRX;
|
||||
Event<Socket_p> OnTX;
|
||||
Socket_p sock {new TcpSocket_Old};
|
||||
Socket_p sock {new TcpSocket};
|
||||
Socket_p client;
|
||||
public:
|
||||
TCPServer() {};
|
||||
@@ -128,9 +128,9 @@ public:
|
||||
/// nc localhost 40269
|
||||
void TCP_ReceiveTest() {
|
||||
ServerLogger TCPLogger;
|
||||
using Socket_p = std::shared_ptr<TcpSocket_Old>;
|
||||
using Socket_p = std::shared_ptr<TcpSocket>;
|
||||
|
||||
Socket_p sock(new TcpSocket_Old);
|
||||
Socket_p sock(new TcpSocket);
|
||||
Socket_p client;
|
||||
|
||||
try {
|
||||
@@ -164,9 +164,9 @@ public:
|
||||
}
|
||||
|
||||
void UDP_ReceiveTest() {
|
||||
using UdpSocket_p = std::shared_ptr<UdpSocket_Old>;
|
||||
using UdpSocket_p = std::shared_ptr<UdpSocket>;
|
||||
|
||||
UdpSocket_p sock(new UdpSocket_Old);
|
||||
UdpSocket_p sock(new UdpSocket);
|
||||
|
||||
try {
|
||||
sock->Open();
|
||||
@@ -178,7 +178,7 @@ public:
|
||||
}
|
||||
|
||||
|
||||
UdpSocket_p client(new UdpSocket_Old);
|
||||
UdpSocket_p client(new UdpSocket);
|
||||
|
||||
try {
|
||||
client->Open();
|
||||
|
Reference in New Issue
Block a user