tcp-server/client a bit improved

This commit is contained in:
VaclavT 2021-11-05 15:27:21 +01:00
parent 159845bb9b
commit 865e198a5b
6 changed files with 85 additions and 47 deletions

View File

@ -23,7 +23,7 @@ TcpNet::TcpNet() {}
#define TCPNET_BUFFER_SIZE 16256 #define TCPNET_BUFFER_SIZE 16256
int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::string)> process_request) { int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::string)> process_request) const {
int sockfd, newsockfd; int sockfd, newsockfd;
socklen_t clilen; socklen_t clilen;
@ -40,6 +40,15 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
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);
// this allows immediate bind after exit of ml
int reuse = 1;
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)
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"); error("ERROR on binding");
@ -53,13 +62,15 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
if (newsockfd < 0) if (newsockfd < 0)
error("ERROR on accept"); error("ERROR on accept");
while (true) {
bzero(buffer,TCPNET_BUFFER_SIZE); bzero(buffer,TCPNET_BUFFER_SIZE);
n = read(newsockfd,buffer,TCPNET_BUFFER_SIZE - 1); n = read(newsockfd,buffer,TCPNET_BUFFER_SIZE - 1);
if (n == 0) break; // nothing to read from client anymore
if (n < 0) error("ERROR reading from socket"); if (n < 0) error("ERROR reading from socket");
std::string request{buffer}; 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;
@ -67,19 +78,23 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
if (n < 0) if (n < 0)
error("ERROR writing to socket"); error("ERROR writing to socket");
close(newsockfd);
requests_processed++; requests_processed++;
} }
close(newsockfd);
}
close(sockfd); close(sockfd);
return requests_processed; return requests_processed;
} }
std::string TcpNet::client(const std::string address, int portno, const std::string &content) {
std::vector<std::string> TcpNet::client(const std::string &address, int portno, const std::vector<std::string> &requests) const {
int sockfd, n; int sockfd, n;
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
struct hostent *server; struct hostent *server;
std::vector<std::string> responses;
char buffer[TCPNET_BUFFER_SIZE]; char buffer[TCPNET_BUFFER_SIZE];
@ -102,7 +117,9 @@ std::string TcpNet::client(const std::string address, int portno, const std::str
error("ERROR connecting"); error("ERROR connecting");
n = write(sockfd, content.c_str(), strlen(content.c_str())); responses.reserve(requests.size());
for(const auto &req : requests) {
n = write(sockfd, req.c_str(), strlen(req.c_str()));
if (n < 0) if (n < 0)
error("ERROR writing to socket"); error("ERROR writing to socket");
@ -111,8 +128,18 @@ std::string TcpNet::client(const std::string address, int portno, const std::str
if (n < 0) if (n < 0)
error("ERROR reading from socket"); error("ERROR reading from socket");
close(sockfd); responses.push_back(std::string(buffer));
return std::string(buffer);
} }
close(sockfd);
return responses;
}
std::string TcpNet::client(const std::string &address, int portno, const std::string &request) const {
std::vector<std::string> c{request};
auto response = client(address, portno, c);
return response[0];
}

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <vector>
class TcpNet { class TcpNet {
@ -9,12 +10,11 @@ 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); int server(int port, std::function<std::pair<bool, std::string>(std::string)> process_request) const;
// writes content 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 port, const std::string &content); std::string client(const std::string &address, int portno, const std::string &request) const;
// TODO add support for vector of strings to be sent to server 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 port, const std::vector<std::string> &content);
}; };

View File

@ -1,11 +1,17 @@
(thread-create (thread-create
(tcp-server 7777 (lambda (str) (list #t (+ "(print \"" (string-upcase str) "\")")))) (tcp-server 7777 (lambda (str) (list #f (+ "(print \"" (string-upcase str) "\")"))))
) )
(thread-sleep 1) (thread-sleep 1)
(thread-create (thread-create
(define code (tcp-client "127.0.0.1" 7777 ("abcd" "xyz")))
(for c code
(print "executing code:" c)
(eval (parse c))
)
(define code (tcp-client "127.0.0.1" 7777 "abcd")) (define code (tcp-client "127.0.0.1" 7777 "abcd"))
(print "executing code:" code) (print "executing code:" code)
(eval (parse code)) (eval (parse code))

30
ml.cpp
View File

@ -83,6 +83,12 @@ MlValue::MlValue(bool b) {
MlValue::MlValue(const std::vector<MlValue> &list) : type(LIST), list(list) {} MlValue::MlValue(const std::vector<MlValue> &list) : type(LIST), list(list) {}
MlValue::MlValue(const std::vector<std::string> &slist) : type(LIST) {
list.reserve(slist.size());
for (size_t i = 0; i < slist.size(); i++)
list.push_back(MlValue::string(slist[i]));
}
MlValue MlValue::quote(const MlValue &quoted) { MlValue MlValue::quote(const MlValue &quoted) {
MlValue result; MlValue result;
result.type = QUOTE; result.type = QUOTE;
@ -1346,9 +1352,18 @@ MlValue tcp_client(std::vector<MlValue> args, MlEnvironment &env) {
throw MlError(MlValue("tcp-client", tcp_client), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("tcp-client", tcp_client), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
TcpNet tcpclient; TcpNet tcpclient;
if (args[2].is_list()) {
auto request_list = args[2].as_list();
std::vector<std::string> requests;// PERF reserve
std::transform(request_list.begin(), request_list.end(), back_inserter(requests), std::mem_fn(&MlValue::as_string));
std::vector<std::string> response = tcpclient.client(args[0].as_string(), args[1].as_int(), requests);
return MlValue(response);
} else {
std::string response = tcpclient.client(args[0].as_string(), args[1].as_int(), args[2].as_string()); std::string response = tcpclient.client(args[0].as_string(), args[1].as_int(), args[2].as_string());
return MlValue::string(response); return MlValue::string(response);
} }
}
// Read a file and execute its code // Read a file and execute its code
MlValue include(std::vector<MlValue> args, MlEnvironment &env) { MlValue include(std::vector<MlValue> args, MlEnvironment &env) {
@ -1640,7 +1655,7 @@ MlValue tail(std::vector<MlValue> args, MlEnvironment &env) {
throw MlError(MlValue("tail", tail), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("tail", tail), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result, list = args[0].as_list(); std::vector<MlValue> result, list = args[0].as_list();
// PERF reserve result
for (size_t i = 1; i < list.size(); i++) for (size_t i = 1; i < list.size(); i++)
result.push_back(list[i]); result.push_back(list[i]);
@ -1721,11 +1736,7 @@ MlValue string_regex_list(std::vector<MlValue> args, MlEnvironment &env) {
auto found_matches = regexp_search2(args[0].as_string(), args[1].as_string(), match_mode, ignore_case); auto found_matches = regexp_search2(args[0].as_string(), args[1].as_string(), match_mode, ignore_case);
std::vector<MlValue> list; std::vector<MlValue> list;
for(auto &l : found_matches) { for(auto &l : found_matches) {
std::vector<MlValue> sublist; list.push_back(l);
for(auto &v : l) {
sublist.push_back(MlValue::string(v));
}
list.push_back(sublist);
} }
return MlValue(list); return MlValue(list);
} }
@ -1739,12 +1750,7 @@ MlValue string_split(std::vector<MlValue> args, MlEnvironment &env) {
// TODO do it more efficient // TODO do it more efficient
std::vector<std::string> elements = regexp_strsplit(args[0].as_string(), args[1].as_string()); std::vector<std::string> elements = regexp_strsplit(args[0].as_string(), args[1].as_string());
std::vector<MlValue> result{}; return MlValue(elements);
for (size_t i = 0; i < elements.size(); i++)
result.push_back(MlValue::string(elements[i]));
return MlValue(result);
} }
// converts string to upper or lower case // converts string to upper or lower case

1
ml.h
View File

@ -103,6 +103,7 @@ public:
MlValue(double f); MlValue(double f);
MlValue(bool b); MlValue(bool b);
MlValue(const std::vector<MlValue> &list); // Constructs a list MlValue(const std::vector<MlValue> &list); // Constructs a list
MlValue(const std::vector<std::string> &slist); // Constructs a list from vector of strings
static MlValue quote(const MlValue &quoted); // Construct a quoted value static MlValue quote(const MlValue &quoted); // Construct a quoted value
static MlValue atom(const std::string &s); static MlValue atom(const std::string &s);

View File

@ -135,8 +135,6 @@
(ut::define-test "result of create table" '(ut::assert-equal ((0 "table created" 0)) (usql "create table a (i integer not null, s varchar(64), f float null, d date null, b boolean)"))) (ut::define-test "result of create table" '(ut::assert-equal ((0 "table created" 0)) (usql "create table a (i integer not null, s varchar(64), f float null, d date null, b boolean)")))
(ut::define-test "result tcp-client" '(ut::assert-equal "(print \"ABCD\")" (tcp-client "127.0.0.1" 7777 "abcd"))) (ut::define-test "result tcp-client" '(ut::assert-equal "(print \"ABCD\")" (tcp-client "127.0.0.1" 7777 "abcd")))
;(ut::define-test "result of " '(ut::assert-true ) ;(ut::define-test "result of " '(ut::assert-true )