Ejemplo n.º 1
0
 /// <summary>
 /// 关闭设备 Socket,并恢复该对象为可再次使用状态
 /// </summary>
 /// <param name="args"></param>
 private void SocketAsyncFinish(SocketAsyncEventArgs args)
 {
     try
     {
         SocketClient _SocketClient = args.UserToken as SocketClient;
         _SocketClient.ChangeState(SocketClientStateOptions.Free);
     }
     catch (Exception ex)
     {
         this.DoException(args, ex);
     }
 }
Ejemplo n.º 2
0
 //处理登录队列
 private void LogonTaskTake()
 {
     while (true)
     {
         SocketAsyncEventArgs args = this.m_LogonTask.Take();
         try
         {
             SocketClient _SocketClient = args.UserToken as SocketClient;
             _SocketClient.ChangeState(SocketClientStateOptions.Online);
         }
         catch (Exception ex)
         {
             this.DoException(args, ex);
         }
     }
 }
Ejemplo n.º 3
0
        //有新的设备 Socket 连接,将该对象设置为已连接状态
        private void AcceptCallback(SocketAsyncEventArgs args)
        {
            try
            {
                if (args.SocketError == SocketError.Success)
                {
                    byte[] bytBuffer = new byte[this.BufferSize];
                    args.SetBuffer(bytBuffer, 0, bytBuffer.Length);

                    SocketClient _SocketClient = args.UserToken as SocketClient;
                    _SocketClient.ChangeState(SocketClientStateOptions.Connected);

                    try
                    {
                        if (args.AcceptSocket.ReceiveAsync(args) == false)
                        {
                            this.ReceiveCallback(args);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.DoException(args, ex);

                        this.SocketAsyncFinish(args);
                    }
                }
                else
                {
                    this.DoException(args, new Exception("在 BeginAccept 方法中 SocketAsyncEventArgs 的 SocketError 属性为 " + args.SocketError.ToString() + "。"));

                    this.SocketAsyncFinish(args);
                }
            }
            catch (Exception ex)
            {
                this.DoException(args, ex);
            }
            finally
            {
                this.Accept();
            }
        }
Ejemplo n.º 4
0
            /// <summary>
            /// 改变状态。
            /// </summary>
            /// <param name="state"></param>
            public void ChangeState(SocketClientStateOptions state)
            {
                lock (this)
                {
                    PrintState(state, false);

                    if (state != this.State)
                    {
                        switch (this.State)
                        {
                        case SocketClientStateOptions.Free:
                            if (state == SocketClientStateOptions.Connected)
                            {
                                if (this.m_Server.m_Connecteds.Contains(this.m_Args) == false)
                                {
                                    this.m_Server.m_Connecteds.Add(this.m_Args);
                                    this.State = state;
                                }
                                else
                                {
                                    this.m_Server.DoException(this.m_Args, new Exception("状态从 " + this.State + " 变更为 " + state + " 时,连接队列中已存在当前异步套接字操作"));
                                }
                            }
                            else
                            {
                                if (this.CloseCount != 0)
                                {
                                    PrintState(state, true);
                                }
                            }
                            break;

                        case SocketClientStateOptions.Connected:
                            SocketAsyncEventArgs argsOut;
                            if (this.m_Server.m_Connecteds.TryTake(out argsOut) == false)
                            {
                                this.m_Server.DoException(this.m_Args, new Exception("从异步套接字连接集合中移除连接时发生错误:未发现该套接字的连接信息。"));
                            }

                            if (state == SocketClientStateOptions.Online)
                            {
                                while (this.m_Server.m_Onlines.TryAdd(this.Address, this.m_Args) == false)
                                {
                                    //如果加入登录后的集合中失败,则表示已有相同地址的连接已经登录
                                    if (this.m_Server.m_Onlines.TryGetValue(this.Address, out argsOut))
                                    {
                                        SocketClient _SocketClientOut = argsOut.UserToken as SocketClient;
                                        _SocketClientOut.ChangeState(SocketClientStateOptions.Free);
                                    }
                                }
                                this.State = state;
                            }
                            else if (state == SocketClientStateOptions.Free)
                            {
                                this.Close();
                            }
                            break;

                        case SocketClientStateOptions.Online:
                            if (state == SocketClientStateOptions.Free)
                            {
                                SocketAsyncEventArgs argsDis;
                                if (this.m_Server.m_Onlines.TryRemove(this.Address, out argsDis) == false)
                                {
                                    this.m_Server.DoException(this.m_Args, new Exception("从异步套接字在线集合中移除连接时发生错误:未发现该套接字的在线信息。(该异常通常为强制关闭连接后出现,不影响后续操作)"));
                                }

                                this.Close();
                            }
                            else
                            {
                                PrintState(state, true);
                            }
                            break;
                        }
                    }
                    else
                    {
                        PrintState(state, true);
                    }
                }
            }
Ejemplo n.º 5
0
 protected void ChangeState(SocketClient.SocketState state)
 {
     m_SocketClient.ChangeState(state);
 }