CustusX  2021.04.21-dev+develop.337f9
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 
54 class cxResource_EXPORT SocketConnection : public QObject
55 {
56  Q_OBJECT
57 public:
58  explicit SocketConnection(QObject *parent = 0);
59 
60  struct cxResource_EXPORT ConnectionInfo
61  {
62  QString role;
63  QString protocol;
64  QString host;
65  int port;
66 
67  bool operator==(const ConnectionInfo& rhs) const;
68  bool isServer() const;
69  bool isClient() const;
70  bool isLocalhostConnection() const;
71  QString getDescription() const;
72  };
73 
78  ConnectionInfo getConnectionInfo();
79  CX_SOCKETCONNECTION_STATE getState();
80  virtual void setConnectionInfo(ConnectionInfo info);
81 
82  virtual void requestConnect();
83  virtual void requestDisconnect();
84 
85 public slots:
86  bool sendData(const char* data, qint64 maxSize);
87 
88 signals:
89  void connectionInfoChanged();
90  void stateChanged(CX_SOCKETCONNECTION_STATE status);
91  void connected();
92  void disconnected();
93  void error();
94 
95 private slots:
96  void internalConnected();
97  void internalDisconnected();
98  void internalError(QAbstractSocket::SocketError socketError);
99  virtual void internalDataAvailable() = 0;
100 
101 protected:
102  virtual void setProtocol(QString protocolname) = 0;
103  SocketConnectorPtr createConnector(ConnectionInfo info);
104  bool socketIsConnected();
105  bool enoughBytesAvailableOnSocket(int bytes) const;
106  bool socketReceive(void *packPointer, int packSize) const;
107  QStringList getAllServerHostnames();
108  void setCurrentConnectionInfo();
109  void stateChange(CX_SOCKETCONNECTION_STATE newState);
110  CX_SOCKETCONNECTION_STATE computeState();
111 
112  QTcpSocket* mSocket;
113 
117 
118 protected:
119  SocketConnectorPtr mConnector;
120 };
121 
122 } //namespace cx
123 
124 #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 SocketConnector > SocketConnectorPtr
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
SocketConnectorPtr mConnector
Namespace for all CustusX production code.