Beispiel #1
0
 /// <summary>
 /// 发送数据块
 /// </summary>
 /// <param name="dataBlock"></param>
 public virtual void Send(DataBlock dataBlock)
 {
     SafeSend(dataBlock);
 }
Beispiel #2
0
 /// <summary>
 /// 收到数据
 /// </summary>
 /// <param name="dataBlock">数据块</param>
 protected virtual void OnReceivedData(DataBlock dataBlock)
 {
 }
Beispiel #3
0
 protected virtual void OnSendEnd(DataBlock target)
 {
 }
Beispiel #4
0
 /// <summary>
 /// 发送数据块
 /// </summary>
 /// <param name="dataBlock"></param>
 public override void Send(DataBlock dataBlock)
 {
     Send(new MessageBlock(dataBlock));
 }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataBlockArgs"/> class.
 /// </summary>
 /// <param name="dataBlock">The data block.</param>
 public DataBlockArgs(DataBlock dataBlock)
 {
     this.dataBlock = dataBlock;
 }
Beispiel #6
0
 /// <summary>
 /// 接收数据,逻辑处理需要重载该函数
 /// </summary>
 /// <param name="session">会话</param>
 /// <param name="dataBlock">数据</param>
 protected virtual void OnReceivedData(TSession session, DataBlock dataBlock)
 {
 }
Beispiel #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageBlock"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="body">The body.</param>
        public MessageBlock(MessageBlockType type, int parameter, DataBlock body)
            : this()
        {
            Type = type;
            Parameter = parameter;

            if (body != null)
                Body = body;

            CreateHead();
        }
Beispiel #8
0
        /// <summary>
        /// �ϲ��������ݿ�
        /// </summary>
        /// <param name="lhs">��߲�������</param>
        /// <param name="rhs">�ұ߲�������</param>
        /// <returns></returns>
        public static DataBlock operator +(DataBlock lhs, DataBlock rhs)
        {
            if (lhs == null && rhs == null)
                return null;
            else if (lhs == null)
            {
                DataBlock result = new DataBlock(rhs.DataLength);
                result.Add(rhs.buffer, rhs.ReadIndex, rhs.DataLength);
                return result;
            }
            else if (rhs == null)
            {
                DataBlock result = new DataBlock(lhs.DataLength);
                result.Add(lhs.buffer, lhs.ReadIndex, lhs.DataLength);
                return result;
            }
            else
            {
                DataBlock result = new DataBlock(lhs.DataLength + rhs.DataLength);

                result.Add(lhs.buffer, lhs.ReadIndex, lhs.DataLength);
                result.Add(rhs.buffer, rhs.ReadIndex, rhs.DataLength);

                return result;
            }
        }
        /// <summary>
        /// ���յ�readCount�����ݡ�
        /// </summary>
        /// <param name="readCount">���ݶ�ȡ�ĸ���</param>
        protected internal override void ReceivedData(int readCount)
        {
            //�̰߳�ȫ���ѣ������ڶ��߳��е���
            this.Buffer.WriteIndex += readCount;
            //����ϴ�Ϊʹ�õ����ݣ����½���
            cmdData += Buffer;
            //����������
            Buffer.Reset();

            //ʹ�ù涨���ַ��������ַ�
            string rawCmd = Encoding.GetString(cmdData.Buffer, cmdData.ReadIndex, cmdData.DataLength);

            //��ֹ����������ַ������������Ƶ�����
            if(rawCmd.Length > MaxCommandLength)
            {
                throw new NetException("����̫��");
            }

            int length;
            string[] cmdArray = StringEx.Split(rawCmd, NewLines,out length);

            foreach (string cmd in cmdArray)
            {
                //�̰߳�ȫ
                EventHandler<TEventArgs<string>> temp = ReceivedCommand;
                if (temp != null)
                {
                    temp(this, new TEventArgs<string>(cmd));
                }
            }

            //����Ѿ�ʹ�õ��ַ��������ݳ��ȣ�Ȼ������Ϊ�Ѿ���ȡ
            cmdData.ReadIndex += Encoding.GetByteCount(rawCmd.Substring(0, length));
        }
Beispiel #10
0
 /// <summary>
 /// 创建一个空MessageBlock,只包含头信息
 /// </summary>
 public MessageBlock()
 {
     Head = new DataBlock(HeadLength);
 }
Beispiel #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageBlock"/> class.
 /// </summary>
 /// <param name="body">The body.</param>
 public MessageBlock(DataBlock body)
     : this(MessageBlockType.AppData, 0, body)
 {
 }
Beispiel #12
0
 /// <summary>
 /// ����һ����MessageBlock,ֻ����ͷ��Ϣ
 /// </summary>
 public MessageBlock()
 {
     Head = new DataBlock(HeadLength);
 }
Beispiel #13
0
        /// <summary>
        /// �����ݵõ�һ����Ϣ
        /// </summary>
        /// <param name="data">������Ϣ������</param>
        /// <param name="getHeadData">�Ƿ�õ�ͷ��Ϣ</param>
        public void FromBytes(byte[] data, bool getHeadData, int maxBodyLength)
        {
            int index = 0;
            int length = (int)MyConvert.ToUInt32(data, index);
            bodyLength = length - HeadLength;
            if (bodyLength > maxBodyLength)
            {
                throw new NetException(string.Format("message is too large {0}/{1}", bodyLength, maxBodyLength));
            }

            index += 4;
            Type = (MessageBlockType)MyConvert.ToUInt32(data, index);
            index += 4;
            Parameter = (int)MyConvert.ToUInt32(data, index);
            if (!getHeadData && length > HeadLength)
            {
                index += 4;
                body = new DataBlock(length - HeadLength);
                Array.Copy(data, index, body.Buffer, body.WriteIndex, body.WritableLength);
                body.WriteIndex += body.WritableLength;
            }
            else if (BodyLength >= 0)
            {
                body = new DataBlock(length - HeadLength);
            }
            else
            {
                throw new NetException("Bad message block head data.");
            }
        }
Beispiel #14
0
        /// <summary>
        /// ������Ϣ���ã����ɱ���ͷ��Ϣ
        /// </summary>
        public void CreateHead()
        {
            if (Head == null || Head.Buffer == null)
            {
                Head = new DataBlock(HeadLength);
            }

            byte[] result = this.head.Buffer;

            byte[] lengthBytes = MyConvert.ToBytes(Length);
            byte[] typeBytes = MyConvert.ToBytes((int)Type);
            byte[] parameterBytes = MyConvert.ToBytes(Parameter);

            int index = 0;
            //��ӳ�����Ϣ
            Array.Copy(lengthBytes, 0, result, index, 4);
            index += 4;
            //���������Ϣ
            Array.Copy(typeBytes, 0, result, index, 4);
            index += 4;
            //��Ӳ�����Ϣ
            Array.Copy(parameterBytes, 0, result, index, 4);

            //��չ��Ϣ��δʹ�ã���Ϊ0
            index += 8;

            this.Head.WriteIndex = HeadLength;
        }
Beispiel #15
0
 /// <summary>
 /// Session������ʱ�Զ������á��̳�������޸����Ķ��塣
 /// </summary>
 protected virtual void OnCreate()
 {
     Buffer = new DataBlock(4096);
 }
Beispiel #16
0
 /// <summary>
 /// Session被创建时自动被调用。继承类可以修改它的定义。
 /// </summary>
 protected virtual void OnCreate()
 {
     Buffer = new DataBlock(4096);
 }
Beispiel #17
0
 /// <summary>
 /// 把数据封装到信息中发送
 /// </summary>
 /// <param name="session"></param>
 /// <param name="data"></param>
 public override void Send(TSession session, DataBlock data)
 {
     Send(session, new MessageBlock(data));
 }
Beispiel #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataBlockArgs"/> class.
 /// </summary>
 /// <param name="dataBlock">The data block.</param>
 public DataBlockArgs(DataBlock dataBlock)
 {
     this.dataBlock = dataBlock;
 }
Beispiel #19
0
 public virtual void Send(TSession session, DataBlock data)
 {
     AtomSend(session, data.Buffer, data.ReadIndex, data.DataLength);
 }
Beispiel #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MessageBlock"/> class.
 /// </summary>
 /// <param name="body">The body.</param>
 public MessageBlock(DataBlock body)
     : this(MessageBlockType.AppData, 0, body)
 {
 }