fixes
This commit is contained in:
parent
64739c39d8
commit
9bdf133d0f
12 changed files with 301 additions and 168 deletions
|
|
@ -32,6 +32,7 @@ module;
|
|||
#include <sys/ioctl.h>
|
||||
#include <netdb.h>
|
||||
#include <strings.h>
|
||||
#include <cerrno>
|
||||
|
||||
module Crafter.Network:ClientTCP_impl;
|
||||
import :ClientTCP;
|
||||
|
|
@ -72,7 +73,16 @@ ClientTCP::ClientTCP(std::string hostName, std::uint16_t port): ClientTCP(hostNa
|
|||
|
||||
ClientTCP::~ClientTCP()
|
||||
{
|
||||
if(socketid != 1) {
|
||||
shutdown(socketid, SHUT_RDWR);
|
||||
close(socketid);
|
||||
}
|
||||
}
|
||||
|
||||
void ClientTCP::Stop() {
|
||||
shutdown(socketid, SHUT_RDWR);
|
||||
close(socketid);
|
||||
socketid = -1;
|
||||
}
|
||||
|
||||
void ClientTCP::Send(const void* buffer, std::uint32_t size) const {
|
||||
|
|
@ -82,8 +92,10 @@ void ClientTCP::Send(const void* buffer, std::uint32_t size) const {
|
|||
std::vector<char> ClientTCP::RecieveSync(std::uint32_t bufferSize) const {
|
||||
std::vector<char> totalBuffer(bufferSize);
|
||||
int read = recv(socketid, totalBuffer.data(), bufferSize, 0);
|
||||
if(read < bufferSize){
|
||||
totalBuffer.resize(read);
|
||||
if(read < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
throw SocketClosedException();
|
||||
}
|
||||
return totalBuffer;
|
||||
}
|
||||
|
|
@ -94,22 +106,49 @@ int ClientTCP::RecieveSync(std::uint32_t bufferSize, void* buffer) const {
|
|||
std::vector<char> ClientTCP::RecieveSync() const {
|
||||
int count;
|
||||
ioctl(socketid, FIONREAD, &count);
|
||||
if(count == 0){
|
||||
count = 1024;
|
||||
}
|
||||
std::vector<char> buffer(count);
|
||||
recv(socketid, buffer.data(), count, 0);
|
||||
int read = recv(socketid, buffer.data(), count, 0);
|
||||
if(read < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
throw SocketClosedException();
|
||||
} else if(count != read) {
|
||||
buffer.resize(read);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::vector<char> ClientTCP::RecieveUntilCloseSync() const {
|
||||
int count;
|
||||
ioctl(socketid, FIONREAD, &count);
|
||||
if(count == 0){
|
||||
count = 1024;
|
||||
}
|
||||
std::vector<char> buffer(count);
|
||||
recv(socketid, buffer.data(), count, 0);
|
||||
int read = recv(socketid, buffer.data(), count, 0);
|
||||
if(read < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
throw SocketClosedException();
|
||||
}
|
||||
|
||||
while(true) {
|
||||
ioctl(socketid, FIONREAD, &count);
|
||||
if(count == 0){
|
||||
count = 1024;
|
||||
}
|
||||
unsigned int oldSize = buffer.size();
|
||||
buffer.resize(buffer.size()+count);
|
||||
if(recv(socketid, buffer.data()+oldSize, count, 0) == -1) {
|
||||
break;
|
||||
int read = recv(socketid, buffer.data()+oldSize, count, 0);
|
||||
if(read < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
return buffer;
|
||||
} else if(count != read) {
|
||||
buffer.resize(read);
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
|
|
@ -120,11 +159,12 @@ std::vector<char> ClientTCP::RecieveUntilFullSync(std::uint32_t bufferSize) cons
|
|||
int read = 0;
|
||||
while(read < bufferSize) {
|
||||
int newRead = recv(socketid, buffer.data()+read, bufferSize-read, 0);
|
||||
if(newRead == -1) {
|
||||
break;
|
||||
} else{
|
||||
read+=newRead;
|
||||
if(read < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
throw SocketClosedException();
|
||||
}
|
||||
read+=newRead;
|
||||
}
|
||||
buffer.resize(read);
|
||||
return buffer;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue