summaryrefslogtreecommitdiff
path: root/src/MTS_IO_Connection.cpp
blob: 40e460ed3f651c4c9a3dcbaf24323eb62322cf49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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;
}