File indexing completed on 2024-04-28 16:59:42
0001 /* 0002 Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LGPL. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 0019 Alternatively, this file is available under the Mozilla Public License 0020 Version 1.1. You may obtain a copy of the License at 0021 http://www.mozilla.org/MPL/ 0022 */ 0023 0024 #ifndef CONNECTION_H 0025 #define CONNECTION_H 0026 0027 #include "commutex.h" 0028 #include "types.h" 0029 0030 #include <string> 0031 0032 class Connection; 0033 class ConnectAddress; 0034 class ConnectionPrivate; 0035 class Error; 0036 class EventDispatcher; 0037 class IConnectionStateListener; 0038 class IMessageReceiver; 0039 class ITransport; 0040 class Message; 0041 class PendingReply; 0042 class Server; 0043 0044 class DFERRY_EXPORT Connection 0045 { 0046 public: 0047 enum State 0048 { 0049 Unconnected = 0, 0050 Connecting, 0051 Connected 0052 }; 0053 0054 enum ThreadAffinity 0055 { 0056 MainConnection = 0, 0057 ThreadLocalConnection 0058 }; 0059 0060 // Reference for passing to another thread; it guarantees that the target Connection 0061 // either exists or not, but is not currently being destroyed. Yes, the data is all private. 0062 class CommRef 0063 { 0064 friend class Connection; 0065 ConnectionPrivate *connection; 0066 CommutexPeer commutex; 0067 }; 0068 0069 // for connecting to the session or system bus 0070 Connection(EventDispatcher *dispatcher, const ConnectAddress &connectAddress); 0071 // for reusing the connection of a Connection in another thread 0072 Connection(EventDispatcher *dispatcher, CommRef otherConnection); 0073 0074 Connection(Connection &&other); 0075 Connection &operator=(Connection &&other); 0076 0077 ~Connection(); 0078 Connection(Connection &other) = delete; 0079 Connection &operator=(Connection &other) = delete; 0080 0081 State state() const; 0082 void close(); 0083 0084 CommRef createCommRef(); 0085 0086 uint32 supportedFileDescriptorsPerMessage() const; 0087 0088 void setDefaultReplyTimeout(int msecs); 0089 int defaultReplyTimeout() const; 0090 enum TimeoutSpecialValues { 0091 DefaultTimeout = -1, 0092 NoTimeout = -2 0093 }; 0094 // if a message expects no reply, that is not absolutely binding; this method allows to send a message that 0095 // does not expect (request) a reply, but we get it if it comes - not terribly useful in most cases 0096 // NOTE: this takes ownership of the message! The message will be deleted after sending in some future 0097 // event loop iteration, so it is guaranteed to stay valid before the next event loop iteration. 0098 PendingReply send(Message m, int timeoutMsecs = DefaultTimeout); 0099 // Mostly same as above. 0100 // This one ignores the reply, if any. Reports any locally detectable errors in the return value. 0101 Error sendNoReply(Message m); 0102 0103 size_t sendQueueLength() const; 0104 0105 void waitForConnectionEstablished(); 0106 ConnectAddress connectAddress() const; 0107 std::string uniqueName() const; 0108 bool isConnected() const; 0109 0110 EventDispatcher *eventDispatcher() const; 0111 0112 // TODO matching patterns for subscription; note that a signal requires path, interface and 0113 // "method" (signal name) of sender 0114 void subscribeToSignal(); 0115 0116 IMessageReceiver *spontaneousMessageReceiver() const; 0117 void setSpontaneousMessageReceiver(IMessageReceiver *receiver); 0118 0119 IConnectionStateListener *connectionStateListener() const; 0120 void setConnectionStateListener(IConnectionStateListener *listener); 0121 0122 private: 0123 friend class Server; 0124 // called from Server 0125 Connection(ITransport *transport, EventDispatcher *eventDispatcher, const ConnectAddress &address); 0126 0127 friend class ConnectionPrivate; 0128 ConnectionPrivate *d; 0129 }; 0130 0131 #endif // CONNECTION_H