whitespaces

This commit is contained in:
VaclavT 2022-01-16 20:47:55 +01:00
parent 796f6a5785
commit 7616553c63
1 changed files with 24 additions and 25 deletions

View File

@ -5,7 +5,7 @@
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netdb.h>
#include <cerrno>
#include <functional>
#include <stdexcept>
@ -25,15 +25,14 @@ void error(const char *msg) {
TcpNet::TcpNet() = default;
int TcpNet::server(int portno, const std::function<std::pair<bool, std::string>(std::string)>& process_request) const {
int TcpNet::server(int portno, const std::function<std::pair<bool, std::string>(std::string)> &process_request) const {
int sockfd, newsockfd;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
if (sockfd < 0)
error("ERROR opening socket");
memset((char *) &serv_addr, 0, sizeof(serv_addr));
@ -43,26 +42,26 @@ int TcpNet::server(int portno, const std::function<std::pair<bool, std::string>(
// this allows immediate bind after exit of ml
int reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse, sizeof(reuse)) < 0)
error("setsockopt(SO_REUSEADDR) failed");
#ifdef SO_REUSEPORT
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char *) &reuse, sizeof(reuse)) < 0)
error("setsockopt(SO_REUSEPORT) failed");
#endif
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
listen(sockfd, 5);
clilen = sizeof(cli_addr);
int requests_processed = 0;
bool shutdown = false;
while (!shutdown) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
if (newsockfd < 0)
error("ERROR on accept");
while (true) {
std::string request = read_from_socket(newsockfd);
if (request.empty()) break;
@ -78,14 +77,14 @@ int TcpNet::server(int portno, const std::function<std::pair<bool, std::string>(
error("ERROR writing to socket");
requests_processed++;
}
}
close(newsockfd);
}
close(sockfd);
return requests_processed;
return requests_processed;
}
@ -96,10 +95,10 @@ std::vector<std::string> TcpNet::client(const std::string &address, int portno,
std::vector<std::string> responses;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(address.c_str());
server = gethostbyname(address.c_str());
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
@ -107,15 +106,15 @@ std::vector<std::string> TcpNet::client(const std::string &address, int portno,
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
responses.reserve(requests.size());
for(const auto &req : requests) {
for (const auto &req : requests) {
write_to_socket(sockfd, req);
std::string response = read_from_socket(sockfd);
@ -154,17 +153,17 @@ std::string TcpNet::read_from_socket(int sockfd) {
std::string part{buffer};
request.append(part);
} while (n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
} while (n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
return request;
}
void TcpNet::write_to_socket(int sockfd, const std::string &str) {
const char * buffer = str.c_str();
const char *buffer = str.c_str();
int pos = 0;
long n;
do {
n = write(sockfd, buffer + pos, (int)(str.length() - pos));
n = write(sockfd, buffer + pos, (int) (str.length() - pos));
if (n < 0)
error("ERROR writing to socket");
} while (pos + n < str.length());