source: psi/trunk/cutestuff/rtsp/rtspbase.h@ 59

Last change on this file since 59 was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 4.8 KB
Line 
1/*
2 * rtspbase.h - very basic RTSP client/server
3 * Copyright (C) 2004 Justin Karneges
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21// TODO:
22// - only listen on the local interface (or let the caller specify the interface)
23// - client should not emit server replies unless the client actually sent a packet,
24// to keep things in sync
25// - if content-length maximum is exceeded, signal this fact?
26
27#ifndef RTSPBASE_H
28#define RTSPBASE_H
29
30#include <qstring.h>
31#include <qcstring.h>
32#include <qstringlist.h>
33#include <qvaluelist.h>
34#include <qobject.h>
35#include <qhostaddress.h>
36
37class ByteStream;
38
39namespace RTSP
40{
41 class Parser;
42 class Server;
43
44 struct Var
45 {
46 QString name, value;
47 };
48
49 // This should probably be a QMap, but it isn't, so there.
50 class HeaderList : public QValueList<Var>
51 {
52 public:
53 HeaderList();
54
55 bool has(const QString &var) const;
56 QString get(const QString &var) const;
57 void set(const QString &var, const QString &val);
58 void remove(const QString &var);
59 };
60
61 typedef QValueList<Var> TransportArgs;
62 class Transport
63 {
64 public:
65 enum ReadStatus { Ok, Done, Error };
66 Transport();
67
68 QString name() const;
69 TransportArgs arguments() const;
70 QString argument(const QString &name) const;
71
72 void setArguments(const TransportArgs &list);
73 void setArgument(const QString &name, const QString &value);
74
75 QString toString() const;
76 ReadStatus readFromString(QString *s);
77
78 private:
79 QString _name;
80 TransportArgs _args;
81 };
82
83 class TransportList : public QValueList<Transport>
84 {
85 public:
86 TransportList();
87
88 QString toString() const;
89 bool fromString(const QString &s);
90 };
91
92 class Packet
93 {
94 public:
95 enum Type { Empty, Request, Response, Data };
96 Packet();
97 Packet(const QString &command, const QString &resource, const HeaderList &headers=HeaderList());
98 Packet(int responseCode, const QString &responseString, const HeaderList &headers=HeaderList());
99 ~Packet();
100
101 bool isNull() const;
102 Type type() const;
103
104 QString version() const;
105 QString command() const;
106 QString resource() const;
107 int responseCode() const;
108 QString responseString() const;
109 int channel() const;
110 QByteArray data() const;
111
112 HeaderList & headers();
113 const HeaderList & headers() const;
114
115 TransportList transports() const;
116 void setTransports(const TransportList &list);
117
118 void setResource(const QString &s);
119
120 QByteArray toArray() const;
121
122 private:
123 friend class Parser;
124 Type t;
125 QString ver, cmd, res, rstr;
126 int rcode, chan;
127 QByteArray _data;
128 HeaderList _headers;
129 };
130
131 class Parser
132 {
133 public:
134 enum Mode { Client, Server };
135 Parser();
136
137 void reset(Mode mode);
138 void appendData(const QByteArray &a);
139 Packet read(bool *ok=0);
140
141 private:
142 Mode mode;
143 QByteArray in;
144 QStringList cur;
145 Packet tmp;
146 QValueList<Packet> list;
147 int clen;
148 bool readingContent;
149
150 bool readPacket();
151 bool packetFromLines(const QStringList &lines);
152 bool readHeaders(const QStringList &lines, HeaderList *headers);
153 QStringList readPacketLines();
154 QString tryReadLine();
155 };
156
157 class Client : public QObject
158 {
159 Q_OBJECT
160 public:
161 enum Mode { MClient, MServer };
162 enum Error { ErrConnect, ErrParse, ErrStream };
163 Client(QObject *parent=0);
164 ~Client();
165
166 void connectToHost(const QString &host, int port);
167 void setByteStream(ByteStream *bs, Mode mode);
168 void close();
169
170 void write(const Packet &p);
171
172 QHostAddress peerAddress() const;
173
174 signals:
175 void connected();
176 void connectionClosed();
177 void packetReady(const Packet &p);
178 void packetWritten();
179 void error(int);
180
181 private slots:
182 void sock_connected();
183 void bs_connectionClosed();
184 void bs_readyRead();
185 void bs_bytesWritten(int);
186 void bs_error(int);
187 void serve();
188
189 private:
190 class Private;
191 Private *d;
192
193 friend class Server;
194 Client(int socket);
195 void hook();
196 void processPackets();
197 };
198
199 class Server : public QObject
200 {
201 Q_OBJECT
202 public:
203 Server(QObject *parent=0);
204 ~Server();
205
206 bool isActive() const;
207 bool start(int port);
208 void stop();
209 int port() const;
210 QHostAddress address() const;
211 Client *takeIncoming();
212
213 signals:
214 void incomingReady();
215
216 private slots:
217 void connectionReady(int);
218 void connectionError();
219
220 private:
221 class Private;
222 Private *d;
223 };
224}
225
226#endif
Note: See TracBrowser for help on using the repository browser.