Compare commits

..

No commits in common. "e0423f9bfc75353461586948c1d65c4596aeb613" and "8bdf357e1a58784c1b46e7ff8886295381862da0" have entirely different histories.

5 changed files with 19 additions and 69 deletions

View File

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

View File

@ -5,6 +5,8 @@
#include <vector> #include <vector>
#include "Payload.h" #include "Payload.h"
class Payload;
class Connection class Connection
{ {
private: private:
@ -16,10 +18,7 @@ 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();
@ -27,8 +26,7 @@ public:
Connection(std::string server_ip, int port); Connection(std::string server_ip, int port);
~Connection(); ~Connection();
bool send(std::vector<Payload> payload, bool size); bool send(std::vector<Payload> payload);
bool send(Payload payload, bool size);
Payload recv(); Payload recv();
/** /**

View File

@ -1,23 +1,13 @@
#include "Payload.h" #include "Payload.h"
void Payload::push_char(unsigned char load){ void Payload::push_char(char load){
Data.push_back(load); Data.push_back(load);
} }
unsigned char* Payload::getData(){ 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,17 +7,15 @@
class Payload class Payload
{ {
private: private:
std::vector<unsigned char> Data; std::vector<char> Data;
public: public:
size_t getSize(); size_t getSize();
unsigned char* getData(); char* getData();
void setData(std::vector<unsigned char>); void push_char(char);
void push_char(unsigned char);
Payload(); Payload();
Payload(std::vector<unsigned char>); virtual ~Payload();
}; };
#endif /* PAYLOAD_H */ #endif /* PAYLOAD_H */

View File

@ -1,29 +1,8 @@
#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; Connection c("145.239.73.162", 25565);
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);
}