OSDN Git Service

IM0027-4
[peercast-im/PeerCastIM.git] / c: / Git / PeerCast.root / PeerCast / core / common / servent.h
1 // ------------------------------------------------
2 // File : servent.h
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //
7 // (c) 2002 peercast.org
8 // ------------------------------------------------
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 // ------------------------------------------------
19
20
21 #ifndef _SERVENT_H
22 #define _SERVENT_H
23
24 // ----------------------------------
25 #include "socket.h"
26 #include "sys.h"
27 #include "gnutella.h"
28 #include "channel.h"
29 #include "http.h"
30 #include "rtsp.h"
31 #include "pcp.h"
32
33 class HTML;
34
35 class AtomStream;
36
37 // ----------------------------------
38 // Servent handles the actual connection between clients
39 class Servent
40 {
41 public:
42
43         enum 
44         {
45                 MAX_HASH = 500,                 // max. amount of packet hashes Servents can store
46                 MAX_OUTPACKETS = 32             // max. output packets per queue (normal/priority)
47         };
48
49         enum
50         {
51                 MAX_PROC_PACKETS = 300,
52                 MAX_OUTWARD_SIZE = 1024 * 10
53         };
54
55     enum TYPE                                   
56     {
57                 T_NONE,                                 // Not allocated
58         T_INCOMING,                             // Unknown incoming
59         T_SERVER,                               // The main server 
60                 T_RELAY,                                // Outgoing relay
61                 T_DIRECT,                               // Outgoing direct connection
62                 T_COUT,                                 // PCP out connection
63                 T_CIN,                                  // PCP in connection
64                 T_PGNU                                  // old protocol connection              
65     };
66
67     enum STATUS
68     {
69         S_NONE,
70         S_CONNECTING,
71         S_PROTOCOL,
72         S_HANDSHAKE,
73         S_CONNECTED,
74         S_CLOSING,
75                 S_LISTENING,
76                 S_TIMEOUT,
77                 S_REFUSED,
78                 S_VERIFIED,
79                 S_ERROR,
80                 S_WAIT,
81                 S_FREE
82     };
83
84         enum PROTOCOL
85         {
86                 P_UNKNOWN,
87                 P_GNUTELLA06,
88                 P_PCP
89         };
90
91
92         enum SORT
93         {
94                 SORT_NAME = 0,
95                 SORT_BITRATE,
96                 SORT_LISTENERS,
97                 SORT_HOSTS,
98                 SORT_TYPE,
99                 SORT_GENRE
100         };
101
102         enum ALLOW
103         {
104                 ALLOW_HTML              = 0x01,
105                 ALLOW_BROADCAST = 0x02,
106                 ALLOW_NETWORK   = 0x04,
107                 ALLOW_DIRECT    = 0x08,
108                 ALLOW_ALL               = 0xff
109         };
110
111         Servent(int);
112         ~Servent();
113
114         void    reset();
115     bool        initServer(Host &);
116     void        initIncoming(ClientSocket *,unsigned int);
117     void        initOutgoing(TYPE);
118         void    initGIV(Host &, GnuID &);
119         void    initPCP(Host &);
120
121         void    checkFree();
122
123
124
125
126         //      funcs for handling status/type
127         void    setStatus(STATUS);
128         static char *   getTypeStr(Servent::TYPE t) {return typeMsgs[t];}
129         char *  getTypeStr() {return getTypeStr(type);}
130         char *  getStatusStr() {return statusMsgs[status];}
131         int             getOutput();
132         void    addBytes(unsigned int);
133         bool    isOlderThan(Servent *s)
134         {
135                 if (s)
136                 {
137                         unsigned int t = sys->getTime();
138                         return ((t-lastConnect) > (t-s->lastConnect));
139                 }else
140                         return true;
141         }
142
143
144
145         // static funcs that do the actual work in the servent thread
146         static  THREAD_PROC             serverProc(ThreadInfo *);
147         static  THREAD_PROC             outgoingProc(ThreadInfo *);
148         static  THREAD_PROC             incomingProc(ThreadInfo *);
149         static  THREAD_PROC             givProc(ThreadInfo *);
150         static  THREAD_PROC             pcpProc(ThreadInfo *);
151         static  THREAD_PROC             fetchProc(ThreadInfo *);
152
153         static bool     pingHost(Host &,GnuID &);
154
155         bool    getLocalURL(char *);
156         bool    getLocalTypeURL(char *, ChanInfo::TYPE);
157
158         // various types of handshaking are needed
159         void    handshakePLS(ChanHitList **, int, bool);
160         void    handshakePLS(ChanInfo &, bool);
161
162         void    handshakeHTML(char *);
163         void    handshakeXML();
164         void    handshakeCMD(char *);
165         bool    handshakeAuth(HTTP &,const char *,bool);
166         void    handshakeIn();
167         void    handshakeOut();
168
169
170         void    processOutPCP();
171         void    processOutChannel();
172
173         bool    handshakeStream(ChanInfo &);
174         void    handshakeGiv(GnuID &);
175
176         void    handshakeICY(Channel::SRC_TYPE,bool);
177         void    handshakeIncoming();
178         void    handshakePOST();
179         void    handshakeRTSP(RTSP &);
180         void    handshakeHTTP(HTTP &,bool);
181
182         void    handshakeRemoteFile(const char *);
183         void    handshakeLocalFile(const char *);
184
185         static void     handshakeOutgoingPCP(AtomStream &,Host &,GnuID &,String &,bool);
186         static void     handshakeIncomingPCP(AtomStream &,Host &,GnuID &,String &);
187
188         void    processIncomingPCP(bool);
189
190         bool    waitForChannelHeader(ChanInfo &);
191         ChanInfo        findChannel(char *str,ChanInfo &);
192
193         bool    writeVariable(Stream &, const String &);
194
195
196         // the "mainloop" of servents 
197         void    processGnutella();
198         void    processRoot();
199         void    processServent();
200         void    processStream(bool,ChanInfo &);
201         void    processPCP(bool,bool);
202
203         bool    procAtoms(AtomStream &);
204         void    procRootAtoms(AtomStream &,int);
205         void    procHeloAtoms(AtomStream &,int,bool);
206         void    procGetAtoms(AtomStream &,int);
207
208         void    triggerChannel(char *,ChanInfo::PROTOCOL,bool);
209         void    sendPeercastChannel();
210         void    sendRawChannel(bool,bool);
211 //      void    sendRawMultiChannel(bool,bool);
212         void    sendRawMetaChannel(int);
213         void    sendPCPChannel();
214         void    checkPCPComms(Channel *, AtomStream &);
215
216         static void     readICYHeader(HTTP &, ChanInfo &, char *, size_t);
217         bool    canStream(Channel *);
218
219         bool    isConnected() {return status == S_CONNECTED;}
220         bool    isListening() {return status == S_LISTENING;}
221
222         bool    isAllowed(int);
223         bool    isFiltered(int);
224
225         // connection handling funcs
226         void    createSocket();
227         void    kill();
228         void    abort();
229         bool    isPrivate();
230         bool    isLocal();
231
232
233         Host    getHost();
234
235         bool    outputPacket(GnuPacket &,bool);
236         bool    hasSeenPacket(GnuPacket &p) {return seenIDs.contains(p.id);}
237         bool    acceptGIV(ClientSocket *);
238         bool    sendPacket(ChanPacket &,GnuID &,GnuID &,GnuID &,Servent::TYPE);
239
240
241         TYPE type;
242         STATUS status;
243
244         static char *statusMsgs[],*typeMsgs[];
245         GnuStream gnuStream;
246         GnuPacket pack;
247         unsigned int    lastConnect,lastPing,lastPacket;
248         String  agent;
249
250         GnuIDList       seenIDs;
251         GnuID   networkID;
252         int             serventIndex;
253
254         GnuID   remoteID;
255
256         GnuID   chanID;
257
258         GnuID   givID;
259
260         ThreadInfo      thread;
261
262
263         String  loginPassword;
264         String  loginMount;
265
266         bool    priorityConnect;
267         bool    addMetadata;
268         int             nsSwitchNum;
269
270         unsigned        int allow;
271
272     ClientSocket *sock,*pushSock;
273
274         WLock   lock;
275
276         bool    sendHeader;
277         unsigned int syncPos,streamPos;
278         int             servPort;
279
280         ChanInfo::PROTOCOL      outputProtocol;
281
282         GnuPacketBuffer outPacketsNorm,outPacketsPri;
283
284         unsigned int bytesPerSecond;
285         bool    flowControl;
286
287         Servent *next;
288
289         PCPStream *pcpStream;
290         Cookie  cookie;
291
292         int servent_id;
293         unsigned int lastSkipTime;
294         unsigned int lastSkipCount;
295         unsigned int waitPort;
296
297         ChanHit serventHit;
298
299         int channel_id;
300 };
301
302 extern char *nextCGIarg(char *cp, char *cmd, char *arg);
303
304
305 #endif
306