whitespaces
This commit is contained in:
parent
796f6a5785
commit
7616553c63
|
|
@ -25,8 +25,7 @@ void error(const char *msg) {
|
||||||
TcpNet::TcpNet() = default;
|
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;
|
int sockfd, newsockfd;
|
||||||
socklen_t clilen;
|
socklen_t clilen;
|
||||||
|
|
||||||
|
|
@ -43,17 +42,17 @@ int TcpNet::server(int portno, const std::function<std::pair<bool, std::string>(
|
||||||
|
|
||||||
// this allows immediate bind after exit of ml
|
// this allows immediate bind after exit of ml
|
||||||
int reuse = 1;
|
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");
|
error("setsockopt(SO_REUSEADDR) failed");
|
||||||
#ifdef SO_REUSEPORT
|
#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");
|
error("setsockopt(SO_REUSEPORT) failed");
|
||||||
#endif
|
#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");
|
error("ERROR on binding");
|
||||||
|
|
||||||
listen(sockfd,5);
|
listen(sockfd, 5);
|
||||||
clilen = sizeof(cli_addr);
|
clilen = sizeof(cli_addr);
|
||||||
|
|
||||||
int requests_processed = 0;
|
int requests_processed = 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));
|
memset((char *) &serv_addr, 0, sizeof(serv_addr));
|
||||||
serv_addr.sin_family = AF_INET;
|
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);
|
serv_addr.sin_port = htons(portno);
|
||||||
|
|
||||||
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
|
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
|
||||||
error("ERROR connecting");
|
error("ERROR connecting");
|
||||||
|
|
||||||
|
|
||||||
responses.reserve(requests.size());
|
responses.reserve(requests.size());
|
||||||
for(const auto &req : requests) {
|
for (const auto &req : requests) {
|
||||||
write_to_socket(sockfd, req);
|
write_to_socket(sockfd, req);
|
||||||
|
|
||||||
std::string response = read_from_socket(sockfd);
|
std::string response = read_from_socket(sockfd);
|
||||||
|
|
@ -160,11 +159,11 @@ std::string TcpNet::read_from_socket(int sockfd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpNet::write_to_socket(int sockfd, const std::string &str) {
|
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;
|
int pos = 0;
|
||||||
long n;
|
long n;
|
||||||
do {
|
do {
|
||||||
n = write(sockfd, buffer + pos, (int)(str.length() - pos));
|
n = write(sockfd, buffer + pos, (int) (str.length() - pos));
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
error("ERROR writing to socket");
|
error("ERROR writing to socket");
|
||||||
} while (pos + n < str.length());
|
} while (pos + n < str.length());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue