ClientViewBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Timers;
  33. using libsecondlife;
  34. using libsecondlife.Packets;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Utilities;
  37. namespace OpenSim.Region.ClientStack
  38. {
  39. public class ClientViewBase
  40. {
  41. protected BlockingQueue<QueItem> PacketQueue;
  42. protected Dictionary<uint, uint> PendingAcks = new Dictionary<uint, uint>();
  43. protected Dictionary<uint, Packet> NeedAck = new Dictionary<uint, Packet>();
  44. protected Timer AckTimer;
  45. protected uint Sequence = 0;
  46. protected object SequenceLock = new object();
  47. protected const int MAX_APPENDED_ACKS = 10;
  48. protected const int RESEND_TIMEOUT = 4000;
  49. protected const int MAX_SEQUENCE = 0xFFFFFF;
  50. public uint CircuitCode;
  51. public EndPoint userEP;
  52. protected PacketServer m_networkServer;
  53. public ClientViewBase()
  54. {
  55. }
  56. protected virtual void ProcessInPacket(Packet Pack)
  57. {
  58. }
  59. protected virtual void ProcessOutPacket(Packet Pack)
  60. {
  61. // Keep track of when this packet was sent out
  62. Pack.TickCount = Environment.TickCount;
  63. Console.WriteLine(CircuitCode + ":OUT: " + Pack.Type.ToString());
  64. if (!Pack.Header.Resent)
  65. {
  66. // Set the sequence number
  67. lock (SequenceLock)
  68. {
  69. if (Sequence >= MAX_SEQUENCE)
  70. Sequence = 1;
  71. else
  72. Sequence++;
  73. Pack.Header.Sequence = Sequence;
  74. }
  75. if (Pack.Header.Reliable) //DIRTY HACK
  76. {
  77. lock (NeedAck)
  78. {
  79. if (!NeedAck.ContainsKey(Pack.Header.Sequence))
  80. {
  81. try
  82. {
  83. NeedAck.Add(Pack.Header.Sequence, Pack);
  84. }
  85. catch (Exception e) // HACKY
  86. {
  87. e.ToString();
  88. // Ignore
  89. // Seems to throw a exception here occasionally
  90. // of 'duplicate key' despite being locked.
  91. // !?!?!?
  92. }
  93. }
  94. else
  95. {
  96. // Client.Log("Attempted to add a duplicate sequence number (" +
  97. // packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
  98. // packet.Type.ToString(), Helpers.LogLevel.Warning);
  99. }
  100. }
  101. // Don't append ACKs to resent packets, in case that's what was causing the
  102. // delivery to fail
  103. if (!Pack.Header.Resent)
  104. {
  105. // Append any ACKs that need to be sent out to this packet
  106. lock (PendingAcks)
  107. {
  108. if (PendingAcks.Count > 0 && PendingAcks.Count < MAX_APPENDED_ACKS &&
  109. Pack.Type != PacketType.PacketAck &&
  110. Pack.Type != PacketType.LogoutRequest)
  111. {
  112. Pack.Header.AckList = new uint[PendingAcks.Count];
  113. int i = 0;
  114. foreach (uint ack in PendingAcks.Values)
  115. {
  116. Pack.Header.AckList[i] = ack;
  117. i++;
  118. }
  119. PendingAcks.Clear();
  120. Pack.Header.AppendedAcks = true;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. byte[] ZeroOutBuffer = new byte[4096];
  127. byte[] sendbuffer;
  128. sendbuffer = Pack.ToBytes();
  129. try
  130. {
  131. if (Pack.Header.Zerocoded)
  132. {
  133. int packetsize = Helpers.ZeroEncode(sendbuffer, sendbuffer.Length, ZeroOutBuffer);
  134. m_networkServer.SendPacketTo(ZeroOutBuffer, packetsize, SocketFlags.None, CircuitCode);//userEP);
  135. }
  136. else
  137. {
  138. m_networkServer.SendPacketTo(sendbuffer, sendbuffer.Length, SocketFlags.None, CircuitCode); //userEP);
  139. }
  140. }
  141. catch (Exception)
  142. {
  143. MainLog.Instance.Warn("OpenSimClient.cs:ProcessOutPacket() - WARNING: Socket exception occurred on connection " + userEP.ToString() + " - killing thread");
  144. this.KillThread();
  145. }
  146. }
  147. public virtual void InPacket(Packet NewPack)
  148. {
  149. // Handle appended ACKs
  150. if (NewPack.Header.AppendedAcks)
  151. {
  152. lock (NeedAck)
  153. {
  154. foreach (uint ack in NewPack.Header.AckList)
  155. {
  156. NeedAck.Remove(ack);
  157. }
  158. }
  159. }
  160. // Handle PacketAck packets
  161. if (NewPack.Type == PacketType.PacketAck)
  162. {
  163. PacketAckPacket ackPacket = (PacketAckPacket)NewPack;
  164. lock (NeedAck)
  165. {
  166. foreach (PacketAckPacket.PacketsBlock block in ackPacket.Packets)
  167. {
  168. NeedAck.Remove(block.ID);
  169. }
  170. }
  171. }
  172. else if ((NewPack.Type == PacketType.StartPingCheck))
  173. {
  174. //reply to pingcheck
  175. StartPingCheckPacket startPing = (StartPingCheckPacket)NewPack;
  176. CompletePingCheckPacket endPing = new CompletePingCheckPacket();
  177. endPing.PingID.PingID = startPing.PingID.PingID;
  178. OutPacket(endPing);
  179. }
  180. else
  181. {
  182. QueItem item = new QueItem();
  183. item.Packet = NewPack;
  184. item.Incoming = true;
  185. this.PacketQueue.Enqueue(item);
  186. }
  187. }
  188. public virtual void OutPacket(Packet NewPack)
  189. {
  190. QueItem item = new QueItem();
  191. item.Packet = NewPack;
  192. item.Incoming = false;
  193. this.PacketQueue.Enqueue(item);
  194. }
  195. # region Low Level Packet Methods
  196. protected void ack_pack(Packet Pack)
  197. {
  198. if (Pack.Header.Reliable)
  199. {
  200. PacketAckPacket ack_it = new PacketAckPacket();
  201. ack_it.Packets = new PacketAckPacket.PacketsBlock[1];
  202. ack_it.Packets[0] = new PacketAckPacket.PacketsBlock();
  203. ack_it.Packets[0].ID = Pack.Header.Sequence;
  204. ack_it.Header.Reliable = false;
  205. OutPacket(ack_it);
  206. }
  207. /*
  208. if (Pack.Header.Reliable)
  209. {
  210. lock (PendingAcks)
  211. {
  212. uint sequence = (uint)Pack.Header.Sequence;
  213. if (!PendingAcks.ContainsKey(sequence)) { PendingAcks[sequence] = sequence; }
  214. }
  215. }*/
  216. }
  217. protected void ResendUnacked()
  218. {
  219. int now = Environment.TickCount;
  220. lock (NeedAck)
  221. {
  222. foreach (Packet packet in NeedAck.Values)
  223. {
  224. if ((now - packet.TickCount > RESEND_TIMEOUT) && (!packet.Header.Resent))
  225. {
  226. MainLog.Instance.Verbose( "Resending " + packet.Type.ToString() + " packet, " +
  227. (now - packet.TickCount) + "ms have passed");
  228. packet.Header.Resent = true;
  229. OutPacket(packet);
  230. }
  231. }
  232. }
  233. }
  234. protected void SendAcks()
  235. {
  236. lock (PendingAcks)
  237. {
  238. if (PendingAcks.Count > 0)
  239. {
  240. if (PendingAcks.Count > 250)
  241. {
  242. // FIXME: Handle the odd case where we have too many pending ACKs queued up
  243. MainLog.Instance.Verbose( "Too many ACKs queued up!");
  244. return;
  245. }
  246. //OpenSim.Framework.Console.MainLog.Instance.WriteLine("Sending PacketAck");
  247. int i = 0;
  248. PacketAckPacket acks = new PacketAckPacket();
  249. acks.Packets = new PacketAckPacket.PacketsBlock[PendingAcks.Count];
  250. foreach (uint ack in PendingAcks.Values)
  251. {
  252. acks.Packets[i] = new PacketAckPacket.PacketsBlock();
  253. acks.Packets[i].ID = ack;
  254. i++;
  255. }
  256. acks.Header.Reliable = false;
  257. OutPacket(acks);
  258. PendingAcks.Clear();
  259. }
  260. }
  261. }
  262. protected void AckTimer_Elapsed(object sender, ElapsedEventArgs ea)
  263. {
  264. SendAcks();
  265. ResendUnacked();
  266. }
  267. #endregion
  268. protected virtual void KillThread()
  269. {
  270. }
  271. #region Nested Classes
  272. public class QueItem
  273. {
  274. public QueItem()
  275. {
  276. }
  277. public Packet Packet;
  278. public bool Incoming;
  279. }
  280. #endregion
  281. }
  282. }