Connection and payload quite done
This commit is contained in:
parent
8bdf357e1a
commit
15bb2e874c
@ -47,22 +47,37 @@ Connection::~Connection(){
|
||||
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) {
|
||||
::send(fd, p.getData() , p.getSize(), 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Connection::send(Payload payload, bool size){
|
||||
return send(std::vector<Payload>{payload}, size);
|
||||
}
|
||||
|
||||
Payload Connection::recv(){
|
||||
Payload ret;
|
||||
char temp[100];
|
||||
memset(temp, 0, 100);
|
||||
ssize_t size;
|
||||
while((size = ::recv(fd, temp, sizeof(temp), 0)) > 0){
|
||||
for(char& in: temp){
|
||||
ret.push_char(in);
|
||||
|
||||
ssize_t size = 0, bytes;
|
||||
std::vector<unsigned char> received(100);
|
||||
while((bytes = ::recv(fd, received.data() + size, 100, 0)) > 0){
|
||||
size += bytes;
|
||||
received.resize(size+100);
|
||||
}
|
||||
if(bytes == 0){
|
||||
received.shrink_to_fit();
|
||||
ret.setData(received);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//Error Handling
|
||||
error = ErrorTypes::recv;
|
||||
return ret;
|
||||
}
|
||||
|
@ -5,8 +5,6 @@
|
||||
#include <vector>
|
||||
#include "Payload.h"
|
||||
|
||||
class Payload;
|
||||
|
||||
class Connection
|
||||
{
|
||||
private:
|
||||
@ -18,6 +16,9 @@ private:
|
||||
socket_creation = 10,
|
||||
get_ip,
|
||||
connect,
|
||||
|
||||
recv = 20,
|
||||
send
|
||||
}error;
|
||||
|
||||
void p_HandleError();
|
||||
@ -26,7 +27,8 @@ public:
|
||||
Connection(std::string server_ip, int port);
|
||||
~Connection();
|
||||
|
||||
bool send(std::vector<Payload> payload);
|
||||
bool send(std::vector<Payload> payload, bool size);
|
||||
bool send(Payload payload, bool size);
|
||||
Payload recv();
|
||||
|
||||
/**
|
||||
|
@ -1,13 +1,17 @@
|
||||
#include "Payload.h"
|
||||
|
||||
void Payload::push_char(char load){
|
||||
void Payload::push_char(unsigned char load){
|
||||
Data.push_back(load);
|
||||
}
|
||||
|
||||
char* Payload::getData(){
|
||||
unsigned char* Payload::getData(){
|
||||
return Data.data();
|
||||
}
|
||||
|
||||
void Payload::setData(std::vector<unsigned char> m_data){
|
||||
Data = m_data;
|
||||
}
|
||||
|
||||
size_t Payload::getSize(){
|
||||
return Data.size();
|
||||
}
|
||||
|
@ -7,15 +7,14 @@
|
||||
class Payload
|
||||
{
|
||||
private:
|
||||
std::vector<char> Data;
|
||||
std::vector<unsigned char> Data;
|
||||
public:
|
||||
size_t getSize();
|
||||
char* getData();
|
||||
unsigned char* getData();
|
||||
|
||||
void push_char(char);
|
||||
void setData(std::vector<unsigned char>);
|
||||
|
||||
Payload();
|
||||
virtual ~Payload();
|
||||
void push_char(unsigned char);
|
||||
};
|
||||
|
||||
#endif /* PAYLOAD_H */
|
||||
|
16
main.cpp
16
main.cpp
@ -1,8 +1,22 @@
|
||||
#include "Connection.h"
|
||||
#include "Payload.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user