1 | /****************************************************************************
|
---|
2 | ** $Id: qsocket.h 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Definition of QSocket class.
|
---|
5 | **
|
---|
6 | ** Created : 970521
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the network module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition licenses may use this
|
---|
22 | ** file in accordance with the Qt Commercial License Agreement provided
|
---|
23 | ** with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #ifndef QSOCKET_H
|
---|
39 | #define QSOCKET_H
|
---|
40 |
|
---|
41 | #ifndef QT_H
|
---|
42 | #include "qobject.h"
|
---|
43 | #include "qiodevice.h"
|
---|
44 | #include "qhostaddress.h" // int->QHostAddress conversion
|
---|
45 | #endif // QT_H
|
---|
46 |
|
---|
47 | #if !defined( QT_MODULE_NETWORK ) || defined( QT_LICENSE_PROFESSIONAL ) || defined( QT_INTERNAL_NETWORK )
|
---|
48 | #define QM_EXPORT_NETWORK
|
---|
49 | #else
|
---|
50 | #define QM_EXPORT_NETWORK Q_EXPORT
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #ifndef QT_NO_NETWORK
|
---|
54 | class QSocketPrivate;
|
---|
55 | class QSocketDevice;
|
---|
56 |
|
---|
57 |
|
---|
58 | class QM_EXPORT_NETWORK QSocket : public QObject, public QIODevice
|
---|
59 | {
|
---|
60 | Q_OBJECT
|
---|
61 | public:
|
---|
62 | enum Error {
|
---|
63 | ErrConnectionRefused,
|
---|
64 | ErrHostNotFound,
|
---|
65 | ErrSocketRead
|
---|
66 | };
|
---|
67 |
|
---|
68 | QSocket( QObject *parent=0, const char *name=0 );
|
---|
69 | virtual ~QSocket();
|
---|
70 |
|
---|
71 | enum State { Idle, HostLookup, Connecting,
|
---|
72 | Connected, Closing,
|
---|
73 | Connection=Connected };
|
---|
74 | State state() const;
|
---|
75 |
|
---|
76 | int socket() const;
|
---|
77 | virtual void setSocket( int );
|
---|
78 |
|
---|
79 | QSocketDevice *socketDevice();
|
---|
80 | virtual void setSocketDevice( QSocketDevice * );
|
---|
81 |
|
---|
82 | #ifndef QT_NO_DNS
|
---|
83 | virtual void connectToHost( const QString &host, Q_UINT16 port );
|
---|
84 | #endif
|
---|
85 | QString peerName() const;
|
---|
86 |
|
---|
87 | // Implementation of QIODevice abstract virtual functions
|
---|
88 | bool open( int mode );
|
---|
89 | void close();
|
---|
90 | void flush();
|
---|
91 | Offset size() const;
|
---|
92 | Offset at() const;
|
---|
93 | bool at( Offset );
|
---|
94 | bool atEnd() const;
|
---|
95 |
|
---|
96 | Q_ULONG bytesAvailable() const; // ### QIODevice::Offset instead?
|
---|
97 | Q_ULONG waitForMore( int msecs, bool *timeout ) const;
|
---|
98 | Q_ULONG waitForMore( int msecs ) const; // ### Qt 4.0: merge the two overloads
|
---|
99 | Q_ULONG bytesToWrite() const;
|
---|
100 | void clearPendingData();
|
---|
101 |
|
---|
102 | Q_LONG readBlock( char *data, Q_ULONG maxlen );
|
---|
103 | Q_LONG writeBlock( const char *data, Q_ULONG len );
|
---|
104 | Q_LONG readLine( char *data, Q_ULONG maxlen );
|
---|
105 |
|
---|
106 | int getch();
|
---|
107 | int putch( int );
|
---|
108 | int ungetch(int);
|
---|
109 |
|
---|
110 | bool canReadLine() const;
|
---|
111 | virtual QString readLine();
|
---|
112 |
|
---|
113 | Q_UINT16 port() const;
|
---|
114 | Q_UINT16 peerPort() const;
|
---|
115 | QHostAddress address() const;
|
---|
116 | QHostAddress peerAddress() const;
|
---|
117 |
|
---|
118 | void setReadBufferSize( Q_ULONG );
|
---|
119 | Q_ULONG readBufferSize() const;
|
---|
120 |
|
---|
121 | signals:
|
---|
122 | void hostFound();
|
---|
123 | void connected();
|
---|
124 | void connectionClosed();
|
---|
125 | void delayedCloseFinished();
|
---|
126 | void readyRead();
|
---|
127 | void bytesWritten( int nbytes );
|
---|
128 | void error( int );
|
---|
129 |
|
---|
130 | protected slots:
|
---|
131 | virtual void sn_read( bool force=FALSE );
|
---|
132 | virtual void sn_write();
|
---|
133 |
|
---|
134 | private slots:
|
---|
135 | void tryConnecting();
|
---|
136 | void emitErrorConnectionRefused();
|
---|
137 |
|
---|
138 | private:
|
---|
139 | QSocketPrivate *d;
|
---|
140 |
|
---|
141 | bool consumeWriteBuf( Q_ULONG nbytes );
|
---|
142 | void tryConnection();
|
---|
143 | void setSocketIntern( int socket );
|
---|
144 |
|
---|
145 | private: // Disabled copy constructor and operator=
|
---|
146 | #if defined(Q_DISABLE_COPY)
|
---|
147 | QSocket( const QSocket & );
|
---|
148 | QSocket &operator=( const QSocket & );
|
---|
149 | #endif
|
---|
150 | };
|
---|
151 |
|
---|
152 | #endif //QT_NO_NETWORK
|
---|
153 | #endif // QSOCKET_H
|
---|