/*
* Copyright (C) 2015 by Multi-Tech Systems
*
* This file is part of libmts-io.
*
* libmts-io is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* libmts-io 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libmts-io. If not, see .
*
*/
/*!
\file MTS_IO_Connection.h
\brief A brief description
\date Jan 15, 2013
\author sgodinez
A more elaborate description
*/
#ifndef MTS_IO_CONNECTION_H_
#define MTS_IO_CONNECTION_H_
#include
#include
#include
#include
namespace MTS {
class Buffer;
namespace IO {
class Connection : public MTS::NonCopyable {
private:
static int32_t BLOCKING;
public:
virtual ~Connection();
const std::string& getName() const;
bool open(const int32_t& timeoutMillis = 1000);
void close();
bool isClosed() const;
void setCloseable(bool bCloseable);
bool isCloseable() const;
int read(char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis = BLOCKING);
int read(std::string& sBuffer, const uint32_t& iSize, int32_t& timeoutMillis = BLOCKING);
int read(Buffer& oBuffer, const uint32_t& iSize, int32_t& timeoutMillis = BLOCKING);
int write(const char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis = BLOCKING);
int write(const std::string& sBuffer, const uint32_t& iSize, int32_t& timeoutMillis = BLOCKING);
int write(const Buffer& oBuffer, const uint32_t& iSize, int32_t& timeoutMillis = BLOCKING);
protected:
explicit Connection(const std::string& sName);
private:
const std::string m_sName;
bool m_bClosed;
bool m_bCloseable;
AutoPtr m_apLock;
AutoPtr m_apReadLock;
AutoPtr m_apWriteLock;
/*! Opens the connection
* \return true if the connection has been opened, false otherwise
*/
virtual bool doOpen(const int32_t& timeoutMillis) = 0;
/*! Closes the connection
*/
virtual void doClose() = 0;
/*! Perform read from connection
* \param pBuffer the buffer allocated for read bytes
* \param iSize the number of bytes to be read. the size allocated for pBuffer must be greater or equal to iSize
* \param timeoutMillis the amount of time to attempt to read iSize bytes. less than zero will block until iSize bytes has been read
* \return the number of bytes read. -1 for error
*/
virtual int doRead(char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis) = 0;
/*! Perform write to connection
* \param pBuffer the buffer that contains the data to be written
* \param iSize the number of bytes to be written
* \param timeoutMillis the amount of time to attempt to write iSize bytes. less than zero will block until iSize bytes has been written
* \return the number of bytes written. -1 for error
*/
virtual int doWrite(const char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis) = 0;
};
}
}
#endif /* MTS_IO_CONNECTION_H_ */