示例#1
0
 protected static void ReadFloat(ref byte[] byteArr, ref int byteOffset, out float outputValue)
 {
     outputValue = ConvertHelper.GetSingle(byteArr, byteOffset);
     byteOffset += ConvertHelper.FLOAT_LENGTH;
 }
示例#2
0
 protected static void ReadUInt64(ref byte[] byteArr, ref int byteOffset, out ulong outputValue)
 {
     outputValue = ConvertHelper.GetUInt64(byteArr, byteOffset);
     byteOffset += ConvertHelper.ULONG_LENGTH;
 }
示例#3
0
 protected static void ReadUInt32(ref byte[] byteArr, ref int byteOffset, out uint outputValue)
 {
     outputValue = ConvertHelper.GetUInt32(byteArr, byteOffset);
     byteOffset += ConvertHelper.UINT_LENGTH;
 }
示例#4
0
 protected static void ReadBoolean(ref byte[] byteArr, ref int byteOffset, out bool outputValue)
 {
     outputValue = ConvertHelper.GetBoolean(byteArr, byteOffset);
     byteOffset += ConvertHelper.BOOL_LENGTH;
 }
示例#5
0
 protected static void ReadShort(ref byte[] byteArr, ref int byteOffset, out short outputValue)
 {
     outputValue = ConvertHelper.GetInt16(byteArr, byteOffset);
     byteOffset += ConvertHelper.SHORT_LENGTH;
 }
示例#6
0
        private bool Process()
        {
            if (_receiver.ReceivedLength != _receiver.Length)
            {
                throw new Exception(string.Format("Receive length '{0}' is not equal to length '{1}'.", _receiver.ReceivedLength.ToString(), _receiver.Length.ToString()));
            }
            if (_receiver.Length < _defaultPacketLength)
            {
                throw new Exception(string.Format("Length '{0}' is smaller than length header.", _receiver.Length.ToString()));
            }
            if (_receiver.Length == _defaultPacketLength)
            {
                int packetLength = ConvertHelper.GetInt32(_receiver.GetBuffer());
                if (packetLength <= 0)
                {
                    string errorMessage = string.Format("Packet length '{0}' is invalid.", packetLength.ToString());
                    if (ErrorHandler != null)
                    {
                        ErrorHandler(this, SessionErrorCode.HeaderError, errorMessage);
                        return(false);
                    }
                    throw new Exception(errorMessage);
                }
                _receiver.Length += packetLength;
                if (_receiver.Length > _receiver.BufferSize)
                {
                    string errorMessage = string.Format("Length '{0}' is larger than buffer size '{1}'.", _receiver.Length.ToString(), _receiver.BufferSize.ToString());
                    if (ErrorHandler != null)
                    {
                        ErrorHandler(this, SessionErrorCode.OutOfRangeError, errorMessage);//未接收完成,继续;
                        return(false);
                    }
                    throw new Exception(errorMessage);
                }
                return(true);
            }
            Packet packet = null;

            try
            {
                int    packetLength    = _receiver.Length - _defaultPacketLength;
                object customErrorData = null;
                using (MemoryStream memoryStream = new MemoryStream(_receiver.GetBuffer(), _defaultPacketLength, packetLength, false))
                {
                    lock (thisLock)
                    {
                        packet = SessionUtil.Deserialize(this, memoryStream, out customErrorData);
                    }
                }
                _receiver.Reset(_defaultPacketLength);
                if (packet == null)
                {
                    if (CustomErrorHandler != null)
                    {
                        CustomErrorHandler(this, customErrorData);
                    }
                }
                else
                {
                    var  id         = packet.GetPacketId();
                    bool overridecs = false;
                    if (LuaProtoRegister.LuaProtoDict.TryGetValue(id, out overridecs))
                    {
                        if (!overridecs)
                        {
                            if (ReceiveHandler != null)
                            {
                                ReceiveHandler(this, packet);
                            }
                        }
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            packet.Serialize(memoryStream);
                            LuaNetHelper.Send2Lua(id, memoryStream.ToArray());
                        }
                    }
                    else
                    {
                        if (ReceiveHandler != null)
                        {
                            ReceiveHandler(this, packet);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Active = false;
                if (ErrorHandler != null)
                {
                    ErrorHandler(this, SessionErrorCode.DeserializeError, exception.ToString());
                    return(false);
                }
                throw;
            }
            return(true);
        }