fixes
This commit is contained in:
parent
9bdf133d0f
commit
c49f947a9b
6 changed files with 123 additions and 21 deletions
|
|
@ -55,7 +55,8 @@ HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) {
|
|||
try {
|
||||
buffer = client.RecieveSync();
|
||||
} catch(const SocketClosedException& e) {
|
||||
client = ClientTCP(host.c_str(), port);
|
||||
client.Stop();
|
||||
client.Connect();
|
||||
client.Send(request, length);
|
||||
buffer = client.RecieveSync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,22 +48,13 @@ ClientTCP::ClientTCP(int socketid) : socketid(socketid)
|
|||
|
||||
ClientTCP::ClientTCP(const char* hostName, std::uint16_t port)
|
||||
{
|
||||
hostent *host;
|
||||
sockaddr_in serv_addr;
|
||||
|
||||
host=gethostbyname(hostName);
|
||||
if((socketid = socket(AF_INET, SOCK_STREAM, 0)) == -1){
|
||||
std::cerr << "Could not open socket" << std::endl;
|
||||
}
|
||||
|
||||
host = gethostbyname(hostName);
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(port);
|
||||
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
|
||||
bzero(&(serv_addr.sin_zero),8);
|
||||
|
||||
if(connect(socketid,(sockaddr*)&serv_addr, sizeof(sockaddr)) == -1){
|
||||
std::cerr << "Could not connect to server" << std::endl;
|
||||
}
|
||||
Connect();
|
||||
}
|
||||
|
||||
ClientTCP::ClientTCP(std::string hostName, std::uint16_t port): ClientTCP(hostName.c_str(), port)
|
||||
|
|
@ -71,6 +62,14 @@ ClientTCP::ClientTCP(std::string hostName, std::uint16_t port): ClientTCP(hostNa
|
|||
|
||||
}
|
||||
|
||||
ClientTCP::ClientTCP(ClientTCP&& other) noexcept : socketid(other.socketid) {
|
||||
if(socketid != 1) {
|
||||
shutdown(socketid, SHUT_RDWR);
|
||||
close(socketid);
|
||||
}
|
||||
other.socketid = -1;
|
||||
}
|
||||
|
||||
ClientTCP::~ClientTCP()
|
||||
{
|
||||
if(socketid != 1) {
|
||||
|
|
@ -79,6 +78,16 @@ ClientTCP::~ClientTCP()
|
|||
}
|
||||
}
|
||||
|
||||
void ClientTCP::Connect() {
|
||||
if((socketid = socket(AF_INET, SOCK_STREAM, 0)) == -1){
|
||||
std::cerr << "Could not open socket" << std::endl;
|
||||
}
|
||||
|
||||
if(connect(socketid,(sockaddr*)&serv_addr, sizeof(sockaddr)) == -1){
|
||||
std::cerr << "Could not connect to server" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ClientTCP::Stop() {
|
||||
shutdown(socketid, SHUT_RDWR);
|
||||
close(socketid);
|
||||
|
|
@ -86,9 +95,23 @@ void ClientTCP::Stop() {
|
|||
}
|
||||
|
||||
void ClientTCP::Send(const void* buffer, std::uint32_t size) const {
|
||||
send(socketid, reinterpret_cast<const char*>(buffer), size, 0);
|
||||
}
|
||||
const char* data = reinterpret_cast<const char*>(buffer);
|
||||
std::uint_fast32_t totalSent = 0;
|
||||
const std::uint_fast32_t chunkSize = 1024;
|
||||
|
||||
while (totalSent < size) {
|
||||
std::uint_fast32_t bytesToSend = std::min(chunkSize, size - totalSent);
|
||||
int status = send(socketid, data + totalSent, bytesToSend, 0);
|
||||
|
||||
if (status == 0) {
|
||||
throw SocketClosedException();
|
||||
} else if (status < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
}
|
||||
|
||||
totalSent += status;
|
||||
}
|
||||
}
|
||||
std::vector<char> ClientTCP::RecieveSync(std::uint32_t bufferSize) const {
|
||||
std::vector<char> totalBuffer(bufferSize);
|
||||
int read = recv(socketid, totalBuffer.data(), bufferSize, 0);
|
||||
|
|
@ -159,9 +182,9 @@ 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(read < 0) {
|
||||
if(newRead < 0) {
|
||||
throw std::runtime_error(std::strerror(errno));
|
||||
} else if(read == 0) {
|
||||
} else if(newRead == 0) {
|
||||
throw SocketClosedException();
|
||||
}
|
||||
read+=newRead;
|
||||
|
|
|
|||
|
|
@ -123,8 +123,8 @@ void ListenerHTTPClient::ListenRoutes() {
|
|||
std::uint32_t i = 0;
|
||||
std::uint32_t routeStart = 0;
|
||||
while(true) {
|
||||
buffer = client.RecieveSync();
|
||||
while(true) {
|
||||
buffer = client.RecieveSync();
|
||||
std::string str(buffer.begin(), buffer.end());
|
||||
for(; i < buffer.size(); i++) {
|
||||
if(buffer[i] == ' ') {
|
||||
|
|
@ -184,7 +184,7 @@ void ListenerHTTPClient::ListenRoutes() {
|
|||
const int lenght = std::stoi(it->second);
|
||||
request.body.resize(lenght, 0);
|
||||
if(lenght > 0 ){
|
||||
std::int_fast32_t remaining = lenght+i-buffer.size();
|
||||
std::int_fast32_t remaining = lenght-(buffer.size()-i);
|
||||
if(remaining < 0) {
|
||||
std::memcpy(&request.body[0], buffer.data()+i, lenght);
|
||||
std::string response = server->routes.at(route)(request);
|
||||
|
|
@ -196,9 +196,9 @@ void ListenerHTTPClient::ListenRoutes() {
|
|||
client.Send(&response[0], response.size());
|
||||
break;
|
||||
} else {
|
||||
std::memcpy(&request.body[0], buffer.data()+i, lenght-remaining);
|
||||
std::memcpy(&request.body[0], buffer.data()+i, buffer.size()-i);
|
||||
std::vector<char> bodyBuffer = client.RecieveUntilFullSync(remaining);
|
||||
std::memcpy(&request.body[remaining], bodyBuffer.data(), remaining);
|
||||
std::memcpy(&request.body[buffer.size()-i], bodyBuffer.data(), remaining);
|
||||
std::string response = server->routes.at(route)(request);
|
||||
client.Send(&response[0], response.size());
|
||||
break;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue