ClientViewBase.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using libsecondlife;
  5. using libsecondlife.Packets;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Timers;
  11. using OpenSim.Framework.Utilities;
  12. using OpenSim.Framework.Interfaces;
  13. namespace OpenSim
  14. {
  15. public class ClientViewBase
  16. {
  17. protected BlockingQueue<QueItem> PacketQueue;
  18. protected Dictionary<uint, uint> PendingAcks = new Dictionary<uint, uint>();
  19. protected Dictionary<uint, Packet> NeedAck = new Dictionary<uint, Packet>();
  20. protected System.Timers.Timer AckTimer;
  21. protected uint Sequence = 0;
  22. protected object SequenceLock = new object();
  23. protected const int MAX_APPENDED_ACKS = 10;
  24. protected const int RESEND_TIMEOUT = 4000;
  25. protected const int MAX_SEQUENCE = 0xFFFFFF;
  26. public uint CircuitCode;
  27. public EndPoint userEP;
  28. protected PacketServer m_networkServer;
  29. public ClientViewBase()
  30. {
  31. }
  32. protected virtual void ProcessInPacket(Packet Pack)
  33. {
  34. }
  35. protected virtual void ProcessOutPacket(Packet Pack)
  36. {
  37. // Keep track of when this packet was sent out
  38. Pack.TickCount = Environment.TickCount;
  39. if (!Pack.Header.Resent)
  40. {
  41. // Set the sequence number
  42. lock (SequenceLock)
  43. {
  44. if (Sequence >= MAX_SEQUENCE)
  45. Sequence = 1;
  46. else
  47. Sequence++;
  48. Pack.Header.Sequence = Sequence;
  49. }
  50. if (Pack.Header.Reliable) //DIRTY HACK
  51. {
  52. lock (NeedAck)
  53. {
  54. if (!NeedAck.ContainsKey(Pack.Header.Sequence))
  55. {
  56. try
  57. {
  58. NeedAck.Add(Pack.Header.Sequence, Pack);
  59. }
  60. catch (Exception e) // HACKY
  61. {
  62. e.ToString();
  63. // Ignore
  64. // Seems to throw a exception here occasionally
  65. // of 'duplicate key' despite being locked.
  66. // !?!?!?
  67. }
  68. }
  69. else
  70. {
  71. // Client.Log("Attempted to add a duplicate sequence number (" +
  72. // packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
  73. // packet.Type.ToString(), Helpers.LogLevel.Warning);
  74. }
  75. }
  76. // Don't append ACKs to resent packets, in case that's what was causing the
  77. // delivery to fail
  78. if (!Pack.Header.Resent)
  79. {
  80. // Append any ACKs that need to be sent out to this packet
  81. lock (PendingAcks)
  82. {
  83. if (PendingAcks.Count > 0 && PendingAcks.Count < MAX_APPENDED_ACKS &&
  84. Pack.Type != PacketType.PacketAck &&
  85. Pack.Type != PacketType.LogoutRequest)
  86. {
  87. Pack.Header.AckList = new uint[PendingAcks.Count];
  88. int i = 0;
  89. foreach (uint ack in PendingAcks.Values)
  90. {
  91. Pack.Header.AckList[i] = ack;
  92. i++;
  93. }
  94. PendingAcks.Clear();
  95. Pack.Header.AppendedAcks = true;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. byte[] ZeroOutBuffer = new byte[4096];
  102. byte[] sendbuffer;
  103. sendbuffer = Pack.ToBytes();
  104. try
  105. {
  106. if (Pack.Header.Zerocoded)
  107. {
  108. int packetsize = Helpers.ZeroEncode(sendbuffer, sendbuffer.Length, ZeroOutBuffer);
  109. m_networkServer.SendPacketTo(ZeroOutBuffer, packetsize, SocketFlags.None, CircuitCode);//userEP);
  110. }
  111. else
  112. {
  113. m_networkServer.SendPacketTo(sendbuffer, sendbuffer.Length, SocketFlags.None, CircuitCode); //userEP);
  114. }
  115. }
  116. catch (Exception)
  117. {
  118. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "OpenSimClient.cs:ProcessOutPacket() - WARNING: Socket exception occurred on connection " + userEP.ToString() + " - killing thread");
  119. this.KillThread();
  120. }
  121. }
  122. public virtual void InPacket(Packet NewPack)
  123. {
  124. // Handle appended ACKs
  125. if (NewPack.Header.AppendedAcks)
  126. {
  127. lock (NeedAck)
  128. {
  129. foreach (uint ack in NewPack.Header.AckList)
  130. {
  131. NeedAck.Remove(ack);
  132. }
  133. }
  134. }
  135. // Handle PacketAck packets
  136. if (NewPack.Type == PacketType.PacketAck)
  137. {
  138. PacketAckPacket ackPacket = (PacketAckPacket)NewPack;
  139. lock (NeedAck)
  140. {
  141. foreach (PacketAckPacket.PacketsBlock block in ackPacket.Packets)
  142. {
  143. NeedAck.Remove(block.ID);
  144. }
  145. }
  146. }
  147. else if ((NewPack.Type == PacketType.StartPingCheck))
  148. {
  149. //reply to pingcheck
  150. libsecondlife.Packets.StartPingCheckPacket startPing = (libsecondlife.Packets.StartPingCheckPacket)NewPack;
  151. libsecondlife.Packets.CompletePingCheckPacket endPing = new CompletePingCheckPacket();
  152. endPing.PingID.PingID = startPing.PingID.PingID;
  153. OutPacket(endPing);
  154. }
  155. else
  156. {
  157. QueItem item = new QueItem();
  158. item.Packet = NewPack;
  159. item.Incoming = true;
  160. this.PacketQueue.Enqueue(item);
  161. }
  162. }
  163. public virtual void OutPacket(Packet NewPack)
  164. {
  165. QueItem item = new QueItem();
  166. item.Packet = NewPack;
  167. item.Incoming = false;
  168. this.PacketQueue.Enqueue(item);
  169. }
  170. # region Low Level Packet Methods
  171. protected void ack_pack(Packet Pack)
  172. {
  173. if (Pack.Header.Reliable)
  174. {
  175. libsecondlife.Packets.PacketAckPacket ack_it = new PacketAckPacket();
  176. ack_it.Packets = new PacketAckPacket.PacketsBlock[1];
  177. ack_it.Packets[0] = new PacketAckPacket.PacketsBlock();
  178. ack_it.Packets[0].ID = Pack.Header.Sequence;
  179. ack_it.Header.Reliable = false;
  180. OutPacket(ack_it);
  181. }
  182. /*
  183. if (Pack.Header.Reliable)
  184. {
  185. lock (PendingAcks)
  186. {
  187. uint sequence = (uint)Pack.Header.Sequence;
  188. if (!PendingAcks.ContainsKey(sequence)) { PendingAcks[sequence] = sequence; }
  189. }
  190. }*/
  191. }
  192. protected void ResendUnacked()
  193. {
  194. int now = Environment.TickCount;
  195. lock (NeedAck)
  196. {
  197. foreach (Packet packet in NeedAck.Values)
  198. {
  199. if ((now - packet.TickCount > RESEND_TIMEOUT) && (!packet.Header.Resent))
  200. {
  201. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.VERBOSE, "Resending " + packet.Type.ToString() + " packet, " +
  202. (now - packet.TickCount) + "ms have passed");
  203. packet.Header.Resent = true;
  204. OutPacket(packet);
  205. }
  206. }
  207. }
  208. }
  209. protected void SendAcks()
  210. {
  211. lock (PendingAcks)
  212. {
  213. if (PendingAcks.Count > 0)
  214. {
  215. if (PendingAcks.Count > 250)
  216. {
  217. // FIXME: Handle the odd case where we have too many pending ACKs queued up
  218. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.VERBOSE, "Too many ACKs queued up!");
  219. return;
  220. }
  221. //OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Sending PacketAck");
  222. int i = 0;
  223. PacketAckPacket acks = new PacketAckPacket();
  224. acks.Packets = new PacketAckPacket.PacketsBlock[PendingAcks.Count];
  225. foreach (uint ack in PendingAcks.Values)
  226. {
  227. acks.Packets[i] = new PacketAckPacket.PacketsBlock();
  228. acks.Packets[i].ID = ack;
  229. i++;
  230. }
  231. acks.Header.Reliable = false;
  232. OutPacket(acks);
  233. PendingAcks.Clear();
  234. }
  235. }
  236. }
  237. protected void AckTimer_Elapsed(object sender, ElapsedEventArgs ea)
  238. {
  239. SendAcks();
  240. ResendUnacked();
  241. }
  242. #endregion
  243. protected virtual void KillThread()
  244. {
  245. }
  246. #region Nested Classes
  247. public class QueItem
  248. {
  249. public QueItem()
  250. {
  251. }
  252. public Packet Packet;
  253. public bool Incoming;
  254. }
  255. #endregion
  256. }
  257. }