cleaning up socket

This commit is contained in:
Erik Frojdh
2019-06-06 10:34:57 +02:00
parent 7a11c6c986
commit 5944957977
4 changed files with 118 additions and 58 deletions

View File

@ -1,18 +1,20 @@
#include "ClientSocket.h"
#include "sls_detector_exceptions.h"
#include "clara.hpp"
#include "tests/testenum.h"
#include <iostream>
#include "container_utils.h"
#include <iostream>
bool help = false;
int main(int argc, char** argv) {
int main(int argc, char **argv) {
std::cout << "Test client\n";
std::string hostname{"localhost"};
int port = 2345;
auto cli =
clara::Opt(hostname, "hostname")["-hn"]["--hostname"]("Hostname") |
clara::Help(help) |
clara::Opt(hostname, "hostname")["-n"]["--hostname"]("Hostname") |
clara::Opt(port, "port")["-p"]["--port"]("Port to send to");
auto result = cli.parse(clara::Args(argc, argv));
@ -21,25 +23,58 @@ int main(int argc, char** argv) {
<< std::endl;
exit(1);
}
if (help) {
std::cout << cli << std::endl;
return 0;
}
std::cout << "Sending to: " << hostname << ":" << port << "\n";
auto data = sls::make_unique<char[]>(DATA_SIZE);
for (int64_t i = 0; i!=50; ++i){
// Many connections sending small amounts
for (int i = 0; i != 100; ++i) {
std::cout << "Sending: " << i << "\n";
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(func_id::read_int) << " bytes\n";
std::cout << "Sent: " << socket.sendData(func_id::read_int)
<< " bytes\n";
std::cout << "Sent: " << socket.sendData(i) << " bytes\n";
}
for (int64_t i = 0; i!=5; ++i){
// Sending larger blocks
for (int i = 0; i != 5; ++i) {
std::cout << "Sending data\n";
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(func_id::read_data) << " bytes\n";
std::cout << "Sent: " << socket.sendData(data.get(), DATA_SIZE) << " bytes\n";
std::cout << "Sent: " << socket.sendData(func_id::read_data)
<< " bytes\n";
std::cout << "Sent: " << socket.sendData(data.get(), DATA_SIZE)
<< " bytes\n";
}
// Send too little data
{
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(func_id::read_data)
<< " bytes\n";
std::cout << "Sent: " << socket.sendData(data.get(), DATA_SIZE / 2)
<< " bytes\n";
}
// Send too much data
try{
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(func_id::read_half_data)
<< " bytes\n";
std::cout << "Sent: " << socket.sendData(data.get(), DATA_SIZE)
<< " bytes\n";
}catch(const sls::SocketError& e){
}
// Some ints again
for (int i = 0; i != 10; ++i) {
std::cout << "Sending: " << i << "\n";
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(func_id::read_int)
<< " bytes\n";
std::cout << "Sent: " << socket.sendData(i) << " bytes\n";
}
}

View File

@ -3,44 +3,54 @@
#include "tests/testenum.h"
#include "ServerInterface2.h"
#include "container_utils.h"
#include <iostream>
#include <unordered_map>
#include "ServerInterface2.h"
struct EnumClassHash
{
template <typename T>
std::size_t operator()(T t) const
{
// For hashing of enum with C++11, not needed in 14
struct EnumClassHash {
template <typename T> std::size_t operator()(T t) const {
return static_cast<std::size_t>(t);
}
};
using Interface = sls::ServerInterface2;
using func_ptr = int (*)(Interface &);
using func_ptr = void (*)(Interface &);
int read_data(Interface &socket) {
/********************************************
* Mapped functions *
********************************************/
void read_data(Interface &socket) {
auto data = sls::make_unique<char[]>(DATA_SIZE);
std::cout << "Read: " << socket.receiveData(data.get(), DATA_SIZE)
<< " bytes into buffer\n";
return 0;
}
int read_int(Interface &socket) {
void read_half_data(Interface &socket) {
auto data = sls::make_unique<char[]>(DATA_SIZE);
std::cout << "Read: " << socket.receiveData(data.get(), DATA_SIZE / 2)
<< " bytes into buffer\n";
}
void read_int(Interface &socket) {
auto i = socket.receive<int>();
std::cout << "Read <int>: " << i << "\n";
return 0;
}
static std::unordered_map<func_id, func_ptr, EnumClassHash> fmap{
{func_id::read_data, &read_data}, {func_id::read_int, &read_int}};
// Map from int to function pointer, in this case probably a map would be faster
std::unordered_map<func_id, func_ptr, EnumClassHash> fmap{
{func_id::read_data, &read_data},
{func_id::read_int, &read_int},
{func_id::read_half_data, &read_half_data}};
int main(int argc, char **argv) {
std::cout << "Test server\n";
std::cout << "Starting test server...\n";
int port = 2345;
auto cli = clara::Opt(port, "port")["-p"]["--port"]("Port to send to");
// Parse command line arguments using clara
auto cli = clara::Opt(port, "port")["-p"]["--port"]("Port to send to");
auto result = cli.parse(clara::Args(argc, argv));
if (!result) {
std::cerr << "Error in command line: " << result.errorMessage()
@ -48,17 +58,17 @@ int main(int argc, char **argv) {
exit(1);
}
std::cout << "Listening to port: " << port << "\n";
auto server = sls::ServerSocket(port);
auto server = sls::ServerSocket(port);
while (true) {
try {
auto socket = server.accept();
auto fnum = socket.receive<func_id>();
std::cout << "Calling func: " << (int)fnum << "\n";
auto ret = (*fmap[fnum])(socket);
// std::cout << "function returned: " << ret << "\n";
(*fmap[fnum])(socket); // call mapped function
} catch (const sls::RuntimeError &e) {
// Do nothing, error is printed when the exeption is created
}
}
}