Program Listing for File socket_client_pos.hpp

Return to documentation for file (include/ecl/devices/socket_client_pos.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_DEVICES_SOCKET_CLIENT_POS_HPP_
#define ECL_DEVICES_SOCKET_CLIENT_POS_HPP_

/*****************************************************************************
** Cross platform
*****************************************************************************/

#include <ecl/config/ecl.hpp>
#ifndef ECL_IS_APPLE
#ifdef ECL_IS_POSIX


/*****************************************************************************
** Includes
*****************************************************************************/

#include <arpa/inet.h> // inet_addr()
#include <errno.h>
#include <string>
#include <sys/ioctl.h> // used in remaining()
#include <sys/socket.h>
#include <ecl/exceptions/macros.hpp>
#include <ecl/exceptions/standard_exception.hpp>
#include "socket_connection_status.hpp"
#include "detail/socket_error_handler_pos.hpp"
#include "traits.hpp"

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Interface [SocketClient]
*****************************************************************************/
class SocketClient {
public:
    /*********************
    ** C&D
    **********************/
    SocketClient() : is_open(false), error_handler(NoError) {};
    SocketClient(const std::string &host_name, const unsigned int &port_number = 5555);
    virtual ~SocketClient() { close(); }
    /*********************
    ** Configuration
    **********************/
    bool open( const std::string &hostname, const unsigned int& port_number );
    void close();
    bool open() const { return is_open; }

    /*********************
    ** Writing
    **********************/
    long write(const char &c);

    long write(const char *s, unsigned long n);

    void flush() {}

    /*********************
    ** Reading
    **********************/
    long remaining();
    long read(char &c);
    long read(char *s, const unsigned long &n);
    long peek(char*s, const unsigned long &n);

    const Error& error() const { return error_handler; }

private:
    std::string hostname;
    int port;
    int socket_fd;
    bool is_open;
    ecl::Error error_handler;
};

/*****************************************************************************
** Traits [Serial]
*****************************************************************************/
template <>
class is_sink<SocketClient> : public True {};

template <>
class is_source<SocketClient> : public True {};

template <>
class is_sourcesink<SocketClient> : public True {};

} // namespace ecl

#endif  /* ECL_IS_POSIX */
#endif  /* !ECL_IS_APPLE */

#endif /* ECL_DEVICES_SOCKET_CLIENT_POS_HPP_ */