diff options
author | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 17:14:31 -0500 |
---|---|---|
committer | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 17:14:31 -0500 |
commit | d84d880627bcc1e1898a8f96b861bc25863ec86c (patch) | |
tree | e7db4eef6a8e8254eaa6ba0c7e5d56098af19d16 /src/MTS_IO_Connection.cpp | |
download | libmts-io-d84d880627bcc1e1898a8f96b861bc25863ec86c.tar.gz libmts-io-d84d880627bcc1e1898a8f96b861bc25863ec86c.tar.bz2 libmts-io-d84d880627bcc1e1898a8f96b861bc25863ec86c.zip |
initial commit
Diffstat (limited to 'src/MTS_IO_Connection.cpp')
-rw-r--r-- | src/MTS_IO_Connection.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/MTS_IO_Connection.cpp b/src/MTS_IO_Connection.cpp new file mode 100644 index 0000000..36a91ed --- /dev/null +++ b/src/MTS_IO_Connection.cpp @@ -0,0 +1,155 @@ +/* + * 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 <http://www.gnu.org/licenses/>. + * + */ + +/*! + \file MTS_IO_Connection.cpp + \brief A brief description + \date Jan 15, 2013 + \author sgodinez + + A more elaborate description than necessary. +*/ + + +#include <mts/MTS_IO_Connection.h> +#include <mts/MTS_Buffer.h> +#include <sys/select.h> + +using namespace MTS; +using namespace MTS::IO; + +int32_t Connection::BLOCKING = -1; + +Connection::Connection(const std::string& name) +: m_sName(name) +, m_bClosed(true) +, m_bCloseable(true) { + m_apLock.reset(new Lock()); + m_apReadLock.reset(new Lock()); + m_apWriteLock.reset(new Lock()); +} + +Connection::~Connection() { + m_apWriteLock.reset(); + m_apReadLock.reset(); + m_apLock.reset(); +} + +const std::string& Connection::getName() const { + return m_sName; +} + +bool Connection::open(const int32_t& timeoutMillis) { + bool bOpened = true; + m_apLock->lock(); + if (m_bClosed) { + bOpened = doOpen(timeoutMillis); + if (bOpened) { + m_bClosed = false; + } + } + m_apLock->unlock(); + return bOpened; +} + +void Connection::close() { + m_apLock->lock(); + if (m_bCloseable) { + doClose(); + m_bClosed = true; + } + m_apLock->unlock(); +} + +bool Connection::isClosed() const { + m_apLock->lock(); + bool bResult = m_bClosed; + m_apLock->unlock(); + return bResult; +} + +void Connection::setCloseable(bool bCloseable) { + m_apLock->lock(); + m_bCloseable = bCloseable; + m_apLock->unlock(); +} + +bool Connection::isCloseable() const { + m_apLock->lock(); + bool bResult = m_bCloseable; + m_apLock->unlock(); + return bResult; +} + +int Connection::read(std::string& sBuffer, const uint32_t& iSize, int32_t& timeoutMillis) { + int result = -1; + if (isClosed()) { + return result; + } else { + char buffer[iSize]; + m_apReadLock->lock(); + result = doRead(buffer, iSize, timeoutMillis); + m_apReadLock->unlock(); + if(result > 0) { + sBuffer = std::string(buffer, result); + } + } + + return result; +} + +int Connection::read(Buffer& oBuffer, const uint32_t& iSize, int32_t& timeoutMillis) { + int result = -1; + if (isClosed()) { + return result; + } else { + oBuffer.setCapacity(iSize); + m_apReadLock->lock(); + result = doRead((char*)oBuffer.getBuffer(), iSize, timeoutMillis); + m_apReadLock->unlock(); + } + + return result; +} + +int Connection::read(char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis) { + int result = -1; + if (isClosed()) { + return result; + } else { + m_apReadLock->lock(); + result = doRead(pBuffer, iSize, timeoutMillis); + m_apReadLock->unlock(); + } + return result; +} + +int Connection::write(const char* pBuffer, const uint32_t& iSize, int32_t& timeoutMillis) { + int result = -1; + if (isClosed()) { + return result; + } else { + m_apWriteLock->lock(); + result = doWrite(pBuffer, iSize, timeoutMillis); + m_apWriteLock->unlock(); + } + return result; +} + |