CustusX  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:
122  SocketConnectorPtr mConnector;
123 };
124 
125 } //namespace cx
126 
127 #endif // CXSOCKETCONNECTION_H
CX_SOCKETCONNECTION_STATE
ConnectionInfo mNextConnectionInfo
info to be used for the next connect(), mutexed.
SNW_DECLARE_ENUM_STRING_CONVERTERS(cxResource_EXPORT, cx, CX_SOCKETCONNECTION_STATE)
CX_SOCKETCONNECTION_STATE mCurrentState
boost::shared_ptr< class SocketConnection > SocketConnectionPtr
boost::shared_ptr< class SocketConnector > SocketConnectorPtr
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
SocketConnectorPtr mConnector
Namespace for all CustusX production code.