123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Slice;
- using System.Threading;
- using CommonNetwork_ICE.Util;
- using CommonNetwork_ICE.Common;
- using System.Collections.Concurrent;
- using System.Collections;
- using CommonLang.Log;
- namespace CommonServer_ICE.Msg
- {
-
-
-
- internal class SeverSendMsgQueue
- {
- private static Logger log = LoggerFactory.GetLogger("SeverSendMsgQueue");
- private Thread thread;
-
- private bool runFlag;
-
- private Hashtable map;
- public SeverSendMsgQueue(Hashtable map)
- {
- this.map = map;
- }
- public void Start()
- {
- thread = new Thread(Run);
- runFlag = true;
- thread.Start();
- }
- public void End()
- {
- runFlag = false;
- if (thread != null)
- {
- thread.Join();
- thread.Abort();
- }
- }
-
-
-
-
- public void AddPacket(MsgSessionPacket packet)
- {
- lock (packet.ID)
- {
- LinkedList<MsgSessionPacket> packetList = (LinkedList<MsgSessionPacket>)map[packet.ID];
- if (packetList == null)
- {
- packetList = new LinkedList<MsgSessionPacket>();
- packetList.AddFirst(packet);
- map.Add(packet.ID, packetList);
- return;
- }
- packetList.AddLast(packet);
- }
- }
-
-
-
- private void Run()
- {
- while (runFlag)
- {
- try
- {
- if (map.Count == 0)
- {
-
- Thread.Sleep(Env.SEND_WAIT_ACT_THREAD_SLEEP_TIME);
- continue;
- }
- LinkedList<MsgSessionPacket>[] al = new LinkedList<MsgSessionPacket>[map.Count];
- map.Values.CopyTo(al, 0);
-
- foreach (LinkedList<MsgSessionPacket> packetList in al)
- {
- if (packetList.Count == 0)
- {
- continue;
- }
- MsgSessionPacket packet = packetList.First.Value;
- lock (packet.ID)
- {
- if (packet.Session == null || !packet.Session.IsConnected)
- {
-
- map.Remove(packet.ID);
- continue;
- }
- if (packet.Session.SendedRecvLastSerial == packet.Session.SendedLastSerial)
- {
-
- if (packet.Message.serial == packet.Session.SendedRecvLastSerial)
- {
- packetList.RemoveFirst();
- }
-
- if (packetList.Count > 0)
- {
-
- packet = packetList.First.Value;
- bool successed = packet.Session.SendTo(packet.Message);
- if (successed)
- {
- packet.Session.LastHingeSendTime = DateTime.Now;
- packet.Session.SendedLastSerial = packet.Message.serial;
- packet.Session.NotifySentMsg(packet.SrcMessage);
- }
- }
- }
- else
- {
-
- if (packet.Session.LastHingeSendTime.AddSeconds(Env.SEND_WAIT_ACK_TIME_OUT).CompareTo(DateTime.Now) < 0)
- {
- log.Trace("没收到关键包应答,服务器重发数据:" + packet.Message.serial);
- bool successed = packet.Session.SendTo(packet.Message);
- if (successed)
- {
- packet.Session.LastHingeSendTime = DateTime.Now;
- }
- }
- }
- }
- }
- }
- catch (Exception err)
- {
- log.Error(err.Message, err);
- }
- }
- }
- }
- }
|