diff --git a/include/f1x/aasdk/Error/ErrorCode.hpp b/include/f1x/aasdk/Error/ErrorCode.hpp index 7f3be33..16849bb 100644 --- a/include/f1x/aasdk/Error/ErrorCode.hpp +++ b/include/f1x/aasdk/Error/ErrorCode.hpp @@ -29,40 +29,41 @@ namespace error enum class ErrorCode { - NONE, - USB_CLAIM_INTERFACE, - USB_INVALID_CONFIG_DESCRIPTOR, - USB_OBTAIN_INTERFACE_DESCRIPTOR, - USB_EMPTY_INTERFACES, - USB_INVALID_DEVICE_ENDPOINTS, - USB_INVALID_TRANSFER_METHOD, - USB_TRANSFER_ALLOCATION, - USB_LIST_DEVICES, - USB_OBTAIN_CONFIG_DESCRIPTOR, - USB_TRANSFER, - USB_SINK_COMMIT_OVERFLOW, - USB_SINK_CONSUME_UNDERFLOW, - USB_AOAP_PROTOCOL_VERSION, - USB_EMPTY_DEVICE_LIST, - USB_AOAP_DEVICE_NOT_FOUND, - SSL_READ_CERTIFICATE, - SSL_READ_PRIVATE_KEY, - SSL_METHOD, - SSL_CONTEXT_CREATION, - SSL_USE_CERTIFICATE, - SSL_USE_PRIVATE_KEY, - SSL_HANDLER_CREATION, - SSL_READ_BIO_CREATION, - SSL_WRITE_BIO_CREATION, - SSL_HANDSHAKE, - SSL_WRITE, - SSL_READ, - SSL_BIO_READ, - SSL_BIO_WRITE, - MESSENGER_INTERTWINED_CHANNELS, - OPERATION_ABORTED, - OPERATION_IN_PROGRESS, - PARSE_PAYLOAD + NONE = 0, + USB_CLAIM_INTERFACE = 1, + USB_INVALID_CONFIG_DESCRIPTOR = 2, + USB_OBTAIN_INTERFACE_DESCRIPTOR = 3, + USB_EMPTY_INTERFACES = 4, + USB_INVALID_DEVICE_ENDPOINTS = 5, + USB_INVALID_TRANSFER_METHOD = 6, + USB_TRANSFER_ALLOCATION = 7, + USB_LIST_DEVICES = 8, + USB_OBTAIN_CONFIG_DESCRIPTOR = 9, + USB_TRANSFER = 10, + USB_SINK_COMMIT_OVERFLOW = 11, + USB_SINK_CONSUME_UNDERFLOW = 12, + USB_AOAP_PROTOCOL_VERSION = 13, + USB_EMPTY_DEVICE_LIST = 14, + USB_AOAP_DEVICE_NOT_FOUND = 15, + SSL_READ_CERTIFICATE = 16, + SSL_READ_PRIVATE_KEY = 17, + SSL_METHOD = 18, + SSL_CONTEXT_CREATION = 19, + SSL_USE_CERTIFICATE = 20, + SSL_USE_PRIVATE_KEY = 21, + SSL_HANDLER_CREATION = 22, + SSL_READ_BIO_CREATION = 23, + SSL_WRITE_BIO_CREATION = 24, + SSL_HANDSHAKE = 25, + SSL_WRITE = 26, + SSL_READ = 27, + SSL_BIO_READ = 28, + SSL_BIO_WRITE = 29, + MESSENGER_INTERTWINED_CHANNELS = 30, + OPERATION_ABORTED = 31, + OPERATION_IN_PROGRESS = 32, + PARSE_PAYLOAD = 33, + TCP_TRANSFER = 34 }; } diff --git a/include/f1x/aasdk/TCP/ITCPEndpoint.hpp b/include/f1x/aasdk/TCP/ITCPEndpoint.hpp new file mode 100644 index 0000000..80c01c9 --- /dev/null +++ b/include/f1x/aasdk/TCP/ITCPEndpoint.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include + +namespace f1x +{ +namespace aasdk +{ +namespace tcp +{ + +class ITCPEndpoint +{ +public: + typedef std::shared_ptr Pointer; + typedef io::Promise Promise; + + virtual ~ITCPEndpoint() = default; + + virtual void send(common::DataConstBuffer buffer, Promise::Pointer promise) = 0; + virtual void receive(common::DataBuffer buffer, Promise::Pointer promise) = 0; + virtual void stop() = 0; +}; + +} +} +} diff --git a/include/f1x/aasdk/TCP/ITCPWrapper.hpp b/include/f1x/aasdk/TCP/ITCPWrapper.hpp new file mode 100644 index 0000000..978acf9 --- /dev/null +++ b/include/f1x/aasdk/TCP/ITCPWrapper.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +namespace f1x +{ +namespace aasdk +{ +namespace tcp +{ + +class ITCPWrapper +{ +public: + typedef std::function Handler; + + virtual ~ITCPWrapper() = default; + + virtual void asyncWrite(boost::asio::ip::tcp::socket& socket, common::DataConstBuffer buffer, Handler handler) = 0; + virtual void asyncRead(boost::asio::ip::tcp::socket& socket, common::DataBuffer buffer, Handler handler) = 0; +}; + +} +} +} diff --git a/include/f1x/aasdk/TCP/TCPEndpoint.hpp b/include/f1x/aasdk/TCP/TCPEndpoint.hpp new file mode 100644 index 0000000..199537f --- /dev/null +++ b/include/f1x/aasdk/TCP/TCPEndpoint.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include + +namespace f1x +{ +namespace aasdk +{ +namespace tcp +{ + +class TCPEndpoint: public ITCPEndpoint, public std::enable_shared_from_this +{ +public: + TCPEndpoint(ITCPWrapper& tcpWrapper, boost::asio::ip::tcp::socket socket); + + void send(common::DataConstBuffer buffer, Promise::Pointer promise) override; + void receive(common::DataBuffer buffer, Promise::Pointer promise) override; + void stop() override; + +private: + using std::enable_shared_from_this::shared_from_this; + + void asyncOperationHandler(const boost::system::error_code& ec, size_t bytesTransferred, Promise::Pointer promise); + + ITCPWrapper& tcpWrapper_; + boost::asio::ip::tcp::socket socket_; +}; + +} +} +} diff --git a/include/f1x/aasdk/TCP/TCPWrapper.hpp b/include/f1x/aasdk/TCP/TCPWrapper.hpp new file mode 100644 index 0000000..80aea86 --- /dev/null +++ b/include/f1x/aasdk/TCP/TCPWrapper.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +namespace f1x +{ +namespace aasdk +{ +namespace tcp +{ + +class TCPWrapper: public ITCPWrapper +{ +public: + void asyncWrite(boost::asio::ip::tcp::socket& socket, common::DataConstBuffer buffer, Handler handler) override; + void asyncRead(boost::asio::ip::tcp::socket& socket, common::DataBuffer buffer, Handler handler) override; +}; + +} +} +} diff --git a/src/TCP/TCPEndpoint.cpp b/src/TCP/TCPEndpoint.cpp new file mode 100644 index 0000000..763e918 --- /dev/null +++ b/src/TCP/TCPEndpoint.cpp @@ -0,0 +1,64 @@ +#include + +namespace f1x +{ +namespace aasdk +{ +namespace tcp +{ + +TCPEndpoint::TCPEndpoint(ITCPWrapper& tcpWrapper, boost::asio::ip::tcp::socket socket) + : tcpWrapper_(tcpWrapper) + , socket_(std::move(socket)) +{ + +} + +void TCPEndpoint::send(common::DataConstBuffer buffer, Promise::Pointer promise) +{ + tcpWrapper_.asyncWrite(socket_, std::move(buffer), + std::bind(&TCPEndpoint::asyncOperationHandler, + this->shared_from_this(), + std::placeholders::_1, + std::placeholders::_2, + std::move(promise))); +} + +void TCPEndpoint::receive(common::DataBuffer buffer, Promise::Pointer promise) +{ + tcpWrapper_.asyncRead(socket_, std::move(buffer), + std::bind(&TCPEndpoint::asyncOperationHandler, + this->shared_from_this(), + std::placeholders::_1, + std::placeholders::_2, + std::move(promise))); +} + +void TCPEndpoint::stop() +{ + try + { + socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both); + socket_.close(); + } + catch(const boost::system::system_error&) + { + + } +} + +void TCPEndpoint::asyncOperationHandler(const boost::system::error_code& ec, size_t bytesTransferred, Promise::Pointer promise) +{ + if(!ec) + { + promise->resolve(bytesTransferred); + } + else + { + promise->reject(error::Error(error::ErrorCode::TCP_TRANSFER, static_cast(ec.value()))); + } +} + +} +} +} diff --git a/src/TCP/TCPWrapper.cpp b/src/TCP/TCPWrapper.cpp new file mode 100644 index 0000000..0de693b --- /dev/null +++ b/src/TCP/TCPWrapper.cpp @@ -0,0 +1,23 @@ +#include +#include + +namespace f1x +{ +namespace aasdk +{ +namespace tcp +{ + +void TCPWrapper::asyncWrite(boost::asio::ip::tcp::socket& socket, common::DataConstBuffer buffer, Handler handler) +{ + boost::asio::async_write(socket, boost::asio::buffer(buffer.cdata, buffer.size), std::move(handler)); +} + +void TCPWrapper::asyncRead(boost::asio::ip::tcp::socket& socket, common::DataBuffer buffer, Handler handler) +{ + boost::asio::async_read(socket, boost::asio::buffer(buffer.data, buffer.size), std::move(handler)); +} + +} +} +}