Exemple #1
0
        private void AsyncSendHandler(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                try
                {
                    int len = _client.EndSend(ar);
                    LoggerFactory.Debug("send:{0} bytes", len);
                }
                catch (Exception e)
                {
                    OnError(Errors.NetworkError, "发送包失败。", e);
                }

                SendState state = (SendState)ar.AsyncState;
                if (state.LastPacket)
                {
                    //最后一个包发完开始检测结果
                    UdpPacket packet = state.Packet;
                    if (packet.CheckSendResponse)
                    {
                        // 添加到正在发送包一览,随后启动定时器检测有无返回结果
                        LoggerFactory.Debug("add to check response:id={0}", packet.ID);

                        if (!_sendingPacketDic.TryAdd(packet.ID, packet))
                        {
                            //应该不会到这里来
                            OnError(Errors.Unknow, "未想定异常:AsyncSendHandler", null);
                        }

                        WaitTimer.Start(this.SendResponseCheckDelay, SendResultCheckHandler, packet);
                    }
                    else
                    {
                        //对于不需要确认的直接认为发送完毕
                        OnSendPackage(packet, true);
                    }
                }
            }
        }