Exemplo n.º 1
0
        public void EndRead(IAsyncResult ar)
        {
            SocketHandlerState state = (SocketHandlerState)ar.AsyncState;

            if (state.Data[0].Equals(0))
            {
                lock (StateSet)
                    StateSet.Add(state.AsyncResult, state);
                //设定接收到的数据位空byte数组
                state.Data = new byte[0];
                //允许等待线程继续
                ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set();
                //执行异步回调函数
                state.AsyncCallBack(state.AsyncResult);
                return;
            }
            state.Completed = true;
            lock (StateSet)
                StateSet.Add(state.AsyncResult, state);
            ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set();
            state.AsyncCallBack(state.AsyncResult);
        }
Exemplo n.º 2
0
        //stream异步结束写入
        private void EndWrite(IAsyncResult ar)
        {
            SocketHandlerState state = (SocketHandlerState)ar.AsyncState;

            //锁定StateSet
            lock (StateSet)
                StateSet.Add(state.AsyncResult, state);

            try
            {
                state.Stream.EndWrite(ar);
            }
            catch
            {
                //出现Socket异常,发送失败
                state.Completed = false;
                //允许等待线程继续
                ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set();
                //执行异步回调函数
                state.AsyncCallBack(state.AsyncResult);
                return;
            }
            //发送成功
            state.Completed = true;
            //允许等待线程继续
            ((AutoResetEvent)state.AsyncResult.AsyncWaitHandle).Set();
            //执行异步回调函数
            state.AsyncCallBack(state.AsyncResult);

            //锁定SendQueue
            lock (SendQueue)
            {
                SocketHandlerState prepare = null;
                //移除当前发送完成的数据
                SendQueue.Remove(state);
                //如果SendQueue还有数据存在,则继续发送
                if (SendQueue.Count > 0)
                {
                    prepare = SendQueue[0];
                }
                if (prepare != null)
                {
                    //获取数据长度
                    //ushort的最大值为65535
                    //转换为byte[]长度为2
                    //var dataLength = BitConverter.GetBytes((ushort)prepare.Data.Length);
                    //向对方发送长度为2的头信息,表示接下来要发送的数据长度
                    try
                    {
                        //prepare.Stream.Write(dataLength, 0, dataLength.Length);
                        //开始异步发送数据
                        prepare.Stream.BeginWrite(prepare.Data, 0, prepare.Data.Length, EndWrite, prepare).AsyncWaitHandle.WaitOne();
                    }
                    catch
                    {
                        prepare.Completed = false;
                        ((AutoResetEvent)prepare.AsyncResult.AsyncWaitHandle).Set();
                        prepare.AsyncCallBack(prepare.AsyncResult);
                    }
                }
            }
        }