CCIMXDesktop
 
Loading...
Searching...
No Matches
PeerConnection.h
1#pragma once
2#include "NetInfo.h"
3#include <QAbstractSocket>
4#include <QObject>
5class QTcpSocket;
6class QTcpServer;
7
8class PeerConnection : public QObject {
9 Q_OBJECT
10public:
15 enum class ConnectionState {
17 Listening,
20 };
21
22 Q_ENUM(ConnectionState); // register to the Qt Metas
23
24 explicit PeerConnection(QObject* parent);
26 Q_DISABLE_COPY_MOVE(PeerConnection);
27
28 bool availableSelfListen(const SelfInfo& info);
29 bool connectToPeer(const PeerInfo& peerInfo);
30
31 SelfInfo selfInfo() const;
36 PeerInfo peerInfo() const;
40 ConnectionState state() const;
41
47 bool send_data(const QByteArray& raw_data);
48
53 void disconnectFromPeer();
54
59 const QString uuid() const { return uuid_marker; }
60
61signals:
67
71 void connected();
72
77
82 void dataReceived(const QByteArray& data);
83
89
94 void errorOccurred(QAbstractSocket::SocketError socketError);
95
96private slots:
100 void handleNewConnection();
105 void onSocketStateChanged(QAbstractSocket::SocketState socketState);
110 void onSocketError(QAbstractSocket::SocketError socketError);
114 void processSocketRead();
115
116private:
121 void setupSocket(QTcpSocket* socket);
126 void setState(ConnectionState newState);
128 QTcpServer* server_socket {};
129 QTcpSocket* peer_socket {};
130 PeerInfo peer_info;
131 SelfInfo self_info;
132 const QString uuid_marker;
133};
socket positive
Definition PeerConnection.h:8
void bytesWritten(qint64 bytesWritten)
Emitted to report the result of a send operation.
ConnectionState state() const
Returns the current state of the connection.
Definition PeerConnection.cpp:69
ConnectionState
Defines the current state of the peer connection.
Definition PeerConnection.h:15
@ Connected
An active connection is established.
@ Listening
Listening for an incoming connection.
@ Connecting
Attempting to connect to a peer.
@ Disconnected
No active connection.
bool availableSelfListen(const SelfInfo &info)
listen for the P2P available
Definition PeerConnection.cpp:17
void errorOccurred(QAbstractSocket::SocketError socketError)
Emitted when a network error occurs.
void stateChanged(PeerConnection::ConnectionState state)
Emitted when the connection state changes.
PeerInfo peerInfo() const
Gets the information of the connected remote peer.
Definition PeerConnection.cpp:65
void connected()
Emitted when a connection is successfully established.
void disconnectFromPeer()
Closes the current connection and stops listening. Resets the object to its initial state.
Definition PeerConnection.cpp:83
void disconnected()
Emitted when the connection is terminated.
void dataReceived(const QByteArray &data)
Emitted when data is received from the peer.
bool send_data(const QByteArray &raw_data)
Asynchronously sends data to the connected peer.
Definition PeerConnection.cpp:73
const QString uuid() const
get the only one uuid
Definition PeerConnection.h:59
Definition NetInfo.h:10
Definition NetInfo.h:4