X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fandroid%2Fservice%2Flocal_socket_connection.cpp;fp=src%2Ftype3_AndroidCloud%2Fanbox-master%2Fandroid%2Fservice%2Flocal_socket_connection.cpp;h=aa8567ae3cb7c8a6d76cdddb2d3644ab781c357e;hb=e26c1ec581be598521517829adba8c8dd23a768f;hp=0000000000000000000000000000000000000000;hpb=6699c1aea74eeb0eb400e6299079f0c7576f716f;p=iec.git diff --git a/src/type3_AndroidCloud/anbox-master/android/service/local_socket_connection.cpp b/src/type3_AndroidCloud/anbox-master/android/service/local_socket_connection.cpp new file mode 100644 index 0000000..aa8567a --- /dev/null +++ b/src/type3_AndroidCloud/anbox-master/android/service/local_socket_connection.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2016 Simon Fels + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + */ + +#include "android/service/local_socket_connection.h" + +#include +#include +#include +#include +#include + +#define LOG_TAG "Anboxd" +#include + +namespace { +bool socket_error_is_transient(int error_code) { + return (error_code == EINTR); +} +} + +namespace anbox { +LocalSocketConnection::LocalSocketConnection(const std::string &path) : + fd_(Fd::invalid) { + + struct sockaddr_un socket_address; + memset(&socket_address, 0, sizeof(socket_address)); + + socket_address.sun_family = AF_UNIX; + memcpy(socket_address.sun_path, path.data(), path.size()); + + fd_ = Fd{socket(AF_UNIX, SOCK_STREAM, 0)}; + if (connect(fd_, reinterpret_cast(&socket_address), sizeof(socket_address)) < 0) + throw std::runtime_error("Failed to connect to server socket"); +} + +LocalSocketConnection::~LocalSocketConnection() { + if (fd_ > 0) + ::close(fd_); +} + +ssize_t LocalSocketConnection::read_all(std::uint8_t *buffer, const size_t &size) { + ssize_t bytes_read = ::recv(fd_, reinterpret_cast(buffer), size, 0); + return bytes_read; +} + +void LocalSocketConnection::send(char const* data, size_t length) { + size_t bytes_written{0}; + + while(bytes_written < length) { + ssize_t const result = ::send(fd_, + data + bytes_written, + length - bytes_written, + MSG_NOSIGNAL); + if (result < 0) { + if (socket_error_is_transient(errno)) + continue; + else + throw std::runtime_error("Failed to send message to server"); + } + + bytes_written += result; + } +} + +ssize_t LocalSocketConnection::send_raw(char const* data, size_t length) { + (void)data; + (void)length; + return -EIO; +} +} // namespace anbox