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
/* 
 * File:   DebugCommunicator.h
 * Author: thomas
 *
 * Created on 8. march 2009, 19:31
 */

#ifndef _DEBUGCOMMUNICATOR_H
#define _DEBUGCOMMUNICATOR_H

#include <gio/gio.h>

/**
* TCP/IP based debug communication.
* Please note that this class is not thread-safe, use an external
* synchronization mechanism to ensure that only one threads interacts with
* the communicator at the same time.
*/
class DebugCommunicator
{
public:
  DebugCommunicator();
  virtual ~DebugCommunicator();

  /**
  * Inits the DebugCommunicator, binds and listens to its underlying socket.
  *
  * @param port The port the socket will listen to.
  */
  void init(unsigned short port);<--- Function 'init' argument 1 names different: declaration 'port' definition 'portNum'.

  /**
  * Accept a connection made by the outside world.
  *
  * @param timeout Time in seconds which should be waited for connection.
  *                 0 means forever.
  *
  * @return True if connection was etablished. False on error.
  */
  bool connect(int timeout);
  /**
  * Disconnects a existing connection.
  */
  void disconnect();

  bool isConnected();

  /**
  * Send a message. Will block until whole message was send.
  *
  * @param raw data to send
  * @param size size of the data
  *
  * @return True if message could be send, false if some error occured.
  */
  bool sendMessage(gint32 id, const char* data, size_t size);

  /**
  * Read a new message. Blocks until a complete message was received or an
  * error occured.
  * Complete Messages are always ended by a "\n" character (which is not part
  * of the result)
  *
  * @return A null terminated message string or NULL if nothing to read after timeout
  */
  GString* readMessage(gint32& id);

  /** 
  * Set the time after which the connection should be closed in case of inactivity.
  * 0 - means the connection never times out.
  */
  void setTimeOut(unsigned int t) {
    this->time_out_delta = t;
  }

private:
  GSocket* serverSocket;
  GSocket* connection;

  unsigned short port;
  bool fatalFail;
  unsigned long long time_of_last_message;
  unsigned int time_out_delta;

  GError* internalSendMessage(gint32 id, const char* data, size_t size);
  GError* internalInit();
  void closeServerSocket();
  gint32 internalReadMessage(GString** result, GError** err);
};

#endif  /* _DEBUGCOMMUNICATOR_H */