summaryrefslogtreecommitdiff
path: root/include/mts/MTS_IO_Connection.h
blob: f90b6c977d034a829a348391f9a547cdcb042a0d (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
/*
 * 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.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 <mts/MTS_AutoPtr.h>
#include <mts/MTS_Lock.h>
#include <mts/MTS_NonCopyable.h>
#include <string>

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<Lock> m_apLock;
            AutoPtr<Lock> m_apReadLock;
            AutoPtr<Lock> 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_ */