QxtRPCPeer

From qtnode

Jump to: navigation, search

QxtRPCPeer is a tool for transmitting Qt signals over a network connection. It can operate in peer-to-peer mode or client-server mode. (header implementation)

All data types used in attached signals and slots must be declared and registered with QMetaType using Q_DECLARE_METATYPE and qRegisterMetaType, and they must have stream operators registered with qRegisterMetaTypeStreamOperators.

Feel free to ask questions on the Talk page.

Contents

Member Type Documentation

enum QxtRPCPeer::RPCTypes

This enum is used with the setRPCType() to describe the role played in a connection. It is also returned by rpcType().

Constant Value Description
QxtRPCPeer::Server 0x0 Listen for clients and accept multiple connections.
QxtRPCPeer::Client 0x1 Connect to a server.
QxtRPCPeer::Peer 0x2 Listen for a connection or connect to a peer.

Public Member Functions

QxtRPCPeer(QObject* parent = 0)

Creates a QxtRPCPeer object with the given parent.

void setRPCType(RPCTypes type)

Sets the RPC type. Attempting to change the RPC type while listening or connected will be ignored with a warning.

RPCTypes rpcType()

Gets the current RPC type.

void connect(QHostAddress addr, int port = 80)

Connects to the specified peer or server on the selected port. When the connection is complete, the peerConnected() signal will be emitted. If an error occurs, the peerError() signal will be emitted.

void listen(QHostAddress iface = QHostAddress::Any, int port = 80)

Listens on the specified interface on the specified port for connections. Attempting to listen while in Client mode or while connected in Peer mode will be ignored with a warning.

In Peer mode, only one connection can be active at a time. Additional incoming connections while connected to a peer will be dropped. When a peer connects, the peerConnected() signal will be emitted.

In Server mode, multiple connections can be active at a time. Each client that connects will be provided a unique ID, included in the clientConnected() signal that will be emitted.

void disconnectPeer(int id = -1)

Disconnects from a server, client, or peer. Servers must provide a client ID, provided by the clientConnected() signal; clients and peers must not.

void disconnectAll()

Disconnects from all clients, or from the server or peer.

void stopListening()

Stops listening for connections. Any connections still open will remain connected.

void attachSignal(QObject* sender, const char* signal, QString rpcFunction = QString())

Attaches the given signal. When the attached signal is emitted, it will be transmitted to all connected servers, clients, or peers. If an optional rpcFunction is provided, it will be used as the name of the transmitted signal instead.

Use the SIGNAL() macro to specify the signal, just as you would for QObject::connect().

void attachSlot(QString rpcFunction, QObject* receiver, const char* slot)

Attaches the given slot. When a signal with the name given by rpcFunction is received from the network, the attached slot is executed.

Use the SLOT() macro to specify the slot, just as you would for QObject::connect().

Note: In Server mode, the first parameter of the slot must be int id. The parameters of the signal follow. For example, SIGNAL(mySignal(QString)) from the client connects to SLOT(mySlot(int, QString)) on the server.

void detachObject(QObject* obj)

Detaches all signals and slots for the given object.

Public Slots

void call(QString fn, QVariant p1 = QVariant(), ...)

This function accepts up to 9 QVariant parameters. Sends the signal fn with the given parameter list to the server or peer. The receiver is not obligated to act upon the signal. If no server or peer is connected, the call is ignored.

void callClient(int id, QString fn, QVariant p1 = QVariant(), ...)

This function accepts up to 8 QVariant parameters. Sends the signal fn with the given parameter list to the selected client. The receiver is not obligated to act upon the signal. If no client with the given ID is connected, the call will be ignored with a warning.

void detachSender()

Detaches all signals and slots for the object that emitted the signal connected to detachSender().

Signals

void peerConnected()

This signal is emitted after a successful connection to or from a peer or server.

void clientConnected(int id)

This signal is emitted after a successful connection from a client. The given ID is used for callClient() and disconnectPeer().

void peerDisconnected()

This signal is emitted when a peer or server is disconnected.

void clientDisconnected(int id)

This signal is emitted when a client disconnects. The given ID is no longer valid.

void peerError(QAbstractSocket::SocketError)

This signal is emitted whenever an error occurs on a socket. Currently, no information about the socket that raised the error is available.

Licensing

QxtRPCPeer is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2 or greater, or under the terms of the GNU Lesser General Public License, version 2.1 or greater, or under the terms of the Q Public License as published by Trolltech.

This disjunctive license follows the license of the linked Qt toolkit. Specifically, if you use Qt under the GPL, QxtRPCPeer is licensed to you under the GPL. If you use Qt under the QPL, this is licensed to you under the QPL. If you use Qt with a commercial license purchased from Trolltech, this is licensed to you under the LGPL.

QxtRPCPeer 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 appropriate license agreement for more details.

Personal tools