From: TaeminYeom Date: Tue, 12 Jul 2022 08:42:12 +0000 (+0900) Subject: Unify socket class X-Git-Tag: accepted/tizen/unified/20220713.144410^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F14%2F277714%2F2;p=platform%2Fcore%2Fapi%2Fsensor.git Unify socket class Remove seqpacket_socket, stream_socket, ipc_client, ipc_server, accept_event_handler class Change-Id: Idec7923636db1092cc9395692e8b79acd09952d7 Signed-off-by: TaeminYeom --- diff --git a/src/api/sensor_listener.cpp b/src/api/sensor_listener.cpp index 3a45899..12a1920 100644 --- a/src/api/sensor_listener.cpp +++ b/src/api/sensor_listener.cpp @@ -23,7 +23,6 @@ #include #include #include -#include using namespace sensor; @@ -97,7 +96,6 @@ private: sensor_listener::sensor_listener(sensor_t sensor) : m_id(0) , m_sensor(reinterpret_cast(sensor)) -, m_client(NULL) , m_cmd_channel(NULL) , m_evt_channel(NULL) , m_handler(NULL) @@ -114,7 +112,6 @@ sensor_listener::sensor_listener(sensor_t sensor) sensor_listener::sensor_listener(sensor_t sensor, ipc::event_loop *loop) : m_id(0) , m_sensor(reinterpret_cast(sensor)) -, m_client(NULL) , m_cmd_channel(NULL) , m_evt_channel(NULL) , m_handler(NULL) @@ -136,21 +133,15 @@ sensor_listener::~sensor_listener() bool sensor_listener::init(void) { - m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); - retvm_if(!m_client, false, "Failed to allocate memory"); - m_handler = new(std::nothrow) listener_handler(this); if (!m_handler) { _E("Failed to allocate memory"); - delete m_client; return false; } if (!connect()) { delete m_handler; - delete m_client; m_handler = NULL; - m_client = NULL; return false; } @@ -173,9 +164,6 @@ void sensor_listener::deinit(void) m_loop->add_channel_handler_release_list(m_handler); m_handler = NULL; - delete m_client; - m_client = NULL; - m_attributes_int.clear(); m_attributes_str.clear(); _D("Deinitialized.."); @@ -226,11 +214,13 @@ void sensor_listener::restore(void) bool sensor_listener::connect(void) { - m_cmd_channel = m_client->connect(NULL); - retvm_if(!m_cmd_channel, false, "Failed to connect to server"); + m_cmd_channel = new(std::nothrow) ipc::channel(); + retvm_if(!m_cmd_channel, false, "Failed to allocate memory"); + m_cmd_channel->connect(NULL, NULL, true); - m_evt_channel = m_client->connect(m_handler, m_loop, false); - retvm_if(!m_evt_channel, false, "Failed to connect to server"); + m_evt_channel = new(std::nothrow) ipc::channel(); + retvm_if(!m_evt_channel, false, "Failed to allocate memory"); + m_evt_channel->connect(m_handler, m_loop, false); ipc::message msg; ipc::message reply; diff --git a/src/api/sensor_listener.h b/src/api/sensor_listener.h index 6989c52..f654353 100644 --- a/src/api/sensor_listener.h +++ b/src/api/sensor_listener.h @@ -20,7 +20,6 @@ #ifndef __SENSOR_LISTENER_H__ #define __SENSOR_LISTENER_H__ -#include #include #include #include @@ -92,7 +91,6 @@ private: int m_id; sensor_info *m_sensor; - ipc::ipc_client *m_client; ipc::channel *m_cmd_channel; ipc::channel *m_evt_channel; ipc::channel_handler *m_handler; diff --git a/src/api/sensor_manager.cpp b/src/api/sensor_manager.cpp index dee567a..e576b9d 100644 --- a/src/api/sensor_manager.cpp +++ b/src/api/sensor_manager.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -32,8 +31,7 @@ using namespace sensor; sensor_manager::sensor_manager() -: m_client(NULL) -, m_cmd_channel(NULL) +: m_cmd_channel(NULL) , m_mon_channel(NULL) , m_connected(false) , m_handler(NULL) @@ -173,13 +171,8 @@ void sensor_manager::remove_sensor_removed_cb(sensord_removed_cb cb) bool sensor_manager::init(void) { - m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); - retvm_if(!m_client, false, "Failed to allocate memory"); - m_handler = new(std::nothrow) channel_handler(this); if (!m_handler) { - delete m_client; - m_client = NULL; return false; } @@ -192,20 +185,19 @@ void sensor_manager::deinit(void) delete m_handler; m_handler = NULL; - - delete m_client; - m_client = NULL; } bool sensor_manager::connect_channel(void) { ipc::message msg; - m_cmd_channel = m_client->connect(NULL); - retvm_if(!m_cmd_channel, false, "Failed to connect to server"); + m_cmd_channel = new(std::nothrow) ipc::channel(); + retvm_if(!m_cmd_channel, false, "Failed to allocate memory"); + m_cmd_channel->connect(NULL, NULL, true); - m_mon_channel = m_client->connect(m_handler, &m_loop); - retvm_if(!m_mon_channel, false, "Failed to connect to server"); + m_mon_channel = new(std::nothrow) ipc::channel(); + retvm_if(!m_mon_channel, false, "Failed to allocate memory"); + m_mon_channel->connect(m_handler, &m_loop, true); msg.set_type(CMD_MANAGER_CONNECT); m_mon_channel->send_sync(msg); diff --git a/src/api/sensor_manager.h b/src/api/sensor_manager.h index 770b80d..1f6f1f9 100644 --- a/src/api/sensor_manager.h +++ b/src/api/sensor_manager.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -75,7 +74,6 @@ private: sensor_info *get_info(const char *uri); std::vector get_infos(const char *uri); - ipc::ipc_client *m_client; ipc::channel *m_cmd_channel; /* get sensor information */ ipc::channel *m_mon_channel; /* monitor sensors dinamically added/removed */ ipc::event_loop m_loop; diff --git a/src/api/sensor_provider_internal.cpp b/src/api/sensor_provider_internal.cpp index f3fccd3..1563ac7 100644 --- a/src/api/sensor_provider_internal.cpp +++ b/src/api/sensor_provider_internal.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -36,8 +35,7 @@ using namespace sensor; sensor_provider::sensor_provider(const char *uri) -: m_client(NULL) -, m_channel(NULL) +: m_channel(NULL) , m_handler(NULL) , m_connected(false) { @@ -51,12 +49,8 @@ sensor_provider::~sensor_provider() bool sensor_provider::init(const char *uri) { - m_client = new(std::nothrow) ipc::ipc_client(SENSOR_CHANNEL_PATH); - retvm_if(!m_client, false, "Failed to allocate memory"); - m_handler = new(std::nothrow) channel_handler(this); if (!m_handler) { - delete m_client; return false; } @@ -77,9 +71,6 @@ void sensor_provider::deinit(void) delete m_handler; m_handler = NULL; - - delete m_client; - m_client = NULL; } const char *sensor_provider::get_uri(void) @@ -135,8 +126,9 @@ int sensor_provider::send_sensor_info(sensor_info *info) int sensor_provider::connect(void) { - m_channel = m_client->connect(m_handler, &m_loop); - retvm_if(!m_channel, -EIO, "Failed to connect to server"); + m_channel = new(std::nothrow) ipc::channel(); + retvm_if(!m_channel, -EIO, "Failed to allocate memory"); + m_channel->connect(m_handler, &m_loop, true); /* serialize and send sensor info */ send_sensor_info(get_sensor_info()); diff --git a/src/api/sensor_provider_internal.h b/src/api/sensor_provider_internal.h index f4bfa17..4d49f58 100644 --- a/src/api/sensor_provider_internal.h +++ b/src/api/sensor_provider_internal.h @@ -20,7 +20,6 @@ #ifndef __SENSOR_PROVIDER_INTERNAL_H__ #define __SENSOR_PROVIDER_INTERNAL_H__ -#include #include #include #include @@ -65,7 +64,6 @@ private: sensor_info m_sensor; - ipc::ipc_client *m_client; ipc::channel *m_channel; ipc::event_loop m_loop; channel_handler *m_handler; diff --git a/src/shared/accept_event_handler.cpp b/src/shared/accept_event_handler.cpp deleted file mode 100644 index 1ee14a8..0000000 --- a/src/shared/accept_event_handler.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "accept_event_handler.h" - -#include "sensor-log-private.h" -#include "ipc_server.h" - -using namespace ipc; - -accept_event_handler::accept_event_handler(ipc_server *server) -: m_server(server) -{ -} - -bool accept_event_handler::handle(int fd, event_condition condition, void **data) -{ - retv_if((condition & (EVENT_HUP)), false); - - stream_socket *cli_sock = new(std::nothrow) stream_socket(); - retvm_if(!cli_sock, false, "Failed to allocate memory"); - - m_server->accept(*cli_sock); - - channel *_ch = new(std::nothrow) channel(cli_sock); - if (!_ch) { - delete cli_sock; - _E("Failed to allocate memory"); - return false; - } - - m_server->register_channel(cli_sock->get_fd(), _ch); - - return true; -} diff --git a/src/shared/accept_event_handler.h b/src/shared/accept_event_handler.h deleted file mode 100644 index b732238..0000000 --- a/src/shared/accept_event_handler.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __ACCEPT_EVENT_HANDLER__ -#define __ACCEPT_EVENT_HANDLER__ - -#include "event_handler.h" - -namespace ipc { - -class ipc_server; - -class accept_event_handler : public event_handler -{ -public: - accept_event_handler(ipc_server *server); - - bool handle(int fd, event_condition condition, void **data); - -private: - ipc_server *m_server; -}; - -} - -#endif /* __ACCEPT_EVENT_HANDLER__ */ diff --git a/src/shared/channel.cpp b/src/shared/channel.cpp index e8021cd..26091f4 100644 --- a/src/shared/channel.cpp +++ b/src/shared/channel.cpp @@ -26,6 +26,7 @@ #include "sensor-log-private.h" #include "channel_event_handler.h" +#include "command_types.h" #define SYSTEMD_SOCK_BUF_SIZE (128*1024) @@ -110,10 +111,10 @@ private: channel *m_ch; }; -channel::channel(socket *sock) -: m_fd(sock->get_fd()) +channel::channel(void) +: m_fd(-1) , m_event_id(0) -, m_socket(sock) +, m_socket(NULL) , m_handler(NULL) , m_loop(NULL) , m_connected(false) @@ -146,9 +147,8 @@ uint64_t channel::bind(void) return m_event_id; } -uint64_t channel::bind(channel_handler *handler, event_loop *loop, bool loop_bind) +uint64_t channel::bind(event_loop *loop, bool loop_bind) { - m_handler = handler; m_loop = loop; m_connected.store(true); @@ -163,11 +163,21 @@ uint64_t channel::bind(channel_handler *handler, event_loop *loop, bool loop_bin uint64_t channel::connect(channel_handler *handler, event_loop *loop, bool loop_bind) { + m_socket = new(std::nothrow) socket(); + retvm_if(!m_socket, NULL, "Failed to allocate memory"); + + if (!m_socket->create(SENSOR_CHANNEL_PATH)) { + delete m_socket; + return 0; + } + if (!m_socket->connect()) - return false; + return 0; + + m_handler = new(std::nothrow) channel_event_handler(this, handler); + retvm_if(!m_handler, NULL, "Failed to allocate memory"); - if (handler) - bind(handler, loop, loop_bind); + bind(loop, loop_bind); _D("Connect channel[%p] : event id[%llu]", this, m_event_id); return m_event_id; diff --git a/src/shared/channel.h b/src/shared/channel.h index 5af06da..dec1149 100644 --- a/src/shared/channel.h +++ b/src/shared/channel.h @@ -37,11 +37,11 @@ class channel_handler; class channel { public: /* move owernership of the socket to the channel */ - channel(socket *sock); + channel(); ~channel(); uint64_t bind(void); - uint64_t bind(channel_handler *handler, event_loop *loop, bool loop_bind); + uint64_t bind(event_loop *loop, bool loop_bind); uint64_t connect(channel_handler *handler, event_loop *loop, bool loop_bind); void disconnect(void); diff --git a/src/shared/ipc_client.cpp b/src/shared/ipc_client.cpp deleted file mode 100644 index 3d2365e..0000000 --- a/src/shared/ipc_client.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "ipc_client.h" - -#include "sensor-log-private.h" -#include "stream_socket.h" -#include "event_handler.h" -#include "channel_event_handler.h" - -using namespace ipc; - -ipc_client::ipc_client(const std::string &path) -{ - m_path = path; -} - -ipc_client::~ipc_client() -{ -} - -bool ipc_client::set_option(int option, int value) -{ - return true; -} - -bool ipc_client::set_option(const std::string &option, int value) -{ - return true; -} - -channel *ipc_client::connect(channel_handler *handler) -{ - return connect(handler, NULL); -} - -channel *ipc_client::connect(channel_handler *handler, event_loop *loop, bool bind) -{ - socket *sock = NULL; - channel *ch = NULL; - channel_event_handler *ev_handler = NULL; - - sock = new(std::nothrow) stream_socket(); - retvm_if(!sock, NULL, "Failed to allocate memory"); - - if (!sock->create(m_path)) { - delete sock; - return NULL; - } - - ch = new(std::nothrow) channel(sock); - if (!ch) { - delete sock; - _E("Failed to allocate memory"); - return NULL; - } - - ev_handler = new(std::nothrow) channel_event_handler(ch, handler); - if (!ev_handler) { - delete ch; - _E("Failed to allocate memory"); - return NULL; - } - - uint64_t id = ch->connect(ev_handler, loop, bind); - - _D("Connected[%llu]", id); - - return ch; -} diff --git a/src/shared/ipc_client.h b/src/shared/ipc_client.h deleted file mode 100644 index 40d9182..0000000 --- a/src/shared/ipc_client.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __IPC_CLIENT_H__ -#define __IPC_CLIENT_H__ - -#include - -#include "channel.h" -#include "channel_handler.h" -#include "event_loop.h" - -namespace ipc { - -class ipc_client { -public: - ipc_client(const std::string &path); - ~ipc_client(); - - bool set_option(int option, int value); - bool set_option(const std::string &option, int value); - - /* call channel->disconnect() after you have used it */ - channel *connect(channel_handler *handler); - /* TODO: remove bind parameter */ - channel *connect(channel_handler *handler, event_loop *loop, bool bind = true); - -private: - std::string m_path; -}; - -} - -#endif /* __IPC_CLIENT_H__ */ diff --git a/src/shared/ipc_server.cpp b/src/shared/ipc_server.cpp deleted file mode 100644 index c0eefe0..0000000 --- a/src/shared/ipc_server.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "ipc_server.h" - -#include "channel.h" -#include "sensor-log-private.h" -#include "event_loop.h" -#include "channel_event_handler.h" -#include "accept_event_handler.h" - -using namespace ipc; - -#define MAX_CONNECTIONS 1000 - -ipc_server::ipc_server(const std::string &path) -: m_event_loop(NULL) -, m_handler(NULL) -, m_accept_handler(NULL) -{ - m_accept_sock.create(path); -} - -ipc_server::~ipc_server() -{ -} - -bool ipc_server::set_option(int option, int value) -{ - /* TODO */ - return true; -} - -bool ipc_server::set_option(const std::string &option, int value) -{ - /* TODO */ - return true; -} - -void ipc_server::accept(ipc::socket &cli_sock) -{ - m_accept_sock.accept(cli_sock); - - _D("Accepted[%d]", cli_sock.get_fd()); -} - -bool ipc_server::bind(channel_handler *handler, event_loop *loop) -{ - m_handler = handler; - m_event_loop = loop; - - m_accept_sock.bind(); - m_accept_sock.listen(MAX_CONNECTIONS); - - register_acceptor(); - - _D("Bound[%d]", m_accept_sock.get_fd()); - return true; -} - -void ipc_server::register_channel(int fd, channel *ch) -{ - channel_event_handler *ev_handler = new(std::nothrow) channel_event_handler(ch, m_handler); - retm_if(!ev_handler, "Failed to allocate memory"); - - uint64_t id = ch->bind(ev_handler, m_event_loop, true); - - if (id == 0) { - _E("Failed to register channel"); - delete ev_handler; - return; - } - - _D("Register channel[%p] : event_id[%llu]", ch, id); -} - -void ipc_server::register_acceptor(void) -{ - int fd = m_accept_sock.get_fd(); - - m_accept_handler = new(std::nothrow) accept_event_handler(this); - retm_if(!m_accept_handler, "Failed to allocate memory"); - - uint64_t id = m_event_loop->add_event(fd, - (event_condition)(EVENT_IN | EVENT_HUP | EVENT_NVAL), m_accept_handler); - - if (id == 0) { - _D("Failed to add accept event handler"); - delete m_accept_handler; - m_accept_handler = NULL; - } -} - -bool ipc_server::close(void) -{ - m_accept_sock.close(); - - m_handler = NULL; - - return true; -} diff --git a/src/shared/ipc_server.h b/src/shared/ipc_server.h deleted file mode 100644 index 7e033d0..0000000 --- a/src/shared/ipc_server.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __IPC_SERVER_H__ -#define __IPC_SERVER_H__ - -#include - -#include "stream_socket.h" -#include "channel.h" -#include "channel_handler.h" -#include "accept_event_handler.h" -#include "event_loop.h" - -namespace ipc { - -class ipc_server { -public: - ipc_server(const std::string &path); - ~ipc_server(); - - bool set_option(int option, int value); - bool set_option(const std::string &option, int value); - - bool bind(channel_handler *handler, event_loop *loop); - bool close(void); - - /* TODO: only accept_handler should use these functions */ - void accept(ipc::socket &cli_sock); - void register_channel(int fd, channel *ch); - void register_acceptor(void); - -private: - stream_socket m_accept_sock; - - event_loop *m_event_loop; - channel_handler *m_handler; - accept_event_handler *m_accept_handler; -}; - -} - -#endif /* __IPC_SERVER_H__ */ diff --git a/src/shared/seqpacket_socket.cpp b/src/shared/seqpacket_socket.cpp deleted file mode 100644 index b7ad634..0000000 --- a/src/shared/seqpacket_socket.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "seqpacket_socket.h" - -#include -#include - -#include "sensor-log-private.h" - -using namespace ipc; - -seqpacket_socket::seqpacket_socket() -: socket() -{ -} - -seqpacket_socket::~seqpacket_socket() -{ -} - -bool seqpacket_socket::create(const std::string &path) -{ - return socket::create_by_type(path, SOCK_SEQPACKET); -} - -ssize_t seqpacket_socket::on_send(const void *buffer, size_t size) const -{ - ssize_t err, len; - - do { - len = ::send(socket::get_fd(), - reinterpret_cast(buffer), - size, - socket::get_mode()); - - err = len < 0 ? errno : 0; - } while (err == EINTR); - - if (err) { - _ERRNO(errno, _E, "Failed to send(%d, %p, %u) = %d", - socket::get_fd(), buffer, size, len); - } - - return err == 0 ? len : -err; -} - -ssize_t seqpacket_socket::on_recv(void *buffer, size_t size) const -{ - ssize_t err, len; - - do { - len = ::recv(socket::get_fd(), - reinterpret_cast(buffer), - size, - socket::get_mode()); - - if (len > 0) { - err = 0; - } else if (len == 0) { - _E("Failed to recv(%d, %p , %u) = %d, because the peer performed shutdown!", - socket::get_fd(), buffer, size, len); - err = 1; - } else { - err = errno; - } - } while (err == EINTR); - - if ((err == EAGAIN) || (err == EWOULDBLOCK)) - return 0; - - if (err) { - _ERRNO(errno, _E, "Failed to recv(%d, %p, %u) = %d", - socket::get_fd(), buffer, size, len); - } - - return err == 0 ? len : -err; -} - diff --git a/src/shared/seqpacket_socket.h b/src/shared/seqpacket_socket.h deleted file mode 100644 index 05c25d8..0000000 --- a/src/shared/seqpacket_socket.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __SEQPACKET_SOCKET_H__ -#define __SEQPACKET_SOCKET_H__ - -#include "socket.h" - -namespace ipc { - -class seqpacket_socket : public socket { -public: - seqpacket_socket(); - ~seqpacket_socket(); - - bool create(const std::string &path); - -private: - ssize_t on_send(const void *buffer, size_t size) const; - ssize_t on_recv(void *buffer, size_t size) const; -}; - -} - -#endif /* __SEQPACKET_SOCKET_H__ */ diff --git a/src/shared/socket.cpp b/src/shared/socket.cpp index c47cd2b..ee5b059 100644 --- a/src/shared/socket.cpp +++ b/src/shared/socket.cpp @@ -30,6 +30,7 @@ #include "sensor-log-private.h" #define SOCK_TIMEOUT 10 +#define SLEEP_10_MS usleep(10000) using namespace ipc; @@ -283,7 +284,24 @@ bool socket::set_mode(int mode) bool socket::create(const std::string &path) { - return false; + m_sock_fd = ::create_systemd_socket(path, SOCK_STREAM); + if (m_sock_fd < 0) + m_sock_fd = create_unix_socket(SOCK_STREAM); + else + m_listening.store(true); + + retvm_if((m_sock_fd < 0), false, "Failed to create socket"); + + /* non-blocking mode */ + retvm_if(!set_blocking_mode(false), false, "Failed to set non-blocking mode"); + /* recv timeout */ + retvm_if(!set_recv_timeout(1), false, "Failed to set timeout"); + /* TODO */ + /*retvm_if(!set_reuse_addr(), false, "Failed to reuse address"); */ + + m_path = path; + + return true; } ssize_t socket::send(const void *buffer, size_t size, bool select) const @@ -318,28 +336,6 @@ ssize_t socket::recv(void* buffer, size_t size, bool select) const return on_recv(buffer, size); } -bool socket::create_by_type(const std::string &path, int type) -{ - m_sock_fd = ::create_systemd_socket(path, type); - if (m_sock_fd < 0) - m_sock_fd = create_unix_socket(type); - else - m_listening.store(true); - - retvm_if((m_sock_fd < 0), false, "Failed to create socket"); - - /* non-blocking mode */ - retvm_if(!set_blocking_mode(false), false, "Failed to set non-blocking mode"); - /* recv timeout */ - retvm_if(!set_recv_timeout(1), false, "Failed to set timeout"); - /* TODO */ - /*retvm_if(!set_reuse_addr(), false, "Failed to reuse address"); */ - - m_path = path; - - return true; -} - int socket::get_sock_type(void) { socklen_t opt_len; @@ -454,3 +450,63 @@ int socket::get_current_buffer_size(void) return queue_size; } +ssize_t socket::on_send(const void *buffer, size_t size) const +{ + ssize_t len = 0; + size_t total_size = 0; + + do { + len = ::send(get_fd(), + reinterpret_cast(buffer) + total_size, + size - total_size, get_mode()); + + if (len < 0) { + if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) { + SLEEP_10_MS; + continue; + } + + _ERRNO(errno, _E, "Failed to send(%d, %p, %u, %u) = %d", + get_fd(), buffer, total_size, size - total_size, len); + return -errno; + } + + total_size += len; + } while (total_size < size); + + return total_size; +} + +ssize_t socket::on_recv(void *buffer, size_t size) const +{ + ssize_t len = 0; + size_t total_size = 0; + + do { + len = ::recv(get_fd(), + reinterpret_cast(buffer) + total_size, + size - total_size, + socket::get_mode()); + + if (len == 0) { + _E("Failed to recv(%d, %p + %x, %u) = %d, because the peer performed shutdown", + get_fd(), buffer, total_size, size - total_size, len); + return -1; + } + + if (len < 0) { + if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) { + SLEEP_10_MS; + continue; + } + + _ERRNO(errno, _E, "Failed to recv(%d, %p, %u, %u) = %d", + get_fd(), buffer, total_size, size - total_size, len); + return -errno; + } + + total_size += len; + } while (total_size < size); + + return total_size; +} diff --git a/src/shared/socket.h b/src/shared/socket.h index e9b6b66..af40640 100644 --- a/src/shared/socket.h +++ b/src/shared/socket.h @@ -61,12 +61,9 @@ public: ssize_t send(const void *buffer, size_t size, bool select = false) const; ssize_t recv(void* buffer, size_t size, bool select = false) const; -protected: - bool create_by_type(const std::string &path, int type); - private: - virtual ssize_t on_send(const void *buffer, size_t size) const = 0; - virtual ssize_t on_recv(void* buffer, size_t size) const = 0; + ssize_t on_send(const void *buffer, size_t size) const; + ssize_t on_recv(void* buffer, size_t size) const; int get_sock_type(void); bool set_sock_type(int type); diff --git a/src/shared/stream_socket.cpp b/src/shared/stream_socket.cpp deleted file mode 100644 index eaaad0d..0000000 --- a/src/shared/stream_socket.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "stream_socket.h" - -#include -#include - -#include "sensor-log-private.h" - -#define SLEEP_10_MS usleep(10000) - -using namespace ipc; - -stream_socket::stream_socket() -: socket() -{ -} - -stream_socket::~stream_socket() -{ -} - -bool stream_socket::create(const std::string &path) -{ - return socket::create_by_type(path, SOCK_STREAM); -} - -ssize_t stream_socket::on_send(const void *buffer, size_t size) const -{ - ssize_t len = 0; - size_t total_size = 0; - - do { - len = ::send(get_fd(), - reinterpret_cast(buffer) + total_size, - size - total_size, get_mode()); - - if (len < 0) { - if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) { - SLEEP_10_MS; - continue; - } - - _ERRNO(errno, _E, "Failed to send(%d, %p, %u, %u) = %d", - get_fd(), buffer, total_size, size - total_size, len); - return -errno; - } - - total_size += len; - } while (total_size < size); - - return total_size; -} - -ssize_t stream_socket::on_recv(void *buffer, size_t size) const -{ - ssize_t len = 0; - size_t total_size = 0; - - do { - len = ::recv(get_fd(), - reinterpret_cast(buffer) + total_size, - size - total_size, - socket::get_mode()); - - if (len == 0) { - _E("Failed to recv(%d, %p + %x, %u) = %d, because the peer performed shutdown", - get_fd(), buffer, total_size, size - total_size, len); - return -1; - } - - if (len < 0) { - if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) { - SLEEP_10_MS; - continue; - } - - _ERRNO(errno, _E, "Failed to recv(%d, %p, %u, %u) = %d", - get_fd(), buffer, total_size, size - total_size, len); - return -errno; - } - - total_size += len; - } while (total_size < size); - - return total_size; -} diff --git a/src/shared/stream_socket.h b/src/shared/stream_socket.h deleted file mode 100644 index 42af03e..0000000 --- a/src/shared/stream_socket.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * sensord - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef __STREAM_SOCKET_H__ -#define __STREAM_SOCKET_H__ - -#include "socket.h" - -namespace ipc { - -class stream_socket : public socket { -public: - stream_socket(); - ~stream_socket(); - - bool create(const std::string &path); - -private: - ssize_t on_send(const void *buffer, size_t size) const; - ssize_t on_recv(void *buffer, size_t size) const; -}; - -} - -#endif /* __STREAM_SOCKET_H__ */