Connection and payload quite done

This commit is contained in:
Debucquoy Anthony 2022-11-07 18:53:11 +01:00
parent 8bdf357e1a
commit 15bb2e874c
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
5 changed files with 53 additions and 19 deletions

View File

@ -47,22 +47,37 @@ Connection::~Connection(){
close(fd); close(fd);
} }
bool Connection::send(std::vector<Payload> payloads){ bool Connection::send(std::vector<Payload> payloads, bool size){
if(size){
unsigned char _size = payloads.size();
::send(fd, &_size , sizeof(_size), 0);
}
for (Payload& p : payloads) { for (Payload& p : payloads) {
::send(fd, p.getData() , p.getSize(), 0); ::send(fd, p.getData() , p.getSize(), 0);
} }
return true; return true;
} }
bool Connection::send(Payload payload, bool size){
return send(std::vector<Payload>{payload}, size);
}
Payload Connection::recv(){ Payload Connection::recv(){
Payload ret; Payload ret;
char temp[100];
memset(temp, 0, 100); ssize_t size = 0, bytes;
ssize_t size; std::vector<unsigned char> received(100);
while((size = ::recv(fd, temp, sizeof(temp), 0)) > 0){ while((bytes = ::recv(fd, received.data() + size, 100, 0)) > 0){
for(char& in: temp){ size += bytes;
ret.push_char(in); received.resize(size+100);
}
} }
if(bytes == 0){
received.shrink_to_fit();
ret.setData(received);
return ret;
}
//Error Handling
error = ErrorTypes::recv;
return ret; return ret;
} }

View File

@ -5,8 +5,6 @@
#include <vector> #include <vector>
#include "Payload.h" #include "Payload.h"
class Payload;
class Connection class Connection
{ {
private: private:
@ -18,6 +16,9 @@ private:
socket_creation = 10, socket_creation = 10,
get_ip, get_ip,
connect, connect,
recv = 20,
send
}error; }error;
void p_HandleError(); void p_HandleError();
@ -26,7 +27,8 @@ public:
Connection(std::string server_ip, int port); Connection(std::string server_ip, int port);
~Connection(); ~Connection();
bool send(std::vector<Payload> payload); bool send(std::vector<Payload> payload, bool size);
bool send(Payload payload, bool size);
Payload recv(); Payload recv();
/** /**

View File

@ -1,13 +1,17 @@
#include "Payload.h" #include "Payload.h"
void Payload::push_char(char load){ void Payload::push_char(unsigned char load){
Data.push_back(load); Data.push_back(load);
} }
char* Payload::getData(){ unsigned char* Payload::getData(){
return Data.data(); return Data.data();
} }
void Payload::setData(std::vector<unsigned char> m_data){
Data = m_data;
}
size_t Payload::getSize(){ size_t Payload::getSize(){
return Data.size(); return Data.size();
} }

View File

@ -7,15 +7,14 @@
class Payload class Payload
{ {
private: private:
std::vector<char> Data; std::vector<unsigned char> Data;
public: public:
size_t getSize(); size_t getSize();
char* getData(); unsigned char* getData();
void push_char(char); void setData(std::vector<unsigned char>);
Payload(); void push_char(unsigned char);
virtual ~Payload();
}; };
#endif /* PAYLOAD_H */ #endif /* PAYLOAD_H */

View File

@ -1,8 +1,22 @@
#include "Connection.h" #include "Connection.h"
#include "Payload.h"
#include <iostream>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Connection c("145.239.73.162", 25565); Connection* c;
if(argc == 1){
c = new Connection("145.239.73.162", 25565);
}else{
c = new Connection(argv[1], atoi(argv[2]));
}
Payload p;
p.push_char(0xFE);
p.push_char(0x01);
p.push_char(0xFA);
c->send(p, true);
Payload response = c->recv();
std::cout << response.getData() << std::endl;
return 0; return 0;
} }