Example #1
0
        private void OnReadMsg(NetSession session)
        {
            if (session != _nowSession)
            {
                session.Free();
                return;
            }
            //判断session状态,只有保持连接状态才能进行消息读取,否则是连接断开或者发生错误
            if (!session.isConnected)
            {
                //连接断开,需要判断错误信息
                if (session.LastErr != null)
                {
                    //错误信息
                    error = session.LastErr.ToString();
                }

                if (onDisconnected != null)
                {
                    onDisconnected(this);
                }
                return;
            }

            //获得消息

            //反序列化
            session.msg.deserialization();

            _receiveQueue.Enqueue(session.msg);
        }
Example #2
0
        /// <summary>
        /// 连接到某个网络地址
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        public void Connect(String host, int port)
        {
            if (String.IsNullOrEmpty(host) || port < 1)
            {
                return;
            }
            mErr = null;
            if (_nowSession != null)
            {
                _nowSession.Free();
            }

            _nowSession             = new NetSession();
            _nowSession.onException = onError;
            _nowSession.Connect(host, port, OnConnected, OnReadMsg);
        }
Example #3
0
        private void OnConnected(NetSession session)
        {
            if (session != _nowSession)
            {
                session.Free();
                return;
            }

            if (session.isConnected)
            {
                //开始读取数据
                session.BeginReadMsg();
                if (onConnected != null)
                {
                    onConnected(this);
                }
            }
        }
Example #4
0
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void Close()
        {
            //关闭连接
            try
            {
                if (_nowSession != null)
                {
                    _nowSession.Free();
                }
                _nowSession = null;
            }
            finally
            {
            }

            if (onDisconnected != null)
            {
                onDisconnected(this);
            }
        }