NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxSocketConnection.h
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #ifndef CXSOCKETCONNECTION_H
13 #define CXSOCKETCONNECTION_H
14 
15 #include <QPointer>
16 #include <QTcpServer>
17 #include <QMutex>
18 #include "cxEnumConverter.h"
19 #include "boost/shared_ptr.hpp"
20 
21 namespace cx {
23 {
29 };
30 } // namespace cx
31 
33 
37 #define assertRunningInObjectThread() \
38 { \
39  if (QThread::currentThread()!=this->thread()) \
40  { \
41  CX_LOG_ERROR() \
42  << QString("Method should be called in the object's thread [%1] only, current thread = [%2]") \
43  .arg(this->thread()->objectName()) \
44  .arg(QThread::currentThread()->objectName()); \
45  } \
46 } \
47 
48 
49 
50 namespace cx {
51 
52 typedef boost::shared_ptr<class SocketConnector> SocketConnectorPtr;
53 typedef boost::shared_ptr<class SocketConnection> SocketConnectionPtr;
54 
55 class cxResource_EXPORT SocketConnection : public QObject
56 {
57  Q_OBJECT
58 public:
59  explicit SocketConnection(QObject *parent = 0);
60 
61  struct cxResource_EXPORT ConnectionInfo
62  {
63  QString role;
64  QString protocol;
65  QString host;
66  int port;
67 
68  bool operator==(const ConnectionInfo& rhs) const;
69  bool isServer() const;
70  bool isClient() const;
71  bool isLocalhostConnection() const;
72  QString getDescription() const;
73  };
74 
79  ConnectionInfo getConnectionInfo();
80  CX_SOCKETCONNECTION_STATE getState();
81  virtual void setConnectionInfo(ConnectionInfo info);
82 
83  virtual void requestConnect();
84  virtual void requestDisconnect();
85 
86  bool socketReceive(void *packPointer, int packSize) const;
87 
88 public slots:
89  bool sendData(const char* data, qint64 maxSize);
90 
91 signals:
92  void connectionInfoChanged();
93  void stateChanged(CX_SOCKETCONNECTION_STATE status);
94  void connected();
95  void disconnected();
96  void error();
97  void dataAvailable();
98 
99 private slots:
100  void internalConnected();
101  void internalDisconnected();
102  void internalError(QAbstractSocket::SocketError socketError);
103  virtual void internalDataAvailable();
104 
105 protected:
106  virtual void setProtocol(QString protocolname);
107  SocketConnectorPtr createConnector(ConnectionInfo info);
108  bool socketIsConnected();
109  bool enoughBytesAvailableOnSocket(int bytes) const;
110  QStringList getAllServerHostnames();
111  void setCurrentConnectionInfo();
112  void stateChange(CX_SOCKETCONNECTION_STATE newState);
113  CX_SOCKETCONNECTION_STATE computeState();
114 
115  QTcpSocket* mSocket;
116 
120 
121 protected:
123 };
124 
125 } //namespace cx
126 
127 #endif // CXSOCKETCONNECTION_H
cx::scsCOUNT
@ scsCOUNT
Definition: cxSocketConnection.h:28
cx::SocketConnection::mConnector
SocketConnectorPtr mConnector
Definition: cxSocketConnection.h:122
cx::SocketConnectionPtr
boost::shared_ptr< class SocketConnection > SocketConnectionPtr
Definition: cxSocketConnection.h:53
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::SocketConnection::mCurrentState
CX_SOCKETCONNECTION_STATE mCurrentState
Definition: cxSocketConnection.h:117
cx::SocketConnection::ConnectionInfo::protocol
QString protocol
Definition: cxSocketConnection.h:64
SNW_DECLARE_ENUM_STRING_CONVERTERS
SNW_DECLARE_ENUM_STRING_CONVERTERS(cxResource_EXPORT, cx, CX_SOCKETCONNECTION_STATE)
cx::SocketConnection::mNextConnectionInfo
ConnectionInfo mNextConnectionInfo
info to be used for the next connect(), mutexed.
Definition: cxSocketConnection.h:119
cx::SocketConnection::ConnectionInfo::host
QString host
Definition: cxSocketConnection.h:65
cx::SocketConnection::ConnectionInfo
Definition: cxSocketConnection.h:61
cx::scsLISTENING
@ scsLISTENING
Definition: cxSocketConnection.h:26
cx::scsINACTIVE
@ scsINACTIVE
Definition: cxSocketConnection.h:24
cx::SocketConnection
Definition: cxSocketConnection.h:55
cxEnumConverter.h
cx::scsCONNECTED
@ scsCONNECTED
Definition: cxSocketConnection.h:25
cx::SocketConnection::mNextConnectionInfoMutex
QMutex mNextConnectionInfoMutex
Definition: cxSocketConnection.h:118
cx::SocketConnection::ConnectionInfo::role
QString role
Definition: cxSocketConnection.h:63
cx::SocketConnection::mSocket
QTcpSocket * mSocket
Definition: cxSocketConnection.h:115
cx::scsCONNECTING
@ scsCONNECTING
Definition: cxSocketConnection.h:27
cx::operator==
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
Definition: cxRegistrationTransform.cpp:151
cx::SocketConnectorPtr
boost::shared_ptr< class SocketConnector > SocketConnectorPtr
Definition: cxSocketConnection.h:52
cx::CX_SOCKETCONNECTION_STATE
CX_SOCKETCONNECTION_STATE
Definition: cxSocketConnection.h:22
cx::SocketConnection::ConnectionInfo::port
int port
Definition: cxSocketConnection.h:66