servent.h
上传用户:chn_coc
上传日期:2007-12-20
资源大小:563k
文件大小:6k
源码类别:

P2P编程

开发平台:

Windows_Unix

  1. // ------------------------------------------------ // File : servent.h // Date: 4-apr-2002 // Author: giles // Desc:  // // (c) 2002 peercast.org // ------------------------------------------------ // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the // GNU General Public License for more details. // ------------------------------------------------ #ifndef _SERVENT_H #define _SERVENT_H // ---------------------------------- #include "socket.h" #include "sys.h" #include "gnutella.h" #include "channel.h" #include "http.h" #include "rtsp.h" #include "pcp.h"
  2. class HTML;
  3. class AtomStream; // ---------------------------------- // Servent handles the actual connection between clients class Servent { public: enum  { MAX_HASH = 500, // max. amount of packet hashes Servents can store MAX_OUTPACKETS = 32 // max. output packets per queue (normal/priority) };     enum TYPE     { T_NONE, // Not allocated
  4.         T_INCOMING, // Unknown incoming         T_SERVER, // The main server 
  5. T_RELAY, // Outgoing relay T_DIRECT, // Outgoing direct connection
  6. T_COUT, // PCP out connection
  7. T_CIN, // PCP in connection
  8. T_PGNU // old protocol connection     };     enum STATUS     {         S_NONE,         S_CONNECTING,         S_PROTOCOL,         S_HANDSHAKE,         S_CONNECTED,         S_CLOSING, S_LISTENING, S_TIMEOUT, S_REFUSED, S_VERIFIED, S_ERROR, S_WAIT, S_FREE     };
  9. enum PROTOCOL
  10. {
  11. P_UNKNOWN,
  12. P_GNUTELLA06,
  13. P_PCP
  14. };
  15. enum SORT { SORT_NAME = 0, SORT_BITRATE, SORT_LISTENERS, SORT_HOSTS, SORT_TYPE, SORT_GENRE }; enum ALLOW { ALLOW_HTML = 0x01, ALLOW_BROADCAST = 0x02, ALLOW_NETWORK = 0x04,
  16. ALLOW_DIRECT = 0x08,
  17. ALLOW_ALL = 0xff }; Servent(int); ~Servent(); void reset();     bool initServer(Host &);     void initIncoming(ClientSocket *,unsigned int);     void initOutgoing(TYPE); void initGIV(Host &, GnuID &); void initPCP(Host &);
  18. void checkFree();
  19. // funcs for handling status/type void setStatus(STATUS); static char * getTypeStr(Servent::TYPE t) {return typeMsgs[t];} char * getTypeStr() {return getTypeStr(type);}
  20. char * getStatusStr() {return statusMsgs[status];} int getOutput(); void addBytes(unsigned int); bool isOlderThan(Servent *s) { if (s) { unsigned int t = sys->getTime(); return ((t-lastConnect) > (t-s->lastConnect)); }else return true; } // static funcs that do the actual work in the servent thread static THREAD_PROC serverProc(ThreadInfo *); static THREAD_PROC outgoingProc(ThreadInfo *); static THREAD_PROC incomingProc(ThreadInfo *); static THREAD_PROC givProc(ThreadInfo *); static THREAD_PROC pcpProc(ThreadInfo *);
  21. static THREAD_PROC fetchProc(ThreadInfo *);
  22. static bool pingHost(Host &,GnuID &); bool getLocalURL(char *); // various types of handshaking are needed void handshakePLS(ChanHitList **, int, bool); void handshakePLS(ChanInfo &, bool);
  23. void handshakeHTML(char *); void handshakeXML(); void handshakeCMD(char *); bool handshakeAuth(HTTP &,const char *,bool);
  24. void handshakeIn(); void handshakeOut();
  25. void processOutPCP();
  26. void processOutChannel();
  27. bool handshakeStream(ChanInfo &); void handshakeGiv(GnuID &);
  28. void handshakeICY(Channel::SRC_TYPE,bool); void handshakeIncoming(); void handshakePOST(); void handshakeRTSP(RTSP &); void handshakeHTTP(HTTP &,bool);
  29. void handshakeRemoteFile(const char *); void handshakeLocalFile(const char *);
  30. static void handshakeOutgoingPCP(AtomStream &,Host &,GnuID &,String &,bool);
  31. static void handshakeIncomingPCP(AtomStream &,Host &,GnuID &,String &);
  32. void processIncomingPCP(bool);
  33. bool waitForChannelHeader(ChanInfo &); ChanInfo findChannel(char *str,ChanInfo &);
  34. bool writeVariable(Stream &, const String &);
  35. // the "mainloop" of servents  void processGnutella(); void processRoot(); void processServent(); void processStream(bool,ChanInfo &);
  36. void processPCP(bool,bool);
  37. bool procAtoms(AtomStream &);
  38. void procRootAtoms(AtomStream &,int);
  39. void procHeloAtoms(AtomStream &,int,bool);
  40. void procGetAtoms(AtomStream &,int);
  41. void triggerChannel(char *,ChanInfo::PROTOCOL,bool); void sendPeercastChannel();
  42. void sendRawChannel(bool,bool); // void sendRawMultiChannel(bool,bool);
  43. void sendRawMetaChannel(int);
  44. void sendPCPChannel();
  45. void checkPCPComms(Channel *, AtomStream &);
  46. static void readICYHeader(HTTP &, ChanInfo &, char *); bool canStream(Channel *); bool isConnected() {return status == S_CONNECTED;}
  47. bool isListening() {return status == S_LISTENING;}
  48. bool isAllowed(int);
  49. bool isFiltered(int); // connection handling funcs void createSocket(); void kill(); void abort(); bool isPrivate(); bool isLocal(); Host getHost(); bool outputPacket(GnuPacket &,bool); bool hasSeenPacket(GnuPacket &p) {return seenIDs.contains(p.id);} bool acceptGIV(ClientSocket *);
  50. bool sendPacket(ChanPacket &,GnuID &,GnuID &,GnuID &,Servent::TYPE);
  51. TYPE type; STATUS status; static char *statusMsgs[],*typeMsgs[]; GnuStream gnuStream; GnuPacket pack; unsigned int lastConnect,lastPing,lastPacket; String agent; GnuIDList seenIDs; GnuID networkID; int serventIndex;
  52. GnuID remoteID;
  53. GnuID chanID;
  54. GnuID givID;
  55. ThreadInfo thread;
  56. char loginPassword[64]; char loginMount[64]; bool priorityConnect;
  57. bool addMetadata;
  58. int nsSwitchNum;
  59. unsigned int allow;     ClientSocket *sock,*pushSock; WLock lock;
  60. bool sendHeader;
  61. unsigned int syncPos,streamPos;
  62. int servPort;
  63. ChanInfo::PROTOCOL outputProtocol;
  64. GnuPacketBuffer outPacketsNorm,outPacketsPri; unsigned int bytesPerSecond; bool flowControl; Servent *next;
  65. PCPStream *pcpStream;
  66. Cookie cookie; }; extern char *nextCGIarg(char *cp, char *cmd, char *arg); #endif