diff options
Diffstat (limited to 'src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc')
-rw-r--r-- | src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc | 116 |
1 files changed, 56 insertions, 60 deletions
diff --git a/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc b/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc index 7d96c685f1..eb92599ae5 100644 --- a/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc +++ b/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc @@ -46,7 +46,7 @@ namespace transport_manager { namespace transport_adapter { -CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager") +SDL_CREATE_LOG_VARIABLE("TransportManager") ThreadedSocketConnection::ThreadedSocketConnection( const DeviceUID& device_id, @@ -69,7 +69,7 @@ ThreadedSocketConnection::ThreadedSocketConnection( } ThreadedSocketConnection::~ThreadedSocketConnection() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); DCHECK(nullptr == thread_); if (-1 != read_fd_) { @@ -83,54 +83,54 @@ ThreadedSocketConnection::~ThreadedSocketConnection() { void ThreadedSocketConnection::StopAndJoinThread() { Disconnect(); if (thread_) { - thread_->join(); - delete thread_->delegate(); + thread_->Stop(threads::Thread::kThreadSoftStop); + delete thread_->GetDelegate(); threads::DeleteThread(thread_); thread_ = nullptr; } } void ThreadedSocketConnection::Abort() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); unexpected_disconnect_ = true; terminate_flag_ = true; } TransportAdapter::Error ThreadedSocketConnection::Start() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); int fds[2]; const int pipe_ret = pipe(fds); if (0 == pipe_ret) { - LOG4CXX_DEBUG(logger_, "pipe created"); + SDL_LOG_DEBUG("pipe created"); read_fd_ = fds[0]; write_fd_ = fds[1]; } else { - LOG4CXX_ERROR(logger_, "pipe creation failed"); + SDL_LOG_ERROR("pipe creation failed"); return TransportAdapter::FAIL; } const int fcntl_ret = fcntl(read_fd_, F_SETFL, fcntl(read_fd_, F_GETFL) | O_NONBLOCK); if (0 != fcntl_ret) { - LOG4CXX_ERROR(logger_, "fcntl failed"); + SDL_LOG_ERROR("fcntl failed"); return TransportAdapter::FAIL; } - if (!thread_->start()) { - LOG4CXX_ERROR(logger_, "thread creation failed"); + if (!thread_->Start()) { + SDL_LOG_ERROR("thread creation failed"); return TransportAdapter::FAIL; } - LOG4CXX_INFO(logger_, "thread created"); + SDL_LOG_INFO("thread created"); return TransportAdapter::OK; } void ThreadedSocketConnection::Finalize() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); if (unexpected_disconnect_) { - LOG4CXX_DEBUG(logger_, "unexpected_disconnect"); + SDL_LOG_DEBUG("unexpected_disconnect"); controller_->ConnectionAborted( device_handle(), application_handle(), CommunicationError()); } else { - LOG4CXX_DEBUG(logger_, "not unexpected_disconnect"); + SDL_LOG_DEBUG("not unexpected_disconnect"); controller_->ConnectionFinished(device_handle(), application_handle()); } @@ -138,17 +138,17 @@ void ThreadedSocketConnection::Finalize() { } TransportAdapter::Error ThreadedSocketConnection::Notify() const { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); if (-1 == write_fd_) { - LOG4CXX_ERROR_WITH_ERRNO( - logger_, "Failed to wake up connection thread for connection " << this); - LOG4CXX_TRACE(logger_, "exit with TransportAdapter::BAD_STATE"); + SDL_LOG_ERROR_WITH_ERRNO( + "Failed to wake up connection thread for connection " << this); + SDL_LOG_TRACE("exit with TransportAdapter::BAD_STATE"); return TransportAdapter::BAD_STATE; } uint8_t c = 0; if (1 != write(write_fd_, &c, 1)) { - LOG4CXX_ERROR_WITH_ERRNO( - logger_, "Failed to wake up connection thread for connection " << this); + SDL_LOG_ERROR_WITH_ERRNO( + "Failed to wake up connection thread for connection " << this); return TransportAdapter::FAIL; } return TransportAdapter::OK; @@ -156,44 +156,44 @@ TransportAdapter::Error ThreadedSocketConnection::Notify() const { TransportAdapter::Error ThreadedSocketConnection::SendData( ::protocol_handler::RawMessagePtr message) { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); sync_primitives::AutoLock auto_lock(frames_to_send_mutex_); frames_to_send_.push(message); return Notify(); } TransportAdapter::Error ThreadedSocketConnection::Disconnect() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); terminate_flag_ = true; ShutdownAndCloseSocket(); return Notify(); } void ThreadedSocketConnection::Terminate() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); StopAndJoinThread(); } void ThreadedSocketConnection::threadMain() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); ConnectError* connect_error = nullptr; if (!Establish(&connect_error)) { - LOG4CXX_ERROR(logger_, "Connection Establish failed"); + SDL_LOG_ERROR("Connection Establish failed"); delete connect_error; Abort(); } else { - LOG4CXX_DEBUG(logger_, "Connection established"); + SDL_LOG_DEBUG("Connection established"); controller_->ConnectDone(device_handle(), application_handle()); } while (!terminate_flag_) { Transmit(); } - LOG4CXX_DEBUG(logger_, "Connection is to finalize"); + SDL_LOG_DEBUG("Connection is to finalize"); Finalize(); sync_primitives::AutoLock auto_lock(frames_to_send_mutex_); while (!frames_to_send_.empty()) { - LOG4CXX_INFO(logger_, "removing message"); + SDL_LOG_INFO("removing message"); ::protocol_handler::RawMessagePtr message = frames_to_send_.front(); frames_to_send_.pop(); controller_->DataSendFailed( @@ -208,23 +208,23 @@ bool ThreadedSocketConnection::IsFramesToSendQueueEmpty() const { } void ThreadedSocketConnection::ShutdownAndCloseSocket() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); const int socket = socket_; socket_ = -1; if (socket != -1) { if (shutdown(socket, SHUT_RDWR) != 0) { - LOG4CXX_WARN(logger_, "Socket was unable to be shutdowned"); + SDL_LOG_WARN("Socket was unable to be shutdowned"); } if (close(socket) != 0) { - LOG4CXX_ERROR_WITH_ERRNO(logger_, "Failed to close socket"); + SDL_LOG_ERROR_WITH_ERRNO("Failed to close socket"); } } else { - LOG4CXX_WARN(logger_, "Socket has been already closed or not created yet"); + SDL_LOG_WARN("Socket has been already closed or not created yet"); } } void ThreadedSocketConnection::Transmit() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); const nfds_t kPollFdsSize = 2; pollfd poll_fds[kPollFdsSize]; @@ -237,26 +237,24 @@ void ThreadedSocketConnection::Transmit() { poll_fds[1].fd = read_fd_; poll_fds[1].events = POLLIN | POLLPRI; - LOG4CXX_DEBUG(logger_, "poll " << this); + SDL_LOG_DEBUG("poll " << this); if (-1 == poll(poll_fds, kPollFdsSize, -1)) { - LOG4CXX_ERROR_WITH_ERRNO(logger_, "poll failed for connection " << this); + SDL_LOG_ERROR_WITH_ERRNO("poll failed for connection " << this); Abort(); return; } - LOG4CXX_DEBUG(logger_, - "poll is ok " << this << " revents0: " << std::hex + SDL_LOG_DEBUG("poll is ok " << this << " revents0: " << std::hex << poll_fds[0].revents << " revents1:" << std::hex << poll_fds[1].revents); // error check if (0 != (poll_fds[1].revents & (POLLERR | POLLHUP | POLLNVAL))) { - LOG4CXX_ERROR(logger_, - "Notification pipe for connection " << this << " terminated"); + SDL_LOG_ERROR("Notification pipe for connection " << this << " terminated"); Abort(); return; } if (poll_fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - LOG4CXX_WARN(logger_, "Connection " << this << " terminated"); + SDL_LOG_WARN("Connection " << this << " terminated"); Abort(); return; } @@ -268,8 +266,8 @@ void ThreadedSocketConnection::Transmit() { bytes_read = read(read_fd_, buffer, sizeof(buffer)); } while (bytes_read > 0); if ((bytes_read < 0) && (EAGAIN != errno)) { - LOG4CXX_ERROR_WITH_ERRNO(logger_, "Failed to clear notification pipe"); - LOG4CXX_ERROR_WITH_ERRNO(logger_, "poll failed for connection " << this); + SDL_LOG_ERROR_WITH_ERRNO("Failed to clear notification pipe"); + SDL_LOG_ERROR_WITH_ERRNO("poll failed for connection " << this); Abort(); return; } @@ -278,12 +276,12 @@ void ThreadedSocketConnection::Transmit() { // Send data if possible if (!is_queue_empty && (poll_fds[0].revents & POLLOUT)) { - LOG4CXX_DEBUG(logger_, "frames_to_send_ not empty() "); + SDL_LOG_DEBUG("frames_to_send_ not empty() "); // send data const bool send_ok = Send(); if (!send_ok) { - LOG4CXX_ERROR(logger_, "Send() failed "); + SDL_LOG_ERROR("Send() failed "); Abort(); return; } @@ -293,7 +291,7 @@ void ThreadedSocketConnection::Transmit() { if (poll_fds[0].revents & (POLLIN | POLLPRI)) { const bool receive_ok = Receive(); if (!receive_ok) { - LOG4CXX_ERROR(logger_, "Receive() failed "); + SDL_LOG_ERROR("Receive() failed "); Abort(); return; } @@ -301,7 +299,7 @@ void ThreadedSocketConnection::Transmit() { } bool ThreadedSocketConnection::Receive() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); uint8_t buffer[4096]; ssize_t bytes_read = -1; @@ -309,21 +307,19 @@ bool ThreadedSocketConnection::Receive() { bytes_read = recv(socket_, buffer, sizeof(buffer), MSG_DONTWAIT); if (bytes_read > 0) { - LOG4CXX_DEBUG( - logger_, - "Received " << bytes_read << " bytes for connection " << this); + SDL_LOG_DEBUG("Received " << bytes_read << " bytes for connection " + << this); ::protocol_handler::RawMessagePtr frame( new protocol_handler::RawMessage(0, 0, buffer, bytes_read, false)); controller_->DataReceiveDone( device_handle(), application_handle(), frame); } else if (bytes_read < 0) { if (EAGAIN != errno && EWOULDBLOCK != errno) { - LOG4CXX_ERROR_WITH_ERRNO(logger_, - "recv() failed for connection " << this); + SDL_LOG_ERROR_WITH_ERRNO("recv() failed for connection " << this); return false; } } else { - LOG4CXX_WARN(logger_, "Connection " << this << " closed by remote peer"); + SDL_LOG_WARN("Connection " << this << " closed by remote peer"); return false; } } while (bytes_read > 0); @@ -332,7 +328,7 @@ bool ThreadedSocketConnection::Receive() { } bool ThreadedSocketConnection::Send() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); FrameQueue frames_to_send_local; { @@ -342,13 +338,13 @@ bool ThreadedSocketConnection::Send() { size_t offset = 0; while (!frames_to_send_local.empty()) { - LOG4CXX_INFO(logger_, "frames_to_send is not empty"); + SDL_LOG_INFO("frames_to_send is not empty"); ::protocol_handler::RawMessagePtr frame = frames_to_send_local.front(); const ssize_t bytes_sent = ::send(socket_, frame->data() + offset, frame->data_size() - offset, 0); if (bytes_sent >= 0) { - LOG4CXX_DEBUG(logger_, "bytes_sent >= 0"); + SDL_LOG_DEBUG("bytes_sent >= 0"); offset += bytes_sent; if (offset == frame->data_size()) { frames_to_send_local.pop(); @@ -356,8 +352,8 @@ bool ThreadedSocketConnection::Send() { controller_->DataSendDone(device_handle(), application_handle(), frame); } } else { - LOG4CXX_DEBUG(logger_, "bytes_sent < 0"); - LOG4CXX_ERROR_WITH_ERRNO(logger_, "Send failed for connection " << this); + SDL_LOG_DEBUG("bytes_sent < 0"); + SDL_LOG_ERROR_WITH_ERRNO("Send failed for connection " << this); frames_to_send_local.pop(); offset = 0; controller_->DataSendFailed( @@ -373,13 +369,13 @@ ThreadedSocketConnection::SocketConnectionDelegate::SocketConnectionDelegate( : connection_(connection) {} void ThreadedSocketConnection::SocketConnectionDelegate::threadMain() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); DCHECK(connection_); connection_->threadMain(); } void ThreadedSocketConnection::SocketConnectionDelegate::exitThreadMain() { - LOG4CXX_AUTO_TRACE(logger_); + SDL_LOG_AUTO_TRACE(); } } // namespace transport_adapter |