public void OnReceived(Connection connection, byte[] buffer, int offset, int count)
        {
            UInt16 MSG_TYPE  = BigEndianUtil.ToUInt16(buffer, offset);
            Int32  msgLength = BigEndianUtil.ToInt32(buffer, offset + 2);

            if (MSG_TYPE == NHNet.MSG_TYPE_DATA)
            {
                connection.OnReceived(connection, buffer, offset + 6, count - 6);
            }
            else if (MSG_TYPE == NHNet.MSG_TYPE_DATA_COPMRESS)
            {
                //never use
                byte[] uncompressData      = new byte[NHNet._MAX_UNCOMPRESS_MESSAGE_SIZE];
                int    uncompressDataCount = -1;
                try
                {
                    //uncompressDataCount = null;//TODO uncompress
                }
                catch (OverflowException ex)
                {
                    LogU.Debug("uncompress exception {0}", ex.Message);
                }
                connection.OnReceived(connection, uncompressData, 0, uncompressDataCount);
            }
        }
Esempio n. 2
0
        public bool Serialize <T>(ref byte[] buffer, ref int allLength, T instance, int msgId, int playerId)
        {
            MemoryStream stream = new MemoryStream();

            ProtoBuf.Serializer.Serialize <T>(stream, instance);

            BaseMsg baseMsg = new BaseMsg();

            baseMsg.id       = msgId;
            baseMsg.playerId = playerId;
            baseMsg.msgData  = stream.ToArray();


            MemoryStream baseStream = new MemoryStream(buffer);

            baseStream.Position = 6;//head
            ProtoBuf.Serializer.Serialize(baseStream, baseMsg);
            allLength = (int)baseStream.Position;
            int bodyLength = allLength - 6;

            if (bodyLength <= 0)
            {
                return(false);
            }

            byte[] header = new byte[6];
            byte[] tmp    = BigEndianUtil.ToBytes(NHNet.MSG_TYPE_DATA);
            Array.Copy(tmp, 0, header, 0, 2);
            byte[] tmp1 = BigEndianUtil.ToBytes(bodyLength);
            Array.Copy(tmp1, 0, header, 2, 4);


            baseStream.Position = 0;
            BinaryWriter writer = new BinaryWriter(baseStream);

            writer.Write(header, 0, header.Length);

            return(true);
        }