Commit 1b8f020b authored by Matthieu Kermagoret's avatar Matthieu Kermagoret
Browse files

Connector SSH: added socket_handle class.

parent 565d609f
......@@ -129,6 +129,7 @@ add_library("${CONNECTORLIB}"
"${SRC_DIR}/multiplexer.cc"
# "${SRC_DIR}/session.cc"
# "${SRC_DIR}/sessions.cc"
"${SRC_DIR}/socket_handle.cc"
# "${SRC_DIR}/std_io.cc"
# Headers.
"${INC_DIR}/channel.hh"
......@@ -139,6 +140,7 @@ add_library("${CONNECTORLIB}"
"${INC_DIR}/namespace.hh"
# "${INC_DIR}/session.hh"
# "${INC_DIR}/sessions.hh"
"${INC_DIR}/socket_handle.hh"
# "${INC_DIR}/std_io.hh"
)
target_link_libraries(
......
/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Connector SSH.
**
** Centreon Connector SSH is free software: you can redistribute it
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
** Centreon Connector SSH is distributed in the hope that it will be
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
** License along with Centreon Connector SSH. If not, see
** <http://www.gnu.org/licenses/>.
*/
#ifndef CCCS_SOCKET_HANDLE_HH
# define CCCS_SOCKET_HANDLE_HH
# include "com/centreon/connector/ssh/namespace.hh"
# include "com/centreon/handle.hh"
CCCS_BEGIN()
/**
* @class socket_handle socket_handle.hh "com/centreon/connector/ssh/socket_handle.hh"
* @brief Socket handle.
*
* Wrapper around a socket descriptor.
*/
class socket_handle : public com::centreon::handle {
public:
socket_handle(native_handle internal_handle = -1);
~socket_handle() throw ();
void close();
unsigned long read(void* data, unsigned long size);
void set_native_handle(native_handle internal_handle);
unsigned long write(void const* data, unsigned long size);
private:
socket_handle(socket_handle const& sh);
socket_handle& operator=(socket_handle const& sh);
};
CCCS_END()
#endif // !CCCS_SOCKET_HANDLE_HH
/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Connector SSH.
**
** Centreon Connector SSH is free software: you can redistribute it
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
** Centreon Connector SSH is distributed in the hope that it will be
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
** License along with Centreon Connector SSH. If not, see
** <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "com/centreon/connector/ssh/socket_handle.hh"
#include "com/centreon/exceptions/basic.hh"
using namespace com::centreon;
using namespace com::centreon::connector::ssh;
/**************************************
* *
* Public Methods *
* *
**************************************/
/**
* Default constructor.
*
* @param[in] internal_handle Native socket descriptor.
*/
socket_handle::socket_handle(native_handle internal_handle)
: handle(internal_handle) {}
/**
* Destructor.
*/
socket_handle::~socket_handle() throw () {
this->close();
}
/**
* Close socket descriptor.
*/
void socket_handle::close() {
::close(_internal_handle);
_internal_handle = -1;
return ;
}
/**
* Read from socket descriptor.
*
* @param[out] data Where data will be stored.
* @param[in] size How much data in bytes to read at most.
*
* @return Number of bytes actually read.
*/
unsigned long socket_handle::read(void* data, unsigned long size) {
ssize_t rb(::read(_internal_handle, data, size));
if (rb < 0) {
char const* msg(strerror(errno));
throw (basic_error() << "socket read error: " << msg);
}
return (rb);
}
/**
* Set socket descriptor.
*
* @param[in] internal_handle Native socket descriptor.
*/
void socket_handle::set_native_handle(native_handle internal_handle) {
this->close();
_internal_handle = internal_handle;
return ;
}
/**
* Write to socket descriptor.
*
* @param[in] data Data to write.
* @param[in] size How much data to write at most.
*
* @return Number of bytes actually written.
*/
unsigned long socket_handle::write(
void const* data,
unsigned long size) {
ssize_t wb(::write(_internal_handle, data, size));
if (wb < 0) {
char const* msg(strerror(errno));
throw (basic_error() << "socket write error: " << msg);
}
return (wb);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment