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

P2P编程

开发平台:

Windows_Unix

  1. // ------------------------------------------------
  2. // File : pcp.h
  3. // Date: 1-mar-2004
  4. // Author: giles
  5. //
  6. // (c) 2002-4 peercast.org
  7. // ------------------------------------------------
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 2 of the License, or
  11. // (at your option) any later version.
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. // GNU General Public License for more details.
  16. // ------------------------------------------------
  17. #ifndef _PCP_H
  18. #define _PCP_H
  19. // ------------------------------------------------
  20. #include "id.h"
  21. #include "cstream.h"
  22. // ------------------------------------------------
  23. class Servent;
  24. // ------------------------------------------------
  25. static const ID4 PCP_CONNECT = "pcpn";
  26. static const ID4 PCP_OK = "ok";
  27. static const ID4 PCP_HELO = "helo";
  28. static const ID4 PCP_HELO_AGENT = "agnt";
  29. static const ID4 PCP_HELO_OSTYPE = "ostp";
  30. static const ID4 PCP_HELO_SESSIONID = "sid";
  31. static const ID4 PCP_HELO_PORT = "port";
  32. static const ID4 PCP_HELO_PING = "ping";
  33. static const ID4 PCP_HELO_PONG = "pong";
  34. static const ID4 PCP_HELO_REMOTEIP = "rip";
  35. static const ID4 PCP_HELO_VERSION = "ver";
  36. static const ID4 PCP_HELO_BCID = "bcid";
  37. static const ID4 PCP_HELO_DISABLE = "dis";
  38. static const ID4 PCP_OLEH = "oleh";
  39. static const ID4 PCP_MODE = "mode";
  40. static const ID4 PCP_MODE_GNUT06 = "gn06";
  41. static const ID4 PCP_ROOT = "root";
  42. static const ID4 PCP_ROOT_UPDINT = "uint";
  43. static const ID4 PCP_ROOT_CHECKVER = "chkv";
  44. static const ID4 PCP_ROOT_URL = "url";
  45. static const ID4 PCP_ROOT_UPDATE = "upd";
  46. static const ID4 PCP_ROOT_NEXT = "next";
  47. static const ID4 PCP_OS_LINUX = "lnux";
  48. static const ID4 PCP_OS_WINDOWS = "w32";
  49. static const ID4 PCP_OS_OSX = "osx";
  50. static const ID4 PCP_OS_WINAMP = "wamp";
  51. static const ID4 PCP_OS_ZAURUS = "zaur";
  52. static const ID4 PCP_GET = "get";
  53. static const ID4 PCP_GET_ID = "id";
  54. static const ID4 PCP_GET_NAME = "name";
  55. static const ID4 PCP_HOST = "host";
  56. static const ID4 PCP_HOST_ID = "id";
  57. static const ID4 PCP_HOST_IP = "ip";
  58. static const ID4 PCP_HOST_PORT = "port";
  59. static const ID4 PCP_HOST_NUML = "numl";
  60. static const ID4 PCP_HOST_NUMR = "numr";
  61. static const ID4 PCP_HOST_UPTIME = "uptm";
  62. static const ID4 PCP_HOST_TRACKER = "trkr";
  63. static const ID4 PCP_HOST_CHANID = "cid";
  64. static const ID4 PCP_HOST_VERSION = "ver";
  65. static const ID4 PCP_HOST_FLAGS1 = "flg1";
  66. static const ID4 PCP_HOST_OLDPOS = "oldp";
  67. static const ID4 PCP_HOST_NEWPOS = "newp";
  68. static const ID4 PCP_QUIT = "quit";
  69. static const ID4 PCP_CHAN = "chan";
  70. static const ID4 PCP_CHAN_ID = "id";
  71. static const ID4 PCP_CHAN_BCID = "bcid";
  72. static const ID4 PCP_CHAN_KEY = "key";
  73. static const ID4 PCP_CHAN_PKT = "pkt";
  74. static const ID4 PCP_CHAN_PKT_TYPE = "type";
  75. static const ID4 PCP_CHAN_PKT_POS = "pos";
  76. static const ID4 PCP_CHAN_PKT_HEAD = "head";
  77. static const ID4 PCP_CHAN_PKT_DATA = "data";
  78. static const ID4 PCP_CHAN_PKT_META = "meta";
  79. static const ID4 PCP_CHAN_INFO = "info";
  80. static const ID4 PCP_CHAN_INFO_TYPE = "type";
  81. static const ID4 PCP_CHAN_INFO_BITRATE = "bitr";
  82. static const ID4 PCP_CHAN_INFO_GENRE = "gnre";
  83. static const ID4 PCP_CHAN_INFO_NAME = "name";
  84. static const ID4 PCP_CHAN_INFO_URL = "url";
  85. static const ID4 PCP_CHAN_INFO_DESC = "desc";
  86. static const ID4 PCP_CHAN_INFO_COMMENT = "cmnt";
  87. static const ID4 PCP_CHAN_TRACK = "trck";
  88. static const ID4 PCP_CHAN_TRACK_TITLE = "titl";
  89. static const ID4 PCP_CHAN_TRACK_CREATOR = "crea";
  90. static const ID4 PCP_CHAN_TRACK_URL = "url";
  91. static const ID4 PCP_CHAN_TRACK_ALBUM = "albm";
  92. static const ID4 PCP_MESG = "mesg";
  93. static const ID4 PCP_MESG_ASCII = "asci"; // ascii/sjis to be depreciated.. utf8/unicode is the only supported format from now.
  94. static const ID4 PCP_MESG_SJIS = "sjis";
  95. static const ID4 PCP_BCST = "bcst";
  96. static const ID4 PCP_BCST_TTL = "ttl";
  97. static const ID4 PCP_BCST_HOPS = "hops";
  98. static const ID4 PCP_BCST_FROM = "from";
  99. static const ID4 PCP_BCST_DEST = "dest";
  100. static const ID4 PCP_BCST_GROUP = "grp";
  101. static const ID4 PCP_BCST_CHANID = "cid";
  102. static const ID4 PCP_BCST_VERSION = "vers";
  103. static const ID4 PCP_PUSH = "push";
  104. static const ID4 PCP_PUSH_IP = "ip";
  105. static const ID4 PCP_PUSH_PORT = "port";
  106. static const ID4 PCP_PUSH_CHANID = "cid";
  107. static const ID4 PCP_SPKT = "spkt";
  108. static const ID4 PCP_ATOM = "atom";
  109. static const ID4 PCP_SESSIONID = "sid";
  110. static const int PCP_BCST_GROUP_ALL = (char)0xff;
  111. static const int PCP_BCST_GROUP_ROOT = 1;
  112. static const int PCP_BCST_GROUP_TRACKERS = 2;
  113. static const int PCP_BCST_GROUP_RELAYS = 4;
  114. static const int PCP_ERROR_QUIT = 1000;
  115. static const int PCP_ERROR_BCST = 2000;
  116. static const int PCP_ERROR_READ = 3000;
  117. static const int PCP_ERROR_WRITE = 4000;
  118. static const int PCP_ERROR_GENERAL = 5000;
  119. static const int PCP_ERROR_SKIP = 1;
  120. static const int PCP_ERROR_ALREADYCONNECTED = 2;
  121. static const int PCP_ERROR_UNAVAILABLE = 3;
  122. static const int PCP_ERROR_LOOPBACK = 4;
  123. static const int PCP_ERROR_NOTIDENTIFIED = 5;
  124. static const int PCP_ERROR_BADRESPONSE = 6;
  125. static const int PCP_ERROR_BADAGENT = 7;
  126. static const int PCP_ERROR_OFFAIR = 8;
  127. static const int PCP_ERROR_SHUTDOWN = 9;
  128. static const int PCP_ERROR_NOROOT = 10;
  129. static const int PCP_ERROR_BANNED = 11;
  130. static const int PCP_HOST_FLAGS1_TRACKER = 0x01;
  131. static const int PCP_HOST_FLAGS1_RELAY = 0x02;
  132. static const int PCP_HOST_FLAGS1_DIRECT = 0x04;
  133. static const int PCP_HOST_FLAGS1_PUSH = 0x08;
  134. static const int PCP_HOST_FLAGS1_RECV = 0x10;
  135. static const int PCP_HOST_FLAGS1_CIN = 0x20;
  136. static const int PCP_HOST_FLAGS1_PRIVATE = 0x40;
  137. // ----------------------------------------------
  138. class BroadcastState 
  139. {
  140. public:
  141. BroadcastState()
  142. :numHops(0)
  143. ,forMe(false) 
  144. ,streamPos(0)
  145. ,group(0)
  146. {
  147. chanID.clear();
  148. bcID.clear();
  149. }
  150. void initPacketSettings()
  151. {
  152. forMe = false;
  153. group = 0;
  154. numHops = 0;
  155. bcID.clear();
  156. chanID.clear();
  157. }
  158. GnuID chanID,bcID;
  159. int numHops;
  160. bool forMe;
  161. unsigned int streamPos;
  162. int group;
  163. };
  164. // ----------------------------------------------
  165. class PCPStream : public ChannelStream
  166. {
  167. public:
  168. PCPStream(GnuID &rid) 
  169. :routeList(1000)
  170. {
  171. init(rid);
  172. }
  173. void init(GnuID &);
  174. virtual void kill()
  175. {
  176. inData.lock.on();
  177. outData.lock.on();
  178. }
  179. virtual bool sendPacket(ChanPacket &,GnuID &);
  180. virtual void flush(Stream &);
  181. virtual void readHeader(Stream &,Channel *);
  182. virtual int readPacket(Stream &,Channel *);
  183. virtual void readEnd(Stream &,Channel *);
  184. int readPacket(Stream &,BroadcastState &);
  185. void flushOutput(Stream &in,BroadcastState &);
  186. static void readVersion(Stream &);
  187. int procAtom(AtomStream &,ID4,int,int,BroadcastState &);
  188. int readAtom(AtomStream &,BroadcastState &);
  189. void readChanAtoms(AtomStream &,int,BroadcastState &);
  190. void readHostAtoms(AtomStream &, int, BroadcastState &);
  191. void readPushAtoms(AtomStream &, int,BroadcastState &);
  192. void readPktAtoms(Channel *,AtomStream &,int,BroadcastState &);
  193. void readRootAtoms(AtomStream &, int,BroadcastState &);
  194. int readBroadcastAtoms(AtomStream &,int,BroadcastState &);
  195. ChanPacketBuffer inData,outData;
  196. unsigned int lastPacketTime;
  197. unsigned int nextRootPacket;
  198. //int error;
  199. GnuIDList routeList;
  200. GnuID remoteID;
  201. };
  202. #endif