This commit is contained in:
Erik Frojdh
2019-06-06 17:00:05 +02:00
parent fc5306f7cc
commit c86a1e7d48
11 changed files with 253 additions and 250 deletions

View File

@ -1,6 +1,6 @@
#include "ClientSocket.h"
#include "sls_detector_exceptions.h"
#include "clara.hpp"
#include "sls_detector_exceptions.h"
#include "tests/testenum.h"
#include "container_utils.h"
@ -36,45 +36,50 @@ int main(int argc, char **argv) {
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(i) << " bytes\n";
std::cout << "Sent: " << socket.Send(func_id::read_int) << " bytes\n";
std::cout << "Sent: " << socket.Send(i) << " bytes\n";
}
// 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)
std::cout << "Sent: " << socket.Send(func_id::read_data) << " bytes\n";
std::cout << "Sent: " << socket.Send(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)
std::cout << "Sent: " << socket.Send(func_id::read_data) << " bytes\n";
std::cout << "Sent: " << socket.Send(data.get(), DATA_SIZE / 2)
<< " bytes\n";
}
// Send too much data
try{
// Send too much data
try {
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(func_id::read_half_data)
std::cout << "Sent: " << socket.Send(func_id::read_half_data)
<< " bytes\n";
std::cout << "Sent: " << socket.sendData(data.get(), DATA_SIZE)
std::cout << "Sent: " << socket.Send(data.get(), DATA_SIZE)
<< " bytes\n";
}catch(const sls::SocketError& e){
} 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";
std::cout << "Sent: " << socket.Send(func_id::read_int) << " bytes\n";
std::cout << "Sent: " << socket.Send(i) << " bytes\n";
}
// some combined sends
{
int a = 9;
double b = 18.3;
float c = -1.2;
auto socket = sls::ClientSocket("test", hostname, port);
int s = socket.SendAll(func_id::combined, a, b, c);
std::cout << "send all: " << s << "\n";
}
}

View File

@ -24,26 +24,34 @@ using func_ptr = void (*)(Interface &);
void read_data(Interface &socket) {
auto data = sls::make_unique<char[]>(DATA_SIZE);
std::cout << "Read: " << socket.receiveData(data.get(), DATA_SIZE)
std::cout << "Read: " << socket.Receive(data.get(), DATA_SIZE)
<< " bytes into buffer\n";
}
void read_half_data(Interface &socket) {
auto data = sls::make_unique<char[]>(DATA_SIZE);
std::cout << "Read: " << socket.receiveData(data.get(), DATA_SIZE / 2)
std::cout << "Read: " << socket.Receive(data.get(), DATA_SIZE / 2)
<< " bytes into buffer\n";
}
void read_int(Interface &socket) {
auto i = socket.receive<int>();
auto i = socket.Receive<int>();
std::cout << "Read <int>: " << i << "\n";
}
void read_combined(Interface &socket){
auto i = socket.Receive<int>();
auto d = socket.Receive<double>();
auto f = socket.Receive<float>();
std::cout << "read i: " << i << " d: " << d << " f: " << f << "\n";
}
// 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}};
{func_id::read_half_data, &read_half_data},
{func_id::combined, &read_combined}};
int main(int argc, char **argv) {
std::cout << "Starting test server...\n";
@ -63,7 +71,7 @@ int main(int argc, char **argv) {
while (true) {
try {
auto socket = server.accept();
auto fnum = socket.receive<func_id>();
auto fnum = socket.Receive<func_id>();
std::cout << "Calling func: " << (int)fnum << "\n";
(*fmap[fnum])(socket); // call mapped function