improvements in networking code
This commit is contained in:
@@ -1,18 +1,17 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <cstdio>
|
||||||
#include <stdlib.h>
|
#include <cstdlib>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <cstring>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
#include "tcpnet.h"
|
#include "tcpnet.h"
|
||||||
|
|
||||||
|
|
||||||
#define TCPNET_BUFFER_SIZE 131072
|
#define TCPNET_BUFFER_SIZE 16384
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void error(const char *msg) {
|
void error(const char *msg) {
|
||||||
@@ -21,23 +20,21 @@ void error(const char *msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO do it as RAII
|
// TODO do it as RAII
|
||||||
TcpNet::TcpNet() {}
|
TcpNet::TcpNet() = default;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int TcpNet::server(int portno, 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;
|
||||||
|
|
||||||
char buffer[TCPNET_BUFFER_SIZE];
|
|
||||||
struct sockaddr_in serv_addr, cli_addr;
|
struct sockaddr_in serv_addr, cli_addr;
|
||||||
|
|
||||||
int n;
|
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd < 0)
|
if (sockfd < 0)
|
||||||
error("ERROR opening socket");
|
error("ERROR opening socket");
|
||||||
|
|
||||||
bzero((char *) &serv_addr, sizeof(serv_addr));
|
memset((char *) &serv_addr, 0, sizeof(serv_addr));
|
||||||
serv_addr.sin_family = AF_INET;
|
serv_addr.sin_family = AF_INET;
|
||||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
serv_addr.sin_port = htons(portno);
|
serv_addr.sin_port = htons(portno);
|
||||||
@@ -65,19 +62,17 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
|
|||||||
error("ERROR on accept");
|
error("ERROR on accept");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
bzero(buffer,TCPNET_BUFFER_SIZE);
|
std::string request = read_from_socket(newsockfd);
|
||||||
n = read(newsockfd,buffer,TCPNET_BUFFER_SIZE - 1);
|
if (request.empty()) break;
|
||||||
if (n == 0) break; // nothing to read from client anymore
|
|
||||||
if (n < 0) error("ERROR reading from socket");
|
|
||||||
|
|
||||||
std::string request{buffer};
|
|
||||||
std::pair<bool, std::string> response = process_request(request);
|
std::pair<bool, std::string> response = process_request(request);
|
||||||
|
|
||||||
shutdown = response.first;
|
shutdown = response.first;
|
||||||
std::string response_str = response.second;
|
std::string response_str = response.second;
|
||||||
|
|
||||||
n = write(newsockfd, response_str.c_str(), response_str.size());
|
auto response_len = response_str.size();
|
||||||
if (n < 0)
|
auto n = write(newsockfd, response_str.c_str(), response_len);
|
||||||
|
if (n < 0 || response_len != n)
|
||||||
error("ERROR writing to socket");
|
error("ERROR writing to socket");
|
||||||
|
|
||||||
requests_processed++;
|
requests_processed++;
|
||||||
@@ -93,13 +88,11 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
|
|||||||
|
|
||||||
|
|
||||||
std::vector<std::string> TcpNet::client(const std::string &address, int portno, const std::vector<std::string> &requests) const {
|
std::vector<std::string> TcpNet::client(const std::string &address, int portno, const std::vector<std::string> &requests) const {
|
||||||
int sockfd, n;
|
int sockfd;
|
||||||
struct sockaddr_in serv_addr;
|
struct sockaddr_in serv_addr;
|
||||||
struct hostent *server;
|
struct hostent *server;
|
||||||
std::vector<std::string> responses;
|
std::vector<std::string> responses;
|
||||||
|
|
||||||
char buffer[TCPNET_BUFFER_SIZE];
|
|
||||||
|
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd < 0)
|
if (sockfd < 0)
|
||||||
error("ERROR opening socket");
|
error("ERROR opening socket");
|
||||||
@@ -110,7 +103,7 @@ std::vector<std::string> TcpNet::client(const std::string &address, int portno,
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bzero((char *) &serv_addr, 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);
|
||||||
@@ -121,16 +114,10 @@ std::vector<std::string> TcpNet::client(const std::string &address, int portno,
|
|||||||
|
|
||||||
responses.reserve(requests.size());
|
responses.reserve(requests.size());
|
||||||
for(const auto &req : requests) {
|
for(const auto &req : requests) {
|
||||||
n = write(sockfd, req.c_str(), strlen(req.c_str()));
|
write_to_socket(sockfd, req);
|
||||||
if (n < 0)
|
|
||||||
error("ERROR writing to socket");
|
|
||||||
|
|
||||||
bzero(buffer, TCPNET_BUFFER_SIZE);
|
std::string response = read_from_socket(sockfd);
|
||||||
n = read(sockfd,buffer, TCPNET_BUFFER_SIZE - 1);
|
responses.push_back(response);
|
||||||
if (n < 0)
|
|
||||||
error("ERROR reading from socket");
|
|
||||||
|
|
||||||
responses.push_back(std::string(buffer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
@@ -145,3 +132,38 @@ std::string TcpNet::client(const std::string &address, int portno, const std::st
|
|||||||
|
|
||||||
return response[0];
|
return response[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string TcpNet::read_from_socket(int sockfd) {
|
||||||
|
char buffer[TCPNET_BUFFER_SIZE];
|
||||||
|
std::string request;
|
||||||
|
|
||||||
|
long n;
|
||||||
|
do {
|
||||||
|
memset(buffer, 0, TCPNET_BUFFER_SIZE);
|
||||||
|
n = read(sockfd, buffer, TCPNET_BUFFER_SIZE - 1);
|
||||||
|
if (n == 0) break; // nothing to read from client anymore
|
||||||
|
if (n < 0) {
|
||||||
|
if (errno == ECONNRESET) break; // connection reset by peer
|
||||||
|
|
||||||
|
std::string err_desc{"ERROR reading from socket "};
|
||||||
|
err_desc.append(strerror(errno));
|
||||||
|
error(err_desc.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string part{buffer};
|
||||||
|
request.append(part);
|
||||||
|
} 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();
|
||||||
|
int pos = 0;
|
||||||
|
long n;
|
||||||
|
do {
|
||||||
|
n = write(sockfd, buffer + pos, (int)(str.length() - pos));
|
||||||
|
if (n < 0)
|
||||||
|
error("ERROR writing to socket");
|
||||||
|
} while (pos + n < str.length());
|
||||||
|
}
|
||||||
@@ -10,11 +10,13 @@ public:
|
|||||||
TcpNet();
|
TcpNet();
|
||||||
|
|
||||||
// starts listening on port
|
// starts listening on port
|
||||||
int server(int port, std::function<std::pair<bool, std::string>(std::string)> process_request) const;
|
int server(int port, const std::function<std::pair<bool, std::string>(std::string)>& process_request) const;
|
||||||
|
|
||||||
// writes request to server on address:port and returns response
|
// writes request to server on address:port and returns response
|
||||||
std::string client(const std::string &address, int portno, const std::string &request) const;
|
[[nodiscard]] std::string client(const std::string &address, int portno, const std::string &request) const;
|
||||||
|
[[nodiscard]] std::vector<std::string> client(const std::string &address, int portno, const std::vector<std::string> &requests) const;
|
||||||
std::vector<std::string> client(const std::string &address, int portno, const std::vector<std::string> &requests) const;
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::string read_from_socket(int sockfd) ;
|
||||||
|
static void write_to_socket(int sockfd, const std::string &str) ;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user