summaryrefslogtreecommitdiffstats
path: root/Master/Masterarbeit/src/XorayaPluginExecutor/TcpConnection.h
blob: 731cd984a8f55e2a3b728dde6084e191fb03ba4e (plain)
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
/*!
 * \file TcpConnection.h
 * \author S. Eisenhauer
 * \date 27.10.2011
 * \brief Header of CTcpConnection
 */
#ifndef CTCPCONNECTION_H_
#define CTCPCONNECTION_H_

#include <boost/asio.hpp>
#include <boost/intrusive/set.hpp>
#include <boost/circular_buffer.hpp>
#include <x2e/sys/Mutex.hpp>
#include <CNetworkStream.h>
#include <NetworkTypes.h>
#include "protocol.h"

/// maximum number of network connections
#define nMAX_CONNECTIONS (16)

/// size of buffer for logged message to upload over network
#define nUPLOAD_BUFFERSIZE (16)

/// forward declaration of main application type
class CPluginExecutor;

/// type definition for buffer of logged messages to upload
typedef boost::circular_buffer<tstLogMessage> tUploadBuffer;

/// network communication
class CTcpConnection :
	public boost::intrusive::set_base_hook<>
{
public:
	/// type definition for object pointer
  typedef CTcpConnection* tConnectionPtr;

  static tConnectionPtr pxCreate(boost::asio::io_service& xIoService, CPluginExecutor* pxExecutor);
  void vUploadLogMessage(const tstLogMessage& stLogMsg);

  boost::asio::ip::tcp::socket& xGetSocket();
  void vWaitForClientRequest();
  /*!
   * \brief set the activation state of tcp connection object
   * \param[in] boActive activation state
   */
  void vSetActive(bool boActive)
  {
	  m_boActive = boActive;
  }
  /*!
   * \brief get the activation state of a tcp sonnection
   * \return TRUE if connection is active, FALSE if not.
   */
  bool boIsActive()
  {
	  return m_boActive;
  }
  
  /// operator less for connection objects, needed for ordering in the container
  friend bool operator<(const CTcpConnection& a, const CTcpConnection& b)
  {
  	return &a < &b;
  }

  /// destructor
  ~CTcpConnection(void);

private:
	tNetworkBuffer m_xRxBuff; //!< request buffer
	tNetworkBuffer m_xTxBuff; //!< response buffer
	CNetworkInStream m_xInStream; //!< instream for receive buffer
	CNetworkOutStream m_xOutStream; //!< outstream for transmit buffer
	CNetworkDatacontainer m_xNetworkDatacontainer; //!< datacontainer for data from and to network
    boost::asio::ip::tcp::socket m_xSocket; //!< socket of this connection
    CPluginExecutor* m_pxExecutor; //!< pointer to main application
    uint8_t m_u8ConNum; //!< number of this connection
    bool m_boActive; //!< activation state of this object
    tUploadBuffer m_xUploadBuffer; //!< buffer for logged messages, that should be uploaded over this conenction
    x2e::Mutex m_xUploadBufferMutex; //!< mutex to protect upload buffer from concurrent access
    int32_t m_i32UploadBufferUnread; //!< number of elements in upload buffer
    bool m_boUploadActive; //!< is an upload of logged messages active yet

	/// constructor
    CTcpConnection(boost::asio::io_service& io_service, CPluginExecutor* pxExecutor, uint8_t u8ConNum);
    void vWriteHandler(const boost::system::error_code& err, size_t bytesTransferred);
    void vReadHandler(const boost::system::error_code& err, size_t bytesTransferred);
	
    void vUploadNextMessage();
};

/// type definition for connection container
typedef boost::intrusive::set<CTcpConnection> tConnectionSet;

/// type definition for connection container iterator
typedef boost::intrusive::set<CTcpConnection>::iterator tConIter;

/// global connection container
extern tConnectionSet g_astActiveConnections;

#endif /*CTCPCONNECTION_H_*/