Exemple #1
0
        public void Send(string msg)
        {
            //byte[] result = Encoding.UTF8.GetBytes(msg);
            //Encoding.Default.GetBytes(msg)
            //Buffer
            //Arrary
            //BitConvert
            //MemoryStream
            //StreamReader
            //StreamWriter
            //BinaryReader
            //BinaryWriter

            //数据信息
            NetModel model = new NetModel()
            {
                ID = 1, Commit = "LanOu", Message = msg
            };

            byte[] dataModel = NetModel.Serialize(model);

            //长度信息
            int len = dataModel.Length;

            byte[] dataLength = BitConverter.GetBytes(len);

            //合并数组
            byte[] result = NetEncode.Encode(dataModel);

            tc.GetStream().Write(result, 0, result.Length);
        }
Exemple #2
0
        private void ReadData()
        {
            byte[] data = NetEncode.Decode(ref receiveCache);
            if (null != data)
            {
                NetModel item = NetModel.Deserialize(data);
                print("cleint:" + item.Message);

                ReadData();
            }
            else
            {
                isReceiving = false;
            }
        }
Exemple #3
0
        private void ReadData()
        {
            byte[] data = NetEncode.Decode(ref receiveCache);
            if (null != data)
            {
                int    offset   = 0;
                uint   uSession = NetEnCoder.DecodeUInt(data, ref offset);
                uint   uCode    = NetEnCoder.DecodeUInt(data, ref offset);
                int    nCount   = data.Length - offset;
                object msg      = PBEnCoder.Decode(uCode, data, offset, nCount);

                if (0 != uCode)
                {
                    HandleMsg(uSession, uCode, msg);
                }

                ReadData();
            }
            else
            {
                isReceiving = false;
            }
        }