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

@ -3,13 +3,15 @@
#include "sls_detector_exceptions.h" #include "sls_detector_exceptions.h"
#include <algorithm> #include <algorithm>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <cassert>
#include <cstring> #include <cstring>
#include <fcntl.h>
#include <iostream> #include <iostream>
#include <netdb.h> #include <netdb.h>
#include <sstream>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
namespace sls { namespace sls {
@ -41,27 +43,54 @@ DataSocket &DataSocket::operator=(DataSocket &&move) noexcept {
} }
int DataSocket::receiveData(void *buffer, size_t size) { int DataSocket::receiveData(void *buffer, size_t size) {
//TODO!(Erik) Add sleep? how many reties? // TODO!(Erik) Add sleep? how many reties?
int dataRead = 0; assert(size > 0);
while (dataRead < size) { int bytes_expected = static_cast<int>(size); // signed size
auto thisRead = int bytes_read = 0;
::read(getSocketId(), reinterpret_cast<char *>(buffer) + dataRead, while (bytes_read < bytes_expected) {
size - dataRead); auto this_read =
if (thisRead <= 0) ::read(getSocketId(), reinterpret_cast<char *>(buffer) + bytes_read,
bytes_expected - bytes_read);
if (this_read <= 0)
break; break;
dataRead += thisRead; bytes_read += this_read;
}
if (bytes_read == bytes_expected) {
return bytes_read;
} else {
std::ostringstream ss;
ss << "TCP socket error read " << bytes_read << " bytes instead of "
<< bytes_expected << " bytes";
throw sls::SocketError(ss.str());
} }
return dataRead;
} }
int DataSocket::read(void *buffer, size_t size){ int DataSocket::sendData(const void *buffer, size_t size) {
return ::read(getSocketId(), buffer, size); int bytes_sent = 0;
int data_size = static_cast<int>(size); // signed size
while (bytes_sent < (data_size)) {
auto this_send = ::write(getSocketId(), buffer, size);
if (this_send <= 0)
break;
bytes_sent += this_send;
}
if (bytes_sent != data_size){
std::ostringstream ss;
ss << "TCP socket error sent " << bytes_sent << " bytes instead of "
<< data_size << " bytes";
throw sls::SocketError(ss.str());
}
return bytes_sent;
} }
int DataSocket::write(void *buffer, size_t size){ int DataSocket::write(void *buffer, size_t size) {
return ::write(getSocketId(), buffer, size); return ::write(getSocketId(), buffer, size);
} }
int DataSocket::read(void *buffer, size_t size) {
return ::read(getSocketId(), buffer, size);
}
int DataSocket::setReceiveTimeout(int us) { int DataSocket::setReceiveTimeout(int us) {
timeval t{}; timeval t{};
t.tv_sec = 0; t.tv_sec = 0;
@ -70,20 +99,6 @@ int DataSocket::setReceiveTimeout(int us) {
sizeof(struct timeval)); sizeof(struct timeval));
} }
int DataSocket::sendData(const void *buffer, size_t size) {
int dataSent = 0;
while (dataSent < (int)size) {
auto thisSend = ::write(getSocketId(), buffer, size);
if (thisSend <= 0)
break;
dataSent += thisSend;
}
if(dataSent != size)
throw SocketError("Could not send\n");
return dataSent;
}
int DataSocket::setTimeOut(int t_seconds) { int DataSocket::setTimeOut(int t_seconds) {
if (t_seconds <= 0) if (t_seconds <= 0)
return -1; return -1;

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
enum class func_id{read_data, read_int}; enum class func_id{read_data, read_int, read_half_data};
constexpr size_t MB = 1048576; constexpr size_t MB = 1048576;
constexpr size_t DATA_SIZE = 50*MB; constexpr size_t DATA_SIZE = 50*MB;

View File

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