Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / connection.h
blob45fca5163f58f44c2f484fc2fdc914ece215c490
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef CONNECTION_H
12 #define CONNECTION_H
14 #include <QObject>
16 class QTextStream;
17 class QIODevice;
18 class QFile;
19 class QHostInfo;
21 /**
22 * @brief Manage a connection with a remote server.
24 * This class can be used to manage a connection with a chess server. Once connected,
25 * it emits the signal @ref receivedLine whenever a new whole line is received.
26 * Partial lines are kept in an internal buffer.
27 * The Connection class supports the use of a helper such as timeseal.
29 class Connection : public QObject {
30 Q_OBJECT
31 QIODevice* m_device;
32 QString buffer;
33 QFile* logFile;
34 QTextStream* logStream;
35 bool m_connected;
36 bool m_initialized;
37 QString m_unsent_text;
39 char m_host[256];
40 quint16 m_port;
41 QString m_helper;
42 QString m_helper_cmd;
44 public:
45 /**
46 * Create a new connection object. No actual connection is made
47 * until @ref establish is called.
49 Connection();
51 /**
52 * Destroy the object and kill the helper, if present.
54 virtual ~Connection();
56 /**
57 * Create an actual connection to an internet host.
58 * @param host Fully qualified name of the host.
59 * @param port Host port to connect to.
60 * @param helper Full path of a helper. A null or empty string if no helper is needed.
61 * @param args Helper arguments. They can optionally contain variabiles which will be expaded
62 * before they are passed to the helper.
63 * Supported variables: $(HOST_NAME), $(HOST_IP), $(PORT).
65 void establish(const char* host, quint16 port,
66 const QString& helper, const QString& args);
68 /**
69 * Whether the connection has been initialized.
71 inline bool initialized() const { return m_initialized; }
73 /**
74 * Set the inizialized flag of the connection. Use this after you have
75 * sent initial commands.
77 inline void setInitialized() { m_initialized = true; }
79 public Q_SLOTS:
80 /**
81 * Send some text to the server using the connection. A newline will be sent afterwards.
83 void sendText(const QString&);
84 void lookedUp(const QHostInfo &host);
86 protected Q_SLOTS:
87 /**
88 * Read data from the socket and appropriate signals.
90 void processLine();
92 Q_SIGNALS:
93 void hostLookup();
94 void hostFound();
95 void established();
97 /**
98 * Emitted when a partial line is received.
100 * @param text Received text.
101 * @param newDataOffset Offset where new data starts.
102 * @note Whenever a partial line is received, is it recorded in an internal buffer,
103 * and an offset is incremented, to distinguish new data from old data
104 * as the next payload arrives.
106 void receivedText(QString text, int newDataOffset);
109 * Emitted when a full line is received.
111 * @param line Received line.
112 * @param newDataOffset Offset where new data starts.
114 * @sa receivedText
116 void receivedLine(QString line, int newDataOffset);
119 #endif // CONNECTION_H