Compare commits

..

2 Commits

Author SHA1 Message Date
e0423f9bfc
. 2022-11-09 17:49:55 +01:00
15bb2e874c
Connection and payload quite done 2022-11-07 18:53:11 +01:00
5 changed files with 69 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,7 +16,10 @@ private:
socket_creation = 10, socket_creation = 10,
get_ip, get_ip,
connect, connect,
}error;
recv = 20,
send
}error = ErrorTypes::none;
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,23 @@
#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();
} }
Payload::Payload(){ }
Payload::Payload(std::vector<unsigned char> m_data)
: Data(m_data){}

View File

@ -7,15 +7,17 @@
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>);
void push_char(unsigned char);
Payload(); Payload();
virtual ~Payload(); Payload(std::vector<unsigned char>);
}; };
#endif /* PAYLOAD_H */ #endif /* PAYLOAD_H */

View File

@ -1,8 +1,29 @@
#include "Connection.h" #include "Connection.h"
#include "Payload.h"
#include <thread>
#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]));
}
if(c->status() != 0){
return 1;
}
std::vector<unsigned char> p_data{0xFE, 0x01, 0xFA, 0x00, 0x08, 0x00, 0x4D, 0x00, 0x43, 0x00, 0x7C, 0x00, 0x50, 0x00, 0x69, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x48, 0x00, 0x6F, 0x00, 0x73, 0x00, 0x74};
Payload p(p_data);
c->send(p, true);
Payload response = c->recv();
std::cout << response.getData() << std::endl;
return 0; return 0;
} }
void test_server(std::string addr, int port, Payload p){
Connection c(addr, port);
}