Connection and payload quite done

This commit is contained in:
2022-11-07 18:53:11 +01:00
parent 8bdf357e1a
commit 15bb2e874c
5 changed files with 53 additions and 19 deletions

View File

@@ -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;
}