1/** @addtogroup MCD_MCDIMPL_DAEMON_SRV
2 * @{
3 * @file
4 *
5 * Connection data.
6 *
7 * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 -->
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote
18 *    products derived from this software without specific prior
19 *    written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33#ifndef CONNECTION_H_
34#define CONNECTION_H_
35
36#include <list>
37#include <exception>
38
39#include <inttypes.h>
40
41#include <sys/types.h>
42#include <sys/socket.h>
43#include <sys/un.h>
44
45
46class Connection
47{
48
49public:
50    struct sockaddr_un remote; /**< Remote address */
51    int32_t socketDescriptor; /**< Local socket descriptor */
52    void *connectionData; /**< reference to data related with the connection */
53    bool detached; /**< Connection state */
54
55    Connection(
56        void
57    );
58
59    Connection(
60        int          socketDescriptor,
61        sockaddr_un  *remote
62    );
63
64    virtual ~Connection(
65        void
66    );
67
68    /**
69     * Connect to destination.
70     *
71     * @param Destination pointer.
72     * @return true on success.
73     */
74    virtual bool connect(
75        const char *dest
76    );
77
78    /**
79     * Read bytes from the connection.
80     *
81     * @param buffer    Pointer to destination buffer.
82     * @param len       Number of bytes to read.
83     * @param timeout   Timeout in milliseconds
84     * @return Number of bytes read.
85     * @return -1 if select() failed (returned -1)
86     * @return -2 if no data available, i.e. timeout
87     */
88    virtual size_t readData(void *buffer, uint32_t len, int32_t timeout);
89
90    /**
91     * Read bytes from the connection.
92     *
93     * @param buffer    Pointer to destination buffer.
94     * @param len       Number of bytes to read.
95     * @return Number of bytes read.
96     */
97    virtual size_t readData(void *buffer, uint32_t len);
98
99    /**
100     * Write bytes to the connection.
101     *
102     * @param buffer    Pointer to source buffer.
103     * @param len       Number of bytes to read.
104     * @return Number of bytes written.
105     * @return -1 if written bytes not equal to len.
106     */
107    virtual size_t writeData(void *buffer, uint32_t len);
108
109    /**
110     * Wait for data to be available.
111     *
112     * @param timeout   Timeout in milliseconds
113     * @return 0 if data is available
114     * @return error code if otherwise
115     */
116    virtual int waitData(int32_t timeout);
117
118};
119
120typedef std::list<Connection *>         connectionList_t;
121typedef connectionList_t::iterator     connectionIterator_t;
122
123
124#endif /* CONNECTION_H_ */
125
126/** @} */
127