Esempio n. 1
0
        /// <summary>
        /// 检测当前的DTU是否在线
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        private bool IsClientOnline(AlienSession session)
        {
            bool result = false;

            alreadyLock.Enter( );

            for (int i = 0; i < alreadyOnline.Count; i++)
            {
                if (alreadyOnline[i].DTU == session.DTU)
                {
                    result = true;
                    break;
                }
            }


            if (!result)
            {
                alreadyOnline.Add(session);
            }

            alreadyLock.Leave( );

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 检测当前的dtu是否允许登录
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        private bool IsClientPermission(AlienSession session)
        {
            bool result = false;

            trustLock.Enter( );

            if (trustOnline.Count == 0)
            {
                result = true;
            }
            else
            {
                for (int i = 0; i < trustOnline.Count; i++)
                {
                    if (trustOnline[i] == session.DTU)
                    {
                        result = true;
                        break;
                    }
                }
            }

            trustLock.Leave( );

            return(result);
        }
        /// <summary>
        /// 使用指定的套接字创建异形客户端
        /// </summary>
        /// <param name="session">异形客户端对象,查看<seealso cref="NetworkAlienClient"/>类型创建的客户端</param>
        /// <returns>通常都为成功</returns>
        /// <example>
        ///   简单的创建示例。
        ///   <code lang="cs" source="YumpooDrive_Net45.Test\Documentation\Samples\Core\NetworkDoubleBase.cs" region="AlienConnect1" title="连接设备" />
        ///   如果想知道是否创建成功。通常都是成功。
        ///   <code lang="cs" source="YumpooDrive_Net45.Test\Documentation\Samples\Core\NetworkDoubleBase.cs" region="AlienConnect2" title="判断连接结果" />
        /// </example>
        /// <remarks>
        /// 不能和之前的长连接和短连接混用,详细参考 Demo程序
        /// </remarks>
        public OperateResult ConnectServer(AlienSession session)
        {
            isPersistentConn     = true;
            isUseSpecifiedSocket = true;


            if (session != null)
            {
                AlienSession?.Socket?.Close();

                if (string.IsNullOrEmpty(ConnectionId))
                {
                    ConnectionId = session.DTU;
                }

                if (ConnectionId == session.DTU)
                {
                    CoreSocket    = session.Socket;
                    IsSocketError = false;
                    AlienSession  = session;
                    return(InitializationOnConnect(session.Socket));
                }
                else
                {
                    IsSocketError = true;
                    return(new OperateResult());
                }
            }
            else
            {
                IsSocketError = true;
                return(new OperateResult());
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 退出异形客户端
        /// </summary>
        /// <param name="session">异形客户端的会话</param>
        public void AlienSessionLoginOut(AlienSession session)
        {
            alreadyLock.Enter( );

            alreadyOnline.Remove(session);

            alreadyLock.Leave( );
        }
Esempio n. 5
0
        /// <summary>
        /// 登录的回调方法
        /// </summary>
        /// <param name="obj">传入的异步对象</param>
        protected override void ThreadPoolLogin(object obj)
        {
            if (obj is Socket socket)
            {
                // 注册包
                // 0x48 0x73 0x6E 0x00 0x17 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x30 0x31 0x00 0x00 0x00 0x00 0x00 0x00 0xC0 0xA8 0x00 0x01 0x17 0x10
                // +------------+ +--+ +--+ +----------------------------------------------------+ +---------------------------+ +-----------------+ +-------+
                // + 固定消息头  +备用 长度           DTU码 12345678901 (唯一标识)                  登录密码(不受信的排除)     Ip:192.168.0.1    端口10000
                // +------------+ +--+ +--+ +----------------------------------------------------+ +---------------------------+ +-----------------+

                // 返回
                // 0x48 0x73 0x6E 0x00 0x01 0x00
                // +------------+ +--+ +--+ +--+
                //   固定消息头  备用 长度 结果代码

                // 结果代码
                // 0x00: 登录成功
                // 0x01: DTU重复登录
                // 0x02: DTU禁止登录
                // 0x03: 密码验证失败

                OperateResult <AlienMessage> check = ReceiveMessage(socket, 5000, new AlienMessage( ));
                if (!check.IsSuccess)
                {
                    return;
                }

                if (check.Content.HeadBytes[4] != 0x17 || check.Content.ContentBytes.Length != 0x17)
                {
                    socket?.Close( );
                    LogNet?.WriteWarn(ToString( ), "Length Check Failed");
                    return;
                }

                // 密码验证
                bool isPasswrodRight = true;
                for (int i = 0; i < password.Length; i++)
                {
                    if (check.Content.ContentBytes[11 + i] != password[i])
                    {
                        isPasswrodRight = false;
                        break;
                    }
                }

                string dtu = Encoding.ASCII.GetString(check.Content.ContentBytes, 0, 11).Trim( );

                // 密码失败的情况
                if (!isPasswrodRight)
                {
                    OperateResult send = Send(socket, GetResponse(StatusPasswodWrong));
                    if (send.IsSuccess)
                    {
                        socket?.Close( );
                    }
                    LogNet?.WriteWarn(ToString( ), "Login Password Wrong, Id:" + dtu);
                    return;
                }

                AlienSession session = new AlienSession( )
                {
                    DTU    = dtu,
                    Socket = socket,
                };

                // 检测是否禁止登录
                if (!IsClientPermission(session))
                {
                    OperateResult send = Send(socket, GetResponse(StatusLoginForbidden));
                    if (send.IsSuccess)
                    {
                        socket?.Close( );
                    }
                    LogNet?.WriteWarn(ToString( ), "Login Forbidden, Id:" + session.DTU);
                    return;
                }

                // 检测是否重复登录,不重复的话,也就是意味着登录成功了
                if (IsClientOnline(session))
                {
                    OperateResult send = Send(socket, GetResponse(StatusLoginRepeat));
                    if (send.IsSuccess)
                    {
                        socket?.Close( );
                    }
                    LogNet?.WriteWarn(ToString( ), "Login Repeat, Id:" + session.DTU);
                    return;
                }
                else
                {
                    OperateResult send = Send(socket, GetResponse(StatusOk));
                    if (!send.IsSuccess)
                    {
                        return;
                    }
                }

                // 触发上线消息
                OnClientConnected?.Invoke(this, session);
            }
        }