diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/connection.cc | 13 | ||||
-rw-r--r-- | src/server/server.cc | 21 | ||||
-rw-r--r-- | src/server/server.h | 5 |
3 files changed, 26 insertions, 13 deletions
diff --git a/src/server/connection.cc b/src/server/connection.cc index 499b5a0..efd6802 100644 --- a/src/server/connection.cc +++ b/src/server/connection.cc @@ -5,14 +5,12 @@ #include <ios> #include <arpa/inet.h> #include <cerrno> +#include <nlohmann/json.hpp> #include "connection.h" #include "../p18/commands.h" -#include "../p18/response.h" #include "../logging.h" #include "../common.h" -#include "hexdump/hexdump.h" -#include "signal.h" #define CHECK_ARGUMENTS_LENGTH(__size__) \ if (arguments.size() != (__size__)) { \ @@ -239,8 +237,13 @@ Response Connection::processRequest(char* buf) { resp.type = ResponseType::Error; - auto err = p18::response_type::ErrorResponse(e.what()); - resp.buf << *(err.format(options_.format)); + try { + auto err = p18::response_type::ErrorResponse(e.what()); + resp.buf << *(err.format(options_.format)); + } catch (nlohmann::detail::exception& e) { + myerr << e.what(); + resp.buf << "error while formatting json: " << e.what(); + } } return resp; diff --git a/src/server/server.cc b/src/server/server.cc index a025a7d..e89de0d 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -8,13 +8,13 @@ #include <utility> #include <arpa/inet.h> #include <sys/socket.h> +#include <stdexcept> #include <unistd.h> #include "../voltronic/exceptions.h" #include "../p18/exceptions.h" #include "../voltronic/time.h" #include "../logging.h" -//#include "hexdump/hexdump.h" #include "server.h" #include "connection.h" #include "signal.h" @@ -31,9 +31,20 @@ Server::Server(std::shared_ptr<voltronic::Device> device) , deviceErrorCounter_(0) , verbose_(false) , device_(std::move(device)) { + client_.setDevice(device_); } +Server::~Server() { + if (sock_ > 0) + close(sock_); +} + + +/** + * Common stuff, getters, setters + */ + void Server::setVerbose(bool verbose) { verbose_ = verbose; device_->setVerbose(verbose); @@ -51,10 +62,10 @@ void Server::setDeviceErrorLimit(u32 deviceErrorLimit) { deviceErrorLimit_ = deviceErrorLimit; } -Server::~Server() { - if (sock_ > 0) - close(sock_); -} + +/** + * TCP Server + */ void Server::start(std::string& host, int port) { host_ = host; diff --git a/src/server/server.h b/src/server/server.h index 705503f..0f65688 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -13,7 +13,7 @@ #include <netinet/in.h> #include "connection.h" -#include "../numeric_types.h" +#include "../types.h" #include "../formatter/formatter.h" #include "../p18/client.h" #include "../p18/types.h" @@ -22,8 +22,6 @@ namespace server { -typedef std::lock_guard<std::mutex> LockGuard; - class Connection; struct CachedResponse { @@ -69,6 +67,7 @@ public: void setDeviceErrorLimit(u32 deviceErrorLimit); void start(std::string& host, int port); + void createDeviceThread(); bool verbose() const { return verbose_; } void addConnection(Connection* conn); |