示例#1
0
        /// <summary>
        /// 关闭一个与客户端之间的会话
        /// </summary>
        /// <param name="state">需要关闭的客户端会话对象</param>
        public void Close(AsyncSocketState state)
        {
            if (state != null)
            {
                state.Datagram       = null;
                state.RecvDataBuffer = null;

                _clients.Remove(state);
                _clientCount--;
                //TODO 触发关闭事件
                state.Close();
            }
        }
示例#2
0
 /// <summary>
 /// 处理客户端数据
 /// </summary>
 /// <param name="ar"></param>
 private void HandleDataReceived(IAsyncResult ar)
 {
     if (IsRunning)
     {
         AsyncSocketState state  = (AsyncSocketState)ar.AsyncState;
         Socket           client = state.ClientSocket;
         try
         {
             //如果两次开始了异步的接收,所以当客户端退出的时候
             //会两次执行EndReceive
             int recv = client.EndReceive(ar);
             if (recv == 0)
             {
                 //C- TODO 触发事件 (关闭客户端)
                 Close(state);
                 RaiseNetError(state);
                 return;
             }
             //TODO 处理已经读取的数据 ps:数据在state的RecvDataBuffer中
             if (recv > 0)
             {
                 var _bReceiveData = new byte[recv];               //存放已经接收返回的数据
                 Array.Copy(state.RecvDataBuffer, 0, _bReceiveData, 0, _bReceiveData.Length);
                 //C- TODO 触发数据接收事件
                 RaiseDataReceived(state, _bReceiveData);
             }
         }
         catch (SocketException)
         {
             //C- TODO 异常处理
             RaiseNetError(state);
         }
         finally
         {
             if (state.RecvDataBuffer != null)
             {
                 //继续接收来自来客户端的数据
                 client.BeginReceive(state.RecvDataBuffer, 0, state.RecvDataBuffer.Length, SocketFlags.None,
                                     new AsyncCallback(HandleDataReceived), state);
             }
         }
     }
 }
示例#3
0
 private void RaiseOtherException(AsyncSocketState state)
 {
     RaiseOtherException(state, "");
 }
示例#4
0
 /// <summary>
 /// 触发异常事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseOtherException(AsyncSocketState state, string descrip)
 {
     OtherException?.Invoke(this, new AsyncSocketEventArgs(descrip, state));
 }
示例#5
0
 /// <summary>
 /// 触发网络错误事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseNetError(AsyncSocketState state)
 {
     NetError?.Invoke(this, new AsyncSocketEventArgs(state));
 }
示例#6
0
 /// <summary>
 /// 触发数据发送完毕的事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseCompletedSend(int num, AsyncSocketState state)
 {
     CompletedSend?.Invoke(this, new AsyncSocketEventArgs(num.ToString(), state));
 }
示例#7
0
 /// <summary>
 /// 触发发送数据前的事件
 /// </summary>
 /// <param name="state"></param>
 private void RaisePrepareSend(AsyncSocketState state)
 {
     PrepareSend?.Invoke(this, new AsyncSocketEventArgs(state));
 }
示例#8
0
 private void RaiseDataReceived(AsyncSocketState state, byte[] _bReceiveData)
 {
     DataReceived?.Invoke(_bReceiveData, new AsyncSocketEventArgs(state));
 }
示例#9
0
 /// <summary>
 /// 触发客户端连接事件
 /// </summary>
 /// <param name="state"></param>
 private void RaiseClientConnected(AsyncSocketState state)
 {
     ClientConnected?.Invoke(this, new AsyncSocketEventArgs(state));
 }
示例#10
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="state">接收数据的客户端会话</param>
 /// <param name="data">数据报文</param>
 public void Send(AsyncSocketState state, byte[] data)
 {
     RaisePrepareSend(state);
     Send(state.ClientSocket, data);
 }
示例#11
0
 public AsyncSocketEventArgs(string msg, AsyncSocketState state)
 {
     this._msg   = msg;
     this._state = state;
     IsHandled   = false;
 }
示例#12
0
 public AsyncSocketEventArgs(AsyncSocketState state)
 {
     this._state = state;
     IsHandled   = false;
 }