summaryrefslogtreecommitdiffstats
path: root/tdeio/tdeio/connection.h
blob: fb0b50e15a186a9537579bf64a83a11906a76cc9 (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
156
157
158
// -*- c++ -*-
/* This file is part of the KDE libraries
    Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
                       David Faure <faure@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef __connection_h__
#define __connection_h__

#include <tdelibs_export.h>

#include <sys/types.h>

#include <stdio.h>
#include <tqptrlist.h>
#include <tqobject.h>

class TDESocket;
class TQSocketNotifier;

namespace TDEIO {

    struct TDEIO_EXPORT Task {
	int cmd;
	TQByteArray data;
    };

    /**
     * This class provides a simple means for IPC between two applications
     * via a pipe.
     * It handles a queue of commands to be sent which makes it possible to
     * queue data before an actual connection has been established.
     */
    class TDEIO_EXPORT Connection : public TQObject
    {
	Q_OBJECT
    public:
	/**
	 * Creates a new connection.
	 * @see init()
	 */
	Connection();
	virtual ~Connection();

        /**
	 * Initialize this connection to use the given socket.
	 * @param sock the socket to use
	 * @see inited()
	 */
	void init(TDESocket *sock);
        /**
	 * Initialize the connection to use the given file
	 * descriptors.
	 * @param fd_in the input file descriptor to use
	 * @param fd_out the output file descriptor to use
	 * @see inited()
	 */
	void init(int fd_in, int fd_out); // Used by KDENOX
	void connect(TQObject *receiver = 0, const char *member = 0);
        /// Closes the connection.
	void close();
	
        /**
	 * Returns the input file descriptor.
	 * @return the input file descriptor
	 */
	int fd_from() const { return fd_in; }
        /**
	 * Returns the output file descriptor.
	 * @return the output file descriptor
	 */
        int fd_to() const { return fileno( f_out ); }

        /**
	 * Checks whether the connection has been initialized.
	 * @return true if the initialized
	 * @see init()
	 */
	bool inited() const { return (fd_in != -1) && (f_out != 0); }
	
        /** 
	 * Sends/queues the given command to be sent.
	 * @param cmd the command to set
	 * @param arr the bytes to send
	 */
	void send(int cmd, const TQByteArray &arr = TQByteArray());

        /** 
	 * Sends the given command immediately.
	 * @param _cmd the command to set
	 * @param data the bytes to send
	 * @return true if successful, false otherwise
	 */
	bool sendnow( int _cmd, const TQByteArray &data );

	/**
	 * Receive data.
	 *
	 * @param _cmd the received command will be written here
	 * @param data the received data will be written here
	 * @return >=0 indicates the received data size upon success
	 *         -1  indicates error
	 */
	int read( int* _cmd, TQByteArray &data );

        /**
         * Don't handle incoming data until resumed.
         */
        void suspend();

        /**
         * Resume handling of incoming data.
         */
        void resume();

        /**
         * Returns status of connection.
	 * @return true if suspended, false otherwise
         */
        bool suspended() const { return m_suspended; }

    protected slots:
        void dequeue();
	
    protected:
	
	
    private:
	int fd_in;
	FILE *f_out;
	TDESocket *socket;
	TQSocketNotifier *notifier;
	TQObject *receiver;
	const char *member;
	TQPtrList<Task> tasks;
        bool m_suspended;
    private:
	class ConnectionPrivate* d;
    };

}

#endif