Blame view

src/tcpsocket.h 5.51 KB
99048c1c   Steven de Ridder   Initial commit. d...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  /* ****************************************************************************
   * Copyright 2019 Open Systems Development BV                                 *
   *                                                                            *
   * Permission is hereby granted, free of charge, to any person obtaining a    *
   * copy of this software and associated documentation files (the "Software"), *
   * to deal in the Software without restriction, including without limitation  *
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
   * and/or sell copies of the Software, and to permit persons to whom the      *
   * Software is furnished to do so, subject to the following conditions:       *
   *                                                                            *
   * The above copyright notice and this permission notice shall be included in *
   * all copies or substantial portions of the Software.                        *
   *                                                                            *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
   * DEALINGS IN THE SOFTWARE.                                                  *
   * ***************************************************************************/
  
  #ifndef OSDEV_COMPONENTS_CTCPSOCKET_H
  #define OSDEV_COMPONENTS_CTCPSOCKET_H
  
  #include <QObject>
  //#include <QTimer>
  //#include <QTcpSocket>
  //#include <QHostAddress>
  //#include <QDataStream>
  //#include <QString>
  
  #include <memory>
  
  class QTcpSocket;
  class QTimer;
  
  namespace osdev  {
  namespace components {
  
  /**
   * @brief Wrapper around QTcpSocket
   */
  class TcpSocket : public QObject
  {
      Q_OBJECT
  
  public:
      /**
       * @brief Create a TcpSocket connecting to the given address
       * @param ipAddress Host to connect to
       * @param portNumber Port number to connect at
       */
      TcpSocket( const QString& ipAddress, int portNumber );
  
      /**
       * @brief Create a TcpSocket based on an existing QTcpSocket
       * @param pSocket Socket used. This class does NOT take ownership of the
       *                provided socket.
       */
      TcpSocket( QTcpSocket *pSocket );
  
      /// Deleted copy-constructor
      TcpSocket(const TcpSocket&) = delete;
      /// Deleted assignment operator
      TcpSocket& operator=(const TcpSocket&) = delete;
      /// Deleted move-constructor
      TcpSocket(TcpSocket&&) = delete;
      /// Deleted move operator
      TcpSocket& operator=(TcpSocket&&) = delete;
  
      /// @brief Destructor
      ~TcpSocket();
  
      /**
       * @brief Retrieve the last error registered in the socket
       * @return Error string
       */
      QString     showError();
  
      /**
       * @brief See if a connection is available
       * @return True if a connection is available, false otherwise
       */
      bool        isConnected();
  
  public slots:
      /**
       * @brief Slot called to send data
       * @param sData Data to send
       * @param pSocket Socket to which to send. If it is not equal to this, the
       *                slot does nothing and returns
       */
      void        slotSendData( const QString &sData, TcpSocket *pSocket );
  
      /**
       * @brief Updates the current QTcpSocket used
       * @param pSocket New socket
       */
      void        slotSetSocket( QTcpSocket *pSocket );
  
      /// @brief Slot called when a socket gets connected
      void        slotConnected();
  
      /// @brief Slot called when a socket gets disconnected
      void        slotDisconnected();
  
  signals:
      /**
       * @brief Signal emitted when data is received
       * @param sData Block of data received
       * @param pSocket Receiving socket
       */
      void        signalDataReceived( const QString &sData, TcpSocket *pSocket );
  
      /**
       * @brief Signal emitted when data was sent
       * @param pSocket Sending socket
       */
      void        signalDataSent( TcpSocket *pSocket );
  
      /**
       * @brief Signal emitted when data is being received
       * @param pSocket Receiving socket
       */
      void        signalReceivingData( TcpSocket *pSocket );
  
      /**
       * @brief Signal emitted when a socket gets connected
       * @param pSocket Socket that gets connected
       */
      void        signalConnected( TcpSocket *pSocket );
  
      /**
       * @brief Signal emitted when a socket gets disconnected
       * @param pSocket Socket that gets disconnected
       */
      void        signalDisconnected( TcpSocket *pSocket );
  
  private slots:
      /// @brief Slot called when a socket is ready to read
      void        slotReadyRead();
  
      /// @brief Emits the signalDataSent when all data was sent
      void        chkSendBuffer();
  
  private:
      /// @brief Connects the socket signals to the slots of this class
      void        connectSocketSignals();
  
      /// @brief Disconnects the socket signals to the slots of this class
      void        disconnectSocketSignals();
  
      QTcpSocket              *m_pSocket;     ///< Current communication socket
      int                     m_blockSize;    ///< Maximum block size
      QString                 m_dataBuffer;   ///< Data to be sent
  
      std::unique_ptr<QTimer> m_pTimer;       ///< Periodic check if all data was sent
  };
  
  } // End namespace components
  } // End namespace osdev 
  
  #endif // OSDEV_COMPONENTS_CTCPSOCKET_H