/// <summary>
        /// 异步发送并且等待接收数据完成
        /// </summary>
        /// <param name="smsSocket"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        protected byte[] SendAndWait(SmsSocket smsSocket, SmsMessage message)
        {
            if (smsSocket == null)
            {
                return(null);
            }

            SocketAsyncEventArgs sendArgs;

            try
            {
                smsSocket.WaitTraffic();
                lock (((ICollection)sendPool).SyncRoot)
                {
                    sendArgs = sendPool.Dequeue();
                }
            }
            catch (InvalidOperationException) // 队列为空
            {
                sendArgs            = new SocketAsyncEventArgs();
                sendArgs.Completed += AfterSend;
            }

            // 发送完加入等待回复的队列
            var token = new WaitingDataToken {
                SequenceId = message.GetSequenceId()
            };

            lock (msgLocker)
            {
                messageBuffer.Add(token.SequenceId, token);
            }

            sendArgs.AcceptSocket = smsSocket;
            sendArgs.UserToken    = token;
            var buffer = message.ToBytes();

            sendArgs.SetBuffer(buffer, 0, buffer.Length); // 填充要发送的数据

            Interlocked.Increment(ref trySending);

            if (!smsSocket.SendAsync(sendArgs)) // 未异步发送,应当在同步上下文中处理
            {
                AfterSend(null, sendArgs);
            }

            WaitHandle.WaitAll(new WaitHandle[] { token.WaitHandle }, 60 * 1000); // 等待异步请求结束

            lock (msgLocker)
            {
                messageBuffer.Remove(token.SequenceId);
            }

            if (token.Bytes == null)
            {
                token.SocketError = SocketError.TimedOut;
            }

            smsSocket.IsLogin = smsSocket.Connected;

            if (token.SocketError != SocketError.Success)
            {
                throw new SocketException((int)token.SocketError);
            }

            return(token.Bytes);
        }
Beispiel #2
0
        /// <summary>
        /// 异步发送并且等待接收数据完成  此方法存在如果一条消息超时了导致后面所有的消息都会超时
        /// </summary>
        /// <param name="smsSocket"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        protected byte[] SendAndWait(SmsSocket smsSocket, SmsMessage message)
        {
            if (smsSocket == null)
            {
                return(null);
            }
            //if (!socket.Connected)
            if (!SocketManager.IsSocketConnected(smsSocket))
            {
                _log.InfoFormat("Socket未连接-SendAndWait");
                //return null;
                //socket = SocketManager.Reconnect(socket);
                //messageList.Add(string.Format("{0}:Socket未连接-SendAndWait", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")));
            }
            if (!smsSocket.IsLogin)
            {
                _log.InfoFormat("Socket未登录-SendAndWait");
                //messageList.Add(string.Format("{0}:Socket未登录-SendAndWait", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")));
            }
            SocketAsyncEventArgs sendArgs;

            try
            {
                smsSocket.WaitTraffic();
                lock (((ICollection)sendPool).SyncRoot)
                {
                    sendArgs = sendPool.Dequeue();
                }
            }
            catch (InvalidOperationException) // 队列为空
            {
                sendArgs            = new SocketAsyncEventArgs();
                sendArgs.Completed += AfterSend;
            }

            // 发送完加入等待回复的队列
            var token = new WaitingDataToken {
                SequenceId = message.GetSequenceId()
            };

            lock (msgLocker)
            {
                messageBuffer.Add(token.SequenceId, token);
            }

            sendArgs.AcceptSocket = smsSocket;
            sendArgs.UserToken    = token;
            var buffer = message.ToBytes();

            sendArgs.SetBuffer(buffer, 0, buffer.Length); // 填充要发送的数据

            Interlocked.Increment(ref trySending);

            if (!smsSocket.SendAsync(sendArgs)) // 未异步发送,应当在同步上下文中处理
            {
                AfterSend(null, sendArgs);
            }
            //_log.InfoFormat("等待:{0}", token.SequenceId);
            WaitHandle.WaitAll(new WaitHandle[] { token.WaitHandle }, 60 * 1000); // 等待异步请求结束  65 * 1000
            //_log.InfoFormat("收到:{0}", token.SequenceId);
            lock (msgLocker)
            {
                messageBuffer.Remove(token.SequenceId);
            }

            if (token.Bytes == null)
            {
                token.SocketError = SocketError.TimedOut;
            }

            smsSocket.IsLogin = smsSocket.Connected;
            if (token.SocketError != SocketError.Success)
            {
                throw new SocketException((int)token.SocketError);
            }
            return(token.Bytes);
        }