예제 #1
0
        private void Initialize(AddressFamily addressFamily)
        {
            if (_socket != null)
            {
                Close();
                _socket = null;
            }
            switch (addressFamily)
            {
            case AddressFamily.InterNetwork:
                Type = SessionType.IPv4;
                break;

            case AddressFamily.InterNetworkV6:
                Type = SessionType.IPv6;
                break;

            default:
                throw new Exception($"Not supported address family '{addressFamily.ToString()}'.");
            }
            _socket   = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
            _receiver = new SessionReceiver(_defaultMaxPacketLength);
            _receiver.Reset(_defaultPacketLength);
        }
예제 #2
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)
                {
                    CustomErrorHandler?.Invoke(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
                    {
                        ReceiveHandler?.Invoke(this, packet);
                    }
                }
            }
            catch (Exception exception)
            {
                Active = false;
                if (ErrorHandler != null)
                {
                    ErrorHandler(this, SessionErrorCode.DeserializeError, exception.ToString());
                    return(false);
                }
                throw;
            }
            return(true);
        }
예제 #3
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 = ConverterUtility.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
                {
                    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);
        }